blob: 0fbe8fd3ce85e3164e1ddee82b0619d3088c454c [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"
akmhoque1771c412012-11-09 13:06:08 -060028#include "utility.h"
akmhoque29c1db52012-09-07 14:47:43 -050029
30int
31route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
32{
akmhoqueffacaa82012-09-13 17:48:30 -050033
34 if(flags == CCN_SCHEDULE_CANCEL)
35 {
36 return -1;
37 }
38
39 nlsr_lock();
40
akmhoque1771c412012-11-09 13:06:08 -060041 if ( nlsr->debugging )
42 printf("route_calculate called\n");
43 if ( nlsr->detailed_logging )
44 writeLogg(__FILE__,__FUNCTION__,__LINE__,"route_calculate called\n");
akmhoque29c1db52012-09-07 14:47:43 -050045
46 if( ! nlsr->is_build_adj_lsa_sheduled )
47 {
48 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050049 print_routing_table();
50 print_npt();
51
akmhoque29c1db52012-09-07 14:47:43 -050052 struct hashtb_param param_me = {0};
53 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoquefbfd0982012-09-09 20:59:03 -050054 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoque29c1db52012-09-07 14:47:43 -050055 make_map();
56 assign_mapping_number();
57 print_map();
akmhoquefbfd0982012-09-09 20:59:03 -050058 print_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -050059
akmhoque3560cb62012-09-09 10:52:30 -050060 do_old_routing_table_updates();
akmhoquede61ba92012-09-20 22:19:12 -050061 clear_old_routing_table();
62 print_routing_table();
akmhoquea30cb772012-10-07 09:50:34 -050063 print_npt();
akmhoque3560cb62012-09-09 10:52:30 -050064
akmhoque29c1db52012-09-07 14:47:43 -050065 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 print_adj_matrix(adj_matrix,map_element);
75
76 long int source=get_mapping_no(nlsr->router_name);
77 long int *parent=(long int *)malloc(map_element * sizeof(long int));
akmhoquede61ba92012-09-20 22:19:12 -050078 long int *dist=(long int *)malloc(map_element * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -050079
akmhoque9fe296b2012-09-24 09:52:08 -050080 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoque29c1db52012-09-07 14:47:43 -050081
akmhoque0820a2e2012-09-24 20:23:01 -050082 if ( (num_link == 0) || (nlsr->multi_path_face_num <= 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050083 {
84 calculate_path(adj_matrix,parent,dist, map_element, source);
85 print_all_path_from_source(parent,source);
86 print_all_next_hop(parent,source);
87 update_routing_table_with_new_route(parent, dist,source);
88 }
akmhoque59d38c82012-09-24 20:24:03 -050089 else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050090 {
91 long int *links=(long int *)malloc(num_link*sizeof(long int));
92 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
93 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
94 for ( i=0 ; i < num_link; i++)
95 {
96 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
97 calculate_path(adj_matrix,parent,dist, map_element, source);
98 print_all_path_from_source(parent,source);
99 print_all_next_hop(parent,source);
100 update_routing_table_with_new_route(parent, dist,source);
101 }
akmhoque29c1db52012-09-07 14:47:43 -0500102
akmhoque9fe296b2012-09-24 09:52:08 -0500103 free(links);
104 free(link_costs);
105 }
akmhoquede61ba92012-09-20 22:19:12 -0500106
107 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -0500108
akmhoqueffacaa82012-09-13 17:48:30 -0500109 print_routing_table();
110 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -0500111
akmhoque29c1db52012-09-07 14:47:43 -0500112 for(i = 0; i < map_element; i++)
113 {
114 free(adj_matrix[i]);
115 }
116 free(parent);
akmhoquede61ba92012-09-20 22:19:12 -0500117 free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500118 free(adj_matrix);
119 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -0500120 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -0500121
122 }
123 nlsr->is_route_calculation_scheduled=0;
124
akmhoqueffacaa82012-09-13 17:48:30 -0500125 nlsr_unlock();
126
akmhoque29c1db52012-09-07 14:47:43 -0500127 return 0;
128}
129
130void
akmhoquede61ba92012-09-20 22:19:12 -0500131calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500132{
133 int i;
134 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500135 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500136 long int *Q=(long int *)malloc(V * sizeof(long int));
137 long int head=0;
138 /* Initial the Parent */
139 for (i = 0 ; i < V; i++)
140 {
141 parent[i]=EMPTY_PARENT;
142 dist[i]=INF_DISTANCE;
143 Q[i]=i;
144 }
145
akmhoque9fe296b2012-09-24 09:52:08 -0500146 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500147 {
akmhoque9fe296b2012-09-24 09:52:08 -0500148 dist[S]=0;
149 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500150
akmhoque9fe296b2012-09-24 09:52:08 -0500151 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500152 {
akmhoque9fe296b2012-09-24 09:52:08 -0500153 u=Q[head];
154 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500155 {
akmhoque9fe296b2012-09-24 09:52:08 -0500156 break;
157 }
158
159 for(v=0 ; v <V; v++)
160 {
161 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500162 {
akmhoque9fe296b2012-09-24 09:52:08 -0500163 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500164 {
akmhoque9fe296b2012-09-24 09:52:08 -0500165
166 if( dist[u] + adj_matrix[u][v] < dist[v])
167 {
168 dist[v]=dist[u] + adj_matrix[u][v] ;
169 parent[v]=u;
170 }
171
172 }
akmhoque29c1db52012-09-07 14:47:43 -0500173
174 }
175
176 }
177
akmhoque9fe296b2012-09-24 09:52:08 -0500178 head++;
179 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500180 }
akmhoque29c1db52012-09-07 14:47:43 -0500181 }
akmhoque29c1db52012-09-07 14:47:43 -0500182 free(Q);
akmhoquede61ba92012-09-20 22:19:12 -0500183 //free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500184}
185
186void
187print_all_path_from_source(long int *parent,long int source)
188{
189 int i, map_element;
190 struct map_entry *me;
191
192 struct hashtb_enumerator ee;
193 struct hashtb_enumerator *e = &ee;
194
195 hashtb_start(nlsr->map, e);
196 map_element=hashtb_n(nlsr->map);
197
akmhoque9fe296b2012-09-24 09:52:08 -0500198 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500199 {
akmhoque9fe296b2012-09-24 09:52:08 -0500200 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500201 {
akmhoque9fe296b2012-09-24 09:52:08 -0500202 me=e->data;
203 if(me->mapping != source)
204 {
205 print_path(parent,(long int)me->mapping);
206 printf("\n");
207 }
208 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500209 }
akmhoque29c1db52012-09-07 14:47:43 -0500210 }
akmhoque29c1db52012-09-07 14:47:43 -0500211 hashtb_end(e);
212
213}
214
akmhoquefbfd0982012-09-09 20:59:03 -0500215void
216print_all_next_hop(long int *parent,long int source)
217{
218 int i, map_element;
219 struct map_entry *me;
220
221 struct hashtb_enumerator ee;
222 struct hashtb_enumerator *e = &ee;
223
224 hashtb_start(nlsr->map, e);
225 map_element=hashtb_n(nlsr->map);
226
227 for(i=0;i<map_element;i++)
228 {
229 me=e->data;
230 if(me->mapping != source)
231 {
akmhoque1771c412012-11-09 13:06:08 -0600232 if ( nlsr->debugging )
233 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
234 if ( nlsr->detailed_logging )
235 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
akmhoquefbfd0982012-09-09 20:59:03 -0500236 }
237 hashtb_next(e);
238 }
239
240 hashtb_end(e);
241
242}
243
244long int
245get_next_hop_from_calculation(long int *parent, long int dest,long int source)
246{
247 long int next_hop;
248 while ( parent[dest] != EMPTY_PARENT )
249 {
250 next_hop=dest;
251 dest=parent[dest];
252
253 }
254
255 if ( dest != source )
256 {
257 next_hop=NO_NEXT_HOP;
258 }
259
260 return next_hop;
261
262}
263
akmhoque29c1db52012-09-07 14:47:43 -0500264void
265print_path(long int *parent, long int dest)
266{
267 if (parent[dest] != EMPTY_PARENT )
268 print_path(parent,parent[dest]);
269
270 printf(" %ld",dest);
271}
272
273int
274is_not_explored(long int *Q, long int u,long int start, long int element)
275{
276 int ret=0;
277 long int i;
278 for(i=start; i< element; i++)
279 {
280 if ( Q[i] == u )
281 {
282 ret=1;
283 break;
284 }
285 }
286 return ret;
287}
288
289void
290sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
291{
292 long int i,j;
293 long int temp_u;
294
295 for ( i=start ; i < element ; i ++)
296 {
297 for( j=i+1; j<element; j ++)
298 {
299 if (dist[Q[j]] < dist[Q[i]])
300 {
301 temp_u=Q[j];
302 Q[j]=Q[i];
303 Q[i]=temp_u;
304 }
305 }
306 }
307
308}
309
310void
311print_map(void)
312{
313 int i, map_element;
314 struct map_entry *me;
315
316 struct hashtb_enumerator ee;
317 struct hashtb_enumerator *e = &ee;
318
319 hashtb_start(nlsr->map, e);
320 map_element=hashtb_n(nlsr->map);
321
322 for(i=0;i<map_element;i++)
323 {
324 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600325 if ( nlsr->debugging )
326 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
327 if ( nlsr->detailed_logging )
328 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
akmhoque29c1db52012-09-07 14:47:43 -0500329 hashtb_next(e);
330 }
331
332 hashtb_end(e);
333}
334
335
336void
337assign_mapping_number(void)
338{
339 int i, map_element;
340 struct map_entry *me;
341
342 struct hashtb_enumerator ee;
343 struct hashtb_enumerator *e = &ee;
344
345 hashtb_start(nlsr->map, e);
346 map_element=hashtb_n(nlsr->map);
347
348 for(i=0;i<map_element;i++)
349 {
350 me=e->data;
351 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500352 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500353 hashtb_next(e);
354 }
355
356 hashtb_end(e);
357}
358
359void
360make_map(void)
361{
362
363
364 int i, adj_lsdb_element;
365 struct alsa *adj_lsa;
366
367 struct hashtb_enumerator ee;
368 struct hashtb_enumerator *e = &ee;
369
370 hashtb_start(nlsr->lsdb->adj_lsdb, e);
371 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
372
akmhoque9fe296b2012-09-24 09:52:08 -0500373 add_map_entry(nlsr->router_name);
374
akmhoque29c1db52012-09-07 14:47:43 -0500375 for(i=0;i<adj_lsdb_element;i++)
376 {
377 adj_lsa=e->data;
378 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
379 hashtb_next(e);
380 }
381
382 hashtb_end(e);
383
384}
385
386void
387add_map_entry(char *router)
388{
389
390 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
391
392 struct hashtb_enumerator ee;
393 struct hashtb_enumerator *e = &ee;
394 int res;
395
396 hashtb_start(nlsr->map, e);
397 res = hashtb_seek(e, router, strlen(router), 0);
398
399 if(res == HT_NEW_ENTRY)
400 {
401 me=e->data;
402 me->router=(char *)malloc(strlen(router)+1);
403 memset(me->router,0,strlen(router)+1);
404 memcpy(me->router,router,strlen(router));
405 me->mapping=0;
406 }
407
408 hashtb_end(e);
409
410}
411
412
413void
414add_adj_data_to_map(char *orig_router, char *body, int no_link)
415{
416 add_map_entry(orig_router);
417
418 int i=0;
419 char *lsa_data=(char *)malloc(strlen(body)+1);
420 memset( lsa_data,0,strlen(body)+1);
421 memcpy(lsa_data,body,strlen(body)+1);
422 char *sep="|";
423 char *rem;
424 char *rtr_id;
425 char *length;
426 char *face;
427 char *metric;
428
429 if(no_link >0 )
430 {
431 rtr_id=strtok_r(lsa_data,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 for(i=1;i<no_link;i++)
439 {
440 rtr_id=strtok_r(NULL,sep,&rem);
441 length=strtok_r(NULL,sep,&rem);
442 face=strtok_r(NULL,sep,&rem);
443 metric=strtok_r(NULL,sep,&rem);
444
445 add_map_entry(rtr_id);
446
447 }
448 }
449
450 free(lsa_data);
451}
452
453int
454get_mapping_no(char *router)
455{
456 struct map_entry *me;
457
458 struct hashtb_enumerator ee;
459 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500460 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500461 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500462
akmhoque63152c62012-09-18 08:43:42 -0500463 int n = hashtb_n(nlsr->map);
464
465 if ( n < 1)
466 {
467 return NO_MAPPING_NUM;
468 }
469
akmhoque29c1db52012-09-07 14:47:43 -0500470 hashtb_start(nlsr->map, e);
471 res = hashtb_seek(e, router, strlen(router), 0);
472
473 if( res == HT_OLD_ENTRY )
474 {
475 me=e->data;
476 ret=me->mapping;
477 }
478 else if(res == HT_NEW_ENTRY)
479 {
480 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500481 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500482 }
483
484 hashtb_end(e);
485
486 return ret;
487
488}
489
akmhoquefbfd0982012-09-09 20:59:03 -0500490
491void
492add_rev_map_entry(long int mapping_number, char *router)
493{
494
495 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
496
497 struct hashtb_enumerator ee;
498 struct hashtb_enumerator *e = &ee;
499 int res;
500
501 hashtb_start(nlsr->rev_map, e);
502 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
503
504 if(res == HT_NEW_ENTRY)
505 {
506 me=e->data;
507 me->router=(char *)malloc(strlen(router)+1);
508 memset(me->router,0,strlen(router)+1);
509 memcpy(me->router,router,strlen(router));
510 me->mapping=mapping_number;
511 }
512
513 hashtb_end(e);
514
515}
516
517
518
519char *
520get_router_from_rev_map(long int mapping_number)
521{
522
523 struct map_entry *me;
524
525 struct hashtb_enumerator ee;
526 struct hashtb_enumerator *e = &ee;
527 int res;
528
529 hashtb_start(nlsr->rev_map, e);
530 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
531
532 if(res == HT_OLD_ENTRY)
533 {
534 me=e->data;
535 hashtb_end(e);
536 return me->router;
537 }
538 else if(res == HT_NEW_ENTRY)
539 {
540 hashtb_delete(e);
541 hashtb_end(e);
542 }
543
544 return NULL;
545}
546
547void
548print_rev_map(void)
549{
550 int i, map_element;
551 struct map_entry *me;
552
553 struct hashtb_enumerator ee;
554 struct hashtb_enumerator *e = &ee;
555
556 hashtb_start(nlsr->map, e);
557 map_element=hashtb_n(nlsr->map);
558
559 for(i=0;i<map_element;i++)
560 {
561 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600562 if ( nlsr->debugging )
563 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
564 if ( nlsr->detailed_logging )
565 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
akmhoquefbfd0982012-09-09 20:59:03 -0500566 hashtb_next(e);
567 }
568
569 hashtb_end(e);
570}
571
572
akmhoque29c1db52012-09-07 14:47:43 -0500573void
574assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
575{
576 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
577 int mapping_nbr_router;
578
579 int i;
580 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
581 memset( lsa_data,0,strlen(adj_lsa->body)+1);
582 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
583 char *sep="|";
584 char *rem;
585 char *rtr_id;
586 char *length;
587 char *face;
588 char *metric;
589
590 if(adj_lsa->no_link >0 )
591 {
592 rtr_id=strtok_r(lsa_data,sep,&rem);
593 length=strtok_r(NULL,sep,&rem);
594 face=strtok_r(NULL,sep,&rem);
595 metric=strtok_r(NULL,sep,&rem);
596
597 mapping_nbr_router=get_mapping_no(rtr_id);
598 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
599
600 for(i=1;i<adj_lsa->no_link;i++)
601 {
602 rtr_id=strtok_r(NULL,sep,&rem);
603 length=strtok_r(NULL,sep,&rem);
604 face=strtok_r(NULL,sep,&rem);
605 metric=strtok_r(NULL,sep,&rem);
606
607 mapping_nbr_router=get_mapping_no(rtr_id);
608 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
609
610 }
611 }
612
613 free(lsa_data);
614}
615
616void
617make_adj_matrix(int **adj_matrix,int map_element)
618{
619
620 init_adj_matrix(adj_matrix,map_element);
621
622 int i, adj_lsdb_element;
623 struct alsa *adj_lsa;
624
625 struct hashtb_enumerator ee;
626 struct hashtb_enumerator *e = &ee;
627
628 hashtb_start(nlsr->lsdb->adj_lsdb, e);
629 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
630
631 for(i=0;i<adj_lsdb_element;i++)
632 {
633 adj_lsa=e->data;
634 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
635 hashtb_next(e);
636 }
637
638 hashtb_end(e);
639
640}
641
642void
643init_adj_matrix(int **adj_matrix,int map_element)
644{
645 int i, j;
646 for(i=0;i<map_element;i++)
647 for(j=0;j<map_element;j++)
648 adj_matrix[i][j]=0;
649}
650
651void print_adj_matrix(int **adj_matrix, int map_element)
652{
653 int i, j;
654 for(i=0;i<map_element;i++)
655 {
656 for(j=0;j<map_element;j++)
657 printf("%d ",adj_matrix[i][j]);
658 printf("\n");
659 }
660}
661
akmhoquede61ba92012-09-20 22:19:12 -0500662
akmhoque3560cb62012-09-09 10:52:30 -0500663int
akmhoque9fe296b2012-09-24 09:52:08 -0500664get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
665{
666 int no_link=0;
667 int i;
668
669 for(i=0;i<V;i++)
670 {
671 if ( adj_matrix[S][i] > 0 )
672 {
673 no_link++;
674 }
675 }
676 return no_link;
677}
678
679void
680get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
681{
682 int i,j;
683 j=0;
684 for (i=0; i <V; i++)
685 {
686 if ( adj_matrix[S][i] > 0 )
687 {
688 links[j]=i;
689 link_costs[j]=adj_matrix[S][i];
690 j++;
691 }
692 }
693}
694
695void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
696{
697 int i;
698 for ( i = 0; i < V; i++ )
699 {
700 if ( i == link )
701 {
702 adj_matrix[S][i]=link_cost;
703 }
704 else
705 {
706 adj_matrix[S][i]=0;
707 }
708 }
709
710}
711
712int
akmhoquede61ba92012-09-20 22:19:12 -0500713get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500714{
715 struct routing_table_entry *rte;
716
717 struct hashtb_enumerator ee;
718 struct hashtb_enumerator *e = &ee;
719 int res,ret;
720
721 hashtb_start(nlsr->routing_table, e);
722 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
723
724 if( res == HT_OLD_ENTRY )
725 {
726 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500727 ret=hashtb_n(rte->face_list);
728 //nhl=rte->face_list;
729 }
730 else if(res == HT_NEW_ENTRY)
731 {
732 hashtb_delete(e);
733 ret=NO_NEXT_HOP;
734 }
735
736 hashtb_end(e);
737
738 return ret;
739}
740
741
742int
743get_next_hop(char *dest_router,int *faces, int *route_costs)
744{
745 struct routing_table_entry *rte;
746
747 struct hashtb_enumerator ee;
748 struct hashtb_enumerator *e = &ee;
749 int res,ret;
750
751 hashtb_start(nlsr->routing_table, e);
752 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
753
754 if( res == HT_OLD_ENTRY )
755 {
756 rte=e->data;
757 ret=hashtb_n(rte->face_list);
758 //nhl=rte->face_list;
759 int j,face_list_element;
760 struct face_list_entry *fle;
761
762 struct hashtb_enumerator eef;
763 struct hashtb_enumerator *ef = &eef;
764
765 hashtb_start(rte->face_list, ef);
766 face_list_element=hashtb_n(rte->face_list);
767 for(j=0;j<face_list_element;j++)
768 {
769 fle=ef->data;
770 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
771 faces[j]=fle->next_hop_face;
772 route_costs[j]=fle->route_cost;
773 hashtb_next(ef);
774 }
775 hashtb_end(ef);
776
akmhoque3560cb62012-09-09 10:52:30 -0500777 }
778 else if(res == HT_NEW_ENTRY)
779 {
780 hashtb_delete(e);
781 ret=NO_NEXT_HOP;
782 }
783
784 hashtb_end(e);
785
786 return ret;
787}
788
789void
790add_next_hop_router(char *dest_router)
791{
792 if ( strcmp(dest_router,nlsr->router_name)== 0)
793 {
794 return ;
795 }
796
797 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
798
799 struct hashtb_enumerator ee;
800 struct hashtb_enumerator *e = &ee;
801 int res;
802
803 hashtb_start(nlsr->routing_table, e);
804 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
805
806 if( res == HT_NEW_ENTRY )
807 {
808 rte=e->data;
809 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
810 memset(rte->dest_router,0,strlen(dest_router)+1);
811 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500812 //rte->next_hop_face=NO_NEXT_HOP;
813 struct hashtb_param param_fle = {0};
814 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
815
816 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500817 }
818 hashtb_end(e);
819
820}
821
822void
823add_next_hop_from_lsa_adj_body(char *body, int no_link)
824{
825
826 int i=0;
827 char *lsa_data=(char *)malloc(strlen(body)+1);
828 memset( lsa_data,0,strlen(body)+1);
829 memcpy(lsa_data,body,strlen(body)+1);
830 char *sep="|";
831 char *rem;
832 char *rtr_id;
833 char *length;
834 char *face;
835 char *metric;
836
837 if(no_link >0 )
838 {
839 rtr_id=strtok_r(lsa_data,sep,&rem);
840 length=strtok_r(NULL,sep,&rem);
841 face=strtok_r(NULL,sep,&rem);
842 metric=strtok_r(NULL,sep,&rem);
843
akmhoque3560cb62012-09-09 10:52:30 -0500844
845 add_next_hop_router(rtr_id);
846
847 for(i=1;i<no_link;i++)
848 {
849 rtr_id=strtok_r(NULL,sep,&rem);
850 length=strtok_r(NULL,sep,&rem);
851 face=strtok_r(NULL,sep,&rem);
852 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500853
854 add_next_hop_router(rtr_id);
855
856 }
857 }
858
859 free(lsa_data);
860
861
862}
863
864void
akmhoquede61ba92012-09-20 22:19:12 -0500865update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -0500866{
akmhoquede61ba92012-09-20 22:19:12 -0500867 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -0500868 struct routing_table_entry *rte;
869
870 struct hashtb_enumerator ee;
871 struct hashtb_enumerator *e = &ee;
872
873 hashtb_start(nlsr->routing_table, e);
874 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
875
876 if( res == HT_OLD_ENTRY )
877 {
878 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500879
880 struct hashtb_enumerator eef;
881 struct hashtb_enumerator *ef = &eef;
882
883 hashtb_start(rte->face_list, ef);
884 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
885 if( res1 == HT_NEW_ENTRY)
886 {
887 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
888 fle=ef->data;
889 fle->next_hop_face=next_hop_face;
890 fle->route_cost=route_cost;
891 }
892 else if ( res1 == HT_OLD_ENTRY )
893 {
894 struct face_list_entry *fle;
895 fle=ef->data;
896 fle->route_cost=route_cost;
897 }
898 hashtb_end(ef);
899
900 /*
901 //updating the face for the router prefix itself
902 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
903 {
904 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
905 }
906 if ( (next_hop_face != NO_FACE || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
907 {
908 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
909 }
910
akmhoqueffacaa82012-09-13 17:48:30 -0500911 rte->next_hop_face=next_hop_face;
akmhoquede61ba92012-09-20 22:19:12 -0500912 */
akmhoqueffacaa82012-09-13 17:48:30 -0500913 }
914 else if ( res == HT_OLD_ENTRY )
915 {
916 hashtb_delete(e);
917 }
918
919 hashtb_end(e);
920
921}
922
923void
akmhoque3560cb62012-09-09 10:52:30 -0500924print_routing_table(void)
925{
akmhoque1771c412012-11-09 13:06:08 -0600926 if ( nlsr->debugging )
927 printf("print_routing_table called\n");
928 if ( nlsr->detailed_logging )
929 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500930 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -0500931
932 struct routing_table_entry *rte;
933
934 struct hashtb_enumerator ee;
935 struct hashtb_enumerator *e = &ee;
936
937 hashtb_start(nlsr->routing_table, e);
938 rt_element=hashtb_n(nlsr->routing_table);
939
940 for(i=0;i<rt_element;i++)
941 {
akmhoque1771c412012-11-09 13:06:08 -0600942 if ( nlsr->debugging )
943 printf("----------Routing Table Entry %d------------------\n",i+1);
944 if ( nlsr->detailed_logging )
945 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
946
akmhoque3560cb62012-09-09 10:52:30 -0500947 rte=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600948
949 if ( nlsr->debugging )
950 printf(" Destination Router: %s \n",rte->dest_router);
951 if ( nlsr->detailed_logging )
952 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
953
954
akmhoquede61ba92012-09-20 22:19:12 -0500955 //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);
956
957 struct face_list_entry *fle;
958
959 struct hashtb_enumerator eef;
960 struct hashtb_enumerator *ef = &eef;
961
962 hashtb_start(rte->face_list, ef);
963 face_list_element=hashtb_n(rte->face_list);
964 if ( face_list_element <= 0 )
965 {
akmhoque1771c412012-11-09 13:06:08 -0600966 if ( nlsr->debugging )
967 printf(" Face: No Face \n");
968 if ( nlsr->detailed_logging )
969 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -0500970 }
971 else
972 {
973 for(j=0;j<face_list_element;j++)
974 {
975 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -0600976 if ( nlsr->debugging )
977 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
978 if ( nlsr->detailed_logging )
979 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -0500980 hashtb_next(ef);
981 }
982 }
983 hashtb_end(ef);
984
akmhoque3560cb62012-09-09 10:52:30 -0500985 hashtb_next(e);
986 }
987
988 hashtb_end(e);
989
990 printf("\n");
991}
992
akmhoque63152c62012-09-18 08:43:42 -0500993
994int
995delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
996{
akmhoque1771c412012-11-09 13:06:08 -0600997 if ( nlsr->debugging )
998 {
999 printf("delete_empty_rte called\n");
1000 printf("Router: %s \n",(char *)ev->evdata);
1001 }
1002 if ( nlsr->detailed_logging )
1003 {
1004 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1005 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
1006 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
1007 }
1008
akmhoque63152c62012-09-18 08:43:42 -05001009 if(flags == CCN_SCHEDULE_CANCEL)
1010 {
1011 return -1;
1012 }
akmhoque63152c62012-09-18 08:43:42 -05001013 int res;
1014 struct hashtb_enumerator ee;
1015 struct hashtb_enumerator *e = &ee;
1016
1017 hashtb_start(nlsr->routing_table, e);
1018 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1019
1020 if ( res == HT_OLD_ENTRY )
1021 {
1022 hashtb_delete(e);
1023 }
1024 else if ( res == HT_NEW_ENTRY )
1025 {
1026 hashtb_delete(e);
1027 }
1028
akmhoquede61ba92012-09-20 22:19:12 -05001029 print_routing_table();
1030
akmhoque63152c62012-09-18 08:43:42 -05001031 return 0;
1032}
1033
akmhoquede61ba92012-09-20 22:19:12 -05001034void
akmhoque3cced642012-09-24 16:20:20 -05001035clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001036{
akmhoque1771c412012-11-09 13:06:08 -06001037 if ( nlsr->debugging )
1038 printf("clear_old_routing_table called\n");
1039 if ( nlsr->detailed_logging )
1040 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001041 int i,rt_element;
1042
1043 struct routing_table_entry *rte;
1044
1045 struct hashtb_enumerator ee;
1046 struct hashtb_enumerator *e = &ee;
1047
1048 hashtb_start(nlsr->routing_table, e);
1049 rt_element=hashtb_n(nlsr->routing_table);
1050
1051 for(i=0;i<rt_element;i++)
1052 {
1053 rte=e->data;
1054 hashtb_destroy(&rte->face_list);
1055 struct hashtb_param param_fle = {0};
1056 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1057
1058 hashtb_next(e);
1059 }
1060
1061 hashtb_end(e);
1062}
1063
akmhoque63152c62012-09-18 08:43:42 -05001064
akmhoque3560cb62012-09-09 10:52:30 -05001065void
akmhoque3cced642012-09-24 16:20:20 -05001066do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001067{
akmhoque1771c412012-11-09 13:06:08 -06001068 if ( nlsr->debugging )
1069 printf("do_old_routing_table_updates called\n");
1070 if ( nlsr->detailed_logging )
1071 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1072
akmhoque810a5b52012-09-09 16:53:14 -05001073 int i, rt_element;
1074 int mapping_no;
1075
1076 struct routing_table_entry *rte;
1077
1078 struct hashtb_enumerator ee;
1079 struct hashtb_enumerator *e = &ee;
1080
1081 hashtb_start(nlsr->routing_table, e);
1082 rt_element=hashtb_n(nlsr->routing_table);
1083
1084 for(i=0;i<rt_element;i++)
1085 {
1086 rte=e->data;
1087 mapping_no=get_mapping_no(rte->dest_router);
1088 if ( mapping_no == NO_MAPPING_NUM)
1089 {
akmhoquede61ba92012-09-20 22:19:12 -05001090 delete_orig_router_from_npt(rte->dest_router);
akmhoque63152c62012-09-18 08:43:42 -05001091 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1092 memset(router,0,strlen(rte->dest_router)+1);
1093 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1094 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque810a5b52012-09-09 16:53:14 -05001095 }
1096 hashtb_next(e);
1097 }
1098
1099 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001100}
akmhoque29c1db52012-09-07 14:47:43 -05001101
akmhoquede61ba92012-09-20 22:19:12 -05001102
1103
akmhoquefbfd0982012-09-09 20:59:03 -05001104void
akmhoquede61ba92012-09-20 22:19:12 -05001105update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001106{
akmhoque1771c412012-11-09 13:06:08 -06001107 if ( nlsr->debugging )
1108 printf("update_routing_table_with_new_route called\n");
1109 if ( nlsr->detailed_logging )
1110 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001111 int i, map_element;
1112 struct map_entry *me;
1113
1114 struct hashtb_enumerator ee;
1115 struct hashtb_enumerator *e = &ee;
1116
1117 hashtb_start(nlsr->rev_map, e);
1118 map_element=hashtb_n(nlsr->rev_map);
1119
1120 for(i=0;i<map_element;i++)
1121 {
1122 me=e->data;
1123 if(me->mapping != source)
1124 {
1125
1126 char *orig_router=get_router_from_rev_map(me->mapping);
1127 if (orig_router != NULL )
1128 {
1129 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
1130 //printf(" Next hop router Num: %d ",next_hop_router_num);
1131 if ( next_hop_router_num == NO_NEXT_HOP )
1132 {
akmhoquede61ba92012-09-20 22:19:12 -05001133 //update_npt_with_new_route(orig_router,NO_FACE);
akmhoque1771c412012-11-09 13:06:08 -06001134 if ( nlsr->debugging )
1135 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1136 if ( nlsr->detailed_logging )
1137 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001138 }
1139 else
1140 {
1141 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
1142 //printf("Next hop router name: %s \n",next_hop_router);
1143 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001144 //update_npt_with_new_route(orig_router,next_hop_face);
1145 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoque1771c412012-11-09 13:06:08 -06001146 if ( nlsr->debugging )
1147 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1148 if ( nlsr->detailed_logging )
1149 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1150
akmhoquefbfd0982012-09-09 20:59:03 -05001151
1152 }
1153 }
1154 }
1155 hashtb_next(e);
1156 }
1157
1158 hashtb_end(e);
1159}
1160
akmhoquede61ba92012-09-20 22:19:12 -05001161int
1162does_face_exist_for_router(char *dest_router, int face_id)
1163{
1164 int ret=0;
1165
1166 int res,res1;
1167 struct routing_table_entry *rte;
1168
1169 struct hashtb_enumerator ee;
1170 struct hashtb_enumerator *e = &ee;
1171
1172 hashtb_start(nlsr->routing_table, e);
1173 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1174
1175 if( res == HT_OLD_ENTRY )
1176 {
1177 rte=e->data;
1178 struct hashtb_enumerator eef;
1179 struct hashtb_enumerator *ef = &eef;
1180
1181 hashtb_start(rte->face_list, ef);
1182 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1183 if( res1 == HT_OLD_ENTRY)
1184 {
1185 ret=1;
1186 }
1187 else if ( res1 == HT_OLD_ENTRY )
1188 {
1189 hashtb_delete(ef);
1190 }
1191 hashtb_end(ef);
1192 }
1193 else if( res == HT_NEW_ENTRY )
1194 {
1195 hashtb_delete(e);
1196 }
1197
1198 hashtb_end(e);
1199
1200 return ret;
1201}