-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.c
More file actions
428 lines (316 loc) · 11.7 KB
/
node.c
File metadata and controls
428 lines (316 loc) · 11.7 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
/**
* Implements all operation for client.
*
* @author : Probal, Debarghya, Arup
* @Filename : server.c
* @Date : 04/17/17
* @course : COP5990
* @Project # : 5
* @Usage: ./node <routerLabel> <portNum> <totalNumRouters> <discoverFile> [-dynamic]
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <netdb.h>
/* According to POSIX.1-2001, POSIX.1-2008 */
#include <sys/select.h>
/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include "node.h"
// global adjacency matrix
int **adjMat;
int main(int argc, char *argv[] ) {
// For return values for socket creation, send & receive message
int nodeSd, sendRet, dynamic = 0, i, k, debug = 0;
// sockaddr_in to send the message
struct sockaddr_in ownAddress, serveraddress, recvAddress, neighbors[NUM_NEIGHBOR]; // neighbors to make the connections
// length of sockaddr_in
socklen_t addrlen = sizeof(serveraddress);
// get the host information
struct hostent *hostptr;
// store the hostname
char hostname[128];
// For storing the argument
char *portNum, *rounterLabel, *totalNumRouters, *discoverFile, FileNameWithPath[BUFSIZE];
// Link state packets for each record in the file
allLSP allLSP, dynamicLSP;
// Usage
if ( argc < 5 ){
printf("Usage: ./node <routerLabel> <portNum> <totalNumRouters> <discoverFile> [-dynamic] [-Debug]\n");
exit(1);
}
// setting memory for the argument strings separately
rounterLabel = (char * )malloc(sizeof(char)*BUFSIZE);
portNum = (char * )malloc(sizeof(char)*BUFSIZE);
totalNumRouters = (char * )malloc(sizeof(char)*BUFSIZE);
discoverFile = (char * )malloc(sizeof(char)*BUFSIZE);
// check if dynamic
if ( argc == 6 && !strcmp(argv[5], "-dynamic") )
dynamic = 1; // setting the flag to 1
else if ( argc == 7 && !strcmp(argv[5], "-dynamic") )
dynamic = 1;
// check if debug
if ( argc == 6 && !strcmp(argv[5], "-Debug") )
debug = 1; // setting the flag to 1
else if ( argc == 7 && !strcmp(argv[6], "-Debug") )
debug = 1; // setting the flag to 1
/****** storing the arguments *****/
rounterLabel = argv[1]; // router label
portNum = argv[2]; // own port
totalNumRouters = argv[3]; // Total number of routers
discoverFile = argv[4]; // File for neighbor discovery
//converting totalNumRouters - string to int
int rowCol = atoi(totalNumRouters);
// sequence matrix
int seqMat[rowCol][rowCol];
// dynamic memory allocation for adjacency matrix
adjMat = (int **)malloc(rowCol * sizeof(int *));
for ( i = 0; i < rowCol; i++ )
adjMat[i] = (int *)malloc(rowCol * sizeof(int));
// assign -1 to each of the record of adjMat
for ( i = 0; i < rowCol; i++ ) {
for ( k = 0; k < rowCol; k++ ) {
if ( i == k ) {
// adjacency matrix
adjMat[i][k] = 0;
// sequence matrix
seqMat[i][k] = 0;
} else {
// adjacency matrix
adjMat[i][k] = inf;
// sequence matrix
seqMat[i][k] = inf;
}
}
}
if ( debug ) {
printf("matrix check\n");
printArray(rowCol, adjMat);
}
// combining the filename with path
if ( snprintf(FileNameWithPath, sizeof(FileNameWithPath), "%s%s", "routers/", discoverFile) < 0 ) {
printf("something went wrong with snprintf:");
exit(1);
}
// creating socket
if ( (nodeSd = socket(AF_INET, SOCK_DGRAM, 0)) < 0){
perror("socket creation failed");
return -1;
}
// // Get information about client:
if ( gethostname(hostname, sizeof(hostname) ) ){
printf("Error\n");
}
// get network host entry
hostptr = gethostbyname(hostname);
// hostptr = gethostbyname("127.0.0.1"); // THIS SHOULD BE REPLACED BY THE PREVIOUS LINE
// setting memory to recvAddress
memset( &recvAddress, 0, sizeof(recvAddress) );
// own address
memset( &ownAddress, 0, sizeof(ownAddress) );
ownAddress.sin_family = AF_INET;
ownAddress.sin_port = htons(atoi(portNum));//PORT NO
ownAddress.sin_addr.s_addr = htonl(INADDR_ANY);//ADDRESS
memcpy((void *)&ownAddress.sin_addr, (void *)hostptr->h_addr, hostptr->h_length);
if ( debug ) {
// printing the hostname, IP & port
printf("name: %s\n", hostptr->h_name);
printf("addr: [%s]\n", inet_ntoa(ownAddress.sin_addr));
printf("port: %d\n",ntohs(ownAddress.sin_port));
printf("router Starting service\n");
}
// binding to a specific port
if ( bind(nodeSd, (struct sockaddr *) &ownAddress, sizeof(ownAddress) ) < 0 ){
perror("Bind Fails:");
exit(1);
}
// File operation
FILE *fp = fopen(FileNameWithPath, "r");
// delimeter
const char s[2] = ", ";
char *token;
// neighbor counter
int neighborCounter = 0;
// Check if the file is open or not
if(fp == NULL)
{
perror("cannot open File:");
exit(1);
}
// Tokenize a line from the file
char line[32];
while(fgets(line, sizeof(line), fp) != NULL)
{
// loop counter
i = 0;
/********* LSP create for each record in the file *********/
// copying source router's label to allLSP
// sequence number, initially 1
allLSP.singleLSP[neighborCounter].seqNum = 1;
token = strtok(line, s);
while (token!= NULL)
{
// routerLabel
if ( i % 4 == 0 ) { // rounterLabel
strcpy(allLSP.singleLSP[neighborCounter].label, token);
} else if ( i % 4 == 1 ) { // IP_address/hostname
strcpy(allLSP.singleLSP[neighborCounter].nodeIP, token);
} else if ( i % 4 == 2 ) { // portNumber
allLSP.singleLSP[neighborCounter].nodePort = atoi(token);
} else {
allLSP.singleLSP[neighborCounter].cost = atoi(token);
}
token = strtok (NULL, s);
i++; // loop counter
}
neighborCounter++; // adding the neighbor counter
}
// initial sleep
printf("initial %d seconds delay so that every router can up & running.\n", TIMEOUT);
sleep(TIMEOUT);
// assign the number of neighbor in the LSP packet
allLSP.numberOfNeighbor = neighborCounter;
// assigning hop count to packet
allLSP.hopCount = atoi(totalNumRouters) - 1;
// copy the router source label to the packet
strcpy(allLSP.source, rounterLabel);
// packet from FILE
int j;
for ( j = 0; j < neighborCounter; j++ ) {
// initial matrix build
adjMatrixChange( adjMat, allLSP.source, allLSP.singleLSP[j].label, allLSP.singleLSP[j].cost );
seqMatrixChange( rowCol, seqMat, allLSP.source, allLSP.singleLSP[j].label, allLSP.singleLSP[j].seqNum );
if ( debug ) {
// printing the values of LSPs
printf("%s %d %s %s %d %d\n", allLSP.source, allLSP.singleLSP[j].seqNum, allLSP.singleLSP[j].label, allLSP.singleLSP[j].nodeIP, allLSP.singleLSP[j].nodePort, allLSP.singleLSP[j].cost);
}
// what if the file don't have IP, have to find out IP from hostname
hostptr = gethostbyname(allLSP.singleLSP[j].nodeIP);
// error checking
if ( hostptr == NULL ){
perror("NULL when calculating gethostbyname from file hostname:");
return 0;
}
// sockaddr_in create for each record in the file
memset( &neighbors[j], 0, sizeof(neighbors[j]) );
neighbors[j].sin_family = AF_INET;
neighbors[j].sin_port = htons(allLSP.singleLSP[j].nodePort);//PORT NO
neighbors[j].sin_addr.s_addr = inet_addr(allLSP.singleLSP[j].nodeIP);//ADDRESS
memcpy((void *)&neighbors[j].sin_addr, (void *)hostptr->h_addr, hostptr->h_length);
if ( debug ) {
printf("Neighboring connection information.\n");
printf("name: %s\n", hostptr->h_name);
printf("addr: [%s]\n", inet_ntoa(neighbors[j].sin_addr));
printf("port: %d\n",ntohs(neighbors[j].sin_port));
}
// size of sockaddr_in
addrlen = sizeof(neighbors[i]);
// send "allLSP" to every record of the neighbors
sendRet = sendto( nodeSd, &allLSP, sizeof(allLSP), 0, (struct sockaddr*)&neighbors[j],addrlen);
if ( sendRet < 0 ) {
perror("something went wrong while sending:");
exit(1);
}
}
if ( debug ) {
// printing adjacency matrix so far
printArray(rowCol, adjMat);
}
// receive & send packet
floodReceiveWithSelect( nodeSd, neighbors, rowCol, adjMat, neighborCounter, seqMat, debug );
/********* start after receive *********
1. check the hop count - DONE
2. check where it's coming from - DONE
3. send the packet if hop > 0 to others router - DONE
4. wait for any other packets - wait for a certain time - DONE
********** end after receive *********/
if ( debug ) {
// Final adjacency matrix
printArray(rowCol, adjMat);
}
// calculating shortest path & printing forwarding table for router
djikstra(adjMat, rounterLabel, atoi(totalNumRouters), debug);
// check if dynamic given in the argument
if ( dynamic ) {
/********* start if dynamic *********
To-do:
0. change a path cost - DONE
1. Make the packet with only one LSP - DONE
2. send the packet to this router's neighbors - DONE
3. other router should wait for any dynamic packet - if no dynamic in the argument - DONE
4. flooding - DONE
5. make updated adjacency matrix - DONE
6. call djikstra() with the updated adjacency matrix - DONE
7. print the forwarding table - DONE
********** end if dynamic *********/
// create a dynamic cost between 1-10
srand(time(NULL)); // should only be called once
int dynamicCost = ( rand() % 10 ) + 1;
if ( debug )
printf("dynamicCost: %d\n", dynamicCost);
/******** 1. Make the packet with only one LSP ********/
// check allLSP and make the change to it's first child
// sending only one LSP - the changed one
dynamicLSP.numberOfNeighbor = 1;
// assigning hop count to packet
dynamicLSP.hopCount = atoi(totalNumRouters) - 1;
// copy the router source label to the packet
strcpy(dynamicLSP.source, rounterLabel);
// copying allLSP's first element to dynamicLSP's first singleLSP - see allLSP struct in node.h
memcpy(&dynamicLSP.singleLSP[0], &allLSP.singleLSP[0], sizeof(dynamicLSP.singleLSP[0]));
// assign dynamic cost to dynamicLSP
// adding the dynamic cost to previous cost
// current_cost = previous_cost + dynamic_cost
dynamicLSP.singleLSP[0].cost += dynamicCost;
// add 1 to present sequence number
dynamicLSP.singleLSP[0].seqNum++;
if ( debug ) {
printf("copied allLSP\n");
printf("[%d] %s %d %s %s %d %d\n", dynamicLSP.numberOfNeighbor, dynamicLSP.source, dynamicLSP.singleLSP[0].seqNum, dynamicLSP.singleLSP[0].label, dynamicLSP.singleLSP[0].nodeIP, dynamicLSP.singleLSP[0].nodePort, dynamicLSP.singleLSP[0].cost);
}
// update own adjacency matrix
adjMatrixChange( adjMat, dynamicLSP.source, dynamicLSP.singleLSP[0].label, dynamicLSP.singleLSP[0].cost );
if ( debug ) {
// adjacency matrix with dynamic cost
printf("adjacency matrix with dynamic cost\n");
printArray(rowCol, adjMat);
}
/******** 2. send the packet to this router's neighbors ********/
for ( j = 0; j < neighborCounter; j++ ) {
// size of sockaddr_in
addrlen = sizeof(neighbors[j]);
// send the "dynamicLSP" to other routers
sendRet = sendto( nodeSd, &dynamicLSP, sizeof(dynamicLSP), 0, (struct sockaddr*)&neighbors[j], addrlen);
// error checking
if ( sendRet < 0 ) {
perror("something went wrong while sending:");
exit(1);
}
}
}
/********* start if not dynamic *********
To-do:
1. wait for any incoming dynamic packets
********** end if not dynamic *********/
printf("\nWaiting for any dynamic cost change in router.\n");
// receive & send packet
floodReceiveWithSelect( nodeSd, neighbors, rowCol, adjMat, neighborCounter, seqMat, debug );
// printing final adjacency matrix with dynamic cost
if ( debug ) {
printf("Final adjacency matrix with dynamic cost change\n");
printArray(rowCol, adjMat);
printf("Sequence matrix\n");
printArrayArray(rowCol, seqMat);
}
// calculating shortest path & printing forwarding table for router
djikstra(adjMat, rounterLabel, atoi(totalNumRouters), debug);
fclose(fp);
return 0;
}