-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.h
More file actions
236 lines (211 loc) · 5.38 KB
/
Copy pathnode.h
File metadata and controls
236 lines (211 loc) · 5.38 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
#ifndef __NODE_H__
#define __NODE_H__
#include "shm.h"
extern Graph graph;
extern int nodeNum;
extern int version;
extern int version_fd;
extern int distance[64];
extern int pred[64];
Routing_table* Initialize_Shared_Memory(int* ShmID){
key_t ShmKEY;
Routing_table *ShmPTR;
ShmKEY = ftok(".", 69);
*ShmID = shmget(ShmKEY, sizeof(struct RoutingTable), IPC_CREAT | 0666);
if (*ShmID < 0) {
printf("*** shmget error (server) ***\n");
exit(1);
}
ShmPTR = (Routing_table *) shmat(*ShmID, NULL, 0);
if ((Routing_table *) ShmPTR == (Routing_table *)-1) {
printf("*** shmat error (server) ***\n");
exit(1);
}
return ShmPTR;
}
Graph AddNewNode(char* fileName){
int port, dest_port;
int ret;
char lhost[HOST_MAX_LENGTH];
char rhost[HOST_MAX_LENGTH], lv[INET_ADDRSTRLEN], rv[INET_ADDRSTRLEN];
FILE *f;
f = fopen(fileName, "r");
fscanf(f, "%s %d", lhost, &port);
bool check = false;
Node node;
while (!feof(f)) {
ret = fscanf(f, "%s %d %s %s", rhost, &dest_port, lv, rv);
if (ret == EOF) {
ret = 0;
break;
}
if (!check)
{
for (int i = 0; i < graph.size; i++)
{
if (graph.node[i].port == port){
node = graph.node[i];
nodeNum = i;
node.is_on = true;
strncpy(node.host, lhost, 30);
check = true;
break;
}
}
if (!check)
{
nodeNum = graph.size;
node.is_on = true;
node.port = port;
strncpy(node.host, lhost, 30);
node.interface_size = 0;
graph.node[nodeNum] = node;
graph.size++;
}
check = true;
}
int dest_num;
bool check2 = false;
for (int i = 0; i < graph.size; i++)
{
if (graph.node[i].port == dest_port)
{
dest_num = i;
check2 = true;
break;
}
}
if(!check2){
Node node2;
node2.is_on = false;
node2.interface_size = 0;
node2.port = dest_port;
graph.node[graph.size] = node2;
graph.size = graph.size + 1;
dest_num = graph.size - 1;
}
Interface interface;
interface.dest_id = dest_num;
interface.is_up = true;
strncpy(interface.src_virtual_ip, lv, 30);
strncpy(interface.dst_virtual_ip, rv, 30);
node.interface[node.interface_size] = interface;
node.interface_size = node.interface_size + 1;
}
graph.node[nodeNum] = node;
return graph;
}
void dijkstra(int G[64][64])
{
int n = graph.size;
int startnode = nodeNum;
int cost[64][64];
int visited[64],count,mindistance,nextnode,i,j;
//pred[] stores the predecessor of each node
//count gives the number of nodes seen so far
//create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=64;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[]
for(i=0;i<n;i++)
{
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=64;
//nextnode gives the node at minimum distance
for(i=0;i<n;i++){
if(distance[i]<mindistance&&!visited[i])
{
mindistance=distance[i];
nextnode=i;
}
}
//check if a better path exists through nextnode
visited[nextnode]=1;
for(i=0;i<n;i++){
if(!visited[i]){
if(mindistance+cost[nextnode][i]<distance[i])
{
distance[i]=mindistance+cost[nextnode][i];
pred[i]=nextnode;
}
}
}
count++;
}
}
void find_forwarding_table(){
int g[64][64] = {0};
for (int i = 0; i < graph.size; i++)
{
if (graph.node[i].is_on)
{
for (int j = 0; j < graph.node[i].interface_size; j++)
{
if (graph.node[i].interface[j].is_up)
{
g[i][graph.node[i].interface[j].dest_id] = 1;
}
}
}
}
for (int i = 0; i < graph.size; i++)
{
for (int j = 0; j < graph.size; j++)
{
if (g[i][j] == 1 && g[j][i] == 0)
{
g[i][j] = 0;
}
}
}
dijkstra(g);
}
int find_source(int m){
int source_virtual_ip = 0;
while (pred[m] != nodeNum)
{
m = pred[m];
}
for (int i = 0; i < graph.node[nodeNum].interface_size; i++)
{
if(graph.node[nodeNum].interface[i].dest_id == m){
return i;
}
}
return source_virtual_ip;
}
int find_node(char ip_string[30]){
for (int i = 0; i < graph.size; i++)
{
for (int j = 0; j < graph.node[i].interface_size; j++)
{
if(strcmp(graph.node[i].interface[j].src_virtual_ip, ip_string) == 0){
return i;
}
}
}
return -1;
}
bool is_empty(){
for (int i = 0; i < graph.size; i++)
{
if (graph.node[i].is_on)
{
return false;
}
}
return true;
}
#endif