blob: f71e4ae05b01985a98080f3dbf4d85b494c5976d [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>
akmhoque95041802012-11-16 09:18:02 -060015#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
akmhoque1771c412012-11-09 13:06:08 -060018
akmhoque59980a52012-08-09 12:36:09 -050019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
akmhoquebfefef22012-09-26 10:09:34 -050022
akmhoque59980a52012-08-09 12:36:09 -050023#include <ccn/ccn.h>
24#include <ccn/uri.h>
25#include <ccn/keystore.h>
26#include <ccn/signing.h>
27#include <ccn/schedule.h>
28#include <ccn/hashtb.h>
29
30#include "nlsr.h"
31#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050032#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050033#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050034#include "nlsr_npl.h"
35#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050036#include "nlsr_npt.h"
37#include "nlsr_route.h"
38
39
akmhoque81c25e02012-09-10 14:50:33 -050040#define ON_ERROR_DESTROY(resval) \
41{ \
42 if ((resval) < 0) { \
43 nlsr_destroy(); \
akmhoqueffacaa82012-09-13 17:48:30 -050044 exit(1);\
akmhoque81c25e02012-09-10 14:50:33 -050045 } \
46}
47
48
49#define ON_ERROR_EXIT(resval) \
50{ \
51 if ((resval) < 0) { \
akmhoqueffacaa82012-09-13 17:48:30 -050052 exit(1); \
akmhoque81c25e02012-09-10 14:50:33 -050053 } \
54}
akmhoque59980a52012-08-09 12:36:09 -050055
56struct option longopts[] =
57{
58 { "daemon", no_argument, NULL, 'd'},
59 { "config_file", required_argument, NULL, 'f'},
akmhoque95041802012-11-16 09:18:02 -060060 { "api_port", required_argument, NULL, 'p'},
akmhoque59980a52012-08-09 12:36:09 -050061 { "help", no_argument, NULL, 'h'},
62 { 0 }
63};
64
65static int
66usage(char *progname)
67{
68
69 printf("Usage: %s [OPTIONS...]\n\
70 NDN routing....\n\
71 -d, --daemon Run in daemon mode\n\
72 -f, --config_file Specify configuration file name\n\
akmhoque95041802012-11-16 09:18:02 -060073 -p, --api_port port where api client will connect\n\
akmhoque59980a52012-08-09 12:36:09 -050074 -h, --help Display this help message\n", progname);
75
76 exit(1);
77}
78
79void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
80{
81 struct timeval now = {0};
82 gettimeofday(&now, 0);
83 result->s = now.tv_sec;
84 result->micros = now.tv_usec;
85}
86
87static struct ccn_gettime ndn_rtr_ticker = {
88 "timer",
89 &ndn_rtr_gettime,
90 1000000,
91 NULL
92};
93
akmhoqueffacaa82012-09-13 17:48:30 -050094void
95nlsr_lock(void)
96{
97 nlsr->semaphor=NLSR_LOCKED;
98}
99
100void
101nlsr_unlock(void)
102{
103 nlsr->semaphor=NLSR_UNLOCKED;
104}
akmhoque42098b12012-08-27 22:54:23 -0500105
106void
akmhoque03004e62012-09-06 01:12:28 -0500107nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -0500108{
akmhoque03004e62012-09-06 01:12:28 -0500109 signal(sig, SIG_IGN);
akmhoqueffacaa82012-09-13 17:48:30 -0500110 nlsr_destroy();
111 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500112}
113
akmhoquebfefef22012-09-26 10:09:34 -0500114void
115daemonize_nlsr(void)
116{
117 int ret;
118 pid_t process_id = 0;
119 pid_t sid = 0;
120 process_id = fork();
121 if (process_id < 0)
122 {
akmhoque7b791452012-10-30 11:24:56 -0500123 printf("Daemonization failed!\n");
akmhoquebfefef22012-09-26 10:09:34 -0500124 ON_ERROR_DESTROY(process_id);
125 }
126 if (process_id > 0)
127 {
128 printf("Process daemonized. Process id: %d \n", process_id);
129 ret=process_id;
130 exit(0);
131 }
132
133 umask(0);
134 sid = setsid();
135 if(sid < 0)
136 {
137 ON_ERROR_DESTROY(sid);
138 }
139
140 chdir("/");
141 close(STDIN_FILENO);
142 close(STDOUT_FILENO);
143 close(STDERR_FILENO);
144}
145
akmhoque59980a52012-08-09 12:36:09 -0500146void
147process_command_ccnneighbor(char *command)
148{
149 if(command==NULL)
150 {
akmhoque28c45022012-08-09 15:38:02 -0500151 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500152 return;
153 }
154 char *rem;
155 const char *sep=" \t\n";
akmhoque28c45022012-08-09 15:38:02 -0500156 char *rtr_name,*face;
akmhoque59980a52012-08-09 12:36:09 -0500157
akmhoque28c45022012-08-09 15:38:02 -0500158 rtr_name=strtok_r(command,sep,&rem);
159 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500160 {
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
165 face=strtok_r(NULL,sep,&rem);
166 if(face==NULL)
167 {
akmhoque28c45022012-08-09 15:38:02 -0500168 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500169 return;
170 }
171
akmhoque28c45022012-08-09 15:38:02 -0500172 printf("Router: %s face: %s\n",rtr_name,face);
173 int face_id;
174 int res;
175 res=sscanf(face,"face%d",&face_id);
176
177 if(res != 1 )
178 {
179 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
180 return;
181 }
akmhoqued5152122012-09-19 06:44:23 -0500182 if ( rtr_name[strlen(rtr_name)-1] == '/' )
183 {
184 rtr_name[strlen(rtr_name)-1]='\0';
185 }
akmhoque03004e62012-09-06 01:12:28 -0500186 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
187 nbr->name=(char *)malloc(strlen(rtr_name)+1);
188 memset(nbr->name,0,strlen(rtr_name)+1);
189 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
190 nbr->length=strlen(rtr_name)+1;
191
192 add_nbr_to_adl(nbr,face_id);
193
194 free(nbr->name);
195 free(nbr);
196}
197
198void
199process_command_ccnname(char *command)
200{
201
202 if(command==NULL)
203 {
204 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
205 return;
206 }
207 char *rem;
208 const char *sep=" \t\n";
209 char *name;
210 name=strtok_r(command,sep,&rem);
211 if(name==NULL)
212 {
213 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
214 return;
215 }
216
217 printf("Name Prefix: %s \n",name);
218
akmhoqued5152122012-09-19 06:44:23 -0500219 if ( name[strlen(name)-1] == '/' )
220 name[strlen(name)-1]='\0';
221
akmhoque53f64222012-09-05 13:57:51 -0500222 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500223 np->name=(char *)malloc(strlen(name)+1);
224 memset(np->name,0,strlen(name)+1);
225 memcpy(np->name,name,strlen(name)+1);
226 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500227
akmhoque03004e62012-09-06 01:12:28 -0500228 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500229
akmhoque03004e62012-09-06 01:12:28 -0500230 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500231 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500232}
233
akmhoque03004e62012-09-06 01:12:28 -0500234
235void
236process_command_router_name(char *command)
237{
238 if(command==NULL)
239 {
240 printf(" Wrong Command Format ( router-name /router/name )\n");
241 return;
242 }
243 char *rem;
244 const char *sep=" \t\n";
245 char *rtr_name;
246
247 rtr_name=strtok_r(command,sep,&rem);
248 if(rtr_name==NULL)
249 {
250 printf(" Wrong Command Format ( router-name /router/name )\n");
251 return;
252 }
253
254
akmhoqued5152122012-09-19 06:44:23 -0500255 if ( rtr_name[strlen(rtr_name)-1] == '/' )
256 rtr_name[strlen(rtr_name)-1]='\0';
257
akmhoque03004e62012-09-06 01:12:28 -0500258 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
259 memset(nlsr->router_name,0,strlen(rtr_name)+1);
260 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
261
262
263}
264
akmhoque59980a52012-08-09 12:36:09 -0500265void
akmhoqued79438d2012-08-27 13:31:42 -0500266process_command_lsdb_synch_interval(char *command)
267{
268 if(command==NULL)
269 {
270 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
271 return;
272 }
273 char *rem;
274 const char *sep=" \t\n";
275 char *secs;
276 long int seconds;
277
278 secs=strtok_r(command,sep,&rem);
279 if(secs==NULL)
280 {
281 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
282 return;
283 }
284
285 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500286 if ( seconds >= 120 && seconds <= 3600 )
287 {
288 nlsr->lsdb_synch_interval=seconds;
289 }
akmhoqued79438d2012-08-27 13:31:42 -0500290
291}
292
293
294void
295process_command_interest_retry(char *command)
296{
297 if(command==NULL)
298 {
299 printf(" Wrong Command Format ( interest-retry number )\n");
300 return;
301 }
302 char *rem;
303 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500304 char *retry;
305 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500306
akmhoqueffacaa82012-09-13 17:48:30 -0500307 retry=strtok_r(command,sep,&rem);
308 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500309 {
310 printf(" Wrong Command Format ( interest-retry number)\n");
311 return;
312 }
313
akmhoqueffacaa82012-09-13 17:48:30 -0500314 retry_number=atoi(retry);
315 if ( retry_number >= 1 && retry_number<=10 )
316 {
317 nlsr->interest_retry=retry_number;
318 }
akmhoqued79438d2012-08-27 13:31:42 -0500319
320}
321
322void
323process_command_interest_resend_time(char *command)
324{
325 if(command==NULL)
326 {
327 printf(" Wrong Command Format ( interest-resend-time secs )\n");
328 return;
329 }
330 char *rem;
331 const char *sep=" \t\n";
332 char *secs;
333 long int seconds;
334
335 secs=strtok_r(command,sep,&rem);
336 if(secs==NULL)
337 {
338 printf(" Wrong Command Format ( interest-resend-time secs)\n");
339 return;
340 }
341
342 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500343 if ( seconds <= 60 && seconds >= 1 )
344 {
345 nlsr->interest_resend_time=seconds;
346 }
akmhoqued79438d2012-08-27 13:31:42 -0500347}
348
akmhoque03004e62012-09-06 01:12:28 -0500349
akmhoqued5152122012-09-19 06:44:23 -0500350void
351process_command_lsa_refresh_time(char *command)
352{
353 if(command==NULL)
354 {
355 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
356 return;
357 }
358 char *rem;
359 const char *sep=" \t\n";
360 char *secs;
361 long int seconds;
362
363 secs=strtok_r(command,sep,&rem);
364 if(secs==NULL)
365 {
366 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
367 return;
368 }
369
370 seconds=atoi(secs);
371 if ( seconds >= 240 && seconds <= 3600 )
372 {
373 nlsr->lsa_refresh_time=seconds;
374 }
375
376}
377
378void
379process_command_router_dead_interval(char *command)
380{
381 if(command==NULL)
382 {
383 printf(" Wrong Command Format ( router-dead-interval secs )\n");
384 return;
385 }
386 char *rem;
387 const char *sep=" \t\n";
388 char *secs;
389 long int seconds;
390
391 secs=strtok_r(command,sep,&rem);
392 if(secs==NULL)
393 {
394 printf(" Wrong Command Format ( router-dead-interval secs)\n");
395 return;
396 }
397
398 seconds=atoi(secs);
399 if ( seconds >= 360 && seconds <= 5400 )
400 {
401 nlsr->router_dead_interval=seconds;
402 }
403
404}
akmhoque03004e62012-09-06 01:12:28 -0500405
akmhoqued79438d2012-08-27 13:31:42 -0500406void
akmhoque3cced642012-09-24 16:20:20 -0500407process_command_multi_path_face_num(char *command)
408{
409 if(command==NULL)
410 {
411 printf(" Wrong Command Format ( multi-path-face-num n )\n");
412 return;
413 }
414 char *rem;
415 const char *sep=" \t\n";
416 char *num;
417 long int number;
418
419 num=strtok_r(command,sep,&rem);
420 if(num==NULL)
421 {
422 printf(" Wrong Command Format ( multi-path-face-num n)\n");
423 return;
424 }
425
426 number=atoi(num);
427 if ( number >= 0 && number <= 60 )
428 {
429 nlsr->multi_path_face_num=number;
430 }
431
432}
433
434void
akmhoquebfefef22012-09-26 10:09:34 -0500435process_command_logdir(char *command)
436{
437 if(command==NULL)
438 {
439 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
440 return;
441 }
442 char *rem;
443 const char *sep=" \t\n";
444 char *dir;
445
446 dir=strtok_r(command,sep,&rem);
447 if(dir==NULL)
448 {
449 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
450 return;
451 }
452
453 nlsr->logDir=(char *)malloc(strlen(dir)+1);
454 memset(nlsr->logDir,0,strlen(dir)+1);
455 memcpy(nlsr->logDir,dir,strlen(dir));
456}
457
458void
akmhoque7b791452012-10-30 11:24:56 -0500459process_command_detailed_log(char *command)
460{
461 if(command==NULL)
462 {
463 printf(" Wrong Command Format ( detailed-log on/off )\n");
464 return;
465 }
466 char *rem;
467 const char *sep=" \t\n";
468 char *on_off;
469
470 on_off=strtok_r(command,sep,&rem);
471 if(on_off==NULL)
472 {
473 printf(" Wrong Command Format ( detailed-log on/off )\n");
474 return;
475 }
476
477 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
478 {
479 nlsr->detailed_logging=1;
480 }
481}
482
483void
484process_command_debug(char *command)
485{
486 if(command==NULL)
487 {
488 printf(" Wrong Command Format ( debug on/off )\n");
489 return;
490 }
491 char *rem;
492 const char *sep=" \t\n";
493 char *on_off;
494
495 on_off=strtok_r(command,sep,&rem);
496 if(on_off==NULL)
497 {
498 printf(" Wrong Command Format ( debug on/off )\n");
499 return;
500 }
501
502 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
503 {
504 nlsr->debugging=1;
505 }
506}
507
508void
akmhoque59980a52012-08-09 12:36:09 -0500509process_conf_command(char *command)
510{
511 const char *separators=" \t\n";
512 char *remainder=NULL;
513 char *cmd_type=NULL;
514
515 if(command==NULL || strlen(command)==0 || command[0]=='!')
516 return;
517
518 cmd_type=strtok_r(command,separators,&remainder);
519
520 if(!strcmp(cmd_type,"router-name") )
521 {
522 process_command_router_name(remainder);
523 }
524 else if(!strcmp(cmd_type,"ccnneighbor") )
525 {
526 process_command_ccnneighbor(remainder);
527 }
528 else if(!strcmp(cmd_type,"ccnname") )
529 {
530 process_command_ccnname(remainder);
531 }
akmhoqued79438d2012-08-27 13:31:42 -0500532 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
533 {
534 process_command_lsdb_synch_interval(remainder);
535 }
536 else if(!strcmp(cmd_type,"interest-retry") )
537 {
538 process_command_interest_retry(remainder);
539 }
540 else if(!strcmp(cmd_type,"interest-resend-time") )
541 {
542 process_command_interest_resend_time(remainder);
543 }
akmhoqued5152122012-09-19 06:44:23 -0500544 else if(!strcmp(cmd_type,"lsa-refresh-time") )
545 {
546 process_command_lsa_refresh_time(remainder);
547 }
548 else if(!strcmp(cmd_type,"router-dead-interval") )
549 {
550 process_command_router_dead_interval(remainder);
551 }
akmhoque3cced642012-09-24 16:20:20 -0500552 else if(!strcmp(cmd_type,"multi-path-face-num") )
553 {
554 process_command_multi_path_face_num(remainder);
555 }
akmhoquebfefef22012-09-26 10:09:34 -0500556 else if(!strcmp(cmd_type,"logdir") )
557 {
558 process_command_logdir(remainder);
559 }
akmhoque7b791452012-10-30 11:24:56 -0500560 else if(!strcmp(cmd_type,"detailed-log") )
561 {
562 process_command_detailed_log(remainder);
563 }
564 else if(!strcmp(cmd_type,"debug") )
565 {
566 process_command_debug(remainder);
567 }
akmhoqued5152122012-09-19 06:44:23 -0500568 else
akmhoque59980a52012-08-09 12:36:09 -0500569 {
570 printf("Wrong configuration Command %s \n",cmd_type);
571 }
572}
573
akmhoque03004e62012-09-06 01:12:28 -0500574
akmhoque59980a52012-08-09 12:36:09 -0500575int
576readConfigFile(const char *filename)
577{
578 FILE *cfg;
579 char buf[1024];
580 int len;
581
582 cfg=fopen(filename, "r");
583
584 if(cfg == NULL)
585 {
586 printf("\nConfiguration File does not exists\n");
587 exit(1);
588 }
589
590 while(fgets((char *)buf, sizeof(buf), cfg))
591 {
592 len=strlen(buf);
593 if(buf[len-1] == '\n')
594 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500595 if ( buf[0] != '#' && buf[0] != '!')
596 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500597 }
598
599 fclose(cfg);
600
601 return 0;
602}
603
akmhoque562caef2012-11-09 13:29:06 -0600604char *
605process_api_client_command(char *command)
606{
607 char *msg;
608 msg=(char *)malloc(100);
609 memset(msg,100,0);
akmhoque3171d652012-11-13 11:44:33 -0600610 //strcpy(msg,"Action Carried Out for NLSR Api Client");
akmhoque562caef2012-11-09 13:29:06 -0600611
akmhoque3171d652012-11-13 11:44:33 -0600612 const char *sep=" \t\n";
613 char *rem=NULL;
614 char *cmd_type=NULL;
615 char *op_type=NULL;
616 char *name=NULL;
617 char *face=NULL;
618 int face_id;
619 int res;
620
621 op_type=strtok_r(command,sep,&rem);
622 cmd_type=strtok_r(NULL,sep,&rem);
623 name=strtok_r(NULL,sep,&rem);
624 if ( name[strlen(name)-1] == '/' )
625 name[strlen(name)-1]='\0';
626
627 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
628 np->name=(char *)malloc(strlen(name)+1);
629 memset(np->name,0,strlen(name)+1);
630 memcpy(np->name,name,strlen(name)+1);
631 np->length=strlen(name)+1;
632
633 if ( strcmp(cmd_type,"name")!= 0 )
634 {
635 face=strtok_r(NULL,sep,&rem);
636 sscanf(face,"face%d",&face_id);
637 }
akmhoque562caef2012-11-09 13:29:06 -0600638
akmhoque3171d652012-11-13 11:44:33 -0600639 if ( strcmp(cmd_type,"name")== 0 )
640 {
641 if ( strcmp(op_type,"del") == 0 )
642 {
643 res=does_name_exist_in_npl(np);
644 if ( res == 0)
645 {
646 sprintf(msg,"Name %s does not exist !!",name);
647 }
648 else
649 {
650 long int ls_id=get_lsa_id_from_npl(np);
651 if ( ls_id != 0 )
652 {
653 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
654 sprintf(msg,"Name %s has been deleted and Advertised.",name);
655 }
656 else
657 {
658 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
659 }
660 }
661 }
662 else if ( strcmp(op_type,"add") == 0 )
663 {
664 res=does_name_exist_in_npl(np);
665 if ( res == 0)
666 {
667 add_name_to_npl(np);
668 build_and_install_single_name_lsa(np);
669 sprintf(msg,"Name %s has been added to advertise.",name);
670 }
671 else
672 {
673 sprintf(msg,"Name %s has already been advertised from this router !!",name);
674 }
675 }
676 }
677 else if ( strcmp(cmd_type,"neighbor") == 0 )
678 {
679 if ( strcmp(op_type,"del") == 0 )
680 {
681 res=is_neighbor(np->name);
682 if ( res == 0)
683 {
684 sprintf(msg,"Neighbor %s does not exist !!",name);
685 }
686 else
687 {
688 update_adjacent_status_to_adl(np,NBR_DOWN);
689 delete_nbr_from_adl(np);
690 if(!nlsr->is_build_adj_lsa_sheduled)
691 {
692 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
693 nlsr->is_build_adj_lsa_sheduled=1;
694 }
695 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
696 }
697 }
698 else if ( strcmp(op_type,"add") == 0 )
699 {
700 res=is_neighbor(np->name);
701 if ( res == 0 )
702 {
703 add_nbr_to_adl(np,face_id);
704 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
705 }
706 else
707 {
708 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
709 }
710 }
711 }
712
akmhoque562caef2012-11-09 13:29:06 -0600713
714 return msg;
715}
akmhoque1771c412012-11-09 13:06:08 -0600716
717int
718nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
719{
720 struct timeval timeout;
721 if ( time_out_micro_sec < 0 )
722 {
723 timeout.tv_sec=1;
724 timeout.tv_usec=0;
725 }
726 else
727 {
728 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
729 timeout.tv_sec=time_out_micro_sec / 1000000;
730 timeout.tv_usec=time_out_micro_sec % 1000000;
731 }
732
733
734 int fd;
735 int nread;
736 int result;
737 fd_set testfds;
738 unsigned int client_len;
739 int client_sockfd;
740 char recv_buffer[1024];
741 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -0600742 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -0600743
744 testfds=nlsr->readfds;
745 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
746
747 for(fd = 0; fd < FD_SETSIZE; fd++)
748 {
749 if(FD_ISSET(fd,&testfds))
750 {
751 if ( fd == ccn_fd )
752 {
753 return 0;
754 }
755 else if(fd == nlsr->nlsr_api_server_sock_fd)
756 {
Adam Alyyanb5fff372013-01-09 14:32:52 -0600757 printf("Setting up socket....\n");
akmhoque1771c412012-11-09 13:06:08 -0600758 client_len = sizeof(client_address);
759 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
760 FD_SET(client_sockfd, &nlsr->readfds);
761 }
762 else
763 {
Adam Alyyanb5fff372013-01-09 14:32:52 -0600764 printf("Else...\n");
akmhoque1771c412012-11-09 13:06:08 -0600765 ioctl(fd, FIONREAD, &nread);
766 if(nread == 0)
767 {
768 close(fd);
769 FD_CLR(fd, &nlsr->readfds);
770 }
771 else
772 {
773 recv(fd, recv_buffer, 1024, 0);
Adam Alyyanb5fff372013-01-09 14:32:52 -0600774 printf("Test Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -0600775 char *msg=process_api_client_command(recv_buffer);
776 send(fd, msg, strlen(msg),0);
777 free(msg);
akmhoque1771c412012-11-09 13:06:08 -0600778 close(fd);
779 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -0600780 }
781 }
782 }
783 }
784
785 return 0;
786}
787
akmhoque386081b2012-08-10 10:53:21 -0500788void
789nlsr_destroy( void )
790{
akmhoque7b791452012-10-30 11:24:56 -0500791 if ( nlsr->debugging )
792 {
793 printf("Freeing Allocated Memory....\n");
794 }
akmhoque9e9fc722012-09-26 14:03:25 -0500795 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500796 /* Destroying all face created by nlsr in CCND */
797 destroy_all_face_by_nlsr();
798
akmhoque386081b2012-08-10 10:53:21 -0500799 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500800 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500801 hashtb_destroy(&nlsr->adl);
802 hashtb_destroy(&nlsr->lsdb->name_lsdb);
803 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500804 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500805
806 //To Do: has to destroy the face_list one by one
807
akmhoque3560cb62012-09-09 10:52:30 -0500808 hashtb_destroy(&nlsr->routing_table);
809
810
811 int i, npt_element;
812 struct npt_entry *ne;
813 struct hashtb_enumerator ee;
814 struct hashtb_enumerator *e = &ee;
815 hashtb_start(nlsr->npt, e);
816 npt_element=hashtb_n(nlsr->npt);
817 for(i=0;i<npt_element;i++)
818 {
819 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500820 hashtb_destroy(&ne->name_list);
821 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500822 hashtb_next(e);
823 }
824
825 hashtb_end(e);
826 hashtb_destroy(&nlsr->npt);
827
akmhoque95041802012-11-16 09:18:02 -0600828
829 close(nlsr->nlsr_api_server_sock_fd);
830
akmhoque386081b2012-08-10 10:53:21 -0500831 ccn_schedule_destroy(&nlsr->sched);
832 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500833
834 free(nlsr->lsdb->lsdb_version);
835 free(nlsr->lsdb);
836 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500837 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -0500838 if ( nlsr->debugging )
839 {
840 printf("Finished freeing allocated memory\n");
841 }
akmhoque9e9fc722012-09-26 14:03:25 -0500842 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -0500843
akmhoque386081b2012-08-10 10:53:21 -0500844}
845
akmhoque03004e62012-09-06 01:12:28 -0500846
akmhoque1771c412012-11-09 13:06:08 -0600847void
848init_api_server(int ccn_fd)
849{
850 int server_sockfd;
851 int server_len;
akmhoque95041802012-11-16 09:18:02 -0600852 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -0600853 unsigned int yes=1;
854
akmhoque95041802012-11-16 09:18:02 -0600855 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -0600856
857 int flags = fcntl(server_sockfd, F_GETFL, 0);
858 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
859
akmhoquef31f13b2012-11-16 09:42:24 -0600860 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
861 {
862 ON_ERROR_DESTROY(-1);
863 }
akmhoque95041802012-11-16 09:18:02 -0600864
865 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -0600866 //server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
867 server_address.sin_addr.s_addr = INADDR_ANY;
868 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -0600869
akmhoque1771c412012-11-09 13:06:08 -0600870 server_len = sizeof(server_address);
871 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
Adam Alyyanb5fff372013-01-09 14:32:52 -0600872 //printf("port number %d\n", ntohs(server_address.sin_port));
akmhoque1771c412012-11-09 13:06:08 -0600873 listen(server_sockfd, 100);
874 FD_ZERO(&nlsr->readfds);
875 FD_SET(server_sockfd, &nlsr->readfds);
876 FD_SET(ccn_fd, &nlsr->readfds);
877 nlsr->nlsr_api_server_sock_fd=server_sockfd;
878
879}
880
akmhoque81c25e02012-09-10 14:50:33 -0500881int
akmhoque902d57e2012-08-17 09:24:38 -0500882init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500883{
akmhoque03004e62012-09-06 01:12:28 -0500884 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
885 {
886 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500887 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500888 }
889 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
890 {
891 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500892 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500893 }
894 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
895 {
896 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500897 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500898 }
akmhoque902d57e2012-08-17 09:24:38 -0500899
akmhoque59980a52012-08-09 12:36:09 -0500900 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500901
902 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500903 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500904 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -0600905 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500906 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500907 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500908 struct hashtb_param param_npt = {0};
909 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
910 struct hashtb_param param_rte = {0};
911 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500912
akmhoque59980a52012-08-09 12:36:09 -0500913 nlsr->in_interest.p = &incoming_interest;
914 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500915
akmhoque03004e62012-09-06 01:12:28 -0500916 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500917
akmhoque03004e62012-09-06 01:12:28 -0500918 char *time_stamp=(char *)malloc(20);
919 memset(time_stamp,0,20);
920 get_current_timestamp_micro(time_stamp);
921 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
922 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
923 free(time_stamp);
924
925 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400926 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500927 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400928 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500929
930
akmhoque902d57e2012-08-17 09:24:38 -0500931
akmhoque53f64222012-09-05 13:57:51 -0500932
akmhoque59980a52012-08-09 12:36:09 -0500933 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500934 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500935 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500936 nlsr->adj_build_count=0;
937 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500938 nlsr->is_send_lsdb_interest_scheduled=0;
939 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500940
akmhoque7b791452012-10-30 11:24:56 -0500941 nlsr->detailed_logging=0;
942 nlsr->debugging=0;
943
akmhoqued79438d2012-08-27 13:31:42 -0500944 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
945 nlsr->interest_retry = INTEREST_RETRY;
946 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500947 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
948 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500949 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueffacaa82012-09-13 17:48:30 -0500950 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500951
akmhoque95041802012-11-16 09:18:02 -0600952 nlsr->api_port=API_PORT;
953
akmhoque81c25e02012-09-10 14:50:33 -0500954 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500955}
956
akmhoque03004e62012-09-06 01:12:28 -0500957
akmhoque902d57e2012-08-17 09:24:38 -0500958int
959main(int argc, char *argv[])
960{
akmhoque81c25e02012-09-10 14:50:33 -0500961 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500962 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500963 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -0600964 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -0500965
akmhoquebfefef22012-09-26 10:09:34 -0500966
akmhoque81c25e02012-09-10 14:50:33 -0500967
akmhoque95041802012-11-16 09:18:02 -0600968 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -0500969 {
970 switch (res)
971 {
972 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500973 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500974 break;
975 case 'f':
976 config_file = optarg;
977 break;
akmhoque95041802012-11-16 09:18:02 -0600978 case 'p':
979 port = atoi(optarg);
980 break;
akmhoque59980a52012-08-09 12:36:09 -0500981 case 'h':
982 default:
983 usage(argv[0]);
984 }
985 }
986
akmhoquebfefef22012-09-26 10:09:34 -0500987 ret=init_nlsr();
988 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -0600989
990 if ( port !=0 )
991 nlsr->api_port=port;
992
akmhoque59980a52012-08-09 12:36:09 -0500993 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500994 if ( daemon_mode == 1 )
995 {
996 daemonize_nlsr();
997 }
998
999 startLogging(nlsr->logDir);
1000
akmhoque59980a52012-08-09 12:36:09 -05001001 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001002 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1003 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001004 {
1005 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001006 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001007 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001008 }
akmhoque1771c412012-11-09 13:06:08 -06001009
1010 init_api_server(ccn_fd);
1011
akmhoque53f64222012-09-05 13:57:51 -05001012 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001013 router_prefix=ccn_charbuf_create();
1014 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001015 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001016 {
1017 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001018 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001019 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001020 }
akmhoque59980a52012-08-09 12:36:09 -05001021
1022 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001023 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001024 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1025 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001026 {
1027 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001028 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001029 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001030 }
1031 ccn_charbuf_destroy(&router_prefix);
1032
akmhoque7b791452012-10-30 11:24:56 -05001033 if ( nlsr->debugging )
1034 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001035 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001036 if ( nlsr->debugging )
1037 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001038 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001039
akmhoque53f64222012-09-05 13:57:51 -05001040 print_name_prefix_from_npl();
1041 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -05001042 build_and_install_name_lsas();
1043 print_name_lsdb();
1044
1045 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001046 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001047 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001048
akmhoque1771c412012-11-09 13:06:08 -06001049
akmhoque59980a52012-08-09 12:36:09 -05001050 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001051 {
akmhoqueffacaa82012-09-13 17:48:30 -05001052 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001053 {
akmhoqueffacaa82012-09-13 17:48:30 -05001054 if( nlsr->sched != NULL )
1055 {
akmhoque1771c412012-11-09 13:06:08 -06001056 long int micro_sec=ccn_schedule_run(nlsr->sched);
1057 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1058 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001059 }
1060 if(nlsr->ccn != NULL)
1061 {
akmhoque1771c412012-11-09 13:06:08 -06001062 res = ccn_run(nlsr->ccn, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001063 }
1064 if (!(nlsr->sched && nlsr->ccn))
1065 {
1066 break;
1067 }
akmhoque29c1db52012-09-07 14:47:43 -05001068 }
1069
akmhoque59980a52012-08-09 12:36:09 -05001070 }
akmhoque1771c412012-11-09 13:06:08 -06001071
akmhoque59980a52012-08-09 12:36:09 -05001072
akmhoque59980a52012-08-09 12:36:09 -05001073 return 0;
1074}
1075