-
Notifications
You must be signed in to change notification settings - Fork 1
/
vodserver.c
2238 lines (1944 loc) · 63.8 KB
/
vodserver.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
/*
* echoserver.c - A simple connection-based echo server
* usage: echoserver <port>
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdarg.h>
#include <sys/time.h>
#include <time.h>
#include <uuid/uuid.h>
#include <mach/clock.h>
#include <mach/mach.h>
#define BUFSIZE 1024
#define MAXLINE 8192
#define SERVLEN 100
#define HOSTLEN 256
#define PACKET_SIZE 1400
#define HEADER 16
#if 0
/*
* Structs exported from netinet/in.h (for easy reference)
*/
/* Internet address */
struct in_addr {
unsigned int s_addr;
};
/* Internet style socket address */
struct sockaddr_in {
unsigned short int sin_family; /* Address family */
unsigned short int sin_port; /* Port number */
struct in_addr sin_addr; /* IP address */
unsigned char sin_zero[...]; /* Pad to size of 'struct sockaddr' */
};
/*
* Struct exported from netdb.h
*/
/* Domain name service (DNS) host entry */
struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length; /* length of address */
char **h_addr_list; /* list of addresses */
}
#endif
const char* error404 = "<html><head><title>404 Error: Not Found</title></head><body>404 File Not Found</body></html>";
/* URI parsing results. */
typedef enum {
PARSE_ERROR,
PARSE_CORRECT
} parse_result;
/* Peer Functions*/
typedef enum {
VIEW = 0,
ADD = 1,
CONFIG = 2,
STATUS = 3,
NONE = 4,
KILL = 5,
UUID = 6,
ADDNEIGHBOR = 7,
NEIGHBORS = 8,
MAP = 9
} peer_method;
/* Client Info for Connection Thread*/
typedef struct {
struct sockaddr_in addr; // Socket address
socklen_t addrlen; // Socket address length
int connfd; // Client connection file descriptor
char host[HOSTLEN]; // Client host
char serv[SERVLEN]; // Client service (port)
} client_info;
/* Parsed URI structure */
typedef struct {
char method[MAXLINE];
char path[MAXLINE];
char version;
char temp[MAXLINE];
char ext[MAXLINE];
char host[MAXLINE];
uuid_t uuid;
unsigned int back_port;
unsigned int front_port;
unsigned int rate;
unsigned int distance;
parse_result result;
peer_method pm;
} url_info;
typedef struct {
const char *ext;
char *iana;
} ftype;
ftype file_types [] = {
{".txt", "text/plain"},
{".css", "text/css"},
{".htm", "text/html"},
{".html", "text/html"},
{".gif", "image/gif"},
{".jpeg", "image/jpeg"},
{".jpg", "image/jpeg"},
{".ico", "image/x-icon"},
{".png", "image/png"},
{".js", "application/javascript"},
{".ogg", "video/ogg"},
{".mp4", "video/mp4"},
{".webm", "video/webm"},
{".octet-stream","application/octet-stream"},
{".json", "application/json"},
{NULL, NULL},
};
typedef struct point{
char* name;
int distance;
} point, *point_t;
typedef struct submap{
char* name;
point_t* neighbors;
int len;
} submap, *submap_t;
int rand_close = 0;
/*Our own custom UDP packet*/
typedef struct {
char flags; //byte 0 ASFX (Ack, Syn, Fin, Unused)
char pack; //byte 1
uint16_t source_port; //bytes 2 - 3
uint16_t dest_port; //bytes 4 - 5
uint16_t length; //bytes 6 - 7
uint16_t syn; //bytes 8 - 9
uint16_t ack; //bytes 10 - 11
uint16_t window; //bytes 12 - 13
uint16_t rtt; //bytes 14 - 15
char* data; //bytes 16 - (17+length)
} packet;
typedef struct peer{
uuid_t uuid;
char* name;
uint16_t front_port;
uint16_t back_port;
uint16_t syn;
char* content_dir;
int distance;
int num_files;
char* files[20]; //Max number of files one peer can have is 20 **** MAYBE REVISIT AND MAKE IT DYNAMICALLY ALLOCATE
char* host;
struct sockaddr_in addr;
uint64_t last_received;
submap_t sm;
} peer, *peer_t;
void printPeer(peer_t self)
{
char* ud = malloc(sizeof(char)*37);
printf("------------------------Peer: %s---------------------\n", self->name);
uuid_unparse(self->uuid, ud);
printf("uuid: %s\n", ud);
printf("name: %s\n", self->name);
printf("frontend_port: %u\n", self->front_port);
printf("backend_port: %u\n", self->back_port);
printf("content_dir: %s\n", self->content_dir);
printf("-----------------------------------------------------------\n");
}
peer_t peer_table[100];
peer_t non_neighbors[100];
static int num_peers = 0;
static int num_nn = 0;
typedef struct{
char *filename;
struct sockaddr_in addr; //client socket address
unsigned short port;
} New_peer;
//static New_peer new_peer;
/* peer database table*/
New_peer my_db[100];
static int db_entries = 0;
/* newflow struct for flow table */
typedef struct{
char pack; // ID for the flow
struct sockaddr_in addr;
socklen_t addrlen;
uint16_t base_syn;
uint16_t syn; //init
uint16_t nss;
uint16_t naa;
uint16_t ack;
int src_port;
int last_ack;
int window_size;
int last_sent;
char filename[MAXLINE];
FILE* file;
long file_size;
int client_fd;
uint16_t window;
int available;
uint64_t last_ack_time;
int error;
int on_fd;
} New_flow;
//static New_flow new_flow;
/* flow database */
New_flow my_flow[25];
static int flow_entries = 0;
static int new_names = 0;
char* get_name()
{
printf("gimme dat new name %d\n", new_names);
char* name = malloc(sizeof(char)* 20);
sprintf(name, "friend_%d", new_names);
new_names += 1;
return name;
}
static int back_port;
static int back_fd;
static int window_g = 10; //change accordgingly
static unsigned int max_window_size;
static int rate;
uint64_t expiration = 30000;
packet* unwrap(char* buf);
char* package(packet* p);
void addPeer(char *file, struct sockaddr_in, unsigned short int s_port);
void flow(char flow_ID, unsigned int s_addr, unsigned short int s_port, uint16_t base_syn, char *file);
packet* request_new_packet(char* path, char flowID, uint16_t source_port, uint16_t dest_port);
char getFlowID();
uint16_t getSequence();
void *serve(int connfd, fd_set* live_set);
char* get_rfc_time();
void re_tx_last(packet* p, New_flow* prev_flow, int fd);
void re_tx_last_sender(packet* p, New_flow* nf, int fd);
struct sockaddr_in get_sockaddr_from_host(char* host, uint16_t back_port);
char* submap_to_json(submap_t sm);
int send_len(packet* p);
char* get_uuid_from_name(char* name);
char* submap_to_json_lsa(submap_t sm);
peer_t get_peer(uuid_t uuid);
int getTimeMilliseconds() {
struct timespec ts;
clock_serv_t cclock;
mach_timespec_t mts;
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
clock_get_time(cclock, &mts);
mach_port_deallocate(mach_task_self(), cclock);
ts.tv_sec = mts.tv_sec;
ts.tv_nsec = mts.tv_nsec;
uint64_t delta_ms = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
return delta_ms;
}
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
// exit(1);
}
packet* get_LSA(peer_t peer)
{
packet* p = (packet*)malloc(sizeof(packet));
char buf[MAXLINE];
char uuid[40];
uuid_unparse(peer_table[0]->uuid, uuid);
sprintf(buf, "%s&%s", uuid, submap_to_json_lsa(peer_table[0]->sm));
p->flags = 0x2; //0x0011 LSA announcement key
p->pack = 0;
peer->syn++;
p->syn = peer->syn;
p->source_port = peer_table[0]->back_port;
p->dest_port = peer->back_port;
p->length = strlen(buf);
p->data = buf;
p->window = window_g;
return p;
}
void send_LSA()
{
char* buf;
struct sockaddr_in* provider = NULL;
unsigned short port;
packet* p;
for(int i = 1; i < num_peers; i++)
{
p = get_LSA(peer_table[i]);
buf = package(p);
provider = &(peer_table[i]->addr);
port = peer_table[i]->back_port;
if(sendto(back_fd, buf, send_len(p), 0, (struct sockaddr*)provider, sizeof(*provider)) < 0)
printf("Error sending LSA packet");
}
}
void forward_to_peers(packet* p)
{
struct sockaddr_in* provider = NULL;
unsigned short port;
char* buf = package(p);
for(int i = 1; i < num_peers; i++)
{
provider = &(peer_table[i]->addr);
port = peer_table[i]->back_port;
if(sendto(back_fd, buf, send_len(p), 0, (struct sockaddr*)provider, sizeof(*provider)) < 0)
printf("Error sending LSA packet");
}
return;
}
submap_t gen_submap()
{
int index;
submap_t sm = malloc(sizeof(submap));
sm->name = peer_table[0]->name;
sm->neighbors = malloc(sizeof(point_t)*(num_peers-1));
for(int i = 1; i < num_peers; i++)
{
index = i-1;
sm->neighbors[index] = malloc(sizeof(point));
sm->neighbors[index]->name = peer_table[i]->name;
sm->neighbors[index]->distance = peer_table[i]->distance;
}
sm->len = num_peers-1;
return sm;
}
char* point_to_json_lsa(point_t p)
{
char* buf = malloc(sizeof(char)* BUFSIZE);
sprintf(buf, "\"%s\":%d", get_uuid_from_name(p->name), p->distance);
return buf;
}
char* submap_to_json_lsa(submap_t sm)
{
char* buf = malloc(sizeof(char)*BUFSIZE);
if(sm->len == 0)
{
sprintf(buf, "\"%s\":{}", sm->name);
return buf;
}
sprintf(buf, "\"%s\":{%s", sm->name, point_to_json_lsa(sm->neighbors[0]));
for(int i = 1; i < sm->len; i++)
{
buf = strcat(buf, ",");
buf = strcat(buf, point_to_json_lsa(sm->neighbors[i]));
}
buf = strcat(buf, "}");
return buf;
}
char* point_to_json(point_t p)
{
char* buf = malloc(sizeof(char)* BUFSIZE);
sprintf(buf, "\"%s\":%d", p->name, p->distance);
return buf;
}
char* submap_to_json(submap_t sm)
{
if(sm == NULL)
{
printf("Submap is empty");
return "";
}
char* buf = malloc(sizeof(char)*BUFSIZE);
if(sm->len == 0)
{
sprintf(buf, "\"%s\":{}", sm->name);
return buf;
}
sprintf(buf, "\"%s\":{%s", sm->name, point_to_json(sm->neighbors[0]));
for(int i = 1; i < sm->len; i++)
{
buf = strcat(buf, ",");
buf = strcat(buf, point_to_json(sm->neighbors[i]));
}
buf = strcat(buf, "}");
return buf;
}
submap_t json_to_submap(char* str)
{
submap_t sm = malloc(sizeof(submap));
char name[200];
char* uuid_str = malloc(sizeof(char)*40);
char rest[BUFSIZE];
uuid_t uuid;
char* token;
int len = 0;
int distance = -1;
sscanf(str, "\"%[^\"]\":{%[^}]", name, rest);
printf("name: %s rest: %s\n", name, rest);
sm->name = name;
sm->neighbors = malloc(sizeof(point_t)*40);
token = strtok(rest, ",");
while(token != NULL)
{
sscanf(token, "\"%[^\"]\":%d", uuid_str, &distance);
uuid_parse(uuid_str, uuid);
sm->neighbors[len] = malloc(sizeof(point));
sm->neighbors[len]->name = (get_peer(uuid))->name;
sm->neighbors[len]->distance = distance;
len++;
token = strtok(NULL, ",");
}
sm->len = len;
printf("unparsed to this beaut: %s\n", submap_to_json(sm));
return sm;
}
void printFlowTable()
{
// printf("~~~~~~~~~~~f l o w t a b l e~~~~~~~~~~~~~~\n");
// for(int i = 0; i < flow_entries; i++)
// {
// printf("%d: %s | ", my_flow[i].pack, my_flow[i].filename);
// }
// printf("\n~~~~~~~~~~~e n d t a b l e ~~~~~~~~~~~~~~\n");
}
void printPacket(packet* p)
{
printf("------------------------Printing Packet-------------------------------------------\n");
printf("flags: %u | flowID: %d | source_port: %u\n", p->flags, (int)p->pack, p->source_port);
printf("dest_port: %u | length: %u\n", p->dest_port, p->length);
printf("syn: %u | ack: %u\n", p->syn, p->ack);
printf("window: %u | rtt: %u\n", p->window, p->rtt);
printf("data: %s\n", p->data);
printf("-----------------------------End Packet-------------------------------------------\n");
}
char getFlowID()
{
//Random Number Gen
return (char)(rand()%255);
}
uint16_t getSequence()
{
//Random Number Gen
return (uint16_t)(rand());
}
void addNeighbor(uuid_t uuid, char* host, uint16_t frontend, uint16_t backend, int distance)
{
peer_t np = malloc(sizeof(peer));
uuid_copy(np->uuid, uuid);
np->host = malloc(sizeof(char)*strlen(host));
sprintf(np->host, "%s", host);
np->name = get_name();
np->back_port = backend;
np->front_port = frontend;
np->distance = distance;
np->content_dir = malloc(sizeof(char)*9);
np->content_dir = "content/";
np->num_files = 0;
np->syn = 0;
np->last_received = getTimeMilliseconds();
np->sm = malloc(sizeof(submap));
np->sm->name = np->name;
np->sm->len = 0;
peer_table[num_peers] = np;
np->addr = get_sockaddr_from_host(np->host, np->back_port);
num_peers++;
}
char* peerToJSON(peer_t p)
{
char* json = malloc(sizeof(char)*400);
char uuid[40];
uuid_unparse(p->uuid, uuid);
sprintf(json, "{\"uuid\":\"%s\","
"\"name\":\"%s\","
"\"host\":\"%s\","
"\"frontend\":\"%u\","
"\"backend\":\"%u\","
"\"num_files\":\"%d\","
"\"metric\":\"%d\"}", uuid, p->name, p->host, p->front_port, p->back_port, p->num_files, p->distance);
return json;
}
char* tableToJSON()
{
char* json = malloc(sizeof(char)*MAXLINE);
sprintf(json, "[%s", peerToJSON(peer_table[0]));
for(int i = 1; i < num_peers; i++)
{
json = strcat(json, ",");
json = strcat(json, peerToJSON(peer_table[i]));
}
json = strcat(json, "]");
return json;
}
/* Addpeer: adds the peer address and port to the table */
void addPeer(char *file, struct sockaddr_in serveraddr, unsigned short int s_port){
New_peer* np;
printf("Adding new peer! %s %d %u\n", file, serveraddr.sin_addr.s_addr, s_port);
//FIX THE GET ADDR INFO
for(int i = 0; i < db_entries; i++){
printf("Checking #%d %s\n", i, my_db[i].filename);
np = &my_db[i];
if (strcmp(np->filename, file) == 0) { //file found in database
return;
}
}
// if file not found, add to my_db (i value should be the one pointing to end of table from above
np = malloc(sizeof(New_peer));
np->filename = file;
np->addr = serveraddr;
np->port = s_port;
my_db[db_entries] = *np;
db_entries++;
// printf("added!\n");
return;
}
void addFile(char* file, uuid_t uuid)
{
peer_t p;
for(int i = 0; i < num_peers; i++)
{
if(uuid_compare(uuid, peer_table[i]->uuid) == 0)
{
p = peer_table[i];
printf("Adding file: %s to peer: %s\n", file, p->name);
p->files[p->num_files] = malloc(sizeof(char)*strlen(file));
sprintf(p->files[p->num_files], "%s", file);
peer_table[i]->num_files += 1;
return;
}
}
printf("No peer with that UUID was found so the file could not be added!\n");
}
char* get_uuid_from_name(char* name)
{
uuid_t u;
char* uuid = malloc(sizeof(char) * 40);
bzero(u, sizeof(uuid_t));
for(int i = 1; i < num_peers; i++)
{
if(strcmp(name, peer_table[i]->name) == 0)
{
uuid_unparse(peer_table[i]->uuid, uuid);
return uuid;
}
}
for(int i = 0; i < num_nn; i++)
{
if(strcmp(name, non_neighbors[i]->name) == 0)
{
uuid_unparse(non_neighbors[i]->uuid, uuid);
return uuid;
}
}
return "";
}
peer_t get_peer(uuid_t uuid)
{
peer_t np;
for(int i = 0; i < num_peers; i++)
{
if(uuid_compare(uuid, peer_table[i]->uuid) == 0)
{
printf("found neibr frand\n");
return peer_table[i];
}
}
for(int i = 0; i < num_nn; i++)
{
if(uuid_compare(uuid, non_neighbors[i]->uuid) == 0)
{
return non_neighbors[i];
}
}
printf("Adding non-neighbor \n");
//Not a neighbor and not a known non-neighbor so add to Non-neighbors list
np = malloc(sizeof(peer));
uuid_copy(np->uuid, uuid);
np->name = get_name();
np->distance = -1;
np->syn = 0;
np->sm = malloc(sizeof(submap));
np->sm->name = np->name;
np->sm->len = 0;
np->last_received = getTimeMilliseconds();
non_neighbors[num_nn] = np;
num_nn++;
return np;
}
//Takes a file name and returns the index of the peer with it in the peer_table
//returns -1 if not found
int fileLook(char* file)
{
peer_t p;
printf("num_peers = %d\n", num_peers);
for(int i = 1; i < num_peers; i++)
{
printf("checking peer #%d which has %d files\n", i, p->num_files);
p = peer_table[i];
for(int j = 0; j < p->num_files; j++)
{
printf("Checking \"%s\"\n", p->files[j]);
if(strcmp(file, p->files[j]) == 0)
{
printf("FOUND\n");
return i;
}
}
}
return -1;
}
void sendHeaders(char* file_size, char* filename, int clientfd){
int n;
char temp[MAXLINE];
char extension[MAXLINE];
char* content_type = NULL;
char response[MAXLINE];
if (sscanf(filename, "%[^.]%s", temp, extension) != 2) {
printf("500 Internal Server Error:Received a malformed request due to extension \n");
exit(1);
return;
}
ftype *f_ext = file_types;
while(f_ext->ext){
if(strcmp(f_ext->ext,extension)==0){
content_type = f_ext->iana;
break;
}
f_ext++;
}
if (strcmp(content_type, "x-icon")==0){
return;
}
sprintf(response, "HTTP/1.1 200 OK\r\n"
"Content-Length: %s\r\n"
"Content-Type: %s\r\n"
"Connection: Keep-Alive\r\n"
"Accept-Ranges: bytes\r\n"
"Date: %s\r\n\r\n", file_size, content_type, get_rfc_time());
// Write response to fd
n = write(clientfd, response, strlen(response));
printf("The length of the headers: %lu written to clientfd: %d\n\n", strlen(response), clientfd);
if (n < 0)
error("ERROR writing to socket");
// printf("~~~~Sending Headers~~~~~\n%s\n\n", response);
return;
}
int send_len(packet* p)
{
return (16 + p->length) * sizeof(char);
}
New_flow* flow_look(char flow_ID)
{
New_flow* nf;
for(int i = 0; i < flow_entries; i++)
{
nf = &my_flow[i];
if(nf->pack == flow_ID) // Flow exists
{
return nf;
}
}
return NULL;
}
int remove_nn(int i)
{
num_nn--;
while(i < num_nn)
{
non_neighbors[i] = non_neighbors[i+1];
}
return 0;
}
int remove_peer(uuid_t uuid)
{
int res = 0;
int i;
peer_t p;
for(i = 1; i < num_peers; i++)
{
p = peer_table[i];
if(uuid_compare(uuid, p->uuid) == 0) // Flow exists
{
res = 1;
num_peers--;
break;
}
}
while(i < num_peers)
{
peer_table[i] = peer_table[i+1];
i++;
}
return res;
}
int remove_flow(char flow_ID)
{
int res = 0;
int i;
New_flow* nf;
for(i = 0; i < flow_entries; i++)
{
nf = &my_flow[i];
if(nf->pack == flow_ID) // Flow exists
{
res = 1;
flow_entries--;
break;
}
}
while(i < flow_entries)
{
my_flow[i] = my_flow[i+1];
i++;
}
return res;
}
New_flow flow_add(char flow_ID, struct sockaddr_in addr, uint16_t syn, uint16_t ack, char* path, FILE* file, uint16_t size, int fd, uint16_t window)
{
New_flow nf;
nf.pack = flow_ID;
nf.addr = addr;
nf.base_syn = syn;
nf.syn = syn;
nf.ack = ack;
strcpy(nf.filename, path);
nf.file = file;
nf.file_size = size;
nf.client_fd = fd;
nf.window = window;
my_flow[flow_entries] = nf;
flow_entries++;
return nf;
}
void getContent(char* path, int fd)
{
struct sockaddr_in* provider = NULL;
unsigned short port;
char* buf;
packet* req;
int index;
//Look for who has the file in the lookup table
index = fileLook(path);
if(index == -1)
{
printf("This file has not been added yet\n\n");
return;
}
provider = &(peer_table[index]->addr);
port = peer_table[index]->back_port;
//create a new request packet
req = request_new_packet(path, getFlowID(), back_port, port);
buf = package(req);
// printf("Sending Request Packet: \n");
// printPacket(req);
//Add to Flow Table
flow_add(req->pack, *provider, req->syn, 0, path, NULL, 0, fd, req->window);
//send the first request packet out
//Timeout ask again sitch
// printf("Send Length: %d\n", send_len(req));
if(sendto(back_fd, buf, send_len(req), 0, (struct sockaddr*)provider, sizeof(*provider)) < 0)
printf("Error sending first request packet");
return;
}
char* package(packet* p) // storing in packet in buf
{
char* buf = malloc(sizeof(char)*p->length + sizeof(char)*16);
memcpy(buf, &(p->flags), 1);
memcpy(buf+1, &(p->pack), 1);
memcpy(buf+2, &(p->source_port), 2);
memcpy(buf+4, &(p->dest_port), 2);
memcpy(buf+6, &(p->length), 2);
memcpy(buf+8, &(p->syn), 2);
memcpy(buf+10, &(p->ack), 2);
memcpy(buf+12, &(p->window), 2);
memcpy(buf+14, &(p->rtt), 2);
memcpy(buf+16, p->data, p->length);
return buf;
}
packet* unwrap(char* buf)
{
packet* p = (packet *)malloc(sizeof(packet));
p->flags = buf[0];
p->pack = buf[1];
memcpy(&(p->source_port), buf+2, 2);
memcpy(&(p->dest_port), buf+4, 2);
memcpy(&(p->length), buf+6, 2);
memcpy(&(p->syn), buf+8, 2);
memcpy(&(p->ack), buf+10, 2);
memcpy(&(p->window), buf+12, 2);
memcpy(&(p->rtt), buf+14, 2);
p->data = malloc(sizeof(char) * p->length);
memcpy(p->data, buf+16, p->length);
return p;
}
packet* request_new_packet(char* path, char flowID, uint16_t source_port, uint16_t dest_port)
{
packet* p = (packet*)malloc(sizeof(packet));
p->flags = 0x04; //0x0100 Syn, no Ack, no Fin
p->pack = flowID;
p->source_port = source_port;
p->dest_port = dest_port;
p->length = strlen(path);
p->syn = 0;
p->ack = 67; //NOT IMPORTANT
p->data = path;
p->window = window_g;
return p;
}
static int v;
packet* get_syn_ack(char flowID, uint16_t dest_port, uint16_t ack, char* data)
{
clock_t t = clock();
clock_t t1 = t/CLOCKS_PER_SEC;
v = t1;
packet* p = (packet*)malloc(sizeof(packet));
p->flags = 0x0c; //0x1100 Syn, Ack, no Fin
p->pack = flowID;
p->source_port = back_port;
p->dest_port = dest_port;
p->ack = ack + 1;
p->syn = 0;
p->length = strlen(data);
p->data = data;
p->rtt = t; // base_time for rtt
return p;
}
static int rtt_val;
packet* get_ack(packet* p, New_flow* nf)
{
static int rtt;
// clock_t t = clock();
p->rtt = p->rtt/CLOCKS_PER_SEC;
packet* g = (packet*)malloc(sizeof(packet));
g->rtt = (p->rtt - v);
rtt_val = g->rtt/1e3; // in seconds
g->pack = p->pack;
g->flags = 0x08;
g->source_port = back_port;
g->dest_port = p->source_port;
g->length = 0;
g->syn = nf->last_ack + 1;
g->data = NULL;
if (rtt != 0)
p->rtt = rtt;
return g;
}
/* Time Declarations */
char* weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char* months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
/*
* get_rfc_time - returns a string with an RFC 1123 formatted string with the time in GMT
* input: none
* output: string - RFC formatted time
*/
char* get_rfc_time() {
char time_string[30];
time_t t;
struct tm* tm;
time(&t);
tm = gmtime(&t);
sprintf(time_string, "%s, %d %s %d %d:%d:%d GMT", weekdays[tm->tm_wday], tm->tm_mday, months[tm->tm_mon],
(tm->tm_year + 1900), tm->tm_hour, tm->tm_min, tm->tm_sec);
return (char*)time_string;
}
static int bps = 0;
/*
* parse - parse function takes care of the parsing of the requet sent by the
* client. It returns a structure url_info, which contains different parts of
* request. It returns a PARSE_ERROR if the request was malformed. For example:
* input : char *buf - pointer to request string.
* output : url_info parse_url- a structure containing different parsed values.
* .method - "GET"
* .url - "www.example.com:8080/home.html"
* .host - "www.example.com"
* .path - "Users/Sanika/Desktop/cat.jpg"
* .port - "8080"
* .version - 0
* .extension - jpg/txt etc.
* .parse_result - PARSE_CORRECT(PARSE_ERROR if Error)
*/