blob: cea17bb3f2fc7a9778f1af0be8ce5fe259208ace [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();
akmhoqueae388862013-03-15 01:46:22 -050051 print_npt();
akmhoque0b60ba92013-02-25 17:55:35 -060052 nlsr->map = hashtb_create(sizeof(struct map_entry), NULL);
53 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), NULL);
akmhoque29c1db52012-09-07 14:47:43 -050054 make_map();
55 assign_mapping_number();
56 print_map();
akmhoquefbfd0982012-09-09 20:59:03 -050057 print_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -050058
akmhoque3560cb62012-09-09 10:52:30 -050059 do_old_routing_table_updates();
akmhoquede61ba92012-09-20 22:19:12 -050060 clear_old_routing_table();
61 print_routing_table();
akmhoquea30cb772012-10-07 09:50:34 -050062 print_npt();
akmhoque3560cb62012-09-09 10:52:30 -050063
akmhoque29c1db52012-09-07 14:47:43 -050064 int i;
65 int **adj_matrix;
66 int map_element=hashtb_n(nlsr->map);
67 adj_matrix=malloc(map_element * sizeof(int *));
68 for(i = 0; i < map_element; i++)
69 {
70 adj_matrix[i] = malloc(map_element * sizeof(int));
71 }
72 make_adj_matrix(adj_matrix,map_element);
akmhoque3171d652012-11-13 11:44:33 -060073 if ( nlsr->debugging )
74 print_adj_matrix(adj_matrix,map_element);
akmhoque29c1db52012-09-07 14:47:43 -050075
76 long int source=get_mapping_no(nlsr->router_name);
akmhoque9fe296b2012-09-24 09:52:08 -050077 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoqueb77b95f2013-02-08 12:28:47 -060078
79 if ( nlsr->is_hyperbolic_calc == 1)
akmhoque9fe296b2012-09-24 09:52:08 -050080 {
81 long int *links=(long int *)malloc(num_link*sizeof(long int));
82 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
83 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
akmhoqueb77b95f2013-02-08 12:28:47 -060084
85 struct hashtb_enumerator ee;
86 struct hashtb_enumerator *e = &ee;
87 for (hashtb_start(nlsr->rev_map, e); e->key != NULL; hashtb_next(e))
akmhoque9fe296b2012-09-24 09:52:08 -050088 {
akmhoqueb77b95f2013-02-08 12:28:47 -060089 struct map_entry *me=e->data;
90 if ( me->mapping != source )
91 {
92 long int *faces=(long int *)calloc(num_link,sizeof(long int));
93 double *nbr_dist=(double *)calloc(num_link,sizeof(double));
94 double *nbr_to_dest=(double *)calloc(num_link,sizeof(double));
95 for ( i=0 ; i < num_link; i++)
96 {
97 int face=get_next_hop_face_from_adl(get_router_from_rev_map(links[i]));
98 double dist_to_nbr=get_hyperbolic_distance(source,links[i]);
99 double dist_to_dest_from_nbr=get_hyperbolic_distance(links[i],me->mapping);
100 faces[i]=face;
101 nbr_dist[i]=dist_to_nbr;
102 nbr_to_dest[i]= dist_to_dest_from_nbr;
103
104
105 }
106 sort_hyperbolic_route(nbr_to_dest,nbr_dist, faces,0,num_link);
107 if (nlsr->max_faces_per_prefix == 0 )
108 {
109 for ( i=0 ; i < num_link; i++)
110 {
111 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
112 }
113 }
114 else if ( nlsr->max_faces_per_prefix > 0 )
115 {
116 if ( num_link <= nlsr->max_faces_per_prefix )
117 {
118 for ( i=0 ; i < num_link; i++)
119 {
120 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
121 }
122 }
123 else if (num_link > nlsr->max_faces_per_prefix)
124 {
125 for ( i=0 ; i < nlsr->max_faces_per_prefix; i++)
126 {
127 update_routing_table_with_new_hyperbolic_route(me->mapping,faces[i],nbr_to_dest[i]);
128 }
129 }
130
131 }
132 free(faces);
133 free(nbr_dist);
134 free(nbr_to_dest);
135 }
136 }
137 hashtb_end(e);
138
139
140 free(links);
141 free(link_costs);
142 }
143 else if (nlsr->is_hyperbolic_calc == 0 )
144 {
145
146 long int *parent=(long int *)malloc(map_element * sizeof(long int));
147 long int *dist=(long int *)malloc(map_element * sizeof(long int));
148
149
150 if ( (num_link == 0) || (nlsr->max_faces_per_prefix == 1 ) )
151 {
akmhoque9fe296b2012-09-24 09:52:08 -0500152 calculate_path(adj_matrix,parent,dist, map_element, source);
153 print_all_path_from_source(parent,source);
154 print_all_next_hop(parent,source);
155 update_routing_table_with_new_route(parent, dist,source);
156 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600157 else if ( (num_link != 0) && (nlsr->max_faces_per_prefix == 0 || nlsr->max_faces_per_prefix > 1 ) )
158 {
159 long int *links=(long int *)malloc(num_link*sizeof(long int));
160 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
161 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
162 for ( i=0 ; i < num_link; i++)
163 {
164 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
165 calculate_path(adj_matrix,parent,dist, map_element, source);
166 print_all_path_from_source(parent,source);
167 print_all_next_hop(parent,source);
168 update_routing_table_with_new_route(parent, dist,source);
169 }
akmhoque29c1db52012-09-07 14:47:43 -0500170
akmhoqueb77b95f2013-02-08 12:28:47 -0600171 free(links);
172 free(link_costs);
173 }
174 free(parent);
175 free(dist);
akmhoque9fe296b2012-09-24 09:52:08 -0500176 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600177
178 print_routing_table();
179 print_npt();
akmhoquede61ba92012-09-20 22:19:12 -0500180
181 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -0500182
akmhoqueffacaa82012-09-13 17:48:30 -0500183 print_routing_table();
184 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -0500185
akmhoqueb77b95f2013-02-08 12:28:47 -0600186
akmhoque29c1db52012-09-07 14:47:43 -0500187 for(i = 0; i < map_element; i++)
188 {
189 free(adj_matrix[i]);
190 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600191
akmhoque29c1db52012-09-07 14:47:43 -0500192 free(adj_matrix);
akmhoque54a14f42013-02-24 06:16:20 -0600193 destroy_map();
194 destroy_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -0500195
196 }
197 nlsr->is_route_calculation_scheduled=0;
198
akmhoqueffacaa82012-09-13 17:48:30 -0500199 nlsr_unlock();
200
akmhoque29c1db52012-09-07 14:47:43 -0500201 return 0;
202}
203
204void
akmhoquede61ba92012-09-20 22:19:12 -0500205calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500206{
207 int i;
208 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500209 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500210 long int *Q=(long int *)malloc(V * sizeof(long int));
211 long int head=0;
akmhoqueb77b95f2013-02-08 12:28:47 -0600212 /* Initiate the Parent */
akmhoque29c1db52012-09-07 14:47:43 -0500213 for (i = 0 ; i < V; i++)
214 {
215 parent[i]=EMPTY_PARENT;
216 dist[i]=INF_DISTANCE;
217 Q[i]=i;
218 }
219
akmhoque9fe296b2012-09-24 09:52:08 -0500220 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500221 {
akmhoque9fe296b2012-09-24 09:52:08 -0500222 dist[S]=0;
223 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500224
akmhoque9fe296b2012-09-24 09:52:08 -0500225 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500226 {
akmhoque9fe296b2012-09-24 09:52:08 -0500227 u=Q[head];
228 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500229 {
akmhoque9fe296b2012-09-24 09:52:08 -0500230 break;
231 }
232
233 for(v=0 ; v <V; v++)
234 {
235 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500236 {
akmhoque9fe296b2012-09-24 09:52:08 -0500237 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500238 {
akmhoque9fe296b2012-09-24 09:52:08 -0500239
240 if( dist[u] + adj_matrix[u][v] < dist[v])
241 {
242 dist[v]=dist[u] + adj_matrix[u][v] ;
243 parent[v]=u;
244 }
245
246 }
akmhoque29c1db52012-09-07 14:47:43 -0500247
248 }
249
250 }
251
akmhoque9fe296b2012-09-24 09:52:08 -0500252 head++;
253 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500254 }
akmhoque29c1db52012-09-07 14:47:43 -0500255 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600256 free(Q);
akmhoque29c1db52012-09-07 14:47:43 -0500257}
258
259void
260print_all_path_from_source(long int *parent,long int source)
261{
262 int i, map_element;
263 struct map_entry *me;
264
265 struct hashtb_enumerator ee;
266 struct hashtb_enumerator *e = &ee;
267
268 hashtb_start(nlsr->map, e);
269 map_element=hashtb_n(nlsr->map);
270
akmhoque9fe296b2012-09-24 09:52:08 -0500271 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500272 {
akmhoque9fe296b2012-09-24 09:52:08 -0500273 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500274 {
akmhoque9fe296b2012-09-24 09:52:08 -0500275 me=e->data;
276 if(me->mapping != source)
277 {
akmhoque3171d652012-11-13 11:44:33 -0600278
279 if ( nlsr->debugging )
280 {
281 print_path(parent,(long int)me->mapping);
282 printf("\n");
283 }
284 if ( nlsr->detailed_logging )
285 {
286 print_path(parent,(long int)me->mapping);
287 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
288 }
289
akmhoque9fe296b2012-09-24 09:52:08 -0500290 }
291 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500292 }
akmhoque29c1db52012-09-07 14:47:43 -0500293 }
akmhoque29c1db52012-09-07 14:47:43 -0500294 hashtb_end(e);
295
296}
297
akmhoquefbfd0982012-09-09 20:59:03 -0500298void
299print_all_next_hop(long int *parent,long int source)
300{
301 int i, map_element;
302 struct map_entry *me;
303
304 struct hashtb_enumerator ee;
305 struct hashtb_enumerator *e = &ee;
306
307 hashtb_start(nlsr->map, e);
308 map_element=hashtb_n(nlsr->map);
309
310 for(i=0;i<map_element;i++)
311 {
312 me=e->data;
313 if(me->mapping != source)
314 {
akmhoque1771c412012-11-09 13:06:08 -0600315 if ( nlsr->debugging )
316 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
317 if ( nlsr->detailed_logging )
318 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 -0500319 }
320 hashtb_next(e);
321 }
322
323 hashtb_end(e);
324
325}
326
327long int
328get_next_hop_from_calculation(long int *parent, long int dest,long int source)
329{
330 long int next_hop;
331 while ( parent[dest] != EMPTY_PARENT )
332 {
333 next_hop=dest;
334 dest=parent[dest];
335
336 }
337
338 if ( dest != source )
339 {
340 next_hop=NO_NEXT_HOP;
341 }
342
343 return next_hop;
344
345}
346
akmhoque29c1db52012-09-07 14:47:43 -0500347void
348print_path(long int *parent, long int dest)
349{
350 if (parent[dest] != EMPTY_PARENT )
351 print_path(parent,parent[dest]);
akmhoqueb77b95f2013-02-08 12:28:47 -0600352
353 if ( nlsr->debugging )
354 printf(" %ld",dest);
akmhoque29c1db52012-09-07 14:47:43 -0500355}
356
357int
358is_not_explored(long int *Q, long int u,long int start, long int element)
359{
360 int ret=0;
361 long int i;
362 for(i=start; i< element; i++)
363 {
364 if ( Q[i] == u )
365 {
366 ret=1;
367 break;
368 }
369 }
370 return ret;
371}
372
373void
374sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
375{
376 long int i,j;
377 long int temp_u;
378
379 for ( i=start ; i < element ; i ++)
380 {
381 for( j=i+1; j<element; j ++)
382 {
383 if (dist[Q[j]] < dist[Q[i]])
384 {
385 temp_u=Q[j];
386 Q[j]=Q[i];
387 Q[i]=temp_u;
388 }
389 }
390 }
391
392}
393
akmhoqueb77b95f2013-02-08 12:28:47 -0600394void
395sort_hyperbolic_route(double *dist_dest,double *dist_nbr, long int *faces,long int start,long int element)
396{
397 long int i,j;
398 double temp_dist;
399 long int temp_face;
400
401 for ( i=start ; i < element ; i ++)
402 {
403 for( j=i+1; j<element; j ++)
404 {
405 if (dist_dest[i] < dist_dest[j])
406 {
407 temp_dist=dist_dest[j];
408 dist_dest[j]=dist_dest[i];
409 dist_dest[i]=temp_dist;
410
411 temp_dist=dist_nbr[j];
412 dist_nbr[j]=dist_nbr[i];
413 dist_nbr[i]=temp_dist;
414
415 temp_face=faces[j];
416 faces[j]=faces[i];
417 faces[i]=temp_face;
418 }
419 if ( (dist_dest[i] == dist_dest[j]) && (dist_nbr[i] < dist_nbr[j]) )
420 {
421 temp_dist=dist_dest[j];
422 dist_dest[j]=dist_dest[i];
423 dist_dest[i]=temp_dist;
424
425 temp_dist=dist_nbr[j];
426 dist_nbr[j]=dist_nbr[i];
427 dist_nbr[i]=temp_dist;
428
429 temp_face=faces[j];
430 faces[j]=faces[i];
431 faces[i]=temp_face;
432 }
433 }
434 }
435
436}
437
akmhoque29c1db52012-09-07 14:47:43 -0500438void
439print_map(void)
440{
441 int i, map_element;
442 struct map_entry *me;
443
444 struct hashtb_enumerator ee;
445 struct hashtb_enumerator *e = &ee;
446
447 hashtb_start(nlsr->map, e);
448 map_element=hashtb_n(nlsr->map);
449
450 for(i=0;i<map_element;i++)
451 {
452 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600453 if ( nlsr->debugging )
454 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
455 if ( nlsr->detailed_logging )
456 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s Mapping Number: %d \n",me->router,me->mapping);
akmhoque29c1db52012-09-07 14:47:43 -0500457 hashtb_next(e);
458 }
459
460 hashtb_end(e);
461}
462
463
464void
465assign_mapping_number(void)
466{
467 int i, map_element;
468 struct map_entry *me;
469
470 struct hashtb_enumerator ee;
471 struct hashtb_enumerator *e = &ee;
472
473 hashtb_start(nlsr->map, e);
474 map_element=hashtb_n(nlsr->map);
475
476 for(i=0;i<map_element;i++)
477 {
478 me=e->data;
479 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500480 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500481 hashtb_next(e);
482 }
483
484 hashtb_end(e);
485}
486
487void
488make_map(void)
489{
490
491
492 int i, adj_lsdb_element;
493 struct alsa *adj_lsa;
494
495 struct hashtb_enumerator ee;
496 struct hashtb_enumerator *e = &ee;
497
498 hashtb_start(nlsr->lsdb->adj_lsdb, e);
499 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
500
akmhoque9fe296b2012-09-24 09:52:08 -0500501 add_map_entry(nlsr->router_name);
502
akmhoque29c1db52012-09-07 14:47:43 -0500503 for(i=0;i<adj_lsdb_element;i++)
504 {
505 adj_lsa=e->data;
506 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
507 hashtb_next(e);
508 }
509
510 hashtb_end(e);
511
512}
513
514void
515add_map_entry(char *router)
516{
517
akmhoquececba942013-02-25 17:33:34 -0600518 struct map_entry *me;//=(struct map_entry*)malloc(sizeof(struct map_entry ));
akmhoque29c1db52012-09-07 14:47:43 -0500519
520 struct hashtb_enumerator ee;
521 struct hashtb_enumerator *e = &ee;
522 int res;
523
524 hashtb_start(nlsr->map, e);
525 res = hashtb_seek(e, router, strlen(router), 0);
526
527 if(res == HT_NEW_ENTRY)
528 {
529 me=e->data;
530 me->router=(char *)malloc(strlen(router)+1);
531 memset(me->router,0,strlen(router)+1);
532 memcpy(me->router,router,strlen(router));
533 me->mapping=0;
534 }
535
536 hashtb_end(e);
537
538}
539
540
541void
542add_adj_data_to_map(char *orig_router, char *body, int no_link)
543{
544 add_map_entry(orig_router);
545
546 int i=0;
547 char *lsa_data=(char *)malloc(strlen(body)+1);
548 memset( lsa_data,0,strlen(body)+1);
549 memcpy(lsa_data,body,strlen(body)+1);
550 char *sep="|";
551 char *rem;
552 char *rtr_id;
553 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600554 //char *face;
akmhoque29c1db52012-09-07 14:47:43 -0500555 char *metric;
556
557 if(no_link >0 )
558 {
559 rtr_id=strtok_r(lsa_data,sep,&rem);
560 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600561 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500562 metric=strtok_r(NULL,sep,&rem);
563
akmhoqueb28579d2013-02-12 11:15:52 -0600564 if ( !nlsr->debugging && nlsr->debugging)
565 {
566 printf("Metric: %s Length:%s\n",metric,length);
567 }
akmhoque29c1db52012-09-07 14:47:43 -0500568 add_map_entry(rtr_id);
569
570 for(i=1;i<no_link;i++)
571 {
572 rtr_id=strtok_r(NULL,sep,&rem);
573 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600574 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500575 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600576 if ( !nlsr->debugging && nlsr->debugging)
577 {
578 printf("Metric: %s Length:%s\n",metric,length);
579 }
akmhoque29c1db52012-09-07 14:47:43 -0500580
581 add_map_entry(rtr_id);
582
583 }
584 }
585
586 free(lsa_data);
587}
588
589int
590get_mapping_no(char *router)
591{
592 struct map_entry *me;
593
594 struct hashtb_enumerator ee;
595 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500596 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500597 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500598
akmhoque63152c62012-09-18 08:43:42 -0500599 int n = hashtb_n(nlsr->map);
600
601 if ( n < 1)
602 {
603 return NO_MAPPING_NUM;
604 }
605
akmhoque29c1db52012-09-07 14:47:43 -0500606 hashtb_start(nlsr->map, e);
607 res = hashtb_seek(e, router, strlen(router), 0);
608
609 if( res == HT_OLD_ENTRY )
610 {
611 me=e->data;
612 ret=me->mapping;
613 }
614 else if(res == HT_NEW_ENTRY)
615 {
616 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500617 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500618 }
619
620 hashtb_end(e);
621
622 return ret;
623
624}
625
akmhoquefbfd0982012-09-09 20:59:03 -0500626
627void
628add_rev_map_entry(long int mapping_number, char *router)
629{
630
akmhoquececba942013-02-25 17:33:34 -0600631 struct map_entry *me;//=(struct map_entry*)malloc(sizeof(struct map_entry ));
akmhoquefbfd0982012-09-09 20:59:03 -0500632
633 struct hashtb_enumerator ee;
634 struct hashtb_enumerator *e = &ee;
635 int res;
636
637 hashtb_start(nlsr->rev_map, e);
638 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
639
640 if(res == HT_NEW_ENTRY)
641 {
642 me=e->data;
643 me->router=(char *)malloc(strlen(router)+1);
644 memset(me->router,0,strlen(router)+1);
645 memcpy(me->router,router,strlen(router));
646 me->mapping=mapping_number;
647 }
648
649 hashtb_end(e);
650
651}
652
653
654
655char *
656get_router_from_rev_map(long int mapping_number)
657{
658
659 struct map_entry *me;
660
661 struct hashtb_enumerator ee;
662 struct hashtb_enumerator *e = &ee;
663 int res;
664
665 hashtb_start(nlsr->rev_map, e);
666 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
667
668 if(res == HT_OLD_ENTRY)
669 {
670 me=e->data;
671 hashtb_end(e);
672 return me->router;
673 }
674 else if(res == HT_NEW_ENTRY)
675 {
676 hashtb_delete(e);
677 hashtb_end(e);
678 }
679
680 return NULL;
681}
682
683void
684print_rev_map(void)
685{
686 int i, map_element;
687 struct map_entry *me;
688
689 struct hashtb_enumerator ee;
690 struct hashtb_enumerator *e = &ee;
691
692 hashtb_start(nlsr->map, e);
693 map_element=hashtb_n(nlsr->map);
694
695 for(i=0;i<map_element;i++)
696 {
697 me=e->data;
akmhoque1771c412012-11-09 13:06:08 -0600698 if ( nlsr->debugging )
699 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
700 if ( nlsr->detailed_logging )
701 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Mapping Number: %d Router: %s \n",me->mapping,me->router);
akmhoquefbfd0982012-09-09 20:59:03 -0500702 hashtb_next(e);
703 }
704
705 hashtb_end(e);
706}
707
708
akmhoque29c1db52012-09-07 14:47:43 -0500709void
710assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
711{
712 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
713 int mapping_nbr_router;
714
715 int i;
716 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
717 memset( lsa_data,0,strlen(adj_lsa->body)+1);
718 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
719 char *sep="|";
720 char *rem;
721 char *rtr_id;
722 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600723 //char *face;
akmhoque29c1db52012-09-07 14:47:43 -0500724 char *metric;
725
726 if(adj_lsa->no_link >0 )
727 {
728 rtr_id=strtok_r(lsa_data,sep,&rem);
729 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600730 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500731 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600732
733 if ( !nlsr->debugging && nlsr->debugging)
734 {
735 printf("Metric: %s Length:%s\n",metric,length);
736 }
akmhoque29c1db52012-09-07 14:47:43 -0500737
738 mapping_nbr_router=get_mapping_no(rtr_id);
739 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
740
741 for(i=1;i<adj_lsa->no_link;i++)
742 {
743 rtr_id=strtok_r(NULL,sep,&rem);
744 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600745 //face=strtok_r(NULL,sep,&rem);
akmhoque29c1db52012-09-07 14:47:43 -0500746 metric=strtok_r(NULL,sep,&rem);
747
akmhoqueb28579d2013-02-12 11:15:52 -0600748 if ( !nlsr->debugging && nlsr->debugging)
749 {
750 printf("Metric: %s Length:%s\n",metric,length);
751 }
752
akmhoque29c1db52012-09-07 14:47:43 -0500753 mapping_nbr_router=get_mapping_no(rtr_id);
754 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
755
756 }
757 }
758
759 free(lsa_data);
760}
761
762void
763make_adj_matrix(int **adj_matrix,int map_element)
764{
765
766 init_adj_matrix(adj_matrix,map_element);
767
768 int i, adj_lsdb_element;
769 struct alsa *adj_lsa;
770
771 struct hashtb_enumerator ee;
772 struct hashtb_enumerator *e = &ee;
773
774 hashtb_start(nlsr->lsdb->adj_lsdb, e);
775 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
776
777 for(i=0;i<adj_lsdb_element;i++)
778 {
779 adj_lsa=e->data;
780 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
781 hashtb_next(e);
782 }
783
784 hashtb_end(e);
785
786}
787
788void
789init_adj_matrix(int **adj_matrix,int map_element)
790{
791 int i, j;
792 for(i=0;i<map_element;i++)
793 for(j=0;j<map_element;j++)
794 adj_matrix[i][j]=0;
795}
796
797void print_adj_matrix(int **adj_matrix, int map_element)
798{
799 int i, j;
800 for(i=0;i<map_element;i++)
801 {
802 for(j=0;j<map_element;j++)
803 printf("%d ",adj_matrix[i][j]);
804 printf("\n");
805 }
806}
807
akmhoquede61ba92012-09-20 22:19:12 -0500808
akmhoque3560cb62012-09-09 10:52:30 -0500809int
akmhoque9fe296b2012-09-24 09:52:08 -0500810get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
811{
812 int no_link=0;
813 int i;
814
815 for(i=0;i<V;i++)
816 {
817 if ( adj_matrix[S][i] > 0 )
818 {
819 no_link++;
820 }
821 }
822 return no_link;
823}
824
825void
826get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
827{
828 int i,j;
829 j=0;
830 for (i=0; i <V; i++)
831 {
832 if ( adj_matrix[S][i] > 0 )
833 {
834 links[j]=i;
835 link_costs[j]=adj_matrix[S][i];
836 j++;
837 }
838 }
839}
840
841void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
842{
843 int i;
844 for ( i = 0; i < V; i++ )
845 {
846 if ( i == link )
847 {
848 adj_matrix[S][i]=link_cost;
849 }
850 else
851 {
852 adj_matrix[S][i]=0;
853 }
854 }
855
856}
857
858int
akmhoquede61ba92012-09-20 22:19:12 -0500859get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500860{
861 struct routing_table_entry *rte;
862
863 struct hashtb_enumerator ee;
864 struct hashtb_enumerator *e = &ee;
865 int res,ret;
866
867 hashtb_start(nlsr->routing_table, e);
868 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
869
870 if( res == HT_OLD_ENTRY )
871 {
872 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500873 ret=hashtb_n(rte->face_list);
874 //nhl=rte->face_list;
875 }
876 else if(res == HT_NEW_ENTRY)
877 {
878 hashtb_delete(e);
879 ret=NO_NEXT_HOP;
880 }
881
882 hashtb_end(e);
883
884 return ret;
885}
886
887
888int
889get_next_hop(char *dest_router,int *faces, int *route_costs)
890{
891 struct routing_table_entry *rte;
892
893 struct hashtb_enumerator ee;
894 struct hashtb_enumerator *e = &ee;
895 int res,ret;
896
897 hashtb_start(nlsr->routing_table, e);
898 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
899
900 if( res == HT_OLD_ENTRY )
901 {
902 rte=e->data;
903 ret=hashtb_n(rte->face_list);
904 //nhl=rte->face_list;
905 int j,face_list_element;
906 struct face_list_entry *fle;
907
908 struct hashtb_enumerator eef;
909 struct hashtb_enumerator *ef = &eef;
910
911 hashtb_start(rte->face_list, ef);
912 face_list_element=hashtb_n(rte->face_list);
913 for(j=0;j<face_list_element;j++)
914 {
915 fle=ef->data;
916 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
917 faces[j]=fle->next_hop_face;
918 route_costs[j]=fle->route_cost;
919 hashtb_next(ef);
920 }
921 hashtb_end(ef);
922
akmhoque3560cb62012-09-09 10:52:30 -0500923 }
924 else if(res == HT_NEW_ENTRY)
925 {
926 hashtb_delete(e);
927 ret=NO_NEXT_HOP;
928 }
929
930 hashtb_end(e);
931
932 return ret;
933}
934
935void
936add_next_hop_router(char *dest_router)
937{
938 if ( strcmp(dest_router,nlsr->router_name)== 0)
939 {
940 return ;
941 }
942
akmhoquececba942013-02-25 17:33:34 -0600943 struct routing_table_entry *rte;//=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
akmhoque3560cb62012-09-09 10:52:30 -0500944
945 struct hashtb_enumerator ee;
946 struct hashtb_enumerator *e = &ee;
947 int res;
948
949 hashtb_start(nlsr->routing_table, e);
950 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
951
952 if( res == HT_NEW_ENTRY )
953 {
954 rte=e->data;
955 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
956 memset(rte->dest_router,0,strlen(dest_router)+1);
957 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500958 //rte->next_hop_face=NO_NEXT_HOP;
959 struct hashtb_param param_fle = {0};
960 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
961
962 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500963 }
964 hashtb_end(e);
965
966}
967
968void
969add_next_hop_from_lsa_adj_body(char *body, int no_link)
970{
971
972 int i=0;
973 char *lsa_data=(char *)malloc(strlen(body)+1);
974 memset( lsa_data,0,strlen(body)+1);
975 memcpy(lsa_data,body,strlen(body)+1);
976 char *sep="|";
977 char *rem;
978 char *rtr_id;
979 char *length;
akmhoque866c2222013-02-12 10:49:33 -0600980 //char *face;
akmhoque3560cb62012-09-09 10:52:30 -0500981 char *metric;
982
983 if(no_link >0 )
984 {
985 rtr_id=strtok_r(lsa_data,sep,&rem);
986 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -0600987 //face=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500988 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -0600989 if ( !nlsr->debugging && nlsr->debugging)
990 {
991 printf("Metric: %s Length:%s\n",metric,length);
992 }
akmhoque3560cb62012-09-09 10:52:30 -0500993
akmhoque3560cb62012-09-09 10:52:30 -0500994
995 add_next_hop_router(rtr_id);
996
997 for(i=1;i<no_link;i++)
998 {
999 rtr_id=strtok_r(NULL,sep,&rem);
1000 length=strtok_r(NULL,sep,&rem);
akmhoque866c2222013-02-12 10:49:33 -06001001 //face=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -05001002 metric=strtok_r(NULL,sep,&rem);
akmhoqueb28579d2013-02-12 11:15:52 -06001003 if ( !nlsr->debugging && nlsr->debugging)
1004 {
1005 printf("Metric: %s Length:%s\n",metric,length);
1006 }
akmhoque3560cb62012-09-09 10:52:30 -05001007
1008 add_next_hop_router(rtr_id);
1009
1010 }
1011 }
1012
1013 free(lsa_data);
1014
1015
1016}
1017
1018void
akmhoquede61ba92012-09-20 22:19:12 -05001019update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -05001020{
akmhoqueb77b95f2013-02-08 12:28:47 -06001021 if ( nlsr->debugging )
1022 {
1023 printf("update_routing_table called \n");
1024 printf("Dest Router: %s Next Hop face: %d Route Cost: %d \n",dest_router,next_hop_face,route_cost);
1025 }
1026
akmhoquede61ba92012-09-20 22:19:12 -05001027 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -05001028 struct routing_table_entry *rte;
1029
1030 struct hashtb_enumerator ee;
1031 struct hashtb_enumerator *e = &ee;
1032
1033 hashtb_start(nlsr->routing_table, e);
1034 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1035
1036 if( res == HT_OLD_ENTRY )
1037 {
1038 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -05001039
1040 struct hashtb_enumerator eef;
1041 struct hashtb_enumerator *ef = &eef;
1042
1043 hashtb_start(rte->face_list, ef);
1044 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
1045 if( res1 == HT_NEW_ENTRY)
1046 {
akmhoqued91401d2013-03-04 11:05:27 -06001047 struct face_list_entry *fle;
akmhoquede61ba92012-09-20 22:19:12 -05001048 fle=ef->data;
1049 fle->next_hop_face=next_hop_face;
1050 fle->route_cost=route_cost;
1051 }
1052 else if ( res1 == HT_OLD_ENTRY )
1053 {
1054 struct face_list_entry *fle;
1055 fle=ef->data;
1056 fle->route_cost=route_cost;
1057 }
1058 hashtb_end(ef);
akmhoqueffacaa82012-09-13 17:48:30 -05001059 }
akmhoque54a14f42013-02-24 06:16:20 -06001060 else if ( res == HT_NEW_ENTRY )
akmhoqueffacaa82012-09-13 17:48:30 -05001061 {
1062 hashtb_delete(e);
1063 }
1064
1065 hashtb_end(e);
1066
1067}
1068
1069void
akmhoque3560cb62012-09-09 10:52:30 -05001070print_routing_table(void)
1071{
akmhoque1771c412012-11-09 13:06:08 -06001072 if ( nlsr->debugging )
1073 printf("print_routing_table called\n");
1074 if ( nlsr->detailed_logging )
1075 writeLogg(__FILE__,__FUNCTION__,__LINE__,"print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001076 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -05001077
1078 struct routing_table_entry *rte;
1079
1080 struct hashtb_enumerator ee;
1081 struct hashtb_enumerator *e = &ee;
1082
1083 hashtb_start(nlsr->routing_table, e);
1084 rt_element=hashtb_n(nlsr->routing_table);
1085
1086 for(i=0;i<rt_element;i++)
1087 {
akmhoque1771c412012-11-09 13:06:08 -06001088 if ( nlsr->debugging )
1089 printf("----------Routing Table Entry %d------------------\n",i+1);
1090 if ( nlsr->detailed_logging )
1091 writeLogg(__FILE__,__FUNCTION__,__LINE__,"----------Routing Table Entry %d------------------\n",i+1);
1092
akmhoque3560cb62012-09-09 10:52:30 -05001093 rte=e->data;
akmhoque1771c412012-11-09 13:06:08 -06001094
1095 if ( nlsr->debugging )
1096 printf(" Destination Router: %s \n",rte->dest_router);
1097 if ( nlsr->detailed_logging )
1098 writeLogg(__FILE__,__FUNCTION__,__LINE__," Destination Router: %s \n",rte->dest_router);
1099
1100
akmhoquede61ba92012-09-20 22:19:12 -05001101 //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);
1102
1103 struct face_list_entry *fle;
1104
1105 struct hashtb_enumerator eef;
1106 struct hashtb_enumerator *ef = &eef;
1107
1108 hashtb_start(rte->face_list, ef);
1109 face_list_element=hashtb_n(rte->face_list);
1110 if ( face_list_element <= 0 )
1111 {
akmhoque1771c412012-11-09 13:06:08 -06001112 if ( nlsr->debugging )
1113 printf(" Face: No Face \n");
1114 if ( nlsr->detailed_logging )
1115 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: No Face \n");
akmhoquede61ba92012-09-20 22:19:12 -05001116 }
1117 else
1118 {
1119 for(j=0;j<face_list_element;j++)
1120 {
1121 fle=ef->data;
akmhoque1771c412012-11-09 13:06:08 -06001122 if ( nlsr->debugging )
akmhoqueb77b95f2013-02-08 12:28:47 -06001123 printf(" Face: %d Route_Cost: %f \n",fle->next_hop_face,fle->route_cost);
akmhoque1771c412012-11-09 13:06:08 -06001124 if ( nlsr->detailed_logging )
1125 writeLogg(__FILE__,__FUNCTION__,__LINE__," Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
akmhoquede61ba92012-09-20 22:19:12 -05001126 hashtb_next(ef);
1127 }
1128 }
1129 hashtb_end(ef);
1130
akmhoque3560cb62012-09-09 10:52:30 -05001131 hashtb_next(e);
1132 }
1133
1134 hashtb_end(e);
1135
akmhoque3171d652012-11-13 11:44:33 -06001136 if ( nlsr->debugging )
1137 printf("\n");
1138 if ( nlsr->detailed_logging )
1139 writeLogg(__FILE__,__FUNCTION__,__LINE__,"\n");
akmhoque3560cb62012-09-09 10:52:30 -05001140}
1141
akmhoque866c2222013-02-12 10:49:33 -06001142/*
akmhoque63152c62012-09-18 08:43:42 -05001143int
1144delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
1145{
akmhoque1771c412012-11-09 13:06:08 -06001146 if ( nlsr->debugging )
1147 {
1148 printf("delete_empty_rte called\n");
1149 printf("Router: %s \n",(char *)ev->evdata);
1150 }
1151 if ( nlsr->detailed_logging )
1152 {
1153 writeLogg(__FILE__,__FUNCTION__,__LINE__,"delete_empty_rte called\n");
1154 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router: %s \n",(char *)ev->evdata);
akmhoque1771c412012-11-09 13:06:08 -06001155 }
1156
akmhoque63152c62012-09-18 08:43:42 -05001157 if(flags == CCN_SCHEDULE_CANCEL)
1158 {
1159 return -1;
1160 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001161
1162 nlsr_lock();
akmhoque63152c62012-09-18 08:43:42 -05001163 int res;
1164 struct hashtb_enumerator ee;
1165 struct hashtb_enumerator *e = &ee;
1166
1167 hashtb_start(nlsr->routing_table, e);
1168 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
1169
1170 if ( res == HT_OLD_ENTRY )
1171 {
1172 hashtb_delete(e);
1173 }
1174 else if ( res == HT_NEW_ENTRY )
1175 {
1176 hashtb_delete(e);
1177 }
1178
akmhoquede61ba92012-09-20 22:19:12 -05001179 print_routing_table();
akmhoqueb77b95f2013-02-08 12:28:47 -06001180
1181 nlsr_unlock();
akmhoquede61ba92012-09-20 22:19:12 -05001182
akmhoque63152c62012-09-18 08:43:42 -05001183 return 0;
1184}
akmhoque866c2222013-02-12 10:49:33 -06001185*/
akmhoque63152c62012-09-18 08:43:42 -05001186
akmhoquede61ba92012-09-20 22:19:12 -05001187void
akmhoque3cced642012-09-24 16:20:20 -05001188clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001189{
akmhoque1771c412012-11-09 13:06:08 -06001190 if ( nlsr->debugging )
1191 printf("clear_old_routing_table called\n");
1192 if ( nlsr->detailed_logging )
1193 writeLogg(__FILE__,__FUNCTION__,__LINE__,"clear_old_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -05001194 int i,rt_element;
1195
1196 struct routing_table_entry *rte;
1197
1198 struct hashtb_enumerator ee;
1199 struct hashtb_enumerator *e = &ee;
1200
1201 hashtb_start(nlsr->routing_table, e);
1202 rt_element=hashtb_n(nlsr->routing_table);
1203
1204 for(i=0;i<rt_element;i++)
1205 {
1206 rte=e->data;
1207 hashtb_destroy(&rte->face_list);
1208 struct hashtb_param param_fle = {0};
1209 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1210
1211 hashtb_next(e);
1212 }
1213
1214 hashtb_end(e);
1215}
1216
akmhoque63152c62012-09-18 08:43:42 -05001217
akmhoque3560cb62012-09-09 10:52:30 -05001218void
akmhoque3cced642012-09-24 16:20:20 -05001219do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001220{
akmhoque1771c412012-11-09 13:06:08 -06001221 if ( nlsr->debugging )
1222 printf("do_old_routing_table_updates called\n");
1223 if ( nlsr->detailed_logging )
1224 writeLogg(__FILE__,__FUNCTION__,__LINE__,"do_old_routing_table_updates called\n");
1225
akmhoque810a5b52012-09-09 16:53:14 -05001226 int i, rt_element;
1227 int mapping_no;
1228
1229 struct routing_table_entry *rte;
1230
1231 struct hashtb_enumerator ee;
1232 struct hashtb_enumerator *e = &ee;
1233
1234 hashtb_start(nlsr->routing_table, e);
1235 rt_element=hashtb_n(nlsr->routing_table);
1236
1237 for(i=0;i<rt_element;i++)
1238 {
1239 rte=e->data;
1240 mapping_no=get_mapping_no(rte->dest_router);
1241 if ( mapping_no == NO_MAPPING_NUM)
1242 {
akmhoquede61ba92012-09-20 22:19:12 -05001243 delete_orig_router_from_npt(rte->dest_router);
akmhoque866c2222013-02-12 10:49:33 -06001244 /*
akmhoque63152c62012-09-18 08:43:42 -05001245 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1246 memset(router,0,strlen(rte->dest_router)+1);
1247 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1248 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque866c2222013-02-12 10:49:33 -06001249 */
akmhoque54a14f42013-02-24 06:16:20 -06001250 destroy_routing_table_entry_comp(rte);
akmhoque866c2222013-02-12 10:49:33 -06001251 hashtb_delete(e);
1252 i++;
akmhoque810a5b52012-09-09 16:53:14 -05001253 }
akmhoque866c2222013-02-12 10:49:33 -06001254 else
1255 {
1256 hashtb_next(e);
1257 }
akmhoque810a5b52012-09-09 16:53:14 -05001258 }
1259
1260 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001261}
akmhoque29c1db52012-09-07 14:47:43 -05001262
akmhoqueb77b95f2013-02-08 12:28:47 -06001263void
1264update_routing_table_with_new_hyperbolic_route(long int dest_router_rev_map_index, long int face, double nbr_to_dest_dist)
1265{
1266 if ( nlsr->debugging )
1267 printf("update_routing_table_with_new_hyperbolic_route called\n");
1268 if ( nlsr->detailed_logging )
1269 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_hyperbolic_route called\n");
1270
1271 char *orig_router=get_router_from_rev_map(dest_router_rev_map_index);
1272
1273 if (face != NO_NEXT_HOP && face != NO_FACE )
1274 {
1275 update_routing_table(orig_router,face,nbr_to_dest_dist);
1276 if ( nlsr->debugging )
1277 printf ("Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1278 if ( nlsr->detailed_logging )
1279 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %ld \n",orig_router,face);
1280
1281 }
1282
1283
1284}
akmhoquede61ba92012-09-20 22:19:12 -05001285
1286
akmhoquefbfd0982012-09-09 20:59:03 -05001287void
akmhoquede61ba92012-09-20 22:19:12 -05001288update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001289{
akmhoque1771c412012-11-09 13:06:08 -06001290 if ( nlsr->debugging )
1291 printf("update_routing_table_with_new_route called\n");
1292 if ( nlsr->detailed_logging )
1293 writeLogg(__FILE__,__FUNCTION__,__LINE__,"update_routing_table_with_new_route called\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001294 int i, map_element;
1295 struct map_entry *me;
1296
1297 struct hashtb_enumerator ee;
1298 struct hashtb_enumerator *e = &ee;
1299
1300 hashtb_start(nlsr->rev_map, e);
1301 map_element=hashtb_n(nlsr->rev_map);
1302
1303 for(i=0;i<map_element;i++)
1304 {
1305 me=e->data;
1306 if(me->mapping != source)
1307 {
1308
1309 char *orig_router=get_router_from_rev_map(me->mapping);
1310 if (orig_router != NULL )
1311 {
1312 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
akmhoquefbfd0982012-09-09 20:59:03 -05001313 if ( next_hop_router_num == NO_NEXT_HOP )
1314 {
akmhoque1771c412012-11-09 13:06:08 -06001315 if ( nlsr->debugging )
1316 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1317 if ( nlsr->detailed_logging )
1318 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001319 }
1320 else
1321 {
1322 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
akmhoquefbfd0982012-09-09 20:59:03 -05001323 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001324 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoque1771c412012-11-09 13:06:08 -06001325 if ( nlsr->debugging )
1326 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1327 if ( nlsr->detailed_logging )
1328 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1329
akmhoquefbfd0982012-09-09 20:59:03 -05001330
1331 }
1332 }
1333 }
1334 hashtb_next(e);
1335 }
1336
1337 hashtb_end(e);
1338}
1339
akmhoquede61ba92012-09-20 22:19:12 -05001340int
1341does_face_exist_for_router(char *dest_router, int face_id)
1342{
akmhoqueb77b95f2013-02-08 12:28:47 -06001343 if (nlsr->debugging)
1344 {
1345 printf("does_face_exist_for_router called\n");
1346 printf("Dest Router: %s and Face id: %d \n",dest_router, face_id);
1347 }
1348
akmhoquede61ba92012-09-20 22:19:12 -05001349 int ret=0;
1350
akmhoqueb77b95f2013-02-08 12:28:47 -06001351 int res;
akmhoquede61ba92012-09-20 22:19:12 -05001352 struct routing_table_entry *rte;
1353
1354 struct hashtb_enumerator ee;
1355 struct hashtb_enumerator *e = &ee;
1356
1357 hashtb_start(nlsr->routing_table, e);
1358 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1359
1360 if( res == HT_OLD_ENTRY )
1361 {
1362 rte=e->data;
akmhoqueb77b95f2013-02-08 12:28:47 -06001363 unsigned *v;
1364 v = hashtb_lookup(rte->face_list, &face_id, sizeof(face_id));
1365 if (v != NULL)
1366 ret = 1;
akmhoquede61ba92012-09-20 22:19:12 -05001367 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001368 else
akmhoquede61ba92012-09-20 22:19:12 -05001369 {
1370 hashtb_delete(e);
1371 }
1372
1373 hashtb_end(e);
1374
1375 return ret;
1376}
akmhoqueb77b95f2013-02-08 12:28:47 -06001377
1378double
1379get_hyperbolic_distance(long int source, long int dest)
1380{
1381 double distance;
1382 char *src_router=get_router_from_rev_map(source);
1383 char *dest_router=get_router_from_rev_map(dest);
1384
1385 double src_r=get_hyperbolic_r(src_router);
1386 double src_theta=get_hyperbolic_r(src_router);
1387
1388 double dest_r=get_hyperbolic_r(dest_router);
1389 double dest_theta=get_hyperbolic_r(dest_router);
1390 if ( src_r != -1 && dest_r != -1 )
1391 {
1392 distance=acosh(cosh(src_r)*cosh(dest_r)-sinh(src_r)*sinh(dest_r)*cos(src_theta-dest_theta));
1393 }
1394 else
1395 {
1396 distance= -1.0;
1397 }
1398
1399 return distance;
1400}
akmhoque54a14f42013-02-24 06:16:20 -06001401
1402void
1403destroy_routing_table_entry_comp(struct routing_table_entry *rte)
1404{
1405
1406 free(rte->dest_router);
1407 hashtb_destroy(&rte->face_list);
1408
1409}
1410
1411void
1412destroy_routing_table_entry(struct routing_table_entry *rte)
1413{
1414
1415 destroy_routing_table_entry_comp(rte);
1416 free(rte);
1417}
1418
1419void
1420destroy_routing_table(void)
1421{
1422 int i, rt_element;
1423 struct hashtb_enumerator ee;
1424 struct hashtb_enumerator *e = &ee;
1425
1426 struct routing_table_entry *rte;
1427 hashtb_start(nlsr->routing_table, e);
1428 rt_element=hashtb_n(nlsr->routing_table);
1429 for(i=0;i<rt_element;i++)
1430 {
1431 rte=e->data;
1432 free(rte->dest_router);
1433 hashtb_destroy(&rte->face_list);
1434 hashtb_next(e);
1435 }
1436 hashtb_end(e);
1437 hashtb_destroy(&nlsr->routing_table);
1438
1439}
1440
1441void
1442destroy_map(void)
1443{
1444
1445 int i, map_element;
1446 struct map_entry *me;
1447
1448 struct hashtb_enumerator ee;
1449 struct hashtb_enumerator *e = &ee;
1450
1451 hashtb_start(nlsr->map, e);
1452 map_element=hashtb_n(nlsr->map);
1453
1454 for(i=0;i<map_element;i++)
1455 {
1456 me=e->data;
1457 free(me->router);
1458 hashtb_next(e);
1459 }
1460
1461 hashtb_end(e);
1462
akmhoqueae388862013-03-15 01:46:22 -05001463 hashtb_destroy(&nlsr->map);
1464
akmhoque54a14f42013-02-24 06:16:20 -06001465}
1466
1467void
1468destroy_rev_map(void)
1469{
1470 int i, map_element;
1471 struct map_entry *me;
1472
1473 struct hashtb_enumerator ee;
1474 struct hashtb_enumerator *e = &ee;
1475
1476 hashtb_start(nlsr->rev_map, e);
1477 map_element=hashtb_n(nlsr->rev_map);
1478
1479 for(i=0;i<map_element;i++)
1480 {
1481 me=e->data;
1482 free(me->router);
1483 hashtb_next(e);
1484 }
1485
1486 hashtb_end(e);
akmhoqueae388862013-03-15 01:46:22 -05001487
1488 hashtb_destroy(&nlsr->rev_map);
akmhoque54a14f42013-02-24 06:16:20 -06001489}