blob: 08a574e514081754bbb41d880ba45178c77df1af [file] [log] [blame]
akmhoquef06586e2013-02-05 12:09:38 -06001#include <stdio.h>
2#include <string.h>
3#include <math.h>
4#include <stdlib.h>
akmhoque8fdd6412012-12-04 15:05:33 -06005#include <unistd.h>
6#include <getopt.h>
7#include <sys/time.h>
8#include <assert.h>
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12#include <sys/types.h>
13
14
15#include <ccn/ccn.h>
16#include <ccn/uri.h>
17#include <ccn/keystore.h>
18#include <ccn/signing.h>
19#include <ccn/schedule.h>
20#include <ccn/hashtb.h>
21
22
23#include "nlsr.h"
24#include "nlsr_route.h"
25#include "nlsr_lsdb.h"
26#include "nlsr_npt.h"
27#include "nlsr_adl.h"
28#include "nlsr_fib.h"
29#include "utility.h"
30
31int
32route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
33{
34
35 if(flags == CCN_SCHEDULE_CANCEL)
36 {
37 return -1;
38 }
39
40 nlsr_lock();
41
42 if ( nlsr->debugging )
43 printf("route_calculate called\n");
44 if ( nlsr->detailed_logging )
45 writeLogg(__FILE__,__FUNCTION__,__LINE__,"route_calculate called\n");
46
47 if( ! nlsr->is_build_adj_lsa_sheduled )
48 {
49 /* Calculate Route here */
50 print_routing_table();
51 print_npt();
52
53 struct hashtb_param param_me = {0};
54 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
55 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
56 make_map();
57 assign_mapping_number();
58 print_map();
59 print_rev_map();
60
61 do_old_routing_table_updates();
62 clear_old_routing_table();
63 print_routing_table();
64 print_npt();
65
66 int i;
67 int **adj_matrix;
68 int map_element=hashtb_n(nlsr->map);
69 adj_matrix=malloc(map_element * sizeof(int *));
70 for(i = 0; i < map_element; i++)
71 {
72 adj_matrix[i] = malloc(map_element * sizeof(int));
73 }
74 make_adj_matrix(adj_matrix,map_element);
75 if ( nlsr->debugging )
76 print_adj_matrix(adj_matrix,map_element);
77
78 long int source=get_mapping_no(nlsr->router_name);
akmhoque8fdd6412012-12-04 15:05:33 -060079 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoquef06586e2013-02-05 12:09:38 -060080
81 if ( nlsr->is_hyperbolic_calc == 1)
akmhoque8fdd6412012-12-04 15:05:33 -060082 {
83 long int *links=(long int *)malloc(num_link*sizeof(long int));
84 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
85 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
akmhoquef06586e2013-02-05 12:09:38 -060086
87 struct hashtb_enumerator ee;
88 struct hashtb_enumerator *e = &ee;
89 for (hashtb_start(nlsr->rev_map, e); e->key != NULL; hashtb_next(e))
akmhoque8fdd6412012-12-04 15:05:33 -060090 {
akmhoquef06586e2013-02-05 12:09:38 -060091 struct map_entry *me=e->data;
92 if ( me->mapping != source )
93 {
94 long int *faces=(long int *)calloc(num_link,sizeof(long int));
95 double *nbr_dist=(double *)calloc(num_link,sizeof(double));
96 double *nbr_to_dest=(double *)calloc(num_link,sizeof(double));
97 for ( i=0 ; i < num_link; i++)
98 {
99 int face=get_next_hop_face_from_adl(get_router_from_rev_map(links[i]));
100 double dist_to_nbr=get_hyperbolic_distance(source,links[i]);
101 double dist_to_dest_from_nbr=get_hyperbolic_distance(links[i],me->mapping);
102 faces[i]=face;
103 nbr_dist[i]=dist_to_nbr;
104 nbr_to_dest[i]= dist_to_dest_from_nbr;
105
106
107 }
108 sort_hyperbolic_route(nbr_to_dest,nbr_dist, faces,0,num_link);
109 if (nlsr->multi_path_face_num <= 1 )
110 {
111 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[0],nbr_to_dest[0]);
112 }
113 else
114 {
115 if ( num_link <= nlsr->multi_path_face_num )
116 {
117 for ( i=0 ; i < num_link; i++)
118 {
119 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
120 }
121 }
122 else if (num_link > nlsr->multi_path_face_num)
123 {
124 for ( i=0 ; i < nlsr->multi_path_face_num; i++)
125 {
126 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
127 }
128 }
129
130 }
131 free(faces);
132 free(nbr_dist);
133 free(nbr_to_dest);
134 }
135 }
136 hashtb_end(e);
137
138
139 free(links);
140 free(link_costs);
141 }
142 else if (nlsr->is_hyperbolic_calc == 0 )
143 {
144
145 long int *parent=(long int *)malloc(map_element * sizeof(long int));
146 long int *dist=(long int *)malloc(map_element * sizeof(long int));
147
148
149 if ( (num_link == 0) || (nlsr->multi_path_face_num == 1 ) )
150 {
akmhoque8fdd6412012-12-04 15:05:33 -0600151 calculate_path(adj_matrix,parent,dist, map_element, source);
152 print_all_path_from_source(parent,source);
153 print_all_next_hop(parent,source);
154 update_routing_table_with_new_route(parent, dist,source);
155 }
akmhoquef06586e2013-02-05 12:09:38 -0600156 else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
157 {
158 long int *links=(long int *)malloc(num_link*sizeof(long int));
159 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
160 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
161 for ( i=0 ; i < num_link; i++)
162 {
163 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
164 calculate_path(adj_matrix,parent,dist, map_element, source);
165 print_all_path_from_source(parent,source);
166 print_all_next_hop(parent,source);
167 update_routing_table_with_new_route(parent, dist,source);
168 }
akmhoque8fdd6412012-12-04 15:05:33 -0600169
akmhoquef06586e2013-02-05 12:09:38 -0600170 free(links);
171 free(link_costs);
172 }
173 free(parent);
174 free(dist);
akmhoque8fdd6412012-12-04 15:05:33 -0600175 }
akmhoquef06586e2013-02-05 12:09:38 -0600176
akmhoque8fdd6412012-12-04 15:05:33 -0600177 print_routing_table();
178 print_npt();
179
akmhoque580ccf32013-01-23 09:12:33 -0600180 update_npt_with_new_route();
181
182 print_routing_table();
183 print_npt();
akmhoqueeda9c362013-01-23 08:54:29 -0600184
185
akmhoque8fdd6412012-12-04 15:05:33 -0600186 for(i = 0; i < map_element; i++)
187 {
188 free(adj_matrix[i]);
189 }
akmhoquef06586e2013-02-05 12:09:38 -0600190
akmhoque8fdd6412012-12-04 15:05:33 -0600191 free(adj_matrix);
192 hashtb_destroy(&nlsr->map);
193 hashtb_destroy(&nlsr->rev_map);
194
195 }
196 nlsr->is_route_calculation_scheduled=0;
197
198 nlsr_unlock();
199
200 return 0;
201}
202
203void
204calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
205{
206 int i;
207 long int v,u;
208 //long int *dist=(long int *)malloc(V * sizeof(long int));
209 long int *Q=(long int *)malloc(V * sizeof(long int));
210 long int head=0;
akmhoquef06586e2013-02-05 12:09:38 -0600211 /* Initiate the Parent */
akmhoque8fdd6412012-12-04 15:05:33 -0600212 for (i = 0 ; i < V; i++)
213 {
214 parent[i]=EMPTY_PARENT;
215 dist[i]=INF_DISTANCE;
216 Q[i]=i;
217 }
218
219 if ( S != NO_MAPPING_NUM )
220 {
221 dist[S]=0;
222 sort_queue_by_distance(Q,dist,head,V);
223
224 while (head < V )
225 {
226 u=Q[head];
227 if(dist[u] == INF_DISTANCE)
228 {
229 break;
230 }
231
232 for(v=0 ; v <V; v++)
233 {
234 if( adj_matrix[u][v] > 0 ) //
235 {
236 if ( is_not_explored(Q,v,head+1,V) )
237 {
238
239 if( dist[u] + adj_matrix[u][v] < dist[v])
240 {
241 dist[v]=dist[u] + adj_matrix[u][v] ;
242 parent[v]=u;
243 }
244
245 }
246
247 }
248
249 }
250
251 head++;
252 sort_queue_by_distance(Q,dist,head,V);
253 }
254 }
akmhoque596f7082013-02-04 13:34:13 -0600255 free(Q);
akmhoque8fdd6412012-12-04 15:05:33 -0600256}
257
258void
259print_all_path_from_source(long int *parent,long int source)
260{
261 int i, map_element;
262 struct map_entry *me;
263
264 struct hashtb_enumerator ee;
265 struct hashtb_enumerator *e = &ee;
266
267 hashtb_start(nlsr->map, e);
268 map_element=hashtb_n(nlsr->map);
269
270 if ( source != NO_MAPPING_NUM)
271 {
272 for(i=0;i<map_element;i++)
273 {
274 me=e->data;
275 if(me->mapping != source)
276 {
277
278 if ( nlsr->debugging )
279 {
280 print_path(parent,(long int)me->mapping);
281 printf("\n");
282 }
283 if ( nlsr->detailed_logging )
284 {
285 print_path(parent,(long int)me->mapping);
286 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
287 }
288
289 }
290 hashtb_next(e);
291 }
292 }
293 hashtb_end(e);
294
295}
296
297void
298print_all_next_hop(long int *parent,long int source)
299{
300 int i, map_element;
301 struct map_entry *me;
302
303 struct hashtb_enumerator ee;
304 struct hashtb_enumerator *e = &ee;
305
306 hashtb_start(nlsr->map, e);
307 map_element=hashtb_n(nlsr->map);
308
309 for(i=0;i<map_element;i++)
310 {
311 me=e->data;
312 if(me->mapping != source)
313 {
314 if ( nlsr->debugging )
315 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
316 if ( nlsr->detailed_logging )
317 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
318 }
319 hashtb_next(e);
320 }
321
322 hashtb_end(e);
323
324}
325
326long int
327get_next_hop_from_calculation(long int *parent, long int dest,long int source)
328{
329 long int next_hop;
330 while ( parent[dest] != EMPTY_PARENT )
331 {
332 next_hop=dest;
333 dest=parent[dest];
334
335 }
336
337 if ( dest != source )
338 {
339 next_hop=NO_NEXT_HOP;
340 }
341
342 return next_hop;
343
344}
345
346void
347print_path(long int *parent, long int dest)
348{
349 if (parent[dest] != EMPTY_PARENT )
350 print_path(parent,parent[dest]);
a64adam04be6482013-01-17 11:29:32 -0600351
352 if ( nlsr->debugging )
353 printf(" %ld",dest);
akmhoque8fdd6412012-12-04 15:05:33 -0600354}
355
356int
357is_not_explored(long int *Q, long int u,long int start, long int element)
358{
359 int ret=0;
360 long int i;
361 for(i=start; i< element; i++)
362 {
363 if ( Q[i] == u )
364 {
365 ret=1;
366 break;
367 }
368 }
369 return ret;
370}
371
372void
373sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
374{
375 long int i,j;
376 long int temp_u;
377
378 for ( i=start ; i < element ; i ++)
379 {
380 for( j=i+1; j<element; j ++)
381 {
382 if (dist[Q[j]] < dist[Q[i]])
383 {
384 temp_u=Q[j];
385 Q[j]=Q[i];
386 Q[i]=temp_u;
387 }
388 }
389 }
390
391}
392
akmhoquef06586e2013-02-05 12:09:38 -0600393void
394sort_hyperbolic_route(double *dist_dest,double *dist_nbr, long int *faces,long int start,long int element)
395{
396 long int i,j;
397 double temp_dist;
398 long int temp_face;
399
400 for ( i=start ; i < element ; i ++)
401 {
402 for( j=i+1; j<element; j ++)
403 {
404 if (dist_dest[i] < dist_dest[j])
405 {
406 temp_dist=dist_dest[j];
407 dist_dest[j]=dist_dest[i];
408 dist_dest[i]=temp_dist;
409
410 temp_dist=dist_nbr[j];
411 dist_nbr[j]=dist_nbr[i];
412 dist_nbr[i]=temp_dist;
413
414 temp_face=faces[j];
415 faces[j]=faces[i];
416 faces[i]=temp_face;
417 }
418 if ( (dist_dest[i] == dist_dest[j]) && (dist_nbr[i] < dist_nbr[j]) )
419 {
420 temp_dist=dist_dest[j];
421 dist_dest[j]=dist_dest[i];
422 dist_dest[i]=temp_dist;
423
424 temp_dist=dist_nbr[j];
425 dist_nbr[j]=dist_nbr[i];
426 dist_nbr[i]=temp_dist;
427
428 temp_face=faces[j];
429 faces[j]=faces[i];
430 faces[i]=temp_face;
431 }
432 }
433 }
434
435}
436
akmhoque8fdd6412012-12-04 15:05:33 -0600437void
438print_map(void)
439{
440 int i, map_element;
441 struct map_entry *me;
442
443 struct hashtb_enumerator ee;
444 struct hashtb_enumerator *e = &ee;
445
446 hashtb_start(nlsr->map, e);
447 map_element=hashtb_n(nlsr->map);
448
449 for(i=0;i<map_element;i++)
450 {
451 me=e->data;
452 if ( nlsr->debugging )
453 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
454 if ( nlsr->detailed_logging )
455 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
456 hashtb_next(e);
457 }
458
459 hashtb_end(e);
460}
461
462
463void
464assign_mapping_number(void)
465{
466 int i, map_element;
467 struct map_entry *me;
468
469 struct hashtb_enumerator ee;
470 struct hashtb_enumerator *e = &ee;
471
472 hashtb_start(nlsr->map, e);
473 map_element=hashtb_n(nlsr->map);
474
475 for(i=0;i<map_element;i++)
476 {
477 me=e->data;
478 me->mapping=i;
479 add_rev_map_entry(i,me->router);
480 hashtb_next(e);
481 }
482
483 hashtb_end(e);
484}
485
486void
487make_map(void)
488{
489
490
491 int i, adj_lsdb_element;
492 struct alsa *adj_lsa;
493
494 struct hashtb_enumerator ee;
495 struct hashtb_enumerator *e = &ee;
496
497 hashtb_start(nlsr->lsdb->adj_lsdb, e);
498 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
499
500 add_map_entry(nlsr->router_name);
501
502 for(i=0;i<adj_lsdb_element;i++)
503 {
504 adj_lsa=e->data;
505 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
506 hashtb_next(e);
507 }
508
509 hashtb_end(e);
510
511}
512
513void
514add_map_entry(char *router)
515{
516
517 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
518
519 struct hashtb_enumerator ee;
520 struct hashtb_enumerator *e = &ee;
521 int res;
522
523 hashtb_start(nlsr->map, e);
524 res = hashtb_seek(e, router, strlen(router), 0);
525
526 if(res == HT_NEW_ENTRY)
527 {
528 me=e->data;
529 me->router=(char *)malloc(strlen(router)+1);
530 memset(me->router,0,strlen(router)+1);
531 memcpy(me->router,router,strlen(router));
532 me->mapping=0;
533 }
534
535 hashtb_end(e);
536
537}
538
539
540void
541add_adj_data_to_map(char *orig_router, char *body, int no_link)
542{
543 add_map_entry(orig_router);
544
545 int i=0;
546 char *lsa_data=(char *)malloc(strlen(body)+1);
547 memset( lsa_data,0,strlen(body)+1);
548 memcpy(lsa_data,body,strlen(body)+1);
549 char *sep="|";
550 char *rem;
551 char *rtr_id;
552 char *length;
553 char *face;
554 char *metric;
555
556 if(no_link >0 )
557 {
558 rtr_id=strtok_r(lsa_data,sep,&rem);
559 length=strtok_r(NULL,sep,&rem);
560 face=strtok_r(NULL,sep,&rem);
561 metric=strtok_r(NULL,sep,&rem);
562
563 add_map_entry(rtr_id);
564
565 for(i=1;i<no_link;i++)
566 {
567 rtr_id=strtok_r(NULL,sep,&rem);
568 length=strtok_r(NULL,sep,&rem);
569 face=strtok_r(NULL,sep,&rem);
570 metric=strtok_r(NULL,sep,&rem);
571
572 add_map_entry(rtr_id);
573
574 }
575 }
576
577 free(lsa_data);
578}
579
580int
581get_mapping_no(char *router)
582{
583 struct map_entry *me;
584
585 struct hashtb_enumerator ee;
586 struct hashtb_enumerator *e = &ee;
587 int res;
588 int ret;
589
590 int n = hashtb_n(nlsr->map);
591
592 if ( n < 1)
593 {
594 return NO_MAPPING_NUM;
595 }
596
597 hashtb_start(nlsr->map, e);
598 res = hashtb_seek(e, router, strlen(router), 0);
599
600 if( res == HT_OLD_ENTRY )
601 {
602 me=e->data;
603 ret=me->mapping;
604 }
605 else if(res == HT_NEW_ENTRY)
606 {
607 hashtb_delete(e);
608 ret=NO_MAPPING_NUM;
609 }
610
611 hashtb_end(e);
612
613 return ret;
614
615}
616
617
618void
619add_rev_map_entry(long int mapping_number, char *router)
620{
621
622 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
623
624 struct hashtb_enumerator ee;
625 struct hashtb_enumerator *e = &ee;
626 int res;
627
628 hashtb_start(nlsr->rev_map, e);
629 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
630
631 if(res == HT_NEW_ENTRY)
632 {
633 me=e->data;
634 me->router=(char *)malloc(strlen(router)+1);
635 memset(me->router,0,strlen(router)+1);
636 memcpy(me->router,router,strlen(router));
637 me->mapping=mapping_number;
638 }
639
640 hashtb_end(e);
641
642}
643
644
645
646char *
647get_router_from_rev_map(long int mapping_number)
648{
649
650 struct map_entry *me;
651
652 struct hashtb_enumerator ee;
653 struct hashtb_enumerator *e = &ee;
654 int res;
655
656 hashtb_start(nlsr->rev_map, e);
657 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
658
659 if(res == HT_OLD_ENTRY)
660 {
661 me=e->data;
662 hashtb_end(e);
663 return me->router;
664 }
665 else if(res == HT_NEW_ENTRY)
666 {
667 hashtb_delete(e);
668 hashtb_end(e);
669 }
670
671 return NULL;
672}
673
674void
675print_rev_map(void)
676{
677 int i, map_element;
678 struct map_entry *me;
679
680 struct hashtb_enumerator ee;
681 struct hashtb_enumerator *e = &ee;
682
683 hashtb_start(nlsr->map, e);
684 map_element=hashtb_n(nlsr->map);
685
686 for(i=0;i<map_element;i++)
687 {
688 me=e->data;
689 if ( nlsr->debugging )
690 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
691 if ( nlsr->detailed_logging )
692 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
693 hashtb_next(e);
694 }
695
696 hashtb_end(e);
697}
698
699
700void
701assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
702{
703 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
704 int mapping_nbr_router;
705
706 int i;
707 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
708 memset( lsa_data,0,strlen(adj_lsa->body)+1);
709 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
710 char *sep="|";
711 char *rem;
712 char *rtr_id;
713 char *length;
714 char *face;
715 char *metric;
716
717 if(adj_lsa->no_link >0 )
718 {
719 rtr_id=strtok_r(lsa_data,sep,&rem);
720 length=strtok_r(NULL,sep,&rem);
721 face=strtok_r(NULL,sep,&rem);
722 metric=strtok_r(NULL,sep,&rem);
723
724 mapping_nbr_router=get_mapping_no(rtr_id);
725 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
726
727 for(i=1;i<adj_lsa->no_link;i++)
728 {
729 rtr_id=strtok_r(NULL,sep,&rem);
730 length=strtok_r(NULL,sep,&rem);
731 face=strtok_r(NULL,sep,&rem);
732 metric=strtok_r(NULL,sep,&rem);
733
734 mapping_nbr_router=get_mapping_no(rtr_id);
735 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
736
737 }
738 }
739
740 free(lsa_data);
741}
742
743void
744make_adj_matrix(int **adj_matrix,int map_element)
745{
746
747 init_adj_matrix(adj_matrix,map_element);
748
749 int i, adj_lsdb_element;
750 struct alsa *adj_lsa;
751
752 struct hashtb_enumerator ee;
753 struct hashtb_enumerator *e = &ee;
754
755 hashtb_start(nlsr->lsdb->adj_lsdb, e);
756 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
757
758 for(i=0;i<adj_lsdb_element;i++)
759 {
760 adj_lsa=e->data;
761 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
762 hashtb_next(e);
763 }
764
765 hashtb_end(e);
766
767}
768
769void
770init_adj_matrix(int **adj_matrix,int map_element)
771{
772 int i, j;
773 for(i=0;i<map_element;i++)
774 for(j=0;j<map_element;j++)
775 adj_matrix[i][j]=0;
776}
777
778void print_adj_matrix(int **adj_matrix, int map_element)
779{
780 int i, j;
781 for(i=0;i<map_element;i++)
782 {
783 for(j=0;j<map_element;j++)
784 printf("%d ",adj_matrix[i][j]);
785 printf("\n");
786 }
787}
788
789
790int
791get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
792{
793 int no_link=0;
794 int i;
795
796 for(i=0;i<V;i++)
797 {
798 if ( adj_matrix[S][i] > 0 )
799 {
800 no_link++;
801 }
802 }
803 return no_link;
804}
805
806void
807get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
808{
809 int i,j;
810 j=0;
811 for (i=0; i <V; i++)
812 {
813 if ( adj_matrix[S][i] > 0 )
814 {
815 links[j]=i;
816 link_costs[j]=adj_matrix[S][i];
817 j++;
818 }
819 }
820}
821
822void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
823{
824 int i;
825 for ( i = 0; i < V; i++ )
826 {
827 if ( i == link )
828 {
829 adj_matrix[S][i]=link_cost;
830 }
831 else
832 {
833 adj_matrix[S][i]=0;
834 }
835 }
836
837}
838
839int
840get_number_of_next_hop(char *dest_router)
841{
842 struct routing_table_entry *rte;
843
844 struct hashtb_enumerator ee;
845 struct hashtb_enumerator *e = &ee;
846 int res,ret;
847
848 hashtb_start(nlsr->routing_table, e);
849 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
850
851 if( res == HT_OLD_ENTRY )
852 {
853 rte=e->data;
854 ret=hashtb_n(rte->face_list);
855 //nhl=rte->face_list;
856 }
857 else if(res == HT_NEW_ENTRY)
858 {
859 hashtb_delete(e);
860 ret=NO_NEXT_HOP;
861 }
862
863 hashtb_end(e);
864
865 return ret;
866}
867
868
869int
870get_next_hop(char *dest_router,int *faces, int *route_costs)
871{
872 struct routing_table_entry *rte;
873
874 struct hashtb_enumerator ee;
875 struct hashtb_enumerator *e = &ee;
876 int res,ret;
877
878 hashtb_start(nlsr->routing_table, e);
879 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
880
881 if( res == HT_OLD_ENTRY )
882 {
883 rte=e->data;
884 ret=hashtb_n(rte->face_list);
885 //nhl=rte->face_list;
886 int j,face_list_element;
887 struct face_list_entry *fle;
888
889 struct hashtb_enumerator eef;
890 struct hashtb_enumerator *ef = &eef;
891
892 hashtb_start(rte->face_list, ef);
893 face_list_element=hashtb_n(rte->face_list);
894 for(j=0;j<face_list_element;j++)
895 {
896 fle=ef->data;
897 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
898 faces[j]=fle->next_hop_face;
899 route_costs[j]=fle->route_cost;
900 hashtb_next(ef);
901 }
902 hashtb_end(ef);
903
904 }
905 else if(res == HT_NEW_ENTRY)
906 {
907 hashtb_delete(e);
908 ret=NO_NEXT_HOP;
909 }
910
911 hashtb_end(e);
912
913 return ret;
914}
915
916void
917add_next_hop_router(char *dest_router)
918{
919 if ( strcmp(dest_router,nlsr->router_name)== 0)
920 {
921 return ;
922 }
923
924 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
925
926 struct hashtb_enumerator ee;
927 struct hashtb_enumerator *e = &ee;
928 int res;
929
930 hashtb_start(nlsr->routing_table, e);
931 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
932
933 if( res == HT_NEW_ENTRY )
934 {
935 rte=e->data;
936 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
937 memset(rte->dest_router,0,strlen(dest_router)+1);
938 memcpy(rte->dest_router,dest_router,strlen(dest_router));
939 //rte->next_hop_face=NO_NEXT_HOP;
940 struct hashtb_param param_fle = {0};
941 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
942
943 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
944 }
945 hashtb_end(e);
946
947}
948
949void
950add_next_hop_from_lsa_adj_body(char *body, int no_link)
951{
952
953 int i=0;
954 char *lsa_data=(char *)malloc(strlen(body)+1);
955 memset( lsa_data,0,strlen(body)+1);
956 memcpy(lsa_data,body,strlen(body)+1);
957 char *sep="|";
958 char *rem;
959 char *rtr_id;
960 char *length;
961 char *face;
962 char *metric;
963
964 if(no_link >0 )
965 {
966 rtr_id=strtok_r(lsa_data,sep,&rem);
967 length=strtok_r(NULL,sep,&rem);
968 face=strtok_r(NULL,sep,&rem);
969 metric=strtok_r(NULL,sep,&rem);
970
971
972 add_next_hop_router(rtr_id);
973
974 for(i=1;i<no_link;i++)
975 {
976 rtr_id=strtok_r(NULL,sep,&rem);
977 length=strtok_r(NULL,sep,&rem);
978 face=strtok_r(NULL,sep,&rem);
979 metric=strtok_r(NULL,sep,&rem);
980
981 add_next_hop_router(rtr_id);
982
983 }
984 }
985
986 free(lsa_data);
987
988
989}
990
991void
992update_routing_table(char * dest_router,int next_hop_face, int route_cost)
993{
akmhoque4f4b0212013-01-22 11:54:47 -0600994 if ( nlsr->debugging )
995 {
996 printf("update_routing_table called \n");
997 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
998 }
999
akmhoque8fdd6412012-12-04 15:05:33 -06001000 int res,res1;
1001 struct routing_table_entry *rte;
1002
1003 struct hashtb_enumerator ee;
1004 struct hashtb_enumerator *e = &ee;
1005
1006 hashtb_start(nlsr->routing_table, e);
1007 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1008
1009 if( res == HT_OLD_ENTRY )
1010 {
1011 rte=e->data;
1012
1013 struct hashtb_enumerator eef;
1014 struct hashtb_enumerator *ef = &eef;
1015
1016 hashtb_start(rte->face_list, ef);
1017 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
1018 if( res1 == HT_NEW_ENTRY)
1019 {
1020 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
1021 fle=ef->data;
1022 fle->next_hop_face=next_hop_face;
1023 fle->route_cost=route_cost;
1024 }
1025 else if ( res1 == HT_OLD_ENTRY )
1026 {
1027 struct face_list_entry *fle;
1028 fle=ef->data;
1029 fle->route_cost=route_cost;
1030 }
1031 hashtb_end(ef);
akmhoque8fdd6412012-12-04 15:05:33 -06001032 }
1033 else if ( res == HT_OLD_ENTRY )
1034 {
1035 hashtb_delete(e);
1036 }
1037
1038 hashtb_end(e);
1039
1040}
1041
1042void
1043print_routing_table(void)
1044{
1045 if ( nlsr->debugging )
1046 printf("print_routing_table called\n");
1047 if ( nlsr->detailed_logging )
1048 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
1049 int i,j, rt_element,face_list_element;
1050
1051 struct routing_table_entry *rte;
1052
1053 struct hashtb_enumerator ee;
1054 struct hashtb_enumerator *e = &ee;
1055
1056 hashtb_start(nlsr->routing_table, e);
1057 rt_element=hashtb_n(nlsr->routing_table);
1058
1059 for(i=0;i<rt_element;i++)
1060 {
1061 if ( nlsr->debugging )
1062 printf("----------Routing Table Entry %d------------------\n",i+1);
1063 if ( nlsr->detailed_logging )
1064 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
1065
1066 rte=e->data;
1067
1068 if ( nlsr->debugging )
1069 printf(" Destination Router: %s \n",rte->dest_router);
1070 if ( nlsr->detailed_logging )
1071 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
1072
1073
1074 //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);
1075
1076 struct face_list_entry *fle;
1077
1078 struct hashtb_enumerator eef;
1079 struct hashtb_enumerator *ef = &eef;
1080
1081 hashtb_start(rte->face_list, ef);
1082 face_list_element=hashtb_n(rte->face_list);
1083 if ( face_list_element <= 0 )
1084 {
1085 if ( nlsr->debugging )
1086 printf(" Face: No Face \n");
1087 if ( nlsr->detailed_logging )
1088 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
1089 }
1090 else
1091 {
1092 for(j=0;j<face_list_element;j++)
1093 {
1094 fle=ef->data;
1095 if ( nlsr->debugging )
akmhoquef06586e2013-02-05 12:09:38 -06001096 printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
akmhoque8fdd6412012-12-04 15:05:33 -06001097 if ( nlsr->detailed_logging )
1098 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
1099 hashtb_next(ef);
1100 }
1101 }
1102 hashtb_end(ef);
1103
1104 hashtb_next(e);
1105 }
1106
1107 hashtb_end(e);
1108
1109 if ( nlsr->debugging )
1110 printf("\n");
1111 if ( nlsr->detailed_logging )
1112 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
1113}
1114
1115
1116int
1117delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1118{
1119 if ( nlsr->debugging )
1120 {
1121 printf("delete_empty_rte called\n");
1122 printf("Router: %s \n",(char *)ev->evdata);
1123 }
1124 if ( nlsr->detailed_logging )
1125 {
1126 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1127 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
akmhoque8fdd6412012-12-04 15:05:33 -06001128 }
1129
1130 if(flags == CCN_SCHEDULE_CANCEL)
1131 {
1132 return -1;
1133 }
akmhoque613446f2013-01-23 10:40:12 -06001134
1135 nlsr_lock();
akmhoque8fdd6412012-12-04 15:05:33 -06001136 int res;
1137 struct hashtb_enumerator ee;
1138 struct hashtb_enumerator *e = &ee;
1139
1140 hashtb_start(nlsr->routing_table, e);
1141 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1142
1143 if ( res == HT_OLD_ENTRY )
1144 {
1145 hashtb_delete(e);
1146 }
1147 else if ( res == HT_NEW_ENTRY )
1148 {
1149 hashtb_delete(e);
1150 }
1151
1152 print_routing_table();
akmhoque613446f2013-01-23 10:40:12 -06001153
1154 nlsr_unlock();
akmhoque8fdd6412012-12-04 15:05:33 -06001155
1156 return 0;
1157}
1158
1159void
1160clear_old_routing_table(void)
1161{
1162 if ( nlsr->debugging )
1163 printf("clear_old_routing_table called\n");
1164 if ( nlsr->detailed_logging )
1165 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
1166 int i,rt_element;
1167
1168 struct routing_table_entry *rte;
1169
1170 struct hashtb_enumerator ee;
1171 struct hashtb_enumerator *e = &ee;
1172
1173 hashtb_start(nlsr->routing_table, e);
1174 rt_element=hashtb_n(nlsr->routing_table);
1175
1176 for(i=0;i<rt_element;i++)
1177 {
1178 rte=e->data;
1179 hashtb_destroy(&rte->face_list);
1180 struct hashtb_param param_fle = {0};
1181 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1182
1183 hashtb_next(e);
1184 }
1185
1186 hashtb_end(e);
1187}
1188
1189
1190void
1191do_old_routing_table_updates(void)
1192{
1193 if ( nlsr->debugging )
1194 printf("do_old_routing_table_updates called\n");
1195 if ( nlsr->detailed_logging )
1196 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1197
1198 int i, rt_element;
1199 int mapping_no;
1200
1201 struct routing_table_entry *rte;
1202
1203 struct hashtb_enumerator ee;
1204 struct hashtb_enumerator *e = &ee;
1205
1206 hashtb_start(nlsr->routing_table, e);
1207 rt_element=hashtb_n(nlsr->routing_table);
1208
1209 for(i=0;i<rt_element;i++)
1210 {
1211 rte=e->data;
1212 mapping_no=get_mapping_no(rte->dest_router);
1213 if ( mapping_no == NO_MAPPING_NUM)
1214 {
1215 delete_orig_router_from_npt(rte->dest_router);
1216 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1217 memset(router,0,strlen(rte->dest_router)+1);
1218 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1219 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
1220 }
1221 hashtb_next(e);
1222 }
1223
1224 hashtb_end(e);
1225}
1226
akmhoquef06586e2013-02-05 12:09:38 -06001227void
1228update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist)
1229{
1230 if ( nlsr->debugging )
1231 printf("update_routing_table_with_new_hyperbolic_route called\n");
1232 if ( nlsr->detailed_logging )
1233 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n");
1234
1235 char *orig_router=get_router_from_rev_map(dest_router_rev_map_index);
1236
1237 if (face != NO_NEXT_HOP && face != NO_FACE )
1238 {
1239 update_routing_table(orig_router,face,nbr_to_dest_dist);
1240 if ( nlsr->debugging )
1241 printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1242 if ( nlsr->detailed_logging )
1243 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1244
1245 }
1246
1247
1248}
akmhoque8fdd6412012-12-04 15:05:33 -06001249
1250
1251void
1252update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
1253{
1254 if ( nlsr->debugging )
1255 printf("update_routing_table_with_new_route called\n");
1256 if ( nlsr->detailed_logging )
1257 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
1258 int i, map_element;
1259 struct map_entry *me;
1260
1261 struct hashtb_enumerator ee;
1262 struct hashtb_enumerator *e = &ee;
1263
1264 hashtb_start(nlsr->rev_map, e);
1265 map_element=hashtb_n(nlsr->rev_map);
1266
1267 for(i=0;i<map_element;i++)
1268 {
1269 me=e->data;
1270 if(me->mapping != source)
1271 {
1272
1273 char *orig_router=get_router_from_rev_map(me->mapping);
1274 if (orig_router != NULL )
1275 {
1276 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoque8fdd6412012-12-04 15:05:33 -06001277 if ( next_hop_router_num == NO_NEXT_HOP )
1278 {
akmhoque8fdd6412012-12-04 15:05:33 -06001279 if ( nlsr->debugging )
1280 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1281 if ( nlsr->detailed_logging )
1282 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1283 }
1284 else
1285 {
1286 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoque8fdd6412012-12-04 15:05:33 -06001287 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoque8fdd6412012-12-04 15:05:33 -06001288 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
1289 if ( nlsr->debugging )
1290 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1291 if ( nlsr->detailed_logging )
1292 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1293
1294
1295 }
1296 }
1297 }
1298 hashtb_next(e);
1299 }
1300
1301 hashtb_end(e);
1302}
1303
1304int
1305does_face_exist_for_router(char *dest_router, int face_id)
1306{
akmhoque0b217bf2013-01-23 09:49:47 -06001307 if (nlsr->debugging)
1308 {
akmhoque10489a82013-01-23 09:59:15 -06001309 printf("does_face_exist_for_router called\n");
akmhoque0b217bf2013-01-23 09:49:47 -06001310 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1311 }
1312
akmhoque8fdd6412012-12-04 15:05:33 -06001313 int ret=0;
1314
akmhoque869ce022013-01-23 10:14:47 -06001315 int res;
akmhoque8fdd6412012-12-04 15:05:33 -06001316 struct routing_table_entry *rte;
1317
1318 struct hashtb_enumerator ee;
1319 struct hashtb_enumerator *e = &ee;
1320
1321 hashtb_start(nlsr->routing_table, e);
1322 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1323
1324 if( res == HT_OLD_ENTRY )
1325 {
1326 rte=e->data;
akmhoque869ce022013-01-23 10:14:47 -06001327 unsigned *v;
1328 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1329 if (v != NULL)
1330 ret = 1;
akmhoque8fdd6412012-12-04 15:05:33 -06001331 }
akmhoque869ce022013-01-23 10:14:47 -06001332 else
akmhoque8fdd6412012-12-04 15:05:33 -06001333 {
1334 hashtb_delete(e);
1335 }
1336
1337 hashtb_end(e);
1338
akmhoque8fdd6412012-12-04 15:05:33 -06001339 return ret;
1340}
akmhoquef06586e2013-02-05 12:09:38 -06001341
1342double
1343get_hyperbolic_distance(long int source, long int dest)
1344{
1345 double distance;
1346 char *src_router=get_router_from_rev_map(source);
1347 char *dest_router=get_router_from_rev_map(dest);
1348
1349 double src_r=get_hyperbolic_r(src_router);
1350 double src_theta=get_hyperbolic_r(src_router);
1351
1352 double dest_r=get_hyperbolic_r(dest_router);
1353 double dest_theta=get_hyperbolic_r(dest_router);
1354 if ( src_r != -1 && dest_r != -1 )
1355 {
1356 distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta));
1357 }
1358 else
1359 {
1360 distance= -1.0;
1361 }
1362
1363 return distance;
1364}