-
Notifications
You must be signed in to change notification settings - Fork 1
/
sr_router.c
executable file
·1102 lines (879 loc) · 34 KB
/
sr_router.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
/**********************************************************************
* file: sr_router.c
* date: Mon Feb 18 12:50:42 PST 2002
* Contact: [email protected]
*
* Description:
*
* This file contains all the functions that interact directly
* with the routing table, as well as the main entry method
* for routing.
*
**********************************************************************/
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include "sr_if.h"
#include "sr_rt.h"
#include "sr_router.h"
#include "sr_protocol.h"
#include "pwospf_protocol.h"
#include "sr_pwospf.h"
#include "queue.h"
#include "cache.h"
struct queue_item* packet_queue[3];
struct cache_item* arp_cache;
pthread_t cache_thread;
pthread_t arp_thread[3];
int stop_arp_thread[3];
//uint32_t default_gateway_addr = 290068652;
uint8_t sr_multicast_mac[ETHER_ADDR_LEN];
/*---------------------------------------------------------------------
* Method: sr_init(void)
* Scope: Global
*
* Initialize the routing subsystem
*
*---------------------------------------------------------------------*/
void sr_init(struct sr_instance* sr)
{
/* REQUIRES */
assert(sr);
pwospf_init(sr);
/* Add initialization code here! */
sr_multicast_mac[0] = 0x01;
sr_multicast_mac[1] = 0x00;
sr_multicast_mac[2] = 0x5e;
sr_multicast_mac[3] = 0x00;
sr_multicast_mac[4] = 0x00;
sr_multicast_mac[5] = 0x05;
packet_queue[0] = queue_create_item(NULL, 0, NULL);
packet_queue[1] = queue_create_item(NULL, 0, NULL);
packet_queue[2] = queue_create_item(NULL, 0, NULL);
unsigned char empty_mac[ETHER_ADDR_LEN] = {0};
arp_cache = cache_create_item(0, empty_mac, 0);
pthread_create(&cache_thread, NULL, check_cache, NULL);
} /* -- sr_init -- */
/*---------------------------------------------------------------------
* Method: check_cache
*
*---------------------------------------------------------------------*/
void* check_cache(void* args)
{
while(1)
{
usleep(1000000);
check_cache(arp_cache);
}
}/* end check_cache */
/*---------------------------------------------------------------------
* Method: sr_handlepacket(uint8_t* p,char* interface)
* Scope: Global
*
* This method is called each time the router receives a packet on the
* interface. The packet buffer, the packet length and the receiving
* interface are passed in as parameters. The packet is complete with
* ethernet headers.
*
* Note: Both the packet buffer and the character's memory are handled
* by sr_vns_comm.c that means do NOT delete either. Make a copy of the
* packet instead if you intend to keep it around beyond the scope of
* the method call.
*
*---------------------------------------------------------------------*/
void sr_handlepacket(struct sr_instance* sr,
uint8_t * packet/* lent */,
unsigned int len,
char* interface/* lent */)
{
/* REQUIRES */
assert(sr);
assert(packet);
assert(interface);
struct sr_if* rx_if = sr_get_interface(sr, interface);
struct sr_ethernet_hdr* rx_e_hdr = (struct sr_ethernet_hdr*)packet;
if (chk_ether_addr(rx_e_hdr, rx_if) == 0)
{
return;
}
switch (htons(rx_e_hdr->ether_type))
{
case ETHERTYPE_ARP:
handle_arp_packet(sr, packet, len, rx_if, rx_e_hdr);
break;
case ETHERTYPE_IP:
handle_ip_packet(sr, packet, len, rx_if, rx_e_hdr);
break;
default:
Debug("\nReceived Unknow Packet, length = %d\n", len);
break;
}
}/* end sr_handlepacket */
/*---------------------------------------------------------------------
* Method: handle_ARP_packet
*
*---------------------------------------------------------------------*/
void handle_arp_packet(struct sr_instance* sr, uint8_t* packet, unsigned int len, struct sr_if* rx_if, struct sr_ethernet_hdr* rx_e_hdr)
{
struct sr_ethernet_hdr* tx_e_hdr = ((sr_ethernet_hdr*)(malloc(sizeof(sr_ethernet_hdr))));
uint8_t* tx_packet;
int queue_index;
/***** Getting the ARP header *****/
struct sr_arphdr* rx_arp_hdr = ((sr_arphdr*)(packet + sizeof(sr_ethernet_hdr)));
struct sr_arphdr* tx_arp_hdr = ((sr_arphdr*)(malloc(sizeof(sr_arphdr))));
switch (htons(rx_arp_hdr->ar_op))
{
case ARP_REQUEST:
Debug("\nReceived ARP REQUEST Packet, length = %d\n", len);
Debug("-> Constructing ARP REPLY Packet\n");
/* Destination address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_dhost[i] = rx_e_hdr->ether_shost[i];
}
/* Source address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_shost[i] = ((uint8_t)(rx_if->addr[i]));
}
/* Type */
tx_e_hdr->ether_type = rx_e_hdr->ether_type;
/* Hardware type */
tx_arp_hdr->ar_hrd = rx_arp_hdr->ar_hrd;
/* Protocol type */
tx_arp_hdr->ar_pro = rx_arp_hdr->ar_pro;
/* Hardware address length */
tx_arp_hdr->ar_hln = rx_arp_hdr->ar_hln;
/* Protocol address length */
tx_arp_hdr->ar_pln = rx_arp_hdr->ar_pln;
/* Operation code */
tx_arp_hdr->ar_op = htons(ARP_REPLY);
/* Source hardware address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_arp_hdr->ar_sha[i] = ((uint8_t)(rx_if->addr[i]));
}
/* Source protocol address */
tx_arp_hdr->ar_sip = rx_arp_hdr->ar_tip;
/* Target hardware address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_arp_hdr->ar_tha[i] = rx_arp_hdr->ar_sha[i];
}
/* Target protocol address */
tx_arp_hdr->ar_tip = rx_arp_hdr->ar_sip;
/***** Creating the transmitted packet *****/
tx_packet = ((uint8_t*)(malloc(sizeof(sr_ethernet_hdr) + sizeof(sr_arphdr))));
memcpy(tx_packet, tx_e_hdr, sizeof(sr_ethernet_hdr));
memcpy(tx_packet + sizeof(sr_ethernet_hdr), tx_arp_hdr, sizeof(sr_arphdr));
Debug("-> Sending ARP REPLY Packet, length = %d\n", sizeof(sr_ethernet_hdr) + sizeof(sr_arphdr));
sr_send_packet(sr, ((uint8_t*)(tx_packet)), sizeof(sr_ethernet_hdr) + sizeof(sr_arphdr), rx_if->name);
free(tx_packet);
free(tx_arp_hdr);
free(tx_e_hdr);
break;
case ARP_REPLY:
Debug("\nReceived ARP REPLY Packet, length = %d\n", len);
/* Checking the ARP cache */
struct in_addr ip_address;
ip_address.s_addr = rx_arp_hdr->ar_sip;
if (cache_search(arp_cache, rx_arp_hdr->ar_sip) == NULL)
{
Debug("-> Updating the ARP Cache, [%s, ", inet_ntoa(ip_address));
DebugMAC(rx_arp_hdr->ar_sha);
Debug("]\n");
if (arp_cache == NULL)
{
time_t time_stamp;
time(&time_stamp);
arp_cache = cache_create_item(rx_arp_hdr->ar_sip, rx_arp_hdr->ar_sha, ((int)(time_stamp)));
}
else
{
time_t time_stamp;
time(&time_stamp);
cache_push(arp_cache, cache_create_item(rx_arp_hdr->ar_sip, rx_arp_hdr->ar_sha, ((int)(time_stamp))));
}
}
else
{
Debug("-> Entry already exists in the ARP Cache, [%s, ", inet_ntoa(ip_address));
DebugMAC(rx_arp_hdr->ar_sha);
Debug("]\n");
}
queue_index = ((int)(rx_if->name[strlen(rx_if->name) - 1])) - 48;
/***** Stop the ARP_THREAD *****/
if (stop_arp_thread[queue_index] == 0)
{
Debug("-> Stopping the ARP REQUESTs thread\n");
stop_arp_thread[queue_index] = 1;
//pthread_cancel(arp_thread[queue_index]);
}
if (queue_is_empty(packet_queue[queue_index]) == 0)
{
struct queue_item* item = queue_pop(packet_queue[queue_index]);
Debug("-> Popping a packet from the queue, length = %d\n", item->length);
Debug("-> Updating the popped packet\n");
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
item->packet[i] = rx_arp_hdr->ar_sha[i];
}
Debug("-> Sending the popped packet, length = %d\n", item->length);
sr_send_packet(sr, item->packet, item->length, item->interface);
free(item);
}
break;
}
}/* end handle_ARP_packet */
/*---------------------------------------------------------------------
* Method: handle_IP_packet
*
*---------------------------------------------------------------------*/
void handle_ip_packet(struct sr_instance* sr, uint8_t* packet, unsigned int len, struct sr_if* rx_if, struct sr_ethernet_hdr* rx_e_hdr)
{
Debug("\nReceived IP Packet, length = %d\n", len);
struct sr_ethernet_hdr* tx_e_hdr = ((sr_ethernet_hdr*)(malloc(sizeof(sr_ethernet_hdr))));
struct sr_icmphdr* rx_icmp_hdr;
struct sr_icmphdr* tx_icmp_hdr;
uint8_t* tx_packet;
int queue_index;
/***** Getting the IP header *****/
ip* rx_ip_hdr = ((ip*)(packet + sizeof(sr_ethernet_hdr)));
ip* tx_ip_hdr = ((ip*)(malloc(sizeof(ip))));
/***** Checking the received Checksum *****/
int rx_sum_temp = rx_ip_hdr->ip_sum;
rx_ip_hdr->ip_sum = 0;
uint16_t rx_sum = calc_cksum(((uint8_t*)(rx_ip_hdr)), sizeof(ip));
rx_ip_hdr->ip_sum = rx_sum_temp;
if (rx_sum != rx_sum_temp)
{
Debug("-> Packet dropped: invalid checksum\n");
return;
}
if (chk_ip_addr(rx_ip_hdr, sr) == 0)
{
/***** Checking the received TTL *****/
if (rx_ip_hdr->ip_ttl <= 1)
{
Debug("-> Packet dropped: invalid TTL\n");
send_icmp_error(sr, packet, len, rx_if, ICMP_TIME_EXCEEDED_TYPE, ICMP_TIME_EXCEEDED_CODE);
}
else
{
Debug("-> Forwarding packet, length = %d\n", len);
forward_packet(sr, packet, len);
}
return;
}
/***** Checking the received TTL *****/
if (rx_ip_hdr->ip_ttl <= 1)
{
Debug("-> Packet dropped: invalid TTL\n");
send_icmp_error(sr, packet, len, rx_if, ICMP_DESTINATION_UNREACHABLE_TYPE, ICMP_PORT_UNREACHABLE_CODE);
return;
}
switch (rx_ip_hdr->ip_p)
{
case IP_PROTO_ICMP:
/***** Getting the ICMP header *****/
rx_icmp_hdr = ((sr_icmphdr*)(packet + sizeof(sr_ethernet_hdr) + sizeof(ip)));
tx_icmp_hdr = ((sr_icmphdr*)(malloc(sizeof(sr_icmphdr))));
if ((rx_icmp_hdr->type == ICMP_ECHO_REQUEST_TYPE) & (rx_icmp_hdr->code == ICMP_ECHO_REQUEST_CODE))
{
Debug("-> The IP Packet is ICMP ECHO REQUEST\n");
Debug("-> Constructing ICMP ECHO REPLY Packet\n");
/* Destination address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_dhost[i] = 255;
}
/* Source address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_shost[i] = ((uint8_t)(rx_if->addr[i]));
}
/* Type */
tx_e_hdr->ether_type = rx_e_hdr->ether_type;
/* Version + Header length */
tx_ip_hdr->ip_v = rx_ip_hdr->ip_v;
tx_ip_hdr->ip_hl = rx_ip_hdr->ip_hl;
/* DS */
tx_ip_hdr->ip_tos = rx_ip_hdr->ip_tos;
/* Total length */
tx_ip_hdr->ip_len = rx_ip_hdr->ip_len;
/* Identification */
tx_ip_hdr->ip_id = rx_ip_hdr->ip_id;
/* Fragment */
tx_ip_hdr->ip_off = rx_ip_hdr->ip_off;
/* TTL */
tx_ip_hdr->ip_ttl = 64;
/* Protocol */
tx_ip_hdr->ip_p = rx_ip_hdr->ip_p; // which is 1 = ICMP
/* Checksum */
tx_ip_hdr->ip_sum = 0;
/* Source IP address */
tx_ip_hdr->ip_src = rx_ip_hdr->ip_dst;
/* Destination IP address */
tx_ip_hdr->ip_dst = rx_ip_hdr->ip_src;
/* Re-Calculate checksum of the IP header */
tx_ip_hdr->ip_sum = calc_cksum(((uint8_t*)(tx_ip_hdr)), sizeof(ip));
/* Type */
tx_icmp_hdr->type = ICMP_ECHO_REPLY_TYPE;
/* Code */
tx_icmp_hdr->code = ICMP_ECHO_REPLY_CODE;
/* Checksum */
tx_icmp_hdr->cksum = 0;
/* Identification */
tx_icmp_hdr->id = rx_icmp_hdr->id;
/* Sequence Number */
tx_icmp_hdr->seq_n = rx_icmp_hdr->seq_n;
/***** Creating the transmitted packet *****/
tx_packet = ((uint8_t*)(malloc(sizeof(uint8_t) * len)));
memcpy(tx_packet, tx_e_hdr, sizeof(sr_ethernet_hdr));
memcpy(tx_packet + sizeof(sr_ethernet_hdr), tx_ip_hdr, sizeof(ip));
memcpy(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip), tx_icmp_hdr, sizeof(sr_icmphdr));
/* Copy the Data part */
for (unsigned int i = sizeof(sr_ethernet_hdr) + sizeof(ip) + sizeof(sr_icmphdr); i < len; i++)
{
tx_packet[i] = packet[i];
}
/* Re-Calculate checksum of the ICMP header */
/* Updating the new checksum in tx_packet */
((sr_icmphdr*)(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip)))->cksum =
calc_cksum(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip), len - (sizeof(sr_ethernet_hdr) + sizeof(ip)));
/* Checking the ARP cache */
struct in_addr ip_address;
ip_address.s_addr = get_nex_hop_ip(sr, rx_if->name);
Debug("-> Searching the ARP Cache for [%s]\n", inet_ntoa(ip_address));
cache_item* item = cache_search(arp_cache, get_nex_hop_ip(sr, rx_if->name));
if (item == NULL)
{
/* Push the packet in the queue */
Debug("-> ARP Cache entry NOT found\n");
Debug("-> Pushing the ICMP ECHO REPLY Packet in the queue, length = %d\n", len);
queue_index = ((int)(rx_if->name[strlen(rx_if->name) - 1])) - 48;
if (packet_queue[queue_index] == NULL)
{
packet_queue[queue_index] = queue_create_item(tx_packet, len, rx_if->name);
}
else
{
queue_push(packet_queue[queue_index], queue_create_item(tx_packet, len, rx_if->name));
}
send_arp_request(sr, rx_if, get_nex_hop_ip(sr, rx_if->name));
}
else
{
ip_address.s_addr = item->ip;
Debug("-> ARP Cache entry found, [%s, ", inet_ntoa(ip_address));
DebugMAC(item->mac);
Debug("]\n");
Debug("-> Updating the ICMP ECHO REPLY Packet\n");
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_packet[i] = item->mac[i];
}
Debug("-> Sending the ICMP ECHO REPLY Packet, length = %d\n", len);
sr_send_packet(sr, tx_packet, len, rx_if->name);
free(tx_packet);
}
free(tx_icmp_hdr);
free(tx_ip_hdr);
free(tx_e_hdr);
}
else if ((rx_icmp_hdr->type == ICMP_ECHO_REPLY_TYPE) & (rx_icmp_hdr->code == ICMP_ECHO_REPLY_CODE))
{
Debug("-> The IP Packet is ICMP ECHO REPLY\n");
}
break;
case IP_PROTO_TCP:
case IP_PROTO_UDP:
if (rx_ip_hdr->ip_p == IP_PROTO_TCP)
{
Debug("-> The IP Packet is TCP Packet\n");
}
else
{
Debug("-> The IP Packet is UDP Packet\n");
}
/***** Seding an ICMP PORT UNREACHABLE packet *****/
send_icmp_error(sr, packet, len, rx_if, ICMP_DESTINATION_UNREACHABLE_TYPE, ICMP_PORT_UNREACHABLE_CODE);
break;
case IP_PROTO_OSPFv2:
Debug("-> The IP Packet is PWOSPF Packet\n");
handling_ospfv2_packets(sr, packet, len, rx_if);
break;
}
}/* end handle_ip_packet */
/*---------------------------------------------------------------------
* Method: send_icmp_error
*
*---------------------------------------------------------------------*/
void send_icmp_error(struct sr_instance* sr, uint8_t* packet, unsigned int len, struct sr_if* rx_if, uint8_t type, uint8_t code)
{
struct sr_ethernet_hdr* rx_e_hdr = (struct sr_ethernet_hdr*)packet;
struct sr_ethernet_hdr* tx_e_hdr = ((sr_ethernet_hdr*)(malloc(sizeof(sr_ethernet_hdr))));
struct sr_icmphdr* rx_icmp_hdr;
struct sr_icmphdr* tx_icmp_hdr;
uint8_t* tx_packet;
int queue_index;
/***** Getting the IP header *****/
ip* rx_ip_hdr = ((ip*)(packet + sizeof(sr_ethernet_hdr)));
ip* tx_ip_hdr = ((ip*)(malloc(sizeof(ip))));
if (type == ICMP_DESTINATION_UNREACHABLE_TYPE)
{
if (code == ICMP_HOST_UNREACHABLE_CODE)
{
Debug("-> Constructing ICMP HOST UNREACHABLE Packet\n");
}
else if (code == ICMP_PORT_UNREACHABLE_CODE)
{
Debug("-> Constructing ICMP PORT UNREACHABLE Packet\n");
}
}
else if ((type == ICMP_TIME_EXCEEDED_TYPE) & (code == ICMP_TIME_EXCEEDED_CODE))
{
Debug("-> Constructing ICMP TIME EXCEEDED Packet\n");
}
/***** Getting the ICMP header *****/
rx_icmp_hdr = ((sr_icmphdr*)(packet + sizeof(sr_ethernet_hdr) + sizeof(ip)));
tx_icmp_hdr = ((sr_icmphdr*)(malloc(sizeof(sr_icmphdr))));
/* Destination address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_dhost[i] = 255; //rx_e_hdr->ether_shost[i];
}
/* Source address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_shost[i] = ((uint8_t)(rx_if->addr[i]));
}
/* Type */
tx_e_hdr->ether_type = rx_e_hdr->ether_type;
/* Version + Header length */
tx_ip_hdr->ip_v = rx_ip_hdr->ip_v;
tx_ip_hdr->ip_hl = rx_ip_hdr->ip_hl;
/* DS */
tx_ip_hdr->ip_tos = rx_ip_hdr->ip_tos;
/* Total length */
tx_ip_hdr->ip_len = htons((2 * sizeof(ip)) + sizeof(sr_icmphdr) + 8);
/* Identification */
tx_ip_hdr->ip_id = rx_ip_hdr->ip_id;
/* Fragment */
tx_ip_hdr->ip_off = rx_ip_hdr->ip_off;
/* TTL */
tx_ip_hdr->ip_ttl = 64;
/* Protocol */
tx_ip_hdr->ip_p = IP_PROTO_ICMP; // which is 1 = ICMP
/* Checksum */
tx_ip_hdr->ip_sum = 0;
/* Source IP address */
if (type == 11)
{
tx_ip_hdr->ip_src.s_addr = rx_if->ip;
}
else
{
tx_ip_hdr->ip_src = rx_ip_hdr->ip_dst;
}
/* Destination IP address */
tx_ip_hdr->ip_dst = rx_ip_hdr->ip_src;
/* Re-Calculate checksum of the IP header */
tx_ip_hdr->ip_sum = calc_cksum(((uint8_t*)(tx_ip_hdr)), sizeof(ip));
/* Type */
tx_icmp_hdr->type = type;
/* Code */
tx_icmp_hdr->code = code;
/* Checksum */
tx_icmp_hdr->cksum = 0;
/* Identification */
tx_icmp_hdr->id = 0;
/* Sequence Number */
tx_icmp_hdr->seq_n = 0;
/***** Creating the transmitted packet *****/
tx_packet = ((uint8_t*)(malloc(sizeof(sr_ethernet_hdr) + (2 * sizeof(ip)) + sizeof(sr_icmphdr) + 8)));
memcpy(tx_packet, tx_e_hdr, sizeof(sr_ethernet_hdr));
memcpy(tx_packet + sizeof(sr_ethernet_hdr), tx_ip_hdr, sizeof(ip));
memcpy(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip), tx_icmp_hdr, sizeof(sr_icmphdr));
/* Copy the IP daragram that trrigered the error */
memcpy(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip) + sizeof(sr_icmphdr), rx_ip_hdr, sizeof(ip));
for (unsigned int i = 0; i < 8; i++)
{
tx_packet[sizeof(sr_ethernet_hdr) + (2 * sizeof(ip)) + sizeof(sr_icmphdr) + i] = packet[sizeof(sr_ethernet_hdr) + sizeof(ip) + i];
}
/* Re-Calculate checksum of the ICMP header */
/* Updating the new checksum in tx_packet */
((sr_icmphdr*)(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip)))->cksum =
calc_cksum(tx_packet + sizeof(sr_ethernet_hdr) + sizeof(ip), sizeof(sr_icmphdr) + sizeof(ip) + 8);
/* Checking the ARP cache */
struct in_addr ip_address;
ip_address.s_addr = get_nex_hop_ip(sr, rx_if->name);
Debug("-> Searching the ARP Cache for [%s]\n", inet_ntoa(ip_address));
cache_item* item = cache_search(arp_cache, get_nex_hop_ip(sr, rx_if->name));
if (item == NULL)
{
/* Push the packet in the queue */
Debug("-> ARP Cache entry NOT found\n");
/* Push the packet in the queue */
Debug("-> Pushing the ICMP ERROR MESSAGE Packet in the queue, length = %d\n",
sizeof(uint8_t) * (sizeof(sr_ethernet_hdr) + (2 * sizeof(ip)) + sizeof(sr_icmphdr) + 8));
queue_index = ((int)(rx_if->name[strlen(rx_if->name) - 1])) - 48;
if (packet_queue[queue_index] == NULL)
{
packet_queue[queue_index] = queue_create_item(tx_packet, sizeof(sr_ethernet_hdr) + (2 * sizeof(ip)) + sizeof(sr_icmphdr) + 8, rx_if->name);
}
else
{
queue_push(packet_queue[queue_index], queue_create_item(tx_packet, sizeof(sr_ethernet_hdr) + (2 * sizeof(ip)) + sizeof(sr_icmphdr) + 8,
rx_if->name));
}
send_arp_request(sr, rx_if, get_nex_hop_ip(sr, rx_if->name));
}
else
{
ip_address.s_addr = item->ip;
Debug("-> ARP Cache entry found, [%s, ", inet_ntoa(ip_address));
DebugMAC(item->mac);
Debug("]\n");
if (type == ICMP_DESTINATION_UNREACHABLE_TYPE)
{
if (code == ICMP_HOST_UNREACHABLE_CODE)
{
Debug("-> Updating the ICMP HOST UNREACHABLE Packet\n");
}
else if (code == ICMP_PORT_UNREACHABLE_CODE)
{
Debug("-> Updating the ICMP PORT UNREACHABLE Packet\n");
}
}
else if ((type == ICMP_TIME_EXCEEDED_TYPE) & (code == ICMP_TIME_EXCEEDED_CODE))
{
Debug("-> Updating the ICMP TIME EXCEEDED Packet\n");
}
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_packet[i] = item->mac[i];
}
if (type == ICMP_DESTINATION_UNREACHABLE_TYPE)
{
if (code == ICMP_HOST_UNREACHABLE_CODE)
{
Debug("-> Sending the ICMP HOST UNREACHABLE Packet, length = %d\n", len);
}
else if (code == ICMP_PORT_UNREACHABLE_CODE)
{
Debug("-> Sending the ICMP PORT UNREACHABLE Packet, length = %d\n", len);
}
}
else if ((type == ICMP_TIME_EXCEEDED_TYPE) & (code == ICMP_TIME_EXCEEDED_CODE))
{
Debug("-> Sending the ICMP TIME EXCEEDED Packet, length = %d\n", len);
}
sr_send_packet(sr, tx_packet, sizeof(sr_ethernet_hdr) + (2 * sizeof(ip)) + sizeof(sr_icmphdr) + 8, rx_if->name);
free(tx_packet);
}
free(tx_icmp_hdr);
free(tx_ip_hdr);
free(tx_e_hdr);
}/* end send_icmp_error */
/*---------------------------------------------------------------------
* Method: send_arp_request
*
*---------------------------------------------------------------------*/
void send_arp_request(struct sr_instance* sr, struct sr_if* rx_if, uint32_t target_ip)
{
Debug("-> Constructing ARP REQUEST Packet\n");
struct sr_ethernet_hdr* tx_e_hdr = ((sr_ethernet_hdr*)(malloc(sizeof(sr_ethernet_hdr))));
struct sr_arphdr* tx_arp_hdr = ((sr_arphdr*)(malloc(sizeof(sr_arphdr))));
/* Destination address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_dhost[i] = 255;
}
/* Source address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_e_hdr->ether_shost[i] = ((uint8_t)(rx_if->addr[i]));
}
/* Type */
tx_e_hdr->ether_type = htons(ETHERTYPE_ARP);
/* Hardware type */
tx_arp_hdr->ar_hrd = htons(ARPHDR_ETHER);
/* Protocol type */
tx_arp_hdr->ar_pro = htons(ETHERTYPE_IP);
/* Hardware address length */
tx_arp_hdr->ar_hln = ETHER_ADDR_LEN;
/* Protocol address length */
tx_arp_hdr->ar_pln = IP_ADDR_LEN;
/* Operation code */
tx_arp_hdr->ar_op = htons(ARP_REQUEST);
/* Source hardware address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_arp_hdr->ar_sha[i] = ((uint8_t)(rx_if->addr[i]));
}
/* Source protocol address */
tx_arp_hdr->ar_sip = rx_if->ip;
/* Target hardware address */
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
tx_arp_hdr->ar_tha[i] = 225;
}
/* Target protocol address */
tx_arp_hdr->ar_tip = target_ip;
/***** Creating the transmitted packet *****/
uint8_t* tx_packet = ((uint8_t*)(malloc(sizeof(sr_ethernet_hdr) + sizeof(sr_arphdr))));
memcpy(tx_packet, tx_e_hdr, sizeof(sr_ethernet_hdr));
memcpy(tx_packet + sizeof(sr_ethernet_hdr), tx_arp_hdr, sizeof(sr_arphdr));
struct sr_arp_thread_param* arp_param = ((sr_arp_thread_param*)(malloc(sizeof(sr_arp_thread_param))));
arp_param->sr = sr;
arp_param->counter = ARP_REQUESTS_NUM;
for (int i = 0; i < ARP_REQUEST_PKT_LEN;i++)
{
arp_param->packet[i] = tx_packet[i];
}
for (int i = 0; i < sr_IFACE_NAMELEN; i++)
{
arp_param->interface[i] = rx_if->name[i];
}
arp_param->target_ip = target_ip;
Debug("-> Running the ARP REQUESTs thread for %d attempt(s)\n", ARP_REQUESTS_NUM);
int queue_index = ((int)(rx_if->name[strlen(rx_if->name) - 1])) - 48;
stop_arp_thread[queue_index] = 0;
pthread_create(&arp_thread[queue_index], NULL, sending_arp_request, arp_param);
free(tx_packet);
free(tx_arp_hdr);
free(tx_e_hdr);
}/* send_arp_request */
/*---------------------------------------------------------------------
* Method: sending_arp_request
*
*---------------------------------------------------------------------*/
void* sending_arp_request(void* args)
{
struct sr_arp_thread_param* arp_param = ((sr_arp_thread_param*)(args));
int queue_index = ((int)(arp_param->interface[strlen(arp_param->interface) - 1])) - 48;
while((arp_param->counter > 0) & (stop_arp_thread[queue_index] == 0))
{
struct in_addr target_addr;
target_addr.s_addr = arp_param->target_ip;
Debug("\n\n**** Sending ARP REQUEST Packet to [%s], length = %d [Attempt %d] ****\n\n", inet_ntoa(target_addr), sizeof(sr_ethernet_hdr) +
sizeof(sr_arphdr), ARP_REQUESTS_NUM - arp_param->counter + 1);
sr_send_packet(arp_param->sr, ((uint8_t*)(arp_param->packet)), ARP_REQUEST_PKT_LEN, arp_param->interface);
arp_param->counter--;
usleep(5000000);
}
if (queue_is_empty(packet_queue[queue_index]) == 0)
{
struct queue_item* q_item = queue_pop(packet_queue[queue_index]);
send_icmp_error(arp_param->sr, q_item->packet, q_item->length, sr_get_interface(arp_param->sr, q_item->interface),
ICMP_DESTINATION_UNREACHABLE_TYPE, ICMP_HOST_UNREACHABLE_CODE);
}
return NULL;
}/* end sending_arp_request */
/*---------------------------------------------------------------------
* Method: forward_packet
*
*---------------------------------------------------------------------*/
void forward_packet(struct sr_instance* sr, uint8_t* packet, unsigned int len)
{
/***** Reducing the TTL and Recalculating the Checksum *****/
((ip*)(packet + sizeof(sr_ethernet_hdr)))->ip_ttl--;
((ip*)(packet + sizeof(sr_ethernet_hdr)))->ip_sum = 0;
((ip*)(packet + sizeof(sr_ethernet_hdr)))->ip_sum =
calc_cksum(packet + sizeof(sr_ethernet_hdr), sizeof(ip));
ip* rx_ip_hdr = ((ip*)(packet + sizeof(sr_ethernet_hdr)));
int queue_index;
struct sr_rt* temp = sr->routing_table;
struct sr_rt* default_route = NULL;
struct sr_rt* route = NULL;
while(temp != NULL)
{
if (temp->dest.s_addr == 0)
{
default_route = temp;
}
else
{
if (route == NULL)
{
if ((temp->dest.s_addr & htonl(0xfffffffe)/*temp->mask.s_addr*/) == (rx_ip_hdr->ip_dst.s_addr & htonl(0xfffffffe)/*temp->mask.s_addr*/))
{
route = temp;
}
}
}
temp = temp->next;
}
struct sr_if* tx_interface;
struct in_addr ip_address;
if (route != NULL)
{
tx_interface = sr_get_interface(sr, route->interface);
if (route->gw.s_addr != 0)
{
ip_address = route->gw;
}
else if (tx_interface->neighbor_ip != 0)
{
ip_address.s_addr = tx_interface->neighbor_ip;
}
else
{
ip_address.s_addr = ((ip*)(packet + sizeof(sr_ethernet_hdr)))->ip_dst.s_addr;
}
}
else if (default_route != NULL)
{
tx_interface = sr_get_interface(sr, default_route->interface);
ip_address = default_route->gw;
}
if (tx_interface != NULL)
{
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
packet[i + 6] = tx_interface->addr[i];
}
/* Checking the ARP cache */
Debug("-> Searching the ARP Cache for [%s]\n", inet_ntoa(ip_address));
cache_item* item = cache_search(arp_cache, ip_address.s_addr);
if (item == NULL)
{
Debug("-> ARP Cache entry NOT found\n");
/* Push the packet in the queue */
Debug("-> Pushing forwarded packet in the queue, length = %d\n", len);
queue_index = ((int)(tx_interface->name[strlen(tx_interface->name) - 1])) - 48;
if (packet_queue[queue_index] == NULL)
{
packet_queue[queue_index] = queue_create_item(packet, len, tx_interface->name);
}
else
{
queue_push(packet_queue[queue_index], queue_create_item(packet, len, tx_interface->name));
}
//if (route != NULL)
//{
send_arp_request(sr, tx_interface, ip_address.s_addr/*route->gw.s_addr*/);
//}
//else if (default_route != NULL)
//{
// send_arp_request(sr, tx_interface, default_route->gw.s_addr);
//}
}
else
{
ip_address.s_addr = item->ip;
Debug("-> ARP Cache entry found, [%s, ", inet_ntoa(ip_address));
DebugMAC(item->mac);
Debug("]\n");
Debug("-> Updating the forworded packet\n");
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{
packet[i] = item->mac[i];
}
Debug("-> Sending the forworded packet, length = %d\n", len);
sr_send_packet(sr, packet, len, tx_interface->name);
}
}
else
{
Debug("**** ERROR: no route the destenation ****\n");
}
}/* forward_packet */
/*---------------------------------------------------------------------
* Method: chk_ether_addr
*
*---------------------------------------------------------------------*/
short chk_ether_addr(struct sr_ethernet_hdr* rx_e_hdr, struct sr_if* rx_if)
{
for (int i = 0; i < ETHER_ADDR_LEN; i++)
{