forked from sirius226/Routing-Emulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstation.c
More file actions
617 lines (515 loc) · 14 KB
/
station.c
File metadata and controls
617 lines (515 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*--------------------------------------------------------------------*/
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include <strings.h>
#include <signal.h>
#include <errno.h>
#include "common.h"
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* total number of interfaces */
int numfaces;
/* number of active interfaces */
int gudfaces;
/* interface table */
IfaceEntry ifaces[MAXIFACES];
/* get interface given ip addr */
int GetIfaceByIpAddr(IpAddr ipaddr)
{
int face;
for (face=0; face < numfaces; face++) {
if (ifaces[face].ipaddr == ipaddr)
return(face);
}
return(-1);
}
/* get interface given socket */
int GetIfaceBySocket(int socket)
{
int face;
for (face=0; face < numfaces; face++) {
if (ifaces[face].socket == socket)
return(face);
}
return(-1);
}
/* get interface given net name */
int GetIfaceBySubnet(char *subnet)
{
int face;
for (face=0; face < numfaces; face++) {
if (strcmp(ifaces[face].subnet, subnet) == 0)
return(face);
}
return(-1);
}
/* initialize iface table */
int InitIfaces(char *ifacefile)
{
FILE *fp;
char ifname[MAXSTRING];
char ipaddr[MAXSTRING];
char hwaddr[MAXSTRING];
char subnet[MAXSTRING];
char mask[MAXSTRING];
/* open the file */
fp = fopen(ifacefile, "r");
if (!fp) {
fprintf(stderr, "error : unable to open file '%s'\n", ifacefile);
return(0);
}
/* fill in iface entries */
numfaces = 0;
gudfaces = 0;
while (fscanf(fp, "%s %s %s %s %s %d", ifname,
subnet, hwaddr, ipaddr, mask,
&(ifaces[numfaces].dist)) == 6) {
ifaces[numfaces].ifname = strdup(ifname);
ifaces[numfaces].subnet = strdup(subnet);
strtohwaddr(hwaddr, ifaces[numfaces].hwaddr);
ifaces[numfaces].ipaddr = strtoipaddr(ipaddr);
ifaces[numfaces].mask = strtoipaddr(mask);
ifaces[numfaces].socket = hooktolan(subnet);
if (ifaces[numfaces].socket != -1) {
gudfaces++;
} else {
ifaces[numfaces].dist = INFINITY;
}
numfaces++;
}
/* done */
fclose(fp);
/* return number of active faces */
return(gudfaces);
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* number of entries in the routing table */
int numrouts;
/* routing table */
RouteEntry routes[MAXROUTES];
/* get a route entry given ip addr */
int GetRouteEntry(IpAddr dest)
{
int indx;
for (indx=0; indx < numrouts; indx++) {
if (routes[indx].dist < INFINITY &&
(dest & routes[indx].mask) == routes[indx].dnet) {
return(indx);
}
}
return(-1);
}
/* show routing table */
void ShowRouteTable()
{
int indx;
printf("route table at %s\n", timetostring(getcurtime()));
printf("%16s | %16s | %16s | %6s | %6s\n",
"dnet", "mask", "next", "face", "dist");
for (indx=0; indx < numrouts; indx++) {
printf("%16s | %16s | %16s | %6d | %6d\n",
ipaddrtostr(routes[indx].dnet),
ipaddrtostr(routes[indx].mask),
ipaddrtostr(routes[indx].next),
routes[indx].face, routes[indx].dist);
}
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* info on each arp entry */
typedef struct __arpentry
{
/* ip address */
IpAddr ipaddr;
/* hardware address */
HwAddr hwaddr;
} ArpEntry;
int numcaches;
ArpEntry arpcache[MAXCACHES];
/* look for an ip addr */
int GetArpEntry(IpAddr ipaddr)
{
int indx;
for (indx=0; indx <= numcaches; indx++) {
if (arpcache[indx].ipaddr == ipaddr){
return indx;
}
}
return(-1);
}
/* add an entry */
void AddArpEntry(IpAddr ipaddr, HwAddr hwaddr)
{
char indx;
char hwaddrstr[MAXSTRING];
/* check if its already there */
indx = GetArpEntry(ipaddr);
if (indx == -1) {
numcaches++;
arpcache[numcaches].ipaddr = ipaddr;
hwaddrcpy(arpcache[numcaches].hwaddr,hwaddr);
/* report the addition */
hwaddrtostr(hwaddr, hwaddrstr);
printf("added %s <=> %s to arp cache\n",
ipaddrtostr(ipaddr), hwaddrstr);
} else {
/* already there, update it */
hwaddrcpy(arpcache[indx].hwaddr,hwaddr);
/* report the change */
hwaddrtostr(hwaddr, hwaddrstr);
printf("updated %s <=> %s to arp cache\n",
ipaddrtostr(ipaddr), hwaddrstr);
}
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* info on each item in packet queue */
typedef struct __ippktq
{
/* ip pkt */
IpPkt * pkt;
/* next hop ip address */
IpAddr next;
/* outgoing interface */
int face;
/* rest of the q */
struct __ippktq * rest;
} IpPktQ;
IpPktQ *ippktwaitq = NULL;
/* add the pkt into the q */
void InsertIntoWaitQ(IpPkt *pkt, IpAddr next, int face)
{
IpPktQ *item;
/* create an item */
item = (IpPktQ *) calloc(1, sizeof(IpPktQ));
item->pkt = pkt;
item->next = next;
item->face = face;
/* put this at the head of the q */
item->rest = ippktwaitq;
ippktwaitq = item;
}
/* remove a pkt with given next hop from the q */
IpPkt *RemoveFromWaitQ(IpAddr next)
{
IpPktQ *prev;
IpPktQ *curr;
/* start at the head */
curr = ippktwaitq;
while (curr != NULL) {
/* break if found */
if (curr->next == next) break;
/* keep going */
prev = curr;
curr = curr->rest;
}
/* any such packet? */
if (curr == NULL) {
/* no such packet */
return(NULL);
} else {
IpPkt *pkt;
/* is it at the head */
if (curr == ippktwaitq) {
/* reset the head */
ippktwaitq = curr->rest;
} else {
/* delink this item */
prev->rest = curr->rest;
}
/* return the packet */
pkt = curr->pkt;
free(curr);
return(pkt);
}
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
void SendArpPkt(int face, int operation, IpAddr targetipaddr,
HwAddr targethwaddr, IpAddr senderipaddr,
HwAddr senderhwaddr)
{
ArpPkt *arppkt;
EthPkt *ethpkt;
/* form an arp packet */
arppkt = (ArpPkt *) calloc(1, sizeof(ArpPkt));
arppkt->operation = operation;
arppkt->targetipaddr = targetipaddr;
arppkt->senderipaddr = senderipaddr;
hwaddrcpy(arppkt->targethwaddr, targethwaddr);
hwaddrcpy(arppkt->senderhwaddr, senderhwaddr);
ethpkt = (EthPkt *) calloc(1, sizeof(EthPkt));
ethpkt->typ = ARP;
hwaddrcpy(ethpkt->src, senderhwaddr);
hwaddrcpy(ethpkt->dst, targethwaddr);
ethpkt->dat = (char *) arppkt;
/* send the packet */
if (ifaces[face].socket != -1) {
sendethpkt(ifaces[face].socket,ethpkt);
printf("sent --> ");
} else {
printf("disc --> ");
}
showethpkt(ethpkt);
}
void SendIpPkt(IpPkt *ippkt, IpAddr next, int face)
{
int arpi;
/* figure out hw address */
arpi = GetArpEntry(next);
if (arpi == -1) {
/* broadcast arp request */
SendArpPkt(face, ARP_REQUEST, next, BCASTADDR,
ifaces[face].ipaddr,ifaces[face].hwaddr);
/* queue the packet for later transmission */
InsertIntoWaitQ(ippkt, next, face);
} else {
EthPkt *ethpkt;
/* encapsulate ip pkt in ether pkt */
ethpkt = (EthPkt *) calloc(1, sizeof(EthPkt));
hwaddrcpy(ethpkt->dst, arpcache[arpi].hwaddr);
hwaddrcpy(ethpkt->src, ifaces[face].hwaddr);
ethpkt->typ = IP;
ethpkt->dat = (char *) ippkt;
/* send the pkt */
if (ifaces[face].socket != -1) {
sendethpkt(ifaces[face].socket,ethpkt);
printf("sent --> ");
} else {
printf("disc --> ");
}
showethpkt(ethpkt);
freeethpkt(ethpkt);
}
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* process an arp packet */
void ProcessArpPkt(ArpPkt *pkt, int face)
{
IpPkt *ippkt;
/* could be a request or a reply */
if (pkt->operation == ARP_REQUEST) {
/* am i the target? */
if (ifaces[face].ipaddr != pkt->targetipaddr) {
/* not for us */
return;
}
/* we are the target. should respond */
SendArpPkt(face, ARP_RESPONS, pkt->senderipaddr,
pkt->senderhwaddr, ifaces[face].ipaddr,
ifaces[face].hwaddr);
/* update the cache */
AddArpEntry(pkt->senderipaddr, pkt->senderhwaddr);
} else {
/* update the cache */
AddArpEntry(pkt->senderipaddr, pkt->senderhwaddr);
}
/* any pkts pending for this sender */
while (ippkt = RemoveFromWaitQ(pkt->senderipaddr)) {
SendIpPkt(ippkt, pkt->senderipaddr, face);
}
}
/* process an ip packet */
void ProcessIpPkt(IpPkt *pkt)
{
int routindx;
/* see if its meant for us */
if (pkt->dst == ifaces[0].ipaddr) {
/* for us. display contents */
printf("%s : %s", ipaddrtoname(pkt->src), pkt->dat);
freeippkt(pkt);
return;
}
/* not for us. need to route */
routindx = GetRouteEntry(pkt->dst);
if (routindx == -1) {
printf("no route to %s\n", ipaddrtoname(pkt->dst));
freeippkt(pkt);
return;
}
/* found a route. figure out nexthop */
if (routes[routindx].next == 0) {
/* dest can be reached directly */
SendIpPkt(pkt, pkt->dst, routes[routindx].face);
} else {
SendIpPkt(pkt,routes[routindx].next, routes[routindx].face);
}
}
/* process ether packet */
void ProcessEthPkt(EthPkt *pkt, int frsock)
{
int face;
/* accept only if our unicast packet or broadcast packet */
face = GetIfaceBySocket(frsock);
if (hwaddrcmp(pkt->dst, BCASTADDR)
&& hwaddrcmp(pkt->dst, ifaces[face].hwaddr)) {
/* not for us, just ignore */
printf("disc --> ");
showethpkt(pkt);
freeethpkt(pkt);
return;
}
printf("rcvd --> ");
showethpkt(pkt);
/* action depends on packet type */
switch (pkt->typ) {
case ARP:
ProcessArpPkt((ArpPkt *) pkt->dat, face);
freeethpkt(pkt);
break;
case IP:
ProcessIpPkt((IpPkt *) pkt->dat);
free(pkt);
break;
case DVRP:
ProcessDvrpPkt((DvrpPkt *) pkt->dat, face);
freeethpkt(pkt);
break;
}
}
/* process the keyboard input */
int ProcessText(char *text)
{
char * destname;
IpAddr destipaddr;
IpPkt * ippkt;
char * textcp;
/* figure out the dest host */
destname = text;
text = index(text, ' ');
if (text == NULL) {
fprintf(stderr, "error: message undecipherable\n");
return;
}
*text = '\0';
text++;
/* figure out dest's ip address */
destipaddr = nametoipaddr(destname);
if (destipaddr == 0) {
fprintf(stderr, "error: no host named '%s'\n", destname);
return;
}
/* form an ip packet */
ippkt = (IpPkt *) calloc(1, sizeof(IpPkt));
ippkt->src = ifaces[0].ipaddr;
ippkt->dst = destipaddr;
ippkt->len = strlen(text);
textcp = (char *) calloc(1,MAXSTRING);
strcpy(textcp,text);
ippkt->dat = textcp;
/* treat it like an incoming packet */
ProcessIpPkt(ippkt);
}
/*--------------------------------------------------------------------*/
/*--------------------------------------------------------------------*/
/* interval after which neighbor is marked dead */
int deadint;
/* distance vector refresh interval */
/* we fix this to be 1/3 of deadint */
int refreshint;
/* to or not to split the horizon */
int splithorizon;
/* alarm signal handler */
void timeout();
int main(int argc, char *argv[])
{
int i;
fd_set livesdset;
int livesdmax;
char ifacesfile[MAXSTRING];
char hostsfile[MAXSTRING];
/* check usage */
// if (argc != 4) {
// printf("usage : %s <name> <deadinterval> <splithorizon?>\n", argv[0]);
// exit(1);
//}
/* remember the options */
//deadint = atoi(argv[2]);
//refreshint = deadint/3;
//splithorizon = atoi(argv[3]);
/* init interfaces */
sprintf(ifacesfile, "%s/%s.if", CONFIGPATH, argv[1]);
if (!InitIfaces(ifacesfile))
exit(1);
/* init arp cache */
numcaches = 0;
/* init hosts addresses */
sprintf(hostsfile, "%s/%s", CONFIGPATH, HOSTSFILE);
if (!inithosts(hostsfile))
exit(1);
/* init distance table */
InitDistTable();
/* set the alarm */
setalarm(refreshint);
/* setup alarm signal handler */
signal(SIGALRM, timeout);
/* mimic a timeout right now */
timeout();
/* prepare list of live sockets */
FD_ZERO(&livesdset);
livesdmax = 0;
for (i=0; i < numfaces; i++) {
if (ifaces[i].socket == -1)
continue;
FD_SET(ifaces[i].socket, &livesdset);
if (ifaces[i].socket > livesdmax)
livesdmax = ifaces[i].socket;
}
/* watch out stdin too */
FD_SET(0, &livesdset);
/* keep processing packets */
while (1) {
int frsock, tosock;
fd_set readset;
/* wait for packets */
memcpy(&readset, &livesdset, sizeof(livesdset));
if (select(livesdmax+1, &readset, NULL, NULL, NULL) == -1) {
if (errno == EINTR) continue;
perror("select");
exit(1);
}
/* disable alarm */
sighold(SIGALRM);
printf("========== start of packet processing ==========\n");
/* look for keyboard input */
if (FD_ISSET(0, &readset)) {
char bufr[MAXSTRING];
fgets(bufr, MAXSTRING, stdin);
ProcessText(bufr);
}
/* figure out the socket and read from it */
for (frsock=3; frsock <= livesdmax; frsock++) {
if (FD_ISSET(frsock, &readset)) {
EthPkt *ethpkt;
/* get the pkt */
ethpkt = recvethpkt(frsock);
if (!ethpkt) {
int face;
/* disconnect */
face = GetIfaceBySocket(frsock);
printf("disconnect: hub of net '%s' is down.\n",
ifaces[face].subnet);
close(ifaces[face].socket);
FD_CLR(ifaces[face].socket, &livesdset);
ifaces[face].socket = -1;
gudfaces--;
if (gudfaces == 0) {
printf("all hubs down. me too.\n");
exit(0);
}
MarkFaceDown(face);
} else {
/* process the pkt */
ProcessEthPkt(ethpkt, frsock);
}
}
}
/* enable alarm */
sigrelse(SIGALRM);
printf("========== end of packet processing ==========\n");
}
}
/*--------------------------------------------------------------------*/