-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.c
467 lines (369 loc) · 12.1 KB
/
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
// PATRAȘ ANTON-FABIAN
// 324CB
// MARTIE 2020
#include "skel.h"
#include <sys/types.h>
#include <sys/stat.h>
#define IP_OFF (sizeof(struct ether_header))
#define ARP_OFF (sizeof(struct ether_header))
#define ARP_MESSAGE_OFF (sizeof(struct ether_header) + sizeof(struct ether_arp))
#define ICMP_OFF (IP_OFF + sizeof(struct iphdr))
#define ARP_TABLE_CAP 100
#define MAC_LENGTH 6
#define IP_LENGTH 4
#define MAX_TTL 64
struct arp_entry {
int32_t ip;
uint8_t mac[MAC_LENGTH];
};
// sould store host bite order addresses
struct arp_entry* init_arp_table() {
struct arp_entry* arpTable = calloc (ARP_TABLE_CAP,
sizeof(struct arp_entry));
DIE(arpTable == NULL, "calloc arptable");
return arpTable;
}
// size has to be no of crt entries
// returns 0 on succes
// returns -1 of the IP Address has no associated entry
int search_arp_entry(struct arp_entry* arpTable,
int size,
struct arp_entry* arpEntry,
int32_t ip) {
int i;
for (i = 0; i < size; i++) {
if((arpTable + i)->ip == ip) {
memcpy(arpEntry, arpTable + i, sizeof(struct arp_entry));
return 0;
}
}
return -1;
}
// adds an arp entry to the arp table
// return 0 on succes
int add_arp_entry(struct arp_entry* arpTable,
int* size,
int32_t ip,
uint8_t* mac) {
DIE((*size >= ARP_TABLE_CAP), "size arp table");
struct arp_entry aux;
aux.ip = ip;
//printf("~~~~~seg~~~~\n");
memcpy(&(aux.mac), mac, MAC_LENGTH);
memcpy((arpTable + *size), &aux, sizeof(struct arp_entry));
(*size)++;
return 0;
}
// generates in reply an arp request for ip
// which is to be sent on interface interface
// should work all the time xdddd
int generate_arp_request(int interface,
uint32_t ip,
packet* reply) {
reply->len = ARP_MESSAGE_OFF;
struct ether_header *eth_hdr = (struct ether_header *)reply->payload;
struct ether_arp* reply_ether_arp_header = (struct ether_arp *)
((reply->payload) + ARP_OFF);
// broadcast mac NBO/HBO
uint8_t b_mac[] = {255, 255, 255, 255, 255, 255};
uint8_t mac[MAC_LENGTH];
// mac of the interface
get_interface_mac(interface, mac);
// ip of interface
struct in_addr addr;
inet_aton(get_interface_ip(interface), &addr);
uint32_t s_ip = addr.s_addr;
// ethernet header
memcpy(eth_hdr->ether_shost, mac, MAC_LENGTH);
memcpy(eth_hdr->ether_dhost, b_mac, MAC_LENGTH);
eth_hdr->ether_type = htons(ETHERTYPE_ARP);
// arp header
reply_ether_arp_header->arp_hrd = htons(ARPHRD_ETHER); // Ethernet
reply_ether_arp_header->arp_pro = htons(ETHERTYPE_IP); // IPv4
reply_ether_arp_header->arp_hln = MAC_LENGTH; //
reply_ether_arp_header->arp_pln = IP_LENGTH; //
reply_ether_arp_header->arp_op = htons(ARPOP_REQUEST); // ARP code
memcpy(reply_ether_arp_header->arp_sha, mac, MAC_LENGTH); // s mac
memset(reply_ether_arp_header->arp_tha, 0, MAC_LENGTH);
memcpy(reply_ether_arp_header->arp_spa, &s_ip, sizeof(uint32_t)); // s ip
memcpy(reply_ether_arp_header->arp_tpa, &ip, sizeof(uint32_t)); //
return 0;
}
// return 0 on succes
// idk when it fails?
// "returns" the reply packet based on what if found on
// packet m
// it should be called with m - the packet whom to respond to
// it does not send the packet
int generate_arp_reply(packet m, packet* reply) {
// copy from source packet
memcpy(reply, &m, sizeof(packet));
// source headers
struct ether_header *eth_hdr = (struct ether_header *)m.payload;
struct ether_arp* ether_arp_header = (struct ether_arp *)
(m.payload + ARP_OFF);
// getting the interface mac
uint8_t mac[MAC_LENGTH];
get_interface_mac(m.interface, mac);
// reply headers
struct ether_header *reply_eth_hdr = (struct ether_header *)
(reply->payload);
struct ether_arp* reply_ether_arp_header = (struct ether_arp *)
((reply->payload) + ARP_OFF);
// setting the packet
// ether header
memcpy(reply_eth_hdr->ether_dhost, eth_hdr->ether_shost, MAC_LENGTH);
memcpy(reply_eth_hdr->ether_shost, mac, MAC_LENGTH);
reply_eth_hdr->ether_type = eth_hdr->ether_type;
// arp header
memcpy(reply_ether_arp_header->arp_sha,
&mac, MAC_LENGTH);
memcpy(reply_ether_arp_header->arp_tha,
ether_arp_header->arp_sha, MAC_LENGTH);
memcpy(reply_ether_arp_header->arp_spa,
ether_arp_header->arp_tpa, IP_LENGTH);
memcpy(reply_ether_arp_header->arp_tpa,
ether_arp_header->arp_spa, IP_LENGTH);
reply_ether_arp_header->arp_op = htons(ARPOP_REPLY);
return 0;
}
// return 0 on succes
// dies on error
// "returns" the ip associated with the interface
// in uint32_t format in NETWORK BYTE ORDER
// working
uint32_t get_ip_uint32(int interface, int* ip) {
char* ip_char;
ip_char = get_interface_ip(interface);
int rc = 0;
struct in_addr in;
rc = inet_aton(ip_char, &in);
DIE(rc == -1, "get_ip");
*ip = in.s_addr;
return 0;
}
// returns 4 on succes, forward, ready to be sent
// returns 3 on succes, host_unreachable
// returns 2 on succes, ttl
// returns 1 on succes, ICMP_REPLY
// returns 0 on succes, forward, but has to wait for arp
// returns -1 on some kind of failure
// wrong checksum is a failure so the router
// drops the packet
int forward_packet(packet* m,
packet* reply,
TNode* rt,
struct arp_entry* arpt,
queue q,
int arp_size) {
// headere packet primit
struct ether_header *eth_hdr = (struct ether_header *)m->payload;
struct ip* ip_hdr = (struct ip*) (m->payload + IP_OFF);
//struct icmphdr *icmp_hdr = (struct icmphdr *)(m->payload + ICMP_OFF);
// headere packet care trb trimis
struct ether_header* r_eth_hdr = (struct ether_header *)reply->payload;
struct ip* r_ip_hdr = (struct ip*) (reply->payload + IP_OFF);
struct icmphdr* r_icmp_hdr = (struct icmphdr *)(reply->payload + ICMP_OFF);
uint16_t check = ip_checksum((void*)ip_hdr, sizeof(struct ip));
if (check != 0) {
return -1;
}
memcpy(reply, m, sizeof(packet));
reply->len = m->len;
// daca e pt mine
int my_ip;
get_ip_uint32(m->interface, &my_ip);
uint8_t mac[MAC_LENGTH];
get_interface_mac(m->interface, mac);
if(r_ip_hdr->ip_dst.s_addr == my_ip) {
memcpy(r_eth_hdr->ether_dhost, eth_hdr->ether_shost, MAC_LENGTH);
memcpy(r_eth_hdr->ether_shost, eth_hdr->ether_dhost, MAC_LENGTH);
// ip hdr
r_ip_hdr->ip_ttl = MAX_TTL;
r_ip_hdr->ip_sum = 0;
struct in_addr swap;
memcpy(&swap, &(r_ip_hdr->ip_src), sizeof(struct in_addr));
memcpy(&(r_ip_hdr->ip_src), &(r_ip_hdr->ip_dst), sizeof(struct in_addr));
memcpy(&(r_ip_hdr->ip_dst), &swap, sizeof(struct in_addr));
r_ip_hdr->ip_sum = ip_checksum(r_ip_hdr, sizeof(struct ip));
// icmp hdr
r_icmp_hdr->type = ICMP_ECHOREPLY;
r_icmp_hdr->code = 0;
r_icmp_hdr->checksum = 0;
r_icmp_hdr->checksum = ip_checksum(r_icmp_hdr, sizeof(struct icmphdr));
return 1;
}
if(r_ip_hdr->ip_ttl == 1) {
reply->len = ICMP_OFF + sizeof(struct icmphdr);
// ether hdr
memcpy(r_eth_hdr->ether_dhost, eth_hdr->ether_shost, MAC_LENGTH);
memcpy(r_eth_hdr->ether_shost, mac, MAC_LENGTH);
// ip hdr
struct in_addr swap;
r_ip_hdr->ip_v = IP_LENGTH;
r_ip_hdr->ip_len = htons(sizeof(struct ip) + sizeof(struct icmphdr));
r_ip_hdr->ip_p = 1;
r_ip_hdr->ip_ttl = MAX_TTL;
r_ip_hdr->ip_tos = 0;
memcpy(&swap, &(r_ip_hdr->ip_src), sizeof(struct in_addr));
memcpy(&(r_ip_hdr->ip_src), &(r_ip_hdr->ip_dst), sizeof(struct in_addr));
memcpy(&(r_ip_hdr->ip_dst), &swap, sizeof(struct in_addr));
r_ip_hdr->ip_sum = 0;
r_ip_hdr->ip_sum = ip_checksum(r_ip_hdr, sizeof(struct ip));
// icmp hdr
r_icmp_hdr->type = ICMP_TIME_EXCEEDED;
r_icmp_hdr->code = 0;
r_icmp_hdr->checksum = 0;
r_icmp_hdr->checksum = ip_checksum(r_icmp_hdr, sizeof(struct icmphdr));
return 2;
}
uint32_t next_hop;
int interface;
int err_s;
err_s = search_trie(rt, ip_hdr->ip_dst.s_addr, &next_hop, &interface);
// host unreachable
if(err_s == -1) {
reply->len = ICMP_OFF + sizeof(struct icmphdr);
// ether hdr
memcpy(r_eth_hdr->ether_dhost, eth_hdr->ether_shost, MAC_LENGTH);
memcpy(r_eth_hdr->ether_shost, eth_hdr->ether_dhost, MAC_LENGTH);
// ip hdr
r_ip_hdr->ip_p = 1;
r_ip_hdr->ip_ttl = MAX_TTL;
r_ip_hdr->ip_sum = 0;
r_ip_hdr->ip_len = htons(sizeof(struct ip) + sizeof(struct icmphdr));
struct in_addr swap;
memcpy(&swap, &(r_ip_hdr->ip_src), sizeof(struct in_addr));
memcpy(&(r_ip_hdr->ip_src), &(r_ip_hdr->ip_dst), sizeof(struct in_addr));
memcpy(&(r_ip_hdr->ip_dst), &swap, sizeof(struct in_addr));
r_ip_hdr->ip_sum = ip_checksum(r_ip_hdr, sizeof(struct ip));
// icmp hdr
r_icmp_hdr->type = 3;
r_icmp_hdr->code = 0;
r_icmp_hdr->checksum = 0;
r_icmp_hdr->checksum = ip_checksum(r_icmp_hdr, sizeof(struct icmphdr));
return 3;
}
struct arp_entry arp_entry;
int err_a;
err_a = search_arp_entry(arpt, arp_size, &arp_entry, next_hop);
if(err_a == -1) {
// print
// insert in queue
packet* p = (packet*) calloc(1, sizeof(packet));
DIE(p == NULL, "calloc p");
memcpy(p, m, sizeof(packet));
queue_enq(q, p);
// generate and send arp request
packet request;
generate_arp_request(interface, next_hop, &request);
send_packet(interface, &request);
return 0;
}
// usual forward
uint8_t mac2[MAC_LENGTH];
get_interface_mac(interface, mac2);
// ip header
reply->interface = interface;
// checksum update via incremental updates
// as stated in RFC1624 [Eqn. 4]
// acest "-" e pe aritmetica lui 2
// iar ca sa compensez sa fie pe aritmetica lui 1
// scad inainte sa neg bitii, ca atunci cand se
// efectueaza scaderea pe complementul lui 2 (adunare cu negat + 1)
// 1 se duce cu 1 si face doar adunare cu negatu -> scaderea pe
// complementul lui 1
// suma e pe 16 bits, ttl pe 8, nu ma intereseaza ceilalti 8
// pentru ca nu se schimba (nu ar trebui sa-i schimb la o simpla forwardare)
r_ip_hdr->ip_sum -= ~(r_ip_hdr->ip_ttl - 1);
r_ip_hdr->ip_ttl --;
r_ip_hdr->ip_sum -= r_ip_hdr->ip_ttl;
// ether hdr
memcpy(r_eth_hdr->ether_shost, mac2, MAC_LENGTH);
memcpy(r_eth_hdr->ether_dhost, arp_entry.mac, MAC_LENGTH);
return 4;
}
int main(int argc, char *argv[]) {
setvbuf(stdout, NULL, _IONBF, 0);
packet m;
int rc;
int rt_size = 0;
TNode* rt = parse_routing_table_trie(&rt_size);
// initializing the arp table
struct arp_entry* arpt = init_arp_table();
// arp table currentyl holds 0 entries
int arpt_size = 0;
// init
init();
queue q = queue_create();
int my_ip[4];
int i;
for(i = 0; i < 4; i++) {
get_ip_uint32(i, my_ip + i);
}
while (1) {
rc = get_packet(&m);
DIE(rc < 0, "get_message");
struct ether_header *eth_hdr = (struct ether_header *)m.payload;
if(ntohs(eth_hdr->ether_type) == ETHERTYPE_ARP) {
struct ether_arp* ether_arp_header =
(struct ether_arp *)(m.payload + ARP_OFF);
// ARP request is detected
if(ntohs(ether_arp_header->arp_op) == ARPOP_REQUEST) {
// testing to see if arp req is for me
uint32_t ip;
memcpy(&ip, ether_arp_header->arp_tpa, sizeof(uint32_t));
if(ip == my_ip[m.interface]) {
// reply to arp req
uint32_t s_ip;
memcpy(&s_ip, ether_arp_header->arp_spa, sizeof(uint32_t));
packet reply;
generate_arp_reply(m, &reply);
send_packet(m.interface, &reply);
} else {
DIE(1, "IDK man");
}
} else if (ntohs(ether_arp_header->arp_op) == ARPOP_REPLY) {
// add entry to arp_table
uint32_t ip;
memcpy(&ip, ether_arp_header->arp_spa, sizeof(uint32_t));
struct arp_entry result;
int ec;
ec = search_arp_entry(arpt, arpt_size, &result, ip);
if(ec == -1) {
add_arp_entry(arpt, &arpt_size, ip, ether_arp_header->arp_sha);
} else {
DIE(1, "IDK man");
}
if(!queue_empty(q)) {
packet* p2;
p2 = (packet*) queue_deq(q);
packet repl;
int err_s;
err_s = forward_packet(p2, &repl, rt, arpt, q, arpt_size);
if(err_s == 4) {
send_packet(repl.interface, &repl);
} else {
DIE(1, "IDK man");
}
}
}
// IP is detected
} else if (ntohs(eth_hdr->ether_type) == ETHERTYPE_IP) {
packet repl;
int fw;
fw = forward_packet(&m, &repl, rt, arpt, q, arpt_size); // ???
/*if (fw == -1) {
//continue;
} else if (fw == 1 || fw == 2 || fw == 3 || fw == 4) {
send_packet(repl.interface, &repl);
//continue;
} else if (fw == 0) {
//continue;
}*/
if(fw != -1 && fw != 0) {
send_packet(repl.interface, &repl);
}
}
}
}