blob: e1a49c2817a6a4a45df81e2789025a84e882bd23 [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 {
akmhoque7b791452012-10-30 11:24:56 -0500116 printf("Daemonization failed!\n");
akmhoquebfefef22012-09-26 10:09:34 -0500117 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
akmhoque7b791452012-10-30 11:24:56 -0500453process_command_detailed_log(char *command)
454{
455 if(command==NULL)
456 {
457 printf(" Wrong Command Format ( detailed-log on/off )\n");
458 return;
459 }
460 char *rem;
461 const char *sep=" \t\n";
462 char *on_off;
463
464 on_off=strtok_r(command,sep,&rem);
465 if(on_off==NULL)
466 {
467 printf(" Wrong Command Format ( detailed-log on/off )\n");
468 return;
469 }
470
471 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
472 {
473 nlsr->detailed_logging=1;
474 }
475}
476
477void
478process_command_debug(char *command)
479{
480 if(command==NULL)
481 {
482 printf(" Wrong Command Format ( debug on/off )\n");
483 return;
484 }
485 char *rem;
486 const char *sep=" \t\n";
487 char *on_off;
488
489 on_off=strtok_r(command,sep,&rem);
490 if(on_off==NULL)
491 {
492 printf(" Wrong Command Format ( debug on/off )\n");
493 return;
494 }
495
496 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
497 {
498 nlsr->debugging=1;
499 }
500}
501
502void
akmhoque59980a52012-08-09 12:36:09 -0500503process_conf_command(char *command)
504{
505 const char *separators=" \t\n";
506 char *remainder=NULL;
507 char *cmd_type=NULL;
508
509 if(command==NULL || strlen(command)==0 || command[0]=='!')
510 return;
511
512 cmd_type=strtok_r(command,separators,&remainder);
513
514 if(!strcmp(cmd_type,"router-name") )
515 {
516 process_command_router_name(remainder);
517 }
518 else if(!strcmp(cmd_type,"ccnneighbor") )
519 {
520 process_command_ccnneighbor(remainder);
521 }
522 else if(!strcmp(cmd_type,"ccnname") )
523 {
524 process_command_ccnname(remainder);
525 }
akmhoqued79438d2012-08-27 13:31:42 -0500526 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
527 {
528 process_command_lsdb_synch_interval(remainder);
529 }
530 else if(!strcmp(cmd_type,"interest-retry") )
531 {
532 process_command_interest_retry(remainder);
533 }
534 else if(!strcmp(cmd_type,"interest-resend-time") )
535 {
536 process_command_interest_resend_time(remainder);
537 }
akmhoqued5152122012-09-19 06:44:23 -0500538 else if(!strcmp(cmd_type,"lsa-refresh-time") )
539 {
540 process_command_lsa_refresh_time(remainder);
541 }
542 else if(!strcmp(cmd_type,"router-dead-interval") )
543 {
544 process_command_router_dead_interval(remainder);
545 }
akmhoque3cced642012-09-24 16:20:20 -0500546 else if(!strcmp(cmd_type,"multi-path-face-num") )
547 {
548 process_command_multi_path_face_num(remainder);
549 }
akmhoquebfefef22012-09-26 10:09:34 -0500550 else if(!strcmp(cmd_type,"logdir") )
551 {
552 process_command_logdir(remainder);
553 }
akmhoque7b791452012-10-30 11:24:56 -0500554 else if(!strcmp(cmd_type,"detailed-log") )
555 {
556 process_command_detailed_log(remainder);
557 }
558 else if(!strcmp(cmd_type,"debug") )
559 {
560 process_command_debug(remainder);
561 }
akmhoqued5152122012-09-19 06:44:23 -0500562 else
akmhoque59980a52012-08-09 12:36:09 -0500563 {
564 printf("Wrong configuration Command %s \n",cmd_type);
565 }
566}
567
akmhoque03004e62012-09-06 01:12:28 -0500568
akmhoque59980a52012-08-09 12:36:09 -0500569int
570readConfigFile(const char *filename)
571{
572 FILE *cfg;
573 char buf[1024];
574 int len;
575
576 cfg=fopen(filename, "r");
577
578 if(cfg == NULL)
579 {
580 printf("\nConfiguration File does not exists\n");
581 exit(1);
582 }
583
584 while(fgets((char *)buf, sizeof(buf), cfg))
585 {
586 len=strlen(buf);
587 if(buf[len-1] == '\n')
588 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500589 if ( buf[0] != '#' && buf[0] != '!')
590 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500591 }
592
593 fclose(cfg);
594
595 return 0;
596}
597
akmhoque386081b2012-08-10 10:53:21 -0500598void
599nlsr_destroy( void )
600{
akmhoque7b791452012-10-30 11:24:56 -0500601 if ( nlsr->debugging )
602 {
603 printf("Freeing Allocated Memory....\n");
604 }
akmhoque9e9fc722012-09-26 14:03:25 -0500605 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500606 /* Destroying all face created by nlsr in CCND */
607 destroy_all_face_by_nlsr();
608
akmhoque386081b2012-08-10 10:53:21 -0500609 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500610 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500611 hashtb_destroy(&nlsr->adl);
612 hashtb_destroy(&nlsr->lsdb->name_lsdb);
613 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500614 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500615
616 //To Do: has to destroy the face_list one by one
617
akmhoque3560cb62012-09-09 10:52:30 -0500618 hashtb_destroy(&nlsr->routing_table);
619
620
621 int i, npt_element;
622 struct npt_entry *ne;
623 struct hashtb_enumerator ee;
624 struct hashtb_enumerator *e = &ee;
625 hashtb_start(nlsr->npt, e);
626 npt_element=hashtb_n(nlsr->npt);
627 for(i=0;i<npt_element;i++)
628 {
629 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500630 hashtb_destroy(&ne->name_list);
631 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500632 hashtb_next(e);
633 }
634
635 hashtb_end(e);
636 hashtb_destroy(&nlsr->npt);
637
akmhoque03004e62012-09-06 01:12:28 -0500638
akmhoque386081b2012-08-10 10:53:21 -0500639 ccn_schedule_destroy(&nlsr->sched);
640 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500641
642 free(nlsr->lsdb->lsdb_version);
643 free(nlsr->lsdb);
644 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500645 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -0500646 if ( nlsr->debugging )
647 {
648 printf("Finished freeing allocated memory\n");
649 }
akmhoque9e9fc722012-09-26 14:03:25 -0500650 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -0500651
akmhoque386081b2012-08-10 10:53:21 -0500652}
653
akmhoque03004e62012-09-06 01:12:28 -0500654
akmhoque81c25e02012-09-10 14:50:33 -0500655int
akmhoque902d57e2012-08-17 09:24:38 -0500656init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500657{
akmhoque03004e62012-09-06 01:12:28 -0500658 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
659 {
660 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500661 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500662 }
663 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
664 {
665 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500666 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500667 }
668 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
669 {
670 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500671 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500672 }
akmhoque902d57e2012-08-17 09:24:38 -0500673
akmhoque59980a52012-08-09 12:36:09 -0500674 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500675
676 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500677 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500678 struct hashtb_param param_npl = {0};
679 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500680 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500681 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500682 struct hashtb_param param_npt = {0};
683 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
684 struct hashtb_param param_rte = {0};
685 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500686
akmhoque59980a52012-08-09 12:36:09 -0500687 nlsr->in_interest.p = &incoming_interest;
688 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500689
akmhoque03004e62012-09-06 01:12:28 -0500690 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500691
akmhoque03004e62012-09-06 01:12:28 -0500692 char *time_stamp=(char *)malloc(20);
693 memset(time_stamp,0,20);
694 get_current_timestamp_micro(time_stamp);
695 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
696 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
697 free(time_stamp);
698
699 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400700 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500701 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400702 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500703
704
akmhoque902d57e2012-08-17 09:24:38 -0500705
akmhoque53f64222012-09-05 13:57:51 -0500706
akmhoque59980a52012-08-09 12:36:09 -0500707 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500708 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500709 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500710 nlsr->adj_build_count=0;
711 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500712 nlsr->is_send_lsdb_interest_scheduled=0;
713 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500714
akmhoque7b791452012-10-30 11:24:56 -0500715 nlsr->detailed_logging=0;
716 nlsr->debugging=0;
717
akmhoqued79438d2012-08-27 13:31:42 -0500718 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
719 nlsr->interest_retry = INTEREST_RETRY;
720 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500721 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
722 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500723 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueda5b6832012-09-13 22:33:55 -0500724
akmhoque42098b12012-08-27 22:54:23 -0500725
akmhoqueffacaa82012-09-13 17:48:30 -0500726 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500727
728 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500729}
730
akmhoque03004e62012-09-06 01:12:28 -0500731
akmhoque902d57e2012-08-17 09:24:38 -0500732int
733main(int argc, char *argv[])
734{
akmhoque81c25e02012-09-10 14:50:33 -0500735 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500736 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500737 int daemon_mode=0;
akmhoque902d57e2012-08-17 09:24:38 -0500738
akmhoquebfefef22012-09-26 10:09:34 -0500739
akmhoque81c25e02012-09-10 14:50:33 -0500740
akmhoque59980a52012-08-09 12:36:09 -0500741 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
742 {
743 switch (res)
744 {
745 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500746 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500747 break;
748 case 'f':
749 config_file = optarg;
750 break;
751 case 'h':
752 default:
753 usage(argv[0]);
754 }
755 }
756
akmhoquebfefef22012-09-26 10:09:34 -0500757 ret=init_nlsr();
758 ON_ERROR_EXIT(ret);
akmhoque59980a52012-08-09 12:36:09 -0500759 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500760
761 if ( daemon_mode == 1 )
762 {
763 daemonize_nlsr();
764 }
765
766 startLogging(nlsr->logDir);
767
akmhoque59980a52012-08-09 12:36:09 -0500768 nlsr->ccn=ccn_create();
769 if(ccn_connect(nlsr->ccn, NULL) == -1)
akmhoque03004e62012-09-06 01:12:28 -0500770 {
771 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -0500772 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -0500773 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -0500774 }
akmhoque53f64222012-09-05 13:57:51 -0500775 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500776 router_prefix=ccn_charbuf_create();
777 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500778 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500779 {
780 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -0500781 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -0500782 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500783 }
akmhoque59980a52012-08-09 12:36:09 -0500784
785 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500786 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500787 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
788 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500789 {
790 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -0500791 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -0500792 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500793 }
794 ccn_charbuf_destroy(&router_prefix);
795
akmhoque7b791452012-10-30 11:24:56 -0500796 if ( nlsr->debugging )
797 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -0500798 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -0500799 if ( nlsr->debugging )
800 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -0500801 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500802
akmhoque53f64222012-09-05 13:57:51 -0500803 print_name_prefix_from_npl();
804 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500805 build_and_install_name_lsas();
806 print_name_lsdb();
807
808 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500809 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500810 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500811
akmhoque59980a52012-08-09 12:36:09 -0500812 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500813 {
akmhoqueffacaa82012-09-13 17:48:30 -0500814 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -0500815 {
akmhoqueffacaa82012-09-13 17:48:30 -0500816 if( nlsr->sched != NULL )
817 {
818 ccn_schedule_run(nlsr->sched);
819 }
820 if(nlsr->ccn != NULL)
821 {
822 res = ccn_run(nlsr->ccn, 500);
823 }
824 if (!(nlsr->sched && nlsr->ccn))
825 {
826 break;
827 }
akmhoque29c1db52012-09-07 14:47:43 -0500828 }
829
akmhoque59980a52012-08-09 12:36:09 -0500830 }
akmhoque59980a52012-08-09 12:36:09 -0500831
akmhoque59980a52012-08-09 12:36:09 -0500832 return 0;
833}
834