blob: 36a995761c99c62be7b524d28b741e672398671d [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#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"
25#include "nlsr_npt.h"
26#include "nlsr_adl.h"
27#include "nlsr_fib.h"
28#include "utility.h"
29
30int
31route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
32{
33
34 if(flags == CCN_SCHEDULE_CANCEL)
35 {
36 return -1;
37 }
38
39 nlsr_lock();
40
41 if ( nlsr->debugging )
42 printf("route_calculate called\n");
43 if ( nlsr->detailed_logging )
44 writeLogg(__FILE__,__FUNCTION__,__LINE__,"route_calculate called\n");
45
46 if( ! nlsr->is_build_adj_lsa_sheduled )
47 {
48 /* Calculate Route here */
49 print_routing_table();
50 print_npt();
51
52 struct hashtb_param param_me = {0};
53 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
54 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
55 make_map();
56 assign_mapping_number();
57 print_map();
58 print_rev_map();
59
60 do_old_routing_table_updates();
61 clear_old_routing_table();
62 print_routing_table();
63 print_npt();
64
65 int i;
66 int **adj_matrix;
67 int map_element=hashtb_n(nlsr->map);
68 adj_matrix=malloc(map_element * sizeof(int *));
69 for(i = 0; i < map_element; i++)
70 {
71 adj_matrix[i] = malloc(map_element * sizeof(int));
72 }
73 make_adj_matrix(adj_matrix,map_element);
74 if ( nlsr->debugging )
75 print_adj_matrix(adj_matrix,map_element);
76
77 long int source=get_mapping_no(nlsr->router_name);
78 long int *parent=(long int *)malloc(map_element * sizeof(long int));
79 long int *dist=(long int *)malloc(map_element * sizeof(long int));
80
81 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
82
akmhoqueeda9c362013-01-23 08:54:29 -060083 if ( (num_link == 0) || (nlsr->multi_path_face_num == 1 ) )
akmhoque8fdd6412012-12-04 15:05:33 -060084 {
85 calculate_path(adj_matrix,parent,dist, map_element, source);
86 print_all_path_from_source(parent,source);
87 print_all_next_hop(parent,source);
88 update_routing_table_with_new_route(parent, dist,source);
akmhoque613446f2013-01-23 10:40:12 -060089 //print_routing_table();
akmhoque8fdd6412012-12-04 15:05:33 -060090 }
91 else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
92 {
93 long int *links=(long int *)malloc(num_link*sizeof(long int));
94 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
95 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
96 for ( i=0 ; i < num_link; i++)
97 {
98 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
99 calculate_path(adj_matrix,parent,dist, map_element, source);
100 print_all_path_from_source(parent,source);
101 print_all_next_hop(parent,source);
102 update_routing_table_with_new_route(parent, dist,source);
akmhoque613446f2013-01-23 10:40:12 -0600103 //print_routing_table();
akmhoque8fdd6412012-12-04 15:05:33 -0600104 }
105
106 free(links);
107 free(link_costs);
108 }
109
akmhoque8fdd6412012-12-04 15:05:33 -0600110 print_routing_table();
111 print_npt();
112
akmhoque580ccf32013-01-23 09:12:33 -0600113 update_npt_with_new_route();
114
115 print_routing_table();
116 print_npt();
akmhoqueeda9c362013-01-23 08:54:29 -0600117
118
akmhoque8fdd6412012-12-04 15:05:33 -0600119 for(i = 0; i < map_element; i++)
120 {
121 free(adj_matrix[i]);
122 }
123 free(parent);
124 free(dist);
125 free(adj_matrix);
126 hashtb_destroy(&nlsr->map);
127 hashtb_destroy(&nlsr->rev_map);
128
129 }
130 nlsr->is_route_calculation_scheduled=0;
131
132 nlsr_unlock();
133
134 return 0;
135}
136
137void
138calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
139{
140 int i;
141 long int v,u;
142 //long int *dist=(long int *)malloc(V * sizeof(long int));
143 long int *Q=(long int *)malloc(V * sizeof(long int));
144 long int head=0;
145 /* Initial the Parent */
146 for (i = 0 ; i < V; i++)
147 {
148 parent[i]=EMPTY_PARENT;
149 dist[i]=INF_DISTANCE;
150 Q[i]=i;
151 }
152
153 if ( S != NO_MAPPING_NUM )
154 {
155 dist[S]=0;
156 sort_queue_by_distance(Q,dist,head,V);
157
158 while (head < V )
159 {
160 u=Q[head];
161 if(dist[u] == INF_DISTANCE)
162 {
163 break;
164 }
165
166 for(v=0 ; v <V; v++)
167 {
168 if( adj_matrix[u][v] > 0 ) //
169 {
170 if ( is_not_explored(Q,v,head+1,V) )
171 {
172
173 if( dist[u] + adj_matrix[u][v] < dist[v])
174 {
175 dist[v]=dist[u] + adj_matrix[u][v] ;
176 parent[v]=u;
177 }
178
179 }
180
181 }
182
183 }
184
185 head++;
186 sort_queue_by_distance(Q,dist,head,V);
187 }
188 }
akmhoque596f7082013-02-04 13:34:13 -0600189 free(Q);
akmhoque8fdd6412012-12-04 15:05:33 -0600190}
191
192void
193print_all_path_from_source(long int *parent,long int source)
194{
195 int i, map_element;
196 struct map_entry *me;
197
198 struct hashtb_enumerator ee;
199 struct hashtb_enumerator *e = &ee;
200
201 hashtb_start(nlsr->map, e);
202 map_element=hashtb_n(nlsr->map);
203
204 if ( source != NO_MAPPING_NUM)
205 {
206 for(i=0;i<map_element;i++)
207 {
208 me=e->data;
209 if(me->mapping != source)
210 {
211
212 if ( nlsr->debugging )
213 {
214 print_path(parent,(long int)me->mapping);
215 printf("\n");
216 }
217 if ( nlsr->detailed_logging )
218 {
219 print_path(parent,(long int)me->mapping);
220 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
221 }
222
223 }
224 hashtb_next(e);
225 }
226 }
227 hashtb_end(e);
228
229}
230
231void
232print_all_next_hop(long int *parent,long int source)
233{
234 int i, map_element;
235 struct map_entry *me;
236
237 struct hashtb_enumerator ee;
238 struct hashtb_enumerator *e = &ee;
239
240 hashtb_start(nlsr->map, e);
241 map_element=hashtb_n(nlsr->map);
242
243 for(i=0;i<map_element;i++)
244 {
245 me=e->data;
246 if(me->mapping != source)
247 {
248 if ( nlsr->debugging )
249 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
250 if ( nlsr->detailed_logging )
251 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
252 }
253 hashtb_next(e);
254 }
255
256 hashtb_end(e);
257
258}
259
260long int
261get_next_hop_from_calculation(long int *parent, long int dest,long int source)
262{
263 long int next_hop;
264 while ( parent[dest] != EMPTY_PARENT )
265 {
266 next_hop=dest;
267 dest=parent[dest];
268
269 }
270
271 if ( dest != source )
272 {
273 next_hop=NO_NEXT_HOP;
274 }
275
276 return next_hop;
277
278}
279
280void
281print_path(long int *parent, long int dest)
282{
283 if (parent[dest] != EMPTY_PARENT )
284 print_path(parent,parent[dest]);
a64adam04be6482013-01-17 11:29:32 -0600285
286 if ( nlsr->debugging )
287 printf(" %ld",dest);
akmhoque8fdd6412012-12-04 15:05:33 -0600288}
289
290int
291is_not_explored(long int *Q, long int u,long int start, long int element)
292{
293 int ret=0;
294 long int i;
295 for(i=start; i< element; i++)
296 {
297 if ( Q[i] == u )
298 {
299 ret=1;
300 break;
301 }
302 }
303 return ret;
304}
305
306void
307sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
308{
309 long int i,j;
310 long int temp_u;
311
312 for ( i=start ; i < element ; i ++)
313 {
314 for( j=i+1; j<element; j ++)
315 {
316 if (dist[Q[j]] < dist[Q[i]])
317 {
318 temp_u=Q[j];
319 Q[j]=Q[i];
320 Q[i]=temp_u;
321 }
322 }
323 }
324
325}
326
327void
328print_map(void)
329{
330 int i, map_element;
331 struct map_entry *me;
332
333 struct hashtb_enumerator ee;
334 struct hashtb_enumerator *e = &ee;
335
336 hashtb_start(nlsr->map, e);
337 map_element=hashtb_n(nlsr->map);
338
339 for(i=0;i<map_element;i++)
340 {
341 me=e->data;
342 if ( nlsr->debugging )
343 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
344 if ( nlsr->detailed_logging )
345 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
346 hashtb_next(e);
347 }
348
349 hashtb_end(e);
350}
351
352
353void
354assign_mapping_number(void)
355{
356 int i, map_element;
357 struct map_entry *me;
358
359 struct hashtb_enumerator ee;
360 struct hashtb_enumerator *e = &ee;
361
362 hashtb_start(nlsr->map, e);
363 map_element=hashtb_n(nlsr->map);
364
365 for(i=0;i<map_element;i++)
366 {
367 me=e->data;
368 me->mapping=i;
369 add_rev_map_entry(i,me->router);
370 hashtb_next(e);
371 }
372
373 hashtb_end(e);
374}
375
376void
377make_map(void)
378{
379
380
381 int i, adj_lsdb_element;
382 struct alsa *adj_lsa;
383
384 struct hashtb_enumerator ee;
385 struct hashtb_enumerator *e = &ee;
386
387 hashtb_start(nlsr->lsdb->adj_lsdb, e);
388 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
389
390 add_map_entry(nlsr->router_name);
391
392 for(i=0;i<adj_lsdb_element;i++)
393 {
394 adj_lsa=e->data;
395 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
396 hashtb_next(e);
397 }
398
399 hashtb_end(e);
400
401}
402
403void
404add_map_entry(char *router)
405{
406
407 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
408
409 struct hashtb_enumerator ee;
410 struct hashtb_enumerator *e = &ee;
411 int res;
412
413 hashtb_start(nlsr->map, e);
414 res = hashtb_seek(e, router, strlen(router), 0);
415
416 if(res == HT_NEW_ENTRY)
417 {
418 me=e->data;
419 me->router=(char *)malloc(strlen(router)+1);
420 memset(me->router,0,strlen(router)+1);
421 memcpy(me->router,router,strlen(router));
422 me->mapping=0;
423 }
424
425 hashtb_end(e);
426
427}
428
429
430void
431add_adj_data_to_map(char *orig_router, char *body, int no_link)
432{
433 add_map_entry(orig_router);
434
435 int i=0;
436 char *lsa_data=(char *)malloc(strlen(body)+1);
437 memset( lsa_data,0,strlen(body)+1);
438 memcpy(lsa_data,body,strlen(body)+1);
439 char *sep="|";
440 char *rem;
441 char *rtr_id;
442 char *length;
443 char *face;
444 char *metric;
445
446 if(no_link >0 )
447 {
448 rtr_id=strtok_r(lsa_data,sep,&rem);
449 length=strtok_r(NULL,sep,&rem);
450 face=strtok_r(NULL,sep,&rem);
451 metric=strtok_r(NULL,sep,&rem);
452
453 add_map_entry(rtr_id);
454
455 for(i=1;i<no_link;i++)
456 {
457 rtr_id=strtok_r(NULL,sep,&rem);
458 length=strtok_r(NULL,sep,&rem);
459 face=strtok_r(NULL,sep,&rem);
460 metric=strtok_r(NULL,sep,&rem);
461
462 add_map_entry(rtr_id);
463
464 }
465 }
466
467 free(lsa_data);
468}
469
470int
471get_mapping_no(char *router)
472{
473 struct map_entry *me;
474
475 struct hashtb_enumerator ee;
476 struct hashtb_enumerator *e = &ee;
477 int res;
478 int ret;
479
480 int n = hashtb_n(nlsr->map);
481
482 if ( n < 1)
483 {
484 return NO_MAPPING_NUM;
485 }
486
487 hashtb_start(nlsr->map, e);
488 res = hashtb_seek(e, router, strlen(router), 0);
489
490 if( res == HT_OLD_ENTRY )
491 {
492 me=e->data;
493 ret=me->mapping;
494 }
495 else if(res == HT_NEW_ENTRY)
496 {
497 hashtb_delete(e);
498 ret=NO_MAPPING_NUM;
499 }
500
501 hashtb_end(e);
502
503 return ret;
504
505}
506
507
508void
509add_rev_map_entry(long int mapping_number, char *router)
510{
511
512 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
513
514 struct hashtb_enumerator ee;
515 struct hashtb_enumerator *e = &ee;
516 int res;
517
518 hashtb_start(nlsr->rev_map, e);
519 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
520
521 if(res == HT_NEW_ENTRY)
522 {
523 me=e->data;
524 me->router=(char *)malloc(strlen(router)+1);
525 memset(me->router,0,strlen(router)+1);
526 memcpy(me->router,router,strlen(router));
527 me->mapping=mapping_number;
528 }
529
530 hashtb_end(e);
531
532}
533
534
535
536char *
537get_router_from_rev_map(long int mapping_number)
538{
539
540 struct map_entry *me;
541
542 struct hashtb_enumerator ee;
543 struct hashtb_enumerator *e = &ee;
544 int res;
545
546 hashtb_start(nlsr->rev_map, e);
547 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
548
549 if(res == HT_OLD_ENTRY)
550 {
551 me=e->data;
552 hashtb_end(e);
553 return me->router;
554 }
555 else if(res == HT_NEW_ENTRY)
556 {
557 hashtb_delete(e);
558 hashtb_end(e);
559 }
560
561 return NULL;
562}
563
564void
565print_rev_map(void)
566{
567 int i, map_element;
568 struct map_entry *me;
569
570 struct hashtb_enumerator ee;
571 struct hashtb_enumerator *e = &ee;
572
573 hashtb_start(nlsr->map, e);
574 map_element=hashtb_n(nlsr->map);
575
576 for(i=0;i<map_element;i++)
577 {
578 me=e->data;
579 if ( nlsr->debugging )
580 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
581 if ( nlsr->detailed_logging )
582 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
583 hashtb_next(e);
584 }
585
586 hashtb_end(e);
587}
588
589
590void
591assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
592{
593 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
594 int mapping_nbr_router;
595
596 int i;
597 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
598 memset( lsa_data,0,strlen(adj_lsa->body)+1);
599 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
600 char *sep="|";
601 char *rem;
602 char *rtr_id;
603 char *length;
604 char *face;
605 char *metric;
606
607 if(adj_lsa->no_link >0 )
608 {
609 rtr_id=strtok_r(lsa_data,sep,&rem);
610 length=strtok_r(NULL,sep,&rem);
611 face=strtok_r(NULL,sep,&rem);
612 metric=strtok_r(NULL,sep,&rem);
613
614 mapping_nbr_router=get_mapping_no(rtr_id);
615 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
616
617 for(i=1;i<adj_lsa->no_link;i++)
618 {
619 rtr_id=strtok_r(NULL,sep,&rem);
620 length=strtok_r(NULL,sep,&rem);
621 face=strtok_r(NULL,sep,&rem);
622 metric=strtok_r(NULL,sep,&rem);
623
624 mapping_nbr_router=get_mapping_no(rtr_id);
625 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
626
627 }
628 }
629
630 free(lsa_data);
631}
632
633void
634make_adj_matrix(int **adj_matrix,int map_element)
635{
636
637 init_adj_matrix(adj_matrix,map_element);
638
639 int i, adj_lsdb_element;
640 struct alsa *adj_lsa;
641
642 struct hashtb_enumerator ee;
643 struct hashtb_enumerator *e = &ee;
644
645 hashtb_start(nlsr->lsdb->adj_lsdb, e);
646 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
647
648 for(i=0;i<adj_lsdb_element;i++)
649 {
650 adj_lsa=e->data;
651 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
652 hashtb_next(e);
653 }
654
655 hashtb_end(e);
656
657}
658
659void
660init_adj_matrix(int **adj_matrix,int map_element)
661{
662 int i, j;
663 for(i=0;i<map_element;i++)
664 for(j=0;j<map_element;j++)
665 adj_matrix[i][j]=0;
666}
667
668void print_adj_matrix(int **adj_matrix, int map_element)
669{
670 int i, j;
671 for(i=0;i<map_element;i++)
672 {
673 for(j=0;j<map_element;j++)
674 printf("%d ",adj_matrix[i][j]);
675 printf("\n");
676 }
677}
678
679
680int
681get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
682{
683 int no_link=0;
684 int i;
685
686 for(i=0;i<V;i++)
687 {
688 if ( adj_matrix[S][i] > 0 )
689 {
690 no_link++;
691 }
692 }
693 return no_link;
694}
695
696void
697get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
698{
699 int i,j;
700 j=0;
701 for (i=0; i <V; i++)
702 {
703 if ( adj_matrix[S][i] > 0 )
704 {
705 links[j]=i;
706 link_costs[j]=adj_matrix[S][i];
707 j++;
708 }
709 }
710}
711
712void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
713{
714 int i;
715 for ( i = 0; i < V; i++ )
716 {
717 if ( i == link )
718 {
719 adj_matrix[S][i]=link_cost;
720 }
721 else
722 {
723 adj_matrix[S][i]=0;
724 }
725 }
726
727}
728
729int
730get_number_of_next_hop(char *dest_router)
731{
732 struct routing_table_entry *rte;
733
734 struct hashtb_enumerator ee;
735 struct hashtb_enumerator *e = &ee;
736 int res,ret;
737
738 hashtb_start(nlsr->routing_table, e);
739 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
740
741 if( res == HT_OLD_ENTRY )
742 {
743 rte=e->data;
744 ret=hashtb_n(rte->face_list);
745 //nhl=rte->face_list;
746 }
747 else if(res == HT_NEW_ENTRY)
748 {
749 hashtb_delete(e);
750 ret=NO_NEXT_HOP;
751 }
752
753 hashtb_end(e);
754
755 return ret;
756}
757
758
759int
760get_next_hop(char *dest_router,int *faces, int *route_costs)
761{
762 struct routing_table_entry *rte;
763
764 struct hashtb_enumerator ee;
765 struct hashtb_enumerator *e = &ee;
766 int res,ret;
767
768 hashtb_start(nlsr->routing_table, e);
769 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
770
771 if( res == HT_OLD_ENTRY )
772 {
773 rte=e->data;
774 ret=hashtb_n(rte->face_list);
775 //nhl=rte->face_list;
776 int j,face_list_element;
777 struct face_list_entry *fle;
778
779 struct hashtb_enumerator eef;
780 struct hashtb_enumerator *ef = &eef;
781
782 hashtb_start(rte->face_list, ef);
783 face_list_element=hashtb_n(rte->face_list);
784 for(j=0;j<face_list_element;j++)
785 {
786 fle=ef->data;
787 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
788 faces[j]=fle->next_hop_face;
789 route_costs[j]=fle->route_cost;
790 hashtb_next(ef);
791 }
792 hashtb_end(ef);
793
794 }
795 else if(res == HT_NEW_ENTRY)
796 {
797 hashtb_delete(e);
798 ret=NO_NEXT_HOP;
799 }
800
801 hashtb_end(e);
802
803 return ret;
804}
805
806void
807add_next_hop_router(char *dest_router)
808{
809 if ( strcmp(dest_router,nlsr->router_name)== 0)
810 {
811 return ;
812 }
813
814 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
815
816 struct hashtb_enumerator ee;
817 struct hashtb_enumerator *e = &ee;
818 int res;
819
820 hashtb_start(nlsr->routing_table, e);
821 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
822
823 if( res == HT_NEW_ENTRY )
824 {
825 rte=e->data;
826 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
827 memset(rte->dest_router,0,strlen(dest_router)+1);
828 memcpy(rte->dest_router,dest_router,strlen(dest_router));
829 //rte->next_hop_face=NO_NEXT_HOP;
830 struct hashtb_param param_fle = {0};
831 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
832
833 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
834 }
835 hashtb_end(e);
836
837}
838
839void
840add_next_hop_from_lsa_adj_body(char *body, int no_link)
841{
842
843 int i=0;
844 char *lsa_data=(char *)malloc(strlen(body)+1);
845 memset( lsa_data,0,strlen(body)+1);
846 memcpy(lsa_data,body,strlen(body)+1);
847 char *sep="|";
848 char *rem;
849 char *rtr_id;
850 char *length;
851 char *face;
852 char *metric;
853
854 if(no_link >0 )
855 {
856 rtr_id=strtok_r(lsa_data,sep,&rem);
857 length=strtok_r(NULL,sep,&rem);
858 face=strtok_r(NULL,sep,&rem);
859 metric=strtok_r(NULL,sep,&rem);
860
861
862 add_next_hop_router(rtr_id);
863
864 for(i=1;i<no_link;i++)
865 {
866 rtr_id=strtok_r(NULL,sep,&rem);
867 length=strtok_r(NULL,sep,&rem);
868 face=strtok_r(NULL,sep,&rem);
869 metric=strtok_r(NULL,sep,&rem);
870
871 add_next_hop_router(rtr_id);
872
873 }
874 }
875
876 free(lsa_data);
877
878
879}
880
881void
882update_routing_table(char * dest_router,int next_hop_face, int route_cost)
883{
akmhoque4f4b0212013-01-22 11:54:47 -0600884 if ( nlsr->debugging )
885 {
886 printf("update_routing_table called \n");
887 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
888 }
889
akmhoque8fdd6412012-12-04 15:05:33 -0600890 int res,res1;
891 struct routing_table_entry *rte;
892
893 struct hashtb_enumerator ee;
894 struct hashtb_enumerator *e = &ee;
895
896 hashtb_start(nlsr->routing_table, e);
897 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
898
899 if( res == HT_OLD_ENTRY )
900 {
901 rte=e->data;
902
903 struct hashtb_enumerator eef;
904 struct hashtb_enumerator *ef = &eef;
905
906 hashtb_start(rte->face_list, ef);
907 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
908 if( res1 == HT_NEW_ENTRY)
909 {
910 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
911 fle=ef->data;
912 fle->next_hop_face=next_hop_face;
913 fle->route_cost=route_cost;
914 }
915 else if ( res1 == HT_OLD_ENTRY )
916 {
917 struct face_list_entry *fle;
918 fle=ef->data;
919 fle->route_cost=route_cost;
920 }
921 hashtb_end(ef);
akmhoque8fdd6412012-12-04 15:05:33 -0600922 }
923 else if ( res == HT_OLD_ENTRY )
924 {
925 hashtb_delete(e);
926 }
927
928 hashtb_end(e);
929
930}
931
932void
933print_routing_table(void)
934{
935 if ( nlsr->debugging )
936 printf("print_routing_table called\n");
937 if ( nlsr->detailed_logging )
938 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
939 int i,j, rt_element,face_list_element;
940
941 struct routing_table_entry *rte;
942
943 struct hashtb_enumerator ee;
944 struct hashtb_enumerator *e = &ee;
945
946 hashtb_start(nlsr->routing_table, e);
947 rt_element=hashtb_n(nlsr->routing_table);
948
949 for(i=0;i<rt_element;i++)
950 {
951 if ( nlsr->debugging )
952 printf("----------Routing Table Entry %d------------------\n",i+1);
953 if ( nlsr->detailed_logging )
954 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
955
956 rte=e->data;
957
958 if ( nlsr->debugging )
959 printf(" Destination Router: %s \n",rte->dest_router);
960 if ( nlsr->detailed_logging )
961 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
962
963
964 //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);
965
966 struct face_list_entry *fle;
967
968 struct hashtb_enumerator eef;
969 struct hashtb_enumerator *ef = &eef;
970
971 hashtb_start(rte->face_list, ef);
972 face_list_element=hashtb_n(rte->face_list);
973 if ( face_list_element <= 0 )
974 {
975 if ( nlsr->debugging )
976 printf(" Face: No Face \n");
977 if ( nlsr->detailed_logging )
978 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
979 }
980 else
981 {
982 for(j=0;j<face_list_element;j++)
983 {
984 fle=ef->data;
985 if ( nlsr->debugging )
986 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
987 if ( nlsr->detailed_logging )
988 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
989 hashtb_next(ef);
990 }
991 }
992 hashtb_end(ef);
993
994 hashtb_next(e);
995 }
996
997 hashtb_end(e);
998
999 if ( nlsr->debugging )
1000 printf("\n");
1001 if ( nlsr->detailed_logging )
1002 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1003}
1004
1005
1006int
1007delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1008{
1009 if ( nlsr->debugging )
1010 {
1011 printf("delete_empty_rte called\n");
1012 printf("Router: %s \n",(char *)ev->evdata);
1013 }
1014 if ( nlsr->detailed_logging )
1015 {
1016 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1017 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
akmhoque8fdd6412012-12-04 15:05:33 -06001018 }
1019
1020 if(flags == CCN_SCHEDULE_CANCEL)
1021 {
1022 return -1;
1023 }
akmhoque613446f2013-01-23 10:40:12 -06001024
1025 nlsr_lock();
akmhoque8fdd6412012-12-04 15:05:33 -06001026 int res;
1027 struct hashtb_enumerator ee;
1028 struct hashtb_enumerator *e = &ee;
1029
1030 hashtb_start(nlsr->routing_table, e);
1031 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1032
1033 if ( res == HT_OLD_ENTRY )
1034 {
1035 hashtb_delete(e);
1036 }
1037 else if ( res == HT_NEW_ENTRY )
1038 {
1039 hashtb_delete(e);
1040 }
1041
1042 print_routing_table();
akmhoque613446f2013-01-23 10:40:12 -06001043
1044 nlsr_unlock();
akmhoque8fdd6412012-12-04 15:05:33 -06001045
1046 return 0;
1047}
1048
1049void
1050clear_old_routing_table(void)
1051{
1052 if ( nlsr->debugging )
1053 printf("clear_old_routing_table called\n");
1054 if ( nlsr->detailed_logging )
1055 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
1056 int i,rt_element;
1057
1058 struct routing_table_entry *rte;
1059
1060 struct hashtb_enumerator ee;
1061 struct hashtb_enumerator *e = &ee;
1062
1063 hashtb_start(nlsr->routing_table, e);
1064 rt_element=hashtb_n(nlsr->routing_table);
1065
1066 for(i=0;i<rt_element;i++)
1067 {
1068 rte=e->data;
1069 hashtb_destroy(&rte->face_list);
1070 struct hashtb_param param_fle = {0};
1071 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1072
1073 hashtb_next(e);
1074 }
1075
1076 hashtb_end(e);
1077}
1078
1079
1080void
1081do_old_routing_table_updates(void)
1082{
1083 if ( nlsr->debugging )
1084 printf("do_old_routing_table_updates called\n");
1085 if ( nlsr->detailed_logging )
1086 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1087
1088 int i, rt_element;
1089 int mapping_no;
1090
1091 struct routing_table_entry *rte;
1092
1093 struct hashtb_enumerator ee;
1094 struct hashtb_enumerator *e = &ee;
1095
1096 hashtb_start(nlsr->routing_table, e);
1097 rt_element=hashtb_n(nlsr->routing_table);
1098
1099 for(i=0;i<rt_element;i++)
1100 {
1101 rte=e->data;
1102 mapping_no=get_mapping_no(rte->dest_router);
1103 if ( mapping_no == NO_MAPPING_NUM)
1104 {
1105 delete_orig_router_from_npt(rte->dest_router);
1106 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1107 memset(router,0,strlen(rte->dest_router)+1);
1108 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1109 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
1110 }
1111 hashtb_next(e);
1112 }
1113
1114 hashtb_end(e);
1115}
1116
1117
1118
1119void
1120update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
1121{
1122 if ( nlsr->debugging )
1123 printf("update_routing_table_with_new_route called\n");
1124 if ( nlsr->detailed_logging )
1125 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
1126 int i, map_element;
1127 struct map_entry *me;
1128
1129 struct hashtb_enumerator ee;
1130 struct hashtb_enumerator *e = &ee;
1131
1132 hashtb_start(nlsr->rev_map, e);
1133 map_element=hashtb_n(nlsr->rev_map);
1134
1135 for(i=0;i<map_element;i++)
1136 {
1137 me=e->data;
1138 if(me->mapping != source)
1139 {
1140
1141 char *orig_router=get_router_from_rev_map(me->mapping);
1142 if (orig_router != NULL )
1143 {
1144 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoque8fdd6412012-12-04 15:05:33 -06001145 if ( next_hop_router_num == NO_NEXT_HOP )
1146 {
akmhoque8fdd6412012-12-04 15:05:33 -06001147 if ( nlsr->debugging )
1148 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1149 if ( nlsr->detailed_logging )
1150 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1151 }
1152 else
1153 {
1154 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoque8fdd6412012-12-04 15:05:33 -06001155 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoque8fdd6412012-12-04 15:05:33 -06001156 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
1157 if ( nlsr->debugging )
1158 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1159 if ( nlsr->detailed_logging )
1160 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1161
1162
1163 }
1164 }
1165 }
1166 hashtb_next(e);
1167 }
1168
1169 hashtb_end(e);
1170}
1171
1172int
1173does_face_exist_for_router(char *dest_router, int face_id)
1174{
akmhoque0b217bf2013-01-23 09:49:47 -06001175 if (nlsr->debugging)
1176 {
akmhoque10489a82013-01-23 09:59:15 -06001177 printf("does_face_exist_for_router called\n");
akmhoque0b217bf2013-01-23 09:49:47 -06001178 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1179 }
1180
akmhoque613446f2013-01-23 10:40:12 -06001181 //print_routing_table();
akmhoque0b217bf2013-01-23 09:49:47 -06001182
akmhoque8fdd6412012-12-04 15:05:33 -06001183 int ret=0;
1184
akmhoque869ce022013-01-23 10:14:47 -06001185 int res;
akmhoque8fdd6412012-12-04 15:05:33 -06001186 struct routing_table_entry *rte;
1187
1188 struct hashtb_enumerator ee;
1189 struct hashtb_enumerator *e = &ee;
1190
1191 hashtb_start(nlsr->routing_table, e);
1192 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1193
1194 if( res == HT_OLD_ENTRY )
1195 {
1196 rte=e->data;
akmhoque869ce022013-01-23 10:14:47 -06001197 /*
akmhoque8fdd6412012-12-04 15:05:33 -06001198 struct hashtb_enumerator eef;
1199 struct hashtb_enumerator *ef = &eef;
1200
1201 hashtb_start(rte->face_list, ef);
1202 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1203 if( res1 == HT_OLD_ENTRY)
1204 {
1205 ret=1;
1206 }
akmhoque869ce022013-01-23 10:14:47 -06001207 else
akmhoque8fdd6412012-12-04 15:05:33 -06001208 {
1209 hashtb_delete(ef);
1210 }
1211 hashtb_end(ef);
akmhoque869ce022013-01-23 10:14:47 -06001212 */
1213 unsigned *v;
1214 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1215 if (v != NULL)
1216 ret = 1;
akmhoque8fdd6412012-12-04 15:05:33 -06001217 }
akmhoque869ce022013-01-23 10:14:47 -06001218 else
akmhoque8fdd6412012-12-04 15:05:33 -06001219 {
1220 hashtb_delete(e);
1221 }
1222
1223 hashtb_end(e);
1224
akmhoque613446f2013-01-23 10:40:12 -06001225 //print_routing_table();
akmhoque0b217bf2013-01-23 09:49:47 -06001226
akmhoque8fdd6412012-12-04 15:05:33 -06001227 return ret;
1228}