-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruft_server.c
1031 lines (965 loc) · 27.7 KB
/
ruft_server.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
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
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include<sys/socket.h>
#include<stdlib.h>
#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
#include<unistd.h>
#include<sys/stat.h>
#include<sys/time.h>
#include<signal.h>
#include "ruft.h"
typedef struct ruft_server_stats_
{
unsigned int t_rx;
unsigned int t_tx;
unsigned int ss_rx;
unsigned int ss_tx;
unsigned int ca_rx;
unsigned int ca_tx;
} ruft_server_stats_t;
FILE *debg_ofp;
int udp_serv_sock_fd;
ruft_server_states_t server_state;
ruft_server_traff_info_t *traff_info;
unsigned int max_wd;
unsigned int cwnd = MAX_PAYLOAD;
unsigned int cwnd_seg = 1;
unsigned int rwnd = MAX_PAYLOAD;
unsigned int rwnd_seg = 1;
unsigned int ss_thresh = 50*MAX_PAYLOAD; //64kb
unsigned int ss_thresh_seg = 50;
unsigned long int est_rtt = 0;
unsigned long int dev_rtt = 0;
unsigned long int timeout = 500;
unsigned int no_dist_ack_recvd = 0;
ruft_server_stats_t serv_stats;
#define THRESHOLD 500
/*
* Free the return value after use
*/
typedef struct ruft_server_rqst_info_
{
struct sockaddr_in cli_addr;
size_t cli_len;
char file_name[MAXLINE];
} ruft_server_rqst_info_t;
int cleanup(int serv_sock_fd, FILE *debg_ofp)
{
if (serv_sock_fd >= 0) {
close(serv_sock_fd);
}
if (debg_ofp != NULL) {
fclose(debg_ofp);
}
return 0;
}
int ruft_server_add_rx_stat()
{
switch(server_state)
{
case SV_SLOW_START:
serv_stats.t_rx++;
serv_stats.ss_rx++;
break;
case SV_CONG_AVOID:
serv_stats.t_rx++;
serv_stats.ca_rx++;
break;
default:
serv_stats.t_rx++;
}
return 0;
}
int ruft_server_add_tx_stat()
{
switch(server_state)
{
case SV_SLOW_START:
serv_stats.t_tx++;
serv_stats.ss_tx++;
break;
case SV_CONG_AVOID:
serv_stats.t_tx++;
serv_stats.ca_tx++;
break;
default:
serv_stats.t_tx++;
}
return 0;
}
int ruft_server_print_stat(FILE *debg_ofp)
{
fprintf(debg_ofp, "Total Packets Stats:\n");
fprintf(debg_ofp, "TX: %u\t", serv_stats.t_tx);
fprintf(debg_ofp, "RX: %u\n", serv_stats.t_rx);
fprintf(debg_ofp, "Slow Start Packets Stats:\n");
fprintf(debg_ofp, "TX: %u\t", serv_stats.ss_tx);
fprintf(debg_ofp, "RX: %u\t", serv_stats.ss_rx);
fprintf(debg_ofp, "TX%: %u\t",
(serv_stats.ss_tx*100)/serv_stats.t_tx);
fprintf(debg_ofp, "RX%: %u\n",
(serv_stats.ss_rx*100)/serv_stats.t_rx);
fprintf(debg_ofp, "Congestion Avoidance Packets Stats:\n");
fprintf(debg_ofp, "TX: %u\t", serv_stats.ca_tx);
fprintf(debg_ofp, "RX: %u\t", serv_stats.ca_rx);
fprintf(debg_ofp, "TX%: %u\t",
(serv_stats.ca_tx*100)/serv_stats.t_tx);
fprintf(debg_ofp, "RX%: %u\n",
(serv_stats.ca_rx*100)/serv_stats.t_rx);
return 0;
}
void int_handler(int sig)
{
cleanup(udp_serv_sock_fd, debg_ofp);
exit(0);
}
void ignore_sigpipe(void)
{
struct sigaction act;
int status;
memset(&act, 0, sizeof(act));
act.sa_handler = SIG_IGN;
act.sa_flags = SA_RESTART;
status = sigaction(SIGPIPE, &act, NULL);
if (status) {
fprintf(stderr, "sigaction\n");
}
}
int ruft_server_bootstrap(int *serv_sock_fd, int port_no, FILE *debg_ofp)
{
int status;
struct sockaddr_in serv_addr;
*serv_sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (*serv_sock_fd < 0) {
fprintf(debg_ofp, "Error opening socket\n");
fprintf(stderr, "Error opening socket\n");
return 1;
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port_no);
status = bind(*serv_sock_fd,(struct sockaddr *) &serv_addr,
sizeof(serv_addr));
if (status < 0) {
fprintf(debg_ofp, "Error binding port no: %d\n", port_no);
return 1;
}
return 0;
}
int ruft_server_pkt_info_to_ctx(ruft_pkt_info_t pkt, ruft_pkt_ctx_t *ctx,
FILE *debg_ofp)
{
short flags = 0;
flags = ntohs(pkt.flags);
/*Extract Flags*/
if ((flags&0x10) == 0x10) {
ctx->is_ack = TRUE;
} else {
ctx->is_ack = FALSE;
}
if ((flags&0x20) == 0x20) {
ctx->is_data_pkt = TRUE;
} else {
ctx->is_data_pkt = FALSE;
}
if ((flags&0x40) == 0x40) {
ctx->is_last_pkt = TRUE;
} else {
ctx->is_last_pkt = FALSE;
}
ctx->ack_no = ntohl(pkt.ack_no);
ctx->seq_no = ntohl(pkt.seq_no);
ctx->awnd = ntohs(pkt.awnd);
ctx->payload_length = ntohl(pkt.payload_length);
if (ctx->payload_length != 0) {
strncpy(ctx->payload, pkt.payload, ctx->payload_length);
} else {
ctx->payload[0] = '\0';
}
}
int ruft_server_pkt_ctx_to_info(ruft_pkt_info_t *pkt, ruft_pkt_ctx_t ctx,
FILE *debg_ofp)
{
/*Fill the Flags*/
short flags = 0;
if (ctx.is_ack == TRUE) {
flags = flags|0x10;
} else {
flags = flags|0x00;
}
if (ctx.is_data_pkt == TRUE) {
flags = flags|0x20;
} else {
flags = flags|0x00;
}
if (ctx.is_last_pkt == TRUE) {
flags = flags|0x40;
} else {
flags = flags|0x00;
}
pkt->flags = htons(flags);
pkt->ack_no = htonl(ctx.ack_no);
pkt->seq_no = htonl(ctx.seq_no);
pkt->awnd = htons(ctx.awnd);
pkt->payload_length = htonl(ctx.payload_length);
if(ctx.payload != NULL) {
strncpy(pkt->payload, ctx.payload, ctx.payload_length);
} else {
pkt->payload[0] = '\0';
}
return 0;
}
int ruft_server_print_pkt_ctx(ruft_pkt_ctx_t ctx, FILE *debg_ofp)
{
fprintf(debg_ofp, "\n+++++++++++++Packet Seq: %d++++++++++++++++\n",
ctx.seq_no);
fprintf(debg_ofp, "Message :\nAck : %s \n"\
"Data Pkt: %s\nLast Pkt: %s\n"\
"Advertised Window : %d \nAck No: %d \nSeq No: %d\n"\
"Payload Length: %d \nPayload: %s\n",
(ctx.is_ack == TRUE)?"TRUE":"FALSE",
(ctx.is_data_pkt == TRUE)?"TRUE":"FALSE",
(ctx.is_last_pkt == TRUE)?"TRUE":"FALSE",
ctx.awnd, ctx.ack_no, ctx.seq_no,
ctx.payload_length, ctx.payload);
fprintf(debg_ofp, "\n+++++++++++++++++++++++++++++++++++++++++++\n");
return 0;
}
/* int ruft_server_free_ctx(ruft_pkt_ctx_t *ctx) */
/* { */
/* free(ctx->payload); */
/* ctx->payload = NULL; */
/* return 0; */
/* } */
int ruft_server_send_pkt(ruft_pkt_ctx_t ctx, ruft_server_rqst_info_t req_info,
FILE *debg_ofp)
{
ruft_pkt_info_t pkt;
int num_bytes;
bzero(&pkt, sizeof(pkt));
ruft_server_pkt_ctx_to_info(&pkt, ctx, debg_ofp);
num_bytes = sendto(udp_serv_sock_fd, &pkt, sizeof(pkt), 0,
(struct sockaddr *) &(req_info.cli_addr),
(req_info.cli_len));
if (num_bytes == 0) {
fprintf(debg_ofp,
"UDP client connection closed at client side\n");
return 1;
}
if (num_bytes < 0) {
fprintf(debg_ofp, "ERROR: in UDP connection\n");
fflush(debg_ofp);
return 1;
}
return 0;
}
int ruft_server_recv_pkt(ruft_pkt_ctx_t *ctx, ruft_server_rqst_info_t *req_info,
FILE *debg_ofp)
{
ruft_pkt_info_t pkt;
int num_bytes;
bzero(&pkt, sizeof(pkt));
num_bytes = recvfrom(udp_serv_sock_fd, &pkt, sizeof(pkt), 0,
(struct sockaddr *) &(req_info->cli_addr),
(socklen_t *)&(req_info->cli_len));
if (num_bytes == 0) {
fprintf(debg_ofp,
"UDP client connection closed at client side\n");
return 1;
}
if (num_bytes < 0) {
fprintf(debg_ofp, "ERROR: in UDP connection\n");
fflush(debg_ofp);
return 1;
}
ruft_server_pkt_info_to_ctx(pkt, ctx, debg_ofp);
ruft_server_print_pkt_ctx(*ctx, debg_ofp);
return 0;
}
int ruft_server_recv_pkt_with_timeout(ruft_server_rqst_info_t *req_info,
int wnd, int offset, FILE *debg_ofp)
{
ruft_pkt_info_t pkt;
ruft_pkt_ctx_t ctx;
int num_bytes;
int status;
struct timeval recv_timeout;
fd_set sock_set;
int recv_ctr = offset;
int start_select = TRUE;
bzero(&pkt, sizeof(pkt));
fprintf(debg_ofp, "Time out : %lu\n", timeout);
recv_timeout.tv_sec = 0;
recv_timeout.tv_usec = timeout;
while((recv_ctr < (offset + wnd)) && start_select == TRUE) {
FD_SET(udp_serv_sock_fd, &sock_set);
status = select(udp_serv_sock_fd+1, &sock_set,
NULL, NULL, &recv_timeout);
if (status == 0){
fprintf(debg_ofp, "ERROR time out reached\n");
fflush(debg_ofp);
return 1;
}
if (FD_ISSET(udp_serv_sock_fd, &sock_set)) {
start_select = FALSE;
while ((recv_ctr < (offset + wnd)) && start_select == FALSE)
{
num_bytes = recvfrom(udp_serv_sock_fd, &pkt, sizeof(pkt), 0,
(struct sockaddr *)&(req_info->cli_addr),
(socklen_t *)&(req_info->cli_len));
if (num_bytes == 0) {
fprintf(debg_ofp,
"UDP client connection closed at client side\n");
start_select = TRUE;
/*Come out of inner while loop and poll select*/
/*if the ctr cond is valid*/
continue;
}
if (num_bytes < 0) {
fprintf(debg_ofp, "ERROR: in UDP connection\n");
fflush(debg_ofp);
return -1;
}
ruft_server_pkt_info_to_ctx(pkt, &ctx, debg_ofp);
ruft_server_print_pkt_ctx(ctx, debg_ofp);
fprintf(debg_ofp, "Pkt Recvd: %d\n", ctx.ack_no);
ruft_server_add_rx_stat();
if (ctx.is_last_pkt == TRUE) {
traff_info[ctx.ack_no/MAX_PAYLOAD + 1].no_ack_recvd =
traff_info[ctx.ack_no/MAX_PAYLOAD +1].no_ack_recvd + 1;
ruft_server_set_ack_recv(ctx);
} else {
ruft_server_set_ack_recv(ctx);
}
recv_ctr++;
fprintf(debg_ofp, "In inner while loop at %s\n", __FUNCTION__);
start_select = TRUE;
}
}
fprintf(debg_ofp, "In outer while loop at %s\n", __FUNCTION__);
FD_ZERO(&sock_set);
FD_CLR(udp_serv_sock_fd, &sock_set);
}
return 0;
}
int ruft_server_process_req(ruft_pkt_ctx_t ctx,
ruft_server_rqst_info_t *rqst_info, FILE *debg_ofp)
{
char method[10];
FILE *uri_file_p;
server_state = SV_PROC_REQ;
ruft_server_print_server_state(stdout);
sscanf(ctx.payload, "%s %s", method, rqst_info->file_name);
fprintf(debg_ofp, "Method : %s File_name: %s\n",
method, rqst_info->file_name);
method[strlen("GET")+1] = '\0';
rqst_info->file_name[strlen(rqst_info->file_name)+1] = '\0';
if (strncmp(method, "GET", strlen("GET")) != 0) {
/*Error Request Format*/
fprintf(debg_ofp, "Bad Request\n");
return 2;
}
uri_file_p = fopen(rqst_info->file_name, "r");
if (uri_file_p == NULL) {
fprintf(debg_ofp, "ERROR opening file file not found\n");
return 2;
}
fclose(uri_file_p);
return 1;
}
int ruft_server_add_traff_info(ruft_pkt_ctx_t ctx, unsigned int index,
FILE *debg_ofp)
{
if (index == 1) {
traff_info[index-1].seg_no = index - 1;
traff_info[index-1].no_ack_recvd = 1;
traff_info[index-1].is_acked = TRUE;
traff_info[index-1].first_byte = ctx.seq_no;
traff_info[index-1].last_byte = ctx.payload_length + ctx.seq_no -1;
}
traff_info[index].seg_no = index;
traff_info[index].no_ack_recvd = 0;
traff_info[index].is_acked = FALSE;
traff_info[index].first_byte = ctx.seq_no;
traff_info[index].last_byte = ctx.payload_length + ctx.seq_no -1;
return 0;
}
int ruft_server_set_ett_timeout(unsigned long rtt)
{
static first_time = TRUE;
if (first_time == TRUE) {
est_rtt = rtt;
first_time = FALSE;
}
/*The Jacobson/Karels algo according to the book*/
est_rtt = (0.875)*est_rtt + (0.125)*(rtt);
dev_rtt = (0.75)*dev_rtt + (0.25)*(abs(rtt-est_rtt));
timeout = est_rtt + 4*dev_rtt;
return 0;
}
int ruft_server_set_sent_time(unsigned int index)
{
if (index == 1) {
gettimeofday(&(traff_info[index-1].sent_time), 0);
}
gettimeofday(&(traff_info[index].sent_time), 0);
return 0;
}
int ruft_server_set_ack_recv_time(unsigned int index)
{
unsigned long int rtt;
gettimeofday(&(traff_info[index].ack_recv_time), 0);
traff_info[index].rtt = (traff_info[index].ack_recv_time.tv_sec
-traff_info[index].sent_time.tv_sec)*1000000
+ traff_info[index].ack_recv_time.tv_usec
- traff_info[index].sent_time.tv_usec;
rtt = traff_info[index].rtt;
ruft_server_set_ett_timeout(rtt);
return 0;
}
int ruft_server_set_ack_recv(ruft_pkt_ctx_t ctx)
{
int index = 1;
int wnd = (ctx.ack_no / MAX_PAYLOAD);
for (index = 1 ; index <= wnd; index++) {
if (traff_info[index].no_ack_recvd == 0) {
traff_info[index].no_ack_recvd =
traff_info[index].no_ack_recvd + 1;
no_dist_ack_recvd = no_dist_ack_recvd + 1;
traff_info[index].is_acked = TRUE;
ruft_server_set_ack_recv_time(index);
continue;
}
if (traff_info[index].last_byte == (ctx.ack_no - 1)) {
if (traff_info[index].no_ack_recvd == 0) {
no_dist_ack_recvd = no_dist_ack_recvd + 1;
traff_info[index].is_acked = TRUE;
}
traff_info[index].no_ack_recvd =
traff_info[index].no_ack_recvd + 1;
continue;
}
}
return 0;
}
int ruft_server_set_ack_recv_first_ele()
{
traff_info[0].no_ack_recvd = traff_info[0].no_ack_recvd + 1;
if (traff_info[0].no_ack_recvd < 2) {
ruft_server_set_ack_recv_time(0);
}
return 0;
}
long int ruft_server_get_file_size(FILE *uri_file_p)
{
long int size;
fseek(uri_file_p, 0, SEEK_END);
size = ftell(uri_file_p);
fseek(uri_file_p, 0, SEEK_SET);
return size;
}
int ruft_server_set_max_wd(ruft_server_rqst_info_t rqst_info, FILE *debg_ofp)
{
FILE *uri_file_p;
struct stat f_stat;
int uri_file_fd;
int no_of_segments;
int remainder = 0;
uri_file_p = fopen(rqst_info.file_name, "r");
uri_file_fd = fileno(uri_file_p);
fstat(uri_file_fd, &f_stat);
no_of_segments = f_stat.st_size/MAX_PAYLOAD;
if (f_stat.st_size % MAX_PAYLOAD != 0) {
no_of_segments = no_of_segments + 1;
remainder = f_stat.st_size % MAX_PAYLOAD;
}
max_wd = no_of_segments+1;
fclose(uri_file_p);
return 0;
}
int ruft_server_create_traff_window(ruft_server_rqst_info_t rqst_info,
FILE *debg_ofp)
{
switch(server_state)
{
case SV_REPLY_ERR:
max_wd = 1;
break;
case SV_SLOW_START:
ruft_server_set_max_wd(rqst_info, debg_ofp);
break;
default:
break;
}
traff_info = (ruft_server_traff_info_t *)
malloc(max_wd*sizeof(ruft_server_traff_info_t));
bzero(traff_info, max_wd*sizeof(ruft_server_traff_info_t));
return 0;
}
int ruft_server_send_file_seg(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info,
int is_last_pkt,
unsigned int index, FILE *debg_ofp)
{
ruft_pkt_ctx_t reply_ctx;
int status = 0;
FILE *uri_file_p;
int last_byte = MAX_PAYLOAD;
bzero(&reply_ctx, sizeof(reply_ctx));
uri_file_p = fopen(rqst_info.file_name, "r");
if (is_last_pkt != TRUE) {
fseek(uri_file_p, (index-1)*MAX_PAYLOAD, SEEK_SET);
last_byte = (index-1)*MAX_PAYLOAD + MAX_PAYLOAD;
reply_ctx.payload_length = MAX_PAYLOAD;
} else {
fseek(uri_file_p, 0, SEEK_END);
last_byte = ftell(uri_file_p);
if (last_byte > (index-1)*MAX_PAYLOAD) {
fseek(uri_file_p, (index-1)*MAX_PAYLOAD, SEEK_SET);
reply_ctx.payload_length = last_byte - (index-1)*MAX_PAYLOAD;
}
}
reply_ctx.is_ack = TRUE;
reply_ctx.is_data_pkt = TRUE;
reply_ctx.is_last_pkt = is_last_pkt;
reply_ctx.ack_no = req_ctx.seq_no + req_ctx.payload_length + index-1;
reply_ctx.seq_no = ftell(uri_file_p);
reply_ctx.awnd = MAX_PAYLOAD; //TODO
fread(reply_ctx.payload, 1, reply_ctx.payload_length, uri_file_p);
fseek(uri_file_p, 0, SEEK_SET);
fclose(uri_file_p);
ruft_server_add_traff_info(reply_ctx, index, debg_ofp);
ruft_server_set_sent_time(index);
status = ruft_server_send_pkt(reply_ctx, rqst_info, debg_ofp);
fprintf(debg_ofp, "Sent pkt: %d\n", reply_ctx.seq_no);
ruft_server_add_tx_stat();
return status;
}
int ruft_server_send_file_size(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info,
unsigned int index,FILE *debg_ofp)
{
ruft_pkt_ctx_t reply_ctx;
int status = 0;
char buf[MAXLINE];
FILE *uri_file_p;
uri_file_p = fopen(rqst_info.file_name, "r");
sprintf(buf, "file_size: %ld", ruft_server_get_file_size(uri_file_p));
fclose(uri_file_p);
reply_ctx.is_ack = TRUE;
reply_ctx.is_data_pkt = FALSE;
reply_ctx.is_last_pkt = FALSE;
reply_ctx.ack_no = req_ctx.seq_no+req_ctx.payload_length;
reply_ctx.seq_no = req_ctx.ack_no;
reply_ctx.awnd = MAX_PAYLOAD; //TODO
reply_ctx.payload_length = strlen(buf)+1;
strncpy(reply_ctx.payload, buf,
reply_ctx.payload_length-1);
reply_ctx.payload[reply_ctx.payload_length-1] = '\0';
ruft_server_add_traff_info(reply_ctx, index, debg_ofp);
ruft_server_set_sent_time(index);
status = ruft_server_send_pkt(reply_ctx, rqst_info, debg_ofp);
return status;
}
int is_all_ack_recvd(int wnd, int offset)
{
int index = offset;
for (index = offset ; index < (offset+wnd); index++) {
if (traff_info[index].no_ack_recvd == 0) {
fprintf(debg_ofp,"Chk ack Seg: %d\n", traff_info[index].seg_no);
return FALSE;
}
}
return TRUE;
}
int ruft_server_reconfigure_wnd()
{
switch(server_state)
{
case SV_SLOW_START:
ss_thresh = cwnd/2;
if (ss_thresh == 0) {
ss_thresh = MAX_PAYLOAD;
}
cwnd = MAX_PAYLOAD;
cwnd_seg = cwnd/MAX_PAYLOAD;
break;
case SV_CONG_AVOID:
server_state = SV_SLOW_START;
ss_thresh = cwnd/2;
if (ss_thresh == 0) {
ss_thresh = MAX_PAYLOAD;
}
cwnd = MAX_PAYLOAD;
cwnd_seg = cwnd/MAX_PAYLOAD;
break;
case SV_FAST_RECOV:
ss_thresh = cwnd/2;
if (ss_thresh == 0) {
ss_thresh = MAX_PAYLOAD;
}
cwnd = ss_thresh + 3*MAX_PAYLOAD;
cwnd_seg = cwnd/MAX_PAYLOAD;
server_state = SV_CONG_AVOID;
break;
}
ruft_server_print_server_state(stdout);
return 0;
}
int ruft_server_send_pending_data_segments(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info,
int wnd, int offset, FILE *debg_ofp)
{
int send_ctr = offset;
int is_last_pkt = FALSE;
int first_time = TRUE;
fprintf(debg_ofp, "In func %s \n", __FUNCTION__);
while (send_ctr < (offset+wnd))
{
if (send_ctr == (max_wd -1)) {
is_last_pkt = TRUE;
}
if (traff_info[send_ctr].no_ack_recvd >= 3) {
traff_info[send_ctr].no_ack_recvd = 0;
traff_info[send_ctr].is_acked = FALSE;
ruft_server_send_file_seg(req_ctx, rqst_info, is_last_pkt,
send_ctr, debg_ofp);
fprintf(debg_ofp, "Retransmision for %d\n",
traff_info[send_ctr].first_byte);
server_state = SV_FAST_RECOV;
ruft_server_print_server_state(stdout);
if (first_time == TRUE) {
ruft_server_reconfigure_wnd();
first_time = FALSE;
}
return 0;
}
if(traff_info[send_ctr].no_ack_recvd == 0) {
ruft_server_send_file_seg(req_ctx, rqst_info, is_last_pkt,
send_ctr, debg_ofp);
if(first_time == TRUE) {
ruft_server_reconfigure_wnd();
first_time = FALSE;
}
}
send_ctr++;
fprintf(debg_ofp, "In while loop at %s\n", __FUNCTION__);
}
}
int ruft_server_chk_timeout_threshold(unsigned int count)
{
if(count > THRESHOLD) {
return TRUE;
}
return FALSE;
}
int ruft_server_send_file_seg_win(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info,
int wnd, int offset, FILE *debg_ofp)
{
int ctr = offset;
int is_last_pkt = FALSE;
int all_ack_recvd = FALSE;
int status = 0;
unsigned int timeout_count = 0;
if (ctr == (max_wd - 1)) {
is_last_pkt = TRUE;
}
fprintf(debg_ofp, "wnd: %d offset: %d\n", wnd, offset);
while (ctr < (offset + wnd) && is_last_pkt == FALSE)
{
ruft_server_send_file_seg(req_ctx, rqst_info,
is_last_pkt, ctr, debg_ofp);
fprintf(debg_ofp, "In first while loop at %s seg %d\n",
__FUNCTION__, ctr);
ctr++;
if(ctr == (max_wd - 1)) {
is_last_pkt = TRUE;
}
}
if (is_last_pkt == TRUE) {
ruft_server_send_file_seg(req_ctx, rqst_info,
is_last_pkt, ctr, debg_ofp);
ctr++;
}
while(all_ack_recvd == FALSE) {
status = ruft_server_recv_pkt_with_timeout(&rqst_info, wnd,
offset, debg_ofp);
if (status == -1) {
return status;
}
if (status == 1) {
timeout_count++;
fprintf(debg_ofp, "Ack recv loop timed out count:%u\n",
timeout_count);
if(ruft_server_chk_timeout_threshold(timeout_count) == TRUE) {
all_ack_recvd = TRUE;
server_state = SV_FILE_SENT;
fprintf(stdout, "Time out threshold reached\n");
ruft_server_print_server_state(stdout);
continue;
}
}
all_ack_recvd = is_all_ack_recvd(wnd, offset);
if (all_ack_recvd == FALSE) {
ruft_server_send_pending_data_segments(req_ctx, rqst_info,
wnd, offset, debg_ofp);
}
fprintf(debg_ofp, "In second while loop at %s\n", __FUNCTION__);
}
return 0;
}
int ruft_server_print_server_state(FILE *debg_ofp)
{
switch(server_state)
{
case SV_SLOW_START:
fprintf(debg_ofp, "Server State: SLOW START\n");
break;
case SV_CONG_AVOID:
fprintf(debg_ofp, "Server State: CONGESTION AVOIDANCE\n");
break;
case SV_REPLY_ERR:
fprintf(debg_ofp, "Server State: ERROR REPLY\n");
break;
case SV_FILE_SENT:
fprintf(debg_ofp, "Server State: FILE SENT\n");
break;
case SV_FAST_RECOV:
fprintf(debg_ofp, "Server State: FAST RECOVERY\n");
break;
case SV_WAIT:
fprintf(debg_ofp, "Server State: SERVER WAIT\n");
break;
case SV_PROC_REQ:
fprintf(debg_ofp, "Server State: PROCESS REQUEST\n");
break;
default:
fprintf(debg_ofp, "Server State: SERVER WAIT\n");
break;
}
return 0;
}
int min(int a, int b)
{
if (a >= b) {
return b;
}
if (b > a) {
return a;
}
}
int ruft_server_get_wnd (FILE *debg_ofp)
{
int wnd;
switch(server_state)
{
case SV_SLOW_START:
cwnd = cwnd + MAX_PAYLOAD*no_dist_ack_recvd;
cwnd_seg = cwnd/MAX_PAYLOAD;
break;
case SV_CONG_AVOID:
cwnd = cwnd + (MAX_PAYLOAD*(MAX_PAYLOAD/cwnd))*no_dist_ack_recvd;
cwnd_seg = cwnd/MAX_PAYLOAD;
break;
}
if (cwnd > ss_thresh && server_state == SV_SLOW_START) {
server_state = SV_CONG_AVOID;
}
ruft_server_print_server_state(stdout);
fprintf(debg_ofp, "cwnd_seg: %d Dist_ack_recvd: %d \n",
cwnd_seg, no_dist_ack_recvd);
fprintf(debg_ofp, "cwwnd_seg: %d rwnd_seg: %d\n",
cwnd_seg, rwnd_seg);
wnd = min(cwnd_seg, rwnd_seg);
return wnd;
}
int ruft_server_wnd_init()
{
cwnd = MAX_PAYLOAD;
cwnd_seg = 1;
ss_thresh = 50*MAX_PAYLOAD;
ss_thresh_seg = 50;
no_dist_ack_recvd = 0;
}
int ruft_server_send_file(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info, FILE *debg_ofp)
{
int wnd_ctr = 1;
int wnd = 1;
server_state = SV_SLOW_START;
ruft_server_print_server_state(stdout);
ruft_server_wnd_init();
wnd = ruft_server_get_wnd(debg_ofp);
ruft_server_create_traff_window(rqst_info, debg_ofp);
ruft_server_send_file_size(req_ctx, rqst_info, 0, debg_ofp);
ruft_server_add_tx_stat();
while (server_state != SV_FILE_SENT && wnd_ctr < max_wd)
{
if ((max_wd - wnd_ctr) <= (wnd - wnd_ctr)) {
wnd = max_wd - wnd_ctr;
}
fprintf(debg_ofp, "wnd: %d offset: %d\n", wnd, wnd_ctr);
ruft_server_send_file_seg_win(req_ctx, rqst_info,
wnd, wnd_ctr, debg_ofp);
wnd_ctr = wnd_ctr + wnd;
wnd = ruft_server_get_wnd(debg_ofp);
if (wnd > max_wd) {
wnd = max_wd;
}
if ((wnd + wnd_ctr)>max_wd) {
wnd = max_wd-wnd_ctr;
}
if (is_all_ack_recvd(max_wd, 0) == TRUE) {
server_state = SV_FILE_SENT;
ruft_server_print_server_state(stdout);
fprintf(stdout, "File Sent\n");
}
fprintf(debg_ofp, "In while loop at %s\n", __FUNCTION__);
}
}
int ruft_server_send_err_msg(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info, FILE *debg_ofp)
{
ruft_pkt_ctx_t reply_ctx;
int status = 0;
reply_ctx.is_ack = TRUE;
reply_ctx.is_data_pkt = FALSE;
reply_ctx.is_last_pkt = TRUE;
reply_ctx.ack_no = req_ctx.seq_no+req_ctx.payload_length;
reply_ctx.seq_no = req_ctx.ack_no;
reply_ctx.awnd = MAX_PAYLOAD; //TODO
reply_ctx.payload_length = strlen("Arya Error: File Not Found")+1;
strncpy(reply_ctx.payload, "Arya Error: File Not Found",
reply_ctx.payload_length-1);
reply_ctx.payload[reply_ctx.payload_length-1] = '\0';
ruft_server_add_traff_info(reply_ctx, 0, debg_ofp);
ruft_server_set_sent_time(0);
/*Set the time for Zeroth segment as this is error scenario*/
status = ruft_server_send_pkt(reply_ctx, rqst_info, debg_ofp);
return status;
}
int ruft_server_handle_err(ruft_pkt_ctx_t req_ctx,
ruft_server_rqst_info_t rqst_info, FILE *debg_ofp)
{
ruft_pkt_ctx_t ctx;
ruft_server_rqst_info_t recv_rqst_info;
int status;
bzero(&ctx, sizeof(ctx));
server_state = SV_REPLY_ERR;
ruft_server_print_server_state(stdout);
ruft_server_create_traff_window(rqst_info, debg_ofp);
status = ruft_server_send_err_msg(req_ctx, rqst_info, debg_ofp);
if (status != 0) {
return status;
}
status = ruft_server_recv_pkt(&ctx, &recv_rqst_info, debg_ofp);
if (status != 0) {
return status;
}
if (ctx.is_ack == TRUE && ctx.is_last_pkt == TRUE
&& ctx.is_data_pkt == FALSE){
ruft_server_set_ack_recv_first_ele();
fprintf(debg_ofp, "\nRTT: %lu Timeout: %lu\n",
traff_info[0].rtt, timeout);
}
return status;
}
int ruft_server_handle_pkt(ruft_pkt_info_t pkt,struct sockaddr_in cli_addr,
int cli_len,FILE *debg_ofp)
{
ruft_pkt_ctx_t ctx;
int status;
ruft_server_rqst_info_t rqst_info;
bzero(&ctx, sizeof(ctx));
bzero(&rqst_info, sizeof(rqst_info));
rqst_info.cli_addr = cli_addr;
rqst_info.cli_len = cli_len;
ruft_server_pkt_info_to_ctx(pkt, &ctx, debg_ofp);
fprintf(debg_ofp, "Request Recieved\n");
ruft_server_print_pkt_ctx(ctx, debg_ofp);
status = ruft_server_process_req(ctx, &rqst_info, debg_ofp);
switch(status)
{
case 1:
ruft_server_send_file(ctx, rqst_info, debg_ofp);
break;
case 2:
ruft_server_handle_err(ctx, rqst_info, debg_ofp);
break;
default:
ruft_server_handle_err(ctx, rqst_info, debg_ofp);
break;
}
server_state = SV_WAIT;
ruft_server_print_server_state(stdout);
/* if (traff_info != NULL) { */
/* free(traff_info); */
/* } */
/* traff_info = NULL; */
return 0;
}
int main(int argc, char *argv[])
{
char file_name[] = "udp_server_trace.log";
int num_bytes;
int port_no;
int status;
struct sockaddr_in cli_addr;
size_t cli_len;
ruft_pkt_info_t pkt;
signal(SIGINT, int_handler);
signal(SIGPIPE, int_handler);
debg_ofp = fopen(file_name, "w");
bzero(&pkt, sizeof(pkt));
if (argc < 3) {
fprintf(debg_ofp, "No port no or rwnd entered hence exiting\n"\
"Syntax: ./ruft_server.bin <port-no>\t"\
"<rwnd in multiples of 1280>");
fprintf(stderr, "Very Few Arguments enter port no "\
"and rwnd\n");
fflush(debg_ofp);
cleanup(-1, debg_ofp);
exit(1);
}
port_no = atoi(argv[1]);
rwnd = atoi(argv[2]);
rwnd_seg = rwnd/MAX_PAYLOAD;
status = ruft_server_bootstrap(&udp_serv_sock_fd, port_no, debg_ofp);
if (status != 0){
fprintf(debg_ofp, "ERROR in binding server port\n");
fprintf(stderr,
"ERROR bootstrapping server see log files for details\n");
fflush(debg_ofp);
cleanup(udp_serv_sock_fd, debg_ofp);