-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDistanceVectorRouting.cpp
More file actions
1013 lines (892 loc) · 23.9 KB
/
DistanceVectorRouting.cpp
File metadata and controls
1013 lines (892 loc) · 23.9 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
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
/*
* DistanceVectorRouting.cpp
*
* Created on: Nov 1, 2013
* Author: subhranil
*/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<sys/stat.h>
#include<arpa/inet.h>
#include<cstdlib>
#include<netinet/in.h>
#include <ifaddrs.h>
#include<cstring>
#include<unistd.h>
#include<fcntl.h>
#include<pthread.h>
#include<errno.h>
#include <netdb.h>
#include <sys/time.h>
#include<signal.h>
#include<inttypes.h>
#define NODECOUNTMAX 100
#define MAXPACKETSIZE 10000
#define PROMPTVAR "DistanceVectorRouting>>"
#define MAXRETRY 3
int gTotalNumberOfNodes=0;
int gNumberOfNeighbours=0;
char gTopologyFileName[1024]="";
int gUpdateInterval=0;
char gLocalIP[50]="";
int16_t gLocalPort=0;
int32_t gLocalServerId=0;
struct timeval timeout;
int gUpdateCount=0;
typedef struct Node
{
int16_t id;
int32_t IP;
int16_t port;
int16_t nextHopId;
int16_t cost;
bool isActiveNeighbour;
int updateCount;
} Node;
//typedef struct TimeNode
//{
// struct timeval lastTime;
// int interval;
//} TimeNode;
typedef struct sockaddr_in SocketAddressInfo;
//typedef struct DistanceVectorNode
//{
// Node dest;
//
//} DistanceVectorNode;
//Node nodeList[NODECOUNTMAX];// also the routing table
Node costList[NODECOUNTMAX]={{-1,0,-1,-1,-1}}; // weight of the connecting edge. contains entries only for the neighbours of this host
Node distanceVector[NODECOUNTMAX];//[NODECOUNTMAX];// this 2d matrix and grow to the N*N size in the worst case.
int16_t costMatrix[NODECOUNTMAX][NODECOUNTMAX]={(int16_t)-1};
// For the first two lists index 0 must be self and hence the costList[0]=0;
// cost list (id,cost) for each f the neighbours.
// Distance vector is a map betwen (id,minCost) with size gTotalNumberOfNodes( must be allowed room to expand)
// distance vect
void init()
{
for(int i=0;i<NODECOUNTMAX;i++)
{
costList[i].IP=-1;
costList[i].port=-1;
costList[i].id=-1;
costList[i].cost=-1;
costList[i].nextHopId=-1;
costList[i].updateCount=MAXRETRY;
costList[i].isActiveNeighbour=false;
for(int j=0;j<NODECOUNTMAX;j++)
{
costMatrix[i][j]=-1;
}
}
memcpy(distanceVector,costList,sizeof(Node)*NODECOUNTMAX);
}
// This function returns the IP of the host
char* getLocalIP()
{
SocketAddressInfo *socketAddress; // this structure basically stores info about the socket address
socketAddress=(SocketAddressInfo*)malloc(sizeof(SocketAddressInfo));
socketAddress->sin_family=AF_INET;
socketAddress->sin_port=htons(53);
inet_pton(socketAddress->sin_family,"8.8.8.8",&(socketAddress->sin_addr));
//create a socket for connecting to server
int socketId=socket(AF_INET,SOCK_DGRAM,0);
if(socketId==-1)
{
printf("Error in creating socket");
return "";
}
int retConnect=connect(socketId,(struct sockaddr*)socketAddress,sizeof(SocketAddressInfo));
if(retConnect<0)
{
printf("connection with peer failed with error : %d",retConnect);
return "";
}
SocketAddressInfo localAddressInfo;
socklen_t len=sizeof(localAddressInfo);
getsockname(socketId,(struct sockaddr*)&localAddressInfo,&len);
char buffer[32];
inet_ntop(AF_INET,&localAddressInfo.sin_addr,buffer,sizeof(buffer));
return buffer;
}
int indexOfNodeWithId(int32_t id)
{
for(int i=0;i<gTotalNumberOfNodes;i++)
{
if(costList[i].id==id)
return i;
}
return -1;
}
int indexOfNodeWithIP(int32_t ip,int16_t port)
{
for(int i=0;i<gTotalNumberOfNodes;i++)
{
if(costList[i].IP==ip&&costList[i].port==port)
return i;
}
return -1;
}
//The function below creates the packet data from the distance vector of the host
char *createMessageFormatFromVector(int *numBytes)
{
char *mssg=(char*)malloc(12*gTotalNumberOfNodes+8);
memcpy(mssg+*numBytes,(int16_t*)&gTotalNumberOfNodes,2);
*numBytes+=2;
//memcpy(mssg+numberOfBytesWritten,(int16_t*))
memcpy(mssg+*numBytes,&costList[0].port,2);
*numBytes+=2;
// int32_t tempIp=0;
// inet_pton(AF_INET,,&tempIp);
memcpy(mssg+*numBytes,&costList[0].IP,4);
*numBytes+=4;
for(int i=0;i<gTotalNumberOfNodes;i++)
{
//int32_t tempIp=0;
//inet_pton(AF_INET,,&tempIp);
memcpy(mssg+*numBytes,&distanceVector[i].IP,4);
*numBytes+=4;
memcpy(mssg+*numBytes,&distanceVector[i].port,2);
*numBytes+=4;
memcpy(mssg+*numBytes,&distanceVector[i].id,2);
*numBytes+=2;
// memcpy(mssg+*numBytes,&distanceVector[i].nextHopId,2);
// *numBytes+=2;
memcpy(mssg+*numBytes,&distanceVector[i].cost,2);
*numBytes+=2;
}
return mssg;
}
//After receiving a message, the following function is called to parse the message and setup the cost matrix accordingly
Node* convertMessageIntoDistanceVector(char* message,int32_t *baseId)
{
int16_t tempVar;
int32_t temp;
int numberOfBytesRead=0;
memcpy(&tempVar,message+numberOfBytesRead,2);
numberOfBytesRead+=2;
Node* tempVector=(Node*)malloc(sizeof(Node)*tempVar); // creat the temp array
//gTotalNumberOfNodes=(int)tempVar; //change .. check this line
memcpy(&tempVar,message+numberOfBytesRead,2);
numberOfBytesRead+=2;
memcpy(&temp,message+numberOfBytesRead,4);
numberOfBytesRead+=4;
// char IP[50];
// inet_ntop(AF_INET,&temp,IP,50);
int baseindex=indexOfNodeWithIP(temp,tempVar);
if(!costList[baseindex].isActiveNeighbour)
return NULL;
/*//change .. what happens if new
neighbour(add to costlist)
has to be a new neighbour --> add to cost List (non neighbours dont send Dv's)
can be a newnon neighbour 0--> add directly to distance vector[0];
*/
//bool newNeighbour,newNode;
if(baseindex<0) // should never happen .this means that the node was not there in the topology file
{
//printf("%hd-->%d\n",tempVar,temp);
printf("Invalid message received.\n");
fflush(stdout);
return NULL;
}
else
{
*baseId=costList[baseindex].id;
for(int j=0;j<gTotalNumberOfNodes;j++)
{
memcpy(&temp,message+numberOfBytesRead,4);
numberOfBytesRead+=4;
memcpy(&tempVar,message+numberOfBytesRead,2);
numberOfBytesRead+=4; // to handle the null
int index=indexOfNodeWithIP(temp,tempVar);
if(index<0) // this means that the node does not exist in the system .. should never happen.
{
//printf("%hd-->%d\n",tempVar,temp);
printf("Invalid message received.\n");
fflush(stdout);
return NULL;
}
else
{
tempVector[index].IP=temp;
tempVector[index].port=tempVar;
memcpy(&tempVector[index].id,message+numberOfBytesRead,2);
numberOfBytesRead+=2;
// memcpy(&tempVector[index].nextHopId,message+numberOfBytesRead,2);
// numberOfBytesRead+=2;
memcpy(&tempVector[index].cost,message+numberOfBytesRead,2);
numberOfBytesRead+=2;
costList[baseindex].updateCount=MAXRETRY;
costMatrix[baseindex][index]=tempVector[index].cost;
// if(costList[baseindex].updateCount==0)
// costList[baseindex].updateCount=MAXRETRY;
// else
// costList[baseindex].updateCount++;
// // check if this is a new neighbour
//The following code is not needed since the update command will be executed on both ends of the link
// if(index==0&&costList[0].id==tempVector[index].nextHopId)
// {
// if(costList[baseindex].cost==-1)
// {
// printf("New neighbour added. id:%hd",costList[baseindex].id);
// gNumberOfNeighbours++;
// }
// else if(costList[baseindex].cost!=tempVector[index].cost)
// printf("Edge weight between %hd updated",costList[baseindex].id);
//
// costList[baseindex].cost=tempVector[index].cost;
// //printf("edge weight updated\n");
// //gNumberOfNeighbours++;
// }
}
}
gUpdateCount++;
printf("RECEIVED A MESSAGE FROM SERVER %hd\n",*baseId);
fflush(stdout);
}
return tempVector;
}
// Flushes the distance vector of the host.
void resetDistanceVector()
{
for (int i=1;i<gTotalNumberOfNodes;i++)
{
distanceVector[i].cost=-1;
costMatrix[0][i]=-1;
}
}
//Implements the Bellman Ford Algo to update the distance vector of the host. The isinit parameter toggles initialization and update cases.
void updateSelfDistanceVectorWithVector(bool isInit)
{
//int index=indexOfNodeWithId(id);
//int32_t baseCost=costList[index].cost; //can never be < 0
//costMatrix[0][0]=0;
for(int i=1; i<gTotalNumberOfNodes;i++) // we dont need update to self route
{
//bellman ford implementation
if(isInit) // initialization case
{
distanceVector[i].cost=costList[i].cost;
distanceVector[i].nextHopId=costList[i].id;
costMatrix[0][i]=costList[i].cost;
//costMatrix[i][i]=0;
continue;
}
//int16_t minCost=costMatrix[0][i], minHop=distanceVector[i].nextHopId;
int16_t minCost=-1,minHop=-1;
// loop over all neighbours
for(int j=1;j<gTotalNumberOfNodes;j++)
{
int16_t baseCost=costList[j].cost; //can never be < 0
if(baseCost<0||!costList[j].isActiveNeighbour)// not a neighnour
continue;
//int16_t value=baseCost+costMatrix[j][i].cost;
//printf("i:%d-->j%d-->minCost:%hd---->costMatrix[j][i]:%hd-->baseCost:%hd\n",i,j,minCost,costMatrix[j][i],baseCost);
if(minCost==(int16_t)-1&&costMatrix[j][i]==(int16_t)-1) // you cant help me :( // neither your nor the peer can reach
{
if(i==j) // this new condition is being added since we now have something to reset
{
minCost=baseCost;
minHop=costList[i].id;
}
}
else if(minCost==(int16_t)-1) // you cannot reach but your peer can reach
{
minCost=baseCost+costMatrix[j][i];
minHop=costList[j].id;
}
else if(costMatrix[j][i]!=(int16_t)-1)
{
int16_t value=baseCost+costMatrix[j][i];
if(value<minCost)
{
minCost=value;
minHop=costList[j].id;
}
}
}// finished looping over neighbours
if(minCost>0)
{
distanceVector[i].cost=minCost;
distanceVector[i].nextHopId=minHop;
costMatrix[0][i]=minCost;
}
/*int32_t value;=(costList[i].cost==(int16_t)-1||v[i].cost==(int16_t)-1)?-1:(costList[i].cost+v[i].cost) ;
if(distanceVector[i].cost==-1)
distanceVector[i].cost=value;
else if(value<=distanceVector[i].cost)
{
distanceVector[i].cost=value;
distanceVector[i].nextHopId=id;
}*/
}
}
// Sends out the distance vector to all active neighbours. Also checks if any of the neighbours have died.(3 misses case)
void broadcastDistanceVectorToNeighbours(int isAuto)
{
int numberOfBytesWritten=0;
char* message=createMessageFormatFromVector(&numberOfBytesWritten);
// structure created successfully for sending.
// send to all the neighbours
for(int i=1;i<gTotalNumberOfNodes;i++)
{
if(costList[i].isActiveNeighbour)
{
// check for corresponding timer value
// struct timeval currTime;
// gettimeofday(&currTime,NULL);
if(isAuto)
costList[i].updateCount--;
if(costList[i].updateCount==0)
//if(timerList[i].lastTime.tv_sec!=0 && currTime.tv_sec-timerList[i].lastTime.tv_sec>=(3*gUpdateInterval))
{
costList[i].cost=-1;
// distanceVector[i].cost=-1;
// costMatrix[0][i]=-1;
resetDistanceVector();
updateSelfDistanceVectorWithVector(false);
gNumberOfNeighbours--;
printf("Neighbour with id:%hd has gone down.\n",costList[i].id);
costList[i].updateCount=-1;
continue;
}
//int sockId=createUDPSocket(costList[i].IP,costList[i].port);
SocketAddressInfo *socketAddress; // this structure basically stores info about the socket address
socketAddress=(SocketAddressInfo*)malloc(sizeof(SocketAddressInfo));
socketAddress->sin_family=AF_INET;
socketAddress->sin_port=htons(costList[i].port);
socketAddress->sin_addr.s_addr=costList[i].IP;
//inet_pton(socketAddress->sin_family,ip,&(socketAddress->sin_addr));
//create a socket for connecting to server
int socketId=socket(AF_INET,SOCK_DGRAM,0);
int slen=sizeof(SocketAddressInfo);
int retV=sendto(socketId,message,numberOfBytesWritten,0,(struct sockaddr*)socketAddress,(socklen_t)slen);
//printf("err:%d-->ret:%d",errno,retV);
fflush(stdout);
close(socketId);
}
}
free(message);
}
int compare(const void* a, const void* b)
{
return (int)(((Node*)a)->id-((Node*)b)->id);
}
// prints the routing table in the specified format.
void displayRoutingTable()
{
//printf("gTotalNumberOfNodes:%hd",gTotalNumberOfNodes);
Node* tempDV=(Node*)malloc(sizeof(Node)*gTotalNumberOfNodes);
memcpy(tempDV,distanceVector,sizeof(Node)*gTotalNumberOfNodes);
qsort(tempDV,gTotalNumberOfNodes,sizeof(Node),&compare);
for(int i=0;i<gTotalNumberOfNodes;i++)
{
char cost[8];
if(tempDV[i].cost==-1)
strcpy(cost,"inf");
else
sprintf(cost,"%hd",tempDV[i].cost);
printf("%hd\t%hd\t%s\n",tempDV[i].id,tempDV[i].nextHopId,cost);
}
fflush(stdout);
}
void updateTimer(int id)
{
}
// Assumption : There will be less than 10 nodes in the setup.
int parseStringAndGetLocaLServerId(char *str)
{
int i=0;
int index=strlen(str)-2;
while(index>0)
{
if(str[index]=='\n')
break;
index--;
}
//char *ptr=strrchr(str,'\n');
//ptr++;
return (str[index+1]-'0');
}
// Read the topology file and setup the costList[] and distanceVector[] arrays.
void initialiseLists()
{
try
{
int fd=open(gTopologyFileName,O_RDONLY);
//printf("%d",errno);
//cgange remove hardbound by calc filesize by stat
char buffer[5000];
read(fd,buffer,5000);
int baseId=parseStringAndGetLocaLServerId(buffer);
char line[200],*saveptr1,*saveptr2;
//strcpy(line,strtok_r(buffer,"\n",&saveptr1));
strcpy(line,strtok(buffer,"\n"));
gTotalNumberOfNodes=atoi(line);
//strcpy(line,strtok_r(NULL,"\n",&saveptr1));
strcpy(line,strtok(NULL,"\n"));
gNumberOfNeighbours=atoi(line);
for(int i=1,linecount=0;linecount<gTotalNumberOfNodes;linecount++)
{
//strcpy(line,strtok_r(NULL,"\n"));
strcpy(line,strtok(NULL,"\n"));
int16_t id,port;
char IP[50];
/*char* tok;
tok=strtok_r(line," ",&saveptr2);
sscanf(tok,"%i",&id);
//id=atoi(tok);
tok=strtok_r(NULL," ",&saveptr2);
strcpy(IP,tok);
tok=strtok_r(NULL," ",&saveptr2);
sscanf(tok,"%i",&port);*/
sscanf(line,"%hd%s%hd",&id,IP,&port);
struct in_addr IP_Net;
inet_aton(IP,&IP_Net);
//port=atoi(tok);
//if(id==(int16_t)baseId) // add at 0th index
if(strcmp(gLocalIP,IP)==0)
{
//strcpy(costList[0].IP,IP);
costList[0].IP=IP_Net.s_addr;
costList[0].id=id;
costList[0].nextHopId=-1;
costList[0].port=port;
gLocalPort=(int16_t)port;
costList[0].cost=(int16_t)0;
//testing
//strcpy(gLocalIP,"");
distanceVector[0].IP=IP_Net.s_addr;
distanceVector[0].id=id;
distanceVector[0].nextHopId=0;
distanceVector[0].port=port;
distanceVector[0].cost=0;
costMatrix[0][0]=0;
}
else
{
//strcpy(costList[i].IP,IP);
costList[i].IP=IP_Net.s_addr;
costList[i].id=id;
costList[i].nextHopId=-1;
costList[i].port=port;
distanceVector[i].IP=IP_Net.s_addr;
distanceVector[i].id=id;
distanceVector[i].port=port;
//distanceVector[i].nextHopId=id;
i++;
}
}
//saveptr2=NULL;
// add the costs to the costList array
for(int i=0;i<gNumberOfNeighbours;i++)
{
//change handle source !=localid case
//strcpy(line,strtok_r(NULL,"\n",&saveptr1));
strcpy(line,strtok(NULL,"\n"));
int32_t source,dest,cost;
/*char IP[50] ;
char* tok;
tok =strtok_r(line," ",&saveptr2);
source=atoi(tok);
tok=strtok_r(NULL," ",&saveptr2);
//dest=atoi(tok);
sscanf(tok,"%i",&dest);
tok=strtok_r(NULL," ",&saveptr2);
sscanf(tok,"%i",&cost);*/
sscanf(line,"%hd%hd%hd",&source, &dest,&cost);
//cost=atoi(tok);
int index=indexOfNodeWithId(dest);
if(index<0) // should never happen
continue;
costList[index].cost= cost;
costList[index].isActiveNeighbour=true;
}
}
catch(...)
{
printf("Invalid topology file. Exiting...\n");
fflush(stdout);
exit(0);
}
updateSelfDistanceVectorWithVector(true);
}
// Parses the command line arguments and sets up the global variables accordingly.
void parseShellArguments(int argc,char *argv[])
{
int index=1;
try
{
while(index<argc)
{
if(strcmp(argv[index],"-t")==0)
{
strcpy(gTopologyFileName,argv[++index]);
index++;
}
else if(strcmp(argv[index],"-i")==0)
{
gUpdateInterval=atoi(argv[++index]);
index++;
}
else
{
printf("Unknown option");
exit(0);
}
}
}
catch(...)
{
printf("Invalid usage\n");
exit(0);
}
}
void resetToPrompt()
{
printf(PROMPTVAR);
fflush(stdout);
}
// Handles the queries and exchange commands from the user.
void displayShell()
{
char inputLine[1024];
//fflush(stdout);
fgets(inputLine,1024,stdin);
char *command;//[30];
command=strtok(inputLine,"\n");
command=strtok(inputLine," ");
// strcpy(command,strtok(inputLine,"\n"));
//strcpy(command,strtok(inputLine," "));
//printf("command:%s\n",command);
if(!command)
{
resetToPrompt();
return;
}
if(!strcmp(command,"step"))
{
broadcastDistanceVectorToNeighbours(false);
printf("step SUCCESS\n");
timeout.tv_sec=gUpdateInterval;
timeout.tv_usec=0;
resetToPrompt();
}
else if(!strcmp(command,"MYIP"))
{
printf("%s\n",gLocalIP);
resetToPrompt();
}
else if(!strcmp(command,"display"))
{
printf("display: SUCCESS\n");
displayRoutingTable();
resetToPrompt();
}
else if(!strcmp(command,"packets"))
{
printf("packets: SUCCESS\n");
printf("Number of distance vector updates received:%d\n",gUpdateCount);
gUpdateCount=0;
resetToPrompt();
}
else if(!strcmp(command,"crash"))
{
printf("Server crashed\n");
while(1);
}
else if(!strcmp(command,"update"))
{
try {
int16_t id1=0,id2=0,c=0;
char cost[8]="";
char *t=strtok(NULL," ");
if(t==NULL)
{
printf("Invalid usage.\n");
resetToPrompt();
return;
}
sscanf(t,"%hd",&id1);
t=strtok(NULL," ");
if(t==NULL)
{
printf("Invalid usage.\n");
resetToPrompt();
return;
}
sscanf(t,"%hd",&id2);
t=strtok(NULL," ");
if(t==NULL)
{
printf("Invalid usage.\n");
resetToPrompt();
return;
}
sscanf(t,"%s",&cost);
if(id1<=0||id2<=0||strlen(cost)==0)
{
printf("update: FAILURE: Invalid or empty parameters.\n");
resetToPrompt();
return;
}
if(strcmp(cost,"inf")==0)
c=-1;
else
sscanf(cost,"%hd",&c);
int i=indexOfNodeWithId(id1);
if(i!=0)
{
printf("update: FAILURE: Server id1 is not the local host.\n");
resetToPrompt();
return;
}
int index=indexOfNodeWithId(id2);
if(index==-1)
{
printf("update: FAILURE: Server id2 is not a valid id.\n");
resetToPrompt();
return;
}
costList[index].cost=c;
costList[index].isActiveNeighbour=true;
costList[index].updateCount=MAXRETRY;
//printf("setting costlist[%d]:%hd",index,c);
// distanceVector[index].cost=-1;
// costMatrix[0][index]=-1;
resetDistanceVector();
updateSelfDistanceVectorWithVector(false);
printf("update: SUCCESS\n");
resetToPrompt();
}
catch(...)
{
printf("Invalid usage.\n");
resetToPrompt();
return;
}
}
else if(!strcmp(command,"disable"))
{
try {
int16_t id=0;
char* t=strtok(NULL," ");
if(t==NULL)
{
printf("Invalid usage.\n");
resetToPrompt();
return;
}
sscanf(t,"%hd",&id);
int index=indexOfNodeWithId(id);
if(index<0||costList[index].cost==-1)
{
printf("disable: FAILED since server id is not a neighbour\n");
resetToPrompt();
}
else
{
costList[index].cost=-1;
// distanceVector[index].cost=-1;
// costMatrix[0][index]=-1;
resetDistanceVector();
costList[index].isActiveNeighbour=false;
updateSelfDistanceVectorWithVector(false);
printf("disable: SUCCESS\n");
resetToPrompt();
}
}
catch(...)
{
printf("Invalid usage.\n");
resetToPrompt();
return;
}
}
/*else if(!strcmp(command,"CONNECT"))
{
if(isServer)
{
printf("Invalid Command\n");
resetToPrompt();
return;
}
char *peerIP;//[40];
//strcpy(tempServerIP,strtok(NULL," "));
peerIP=strtok(NULL," ");
char *portStr;//[5];
portStr=strtok(NULL," ");
//if(!strlen(peerIP)||strlen(portStr))
if(!peerIP||!portStr|| !strlen(peerIP)||!strlen(portStr))
printf("Invalid Command Format.\n");
else
{
// check for the validity of the connection
char *fIP=(char*)malloc(40);
int fPort;
char *errString=(char*)malloc(500);
bool status=completeRequestAndCheckValidity(peerIP,portStr,&fPort,&fIP,&errString);
if(!status)
{
printf("%s",errString);
free(errString);
resetToPrompt();
return;
}
printf("Trying to connect to %s at port %d\n",fIP,fPort);
TCPSocketConnection *peerConnectConn=new TCPSocketConnection(fIP,fPort,0);
char connectReqMessage[1024];
sprintf(connectReqMessage,"ConnectRequest\n%s\n%d\n%s\n",hostIP,localPort,localHostName);
peerConnectConn->sendData(connectReqMessage);
addConnection(peerConnectConn);
free(fIP);
//resetToPrompt();
}
}
else if(!strcmp(command,"LIST"))
{
listConnections();
resetToPrompt();
}
else if(!strcmp(command,"DOWNLOAD"))
{
if(isServer)
{
printf("Invalid Command\n");
resetToPrompt();
return;
}
char *fileName=strtok(NULL," ");
char *chunkSizeStr=strtok(NULL," ");
if(!fileName||!chunkSizeStr|| !strlen(fileName)||!strlen(chunkSizeStr))
printf("Invalid Command Format.\n");
else
{
downloadActive=true;
lastWrittenByte=0;
downloadFile(fileName,atoi(chunkSizeStr),-1,NULL);
}
printf("Downloading %s...\n",fileName);
resetToPrompt();
}
else if(!strcmp(command,"TERMINATE"))
{
char *idStr;
idStr=strtok(NULL," ");
if(strlen(idStr)==0)
printf("Invalid Usage.\n");
else
{
if(terminateConnection(atoi(idStr),false))
printf("Connection Terminated.\n");
else
printf("Operation not permitted.\n");
}
resetToPrompt();
}
else if(!strcmp(command,"EXIT"))
{
terminateAllConnections();
//printf("\n*** Transferred %lld bytes in %lld us\n",totalBytesTransferred,totalTimeInMicroSecs);
exit(0);
}
else if(!strcmp(command,"SIP"))
{
displayServerIPList();
resetToPrompt();
}
else if(!strcmp(command,"CREATOR"))
{
printf("Name:Subhranil Banerjee\nUBITName:subhrani\nEmail:subhrani@buffalo.edu\n");
resetToPrompt();
}
*/
else
{
printf("Command not found.\n");
resetToPrompt();
}
//}
}
// Receives data on the UDP socket. Also contains the implementation of the timer.
void setupReceiver()
{
//int sendFileDesc,recvFileDesc;
/* observedSockets file descriptor list and max value */
fd_set observedSockets;
int fdmax;
int mainSocket;
// defining the address for the main socket
SocketAddressInfo *socketAddress; // this structure basically stores info about the socket address
socketAddress=(SocketAddressInfo*)malloc(sizeof(SocketAddressInfo));
socketAddress->sin_family=AF_INET;
inet_pton(socketAddress->sin_family,gLocalIP,&(socketAddress->sin_addr));
socketAddress->sin_port=htons(gLocalPort);
//create a socket for accepting incoming connections
mainSocket=socket(AF_INET,SOCK_DGRAM,IPPROTO_UDP);
if(mainSocket==-1)
{
printf("Error in creating socket");
exit(0);
}
// bind the socket to the addressinfo
if(bind(mainSocket,(struct sockaddr*)socketAddress,sizeof(SocketAddressInfo))==-1)
{
printf("Error in binding socket IP address");
exit(0);
}
// Begin code for socket listening
/* clear the observedSockets and temp sets */
FD_ZERO(&observedSockets);
timeout.tv_sec=gUpdateInterval;
timeout.tv_usec=0;
// run loop for observing sockets
while(true)
{
/* add the listener to the observedSockets set */
FD_SET(mainSocket, &observedSockets);
FD_SET(0,&observedSockets);
/* keep track of the biggest file descriptor */
fdmax = mainSocket ; /* so far, it's this one*/
int activity=select(fdmax+1,&observedSockets,NULL,NULL,&timeout); // blocking all until there is some activity on any of the sockets
if(activity<0)
{
if(errno==EINTR)
continue;
printf("error in select");
return ;
}
else if(activity==0)
{
broadcastDistanceVectorToNeighbours(true);
timeout.tv_sec=gUpdateInterval;
timeout.tv_usec=0;
}
else {
if(FD_ISSET(mainSocket,&observedSockets)) // .. there has been activity on the mainSocket. thus there s a new connection that needs to be added
{
char incomingMessage[MAXPACKETSIZE];
int numberOfBytesRecvd=0;
numberOfBytesRecvd=recv(mainSocket,&incomingMessage,MAXPACKETSIZE-1,0);
//Handle the data
int32_t id;
Node* incomingVector=convertMessageIntoDistanceVector(incomingMessage,&id);
//resetToPrompt();
updateSelfDistanceVectorWithVector(false);
}
else if(FD_ISSET(0,&observedSockets))
{
displayShell();
}
}
}
}