blob: 65a28f4d0be3a254f7074001548e36ef1109c6ed [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);
akmhoque580ccf32013-01-23 09:12:33 -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);
akmhoque580ccf32013-01-23 09:12:33 -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 }
189 free(Q);
190 //free(dist);
191}
192
193void
194print_all_path_from_source(long int *parent,long int source)
195{
196 int i, map_element;
197 struct map_entry *me;
198
199 struct hashtb_enumerator ee;
200 struct hashtb_enumerator *e = &ee;
201
202 hashtb_start(nlsr->map, e);
203 map_element=hashtb_n(nlsr->map);
204
205 if ( source != NO_MAPPING_NUM)
206 {
207 for(i=0;i<map_element;i++)
208 {
209 me=e->data;
210 if(me->mapping != source)
211 {
212
213 if ( nlsr->debugging )
214 {
215 print_path(parent,(long int)me->mapping);
216 printf("\n");
217 }
218 if ( nlsr->detailed_logging )
219 {
220 print_path(parent,(long int)me->mapping);
221 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
222 }
223
224 }
225 hashtb_next(e);
226 }
227 }
228 hashtb_end(e);
229
230}
231
232void
233print_all_next_hop(long int *parent,long int source)
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 if(me->mapping != source)
248 {
249 if ( nlsr->debugging )
250 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
251 if ( nlsr->detailed_logging )
252 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
253 }
254 hashtb_next(e);
255 }
256
257 hashtb_end(e);
258
259}
260
261long int
262get_next_hop_from_calculation(long int *parent, long int dest,long int source)
263{
264 long int next_hop;
265 while ( parent[dest] != EMPTY_PARENT )
266 {
267 next_hop=dest;
268 dest=parent[dest];
269
270 }
271
272 if ( dest != source )
273 {
274 next_hop=NO_NEXT_HOP;
275 }
276
277 return next_hop;
278
279}
280
281void
282print_path(long int *parent, long int dest)
283{
284 if (parent[dest] != EMPTY_PARENT )
285 print_path(parent,parent[dest]);
a64adam04be6482013-01-17 11:29:32 -0600286
287 if ( nlsr->debugging )
288 printf(" %ld",dest);
akmhoque8fdd6412012-12-04 15:05:33 -0600289}
290
291int
292is_not_explored(long int *Q, long int u,long int start, long int element)
293{
294 int ret=0;
295 long int i;
296 for(i=start; i< element; i++)
297 {
298 if ( Q[i] == u )
299 {
300 ret=1;
301 break;
302 }
303 }
304 return ret;
305}
306
307void
308sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
309{
310 long int i,j;
311 long int temp_u;
312
313 for ( i=start ; i < element ; i ++)
314 {
315 for( j=i+1; j<element; j ++)
316 {
317 if (dist[Q[j]] < dist[Q[i]])
318 {
319 temp_u=Q[j];
320 Q[j]=Q[i];
321 Q[i]=temp_u;
322 }
323 }
324 }
325
326}
327
328void
329print_map(void)
330{
331 int i, map_element;
332 struct map_entry *me;
333
334 struct hashtb_enumerator ee;
335 struct hashtb_enumerator *e = &ee;
336
337 hashtb_start(nlsr->map, e);
338 map_element=hashtb_n(nlsr->map);
339
340 for(i=0;i<map_element;i++)
341 {
342 me=e->data;
343 if ( nlsr->debugging )
344 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
345 if ( nlsr->detailed_logging )
346 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
347 hashtb_next(e);
348 }
349
350 hashtb_end(e);
351}
352
353
354void
355assign_mapping_number(void)
356{
357 int i, map_element;
358 struct map_entry *me;
359
360 struct hashtb_enumerator ee;
361 struct hashtb_enumerator *e = &ee;
362
363 hashtb_start(nlsr->map, e);
364 map_element=hashtb_n(nlsr->map);
365
366 for(i=0;i<map_element;i++)
367 {
368 me=e->data;
369 me->mapping=i;
370 add_rev_map_entry(i,me->router);
371 hashtb_next(e);
372 }
373
374 hashtb_end(e);
375}
376
377void
378make_map(void)
379{
380
381
382 int i, adj_lsdb_element;
383 struct alsa *adj_lsa;
384
385 struct hashtb_enumerator ee;
386 struct hashtb_enumerator *e = &ee;
387
388 hashtb_start(nlsr->lsdb->adj_lsdb, e);
389 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
390
391 add_map_entry(nlsr->router_name);
392
393 for(i=0;i<adj_lsdb_element;i++)
394 {
395 adj_lsa=e->data;
396 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
397 hashtb_next(e);
398 }
399
400 hashtb_end(e);
401
402}
403
404void
405add_map_entry(char *router)
406{
407
408 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
409
410 struct hashtb_enumerator ee;
411 struct hashtb_enumerator *e = &ee;
412 int res;
413
414 hashtb_start(nlsr->map, e);
415 res = hashtb_seek(e, router, strlen(router), 0);
416
417 if(res == HT_NEW_ENTRY)
418 {
419 me=e->data;
420 me->router=(char *)malloc(strlen(router)+1);
421 memset(me->router,0,strlen(router)+1);
422 memcpy(me->router,router,strlen(router));
423 me->mapping=0;
424 }
425
426 hashtb_end(e);
427
428}
429
430
431void
432add_adj_data_to_map(char *orig_router, char *body, int no_link)
433{
434 add_map_entry(orig_router);
435
436 int i=0;
437 char *lsa_data=(char *)malloc(strlen(body)+1);
438 memset( lsa_data,0,strlen(body)+1);
439 memcpy(lsa_data,body,strlen(body)+1);
440 char *sep="|";
441 char *rem;
442 char *rtr_id;
443 char *length;
444 char *face;
445 char *metric;
446
447 if(no_link >0 )
448 {
449 rtr_id=strtok_r(lsa_data,sep,&rem);
450 length=strtok_r(NULL,sep,&rem);
451 face=strtok_r(NULL,sep,&rem);
452 metric=strtok_r(NULL,sep,&rem);
453
454 add_map_entry(rtr_id);
455
456 for(i=1;i<no_link;i++)
457 {
458 rtr_id=strtok_r(NULL,sep,&rem);
459 length=strtok_r(NULL,sep,&rem);
460 face=strtok_r(NULL,sep,&rem);
461 metric=strtok_r(NULL,sep,&rem);
462
463 add_map_entry(rtr_id);
464
465 }
466 }
467
468 free(lsa_data);
469}
470
471int
472get_mapping_no(char *router)
473{
474 struct map_entry *me;
475
476 struct hashtb_enumerator ee;
477 struct hashtb_enumerator *e = &ee;
478 int res;
479 int ret;
480
481 int n = hashtb_n(nlsr->map);
482
483 if ( n < 1)
484 {
485 return NO_MAPPING_NUM;
486 }
487
488 hashtb_start(nlsr->map, e);
489 res = hashtb_seek(e, router, strlen(router), 0);
490
491 if( res == HT_OLD_ENTRY )
492 {
493 me=e->data;
494 ret=me->mapping;
495 }
496 else if(res == HT_NEW_ENTRY)
497 {
498 hashtb_delete(e);
499 ret=NO_MAPPING_NUM;
500 }
501
502 hashtb_end(e);
503
504 return ret;
505
506}
507
508
509void
510add_rev_map_entry(long int mapping_number, char *router)
511{
512
513 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
514
515 struct hashtb_enumerator ee;
516 struct hashtb_enumerator *e = &ee;
517 int res;
518
519 hashtb_start(nlsr->rev_map, e);
520 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
521
522 if(res == HT_NEW_ENTRY)
523 {
524 me=e->data;
525 me->router=(char *)malloc(strlen(router)+1);
526 memset(me->router,0,strlen(router)+1);
527 memcpy(me->router,router,strlen(router));
528 me->mapping=mapping_number;
529 }
530
531 hashtb_end(e);
532
533}
534
535
536
537char *
538get_router_from_rev_map(long int mapping_number)
539{
540
541 struct map_entry *me;
542
543 struct hashtb_enumerator ee;
544 struct hashtb_enumerator *e = &ee;
545 int res;
546
547 hashtb_start(nlsr->rev_map, e);
548 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
549
550 if(res == HT_OLD_ENTRY)
551 {
552 me=e->data;
553 hashtb_end(e);
554 return me->router;
555 }
556 else if(res == HT_NEW_ENTRY)
557 {
558 hashtb_delete(e);
559 hashtb_end(e);
560 }
561
562 return NULL;
563}
564
565void
566print_rev_map(void)
567{
568 int i, map_element;
569 struct map_entry *me;
570
571 struct hashtb_enumerator ee;
572 struct hashtb_enumerator *e = &ee;
573
574 hashtb_start(nlsr->map, e);
575 map_element=hashtb_n(nlsr->map);
576
577 for(i=0;i<map_element;i++)
578 {
579 me=e->data;
580 if ( nlsr->debugging )
581 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
582 if ( nlsr->detailed_logging )
583 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
584 hashtb_next(e);
585 }
586
587 hashtb_end(e);
588}
589
590
591void
592assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
593{
594 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
595 int mapping_nbr_router;
596
597 int i;
598 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
599 memset( lsa_data,0,strlen(adj_lsa->body)+1);
600 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
601 char *sep="|";
602 char *rem;
603 char *rtr_id;
604 char *length;
605 char *face;
606 char *metric;
607
608 if(adj_lsa->no_link >0 )
609 {
610 rtr_id=strtok_r(lsa_data,sep,&rem);
611 length=strtok_r(NULL,sep,&rem);
612 face=strtok_r(NULL,sep,&rem);
613 metric=strtok_r(NULL,sep,&rem);
614
615 mapping_nbr_router=get_mapping_no(rtr_id);
616 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
617
618 for(i=1;i<adj_lsa->no_link;i++)
619 {
620 rtr_id=strtok_r(NULL,sep,&rem);
621 length=strtok_r(NULL,sep,&rem);
622 face=strtok_r(NULL,sep,&rem);
623 metric=strtok_r(NULL,sep,&rem);
624
625 mapping_nbr_router=get_mapping_no(rtr_id);
626 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
627
628 }
629 }
630
631 free(lsa_data);
632}
633
634void
635make_adj_matrix(int **adj_matrix,int map_element)
636{
637
638 init_adj_matrix(adj_matrix,map_element);
639
640 int i, adj_lsdb_element;
641 struct alsa *adj_lsa;
642
643 struct hashtb_enumerator ee;
644 struct hashtb_enumerator *e = &ee;
645
646 hashtb_start(nlsr->lsdb->adj_lsdb, e);
647 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
648
649 for(i=0;i<adj_lsdb_element;i++)
650 {
651 adj_lsa=e->data;
652 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
653 hashtb_next(e);
654 }
655
656 hashtb_end(e);
657
658}
659
660void
661init_adj_matrix(int **adj_matrix,int map_element)
662{
663 int i, j;
664 for(i=0;i<map_element;i++)
665 for(j=0;j<map_element;j++)
666 adj_matrix[i][j]=0;
667}
668
669void print_adj_matrix(int **adj_matrix, int map_element)
670{
671 int i, j;
672 for(i=0;i<map_element;i++)
673 {
674 for(j=0;j<map_element;j++)
675 printf("%d ",adj_matrix[i][j]);
676 printf("\n");
677 }
678}
679
680
681int
682get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
683{
684 int no_link=0;
685 int i;
686
687 for(i=0;i<V;i++)
688 {
689 if ( adj_matrix[S][i] > 0 )
690 {
691 no_link++;
692 }
693 }
694 return no_link;
695}
696
697void
698get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
699{
700 int i,j;
701 j=0;
702 for (i=0; i <V; i++)
703 {
704 if ( adj_matrix[S][i] > 0 )
705 {
706 links[j]=i;
707 link_costs[j]=adj_matrix[S][i];
708 j++;
709 }
710 }
711}
712
713void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
714{
715 int i;
716 for ( i = 0; i < V; i++ )
717 {
718 if ( i == link )
719 {
720 adj_matrix[S][i]=link_cost;
721 }
722 else
723 {
724 adj_matrix[S][i]=0;
725 }
726 }
727
728}
729
730int
731get_number_of_next_hop(char *dest_router)
732{
733 struct routing_table_entry *rte;
734
735 struct hashtb_enumerator ee;
736 struct hashtb_enumerator *e = &ee;
737 int res,ret;
738
739 hashtb_start(nlsr->routing_table, e);
740 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
741
742 if( res == HT_OLD_ENTRY )
743 {
744 rte=e->data;
745 ret=hashtb_n(rte->face_list);
746 //nhl=rte->face_list;
747 }
748 else if(res == HT_NEW_ENTRY)
749 {
750 hashtb_delete(e);
751 ret=NO_NEXT_HOP;
752 }
753
754 hashtb_end(e);
755
756 return ret;
757}
758
759
760int
761get_next_hop(char *dest_router,int *faces, int *route_costs)
762{
763 struct routing_table_entry *rte;
764
765 struct hashtb_enumerator ee;
766 struct hashtb_enumerator *e = &ee;
767 int res,ret;
768
769 hashtb_start(nlsr->routing_table, e);
770 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
771
772 if( res == HT_OLD_ENTRY )
773 {
774 rte=e->data;
775 ret=hashtb_n(rte->face_list);
776 //nhl=rte->face_list;
777 int j,face_list_element;
778 struct face_list_entry *fle;
779
780 struct hashtb_enumerator eef;
781 struct hashtb_enumerator *ef = &eef;
782
783 hashtb_start(rte->face_list, ef);
784 face_list_element=hashtb_n(rte->face_list);
785 for(j=0;j<face_list_element;j++)
786 {
787 fle=ef->data;
788 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
789 faces[j]=fle->next_hop_face;
790 route_costs[j]=fle->route_cost;
791 hashtb_next(ef);
792 }
793 hashtb_end(ef);
794
795 }
796 else if(res == HT_NEW_ENTRY)
797 {
798 hashtb_delete(e);
799 ret=NO_NEXT_HOP;
800 }
801
802 hashtb_end(e);
803
804 return ret;
805}
806
807void
808add_next_hop_router(char *dest_router)
809{
810 if ( strcmp(dest_router,nlsr->router_name)== 0)
811 {
812 return ;
813 }
814
815 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
816
817 struct hashtb_enumerator ee;
818 struct hashtb_enumerator *e = &ee;
819 int res;
820
821 hashtb_start(nlsr->routing_table, e);
822 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
823
824 if( res == HT_NEW_ENTRY )
825 {
826 rte=e->data;
827 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
828 memset(rte->dest_router,0,strlen(dest_router)+1);
829 memcpy(rte->dest_router,dest_router,strlen(dest_router));
830 //rte->next_hop_face=NO_NEXT_HOP;
831 struct hashtb_param param_fle = {0};
832 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
833
834 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
835 }
836 hashtb_end(e);
837
838}
839
840void
841add_next_hop_from_lsa_adj_body(char *body, int no_link)
842{
843
844 int i=0;
845 char *lsa_data=(char *)malloc(strlen(body)+1);
846 memset( lsa_data,0,strlen(body)+1);
847 memcpy(lsa_data,body,strlen(body)+1);
848 char *sep="|";
849 char *rem;
850 char *rtr_id;
851 char *length;
852 char *face;
853 char *metric;
854
855 if(no_link >0 )
856 {
857 rtr_id=strtok_r(lsa_data,sep,&rem);
858 length=strtok_r(NULL,sep,&rem);
859 face=strtok_r(NULL,sep,&rem);
860 metric=strtok_r(NULL,sep,&rem);
861
862
863 add_next_hop_router(rtr_id);
864
865 for(i=1;i<no_link;i++)
866 {
867 rtr_id=strtok_r(NULL,sep,&rem);
868 length=strtok_r(NULL,sep,&rem);
869 face=strtok_r(NULL,sep,&rem);
870 metric=strtok_r(NULL,sep,&rem);
871
872 add_next_hop_router(rtr_id);
873
874 }
875 }
876
877 free(lsa_data);
878
879
880}
881
882void
883update_routing_table(char * dest_router,int next_hop_face, int route_cost)
884{
akmhoque4f4b0212013-01-22 11:54:47 -0600885 if ( nlsr->debugging )
886 {
887 printf("update_routing_table called \n");
888 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
889 }
890
akmhoque8fdd6412012-12-04 15:05:33 -0600891 int res,res1;
892 struct routing_table_entry *rte;
893
894 struct hashtb_enumerator ee;
895 struct hashtb_enumerator *e = &ee;
896
897 hashtb_start(nlsr->routing_table, e);
898 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
899
900 if( res == HT_OLD_ENTRY )
901 {
902 rte=e->data;
903
904 struct hashtb_enumerator eef;
905 struct hashtb_enumerator *ef = &eef;
906
907 hashtb_start(rte->face_list, ef);
908 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
909 if( res1 == HT_NEW_ENTRY)
910 {
911 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
912 fle=ef->data;
913 fle->next_hop_face=next_hop_face;
914 fle->route_cost=route_cost;
915 }
916 else if ( res1 == HT_OLD_ENTRY )
917 {
918 struct face_list_entry *fle;
919 fle=ef->data;
920 fle->route_cost=route_cost;
921 }
922 hashtb_end(ef);
akmhoque8fdd6412012-12-04 15:05:33 -0600923 }
924 else if ( res == HT_OLD_ENTRY )
925 {
926 hashtb_delete(e);
927 }
928
929 hashtb_end(e);
930
931}
932
933void
934print_routing_table(void)
935{
936 if ( nlsr->debugging )
937 printf("print_routing_table called\n");
938 if ( nlsr->detailed_logging )
939 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
940 int i,j, rt_element,face_list_element;
941
942 struct routing_table_entry *rte;
943
944 struct hashtb_enumerator ee;
945 struct hashtb_enumerator *e = &ee;
946
947 hashtb_start(nlsr->routing_table, e);
948 rt_element=hashtb_n(nlsr->routing_table);
949
950 for(i=0;i<rt_element;i++)
951 {
952 if ( nlsr->debugging )
953 printf("----------Routing Table Entry %d------------------\n",i+1);
954 if ( nlsr->detailed_logging )
955 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
956
957 rte=e->data;
958
959 if ( nlsr->debugging )
960 printf(" Destination Router: %s \n",rte->dest_router);
961 if ( nlsr->detailed_logging )
962 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
963
964
965 //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);
966
967 struct face_list_entry *fle;
968
969 struct hashtb_enumerator eef;
970 struct hashtb_enumerator *ef = &eef;
971
972 hashtb_start(rte->face_list, ef);
973 face_list_element=hashtb_n(rte->face_list);
974 if ( face_list_element <= 0 )
975 {
976 if ( nlsr->debugging )
977 printf(" Face: No Face \n");
978 if ( nlsr->detailed_logging )
979 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
980 }
981 else
982 {
983 for(j=0;j<face_list_element;j++)
984 {
985 fle=ef->data;
986 if ( nlsr->debugging )
987 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
988 if ( nlsr->detailed_logging )
989 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
990 hashtb_next(ef);
991 }
992 }
993 hashtb_end(ef);
994
995 hashtb_next(e);
996 }
997
998 hashtb_end(e);
999
1000 if ( nlsr->debugging )
1001 printf("\n");
1002 if ( nlsr->detailed_logging )
1003 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1004}
1005
1006
1007int
1008delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1009{
1010 if ( nlsr->debugging )
1011 {
1012 printf("delete_empty_rte called\n");
1013 printf("Router: %s \n",(char *)ev->evdata);
1014 }
1015 if ( nlsr->detailed_logging )
1016 {
1017 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1018 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
1019 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
1020 }
1021
1022 if(flags == CCN_SCHEDULE_CANCEL)
1023 {
1024 return -1;
1025 }
1026 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();
1043
1044 return 0;
1045}
1046
1047void
1048clear_old_routing_table(void)
1049{
1050 if ( nlsr->debugging )
1051 printf("clear_old_routing_table called\n");
1052 if ( nlsr->detailed_logging )
1053 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
1054 int i,rt_element;
1055
1056 struct routing_table_entry *rte;
1057
1058 struct hashtb_enumerator ee;
1059 struct hashtb_enumerator *e = &ee;
1060
1061 hashtb_start(nlsr->routing_table, e);
1062 rt_element=hashtb_n(nlsr->routing_table);
1063
1064 for(i=0;i<rt_element;i++)
1065 {
1066 rte=e->data;
1067 hashtb_destroy(&rte->face_list);
1068 struct hashtb_param param_fle = {0};
1069 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1070
1071 hashtb_next(e);
1072 }
1073
1074 hashtb_end(e);
1075}
1076
1077
1078void
1079do_old_routing_table_updates(void)
1080{
1081 if ( nlsr->debugging )
1082 printf("do_old_routing_table_updates called\n");
1083 if ( nlsr->detailed_logging )
1084 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1085
1086 int i, rt_element;
1087 int mapping_no;
1088
1089 struct routing_table_entry *rte;
1090
1091 struct hashtb_enumerator ee;
1092 struct hashtb_enumerator *e = &ee;
1093
1094 hashtb_start(nlsr->routing_table, e);
1095 rt_element=hashtb_n(nlsr->routing_table);
1096
1097 for(i=0;i<rt_element;i++)
1098 {
1099 rte=e->data;
1100 mapping_no=get_mapping_no(rte->dest_router);
1101 if ( mapping_no == NO_MAPPING_NUM)
1102 {
1103 delete_orig_router_from_npt(rte->dest_router);
1104 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1105 memset(router,0,strlen(rte->dest_router)+1);
1106 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1107 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
1108 }
1109 hashtb_next(e);
1110 }
1111
1112 hashtb_end(e);
1113}
1114
1115
1116
1117void
1118update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
1119{
1120 if ( nlsr->debugging )
1121 printf("update_routing_table_with_new_route called\n");
1122 if ( nlsr->detailed_logging )
1123 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
1124 int i, map_element;
1125 struct map_entry *me;
1126
1127 struct hashtb_enumerator ee;
1128 struct hashtb_enumerator *e = &ee;
1129
1130 hashtb_start(nlsr->rev_map, e);
1131 map_element=hashtb_n(nlsr->rev_map);
1132
1133 for(i=0;i<map_element;i++)
1134 {
1135 me=e->data;
1136 if(me->mapping != source)
1137 {
1138
1139 char *orig_router=get_router_from_rev_map(me->mapping);
1140 if (orig_router != NULL )
1141 {
1142 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoque8fdd6412012-12-04 15:05:33 -06001143 if ( next_hop_router_num == NO_NEXT_HOP )
1144 {
akmhoque8fdd6412012-12-04 15:05:33 -06001145 if ( nlsr->debugging )
1146 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1147 if ( nlsr->detailed_logging )
1148 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1149 }
1150 else
1151 {
1152 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoque8fdd6412012-12-04 15:05:33 -06001153 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoque8fdd6412012-12-04 15:05:33 -06001154 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
1155 if ( nlsr->debugging )
1156 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1157 if ( nlsr->detailed_logging )
1158 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1159
1160
1161 }
1162 }
1163 }
1164 hashtb_next(e);
1165 }
1166
1167 hashtb_end(e);
1168}
1169
1170int
1171does_face_exist_for_router(char *dest_router, int face_id)
1172{
akmhoque0b217bf2013-01-23 09:49:47 -06001173 if (nlsr->debugging)
1174 {
akmhoque10489a82013-01-23 09:59:15 -06001175 printf("does_face_exist_for_router called\n");
akmhoque0b217bf2013-01-23 09:49:47 -06001176 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1177 }
1178
1179 print_routing_table();
1180
akmhoque8fdd6412012-12-04 15:05:33 -06001181 int ret=0;
1182
akmhoque869ce022013-01-23 10:14:47 -06001183 int res;
1184 //int res1;
akmhoque8fdd6412012-12-04 15:05:33 -06001185 struct routing_table_entry *rte;
1186
1187 struct hashtb_enumerator ee;
1188 struct hashtb_enumerator *e = &ee;
1189
1190 hashtb_start(nlsr->routing_table, e);
1191 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1192
1193 if( res == HT_OLD_ENTRY )
1194 {
1195 rte=e->data;
akmhoque869ce022013-01-23 10:14:47 -06001196 /*
akmhoque8fdd6412012-12-04 15:05:33 -06001197 struct hashtb_enumerator eef;
1198 struct hashtb_enumerator *ef = &eef;
1199
1200 hashtb_start(rte->face_list, ef);
1201 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1202 if( res1 == HT_OLD_ENTRY)
1203 {
1204 ret=1;
1205 }
akmhoque869ce022013-01-23 10:14:47 -06001206 else
akmhoque8fdd6412012-12-04 15:05:33 -06001207 {
1208 hashtb_delete(ef);
1209 }
1210 hashtb_end(ef);
akmhoque869ce022013-01-23 10:14:47 -06001211 */
1212 unsigned *v;
1213 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1214 if (v != NULL)
1215 ret = 1;
akmhoque8fdd6412012-12-04 15:05:33 -06001216 }
akmhoque869ce022013-01-23 10:14:47 -06001217 else
akmhoque8fdd6412012-12-04 15:05:33 -06001218 {
1219 hashtb_delete(e);
1220 }
1221
1222 hashtb_end(e);
1223
akmhoque0b217bf2013-01-23 09:49:47 -06001224 print_routing_table();
1225
akmhoque8fdd6412012-12-04 15:05:33 -06001226 return ret;
1227}