blob: a5956d81338be648292c9c2e1e63044627824223 [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();
akmhoquea30cb772012-10-07 09:50:34 -050059 print_npt();
akmhoque3560cb62012-09-09 10:52:30 -050060
akmhoque29c1db52012-09-07 14:47:43 -050061 int i;
62 int **adj_matrix;
63 int map_element=hashtb_n(nlsr->map);
64 adj_matrix=malloc(map_element * sizeof(int *));
65 for(i = 0; i < map_element; i++)
66 {
67 adj_matrix[i] = malloc(map_element * sizeof(int));
68 }
69 make_adj_matrix(adj_matrix,map_element);
70 print_adj_matrix(adj_matrix,map_element);
71
72 long int source=get_mapping_no(nlsr->router_name);
73 long int *parent=(long int *)malloc(map_element * sizeof(long int));
akmhoquede61ba92012-09-20 22:19:12 -050074 long int *dist=(long int *)malloc(map_element * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -050075
akmhoque9fe296b2012-09-24 09:52:08 -050076 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoque29c1db52012-09-07 14:47:43 -050077
akmhoque0820a2e2012-09-24 20:23:01 -050078 if ( (num_link == 0) || (nlsr->multi_path_face_num <= 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050079 {
80 calculate_path(adj_matrix,parent,dist, map_element, source);
81 print_all_path_from_source(parent,source);
82 print_all_next_hop(parent,source);
83 update_routing_table_with_new_route(parent, dist,source);
84 }
akmhoque59d38c82012-09-24 20:24:03 -050085 else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050086 {
87 long int *links=(long int *)malloc(num_link*sizeof(long int));
88 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
89 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
90 for ( i=0 ; i < num_link; i++)
91 {
92 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
93 calculate_path(adj_matrix,parent,dist, map_element, source);
94 print_all_path_from_source(parent,source);
95 print_all_next_hop(parent,source);
96 update_routing_table_with_new_route(parent, dist,source);
97 }
akmhoque29c1db52012-09-07 14:47:43 -050098
akmhoque9fe296b2012-09-24 09:52:08 -050099 free(links);
100 free(link_costs);
101 }
akmhoquede61ba92012-09-20 22:19:12 -0500102
103 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -0500104
akmhoqueffacaa82012-09-13 17:48:30 -0500105 print_routing_table();
106 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -0500107
akmhoque29c1db52012-09-07 14:47:43 -0500108 for(i = 0; i < map_element; i++)
109 {
110 free(adj_matrix[i]);
111 }
112 free(parent);
akmhoquede61ba92012-09-20 22:19:12 -0500113 free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500114 free(adj_matrix);
115 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -0500116 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -0500117
118 }
119 nlsr->is_route_calculation_scheduled=0;
120
akmhoqueffacaa82012-09-13 17:48:30 -0500121 nlsr_unlock();
122
akmhoque29c1db52012-09-07 14:47:43 -0500123 return 0;
124}
125
126void
akmhoquede61ba92012-09-20 22:19:12 -0500127calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500128{
129 int i;
130 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500131 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500132 long int *Q=(long int *)malloc(V * sizeof(long int));
133 long int head=0;
134 /* Initial the Parent */
135 for (i = 0 ; i < V; i++)
136 {
137 parent[i]=EMPTY_PARENT;
138 dist[i]=INF_DISTANCE;
139 Q[i]=i;
140 }
141
akmhoque9fe296b2012-09-24 09:52:08 -0500142 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500143 {
akmhoque9fe296b2012-09-24 09:52:08 -0500144 dist[S]=0;
145 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500146
akmhoque9fe296b2012-09-24 09:52:08 -0500147 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500148 {
akmhoque9fe296b2012-09-24 09:52:08 -0500149 u=Q[head];
150 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500151 {
akmhoque9fe296b2012-09-24 09:52:08 -0500152 break;
153 }
154
155 for(v=0 ; v <V; v++)
156 {
157 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500158 {
akmhoque9fe296b2012-09-24 09:52:08 -0500159 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500160 {
akmhoque9fe296b2012-09-24 09:52:08 -0500161
162 if( dist[u] + adj_matrix[u][v] < dist[v])
163 {
164 dist[v]=dist[u] + adj_matrix[u][v] ;
165 parent[v]=u;
166 }
167
168 }
akmhoque29c1db52012-09-07 14:47:43 -0500169
170 }
171
172 }
173
akmhoque9fe296b2012-09-24 09:52:08 -0500174 head++;
175 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500176 }
akmhoque29c1db52012-09-07 14:47:43 -0500177 }
akmhoque29c1db52012-09-07 14:47:43 -0500178 free(Q);
akmhoquede61ba92012-09-20 22:19:12 -0500179 //free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500180}
181
182void
183print_all_path_from_source(long int *parent,long int source)
184{
185 int i, map_element;
186 struct map_entry *me;
187
188 struct hashtb_enumerator ee;
189 struct hashtb_enumerator *e = &ee;
190
191 hashtb_start(nlsr->map, e);
192 map_element=hashtb_n(nlsr->map);
193
akmhoque9fe296b2012-09-24 09:52:08 -0500194 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500195 {
akmhoque9fe296b2012-09-24 09:52:08 -0500196 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500197 {
akmhoque9fe296b2012-09-24 09:52:08 -0500198 me=e->data;
199 if(me->mapping != source)
200 {
201 print_path(parent,(long int)me->mapping);
202 printf("\n");
203 }
204 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500205 }
akmhoque29c1db52012-09-07 14:47:43 -0500206 }
akmhoque29c1db52012-09-07 14:47:43 -0500207 hashtb_end(e);
208
209}
210
akmhoquefbfd0982012-09-09 20:59:03 -0500211void
212print_all_next_hop(long int *parent,long int source)
213{
214 int i, map_element;
215 struct map_entry *me;
216
217 struct hashtb_enumerator ee;
218 struct hashtb_enumerator *e = &ee;
219
220 hashtb_start(nlsr->map, e);
221 map_element=hashtb_n(nlsr->map);
222
223 for(i=0;i<map_element;i++)
224 {
225 me=e->data;
226 if(me->mapping != source)
227 {
228
229 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
230 }
231 hashtb_next(e);
232 }
233
234 hashtb_end(e);
235
236}
237
238long int
239get_next_hop_from_calculation(long int *parent, long int dest,long int source)
240{
241 long int next_hop;
242 while ( parent[dest] != EMPTY_PARENT )
243 {
244 next_hop=dest;
245 dest=parent[dest];
246
247 }
248
249 if ( dest != source )
250 {
251 next_hop=NO_NEXT_HOP;
252 }
253
254 return next_hop;
255
256}
257
akmhoque29c1db52012-09-07 14:47:43 -0500258void
259print_path(long int *parent, long int dest)
260{
261 if (parent[dest] != EMPTY_PARENT )
262 print_path(parent,parent[dest]);
263
264 printf(" %ld",dest);
265}
266
267int
268is_not_explored(long int *Q, long int u,long int start, long int element)
269{
270 int ret=0;
271 long int i;
272 for(i=start; i< element; i++)
273 {
274 if ( Q[i] == u )
275 {
276 ret=1;
277 break;
278 }
279 }
280 return ret;
281}
282
283void
284sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
285{
286 long int i,j;
287 long int temp_u;
288
289 for ( i=start ; i < element ; i ++)
290 {
291 for( j=i+1; j<element; j ++)
292 {
293 if (dist[Q[j]] < dist[Q[i]])
294 {
295 temp_u=Q[j];
296 Q[j]=Q[i];
297 Q[i]=temp_u;
298 }
299 }
300 }
301
302}
303
304void
305print_map(void)
306{
307 int i, map_element;
308 struct map_entry *me;
309
310 struct hashtb_enumerator ee;
311 struct hashtb_enumerator *e = &ee;
312
313 hashtb_start(nlsr->map, e);
314 map_element=hashtb_n(nlsr->map);
315
316 for(i=0;i<map_element;i++)
317 {
318 me=e->data;
319 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
320 hashtb_next(e);
321 }
322
323 hashtb_end(e);
324}
325
326
327void
328assign_mapping_number(void)
329{
330 int i, map_element;
331 struct map_entry *me;
332
333 struct hashtb_enumerator ee;
334 struct hashtb_enumerator *e = &ee;
335
336 hashtb_start(nlsr->map, e);
337 map_element=hashtb_n(nlsr->map);
338
339 for(i=0;i<map_element;i++)
340 {
341 me=e->data;
342 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500343 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500344 hashtb_next(e);
345 }
346
347 hashtb_end(e);
348}
349
350void
351make_map(void)
352{
353
354
355 int i, adj_lsdb_element;
356 struct alsa *adj_lsa;
357
358 struct hashtb_enumerator ee;
359 struct hashtb_enumerator *e = &ee;
360
361 hashtb_start(nlsr->lsdb->adj_lsdb, e);
362 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
363
akmhoque9fe296b2012-09-24 09:52:08 -0500364 add_map_entry(nlsr->router_name);
365
akmhoque29c1db52012-09-07 14:47:43 -0500366 for(i=0;i<adj_lsdb_element;i++)
367 {
368 adj_lsa=e->data;
369 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
370 hashtb_next(e);
371 }
372
373 hashtb_end(e);
374
375}
376
377void
378add_map_entry(char *router)
379{
380
381 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
382
383 struct hashtb_enumerator ee;
384 struct hashtb_enumerator *e = &ee;
385 int res;
386
387 hashtb_start(nlsr->map, e);
388 res = hashtb_seek(e, router, strlen(router), 0);
389
390 if(res == HT_NEW_ENTRY)
391 {
392 me=e->data;
393 me->router=(char *)malloc(strlen(router)+1);
394 memset(me->router,0,strlen(router)+1);
395 memcpy(me->router,router,strlen(router));
396 me->mapping=0;
397 }
398
399 hashtb_end(e);
400
401}
402
403
404void
405add_adj_data_to_map(char *orig_router, char *body, int no_link)
406{
407 add_map_entry(orig_router);
408
409 int i=0;
410 char *lsa_data=(char *)malloc(strlen(body)+1);
411 memset( lsa_data,0,strlen(body)+1);
412 memcpy(lsa_data,body,strlen(body)+1);
413 char *sep="|";
414 char *rem;
415 char *rtr_id;
416 char *length;
417 char *face;
418 char *metric;
419
420 if(no_link >0 )
421 {
422 rtr_id=strtok_r(lsa_data,sep,&rem);
423 length=strtok_r(NULL,sep,&rem);
424 face=strtok_r(NULL,sep,&rem);
425 metric=strtok_r(NULL,sep,&rem);
426
427 add_map_entry(rtr_id);
428
429 for(i=1;i<no_link;i++)
430 {
431 rtr_id=strtok_r(NULL,sep,&rem);
432 length=strtok_r(NULL,sep,&rem);
433 face=strtok_r(NULL,sep,&rem);
434 metric=strtok_r(NULL,sep,&rem);
435
436 add_map_entry(rtr_id);
437
438 }
439 }
440
441 free(lsa_data);
442}
443
444int
445get_mapping_no(char *router)
446{
447 struct map_entry *me;
448
449 struct hashtb_enumerator ee;
450 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500451 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500452 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500453
akmhoque63152c62012-09-18 08:43:42 -0500454 int n = hashtb_n(nlsr->map);
455
456 if ( n < 1)
457 {
458 return NO_MAPPING_NUM;
459 }
460
akmhoque29c1db52012-09-07 14:47:43 -0500461 hashtb_start(nlsr->map, e);
462 res = hashtb_seek(e, router, strlen(router), 0);
463
464 if( res == HT_OLD_ENTRY )
465 {
466 me=e->data;
467 ret=me->mapping;
468 }
469 else if(res == HT_NEW_ENTRY)
470 {
471 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500472 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500473 }
474
475 hashtb_end(e);
476
477 return ret;
478
479}
480
akmhoquefbfd0982012-09-09 20:59:03 -0500481
482void
483add_rev_map_entry(long int mapping_number, char *router)
484{
485
486 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
487
488 struct hashtb_enumerator ee;
489 struct hashtb_enumerator *e = &ee;
490 int res;
491
492 hashtb_start(nlsr->rev_map, e);
493 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
494
495 if(res == HT_NEW_ENTRY)
496 {
497 me=e->data;
498 me->router=(char *)malloc(strlen(router)+1);
499 memset(me->router,0,strlen(router)+1);
500 memcpy(me->router,router,strlen(router));
501 me->mapping=mapping_number;
502 }
503
504 hashtb_end(e);
505
506}
507
508
509
510char *
511get_router_from_rev_map(long int mapping_number)
512{
513
514 struct map_entry *me;
515
516 struct hashtb_enumerator ee;
517 struct hashtb_enumerator *e = &ee;
518 int res;
519
520 hashtb_start(nlsr->rev_map, e);
521 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
522
523 if(res == HT_OLD_ENTRY)
524 {
525 me=e->data;
526 hashtb_end(e);
527 return me->router;
528 }
529 else if(res == HT_NEW_ENTRY)
530 {
531 hashtb_delete(e);
532 hashtb_end(e);
533 }
534
535 return NULL;
536}
537
538void
539print_rev_map(void)
540{
541 int i, map_element;
542 struct map_entry *me;
543
544 struct hashtb_enumerator ee;
545 struct hashtb_enumerator *e = &ee;
546
547 hashtb_start(nlsr->map, e);
548 map_element=hashtb_n(nlsr->map);
549
550 for(i=0;i<map_element;i++)
551 {
552 me=e->data;
553 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
554 hashtb_next(e);
555 }
556
557 hashtb_end(e);
558}
559
560
akmhoque29c1db52012-09-07 14:47:43 -0500561void
562assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
563{
564 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
565 int mapping_nbr_router;
566
567 int i;
568 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
569 memset( lsa_data,0,strlen(adj_lsa->body)+1);
570 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
571 char *sep="|";
572 char *rem;
573 char *rtr_id;
574 char *length;
575 char *face;
576 char *metric;
577
578 if(adj_lsa->no_link >0 )
579 {
580 rtr_id=strtok_r(lsa_data,sep,&rem);
581 length=strtok_r(NULL,sep,&rem);
582 face=strtok_r(NULL,sep,&rem);
583 metric=strtok_r(NULL,sep,&rem);
584
585 mapping_nbr_router=get_mapping_no(rtr_id);
586 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
587
588 for(i=1;i<adj_lsa->no_link;i++)
589 {
590 rtr_id=strtok_r(NULL,sep,&rem);
591 length=strtok_r(NULL,sep,&rem);
592 face=strtok_r(NULL,sep,&rem);
593 metric=strtok_r(NULL,sep,&rem);
594
595 mapping_nbr_router=get_mapping_no(rtr_id);
596 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
597
598 }
599 }
600
601 free(lsa_data);
602}
603
604void
605make_adj_matrix(int **adj_matrix,int map_element)
606{
607
608 init_adj_matrix(adj_matrix,map_element);
609
610 int i, adj_lsdb_element;
611 struct alsa *adj_lsa;
612
613 struct hashtb_enumerator ee;
614 struct hashtb_enumerator *e = &ee;
615
616 hashtb_start(nlsr->lsdb->adj_lsdb, e);
617 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
618
619 for(i=0;i<adj_lsdb_element;i++)
620 {
621 adj_lsa=e->data;
622 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
623 hashtb_next(e);
624 }
625
626 hashtb_end(e);
627
628}
629
630void
631init_adj_matrix(int **adj_matrix,int map_element)
632{
633 int i, j;
634 for(i=0;i<map_element;i++)
635 for(j=0;j<map_element;j++)
636 adj_matrix[i][j]=0;
637}
638
639void print_adj_matrix(int **adj_matrix, int map_element)
640{
641 int i, j;
642 for(i=0;i<map_element;i++)
643 {
644 for(j=0;j<map_element;j++)
645 printf("%d ",adj_matrix[i][j]);
646 printf("\n");
647 }
648}
649
akmhoquede61ba92012-09-20 22:19:12 -0500650
akmhoque3560cb62012-09-09 10:52:30 -0500651int
akmhoque9fe296b2012-09-24 09:52:08 -0500652get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
653{
654 int no_link=0;
655 int i;
656
657 for(i=0;i<V;i++)
658 {
659 if ( adj_matrix[S][i] > 0 )
660 {
661 no_link++;
662 }
663 }
664 return no_link;
665}
666
667void
668get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
669{
670 int i,j;
671 j=0;
672 for (i=0; i <V; i++)
673 {
674 if ( adj_matrix[S][i] > 0 )
675 {
676 links[j]=i;
677 link_costs[j]=adj_matrix[S][i];
678 j++;
679 }
680 }
681}
682
683void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
684{
685 int i;
686 for ( i = 0; i < V; i++ )
687 {
688 if ( i == link )
689 {
690 adj_matrix[S][i]=link_cost;
691 }
692 else
693 {
694 adj_matrix[S][i]=0;
695 }
696 }
697
698}
699
700int
akmhoquede61ba92012-09-20 22:19:12 -0500701get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500702{
703 struct routing_table_entry *rte;
704
705 struct hashtb_enumerator ee;
706 struct hashtb_enumerator *e = &ee;
707 int res,ret;
708
709 hashtb_start(nlsr->routing_table, e);
710 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
711
712 if( res == HT_OLD_ENTRY )
713 {
714 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500715 ret=hashtb_n(rte->face_list);
716 //nhl=rte->face_list;
717 }
718 else if(res == HT_NEW_ENTRY)
719 {
720 hashtb_delete(e);
721 ret=NO_NEXT_HOP;
722 }
723
724 hashtb_end(e);
725
726 return ret;
727}
728
729
730int
731get_next_hop(char *dest_router,int *faces, int *route_costs)
732{
733 struct routing_table_entry *rte;
734
735 struct hashtb_enumerator ee;
736 struct hashtb_enumerator *e = &ee;
737 int res,ret;
738
739 hashtb_start(nlsr->routing_table, e);
740 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
741
742 if( res == HT_OLD_ENTRY )
743 {
744 rte=e->data;
745 ret=hashtb_n(rte->face_list);
746 //nhl=rte->face_list;
747 int j,face_list_element;
748 struct face_list_entry *fle;
749
750 struct hashtb_enumerator eef;
751 struct hashtb_enumerator *ef = &eef;
752
753 hashtb_start(rte->face_list, ef);
754 face_list_element=hashtb_n(rte->face_list);
755 for(j=0;j<face_list_element;j++)
756 {
757 fle=ef->data;
758 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
759 faces[j]=fle->next_hop_face;
760 route_costs[j]=fle->route_cost;
761 hashtb_next(ef);
762 }
763 hashtb_end(ef);
764
akmhoque3560cb62012-09-09 10:52:30 -0500765 }
766 else if(res == HT_NEW_ENTRY)
767 {
768 hashtb_delete(e);
769 ret=NO_NEXT_HOP;
770 }
771
772 hashtb_end(e);
773
774 return ret;
775}
776
777void
778add_next_hop_router(char *dest_router)
779{
780 if ( strcmp(dest_router,nlsr->router_name)== 0)
781 {
782 return ;
783 }
784
785 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
786
787 struct hashtb_enumerator ee;
788 struct hashtb_enumerator *e = &ee;
789 int res;
790
791 hashtb_start(nlsr->routing_table, e);
792 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
793
794 if( res == HT_NEW_ENTRY )
795 {
796 rte=e->data;
797 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
798 memset(rte->dest_router,0,strlen(dest_router)+1);
799 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500800 //rte->next_hop_face=NO_NEXT_HOP;
801 struct hashtb_param param_fle = {0};
802 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
803
804 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500805 }
806 hashtb_end(e);
807
808}
809
810void
811add_next_hop_from_lsa_adj_body(char *body, int no_link)
812{
813
814 int i=0;
815 char *lsa_data=(char *)malloc(strlen(body)+1);
816 memset( lsa_data,0,strlen(body)+1);
817 memcpy(lsa_data,body,strlen(body)+1);
818 char *sep="|";
819 char *rem;
820 char *rtr_id;
821 char *length;
822 char *face;
823 char *metric;
824
825 if(no_link >0 )
826 {
827 rtr_id=strtok_r(lsa_data,sep,&rem);
828 length=strtok_r(NULL,sep,&rem);
829 face=strtok_r(NULL,sep,&rem);
830 metric=strtok_r(NULL,sep,&rem);
831
akmhoque3560cb62012-09-09 10:52:30 -0500832
833 add_next_hop_router(rtr_id);
834
835 for(i=1;i<no_link;i++)
836 {
837 rtr_id=strtok_r(NULL,sep,&rem);
838 length=strtok_r(NULL,sep,&rem);
839 face=strtok_r(NULL,sep,&rem);
840 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500841
842 add_next_hop_router(rtr_id);
843
844 }
845 }
846
847 free(lsa_data);
848
849
850}
851
852void
akmhoquede61ba92012-09-20 22:19:12 -0500853update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -0500854{
akmhoquede61ba92012-09-20 22:19:12 -0500855 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -0500856 struct routing_table_entry *rte;
857
858 struct hashtb_enumerator ee;
859 struct hashtb_enumerator *e = &ee;
860
861 hashtb_start(nlsr->routing_table, e);
862 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
863
864 if( res == HT_OLD_ENTRY )
865 {
866 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500867
868 struct hashtb_enumerator eef;
869 struct hashtb_enumerator *ef = &eef;
870
871 hashtb_start(rte->face_list, ef);
872 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
873 if( res1 == HT_NEW_ENTRY)
874 {
875 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
876 fle=ef->data;
877 fle->next_hop_face=next_hop_face;
878 fle->route_cost=route_cost;
879 }
880 else if ( res1 == HT_OLD_ENTRY )
881 {
882 struct face_list_entry *fle;
883 fle=ef->data;
884 fle->route_cost=route_cost;
885 }
886 hashtb_end(ef);
887
888 /*
889 //updating the face for the router prefix itself
890 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
891 {
892 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
893 }
894 if ( (next_hop_face != NO_FACE || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
895 {
896 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
897 }
898
akmhoqueffacaa82012-09-13 17:48:30 -0500899 rte->next_hop_face=next_hop_face;
akmhoquede61ba92012-09-20 22:19:12 -0500900 */
akmhoqueffacaa82012-09-13 17:48:30 -0500901 }
902 else if ( res == HT_OLD_ENTRY )
903 {
904 hashtb_delete(e);
905 }
906
907 hashtb_end(e);
908
909}
910
911void
akmhoque3560cb62012-09-09 10:52:30 -0500912print_routing_table(void)
913{
914 printf("\n");
915 printf("print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500916 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -0500917
918 struct routing_table_entry *rte;
919
920 struct hashtb_enumerator ee;
921 struct hashtb_enumerator *e = &ee;
922
923 hashtb_start(nlsr->routing_table, e);
924 rt_element=hashtb_n(nlsr->routing_table);
925
926 for(i=0;i<rt_element;i++)
927 {
928 printf("----------Routing Table Entry %d------------------\n",i+1);
929 rte=e->data;
930 printf(" Destination Router: %s \n",rte->dest_router);
akmhoquede61ba92012-09-20 22:19:12 -0500931 //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);
932
933 struct face_list_entry *fle;
934
935 struct hashtb_enumerator eef;
936 struct hashtb_enumerator *ef = &eef;
937
938 hashtb_start(rte->face_list, ef);
939 face_list_element=hashtb_n(rte->face_list);
940 if ( face_list_element <= 0 )
941 {
942 printf(" Face: No Face \n");
943 }
944 else
945 {
946 for(j=0;j<face_list_element;j++)
947 {
948 fle=ef->data;
949 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
950 hashtb_next(ef);
951 }
952 }
953 hashtb_end(ef);
954
akmhoque3560cb62012-09-09 10:52:30 -0500955 hashtb_next(e);
956 }
957
958 hashtb_end(e);
959
960 printf("\n");
961}
962
akmhoque63152c62012-09-18 08:43:42 -0500963
964int
965delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
966{
967 printf("delete_empty_rte called\n");
968 printf("Router: %s \n",(char *)ev->evdata);
969 if(flags == CCN_SCHEDULE_CANCEL)
970 {
971 return -1;
972 }
973
akmhoquede61ba92012-09-20 22:19:12 -0500974 //struct routing_table_entry *rte;
akmhoque63152c62012-09-18 08:43:42 -0500975 int res;
976 struct hashtb_enumerator ee;
977 struct hashtb_enumerator *e = &ee;
978
979 hashtb_start(nlsr->routing_table, e);
980 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
981
982 if ( res == HT_OLD_ENTRY )
983 {
akmhoquede61ba92012-09-20 22:19:12 -0500984 //rte=e->data;
985
986 /*
987
988 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP) && is_neighbor(ev->evdata)==0 )
989 {
990 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)ev->evdata, OP_UNREG, rte->next_hop_face);
991 }
992
993 */
akmhoque63152c62012-09-18 08:43:42 -0500994 hashtb_delete(e);
995 }
996 else if ( res == HT_NEW_ENTRY )
997 {
998 hashtb_delete(e);
999 }
1000
akmhoquede61ba92012-09-20 22:19:12 -05001001 print_routing_table();
1002
akmhoque63152c62012-09-18 08:43:42 -05001003 return 0;
1004}
1005
akmhoquede61ba92012-09-20 22:19:12 -05001006void
akmhoque3cced642012-09-24 16:20:20 -05001007clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001008{
1009 printf("clear_old_routing_table called\n");
1010 int i,rt_element;
1011
1012 struct routing_table_entry *rte;
1013
1014 struct hashtb_enumerator ee;
1015 struct hashtb_enumerator *e = &ee;
1016
1017 hashtb_start(nlsr->routing_table, e);
1018 rt_element=hashtb_n(nlsr->routing_table);
1019
1020 for(i=0;i<rt_element;i++)
1021 {
1022 rte=e->data;
1023 hashtb_destroy(&rte->face_list);
1024 struct hashtb_param param_fle = {0};
1025 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1026
1027 hashtb_next(e);
1028 }
1029
1030 hashtb_end(e);
1031}
1032
akmhoque63152c62012-09-18 08:43:42 -05001033
akmhoque3560cb62012-09-09 10:52:30 -05001034void
akmhoque3cced642012-09-24 16:20:20 -05001035do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001036{
akmhoque810a5b52012-09-09 16:53:14 -05001037 printf("do_old_routing_table_updates called\n");
1038 int i, rt_element;
1039 int mapping_no;
1040
1041 struct routing_table_entry *rte;
1042
1043 struct hashtb_enumerator ee;
1044 struct hashtb_enumerator *e = &ee;
1045
1046 hashtb_start(nlsr->routing_table, e);
1047 rt_element=hashtb_n(nlsr->routing_table);
1048
1049 for(i=0;i<rt_element;i++)
1050 {
1051 rte=e->data;
1052 mapping_no=get_mapping_no(rte->dest_router);
1053 if ( mapping_no == NO_MAPPING_NUM)
1054 {
akmhoquede61ba92012-09-20 22:19:12 -05001055 delete_orig_router_from_npt(rte->dest_router);
akmhoque63152c62012-09-18 08:43:42 -05001056 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1057 memset(router,0,strlen(rte->dest_router)+1);
1058 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1059 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque810a5b52012-09-09 16:53:14 -05001060 }
1061 hashtb_next(e);
1062 }
1063
1064 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001065}
akmhoque29c1db52012-09-07 14:47:43 -05001066
akmhoquede61ba92012-09-20 22:19:12 -05001067
1068
akmhoquefbfd0982012-09-09 20:59:03 -05001069void
akmhoquede61ba92012-09-20 22:19:12 -05001070update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001071{
1072 printf("update_routing_table_with_new_route called\n");
1073 int i, map_element;
1074 struct map_entry *me;
1075
1076 struct hashtb_enumerator ee;
1077 struct hashtb_enumerator *e = &ee;
1078
1079 hashtb_start(nlsr->rev_map, e);
1080 map_element=hashtb_n(nlsr->rev_map);
1081
1082 for(i=0;i<map_element;i++)
1083 {
1084 me=e->data;
1085 if(me->mapping != source)
1086 {
1087
1088 char *orig_router=get_router_from_rev_map(me->mapping);
1089 if (orig_router != NULL )
1090 {
1091 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
1092 //printf(" Next hop router Num: %d ",next_hop_router_num);
1093 if ( next_hop_router_num == NO_NEXT_HOP )
1094 {
akmhoquede61ba92012-09-20 22:19:12 -05001095 //update_npt_with_new_route(orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001096 printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1097 }
1098 else
1099 {
1100 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
1101 //printf("Next hop router name: %s \n",next_hop_router);
1102 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001103 //update_npt_with_new_route(orig_router,next_hop_face);
1104 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoquefbfd0982012-09-09 20:59:03 -05001105 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1106
1107 }
1108 }
1109 }
1110 hashtb_next(e);
1111 }
1112
1113 hashtb_end(e);
1114}
1115
akmhoquede61ba92012-09-20 22:19:12 -05001116int
1117does_face_exist_for_router(char *dest_router, int face_id)
1118{
1119 int ret=0;
1120
1121 int res,res1;
1122 struct routing_table_entry *rte;
1123
1124 struct hashtb_enumerator ee;
1125 struct hashtb_enumerator *e = &ee;
1126
1127 hashtb_start(nlsr->routing_table, e);
1128 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1129
1130 if( res == HT_OLD_ENTRY )
1131 {
1132 rte=e->data;
1133 struct hashtb_enumerator eef;
1134 struct hashtb_enumerator *ef = &eef;
1135
1136 hashtb_start(rte->face_list, ef);
1137 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1138 if( res1 == HT_OLD_ENTRY)
1139 {
1140 ret=1;
1141 }
1142 else if ( res1 == HT_OLD_ENTRY )
1143 {
1144 hashtb_delete(ef);
1145 }
1146 hashtb_end(ef);
1147 }
1148 else if( res == HT_NEW_ENTRY )
1149 {
1150 hashtb_delete(e);
1151 }
1152
1153 hashtb_end(e);
1154
1155 return ret;
1156}