blob: 47384d86e4e54e2eda6477f244877a5b6dc36ded [file] [log] [blame]
akmhoque29c1db52012-09-07 14:47:43 -05001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <assert.h>
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11#include <sys/types.h>
12
13
14#include <ccn/ccn.h>
15#include <ccn/uri.h>
16#include <ccn/keystore.h>
17#include <ccn/signing.h>
18#include <ccn/schedule.h>
19#include <ccn/hashtb.h>
20
21
22#include "nlsr.h"
23#include "nlsr_route.h"
24#include "nlsr_lsdb.h"
akmhoque3560cb62012-09-09 10:52:30 -050025#include "nlsr_npt.h"
akmhoque29c1db52012-09-07 14:47:43 -050026
27int
28route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
29{
30 printf("route_calculate called\n");
31
32 if( ! nlsr->is_build_adj_lsa_sheduled )
33 {
34 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050035 print_routing_table();
36 print_npt();
37
akmhoque29c1db52012-09-07 14:47:43 -050038 struct hashtb_param param_me = {0};
39 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
40 make_map();
41 assign_mapping_number();
42 print_map();
43
akmhoque3560cb62012-09-09 10:52:30 -050044
45 do_old_routing_table_updates();
46
47
akmhoque29c1db52012-09-07 14:47:43 -050048 int i;
49 int **adj_matrix;
50 int map_element=hashtb_n(nlsr->map);
51 adj_matrix=malloc(map_element * sizeof(int *));
52 for(i = 0; i < map_element; i++)
53 {
54 adj_matrix[i] = malloc(map_element * sizeof(int));
55 }
56 make_adj_matrix(adj_matrix,map_element);
57 print_adj_matrix(adj_matrix,map_element);
58
59 long int source=get_mapping_no(nlsr->router_name);
60 long int *parent=(long int *)malloc(map_element * sizeof(long int));
61
62 calculate_path(adj_matrix,parent, map_element, source);
63
64 print_all_path_from_source(parent,source);
65
akmhoque3560cb62012-09-09 10:52:30 -050066
akmhoque29c1db52012-09-07 14:47:43 -050067
68 for(i = 0; i < map_element; i++)
69 {
70 free(adj_matrix[i]);
71 }
72 free(parent);
73 free(adj_matrix);
74 hashtb_destroy(&nlsr->map);
75
76 }
77 nlsr->is_route_calculation_scheduled=0;
78
79 return 0;
80}
81
82void
83calculate_path(int **adj_matrix, long int *parent, long int V, long int S)
84{
85 int i;
86 long int v,u;
87 long int *dist=(long int *)malloc(V * sizeof(long int));
88 long int *Q=(long int *)malloc(V * sizeof(long int));
89 long int head=0;
90 /* Initial the Parent */
91 for (i = 0 ; i < V; i++)
92 {
93 parent[i]=EMPTY_PARENT;
94 dist[i]=INF_DISTANCE;
95 Q[i]=i;
96 }
97
98 dist[S]=0;
99 sort_queue_by_distance(Q,dist,head,V);
100
101 while (head < V )
102 {
103 u=Q[head];
104 if(dist[u] == INF_DISTANCE)
105 {
106 break;
107 }
108
109 for(v=0 ; v <V; v++)
110 {
111 if( adj_matrix[u][v] > 0 ) //
112 {
113 if ( is_not_explored(Q,v,head+1,V) )
114 {
115
116 if( dist[u] + adj_matrix[u][v] < dist[v])
117 {
118 dist[v]=dist[u] + adj_matrix[u][v] ;
119 parent[v]=u;
120 }
121
122 }
123
124 }
125
126 }
127
128 head++;
129 sort_queue_by_distance(Q,dist,head,V);
130 }
131
132 free(Q);
133 free(dist);
134}
135
136void
137print_all_path_from_source(long int *parent,long int source)
138{
139 int i, map_element;
140 struct map_entry *me;
141
142 struct hashtb_enumerator ee;
143 struct hashtb_enumerator *e = &ee;
144
145 hashtb_start(nlsr->map, e);
146 map_element=hashtb_n(nlsr->map);
147
148 for(i=0;i<map_element;i++)
149 {
150 me=e->data;
151 if(me->mapping != source)
152 {
153 print_path(parent,(long int)me->mapping);
154 printf("\n");
155 }
156 hashtb_next(e);
157 }
158
159 hashtb_end(e);
160
161}
162
163void
164print_path(long int *parent, long int dest)
165{
166 if (parent[dest] != EMPTY_PARENT )
167 print_path(parent,parent[dest]);
168
169 printf(" %ld",dest);
170}
171
172int
173is_not_explored(long int *Q, long int u,long int start, long int element)
174{
175 int ret=0;
176 long int i;
177 for(i=start; i< element; i++)
178 {
179 if ( Q[i] == u )
180 {
181 ret=1;
182 break;
183 }
184 }
185 return ret;
186}
187
188void
189sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
190{
191 long int i,j;
192 long int temp_u;
193
194 for ( i=start ; i < element ; i ++)
195 {
196 for( j=i+1; j<element; j ++)
197 {
198 if (dist[Q[j]] < dist[Q[i]])
199 {
200 temp_u=Q[j];
201 Q[j]=Q[i];
202 Q[i]=temp_u;
203 }
204 }
205 }
206
207}
208
209void
210print_map(void)
211{
212 int i, map_element;
213 struct map_entry *me;
214
215 struct hashtb_enumerator ee;
216 struct hashtb_enumerator *e = &ee;
217
218 hashtb_start(nlsr->map, e);
219 map_element=hashtb_n(nlsr->map);
220
221 for(i=0;i<map_element;i++)
222 {
223 me=e->data;
224 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
225 hashtb_next(e);
226 }
227
228 hashtb_end(e);
229}
230
231
232void
233assign_mapping_number(void)
234{
235 int i, map_element;
236 struct map_entry *me;
237
238 struct hashtb_enumerator ee;
239 struct hashtb_enumerator *e = &ee;
240
241 hashtb_start(nlsr->map, e);
242 map_element=hashtb_n(nlsr->map);
243
244 for(i=0;i<map_element;i++)
245 {
246 me=e->data;
247 me->mapping=i;
248 hashtb_next(e);
249 }
250
251 hashtb_end(e);
252}
253
254void
255make_map(void)
256{
257
258
259 int i, adj_lsdb_element;
260 struct alsa *adj_lsa;
261
262 struct hashtb_enumerator ee;
263 struct hashtb_enumerator *e = &ee;
264
265 hashtb_start(nlsr->lsdb->adj_lsdb, e);
266 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
267
268 for(i=0;i<adj_lsdb_element;i++)
269 {
270 adj_lsa=e->data;
271 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
272 hashtb_next(e);
273 }
274
275 hashtb_end(e);
276
277}
278
279void
280add_map_entry(char *router)
281{
282
283 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
284
285 struct hashtb_enumerator ee;
286 struct hashtb_enumerator *e = &ee;
287 int res;
288
289 hashtb_start(nlsr->map, e);
290 res = hashtb_seek(e, router, strlen(router), 0);
291
292 if(res == HT_NEW_ENTRY)
293 {
294 me=e->data;
295 me->router=(char *)malloc(strlen(router)+1);
296 memset(me->router,0,strlen(router)+1);
297 memcpy(me->router,router,strlen(router));
298 me->mapping=0;
299 }
300
301 hashtb_end(e);
302
303}
304
305
306void
307add_adj_data_to_map(char *orig_router, char *body, int no_link)
308{
309 add_map_entry(orig_router);
310
311 int i=0;
312 char *lsa_data=(char *)malloc(strlen(body)+1);
313 memset( lsa_data,0,strlen(body)+1);
314 memcpy(lsa_data,body,strlen(body)+1);
315 char *sep="|";
316 char *rem;
317 char *rtr_id;
318 char *length;
319 char *face;
320 char *metric;
321
322 if(no_link >0 )
323 {
324 rtr_id=strtok_r(lsa_data,sep,&rem);
325 length=strtok_r(NULL,sep,&rem);
326 face=strtok_r(NULL,sep,&rem);
327 metric=strtok_r(NULL,sep,&rem);
328
329 add_map_entry(rtr_id);
330
331 for(i=1;i<no_link;i++)
332 {
333 rtr_id=strtok_r(NULL,sep,&rem);
334 length=strtok_r(NULL,sep,&rem);
335 face=strtok_r(NULL,sep,&rem);
336 metric=strtok_r(NULL,sep,&rem);
337
338 add_map_entry(rtr_id);
339
340 }
341 }
342
343 free(lsa_data);
344}
345
346int
347get_mapping_no(char *router)
348{
349 struct map_entry *me;
350
351 struct hashtb_enumerator ee;
352 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500353 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500354 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500355
356 hashtb_start(nlsr->map, e);
357 res = hashtb_seek(e, router, strlen(router), 0);
358
359 if( res == HT_OLD_ENTRY )
360 {
361 me=e->data;
362 ret=me->mapping;
363 }
364 else if(res == HT_NEW_ENTRY)
365 {
366 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500367 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500368 }
369
370 hashtb_end(e);
371
372 return ret;
373
374}
375
376void
377assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
378{
379 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
380 int mapping_nbr_router;
381
382 int i;
383 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
384 memset( lsa_data,0,strlen(adj_lsa->body)+1);
385 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
386 char *sep="|";
387 char *rem;
388 char *rtr_id;
389 char *length;
390 char *face;
391 char *metric;
392
393 if(adj_lsa->no_link >0 )
394 {
395 rtr_id=strtok_r(lsa_data,sep,&rem);
396 length=strtok_r(NULL,sep,&rem);
397 face=strtok_r(NULL,sep,&rem);
398 metric=strtok_r(NULL,sep,&rem);
399
400 mapping_nbr_router=get_mapping_no(rtr_id);
401 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
402
403 for(i=1;i<adj_lsa->no_link;i++)
404 {
405 rtr_id=strtok_r(NULL,sep,&rem);
406 length=strtok_r(NULL,sep,&rem);
407 face=strtok_r(NULL,sep,&rem);
408 metric=strtok_r(NULL,sep,&rem);
409
410 mapping_nbr_router=get_mapping_no(rtr_id);
411 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
412
413 }
414 }
415
416 free(lsa_data);
417}
418
419void
420make_adj_matrix(int **adj_matrix,int map_element)
421{
422
423 init_adj_matrix(adj_matrix,map_element);
424
425 int i, adj_lsdb_element;
426 struct alsa *adj_lsa;
427
428 struct hashtb_enumerator ee;
429 struct hashtb_enumerator *e = &ee;
430
431 hashtb_start(nlsr->lsdb->adj_lsdb, e);
432 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
433
434 for(i=0;i<adj_lsdb_element;i++)
435 {
436 adj_lsa=e->data;
437 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
438 hashtb_next(e);
439 }
440
441 hashtb_end(e);
442
443}
444
445void
446init_adj_matrix(int **adj_matrix,int map_element)
447{
448 int i, j;
449 for(i=0;i<map_element;i++)
450 for(j=0;j<map_element;j++)
451 adj_matrix[i][j]=0;
452}
453
454void print_adj_matrix(int **adj_matrix, int map_element)
455{
456 int i, j;
457 for(i=0;i<map_element;i++)
458 {
459 for(j=0;j<map_element;j++)
460 printf("%d ",adj_matrix[i][j]);
461 printf("\n");
462 }
463}
464
akmhoque3560cb62012-09-09 10:52:30 -0500465int
466get_next_hop(char *dest_router)
467{
468 struct routing_table_entry *rte;
469
470 struct hashtb_enumerator ee;
471 struct hashtb_enumerator *e = &ee;
472 int res,ret;
473
474 hashtb_start(nlsr->routing_table, e);
475 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
476
477 if( res == HT_OLD_ENTRY )
478 {
479 rte=e->data;
480 ret=rte->next_hop_face;
481 }
482 else if(res == HT_NEW_ENTRY)
483 {
484 hashtb_delete(e);
485 ret=NO_NEXT_HOP;
486 }
487
488 hashtb_end(e);
489
490 return ret;
491}
492
493void
494add_next_hop_router(char *dest_router)
495{
496 if ( strcmp(dest_router,nlsr->router_name)== 0)
497 {
498 return ;
499 }
500
501 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
502
503 struct hashtb_enumerator ee;
504 struct hashtb_enumerator *e = &ee;
505 int res;
506
507 hashtb_start(nlsr->routing_table, e);
508 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
509
510 if( res == HT_NEW_ENTRY )
511 {
512 rte=e->data;
513 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
514 memset(rte->dest_router,0,strlen(dest_router)+1);
515 memcpy(rte->dest_router,dest_router,strlen(dest_router));
516 rte->next_hop_face=NO_NEXT_HOP;
517 }
518 hashtb_end(e);
519
520}
521
522void
523add_next_hop_from_lsa_adj_body(char *body, int no_link)
524{
525
526 int i=0;
527 char *lsa_data=(char *)malloc(strlen(body)+1);
528 memset( lsa_data,0,strlen(body)+1);
529 memcpy(lsa_data,body,strlen(body)+1);
530 char *sep="|";
531 char *rem;
532 char *rtr_id;
533 char *length;
534 char *face;
535 char *metric;
536
537 if(no_link >0 )
538 {
539 rtr_id=strtok_r(lsa_data,sep,&rem);
540 length=strtok_r(NULL,sep,&rem);
541 face=strtok_r(NULL,sep,&rem);
542 metric=strtok_r(NULL,sep,&rem);
543
akmhoque3560cb62012-09-09 10:52:30 -0500544
545 add_next_hop_router(rtr_id);
546
547 for(i=1;i<no_link;i++)
548 {
549 rtr_id=strtok_r(NULL,sep,&rem);
550 length=strtok_r(NULL,sep,&rem);
551 face=strtok_r(NULL,sep,&rem);
552 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500553
554 add_next_hop_router(rtr_id);
555
556 }
557 }
558
559 free(lsa_data);
560
561
562}
563
564void
565print_routing_table(void)
566{
567 printf("\n");
568 printf("print_routing_table called\n");
569 int i, rt_element;
570
571 struct routing_table_entry *rte;
572
573 struct hashtb_enumerator ee;
574 struct hashtb_enumerator *e = &ee;
575
576 hashtb_start(nlsr->routing_table, e);
577 rt_element=hashtb_n(nlsr->routing_table);
578
579 for(i=0;i<rt_element;i++)
580 {
581 printf("----------Routing Table Entry %d------------------\n",i+1);
582 rte=e->data;
583 printf(" Destination Router: %s \n",rte->dest_router);
584 rte->next_hop_face == NO_NEXT_HOP ? printf(" Next Hop Face: NO_NEXT_HOP \n") : printf(" Next Hop Face: %d \n", rte->next_hop_face);
585 hashtb_next(e);
586 }
587
588 hashtb_end(e);
589
590 printf("\n");
591}
592
593void
594do_old_routing_table_updates()
595{
akmhoque810a5b52012-09-09 16:53:14 -0500596 printf("do_old_routing_table_updates called\n");
597 int i, rt_element;
598 int mapping_no;
599
600 struct routing_table_entry *rte;
601
602 struct hashtb_enumerator ee;
603 struct hashtb_enumerator *e = &ee;
604
605 hashtb_start(nlsr->routing_table, e);
606 rt_element=hashtb_n(nlsr->routing_table);
607
608 for(i=0;i<rt_element;i++)
609 {
610 rte=e->data;
611 mapping_no=get_mapping_no(rte->dest_router);
612 if ( mapping_no == NO_MAPPING_NUM)
613 {
614 delete_orig_router_from_npt(rte->dest_router,rte->next_hop_face);
615 hashtb_delete(e);
616 }
617 hashtb_next(e);
618 }
619
620 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -0500621}
akmhoque29c1db52012-09-07 14:47:43 -0500622