blob: 61bf3dccb56b8a6408c16c4c9e26dcab460feaba [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;
353 int res,ret;
354
355 hashtb_start(nlsr->map, e);
356 res = hashtb_seek(e, router, strlen(router), 0);
357
358 if( res == HT_OLD_ENTRY )
359 {
360 me=e->data;
361 ret=me->mapping;
362 }
363 else if(res == HT_NEW_ENTRY)
364 {
365 hashtb_delete(e);
366 }
367
368 hashtb_end(e);
369
370 return ret;
371
372}
373
374void
375assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix)
376{
377 int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name);
378 int mapping_nbr_router;
379
380 int i;
381 char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1);
382 memset( lsa_data,0,strlen(adj_lsa->body)+1);
383 memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1);
384 char *sep="|";
385 char *rem;
386 char *rtr_id;
387 char *length;
388 char *face;
389 char *metric;
390
391 if(adj_lsa->no_link >0 )
392 {
393 rtr_id=strtok_r(lsa_data,sep,&rem);
394 length=strtok_r(NULL,sep,&rem);
395 face=strtok_r(NULL,sep,&rem);
396 metric=strtok_r(NULL,sep,&rem);
397
398 mapping_nbr_router=get_mapping_no(rtr_id);
399 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
400
401 for(i=1;i<adj_lsa->no_link;i++)
402 {
403 rtr_id=strtok_r(NULL,sep,&rem);
404 length=strtok_r(NULL,sep,&rem);
405 face=strtok_r(NULL,sep,&rem);
406 metric=strtok_r(NULL,sep,&rem);
407
408 mapping_nbr_router=get_mapping_no(rtr_id);
409 adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric);
410
411 }
412 }
413
414 free(lsa_data);
415}
416
417void
418make_adj_matrix(int **adj_matrix,int map_element)
419{
420
421 init_adj_matrix(adj_matrix,map_element);
422
423 int i, adj_lsdb_element;
424 struct alsa *adj_lsa;
425
426 struct hashtb_enumerator ee;
427 struct hashtb_enumerator *e = &ee;
428
429 hashtb_start(nlsr->lsdb->adj_lsdb, e);
430 adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb);
431
432 for(i=0;i<adj_lsdb_element;i++)
433 {
434 adj_lsa=e->data;
435 assign_adj_matrix_for_lsa(adj_lsa,adj_matrix);
436 hashtb_next(e);
437 }
438
439 hashtb_end(e);
440
441}
442
443void
444init_adj_matrix(int **adj_matrix,int map_element)
445{
446 int i, j;
447 for(i=0;i<map_element;i++)
448 for(j=0;j<map_element;j++)
449 adj_matrix[i][j]=0;
450}
451
452void print_adj_matrix(int **adj_matrix, int map_element)
453{
454 int i, j;
455 for(i=0;i<map_element;i++)
456 {
457 for(j=0;j<map_element;j++)
458 printf("%d ",adj_matrix[i][j]);
459 printf("\n");
460 }
461}
462
akmhoque3560cb62012-09-09 10:52:30 -0500463int
464get_next_hop(char *dest_router)
465{
466 struct routing_table_entry *rte;
467
468 struct hashtb_enumerator ee;
469 struct hashtb_enumerator *e = &ee;
470 int res,ret;
471
472 hashtb_start(nlsr->routing_table, e);
473 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
474
475 if( res == HT_OLD_ENTRY )
476 {
477 rte=e->data;
478 ret=rte->next_hop_face;
479 }
480 else if(res == HT_NEW_ENTRY)
481 {
482 hashtb_delete(e);
483 ret=NO_NEXT_HOP;
484 }
485
486 hashtb_end(e);
487
488 return ret;
489}
490
491void
492add_next_hop_router(char *dest_router)
493{
494 if ( strcmp(dest_router,nlsr->router_name)== 0)
495 {
496 return ;
497 }
498
499 struct routing_table_entry *rte=(struct routing_table_entry *)malloc(sizeof(struct routing_table_entry));
500
501 struct hashtb_enumerator ee;
502 struct hashtb_enumerator *e = &ee;
503 int res;
504
505 hashtb_start(nlsr->routing_table, e);
506 res = hashtb_seek(e, dest_router, strlen(dest_router), 0);
507
508 if( res == HT_NEW_ENTRY )
509 {
510 rte=e->data;
511 rte->dest_router=(char *)malloc(strlen(dest_router)+1);
512 memset(rte->dest_router,0,strlen(dest_router)+1);
513 memcpy(rte->dest_router,dest_router,strlen(dest_router));
514 rte->next_hop_face=NO_NEXT_HOP;
515 }
516 hashtb_end(e);
517
518}
519
520void
521add_next_hop_from_lsa_adj_body(char *body, int no_link)
522{
523
524 int i=0;
525 char *lsa_data=(char *)malloc(strlen(body)+1);
526 memset( lsa_data,0,strlen(body)+1);
527 memcpy(lsa_data,body,strlen(body)+1);
528 char *sep="|";
529 char *rem;
530 char *rtr_id;
531 char *length;
532 char *face;
533 char *metric;
534
535 if(no_link >0 )
536 {
537 rtr_id=strtok_r(lsa_data,sep,&rem);
538 length=strtok_r(NULL,sep,&rem);
539 face=strtok_r(NULL,sep,&rem);
540 metric=strtok_r(NULL,sep,&rem);
541
542 //printf(" Link %d \n",i+1);
543 //printf(" Neighbor : %s \n",rtr_id);
544 //printf(" Neighbor Length : %s \n",length);
545 //printf(" Connecting Face : %s \n",face);
546 //printf(" Metric : %s \n",metric);
547
548 add_next_hop_router(rtr_id);
549
550 for(i=1;i<no_link;i++)
551 {
552 rtr_id=strtok_r(NULL,sep,&rem);
553 length=strtok_r(NULL,sep,&rem);
554 face=strtok_r(NULL,sep,&rem);
555 metric=strtok_r(NULL,sep,&rem);
556 //printf(" Link %d \n",i+1);
557 //printf(" Neighbor : %s \n",rtr_id);
558 //printf(" Neighbor Length : %s \n",length);
559 //printf(" Connecting Face : %s \n",face);
560 //printf(" Metric : %s \n",metric);
561
562 add_next_hop_router(rtr_id);
563
564 }
565 }
566
567 free(lsa_data);
568
569
570}
571
572void
573print_routing_table(void)
574{
575 printf("\n");
576 printf("print_routing_table called\n");
577 int i, rt_element;
578
579 struct routing_table_entry *rte;
580
581 struct hashtb_enumerator ee;
582 struct hashtb_enumerator *e = &ee;
583
584 hashtb_start(nlsr->routing_table, e);
585 rt_element=hashtb_n(nlsr->routing_table);
586
587 for(i=0;i<rt_element;i++)
588 {
589 printf("----------Routing Table Entry %d------------------\n",i+1);
590 rte=e->data;
591 printf(" Destination Router: %s \n",rte->dest_router);
592 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);
593 hashtb_next(e);
594 }
595
596 hashtb_end(e);
597
598 printf("\n");
599}
600
601void
602do_old_routing_table_updates()
603{
604
605}
akmhoque29c1db52012-09-07 14:47:43 -0500606