blob: 1d8bc4240702348193871564b874353c0b447e31 [file] [log] [blame]
akmhoque29c1db52012-09-07 14:47:43 -05001#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"
akmhoque3560cb62012-09-09 10:52:30 -050025#include "nlsr_npt.h"
akmhoquefbfd0982012-09-09 20:59:03 -050026#include "nlsr_adl.h"
akmhoquede61ba92012-09-20 22:19:12 -050027#include "nlsr_fib.h"
akmhoque29c1db52012-09-07 14:47:43 -050028
29int
30route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
31{
akmhoqueffacaa82012-09-13 17:48:30 -050032
33 if(flags == CCN_SCHEDULE_CANCEL)
34 {
35 return -1;
36 }
37
38 nlsr_lock();
39
akmhoque29c1db52012-09-07 14:47:43 -050040 printf("route_calculate called\n");
41
42 if( ! nlsr->is_build_adj_lsa_sheduled )
43 {
44 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050045 print_routing_table();
46 print_npt();
47
akmhoque29c1db52012-09-07 14:47:43 -050048 struct hashtb_param param_me = {0};
49 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoquefbfd0982012-09-09 20:59:03 -050050 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoque29c1db52012-09-07 14:47:43 -050051 make_map();
52 assign_mapping_number();
53 print_map();
akmhoquefbfd0982012-09-09 20:59:03 -050054 print_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -050055
akmhoque3560cb62012-09-09 10:52:30 -050056 do_old_routing_table_updates();
akmhoquede61ba92012-09-20 22:19:12 -050057 clear_old_routing_table();
58 print_routing_table();
akmhoque3560cb62012-09-09 10:52:30 -050059
akmhoque29c1db52012-09-07 14:47:43 -050060 int i;
61 int **adj_matrix;
62 int map_element=hashtb_n(nlsr->map);
63 adj_matrix=malloc(map_element * sizeof(int *));
64 for(i = 0; i < map_element; i++)
65 {
66 adj_matrix[i] = malloc(map_element * sizeof(int));
67 }
68 make_adj_matrix(adj_matrix,map_element);
69 print_adj_matrix(adj_matrix,map_element);
70
71 long int source=get_mapping_no(nlsr->router_name);
72 long int *parent=(long int *)malloc(map_element * sizeof(long int));
akmhoquede61ba92012-09-20 22:19:12 -050073 long int *dist=(long int *)malloc(map_element * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -050074
akmhoquede61ba92012-09-20 22:19:12 -050075 calculate_path(adj_matrix,parent,dist, map_element, source);
akmhoque29c1db52012-09-07 14:47:43 -050076
77 print_all_path_from_source(parent,source);
78
akmhoquefbfd0982012-09-09 20:59:03 -050079 print_all_next_hop(parent,source);
80
akmhoquede61ba92012-09-20 22:19:12 -050081 update_routing_table_with_new_route(parent, dist,source);
82
83 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -050084
akmhoqueffacaa82012-09-13 17:48:30 -050085 print_routing_table();
86 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -050087
akmhoque29c1db52012-09-07 14:47:43 -050088 for(i = 0; i < map_element; i++)
89 {
90 free(adj_matrix[i]);
91 }
92 free(parent);
akmhoquede61ba92012-09-20 22:19:12 -050093 free(dist);
akmhoque29c1db52012-09-07 14:47:43 -050094 free(adj_matrix);
95 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -050096 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -050097
98 }
99 nlsr->is_route_calculation_scheduled=0;
100
akmhoqueffacaa82012-09-13 17:48:30 -0500101 nlsr_unlock();
102
akmhoque29c1db52012-09-07 14:47:43 -0500103 return 0;
104}
105
106void
akmhoquede61ba92012-09-20 22:19:12 -0500107calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500108{
109 int i;
110 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500111 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500112 long int *Q=(long int *)malloc(V * sizeof(long int));
113 long int head=0;
114 /* Initial the Parent */
115 for (i = 0 ; i < V; i++)
116 {
117 parent[i]=EMPTY_PARENT;
118 dist[i]=INF_DISTANCE;
119 Q[i]=i;
120 }
121
122 dist[S]=0;
123 sort_queue_by_distance(Q,dist,head,V);
124
125 while (head < V )
126 {
127 u=Q[head];
128 if(dist[u] == INF_DISTANCE)
129 {
130 break;
131 }
132
133 for(v=0 ; v <V; v++)
134 {
135 if( adj_matrix[u][v] > 0 ) //
136 {
137 if ( is_not_explored(Q,v,head+1,V) )
138 {
139
140 if( dist[u] + adj_matrix[u][v] < dist[v])
141 {
142 dist[v]=dist[u] + adj_matrix[u][v] ;
143 parent[v]=u;
144 }
145
146 }
147
148 }
149
150 }
151
152 head++;
153 sort_queue_by_distance(Q,dist,head,V);
154 }
155
156 free(Q);
akmhoquede61ba92012-09-20 22:19:12 -0500157 //free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500158}
159
160void
161print_all_path_from_source(long int *parent,long int source)
162{
163 int i, map_element;
164 struct map_entry *me;
165
166 struct hashtb_enumerator ee;
167 struct hashtb_enumerator *e = &ee;
168
169 hashtb_start(nlsr->map, e);
170 map_element=hashtb_n(nlsr->map);
171
172 for(i=0;i<map_element;i++)
173 {
174 me=e->data;
175 if(me->mapping != source)
176 {
177 print_path(parent,(long int)me->mapping);
178 printf("\n");
179 }
180 hashtb_next(e);
181 }
182
183 hashtb_end(e);
184
185}
186
akmhoquefbfd0982012-09-09 20:59:03 -0500187void
188print_all_next_hop(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 for(i=0;i<map_element;i++)
200 {
201 me=e->data;
202 if(me->mapping != source)
203 {
204
205 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
206 }
207 hashtb_next(e);
208 }
209
210 hashtb_end(e);
211
212}
213
214long int
215get_next_hop_from_calculation(long int *parent, long int dest,long int source)
216{
217 long int next_hop;
218 while ( parent[dest] != EMPTY_PARENT )
219 {
220 next_hop=dest;
221 dest=parent[dest];
222
223 }
224
225 if ( dest != source )
226 {
227 next_hop=NO_NEXT_HOP;
228 }
229
230 return next_hop;
231
232}
233
akmhoque29c1db52012-09-07 14:47:43 -0500234void
235print_path(long int *parent, long int dest)
236{
237 if (parent[dest] != EMPTY_PARENT )
238 print_path(parent,parent[dest]);
239
240 printf(" %ld",dest);
241}
242
243int
244is_not_explored(long int *Q, long int u,long int start, long int element)
245{
246 int ret=0;
247 long int i;
248 for(i=start; i< element; i++)
249 {
250 if ( Q[i] == u )
251 {
252 ret=1;
253 break;
254 }
255 }
256 return ret;
257}
258
259void
260sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
261{
262 long int i,j;
263 long int temp_u;
264
265 for ( i=start ; i < element ; i ++)
266 {
267 for( j=i+1; j<element; j ++)
268 {
269 if (dist[Q[j]] < dist[Q[i]])
270 {
271 temp_u=Q[j];
272 Q[j]=Q[i];
273 Q[i]=temp_u;
274 }
275 }
276 }
277
278}
279
280void
281print_map(void)
282{
283 int i, map_element;
284 struct map_entry *me;
285
286 struct hashtb_enumerator ee;
287 struct hashtb_enumerator *e = &ee;
288
289 hashtb_start(nlsr->map, e);
290 map_element=hashtb_n(nlsr->map);
291
292 for(i=0;i<map_element;i++)
293 {
294 me=e->data;
295 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
296 hashtb_next(e);
297 }
298
299 hashtb_end(e);
300}
301
302
303void
304assign_mapping_number(void)
305{
306 int i, map_element;
307 struct map_entry *me;
308
309 struct hashtb_enumerator ee;
310 struct hashtb_enumerator *e = &ee;
311
312 hashtb_start(nlsr->map, e);
313 map_element=hashtb_n(nlsr->map);
314
315 for(i=0;i<map_element;i++)
316 {
317 me=e->data;
318 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500319 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500320 hashtb_next(e);
321 }
322
323 hashtb_end(e);
324}
325
326void
327make_map(void)
328{
329
330
331 int i, adj_lsdb_element;
332 struct alsa *adj_lsa;
333
334 struct hashtb_enumerator ee;
335 struct hashtb_enumerator *e = &ee;
336
337 hashtb_start(nlsr->lsdb->adj_lsdb, e);
338 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
339
340 for(i=0;i<adj_lsdb_element;i++)
341 {
342 adj_lsa=e->data;
343 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
344 hashtb_next(e);
345 }
346
347 hashtb_end(e);
348
349}
350
351void
352add_map_entry(char *router)
353{
354
355 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
356
357 struct hashtb_enumerator ee;
358 struct hashtb_enumerator *e = &ee;
359 int res;
360
361 hashtb_start(nlsr->map, e);
362 res = hashtb_seek(e, router, strlen(router), 0);
363
364 if(res == HT_NEW_ENTRY)
365 {
366 me=e->data;
367 me->router=(char *)malloc(strlen(router)+1);
368 memset(me->router,0,strlen(router)+1);
369 memcpy(me->router,router,strlen(router));
370 me->mapping=0;
371 }
372
373 hashtb_end(e);
374
375}
376
377
378void
379add_adj_data_to_map(char *orig_router, char *body, int no_link)
380{
381 add_map_entry(orig_router);
382
383 int i=0;
384 char *lsa_data=(char *)malloc(strlen(body)+1);
385 memset( lsa_data,0,strlen(body)+1);
386 memcpy(lsa_data,body,strlen(body)+1);
387 char *sep="|";
388 char *rem;
389 char *rtr_id;
390 char *length;
391 char *face;
392 char *metric;
393
394 if(no_link >0 )
395 {
396 rtr_id=strtok_r(lsa_data,sep,&rem);
397 length=strtok_r(NULL,sep,&rem);
398 face=strtok_r(NULL,sep,&rem);
399 metric=strtok_r(NULL,sep,&rem);
400
401 add_map_entry(rtr_id);
402
403 for(i=1;i<no_link;i++)
404 {
405 rtr_id=strtok_r(NULL,sep,&rem);
406 length=strtok_r(NULL,sep,&rem);
407 face=strtok_r(NULL,sep,&rem);
408 metric=strtok_r(NULL,sep,&rem);
409
410 add_map_entry(rtr_id);
411
412 }
413 }
414
415 free(lsa_data);
416}
417
418int
419get_mapping_no(char *router)
420{
421 struct map_entry *me;
422
423 struct hashtb_enumerator ee;
424 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500425 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500426 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500427
akmhoque63152c62012-09-18 08:43:42 -0500428 int n = hashtb_n(nlsr->map);
429
430 if ( n < 1)
431 {
432 return NO_MAPPING_NUM;
433 }
434
akmhoque29c1db52012-09-07 14:47:43 -0500435 hashtb_start(nlsr->map, e);
436 res = hashtb_seek(e, router, strlen(router), 0);
437
438 if( res == HT_OLD_ENTRY )
439 {
440 me=e->data;
441 ret=me->mapping;
442 }
443 else if(res == HT_NEW_ENTRY)
444 {
445 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500446 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500447 }
448
449 hashtb_end(e);
450
451 return ret;
452
453}
454
akmhoquefbfd0982012-09-09 20:59:03 -0500455
456void
457add_rev_map_entry(long int mapping_number, char *router)
458{
459
460 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
461
462 struct hashtb_enumerator ee;
463 struct hashtb_enumerator *e = &ee;
464 int res;
465
466 hashtb_start(nlsr->rev_map, e);
467 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
468
469 if(res == HT_NEW_ENTRY)
470 {
471 me=e->data;
472 me->router=(char *)malloc(strlen(router)+1);
473 memset(me->router,0,strlen(router)+1);
474 memcpy(me->router,router,strlen(router));
475 me->mapping=mapping_number;
476 }
477
478 hashtb_end(e);
479
480}
481
482
483
484char *
485get_router_from_rev_map(long int mapping_number)
486{
487
488 struct map_entry *me;
489
490 struct hashtb_enumerator ee;
491 struct hashtb_enumerator *e = &ee;
492 int res;
493
494 hashtb_start(nlsr->rev_map, e);
495 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
496
497 if(res == HT_OLD_ENTRY)
498 {
499 me=e->data;
500 hashtb_end(e);
501 return me->router;
502 }
503 else if(res == HT_NEW_ENTRY)
504 {
505 hashtb_delete(e);
506 hashtb_end(e);
507 }
508
509 return NULL;
510}
511
512void
513print_rev_map(void)
514{
515 int i, map_element;
516 struct map_entry *me;
517
518 struct hashtb_enumerator ee;
519 struct hashtb_enumerator *e = &ee;
520
521 hashtb_start(nlsr->map, e);
522 map_element=hashtb_n(nlsr->map);
523
524 for(i=0;i<map_element;i++)
525 {
526 me=e->data;
527 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
528 hashtb_next(e);
529 }
530
531 hashtb_end(e);
532}
533
534
akmhoque29c1db52012-09-07 14:47:43 -0500535void
536assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
537{
538 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
539 int mapping_nbr_router;
540
541 int i;
542 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
543 memset( lsa_data,0,strlen(adj_lsa->body)+1);
544 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
545 char *sep="|";
546 char *rem;
547 char *rtr_id;
548 char *length;
549 char *face;
550 char *metric;
551
552 if(adj_lsa->no_link >0 )
553 {
554 rtr_id=strtok_r(lsa_data,sep,&rem);
555 length=strtok_r(NULL,sep,&rem);
556 face=strtok_r(NULL,sep,&rem);
557 metric=strtok_r(NULL,sep,&rem);
558
559 mapping_nbr_router=get_mapping_no(rtr_id);
560 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
561
562 for(i=1;i<adj_lsa->no_link;i++)
563 {
564 rtr_id=strtok_r(NULL,sep,&rem);
565 length=strtok_r(NULL,sep,&rem);
566 face=strtok_r(NULL,sep,&rem);
567 metric=strtok_r(NULL,sep,&rem);
568
569 mapping_nbr_router=get_mapping_no(rtr_id);
570 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
571
572 }
573 }
574
575 free(lsa_data);
576}
577
578void
579make_adj_matrix(int **adj_matrix,int map_element)
580{
581
582 init_adj_matrix(adj_matrix,map_element);
583
584 int i, adj_lsdb_element;
585 struct alsa *adj_lsa;
586
587 struct hashtb_enumerator ee;
588 struct hashtb_enumerator *e = &ee;
589
590 hashtb_start(nlsr->lsdb->adj_lsdb, e);
591 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
592
593 for(i=0;i<adj_lsdb_element;i++)
594 {
595 adj_lsa=e->data;
596 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
597 hashtb_next(e);
598 }
599
600 hashtb_end(e);
601
602}
603
604void
605init_adj_matrix(int **adj_matrix,int map_element)
606{
607 int i, j;
608 for(i=0;i<map_element;i++)
609 for(j=0;j<map_element;j++)
610 adj_matrix[i][j]=0;
611}
612
613void print_adj_matrix(int **adj_matrix, int map_element)
614{
615 int i, j;
616 for(i=0;i<map_element;i++)
617 {
618 for(j=0;j<map_element;j++)
619 printf("%d ",adj_matrix[i][j]);
620 printf("\n");
621 }
622}
623
akmhoquede61ba92012-09-20 22:19:12 -0500624
akmhoque3560cb62012-09-09 10:52:30 -0500625int
akmhoquede61ba92012-09-20 22:19:12 -0500626get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500627{
628 struct routing_table_entry *rte;
629
630 struct hashtb_enumerator ee;
631 struct hashtb_enumerator *e = &ee;
632 int res,ret;
633
634 hashtb_start(nlsr->routing_table, e);
635 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
636
637 if( res == HT_OLD_ENTRY )
638 {
639 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500640 ret=hashtb_n(rte->face_list);
641 //nhl=rte->face_list;
642 }
643 else if(res == HT_NEW_ENTRY)
644 {
645 hashtb_delete(e);
646 ret=NO_NEXT_HOP;
647 }
648
649 hashtb_end(e);
650
651 return ret;
652}
653
654
655int
656get_next_hop(char *dest_router,int *faces, int *route_costs)
657{
658 struct routing_table_entry *rte;
659
660 struct hashtb_enumerator ee;
661 struct hashtb_enumerator *e = &ee;
662 int res,ret;
663
664 hashtb_start(nlsr->routing_table, e);
665 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
666
667 if( res == HT_OLD_ENTRY )
668 {
669 rte=e->data;
670 ret=hashtb_n(rte->face_list);
671 //nhl=rte->face_list;
672 int j,face_list_element;
673 struct face_list_entry *fle;
674
675 struct hashtb_enumerator eef;
676 struct hashtb_enumerator *ef = &eef;
677
678 hashtb_start(rte->face_list, ef);
679 face_list_element=hashtb_n(rte->face_list);
680 for(j=0;j<face_list_element;j++)
681 {
682 fle=ef->data;
683 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
684 faces[j]=fle->next_hop_face;
685 route_costs[j]=fle->route_cost;
686 hashtb_next(ef);
687 }
688 hashtb_end(ef);
689
akmhoque3560cb62012-09-09 10:52:30 -0500690 }
691 else if(res == HT_NEW_ENTRY)
692 {
693 hashtb_delete(e);
694 ret=NO_NEXT_HOP;
695 }
696
697 hashtb_end(e);
698
699 return ret;
700}
701
702void
703add_next_hop_router(char *dest_router)
704{
705 if ( strcmp(dest_router,nlsr->router_name)== 0)
706 {
707 return ;
708 }
709
710 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
711
712 struct hashtb_enumerator ee;
713 struct hashtb_enumerator *e = &ee;
714 int res;
715
716 hashtb_start(nlsr->routing_table, e);
717 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
718
719 if( res == HT_NEW_ENTRY )
720 {
721 rte=e->data;
722 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
723 memset(rte->dest_router,0,strlen(dest_router)+1);
724 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500725 //rte->next_hop_face=NO_NEXT_HOP;
726 struct hashtb_param param_fle = {0};
727 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
728
729 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500730 }
731 hashtb_end(e);
732
733}
734
735void
736add_next_hop_from_lsa_adj_body(char *body, int no_link)
737{
738
739 int i=0;
740 char *lsa_data=(char *)malloc(strlen(body)+1);
741 memset( lsa_data,0,strlen(body)+1);
742 memcpy(lsa_data,body,strlen(body)+1);
743 char *sep="|";
744 char *rem;
745 char *rtr_id;
746 char *length;
747 char *face;
748 char *metric;
749
750 if(no_link >0 )
751 {
752 rtr_id=strtok_r(lsa_data,sep,&rem);
753 length=strtok_r(NULL,sep,&rem);
754 face=strtok_r(NULL,sep,&rem);
755 metric=strtok_r(NULL,sep,&rem);
756
akmhoque3560cb62012-09-09 10:52:30 -0500757
758 add_next_hop_router(rtr_id);
759
760 for(i=1;i<no_link;i++)
761 {
762 rtr_id=strtok_r(NULL,sep,&rem);
763 length=strtok_r(NULL,sep,&rem);
764 face=strtok_r(NULL,sep,&rem);
765 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500766
767 add_next_hop_router(rtr_id);
768
769 }
770 }
771
772 free(lsa_data);
773
774
775}
776
777void
akmhoquede61ba92012-09-20 22:19:12 -0500778update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -0500779{
akmhoquede61ba92012-09-20 22:19:12 -0500780 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -0500781 struct routing_table_entry *rte;
782
783 struct hashtb_enumerator ee;
784 struct hashtb_enumerator *e = &ee;
785
786 hashtb_start(nlsr->routing_table, e);
787 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
788
789 if( res == HT_OLD_ENTRY )
790 {
791 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500792
793 struct hashtb_enumerator eef;
794 struct hashtb_enumerator *ef = &eef;
795
796 hashtb_start(rte->face_list, ef);
797 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
798 if( res1 == HT_NEW_ENTRY)
799 {
800 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
801 fle=ef->data;
802 fle->next_hop_face=next_hop_face;
803 fle->route_cost=route_cost;
804 }
805 else if ( res1 == HT_OLD_ENTRY )
806 {
807 struct face_list_entry *fle;
808 fle=ef->data;
809 fle->route_cost=route_cost;
810 }
811 hashtb_end(ef);
812
813 /*
814 //updating the face for the router prefix itself
815 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
816 {
817 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
818 }
819 if ( (next_hop_face != NO_FACE || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
820 {
821 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
822 }
823
akmhoqueffacaa82012-09-13 17:48:30 -0500824 rte->next_hop_face=next_hop_face;
akmhoquede61ba92012-09-20 22:19:12 -0500825 */
akmhoqueffacaa82012-09-13 17:48:30 -0500826 }
827 else if ( res == HT_OLD_ENTRY )
828 {
829 hashtb_delete(e);
830 }
831
832 hashtb_end(e);
833
834}
835
836void
akmhoque3560cb62012-09-09 10:52:30 -0500837print_routing_table(void)
838{
839 printf("\n");
840 printf("print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500841 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -0500842
843 struct routing_table_entry *rte;
844
845 struct hashtb_enumerator ee;
846 struct hashtb_enumerator *e = &ee;
847
848 hashtb_start(nlsr->routing_table, e);
849 rt_element=hashtb_n(nlsr->routing_table);
850
851 for(i=0;i<rt_element;i++)
852 {
853 printf("----------Routing Table Entry %d------------------\n",i+1);
854 rte=e->data;
855 printf(" Destination Router: %s \n",rte->dest_router);
akmhoquede61ba92012-09-20 22:19:12 -0500856 //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);
857
858 struct face_list_entry *fle;
859
860 struct hashtb_enumerator eef;
861 struct hashtb_enumerator *ef = &eef;
862
863 hashtb_start(rte->face_list, ef);
864 face_list_element=hashtb_n(rte->face_list);
865 if ( face_list_element <= 0 )
866 {
867 printf(" Face: No Face \n");
868 }
869 else
870 {
871 for(j=0;j<face_list_element;j++)
872 {
873 fle=ef->data;
874 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
875 hashtb_next(ef);
876 }
877 }
878 hashtb_end(ef);
879
akmhoque3560cb62012-09-09 10:52:30 -0500880 hashtb_next(e);
881 }
882
883 hashtb_end(e);
884
885 printf("\n");
886}
887
akmhoque63152c62012-09-18 08:43:42 -0500888
889int
890delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
891{
892 printf("delete_empty_rte called\n");
893 printf("Router: %s \n",(char *)ev->evdata);
894 if(flags == CCN_SCHEDULE_CANCEL)
895 {
896 return -1;
897 }
898
akmhoquede61ba92012-09-20 22:19:12 -0500899 //struct routing_table_entry *rte;
akmhoque63152c62012-09-18 08:43:42 -0500900 int res;
901 struct hashtb_enumerator ee;
902 struct hashtb_enumerator *e = &ee;
903
904 hashtb_start(nlsr->routing_table, e);
905 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
906
907 if ( res == HT_OLD_ENTRY )
908 {
akmhoquede61ba92012-09-20 22:19:12 -0500909 //rte=e->data;
910
911 /*
912
913 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP) && is_neighbor(ev->evdata)==0 )
914 {
915 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)ev->evdata, OP_UNREG, rte->next_hop_face);
916 }
917
918 */
akmhoque63152c62012-09-18 08:43:42 -0500919 hashtb_delete(e);
920 }
921 else if ( res == HT_NEW_ENTRY )
922 {
923 hashtb_delete(e);
924 }
925
akmhoquede61ba92012-09-20 22:19:12 -0500926 print_routing_table();
927
akmhoque63152c62012-09-18 08:43:42 -0500928 return 0;
929}
930
akmhoquede61ba92012-09-20 22:19:12 -0500931void
932clear_old_routing_table()
933{
934 printf("clear_old_routing_table called\n");
935 int i,rt_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 rte=e->data;
948 hashtb_destroy(&rte->face_list);
949 struct hashtb_param param_fle = {0};
950 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
951
952 hashtb_next(e);
953 }
954
955 hashtb_end(e);
956}
957
akmhoque63152c62012-09-18 08:43:42 -0500958
akmhoque3560cb62012-09-09 10:52:30 -0500959void
960do_old_routing_table_updates()
961{
akmhoque810a5b52012-09-09 16:53:14 -0500962 printf("do_old_routing_table_updates called\n");
963 int i, rt_element;
964 int mapping_no;
965
966 struct routing_table_entry *rte;
967
968 struct hashtb_enumerator ee;
969 struct hashtb_enumerator *e = &ee;
970
971 hashtb_start(nlsr->routing_table, e);
972 rt_element=hashtb_n(nlsr->routing_table);
973
974 for(i=0;i<rt_element;i++)
975 {
976 rte=e->data;
977 mapping_no=get_mapping_no(rte->dest_router);
978 if ( mapping_no == NO_MAPPING_NUM)
979 {
akmhoquede61ba92012-09-20 22:19:12 -0500980 delete_orig_router_from_npt(rte->dest_router);
akmhoque63152c62012-09-18 08:43:42 -0500981 char *router=(char *)malloc(strlen(rte->dest_router)+1);
982 memset(router,0,strlen(rte->dest_router)+1);
983 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
984 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque810a5b52012-09-09 16:53:14 -0500985 }
986 hashtb_next(e);
987 }
988
989 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -0500990}
akmhoque29c1db52012-09-07 14:47:43 -0500991
akmhoquede61ba92012-09-20 22:19:12 -0500992
993
akmhoquefbfd0982012-09-09 20:59:03 -0500994void
akmhoquede61ba92012-09-20 22:19:12 -0500995update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -0500996{
997 printf("update_routing_table_with_new_route called\n");
998 int i, map_element;
999 struct map_entry *me;
1000
1001 struct hashtb_enumerator ee;
1002 struct hashtb_enumerator *e = &ee;
1003
1004 hashtb_start(nlsr->rev_map, e);
1005 map_element=hashtb_n(nlsr->rev_map);
1006
1007 for(i=0;i<map_element;i++)
1008 {
1009 me=e->data;
1010 if(me->mapping != source)
1011 {
1012
1013 char *orig_router=get_router_from_rev_map(me->mapping);
1014 if (orig_router != NULL )
1015 {
1016 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
1017 //printf(" Next hop router Num: %d ",next_hop_router_num);
1018 if ( next_hop_router_num == NO_NEXT_HOP )
1019 {
akmhoquede61ba92012-09-20 22:19:12 -05001020 //update_npt_with_new_route(orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001021 printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1022 }
1023 else
1024 {
1025 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
1026 //printf("Next hop router name: %s \n",next_hop_router);
1027 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001028 //update_npt_with_new_route(orig_router,next_hop_face);
1029 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoquefbfd0982012-09-09 20:59:03 -05001030 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1031
1032 }
1033 }
1034 }
1035 hashtb_next(e);
1036 }
1037
1038 hashtb_end(e);
1039}
1040
akmhoquede61ba92012-09-20 22:19:12 -05001041int
1042does_face_exist_for_router(char *dest_router, int face_id)
1043{
1044 int ret=0;
1045
1046 int res,res1;
1047 struct routing_table_entry *rte;
1048
1049 struct hashtb_enumerator ee;
1050 struct hashtb_enumerator *e = &ee;
1051
1052 hashtb_start(nlsr->routing_table, e);
1053 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1054
1055 if( res == HT_OLD_ENTRY )
1056 {
1057 rte=e->data;
1058 struct hashtb_enumerator eef;
1059 struct hashtb_enumerator *ef = &eef;
1060
1061 hashtb_start(rte->face_list, ef);
1062 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1063 if( res1 == HT_OLD_ENTRY)
1064 {
1065 ret=1;
1066 }
1067 else if ( res1 == HT_OLD_ENTRY )
1068 {
1069 hashtb_delete(ef);
1070 }
1071 hashtb_end(ef);
1072 }
1073 else if( res == HT_NEW_ENTRY )
1074 {
1075 hashtb_delete(e);
1076 }
1077
1078 hashtb_end(e);
1079
1080 return ret;
1081}