-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrip-th.c
564 lines (426 loc) · 12.9 KB
/
rip-th.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
#define MAX 1000
static int num_nodes = 6;
static int router_valid[MAX];
static int s;
char * file;
char * temp;
typedef struct link{
int data;
struct link *next;
}queue;
typedef struct router{
int * dest;
int * dist;
int * next;
}router;
router * own;
typedef struct thread_info{
pthread_t thread_id;
int thread_no;
void *argument;
}thread;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void pretty_error(char * message){
fprintf(stderr, " Error: %s\n ", message );
}
void display_own(int node){
//Displays the routing table of n'th node
printf("\n\nDistance Vector Table for %d : \n",node+1 );
if(router_valid[node] == 0){
printf(" ROUTER CURRENTLY INVALIDATED\n");
return;
}
printf("DEST \t VALID \tDIST \t NEXT");
for(int i = 0; i < num_nodes; i++){
if (own[node].next[i]!=0)
printf("\n%d \t %d \t %d \t %d",own[node].dest[i],router_valid[i],own[node].dist[i],own[node].next[i] );
else
printf("\n%d \t %d \t %d \t -",own[node].dest[i],router_valid[i],own[node].dist[i]);
}
printf("\n");
}
void get_init_table(FILE*fp,int file_no){
//Initialise values to the table from the input file
char input[10];
int i=0;
while (fgets(input,10,fp)){
int a,b,c;
sscanf(input,"%d%d%d",&a,&b,&c);
own[file_no].dest[i]=a;
own[file_no].dist[i]=b;
own[file_no].next[i]=c;
i+=1;
}
}
void enqueue(queue **front,queue** rear,int data){
//Enqueues the required node for processing by each thread
queue *temp;
temp=(queue *)malloc(sizeof(queue));
temp->next=NULL;
temp->data=data;
if(*rear==NULL)
*front=*rear=temp;
else {
queue *p=*front;
if((*rear)->data==data)return;
while(p!=*rear){
if (p->data==data)return;
p=p->next;
}
(*rear)->next=temp;
(*rear)=temp;
}
}
int dequeue(queue **front,queue** rear){
//Dequeues the node for a thread
queue * p=*front;
int data=0;
if(p==NULL)return -1;
if(p->next!=NULL) {
data=p->data;
p=p->next;
free(*front);
*front=p;
}
else {
data=p->data;
free(*front);
*front=*rear=NULL ;
}
return data;
}
void exchange_values(int file_no){
//This is where each thread will interact with the remaining
//nodes and update its table if necessary
//if it's an invalid router, notify all it's direct links and those nodes
//that have it as a next hop, that they are no longer reachable from itself.
//Return peacefully. Obtain locks during the entire process
pthread_mutex_lock(&mutex);
if (router_valid[file_no] == 0){
for(int node=0; node<num_nodes; node++){
if (node == file_no)
continue;
if (own[file_no].dist[node] == 1){
//Preserve your own link but fail the link from their side
own[node].dist[file_no] = 16;
own[node].next[file_no] = 0;
}
//For each subnode from this router, if these two have a next_hop
//at the faulty router, then invalidate the route
for (int subnode = 0; subnode<num_nodes; subnode++){
if (node == subnode || subnode == file_no)
continue;
else if (own[node].next[subnode] == file_no + 1){
printf("\n Subnode %d delinked from %d ",subnode+1,node+1);
own[node].next[subnode] = 0;
own[node].dist[subnode] = 16;
}
}
}
}
//else this is a valid router
else{
queue *front = NULL,*rear=NULL;
for(int node=0; node<num_nodes; node++){
if (node == file_no)
continue;
//if the connected router under consideration is faulty
//set its distance as unreachable from the host
if (own[file_no].dist[node] == 1 && router_valid[node] == 0){
own[file_no].dist[node] = 16;
own[file_no].next[node] = 0;
continue;
}
//if its a valid router, then it may have been a direct link to
//the given router before, check for that case!
//if it was direct before, it needs to be direct now
if (router_valid[node] && own[node].dist[file_no] == 1){
own[file_no].dist[node] = 1;
own[file_no].next[node] = 0;
}
//else if it's a normal direct link
// enqueue it to receive updates
if (own[file_no].dist[node] == 1 && router_valid[node])
enqueue(&front,&rear,node);
}
int node = 0;
while((node = dequeue(&front,&rear))!=-1)
for(int k=0; k<num_nodes; k++){
//can't utilise an invalid router as next hop
if (router_valid[k] == 0 ){
own[file_no].dist[k] = 16;
own[file_no].next[k] = 0;
}
if ((own[node].dist[k] + 1 < own[file_no].dist[k]) && router_valid[k]){
own[file_no].next[k] = node + 1;
own[file_no].dist[k] = own[node].dist[k] + 1;
}
}
}
pthread_mutex_unlock(&mutex);
}
void * thread_work(void *arg){
//Each of the created thread has to do this work
char * my_file = (char*)arg;
int file_no = my_file[0]-'0'-1;
//Critical Section Starts
long time_th=10000;
while(time_th--)
exchange_values(file_no);
//Critical Section Ends
// printf("Thread over with file: %d \n",file_no+1);
pthread_exit(NULL);
}
char* get_file_id(int file_id){
//Returns the file name for a particular thread
if (file_id==0) return "1.txt";
else if (file_id==1) return "2.txt";
else if (file_id==2) return "3.txt";
else if (file_id==3) return "4.txt";
else if (file_id==4) return "5.txt";
else if (file_id==5) return "6.txt";
char arg= file_id +1 + '0';
temp = (char *)calloc(5,sizeof(char));
temp[0]=arg;
temp[1]='.',temp[2]='t',temp[3]='x',temp[4]='t';
return temp;
}
void store_initial_files_in_table(){
/*This function stores the initial file data into tables indexed by their id*/
char * my_file=NULL; int file_no=0;
for(int i=0; i<num_nodes; i++){
my_file = get_file_id(i);
file_no=my_file[0]-'0'-1;
// printf("Ich will: %d of %s\n",file_no,my_file );
FILE *fp=fopen(my_file,"r");
assert (fp!=NULL);
get_init_table(fp,file_no);
fclose(fp);
}
}
void start_threads(int verbose){
//This function starts the threads each time a change occurs
//This will automatically call the update routine
pthread_attr_t attr;
s = pthread_attr_init(&attr);
assert (s == 0);
thread *tid=(thread *)calloc(num_nodes,sizeof(thread));
assert (tid!=NULL);
for(int i=0; i<num_nodes; i++){
tid[i].thread_no=i+1;
tid[i].argument=get_file_id(i);
pthread_create(&tid[i].thread_id,&attr,thread_work,tid[i].argument);
}
pthread_attr_destroy(&attr);
for(int i=0; i<num_nodes; i++)
pthread_join(tid[i].thread_id,NULL);
//The main thread will continuously display the output on the current screen
if (verbose)
for(int i=0; i<num_nodes; i++)
display_own(i);
}
void initialise_router(int i){
//Initialises new router in accordance
own[i].dest=(int*)malloc(sizeof(int)*MAX);
own[i].dist=(int*)malloc(sizeof(int)*MAX);
own[i].next=(int*)malloc(sizeof(int)*MAX);
for(int existing=0; existing<num_nodes-1; existing++ ){
own[existing].dest[i]=num_nodes;
own[existing].dist[i]=16;
own[existing].next[i]=0;
}
for(int existing=0; existing<num_nodes-1; existing++ ){
own[i].dest[existing]=existing+1;
own[i].dist[existing]=16;
own[i].next[existing]=0;
}
own[i].dest[i]=i+1;
own[i].dist[i]=0;
own[i].next[i]=0;
}
void add_router(){
//Adds a new router to the network
num_nodes+=1;
printf("Router: %d configuration\n",num_nodes);
initialise_router(num_nodes-1);
// int direct[num_nodes];
printf("Enter direct links (1-%d): (0 to exit) \n",num_nodes-1);
while((scanf("%d",&s))){
if (s==0)break;
own[num_nodes-1].dist[s-1]=1;
own[s-1].dist[num_nodes-1]=1;
}
start_threads(1);
}
void fail_link(int pre_src, int pre_dest, int interactive){
//Removes a working link from the network
int src,dest;
if (interactive){
printf("Enter source: \t"); scanf("%d",&src);
printf("Enter destination: "); scanf("%d",&dest);
src -= 1;
dest -= 1;
printf("f:: %d\n",num_nodes);
if (src > num_nodes){
pretty_error(" Source router doesn't exist\n");
return;
}
if (dest > num_nodes){
pretty_error(" Destination router doesn't exist\n");
return;
}
}
else {
src = pre_src;
dest = pre_dest;
}
//Set infinity from source to the dest
//or if the next hop to reach it is the dest
for(int node=0; node<num_nodes; node++ ){
if (node==src) continue;
if (own[src].next[node] == dest+1 || node==dest){
own[src].dist[node]=16;
own[src].next[node]=0;
own[node].dist[src]=16;
own[node].next[src]=0;
}
}
//Set infinity from dest to the source
//or if the next hop to reach it is the source
for(int node=0; node<num_nodes; node++ ){
if (node==dest) continue;
if (own[dest].next[node] == src +1 || node == src){
own[dest].dist[node]=16;
own[dest].next[node]=0;
own[node].dist[dest]=16;
own[node].next[dest]=0;
}
}
start_threads(1);
}
void revalidate_router(){
static int invalid = 0;
static int revalid = 0;
printf("\n The current invalidated routers are : ");
for( int i = 0; i< MAX; i++)
if (router_valid[i] == 0){
invalid ++;
printf(" %d , \n", i+1 );
}
if (!invalid){
pretty_error(" No current invalid routers exist in the network\n");
return;
}
else {
printf("\n Enter the router to be revalidated: "); scanf("%d",&revalid);
revalid -= 1;
if (router_valid[revalid] != 0 || revalid > num_nodes){
pretty_error(" Enter an invalidated router only\n");
return;
}
else {
router_valid[revalid] = 1;
start_threads(1);
}
}
}
void invalidate_router(){
//Fail all links of a given router, if exists;
printf("\nEnter the faulty router: \t");
scanf("%d",&s);
s -= 1;
if(s > num_nodes){
pretty_error(" Router doesn't exist\n");
return;
}
if (router_valid[s] != 1){
pretty_error(" Router already deactivated\n");
return;
}
//Fail all connected links from the current router
//Send a fail prune packet to all of them
router_valid [s] = 0;
start_threads(1);
}
void add_link(){
//Adds a new link in the network
int src,dest;
printf("Enter source: \t"); scanf("%d",&src);
printf("Enter destination: "); scanf("%d",&dest);
src-=1;
dest-=1;
if (src >= num_nodes){
pretty_error(" Source router doesn't exist\n");
return;
}
if (dest >= num_nodes){
pretty_error(" Destination router doesn't exist\n");
return;
}
own[src].dist[dest]=1;
own[src].next[dest]=0;
own[dest].dist[src]=1;
own[dest].next[src]=0;
start_threads(1);
}
void user_input(){
//User thread that always runs and presents the menu
int choice = 1;
while (choice){
printf("\n*****MENU*******\n\n");
printf("1: Add a router: \n\n");
printf("2: Fail a link:\n\n");
printf("3: Add a link:\n\n");
printf("4: Invalidate a router\n\n");
printf("5: Revalidate a router\n\n");
printf("0: Exit simulation: \t");
scanf("%d",&choice);
printf("\n\n\n");
switch (choice){
case 1: add_router(); break;
case 2: fail_link(0,0,1); break;
case 3: add_link(); break;
case 4: invalidate_router(); break;
case 5: revalidate_router(); break;
case 0: break;
}
}
}
int main(){
/*
1)Make each node get their table from a file
2)Interact with other nodes's tables using threads
3)Thread arguments contain their respective files
3)Update your own table
*/
own = (router*)calloc(MAX,sizeof(router));
for (int i = 0; i < num_nodes; i++){
own[i].dest = (int*)malloc(sizeof(int)*MAX);
own[i].dist = (int*)malloc(sizeof(int)*MAX);
own[i].next = (int*)malloc(sizeof(int)*MAX);
}
//Initialise routing validity
for (int i = 0; i < MAX; i++)
router_valid[i] = 1;
store_initial_files_in_table();
start_threads(1);
user_input();
/*
Once the initial table has been setup, one thread will continuously
display the final table on the main terminal for each node.
The other thread , in a new terminal should wait for user input to:
a) Add a new node
b) invalidate a node
c) Fail a link
d) Restore a link
For, each successful operation,
*/
return 0;
}