-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfinalserver.c
449 lines (366 loc) · 11.9 KB
/
finalserver.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
#include <arpa/inet.h>
#include <limits.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <errno.h>
#include <signal.h>
#include <zconf.h>
#define ACK "ACK"
#define NACK "NACK"
int socket_server, socket_client;
typedef struct packet{
char data[1024];
}Packet;
typedef struct frame{
int frame_kind; //ACK:0, SEQ:1 FIN:2
int sq_no;
int ack;
Packet packet;
}Frame;
void error(char *msg)
{
perror(msg);
exit(0);
}
// CRC parameters (default values are for CRC-8):
const int order = 8;
const unsigned long polynom = 0x07;
const int direct = 1;
const unsigned long crcinit = 0x00;
const unsigned long crcxor = 0x00;
const int refin = 0;
const int refout = 0;
// 'order' [1..32] is the CRC polynom order, counted without the leading '1' bit
// 'polynom' is the CRC polynom without leading '1' bit
// 'direct' [0,1] specifies the kind of algorithm: 1=direct, no augmented zero bits
// 'crcinit' is the initial CRC value belonging to that algorithm
// 'crcxor' is the final XOR value
// 'refin' [0,1] specifies if a data byte is reflected before processing (UART) or not
// 'refout' [0,1] specifies if the CRC will be reflected before XOR
// internal global values:
unsigned long crcmask;
unsigned long crchighbit;
unsigned long crcinit_direct;
unsigned long crcinit_nondirect;
unsigned long crctab[256];
unsigned long reflect (unsigned long crc, int bitnum) {
// reflects the lower 'bitnum' bits of 'crc'
unsigned long i, j=1, crcout=0;
for (i=(unsigned long)1<<(bitnum-1); i; i>>=1) {
if (crc & i) crcout|=j;
j<<= 1;
}
return (crcout);
}
void generate_crc_table() {
// make CRC lookup table used by table algorithms
int i, j;
unsigned long bit, crc;
for (i=0; i<256; i++) {
crc=(unsigned long)i;
if (refin) crc=reflect(crc, 8);
crc<<= order-8;
for (j=0; j<8; j++) {
bit = crc & crchighbit;
crc<<= 1;
if (bit) crc^= polynom;
}
if (refin) crc = reflect(crc, order);
crc&= crcmask;
crctab[i]= crc;
}
}
unsigned long crctablefast (unsigned char* p, unsigned long len) {
// fast lookup table algorithm without augmented zero bytes, e.g. used in pkzip.
// only usable with polynom orders of 8, 16, 24 or 32.
unsigned long crc = crcinit_direct;
if (refin) crc = reflect(crc, order);
if (!refin) while (len--) crc = (crc << 8) ^ crctab[ ((crc >> (order-8)) & 0xff) ^ *p++];
else while (len--) crc = (crc >> 8) ^ crctab[ (crc & 0xff) ^ *p++];
if (refout^refin) crc = reflect(crc, order);
crc^= crcxor;
crc&= crcmask;
return(crc);
}
char *stringToBinary(char *s)
{
if (s == NULL) {
// NULL might be 0 but you cannot be sure about it
return NULL;
}
// get length of string without NUL
size_t slen = strlen(s);
// we cannot do that here, why?
// if(slen == 0){ return s;}
errno = 0;
// allocate "slen" (number of characters in string without NUL)
// times the number of bits in a "char" plus one byte for the NUL
// at the end of the return value
char *binary = malloc(slen * CHAR_BIT + 1);
if(binary == NULL){
fprintf(stderr,"malloc has -1ed in stringToBinary(%s): %s\n",s, strerror(errno));
return NULL;
}
// finally we can put our shortcut from above here
if (slen == 0) {
*binary = '\0';
return binary;
}
char *ptr;
// keep an eye on the beginning
char *start = binary;
int i;
// loop over the input-characters
for (ptr = s; *ptr != '\0'; ptr++) {
/* perform bitwise AND for every bit of the character */
// loop over the input-character bits
for (i = CHAR_BIT - 1; i >= 0; i--, binary++) {
*binary = (*ptr & 1 << i) ? '1' : '0';
}
}
// finalize return value
*binary = '\0';
// reset pointer to beginning
binary = start;
return binary;
}
/* Iterative Function to calculate (x^y) in O(logy) */
float exponent(float x, int y)
{
int res = 1; // Initialize result
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = res*x;
// n must be even now
y = y>>1; // y = y/2
x = x*x; // Change x to x^2
}
return res;
}
void random_flip(char *rem_message, float BER)
{
unsigned int i =0;
srand(time(0));
for (i=0;i<strlen(rem_message);i++){
if ((float)rand() / RAND_MAX < BER)
{
if (rem_message[i] == '0')
{
rem_message[i] = '1';
}
else if (rem_message[i] == '1')
{
rem_message[i] = '0';
}
else
{
fprintf(stderr, "Convert to binary was not successful\n");
}
}
}
}
char *construct_message(char *message,char *temp,int data_len)
{
unsigned int i;
unsigned int x=0;
// convert the data from bits to char
for (i = 0; i < data_len; i++)
{
int j;
int v = 0;
for (j = i; j < i + 8; j++)
{
if (message[j] == '1')
{
v += exponent(2, i + 7 - j);
}
}
temp[x++]=(char)v;
i = i + 7;
}
temp[x]='\0';
return temp;
}
void process(int socket_client, float drop_probability,float ber)
{
int data_len = 1;
char *temp_ack,*temp_nack,*final_ack,*final_nack;
do
{
char message[10000];
// receive from the socket
data_len = read(socket_client, message, 10000);
message[data_len] = '\0';
// printf("%s\n",message);
char *temp = (char *)malloc(10000*sizeof(char));
memset(temp,0x00,10000);
temp = construct_message(message,temp,data_len);
unsigned long msg = crctablefast((unsigned char *)temp, strlen(temp));
// printf("%lu\n",msg);
memset(temp,0x00,10000);
temp = construct_message(message,temp,data_len-8);
// if crc is check is true, we have the correct data and we send ack else we send nack
if (data_len && msg ==0)
{
printf("Message received by server: %s\n",temp);
printf("Sending ACK to sender....");
srand(time(0));
if (((float)rand() / RAND_MAX) < drop_probability)
{
printf("Packet has been dropped!\n");
continue;
}
temp_ack =(char *)malloc(64*sizeof(char));
memset(temp_ack,0x00,64);
temp_ack = stringToBinary(ACK);
random_flip(temp_ack,ber);
final_ack=(char *)malloc(64*sizeof(char));
memset(final_ack,0x00,64);
final_ack = construct_message(temp_ack,final_ack,strlen(temp_ack));
// send to the socket
int sent = send(socket_client, final_ack, strlen(final_ack), 0);
printf("Sent successfully!\n");
free(temp_ack);
free(final_ack);
}
else if (data_len)
{
printf("Message received by server: %s\n",temp);
printf("Message retrieved had some errors, sending NACK to sender....");
srand(time(0));
if (((float)rand() / RAND_MAX) < drop_probability)
{
printf("Packet dropped!\n");
continue;
}
temp_nack =(char *)malloc(64*sizeof(char));
memset(temp_nack,0x00,64);
temp_nack = stringToBinary(NACK);
random_flip(temp_nack,ber);
final_nack=(char *)malloc(64*sizeof(char));
memset(final_nack,0x00,64);
final_nack = construct_message(temp_nack,final_nack,strlen(temp_nack));
// send to the socket
int sent = send(socket_client, final_nack, strlen(final_nack), 0);
printf("Sent successfully!\n");
free(temp_nack);
free(final_nack);
}
else
{
close(socket_client);
}
free(temp);
} while (data_len); // continue till we get proper data from the socket
}
int setup_connection(int port)
{
// create a new socket witch AF_INET for internet domain, stream socket option, TCP(given by os) - reliable, connection oriented
if ((socket_server = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
error("Still creating socket ...");
}
printf("Created socket finally...\n");
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_port = htons(port); // convert a port number in host byte order to a port number in network byte order
server.sin_addr.s_addr = INADDR_ANY;
bzero(&server.sin_zero, 8); // clears the buffer
// bind function binds the socket to the address and port number specified in addr
if ((bind(socket_server, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0))
{
error("Still binding ...");
}
printf("Done with binding...\n");
// put the server socket in a passive mode, where it waits for the client to approach the server to make a connection
// 5 defines the maximum length to which the queue of pending connections for sockfd may grow
// if a connection request arrives when the queue is full, the client may receive an error
if ((listen(socket_server, 5)) < 0)
{
error("Still listening...");
}
printf("Listening...\n");
return 1;
}
void signal_callback(int inthand)
{
printf("Signal received %d. Releasing all resources...\n", inthand);
close(socket_client);
close(socket_server);
exit(inthand);
}
int main(int argc, char **argv)
{
if (argc != 2)
{
fprintf(stderr, "Usage should be the following : ./server port\n");
return EXIT_FAILURE;
}
// handle the interrup signal like ctrl + c
signal(SIGINT, signal_callback);
float drop_probability;
printf("Enter probability to drop packets: ");
scanf("%f", &drop_probability);
float BER;
printf("Enter BER (probability of bit errors): ");
scanf("%f", &BER);
setup_connection(atoi(argv[1]));
// at first, compute constant bit masks for whole CRC and CRC high bit
int i;
unsigned long bit, crc;
crcmask = ((((unsigned long)1<<(order-1))-1)<<1)|1;
crchighbit = (unsigned long)1<<(order-1);
generate_crc_table();
crcinit_direct = crcinit;
crc = crcinit;
for (i=0; i<order; i++) {
bit = crc & 1;
if (bit) crc^= polynom;
crc >>= 1;
if (bit) crc|= crchighbit;
}
crcinit_nondirect = crc;
while (true)
{
struct sockaddr_in client;
unsigned int len;
int process_id;
// extract the first connection request on the queue of pending connections for the listening socket
// creates a new connected socket, and returns a new file descriptor referring to that socket
// connection is established between client and server, and they are ready to transfer data
if ((socket_client = accept(socket_server, (struct sockaddr *)&client, &len)) < 0)
{
error("Accepting connection ...");
}
printf("Accepted...\n");
process_id = fork();
if (process_id < 0)
{
error ("Still forking ...");
}
if (process_id == 0)
{
// child process closes the old socket and works with the new one
close(socket_server);
process(socket_client, drop_probability,BER);
printf("Client served... Server can be terminated now!\n");
// after working with the new socket, it simply exits
return EXIT_SUCCESS;
}
else
{
// parent process does not need new socket, so it closes it
// and keeps listening on old socket
close(socket_client);
}
}
}