forked from shoaib-a-khan/Distance-Vector-Routing-Protocol
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode1.c
More file actions
188 lines (148 loc) · 4.53 KB
/
node1.c
File metadata and controls
188 lines (148 loc) · 4.53 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
#include <stdio.h>
extern struct rtpkt {
int sourceid; /* id of sending router sending this pkt */
int destid; /* id of router to which pkt being sent
(must be an immediate neighbor) */
int mincost[4]; /* min cost to node 0 ... 3 */
};
extern int TRACE;
extern int YES;
extern int NO;
extern float clocktime;
int connectcosts1[4] = { 1, 0, 1, 999 };
struct distance_table
{
int costs[4][4];
} dt1;
/* students to write the following two routines, and maybe some others */
rtinit1()
{
printf("\nrtinit1 invoked at time %f \n", clocktime);
/*initialize distance table*/
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
dt1.costs[i][j] = 999;
/*updating costs based on given initial network topology*/
dt1.costs[0][0] = 1;
dt1.costs[0][1] = 1;
dt1.costs[1][1] = 0;
dt1.costs[2][1] = 1;
dt1.costs[2][2] = 1;
/*printing the initial distance table*/
printdt1(&dt1);
/*make dv packets to send to neighbours*/
struct rtpkt pkt0, pkt2;
pkt0.sourceid = 1;
pkt0.destid = 0;
pkt2.sourceid = 1;
pkt2.destid = 2;
for (int i = 0; i < 4; i++)
{
pkt0.mincost[i] = dt1.costs[i][1];
pkt2.mincost[i] = dt1.costs[i][1];
}
/*sending dv packets to neighbours*/
tolayer2(pkt0);
tolayer2(pkt2);
}
rtupdate1(rcvdpkt)
struct rtpkt *rcvdpkt;
{
printf("\nrtupdate1 invoked at time %f \n", clocktime);
if (rcvdpkt->destid != 1) //wrong destination check
return;
int j = rcvdpkt->sourceid; /*srouce id signifies the column vector in dt0 that could be updated potentially*/
int flag = 0; /*flag is set to 1 in case there is an update in the distance table*/
printf("\nPacket rcvd at node 1 from node %d \n", j);
/*traversing column j of distance table to see if it needs to be updated*/
for (int i = 0; i < 4; i++)
{
if (dt1.costs[i][j] > dt1.costs[j][1] + rcvdpkt->mincost[i])
{
dt1.costs[i][j] = dt1.costs[j][1] + rcvdpkt->mincost[i];
if(i != 1)
flag = 1;
}
}
if (flag == 1) //change in dt1 detected!
{
printf("\nDistance table at node 1 updated: \n");
printdt1(&dt1);
struct rtpkt pkt0, pkt2;
pkt0.sourceid = 1;
pkt0.destid = 0;
pkt2.sourceid = 1;
pkt2.destid = 2;
pkt0.mincost[1] = 0;
pkt2.mincost[1] = 0;
for (int i = 0; i < 4; i++)
{
if (i == 1)
continue;
pkt0.mincost[i] = min(dt1.costs[i][0], dt1.costs[i][2], dt1.costs[i][3]);
pkt2.mincost[i] = min(dt1.costs[i][0], dt1.costs[i][2], dt1.costs[i][3]);
}
printf("\nSending routing packets to nodes 0 and 2 with following mincost vector: \n");
for (int i = 0; i < 4; i++)
printf("%d\t", pkt0.mincost[i]);
printf("\n");
/*sending dv packets to neighbours*/
tolayer2(pkt0);
tolayer2(pkt2);
}
}
printdt1(dtptr)
struct distance_table *dtptr;
{
printf(" via \n");
printf(" D1 | 0 2 \n");
printf(" ----|-----------\n");
printf(" 0| %3d %3d\n",dtptr->costs[0][0], dtptr->costs[0][2]);
printf("dest 2| %3d %3d\n",dtptr->costs[2][0], dtptr->costs[2][2]);
printf(" 3| %3d %3d\n",dtptr->costs[3][0], dtptr->costs[3][2]);
}
linkhandler1(linkid, newcost)
int linkid, newcost;
/* called when cost from 1 to linkid changes from current value to newcost*/
/* You can leave this routine empty if you're an undergrad. If you want */
/* to use this routine, you'll need to change the value of the LINKCHANGE */
/* constant definition in prog3.c from 0 to 1 */
{
printf("\nlinkhandler1 invoked at time %f \n", clocktime);
if (linkid != 0) //checking valid neighbour id
return;
if (dt1.costs[0][0] == min(dt1.costs[0][0], dt1.costs[0][2], dt1.costs[0][3]) || newcost <= min(dt1.costs[0][0], dt1.costs[0][2], dt1.costs[0][3]))
{
dt1.costs[0][0] = newcost;
dt1.costs[0][1] = newcost;
printf("\nDistance table at node 1 updated: \n");
printdt1(&dt1);
struct rtpkt pkt0, pkt2;
pkt0.sourceid = 1;
pkt0.destid = 0;
pkt2.sourceid = 1;
pkt2.destid = 2;
pkt0.mincost[1] = 0;
pkt2.mincost[1] = 0;
for (int i = 0; i < 4; i++)
{
if (i == 1)
continue;
pkt0.mincost[i] = min(dt1.costs[i][0], dt1.costs[i][2], dt1.costs[i][3]);
pkt2.mincost[i] = min(dt1.costs[i][0], dt1.costs[i][2], dt1.costs[i][3]);
}
printf("\nSending routing packets to nodes 0 and 2 with following mincost vector: \n");
for (int i = 0; i < 4; i++)
printf("%d\t", pkt0.mincost[i]);
printf("\n");
/*sending dv packets to neighbours*/
tolayer2(pkt0);
tolayer2(pkt2);
}
else
{
printf("\nDistance table at node 1 updated without effecting mincost vector\n");
dt1.costs[0][0] = newcost;
dt1.costs[0][1] = newcost;
}
}