blob: 60e9c76a5ea387cda74fc22a53cfd8dc41bc3726 [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"
akmhoquefbfd0982012-09-09 20:59:03 -050026#include "nlsr_adl.h"
akmhoque29c1db52012-09-07 14:47:43 -050027
28int
29route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
30{
31 printf("route_calculate called\n");
32
33 if( ! nlsr->is_build_adj_lsa_sheduled )
34 {
35 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050036 print_routing_table();
37 print_npt();
38
akmhoque29c1db52012-09-07 14:47:43 -050039 struct hashtb_param param_me = {0};
40 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoquefbfd0982012-09-09 20:59:03 -050041 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoque29c1db52012-09-07 14:47:43 -050042 make_map();
43 assign_mapping_number();
44 print_map();
akmhoquefbfd0982012-09-09 20:59:03 -050045 print_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -050046
akmhoque3560cb62012-09-09 10:52:30 -050047 do_old_routing_table_updates();
48
49
akmhoque29c1db52012-09-07 14:47:43 -050050 int i;
51 int **adj_matrix;
52 int map_element=hashtb_n(nlsr->map);
53 adj_matrix=malloc(map_element * sizeof(int *));
54 for(i = 0; i < map_element; i++)
55 {
56 adj_matrix[i] = malloc(map_element * sizeof(int));
57 }
58 make_adj_matrix(adj_matrix,map_element);
59 print_adj_matrix(adj_matrix,map_element);
60
61 long int source=get_mapping_no(nlsr->router_name);
62 long int *parent=(long int *)malloc(map_element * sizeof(long int));
63
64 calculate_path(adj_matrix,parent, map_element, source);
65
66 print_all_path_from_source(parent,source);
67
akmhoquefbfd0982012-09-09 20:59:03 -050068 print_all_next_hop(parent,source);
69
70 update_routing_table_with_new_route(parent,source);
71
72
akmhoque29c1db52012-09-07 14:47:43 -050073 for(i = 0; i < map_element; i++)
74 {
75 free(adj_matrix[i]);
76 }
77 free(parent);
78 free(adj_matrix);
79 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -050080 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -050081
82 }
83 nlsr->is_route_calculation_scheduled=0;
84
85 return 0;
86}
87
88void
89calculate_path(int **adj_matrix, long int *parent, long int V, long int S)
90{
91 int i;
92 long int v,u;
93 long int *dist=(long int *)malloc(V * sizeof(long int));
94 long int *Q=(long int *)malloc(V * sizeof(long int));
95 long int head=0;
96 /* Initial the Parent */
97 for (i = 0 ; i < V; i++)
98 {
99 parent[i]=EMPTY_PARENT;
100 dist[i]=INF_DISTANCE;
101 Q[i]=i;
102 }
103
104 dist[S]=0;
105 sort_queue_by_distance(Q,dist,head,V);
106
107 while (head < V )
108 {
109 u=Q[head];
110 if(dist[u] == INF_DISTANCE)
111 {
112 break;
113 }
114
115 for(v=0 ; v <V; v++)
116 {
117 if( adj_matrix[u][v] > 0 ) //
118 {
119 if ( is_not_explored(Q,v,head+1,V) )
120 {
121
122 if( dist[u] + adj_matrix[u][v] < dist[v])
123 {
124 dist[v]=dist[u] + adj_matrix[u][v] ;
125 parent[v]=u;
126 }
127
128 }
129
130 }
131
132 }
133
134 head++;
135 sort_queue_by_distance(Q,dist,head,V);
136 }
137
138 free(Q);
139 free(dist);
140}
141
142void
143print_all_path_from_source(long int *parent,long int source)
144{
145 int i, map_element;
146 struct map_entry *me;
147
148 struct hashtb_enumerator ee;
149 struct hashtb_enumerator *e = &ee;
150
151 hashtb_start(nlsr->map, e);
152 map_element=hashtb_n(nlsr->map);
153
154 for(i=0;i<map_element;i++)
155 {
156 me=e->data;
157 if(me->mapping != source)
158 {
159 print_path(parent,(long int)me->mapping);
160 printf("\n");
161 }
162 hashtb_next(e);
163 }
164
165 hashtb_end(e);
166
167}
168
akmhoquefbfd0982012-09-09 20:59:03 -0500169void
170print_all_next_hop(long int *parent,long int source)
171{
172 int i, map_element;
173 struct map_entry *me;
174
175 struct hashtb_enumerator ee;
176 struct hashtb_enumerator *e = &ee;
177
178 hashtb_start(nlsr->map, e);
179 map_element=hashtb_n(nlsr->map);
180
181 for(i=0;i<map_element;i++)
182 {
183 me=e->data;
184 if(me->mapping != source)
185 {
186
187 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
188 }
189 hashtb_next(e);
190 }
191
192 hashtb_end(e);
193
194}
195
196long int
197get_next_hop_from_calculation(long int *parent, long int dest,long int source)
198{
199 long int next_hop;
200 while ( parent[dest] != EMPTY_PARENT )
201 {
202 next_hop=dest;
203 dest=parent[dest];
204
205 }
206
207 if ( dest != source )
208 {
209 next_hop=NO_NEXT_HOP;
210 }
211
212 return next_hop;
213
214}
215
akmhoque29c1db52012-09-07 14:47:43 -0500216void
217print_path(long int *parent, long int dest)
218{
219 if (parent[dest] != EMPTY_PARENT )
220 print_path(parent,parent[dest]);
221
222 printf(" %ld",dest);
223}
224
225int
226is_not_explored(long int *Q, long int u,long int start, long int element)
227{
228 int ret=0;
229 long int i;
230 for(i=start; i< element; i++)
231 {
232 if ( Q[i] == u )
233 {
234 ret=1;
235 break;
236 }
237 }
238 return ret;
239}
240
241void
242sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
243{
244 long int i,j;
245 long int temp_u;
246
247 for ( i=start ; i < element ; i ++)
248 {
249 for( j=i+1; j<element; j ++)
250 {
251 if (dist[Q[j]] < dist[Q[i]])
252 {
253 temp_u=Q[j];
254 Q[j]=Q[i];
255 Q[i]=temp_u;
256 }
257 }
258 }
259
260}
261
262void
263print_map(void)
264{
265 int i, map_element;
266 struct map_entry *me;
267
268 struct hashtb_enumerator ee;
269 struct hashtb_enumerator *e = &ee;
270
271 hashtb_start(nlsr->map, e);
272 map_element=hashtb_n(nlsr->map);
273
274 for(i=0;i<map_element;i++)
275 {
276 me=e->data;
277 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
278 hashtb_next(e);
279 }
280
281 hashtb_end(e);
282}
283
284
285void
286assign_mapping_number(void)
287{
288 int i, map_element;
289 struct map_entry *me;
290
291 struct hashtb_enumerator ee;
292 struct hashtb_enumerator *e = &ee;
293
294 hashtb_start(nlsr->map, e);
295 map_element=hashtb_n(nlsr->map);
296
297 for(i=0;i<map_element;i++)
298 {
299 me=e->data;
300 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500301 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500302 hashtb_next(e);
303 }
304
305 hashtb_end(e);
306}
307
308void
309make_map(void)
310{
311
312
313 int i, adj_lsdb_element;
314 struct alsa *adj_lsa;
315
316 struct hashtb_enumerator ee;
317 struct hashtb_enumerator *e = &ee;
318
319 hashtb_start(nlsr->lsdb->adj_lsdb, e);
320 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
321
322 for(i=0;i<adj_lsdb_element;i++)
323 {
324 adj_lsa=e->data;
325 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
326 hashtb_next(e);
327 }
328
329 hashtb_end(e);
330
331}
332
333void
334add_map_entry(char *router)
335{
336
337 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
338
339 struct hashtb_enumerator ee;
340 struct hashtb_enumerator *e = &ee;
341 int res;
342
343 hashtb_start(nlsr->map, e);
344 res = hashtb_seek(e, router, strlen(router), 0);
345
346 if(res == HT_NEW_ENTRY)
347 {
348 me=e->data;
349 me->router=(char *)malloc(strlen(router)+1);
350 memset(me->router,0,strlen(router)+1);
351 memcpy(me->router,router,strlen(router));
352 me->mapping=0;
353 }
354
355 hashtb_end(e);
356
357}
358
359
360void
361add_adj_data_to_map(char *orig_router, char *body, int no_link)
362{
363 add_map_entry(orig_router);
364
365 int i=0;
366 char *lsa_data=(char *)malloc(strlen(body)+1);
367 memset( lsa_data,0,strlen(body)+1);
368 memcpy(lsa_data,body,strlen(body)+1);
369 char *sep="|";
370 char *rem;
371 char *rtr_id;
372 char *length;
373 char *face;
374 char *metric;
375
376 if(no_link >0 )
377 {
378 rtr_id=strtok_r(lsa_data,sep,&rem);
379 length=strtok_r(NULL,sep,&rem);
380 face=strtok_r(NULL,sep,&rem);
381 metric=strtok_r(NULL,sep,&rem);
382
383 add_map_entry(rtr_id);
384
385 for(i=1;i<no_link;i++)
386 {
387 rtr_id=strtok_r(NULL,sep,&rem);
388 length=strtok_r(NULL,sep,&rem);
389 face=strtok_r(NULL,sep,&rem);
390 metric=strtok_r(NULL,sep,&rem);
391
392 add_map_entry(rtr_id);
393
394 }
395 }
396
397 free(lsa_data);
398}
399
400int
401get_mapping_no(char *router)
402{
403 struct map_entry *me;
404
405 struct hashtb_enumerator ee;
406 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500407 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500408 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500409
410 hashtb_start(nlsr->map, e);
411 res = hashtb_seek(e, router, strlen(router), 0);
412
413 if( res == HT_OLD_ENTRY )
414 {
415 me=e->data;
416 ret=me->mapping;
417 }
418 else if(res == HT_NEW_ENTRY)
419 {
420 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500421 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500422 }
423
424 hashtb_end(e);
425
426 return ret;
427
428}
429
akmhoquefbfd0982012-09-09 20:59:03 -0500430
431void
432add_rev_map_entry(long int mapping_number, char *router)
433{
434
435 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
436
437 struct hashtb_enumerator ee;
438 struct hashtb_enumerator *e = &ee;
439 int res;
440
441 hashtb_start(nlsr->rev_map, e);
442 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
443
444 if(res == HT_NEW_ENTRY)
445 {
446 me=e->data;
447 me->router=(char *)malloc(strlen(router)+1);
448 memset(me->router,0,strlen(router)+1);
449 memcpy(me->router,router,strlen(router));
450 me->mapping=mapping_number;
451 }
452
453 hashtb_end(e);
454
455}
456
457
458
459char *
460get_router_from_rev_map(long int mapping_number)
461{
462
463 struct map_entry *me;
464
465 struct hashtb_enumerator ee;
466 struct hashtb_enumerator *e = &ee;
467 int res;
468
469 hashtb_start(nlsr->rev_map, e);
470 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
471
472 if(res == HT_OLD_ENTRY)
473 {
474 me=e->data;
475 hashtb_end(e);
476 return me->router;
477 }
478 else if(res == HT_NEW_ENTRY)
479 {
480 hashtb_delete(e);
481 hashtb_end(e);
482 }
483
484 return NULL;
485}
486
487void
488print_rev_map(void)
489{
490 int i, map_element;
491 struct map_entry *me;
492
493 struct hashtb_enumerator ee;
494 struct hashtb_enumerator *e = &ee;
495
496 hashtb_start(nlsr->map, e);
497 map_element=hashtb_n(nlsr->map);
498
499 for(i=0;i<map_element;i++)
500 {
501 me=e->data;
502 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
503 hashtb_next(e);
504 }
505
506 hashtb_end(e);
507}
508
509
akmhoque29c1db52012-09-07 14:47:43 -0500510void
511assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
512{
513 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
514 int mapping_nbr_router;
515
516 int i;
517 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
518 memset( lsa_data,0,strlen(adj_lsa->body)+1);
519 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
520 char *sep="|";
521 char *rem;
522 char *rtr_id;
523 char *length;
524 char *face;
525 char *metric;
526
527 if(adj_lsa->no_link >0 )
528 {
529 rtr_id=strtok_r(lsa_data,sep,&rem);
530 length=strtok_r(NULL,sep,&rem);
531 face=strtok_r(NULL,sep,&rem);
532 metric=strtok_r(NULL,sep,&rem);
533
534 mapping_nbr_router=get_mapping_no(rtr_id);
535 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
536
537 for(i=1;i<adj_lsa->no_link;i++)
538 {
539 rtr_id=strtok_r(NULL,sep,&rem);
540 length=strtok_r(NULL,sep,&rem);
541 face=strtok_r(NULL,sep,&rem);
542 metric=strtok_r(NULL,sep,&rem);
543
544 mapping_nbr_router=get_mapping_no(rtr_id);
545 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
546
547 }
548 }
549
550 free(lsa_data);
551}
552
553void
554make_adj_matrix(int **adj_matrix,int map_element)
555{
556
557 init_adj_matrix(adj_matrix,map_element);
558
559 int i, adj_lsdb_element;
560 struct alsa *adj_lsa;
561
562 struct hashtb_enumerator ee;
563 struct hashtb_enumerator *e = &ee;
564
565 hashtb_start(nlsr->lsdb->adj_lsdb, e);
566 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
567
568 for(i=0;i<adj_lsdb_element;i++)
569 {
570 adj_lsa=e->data;
571 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
572 hashtb_next(e);
573 }
574
575 hashtb_end(e);
576
577}
578
579void
580init_adj_matrix(int **adj_matrix,int map_element)
581{
582 int i, j;
583 for(i=0;i<map_element;i++)
584 for(j=0;j<map_element;j++)
585 adj_matrix[i][j]=0;
586}
587
588void print_adj_matrix(int **adj_matrix, int map_element)
589{
590 int i, j;
591 for(i=0;i<map_element;i++)
592 {
593 for(j=0;j<map_element;j++)
594 printf("%d ",adj_matrix[i][j]);
595 printf("\n");
596 }
597}
598
akmhoque3560cb62012-09-09 10:52:30 -0500599int
600get_next_hop(char *dest_router)
601{
602 struct routing_table_entry *rte;
603
604 struct hashtb_enumerator ee;
605 struct hashtb_enumerator *e = &ee;
606 int res,ret;
607
608 hashtb_start(nlsr->routing_table, e);
609 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
610
611 if( res == HT_OLD_ENTRY )
612 {
613 rte=e->data;
614 ret=rte->next_hop_face;
615 }
616 else if(res == HT_NEW_ENTRY)
617 {
618 hashtb_delete(e);
619 ret=NO_NEXT_HOP;
620 }
621
622 hashtb_end(e);
623
624 return ret;
625}
626
627void
628add_next_hop_router(char *dest_router)
629{
630 if ( strcmp(dest_router,nlsr->router_name)== 0)
631 {
632 return ;
633 }
634
635 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
636
637 struct hashtb_enumerator ee;
638 struct hashtb_enumerator *e = &ee;
639 int res;
640
641 hashtb_start(nlsr->routing_table, e);
642 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
643
644 if( res == HT_NEW_ENTRY )
645 {
646 rte=e->data;
647 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
648 memset(rte->dest_router,0,strlen(dest_router)+1);
649 memcpy(rte->dest_router,dest_router,strlen(dest_router));
650 rte->next_hop_face=NO_NEXT_HOP;
651 }
652 hashtb_end(e);
653
654}
655
656void
657add_next_hop_from_lsa_adj_body(char *body, int no_link)
658{
659
660 int i=0;
661 char *lsa_data=(char *)malloc(strlen(body)+1);
662 memset( lsa_data,0,strlen(body)+1);
663 memcpy(lsa_data,body,strlen(body)+1);
664 char *sep="|";
665 char *rem;
666 char *rtr_id;
667 char *length;
668 char *face;
669 char *metric;
670
671 if(no_link >0 )
672 {
673 rtr_id=strtok_r(lsa_data,sep,&rem);
674 length=strtok_r(NULL,sep,&rem);
675 face=strtok_r(NULL,sep,&rem);
676 metric=strtok_r(NULL,sep,&rem);
677
akmhoque3560cb62012-09-09 10:52:30 -0500678
679 add_next_hop_router(rtr_id);
680
681 for(i=1;i<no_link;i++)
682 {
683 rtr_id=strtok_r(NULL,sep,&rem);
684 length=strtok_r(NULL,sep,&rem);
685 face=strtok_r(NULL,sep,&rem);
686 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500687
688 add_next_hop_router(rtr_id);
689
690 }
691 }
692
693 free(lsa_data);
694
695
696}
697
698void
699print_routing_table(void)
700{
701 printf("\n");
702 printf("print_routing_table called\n");
703 int i, rt_element;
704
705 struct routing_table_entry *rte;
706
707 struct hashtb_enumerator ee;
708 struct hashtb_enumerator *e = &ee;
709
710 hashtb_start(nlsr->routing_table, e);
711 rt_element=hashtb_n(nlsr->routing_table);
712
713 for(i=0;i<rt_element;i++)
714 {
715 printf("----------Routing Table Entry %d------------------\n",i+1);
716 rte=e->data;
717 printf(" Destination Router: %s \n",rte->dest_router);
718 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);
719 hashtb_next(e);
720 }
721
722 hashtb_end(e);
723
724 printf("\n");
725}
726
727void
728do_old_routing_table_updates()
729{
akmhoque810a5b52012-09-09 16:53:14 -0500730 printf("do_old_routing_table_updates called\n");
731 int i, rt_element;
732 int mapping_no;
733
734 struct routing_table_entry *rte;
735
736 struct hashtb_enumerator ee;
737 struct hashtb_enumerator *e = &ee;
738
739 hashtb_start(nlsr->routing_table, e);
740 rt_element=hashtb_n(nlsr->routing_table);
741
742 for(i=0;i<rt_element;i++)
743 {
744 rte=e->data;
745 mapping_no=get_mapping_no(rte->dest_router);
746 if ( mapping_no == NO_MAPPING_NUM)
747 {
748 delete_orig_router_from_npt(rte->dest_router,rte->next_hop_face);
749 hashtb_delete(e);
750 }
751 hashtb_next(e);
752 }
753
754 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -0500755}
akmhoque29c1db52012-09-07 14:47:43 -0500756
akmhoquefbfd0982012-09-09 20:59:03 -0500757void
758update_routing_table_with_new_route(long int *parent, long int source)
759{
760 printf("update_routing_table_with_new_route called\n");
761 int i, map_element;
762 struct map_entry *me;
763
764 struct hashtb_enumerator ee;
765 struct hashtb_enumerator *e = &ee;
766
767 hashtb_start(nlsr->rev_map, e);
768 map_element=hashtb_n(nlsr->rev_map);
769
770 for(i=0;i<map_element;i++)
771 {
772 me=e->data;
773 if(me->mapping != source)
774 {
775
776 char *orig_router=get_router_from_rev_map(me->mapping);
777 if (orig_router != NULL )
778 {
779 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
780 //printf(" Next hop router Num: %d ",next_hop_router_num);
781 if ( next_hop_router_num == NO_NEXT_HOP )
782 {
783 update_npt_with_new_route(orig_router,NO_FACE);
784 printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
785 }
786 else
787 {
788 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
789 //printf("Next hop router name: %s \n",next_hop_router);
790 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
791 update_npt_with_new_route(orig_router,next_hop_face);
792
793 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
794
795 }
796 }
797 }
798 hashtb_next(e);
799 }
800
801 hashtb_end(e);
802}
803