blob: 8537b2e35a692125c5c0d8e21d37136af1d50eb7 [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);
akmhoque3171d652012-11-13 11:44:33 -060074 if ( nlsr->debugging )
75 print_adj_matrix(adj_matrix,map_element);
akmhoque29c1db52012-09-07 14:47:43 -050076
77 long int source=get_mapping_no(nlsr->router_name);
78 long int *parent=(long int *)malloc(map_element * sizeof(long int));
akmhoquede61ba92012-09-20 22:19:12 -050079 long int *dist=(long int *)malloc(map_element * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -050080
akmhoque9fe296b2012-09-24 09:52:08 -050081 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoque29c1db52012-09-07 14:47:43 -050082
akmhoque0820a2e2012-09-24 20:23:01 -050083 if ( (num_link == 0) || (nlsr->multi_path_face_num <= 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050084 {
85 calculate_path(adj_matrix,parent,dist, map_element, source);
86 print_all_path_from_source(parent,source);
87 print_all_next_hop(parent,source);
88 update_routing_table_with_new_route(parent, dist,source);
89 }
akmhoque59d38c82012-09-24 20:24:03 -050090 else if ( (num_link != 0) && (nlsr->multi_path_face_num > 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050091 {
92 long int *links=(long int *)malloc(num_link*sizeof(long int));
93 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
94 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
95 for ( i=0 ; i < num_link; i++)
96 {
97 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
98 calculate_path(adj_matrix,parent,dist, map_element, source);
99 print_all_path_from_source(parent,source);
100 print_all_next_hop(parent,source);
101 update_routing_table_with_new_route(parent, dist,source);
102 }
akmhoque29c1db52012-09-07 14:47:43 -0500103
akmhoque9fe296b2012-09-24 09:52:08 -0500104 free(links);
105 free(link_costs);
106 }
akmhoquede61ba92012-09-20 22:19:12 -0500107
108 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -0500109
akmhoqueffacaa82012-09-13 17:48:30 -0500110 print_routing_table();
111 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -0500112
akmhoque29c1db52012-09-07 14:47:43 -0500113 for(i = 0; i < map_element; i++)
114 {
115 free(adj_matrix[i]);
116 }
117 free(parent);
akmhoquede61ba92012-09-20 22:19:12 -0500118 free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500119 free(adj_matrix);
120 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -0500121 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -0500122
123 }
124 nlsr->is_route_calculation_scheduled=0;
125
akmhoqueffacaa82012-09-13 17:48:30 -0500126 nlsr_unlock();
127
akmhoque29c1db52012-09-07 14:47:43 -0500128 return 0;
129}
130
131void
akmhoquede61ba92012-09-20 22:19:12 -0500132calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500133{
134 int i;
135 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500136 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500137 long int *Q=(long int *)malloc(V * sizeof(long int));
138 long int head=0;
139 /* Initial the Parent */
140 for (i = 0 ; i < V; i++)
141 {
142 parent[i]=EMPTY_PARENT;
143 dist[i]=INF_DISTANCE;
144 Q[i]=i;
145 }
146
akmhoque9fe296b2012-09-24 09:52:08 -0500147 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500148 {
akmhoque9fe296b2012-09-24 09:52:08 -0500149 dist[S]=0;
150 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500151
akmhoque9fe296b2012-09-24 09:52:08 -0500152 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500153 {
akmhoque9fe296b2012-09-24 09:52:08 -0500154 u=Q[head];
155 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500156 {
akmhoque9fe296b2012-09-24 09:52:08 -0500157 break;
158 }
159
160 for(v=0 ; v <V; v++)
161 {
162 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500163 {
akmhoque9fe296b2012-09-24 09:52:08 -0500164 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500165 {
akmhoque9fe296b2012-09-24 09:52:08 -0500166
167 if( dist[u] + adj_matrix[u][v] < dist[v])
168 {
169 dist[v]=dist[u] + adj_matrix[u][v] ;
170 parent[v]=u;
171 }
172
173 }
akmhoque29c1db52012-09-07 14:47:43 -0500174
175 }
176
177 }
178
akmhoque9fe296b2012-09-24 09:52:08 -0500179 head++;
180 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500181 }
akmhoque29c1db52012-09-07 14:47:43 -0500182 }
akmhoque29c1db52012-09-07 14:47:43 -0500183 free(Q);
akmhoquede61ba92012-09-20 22:19:12 -0500184 //free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500185}
186
187void
188print_all_path_from_source(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
akmhoque9fe296b2012-09-24 09:52:08 -0500199 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500200 {
akmhoque9fe296b2012-09-24 09:52:08 -0500201 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500202 {
akmhoque9fe296b2012-09-24 09:52:08 -0500203 me=e->data;
204 if(me->mapping != source)
205 {
akmhoque3171d652012-11-13 11:44:33 -0600206
207 if ( nlsr->debugging )
208 {
209 print_path(parent,(long int)me->mapping);
210 printf("\n");
211 }
212 if ( nlsr->detailed_logging )
213 {
214 print_path(parent,(long int)me->mapping);
215 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
216 }
217
akmhoque9fe296b2012-09-24 09:52:08 -0500218 }
219 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500220 }
akmhoque29c1db52012-09-07 14:47:43 -0500221 }
akmhoque29c1db52012-09-07 14:47:43 -0500222 hashtb_end(e);
223
224}
225
akmhoquefbfd0982012-09-09 20:59:03 -0500226void
227print_all_next_hop(long int *parent,long int source)
228{
229 int i, map_element;
230 struct map_entry *me;
231
232 struct hashtb_enumerator ee;
233 struct hashtb_enumerator *e = &ee;
234
235 hashtb_start(nlsr->map, e);
236 map_element=hashtb_n(nlsr->map);
237
238 for(i=0;i<map_element;i++)
239 {
240 me=e->data;
241 if(me->mapping != source)
242 {
akmhoque1771c412012-11-09 13:06:08 -0600243 if ( nlsr->debugging )
244 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
245 if ( nlsr->detailed_logging )
246 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 -0500247 }
248 hashtb_next(e);
249 }
250
251 hashtb_end(e);
252
253}
254
255long int
256get_next_hop_from_calculation(long int *parent, long int dest,long int source)
257{
258 long int next_hop;
259 while ( parent[dest] != EMPTY_PARENT )
260 {
261 next_hop=dest;
262 dest=parent[dest];
263
264 }
265
266 if ( dest != source )
267 {
268 next_hop=NO_NEXT_HOP;
269 }
270
271 return next_hop;
272
273}
274
akmhoque29c1db52012-09-07 14:47:43 -0500275void
276print_path(long int *parent, long int dest)
277{
278 if (parent[dest] != EMPTY_PARENT )
279 print_path(parent,parent[dest]);
akmhoque29c1db52012-09-07 14:47:43 -0500280 printf(" %ld",dest);
281}
282
283int
284is_not_explored(long int *Q, long int u,long int start, long int element)
285{
286 int ret=0;
287 long int i;
288 for(i=start; i< element; i++)
289 {
290 if ( Q[i] == u )
291 {
292 ret=1;
293 break;
294 }
295 }
296 return ret;
297}
298
299void
300sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
301{
302 long int i,j;
303 long int temp_u;
304
305 for ( i=start ; i < element ; i ++)
306 {
307 for( j=i+1; j<element; j ++)
308 {
309 if (dist[Q[j]] < dist[Q[i]])
310 {
311 temp_u=Q[j];
312 Q[j]=Q[i];
313 Q[i]=temp_u;
314 }
315 }
316 }
317
318}
319
320void
321print_map(void)
322{
323 int i, map_element;
324 struct map_entry *me;
325
326 struct hashtb_enumerator ee;
327 struct hashtb_enumerator *e = &ee;
328
329 hashtb_start(nlsr->map, e);
330 map_element=hashtb_n(nlsr->map);
331
332 for(i=0;i<map_element;i++)
333 {
334 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600335 if ( nlsr->debugging )
336 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
337 if ( nlsr->detailed_logging )
338 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
akmhoque29c1db52012-09-07 14:47:43 -0500339 hashtb_next(e);
340 }
341
342 hashtb_end(e);
343}
344
345
346void
347assign_mapping_number(void)
348{
349 int i, map_element;
350 struct map_entry *me;
351
352 struct hashtb_enumerator ee;
353 struct hashtb_enumerator *e = &ee;
354
355 hashtb_start(nlsr->map, e);
356 map_element=hashtb_n(nlsr->map);
357
358 for(i=0;i<map_element;i++)
359 {
360 me=e->data;
361 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500362 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500363 hashtb_next(e);
364 }
365
366 hashtb_end(e);
367}
368
369void
370make_map(void)
371{
372
373
374 int i, adj_lsdb_element;
375 struct alsa *adj_lsa;
376
377 struct hashtb_enumerator ee;
378 struct hashtb_enumerator *e = &ee;
379
380 hashtb_start(nlsr->lsdb->adj_lsdb, e);
381 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
382
akmhoque9fe296b2012-09-24 09:52:08 -0500383 add_map_entry(nlsr->router_name);
384
akmhoque29c1db52012-09-07 14:47:43 -0500385 for(i=0;i<adj_lsdb_element;i++)
386 {
387 adj_lsa=e->data;
388 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
389 hashtb_next(e);
390 }
391
392 hashtb_end(e);
393
394}
395
396void
397add_map_entry(char *router)
398{
399
400 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
401
402 struct hashtb_enumerator ee;
403 struct hashtb_enumerator *e = &ee;
404 int res;
405
406 hashtb_start(nlsr->map, e);
407 res = hashtb_seek(e, router, strlen(router), 0);
408
409 if(res == HT_NEW_ENTRY)
410 {
411 me=e->data;
412 me->router=(char *)malloc(strlen(router)+1);
413 memset(me->router,0,strlen(router)+1);
414 memcpy(me->router,router,strlen(router));
415 me->mapping=0;
416 }
417
418 hashtb_end(e);
419
420}
421
422
423void
424add_adj_data_to_map(char *orig_router, char *body, int no_link)
425{
426 add_map_entry(orig_router);
427
428 int i=0;
429 char *lsa_data=(char *)malloc(strlen(body)+1);
430 memset( lsa_data,0,strlen(body)+1);
431 memcpy(lsa_data,body,strlen(body)+1);
432 char *sep="|";
433 char *rem;
434 char *rtr_id;
435 char *length;
436 char *face;
437 char *metric;
438
439 if(no_link >0 )
440 {
441 rtr_id=strtok_r(lsa_data,sep,&rem);
442 length=strtok_r(NULL,sep,&rem);
443 face=strtok_r(NULL,sep,&rem);
444 metric=strtok_r(NULL,sep,&rem);
445
446 add_map_entry(rtr_id);
447
448 for(i=1;i<no_link;i++)
449 {
450 rtr_id=strtok_r(NULL,sep,&rem);
451 length=strtok_r(NULL,sep,&rem);
452 face=strtok_r(NULL,sep,&rem);
453 metric=strtok_r(NULL,sep,&rem);
454
455 add_map_entry(rtr_id);
456
457 }
458 }
459
460 free(lsa_data);
461}
462
463int
464get_mapping_no(char *router)
465{
466 struct map_entry *me;
467
468 struct hashtb_enumerator ee;
469 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500470 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500471 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500472
akmhoque63152c62012-09-18 08:43:42 -0500473 int n = hashtb_n(nlsr->map);
474
475 if ( n < 1)
476 {
477 return NO_MAPPING_NUM;
478 }
479
akmhoque29c1db52012-09-07 14:47:43 -0500480 hashtb_start(nlsr->map, e);
481 res = hashtb_seek(e, router, strlen(router), 0);
482
483 if( res == HT_OLD_ENTRY )
484 {
485 me=e->data;
486 ret=me->mapping;
487 }
488 else if(res == HT_NEW_ENTRY)
489 {
490 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500491 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500492 }
493
494 hashtb_end(e);
495
496 return ret;
497
498}
499
akmhoquefbfd0982012-09-09 20:59:03 -0500500
501void
502add_rev_map_entry(long int mapping_number, char *router)
503{
504
505 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
506
507 struct hashtb_enumerator ee;
508 struct hashtb_enumerator *e = &ee;
509 int res;
510
511 hashtb_start(nlsr->rev_map, e);
512 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
513
514 if(res == HT_NEW_ENTRY)
515 {
516 me=e->data;
517 me->router=(char *)malloc(strlen(router)+1);
518 memset(me->router,0,strlen(router)+1);
519 memcpy(me->router,router,strlen(router));
520 me->mapping=mapping_number;
521 }
522
523 hashtb_end(e);
524
525}
526
527
528
529char *
530get_router_from_rev_map(long int mapping_number)
531{
532
533 struct map_entry *me;
534
535 struct hashtb_enumerator ee;
536 struct hashtb_enumerator *e = &ee;
537 int res;
538
539 hashtb_start(nlsr->rev_map, e);
540 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
541
542 if(res == HT_OLD_ENTRY)
543 {
544 me=e->data;
545 hashtb_end(e);
546 return me->router;
547 }
548 else if(res == HT_NEW_ENTRY)
549 {
550 hashtb_delete(e);
551 hashtb_end(e);
552 }
553
554 return NULL;
555}
556
557void
558print_rev_map(void)
559{
560 int i, map_element;
561 struct map_entry *me;
562
563 struct hashtb_enumerator ee;
564 struct hashtb_enumerator *e = &ee;
565
566 hashtb_start(nlsr->map, e);
567 map_element=hashtb_n(nlsr->map);
568
569 for(i=0;i<map_element;i++)
570 {
571 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600572 if ( nlsr->debugging )
573 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
574 if ( nlsr->detailed_logging )
575 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
akmhoquefbfd0982012-09-09 20:59:03 -0500576 hashtb_next(e);
577 }
578
579 hashtb_end(e);
580}
581
582
akmhoque29c1db52012-09-07 14:47:43 -0500583void
584assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
585{
586 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
587 int mapping_nbr_router;
588
589 int i;
590 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
591 memset( lsa_data,0,strlen(adj_lsa->body)+1);
592 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
593 char *sep="|";
594 char *rem;
595 char *rtr_id;
596 char *length;
597 char *face;
598 char *metric;
599
600 if(adj_lsa->no_link >0 )
601 {
602 rtr_id=strtok_r(lsa_data,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 for(i=1;i<adj_lsa->no_link;i++)
611 {
612 rtr_id=strtok_r(NULL,sep,&rem);
613 length=strtok_r(NULL,sep,&rem);
614 face=strtok_r(NULL,sep,&rem);
615 metric=strtok_r(NULL,sep,&rem);
616
617 mapping_nbr_router=get_mapping_no(rtr_id);
618 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
619
620 }
621 }
622
623 free(lsa_data);
624}
625
626void
627make_adj_matrix(int **adj_matrix,int map_element)
628{
629
630 init_adj_matrix(adj_matrix,map_element);
631
632 int i, adj_lsdb_element;
633 struct alsa *adj_lsa;
634
635 struct hashtb_enumerator ee;
636 struct hashtb_enumerator *e = &ee;
637
638 hashtb_start(nlsr->lsdb->adj_lsdb, e);
639 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
640
641 for(i=0;i<adj_lsdb_element;i++)
642 {
643 adj_lsa=e->data;
644 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
645 hashtb_next(e);
646 }
647
648 hashtb_end(e);
649
650}
651
652void
653init_adj_matrix(int **adj_matrix,int map_element)
654{
655 int i, j;
656 for(i=0;i<map_element;i++)
657 for(j=0;j<map_element;j++)
658 adj_matrix[i][j]=0;
659}
660
661void print_adj_matrix(int **adj_matrix, int map_element)
662{
663 int i, j;
664 for(i=0;i<map_element;i++)
665 {
666 for(j=0;j<map_element;j++)
667 printf("%d ",adj_matrix[i][j]);
668 printf("\n");
669 }
670}
671
akmhoquede61ba92012-09-20 22:19:12 -0500672
akmhoque3560cb62012-09-09 10:52:30 -0500673int
akmhoque9fe296b2012-09-24 09:52:08 -0500674get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
675{
676 int no_link=0;
677 int i;
678
679 for(i=0;i<V;i++)
680 {
681 if ( adj_matrix[S][i] > 0 )
682 {
683 no_link++;
684 }
685 }
686 return no_link;
687}
688
689void
690get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
691{
692 int i,j;
693 j=0;
694 for (i=0; i <V; i++)
695 {
696 if ( adj_matrix[S][i] > 0 )
697 {
698 links[j]=i;
699 link_costs[j]=adj_matrix[S][i];
700 j++;
701 }
702 }
703}
704
705void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
706{
707 int i;
708 for ( i = 0; i < V; i++ )
709 {
710 if ( i == link )
711 {
712 adj_matrix[S][i]=link_cost;
713 }
714 else
715 {
716 adj_matrix[S][i]=0;
717 }
718 }
719
720}
721
722int
akmhoquede61ba92012-09-20 22:19:12 -0500723get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500724{
725 struct routing_table_entry *rte;
726
727 struct hashtb_enumerator ee;
728 struct hashtb_enumerator *e = &ee;
729 int res,ret;
730
731 hashtb_start(nlsr->routing_table, e);
732 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
733
734 if( res == HT_OLD_ENTRY )
735 {
736 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500737 ret=hashtb_n(rte->face_list);
738 //nhl=rte->face_list;
739 }
740 else if(res == HT_NEW_ENTRY)
741 {
742 hashtb_delete(e);
743 ret=NO_NEXT_HOP;
744 }
745
746 hashtb_end(e);
747
748 return ret;
749}
750
751
752int
753get_next_hop(char *dest_router,int *faces, int *route_costs)
754{
755 struct routing_table_entry *rte;
756
757 struct hashtb_enumerator ee;
758 struct hashtb_enumerator *e = &ee;
759 int res,ret;
760
761 hashtb_start(nlsr->routing_table, e);
762 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
763
764 if( res == HT_OLD_ENTRY )
765 {
766 rte=e->data;
767 ret=hashtb_n(rte->face_list);
768 //nhl=rte->face_list;
769 int j,face_list_element;
770 struct face_list_entry *fle;
771
772 struct hashtb_enumerator eef;
773 struct hashtb_enumerator *ef = &eef;
774
775 hashtb_start(rte->face_list, ef);
776 face_list_element=hashtb_n(rte->face_list);
777 for(j=0;j<face_list_element;j++)
778 {
779 fle=ef->data;
780 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
781 faces[j]=fle->next_hop_face;
782 route_costs[j]=fle->route_cost;
783 hashtb_next(ef);
784 }
785 hashtb_end(ef);
786
akmhoque3560cb62012-09-09 10:52:30 -0500787 }
788 else if(res == HT_NEW_ENTRY)
789 {
790 hashtb_delete(e);
791 ret=NO_NEXT_HOP;
792 }
793
794 hashtb_end(e);
795
796 return ret;
797}
798
799void
800add_next_hop_router(char *dest_router)
801{
802 if ( strcmp(dest_router,nlsr->router_name)== 0)
803 {
804 return ;
805 }
806
807 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
808
809 struct hashtb_enumerator ee;
810 struct hashtb_enumerator *e = &ee;
811 int res;
812
813 hashtb_start(nlsr->routing_table, e);
814 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
815
816 if( res == HT_NEW_ENTRY )
817 {
818 rte=e->data;
819 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
820 memset(rte->dest_router,0,strlen(dest_router)+1);
821 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500822 //rte->next_hop_face=NO_NEXT_HOP;
823 struct hashtb_param param_fle = {0};
824 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
825
826 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500827 }
828 hashtb_end(e);
829
830}
831
832void
833add_next_hop_from_lsa_adj_body(char *body, int no_link)
834{
835
836 int i=0;
837 char *lsa_data=(char *)malloc(strlen(body)+1);
838 memset( lsa_data,0,strlen(body)+1);
839 memcpy(lsa_data,body,strlen(body)+1);
840 char *sep="|";
841 char *rem;
842 char *rtr_id;
843 char *length;
844 char *face;
845 char *metric;
846
847 if(no_link >0 )
848 {
849 rtr_id=strtok_r(lsa_data,sep,&rem);
850 length=strtok_r(NULL,sep,&rem);
851 face=strtok_r(NULL,sep,&rem);
852 metric=strtok_r(NULL,sep,&rem);
853
akmhoque3560cb62012-09-09 10:52:30 -0500854
855 add_next_hop_router(rtr_id);
856
857 for(i=1;i<no_link;i++)
858 {
859 rtr_id=strtok_r(NULL,sep,&rem);
860 length=strtok_r(NULL,sep,&rem);
861 face=strtok_r(NULL,sep,&rem);
862 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500863
864 add_next_hop_router(rtr_id);
865
866 }
867 }
868
869 free(lsa_data);
870
871
872}
873
874void
akmhoquede61ba92012-09-20 22:19:12 -0500875update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -0500876{
akmhoquede61ba92012-09-20 22:19:12 -0500877 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -0500878 struct routing_table_entry *rte;
879
880 struct hashtb_enumerator ee;
881 struct hashtb_enumerator *e = &ee;
882
883 hashtb_start(nlsr->routing_table, e);
884 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
885
886 if( res == HT_OLD_ENTRY )
887 {
888 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500889
890 struct hashtb_enumerator eef;
891 struct hashtb_enumerator *ef = &eef;
892
893 hashtb_start(rte->face_list, ef);
894 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
895 if( res1 == HT_NEW_ENTRY)
896 {
897 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
898 fle=ef->data;
899 fle->next_hop_face=next_hop_face;
900 fle->route_cost=route_cost;
901 }
902 else if ( res1 == HT_OLD_ENTRY )
903 {
904 struct face_list_entry *fle;
905 fle=ef->data;
906 fle->route_cost=route_cost;
907 }
908 hashtb_end(ef);
909
910 /*
911 //updating the face for the router prefix itself
912 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
913 {
914 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
915 }
916 if ( (next_hop_face != NO_FACE || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
917 {
918 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
919 }
920
akmhoqueffacaa82012-09-13 17:48:30 -0500921 rte->next_hop_face=next_hop_face;
akmhoquede61ba92012-09-20 22:19:12 -0500922 */
akmhoqueffacaa82012-09-13 17:48:30 -0500923 }
924 else if ( res == HT_OLD_ENTRY )
925 {
926 hashtb_delete(e);
927 }
928
929 hashtb_end(e);
930
931}
932
933void
akmhoque3560cb62012-09-09 10:52:30 -0500934print_routing_table(void)
935{
akmhoque1771c412012-11-09 13:06:08 -0600936 if ( nlsr->debugging )
937 printf("print_routing_table called\n");
938 if ( nlsr->detailed_logging )
939 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500940 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -0500941
942 struct routing_table_entry *rte;
943
944 struct hashtb_enumerator ee;
945 struct hashtb_enumerator *e = &ee;
946
947 hashtb_start(nlsr->routing_table, e);
948 rt_element=hashtb_n(nlsr->routing_table);
949
950 for(i=0;i<rt_element;i++)
951 {
akmhoque1771c412012-11-09 13:06:08 -0600952 if ( nlsr->debugging )
953 printf("----------Routing Table Entry %d------------------\n",i+1);
954 if ( nlsr->detailed_logging )
955 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
956
akmhoque3560cb62012-09-09 10:52:30 -0500957 rte=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600958
959 if ( nlsr->debugging )
960 printf(" Destination Router: %s \n",rte->dest_router);
961 if ( nlsr->detailed_logging )
962 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
963
964
akmhoquede61ba92012-09-20 22:19:12 -0500965 //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);
966
967 struct face_list_entry *fle;
968
969 struct hashtb_enumerator eef;
970 struct hashtb_enumerator *ef = &eef;
971
972 hashtb_start(rte->face_list, ef);
973 face_list_element=hashtb_n(rte->face_list);
974 if ( face_list_element <= 0 )
975 {
akmhoque1771c412012-11-09 13:06:08 -0600976 if ( nlsr->debugging )
977 printf(" Face: No Face \n");
978 if ( nlsr->detailed_logging )
979 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -0500980 }
981 else
982 {
983 for(j=0;j<face_list_element;j++)
984 {
985 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -0600986 if ( nlsr->debugging )
987 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
988 if ( nlsr->detailed_logging )
989 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -0500990 hashtb_next(ef);
991 }
992 }
993 hashtb_end(ef);
994
akmhoque3560cb62012-09-09 10:52:30 -0500995 hashtb_next(e);
996 }
997
998 hashtb_end(e);
999
akmhoque3171d652012-11-13 11:44:33 -06001000 if ( nlsr->debugging )
1001 printf("\n");
1002 if ( nlsr->detailed_logging )
1003 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -05001004}
1005
akmhoque63152c62012-09-18 08:43:42 -05001006
1007int
1008delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1009{
akmhoque1771c412012-11-09 13:06:08 -06001010 if ( nlsr->debugging )
1011 {
1012 printf("delete_empty_rte called\n");
1013 printf("Router: %s \n",(char *)ev->evdata);
1014 }
1015 if ( nlsr->detailed_logging )
1016 {
1017 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1018 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
1019 //writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
1020 }
1021
akmhoque63152c62012-09-18 08:43:42 -05001022 if(flags == CCN_SCHEDULE_CANCEL)
1023 {
1024 return -1;
1025 }
akmhoque63152c62012-09-18 08:43:42 -05001026 int res;
1027 struct hashtb_enumerator ee;
1028 struct hashtb_enumerator *e = &ee;
1029
1030 hashtb_start(nlsr->routing_table, e);
1031 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1032
1033 if ( res == HT_OLD_ENTRY )
1034 {
1035 hashtb_delete(e);
1036 }
1037 else if ( res == HT_NEW_ENTRY )
1038 {
1039 hashtb_delete(e);
1040 }
1041
akmhoquede61ba92012-09-20 22:19:12 -05001042 print_routing_table();
1043
akmhoque63152c62012-09-18 08:43:42 -05001044 return 0;
1045}
1046
akmhoquede61ba92012-09-20 22:19:12 -05001047void
akmhoque3cced642012-09-24 16:20:20 -05001048clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001049{
akmhoque1771c412012-11-09 13:06:08 -06001050 if ( nlsr->debugging )
1051 printf("clear_old_routing_table called\n");
1052 if ( nlsr->detailed_logging )
1053 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001054 int i,rt_element;
1055
1056 struct routing_table_entry *rte;
1057
1058 struct hashtb_enumerator ee;
1059 struct hashtb_enumerator *e = &ee;
1060
1061 hashtb_start(nlsr->routing_table, e);
1062 rt_element=hashtb_n(nlsr->routing_table);
1063
1064 for(i=0;i<rt_element;i++)
1065 {
1066 rte=e->data;
1067 hashtb_destroy(&rte->face_list);
1068 struct hashtb_param param_fle = {0};
1069 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1070
1071 hashtb_next(e);
1072 }
1073
1074 hashtb_end(e);
1075}
1076
akmhoque63152c62012-09-18 08:43:42 -05001077
akmhoque3560cb62012-09-09 10:52:30 -05001078void
akmhoque3cced642012-09-24 16:20:20 -05001079do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001080{
akmhoque1771c412012-11-09 13:06:08 -06001081 if ( nlsr->debugging )
1082 printf("do_old_routing_table_updates called\n");
1083 if ( nlsr->detailed_logging )
1084 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1085
akmhoque810a5b52012-09-09 16:53:14 -05001086 int i, rt_element;
1087 int mapping_no;
1088
1089 struct routing_table_entry *rte;
1090
1091 struct hashtb_enumerator ee;
1092 struct hashtb_enumerator *e = &ee;
1093
1094 hashtb_start(nlsr->routing_table, e);
1095 rt_element=hashtb_n(nlsr->routing_table);
1096
1097 for(i=0;i<rt_element;i++)
1098 {
1099 rte=e->data;
1100 mapping_no=get_mapping_no(rte->dest_router);
1101 if ( mapping_no == NO_MAPPING_NUM)
1102 {
akmhoquede61ba92012-09-20 22:19:12 -05001103 delete_orig_router_from_npt(rte->dest_router);
akmhoque63152c62012-09-18 08:43:42 -05001104 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1105 memset(router,0,strlen(rte->dest_router)+1);
1106 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1107 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque810a5b52012-09-09 16:53:14 -05001108 }
1109 hashtb_next(e);
1110 }
1111
1112 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001113}
akmhoque29c1db52012-09-07 14:47:43 -05001114
akmhoquede61ba92012-09-20 22:19:12 -05001115
1116
akmhoquefbfd0982012-09-09 20:59:03 -05001117void
akmhoquede61ba92012-09-20 22:19:12 -05001118update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001119{
akmhoque1771c412012-11-09 13:06:08 -06001120 if ( nlsr->debugging )
1121 printf("update_routing_table_with_new_route called\n");
1122 if ( nlsr->detailed_logging )
1123 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001124 int i, map_element;
1125 struct map_entry *me;
1126
1127 struct hashtb_enumerator ee;
1128 struct hashtb_enumerator *e = &ee;
1129
1130 hashtb_start(nlsr->rev_map, e);
1131 map_element=hashtb_n(nlsr->rev_map);
1132
1133 for(i=0;i<map_element;i++)
1134 {
1135 me=e->data;
1136 if(me->mapping != source)
1137 {
1138
1139 char *orig_router=get_router_from_rev_map(me->mapping);
1140 if (orig_router != NULL )
1141 {
1142 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
1143 //printf(" Next hop router Num: %d ",next_hop_router_num);
1144 if ( next_hop_router_num == NO_NEXT_HOP )
1145 {
akmhoquede61ba92012-09-20 22:19:12 -05001146 //update_npt_with_new_route(orig_router,NO_FACE);
akmhoque1771c412012-11-09 13:06:08 -06001147 if ( nlsr->debugging )
1148 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1149 if ( nlsr->detailed_logging )
1150 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001151 }
1152 else
1153 {
1154 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
1155 //printf("Next hop router name: %s \n",next_hop_router);
1156 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001157 //update_npt_with_new_route(orig_router,next_hop_face);
1158 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoque1771c412012-11-09 13:06:08 -06001159 if ( nlsr->debugging )
1160 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1161 if ( nlsr->detailed_logging )
1162 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1163
akmhoquefbfd0982012-09-09 20:59:03 -05001164
1165 }
1166 }
1167 }
1168 hashtb_next(e);
1169 }
1170
1171 hashtb_end(e);
1172}
1173
akmhoquede61ba92012-09-20 22:19:12 -05001174int
1175does_face_exist_for_router(char *dest_router, int face_id)
1176{
1177 int ret=0;
1178
1179 int res,res1;
1180 struct routing_table_entry *rte;
1181
1182 struct hashtb_enumerator ee;
1183 struct hashtb_enumerator *e = &ee;
1184
1185 hashtb_start(nlsr->routing_table, e);
1186 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1187
1188 if( res == HT_OLD_ENTRY )
1189 {
1190 rte=e->data;
1191 struct hashtb_enumerator eef;
1192 struct hashtb_enumerator *ef = &eef;
1193
1194 hashtb_start(rte->face_list, ef);
1195 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1196 if( res1 == HT_OLD_ENTRY)
1197 {
1198 ret=1;
1199 }
1200 else if ( res1 == HT_OLD_ENTRY )
1201 {
1202 hashtb_delete(ef);
1203 }
1204 hashtb_end(ef);
1205 }
1206 else if( res == HT_NEW_ENTRY )
1207 {
1208 hashtb_delete(e);
1209 }
1210
1211 hashtb_end(e);
1212
1213 return ret;
1214}