blob: 2faaf21dff582cc6155266f0732cad02515dacf8 [file] [log] [blame]
akmhoque29c1db52012-09-07 14:47:43 -05001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <assert.h>
8#ifdef HAVE_CONFIG_H
9#include <config.h>
10#endif
11#include <sys/types.h>
12
13
14#include <ccn/ccn.h>
15#include <ccn/uri.h>
16#include <ccn/keystore.h>
17#include <ccn/signing.h>
18#include <ccn/schedule.h>
19#include <ccn/hashtb.h>
20
21
22#include "nlsr.h"
23#include "nlsr_route.h"
24#include "nlsr_lsdb.h"
akmhoque3560cb62012-09-09 10:52:30 -050025#include "nlsr_npt.h"
akmhoquefbfd0982012-09-09 20:59:03 -050026#include "nlsr_adl.h"
akmhoquede61ba92012-09-20 22:19:12 -050027#include "nlsr_fib.h"
akmhoque29c1db52012-09-07 14:47:43 -050028
29int
30route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
31{
akmhoqueffacaa82012-09-13 17:48:30 -050032
33 if(flags == CCN_SCHEDULE_CANCEL)
34 {
35 return -1;
36 }
37
38 nlsr_lock();
39
akmhoque29c1db52012-09-07 14:47:43 -050040 printf("route_calculate called\n");
41
42 if( ! nlsr->is_build_adj_lsa_sheduled )
43 {
44 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050045 print_routing_table();
46 print_npt();
47
akmhoque29c1db52012-09-07 14:47:43 -050048 struct hashtb_param param_me = {0};
49 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoquefbfd0982012-09-09 20:59:03 -050050 nlsr->rev_map = hashtb_create(sizeof(struct map_entry), &param_me);
akmhoque29c1db52012-09-07 14:47:43 -050051 make_map();
52 assign_mapping_number();
53 print_map();
akmhoquefbfd0982012-09-09 20:59:03 -050054 print_rev_map();
akmhoque29c1db52012-09-07 14:47:43 -050055
akmhoque3560cb62012-09-09 10:52:30 -050056 do_old_routing_table_updates();
akmhoquede61ba92012-09-20 22:19:12 -050057 clear_old_routing_table();
58 print_routing_table();
akmhoque3560cb62012-09-09 10:52:30 -050059
akmhoque29c1db52012-09-07 14:47:43 -050060 int i;
61 int **adj_matrix;
62 int map_element=hashtb_n(nlsr->map);
63 adj_matrix=malloc(map_element * sizeof(int *));
64 for(i = 0; i < map_element; i++)
65 {
66 adj_matrix[i] = malloc(map_element * sizeof(int));
67 }
68 make_adj_matrix(adj_matrix,map_element);
69 print_adj_matrix(adj_matrix,map_element);
70
71 long int source=get_mapping_no(nlsr->router_name);
72 long int *parent=(long int *)malloc(map_element * sizeof(long int));
akmhoquede61ba92012-09-20 22:19:12 -050073 long int *dist=(long int *)malloc(map_element * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -050074
akmhoque9fe296b2012-09-24 09:52:08 -050075 int num_link=get_no_link_from_adj_matrix(adj_matrix, map_element ,source);
akmhoque29c1db52012-09-07 14:47:43 -050076
akmhoque0820a2e2012-09-24 20:23:01 -050077 if ( (num_link == 0) || (nlsr->multi_path_face_num <= 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050078 {
79 calculate_path(adj_matrix,parent,dist, map_element, source);
80 print_all_path_from_source(parent,source);
81 print_all_next_hop(parent,source);
82 update_routing_table_with_new_route(parent, dist,source);
83 }
akmhoque0820a2e2012-09-24 20:23:01 -050084 else if ( (num_link != 0) && (nlsr->multi_path_face_num >= 1 ) )
akmhoque9fe296b2012-09-24 09:52:08 -050085 {
86 long int *links=(long int *)malloc(num_link*sizeof(long int));
87 long int *link_costs=(long int *)malloc(num_link*sizeof(long int));
88 get_links_from_adj_matrix(adj_matrix, map_element , links, link_costs, source);
89 for ( i=0 ; i < num_link; i++)
90 {
91 adjust_adj_matrix(adj_matrix, map_element,source,links[i],link_costs[i]);
92 calculate_path(adj_matrix,parent,dist, map_element, source);
93 print_all_path_from_source(parent,source);
94 print_all_next_hop(parent,source);
95 update_routing_table_with_new_route(parent, dist,source);
96 }
akmhoque29c1db52012-09-07 14:47:43 -050097
akmhoque9fe296b2012-09-24 09:52:08 -050098 free(links);
99 free(link_costs);
100 }
akmhoquede61ba92012-09-20 22:19:12 -0500101
102 update_npt_with_new_route();
akmhoquefbfd0982012-09-09 20:59:03 -0500103
akmhoqueffacaa82012-09-13 17:48:30 -0500104 print_routing_table();
105 print_npt();
akmhoquefbfd0982012-09-09 20:59:03 -0500106
akmhoque29c1db52012-09-07 14:47:43 -0500107 for(i = 0; i < map_element; i++)
108 {
109 free(adj_matrix[i]);
110 }
111 free(parent);
akmhoquede61ba92012-09-20 22:19:12 -0500112 free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500113 free(adj_matrix);
114 hashtb_destroy(&nlsr->map);
akmhoquefbfd0982012-09-09 20:59:03 -0500115 hashtb_destroy(&nlsr->rev_map);
akmhoque29c1db52012-09-07 14:47:43 -0500116
117 }
118 nlsr->is_route_calculation_scheduled=0;
119
akmhoqueffacaa82012-09-13 17:48:30 -0500120 nlsr_unlock();
121
akmhoque29c1db52012-09-07 14:47:43 -0500122 return 0;
123}
124
125void
akmhoquede61ba92012-09-20 22:19:12 -0500126calculate_path(int **adj_matrix, long int *parent,long int *dist ,long int V, long int S)
akmhoque29c1db52012-09-07 14:47:43 -0500127{
128 int i;
129 long int v,u;
akmhoquede61ba92012-09-20 22:19:12 -0500130 //long int *dist=(long int *)malloc(V * sizeof(long int));
akmhoque29c1db52012-09-07 14:47:43 -0500131 long int *Q=(long int *)malloc(V * sizeof(long int));
132 long int head=0;
133 /* Initial the Parent */
134 for (i = 0 ; i < V; i++)
135 {
136 parent[i]=EMPTY_PARENT;
137 dist[i]=INF_DISTANCE;
138 Q[i]=i;
139 }
140
akmhoque9fe296b2012-09-24 09:52:08 -0500141 if ( S != NO_MAPPING_NUM )
akmhoque29c1db52012-09-07 14:47:43 -0500142 {
akmhoque9fe296b2012-09-24 09:52:08 -0500143 dist[S]=0;
144 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500145
akmhoque9fe296b2012-09-24 09:52:08 -0500146 while (head < V )
akmhoque29c1db52012-09-07 14:47:43 -0500147 {
akmhoque9fe296b2012-09-24 09:52:08 -0500148 u=Q[head];
149 if(dist[u] == INF_DISTANCE)
akmhoque29c1db52012-09-07 14:47:43 -0500150 {
akmhoque9fe296b2012-09-24 09:52:08 -0500151 break;
152 }
153
154 for(v=0 ; v <V; v++)
155 {
156 if( adj_matrix[u][v] > 0 ) //
akmhoque29c1db52012-09-07 14:47:43 -0500157 {
akmhoque9fe296b2012-09-24 09:52:08 -0500158 if ( is_not_explored(Q,v,head+1,V) )
akmhoque29c1db52012-09-07 14:47:43 -0500159 {
akmhoque9fe296b2012-09-24 09:52:08 -0500160
161 if( dist[u] + adj_matrix[u][v] < dist[v])
162 {
163 dist[v]=dist[u] + adj_matrix[u][v] ;
164 parent[v]=u;
165 }
166
167 }
akmhoque29c1db52012-09-07 14:47:43 -0500168
169 }
170
171 }
172
akmhoque9fe296b2012-09-24 09:52:08 -0500173 head++;
174 sort_queue_by_distance(Q,dist,head,V);
akmhoque29c1db52012-09-07 14:47:43 -0500175 }
akmhoque29c1db52012-09-07 14:47:43 -0500176 }
akmhoque29c1db52012-09-07 14:47:43 -0500177 free(Q);
akmhoquede61ba92012-09-20 22:19:12 -0500178 //free(dist);
akmhoque29c1db52012-09-07 14:47:43 -0500179}
180
181void
182print_all_path_from_source(long int *parent,long int source)
183{
184 int i, map_element;
185 struct map_entry *me;
186
187 struct hashtb_enumerator ee;
188 struct hashtb_enumerator *e = &ee;
189
190 hashtb_start(nlsr->map, e);
191 map_element=hashtb_n(nlsr->map);
192
akmhoque9fe296b2012-09-24 09:52:08 -0500193 if ( source != NO_MAPPING_NUM)
akmhoque29c1db52012-09-07 14:47:43 -0500194 {
akmhoque9fe296b2012-09-24 09:52:08 -0500195 for(i=0;i<map_element;i++)
akmhoque29c1db52012-09-07 14:47:43 -0500196 {
akmhoque9fe296b2012-09-24 09:52:08 -0500197 me=e->data;
198 if(me->mapping != source)
199 {
200 print_path(parent,(long int)me->mapping);
201 printf("\n");
202 }
203 hashtb_next(e);
akmhoque29c1db52012-09-07 14:47:43 -0500204 }
akmhoque29c1db52012-09-07 14:47:43 -0500205 }
akmhoque29c1db52012-09-07 14:47:43 -0500206 hashtb_end(e);
207
208}
209
akmhoquefbfd0982012-09-09 20:59:03 -0500210void
211print_all_next_hop(long int *parent,long int source)
212{
213 int i, map_element;
214 struct map_entry *me;
215
216 struct hashtb_enumerator ee;
217 struct hashtb_enumerator *e = &ee;
218
219 hashtb_start(nlsr->map, e);
220 map_element=hashtb_n(nlsr->map);
221
222 for(i=0;i<map_element;i++)
223 {
224 me=e->data;
225 if(me->mapping != source)
226 {
227
228 printf("Dest: %d Next Hop: %ld\n",me->mapping,get_next_hop_from_calculation(parent,me->mapping,source));
229 }
230 hashtb_next(e);
231 }
232
233 hashtb_end(e);
234
235}
236
237long int
238get_next_hop_from_calculation(long int *parent, long int dest,long int source)
239{
240 long int next_hop;
241 while ( parent[dest] != EMPTY_PARENT )
242 {
243 next_hop=dest;
244 dest=parent[dest];
245
246 }
247
248 if ( dest != source )
249 {
250 next_hop=NO_NEXT_HOP;
251 }
252
253 return next_hop;
254
255}
256
akmhoque29c1db52012-09-07 14:47:43 -0500257void
258print_path(long int *parent, long int dest)
259{
260 if (parent[dest] != EMPTY_PARENT )
261 print_path(parent,parent[dest]);
262
263 printf(" %ld",dest);
264}
265
266int
267is_not_explored(long int *Q, long int u,long int start, long int element)
268{
269 int ret=0;
270 long int i;
271 for(i=start; i< element; i++)
272 {
273 if ( Q[i] == u )
274 {
275 ret=1;
276 break;
277 }
278 }
279 return ret;
280}
281
282void
283sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
284{
285 long int i,j;
286 long int temp_u;
287
288 for ( i=start ; i < element ; i ++)
289 {
290 for( j=i+1; j<element; j ++)
291 {
292 if (dist[Q[j]] < dist[Q[i]])
293 {
294 temp_u=Q[j];
295 Q[j]=Q[i];
296 Q[i]=temp_u;
297 }
298 }
299 }
300
301}
302
303void
304print_map(void)
305{
306 int i, map_element;
307 struct map_entry *me;
308
309 struct hashtb_enumerator ee;
310 struct hashtb_enumerator *e = &ee;
311
312 hashtb_start(nlsr->map, e);
313 map_element=hashtb_n(nlsr->map);
314
315 for(i=0;i<map_element;i++)
316 {
317 me=e->data;
318 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
319 hashtb_next(e);
320 }
321
322 hashtb_end(e);
323}
324
325
326void
327assign_mapping_number(void)
328{
329 int i, map_element;
330 struct map_entry *me;
331
332 struct hashtb_enumerator ee;
333 struct hashtb_enumerator *e = &ee;
334
335 hashtb_start(nlsr->map, e);
336 map_element=hashtb_n(nlsr->map);
337
338 for(i=0;i<map_element;i++)
339 {
340 me=e->data;
341 me->mapping=i;
akmhoquefbfd0982012-09-09 20:59:03 -0500342 add_rev_map_entry(i,me->router);
akmhoque29c1db52012-09-07 14:47:43 -0500343 hashtb_next(e);
344 }
345
346 hashtb_end(e);
347}
348
349void
350make_map(void)
351{
352
353
354 int i, adj_lsdb_element;
355 struct alsa *adj_lsa;
356
357 struct hashtb_enumerator ee;
358 struct hashtb_enumerator *e = &ee;
359
360 hashtb_start(nlsr->lsdb->adj_lsdb, e);
361 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
362
akmhoque9fe296b2012-09-24 09:52:08 -0500363 add_map_entry(nlsr->router_name);
364
akmhoque29c1db52012-09-07 14:47:43 -0500365 for(i=0;i<adj_lsdb_element;i++)
366 {
367 adj_lsa=e->data;
368 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
369 hashtb_next(e);
370 }
371
372 hashtb_end(e);
373
374}
375
376void
377add_map_entry(char *router)
378{
379
380 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
381
382 struct hashtb_enumerator ee;
383 struct hashtb_enumerator *e = &ee;
384 int res;
385
386 hashtb_start(nlsr->map, e);
387 res = hashtb_seek(e, router, strlen(router), 0);
388
389 if(res == HT_NEW_ENTRY)
390 {
391 me=e->data;
392 me->router=(char *)malloc(strlen(router)+1);
393 memset(me->router,0,strlen(router)+1);
394 memcpy(me->router,router,strlen(router));
395 me->mapping=0;
396 }
397
398 hashtb_end(e);
399
400}
401
402
403void
404add_adj_data_to_map(char *orig_router, char *body, int no_link)
405{
406 add_map_entry(orig_router);
407
408 int i=0;
409 char *lsa_data=(char *)malloc(strlen(body)+1);
410 memset( lsa_data,0,strlen(body)+1);
411 memcpy(lsa_data,body,strlen(body)+1);
412 char *sep="|";
413 char *rem;
414 char *rtr_id;
415 char *length;
416 char *face;
417 char *metric;
418
419 if(no_link >0 )
420 {
421 rtr_id=strtok_r(lsa_data,sep,&rem);
422 length=strtok_r(NULL,sep,&rem);
423 face=strtok_r(NULL,sep,&rem);
424 metric=strtok_r(NULL,sep,&rem);
425
426 add_map_entry(rtr_id);
427
428 for(i=1;i<no_link;i++)
429 {
430 rtr_id=strtok_r(NULL,sep,&rem);
431 length=strtok_r(NULL,sep,&rem);
432 face=strtok_r(NULL,sep,&rem);
433 metric=strtok_r(NULL,sep,&rem);
434
435 add_map_entry(rtr_id);
436
437 }
438 }
439
440 free(lsa_data);
441}
442
443int
444get_mapping_no(char *router)
445{
446 struct map_entry *me;
447
448 struct hashtb_enumerator ee;
449 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500450 int res;
akmhoque810a5b52012-09-09 16:53:14 -0500451 int ret;
akmhoque29c1db52012-09-07 14:47:43 -0500452
akmhoque63152c62012-09-18 08:43:42 -0500453 int n = hashtb_n(nlsr->map);
454
455 if ( n < 1)
456 {
457 return NO_MAPPING_NUM;
458 }
459
akmhoque29c1db52012-09-07 14:47:43 -0500460 hashtb_start(nlsr->map, e);
461 res = hashtb_seek(e, router, strlen(router), 0);
462
463 if( res == HT_OLD_ENTRY )
464 {
465 me=e->data;
466 ret=me->mapping;
467 }
468 else if(res == HT_NEW_ENTRY)
469 {
470 hashtb_delete(e);
akmhoque810a5b52012-09-09 16:53:14 -0500471 ret=NO_MAPPING_NUM;
akmhoque29c1db52012-09-07 14:47:43 -0500472 }
473
474 hashtb_end(e);
475
476 return ret;
477
478}
479
akmhoquefbfd0982012-09-09 20:59:03 -0500480
481void
482add_rev_map_entry(long int mapping_number, char *router)
483{
484
485 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
486
487 struct hashtb_enumerator ee;
488 struct hashtb_enumerator *e = &ee;
489 int res;
490
491 hashtb_start(nlsr->rev_map, e);
492 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
493
494 if(res == HT_NEW_ENTRY)
495 {
496 me=e->data;
497 me->router=(char *)malloc(strlen(router)+1);
498 memset(me->router,0,strlen(router)+1);
499 memcpy(me->router,router,strlen(router));
500 me->mapping=mapping_number;
501 }
502
503 hashtb_end(e);
504
505}
506
507
508
509char *
510get_router_from_rev_map(long int mapping_number)
511{
512
513 struct map_entry *me;
514
515 struct hashtb_enumerator ee;
516 struct hashtb_enumerator *e = &ee;
517 int res;
518
519 hashtb_start(nlsr->rev_map, e);
520 res = hashtb_seek(e, &mapping_number, sizeof(mapping_number), 0);
521
522 if(res == HT_OLD_ENTRY)
523 {
524 me=e->data;
525 hashtb_end(e);
526 return me->router;
527 }
528 else if(res == HT_NEW_ENTRY)
529 {
530 hashtb_delete(e);
531 hashtb_end(e);
532 }
533
534 return NULL;
535}
536
537void
538print_rev_map(void)
539{
540 int i, map_element;
541 struct map_entry *me;
542
543 struct hashtb_enumerator ee;
544 struct hashtb_enumerator *e = &ee;
545
546 hashtb_start(nlsr->map, e);
547 map_element=hashtb_n(nlsr->map);
548
549 for(i=0;i<map_element;i++)
550 {
551 me=e->data;
552 printf("Mapping Number: %d Router: %s \n",me->mapping,me->router);
553 hashtb_next(e);
554 }
555
556 hashtb_end(e);
557}
558
559
akmhoque29c1db52012-09-07 14:47:43 -0500560void
561assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
562{
563 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
564 int mapping_nbr_router;
565
566 int i;
567 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
568 memset( lsa_data,0,strlen(adj_lsa->body)+1);
569 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
570 char *sep="|";
571 char *rem;
572 char *rtr_id;
573 char *length;
574 char *face;
575 char *metric;
576
577 if(adj_lsa->no_link >0 )
578 {
579 rtr_id=strtok_r(lsa_data,sep,&rem);
580 length=strtok_r(NULL,sep,&rem);
581 face=strtok_r(NULL,sep,&rem);
582 metric=strtok_r(NULL,sep,&rem);
583
584 mapping_nbr_router=get_mapping_no(rtr_id);
585 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
586
587 for(i=1;i<adj_lsa->no_link;i++)
588 {
589 rtr_id=strtok_r(NULL,sep,&rem);
590 length=strtok_r(NULL,sep,&rem);
591 face=strtok_r(NULL,sep,&rem);
592 metric=strtok_r(NULL,sep,&rem);
593
594 mapping_nbr_router=get_mapping_no(rtr_id);
595 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
596
597 }
598 }
599
600 free(lsa_data);
601}
602
603void
604make_adj_matrix(int **adj_matrix,int map_element)
605{
606
607 init_adj_matrix(adj_matrix,map_element);
608
609 int i, adj_lsdb_element;
610 struct alsa *adj_lsa;
611
612 struct hashtb_enumerator ee;
613 struct hashtb_enumerator *e = &ee;
614
615 hashtb_start(nlsr->lsdb->adj_lsdb, e);
616 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
617
618 for(i=0;i<adj_lsdb_element;i++)
619 {
620 adj_lsa=e->data;
621 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
622 hashtb_next(e);
623 }
624
625 hashtb_end(e);
626
627}
628
629void
630init_adj_matrix(int **adj_matrix,int map_element)
631{
632 int i, j;
633 for(i=0;i<map_element;i++)
634 for(j=0;j<map_element;j++)
635 adj_matrix[i][j]=0;
636}
637
638void print_adj_matrix(int **adj_matrix, int map_element)
639{
640 int i, j;
641 for(i=0;i<map_element;i++)
642 {
643 for(j=0;j<map_element;j++)
644 printf("%d ",adj_matrix[i][j]);
645 printf("\n");
646 }
647}
648
akmhoquede61ba92012-09-20 22:19:12 -0500649
akmhoque3560cb62012-09-09 10:52:30 -0500650int
akmhoque9fe296b2012-09-24 09:52:08 -0500651get_no_link_from_adj_matrix(int **adj_matrix,long int V, long int S)
652{
653 int no_link=0;
654 int i;
655
656 for(i=0;i<V;i++)
657 {
658 if ( adj_matrix[S][i] > 0 )
659 {
660 no_link++;
661 }
662 }
663 return no_link;
664}
665
666void
667get_links_from_adj_matrix(int **adj_matrix, long int V ,long int *links, long int *link_costs,long int S)
668{
669 int i,j;
670 j=0;
671 for (i=0; i <V; i++)
672 {
673 if ( adj_matrix[S][i] > 0 )
674 {
675 links[j]=i;
676 link_costs[j]=adj_matrix[S][i];
677 j++;
678 }
679 }
680}
681
682void adjust_adj_matrix(int **adj_matrix, long int V, long int S, long int link,long int link_cost)
683{
684 int i;
685 for ( i = 0; i < V; i++ )
686 {
687 if ( i == link )
688 {
689 adj_matrix[S][i]=link_cost;
690 }
691 else
692 {
693 adj_matrix[S][i]=0;
694 }
695 }
696
697}
698
699int
akmhoquede61ba92012-09-20 22:19:12 -0500700get_number_of_next_hop(char *dest_router)
akmhoque3560cb62012-09-09 10:52:30 -0500701{
702 struct routing_table_entry *rte;
703
704 struct hashtb_enumerator ee;
705 struct hashtb_enumerator *e = &ee;
706 int res,ret;
707
708 hashtb_start(nlsr->routing_table, e);
709 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
710
711 if( res == HT_OLD_ENTRY )
712 {
713 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500714 ret=hashtb_n(rte->face_list);
715 //nhl=rte->face_list;
716 }
717 else if(res == HT_NEW_ENTRY)
718 {
719 hashtb_delete(e);
720 ret=NO_NEXT_HOP;
721 }
722
723 hashtb_end(e);
724
725 return ret;
726}
727
728
729int
730get_next_hop(char *dest_router,int *faces, int *route_costs)
731{
732 struct routing_table_entry *rte;
733
734 struct hashtb_enumerator ee;
735 struct hashtb_enumerator *e = &ee;
736 int res,ret;
737
738 hashtb_start(nlsr->routing_table, e);
739 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
740
741 if( res == HT_OLD_ENTRY )
742 {
743 rte=e->data;
744 ret=hashtb_n(rte->face_list);
745 //nhl=rte->face_list;
746 int j,face_list_element;
747 struct face_list_entry *fle;
748
749 struct hashtb_enumerator eef;
750 struct hashtb_enumerator *ef = &eef;
751
752 hashtb_start(rte->face_list, ef);
753 face_list_element=hashtb_n(rte->face_list);
754 for(j=0;j<face_list_element;j++)
755 {
756 fle=ef->data;
757 //printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
758 faces[j]=fle->next_hop_face;
759 route_costs[j]=fle->route_cost;
760 hashtb_next(ef);
761 }
762 hashtb_end(ef);
763
akmhoque3560cb62012-09-09 10:52:30 -0500764 }
765 else if(res == HT_NEW_ENTRY)
766 {
767 hashtb_delete(e);
768 ret=NO_NEXT_HOP;
769 }
770
771 hashtb_end(e);
772
773 return ret;
774}
775
776void
777add_next_hop_router(char *dest_router)
778{
779 if ( strcmp(dest_router,nlsr->router_name)== 0)
780 {
781 return ;
782 }
783
784 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
785
786 struct hashtb_enumerator ee;
787 struct hashtb_enumerator *e = &ee;
788 int res;
789
790 hashtb_start(nlsr->routing_table, e);
791 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
792
793 if( res == HT_NEW_ENTRY )
794 {
795 rte=e->data;
796 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
797 memset(rte->dest_router,0,strlen(dest_router)+1);
798 memcpy(rte->dest_router,dest_router,strlen(dest_router));
akmhoquede61ba92012-09-20 22:19:12 -0500799 //rte->next_hop_face=NO_NEXT_HOP;
800 struct hashtb_param param_fle = {0};
801 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
802
803 add_npt_entry(dest_router, dest_router, 0, NULL, NULL);
akmhoque3560cb62012-09-09 10:52:30 -0500804 }
805 hashtb_end(e);
806
807}
808
809void
810add_next_hop_from_lsa_adj_body(char *body, int no_link)
811{
812
813 int i=0;
814 char *lsa_data=(char *)malloc(strlen(body)+1);
815 memset( lsa_data,0,strlen(body)+1);
816 memcpy(lsa_data,body,strlen(body)+1);
817 char *sep="|";
818 char *rem;
819 char *rtr_id;
820 char *length;
821 char *face;
822 char *metric;
823
824 if(no_link >0 )
825 {
826 rtr_id=strtok_r(lsa_data,sep,&rem);
827 length=strtok_r(NULL,sep,&rem);
828 face=strtok_r(NULL,sep,&rem);
829 metric=strtok_r(NULL,sep,&rem);
830
akmhoque3560cb62012-09-09 10:52:30 -0500831
832 add_next_hop_router(rtr_id);
833
834 for(i=1;i<no_link;i++)
835 {
836 rtr_id=strtok_r(NULL,sep,&rem);
837 length=strtok_r(NULL,sep,&rem);
838 face=strtok_r(NULL,sep,&rem);
839 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500840
841 add_next_hop_router(rtr_id);
842
843 }
844 }
845
846 free(lsa_data);
847
848
849}
850
851void
akmhoquede61ba92012-09-20 22:19:12 -0500852update_routing_table(char * dest_router,int next_hop_face, int route_cost)
akmhoqueffacaa82012-09-13 17:48:30 -0500853{
akmhoquede61ba92012-09-20 22:19:12 -0500854 int res,res1;
akmhoqueffacaa82012-09-13 17:48:30 -0500855 struct routing_table_entry *rte;
856
857 struct hashtb_enumerator ee;
858 struct hashtb_enumerator *e = &ee;
859
860 hashtb_start(nlsr->routing_table, e);
861 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
862
863 if( res == HT_OLD_ENTRY )
864 {
865 rte=e->data;
akmhoquede61ba92012-09-20 22:19:12 -0500866
867 struct hashtb_enumerator eef;
868 struct hashtb_enumerator *ef = &eef;
869
870 hashtb_start(rte->face_list, ef);
871 res1 = hashtb_seek(ef, &next_hop_face, sizeof(next_hop_face), 0);
872 if( res1 == HT_NEW_ENTRY)
873 {
874 struct face_list_entry *fle=(struct face_list_entry *)malloc(sizeof(struct face_list_entry));
875 fle=ef->data;
876 fle->next_hop_face=next_hop_face;
877 fle->route_cost=route_cost;
878 }
879 else if ( res1 == HT_OLD_ENTRY )
880 {
881 struct face_list_entry *fle;
882 fle=ef->data;
883 fle->route_cost=route_cost;
884 }
885 hashtb_end(ef);
886
887 /*
888 //updating the face for the router prefix itself
889 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
890 {
891 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_UNREG, rte->next_hop_face);
892 }
893 if ( (next_hop_face != NO_FACE || next_hop_face != NO_NEXT_HOP ) && is_neighbor(dest_router)==0 )
894 {
895 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)dest_router, OP_REG, next_hop_face);
896 }
897
akmhoqueffacaa82012-09-13 17:48:30 -0500898 rte->next_hop_face=next_hop_face;
akmhoquede61ba92012-09-20 22:19:12 -0500899 */
akmhoqueffacaa82012-09-13 17:48:30 -0500900 }
901 else if ( res == HT_OLD_ENTRY )
902 {
903 hashtb_delete(e);
904 }
905
906 hashtb_end(e);
907
908}
909
910void
akmhoque3560cb62012-09-09 10:52:30 -0500911print_routing_table(void)
912{
913 printf("\n");
914 printf("print_routing_table called\n");
akmhoquede61ba92012-09-20 22:19:12 -0500915 int i,j, rt_element,face_list_element;
akmhoque3560cb62012-09-09 10:52:30 -0500916
917 struct routing_table_entry *rte;
918
919 struct hashtb_enumerator ee;
920 struct hashtb_enumerator *e = &ee;
921
922 hashtb_start(nlsr->routing_table, e);
923 rt_element=hashtb_n(nlsr->routing_table);
924
925 for(i=0;i<rt_element;i++)
926 {
927 printf("----------Routing Table Entry %d------------------\n",i+1);
928 rte=e->data;
929 printf(" Destination Router: %s \n",rte->dest_router);
akmhoquede61ba92012-09-20 22:19:12 -0500930 //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);
931
932 struct face_list_entry *fle;
933
934 struct hashtb_enumerator eef;
935 struct hashtb_enumerator *ef = &eef;
936
937 hashtb_start(rte->face_list, ef);
938 face_list_element=hashtb_n(rte->face_list);
939 if ( face_list_element <= 0 )
940 {
941 printf(" Face: No Face \n");
942 }
943 else
944 {
945 for(j=0;j<face_list_element;j++)
946 {
947 fle=ef->data;
948 printf(" Face: %d Route_Cost: %d \n",fle->next_hop_face,fle->route_cost);
949 hashtb_next(ef);
950 }
951 }
952 hashtb_end(ef);
953
akmhoque3560cb62012-09-09 10:52:30 -0500954 hashtb_next(e);
955 }
956
957 hashtb_end(e);
958
959 printf("\n");
960}
961
akmhoque63152c62012-09-18 08:43:42 -0500962
963int
964delete_empty_rte(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
965{
966 printf("delete_empty_rte called\n");
967 printf("Router: %s \n",(char *)ev->evdata);
968 if(flags == CCN_SCHEDULE_CANCEL)
969 {
970 return -1;
971 }
972
akmhoquede61ba92012-09-20 22:19:12 -0500973 //struct routing_table_entry *rte;
akmhoque63152c62012-09-18 08:43:42 -0500974 int res;
975 struct hashtb_enumerator ee;
976 struct hashtb_enumerator *e = &ee;
977
978 hashtb_start(nlsr->routing_table, e);
979 res = hashtb_seek(e, (char *)ev->evdata, strlen((char *)ev->evdata), 0);
980
981 if ( res == HT_OLD_ENTRY )
982 {
akmhoquede61ba92012-09-20 22:19:12 -0500983 //rte=e->data;
984
985 /*
986
987 if ( (rte->next_hop_face != NO_FACE || rte->next_hop_face != NO_NEXT_HOP) && is_neighbor(ev->evdata)==0 )
988 {
989 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)ev->evdata, OP_UNREG, rte->next_hop_face);
990 }
991
992 */
akmhoque63152c62012-09-18 08:43:42 -0500993 hashtb_delete(e);
994 }
995 else if ( res == HT_NEW_ENTRY )
996 {
997 hashtb_delete(e);
998 }
999
akmhoquede61ba92012-09-20 22:19:12 -05001000 print_routing_table();
1001
akmhoque63152c62012-09-18 08:43:42 -05001002 return 0;
1003}
1004
akmhoquede61ba92012-09-20 22:19:12 -05001005void
akmhoque3cced642012-09-24 16:20:20 -05001006clear_old_routing_table(void)
akmhoquede61ba92012-09-20 22:19:12 -05001007{
1008 printf("clear_old_routing_table called\n");
1009 int i,rt_element;
1010
1011 struct routing_table_entry *rte;
1012
1013 struct hashtb_enumerator ee;
1014 struct hashtb_enumerator *e = &ee;
1015
1016 hashtb_start(nlsr->routing_table, e);
1017 rt_element=hashtb_n(nlsr->routing_table);
1018
1019 for(i=0;i<rt_element;i++)
1020 {
1021 rte=e->data;
1022 hashtb_destroy(&rte->face_list);
1023 struct hashtb_param param_fle = {0};
1024 rte->face_list=hashtb_create(sizeof(struct face_list_entry), &param_fle);
1025
1026 hashtb_next(e);
1027 }
1028
1029 hashtb_end(e);
1030}
1031
akmhoque63152c62012-09-18 08:43:42 -05001032
akmhoque3560cb62012-09-09 10:52:30 -05001033void
akmhoque3cced642012-09-24 16:20:20 -05001034do_old_routing_table_updates(void)
akmhoque3560cb62012-09-09 10:52:30 -05001035{
akmhoque810a5b52012-09-09 16:53:14 -05001036 printf("do_old_routing_table_updates called\n");
1037 int i, rt_element;
1038 int mapping_no;
1039
1040 struct routing_table_entry *rte;
1041
1042 struct hashtb_enumerator ee;
1043 struct hashtb_enumerator *e = &ee;
1044
1045 hashtb_start(nlsr->routing_table, e);
1046 rt_element=hashtb_n(nlsr->routing_table);
1047
1048 for(i=0;i<rt_element;i++)
1049 {
1050 rte=e->data;
1051 mapping_no=get_mapping_no(rte->dest_router);
1052 if ( mapping_no == NO_MAPPING_NUM)
1053 {
akmhoquede61ba92012-09-20 22:19:12 -05001054 delete_orig_router_from_npt(rte->dest_router);
akmhoque63152c62012-09-18 08:43:42 -05001055 char *router=(char *)malloc(strlen(rte->dest_router)+1);
1056 memset(router,0,strlen(rte->dest_router)+1);
1057 memcpy(router,rte->dest_router,strlen(rte->dest_router)+1);
1058 nlsr->event = ccn_schedule_event(nlsr->sched, 1, &delete_empty_rte, (void *)router , 0);
akmhoque810a5b52012-09-09 16:53:14 -05001059 }
1060 hashtb_next(e);
1061 }
1062
1063 hashtb_end(e);
akmhoque3560cb62012-09-09 10:52:30 -05001064}
akmhoque29c1db52012-09-07 14:47:43 -05001065
akmhoquede61ba92012-09-20 22:19:12 -05001066
1067
akmhoquefbfd0982012-09-09 20:59:03 -05001068void
akmhoquede61ba92012-09-20 22:19:12 -05001069update_routing_table_with_new_route(long int *parent, long int *dist,long int source)
akmhoquefbfd0982012-09-09 20:59:03 -05001070{
1071 printf("update_routing_table_with_new_route called\n");
1072 int i, map_element;
1073 struct map_entry *me;
1074
1075 struct hashtb_enumerator ee;
1076 struct hashtb_enumerator *e = &ee;
1077
1078 hashtb_start(nlsr->rev_map, e);
1079 map_element=hashtb_n(nlsr->rev_map);
1080
1081 for(i=0;i<map_element;i++)
1082 {
1083 me=e->data;
1084 if(me->mapping != source)
1085 {
1086
1087 char *orig_router=get_router_from_rev_map(me->mapping);
1088 if (orig_router != NULL )
1089 {
1090 int next_hop_router_num=get_next_hop_from_calculation(parent,me->mapping,source);
1091 //printf(" Next hop router Num: %d ",next_hop_router_num);
1092 if ( next_hop_router_num == NO_NEXT_HOP )
1093 {
akmhoquede61ba92012-09-20 22:19:12 -05001094 //update_npt_with_new_route(orig_router,NO_FACE);
akmhoquefbfd0982012-09-09 20:59:03 -05001095 printf ("\nOrig_router: %s Next Hop Face: %d \n",orig_router,NO_FACE);
1096 }
1097 else
1098 {
1099 char *next_hop_router=get_router_from_rev_map(next_hop_router_num);
1100 //printf("Next hop router name: %s \n",next_hop_router);
1101 int next_hop_face=get_next_hop_face_from_adl(next_hop_router);
akmhoquede61ba92012-09-20 22:19:12 -05001102 //update_npt_with_new_route(orig_router,next_hop_face);
1103 update_routing_table(orig_router,next_hop_face,dist[me->mapping]);
akmhoquefbfd0982012-09-09 20:59:03 -05001104 printf ("Orig_router: %s Next Hop Face: %d \n",orig_router,next_hop_face);
1105
1106 }
1107 }
1108 }
1109 hashtb_next(e);
1110 }
1111
1112 hashtb_end(e);
1113}
1114
akmhoquede61ba92012-09-20 22:19:12 -05001115int
1116does_face_exist_for_router(char *dest_router, int face_id)
1117{
1118 int ret=0;
1119
1120 int res,res1;
1121 struct routing_table_entry *rte;
1122
1123 struct hashtb_enumerator ee;
1124 struct hashtb_enumerator *e = &ee;
1125
1126 hashtb_start(nlsr->routing_table, e);
1127 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
1128
1129 if( res == HT_OLD_ENTRY )
1130 {
1131 rte=e->data;
1132 struct hashtb_enumerator eef;
1133 struct hashtb_enumerator *ef = &eef;
1134
1135 hashtb_start(rte->face_list, ef);
1136 res1 = hashtb_seek(ef, &face_id, sizeof(face_id), 0);
1137 if( res1 == HT_OLD_ENTRY)
1138 {
1139 ret=1;
1140 }
1141 else if ( res1 == HT_OLD_ENTRY )
1142 {
1143 hashtb_delete(ef);
1144 }
1145 hashtb_end(ef);
1146 }
1147 else if( res == HT_NEW_ENTRY )
1148 {
1149 hashtb_delete(e);
1150 }
1151
1152 hashtb_end(e);
1153
1154 return ret;
1155}