blob: 06cc75f04213a7ebb09c86ea1964326bc7ab09e9 [file] [log] [blame]
akmhoque1771c412012-11-09 13:06:08 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
akmhoque59980a52012-08-09 12:36:09 -05004#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>
akmhoque1771c412012-11-09 13:06:08 -06009#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
15
akmhoque59980a52012-08-09 12:36:09 -050016#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
akmhoquebfefef22012-09-26 10:09:34 -050019
akmhoque59980a52012-08-09 12:36:09 -050020#include <ccn/ccn.h>
21#include <ccn/uri.h>
22#include <ccn/keystore.h>
23#include <ccn/signing.h>
24#include <ccn/schedule.h>
25#include <ccn/hashtb.h>
26
27#include "nlsr.h"
28#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050029#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050030#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050031#include "nlsr_npl.h"
32#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050033#include "nlsr_npt.h"
34#include "nlsr_route.h"
35
36
akmhoque81c25e02012-09-10 14:50:33 -050037#define ON_ERROR_DESTROY(resval) \
38{ \
39 if ((resval) < 0) { \
40 nlsr_destroy(); \
akmhoqueffacaa82012-09-13 17:48:30 -050041 exit(1);\
akmhoque81c25e02012-09-10 14:50:33 -050042 } \
43}
44
45
46#define ON_ERROR_EXIT(resval) \
47{ \
48 if ((resval) < 0) { \
akmhoqueffacaa82012-09-13 17:48:30 -050049 exit(1); \
akmhoque81c25e02012-09-10 14:50:33 -050050 } \
51}
akmhoque59980a52012-08-09 12:36:09 -050052
53struct option longopts[] =
54{
55 { "daemon", no_argument, NULL, 'd'},
56 { "config_file", required_argument, NULL, 'f'},
57 { "help", no_argument, NULL, 'h'},
58 { 0 }
59};
60
61static int
62usage(char *progname)
63{
64
65 printf("Usage: %s [OPTIONS...]\n\
66 NDN routing....\n\
67 -d, --daemon Run in daemon mode\n\
68 -f, --config_file Specify configuration file name\n\
69 -h, --help Display this help message\n", progname);
70
71 exit(1);
72}
73
74void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
75{
76 struct timeval now = {0};
77 gettimeofday(&now, 0);
78 result->s = now.tv_sec;
79 result->micros = now.tv_usec;
80}
81
82static struct ccn_gettime ndn_rtr_ticker = {
83 "timer",
84 &ndn_rtr_gettime,
85 1000000,
86 NULL
87};
88
akmhoqueffacaa82012-09-13 17:48:30 -050089void
90nlsr_lock(void)
91{
92 nlsr->semaphor=NLSR_LOCKED;
93}
94
95void
96nlsr_unlock(void)
97{
98 nlsr->semaphor=NLSR_UNLOCKED;
99}
akmhoque42098b12012-08-27 22:54:23 -0500100
101void
akmhoque03004e62012-09-06 01:12:28 -0500102nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -0500103{
akmhoque03004e62012-09-06 01:12:28 -0500104 signal(sig, SIG_IGN);
akmhoqueffacaa82012-09-13 17:48:30 -0500105 nlsr_destroy();
106 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500107}
108
akmhoquebfefef22012-09-26 10:09:34 -0500109void
110daemonize_nlsr(void)
111{
112 int ret;
113 pid_t process_id = 0;
114 pid_t sid = 0;
115 process_id = fork();
116 if (process_id < 0)
117 {
akmhoque7b791452012-10-30 11:24:56 -0500118 printf("Daemonization failed!\n");
akmhoquebfefef22012-09-26 10:09:34 -0500119 ON_ERROR_DESTROY(process_id);
120 }
121 if (process_id > 0)
122 {
123 printf("Process daemonized. Process id: %d \n", process_id);
124 ret=process_id;
125 exit(0);
126 }
127
128 umask(0);
129 sid = setsid();
130 if(sid < 0)
131 {
132 ON_ERROR_DESTROY(sid);
133 }
134
135 chdir("/");
136 close(STDIN_FILENO);
137 close(STDOUT_FILENO);
138 close(STDERR_FILENO);
139}
140
akmhoque59980a52012-08-09 12:36:09 -0500141void
142process_command_ccnneighbor(char *command)
143{
144 if(command==NULL)
145 {
akmhoque28c45022012-08-09 15:38:02 -0500146 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500147 return;
148 }
149 char *rem;
150 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500151 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500152
akmhoque28c45022012-08-09 15:38:02 -0500153 rtr_name=strtok_r(command,sep,&rem);
154 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500155 {
akmhoque28c45022012-08-09 15:38:02 -0500156 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500157 return;
158 }
159
160 face=strtok_r(NULL,sep,&rem);
161 if(face==NULL)
162 {
akmhoque28c45022012-08-09 15:38:02 -0500163 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500164 return;
165 }
166
akmhoque28c45022012-08-09 15:38:02 -0500167 printf("Router: %s face: %s\n",rtr_name,face);
168 int face_id;
169 int res;
170 res=sscanf(face,"face%d",&face_id);
171
172 if(res != 1 )
173 {
174 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
175 return;
176 }
177
akmhoqued5152122012-09-19 06:44:23 -0500178 if ( rtr_name[strlen(rtr_name)-1] == '/' )
179 {
180 rtr_name[strlen(rtr_name)-1]='\0';
181 }
akmhoque03004e62012-09-06 01:12:28 -0500182 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
183 nbr->name=(char *)malloc(strlen(rtr_name)+1);
184 memset(nbr->name,0,strlen(rtr_name)+1);
185 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
186 nbr->length=strlen(rtr_name)+1;
187
188 add_nbr_to_adl(nbr,face_id);
189
190 free(nbr->name);
191 free(nbr);
192}
193
194void
195process_command_ccnname(char *command)
196{
197
198 if(command==NULL)
199 {
200 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
201 return;
202 }
203 char *rem;
204 const char *sep=" \t\n";
205 char *name;
206 name=strtok_r(command,sep,&rem);
207 if(name==NULL)
208 {
209 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
210 return;
211 }
212
213 printf("Name Prefix: %s \n",name);
214
akmhoqued5152122012-09-19 06:44:23 -0500215 if ( name[strlen(name)-1] == '/' )
216 name[strlen(name)-1]='\0';
217
akmhoque53f64222012-09-05 13:57:51 -0500218 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500219 np->name=(char *)malloc(strlen(name)+1);
220 memset(np->name,0,strlen(name)+1);
221 memcpy(np->name,name,strlen(name)+1);
222 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500223
akmhoque03004e62012-09-06 01:12:28 -0500224 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500225
akmhoque03004e62012-09-06 01:12:28 -0500226 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500227 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500228}
229
akmhoque03004e62012-09-06 01:12:28 -0500230
231void
232process_command_router_name(char *command)
233{
234 if(command==NULL)
235 {
236 printf(" Wrong Command Format ( router-name /router/name )\n");
237 return;
238 }
239 char *rem;
240 const char *sep=" \t\n";
241 char *rtr_name;
242
243 rtr_name=strtok_r(command,sep,&rem);
244 if(rtr_name==NULL)
245 {
246 printf(" Wrong Command Format ( router-name /router/name )\n");
247 return;
248 }
249
250
akmhoqued5152122012-09-19 06:44:23 -0500251 if ( rtr_name[strlen(rtr_name)-1] == '/' )
252 rtr_name[strlen(rtr_name)-1]='\0';
253
akmhoque03004e62012-09-06 01:12:28 -0500254 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
255 memset(nlsr->router_name,0,strlen(rtr_name)+1);
256 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
257
258
259}
260
akmhoque59980a52012-08-09 12:36:09 -0500261void
akmhoqued79438d2012-08-27 13:31:42 -0500262process_command_lsdb_synch_interval(char *command)
263{
264 if(command==NULL)
265 {
266 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
267 return;
268 }
269 char *rem;
270 const char *sep=" \t\n";
271 char *secs;
272 long int seconds;
273
274 secs=strtok_r(command,sep,&rem);
275 if(secs==NULL)
276 {
277 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
278 return;
279 }
280
281 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500282 if ( seconds >= 120 && seconds <= 3600 )
283 {
284 nlsr->lsdb_synch_interval=seconds;
285 }
akmhoqued79438d2012-08-27 13:31:42 -0500286
287}
288
289
290void
291process_command_interest_retry(char *command)
292{
293 if(command==NULL)
294 {
295 printf(" Wrong Command Format ( interest-retry number )\n");
296 return;
297 }
298 char *rem;
299 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500300 char *retry;
301 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500302
akmhoqueffacaa82012-09-13 17:48:30 -0500303 retry=strtok_r(command,sep,&rem);
304 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500305 {
306 printf(" Wrong Command Format ( interest-retry number)\n");
307 return;
308 }
309
akmhoqueffacaa82012-09-13 17:48:30 -0500310 retry_number=atoi(retry);
311 if ( retry_number >= 1 && retry_number<=10 )
312 {
313 nlsr->interest_retry=retry_number;
314 }
akmhoqued79438d2012-08-27 13:31:42 -0500315
316}
317
318void
319process_command_interest_resend_time(char *command)
320{
321 if(command==NULL)
322 {
323 printf(" Wrong Command Format ( interest-resend-time secs )\n");
324 return;
325 }
326 char *rem;
327 const char *sep=" \t\n";
328 char *secs;
329 long int seconds;
330
331 secs=strtok_r(command,sep,&rem);
332 if(secs==NULL)
333 {
334 printf(" Wrong Command Format ( interest-resend-time secs)\n");
335 return;
336 }
337
338 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500339 if ( seconds <= 60 && seconds >= 1 )
340 {
341 nlsr->interest_resend_time=seconds;
342 }
akmhoqued79438d2012-08-27 13:31:42 -0500343}
344
akmhoque03004e62012-09-06 01:12:28 -0500345
akmhoqued5152122012-09-19 06:44:23 -0500346void
347process_command_lsa_refresh_time(char *command)
348{
349 if(command==NULL)
350 {
351 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
352 return;
353 }
354 char *rem;
355 const char *sep=" \t\n";
356 char *secs;
357 long int seconds;
358
359 secs=strtok_r(command,sep,&rem);
360 if(secs==NULL)
361 {
362 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
363 return;
364 }
365
366 seconds=atoi(secs);
367 if ( seconds >= 240 && seconds <= 3600 )
368 {
369 nlsr->lsa_refresh_time=seconds;
370 }
371
372}
373
374void
375process_command_router_dead_interval(char *command)
376{
377 if(command==NULL)
378 {
379 printf(" Wrong Command Format ( router-dead-interval secs )\n");
380 return;
381 }
382 char *rem;
383 const char *sep=" \t\n";
384 char *secs;
385 long int seconds;
386
387 secs=strtok_r(command,sep,&rem);
388 if(secs==NULL)
389 {
390 printf(" Wrong Command Format ( router-dead-interval secs)\n");
391 return;
392 }
393
394 seconds=atoi(secs);
395 if ( seconds >= 360 && seconds <= 5400 )
396 {
397 nlsr->router_dead_interval=seconds;
398 }
399
400}
akmhoque03004e62012-09-06 01:12:28 -0500401
akmhoqued79438d2012-08-27 13:31:42 -0500402void
akmhoque3cced642012-09-24 16:20:20 -0500403process_command_multi_path_face_num(char *command)
404{
405 if(command==NULL)
406 {
407 printf(" Wrong Command Format ( multi-path-face-num n )\n");
408 return;
409 }
410 char *rem;
411 const char *sep=" \t\n";
412 char *num;
413 long int number;
414
415 num=strtok_r(command,sep,&rem);
416 if(num==NULL)
417 {
418 printf(" Wrong Command Format ( multi-path-face-num n)\n");
419 return;
420 }
421
422 number=atoi(num);
423 if ( number >= 0 && number <= 60 )
424 {
425 nlsr->multi_path_face_num=number;
426 }
427
428}
429
430void
akmhoquebfefef22012-09-26 10:09:34 -0500431process_command_logdir(char *command)
432{
433 if(command==NULL)
434 {
435 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
436 return;
437 }
438 char *rem;
439 const char *sep=" \t\n";
440 char *dir;
441
442 dir=strtok_r(command,sep,&rem);
443 if(dir==NULL)
444 {
445 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
446 return;
447 }
448
449 nlsr->logDir=(char *)malloc(strlen(dir)+1);
450 memset(nlsr->logDir,0,strlen(dir)+1);
451 memcpy(nlsr->logDir,dir,strlen(dir));
452}
453
454void
akmhoque7b791452012-10-30 11:24:56 -0500455process_command_detailed_log(char *command)
456{
457 if(command==NULL)
458 {
459 printf(" Wrong Command Format ( detailed-log on/off )\n");
460 return;
461 }
462 char *rem;
463 const char *sep=" \t\n";
464 char *on_off;
465
466 on_off=strtok_r(command,sep,&rem);
467 if(on_off==NULL)
468 {
469 printf(" Wrong Command Format ( detailed-log on/off )\n");
470 return;
471 }
472
473 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
474 {
475 nlsr->detailed_logging=1;
476 }
477}
478
479void
480process_command_debug(char *command)
481{
482 if(command==NULL)
483 {
484 printf(" Wrong Command Format ( debug on/off )\n");
485 return;
486 }
487 char *rem;
488 const char *sep=" \t\n";
489 char *on_off;
490
491 on_off=strtok_r(command,sep,&rem);
492 if(on_off==NULL)
493 {
494 printf(" Wrong Command Format ( debug on/off )\n");
495 return;
496 }
497
498 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
499 {
500 nlsr->debugging=1;
501 }
502}
503
504void
akmhoque59980a52012-08-09 12:36:09 -0500505process_conf_command(char *command)
506{
507 const char *separators=" \t\n";
508 char *remainder=NULL;
509 char *cmd_type=NULL;
510
511 if(command==NULL || strlen(command)==0 || command[0]=='!')
512 return;
513
514 cmd_type=strtok_r(command,separators,&remainder);
515
516 if(!strcmp(cmd_type,"router-name") )
517 {
518 process_command_router_name(remainder);
519 }
520 else if(!strcmp(cmd_type,"ccnneighbor") )
521 {
522 process_command_ccnneighbor(remainder);
523 }
524 else if(!strcmp(cmd_type,"ccnname") )
525 {
526 process_command_ccnname(remainder);
527 }
akmhoqued79438d2012-08-27 13:31:42 -0500528 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
529 {
530 process_command_lsdb_synch_interval(remainder);
531 }
532 else if(!strcmp(cmd_type,"interest-retry") )
533 {
534 process_command_interest_retry(remainder);
535 }
536 else if(!strcmp(cmd_type,"interest-resend-time") )
537 {
538 process_command_interest_resend_time(remainder);
539 }
akmhoqued5152122012-09-19 06:44:23 -0500540 else if(!strcmp(cmd_type,"lsa-refresh-time") )
541 {
542 process_command_lsa_refresh_time(remainder);
543 }
544 else if(!strcmp(cmd_type,"router-dead-interval") )
545 {
546 process_command_router_dead_interval(remainder);
547 }
akmhoque3cced642012-09-24 16:20:20 -0500548 else if(!strcmp(cmd_type,"multi-path-face-num") )
549 {
550 process_command_multi_path_face_num(remainder);
551 }
akmhoquebfefef22012-09-26 10:09:34 -0500552 else if(!strcmp(cmd_type,"logdir") )
553 {
554 process_command_logdir(remainder);
555 }
akmhoque7b791452012-10-30 11:24:56 -0500556 else if(!strcmp(cmd_type,"detailed-log") )
557 {
558 process_command_detailed_log(remainder);
559 }
560 else if(!strcmp(cmd_type,"debug") )
561 {
562 process_command_debug(remainder);
563 }
akmhoqued5152122012-09-19 06:44:23 -0500564 else
akmhoque59980a52012-08-09 12:36:09 -0500565 {
566 printf("Wrong configuration Command %s \n",cmd_type);
567 }
568}
569
akmhoque03004e62012-09-06 01:12:28 -0500570
akmhoque59980a52012-08-09 12:36:09 -0500571int
572readConfigFile(const char *filename)
573{
574 FILE *cfg;
575 char buf[1024];
576 int len;
577
578 cfg=fopen(filename, "r");
579
580 if(cfg == NULL)
581 {
582 printf("\nConfiguration File does not exists\n");
583 exit(1);
584 }
585
586 while(fgets((char *)buf, sizeof(buf), cfg))
587 {
588 len=strlen(buf);
589 if(buf[len-1] == '\n')
590 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500591 if ( buf[0] != '#' && buf[0] != '!')
592 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500593 }
594
595 fclose(cfg);
596
597 return 0;
598}
599
akmhoque562caef2012-11-09 13:29:06 -0600600char *
601process_api_client_command(char *command)
602{
603 char *msg;
604 msg=(char *)malloc(100);
605 memset(msg,100,0);
akmhoque3171d652012-11-13 11:44:33 -0600606 //strcpy(msg,"Action Carried Out for NLSR Api Client");
akmhoque562caef2012-11-09 13:29:06 -0600607
akmhoque3171d652012-11-13 11:44:33 -0600608 const char *sep=" \t\n";
609 char *rem=NULL;
610 char *cmd_type=NULL;
611 char *op_type=NULL;
612 char *name=NULL;
613 char *face=NULL;
614 int face_id;
615 int res;
616
617 op_type=strtok_r(command,sep,&rem);
618 cmd_type=strtok_r(NULL,sep,&rem);
619 name=strtok_r(NULL,sep,&rem);
620 if ( name[strlen(name)-1] == '/' )
621 name[strlen(name)-1]='\0';
622
623 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
624 np->name=(char *)malloc(strlen(name)+1);
625 memset(np->name,0,strlen(name)+1);
626 memcpy(np->name,name,strlen(name)+1);
627 np->length=strlen(name)+1;
628
629 if ( strcmp(cmd_type,"name")!= 0 )
630 {
631 face=strtok_r(NULL,sep,&rem);
632 sscanf(face,"face%d",&face_id);
633 }
akmhoque562caef2012-11-09 13:29:06 -0600634
akmhoque3171d652012-11-13 11:44:33 -0600635 if ( strcmp(cmd_type,"name")== 0 )
636 {
637 if ( strcmp(op_type,"del") == 0 )
638 {
639 res=does_name_exist_in_npl(np);
640 if ( res == 0)
641 {
642 sprintf(msg,"Name %s does not exist !!",name);
643 }
644 else
645 {
646 long int ls_id=get_lsa_id_from_npl(np);
647 if ( ls_id != 0 )
648 {
649 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
650 sprintf(msg,"Name %s has been deleted and Advertised.",name);
651 }
652 else
653 {
654 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
655 }
656 }
657 }
658 else if ( strcmp(op_type,"add") == 0 )
659 {
660 res=does_name_exist_in_npl(np);
661 if ( res == 0)
662 {
663 add_name_to_npl(np);
664 build_and_install_single_name_lsa(np);
665 sprintf(msg,"Name %s has been added to advertise.",name);
666 }
667 else
668 {
669 sprintf(msg,"Name %s has already been advertised from this router !!",name);
670 }
671 }
672 }
673 else if ( strcmp(cmd_type,"neighbor") == 0 )
674 {
675 if ( strcmp(op_type,"del") == 0 )
676 {
677 res=is_neighbor(np->name);
678 if ( res == 0)
679 {
680 sprintf(msg,"Neighbor %s does not exist !!",name);
681 }
682 else
683 {
684 update_adjacent_status_to_adl(np,NBR_DOWN);
685 delete_nbr_from_adl(np);
686 if(!nlsr->is_build_adj_lsa_sheduled)
687 {
688 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
689 nlsr->is_build_adj_lsa_sheduled=1;
690 }
691 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
692 }
693 }
694 else if ( strcmp(op_type,"add") == 0 )
695 {
696 res=is_neighbor(np->name);
697 if ( res == 0 )
698 {
699 add_nbr_to_adl(np,face_id);
700 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
701 }
702 else
703 {
704 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
705 }
706 }
707 }
708
akmhoque562caef2012-11-09 13:29:06 -0600709
710 return msg;
711}
akmhoque1771c412012-11-09 13:06:08 -0600712
713int
714nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
715{
716 struct timeval timeout;
717 if ( time_out_micro_sec < 0 )
718 {
719 timeout.tv_sec=1;
720 timeout.tv_usec=0;
721 }
722 else
723 {
724 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
725 timeout.tv_sec=time_out_micro_sec / 1000000;
726 timeout.tv_usec=time_out_micro_sec % 1000000;
727 }
728
729
730 int fd;
731 int nread;
732 int result;
733 fd_set testfds;
734 unsigned int client_len;
735 int client_sockfd;
736 char recv_buffer[1024];
737 bzero(recv_buffer,1024);
738 struct sockaddr_un client_address;
739
740 testfds=nlsr->readfds;
741 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
742
743 for(fd = 0; fd < FD_SETSIZE; fd++)
744 {
745 if(FD_ISSET(fd,&testfds))
746 {
747 if ( fd == ccn_fd )
748 {
749 return 0;
750 }
751 else if(fd == nlsr->nlsr_api_server_sock_fd)
752 {
753 client_len = sizeof(client_address);
754 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
755 FD_SET(client_sockfd, &nlsr->readfds);
756 }
757 else
758 {
759
760 ioctl(fd, FIONREAD, &nread);
761 if(nread == 0)
762 {
763 close(fd);
764 FD_CLR(fd, &nlsr->readfds);
765 }
766 else
767 {
768 recv(fd, recv_buffer, 1024, 0);
769 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -0600770 char *msg=process_api_client_command(recv_buffer);
771 send(fd, msg, strlen(msg),0);
772 free(msg);
akmhoque1771c412012-11-09 13:06:08 -0600773 close(fd);
774 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -0600775 }
776 }
777 }
778 }
779
780 return 0;
781}
782
akmhoque386081b2012-08-10 10:53:21 -0500783void
784nlsr_destroy( void )
785{
akmhoque7b791452012-10-30 11:24:56 -0500786 if ( nlsr->debugging )
787 {
788 printf("Freeing Allocated Memory....\n");
789 }
akmhoque9e9fc722012-09-26 14:03:25 -0500790 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500791 /* Destroying all face created by nlsr in CCND */
792 destroy_all_face_by_nlsr();
793
akmhoque386081b2012-08-10 10:53:21 -0500794 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500795 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500796 hashtb_destroy(&nlsr->adl);
797 hashtb_destroy(&nlsr->lsdb->name_lsdb);
798 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500799 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500800
801 //To Do: has to destroy the face_list one by one
802
akmhoque3560cb62012-09-09 10:52:30 -0500803 hashtb_destroy(&nlsr->routing_table);
804
805
806 int i, npt_element;
807 struct npt_entry *ne;
808 struct hashtb_enumerator ee;
809 struct hashtb_enumerator *e = &ee;
810 hashtb_start(nlsr->npt, e);
811 npt_element=hashtb_n(nlsr->npt);
812 for(i=0;i<npt_element;i++)
813 {
814 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500815 hashtb_destroy(&ne->name_list);
816 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500817 hashtb_next(e);
818 }
819
820 hashtb_end(e);
821 hashtb_destroy(&nlsr->npt);
822
akmhoque03004e62012-09-06 01:12:28 -0500823
akmhoque386081b2012-08-10 10:53:21 -0500824 ccn_schedule_destroy(&nlsr->sched);
825 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500826
827 free(nlsr->lsdb->lsdb_version);
828 free(nlsr->lsdb);
829 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500830 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -0500831 if ( nlsr->debugging )
832 {
833 printf("Finished freeing allocated memory\n");
834 }
akmhoque9e9fc722012-09-26 14:03:25 -0500835 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -0500836
akmhoque386081b2012-08-10 10:53:21 -0500837}
838
akmhoque03004e62012-09-06 01:12:28 -0500839
akmhoque1771c412012-11-09 13:06:08 -0600840void
841init_api_server(int ccn_fd)
842{
843 int server_sockfd;
844 int server_len;
845 struct sockaddr_un server_address;
846
847 unlink("/tmp/nlsr_api_server_socket");
848 server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
849
850 int flags = fcntl(server_sockfd, F_GETFL, 0);
851 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
852
853 server_address.sun_family = AF_UNIX;
854 strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket");
855 server_len = sizeof(server_address);
856 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
857 listen(server_sockfd, 100);
858 FD_ZERO(&nlsr->readfds);
859 FD_SET(server_sockfd, &nlsr->readfds);
860 FD_SET(ccn_fd, &nlsr->readfds);
861 nlsr->nlsr_api_server_sock_fd=server_sockfd;
862
863}
864
akmhoque81c25e02012-09-10 14:50:33 -0500865int
akmhoque902d57e2012-08-17 09:24:38 -0500866init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500867{
akmhoque03004e62012-09-06 01:12:28 -0500868 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
869 {
870 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500871 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500872 }
873 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
874 {
875 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500876 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500877 }
878 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
879 {
880 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500881 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500882 }
akmhoque902d57e2012-08-17 09:24:38 -0500883
akmhoque59980a52012-08-09 12:36:09 -0500884 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500885
886 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500887 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500888 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -0600889 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500890 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500891 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500892 struct hashtb_param param_npt = {0};
893 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
894 struct hashtb_param param_rte = {0};
895 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500896
akmhoque59980a52012-08-09 12:36:09 -0500897 nlsr->in_interest.p = &incoming_interest;
898 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500899
akmhoque03004e62012-09-06 01:12:28 -0500900 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500901
akmhoque03004e62012-09-06 01:12:28 -0500902 char *time_stamp=(char *)malloc(20);
903 memset(time_stamp,0,20);
904 get_current_timestamp_micro(time_stamp);
905 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
906 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
907 free(time_stamp);
908
909 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400910 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500911 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400912 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500913
914
akmhoque902d57e2012-08-17 09:24:38 -0500915
akmhoque53f64222012-09-05 13:57:51 -0500916
akmhoque59980a52012-08-09 12:36:09 -0500917 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500918 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500919 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500920 nlsr->adj_build_count=0;
921 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500922 nlsr->is_send_lsdb_interest_scheduled=0;
923 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500924
akmhoque7b791452012-10-30 11:24:56 -0500925 nlsr->detailed_logging=0;
926 nlsr->debugging=0;
927
akmhoqued79438d2012-08-27 13:31:42 -0500928 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
929 nlsr->interest_retry = INTEREST_RETRY;
930 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500931 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
932 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500933 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueffacaa82012-09-13 17:48:30 -0500934 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500935
936 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500937}
938
akmhoque03004e62012-09-06 01:12:28 -0500939
akmhoque902d57e2012-08-17 09:24:38 -0500940int
941main(int argc, char *argv[])
942{
akmhoque81c25e02012-09-10 14:50:33 -0500943 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500944 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500945 int daemon_mode=0;
akmhoque902d57e2012-08-17 09:24:38 -0500946
akmhoquebfefef22012-09-26 10:09:34 -0500947
akmhoque81c25e02012-09-10 14:50:33 -0500948
akmhoque59980a52012-08-09 12:36:09 -0500949 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
950 {
951 switch (res)
952 {
953 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500954 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500955 break;
956 case 'f':
957 config_file = optarg;
958 break;
959 case 'h':
960 default:
961 usage(argv[0]);
962 }
963 }
964
akmhoquebfefef22012-09-26 10:09:34 -0500965 ret=init_nlsr();
966 ON_ERROR_EXIT(ret);
akmhoque59980a52012-08-09 12:36:09 -0500967 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500968
969 if ( daemon_mode == 1 )
970 {
971 daemonize_nlsr();
972 }
973
974 startLogging(nlsr->logDir);
975
akmhoque59980a52012-08-09 12:36:09 -0500976 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -0600977 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
978 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -0500979 {
980 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -0500981 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -0500982 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -0500983 }
akmhoque1771c412012-11-09 13:06:08 -0600984
985 init_api_server(ccn_fd);
986
akmhoque53f64222012-09-05 13:57:51 -0500987 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500988 router_prefix=ccn_charbuf_create();
989 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500990 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500991 {
992 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -0500993 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -0500994 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500995 }
akmhoque59980a52012-08-09 12:36:09 -0500996
997 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500998 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500999 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1000 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001001 {
1002 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001003 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001004 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001005 }
1006 ccn_charbuf_destroy(&router_prefix);
1007
akmhoque7b791452012-10-30 11:24:56 -05001008 if ( nlsr->debugging )
1009 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001010 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001011 if ( nlsr->debugging )
1012 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001013 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001014
akmhoque53f64222012-09-05 13:57:51 -05001015 print_name_prefix_from_npl();
1016 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -05001017 build_and_install_name_lsas();
1018 print_name_lsdb();
1019
1020 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001021 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001022 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001023
akmhoque1771c412012-11-09 13:06:08 -06001024
akmhoque59980a52012-08-09 12:36:09 -05001025 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001026 {
akmhoqueffacaa82012-09-13 17:48:30 -05001027 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001028 {
akmhoqueffacaa82012-09-13 17:48:30 -05001029 if( nlsr->sched != NULL )
1030 {
akmhoque1771c412012-11-09 13:06:08 -06001031 long int micro_sec=ccn_schedule_run(nlsr->sched);
1032 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1033 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001034 }
1035 if(nlsr->ccn != NULL)
1036 {
akmhoque1771c412012-11-09 13:06:08 -06001037 res = ccn_run(nlsr->ccn, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001038 }
1039 if (!(nlsr->sched && nlsr->ccn))
1040 {
1041 break;
1042 }
akmhoque29c1db52012-09-07 14:47:43 -05001043 }
1044
akmhoque59980a52012-08-09 12:36:09 -05001045 }
akmhoque1771c412012-11-09 13:06:08 -06001046
akmhoque59980a52012-08-09 12:36:09 -05001047
akmhoque59980a52012-08-09 12:36:09 -05001048 return 0;
1049}
1050