blob: 9c3b80b3f84ef47d0a756ec897e0600435c32ba9 [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
akmhoque0b60ba92013-02-25 17:55:35 -060053 //struct hashtb_param param_me = {0};
54 nlsr->map = hashtb_create(sizeof(struct map_entry), NULL);
55 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), NULL);
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);
akmhoque54a14f42013-02-24 06:16:20 -0600195 destroy_map();
196 destroy_rev_map();
197 //hashtb_destroy(&nlsr->map);
198 //hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -0500199
200 }
201 nlsr->is_route_calculation_scheduled=0;
202
akmhoqueffacaa82012-09-13 17:48:30 -0500203 nlsr_unlock();
204
akmhoque29c1db52012-09-07 14:47:43 -0500205 return 0;
206}
207
208void
akmhoquede61ba92012-09-20 22:19:12 -0500209calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500210{
211 int i;
212 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500213 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500214 long int *Q=(long int *)malloc(V * sizeof(long int));
215 long int head=0;
akmhoqueb77b95f2013-02-08 12:28:47 -0600216 /* Initiate the Parent */
akmhoque29c1db52012-09-07 14:47:43 -0500217 for (i = 0 ; i < V; i++)
218 {
219 parent[i]=EMPTY_PARENT;
220 dist[i]=INF_DISTANCE;
221 Q[i]=i;
222 }
223
akmhoque9fe296b2012-09-24 09:52:08 -0500224 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500225 {
akmhoque9fe296b2012-09-24 09:52:08 -0500226 dist[S]=0;
227 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500228
akmhoque9fe296b2012-09-24 09:52:08 -0500229 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500230 {
akmhoque9fe296b2012-09-24 09:52:08 -0500231 u=Q[head];
232 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500233 {
akmhoque9fe296b2012-09-24 09:52:08 -0500234 break;
235 }
236
237 for(v=0 ; v <V; v++)
238 {
239 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500240 {
akmhoque9fe296b2012-09-24 09:52:08 -0500241 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500242 {
akmhoque9fe296b2012-09-24 09:52:08 -0500243
244 if( dist[u] + adj_matrix[u][v] < dist[v])
245 {
246 dist[v]=dist[u] + adj_matrix[u][v] ;
247 parent[v]=u;
248 }
249
250 }
akmhoque29c1db52012-09-07 14:47:43 -0500251
252 }
253
254 }
255
akmhoque9fe296b2012-09-24 09:52:08 -0500256 head++;
257 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500258 }
akmhoque29c1db52012-09-07 14:47:43 -0500259 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600260 free(Q);
akmhoque29c1db52012-09-07 14:47:43 -0500261}
262
263void
264print_all_path_from_source(long int *parent,long int source)
265{
266 int i, map_element;
267 struct map_entry *me;
268
269 struct hashtb_enumerator ee;
270 struct hashtb_enumerator *e = &ee;
271
272 hashtb_start(nlsr->map, e);
273 map_element=hashtb_n(nlsr->map);
274
akmhoque9fe296b2012-09-24 09:52:08 -0500275 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500276 {
akmhoque9fe296b2012-09-24 09:52:08 -0500277 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500278 {
akmhoque9fe296b2012-09-24 09:52:08 -0500279 me=e->data;
280 if(me->mapping != source)
281 {
akmhoque3171d652012-11-13 11:44:33 -0600282
283 if ( nlsr->debugging )
284 {
285 print_path(parent,(long int)me->mapping);
286 printf("\n");
287 }
288 if ( nlsr->detailed_logging )
289 {
290 print_path(parent,(long int)me->mapping);
291 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
292 }
293
akmhoque9fe296b2012-09-24 09:52:08 -0500294 }
295 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500296 }
akmhoque29c1db52012-09-07 14:47:43 -0500297 }
akmhoque29c1db52012-09-07 14:47:43 -0500298 hashtb_end(e);
299
300}
301
akmhoquefbfd0982012-09-09 20:59:03 -0500302void
303print_all_next_hop(long int *parent,long int source)
304{
305 int i, map_element;
306 struct map_entry *me;
307
308 struct hashtb_enumerator ee;
309 struct hashtb_enumerator *e = &ee;
310
311 hashtb_start(nlsr->map, e);
312 map_element=hashtb_n(nlsr->map);
313
314 for(i=0;i<map_element;i++)
315 {
316 me=e->data;
317 if(me->mapping != source)
318 {
akmhoque1771c412012-11-09 13:06:08 -0600319 if ( nlsr->debugging )
320 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
321 if ( nlsr->detailed_logging )
322 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 -0500323 }
324 hashtb_next(e);
325 }
326
327 hashtb_end(e);
328
329}
330
331long int
332get_next_hop_from_calculation(long int *parent, long int dest,long int source)
333{
334 long int next_hop;
335 while ( parent[dest] != EMPTY_PARENT )
336 {
337 next_hop=dest;
338 dest=parent[dest];
339
340 }
341
342 if ( dest != source )
343 {
344 next_hop=NO_NEXT_HOP;
345 }
346
347 return next_hop;
348
349}
350
akmhoque29c1db52012-09-07 14:47:43 -0500351void
352print_path(long int *parent, long int dest)
353{
354 if (parent[dest] != EMPTY_PARENT )
355 print_path(parent,parent[dest]);
akmhoqueb77b95f2013-02-08 12:28:47 -0600356
357 if ( nlsr->debugging )
358 printf(" %ld",dest);
akmhoque29c1db52012-09-07 14:47:43 -0500359}
360
361int
362is_not_explored(long int *Q, long int u,long int start, long int element)
363{
364 int ret=0;
365 long int i;
366 for(i=start; i< element; i++)
367 {
368 if ( Q[i] == u )
369 {
370 ret=1;
371 break;
372 }
373 }
374 return ret;
375}
376
377void
378sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
379{
380 long int i,j;
381 long int temp_u;
382
383 for ( i=start ; i < element ; i ++)
384 {
385 for( j=i+1; j<element; j ++)
386 {
387 if (dist[Q[j]] < dist[Q[i]])
388 {
389 temp_u=Q[j];
390 Q[j]=Q[i];
391 Q[i]=temp_u;
392 }
393 }
394 }
395
396}
397
akmhoqueb77b95f2013-02-08 12:28:47 -0600398void
399sort_hyperbolic_route(double *dist_dest,double *dist_nbr, long int *faces,long int start,long int element)
400{
401 long int i,j;
402 double temp_dist;
403 long int temp_face;
404
405 for ( i=start ; i < element ; i ++)
406 {
407 for( j=i+1; j<element; j ++)
408 {
409 if (dist_dest[i] < dist_dest[j])
410 {
411 temp_dist=dist_dest[j];
412 dist_dest[j]=dist_dest[i];
413 dist_dest[i]=temp_dist;
414
415 temp_dist=dist_nbr[j];
416 dist_nbr[j]=dist_nbr[i];
417 dist_nbr[i]=temp_dist;
418
419 temp_face=faces[j];
420 faces[j]=faces[i];
421 faces[i]=temp_face;
422 }
423 if ( (dist_dest[i] == dist_dest[j]) && (dist_nbr[i] < dist_nbr[j]) )
424 {
425 temp_dist=dist_dest[j];
426 dist_dest[j]=dist_dest[i];
427 dist_dest[i]=temp_dist;
428
429 temp_dist=dist_nbr[j];
430 dist_nbr[j]=dist_nbr[i];
431 dist_nbr[i]=temp_dist;
432
433 temp_face=faces[j];
434 faces[j]=faces[i];
435 faces[i]=temp_face;
436 }
437 }
438 }
439
440}
441
akmhoque29c1db52012-09-07 14:47:43 -0500442void
443print_map(void)
444{
445 int i, map_element;
446 struct map_entry *me;
447
448 struct hashtb_enumerator ee;
449 struct hashtb_enumerator *e = &ee;
450
451 hashtb_start(nlsr->map, e);
452 map_element=hashtb_n(nlsr->map);
453
454 for(i=0;i<map_element;i++)
455 {
456 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600457 if ( nlsr->debugging )
458 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
459 if ( nlsr->detailed_logging )
460 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
akmhoque29c1db52012-09-07 14:47:43 -0500461 hashtb_next(e);
462 }
463
464 hashtb_end(e);
465}
466
467
468void
469assign_mapping_number(void)
470{
471 int i, map_element;
472 struct map_entry *me;
473
474 struct hashtb_enumerator ee;
475 struct hashtb_enumerator *e = &ee;
476
477 hashtb_start(nlsr->map, e);
478 map_element=hashtb_n(nlsr->map);
479
480 for(i=0;i<map_element;i++)
481 {
482 me=e->data;
483 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500484 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500485 hashtb_next(e);
486 }
487
488 hashtb_end(e);
489}
490
491void
492make_map(void)
493{
494
495
496 int i, adj_lsdb_element;
497 struct alsa *adj_lsa;
498
499 struct hashtb_enumerator ee;
500 struct hashtb_enumerator *e = &ee;
501
502 hashtb_start(nlsr->lsdb->adj_lsdb, e);
503 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
504
akmhoque9fe296b2012-09-24 09:52:08 -0500505 add_map_entry(nlsr->router_name);
506
akmhoque29c1db52012-09-07 14:47:43 -0500507 for(i=0;i<adj_lsdb_element;i++)
508 {
509 adj_lsa=e->data;
510 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
511 hashtb_next(e);
512 }
513
514 hashtb_end(e);
515
516}
517
518void
519add_map_entry(char *router)
520{
521
akmhoquececba942013-02-25 17:33:34 -0600522 struct map_entry *me;//=(struct map_entry*)malloc(sizeof(struct map_entry ));
akmhoque29c1db52012-09-07 14:47:43 -0500523
524 struct hashtb_enumerator ee;
525 struct hashtb_enumerator *e = &ee;
526 int res;
527
528 hashtb_start(nlsr->map, e);
529 res = hashtb_seek(e, router, strlen(router), 0);
530
531 if(res == HT_NEW_ENTRY)
532 {
533 me=e->data;
534 me->router=(char *)malloc(strlen(router)+1);
535 memset(me->router,0,strlen(router)+1);
536 memcpy(me->router,router,strlen(router));
537 me->mapping=0;
538 }
539
540 hashtb_end(e);
541
542}
543
544
545void
546add_adj_data_to_map(char *orig_router, char *body, int no_link)
547{
548 add_map_entry(orig_router);
549
550 int i=0;
551 char *lsa_data=(char *)malloc(strlen(body)+1);
552 memset( lsa_data,0,strlen(body)+1);
553 memcpy(lsa_data,body,strlen(body)+1);
554 char *sep="|";
555 char *rem;
556 char *rtr_id;
557 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600558 //char *face;
akmhoque29c1db52012-09-07 14:47:43 -0500559 char *metric;
560
561 if(no_link >0 )
562 {
563 rtr_id=strtok_r(lsa_data,sep,&rem);
564 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600565 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500566 metric=strtok_r(NULL,sep,&rem);
567
akmhoqueb28579d2013-02-12 11:15:52 -0600568 if ( !nlsr->debugging && nlsr->debugging)
569 {
570 printf("Metric: %s Length:%s\n",metric,length);
571 }
akmhoque29c1db52012-09-07 14:47:43 -0500572 add_map_entry(rtr_id);
573
574 for(i=1;i<no_link;i++)
575 {
576 rtr_id=strtok_r(NULL,sep,&rem);
577 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600578 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500579 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600580 if ( !nlsr->debugging && nlsr->debugging)
581 {
582 printf("Metric: %s Length:%s\n",metric,length);
583 }
akmhoque29c1db52012-09-07 14:47:43 -0500584
585 add_map_entry(rtr_id);
586
587 }
588 }
589
590 free(lsa_data);
591}
592
593int
594get_mapping_no(char *router)
595{
596 struct map_entry *me;
597
598 struct hashtb_enumerator ee;
599 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500600 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500601 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500602
akmhoque63152c62012-09-18 08:43:42 -0500603 int n = hashtb_n(nlsr->map);
604
605 if ( n < 1)
606 {
607 return NO_MAPPING_NUM;
608 }
609
akmhoque29c1db52012-09-07 14:47:43 -0500610 hashtb_start(nlsr->map, e);
611 res = hashtb_seek(e, router, strlen(router), 0);
612
613 if( res == HT_OLD_ENTRY )
614 {
615 me=e->data;
616 ret=me->mapping;
617 }
618 else if(res == HT_NEW_ENTRY)
619 {
620 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500621 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500622 }
623
624 hashtb_end(e);
625
626 return ret;
627
628}
629
akmhoquefbfd0982012-09-09 20:59:03 -0500630
631void
632add_rev_map_entry(long int mapping_number, char *router)
633{
634
akmhoquececba942013-02-25 17:33:34 -0600635 struct map_entry *me;//=(struct map_entry*)malloc(sizeof(struct map_entry ));
akmhoquefbfd0982012-09-09 20:59:03 -0500636
637 struct hashtb_enumerator ee;
638 struct hashtb_enumerator *e = &ee;
639 int res;
640
641 hashtb_start(nlsr->rev_map, e);
642 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
643
644 if(res == HT_NEW_ENTRY)
645 {
646 me=e->data;
647 me->router=(char *)malloc(strlen(router)+1);
648 memset(me->router,0,strlen(router)+1);
649 memcpy(me->router,router,strlen(router));
650 me->mapping=mapping_number;
651 }
652
653 hashtb_end(e);
654
655}
656
657
658
659char *
660get_router_from_rev_map(long int mapping_number)
661{
662
663 struct map_entry *me;
664
665 struct hashtb_enumerator ee;
666 struct hashtb_enumerator *e = &ee;
667 int res;
668
669 hashtb_start(nlsr->rev_map, e);
670 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
671
672 if(res == HT_OLD_ENTRY)
673 {
674 me=e->data;
675 hashtb_end(e);
676 return me->router;
677 }
678 else if(res == HT_NEW_ENTRY)
679 {
680 hashtb_delete(e);
681 hashtb_end(e);
682 }
683
684 return NULL;
685}
686
687void
688print_rev_map(void)
689{
690 int i, map_element;
691 struct map_entry *me;
692
693 struct hashtb_enumerator ee;
694 struct hashtb_enumerator *e = &ee;
695
696 hashtb_start(nlsr->map, e);
697 map_element=hashtb_n(nlsr->map);
698
699 for(i=0;i<map_element;i++)
700 {
701 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600702 if ( nlsr->debugging )
703 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
704 if ( nlsr->detailed_logging )
705 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
akmhoquefbfd0982012-09-09 20:59:03 -0500706 hashtb_next(e);
707 }
708
709 hashtb_end(e);
710}
711
712
akmhoque29c1db52012-09-07 14:47:43 -0500713void
714assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
715{
716 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
717 int mapping_nbr_router;
718
719 int i;
720 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
721 memset( lsa_data,0,strlen(adj_lsa->body)+1);
722 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
723 char *sep="|";
724 char *rem;
725 char *rtr_id;
726 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600727 //char *face;
akmhoque29c1db52012-09-07 14:47:43 -0500728 char *metric;
729
730 if(adj_lsa->no_link >0 )
731 {
732 rtr_id=strtok_r(lsa_data,sep,&rem);
733 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600734 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500735 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600736
737 if ( !nlsr->debugging && nlsr->debugging)
738 {
739 printf("Metric: %s Length:%s\n",metric,length);
740 }
akmhoque29c1db52012-09-07 14:47:43 -0500741
742 mapping_nbr_router=get_mapping_no(rtr_id);
743 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
744
745 for(i=1;i<adj_lsa->no_link;i++)
746 {
747 rtr_id=strtok_r(NULL,sep,&rem);
748 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600749 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500750 metric=strtok_r(NULL,sep,&rem);
751
akmhoqueb28579d2013-02-12 11:15:52 -0600752 if ( !nlsr->debugging && nlsr->debugging)
753 {
754 printf("Metric: %s Length:%s\n",metric,length);
755 }
756
akmhoque29c1db52012-09-07 14:47:43 -0500757 mapping_nbr_router=get_mapping_no(rtr_id);
758 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
759
760 }
761 }
762
763 free(lsa_data);
764}
765
766void
767make_adj_matrix(int **adj_matrix,int map_element)
768{
769
770 init_adj_matrix(adj_matrix,map_element);
771
772 int i, adj_lsdb_element;
773 struct alsa *adj_lsa;
774
775 struct hashtb_enumerator ee;
776 struct hashtb_enumerator *e = &ee;
777
778 hashtb_start(nlsr->lsdb->adj_lsdb, e);
779 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
780
781 for(i=0;i<adj_lsdb_element;i++)
782 {
783 adj_lsa=e->data;
784 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
785 hashtb_next(e);
786 }
787
788 hashtb_end(e);
789
790}
791
792void
793init_adj_matrix(int **adj_matrix,int map_element)
794{
795 int i, j;
796 for(i=0;i<map_element;i++)
797 for(j=0;j<map_element;j++)
798 adj_matrix[i][j]=0;
799}
800
801void print_adj_matrix(int **adj_matrix, int map_element)
802{
803 int i, j;
804 for(i=0;i<map_element;i++)
805 {
806 for(j=0;j<map_element;j++)
807 printf("%d ",adj_matrix[i][j]);
808 printf("\n");
809 }
810}
811
akmhoquede61ba92012-09-20 22:19:12 -0500812
akmhoque3560cb62012-09-09 10:52:30 -0500813int
akmhoque9fe296b2012-09-24 09:52:08 -0500814get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
815{
816 int no_link=0;
817 int i;
818
819 for(i=0;i<V;i++)
820 {
821 if ( adj_matrix[S][i] > 0 )
822 {
823 no_link++;
824 }
825 }
826 return no_link;
827}
828
829void
830get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
831{
832 int i,j;
833 j=0;
834 for (i=0; i <V; i++)
835 {
836 if ( adj_matrix[S][i] > 0 )
837 {
838 links[j]=i;
839 link_costs[j]=adj_matrix[S][i];
840 j++;
841 }
842 }
843}
844
845void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
846{
847 int i;
848 for ( i = 0; i < V; i++ )
849 {
850 if ( i == link )
851 {
852 adj_matrix[S][i]=link_cost;
853 }
854 else
855 {
856 adj_matrix[S][i]=0;
857 }
858 }
859
860}
861
862int
akmhoquede61ba92012-09-20 22:19:12 -0500863get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500864{
865 struct routing_table_entry *rte;
866
867 struct hashtb_enumerator ee;
868 struct hashtb_enumerator *e = &ee;
869 int res,ret;
870
871 hashtb_start(nlsr->routing_table, e);
872 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
873
874 if( res == HT_OLD_ENTRY )
875 {
876 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500877 ret=hashtb_n(rte->face_list);
878 //nhl=rte->face_list;
879 }
880 else if(res == HT_NEW_ENTRY)
881 {
882 hashtb_delete(e);
883 ret=NO_NEXT_HOP;
884 }
885
886 hashtb_end(e);
887
888 return ret;
889}
890
891
892int
893get_next_hop(char *dest_router,int *faces, int *route_costs)
894{
895 struct routing_table_entry *rte;
896
897 struct hashtb_enumerator ee;
898 struct hashtb_enumerator *e = &ee;
899 int res,ret;
900
901 hashtb_start(nlsr->routing_table, e);
902 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
903
904 if( res == HT_OLD_ENTRY )
905 {
906 rte=e->data;
907 ret=hashtb_n(rte->face_list);
908 //nhl=rte->face_list;
909 int j,face_list_element;
910 struct face_list_entry *fle;
911
912 struct hashtb_enumerator eef;
913 struct hashtb_enumerator *ef = &eef;
914
915 hashtb_start(rte->face_list, ef);
916 face_list_element=hashtb_n(rte->face_list);
917 for(j=0;j<face_list_element;j++)
918 {
919 fle=ef->data;
920 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
921 faces[j]=fle->next_hop_face;
922 route_costs[j]=fle->route_cost;
923 hashtb_next(ef);
924 }
925 hashtb_end(ef);
926
akmhoque3560cb62012-09-09 10:52:30 -0500927 }
928 else if(res == HT_NEW_ENTRY)
929 {
930 hashtb_delete(e);
931 ret=NO_NEXT_HOP;
932 }
933
934 hashtb_end(e);
935
936 return ret;
937}
938
939void
940add_next_hop_router(char *dest_router)
941{
942 if ( strcmp(dest_router,nlsr->router_name)== 0)
943 {
944 return ;
945 }
946
akmhoquececba942013-02-25 17:33:34 -0600947 struct routing_table_entry *rte;//=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
akmhoque3560cb62012-09-09 10:52:30 -0500948
949 struct hashtb_enumerator ee;
950 struct hashtb_enumerator *e = &ee;
951 int res;
952
953 hashtb_start(nlsr->routing_table, e);
954 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
955
956 if( res == HT_NEW_ENTRY )
957 {
958 rte=e->data;
959 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
960 memset(rte->dest_router,0,strlen(dest_router)+1);
961 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500962 //rte->next_hop_face=NO_NEXT_HOP;
963 struct hashtb_param param_fle = {0};
964 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
965
966 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500967 }
968 hashtb_end(e);
969
970}
971
972void
973add_next_hop_from_lsa_adj_body(char *body, int no_link)
974{
975
976 int i=0;
977 char *lsa_data=(char *)malloc(strlen(body)+1);
978 memset( lsa_data,0,strlen(body)+1);
979 memcpy(lsa_data,body,strlen(body)+1);
980 char *sep="|";
981 char *rem;
982 char *rtr_id;
983 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600984 //char *face;
akmhoque3560cb62012-09-09 10:52:30 -0500985 char *metric;
986
987 if(no_link >0 )
988 {
989 rtr_id=strtok_r(lsa_data,sep,&rem);
990 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600991 //face=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500992 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600993 if ( !nlsr->debugging && nlsr->debugging)
994 {
995 printf("Metric: %s Length:%s\n",metric,length);
996 }
akmhoque3560cb62012-09-09 10:52:30 -0500997
akmhoque3560cb62012-09-09 10:52:30 -0500998
999 add_next_hop_router(rtr_id);
1000
1001 for(i=1;i<no_link;i++)
1002 {
1003 rtr_id=strtok_r(NULL,sep,&rem);
1004 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001005 //face=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -05001006 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001007 if ( !nlsr->debugging && nlsr->debugging)
1008 {
1009 printf("Metric: %s Length:%s\n",metric,length);
1010 }
akmhoque3560cb62012-09-09 10:52:30 -05001011
1012 add_next_hop_router(rtr_id);
1013
1014 }
1015 }
1016
1017 free(lsa_data);
1018
1019
1020}
1021
1022void
akmhoquede61ba92012-09-20 22:19:12 -05001023update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -05001024{
akmhoqueb77b95f2013-02-08 12:28:47 -06001025 if ( nlsr->debugging )
1026 {
1027 printf("update_routing_table called \n");
1028 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
1029 }
1030
akmhoquede61ba92012-09-20 22:19:12 -05001031 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -05001032 struct routing_table_entry *rte;
1033
1034 struct hashtb_enumerator ee;
1035 struct hashtb_enumerator *e = &ee;
1036
1037 hashtb_start(nlsr->routing_table, e);
1038 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1039
1040 if( res == HT_OLD_ENTRY )
1041 {
1042 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -05001043
1044 struct hashtb_enumerator eef;
1045 struct hashtb_enumerator *ef = &eef;
1046
1047 hashtb_start(rte->face_list, ef);
1048 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
1049 if( res1 == HT_NEW_ENTRY)
1050 {
akmhoqued91401d2013-03-04 11:05:27 -06001051 struct face_list_entry *fle;
akmhoquede61ba92012-09-20 22:19:12 -05001052 fle=ef->data;
1053 fle->next_hop_face=next_hop_face;
1054 fle->route_cost=route_cost;
1055 }
1056 else if ( res1 == HT_OLD_ENTRY )
1057 {
1058 struct face_list_entry *fle;
1059 fle=ef->data;
1060 fle->route_cost=route_cost;
1061 }
1062 hashtb_end(ef);
akmhoqueffacaa82012-09-13 17:48:30 -05001063 }
akmhoque54a14f42013-02-24 06:16:20 -06001064 else if ( res == HT_NEW_ENTRY )
akmhoqueffacaa82012-09-13 17:48:30 -05001065 {
1066 hashtb_delete(e);
1067 }
1068
1069 hashtb_end(e);
1070
1071}
1072
1073void
akmhoque3560cb62012-09-09 10:52:30 -05001074print_routing_table(void)
1075{
akmhoque1771c412012-11-09 13:06:08 -06001076 if ( nlsr->debugging )
1077 printf("print_routing_table called\n");
1078 if ( nlsr->detailed_logging )
1079 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001080 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -05001081
1082 struct routing_table_entry *rte;
1083
1084 struct hashtb_enumerator ee;
1085 struct hashtb_enumerator *e = &ee;
1086
1087 hashtb_start(nlsr->routing_table, e);
1088 rt_element=hashtb_n(nlsr->routing_table);
1089
1090 for(i=0;i<rt_element;i++)
1091 {
akmhoque1771c412012-11-09 13:06:08 -06001092 if ( nlsr->debugging )
1093 printf("----------Routing Table Entry %d------------------\n",i+1);
1094 if ( nlsr->detailed_logging )
1095 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
1096
akmhoque3560cb62012-09-09 10:52:30 -05001097 rte=e->data;
akmhoque1771c412012-11-09 13:06:08 -06001098
1099 if ( nlsr->debugging )
1100 printf(" Destination Router: %s \n",rte->dest_router);
1101 if ( nlsr->detailed_logging )
1102 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
1103
1104
akmhoquede61ba92012-09-20 22:19:12 -05001105 //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);
1106
1107 struct face_list_entry *fle;
1108
1109 struct hashtb_enumerator eef;
1110 struct hashtb_enumerator *ef = &eef;
1111
1112 hashtb_start(rte->face_list, ef);
1113 face_list_element=hashtb_n(rte->face_list);
1114 if ( face_list_element <= 0 )
1115 {
akmhoque1771c412012-11-09 13:06:08 -06001116 if ( nlsr->debugging )
1117 printf(" Face: No Face \n");
1118 if ( nlsr->detailed_logging )
1119 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -05001120 }
1121 else
1122 {
1123 for(j=0;j<face_list_element;j++)
1124 {
1125 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -06001126 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -06001127 printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
akmhoque1771c412012-11-09 13:06:08 -06001128 if ( nlsr->detailed_logging )
1129 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -05001130 hashtb_next(ef);
1131 }
1132 }
1133 hashtb_end(ef);
1134
akmhoque3560cb62012-09-09 10:52:30 -05001135 hashtb_next(e);
1136 }
1137
1138 hashtb_end(e);
1139
akmhoque3171d652012-11-13 11:44:33 -06001140 if ( nlsr->debugging )
1141 printf("\n");
1142 if ( nlsr->detailed_logging )
1143 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -05001144}
1145
akmhoque866c2222013-02-12 10:49:33 -06001146/*
akmhoque63152c62012-09-18 08:43:42 -05001147int
1148delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1149{
akmhoque1771c412012-11-09 13:06:08 -06001150 if ( nlsr->debugging )
1151 {
1152 printf("delete_empty_rte called\n");
1153 printf("Router: %s \n",(char *)ev->evdata);
1154 }
1155 if ( nlsr->detailed_logging )
1156 {
1157 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1158 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
akmhoque1771c412012-11-09 13:06:08 -06001159 }
1160
akmhoque63152c62012-09-18 08:43:42 -05001161 if(flags == CCN_SCHEDULE_CANCEL)
1162 {
1163 return -1;
1164 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001165
1166 nlsr_lock();
akmhoque63152c62012-09-18 08:43:42 -05001167 int res;
1168 struct hashtb_enumerator ee;
1169 struct hashtb_enumerator *e = &ee;
1170
1171 hashtb_start(nlsr->routing_table, e);
1172 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1173
1174 if ( res == HT_OLD_ENTRY )
1175 {
1176 hashtb_delete(e);
1177 }
1178 else if ( res == HT_NEW_ENTRY )
1179 {
1180 hashtb_delete(e);
1181 }
1182
akmhoquede61ba92012-09-20 22:19:12 -05001183 print_routing_table();
akmhoqueb77b95f2013-02-08 12:28:47 -06001184
1185 nlsr_unlock();
akmhoquede61ba92012-09-20 22:19:12 -05001186
akmhoque63152c62012-09-18 08:43:42 -05001187 return 0;
1188}
akmhoque866c2222013-02-12 10:49:33 -06001189*/
akmhoque63152c62012-09-18 08:43:42 -05001190
akmhoquede61ba92012-09-20 22:19:12 -05001191void
akmhoque3cced642012-09-24 16:20:20 -05001192clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001193{
akmhoque1771c412012-11-09 13:06:08 -06001194 if ( nlsr->debugging )
1195 printf("clear_old_routing_table called\n");
1196 if ( nlsr->detailed_logging )
1197 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001198 int i,rt_element;
1199
1200 struct routing_table_entry *rte;
1201
1202 struct hashtb_enumerator ee;
1203 struct hashtb_enumerator *e = &ee;
1204
1205 hashtb_start(nlsr->routing_table, e);
1206 rt_element=hashtb_n(nlsr->routing_table);
1207
1208 for(i=0;i<rt_element;i++)
1209 {
1210 rte=e->data;
1211 hashtb_destroy(&rte->face_list);
1212 struct hashtb_param param_fle = {0};
1213 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1214
1215 hashtb_next(e);
1216 }
1217
1218 hashtb_end(e);
1219}
1220
akmhoque63152c62012-09-18 08:43:42 -05001221
akmhoque3560cb62012-09-09 10:52:30 -05001222void
akmhoque3cced642012-09-24 16:20:20 -05001223do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001224{
akmhoque1771c412012-11-09 13:06:08 -06001225 if ( nlsr->debugging )
1226 printf("do_old_routing_table_updates called\n");
1227 if ( nlsr->detailed_logging )
1228 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1229
akmhoque810a5b52012-09-09 16:53:14 -05001230 int i, rt_element;
1231 int mapping_no;
1232
1233 struct routing_table_entry *rte;
1234
1235 struct hashtb_enumerator ee;
1236 struct hashtb_enumerator *e = &ee;
1237
1238 hashtb_start(nlsr->routing_table, e);
1239 rt_element=hashtb_n(nlsr->routing_table);
1240
1241 for(i=0;i<rt_element;i++)
1242 {
1243 rte=e->data;
1244 mapping_no=get_mapping_no(rte->dest_router);
1245 if ( mapping_no == NO_MAPPING_NUM)
1246 {
akmhoquede61ba92012-09-20 22:19:12 -05001247 delete_orig_router_from_npt(rte->dest_router);
akmhoque866c2222013-02-12 10:49:33 -06001248 /*
akmhoque63152c62012-09-18 08:43:42 -05001249 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1250 memset(router,0,strlen(rte->dest_router)+1);
1251 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1252 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque866c2222013-02-12 10:49:33 -06001253 */
akmhoque54a14f42013-02-24 06:16:20 -06001254 destroy_routing_table_entry_comp(rte);
akmhoque866c2222013-02-12 10:49:33 -06001255 hashtb_delete(e);
1256 i++;
akmhoque810a5b52012-09-09 16:53:14 -05001257 }
akmhoque866c2222013-02-12 10:49:33 -06001258 else
1259 {
1260 hashtb_next(e);
1261 }
akmhoque810a5b52012-09-09 16:53:14 -05001262 }
1263
1264 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001265}
akmhoque29c1db52012-09-07 14:47:43 -05001266
akmhoqueb77b95f2013-02-08 12:28:47 -06001267void
1268update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist)
1269{
1270 if ( nlsr->debugging )
1271 printf("update_routing_table_with_new_hyperbolic_route called\n");
1272 if ( nlsr->detailed_logging )
1273 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n");
1274
1275 char *orig_router=get_router_from_rev_map(dest_router_rev_map_index);
1276
1277 if (face != NO_NEXT_HOP && face != NO_FACE )
1278 {
1279 update_routing_table(orig_router,face,nbr_to_dest_dist);
1280 if ( nlsr->debugging )
1281 printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1282 if ( nlsr->detailed_logging )
1283 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1284
1285 }
1286
1287
1288}
akmhoquede61ba92012-09-20 22:19:12 -05001289
1290
akmhoquefbfd0982012-09-09 20:59:03 -05001291void
akmhoquede61ba92012-09-20 22:19:12 -05001292update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001293{
akmhoque1771c412012-11-09 13:06:08 -06001294 if ( nlsr->debugging )
1295 printf("update_routing_table_with_new_route called\n");
1296 if ( nlsr->detailed_logging )
1297 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001298 int i, map_element;
1299 struct map_entry *me;
1300
1301 struct hashtb_enumerator ee;
1302 struct hashtb_enumerator *e = &ee;
1303
1304 hashtb_start(nlsr->rev_map, e);
1305 map_element=hashtb_n(nlsr->rev_map);
1306
1307 for(i=0;i<map_element;i++)
1308 {
1309 me=e->data;
1310 if(me->mapping != source)
1311 {
1312
1313 char *orig_router=get_router_from_rev_map(me->mapping);
1314 if (orig_router != NULL )
1315 {
1316 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoquefbfd0982012-09-09 20:59:03 -05001317 if ( next_hop_router_num == NO_NEXT_HOP )
1318 {
akmhoque1771c412012-11-09 13:06:08 -06001319 if ( nlsr->debugging )
1320 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1321 if ( nlsr->detailed_logging )
1322 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001323 }
1324 else
1325 {
1326 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoquefbfd0982012-09-09 20:59:03 -05001327 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001328 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoque1771c412012-11-09 13:06:08 -06001329 if ( nlsr->debugging )
1330 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1331 if ( nlsr->detailed_logging )
1332 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1333
akmhoquefbfd0982012-09-09 20:59:03 -05001334
1335 }
1336 }
1337 }
1338 hashtb_next(e);
1339 }
1340
1341 hashtb_end(e);
1342}
1343
akmhoquede61ba92012-09-20 22:19:12 -05001344int
1345does_face_exist_for_router(char *dest_router, int face_id)
1346{
akmhoqueb77b95f2013-02-08 12:28:47 -06001347 if (nlsr->debugging)
1348 {
1349 printf("does_face_exist_for_router called\n");
1350 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1351 }
1352
akmhoquede61ba92012-09-20 22:19:12 -05001353 int ret=0;
1354
akmhoqueb77b95f2013-02-08 12:28:47 -06001355 int res;
akmhoquede61ba92012-09-20 22:19:12 -05001356 struct routing_table_entry *rte;
1357
1358 struct hashtb_enumerator ee;
1359 struct hashtb_enumerator *e = &ee;
1360
1361 hashtb_start(nlsr->routing_table, e);
1362 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1363
1364 if( res == HT_OLD_ENTRY )
1365 {
1366 rte=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001367 unsigned *v;
1368 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1369 if (v != NULL)
1370 ret = 1;
akmhoquede61ba92012-09-20 22:19:12 -05001371 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001372 else
akmhoquede61ba92012-09-20 22:19:12 -05001373 {
1374 hashtb_delete(e);
1375 }
1376
1377 hashtb_end(e);
1378
1379 return ret;
1380}
akmhoqueb77b95f2013-02-08 12:28:47 -06001381
1382double
1383get_hyperbolic_distance(long int source, long int dest)
1384{
1385 double distance;
1386 char *src_router=get_router_from_rev_map(source);
1387 char *dest_router=get_router_from_rev_map(dest);
1388
1389 double src_r=get_hyperbolic_r(src_router);
1390 double src_theta=get_hyperbolic_r(src_router);
1391
1392 double dest_r=get_hyperbolic_r(dest_router);
1393 double dest_theta=get_hyperbolic_r(dest_router);
1394 if ( src_r != -1 && dest_r != -1 )
1395 {
1396 distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta));
1397 }
1398 else
1399 {
1400 distance= -1.0;
1401 }
1402
1403 return distance;
1404}
akmhoque54a14f42013-02-24 06:16:20 -06001405
1406void
1407destroy_routing_table_entry_comp(struct routing_table_entry *rte)
1408{
1409
1410 free(rte->dest_router);
1411 hashtb_destroy(&rte->face_list);
1412
1413}
1414
1415void
1416destroy_routing_table_entry(struct routing_table_entry *rte)
1417{
1418
1419 destroy_routing_table_entry_comp(rte);
1420 free(rte);
1421}
1422
1423void
1424destroy_routing_table(void)
1425{
1426 int i, rt_element;
1427 struct hashtb_enumerator ee;
1428 struct hashtb_enumerator *e = &ee;
1429
1430 struct routing_table_entry *rte;
1431 hashtb_start(nlsr->routing_table, e);
1432 rt_element=hashtb_n(nlsr->routing_table);
1433 for(i=0;i<rt_element;i++)
1434 {
1435 rte=e->data;
1436 free(rte->dest_router);
1437 hashtb_destroy(&rte->face_list);
1438 hashtb_next(e);
1439 }
1440 hashtb_end(e);
1441 hashtb_destroy(&nlsr->routing_table);
1442
1443}
1444
1445void
1446destroy_map(void)
1447{
1448
1449 int i, map_element;
1450 struct map_entry *me;
1451
1452 struct hashtb_enumerator ee;
1453 struct hashtb_enumerator *e = &ee;
1454
1455 hashtb_start(nlsr->map, e);
1456 map_element=hashtb_n(nlsr->map);
1457
1458 for(i=0;i<map_element;i++)
1459 {
1460 me=e->data;
1461 free(me->router);
1462 hashtb_next(e);
1463 }
1464
1465 hashtb_end(e);
1466
1467}
1468
1469void
1470destroy_rev_map(void)
1471{
1472 int i, map_element;
1473 struct map_entry *me;
1474
1475 struct hashtb_enumerator ee;
1476 struct hashtb_enumerator *e = &ee;
1477
1478 hashtb_start(nlsr->rev_map, e);
1479 map_element=hashtb_n(nlsr->rev_map);
1480
1481 for(i=0;i<map_element;i++)
1482 {
1483 me=e->data;
1484 free(me->router);
1485 hashtb_next(e);
1486 }
1487
1488 hashtb_end(e);
1489}