blob: c3dc3aca1f48c5d7bc19c5137519d2c5e911edd0 [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"
akmhoque29c1db52012-09-07 14:47:43 -050026
27int
28route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags)
29{
30 printf("route_calculate called\n");
31
32 if( ! nlsr->is_build_adj_lsa_sheduled )
33 {
34 /* Calculate Route here */
akmhoque3560cb62012-09-09 10:52:30 -050035 print_routing_table();
36 print_npt();
37
akmhoque29c1db52012-09-07 14:47:43 -050038 struct hashtb_param param_me = {0};
39 nlsr->map = hashtb_create(sizeof(struct map_entry), &param_me);
40 make_map();
41 assign_mapping_number();
42 print_map();
43
akmhoque3560cb62012-09-09 10:52:30 -050044
45 do_old_routing_table_updates();
46
47
akmhoque29c1db52012-09-07 14:47:43 -050048 int i;
49 int **adj_matrix;
50 int map_element=hashtb_n(nlsr->map);
51 adj_matrix=malloc(map_element * sizeof(int *));
52 for(i = 0; i < map_element; i++)
53 {
54 adj_matrix[i] = malloc(map_element * sizeof(int));
55 }
56 make_adj_matrix(adj_matrix,map_element);
57 print_adj_matrix(adj_matrix,map_element);
58
59 long int source=get_mapping_no(nlsr->router_name);
60 long int *parent=(long int *)malloc(map_element * sizeof(long int));
61
62 calculate_path(adj_matrix,parent, map_element, source);
63
64 print_all_path_from_source(parent,source);
65
akmhoque3560cb62012-09-09 10:52:30 -050066
akmhoque29c1db52012-09-07 14:47:43 -050067
68 for(i = 0; i < map_element; i++)
69 {
70 free(adj_matrix[i]);
71 }
72 free(parent);
73 free(adj_matrix);
74 hashtb_destroy(&nlsr->map);
75
76 }
77 nlsr->is_route_calculation_scheduled=0;
78
79 return 0;
80}
81
82void
83calculate_path(int **adj_matrix, long int *parent, long int V, long int S)
84{
85 int i;
86 long int v,u;
87 long int *dist=(long int *)malloc(V * sizeof(long int));
88 long int *Q=(long int *)malloc(V * sizeof(long int));
89 long int head=0;
90 /* Initial the Parent */
91 for (i = 0 ; i < V; i++)
92 {
93 parent[i]=EMPTY_PARENT;
94 dist[i]=INF_DISTANCE;
95 Q[i]=i;
96 }
97
98 dist[S]=0;
99 sort_queue_by_distance(Q,dist,head,V);
100
101 while (head < V )
102 {
103 u=Q[head];
104 if(dist[u] == INF_DISTANCE)
105 {
106 break;
107 }
108
109 for(v=0 ; v <V; v++)
110 {
111 if( adj_matrix[u][v] > 0 ) //
112 {
113 if ( is_not_explored(Q,v,head+1,V) )
114 {
115
116 if( dist[u] + adj_matrix[u][v] < dist[v])
117 {
118 dist[v]=dist[u] + adj_matrix[u][v] ;
119 parent[v]=u;
120 }
121
122 }
123
124 }
125
126 }
127
128 head++;
129 sort_queue_by_distance(Q,dist,head,V);
130 }
131
132 free(Q);
133 free(dist);
134}
135
136void
137print_all_path_from_source(long int *parent,long int source)
138{
139 int i, map_element;
140 struct map_entry *me;
141
142 struct hashtb_enumerator ee;
143 struct hashtb_enumerator *e = &ee;
144
145 hashtb_start(nlsr->map, e);
146 map_element=hashtb_n(nlsr->map);
147
148 for(i=0;i<map_element;i++)
149 {
150 me=e->data;
151 if(me->mapping != source)
152 {
153 print_path(parent,(long int)me->mapping);
154 printf("\n");
155 }
156 hashtb_next(e);
157 }
158
159 hashtb_end(e);
160
161}
162
163void
164print_path(long int *parent, long int dest)
165{
166 if (parent[dest] != EMPTY_PARENT )
167 print_path(parent,parent[dest]);
168
169 printf(" %ld",dest);
170}
171
172int
173is_not_explored(long int *Q, long int u,long int start, long int element)
174{
175 int ret=0;
176 long int i;
177 for(i=start; i< element; i++)
178 {
179 if ( Q[i] == u )
180 {
181 ret=1;
182 break;
183 }
184 }
185 return ret;
186}
187
188void
189sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element)
190{
191 long int i,j;
192 long int temp_u;
193
194 for ( i=start ; i < element ; i ++)
195 {
196 for( j=i+1; j<element; j ++)
197 {
198 if (dist[Q[j]] < dist[Q[i]])
199 {
200 temp_u=Q[j];
201 Q[j]=Q[i];
202 Q[i]=temp_u;
203 }
204 }
205 }
206
207}
208
209void
210print_map(void)
211{
212 int i, map_element;
213 struct map_entry *me;
214
215 struct hashtb_enumerator ee;
216 struct hashtb_enumerator *e = &ee;
217
218 hashtb_start(nlsr->map, e);
219 map_element=hashtb_n(nlsr->map);
220
221 for(i=0;i<map_element;i++)
222 {
223 me=e->data;
224 printf("Router: %s Mapping Number: %d \n",me->router,me->mapping);
225 hashtb_next(e);
226 }
227
228 hashtb_end(e);
229}
230
231
232void
233assign_mapping_number(void)
234{
235 int i, map_element;
236 struct map_entry *me;
237
238 struct hashtb_enumerator ee;
239 struct hashtb_enumerator *e = &ee;
240
241 hashtb_start(nlsr->map, e);
242 map_element=hashtb_n(nlsr->map);
243
244 for(i=0;i<map_element;i++)
245 {
246 me=e->data;
247 me->mapping=i;
248 hashtb_next(e);
249 }
250
251 hashtb_end(e);
252}
253
254void
255make_map(void)
256{
257
258
259 int i, adj_lsdb_element;
260 struct alsa *adj_lsa;
261
262 struct hashtb_enumerator ee;
263 struct hashtb_enumerator *e = &ee;
264
265 hashtb_start(nlsr->lsdb->adj_lsdb, e);
266 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
267
268 for(i=0;i<adj_lsdb_element;i++)
269 {
270 adj_lsa=e->data;
271 add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link);
272 hashtb_next(e);
273 }
274
275 hashtb_end(e);
276
277}
278
279void
280add_map_entry(char *router)
281{
282
283 struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry ));
284
285 struct hashtb_enumerator ee;
286 struct hashtb_enumerator *e = &ee;
287 int res;
288
289 hashtb_start(nlsr->map, e);
290 res = hashtb_seek(e, router, strlen(router), 0);
291
292 if(res == HT_NEW_ENTRY)
293 {
294 me=e->data;
295 me->router=(char *)malloc(strlen(router)+1);
296 memset(me->router,0,strlen(router)+1);
297 memcpy(me->router,router,strlen(router));
298 me->mapping=0;
299 }
300
301 hashtb_end(e);
302
303}
304
305
306void
307add_adj_data_to_map(char *orig_router, char *body, int no_link)
308{
309 add_map_entry(orig_router);
310
311 int i=0;
312 char *lsa_data=(char *)malloc(strlen(body)+1);
313 memset( lsa_data,0,strlen(body)+1);
314 memcpy(lsa_data,body,strlen(body)+1);
315 char *sep="|";
316 char *rem;
317 char *rtr_id;
318 char *length;
319 char *face;
320 char *metric;
321
322 if(no_link >0 )
323 {
324 rtr_id=strtok_r(lsa_data,sep,&rem);
325 length=strtok_r(NULL,sep,&rem);
326 face=strtok_r(NULL,sep,&rem);
327 metric=strtok_r(NULL,sep,&rem);
328
329 add_map_entry(rtr_id);
330
331 for(i=1;i<no_link;i++)
332 {
333 rtr_id=strtok_r(NULL,sep,&rem);
334 length=strtok_r(NULL,sep,&rem);
335 face=strtok_r(NULL,sep,&rem);
336 metric=strtok_r(NULL,sep,&rem);
337
338 add_map_entry(rtr_id);
339
340 }
341 }
342
343 free(lsa_data);
344}
345
346int
347get_mapping_no(char *router)
348{
349 struct map_entry *me;
350
351 struct hashtb_enumerator ee;
352 struct hashtb_enumerator *e = &ee;
akmhoque0315f982012-09-09 11:28:05 -0500353 int res;
354 int ret=-1;
akmhoque29c1db52012-09-07 14:47:43 -0500355
356 hashtb_start(nlsr->map, e);
357 res = hashtb_seek(e, router, strlen(router), 0);
358
359 if( res == HT_OLD_ENTRY )
360 {
361 me=e->data;
362 ret=me->mapping;
363 }
364 else if(res == HT_NEW_ENTRY)
365 {
366 hashtb_delete(e);
367 }
368
369 hashtb_end(e);
370
371 return ret;
372
373}
374
375void
376assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
377{
378 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
379 int mapping_nbr_router;
380
381 int i;
382 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
383 memset( lsa_data,0,strlen(adj_lsa->body)+1);
384 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
385 char *sep="|";
386 char *rem;
387 char *rtr_id;
388 char *length;
389 char *face;
390 char *metric;
391
392 if(adj_lsa->no_link >0 )
393 {
394 rtr_id=strtok_r(lsa_data,sep,&rem);
395 length=strtok_r(NULL,sep,&rem);
396 face=strtok_r(NULL,sep,&rem);
397 metric=strtok_r(NULL,sep,&rem);
398
399 mapping_nbr_router=get_mapping_no(rtr_id);
400 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
401
402 for(i=1;i<adj_lsa->no_link;i++)
403 {
404 rtr_id=strtok_r(NULL,sep,&rem);
405 length=strtok_r(NULL,sep,&rem);
406 face=strtok_r(NULL,sep,&rem);
407 metric=strtok_r(NULL,sep,&rem);
408
409 mapping_nbr_router=get_mapping_no(rtr_id);
410 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
411
412 }
413 }
414
415 free(lsa_data);
416}
417
418void
419make_adj_matrix(int **adj_matrix,int map_element)
420{
421
422 init_adj_matrix(adj_matrix,map_element);
423
424 int i, adj_lsdb_element;
425 struct alsa *adj_lsa;
426
427 struct hashtb_enumerator ee;
428 struct hashtb_enumerator *e = &ee;
429
430 hashtb_start(nlsr->lsdb->adj_lsdb, e);
431 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
432
433 for(i=0;i<adj_lsdb_element;i++)
434 {
435 adj_lsa=e->data;
436 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
437 hashtb_next(e);
438 }
439
440 hashtb_end(e);
441
442}
443
444void
445init_adj_matrix(int **adj_matrix,int map_element)
446{
447 int i, j;
448 for(i=0;i<map_element;i++)
449 for(j=0;j<map_element;j++)
450 adj_matrix[i][j]=0;
451}
452
453void print_adj_matrix(int **adj_matrix, int map_element)
454{
455 int i, j;
456 for(i=0;i<map_element;i++)
457 {
458 for(j=0;j<map_element;j++)
459 printf("%d ",adj_matrix[i][j]);
460 printf("\n");
461 }
462}
463
akmhoque3560cb62012-09-09 10:52:30 -0500464int
465get_next_hop(char *dest_router)
466{
467 struct routing_table_entry *rte;
468
469 struct hashtb_enumerator ee;
470 struct hashtb_enumerator *e = &ee;
471 int res,ret;
472
473 hashtb_start(nlsr->routing_table, e);
474 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
475
476 if( res == HT_OLD_ENTRY )
477 {
478 rte=e->data;
479 ret=rte->next_hop_face;
480 }
481 else if(res == HT_NEW_ENTRY)
482 {
483 hashtb_delete(e);
484 ret=NO_NEXT_HOP;
485 }
486
487 hashtb_end(e);
488
489 return ret;
490}
491
492void
493add_next_hop_router(char *dest_router)
494{
495 if ( strcmp(dest_router,nlsr->router_name)== 0)
496 {
497 return ;
498 }
499
500 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
501
502 struct hashtb_enumerator ee;
503 struct hashtb_enumerator *e = &ee;
504 int res;
505
506 hashtb_start(nlsr->routing_table, e);
507 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
508
509 if( res == HT_NEW_ENTRY )
510 {
511 rte=e->data;
512 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
513 memset(rte->dest_router,0,strlen(dest_router)+1);
514 memcpy(rte->dest_router,dest_router,strlen(dest_router));
515 rte->next_hop_face=NO_NEXT_HOP;
516 }
517 hashtb_end(e);
518
519}
520
521void
522add_next_hop_from_lsa_adj_body(char *body, int no_link)
523{
524
525 int i=0;
526 char *lsa_data=(char *)malloc(strlen(body)+1);
527 memset( lsa_data,0,strlen(body)+1);
528 memcpy(lsa_data,body,strlen(body)+1);
529 char *sep="|";
530 char *rem;
531 char *rtr_id;
532 char *length;
533 char *face;
534 char *metric;
535
536 if(no_link >0 )
537 {
538 rtr_id=strtok_r(lsa_data,sep,&rem);
539 length=strtok_r(NULL,sep,&rem);
540 face=strtok_r(NULL,sep,&rem);
541 metric=strtok_r(NULL,sep,&rem);
542
akmhoque3560cb62012-09-09 10:52:30 -0500543
544 add_next_hop_router(rtr_id);
545
546 for(i=1;i<no_link;i++)
547 {
548 rtr_id=strtok_r(NULL,sep,&rem);
549 length=strtok_r(NULL,sep,&rem);
550 face=strtok_r(NULL,sep,&rem);
551 metric=strtok_r(NULL,sep,&rem);
akmhoque3560cb62012-09-09 10:52:30 -0500552
553 add_next_hop_router(rtr_id);
554
555 }
556 }
557
558 free(lsa_data);
559
560
561}
562
563void
564print_routing_table(void)
565{
566 printf("\n");
567 printf("print_routing_table called\n");
568 int i, rt_element;
569
570 struct routing_table_entry *rte;
571
572 struct hashtb_enumerator ee;
573 struct hashtb_enumerator *e = &ee;
574
575 hashtb_start(nlsr->routing_table, e);
576 rt_element=hashtb_n(nlsr->routing_table);
577
578 for(i=0;i<rt_element;i++)
579 {
580 printf("----------Routing Table Entry %d------------------\n",i+1);
581 rte=e->data;
582 printf(" Destination Router: %s \n",rte->dest_router);
583 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);
584 hashtb_next(e);
585 }
586
587 hashtb_end(e);
588
589 printf("\n");
590}
591
592void
593do_old_routing_table_updates()
594{
akmhoque0315f982012-09-09 11:28:05 -0500595
akmhoque3560cb62012-09-09 10:52:30 -0500596}
akmhoque29c1db52012-09-07 14:47:43 -0500597