-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgbn.c
More file actions
733 lines (505 loc) · 23.1 KB
/
gbn.c
File metadata and controls
733 lines (505 loc) · 23.1 KB
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
#include "gbn.h"
state_t s;
uint16_t checksum(uint16_t *buf, int nwords)
{
uint32_t sum;
for (sum = 0; nwords > 0; nwords--)
sum += *buf++;
sum = (sum >> 16) + (sum & 0xffff);
sum += (sum >> 16);
return ~sum;
}
ssize_t gbn_send(int sockfd, const void *buf, size_t len, int flags){
/* Create socket */
struct sockaddr server;
socklen_t server_len = sizeof(server);
printf("In Send() buf len: %d, flags: %d\n", (int)len, flags);
printf("Data length: %d\n", (int)len);
int data_offset = 0; /* Input buffer location pointer */
int num_packets = ((len + DATALEN - 1) / DATALEN); /* Get number of packets to send */
gbnhdr *pkt_buffer[num_packets]; /* Create packet buffer for packets remaining to be sent */
int pkt_buf_counter = 0; /* and create Packet buffer counter */
int curr_len = len; /* Make a copy of len for remaining len to be added to buffer */
if (s.curr_state == ESTABLISHED) {
printf("Creating %d packet(s)\n\n", num_packets);
/* ----------Create data packets to be sent and add them to the packet buffer-----------------*/
/* While the remaining length is greater than DATALEN, create packets
* with data of length 1024 and add it them to the packet buffer */
while (curr_len > DATALEN) {
printf("Creating packet %d\n", pkt_buf_counter + 1);
gbnhdr *DATA_packet = malloc(sizeof(*DATA_packet));
/* Call create_DATA_packet to complete packet creation */
create_DATA_packet(DATA_packet, s.seq_num+data_offset, buf+data_offset, DATALEN, DATA);
printf("gbn_send DATA_packet checksum: %d\nDATA packet content: %s\n",
DATA_packet->checksum, DATA_packet->data);
pkt_buffer[pkt_buf_counter] = DATA_packet; /* Add DATA_packet to packet buffer array */
printf("Packet %d added to pkt_buffer\n", pkt_buf_counter + 1);
data_offset += DATALEN; /* Increase data offset pointer to next data location */
pkt_buf_counter += 1; /* Increase the packet buffer counter */
curr_len -= DATALEN; /* Decrement curr_len by added data length */
printf("Remaining length: %d\n", curr_len);
}
/* If length is less than DATALEN, create packet with corresponding data length */
printf("Creating packet %d\n", pkt_buf_counter + 1);
gbnhdr *DATA_packet = malloc(sizeof(*DATA_packet));
/* Create (last) DATA_packet using create_DATA_packet function */
create_DATA_packet(DATA_packet, s.seq_num+data_offset, buf+data_offset, curr_len, DATA);
printf("gbn_send DATA_packet checksum: %d\nDATA packet content: %s\n",
DATA_packet->checksum, DATA_packet->data);
pkt_buffer[pkt_buf_counter] = DATA_packet; /* Add DATA_packet to packet buffer array */
printf("Packet %d added to pkt_buffer\n\n\n", pkt_buf_counter + 1);
/* ------------------------------------------------------------------------------------------*/
/* ----------------------Send data packets from packet buffer--------------------------------*/
printf("Sending %d packets\n\n", num_packets);
gbnhdr *DATAACK_packet = create_rcv_pkt();
int attempts = 0;
int confirmed_pkts = 0;
while (confirmed_pkts < num_packets) {
s.seq_num = pkt_buffer[confirmed_pkts]->seqnum; /* Set current state seq_num to cur_pkt seqnum */
printf("Current seq_num: %d\n", s.seq_num);
int pkts_sent = 0;
printf("Window size %d...\nConfirmed Packets: %d\n", s.window_size, confirmed_pkts);
int i = 0;
while ((i < s.window_size) && ((confirmed_pkts + i) < num_packets)) {
printf("Sending packet num %d...\n", confirmed_pkts + i + 1);
if (sendto(sockfd, pkt_buffer[confirmed_pkts + i], sizeof(*pkt_buffer[confirmed_pkts + i]), \
flags, &s.address, s.sock_len) == -1) {
perror("Data packet send error");
return (-1);
}
printf("DATA_packet content: %s\nDATA_packet sent successfully...\n\n",
pkt_buffer[confirmed_pkts + i]->data);
pkts_sent += 1;
i++;
}
int x = 0;
while (x < pkts_sent) {
printf("\nAttempt number: %d\n", attempts + 1);
if (attempts > MAX_ATTEMPTS) {
printf("MAX attempts reached, exiting program...\n");
free(DATAACK_packet);
return (-1);
}
printf("Last packet actual length: %d\n", pkt_buffer[confirmed_pkts]->actual_len);
/*alarm(1);*/
int rcv_res;
rcv_res = recvfrom(sockfd, DATAACK_packet, sizeof(*DATAACK_packet), flags, &server,
&server_len);
if (rcv_res == -1) {
perror("Data ack packet recv error");
if (errno != EINTR) {
continue;
}
return (-1);
}
if (validate_packet(DATAACK_packet) == -1) {
attempts++;
continue;
}
if (DATAACK_packet->type == DATAACK) {
attempts = 0;
x++;
printf("Expected DATAACK_packet seq_num: %d\n DATAACK_packet with seq_num %d received...\n",
(s.seq_num + pkt_buffer[confirmed_pkts]->actual_len), DATAACK_packet->seqnum);
if (DATAACK_packet->seqnum == (s.seq_num + pkt_buffer[confirmed_pkts]->actual_len)) {
printf("Expected DATAACK_packet received...\n");
s.seq_num = pkt_buffer[confirmed_pkts]->seqnum + pkt_buffer[confirmed_pkts]->actual_len;
printf("Current seq_num: %d\n", s.seq_num);
confirmed_pkts++;
switch (s.window_size) {
case 1:
s.window_size = 2;
break;
case 2:
s.window_size = 4;
break;
case 4:
break;
default:
break;
}
printf("Window size changed to %d...\n", s.window_size);
} else {
s.window_size = 1;
printf("Window size changed to %d...\n", s.window_size);
printf("Incorrect DATAACK_packet received...\n");
}
} else {
s.window_size = 1;
printf("Window size changed to %d...\n", s.window_size);
printf("Wrong packet received...\n");
}
}
}
printf("Freeing memory...\n");
free(DATAACK_packet);
int z;
for (z = 0; z < pkt_buf_counter; z++) {
free(pkt_buffer[z]);
}
return (len);
}
return (-1);
}
ssize_t gbn_recv(int sockfd, void *buf, size_t len, int flags){
printf("In gbn_recv()\n");
/*------- create the data and data ack used to receive and respond to incoming packet -------*/
gbnhdr *DATA_packet = create_rcv_pkt();
gbnhdr *DATAACK_packet = malloc(sizeof(*DATAACK_packet));
memset(DATAACK_packet, 0, sizeof(*DATAACK_packet));
DATAACK_packet->type = DATAACK;
/* ------------------------------------------------------------------------------------------*/
/* When the sender is done transmitting data will attempt to close by sending FIN, respond with FINACK */
gbnhdr *FINACK_packet = malloc(sizeof(*FINACK_packet));
memset(FINACK_packet, 0, sizeof(*FINACK_packet));
FINACK_packet->type = FINACK;
/* Variable declaration to be populated from peer client */
struct sockaddr client;
socklen_t client_len = sizeof(client);
/* Actual number of bytes received from the sender */
ssize_t byte_length;
int attempts = 0;
while(s.curr_state == ESTABLISHED) {
printf("Connection is established. Waiting for DATA packet...\n");
/* if (attempts > MAX_ATTEMPTS) {
printf("\nMax attempt exceeded, exiting program...\n");
s.curr_state = CLOSED;
free(DATAACK_packet);
free(FINACK_packet);
return(-1);
}
*/
/*alarm(1);*/
if ((byte_length = maybe_recvfrom(sockfd,DATA_packet, sizeof(*DATA_packet), flags, &client, &client_len)) == -1) {
perror("Data packet recv error");
return(-1);
}
printf("Data packet size: %d\n Byte length upon receive: %d\n",
sizeof(*DATA_packet),
(unsigned int)byte_length);
if (DATA_packet->type == DATA && validate_packet(DATA_packet)) {
printf("SEQUENCE NUMBER %d\n", s.seq_num);
if (s.seq_num == DATA_packet->seqnum) {
attempts = 0;
/* Copy data received to buffer */
memcpy(buf, DATA_packet->data, DATA_packet->actual_len);
/* Create DATAACK packet to be sent to peer */
DATAACK_packet->seqnum = DATA_packet->seqnum + DATA_packet->actual_len;
DATAACK_packet->checksum = checksum((uint16_t *)DATAACK_packet, sizeof(*DATAACK_packet) / sizeof(uint16_t));
s.seq_num = DATAACK_packet->seqnum;
printf("Data packet content: %s\n Buffer content: %s\n", DATA_packet->data, (char *)buf);
printf("Data packet data length: %d\n", DATA_packet->actual_len);
printf("DATA_packet received.\n Sending DATAACK_packet with seqnum %d...\n", DATAACK_packet->seqnum);
if(sendto(sockfd, DATAACK_packet, sizeof(*DATAACK_packet), flags, &client, client_len) == -1) {
perror("DATAACK packet error");
return(-1);
}
return(DATA_packet->actual_len);
} else {
printf("Unexpected sequence number: %d\n Expected sequence number: %d\n",
DATA_packet->seqnum, s.seq_num);
DATAACK_packet->seqnum = s.seq_num;
DATAACK_packet->checksum = checksum((uint16_t *)DATAACK_packet, sizeof(*DATAACK_packet) / sizeof(uint16_t));
printf("Resending DATAACK_packet with seqnum: %d\n", DATAACK_packet->seqnum);
if(sendto(sockfd, DATAACK_packet, sizeof(*DATAACK_packet), flags, &client, client_len) == -1) {
perror("DATAACK packet error");
return(-1);
}
printf("DATAACK duplicate ACK sent\n");
}
}
if (DATA_packet->type == FIN) {
s.curr_state = FIN_RCVD;
printf("FIN received, responding with FINACK\n");
if (sendto(sockfd, FINACK_packet, sizeof(*FINACK_packet), flags, &client, client_len) == -1) {
perror("FINACK sendto error\n");
s.curr_state = CLOSED;
free(DATA_packet);
free(DATAACK_packet);
free(FINACK_packet);
return(-1);
}
printf("FINACK successfully sent, closing connection...\n");
/* TODO need to return the buffer now to write it to the file? */
/*
free(DATA_packet);
free(DATAACK_packet);
free(FINACK_packet);*/
return(0);
}
printf("Attempt number: %d\n", attempts);
/*attempts++*/;
}
printf("DATA_packet->data length: %d\n", (int)sizeof(DATA_packet->data));
/*
free(DATA_packet);
free(DATAACK_packet);
free(FINACK_packet);*/
return(-1);
}
int gbn_close(int sockfd){
/*--------- for sending FIN and receiving FINACK ---------*/
gbnhdr *FIN_packet = malloc(sizeof(*FIN_packet));
memset(FIN_packet->data, 0, sizeof(FIN_packet->data));
FIN_packet->type = FIN;
gbnhdr *FINACK_packet = create_rcv_pkt();
/*--------- END FIN/FINACK packet creation ---------*/
if (s.curr_state == FIN_RCVD) {
printf("Closing connection from receiver side...\n");
s.curr_state = CLOSED;
return(1);
}
int attempts = 0;
while (s.curr_state != CLOSED) {
if (attempts > MAX_ATTEMPTS) {
printf("MAX attempts reached, exiting program...\n");
free(FIN_packet);
free(FINACK_packet);
return(-1);
}
printf("Sending FIN_packet...\n");
if (sendto(sockfd, FIN_packet, sizeof(*FIN_packet), 0, &s.address, s.sock_len) == -1) {
perror("close FIN_packet error\n");
s.curr_state = CLOSED;
free(FIN_packet);
free(FINACK_packet);
return(-1);
}
if (FIN_packet->type == FIN) {
printf("FIN_packet sent.\nWaiting for FINACK_packet...\n");
alarm(TIMEOUT);
attempts++;
printf("Attempt number: %d\n", attempts);
if (recvfrom(sockfd, FINACK_packet, sizeof(*FINACK_packet), 0, &s.address, &s.sock_len) == -1) {
if (errno != EINTR) {
perror("close FINACK_packet error\n");
s.curr_state = CLOSED;
free(FIN_packet);
free(FINACK_packet);
continue;
}
} else {
s.curr_state = CLOSED;
}
}
}
printf("Freeing memory...\n");
printf("Closing socket...\n");
free(FIN_packet);
free(FINACK_packet);
close(sockfd);
return(0);
}
int gbn_connect(int sockfd, const struct sockaddr *server, socklen_t socklen){
printf("In Connect() with socket: %d, server: %d, socklen: %d\n", sockfd, server->sa_family, socklen);
/* create the SYN packet and populate it */
gbnhdr *SYN_packet = malloc(sizeof(*SYN_packet));
memset(SYN_packet->data, 0, sizeof(SYN_packet->data));
SYN_packet->type = SYN;
SYN_packet->seqnum = s.seq_num;
SYN_packet->checksum = 0;
SYN_packet->checksum = checksum((uint16_t *)SYN_packet, sizeof(*SYN_packet) / sizeof(uint16_t));
printf("gbn_connect SYN_PACKET checksum: %d\n", SYN_packet->checksum ) ;
gbnhdr *SYNACK_packet = create_rcv_pkt(); /* init the SYNACK packet to be sent */
/* counter that will handle when the close the connection on timeout/fail */
int attempts = 0;
while(1) {
if (attempts > MAX_ATTEMPTS) {
printf("\nMax attempt exceeded, exiting program...\n");
s.curr_state = CLOSED;
free(SYN_packet);
free(SYNACK_packet);
return(-1);
}
if (s.curr_state == CLOSED) {
printf("Sending SYN_packet with seqnum: %d...\n", SYN_packet->seqnum);
if (sendto(sockfd, SYN_packet, sizeof(*SYN_packet), 0, server, socklen) == -1) {
perror("Sendto error");
return(-1);
}
s.curr_state = SYN_SENT;
printf("Current state SYN_SENT: %d\n", s.curr_state);
}
if (s.curr_state == SYN_SENT) {
alarm(TIMEOUT);
attempts++;
printf("\nAttempt number: %d \nWaiting for SYNACK_packet...\n", attempts);
if (maybe_recvfrom(
sockfd, SYNACK_packet, sizeof(*SYNACK_packet), 0, (struct sockaddr *) &server, &socklen) == -1)
{
if (errno != EINTR) {
perror("Recvfrom SYNACK_packet error\n");
s.curr_state = CLOSED;
continue;
}
}
if ((SYNACK_packet->type == SYNACK) && (validate_packet(SYNACK_packet) == 1)) {
/* resetting the alarm to no alarm */
alarm(0);
printf("SYNACK_packet received\n");
s.address = *(struct sockaddr *) &server;
s.sock_len = socklen;
s.curr_state = ESTABLISHED;
s.seq_num = SYNACK_packet->seqnum;
printf("Current state ESTABLISHED: %d\n", s.curr_state);
free(SYN_packet);
free(SYNACK_packet);
return sockfd;
}
}
}
}
int gbn_listen(int sockfd, int backlog){
/* ---- There is no need to listen. Using only UDP calls. ---- */
printf("In Listen(), sockfd: %d, backlog: %d \n", sockfd, backlog);
/* Setting the default to closed since there is no connection established yet */
s.curr_state = CLOSED;
return(0);
}
int gbn_bind(int sockfd, const struct sockaddr *server, socklen_t socklen){
int status;
printf("In bind(), sockfd: %d\n", sockfd);
/* Binding to the socket for further use */
if ((status = bind(sockfd, server, socklen)) == -1) {
perror("Bind error");
return(-1);
}
return status;
}
int gbn_socket(int domain, int type, int protocol){
/*----- Randomizing the seed. This is used by the rand() function -----*/
srand((unsigned)time(0));
printf("domain: %d, type %d, protocol %d\n", domain, type, protocol);
/* Assign a random seq number used when sending or receiving packets */
s.seq_num = (uint8_t)rand();
/* Set the size to 1. This state will be modified according to the go-back-n protocol */
s.window_size = 1;
/* declare signal for setting alarm */
signal(SIGALRM, timeout_hdler);
printf("Seq num: %d, Window size: %d\n", s.seq_num, s.window_size);
int sockfd = socket(domain, type, protocol);
printf("Socket created %d\n", sockfd);
return sockfd;
}
int gbn_accept(int sockfd, struct sockaddr *client, socklen_t *socklen){
printf("In Accept() socket: %d, client address: %d\n", sockfd, client->sa_family);
printf("Current state: %d\n", s.curr_state);
/* init SYN_packet to be populated */
gbnhdr *SYN_packet = create_rcv_pkt();
/* init the SYNACK packet to be sent after the SYN packet */
gbnhdr *SYNACK_packet = malloc(sizeof(*SYNACK_packet));
memset(SYNACK_packet->data, 0, sizeof(SYNACK_packet->data));
SYNACK_packet->type = SYNACK;
gbnhdr *RST_packet = malloc(sizeof(*RST_packet));
memset(RST_packet->data, 0, sizeof(RST_packet->data));
RST_packet->type = RST;
/* number of bytes received */
ssize_t byte_count;
int attempts = 0;
while(1) {
if (attempts > MAX_ATTEMPTS) {
printf("\nMax attempt exceeded, exiting program...\n");
s.curr_state = CLOSED;
free(SYN_packet);
free(SYNACK_packet);
return(-1);
}
if (s.curr_state == CLOSED) {
/* Waiting for a SYN packet to establish connection */
alarm(TIMEOUT);
attempts++;
printf("Attempt number: %d\n Waiting for SYN_packet...\n", attempts);
if ((byte_count = maybe_recvfrom(sockfd, SYN_packet, sizeof(*SYN_packet), 0, client, socklen) == -1)) {
if (errno != EINTR) {
perror("SYN receive error\n");
s.curr_state = CLOSED;
continue;
}
}
printf("\nPacket received with byte_count: %d, SYN type: %d\n", (int)byte_count, SYN_packet->type);
}
if ((SYN_packet->type == SYN) && (validate_packet(SYN_packet) == 1)) {
/* reseting the alarm to zero (no alarm) for further use */
alarm(0);
printf("Packet is SYN_packet with size of: %d\n", sizeof(*SYN_packet));
s.curr_state = SYN_RCVD;
s.seq_num = SYN_packet->seqnum;
SYNACK_packet->seqnum = s.seq_num;
SYNACK_packet->checksum = checksum((uint16_t *)SYNACK_packet, sizeof(*SYNACK_packet) / sizeof(uint16_t));
printf("Current state SYN_RCVD: %d\n Sending SYNACK_packet...\n with seqnum: %d, checksum: %d\n",
s.curr_state,
SYNACK_packet->seqnum,
SYNACK_packet->checksum);
if (sendto(sockfd, SYNACK_packet, sizeof(*SYNACK_packet), 0, client, *socklen) == -1) {
perror("SYNACK send error\n");
return(-1); /* TODO retry sending SYNACK */
}
s.curr_state = ESTABLISHED;
s.address = *client;
s.sock_len = *socklen;
printf("Current state ESTABLISHED: %d\n", s.curr_state);
free(SYN_packet);
free(SYNACK_packet);
return sockfd;
}
}
}
uint8_t validate_packet(gbnhdr *packet){
uint32_t received_checksum = packet->checksum;
packet->checksum = 0;
uint16_t packet_checksum = checksum((uint16_t *)packet, sizeof(*packet) / sizeof(uint16_t));
printf("packet received checksum: %d, and calculated checksum: %d\n", received_checksum, packet_checksum);
if (packet_checksum == received_checksum) {
return(1);
}
printf("CHECKSUM FAILED: %d != %d\n",packet_checksum, received_checksum);
return(-1);
}
void create_DATA_packet(gbnhdr *DATA_packet, uint32_t pkt_seqnum, \
const void *buf_pointer, size_t data_len, int data_type) {
printf("Received data length: %d\n", (int)data_len);
memset(DATA_packet->data, 0, sizeof(DATA_packet->data));
DATA_packet->type = DATA;
DATA_packet->seqnum = pkt_seqnum;
DATA_packet->checksum = 0;
DATA_packet->actual_len = data_len;
memcpy(DATA_packet->data, buf_pointer, data_len);
printf("Packet seq number %d\n Packet data length: %d\n",
DATA_packet->seqnum, DATA_packet->actual_len);
printf("Adding data to packet %d\n Packet data in create_packet_func: %s\n",
DATA_packet->seqnum, DATA_packet->data);
DATA_packet->checksum = checksum((uint16_t *)DATA_packet, sizeof(*DATA_packet) / sizeof(uint16_t));
}
void timeout_hdler(int signum) {
/* apparently bad practice to printf in signal use flag instead */
printf("\nTIMEOUT has occured with signum: %d\n", signum);
/* TODO is this safe? race condition? */
signal(SIGALRM, timeout_hdler);
}
gbnhdr *create_rcv_pkt() {
gbnhdr *packet = malloc(sizeof(*packet));
memset(packet, 0, sizeof(*packet));
return packet;
}
ssize_t maybe_recvfrom(int s, char *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen){
/*----- Packet not lost -----*/
if (rand() > LOSS_PROB*RAND_MAX){
/*----- Receiving the packet -----*/
int retval = recvfrom(s, buf, len, flags, from, fromlen);
/*----- Packet corrupted -----*/
if (rand() < CORR_PROB*RAND_MAX){
/*----- Selecting a random byte inside the packet -----*/
int index = (int)((len-1)*rand()/(RAND_MAX + 1.0));
/*----- Inverting a bit -----*/
char c = buf[index];
if (c & 0x01)
c &= 0xFE;
else
c |= 0x01;
buf[index] = c;
}
return retval;
}
/*----- Packet lost -----*/
return(len); /* Simulate a success */
}