blob: 25b99dc7e74b3502112f214dac5b94d7fa4611e2 [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
83 if ( (num_link == 0) || (nlsr->multi_path_face_num <= 1 ) )
84 {
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);
89 }
90 else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
91 {
92 long int *links=(long int *)malloc(num_link*sizeof(long int));
93 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
94 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
95 for ( i=0 ; i < num_link; i++)
96 {
97 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
98 calculate_path(adj_matrix,parent,dist, map_element, source);
99 print_all_path_from_source(parent,source);
100 print_all_next_hop(parent,source);
101 update_routing_table_with_new_route(parent, dist,source);
102 }
103
104 free(links);
105 free(link_costs);
106 }
107
108 update_npt_with_new_route();
109
110 print_routing_table();
111 print_npt();
112
113 for(i = 0; i < map_element; i++)
114 {
115 free(adj_matrix[i]);
116 }
117 free(parent);
118 free(dist);
119 free(adj_matrix);
120 hashtb_destroy(&nlsr->map);
121 hashtb_destroy(&nlsr->rev_map);
122
123 }
124 nlsr->is_route_calculation_scheduled=0;
125
126 nlsr_unlock();
127
128 return 0;
129}
130
131void
132calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
133{
134 int i;
135 long int v,u;
136 //long int *dist=(long int *)malloc(V * sizeof(long int));
137 long int *Q=(long int *)malloc(V * sizeof(long int));
138 long int head=0;
139 /* Initial the Parent */
140 for (i = 0 ; i < V; i++)
141 {
142 parent[i]=EMPTY_PARENT;
143 dist[i]=INF_DISTANCE;
144 Q[i]=i;
145 }
146
147 if ( S != NO_MAPPING_NUM )
148 {
149 dist[S]=0;
150 sort_queue_by_distance(Q,dist,head,V);
151
152 while (head < V )
153 {
154 u=Q[head];
155 if(dist[u] == INF_DISTANCE)
156 {
157 break;
158 }
159
160 for(v=0 ; v <V; v++)
161 {
162 if( adj_matrix[u][v] > 0 ) //
163 {
164 if ( is_not_explored(Q,v,head+1,V) )
165 {
166
167 if( dist[u] + adj_matrix[u][v] < dist[v])
168 {
169 dist[v]=dist[u] + adj_matrix[u][v] ;
170 parent[v]=u;
171 }
172
173 }
174
175 }
176
177 }
178
179 head++;
180 sort_queue_by_distance(Q,dist,head,V);
181 }
182 }
183 free(Q);
184 //free(dist);
185}
186
187void
188print_all_path_from_source(long int *parent,long int source)
189{
190 int i, map_element;
191 struct map_entry *me;
192
193 struct hashtb_enumerator ee;
194 struct hashtb_enumerator *e = &ee;
195
196 hashtb_start(nlsr->map, e);
197 map_element=hashtb_n(nlsr->map);
198
199 if ( source != NO_MAPPING_NUM)
200 {
201 for(i=0;i<map_element;i++)
202 {
203 me=e->data;
204 if(me->mapping != source)
205 {
206
207 if ( nlsr->debugging )
208 {
209 print_path(parent,(long int)me->mapping);
210 printf("\n");
211 }
212 if ( nlsr->detailed_logging )
213 {
214 print_path(parent,(long int)me->mapping);
215 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
216 }
217
218 }
219 hashtb_next(e);
220 }
221 }
222 hashtb_end(e);
223
224}
225
226void
227print_all_next_hop(long int *parent,long int source)
228{
229 int i, map_element;
230 struct map_entry *me;
231
232 struct hashtb_enumerator ee;
233 struct hashtb_enumerator *e = &ee;
234
235 hashtb_start(nlsr->map, e);
236 map_element=hashtb_n(nlsr->map);
237
238 for(i=0;i<map_element;i++)
239 {
240 me=e->data;
241 if(me->mapping != source)
242 {
243 if ( nlsr->debugging )
244 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
245 if ( nlsr->detailed_logging )
246 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
247 }
248 hashtb_next(e);
249 }
250
251 hashtb_end(e);
252
253}
254
255long int
256get_next_hop_from_calculation(long int *parent, long int dest,long int source)
257{
258 long int next_hop;
259 while ( parent[dest] != EMPTY_PARENT )
260 {
261 next_hop=dest;
262 dest=parent[dest];
263
264 }
265
266 if ( dest != source )
267 {
268 next_hop=NO_NEXT_HOP;
269 }
270
271 return next_hop;
272
273}
274
275void
276print_path(long int *parent, long int dest)
277{
278 if (parent[dest] != EMPTY_PARENT )
279 print_path(parent,parent[dest]);
a64adam04be6482013-01-17 11:29:32 -0600280
281 if ( nlsr->debugging )
282 printf(" %ld",dest);
akmhoque8fdd6412012-12-04 15:05:33 -0600283}
284
285int
286is_not_explored(long int *Q, long int u,long int start, long int element)
287{
288 int ret=0;
289 long int i;
290 for(i=start; i< element; i++)
291 {
292 if ( Q[i] == u )
293 {
294 ret=1;
295 break;
296 }
297 }
298 return ret;
299}
300
301void
302sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
303{
304 long int i,j;
305 long int temp_u;
306
307 for ( i=start ; i < element ; i ++)
308 {
309 for( j=i+1; j<element; j ++)
310 {
311 if (dist[Q[j]] < dist[Q[i]])
312 {
313 temp_u=Q[j];
314 Q[j]=Q[i];
315 Q[i]=temp_u;
316 }
317 }
318 }
319
320}
321
322void
323print_map(void)
324{
325 int i, map_element;
326 struct map_entry *me;
327
328 struct hashtb_enumerator ee;
329 struct hashtb_enumerator *e = &ee;
330
331 hashtb_start(nlsr->map, e);
332 map_element=hashtb_n(nlsr->map);
333
334 for(i=0;i<map_element;i++)
335 {
336 me=e->data;
337 if ( nlsr->debugging )
338 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
339 if ( nlsr->detailed_logging )
340 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
341 hashtb_next(e);
342 }
343
344 hashtb_end(e);
345}
346
347
348void
349assign_mapping_number(void)
350{
351 int i, map_element;
352 struct map_entry *me;
353
354 struct hashtb_enumerator ee;
355 struct hashtb_enumerator *e = &ee;
356
357 hashtb_start(nlsr->map, e);
358 map_element=hashtb_n(nlsr->map);
359
360 for(i=0;i<map_element;i++)
361 {
362 me=e->data;
363 me->mapping=i;
364 add_rev_map_entry(i,me->router);
365 hashtb_next(e);
366 }
367
368 hashtb_end(e);
369}
370
371void
372make_map(void)
373{
374
375
376 int i, adj_lsdb_element;
377 struct alsa *adj_lsa;
378
379 struct hashtb_enumerator ee;
380 struct hashtb_enumerator *e = &ee;
381
382 hashtb_start(nlsr->lsdb->adj_lsdb, e);
383 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
384
385 add_map_entry(nlsr->router_name);
386
387 for(i=0;i<adj_lsdb_element;i++)
388 {
389 adj_lsa=e->data;
390 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
391 hashtb_next(e);
392 }
393
394 hashtb_end(e);
395
396}
397
398void
399add_map_entry(char *router)
400{
401
402 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
403
404 struct hashtb_enumerator ee;
405 struct hashtb_enumerator *e = &ee;
406 int res;
407
408 hashtb_start(nlsr->map, e);
409 res = hashtb_seek(e, router, strlen(router), 0);
410
411 if(res == HT_NEW_ENTRY)
412 {
413 me=e->data;
414 me->router=(char *)malloc(strlen(router)+1);
415 memset(me->router,0,strlen(router)+1);
416 memcpy(me->router,router,strlen(router));
417 me->mapping=0;
418 }
419
420 hashtb_end(e);
421
422}
423
424
425void
426add_adj_data_to_map(char *orig_router, char *body, int no_link)
427{
428 add_map_entry(orig_router);
429
430 int i=0;
431 char *lsa_data=(char *)malloc(strlen(body)+1);
432 memset( lsa_data,0,strlen(body)+1);
433 memcpy(lsa_data,body,strlen(body)+1);
434 char *sep="|";
435 char *rem;
436 char *rtr_id;
437 char *length;
438 char *face;
439 char *metric;
440
441 if(no_link >0 )
442 {
443 rtr_id=strtok_r(lsa_data,sep,&rem);
444 length=strtok_r(NULL,sep,&rem);
445 face=strtok_r(NULL,sep,&rem);
446 metric=strtok_r(NULL,sep,&rem);
447
448 add_map_entry(rtr_id);
449
450 for(i=1;i<no_link;i++)
451 {
452 rtr_id=strtok_r(NULL,sep,&rem);
453 length=strtok_r(NULL,sep,&rem);
454 face=strtok_r(NULL,sep,&rem);
455 metric=strtok_r(NULL,sep,&rem);
456
457 add_map_entry(rtr_id);
458
459 }
460 }
461
462 free(lsa_data);
463}
464
465int
466get_mapping_no(char *router)
467{
468 struct map_entry *me;
469
470 struct hashtb_enumerator ee;
471 struct hashtb_enumerator *e = &ee;
472 int res;
473 int ret;
474
475 int n = hashtb_n(nlsr->map);
476
477 if ( n < 1)
478 {
479 return NO_MAPPING_NUM;
480 }
481
482 hashtb_start(nlsr->map, e);
483 res = hashtb_seek(e, router, strlen(router), 0);
484
485 if( res == HT_OLD_ENTRY )
486 {
487 me=e->data;
488 ret=me->mapping;
489 }
490 else if(res == HT_NEW_ENTRY)
491 {
492 hashtb_delete(e);
493 ret=NO_MAPPING_NUM;
494 }
495
496 hashtb_end(e);
497
498 return ret;
499
500}
501
502
503void
504add_rev_map_entry(long int mapping_number, char *router)
505{
506
507 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
508
509 struct hashtb_enumerator ee;
510 struct hashtb_enumerator *e = &ee;
511 int res;
512
513 hashtb_start(nlsr->rev_map, e);
514 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
515
516 if(res == HT_NEW_ENTRY)
517 {
518 me=e->data;
519 me->router=(char *)malloc(strlen(router)+1);
520 memset(me->router,0,strlen(router)+1);
521 memcpy(me->router,router,strlen(router));
522 me->mapping=mapping_number;
523 }
524
525 hashtb_end(e);
526
527}
528
529
530
531char *
532get_router_from_rev_map(long int mapping_number)
533{
534
535 struct map_entry *me;
536
537 struct hashtb_enumerator ee;
538 struct hashtb_enumerator *e = &ee;
539 int res;
540
541 hashtb_start(nlsr->rev_map, e);
542 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
543
544 if(res == HT_OLD_ENTRY)
545 {
546 me=e->data;
547 hashtb_end(e);
548 return me->router;
549 }
550 else if(res == HT_NEW_ENTRY)
551 {
552 hashtb_delete(e);
553 hashtb_end(e);
554 }
555
556 return NULL;
557}
558
559void
560print_rev_map(void)
561{
562 int i, map_element;
563 struct map_entry *me;
564
565 struct hashtb_enumerator ee;
566 struct hashtb_enumerator *e = &ee;
567
568 hashtb_start(nlsr->map, e);
569 map_element=hashtb_n(nlsr->map);
570
571 for(i=0;i<map_element;i++)
572 {
573 me=e->data;
574 if ( nlsr->debugging )
575 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
576 if ( nlsr->detailed_logging )
577 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
578 hashtb_next(e);
579 }
580
581 hashtb_end(e);
582}
583
584
585void
586assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
587{
588 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
589 int mapping_nbr_router;
590
591 int i;
592 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
593 memset( lsa_data,0,strlen(adj_lsa->body)+1);
594 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
595 char *sep="|";
596 char *rem;
597 char *rtr_id;
598 char *length;
599 char *face;
600 char *metric;
601
602 if(adj_lsa->no_link >0 )
603 {
604 rtr_id=strtok_r(lsa_data,sep,&rem);
605 length=strtok_r(NULL,sep,&rem);
606 face=strtok_r(NULL,sep,&rem);
607 metric=strtok_r(NULL,sep,&rem);
608
609 mapping_nbr_router=get_mapping_no(rtr_id);
610 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
611
612 for(i=1;i<adj_lsa->no_link;i++)
613 {
614 rtr_id=strtok_r(NULL,sep,&rem);
615 length=strtok_r(NULL,sep,&rem);
616 face=strtok_r(NULL,sep,&rem);
617 metric=strtok_r(NULL,sep,&rem);
618
619 mapping_nbr_router=get_mapping_no(rtr_id);
620 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
621
622 }
623 }
624
625 free(lsa_data);
626}
627
628void
629make_adj_matrix(int **adj_matrix,int map_element)
630{
631
632 init_adj_matrix(adj_matrix,map_element);
633
634 int i, adj_lsdb_element;
635 struct alsa *adj_lsa;
636
637 struct hashtb_enumerator ee;
638 struct hashtb_enumerator *e = &ee;
639
640 hashtb_start(nlsr->lsdb->adj_lsdb, e);
641 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
642
643 for(i=0;i<adj_lsdb_element;i++)
644 {
645 adj_lsa=e->data;
646 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
647 hashtb_next(e);
648 }
649
650 hashtb_end(e);
651
652}
653
654void
655init_adj_matrix(int **adj_matrix,int map_element)
656{
657 int i, j;
658 for(i=0;i<map_element;i++)
659 for(j=0;j<map_element;j++)
660 adj_matrix[i][j]=0;
661}
662
663void print_adj_matrix(int **adj_matrix, int map_element)
664{
665 int i, j;
666 for(i=0;i<map_element;i++)
667 {
668 for(j=0;j<map_element;j++)
669 printf("%d ",adj_matrix[i][j]);
670 printf("\n");
671 }
672}
673
674
675int
676get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
677{
678 int no_link=0;
679 int i;
680
681 for(i=0;i<V;i++)
682 {
683 if ( adj_matrix[S][i] > 0 )
684 {
685 no_link++;
686 }
687 }
688 return no_link;
689}
690
691void
692get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
693{
694 int i,j;
695 j=0;
696 for (i=0; i <V; i++)
697 {
698 if ( adj_matrix[S][i] > 0 )
699 {
700 links[j]=i;
701 link_costs[j]=adj_matrix[S][i];
702 j++;
703 }
704 }
705}
706
707void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
708{
709 int i;
710 for ( i = 0; i < V; i++ )
711 {
712 if ( i == link )
713 {
714 adj_matrix[S][i]=link_cost;
715 }
716 else
717 {
718 adj_matrix[S][i]=0;
719 }
720 }
721
722}
723
724int
725get_number_of_next_hop(char *dest_router)
726{
727 struct routing_table_entry *rte;
728
729 struct hashtb_enumerator ee;
730 struct hashtb_enumerator *e = &ee;
731 int res,ret;
732
733 hashtb_start(nlsr->routing_table, e);
734 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
735
736 if( res == HT_OLD_ENTRY )
737 {
738 rte=e->data;
739 ret=hashtb_n(rte->face_list);
740 //nhl=rte->face_list;
741 }
742 else if(res == HT_NEW_ENTRY)
743 {
744 hashtb_delete(e);
745 ret=NO_NEXT_HOP;
746 }
747
748 hashtb_end(e);
749
750 return ret;
751}
752
753
754int
755get_next_hop(char *dest_router,int *faces, int *route_costs)
756{
757 struct routing_table_entry *rte;
758
759 struct hashtb_enumerator ee;
760 struct hashtb_enumerator *e = &ee;
761 int res,ret;
762
763 hashtb_start(nlsr->routing_table, e);
764 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
765
766 if( res == HT_OLD_ENTRY )
767 {
768 rte=e->data;
769 ret=hashtb_n(rte->face_list);
770 //nhl=rte->face_list;
771 int j,face_list_element;
772 struct face_list_entry *fle;
773
774 struct hashtb_enumerator eef;
775 struct hashtb_enumerator *ef = &eef;
776
777 hashtb_start(rte->face_list, ef);
778 face_list_element=hashtb_n(rte->face_list);
779 for(j=0;j<face_list_element;j++)
780 {
781 fle=ef->data;
782 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
783 faces[j]=fle->next_hop_face;
784 route_costs[j]=fle->route_cost;
785 hashtb_next(ef);
786 }
787 hashtb_end(ef);
788
789 }
790 else if(res == HT_NEW_ENTRY)
791 {
792 hashtb_delete(e);
793 ret=NO_NEXT_HOP;
794 }
795
796 hashtb_end(e);
797
798 return ret;
799}
800
801void
802add_next_hop_router(char *dest_router)
803{
804 if ( strcmp(dest_router,nlsr->router_name)== 0)
805 {
806 return ;
807 }
808
809 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
810
811 struct hashtb_enumerator ee;
812 struct hashtb_enumerator *e = &ee;
813 int res;
814
815 hashtb_start(nlsr->routing_table, e);
816 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
817
818 if( res == HT_NEW_ENTRY )
819 {
820 rte=e->data;
821 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
822 memset(rte->dest_router,0,strlen(dest_router)+1);
823 memcpy(rte->dest_router,dest_router,strlen(dest_router));
824 //rte->next_hop_face=NO_NEXT_HOP;
825 struct hashtb_param param_fle = {0};
826 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
827
828 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
829 }
830 hashtb_end(e);
831
832}
833
834void
835add_next_hop_from_lsa_adj_body(char *body, int no_link)
836{
837
838 int i=0;
839 char *lsa_data=(char *)malloc(strlen(body)+1);
840 memset( lsa_data,0,strlen(body)+1);
841 memcpy(lsa_data,body,strlen(body)+1);
842 char *sep="|";
843 char *rem;
844 char *rtr_id;
845 char *length;
846 char *face;
847 char *metric;
848
849 if(no_link >0 )
850 {
851 rtr_id=strtok_r(lsa_data,sep,&rem);
852 length=strtok_r(NULL,sep,&rem);
853 face=strtok_r(NULL,sep,&rem);
854 metric=strtok_r(NULL,sep,&rem);
855
856
857 add_next_hop_router(rtr_id);
858
859 for(i=1;i<no_link;i++)
860 {
861 rtr_id=strtok_r(NULL,sep,&rem);
862 length=strtok_r(NULL,sep,&rem);
863 face=strtok_r(NULL,sep,&rem);
864 metric=strtok_r(NULL,sep,&rem);
865
866 add_next_hop_router(rtr_id);
867
868 }
869 }
870
871 free(lsa_data);
872
873
874}
875
876void
877update_routing_table(char * dest_router,int next_hop_face, int route_cost)
878{
879 int res,res1;
880 struct routing_table_entry *rte;
881
882 struct hashtb_enumerator ee;
883 struct hashtb_enumerator *e = &ee;
884
885 hashtb_start(nlsr->routing_table, e);
886 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
887
888 if( res == HT_OLD_ENTRY )
889 {
890 rte=e->data;
891
892 struct hashtb_enumerator eef;
893 struct hashtb_enumerator *ef = &eef;
894
895 hashtb_start(rte->face_list, ef);
896 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
897 if( res1 == HT_NEW_ENTRY)
898 {
899 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
900 fle=ef->data;
901 fle->next_hop_face=next_hop_face;
902 fle->route_cost=route_cost;
903 }
904 else if ( res1 == HT_OLD_ENTRY )
905 {
906 struct face_list_entry *fle;
907 fle=ef->data;
908 fle->route_cost=route_cost;
909 }
910 hashtb_end(ef);
911
912 /*
913 //updating the face for the router prefix itself
914 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
915 {
916 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
917 }
918 if ( (next_hop_face != NO_FACE || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
919 {
920 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
921 }
922
923 rte->next_hop_face=next_hop_face;
924 */
925 }
926 else if ( res == HT_OLD_ENTRY )
927 {
928 hashtb_delete(e);
929 }
930
931 hashtb_end(e);
932
933}
934
935void
936print_routing_table(void)
937{
938 if ( nlsr->debugging )
939 printf("print_routing_table called\n");
940 if ( nlsr->detailed_logging )
941 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
942 int i,j, rt_element,face_list_element;
943
944 struct routing_table_entry *rte;
945
946 struct hashtb_enumerator ee;
947 struct hashtb_enumerator *e = &ee;
948
949 hashtb_start(nlsr->routing_table, e);
950 rt_element=hashtb_n(nlsr->routing_table);
951
952 for(i=0;i<rt_element;i++)
953 {
954 if ( nlsr->debugging )
955 printf("----------Routing Table Entry %d------------------\n",i+1);
956 if ( nlsr->detailed_logging )
957 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
958
959 rte=e->data;
960
961 if ( nlsr->debugging )
962 printf(" Destination Router: %s \n",rte->dest_router);
963 if ( nlsr->detailed_logging )
964 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
965
966
967 //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);
968
969 struct face_list_entry *fle;
970
971 struct hashtb_enumerator eef;
972 struct hashtb_enumerator *ef = &eef;
973
974 hashtb_start(rte->face_list, ef);
975 face_list_element=hashtb_n(rte->face_list);
976 if ( face_list_element <= 0 )
977 {
978 if ( nlsr->debugging )
979 printf(" Face: No Face \n");
980 if ( nlsr->detailed_logging )
981 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
982 }
983 else
984 {
985 for(j=0;j<face_list_element;j++)
986 {
987 fle=ef->data;
988 if ( nlsr->debugging )
989 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
990 if ( nlsr->detailed_logging )
991 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
992 hashtb_next(ef);
993 }
994 }
995 hashtb_end(ef);
996
997 hashtb_next(e);
998 }
999
1000 hashtb_end(e);
1001
1002 if ( nlsr->debugging )
1003 printf("\n");
1004 if ( nlsr->detailed_logging )
1005 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1006}
1007
1008
1009int
1010delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1011{
1012 if ( nlsr->debugging )
1013 {
1014 printf("delete_empty_rte called\n");
1015 printf("Router: %s \n",(char *)ev->evdata);
1016 }
1017 if ( nlsr->detailed_logging )
1018 {
1019 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1020 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
1021 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
1022 }
1023
1024 if(flags == CCN_SCHEDULE_CANCEL)
1025 {
1026 return -1;
1027 }
1028 int res;
1029 struct hashtb_enumerator ee;
1030 struct hashtb_enumerator *e = &ee;
1031
1032 hashtb_start(nlsr->routing_table, e);
1033 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1034
1035 if ( res == HT_OLD_ENTRY )
1036 {
1037 hashtb_delete(e);
1038 }
1039 else if ( res == HT_NEW_ENTRY )
1040 {
1041 hashtb_delete(e);
1042 }
1043
1044 print_routing_table();
1045
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);
1145 //printf(" Next hop router Num: %d ",next_hop_router_num);
1146 if ( next_hop_router_num == NO_NEXT_HOP )
1147 {
1148 //update_npt_with_new_route(orig_router,NO_FACE);
1149 if ( nlsr->debugging )
1150 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1151 if ( nlsr->detailed_logging )
1152 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1153 }
1154 else
1155 {
1156 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
1157 //printf("Next hop router name: %s \n",next_hop_router);
1158 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
1159 //update_npt_with_new_route(orig_router,next_hop_face);
1160 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
1161 if ( nlsr->debugging )
1162 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1163 if ( nlsr->detailed_logging )
1164 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1165
1166
1167 }
1168 }
1169 }
1170 hashtb_next(e);
1171 }
1172
1173 hashtb_end(e);
1174}
1175
1176int
1177does_face_exist_for_router(char *dest_router, int face_id)
1178{
1179 int ret=0;
1180
1181 int res,res1;
1182 struct routing_table_entry *rte;
1183
1184 struct hashtb_enumerator ee;
1185 struct hashtb_enumerator *e = &ee;
1186
1187 hashtb_start(nlsr->routing_table, e);
1188 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1189
1190 if( res == HT_OLD_ENTRY )
1191 {
1192 rte=e->data;
1193 struct hashtb_enumerator eef;
1194 struct hashtb_enumerator *ef = &eef;
1195
1196 hashtb_start(rte->face_list, ef);
1197 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1198 if( res1 == HT_OLD_ENTRY)
1199 {
1200 ret=1;
1201 }
1202 else if ( res1 == HT_OLD_ENTRY )
1203 {
1204 hashtb_delete(ef);
1205 }
1206 hashtb_end(ef);
1207 }
1208 else if( res == HT_NEW_ENTRY )
1209 {
1210 hashtb_delete(e);
1211 }
1212
1213 hashtb_end(e);
1214
1215 return ret;
1216}