blob: 573fcf931d61ba9fb80772c22641c0f21064bb6b [file] [log] [blame]
akmhoqueb77b95f2013-02-08 12:28:47 -06001#include <stdio.h>
2#include <string.h>
3#include <math.h>
4#include <stdlib.h>
akmhoque29c1db52012-09-07 14:47:43 -05005#include <unistd.h>
6#include <getopt.h>
7#include <sys/time.h>
8#include <assert.h>
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12#include <sys/types.h>
13
14
15#include <ccn/ccn.h>
16#include <ccn/uri.h>
17#include <ccn/keystore.h>
18#include <ccn/signing.h>
19#include <ccn/schedule.h>
20#include <ccn/hashtb.h>
21
22
23#include "nlsr.h"
24#include "nlsr_route.h"
25#include "nlsr_lsdb.h"
akmhoque3560cb62012-09-09 10:52:30 -050026#include "nlsr_npt.h"
akmhoquefbfd0982012-09-09 20:59:03 -050027#include "nlsr_adl.h"
akmhoquede61ba92012-09-20 22:19:12 -050028#include "nlsr_fib.h"
akmhoque1771c412012-11-09 13:06:08 -060029#include "utility.h"
akmhoque29c1db52012-09-07 14:47:43 -050030
31int
32route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
33{
akmhoqueffacaa82012-09-13 17:48:30 -050034
35 if(flags == CCN_SCHEDULE_CANCEL)
36 {
37 return -1;
38 }
39
40 nlsr_lock();
41
akmhoque1771c412012-11-09 13:06:08 -060042 if ( nlsr->debugging )
43 printf("route_calculate called\n");
44 if ( nlsr->detailed_logging )
45 writeLogg(__FILE__,__FUNCTION__,__LINE__,"route_calculate called\n");
akmhoque29c1db52012-09-07 14:47:43 -050046
47 if( ! nlsr->is_build_adj_lsa_sheduled )
48 {
49 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050050 print_routing_table();
51 print_npt();
52
akmhoque29c1db52012-09-07 14:47:43 -050053 struct hashtb_param param_me = {0};
54 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoquefbfd0982012-09-09 20:59:03 -050055 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoque29c1db52012-09-07 14:47:43 -050056 make_map();
57 assign_mapping_number();
58 print_map();
akmhoquefbfd0982012-09-09 20:59:03 -050059 print_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -050060
akmhoque3560cb62012-09-09 10:52:30 -050061 do_old_routing_table_updates();
akmhoquede61ba92012-09-20 22:19:12 -050062 clear_old_routing_table();
63 print_routing_table();
akmhoquea30cb772012-10-07 09:50:34 -050064 print_npt();
akmhoque3560cb62012-09-09 10:52:30 -050065
akmhoque29c1db52012-09-07 14:47:43 -050066 int i;
67 int **adj_matrix;
68 int map_element=hashtb_n(nlsr->map);
69 adj_matrix=malloc(map_element * sizeof(int *));
70 for(i = 0; i < map_element; i++)
71 {
72 adj_matrix[i] = malloc(map_element * sizeof(int));
73 }
74 make_adj_matrix(adj_matrix,map_element);
akmhoque3171d652012-11-13 11:44:33 -060075 if ( nlsr->debugging )
76 print_adj_matrix(adj_matrix,map_element);
akmhoque29c1db52012-09-07 14:47:43 -050077
78 long int source=get_mapping_no(nlsr->router_name);
akmhoque9fe296b2012-09-24 09:52:08 -050079 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoqueb77b95f2013-02-08 12:28:47 -060080
81 if ( nlsr->is_hyperbolic_calc == 1)
akmhoque9fe296b2012-09-24 09:52:08 -050082 {
83 long int *links=(long int *)malloc(num_link*sizeof(long int));
84 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
85 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
akmhoqueb77b95f2013-02-08 12:28:47 -060086
87 struct hashtb_enumerator ee;
88 struct hashtb_enumerator *e = &ee;
89 for (hashtb_start(nlsr->rev_map, e); e->key != NULL; hashtb_next(e))
akmhoque9fe296b2012-09-24 09:52:08 -050090 {
akmhoqueb77b95f2013-02-08 12:28:47 -060091 struct map_entry *me=e->data;
92 if ( me->mapping != source )
93 {
94 long int *faces=(long int *)calloc(num_link,sizeof(long int));
95 double *nbr_dist=(double *)calloc(num_link,sizeof(double));
96 double *nbr_to_dest=(double *)calloc(num_link,sizeof(double));
97 for ( i=0 ; i < num_link; i++)
98 {
99 int face=get_next_hop_face_from_adl(get_router_from_rev_map(links[i]));
100 double dist_to_nbr=get_hyperbolic_distance(source,links[i]);
101 double dist_to_dest_from_nbr=get_hyperbolic_distance(links[i],me->mapping);
102 faces[i]=face;
103 nbr_dist[i]=dist_to_nbr;
104 nbr_to_dest[i]= dist_to_dest_from_nbr;
105
106
107 }
108 sort_hyperbolic_route(nbr_to_dest,nbr_dist, faces,0,num_link);
109 if (nlsr->max_faces_per_prefix == 0 )
110 {
111 for ( i=0 ; i < num_link; i++)
112 {
113 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
114 }
115 }
116 else if ( nlsr->max_faces_per_prefix > 0 )
117 {
118 if ( num_link <= nlsr->max_faces_per_prefix )
119 {
120 for ( i=0 ; i < num_link; i++)
121 {
122 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
123 }
124 }
125 else if (num_link > nlsr->max_faces_per_prefix)
126 {
127 for ( i=0 ; i < nlsr->max_faces_per_prefix; i++)
128 {
129 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
130 }
131 }
132
133 }
134 free(faces);
135 free(nbr_dist);
136 free(nbr_to_dest);
137 }
138 }
139 hashtb_end(e);
140
141
142 free(links);
143 free(link_costs);
144 }
145 else if (nlsr->is_hyperbolic_calc == 0 )
146 {
147
148 long int *parent=(long int *)malloc(map_element * sizeof(long int));
149 long int *dist=(long int *)malloc(map_element * sizeof(long int));
150
151
152 if ( (num_link == 0) || (nlsr->max_faces_per_prefix == 1 ) )
153 {
akmhoque9fe296b2012-09-24 09:52:08 -0500154 calculate_path(adj_matrix,parent,dist, map_element, source);
155 print_all_path_from_source(parent,source);
156 print_all_next_hop(parent,source);
157 update_routing_table_with_new_route(parent, dist,source);
158 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600159 else if ( (num_link != 0) && (nlsr->max_faces_per_prefix == 0 || nlsr->max_faces_per_prefix > 1 ) )
160 {
161 long int *links=(long int *)malloc(num_link*sizeof(long int));
162 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
163 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
164 for ( i=0 ; i < num_link; i++)
165 {
166 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
167 calculate_path(adj_matrix,parent,dist, map_element, source);
168 print_all_path_from_source(parent,source);
169 print_all_next_hop(parent,source);
170 update_routing_table_with_new_route(parent, dist,source);
171 }
akmhoque29c1db52012-09-07 14:47:43 -0500172
akmhoqueb77b95f2013-02-08 12:28:47 -0600173 free(links);
174 free(link_costs);
175 }
176 free(parent);
177 free(dist);
akmhoque9fe296b2012-09-24 09:52:08 -0500178 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600179
180 print_routing_table();
181 print_npt();
akmhoquede61ba92012-09-20 22:19:12 -0500182
183 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -0500184
akmhoqueffacaa82012-09-13 17:48:30 -0500185 print_routing_table();
186 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -0500187
akmhoqueb77b95f2013-02-08 12:28:47 -0600188
akmhoque29c1db52012-09-07 14:47:43 -0500189 for(i = 0; i < map_element; i++)
190 {
191 free(adj_matrix[i]);
192 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600193
akmhoque29c1db52012-09-07 14:47:43 -0500194 free(adj_matrix);
195 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -0500196 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -0500197
198 }
199 nlsr->is_route_calculation_scheduled=0;
200
akmhoqueffacaa82012-09-13 17:48:30 -0500201 nlsr_unlock();
202
akmhoque29c1db52012-09-07 14:47:43 -0500203 return 0;
204}
205
206void
akmhoquede61ba92012-09-20 22:19:12 -0500207calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500208{
209 int i;
210 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500211 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500212 long int *Q=(long int *)malloc(V * sizeof(long int));
213 long int head=0;
akmhoqueb77b95f2013-02-08 12:28:47 -0600214 /* Initiate the Parent */
akmhoque29c1db52012-09-07 14:47:43 -0500215 for (i = 0 ; i < V; i++)
216 {
217 parent[i]=EMPTY_PARENT;
218 dist[i]=INF_DISTANCE;
219 Q[i]=i;
220 }
221
akmhoque9fe296b2012-09-24 09:52:08 -0500222 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500223 {
akmhoque9fe296b2012-09-24 09:52:08 -0500224 dist[S]=0;
225 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500226
akmhoque9fe296b2012-09-24 09:52:08 -0500227 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500228 {
akmhoque9fe296b2012-09-24 09:52:08 -0500229 u=Q[head];
230 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500231 {
akmhoque9fe296b2012-09-24 09:52:08 -0500232 break;
233 }
234
235 for(v=0 ; v <V; v++)
236 {
237 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500238 {
akmhoque9fe296b2012-09-24 09:52:08 -0500239 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500240 {
akmhoque9fe296b2012-09-24 09:52:08 -0500241
242 if( dist[u] + adj_matrix[u][v] < dist[v])
243 {
244 dist[v]=dist[u] + adj_matrix[u][v] ;
245 parent[v]=u;
246 }
247
248 }
akmhoque29c1db52012-09-07 14:47:43 -0500249
250 }
251
252 }
253
akmhoque9fe296b2012-09-24 09:52:08 -0500254 head++;
255 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500256 }
akmhoque29c1db52012-09-07 14:47:43 -0500257 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600258 free(Q);
akmhoque29c1db52012-09-07 14:47:43 -0500259}
260
261void
262print_all_path_from_source(long int *parent,long int source)
263{
264 int i, map_element;
265 struct map_entry *me;
266
267 struct hashtb_enumerator ee;
268 struct hashtb_enumerator *e = &ee;
269
270 hashtb_start(nlsr->map, e);
271 map_element=hashtb_n(nlsr->map);
272
akmhoque9fe296b2012-09-24 09:52:08 -0500273 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500274 {
akmhoque9fe296b2012-09-24 09:52:08 -0500275 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500276 {
akmhoque9fe296b2012-09-24 09:52:08 -0500277 me=e->data;
278 if(me->mapping != source)
279 {
akmhoque3171d652012-11-13 11:44:33 -0600280
281 if ( nlsr->debugging )
282 {
283 print_path(parent,(long int)me->mapping);
284 printf("\n");
285 }
286 if ( nlsr->detailed_logging )
287 {
288 print_path(parent,(long int)me->mapping);
289 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
290 }
291
akmhoque9fe296b2012-09-24 09:52:08 -0500292 }
293 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500294 }
akmhoque29c1db52012-09-07 14:47:43 -0500295 }
akmhoque29c1db52012-09-07 14:47:43 -0500296 hashtb_end(e);
297
298}
299
akmhoquefbfd0982012-09-09 20:59:03 -0500300void
301print_all_next_hop(long int *parent,long int source)
302{
303 int i, map_element;
304 struct map_entry *me;
305
306 struct hashtb_enumerator ee;
307 struct hashtb_enumerator *e = &ee;
308
309 hashtb_start(nlsr->map, e);
310 map_element=hashtb_n(nlsr->map);
311
312 for(i=0;i<map_element;i++)
313 {
314 me=e->data;
315 if(me->mapping != source)
316 {
akmhoque1771c412012-11-09 13:06:08 -0600317 if ( nlsr->debugging )
318 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
319 if ( nlsr->detailed_logging )
320 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 -0500321 }
322 hashtb_next(e);
323 }
324
325 hashtb_end(e);
326
327}
328
329long int
330get_next_hop_from_calculation(long int *parent, long int dest,long int source)
331{
332 long int next_hop;
333 while ( parent[dest] != EMPTY_PARENT )
334 {
335 next_hop=dest;
336 dest=parent[dest];
337
338 }
339
340 if ( dest != source )
341 {
342 next_hop=NO_NEXT_HOP;
343 }
344
345 return next_hop;
346
347}
348
akmhoque29c1db52012-09-07 14:47:43 -0500349void
350print_path(long int *parent, long int dest)
351{
352 if (parent[dest] != EMPTY_PARENT )
353 print_path(parent,parent[dest]);
akmhoqueb77b95f2013-02-08 12:28:47 -0600354
355 if ( nlsr->debugging )
356 printf(" %ld",dest);
akmhoque29c1db52012-09-07 14:47:43 -0500357}
358
359int
360is_not_explored(long int *Q, long int u,long int start, long int element)
361{
362 int ret=0;
363 long int i;
364 for(i=start; i< element; i++)
365 {
366 if ( Q[i] == u )
367 {
368 ret=1;
369 break;
370 }
371 }
372 return ret;
373}
374
375void
376sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
377{
378 long int i,j;
379 long int temp_u;
380
381 for ( i=start ; i < element ; i ++)
382 {
383 for( j=i+1; j<element; j ++)
384 {
385 if (dist[Q[j]] < dist[Q[i]])
386 {
387 temp_u=Q[j];
388 Q[j]=Q[i];
389 Q[i]=temp_u;
390 }
391 }
392 }
393
394}
395
akmhoqueb77b95f2013-02-08 12:28:47 -0600396void
397sort_hyperbolic_route(double *dist_dest,double *dist_nbr, long int *faces,long int start,long int element)
398{
399 long int i,j;
400 double temp_dist;
401 long int temp_face;
402
403 for ( i=start ; i < element ; i ++)
404 {
405 for( j=i+1; j<element; j ++)
406 {
407 if (dist_dest[i] < dist_dest[j])
408 {
409 temp_dist=dist_dest[j];
410 dist_dest[j]=dist_dest[i];
411 dist_dest[i]=temp_dist;
412
413 temp_dist=dist_nbr[j];
414 dist_nbr[j]=dist_nbr[i];
415 dist_nbr[i]=temp_dist;
416
417 temp_face=faces[j];
418 faces[j]=faces[i];
419 faces[i]=temp_face;
420 }
421 if ( (dist_dest[i] == dist_dest[j]) && (dist_nbr[i] < dist_nbr[j]) )
422 {
423 temp_dist=dist_dest[j];
424 dist_dest[j]=dist_dest[i];
425 dist_dest[i]=temp_dist;
426
427 temp_dist=dist_nbr[j];
428 dist_nbr[j]=dist_nbr[i];
429 dist_nbr[i]=temp_dist;
430
431 temp_face=faces[j];
432 faces[j]=faces[i];
433 faces[i]=temp_face;
434 }
435 }
436 }
437
438}
439
akmhoque29c1db52012-09-07 14:47:43 -0500440void
441print_map(void)
442{
443 int i, map_element;
444 struct map_entry *me;
445
446 struct hashtb_enumerator ee;
447 struct hashtb_enumerator *e = &ee;
448
449 hashtb_start(nlsr->map, e);
450 map_element=hashtb_n(nlsr->map);
451
452 for(i=0;i<map_element;i++)
453 {
454 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600455 if ( nlsr->debugging )
456 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
457 if ( nlsr->detailed_logging )
458 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
akmhoque29c1db52012-09-07 14:47:43 -0500459 hashtb_next(e);
460 }
461
462 hashtb_end(e);
463}
464
465
466void
467assign_mapping_number(void)
468{
469 int i, map_element;
470 struct map_entry *me;
471
472 struct hashtb_enumerator ee;
473 struct hashtb_enumerator *e = &ee;
474
475 hashtb_start(nlsr->map, e);
476 map_element=hashtb_n(nlsr->map);
477
478 for(i=0;i<map_element;i++)
479 {
480 me=e->data;
481 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500482 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500483 hashtb_next(e);
484 }
485
486 hashtb_end(e);
487}
488
489void
490make_map(void)
491{
492
493
494 int i, adj_lsdb_element;
495 struct alsa *adj_lsa;
496
497 struct hashtb_enumerator ee;
498 struct hashtb_enumerator *e = &ee;
499
500 hashtb_start(nlsr->lsdb->adj_lsdb, e);
501 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
502
akmhoque9fe296b2012-09-24 09:52:08 -0500503 add_map_entry(nlsr->router_name);
504
akmhoque29c1db52012-09-07 14:47:43 -0500505 for(i=0;i<adj_lsdb_element;i++)
506 {
507 adj_lsa=e->data;
508 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
509 hashtb_next(e);
510 }
511
512 hashtb_end(e);
513
514}
515
516void
517add_map_entry(char *router)
518{
519
520 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
521
522 struct hashtb_enumerator ee;
523 struct hashtb_enumerator *e = &ee;
524 int res;
525
526 hashtb_start(nlsr->map, e);
527 res = hashtb_seek(e, router, strlen(router), 0);
528
529 if(res == HT_NEW_ENTRY)
530 {
531 me=e->data;
532 me->router=(char *)malloc(strlen(router)+1);
533 memset(me->router,0,strlen(router)+1);
534 memcpy(me->router,router,strlen(router));
535 me->mapping=0;
536 }
537
538 hashtb_end(e);
539
540}
541
542
543void
544add_adj_data_to_map(char *orig_router, char *body, int no_link)
545{
546 add_map_entry(orig_router);
547
548 int i=0;
549 char *lsa_data=(char *)malloc(strlen(body)+1);
550 memset( lsa_data,0,strlen(body)+1);
551 memcpy(lsa_data,body,strlen(body)+1);
552 char *sep="|";
553 char *rem;
554 char *rtr_id;
555 char *length;
556 char *face;
557 char *metric;
558
559 if(no_link >0 )
560 {
561 rtr_id=strtok_r(lsa_data,sep,&rem);
562 length=strtok_r(NULL,sep,&rem);
563 face=strtok_r(NULL,sep,&rem);
564 metric=strtok_r(NULL,sep,&rem);
565
566 add_map_entry(rtr_id);
567
568 for(i=1;i<no_link;i++)
569 {
570 rtr_id=strtok_r(NULL,sep,&rem);
571 length=strtok_r(NULL,sep,&rem);
572 face=strtok_r(NULL,sep,&rem);
573 metric=strtok_r(NULL,sep,&rem);
574
575 add_map_entry(rtr_id);
576
577 }
578 }
579
580 free(lsa_data);
581}
582
583int
584get_mapping_no(char *router)
585{
586 struct map_entry *me;
587
588 struct hashtb_enumerator ee;
589 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500590 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500591 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500592
akmhoque63152c62012-09-18 08:43:42 -0500593 int n = hashtb_n(nlsr->map);
594
595 if ( n < 1)
596 {
597 return NO_MAPPING_NUM;
598 }
599
akmhoque29c1db52012-09-07 14:47:43 -0500600 hashtb_start(nlsr->map, e);
601 res = hashtb_seek(e, router, strlen(router), 0);
602
603 if( res == HT_OLD_ENTRY )
604 {
605 me=e->data;
606 ret=me->mapping;
607 }
608 else if(res == HT_NEW_ENTRY)
609 {
610 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500611 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500612 }
613
614 hashtb_end(e);
615
616 return ret;
617
618}
619
akmhoquefbfd0982012-09-09 20:59:03 -0500620
621void
622add_rev_map_entry(long int mapping_number, char *router)
623{
624
625 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
626
627 struct hashtb_enumerator ee;
628 struct hashtb_enumerator *e = &ee;
629 int res;
630
631 hashtb_start(nlsr->rev_map, e);
632 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
633
634 if(res == HT_NEW_ENTRY)
635 {
636 me=e->data;
637 me->router=(char *)malloc(strlen(router)+1);
638 memset(me->router,0,strlen(router)+1);
639 memcpy(me->router,router,strlen(router));
640 me->mapping=mapping_number;
641 }
642
643 hashtb_end(e);
644
645}
646
647
648
649char *
650get_router_from_rev_map(long int mapping_number)
651{
652
653 struct map_entry *me;
654
655 struct hashtb_enumerator ee;
656 struct hashtb_enumerator *e = &ee;
657 int res;
658
659 hashtb_start(nlsr->rev_map, e);
660 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
661
662 if(res == HT_OLD_ENTRY)
663 {
664 me=e->data;
665 hashtb_end(e);
666 return me->router;
667 }
668 else if(res == HT_NEW_ENTRY)
669 {
670 hashtb_delete(e);
671 hashtb_end(e);
672 }
673
674 return NULL;
675}
676
677void
678print_rev_map(void)
679{
680 int i, map_element;
681 struct map_entry *me;
682
683 struct hashtb_enumerator ee;
684 struct hashtb_enumerator *e = &ee;
685
686 hashtb_start(nlsr->map, e);
687 map_element=hashtb_n(nlsr->map);
688
689 for(i=0;i<map_element;i++)
690 {
691 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600692 if ( nlsr->debugging )
693 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
694 if ( nlsr->detailed_logging )
695 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
akmhoquefbfd0982012-09-09 20:59:03 -0500696 hashtb_next(e);
697 }
698
699 hashtb_end(e);
700}
701
702
akmhoque29c1db52012-09-07 14:47:43 -0500703void
704assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
705{
706 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
707 int mapping_nbr_router;
708
709 int i;
710 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
711 memset( lsa_data,0,strlen(adj_lsa->body)+1);
712 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
713 char *sep="|";
714 char *rem;
715 char *rtr_id;
716 char *length;
717 char *face;
718 char *metric;
719
720 if(adj_lsa->no_link >0 )
721 {
722 rtr_id=strtok_r(lsa_data,sep,&rem);
723 length=strtok_r(NULL,sep,&rem);
724 face=strtok_r(NULL,sep,&rem);
725 metric=strtok_r(NULL,sep,&rem);
726
727 mapping_nbr_router=get_mapping_no(rtr_id);
728 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
729
730 for(i=1;i<adj_lsa->no_link;i++)
731 {
732 rtr_id=strtok_r(NULL,sep,&rem);
733 length=strtok_r(NULL,sep,&rem);
734 face=strtok_r(NULL,sep,&rem);
735 metric=strtok_r(NULL,sep,&rem);
736
737 mapping_nbr_router=get_mapping_no(rtr_id);
738 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
739
740 }
741 }
742
743 free(lsa_data);
744}
745
746void
747make_adj_matrix(int **adj_matrix,int map_element)
748{
749
750 init_adj_matrix(adj_matrix,map_element);
751
752 int i, adj_lsdb_element;
753 struct alsa *adj_lsa;
754
755 struct hashtb_enumerator ee;
756 struct hashtb_enumerator *e = &ee;
757
758 hashtb_start(nlsr->lsdb->adj_lsdb, e);
759 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
760
761 for(i=0;i<adj_lsdb_element;i++)
762 {
763 adj_lsa=e->data;
764 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
765 hashtb_next(e);
766 }
767
768 hashtb_end(e);
769
770}
771
772void
773init_adj_matrix(int **adj_matrix,int map_element)
774{
775 int i, j;
776 for(i=0;i<map_element;i++)
777 for(j=0;j<map_element;j++)
778 adj_matrix[i][j]=0;
779}
780
781void print_adj_matrix(int **adj_matrix, int map_element)
782{
783 int i, j;
784 for(i=0;i<map_element;i++)
785 {
786 for(j=0;j<map_element;j++)
787 printf("%d ",adj_matrix[i][j]);
788 printf("\n");
789 }
790}
791
akmhoquede61ba92012-09-20 22:19:12 -0500792
akmhoque3560cb62012-09-09 10:52:30 -0500793int
akmhoque9fe296b2012-09-24 09:52:08 -0500794get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
795{
796 int no_link=0;
797 int i;
798
799 for(i=0;i<V;i++)
800 {
801 if ( adj_matrix[S][i] > 0 )
802 {
803 no_link++;
804 }
805 }
806 return no_link;
807}
808
809void
810get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
811{
812 int i,j;
813 j=0;
814 for (i=0; i <V; i++)
815 {
816 if ( adj_matrix[S][i] > 0 )
817 {
818 links[j]=i;
819 link_costs[j]=adj_matrix[S][i];
820 j++;
821 }
822 }
823}
824
825void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
826{
827 int i;
828 for ( i = 0; i < V; i++ )
829 {
830 if ( i == link )
831 {
832 adj_matrix[S][i]=link_cost;
833 }
834 else
835 {
836 adj_matrix[S][i]=0;
837 }
838 }
839
840}
841
842int
akmhoquede61ba92012-09-20 22:19:12 -0500843get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500844{
845 struct routing_table_entry *rte;
846
847 struct hashtb_enumerator ee;
848 struct hashtb_enumerator *e = &ee;
849 int res,ret;
850
851 hashtb_start(nlsr->routing_table, e);
852 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
853
854 if( res == HT_OLD_ENTRY )
855 {
856 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500857 ret=hashtb_n(rte->face_list);
858 //nhl=rte->face_list;
859 }
860 else if(res == HT_NEW_ENTRY)
861 {
862 hashtb_delete(e);
863 ret=NO_NEXT_HOP;
864 }
865
866 hashtb_end(e);
867
868 return ret;
869}
870
871
872int
873get_next_hop(char *dest_router,int *faces, int *route_costs)
874{
875 struct routing_table_entry *rte;
876
877 struct hashtb_enumerator ee;
878 struct hashtb_enumerator *e = &ee;
879 int res,ret;
880
881 hashtb_start(nlsr->routing_table, e);
882 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
883
884 if( res == HT_OLD_ENTRY )
885 {
886 rte=e->data;
887 ret=hashtb_n(rte->face_list);
888 //nhl=rte->face_list;
889 int j,face_list_element;
890 struct face_list_entry *fle;
891
892 struct hashtb_enumerator eef;
893 struct hashtb_enumerator *ef = &eef;
894
895 hashtb_start(rte->face_list, ef);
896 face_list_element=hashtb_n(rte->face_list);
897 for(j=0;j<face_list_element;j++)
898 {
899 fle=ef->data;
900 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
901 faces[j]=fle->next_hop_face;
902 route_costs[j]=fle->route_cost;
903 hashtb_next(ef);
904 }
905 hashtb_end(ef);
906
akmhoque3560cb62012-09-09 10:52:30 -0500907 }
908 else if(res == HT_NEW_ENTRY)
909 {
910 hashtb_delete(e);
911 ret=NO_NEXT_HOP;
912 }
913
914 hashtb_end(e);
915
916 return ret;
917}
918
919void
920add_next_hop_router(char *dest_router)
921{
922 if ( strcmp(dest_router,nlsr->router_name)== 0)
923 {
924 return ;
925 }
926
927 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
928
929 struct hashtb_enumerator ee;
930 struct hashtb_enumerator *e = &ee;
931 int res;
932
933 hashtb_start(nlsr->routing_table, e);
934 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
935
936 if( res == HT_NEW_ENTRY )
937 {
938 rte=e->data;
939 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
940 memset(rte->dest_router,0,strlen(dest_router)+1);
941 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500942 //rte->next_hop_face=NO_NEXT_HOP;
943 struct hashtb_param param_fle = {0};
944 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
945
946 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500947 }
948 hashtb_end(e);
949
950}
951
952void
953add_next_hop_from_lsa_adj_body(char *body, int no_link)
954{
955
956 int i=0;
957 char *lsa_data=(char *)malloc(strlen(body)+1);
958 memset( lsa_data,0,strlen(body)+1);
959 memcpy(lsa_data,body,strlen(body)+1);
960 char *sep="|";
961 char *rem;
962 char *rtr_id;
963 char *length;
964 char *face;
965 char *metric;
966
967 if(no_link >0 )
968 {
969 rtr_id=strtok_r(lsa_data,sep,&rem);
970 length=strtok_r(NULL,sep,&rem);
971 face=strtok_r(NULL,sep,&rem);
972 metric=strtok_r(NULL,sep,&rem);
973
akmhoque3560cb62012-09-09 10:52:30 -0500974
975 add_next_hop_router(rtr_id);
976
977 for(i=1;i<no_link;i++)
978 {
979 rtr_id=strtok_r(NULL,sep,&rem);
980 length=strtok_r(NULL,sep,&rem);
981 face=strtok_r(NULL,sep,&rem);
982 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500983
984 add_next_hop_router(rtr_id);
985
986 }
987 }
988
989 free(lsa_data);
990
991
992}
993
994void
akmhoquede61ba92012-09-20 22:19:12 -0500995update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -0500996{
akmhoqueb77b95f2013-02-08 12:28:47 -0600997 if ( nlsr->debugging )
998 {
999 printf("update_routing_table called \n");
1000 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
1001 }
1002
akmhoquede61ba92012-09-20 22:19:12 -05001003 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -05001004 struct routing_table_entry *rte;
1005
1006 struct hashtb_enumerator ee;
1007 struct hashtb_enumerator *e = &ee;
1008
1009 hashtb_start(nlsr->routing_table, e);
1010 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1011
1012 if( res == HT_OLD_ENTRY )
1013 {
1014 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -05001015
1016 struct hashtb_enumerator eef;
1017 struct hashtb_enumerator *ef = &eef;
1018
1019 hashtb_start(rte->face_list, ef);
1020 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
1021 if( res1 == HT_NEW_ENTRY)
1022 {
1023 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
1024 fle=ef->data;
1025 fle->next_hop_face=next_hop_face;
1026 fle->route_cost=route_cost;
1027 }
1028 else if ( res1 == HT_OLD_ENTRY )
1029 {
1030 struct face_list_entry *fle;
1031 fle=ef->data;
1032 fle->route_cost=route_cost;
1033 }
1034 hashtb_end(ef);
akmhoqueffacaa82012-09-13 17:48:30 -05001035 }
1036 else if ( res == HT_OLD_ENTRY )
1037 {
1038 hashtb_delete(e);
1039 }
1040
1041 hashtb_end(e);
1042
1043}
1044
1045void
akmhoque3560cb62012-09-09 10:52:30 -05001046print_routing_table(void)
1047{
akmhoque1771c412012-11-09 13:06:08 -06001048 if ( nlsr->debugging )
1049 printf("print_routing_table called\n");
1050 if ( nlsr->detailed_logging )
1051 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001052 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -05001053
1054 struct routing_table_entry *rte;
1055
1056 struct hashtb_enumerator ee;
1057 struct hashtb_enumerator *e = &ee;
1058
1059 hashtb_start(nlsr->routing_table, e);
1060 rt_element=hashtb_n(nlsr->routing_table);
1061
1062 for(i=0;i<rt_element;i++)
1063 {
akmhoque1771c412012-11-09 13:06:08 -06001064 if ( nlsr->debugging )
1065 printf("----------Routing Table Entry %d------------------\n",i+1);
1066 if ( nlsr->detailed_logging )
1067 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
1068
akmhoque3560cb62012-09-09 10:52:30 -05001069 rte=e->data;
akmhoque1771c412012-11-09 13:06:08 -06001070
1071 if ( nlsr->debugging )
1072 printf(" Destination Router: %s \n",rte->dest_router);
1073 if ( nlsr->detailed_logging )
1074 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
1075
1076
akmhoquede61ba92012-09-20 22:19:12 -05001077 //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);
1078
1079 struct face_list_entry *fle;
1080
1081 struct hashtb_enumerator eef;
1082 struct hashtb_enumerator *ef = &eef;
1083
1084 hashtb_start(rte->face_list, ef);
1085 face_list_element=hashtb_n(rte->face_list);
1086 if ( face_list_element <= 0 )
1087 {
akmhoque1771c412012-11-09 13:06:08 -06001088 if ( nlsr->debugging )
1089 printf(" Face: No Face \n");
1090 if ( nlsr->detailed_logging )
1091 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -05001092 }
1093 else
1094 {
1095 for(j=0;j<face_list_element;j++)
1096 {
1097 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -06001098 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -06001099 printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
akmhoque1771c412012-11-09 13:06:08 -06001100 if ( nlsr->detailed_logging )
1101 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -05001102 hashtb_next(ef);
1103 }
1104 }
1105 hashtb_end(ef);
1106
akmhoque3560cb62012-09-09 10:52:30 -05001107 hashtb_next(e);
1108 }
1109
1110 hashtb_end(e);
1111
akmhoque3171d652012-11-13 11:44:33 -06001112 if ( nlsr->debugging )
1113 printf("\n");
1114 if ( nlsr->detailed_logging )
1115 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -05001116}
1117
akmhoque63152c62012-09-18 08:43:42 -05001118
1119int
1120delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1121{
akmhoque1771c412012-11-09 13:06:08 -06001122 if ( nlsr->debugging )
1123 {
1124 printf("delete_empty_rte called\n");
1125 printf("Router: %s \n",(char *)ev->evdata);
1126 }
1127 if ( nlsr->detailed_logging )
1128 {
1129 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1130 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
akmhoque1771c412012-11-09 13:06:08 -06001131 }
1132
akmhoque63152c62012-09-18 08:43:42 -05001133 if(flags == CCN_SCHEDULE_CANCEL)
1134 {
1135 return -1;
1136 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001137
1138 nlsr_lock();
akmhoque63152c62012-09-18 08:43:42 -05001139 int res;
1140 struct hashtb_enumerator ee;
1141 struct hashtb_enumerator *e = &ee;
1142
1143 hashtb_start(nlsr->routing_table, e);
1144 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1145
1146 if ( res == HT_OLD_ENTRY )
1147 {
1148 hashtb_delete(e);
1149 }
1150 else if ( res == HT_NEW_ENTRY )
1151 {
1152 hashtb_delete(e);
1153 }
1154
akmhoquede61ba92012-09-20 22:19:12 -05001155 print_routing_table();
akmhoqueb77b95f2013-02-08 12:28:47 -06001156
1157 nlsr_unlock();
akmhoquede61ba92012-09-20 22:19:12 -05001158
akmhoque63152c62012-09-18 08:43:42 -05001159 return 0;
1160}
1161
akmhoquede61ba92012-09-20 22:19:12 -05001162void
akmhoque3cced642012-09-24 16:20:20 -05001163clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001164{
akmhoque1771c412012-11-09 13:06:08 -06001165 if ( nlsr->debugging )
1166 printf("clear_old_routing_table called\n");
1167 if ( nlsr->detailed_logging )
1168 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001169 int i,rt_element;
1170
1171 struct routing_table_entry *rte;
1172
1173 struct hashtb_enumerator ee;
1174 struct hashtb_enumerator *e = &ee;
1175
1176 hashtb_start(nlsr->routing_table, e);
1177 rt_element=hashtb_n(nlsr->routing_table);
1178
1179 for(i=0;i<rt_element;i++)
1180 {
1181 rte=e->data;
1182 hashtb_destroy(&rte->face_list);
1183 struct hashtb_param param_fle = {0};
1184 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1185
1186 hashtb_next(e);
1187 }
1188
1189 hashtb_end(e);
1190}
1191
akmhoque63152c62012-09-18 08:43:42 -05001192
akmhoque3560cb62012-09-09 10:52:30 -05001193void
akmhoque3cced642012-09-24 16:20:20 -05001194do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001195{
akmhoque1771c412012-11-09 13:06:08 -06001196 if ( nlsr->debugging )
1197 printf("do_old_routing_table_updates called\n");
1198 if ( nlsr->detailed_logging )
1199 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1200
akmhoque810a5b52012-09-09 16:53:14 -05001201 int i, rt_element;
1202 int mapping_no;
1203
1204 struct routing_table_entry *rte;
1205
1206 struct hashtb_enumerator ee;
1207 struct hashtb_enumerator *e = &ee;
1208
1209 hashtb_start(nlsr->routing_table, e);
1210 rt_element=hashtb_n(nlsr->routing_table);
1211
1212 for(i=0;i<rt_element;i++)
1213 {
1214 rte=e->data;
1215 mapping_no=get_mapping_no(rte->dest_router);
1216 if ( mapping_no == NO_MAPPING_NUM)
1217 {
akmhoquede61ba92012-09-20 22:19:12 -05001218 delete_orig_router_from_npt(rte->dest_router);
akmhoque63152c62012-09-18 08:43:42 -05001219 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1220 memset(router,0,strlen(rte->dest_router)+1);
1221 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1222 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque810a5b52012-09-09 16:53:14 -05001223 }
1224 hashtb_next(e);
1225 }
1226
1227 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001228}
akmhoque29c1db52012-09-07 14:47:43 -05001229
akmhoqueb77b95f2013-02-08 12:28:47 -06001230void
1231update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist)
1232{
1233 if ( nlsr->debugging )
1234 printf("update_routing_table_with_new_hyperbolic_route called\n");
1235 if ( nlsr->detailed_logging )
1236 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n");
1237
1238 char *orig_router=get_router_from_rev_map(dest_router_rev_map_index);
1239
1240 if (face != NO_NEXT_HOP && face != NO_FACE )
1241 {
1242 update_routing_table(orig_router,face,nbr_to_dest_dist);
1243 if ( nlsr->debugging )
1244 printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1245 if ( nlsr->detailed_logging )
1246 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1247
1248 }
1249
1250
1251}
akmhoquede61ba92012-09-20 22:19:12 -05001252
1253
akmhoquefbfd0982012-09-09 20:59:03 -05001254void
akmhoquede61ba92012-09-20 22:19:12 -05001255update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001256{
akmhoque1771c412012-11-09 13:06:08 -06001257 if ( nlsr->debugging )
1258 printf("update_routing_table_with_new_route called\n");
1259 if ( nlsr->detailed_logging )
1260 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001261 int i, map_element;
1262 struct map_entry *me;
1263
1264 struct hashtb_enumerator ee;
1265 struct hashtb_enumerator *e = &ee;
1266
1267 hashtb_start(nlsr->rev_map, e);
1268 map_element=hashtb_n(nlsr->rev_map);
1269
1270 for(i=0;i<map_element;i++)
1271 {
1272 me=e->data;
1273 if(me->mapping != source)
1274 {
1275
1276 char *orig_router=get_router_from_rev_map(me->mapping);
1277 if (orig_router != NULL )
1278 {
1279 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoquefbfd0982012-09-09 20:59:03 -05001280 if ( next_hop_router_num == NO_NEXT_HOP )
1281 {
akmhoque1771c412012-11-09 13:06:08 -06001282 if ( nlsr->debugging )
1283 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1284 if ( nlsr->detailed_logging )
1285 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001286 }
1287 else
1288 {
1289 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoquefbfd0982012-09-09 20:59:03 -05001290 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001291 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoque1771c412012-11-09 13:06:08 -06001292 if ( nlsr->debugging )
1293 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1294 if ( nlsr->detailed_logging )
1295 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1296
akmhoquefbfd0982012-09-09 20:59:03 -05001297
1298 }
1299 }
1300 }
1301 hashtb_next(e);
1302 }
1303
1304 hashtb_end(e);
1305}
1306
akmhoquede61ba92012-09-20 22:19:12 -05001307int
1308does_face_exist_for_router(char *dest_router, int face_id)
1309{
akmhoqueb77b95f2013-02-08 12:28:47 -06001310 if (nlsr->debugging)
1311 {
1312 printf("does_face_exist_for_router called\n");
1313 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1314 }
1315
akmhoquede61ba92012-09-20 22:19:12 -05001316 int ret=0;
1317
akmhoqueb77b95f2013-02-08 12:28:47 -06001318 int res;
akmhoquede61ba92012-09-20 22:19:12 -05001319 struct routing_table_entry *rte;
1320
1321 struct hashtb_enumerator ee;
1322 struct hashtb_enumerator *e = &ee;
1323
1324 hashtb_start(nlsr->routing_table, e);
1325 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1326
1327 if( res == HT_OLD_ENTRY )
1328 {
1329 rte=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001330 unsigned *v;
1331 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1332 if (v != NULL)
1333 ret = 1;
akmhoquede61ba92012-09-20 22:19:12 -05001334 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001335 else
akmhoquede61ba92012-09-20 22:19:12 -05001336 {
1337 hashtb_delete(e);
1338 }
1339
1340 hashtb_end(e);
1341
1342 return ret;
1343}
akmhoqueb77b95f2013-02-08 12:28:47 -06001344
1345double
1346get_hyperbolic_distance(long int source, long int dest)
1347{
1348 double distance;
1349 char *src_router=get_router_from_rev_map(source);
1350 char *dest_router=get_router_from_rev_map(dest);
1351
1352 double src_r=get_hyperbolic_r(src_router);
1353 double src_theta=get_hyperbolic_r(src_router);
1354
1355 double dest_r=get_hyperbolic_r(dest_router);
1356 double dest_theta=get_hyperbolic_r(dest_router);
1357 if ( src_r != -1 && dest_r != -1 )
1358 {
1359 distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta));
1360 }
1361 else
1362 {
1363 distance= -1.0;
1364 }
1365
1366 return distance;
1367}