-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkstate.c
693 lines (619 loc) · 18.6 KB
/
linkstate.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG(args...)
//#define DEBUG printf
#define LOG printf
#define ERR printf
/* Datastructure to hold the
* routing cost matrix specified in the input file
*/
typedef struct {
int numOfRouters;
int **cost;
} OrigRoutingTable;
/* Datastructure to indicate links
* linkid: Indicates a hop to hop link
* linkType: Indicates a link type to classify the metric.
* linkMetric: the value of linkType metric as an integer
*/
typedef struct {
int linkid;
int linkType;
int linkMetric;
} Link;
/* Data structure to represent the
* link state packet
* contains one or more direct links of the router.
* this is supposed to be advertised to all the routers.
* but in our implmentation we read it from the OrigRoutingTable.
*/
typedef struct {
int lsAge; /* Unused since we are constructing link state from
Original Routing Table */
int lsId; /* Unused : for the same reason as above */
int lsSequence; /* Unused : for the same reason as above */
int lsLength; /* Unused : for the same reason as above */
int numOfLinks;
Link **link;
} LinkStatePacket;
/* Datastructure to represent the forwarding information
* within a router. Cost represents the linkMetric.
*/
typedef struct {
int nxtHop;
int cost;
} Route;
/* Datastructure to represent the complete routing table
* within a router. It contains the forwarding information
* as mentioned in the route.
*/
typedef struct RoutingTable {
Route **rt;
} RoutingTable;
/* This data structure represents the path from src to dst
* in terms of hops between each router node in the path.
*/
typedef struct {
int numOfHops;
Route *rt;
} RoutePath;
OrigRoutingTable *ort; /* Allocated and filled once */
LinkStatePacket *lsp; /* Ideally link states are obtained through network advertisements
but we construct it from original routing table */
RoutingTable *rtTentative; /* Transient routing table during building of confirmed routing table. */
RoutingTable *rtConfirmed; /* Confirmed routing table. */
RoutePath path;
/* Function representing the high level menu */
static int get_input()
{
int ch;
LOG ("1. Load File\n");
LOG ("2. Build Routing Table for Each Router\n");
LOG ("3. Out optimal path with minimum cost\n");
LOG ("0. Exit the program\n");
scanf("%d", &ch);
return ch;
}
/* Functio to read a line from the file
* We first find out how much we have to allocate for the buffer, starting from 80 chars
* incrementing in steps of 80 if we feel short.
* This function also returns the number of spaces between costs. Assumption is each cost
* is delimited by a single space.
*/
static char *readLine(FILE *fd, int *lineLength_, int *numOfSpaces_)
{
const int buffer_size=80;
char *line;
char *line2;
int lineIndex;
int lineLength;
int lineLength2;
int numOfSpace=0;
int err=0;
/* Read a line
* To accomodate lines of arbitrarily large numbe r of routers,
* assume tehre are 80 liines in the file, if it exceeds increase the line size by 80.
*/
lineLength = buffer_size;
line = malloc(lineLength);
memset(line, 0, lineLength);
lineIndex=0;
if (!feof(fd)) {
numOfSpace = 0;
while (!feof(fd)) {
line[lineIndex] = fgetc(fd);
if (line[lineIndex] == '\n') {
/* read line completed */
line[lineIndex] = '\0';
break;
}
else if (lineIndex == (lineLength-1)) {
/* our buffer is small, lets increment it by buffer_size */
lineLength2=lineLength+buffer_size;
line2 = realloc(line, lineLength2);
if (line2) {
/* memset the trailing additionally allocated buffer to 0 */
memset(line+lineLength, 0, buffer_size);
line = line2;
lineLength = lineLength2;
} else {
err=1;
break;
}
}
/* Assume ech cost is delimited by a single space */
if (line[lineIndex] == ' ') {
numOfSpace++;
}
lineIndex++;
}
}
if (err) {
if (line) {
free(line);
}
return NULL;
}
*lineLength_ = lineLength;
*numOfSpaces_ = numOfSpace;
return line;
}
OrigRoutingTable *allocateOrigRoutingTable(int numOfSpace)
{
OrigRoutingTable *ort;
int i;
int err=0;
/* Allocate ort */
ort = (OrigRoutingTable *) malloc(sizeof(OrigRoutingTable));
if (!ort) {
return NULL;
}
ort->numOfRouters = numOfSpace+1;
ort->cost = malloc(sizeof(int *) * ort->numOfRouters);
if (ort->cost) {
memset(ort->cost, 0,sizeof(int *) * ort->numOfRouters);
for (i = 0; i < ort->numOfRouters; i++) {
ort->cost[i] = malloc(sizeof(int) * ort->numOfRouters);
if (!ort->cost[i]) {
err = 1;
break;
}
memset(ort->cost[i], 0,sizeof(int) * ort->numOfRouters);
}
}
else {
err = 1;
}
if (err) {
if (ort->cost) {
for (i = 0; i < ort->numOfRouters; i++) {
if (ort->cost[i]) {
free(ort->cost[i]);
}
}
free(ort->cost);
}
ERR("Error in alloc routers for ort numOfRouters=%d",ort->numOfRouters);
free(ort);
return NULL;
}
return ort;
}
int parseLine(char *line, int lineLength, int numOfRouters, int *cost)
{
char *tempLine;
char *tok = NULL;
char delim[10]=" ";
int i=0;
tempLine = (char *) malloc(lineLength+1);
memset(tempLine, 0, lineLength+1);
strcpy(tempLine, line);
tok = strtok(tempLine, delim);
while (tok) {
cost[i] = atoi(tok);
tok = strtok(NULL, delim);
i++;
}
return 0;
}
/* Function to read the lines in the input file one by one
* and construct the original routing table */
static OrigRoutingTable *loadOrigRoutingTable(char *file)
{
FILE *fd;
int err=0;
int lineLength;
char *line;
int numOfRouters = 0;
int numOfSpaces;
OrigRoutingTable *ort = NULL;
int i, j;
/* Open file */
LOG("Open File %s\n", file);
fd = fopen(file, "r");
if (!fd) {
ERR("Error in opening file: %s\n", file);
return NULL;
}
/* Parse the fist line to know the size of the cost matrix. */
line = readLine(fd, &lineLength, &numOfSpaces);
if (!line) {
ERR("Error in reading line from %s\n", file);
fclose(fd);
return NULL;
}
DEBUG("Num Of Spaces: %d\n", numOfSpaces);
/* Allocate Original Routing table */
ort = allocateOrigRoutingTable(numOfSpaces);
if (!ort) {
ERR("Error in allocating ORT");
fclose(fd);
return NULL;
}
LOG("Original Routing Table is as follows\n");
/* Top Line */
LOG(" |");
for(j = 0; j < ort->numOfRouters; j++)
LOG("%2d ", ort->cost[0][j]);
LOG("\n");
/* Divider Line */
LOG("-----|");
for(j = 0; j < ort->numOfRouters; j++)
LOG("----");
LOG("\n");
parseLine(line, lineLength, ort->numOfRouters, ort->cost[0]);
LOG("%2d. |", 0);
for(j = 0; j < ort->numOfRouters; j++)
LOG("%2d ", ort->cost[0][j]);
LOG("\n");
for (i=1; i < ort->numOfRouters; i++) {
line = readLine(fd, &lineLength, &numOfSpaces);
if (!line) {
DEBUG("Error in reading line from %s\n", file);
fclose(fd);
return NULL;
}
parseLine(line, lineLength, ort->numOfRouters, ort->cost[i]);
LOG("%2d. |", i);
for(j = 0; j < ort->numOfRouters; j++)
LOG("%2d ", ort->cost[i][j]);
LOG("\n");
}
return ort;
}
/* Function to construct a link state packet */
static int constructLSP(OrigRoutingTable *ort, LinkStatePacket **lsp_)
{
int i;
int j;
LinkStatePacket *lsp;
if ((ort == NULL) || (lsp == NULL)) {
ERR("Wrong arguments ..\n");
return -1;
}
lsp = (LinkStatePacket *)
malloc(sizeof(LinkStatePacket) * ort->numOfRouters);
if (!lsp) {
DEBUG("Malloc failed..\n");
return -1;
}
memset(lsp, 0, sizeof(LinkStatePacket)* ort->numOfRouters);
for (i = 0; i < ort->numOfRouters; i++) {
lsp[i].lsId = i;
lsp[i].numOfLinks = 0;
lsp[i].link = (Link **) malloc(sizeof(Link *) * ort->numOfRouters);
if (!lsp[i].link) {
ERR("malloc error %s:%d ..\n",
__FUNCTION__, __LINE__);
}
for (j = 0; j < ort->numOfRouters; j++) {
if (ort->cost[i][j] != -1) { /* ignore non neighbours */
lsp[i].link[j] = (Link *) malloc(sizeof(Link));
lsp[i].link[j]->linkid = i*ort->numOfRouters + j;
lsp[i].link[j]->linkType = 1; /* always same */
DEBUG("LSP %d %d %d\n", i, j,ort->cost[i][j]);
lsp[i].link[j]->linkMetric = ort->cost[i][j];
lsp[i].numOfLinks++;
}
}
}
*lsp_ = lsp;
DEBUG("Link added successfully\n");
return 0;
}
static int printLSP(LinkStatePacket *lsp, int numOfRouters)
{
Link *link;
int i, j;
for (i = 0; i < numOfRouters; i++) {
DEBUG("LSP %d\n", i);
DEBUG("\tlspId = %d\n", lsp[i].lsId);
DEBUG("\tnumOfLinks = %d\n", lsp[i].numOfLinks);
for (j = 0; j < numOfRouters; j++) {
if (lsp[i].link[j]) {
DEBUG("\tLink:%d\n", j);
DEBUG("\t\tlinkid=%d\n", lsp[i].link[j]->linkid);
DEBUG("\t\tlinkType=%d\n", lsp[i].link[j]->linkType); /* always same */
DEBUG("\t\tlinkMetric=%d\n", lsp[i].link[j]->linkMetric);
}
}
}
}
void setConfirmedRoute(int src, int dst, int nxt, int cost)
{
Route *rt;
if (!rtConfirmed[src].rt[dst]) {
rtConfirmed[src].rt[dst] = (Route *) malloc(sizeof(Route));
}
rt = rtConfirmed[src].rt[dst];
if (rt) {
rt->nxtHop = nxt;
rt->cost = cost;
}
return;
}
void setTentativeRoute(int src, int dst, int nxt, int cost)
{
Route *rt;
if (!rtTentative[src].rt[dst]) {
rtTentative[src].rt[dst] = (Route *) malloc(sizeof(Route));
}
rt = rtTentative[src].rt[dst];
if (rt) {
rt->nxtHop = nxt;
rt->cost = cost;
}
return;
}
Route *getConfirmedRoute(int src, int dst)
{
return rtConfirmed[src].rt[dst];
}
Route *getTentativeRoute(int src, int dst)
{
return rtTentative[src].rt[dst];
}
Route *getLeastTentative(int src, int *nxtRtr)
{
int rid;
unsigned int leastCost=0xFFFFFFFF;
Route *leastCostRt = NULL;
DEBUG("Get Least Cost Route ..\n");
for (rid = 0; rid < ort->numOfRouters; rid++) {
if ((rtTentative[src].rt[rid]) &&
((unsigned )rtTentative[src].rt[rid]->cost < (unsigned)leastCost))
{
leastCost = rtTentative[src].rt[rid]->cost;
*nxtRtr = rid;
leastCostRt = rtTentative[src].rt[rid];
DEBUG("leastCost=%d; nxtHop=%d\n",
leastCost, *nxtRtr);
}
}
if (leastCostRt) {
rtTentative[src].rt[*nxtRtr] = NULL;
}
return leastCostRt;
}
/* Function to buils routing table using available
* LinkStatePackets.
* This function uses Dijkstra's algorithm.
*/
RoutingTable *buildRoutingTable2(int my_rid)
{
Route *rtc;
Route *rtt;
unsigned int *cost;
int src_rid;
int dst_rid;
int rid;
cost = (int *) malloc(sizeof(int) * ort->numOfRouters);
memset(cost, 0, sizeof(int) * ort->numOfRouters);
cost[my_rid] = 0;
for (rid = 0; rid < ort->numOfRouters; rid++) {
if (my_rid != rid) cost[rid] = 0xffffffff;
}
cost[my_rid] = 0;
setConfirmedRoute(my_rid, my_rid, my_rid, cost[my_rid]);
DEBUG ("--> Confirmed Route(%d, %d) is via %d at cost %d\n",
my_rid+1, my_rid+1, my_rid+1, cost[my_rid] );
/* Start with my router */
src_rid = my_rid;
while (1) {
DEBUG("!! Next Source = %d\n", src_rid+1);
for (rid = 0; rid < ort->numOfRouters; rid++) {
if (rid == src_rid) {
DEBUG("\tSelf Route(%d, %d)\n", src_rid+1, rid+1);
continue;
}
if (lsp[src_rid].link[rid] != NULL) {
/* We have a link to router */
rtc = getConfirmedRoute(my_rid, rid);
rtt = getTentativeRoute(my_rid, rid);
if ((!rtt) && (!rtc)) {
/* initialise cost */
cost[rid] = lsp[src_rid].link[rid]->linkMetric;
if (src_rid != my_rid) {
cost[rid] += cost[src_rid];
}
setTentativeRoute(my_rid, /* src */
rid, /* dst */
src_rid, /* hop */
cost[rid] /* cost to dst */);
DEBUG ("\tNew Tentative Route(%d, %d) is via %d at cost %d\n",
my_rid+1, rid+1, src_rid+1, cost[rid] );
} else if (rtt) {
int new_cost, old_cost;
Route *rtn = NULL;
/* update cost */
old_cost = cost[rid];
rtn = getConfirmedRoute(my_rid, src_rid);
if (rtn && (lsp[src_rid].link[rid]->linkMetric > -1)) {
DEBUG("newcost_to_%d (%d) = cost_to_neighbour_%d (%d) "
" + hop_distance(%d)\n",
rid, cost[rid],
src_rid,rtn->cost,
lsp[src_rid].link[rid]->linkMetric);
new_cost = rtn->cost + lsp[src_rid].link[rid]->linkMetric;
if (new_cost < old_cost) {
/* set this branch as least in tentative list */
setTentativeRoute(my_rid, /* src */
rid, /* dst */
src_rid, /* hop */
new_cost /* cost */);
DEBUG ("\tUpdate Tentative Route(%d, %d) is via %d at cost %d\n",
my_rid+1, rid+1, src_rid+1, cost[rid] );
} else {
DEBUG ("\tCost to %d from %d via %d is %d : Ignoring this route\n",
rid+1, my_rid+1, src_rid+1, cost[rid] );
}
} else {
DEBUG("\tNo route to %d from %d\n", src_rid+1, my_rid+1);
}
}
}
else {
DEBUG("\tNo route to %d from %d\n", rid+1, src_rid+1);
}
}
rtt = getLeastTentative(my_rid, &rid);
if (rtt) {
setConfirmedRoute(my_rid, rid, rtt->nxtHop, rtt->cost);
DEBUG ("--> Confirmed Route(%d, %d) is via %d at cost %d\n",
my_rid+1, rid+1, rtt->nxtHop+1, rtt->cost );
src_rid = rid;
free(rtt);
} else {
DEBUG("No more tentative routes exiting\n");
break;
}
}
return NULL;
}
RoutingTable *buildRoutingTable(int my_rid)
{
return NULL;
}
void allocRoutingTables(int numOfRouters)
{
int i;
rtConfirmed = (RoutingTable *) malloc(sizeof(RoutingTable)*numOfRouters);
memset(rtConfirmed, 0, sizeof(RoutingTable)*numOfRouters);
for (i = 0; i < numOfRouters; i++) {
rtConfirmed[i].rt = (Route **) malloc(sizeof(Route*)*numOfRouters);
memset(rtConfirmed[i].rt, 0, sizeof(Route*)*numOfRouters);
}
rtTentative = (RoutingTable *) malloc(sizeof(RoutingTable)*numOfRouters);
memset(rtTentative, 0, sizeof(RoutingTable)*numOfRouters);
for (i = 0; i < numOfRouters; i++) {
rtTentative[i].rt = (Route **) malloc(sizeof(Route*)*numOfRouters);
memset(rtTentative[i].rt, 0, sizeof(Route*)*numOfRouters);
}
}
void printRoutingTable(int my_rid)
{
int rid;
Route *rtc;
LOG ("%-10s %-10s %-10s %-10s\n",
"Source", "Destination", "NextHop", "Cost");
LOG ("%10s-%10s-%10s-%10s\n",
"----------",
"----------",
"----------",
"----------",
"----------");
for (rid = 0; rid < ort->numOfRouters; rid++) {
rtc = getConfirmedRoute(my_rid, rid);
LOG ("%-10d %-10d %-10d %-10d\n",
my_rid+1, rid+1, rtc->nxtHop+1, rtc->cost);
}
return;
}
void computePath(int srcRouterId, int dstRouterId, RoutePath *rp)
{
int currentRouter;
Route *rt;
rp->numOfHops = 0;
rp->rt = NULL;
/* we start from index 1 */
srcRouterId--;
dstRouterId--;
currentRouter=srcRouterId;
DEBUG("rp->numOfHops=%d current router=%d\n", rp->numOfHops, currentRouter+1);
while (currentRouter != dstRouterId) {
DEBUG("current router=%d\n", currentRouter+1);
rt = realloc(rp->rt, (rp->numOfHops+1) *sizeof(Route));
if (!rt) break;
rp->rt = rt;
rt = getConfirmedRoute(currentRouter, dstRouterId);
if (rt) {
DEBUG("confirmed nextHop=%d from router %d at cost=%d\n", rt->nxtHop+1, currentRouter+1, rt->cost);
memcpy(&rp->rt[rp->numOfHops], rt, sizeof(Route));
}
else {
break;
}
if (currentRouter == rt->nxtHop) { DEBUG("NxtHop==currentRouter, breaking \n"); break; }
currentRouter = rt->nxtHop;
rp->numOfHops++;
DEBUG("numOfHops=%d\n", rp->numOfHops);
}
return;
}
void printRoutePath(RoutePath *path)
{
int i;
if (path->numOfHops == 0) {
LOG("-");
} else if (path->numOfHops != 0xFF){
if ((path->numOfHops == 1) && (path->rt[0].cost == 0)) {
LOG("-");
}
else
for (i = 0; i < path->numOfHops; i++) {
LOG("-%d-", path->rt[i].nxtHop+1);
}
}
return;
}
int main(int argc, char **argv)
{
int ch=1;
int ret;
while (ch != 0) {
ch = get_input();
LOG("Your choice = %d\n", ch);
switch (ch) {
case 1:
{
char dataFile[25] = { 0 };
LOG("Please load original routing table data file\n");
scanf("%s", dataFile);
ort = loadOrigRoutingTable(dataFile);
if (!ort){
DEBUG("%s:%d error in constructing original routing table\n",
__FUNCTION__, __LINE__);
return -1;
}
DEBUG("%s:%d ort=%p &lsp=%p\n",
__FUNCTION__, __LINE__, ort, &lsp);
ret = constructLSP(ort, &lsp);
if (ret != 0) {
DEBUG("Link State Packets construction failure\n");
} else {
LOG("Link State Packets construction success\n");
printLSP(lsp, ort->numOfRouters);
}
allocRoutingTables(ort->numOfRouters);
}
break;
case 2:
{
int routerId;
LOG("Please select a router\n");
scanf("%d", &routerId);
routerId--;
buildRoutingTable2(routerId);
LOG("The routing table for router %d is\n", routerId);
printRoutingTable(routerId);
}
break;
case 3:
{
int srcRouterId;
int dstRouterId;
LOG("Please input the source and the destination router\n");
scanf("%d", &srcRouterId);
scanf("%d", &dstRouterId);
computePath(srcRouterId, dstRouterId, &path);
LOG("The shrtest path from %d to %d is %d", srcRouterId, dstRouterId, srcRouterId);
printRoutePath(&path);
LOG("%d\n", dstRouterId);
LOG ("Num of Hops = %d\n",path.numOfHops);
free(path.rt);
break;
}
}
}
}