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