blob: 6b45ed2dac3e8dfdf1721076280eafc4ad32ff3b [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;
akmhoque866c2222013-02-12 10:49:33 -0600556 //char *face;
akmhoque29c1db52012-09-07 14:47:43 -0500557 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);
akmhoque866c2222013-02-12 10:49:33 -0600563 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500564 metric=strtok_r(NULL,sep,&rem);
565
akmhoqueb28579d2013-02-12 11:15:52 -0600566 if ( !nlsr->debugging && nlsr->debugging)
567 {
568 printf("Metric: %s Length:%s\n",metric,length);
569 }
akmhoque29c1db52012-09-07 14:47:43 -0500570 add_map_entry(rtr_id);
571
572 for(i=1;i<no_link;i++)
573 {
574 rtr_id=strtok_r(NULL,sep,&rem);
575 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600576 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500577 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600578 if ( !nlsr->debugging && nlsr->debugging)
579 {
580 printf("Metric: %s Length:%s\n",metric,length);
581 }
akmhoque29c1db52012-09-07 14:47:43 -0500582
583 add_map_entry(rtr_id);
584
585 }
586 }
587
588 free(lsa_data);
589}
590
591int
592get_mapping_no(char *router)
593{
594 struct map_entry *me;
595
596 struct hashtb_enumerator ee;
597 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500598 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500599 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500600
akmhoque63152c62012-09-18 08:43:42 -0500601 int n = hashtb_n(nlsr->map);
602
603 if ( n < 1)
604 {
605 return NO_MAPPING_NUM;
606 }
607
akmhoque29c1db52012-09-07 14:47:43 -0500608 hashtb_start(nlsr->map, e);
609 res = hashtb_seek(e, router, strlen(router), 0);
610
611 if( res == HT_OLD_ENTRY )
612 {
613 me=e->data;
614 ret=me->mapping;
615 }
616 else if(res == HT_NEW_ENTRY)
617 {
618 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500619 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500620 }
621
622 hashtb_end(e);
623
624 return ret;
625
626}
627
akmhoquefbfd0982012-09-09 20:59:03 -0500628
629void
630add_rev_map_entry(long int mapping_number, char *router)
631{
632
633 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
634
635 struct hashtb_enumerator ee;
636 struct hashtb_enumerator *e = &ee;
637 int res;
638
639 hashtb_start(nlsr->rev_map, e);
640 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
641
642 if(res == HT_NEW_ENTRY)
643 {
644 me=e->data;
645 me->router=(char *)malloc(strlen(router)+1);
646 memset(me->router,0,strlen(router)+1);
647 memcpy(me->router,router,strlen(router));
648 me->mapping=mapping_number;
649 }
650
651 hashtb_end(e);
652
653}
654
655
656
657char *
658get_router_from_rev_map(long int mapping_number)
659{
660
661 struct map_entry *me;
662
663 struct hashtb_enumerator ee;
664 struct hashtb_enumerator *e = &ee;
665 int res;
666
667 hashtb_start(nlsr->rev_map, e);
668 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
669
670 if(res == HT_OLD_ENTRY)
671 {
672 me=e->data;
673 hashtb_end(e);
674 return me->router;
675 }
676 else if(res == HT_NEW_ENTRY)
677 {
678 hashtb_delete(e);
679 hashtb_end(e);
680 }
681
682 return NULL;
683}
684
685void
686print_rev_map(void)
687{
688 int i, map_element;
689 struct map_entry *me;
690
691 struct hashtb_enumerator ee;
692 struct hashtb_enumerator *e = &ee;
693
694 hashtb_start(nlsr->map, e);
695 map_element=hashtb_n(nlsr->map);
696
697 for(i=0;i<map_element;i++)
698 {
699 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600700 if ( nlsr->debugging )
701 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
702 if ( nlsr->detailed_logging )
703 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
akmhoquefbfd0982012-09-09 20:59:03 -0500704 hashtb_next(e);
705 }
706
707 hashtb_end(e);
708}
709
710
akmhoque29c1db52012-09-07 14:47:43 -0500711void
712assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
713{
714 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
715 int mapping_nbr_router;
716
717 int i;
718 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
719 memset( lsa_data,0,strlen(adj_lsa->body)+1);
720 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
721 char *sep="|";
722 char *rem;
723 char *rtr_id;
724 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600725 //char *face;
akmhoque29c1db52012-09-07 14:47:43 -0500726 char *metric;
727
728 if(adj_lsa->no_link >0 )
729 {
730 rtr_id=strtok_r(lsa_data,sep,&rem);
731 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600732 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500733 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600734
735 if ( !nlsr->debugging && nlsr->debugging)
736 {
737 printf("Metric: %s Length:%s\n",metric,length);
738 }
akmhoque29c1db52012-09-07 14:47:43 -0500739
740 mapping_nbr_router=get_mapping_no(rtr_id);
741 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
742
743 for(i=1;i<adj_lsa->no_link;i++)
744 {
745 rtr_id=strtok_r(NULL,sep,&rem);
746 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600747 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500748 metric=strtok_r(NULL,sep,&rem);
749
akmhoqueb28579d2013-02-12 11:15:52 -0600750 if ( !nlsr->debugging && nlsr->debugging)
751 {
752 printf("Metric: %s Length:%s\n",metric,length);
753 }
754
akmhoque29c1db52012-09-07 14:47:43 -0500755 mapping_nbr_router=get_mapping_no(rtr_id);
756 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
757
758 }
759 }
760
761 free(lsa_data);
762}
763
764void
765make_adj_matrix(int **adj_matrix,int map_element)
766{
767
768 init_adj_matrix(adj_matrix,map_element);
769
770 int i, adj_lsdb_element;
771 struct alsa *adj_lsa;
772
773 struct hashtb_enumerator ee;
774 struct hashtb_enumerator *e = &ee;
775
776 hashtb_start(nlsr->lsdb->adj_lsdb, e);
777 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
778
779 for(i=0;i<adj_lsdb_element;i++)
780 {
781 adj_lsa=e->data;
782 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
783 hashtb_next(e);
784 }
785
786 hashtb_end(e);
787
788}
789
790void
791init_adj_matrix(int **adj_matrix,int map_element)
792{
793 int i, j;
794 for(i=0;i<map_element;i++)
795 for(j=0;j<map_element;j++)
796 adj_matrix[i][j]=0;
797}
798
799void print_adj_matrix(int **adj_matrix, int map_element)
800{
801 int i, j;
802 for(i=0;i<map_element;i++)
803 {
804 for(j=0;j<map_element;j++)
805 printf("%d ",adj_matrix[i][j]);
806 printf("\n");
807 }
808}
809
akmhoquede61ba92012-09-20 22:19:12 -0500810
akmhoque3560cb62012-09-09 10:52:30 -0500811int
akmhoque9fe296b2012-09-24 09:52:08 -0500812get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
813{
814 int no_link=0;
815 int i;
816
817 for(i=0;i<V;i++)
818 {
819 if ( adj_matrix[S][i] > 0 )
820 {
821 no_link++;
822 }
823 }
824 return no_link;
825}
826
827void
828get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
829{
830 int i,j;
831 j=0;
832 for (i=0; i <V; i++)
833 {
834 if ( adj_matrix[S][i] > 0 )
835 {
836 links[j]=i;
837 link_costs[j]=adj_matrix[S][i];
838 j++;
839 }
840 }
841}
842
843void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
844{
845 int i;
846 for ( i = 0; i < V; i++ )
847 {
848 if ( i == link )
849 {
850 adj_matrix[S][i]=link_cost;
851 }
852 else
853 {
854 adj_matrix[S][i]=0;
855 }
856 }
857
858}
859
860int
akmhoquede61ba92012-09-20 22:19:12 -0500861get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500862{
863 struct routing_table_entry *rte;
864
865 struct hashtb_enumerator ee;
866 struct hashtb_enumerator *e = &ee;
867 int res,ret;
868
869 hashtb_start(nlsr->routing_table, e);
870 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
871
872 if( res == HT_OLD_ENTRY )
873 {
874 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500875 ret=hashtb_n(rte->face_list);
876 //nhl=rte->face_list;
877 }
878 else if(res == HT_NEW_ENTRY)
879 {
880 hashtb_delete(e);
881 ret=NO_NEXT_HOP;
882 }
883
884 hashtb_end(e);
885
886 return ret;
887}
888
889
890int
891get_next_hop(char *dest_router,int *faces, int *route_costs)
892{
893 struct routing_table_entry *rte;
894
895 struct hashtb_enumerator ee;
896 struct hashtb_enumerator *e = &ee;
897 int res,ret;
898
899 hashtb_start(nlsr->routing_table, e);
900 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
901
902 if( res == HT_OLD_ENTRY )
903 {
904 rte=e->data;
905 ret=hashtb_n(rte->face_list);
906 //nhl=rte->face_list;
907 int j,face_list_element;
908 struct face_list_entry *fle;
909
910 struct hashtb_enumerator eef;
911 struct hashtb_enumerator *ef = &eef;
912
913 hashtb_start(rte->face_list, ef);
914 face_list_element=hashtb_n(rte->face_list);
915 for(j=0;j<face_list_element;j++)
916 {
917 fle=ef->data;
918 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
919 faces[j]=fle->next_hop_face;
920 route_costs[j]=fle->route_cost;
921 hashtb_next(ef);
922 }
923 hashtb_end(ef);
924
akmhoque3560cb62012-09-09 10:52:30 -0500925 }
926 else if(res == HT_NEW_ENTRY)
927 {
928 hashtb_delete(e);
929 ret=NO_NEXT_HOP;
930 }
931
932 hashtb_end(e);
933
934 return ret;
935}
936
937void
938add_next_hop_router(char *dest_router)
939{
940 if ( strcmp(dest_router,nlsr->router_name)== 0)
941 {
942 return ;
943 }
944
945 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
946
947 struct hashtb_enumerator ee;
948 struct hashtb_enumerator *e = &ee;
949 int res;
950
951 hashtb_start(nlsr->routing_table, e);
952 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
953
954 if( res == HT_NEW_ENTRY )
955 {
956 rte=e->data;
957 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
958 memset(rte->dest_router,0,strlen(dest_router)+1);
959 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500960 //rte->next_hop_face=NO_NEXT_HOP;
961 struct hashtb_param param_fle = {0};
962 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
963
964 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500965 }
966 hashtb_end(e);
967
968}
969
970void
971add_next_hop_from_lsa_adj_body(char *body, int no_link)
972{
973
974 int i=0;
975 char *lsa_data=(char *)malloc(strlen(body)+1);
976 memset( lsa_data,0,strlen(body)+1);
977 memcpy(lsa_data,body,strlen(body)+1);
978 char *sep="|";
979 char *rem;
980 char *rtr_id;
981 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600982 //char *face;
akmhoque3560cb62012-09-09 10:52:30 -0500983 char *metric;
984
985 if(no_link >0 )
986 {
987 rtr_id=strtok_r(lsa_data,sep,&rem);
988 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600989 //face=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500990 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600991 if ( !nlsr->debugging && nlsr->debugging)
992 {
993 printf("Metric: %s Length:%s\n",metric,length);
994 }
akmhoque3560cb62012-09-09 10:52:30 -0500995
akmhoque3560cb62012-09-09 10:52:30 -0500996
997 add_next_hop_router(rtr_id);
998
999 for(i=1;i<no_link;i++)
1000 {
1001 rtr_id=strtok_r(NULL,sep,&rem);
1002 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001003 //face=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -05001004 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001005 if ( !nlsr->debugging && nlsr->debugging)
1006 {
1007 printf("Metric: %s Length:%s\n",metric,length);
1008 }
akmhoque3560cb62012-09-09 10:52:30 -05001009
1010 add_next_hop_router(rtr_id);
1011
1012 }
1013 }
1014
1015 free(lsa_data);
1016
1017
1018}
1019
1020void
akmhoquede61ba92012-09-20 22:19:12 -05001021update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -05001022{
akmhoqueb77b95f2013-02-08 12:28:47 -06001023 if ( nlsr->debugging )
1024 {
1025 printf("update_routing_table called \n");
1026 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
1027 }
1028
akmhoquede61ba92012-09-20 22:19:12 -05001029 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -05001030 struct routing_table_entry *rte;
1031
1032 struct hashtb_enumerator ee;
1033 struct hashtb_enumerator *e = &ee;
1034
1035 hashtb_start(nlsr->routing_table, e);
1036 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1037
1038 if( res == HT_OLD_ENTRY )
1039 {
1040 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -05001041
1042 struct hashtb_enumerator eef;
1043 struct hashtb_enumerator *ef = &eef;
1044
1045 hashtb_start(rte->face_list, ef);
1046 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
1047 if( res1 == HT_NEW_ENTRY)
1048 {
1049 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
1050 fle=ef->data;
1051 fle->next_hop_face=next_hop_face;
1052 fle->route_cost=route_cost;
1053 }
1054 else if ( res1 == HT_OLD_ENTRY )
1055 {
1056 struct face_list_entry *fle;
1057 fle=ef->data;
1058 fle->route_cost=route_cost;
1059 }
1060 hashtb_end(ef);
akmhoqueffacaa82012-09-13 17:48:30 -05001061 }
1062 else if ( res == HT_OLD_ENTRY )
1063 {
1064 hashtb_delete(e);
1065 }
1066
1067 hashtb_end(e);
1068
1069}
1070
1071void
akmhoque3560cb62012-09-09 10:52:30 -05001072print_routing_table(void)
1073{
akmhoque1771c412012-11-09 13:06:08 -06001074 if ( nlsr->debugging )
1075 printf("print_routing_table called\n");
1076 if ( nlsr->detailed_logging )
1077 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001078 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -05001079
1080 struct routing_table_entry *rte;
1081
1082 struct hashtb_enumerator ee;
1083 struct hashtb_enumerator *e = &ee;
1084
1085 hashtb_start(nlsr->routing_table, e);
1086 rt_element=hashtb_n(nlsr->routing_table);
1087
1088 for(i=0;i<rt_element;i++)
1089 {
akmhoque1771c412012-11-09 13:06:08 -06001090 if ( nlsr->debugging )
1091 printf("----------Routing Table Entry %d------------------\n",i+1);
1092 if ( nlsr->detailed_logging )
1093 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
1094
akmhoque3560cb62012-09-09 10:52:30 -05001095 rte=e->data;
akmhoque1771c412012-11-09 13:06:08 -06001096
1097 if ( nlsr->debugging )
1098 printf(" Destination Router: %s \n",rte->dest_router);
1099 if ( nlsr->detailed_logging )
1100 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
1101
1102
akmhoquede61ba92012-09-20 22:19:12 -05001103 //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);
1104
1105 struct face_list_entry *fle;
1106
1107 struct hashtb_enumerator eef;
1108 struct hashtb_enumerator *ef = &eef;
1109
1110 hashtb_start(rte->face_list, ef);
1111 face_list_element=hashtb_n(rte->face_list);
1112 if ( face_list_element <= 0 )
1113 {
akmhoque1771c412012-11-09 13:06:08 -06001114 if ( nlsr->debugging )
1115 printf(" Face: No Face \n");
1116 if ( nlsr->detailed_logging )
1117 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -05001118 }
1119 else
1120 {
1121 for(j=0;j<face_list_element;j++)
1122 {
1123 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -06001124 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -06001125 printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
akmhoque1771c412012-11-09 13:06:08 -06001126 if ( nlsr->detailed_logging )
1127 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -05001128 hashtb_next(ef);
1129 }
1130 }
1131 hashtb_end(ef);
1132
akmhoque3560cb62012-09-09 10:52:30 -05001133 hashtb_next(e);
1134 }
1135
1136 hashtb_end(e);
1137
akmhoque3171d652012-11-13 11:44:33 -06001138 if ( nlsr->debugging )
1139 printf("\n");
1140 if ( nlsr->detailed_logging )
1141 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -05001142}
1143
akmhoque866c2222013-02-12 10:49:33 -06001144/*
akmhoque63152c62012-09-18 08:43:42 -05001145int
1146delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1147{
akmhoque1771c412012-11-09 13:06:08 -06001148 if ( nlsr->debugging )
1149 {
1150 printf("delete_empty_rte called\n");
1151 printf("Router: %s \n",(char *)ev->evdata);
1152 }
1153 if ( nlsr->detailed_logging )
1154 {
1155 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1156 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
akmhoque1771c412012-11-09 13:06:08 -06001157 }
1158
akmhoque63152c62012-09-18 08:43:42 -05001159 if(flags == CCN_SCHEDULE_CANCEL)
1160 {
1161 return -1;
1162 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001163
1164 nlsr_lock();
akmhoque63152c62012-09-18 08:43:42 -05001165 int res;
1166 struct hashtb_enumerator ee;
1167 struct hashtb_enumerator *e = &ee;
1168
1169 hashtb_start(nlsr->routing_table, e);
1170 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1171
1172 if ( res == HT_OLD_ENTRY )
1173 {
1174 hashtb_delete(e);
1175 }
1176 else if ( res == HT_NEW_ENTRY )
1177 {
1178 hashtb_delete(e);
1179 }
1180
akmhoquede61ba92012-09-20 22:19:12 -05001181 print_routing_table();
akmhoqueb77b95f2013-02-08 12:28:47 -06001182
1183 nlsr_unlock();
akmhoquede61ba92012-09-20 22:19:12 -05001184
akmhoque63152c62012-09-18 08:43:42 -05001185 return 0;
1186}
akmhoque866c2222013-02-12 10:49:33 -06001187*/
akmhoque63152c62012-09-18 08:43:42 -05001188
akmhoquede61ba92012-09-20 22:19:12 -05001189void
akmhoque3cced642012-09-24 16:20:20 -05001190clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001191{
akmhoque1771c412012-11-09 13:06:08 -06001192 if ( nlsr->debugging )
1193 printf("clear_old_routing_table called\n");
1194 if ( nlsr->detailed_logging )
1195 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001196 int i,rt_element;
1197
1198 struct routing_table_entry *rte;
1199
1200 struct hashtb_enumerator ee;
1201 struct hashtb_enumerator *e = &ee;
1202
1203 hashtb_start(nlsr->routing_table, e);
1204 rt_element=hashtb_n(nlsr->routing_table);
1205
1206 for(i=0;i<rt_element;i++)
1207 {
1208 rte=e->data;
1209 hashtb_destroy(&rte->face_list);
1210 struct hashtb_param param_fle = {0};
1211 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1212
1213 hashtb_next(e);
1214 }
1215
1216 hashtb_end(e);
1217}
1218
akmhoque63152c62012-09-18 08:43:42 -05001219
akmhoque3560cb62012-09-09 10:52:30 -05001220void
akmhoque3cced642012-09-24 16:20:20 -05001221do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001222{
akmhoque1771c412012-11-09 13:06:08 -06001223 if ( nlsr->debugging )
1224 printf("do_old_routing_table_updates called\n");
1225 if ( nlsr->detailed_logging )
1226 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1227
akmhoque810a5b52012-09-09 16:53:14 -05001228 int i, rt_element;
1229 int mapping_no;
1230
1231 struct routing_table_entry *rte;
1232
1233 struct hashtb_enumerator ee;
1234 struct hashtb_enumerator *e = &ee;
1235
1236 hashtb_start(nlsr->routing_table, e);
1237 rt_element=hashtb_n(nlsr->routing_table);
1238
1239 for(i=0;i<rt_element;i++)
1240 {
1241 rte=e->data;
1242 mapping_no=get_mapping_no(rte->dest_router);
1243 if ( mapping_no == NO_MAPPING_NUM)
1244 {
akmhoquede61ba92012-09-20 22:19:12 -05001245 delete_orig_router_from_npt(rte->dest_router);
akmhoque866c2222013-02-12 10:49:33 -06001246 /*
akmhoque63152c62012-09-18 08:43:42 -05001247 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1248 memset(router,0,strlen(rte->dest_router)+1);
1249 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1250 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque866c2222013-02-12 10:49:33 -06001251 */
1252 hashtb_delete(e);
1253 i++;
akmhoque810a5b52012-09-09 16:53:14 -05001254 }
akmhoque866c2222013-02-12 10:49:33 -06001255 else
1256 {
1257 hashtb_next(e);
1258 }
akmhoque810a5b52012-09-09 16:53:14 -05001259 }
1260
1261 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001262}
akmhoque29c1db52012-09-07 14:47:43 -05001263
akmhoqueb77b95f2013-02-08 12:28:47 -06001264void
1265update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist)
1266{
1267 if ( nlsr->debugging )
1268 printf("update_routing_table_with_new_hyperbolic_route called\n");
1269 if ( nlsr->detailed_logging )
1270 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n");
1271
1272 char *orig_router=get_router_from_rev_map(dest_router_rev_map_index);
1273
1274 if (face != NO_NEXT_HOP && face != NO_FACE )
1275 {
1276 update_routing_table(orig_router,face,nbr_to_dest_dist);
1277 if ( nlsr->debugging )
1278 printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1279 if ( nlsr->detailed_logging )
1280 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1281
1282 }
1283
1284
1285}
akmhoquede61ba92012-09-20 22:19:12 -05001286
1287
akmhoquefbfd0982012-09-09 20:59:03 -05001288void
akmhoquede61ba92012-09-20 22:19:12 -05001289update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001290{
akmhoque1771c412012-11-09 13:06:08 -06001291 if ( nlsr->debugging )
1292 printf("update_routing_table_with_new_route called\n");
1293 if ( nlsr->detailed_logging )
1294 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001295 int i, map_element;
1296 struct map_entry *me;
1297
1298 struct hashtb_enumerator ee;
1299 struct hashtb_enumerator *e = &ee;
1300
1301 hashtb_start(nlsr->rev_map, e);
1302 map_element=hashtb_n(nlsr->rev_map);
1303
1304 for(i=0;i<map_element;i++)
1305 {
1306 me=e->data;
1307 if(me->mapping != source)
1308 {
1309
1310 char *orig_router=get_router_from_rev_map(me->mapping);
1311 if (orig_router != NULL )
1312 {
1313 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoquefbfd0982012-09-09 20:59:03 -05001314 if ( next_hop_router_num == NO_NEXT_HOP )
1315 {
akmhoque1771c412012-11-09 13:06:08 -06001316 if ( nlsr->debugging )
1317 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1318 if ( nlsr->detailed_logging )
1319 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001320 }
1321 else
1322 {
1323 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoquefbfd0982012-09-09 20:59:03 -05001324 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001325 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoque1771c412012-11-09 13:06:08 -06001326 if ( nlsr->debugging )
1327 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1328 if ( nlsr->detailed_logging )
1329 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1330
akmhoquefbfd0982012-09-09 20:59:03 -05001331
1332 }
1333 }
1334 }
1335 hashtb_next(e);
1336 }
1337
1338 hashtb_end(e);
1339}
1340
akmhoquede61ba92012-09-20 22:19:12 -05001341int
1342does_face_exist_for_router(char *dest_router, int face_id)
1343{
akmhoqueb77b95f2013-02-08 12:28:47 -06001344 if (nlsr->debugging)
1345 {
1346 printf("does_face_exist_for_router called\n");
1347 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1348 }
1349
akmhoquede61ba92012-09-20 22:19:12 -05001350 int ret=0;
1351
akmhoqueb77b95f2013-02-08 12:28:47 -06001352 int res;
akmhoquede61ba92012-09-20 22:19:12 -05001353 struct routing_table_entry *rte;
1354
1355 struct hashtb_enumerator ee;
1356 struct hashtb_enumerator *e = &ee;
1357
1358 hashtb_start(nlsr->routing_table, e);
1359 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1360
1361 if( res == HT_OLD_ENTRY )
1362 {
1363 rte=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001364 unsigned *v;
1365 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1366 if (v != NULL)
1367 ret = 1;
akmhoquede61ba92012-09-20 22:19:12 -05001368 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001369 else
akmhoquede61ba92012-09-20 22:19:12 -05001370 {
1371 hashtb_delete(e);
1372 }
1373
1374 hashtb_end(e);
1375
1376 return ret;
1377}
akmhoqueb77b95f2013-02-08 12:28:47 -06001378
1379double
1380get_hyperbolic_distance(long int source, long int dest)
1381{
1382 double distance;
1383 char *src_router=get_router_from_rev_map(source);
1384 char *dest_router=get_router_from_rev_map(dest);
1385
1386 double src_r=get_hyperbolic_r(src_router);
1387 double src_theta=get_hyperbolic_r(src_router);
1388
1389 double dest_r=get_hyperbolic_r(dest_router);
1390 double dest_theta=get_hyperbolic_r(dest_router);
1391 if ( src_r != -1 && dest_r != -1 )
1392 {
1393 distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta));
1394 }
1395 else
1396 {
1397 distance= -1.0;
1398 }
1399
1400 return distance;
1401}