blob: 591a93a894b412a7e050d16a7c0f3b68c6f2f57d [file] [log] [blame]
akmhoque59980a52012-08-09 12:36:09 -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>
akmhoquebfefef22012-09-26 10:09:34 -05007#include <sys/stat.h>
akmhoque59980a52012-08-09 12:36:09 -05008#include <assert.h>
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
akmhoque03004e62012-09-06 01:12:28 -050012#include <sys/types.h>
13#include <signal.h>
14
akmhoque59980a52012-08-09 12:36:09 -050015
16
akmhoquebfefef22012-09-26 10:09:34 -050017
akmhoque59980a52012-08-09 12:36:09 -050018#include <ccn/ccn.h>
19#include <ccn/uri.h>
20#include <ccn/keystore.h>
21#include <ccn/signing.h>
22#include <ccn/schedule.h>
23#include <ccn/hashtb.h>
24
25#include "nlsr.h"
26#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050027#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050028#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050029#include "nlsr_npl.h"
30#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050031#include "nlsr_npt.h"
32#include "nlsr_route.h"
33
34
akmhoque81c25e02012-09-10 14:50:33 -050035#define ON_ERROR_DESTROY(resval) \
36{ \
37 if ((resval) < 0) { \
38 nlsr_destroy(); \
akmhoqueffacaa82012-09-13 17:48:30 -050039 exit(1);\
akmhoque81c25e02012-09-10 14:50:33 -050040 } \
41}
42
43
44#define ON_ERROR_EXIT(resval) \
45{ \
46 if ((resval) < 0) { \
akmhoqueffacaa82012-09-13 17:48:30 -050047 exit(1); \
akmhoque81c25e02012-09-10 14:50:33 -050048 } \
49}
akmhoque59980a52012-08-09 12:36:09 -050050
51struct option longopts[] =
52{
53 { "daemon", no_argument, NULL, 'd'},
54 { "config_file", required_argument, NULL, 'f'},
55 { "help", no_argument, NULL, 'h'},
56 { 0 }
57};
58
59static int
60usage(char *progname)
61{
62
63 printf("Usage: %s [OPTIONS...]\n\
64 NDN routing....\n\
65 -d, --daemon Run in daemon mode\n\
66 -f, --config_file Specify configuration file name\n\
67 -h, --help Display this help message\n", progname);
68
69 exit(1);
70}
71
72void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
73{
74 struct timeval now = {0};
75 gettimeofday(&now, 0);
76 result->s = now.tv_sec;
77 result->micros = now.tv_usec;
78}
79
80static struct ccn_gettime ndn_rtr_ticker = {
81 "timer",
82 &ndn_rtr_gettime,
83 1000000,
84 NULL
85};
86
akmhoqueffacaa82012-09-13 17:48:30 -050087void
88nlsr_lock(void)
89{
90 nlsr->semaphor=NLSR_LOCKED;
91}
92
93void
94nlsr_unlock(void)
95{
96 nlsr->semaphor=NLSR_UNLOCKED;
97}
akmhoque42098b12012-08-27 22:54:23 -050098
99void
akmhoque03004e62012-09-06 01:12:28 -0500100nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -0500101{
akmhoque03004e62012-09-06 01:12:28 -0500102 signal(sig, SIG_IGN);
akmhoqueffacaa82012-09-13 17:48:30 -0500103 nlsr_destroy();
104 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500105}
106
akmhoquebfefef22012-09-26 10:09:34 -0500107void
108daemonize_nlsr(void)
109{
110 int ret;
111 pid_t process_id = 0;
112 pid_t sid = 0;
113 process_id = fork();
114 if (process_id < 0)
115 {
116 printf("fork failed!\n");
117 ON_ERROR_DESTROY(process_id);
118 }
119 if (process_id > 0)
120 {
121 printf("Process daemonized. Process id: %d \n", process_id);
122 ret=process_id;
123 exit(0);
124 }
125
126 umask(0);
127 sid = setsid();
128 if(sid < 0)
129 {
130 ON_ERROR_DESTROY(sid);
131 }
132
133 chdir("/");
134 close(STDIN_FILENO);
135 close(STDOUT_FILENO);
136 close(STDERR_FILENO);
137}
138
akmhoque59980a52012-08-09 12:36:09 -0500139void
140process_command_ccnneighbor(char *command)
141{
142 if(command==NULL)
143 {
akmhoque28c45022012-08-09 15:38:02 -0500144 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500145 return;
146 }
147 char *rem;
148 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500149 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500150
akmhoque28c45022012-08-09 15:38:02 -0500151 rtr_name=strtok_r(command,sep,&rem);
152 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500153 {
akmhoque28c45022012-08-09 15:38:02 -0500154 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500155 return;
156 }
157
158 face=strtok_r(NULL,sep,&rem);
159 if(face==NULL)
160 {
akmhoque28c45022012-08-09 15:38:02 -0500161 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500162 return;
163 }
164
akmhoque28c45022012-08-09 15:38:02 -0500165 printf("Router: %s face: %s\n",rtr_name,face);
166 int face_id;
167 int res;
168 res=sscanf(face,"face%d",&face_id);
169
170 if(res != 1 )
171 {
172 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
173 return;
174 }
175
akmhoqued5152122012-09-19 06:44:23 -0500176 if ( rtr_name[strlen(rtr_name)-1] == '/' )
177 {
178 rtr_name[strlen(rtr_name)-1]='\0';
179 }
akmhoque03004e62012-09-06 01:12:28 -0500180 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
181 nbr->name=(char *)malloc(strlen(rtr_name)+1);
182 memset(nbr->name,0,strlen(rtr_name)+1);
183 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
184 nbr->length=strlen(rtr_name)+1;
185
186 add_nbr_to_adl(nbr,face_id);
187
188 free(nbr->name);
189 free(nbr);
190}
191
192void
193process_command_ccnname(char *command)
194{
195
196 if(command==NULL)
197 {
198 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
199 return;
200 }
201 char *rem;
202 const char *sep=" \t\n";
203 char *name;
204 name=strtok_r(command,sep,&rem);
205 if(name==NULL)
206 {
207 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
208 return;
209 }
210
211 printf("Name Prefix: %s \n",name);
212
akmhoqued5152122012-09-19 06:44:23 -0500213 if ( name[strlen(name)-1] == '/' )
214 name[strlen(name)-1]='\0';
215
akmhoque53f64222012-09-05 13:57:51 -0500216 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500217 np->name=(char *)malloc(strlen(name)+1);
218 memset(np->name,0,strlen(name)+1);
219 memcpy(np->name,name,strlen(name)+1);
220 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500221
akmhoque03004e62012-09-06 01:12:28 -0500222 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500223
akmhoque03004e62012-09-06 01:12:28 -0500224 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500225 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500226}
227
akmhoque03004e62012-09-06 01:12:28 -0500228
229void
230process_command_router_name(char *command)
231{
232 if(command==NULL)
233 {
234 printf(" Wrong Command Format ( router-name /router/name )\n");
235 return;
236 }
237 char *rem;
238 const char *sep=" \t\n";
239 char *rtr_name;
240
241 rtr_name=strtok_r(command,sep,&rem);
242 if(rtr_name==NULL)
243 {
244 printf(" Wrong Command Format ( router-name /router/name )\n");
245 return;
246 }
247
248
akmhoqued5152122012-09-19 06:44:23 -0500249 if ( rtr_name[strlen(rtr_name)-1] == '/' )
250 rtr_name[strlen(rtr_name)-1]='\0';
251
akmhoque03004e62012-09-06 01:12:28 -0500252 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
253 memset(nlsr->router_name,0,strlen(rtr_name)+1);
254 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
255
256
257}
258
akmhoque59980a52012-08-09 12:36:09 -0500259void
akmhoqued79438d2012-08-27 13:31:42 -0500260process_command_lsdb_synch_interval(char *command)
261{
262 if(command==NULL)
263 {
264 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
265 return;
266 }
267 char *rem;
268 const char *sep=" \t\n";
269 char *secs;
270 long int seconds;
271
272 secs=strtok_r(command,sep,&rem);
273 if(secs==NULL)
274 {
275 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
276 return;
277 }
278
279 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500280 if ( seconds >= 120 && seconds <= 3600 )
281 {
282 nlsr->lsdb_synch_interval=seconds;
283 }
akmhoqued79438d2012-08-27 13:31:42 -0500284
285}
286
287
288void
289process_command_interest_retry(char *command)
290{
291 if(command==NULL)
292 {
293 printf(" Wrong Command Format ( interest-retry number )\n");
294 return;
295 }
296 char *rem;
297 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500298 char *retry;
299 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500300
akmhoqueffacaa82012-09-13 17:48:30 -0500301 retry=strtok_r(command,sep,&rem);
302 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500303 {
304 printf(" Wrong Command Format ( interest-retry number)\n");
305 return;
306 }
307
akmhoqueffacaa82012-09-13 17:48:30 -0500308 retry_number=atoi(retry);
309 if ( retry_number >= 1 && retry_number<=10 )
310 {
311 nlsr->interest_retry=retry_number;
312 }
akmhoqued79438d2012-08-27 13:31:42 -0500313
314}
315
316void
317process_command_interest_resend_time(char *command)
318{
319 if(command==NULL)
320 {
321 printf(" Wrong Command Format ( interest-resend-time secs )\n");
322 return;
323 }
324 char *rem;
325 const char *sep=" \t\n";
326 char *secs;
327 long int seconds;
328
329 secs=strtok_r(command,sep,&rem);
330 if(secs==NULL)
331 {
332 printf(" Wrong Command Format ( interest-resend-time secs)\n");
333 return;
334 }
335
336 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500337 if ( seconds <= 60 && seconds >= 1 )
338 {
339 nlsr->interest_resend_time=seconds;
340 }
akmhoqued79438d2012-08-27 13:31:42 -0500341}
342
akmhoque03004e62012-09-06 01:12:28 -0500343
akmhoqued5152122012-09-19 06:44:23 -0500344void
345process_command_lsa_refresh_time(char *command)
346{
347 if(command==NULL)
348 {
349 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
350 return;
351 }
352 char *rem;
353 const char *sep=" \t\n";
354 char *secs;
355 long int seconds;
356
357 secs=strtok_r(command,sep,&rem);
358 if(secs==NULL)
359 {
360 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
361 return;
362 }
363
364 seconds=atoi(secs);
365 if ( seconds >= 240 && seconds <= 3600 )
366 {
367 nlsr->lsa_refresh_time=seconds;
368 }
369
370}
371
372void
373process_command_router_dead_interval(char *command)
374{
375 if(command==NULL)
376 {
377 printf(" Wrong Command Format ( router-dead-interval secs )\n");
378 return;
379 }
380 char *rem;
381 const char *sep=" \t\n";
382 char *secs;
383 long int seconds;
384
385 secs=strtok_r(command,sep,&rem);
386 if(secs==NULL)
387 {
388 printf(" Wrong Command Format ( router-dead-interval secs)\n");
389 return;
390 }
391
392 seconds=atoi(secs);
393 if ( seconds >= 360 && seconds <= 5400 )
394 {
395 nlsr->router_dead_interval=seconds;
396 }
397
398}
akmhoque03004e62012-09-06 01:12:28 -0500399
akmhoqued79438d2012-08-27 13:31:42 -0500400void
akmhoque3cced642012-09-24 16:20:20 -0500401process_command_multi_path_face_num(char *command)
402{
403 if(command==NULL)
404 {
405 printf(" Wrong Command Format ( multi-path-face-num n )\n");
406 return;
407 }
408 char *rem;
409 const char *sep=" \t\n";
410 char *num;
411 long int number;
412
413 num=strtok_r(command,sep,&rem);
414 if(num==NULL)
415 {
416 printf(" Wrong Command Format ( multi-path-face-num n)\n");
417 return;
418 }
419
420 number=atoi(num);
421 if ( number >= 0 && number <= 60 )
422 {
423 nlsr->multi_path_face_num=number;
424 }
425
426}
427
428void
akmhoquebfefef22012-09-26 10:09:34 -0500429process_command_logdir(char *command)
430{
431 if(command==NULL)
432 {
433 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
434 return;
435 }
436 char *rem;
437 const char *sep=" \t\n";
438 char *dir;
439
440 dir=strtok_r(command,sep,&rem);
441 if(dir==NULL)
442 {
443 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
444 return;
445 }
446
447 nlsr->logDir=(char *)malloc(strlen(dir)+1);
448 memset(nlsr->logDir,0,strlen(dir)+1);
449 memcpy(nlsr->logDir,dir,strlen(dir));
450}
451
452void
akmhoque59980a52012-08-09 12:36:09 -0500453process_conf_command(char *command)
454{
455 const char *separators=" \t\n";
456 char *remainder=NULL;
457 char *cmd_type=NULL;
458
459 if(command==NULL || strlen(command)==0 || command[0]=='!')
460 return;
461
462 cmd_type=strtok_r(command,separators,&remainder);
463
464 if(!strcmp(cmd_type,"router-name") )
465 {
466 process_command_router_name(remainder);
467 }
468 else if(!strcmp(cmd_type,"ccnneighbor") )
469 {
470 process_command_ccnneighbor(remainder);
471 }
472 else if(!strcmp(cmd_type,"ccnname") )
473 {
474 process_command_ccnname(remainder);
475 }
akmhoqued79438d2012-08-27 13:31:42 -0500476 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
477 {
478 process_command_lsdb_synch_interval(remainder);
479 }
480 else if(!strcmp(cmd_type,"interest-retry") )
481 {
482 process_command_interest_retry(remainder);
483 }
484 else if(!strcmp(cmd_type,"interest-resend-time") )
485 {
486 process_command_interest_resend_time(remainder);
487 }
akmhoqued5152122012-09-19 06:44:23 -0500488 else if(!strcmp(cmd_type,"lsa-refresh-time") )
489 {
490 process_command_lsa_refresh_time(remainder);
491 }
492 else if(!strcmp(cmd_type,"router-dead-interval") )
493 {
494 process_command_router_dead_interval(remainder);
495 }
akmhoque3cced642012-09-24 16:20:20 -0500496 else if(!strcmp(cmd_type,"multi-path-face-num") )
497 {
498 process_command_multi_path_face_num(remainder);
499 }
akmhoquebfefef22012-09-26 10:09:34 -0500500 else if(!strcmp(cmd_type,"logdir") )
501 {
502 process_command_logdir(remainder);
503 }
akmhoqued5152122012-09-19 06:44:23 -0500504 else
akmhoque59980a52012-08-09 12:36:09 -0500505 {
506 printf("Wrong configuration Command %s \n",cmd_type);
507 }
508}
509
akmhoque03004e62012-09-06 01:12:28 -0500510
akmhoque59980a52012-08-09 12:36:09 -0500511int
512readConfigFile(const char *filename)
513{
514 FILE *cfg;
515 char buf[1024];
516 int len;
517
518 cfg=fopen(filename, "r");
519
520 if(cfg == NULL)
521 {
522 printf("\nConfiguration File does not exists\n");
523 exit(1);
524 }
525
526 while(fgets((char *)buf, sizeof(buf), cfg))
527 {
528 len=strlen(buf);
529 if(buf[len-1] == '\n')
530 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500531 if ( buf[0] != '#' && buf[0] != '!')
532 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500533 }
534
535 fclose(cfg);
536
537 return 0;
538}
539
akmhoque386081b2012-08-10 10:53:21 -0500540void
541nlsr_destroy( void )
542{
543
akmhoque53f64222012-09-05 13:57:51 -0500544 printf("Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500545 /* Destroying all face created by nlsr in CCND */
546 destroy_all_face_by_nlsr();
547
akmhoque386081b2012-08-10 10:53:21 -0500548 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500549 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500550 hashtb_destroy(&nlsr->adl);
551 hashtb_destroy(&nlsr->lsdb->name_lsdb);
552 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500553 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500554
555 //To Do: has to destroy the face_list one by one
556
akmhoque3560cb62012-09-09 10:52:30 -0500557 hashtb_destroy(&nlsr->routing_table);
558
559
560 int i, npt_element;
561 struct npt_entry *ne;
562 struct hashtb_enumerator ee;
563 struct hashtb_enumerator *e = &ee;
564 hashtb_start(nlsr->npt, e);
565 npt_element=hashtb_n(nlsr->npt);
566 for(i=0;i<npt_element;i++)
567 {
568 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500569 hashtb_destroy(&ne->name_list);
570 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500571 hashtb_next(e);
572 }
573
574 hashtb_end(e);
575 hashtb_destroy(&nlsr->npt);
576
akmhoque03004e62012-09-06 01:12:28 -0500577
akmhoque386081b2012-08-10 10:53:21 -0500578 ccn_schedule_destroy(&nlsr->sched);
579 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500580
581 free(nlsr->lsdb->lsdb_version);
582 free(nlsr->lsdb);
583 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500584 free(nlsr);
585
akmhoque53f64222012-09-05 13:57:51 -0500586 printf("Finished freeing allocated memory\n");
587
akmhoque386081b2012-08-10 10:53:21 -0500588}
589
akmhoque03004e62012-09-06 01:12:28 -0500590
akmhoque81c25e02012-09-10 14:50:33 -0500591int
akmhoque902d57e2012-08-17 09:24:38 -0500592init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500593{
akmhoque03004e62012-09-06 01:12:28 -0500594 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
595 {
596 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500597 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500598 }
599 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
600 {
601 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500602 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500603 }
604 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
605 {
606 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500607 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500608 }
akmhoque902d57e2012-08-17 09:24:38 -0500609
akmhoque59980a52012-08-09 12:36:09 -0500610 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500611
612 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500613 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500614 struct hashtb_param param_npl = {0};
615 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500616 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500617 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500618 struct hashtb_param param_npt = {0};
619 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
620 struct hashtb_param param_rte = {0};
621 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500622
akmhoque59980a52012-08-09 12:36:09 -0500623 nlsr->in_interest.p = &incoming_interest;
624 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500625
akmhoque03004e62012-09-06 01:12:28 -0500626 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500627
akmhoque03004e62012-09-06 01:12:28 -0500628 char *time_stamp=(char *)malloc(20);
629 memset(time_stamp,0,20);
630 get_current_timestamp_micro(time_stamp);
631 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
632 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
633 free(time_stamp);
634
635 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400636 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500637 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400638 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500639
640
akmhoque902d57e2012-08-17 09:24:38 -0500641
akmhoque53f64222012-09-05 13:57:51 -0500642
akmhoque59980a52012-08-09 12:36:09 -0500643 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500644 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500645 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500646 nlsr->adj_build_count=0;
647 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500648 nlsr->is_send_lsdb_interest_scheduled=0;
649 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500650
651 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
652 nlsr->interest_retry = INTEREST_RETRY;
653 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500654 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
655 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500656 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueda5b6832012-09-13 22:33:55 -0500657
akmhoque42098b12012-08-27 22:54:23 -0500658
akmhoqueffacaa82012-09-13 17:48:30 -0500659 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500660
661 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500662}
663
akmhoque03004e62012-09-06 01:12:28 -0500664
akmhoque902d57e2012-08-17 09:24:38 -0500665int
666main(int argc, char *argv[])
667{
akmhoque81c25e02012-09-10 14:50:33 -0500668 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500669 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500670 int daemon_mode=0;
akmhoque902d57e2012-08-17 09:24:38 -0500671
akmhoquebfefef22012-09-26 10:09:34 -0500672
akmhoque81c25e02012-09-10 14:50:33 -0500673
akmhoque59980a52012-08-09 12:36:09 -0500674 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
675 {
676 switch (res)
677 {
678 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500679 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500680 break;
681 case 'f':
682 config_file = optarg;
683 break;
684 case 'h':
685 default:
686 usage(argv[0]);
687 }
688 }
689
akmhoquebfefef22012-09-26 10:09:34 -0500690 ret=init_nlsr();
691 ON_ERROR_EXIT(ret);
akmhoque59980a52012-08-09 12:36:09 -0500692 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500693
694 if ( daemon_mode == 1 )
695 {
696 daemonize_nlsr();
697 }
698
699 startLogging(nlsr->logDir);
700
akmhoque59980a52012-08-09 12:36:09 -0500701 nlsr->ccn=ccn_create();
702 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500703 {
704 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -0500705 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -0500706 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -0500707 }
akmhoque53f64222012-09-05 13:57:51 -0500708 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500709 router_prefix=ccn_charbuf_create();
710 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500711 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500712 {
713 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -0500714 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -0500715 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500716 }
akmhoque59980a52012-08-09 12:36:09 -0500717
718 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500719 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500720 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
721 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500722 {
723 fprintf(stderr,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -0500724 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500725 }
726 ccn_charbuf_destroy(&router_prefix);
727
728 printf("Router Name : %s\n",nlsr->router_name);
729 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500730
akmhoque53f64222012-09-05 13:57:51 -0500731 print_name_prefix_from_npl();
732 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500733 build_and_install_name_lsas();
734 print_name_lsdb();
735
736 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500737 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500738 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500739
akmhoque59980a52012-08-09 12:36:09 -0500740 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500741 {
akmhoqueffacaa82012-09-13 17:48:30 -0500742 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -0500743 {
akmhoqueffacaa82012-09-13 17:48:30 -0500744 if( nlsr->sched != NULL )
745 {
746 ccn_schedule_run(nlsr->sched);
747 }
748 if(nlsr->ccn != NULL)
749 {
750 res = ccn_run(nlsr->ccn, 500);
751 }
752 if (!(nlsr->sched && nlsr->ccn))
753 {
754 break;
755 }
akmhoque29c1db52012-09-07 14:47:43 -0500756 }
757
akmhoque59980a52012-08-09 12:36:09 -0500758 }
akmhoque59980a52012-08-09 12:36:09 -0500759
akmhoque59980a52012-08-09 12:36:09 -0500760 return 0;
761}
762