-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodr.c
1868 lines (1577 loc) · 53.9 KB
/
odr.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 "unp.h"
#include<linux/if_packet.h>
#include<linux/if_ether.h>
#include<linux/if_arp.h>
#define IF_NAME 16 /* same as IFNAMSIZ in <net/if.h> */
#define IF_HADDR 6 /* same as IFHWADDRLEN in <net/if.h> */
#define IP_ALIAS 1 /* hwa_addr is an alias */
#define ODR_PATH "/tmp/ndixit_odr"
#define SERVER_PATH "/tmp/ndixit_server"
#define SERVER_PORT 51838
#define CLIENT_PATH "/tmp/ndixit_client_XXXXXX"
#define PROTOCOL_NO 51838
#define PF_PACKET_HEADER 14
#define ETH0 "eth0"
#define LOOPBACK "lo"
int broadcast_id = 1;
char client_msg_recv[100];
char server_msg_recv[100];
int ephemeral_port = 1;
struct hwa_info {
char if_name[IF_NAME]; /* interface name, null terminated */
char if_haddr[IF_HADDR]; /* hardware address */
int if_index; /* interface index */
short ip_alias; /* 1 if hwa_addr is an alias IP address */
struct sockaddr *ip_addr; /* IP address */
struct hwa_info *hwa_next; /* next of these structures */
};
typedef struct route_table_ {
char mac_nh[IF_HADDR];
int hopcount;
uint32_t timestamp;
int if_index_nh;
long cano_ip_dest;
int status;
int broadcast_id;
}route_table;
typedef struct rreq_info_ {
long server_ip;
int port_no;
int route_rediscovery;
int rreq_sent_already;
}rreq_info;
typedef struct rrep_info_{
long destination_ip;
long source_ip;
int source_port_no;
int destination_port_no;
int route_rediscovery;
int broadcast_id_recovered;
int hop_count;
}rrep_info;
typedef struct print_hdr_details_{
int type;
char source_ip[100];
int source_port;
char dest_ip[100];
int dest_port;
int hop_count;
char msg[100];
}print_hdr_details;
typedef struct packet_info_{
long destination_ip;
long source_ip;
int source_port_no;
int destination_port_no;
int route_rediscovery;
int broadcast_id_recovered;
int hop_count;
}packet_info;
typedef struct port_path_ {
int port_num;
char sun_path[25];
int status;
}port_path;
typedef enum aodv_msg_type_ {
PF_PACKET_RREQ,
PF_PACKET_RREP,
PF_PACKET_APP_PAYLOAD,
PF_PACKET_NONE
}aodv_msg_type;
typedef enum route_table_status_ {
ROUTE_VALID,
ROUTE_INVALID,
ROUTE_NONE
}route_table_status;
typedef enum staleness_entry__ {
A_STALE_ENTRY,
NOT_A_STALE_ENTRY,
NONE_STALE
}staleness_entry;
typedef enum payload_destination_info_ {
DESTINATION_SERVER,
DESTINATION_CLIENT,
DESTINATION_NONE
}payload_destination_info;
/* function prototypes */
struct hwa_info *get_hw_addrs();
struct hwa_info *Get_hw_addrs();
void free_hwa_info(struct hwa_info *);
/* Count digits in the number */
int
count_digits(int number) {
int count = 0;
while(number != 0) {
number = number/10;
count++;
}
return count;
}
int
count_digits_long(long number) {
int count = 0;
while(number != 0) {
number = number/10;
count++;
}
return count;
}
int
fill_packet_data(char *packet,
int data,
int data_size) {
int no_digit = count_digits(data);
int iter;
iter = data_size - 1;
for(; iter >= 0; iter --) {
packet[iter] = data%10 + 48;
data = data/10;
}
for(iter = 0; iter < data_size - no_digit; iter ++) {
packet[iter] = 48;
}
return 0;
}
int
fill_packet_data_long(char *packet,
long data,
int data_size) {
int no_digit = count_digits_long(data);
int iter;
iter = data_size - 1;
for(; iter >= 0; iter --) {
packet[iter] = data%10 + 48;
data = data/10;
}
for(iter = 0; iter < data_size - no_digit; iter ++) {
packet[iter] = 48;
}
return 0;
}
uint32_t
get_timestamp_current_seconds(){
uint32_t ts;
struct timeval tv;
Gettimeofday(&tv, NULL);
ts = (tv.tv_sec*1000) + (tv. tv_usec/1000);
return (ts);
}
/*Function Reterives Details from Packet Header Recieved*/
int
get_packet_data(char *packet,
int data_size,
int *ret) {
char number[30];
int iter;
for (iter = 0; iter < data_size; iter++) {
number[iter] = packet[iter];
}
number[iter] = '\0';
*ret = atoi(number);
return 0;
}
/*Function Reterives Details from Packet Header Recieved*/
int
get_packet_data_long(char *packet,
int data_size,
long *ret) {
char number[30];
int iter;
for (iter = 0; iter < data_size; iter++) {
number[iter] = packet[iter];
}
number[iter] = '\0';
*ret = atol(number);
return 0;
}
struct hwa_info *
get_hw_addrs() {
struct hwa_info *hwa, *hwahead, **hwapnext;
int sockfd, len, lastlen, alias, nInterfaces, i;
char *buf, lastname[IF_NAME], *cptr;
struct ifconf ifc;
struct ifreq *ifr, *item, ifrcopy;
struct sockaddr *sinptr;
sockfd = Socket(AF_INET, SOCK_DGRAM, 0);
lastlen = 0;
len = 100 * sizeof(struct ifreq); /* initial buffer size guess */
for ( ; ; ) {
buf = (char*) Malloc(len);
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
if (errno != EINVAL || lastlen != 0)
err_sys("ioctl error");
} else {
if (ifc.ifc_len == lastlen)
break; /* success, len has not changed */
lastlen = ifc.ifc_len;
}
len += 10 * sizeof(struct ifreq); /* increment */
free(buf);
}
hwahead = NULL;
hwapnext = &hwahead;
lastname[0] = 0;
ifr = ifc.ifc_req;
nInterfaces = ifc.ifc_len / sizeof(struct ifreq);
for(i = 0; i < nInterfaces; i++) {
item = &ifr[i];
alias = 0;
hwa = (struct hwa_info *) Calloc(1, sizeof(struct hwa_info));
memcpy(hwa->if_name, item->ifr_name, IF_NAME); /* interface name */
hwa->if_name[IF_NAME-1] = '\0';
/* start to check if alias address */
if ( (cptr = (char *) strchr(item->ifr_name, ':')) != NULL)
*cptr = 0; /* replace colon will null */
if (strncmp(lastname, item->ifr_name, IF_NAME) == 0) {
alias = IP_ALIAS;
}
memcpy(lastname, item->ifr_name, IF_NAME);
ifrcopy = *item;
*hwapnext = hwa; /* prev points to this new one */
hwapnext = &hwa->hwa_next; /* pointer to next one goes here */
hwa->ip_alias = alias; /* alias IP address flag: 0 if no; 1 if yes */
sinptr = &item->ifr_addr;
hwa->ip_addr = (struct sockaddr *) Calloc(1, sizeof(struct sockaddr));
memcpy(hwa->ip_addr, sinptr, sizeof(struct sockaddr)); /* IP address */
if (ioctl(sockfd, SIOCGIFHWADDR, &ifrcopy) < 0)
perror("SIOCGIFHWADDR"); /* get hw address */
memcpy(hwa->if_haddr, ifrcopy.ifr_hwaddr.sa_data, IF_HADDR);
if (ioctl(sockfd, SIOCGIFINDEX, &ifrcopy) < 0)
perror("SIOCGIFINDEX"); /* get interface index */
memcpy(&hwa->if_index, &ifrcopy.ifr_ifindex, sizeof(int));
}
free(buf);
return(hwahead); /* pointer to first structure in linked list */
}
void
free_hwa_info(struct hwa_info *hwahead) {
struct hwa_info *hwa, *hwanext;
for (hwa = hwahead; hwa != NULL; hwa = hwanext) {
free(hwa->ip_addr);
hwanext = hwa->hwa_next; /* can't fetch hwa_next after free() */
free(hwa); /* the hwa_info{} itself */
}
}
/* end free_hwa_info */
struct hwa_info *
Get_hw_addrs() {
struct hwa_info *hwa;
if ( (hwa = get_hw_addrs()) == NULL)
err_quit("get_hw_addrs error");
return(hwa);
}
int
fill_source_mac(char *dest,
int if_index) {
struct hwa_info *hwa;
int status = 1;
hwa = Get_hw_addrs();
for (; hwa != NULL; hwa = hwa->hwa_next) {
if(hwa->if_index == if_index) {
memcpy((void*)dest, (void*)hwa->if_haddr, ETH_ALEN);
status = 0;
break;
}
}
return status;
}
int
check_local_mac(char *if_name) {
int status;
status = strncmp(if_name, LOOPBACK, strlen(LOOPBACK));
if(status == 0){
return(1);
}
status = strncmp(if_name, ETH0, strlen(ETH0));
if(status == 0){
return(1);
}
return(0);
}
void
get_vmname(long ip,
char *dest) {
struct hostent *hesrc = NULL;
struct in_addr ipsrc;
socklen_t len = 0;
//printf("\nIP address in function is %s\n", ip_addr);
//Inet_pton(AF_INET, ip_addr , &ipsrc);
ipsrc.s_addr = ip;
len = sizeof(ipsrc);
hesrc = gethostbyaddr((const char *)&ipsrc, len, AF_INET);
strncpy(dest, hesrc->h_name, strlen(hesrc->h_name));
}
int
get_header_print_details(char *packet_soc_msg){
int status;
char selfhostname[10];
long source_ip, dest_ip;
print_hdr_details hdr_details;
char src_name[100] = {0};
char dest_name[100] = {0};
char *ptr;
int i;
status = gethostname(selfhostname, 10);
if (status < 0) {
printf("\nUnable to get hostname of the machine !!!");
}
status = get_packet_data(packet_soc_msg+PF_PACKET_HEADER, 1, &(hdr_details.type));
if (status != 0) {
printf("\nStatus = %d, Unable to fill type !!! Exiting ...",status);
return -1;
}
status = get_packet_data(packet_soc_msg+PF_PACKET_HEADER+1, 5, &(hdr_details.source_port));
if (status != 0) {
printf("\nStatus = %d, Unable to fill Port Number !!! Exiting ...",status);
return -1;
}
status = get_packet_data_long(packet_soc_msg+PF_PACKET_HEADER+6, 10, &source_ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address of Source !!! Exiting ...",status);
return 0;
}
inet_ntop(AF_INET, &(source_ip), (hdr_details.source_ip), INET_ADDRSTRLEN);
get_vmname(source_ip, src_name);
status = get_packet_data(packet_soc_msg+PF_PACKET_HEADER+16, 5, &(hdr_details.dest_port));
if (status != 0) {
printf("\nStatus = %d, Unable to fill Port Number !!! Exiting ...",status);
return -1;
}
status = get_packet_data_long(packet_soc_msg+PF_PACKET_HEADER+21, 10, &dest_ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address of Destination !!! Exiting ...",status);
return 0;
}
inet_ntop(AF_INET, &(dest_ip), (hdr_details.dest_ip), INET_ADDRSTRLEN);
get_vmname(dest_ip, dest_name);
status = get_packet_data(packet_soc_msg+PF_PACKET_HEADER+31, 5, &(hdr_details.hop_count));
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
if(hdr_details.type == PF_PACKET_RREQ) {
fflush(stdout);
printf("\n=================================================");
printf("\nODR at node %s sending RREQ to ", selfhostname);
ptr = packet_soc_msg;
i = IF_HADDR;
do {
printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
} while (--i > 0);
printf("\nPacket from Source: %s to Destination: %s",src_name, dest_name);
printf("\n=================================================");
} else if(hdr_details.type == PF_PACKET_RREP) {
fflush(stdout);
printf("\n=================================================");
printf("\nODR at node %s sending RREP to ", selfhostname);
ptr = packet_soc_msg;
i = IF_HADDR;
do {
printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
} while (--i > 0);
printf("\nPacket from Source: %s to Destination: %s", dest_name, src_name);
printf("\n=================================================");
} else if(hdr_details.type == PF_PACKET_APP_PAYLOAD) {
memcpy((void*)(hdr_details.msg), (void*)packet_soc_msg+PF_PACKET_HEADER+50, 35);
fflush(stdout);
printf("\n=================================================");
printf("\nODR at node %s sending application PAYLOAD to ", selfhostname);
ptr = packet_soc_msg;
i = IF_HADDR;
do {
printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
} while (--i > 0);
printf("\nPacket from Source: %s to Destination: %s",src_name, dest_name);
printf("\nPayload MSG is - %s",(hdr_details.msg));
printf("\n=================================================");
}
return 0;
}
int
extract_info_from_unix(char *unix_domain_msg,
rreq_info *rreq,
packet_info *packet_msg) {
int status;
char serverip[INET_ADDRSTRLEN];
status = get_packet_data_long(unix_domain_msg+5, 10, &(rreq->server_ip));
if (status != 0) {
printf("\nStatus = %d, Unable to get server IP address !!! Exiting ...",status);
return -1;
}
packet_msg->destination_ip = rreq->server_ip;
inet_ntop(AF_INET, (void *)&(rreq->server_ip), serverip, INET_ADDRSTRLEN);
//printf("\nThe server IP address is %s", serverip);
status = get_packet_data(unix_domain_msg+15, 5, &(rreq->port_no));
if (status != 0) {
printf("\nStatus = %d, Unable to get port number !!! Exiting ...",status);
return -1;
}
packet_msg->destination_port_no=rreq->port_no;
//printf("\nThe server port number is %d", port_no);
status = get_packet_data(unix_domain_msg+20, 1, &(rreq->route_rediscovery));
if (status != 0) {
printf("\nStatus = %d, Unable to get route rediscovery flag !!! Exiting ...",status);
return -1;
}
packet_msg->route_rediscovery=rreq->route_rediscovery;
memcpy((void*)client_msg_recv, (void*)(unix_domain_msg+21), 5);
//printf("\nThe server port number is %d", route_rediscovery);
return 0;
}
int
flood_rreq (int packet_soc_fd,
struct hwa_info *hwa,
char *msg,
int src_ifindex) {
int status, hop_count;
struct sockaddr_ll pack_addr;
struct ethhdr *eh = (struct ethhdr *)msg;
unsigned char dest_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
for(; hwa != NULL; hwa = hwa->hwa_next) {
status = check_local_mac(hwa->if_name);
if(status == 1) {
continue;
}
if (hwa->if_index == src_ifindex) {
continue;
}
pack_addr.sll_family = PF_PACKET;
pack_addr.sll_protocol = htons(PROTOCOL_NO);
pack_addr.sll_ifindex = hwa->if_index;
pack_addr.sll_hatype = ARPHRD_ETHER;
pack_addr.sll_pkttype = PACKET_OTHERHOST;
pack_addr.sll_halen = ETH_ALEN;
pack_addr.sll_addr[0] = 0xFF;
pack_addr.sll_addr[1] = 0xFF;
pack_addr.sll_addr[2] = 0xFF;
pack_addr.sll_addr[3] = 0xFF;
pack_addr.sll_addr[4] = 0xFF;
pack_addr.sll_addr[5] = 0xFF;
pack_addr.sll_addr[6] = 0x00;
pack_addr.sll_addr[7] = 0x00;
memcpy((void*)msg, (void*)dest_mac, ETH_ALEN);
memcpy((void*)(msg+ETH_ALEN), (void*)hwa->if_haddr, ETH_ALEN);
eh->h_proto = PROTOCOL_NO;
status = get_packet_data(msg+PF_PACKET_HEADER+31, 5, &hop_count);
if (status != 0) {
printf("\nStatus = %d, Unable to get route rediscovery flag !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+31, hop_count+1, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
status = sendto(packet_soc_fd, msg, 100, 0, (struct sockaddr *)&pack_addr, sizeof(pack_addr));
if (status <= 0) {
printf("\nError in sending RREQ !!!\n");
return -1;
}
}
return 0;
}
int
generate_packet_soc_msg(packet_info *packet_msg,
long my_ip,
char *msg,
int client_port){
int status;
status = fill_packet_data(msg+PF_PACKET_HEADER+1, client_port, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Port Number !!! Exiting ...",status);
return -1;
}
status = fill_packet_data_long(msg+PF_PACKET_HEADER+6, my_ip, 10);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;
}
/* Fill in the destination ip address and port no */
status = fill_packet_data(msg+PF_PACKET_HEADER+16, packet_msg->destination_port_no, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Port Number of Destination !!! Exiting ...",status);
return -1;
}
status = fill_packet_data_long(msg+PF_PACKET_HEADER+21, packet_msg->destination_ip, 10);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address of Destination !!! Exiting ...",status);
return -1;
}
/* Initialize RREQ hop count to 1 */
status = fill_packet_data(msg+PF_PACKET_HEADER+31, 0, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+36, packet_msg->route_rediscovery, 1);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Rediscovery Flag !!! Exiting ...",status);
return -1;
}
/* Fill in the broad cast Id */
status = fill_packet_data(msg+PF_PACKET_HEADER+37, broadcast_id, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Broadcast Id !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+42, 0, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Payload Bytes !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+47, 0, 3);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Payload Bytes !!! Exiting ...",status);
return -1;
}
msg[64] = '\0';
return 0;
}
int
gen_send_rreq(int packet_soc_fd,
struct hwa_info *hwa,
rreq_info *rreq,
long my_ip,
int client_port) {
char msg[100];
int status;
struct sockaddr_ll pack_addr;
struct ethhdr *eh = (struct ethhdr *)msg;
unsigned char dest_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
for(; hwa != NULL; hwa = hwa->hwa_next) {
status = check_local_mac(hwa->if_name);
if(status == 1) {
continue;
}
pack_addr.sll_family = PF_PACKET;
pack_addr.sll_protocol = htons(PROTOCOL_NO);
pack_addr.sll_ifindex = hwa->if_index;
pack_addr.sll_hatype = ARPHRD_ETHER;
pack_addr.sll_pkttype = PACKET_OTHERHOST;
pack_addr.sll_halen = ETH_ALEN;
pack_addr.sll_addr[0] = 0xFF;
pack_addr.sll_addr[1] = 0xFF;
pack_addr.sll_addr[2] = 0xFF;
pack_addr.sll_addr[3] = 0xFF;
pack_addr.sll_addr[4] = 0xFF;
pack_addr.sll_addr[5] = 0xFF;
pack_addr.sll_addr[6] = 0x00;
pack_addr.sll_addr[7] = 0x00;
memcpy((void*)msg, (void*)dest_mac, ETH_ALEN);
memcpy((void*)(msg+ETH_ALEN), (void*)hwa->if_haddr, ETH_ALEN);
eh->h_proto = PROTOCOL_NO;
status = fill_packet_data(msg+PF_PACKET_HEADER, PF_PACKET_RREQ, 1);
if (status != 0) {
printf("\nStatus = %d, Unable to fill type RREQ !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+1, client_port, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Port Number !!! Exiting ...",status);
return -1;
}
status = fill_packet_data_long(msg+PF_PACKET_HEADER+6, my_ip, 10);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;
}
/* Fill in the destination ip address and port no */
status = fill_packet_data(msg+PF_PACKET_HEADER+16, rreq->port_no, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Port Number of Destination !!! Exiting ...",status);
return -1;
}
status = fill_packet_data_long(msg+PF_PACKET_HEADER+21, rreq->server_ip, 10);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address of Destination !!! Exiting ...",status);
return -1;
}
/* Initialize RREQ hop count to 1 */
status = fill_packet_data(msg+PF_PACKET_HEADER+31, 1, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+36, rreq->route_rediscovery, 1);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Rediscovery Flag !!! Exiting ...",status);
return -1;
}
/* Fill in the broad cast Id */
status = fill_packet_data(msg+PF_PACKET_HEADER+37, broadcast_id, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Broadcast Id !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+42, 0, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Payload Bytes !!! Exiting ...",status);
return -1;
}
rreq->rreq_sent_already=0;
status = fill_packet_data(msg+PF_PACKET_HEADER+47, rreq->rreq_sent_already, 1);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Payload Bytes !!! Exiting ...",status);
return -1;
}
status = fill_packet_data(msg+PF_PACKET_HEADER+48, 0, 2);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Payload Bytes !!! Exiting ...",status);
return -1;
}
msg[64] = '\0';
status = sendto(packet_soc_fd, msg, 100, 0, (struct sockaddr *)&pack_addr, sizeof(pack_addr));
if (status <= 0) {
printf("\nError in sending RREQ !!!\n");
return -1;
}
get_header_print_details(msg);
}
broadcast_id++;
return 0;
}
int
check_route_staleness(int staleness_parameter,
uint32_t route_timestamp){
uint32_t current_timestamp=get_timestamp_current_seconds();
//printf("\ncurrent_timestamp:-%d",current_timestamp);
//printf("\nroute_timestamp:-%d",route_timestamp);
uint32_t route_difference=current_timestamp-route_timestamp;
//printf("\nroute_difference:-%d",route_difference);
if(route_difference<staleness_parameter){
return(NOT_A_STALE_ENTRY);
} else {
return(A_STALE_ENTRY);
}
}
int
gen_update_send_rrep(int packet_soc_fd,
char *packet_soc_msg,
rrep_info *rrep,
int hop_flag,
route_table *table) {
int status;
long ip = 0;
struct ethhdr *eh = (struct ethhdr *)packet_soc_msg;
struct sockaddr_ll pack_addr;
int iter;
//memcpy((void*)packet_soc_msg, (void*)pack_addr->sll_addr, ETH_ALEN);
status = get_packet_data_long(packet_soc_msg+PF_PACKET_HEADER+6, 10, &ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;
}
for(iter = 1; iter <= 10; iter++) {
if (ip == table[iter].cano_ip_dest &&
table[iter].status == ROUTE_VALID) {
pack_addr.sll_family = PF_PACKET;
pack_addr.sll_protocol = htons(PROTOCOL_NO);
pack_addr.sll_ifindex = table[iter].if_index_nh;
pack_addr.sll_hatype = ARPHRD_ETHER;
pack_addr.sll_pkttype = PACKET_OTHERHOST;
pack_addr.sll_halen = ETH_ALEN;
memcpy((void*)packet_soc_msg, (void*)table[iter].mac_nh, ETH_ALEN);
status = fill_source_mac(packet_soc_msg+ETH_ALEN, table[iter].if_index_nh);
if (status != 0) {
printf("\nStatus = %d, Unable to fill source MAC address !!!", status);
return 0;
}
//memcpy((void*)(packet_soc_msg+ETH_ALEN), (void*)source_mac, ETH_ALEN);
eh->h_proto = PROTOCOL_NO;
pack_addr.sll_addr[0] = table[iter].mac_nh[0];
pack_addr.sll_addr[1] = table[iter].mac_nh[1];
pack_addr.sll_addr[2] = table[iter].mac_nh[2];
pack_addr.sll_addr[3] = table[iter].mac_nh[3];
pack_addr.sll_addr[4] = table[iter].mac_nh[4];
pack_addr.sll_addr[5] = table[iter].mac_nh[5];
pack_addr.sll_addr[6] = 0x00;
pack_addr.sll_addr[7] = 0x00;
}
}
status = fill_packet_data(packet_soc_msg+PF_PACKET_HEADER, PF_PACKET_RREP, 1);
if (status != 0) {
printf("\nStatus = %d, Unable to fill type RREP !!! Exiting ...",status);
return -1;
}
/* RREP at intermediate node */
if(hop_flag == 0) {
status = get_packet_data(packet_soc_msg+PF_PACKET_HEADER+31, 5, &(rrep->hop_count));
if (status != 0) {
printf("\nStatus = %d, Unable to get route rediscovery flag !!! Exiting ...",status);
return -1;
}
/* Hop count is to be filled in RREP */
status = fill_packet_data(packet_soc_msg+PF_PACKET_HEADER+31, rrep->hop_count+1, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
} else {
/* RREP at destination */
rrep->hop_count = 1;
status = fill_packet_data(packet_soc_msg+PF_PACKET_HEADER+31, rrep->hop_count, 5);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
}
status = sendto(packet_soc_fd,packet_soc_msg, 100, 0,
(struct sockaddr *)&pack_addr, sizeof(pack_addr));
if (status <= 0) {
printf("\nError in sending RREP !!!\n");
return -1;
}
get_header_print_details(packet_soc_msg);
return 0;
}
int
update_route_entry(route_table *table,
int hop_count,
long ip,
struct sockaddr_ll *pack_addr) {
int iter;
for(iter = 1; iter <= 10; iter++) {
if (ip == table[iter].cano_ip_dest) {
if (table[iter].hopcount > hop_count) {
/* Update entry */
table[iter].hopcount = hop_count;
table[iter].if_index_nh = pack_addr->sll_ifindex;
memcpy((void*)table[iter].mac_nh, (void*)pack_addr->sll_addr, ETH_ALEN);
table[iter].mac_nh[0] = pack_addr->sll_addr[0];
table[iter].mac_nh[1] = pack_addr->sll_addr[1];
table[iter].mac_nh[2] = pack_addr->sll_addr[2];
table[iter].mac_nh[3] = pack_addr->sll_addr[3];
table[iter].mac_nh[4] = pack_addr->sll_addr[4];
table[iter].mac_nh[5] = pack_addr->sll_addr[5];
table[iter].status = ROUTE_VALID;
table[iter].timestamp = get_timestamp_current_seconds();
break;
}
}
}
return 0;
}
int
print_route_table(route_table *table)
{
int iter,i;
char str[INET_ADDRSTRLEN];
char *ptr;
printf("\nRouting Table");
printf("\n===================================================================");
printf("\nDestination IP\tNext Hop\tNext IF_INDEX\tHop Count\tTimestamp");
printf("\n===================================================================\n");
for(iter = 1; iter <= 10; iter++) {
if(table[iter].status == ROUTE_VALID){
inet_ntop(AF_INET, &table[iter].cano_ip_dest, str, INET_ADDRSTRLEN);
printf("\n%s\t",str);
ptr = (char *)table[iter].mac_nh;
i = IF_HADDR;
do {
printf("%.2x%s", *ptr++ & 0xff, (i == 1) ? " " : ":");
} while (--i > 0);
printf("\t%d",table[iter].if_index_nh);
printf("\t%d",table[iter].hopcount);
printf("\t\t%d",table[iter].timestamp);
}
}
printf("\n=================================================================");
return 0;
}
int
update_routing_table(route_table *table,
char *msg,
struct sockaddr_ll *pack_addr) {
int status, packet_type, hop_count;
long ip = 0;
/* Check packet type */
status = get_packet_data(msg+PF_PACKET_HEADER, 1, &packet_type);
if (status != 0) {
printf("\nStatus = %d, Unable to get route rediscovery flag !!! Exiting ...",status);
return -1;
}
status = get_packet_data(msg+PF_PACKET_HEADER+31, 5, &hop_count);
if (status != 0) {
printf("\nStatus = %d, Unable to fill Hop Count !!! Exiting ...",status);
return -1;
}
if (packet_type == PF_PACKET_RREQ) {
/* Extract source address */
status = get_packet_data_long(msg+PF_PACKET_HEADER+6, 10, &ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;
}
} else if (packet_type == PF_PACKET_RREP) {
/* Extract destination address */
status = get_packet_data_long(msg+PF_PACKET_HEADER+21, 10, &ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;
}
} else if (packet_type == PF_PACKET_APP_PAYLOAD) {
/* TBD */
status = get_packet_data_long(msg+PF_PACKET_HEADER+6, 10, &ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;
}
}
status = update_route_entry(table, hop_count, ip, pack_addr);
if (status != 0) {
printf("\nUnable to update routing entry !!!");
return -1;
}
return 0;
}
int
gen_update_send_payload(int packet_soc_fd,
char *packet_soc_msg,
route_table *table,
int hop_flag) {
int status;
long ip = 0;
struct ethhdr *eh = (struct ethhdr *)packet_soc_msg;
struct sockaddr_ll pack_addr;
int hop_count;
int iter;
//memcpy((void*)packet_soc_msg, (void*)pack_addr->sll_addr, ETH_ALEN);
status = get_packet_data_long(packet_soc_msg+PF_PACKET_HEADER+21, 10, &ip);
if (status != 0) {
printf("\nStatus = %d, Unable to fill IP address !!! Exiting ...",status);
return -1;