-
Notifications
You must be signed in to change notification settings - Fork 0
/
sr_router.c
634 lines (529 loc) · 21.7 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
/**********************************************************************
* 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 <stdlib.h>
#include <assert.h>
#include <string.h>
#include "sr_if.h"
#include "sr_rt.h"
#include "sr_router.h"
#include "sr_protocol.h"
#include "sr_arpcache.h"
#include "sr_utils.h"
/*---------------------------------------------------------------------
* Method: sr_init(void)
* Scope: Global
*
* Initialize the routing subsystem
*
*---------------------------------------------------------------------*/
void sr_init(struct sr_instance* sr)
{
/* REQUIRES */
assert(sr);
/* Initialize cache and cache cleanup thread */
sr_arpcache_init(&(sr->cache));
pthread_attr_init(&(sr->attr));
pthread_attr_setdetachstate(&(sr->attr), PTHREAD_CREATE_JOINABLE);
pthread_attr_setscope(&(sr->attr), PTHREAD_SCOPE_SYSTEM);
pthread_attr_setscope(&(sr->attr), PTHREAD_SCOPE_SYSTEM);
pthread_t thread;
pthread_create(&thread, &(sr->attr), sr_arpcache_timeout, sr);
/* Add initialization code here! */
} /* -- sr_init -- */
void handle_arpreq(struct sr_instance* sr, struct sr_arpreq * req)
{
if(!sr || !req)
return;
if (difftime(time(NULL), req->sent) > 1.0)
{
if(req->times_sent >= 5)
{
/* send ICMP host unreachable to source addr of all packets waiting on this request */
struct sr_packet * curr = req->packets;
if (curr)
{
(void)construct_icmp_h_unreach_and_send(sr, curr->buf);
while(curr->next)
{
curr = curr->next;
(void)construct_icmp_h_unreach_and_send(sr, curr->buf);
}
}
sr_arpreq_destroy(&(sr->cache), req);
}
else
{
/* send ARP request */
int retval = construct_arp_buf_and_send(sr, req);
printf("RETURN VALUE: %d\n", retval);
req->sent = time(NULL);
req->times_sent++;
}
}
}
int construct_icmp_h_unreach_and_send(struct sr_instance* sr, uint8_t * buf)
{
uint8_t orig_buf[IP_MAXPACKET];
memcpy(orig_buf, buf, sizeof(orig_buf));
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)buf;
sr_ip_hdr_t * ip_hdr = (sr_ip_hdr_t *)(buf+sizeof(sr_ethernet_hdr_t));
sr_icmp_t11_hdr_t * icmp_hdr = (sr_icmp_t11_hdr_t *) malloc(sizeof(sr_icmp_t11_hdr_t));
uint8_t * shost_copy[ETHER_ADDR_LEN];
memcpy(shost_copy, ehdr->ether_shost, sizeof(shost_copy));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, shost_copy, sizeof(ehdr->ether_dhost));
printf("PRINTING IP HEADER --------- \n\n");
print_hdr_ip(buf+sizeof(sr_ethernet_hdr_t));
ip_hdr->ip_ttl = 64;
/* Longest prefix match with destination IP address */
struct sr_rt * curr = sr->routing_table;
struct sr_rt * longest_match = NULL;
while (curr->next != NULL)
{
if ((ip_hdr->ip_src & curr->mask.s_addr) == curr->dest.s_addr)
{
if (longest_match == NULL)
longest_match = curr;
else if (longest_match != NULL && longest_match->mask.s_addr < curr->mask.s_addr)
longest_match = curr;
}
curr = curr->next;
}
if (longest_match == NULL)
{
if ((ip_hdr->ip_src & curr->mask.s_addr) == curr->dest.s_addr)
longest_match = curr;
else /* No match found in routing table */
{
fprintf(stderr, "No match in routing table -- LPM for ICMP host unreachable!\n");
return 1; /* this case should never occur */
}
}
fprintf(stdout, "MATCHED INTERFACE NAME: %s\n", longest_match->interface);
/* Longest prefix match complete! */
/* set source and dest IP addresses in IP header */
ip_hdr->ip_dst = ip_hdr->ip_src;
ip_hdr->ip_src = sr_get_interface(sr, longest_match->interface)->ip;
ip_hdr->ip_len = htons(sizeof(*ip_hdr)+sizeof(*icmp_hdr));
ip_hdr->ip_sum = 0;
ip_hdr->ip_sum = cksum(ip_hdr, sizeof(*ip_hdr));
icmp_hdr->icmp_type = 3;
icmp_hdr->icmp_code = 1;
icmp_hdr->unused = 0;
memcpy(icmp_hdr->data, orig_buf+sizeof(sr_ethernet_hdr_t), sizeof(icmp_hdr->data));
icmp_hdr->icmp_sum = 0;
icmp_hdr->icmp_sum = cksum(icmp_hdr, sizeof(*icmp_hdr));
/*copy headers into int buffer*/
uint8_t buffer[sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t)];
memcpy(buffer, ehdr, sizeof(sr_ethernet_hdr_t));
memcpy(buffer+sizeof(sr_ethernet_hdr_t), ip_hdr, sizeof(sr_ip_hdr_t));
memcpy(buffer+sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t), icmp_hdr, sizeof(sr_icmp_t11_hdr_t));
/*send packet*/
int retval = sr_send_packet(sr, buffer, sizeof(*ehdr)+sizeof(*ip_hdr)+sizeof(*icmp_hdr),
longest_match->interface);
return retval;
}
int construct_icmp_net_unreach_and_send(struct sr_instance* sr, uint8_t * buf, char * iface)
{
uint8_t orig_buf[IP_MAXPACKET];
memcpy(orig_buf, buf, sizeof(orig_buf));
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)buf;
sr_ip_hdr_t * ip_hdr = (sr_ip_hdr_t *)(buf+sizeof(sr_ethernet_hdr_t));
sr_icmp_t11_hdr_t * icmp_hdr = (sr_icmp_t11_hdr_t *) malloc(sizeof(sr_icmp_t11_hdr_t));
uint8_t * shost_copy[ETHER_ADDR_LEN];
memcpy(shost_copy, ehdr->ether_shost, sizeof(shost_copy));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, shost_copy, sizeof(ehdr->ether_dhost));
ip_hdr->ip_ttl = 64;
/* set source and dest IP addresses in IP header */
ip_hdr->ip_dst = ip_hdr->ip_src;
ip_hdr->ip_src = sr_get_interface(sr, iface)->ip;
ip_hdr->ip_len = htons(sizeof(*ip_hdr)+sizeof(*icmp_hdr));
ip_hdr->ip_sum = 0;
ip_hdr->ip_sum = cksum(ip_hdr, sizeof(*ip_hdr));
icmp_hdr->icmp_type = 3;
icmp_hdr->icmp_code = 0;
icmp_hdr->unused = 0;
memcpy(icmp_hdr->data, orig_buf+sizeof(sr_ethernet_hdr_t), sizeof(icmp_hdr->data));
icmp_hdr->icmp_sum = 0;
icmp_hdr->icmp_sum = cksum(icmp_hdr, sizeof(*icmp_hdr));
/*copy headers into int buffer*/
uint8_t buffer[sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t)];
memcpy(buffer, ehdr, sizeof(sr_ethernet_hdr_t));
memcpy(buffer+sizeof(sr_ethernet_hdr_t), ip_hdr, sizeof(sr_ip_hdr_t));
memcpy(buffer+sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t), icmp_hdr, sizeof(sr_icmp_t11_hdr_t));
/*send packet*/
int retval = sr_send_packet(sr, buffer, sizeof(*ehdr)+sizeof(*ip_hdr)+sizeof(*icmp_hdr), iface);
return retval;
}
int construct_arp_buf_and_send(struct sr_instance* sr, struct sr_arpreq * req)
{
/*get broadcast address*/
uint8_t BROADCAST_ADDR[ETHER_ADDR_LEN];
int index;
for(index = 0; index < ETHER_ADDR_LEN; index++)
{
BROADCAST_ADDR[index] = 255;
}
/* broadcast address setup complete */
struct sr_if * matching_if = sr_get_interface(sr, req->packets->iface);
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *) malloc(sizeof(sr_ethernet_hdr_t));
sr_arp_hdr_t * arp_hdr = (sr_arp_hdr_t *) malloc(sizeof(sr_arp_hdr_t));
/*setup ethernet header*/
memcpy(ehdr->ether_dhost, BROADCAST_ADDR, sizeof(ehdr->ether_dhost));
memcpy(ehdr->ether_shost, matching_if->addr, sizeof(ehdr->ether_shost));
ehdr->ether_type = htons(ethertype_arp);
/*setup arp header*/
arp_hdr->ar_hrd = htons(1); /*ethernet*/
arp_hdr->ar_pro = htons(ethertype_ip);
arp_hdr->ar_hln = 0x06;
arp_hdr->ar_pln = 0x04;
arp_hdr->ar_op = htons(1); /*arp request*/
memcpy(arp_hdr->ar_sha, matching_if->addr, sizeof(arp_hdr->ar_sha));
arp_hdr->ar_sip = matching_if->ip;
memcpy(arp_hdr->ar_tha, BROADCAST_ADDR, sizeof(arp_hdr->ar_tha));
arp_hdr->ar_tip = req->ip;
/*copy headers into int buffer*/
uint8_t buf[sizeof(sr_ethernet_hdr_t)+sizeof(sr_arp_hdr_t)];
memcpy(buf, ehdr, sizeof(sr_ethernet_hdr_t));
memcpy(buf+sizeof(sr_ethernet_hdr_t), arp_hdr, sizeof(sr_arp_hdr_t));
int retval = sr_send_packet(sr, buf, sizeof(*ehdr)+sizeof(*arp_hdr), matching_if->name);
/*free memory*/
free(ehdr);
free(arp_hdr);
return retval;
}
/*---------------------------------------------------------------------
* 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);
printf("*** -> Received packet of length %d \n",len);
int minlength = sizeof(sr_ethernet_hdr_t);
if(len < minlength)
{
fprintf(stderr, "ETHERNET header insufficient length!\n");
return;
}
else if (len > IP_MAXPACKET)
{
fprintf(stderr, "Packet length too long!\n");
return;
}
print_hdr_eth(packet);
uint8_t packet_copy[IP_MAXPACKET];
memcpy(packet_copy, packet, len);
uint16_t ethtype = ethertype(packet);
switch (ethtype)
{
case ethertype_ip:
printf("IP packet: \n");
print_hdr_ip(packet + sizeof(sr_ethernet_hdr_t));
/* SANITY CHECK -- minimum length and checksum validity */
minlength += sizeof(sr_ip_hdr_t);
if (len < minlength)
{
fprintf(stderr, "IP header insufficient length!\n");
return;
}
uint16_t ip_sum_copy;
sr_ip_hdr_t * ip_hdr = (sr_ip_hdr_t *)(packet + sizeof(sr_ethernet_hdr_t));
ip_sum_copy = ip_hdr->ip_sum;
ip_hdr->ip_sum = 0;
if (cksum(ip_hdr, sizeof(*ip_hdr))/*ntohs(ip_hdr->ip_len))*/ != ip_sum_copy)
{
fprintf(stderr, "IP checksum invalid!\n");
return;
}
fprintf(stdout, "CHECKSUM VALID!\n");
/* SANITY CHECK COMPLETE */
/* Check if packet is destined for one of our IP addresses */
struct sr_if * curr_if = sr_get_interface(sr, interface);
while (ip_hdr->ip_dst != curr_if->ip && curr_if->next != NULL)
{
curr_if = curr_if->next;
}
/* Packet IS destined for one of our IP addresses */
if(curr_if->ip == ip_hdr->ip_dst)
{
printf("THIS IP PACKET IS DESTINED FOR US!\n");
/* Is the payload ICMP ? */
if (ip_hdr->ip_p == ip_protocol_icmp)
{
sr_icmp_t11_hdr_t * icmp_hdr =
(sr_icmp_t11_hdr_t *)(packet+sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t));
/* Check if it is an ICMP request */
if (icmp_hdr->icmp_type == 8)
{
/* Is the checksum valid? */
uint16_t icmp_sum_copy = icmp_hdr->icmp_sum;
icmp_hdr->icmp_sum = 0;
if (cksum(icmp_hdr, ntohs(ip_hdr->ip_len)-sizeof(*ip_hdr)) != icmp_sum_copy)
{
printf("ICMP checksum invalid!\n");
return;
}
/* Checksum is valid! */
/* setup ICMP reply */
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)packet;
uint8_t shost_copy[ETHER_ADDR_LEN];
memcpy(shost_copy, ehdr->ether_shost, sizeof(shost_copy));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, shost_copy, sizeof(ehdr->ether_dhost));
uint32_t ip_src_cpy = ip_hdr->ip_src;
ip_hdr->ip_src = ip_hdr->ip_dst;
ip_hdr->ip_dst = ip_src_cpy;
ip_hdr->ip_ttl = 64;
ip_hdr->ip_sum = 0;
ip_hdr->ip_sum = cksum(ip_hdr, sizeof(*ip_hdr));
icmp_hdr->icmp_type = 0;
icmp_hdr->icmp_sum = cksum(icmp_hdr, ntohs(ip_hdr->ip_len)-sizeof(*ip_hdr));
/* send packet */
int retval = sr_send_packet(sr, packet, len, interface);
}
}
/* Check if it contains a TCP or UDP payload */
else if(ip_hdr->ip_p == ip_protocol_udp || ip_hdr->ip_p == ip_protocol_tcp)
{
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)packet;
uint8_t shost_copy[ETHER_ADDR_LEN];
memcpy(shost_copy, ehdr->ether_shost, sizeof(shost_copy));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, shost_copy, sizeof(ehdr->ether_dhost));
uint32_t ip_src_cpy = ip_hdr->ip_src;
ip_hdr->ip_src = ip_hdr->ip_dst;
ip_hdr->ip_dst = ip_src_cpy;
ip_hdr->ip_ttl = 64;
ip_hdr->ip_p = ip_protocol_icmp;
ip_hdr->ip_len = htons(sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t));
ip_hdr->ip_sum = 0;
ip_hdr->ip_sum = cksum(ip_hdr, sizeof(*ip_hdr));
sr_icmp_t11_hdr_t * icmp_hdr = (sr_icmp_t11_hdr_t *)malloc(sizeof(sr_icmp_t11_hdr_t));
icmp_hdr->icmp_type = 3;
icmp_hdr->icmp_code = 3;
icmp_hdr->unused = 0;
memcpy(icmp_hdr->data, packet_copy+sizeof(sr_ethernet_hdr_t), sizeof(icmp_hdr->data));
icmp_hdr->icmp_sum = 0;
icmp_hdr->icmp_sum = cksum(icmp_hdr, sizeof(*icmp_hdr));
/*copy headers into int buffer*/
uint8_t buf[sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t)];
memcpy(buf, ehdr, sizeof(sr_ethernet_hdr_t));
memcpy(buf+sizeof(sr_ethernet_hdr_t), ip_hdr, sizeof(sr_ip_hdr_t));
memcpy(buf+sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t), icmp_hdr, sizeof(sr_icmp_t11_hdr_t));
/* send packet */
int retval = sr_send_packet(sr, buf,
sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t), interface);
}
}
/* Packet is NOT destined for one of our IP addresses */
else
{
/* Decrement TTL and recompute checksum */
ip_hdr->ip_ttl -= 1;
if (ip_hdr->ip_ttl == 0) /* send ICMP time exceeded */
{
/* setup ethernet header */
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)packet;
uint8_t shost_copy[ETHER_ADDR_LEN];
memcpy(shost_copy, ehdr->ether_shost, sizeof(shost_copy));
memcpy(ehdr->ether_shost, ehdr->ether_dhost, sizeof(ehdr->ether_shost));
memcpy(ehdr->ether_dhost, shost_copy, sizeof(ehdr->ether_dhost));
/* setup IP header */
ip_hdr->ip_dst = ip_hdr->ip_src;
ip_hdr->ip_src = sr_get_interface(sr, interface)->ip;
ip_hdr->ip_ttl = 64;
ip_hdr->ip_p = ip_protocol_icmp;
ip_hdr->ip_len = htons(sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t));
ip_hdr->ip_sum = 0;
ip_hdr->ip_sum = cksum(ip_hdr, sizeof(*ip_hdr));
/* setup ICMP header */
sr_icmp_t11_hdr_t * icmp_hdr = (sr_icmp_t11_hdr_t *)malloc(sizeof(sr_icmp_t11_hdr_t));
icmp_hdr->icmp_type = 11;
icmp_hdr->icmp_code = 0;
icmp_hdr->unused = 0;
memcpy(icmp_hdr->data, packet_copy+sizeof(sr_ethernet_hdr_t), sizeof(icmp_hdr->data));
icmp_hdr->icmp_sum = 0;
icmp_hdr->icmp_sum = cksum(icmp_hdr, sizeof(*icmp_hdr));
/*copy headers into int buffer*/
uint8_t buf[sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t)];
memcpy(buf, ehdr, sizeof(sr_ethernet_hdr_t));
memcpy(buf+sizeof(sr_ethernet_hdr_t), ip_hdr, sizeof(sr_ip_hdr_t));
memcpy(buf+sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t), icmp_hdr, sizeof(sr_icmp_t11_hdr_t));
/* send packet */
int retval = sr_send_packet(sr, buf,
sizeof(sr_ethernet_hdr_t)+sizeof(sr_ip_hdr_t)+sizeof(sr_icmp_t11_hdr_t), interface);
}
ip_hdr->ip_sum = cksum(packet + sizeof(sr_ethernet_hdr_t), sizeof(*ip_hdr));
/* Longest prefix match with destination IP address */
struct sr_rt * curr = sr->routing_table;
struct sr_rt * longest_match = NULL;
while (curr->next != NULL)
{
if ((ip_hdr->ip_dst & curr->mask.s_addr) == curr->dest.s_addr)
{
if (longest_match == NULL)
longest_match = curr;
else if (longest_match != NULL && longest_match->mask.s_addr < curr->mask.s_addr)
longest_match = curr;
}
curr = curr->next;
}
if (longest_match == NULL)
{
if ((ip_hdr->ip_dst & curr->mask.s_addr) == curr->dest.s_addr)
longest_match = curr;
else /* No match found in routing table */
{
fprintf(stderr, "No match in routing table!\n");
(void)construct_icmp_net_unreach_and_send(sr, packet, interface);
return;
}
}
fprintf(stdout, "MATCHED INTERFACE NAME: %s\n", longest_match->interface);
/* Longest prefix match complete! */
/* Check ARP cache for next-hop MAC address */
struct sr_arpentry * arp_entry = sr_arpcache_lookup(&(sr->cache), longest_match->gw.s_addr);
if (arp_entry)
{
printf("ARP cache entry found!\n");
/* send ARP packet using IP->MAC mapping in cache */
/* modify ETHERNET header */
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)packet;
memcpy(ehdr->ether_dhost, arp_entry->mac, sizeof(ehdr->ether_dhost));
memcpy(ehdr->ether_shost, sr_get_interface(sr, longest_match->interface)->addr,
sizeof(ehdr->ether_shost));
int val = sr_send_packet(sr, packet, len, longest_match->interface);
free(arp_entry);
}
else
{
printf("Not found in ARP cache.\n");
/* Add packet to ARP cache queue */
struct sr_arpreq * req = sr_arpcache_queuereq(&(sr->cache),
longest_match->gw.s_addr, packet, len, longest_match->interface);
printf("longest_match->gw.s_addr is: %d\n", longest_match->gw.s_addr);
/*struct sr_if * matching_if = sr_get_interface(sr, longest_match->interface);*/
if(req)
{
printf("Request added to req queue properly!\n");
}
handle_arpreq(sr, req);
}
printf("This IP packet is NOT destined for us.\n");
}
break;
case ethertype_arp:
printf("ARP packet: \n");
print_hdr_arp(packet + sizeof(sr_ethernet_hdr_t));
sr_arp_hdr_t * arp_hdr = (sr_arp_hdr_t *)(packet + sizeof(sr_ethernet_hdr_t));
if (ntohs(arp_hdr->ar_op) == arp_op_request) /* opcode is 1 -- request */
{
/* send ARP reply if target IP address is router's interface IP */
if (arp_hdr->ar_tip == sr_get_interface(sr, interface)->ip)
{
int val = sr_send_arp_reply(sr, packet, len, interface);
fprintf(stdout, "\nRETURN VAL ==> %d\n", val);
}
}
else if (ntohs(arp_hdr->ar_op) == arp_op_reply) /* opcode is 2 -- reply */
{
printf("\n\n\nRECEIVED ARP REPLY!!!\n");
pthread_mutex_lock(&(sr->cache.lock));
struct sr_arpreq * req = sr_arpcache_insert(&(sr->cache), arp_hdr->ar_sha, arp_hdr->ar_sip);
pthread_mutex_unlock(&(sr->cache.lock));
if (req)
{
printf("IN REQ IF STATEMENT\n");
struct sr_packet * pkts = req->packets;
if (pkts)
{
printf("SENDING PACKETS\n");
/* modify ETHERNET header */
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)pkts->buf;
memcpy(ehdr->ether_dhost, arp_hdr->ar_sha, sizeof(ehdr->ether_dhost));
memcpy(ehdr->ether_shost, arp_hdr->ar_tha, sizeof(ehdr->ether_shost));
int retval = sr_send_packet(sr, pkts->buf, pkts->len, pkts->iface);
while (pkts->next)
{
pkts = pkts->next;
/* modify ETHERNET header */
ehdr = (sr_ethernet_hdr_t *)pkts->buf;
memcpy(ehdr->ether_dhost, arp_hdr->ar_sha, sizeof(ehdr->ether_dhost));
memcpy(ehdr->ether_shost, arp_hdr->ar_tha, sizeof(ehdr->ether_shost));
retval = sr_send_packet(sr, pkts->buf, pkts->len, pkts->iface);
}
}
sr_arpreq_destroy(&(sr->cache), req);
}
}
break;
default:
printf("Did not match any packet type!\n");
break;
}
}/* end sr_ForwardPacket */
int sr_send_arp_reply(struct sr_instance* sr,
uint8_t * packet,
unsigned int len,
char* interface)
{
/*
DEBUG ****
printf("\n\nBEFORE MODIFICATION: \n");
print_hdr_eth(packet);
print_hdr_arp(packet + sizeof(sr_ethernet_hdr_t));
DEBUG ****
*/
/* modify ETHERNET header */
sr_ethernet_hdr_t * ehdr = (sr_ethernet_hdr_t *)packet;
memcpy(ehdr->ether_dhost, ehdr->ether_shost, sizeof(ehdr->ether_dhost));
memcpy(ehdr->ether_shost, sr_get_interface(sr, interface)->addr, sizeof(ehdr->ether_shost));
/* modify ARP header */
sr_arp_hdr_t * ahdr = (sr_arp_hdr_t *)(packet + sizeof(sr_ethernet_hdr_t));
ahdr->ar_op = htons(2);
memcpy(ahdr->ar_tha, ahdr->ar_sha, sizeof(ahdr->ar_sha));
memcpy(ahdr->ar_sha, sr_get_interface(sr, interface)->addr, sizeof(ahdr->ar_sha));
uint32_t temp = ahdr->ar_sip;
ahdr->ar_sip = ahdr->ar_tip;
ahdr->ar_tip = temp;
/*
DEBUG ***********
printf("\n\nAFTER MODIFICATION: \n");
print_hdr_eth(packet);
print_hdr_arp(packet + sizeof(sr_ethernet_hdr_t));
DEBUG ***********
*/
/* send ARP reply with modified packet */
int val = sr_send_packet(sr, packet, len, interface);
return val;
}