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