blob: bd2ac397047490dcce8b6c1364994c93605a5df9 [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 }
182
akmhoqued5152122012-09-19 06:44:23 -0500183 if ( rtr_name[strlen(rtr_name)-1] == '/' )
184 {
185 rtr_name[strlen(rtr_name)-1]='\0';
186 }
akmhoque03004e62012-09-06 01:12:28 -0500187 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
188 nbr->name=(char *)malloc(strlen(rtr_name)+1);
189 memset(nbr->name,0,strlen(rtr_name)+1);
190 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
191 nbr->length=strlen(rtr_name)+1;
192
193 add_nbr_to_adl(nbr,face_id);
194
195 free(nbr->name);
196 free(nbr);
197}
198
199void
200process_command_ccnname(char *command)
201{
202
203 if(command==NULL)
204 {
205 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
206 return;
207 }
208 char *rem;
209 const char *sep=" \t\n";
210 char *name;
211 name=strtok_r(command,sep,&rem);
212 if(name==NULL)
213 {
214 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
215 return;
216 }
217
218 printf("Name Prefix: %s \n",name);
219
akmhoqued5152122012-09-19 06:44:23 -0500220 if ( name[strlen(name)-1] == '/' )
221 name[strlen(name)-1]='\0';
222
akmhoque53f64222012-09-05 13:57:51 -0500223 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500224 np->name=(char *)malloc(strlen(name)+1);
225 memset(np->name,0,strlen(name)+1);
226 memcpy(np->name,name,strlen(name)+1);
227 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500228
akmhoque03004e62012-09-06 01:12:28 -0500229 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500230
akmhoque03004e62012-09-06 01:12:28 -0500231 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500232 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500233}
234
akmhoque03004e62012-09-06 01:12:28 -0500235
236void
237process_command_router_name(char *command)
238{
239 if(command==NULL)
240 {
241 printf(" Wrong Command Format ( router-name /router/name )\n");
242 return;
243 }
244 char *rem;
245 const char *sep=" \t\n";
246 char *rtr_name;
247
248 rtr_name=strtok_r(command,sep,&rem);
249 if(rtr_name==NULL)
250 {
251 printf(" Wrong Command Format ( router-name /router/name )\n");
252 return;
253 }
254
255
akmhoqued5152122012-09-19 06:44:23 -0500256 if ( rtr_name[strlen(rtr_name)-1] == '/' )
257 rtr_name[strlen(rtr_name)-1]='\0';
258
akmhoque03004e62012-09-06 01:12:28 -0500259 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
260 memset(nlsr->router_name,0,strlen(rtr_name)+1);
261 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
262
263
264}
265
akmhoque59980a52012-08-09 12:36:09 -0500266void
akmhoqued79438d2012-08-27 13:31:42 -0500267process_command_lsdb_synch_interval(char *command)
268{
269 if(command==NULL)
270 {
271 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
272 return;
273 }
274 char *rem;
275 const char *sep=" \t\n";
276 char *secs;
277 long int seconds;
278
279 secs=strtok_r(command,sep,&rem);
280 if(secs==NULL)
281 {
282 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
283 return;
284 }
285
286 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500287 if ( seconds >= 120 && seconds <= 3600 )
288 {
289 nlsr->lsdb_synch_interval=seconds;
290 }
akmhoqued79438d2012-08-27 13:31:42 -0500291
292}
293
294
295void
296process_command_interest_retry(char *command)
297{
298 if(command==NULL)
299 {
300 printf(" Wrong Command Format ( interest-retry number )\n");
301 return;
302 }
303 char *rem;
304 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500305 char *retry;
306 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500307
akmhoqueffacaa82012-09-13 17:48:30 -0500308 retry=strtok_r(command,sep,&rem);
309 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500310 {
311 printf(" Wrong Command Format ( interest-retry number)\n");
312 return;
313 }
314
akmhoqueffacaa82012-09-13 17:48:30 -0500315 retry_number=atoi(retry);
316 if ( retry_number >= 1 && retry_number<=10 )
317 {
318 nlsr->interest_retry=retry_number;
319 }
akmhoqued79438d2012-08-27 13:31:42 -0500320
321}
322
323void
324process_command_interest_resend_time(char *command)
325{
326 if(command==NULL)
327 {
328 printf(" Wrong Command Format ( interest-resend-time secs )\n");
329 return;
330 }
331 char *rem;
332 const char *sep=" \t\n";
333 char *secs;
334 long int seconds;
335
336 secs=strtok_r(command,sep,&rem);
337 if(secs==NULL)
338 {
339 printf(" Wrong Command Format ( interest-resend-time secs)\n");
340 return;
341 }
342
343 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500344 if ( seconds <= 60 && seconds >= 1 )
345 {
346 nlsr->interest_resend_time=seconds;
347 }
akmhoqued79438d2012-08-27 13:31:42 -0500348}
349
akmhoque03004e62012-09-06 01:12:28 -0500350
akmhoqued5152122012-09-19 06:44:23 -0500351void
352process_command_lsa_refresh_time(char *command)
353{
354 if(command==NULL)
355 {
356 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
357 return;
358 }
359 char *rem;
360 const char *sep=" \t\n";
361 char *secs;
362 long int seconds;
363
364 secs=strtok_r(command,sep,&rem);
365 if(secs==NULL)
366 {
367 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
368 return;
369 }
370
371 seconds=atoi(secs);
372 if ( seconds >= 240 && seconds <= 3600 )
373 {
374 nlsr->lsa_refresh_time=seconds;
375 }
376
377}
378
379void
380process_command_router_dead_interval(char *command)
381{
382 if(command==NULL)
383 {
384 printf(" Wrong Command Format ( router-dead-interval secs )\n");
385 return;
386 }
387 char *rem;
388 const char *sep=" \t\n";
389 char *secs;
390 long int seconds;
391
392 secs=strtok_r(command,sep,&rem);
393 if(secs==NULL)
394 {
395 printf(" Wrong Command Format ( router-dead-interval secs)\n");
396 return;
397 }
398
399 seconds=atoi(secs);
400 if ( seconds >= 360 && seconds <= 5400 )
401 {
402 nlsr->router_dead_interval=seconds;
403 }
404
405}
akmhoque03004e62012-09-06 01:12:28 -0500406
akmhoqued79438d2012-08-27 13:31:42 -0500407void
akmhoque3cced642012-09-24 16:20:20 -0500408process_command_multi_path_face_num(char *command)
409{
410 if(command==NULL)
411 {
412 printf(" Wrong Command Format ( multi-path-face-num n )\n");
413 return;
414 }
415 char *rem;
416 const char *sep=" \t\n";
417 char *num;
418 long int number;
419
420 num=strtok_r(command,sep,&rem);
421 if(num==NULL)
422 {
423 printf(" Wrong Command Format ( multi-path-face-num n)\n");
424 return;
425 }
426
427 number=atoi(num);
428 if ( number >= 0 && number <= 60 )
429 {
430 nlsr->multi_path_face_num=number;
431 }
432
433}
434
435void
akmhoquebfefef22012-09-26 10:09:34 -0500436process_command_logdir(char *command)
437{
438 if(command==NULL)
439 {
440 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
441 return;
442 }
443 char *rem;
444 const char *sep=" \t\n";
445 char *dir;
446
447 dir=strtok_r(command,sep,&rem);
448 if(dir==NULL)
449 {
450 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
451 return;
452 }
453
454 nlsr->logDir=(char *)malloc(strlen(dir)+1);
455 memset(nlsr->logDir,0,strlen(dir)+1);
456 memcpy(nlsr->logDir,dir,strlen(dir));
457}
458
459void
akmhoque7b791452012-10-30 11:24:56 -0500460process_command_detailed_log(char *command)
461{
462 if(command==NULL)
463 {
464 printf(" Wrong Command Format ( detailed-log on/off )\n");
465 return;
466 }
467 char *rem;
468 const char *sep=" \t\n";
469 char *on_off;
470
471 on_off=strtok_r(command,sep,&rem);
472 if(on_off==NULL)
473 {
474 printf(" Wrong Command Format ( detailed-log on/off )\n");
475 return;
476 }
477
478 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
479 {
480 nlsr->detailed_logging=1;
481 }
482}
483
484void
485process_command_debug(char *command)
486{
487 if(command==NULL)
488 {
489 printf(" Wrong Command Format ( debug on/off )\n");
490 return;
491 }
492 char *rem;
493 const char *sep=" \t\n";
494 char *on_off;
495
496 on_off=strtok_r(command,sep,&rem);
497 if(on_off==NULL)
498 {
499 printf(" Wrong Command Format ( debug on/off )\n");
500 return;
501 }
502
503 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
504 {
505 nlsr->debugging=1;
506 }
507}
508
509void
akmhoque59980a52012-08-09 12:36:09 -0500510process_conf_command(char *command)
511{
512 const char *separators=" \t\n";
513 char *remainder=NULL;
514 char *cmd_type=NULL;
515
516 if(command==NULL || strlen(command)==0 || command[0]=='!')
517 return;
518
519 cmd_type=strtok_r(command,separators,&remainder);
520
521 if(!strcmp(cmd_type,"router-name") )
522 {
523 process_command_router_name(remainder);
524 }
525 else if(!strcmp(cmd_type,"ccnneighbor") )
526 {
527 process_command_ccnneighbor(remainder);
528 }
529 else if(!strcmp(cmd_type,"ccnname") )
530 {
531 process_command_ccnname(remainder);
532 }
akmhoqued79438d2012-08-27 13:31:42 -0500533 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
534 {
535 process_command_lsdb_synch_interval(remainder);
536 }
537 else if(!strcmp(cmd_type,"interest-retry") )
538 {
539 process_command_interest_retry(remainder);
540 }
541 else if(!strcmp(cmd_type,"interest-resend-time") )
542 {
543 process_command_interest_resend_time(remainder);
544 }
akmhoqued5152122012-09-19 06:44:23 -0500545 else if(!strcmp(cmd_type,"lsa-refresh-time") )
546 {
547 process_command_lsa_refresh_time(remainder);
548 }
549 else if(!strcmp(cmd_type,"router-dead-interval") )
550 {
551 process_command_router_dead_interval(remainder);
552 }
akmhoque3cced642012-09-24 16:20:20 -0500553 else if(!strcmp(cmd_type,"multi-path-face-num") )
554 {
555 process_command_multi_path_face_num(remainder);
556 }
akmhoquebfefef22012-09-26 10:09:34 -0500557 else if(!strcmp(cmd_type,"logdir") )
558 {
559 process_command_logdir(remainder);
560 }
akmhoque7b791452012-10-30 11:24:56 -0500561 else if(!strcmp(cmd_type,"detailed-log") )
562 {
563 process_command_detailed_log(remainder);
564 }
565 else if(!strcmp(cmd_type,"debug") )
566 {
567 process_command_debug(remainder);
568 }
akmhoqued5152122012-09-19 06:44:23 -0500569 else
akmhoque59980a52012-08-09 12:36:09 -0500570 {
571 printf("Wrong configuration Command %s \n",cmd_type);
572 }
573}
574
akmhoque03004e62012-09-06 01:12:28 -0500575
akmhoque59980a52012-08-09 12:36:09 -0500576int
577readConfigFile(const char *filename)
578{
579 FILE *cfg;
580 char buf[1024];
581 int len;
582
583 cfg=fopen(filename, "r");
584
585 if(cfg == NULL)
586 {
587 printf("\nConfiguration File does not exists\n");
588 exit(1);
589 }
590
591 while(fgets((char *)buf, sizeof(buf), cfg))
592 {
593 len=strlen(buf);
594 if(buf[len-1] == '\n')
595 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500596 if ( buf[0] != '#' && buf[0] != '!')
597 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500598 }
599
600 fclose(cfg);
601
602 return 0;
603}
604
akmhoque562caef2012-11-09 13:29:06 -0600605char *
606process_api_client_command(char *command)
607{
608 char *msg;
609 msg=(char *)malloc(100);
610 memset(msg,100,0);
akmhoque3171d652012-11-13 11:44:33 -0600611 //strcpy(msg,"Action Carried Out for NLSR Api Client");
akmhoque562caef2012-11-09 13:29:06 -0600612
akmhoque3171d652012-11-13 11:44:33 -0600613 const char *sep=" \t\n";
614 char *rem=NULL;
615 char *cmd_type=NULL;
616 char *op_type=NULL;
617 char *name=NULL;
618 char *face=NULL;
619 int face_id;
620 int res;
621
622 op_type=strtok_r(command,sep,&rem);
623 cmd_type=strtok_r(NULL,sep,&rem);
624 name=strtok_r(NULL,sep,&rem);
625 if ( name[strlen(name)-1] == '/' )
626 name[strlen(name)-1]='\0';
627
628 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
629 np->name=(char *)malloc(strlen(name)+1);
630 memset(np->name,0,strlen(name)+1);
631 memcpy(np->name,name,strlen(name)+1);
632 np->length=strlen(name)+1;
633
634 if ( strcmp(cmd_type,"name")!= 0 )
635 {
636 face=strtok_r(NULL,sep,&rem);
637 sscanf(face,"face%d",&face_id);
638 }
akmhoque562caef2012-11-09 13:29:06 -0600639
akmhoque3171d652012-11-13 11:44:33 -0600640 if ( strcmp(cmd_type,"name")== 0 )
641 {
642 if ( strcmp(op_type,"del") == 0 )
643 {
644 res=does_name_exist_in_npl(np);
645 if ( res == 0)
646 {
647 sprintf(msg,"Name %s does not exist !!",name);
648 }
649 else
650 {
651 long int ls_id=get_lsa_id_from_npl(np);
652 if ( ls_id != 0 )
653 {
654 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
655 sprintf(msg,"Name %s has been deleted and Advertised.",name);
656 }
657 else
658 {
659 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
660 }
661 }
662 }
663 else if ( strcmp(op_type,"add") == 0 )
664 {
665 res=does_name_exist_in_npl(np);
666 if ( res == 0)
667 {
668 add_name_to_npl(np);
669 build_and_install_single_name_lsa(np);
670 sprintf(msg,"Name %s has been added to advertise.",name);
671 }
672 else
673 {
674 sprintf(msg,"Name %s has already been advertised from this router !!",name);
675 }
676 }
677 }
678 else if ( strcmp(cmd_type,"neighbor") == 0 )
679 {
680 if ( strcmp(op_type,"del") == 0 )
681 {
682 res=is_neighbor(np->name);
683 if ( res == 0)
684 {
685 sprintf(msg,"Neighbor %s does not exist !!",name);
686 }
687 else
688 {
689 update_adjacent_status_to_adl(np,NBR_DOWN);
690 delete_nbr_from_adl(np);
691 if(!nlsr->is_build_adj_lsa_sheduled)
692 {
693 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
694 nlsr->is_build_adj_lsa_sheduled=1;
695 }
696 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
697 }
698 }
699 else if ( strcmp(op_type,"add") == 0 )
700 {
701 res=is_neighbor(np->name);
702 if ( res == 0 )
703 {
704 add_nbr_to_adl(np,face_id);
705 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
706 }
707 else
708 {
709 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
710 }
711 }
712 }
713
akmhoque562caef2012-11-09 13:29:06 -0600714
715 return msg;
716}
akmhoque1771c412012-11-09 13:06:08 -0600717
718int
719nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
720{
721 struct timeval timeout;
722 if ( time_out_micro_sec < 0 )
723 {
724 timeout.tv_sec=1;
725 timeout.tv_usec=0;
726 }
727 else
728 {
729 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
730 timeout.tv_sec=time_out_micro_sec / 1000000;
731 timeout.tv_usec=time_out_micro_sec % 1000000;
732 }
733
734
735 int fd;
736 int nread;
737 int result;
738 fd_set testfds;
739 unsigned int client_len;
740 int client_sockfd;
741 char recv_buffer[1024];
742 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -0600743 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -0600744
745 testfds=nlsr->readfds;
746 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
747
748 for(fd = 0; fd < FD_SETSIZE; fd++)
749 {
750 if(FD_ISSET(fd,&testfds))
751 {
752 if ( fd == ccn_fd )
753 {
754 return 0;
755 }
756 else if(fd == nlsr->nlsr_api_server_sock_fd)
757 {
Adam Alyyanb5fff372013-01-09 14:32:52 -0600758 printf("Setting up socket....\n");
akmhoque1771c412012-11-09 13:06:08 -0600759 client_len = sizeof(client_address);
760 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
761 FD_SET(client_sockfd, &nlsr->readfds);
762 }
763 else
764 {
Adam Alyyanb5fff372013-01-09 14:32:52 -0600765 printf("Else...\n");
akmhoque1771c412012-11-09 13:06:08 -0600766 ioctl(fd, FIONREAD, &nread);
767 if(nread == 0)
768 {
769 close(fd);
770 FD_CLR(fd, &nlsr->readfds);
771 }
772 else
773 {
774 recv(fd, recv_buffer, 1024, 0);
Adam Alyyanb5fff372013-01-09 14:32:52 -0600775 printf("Test Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -0600776 char *msg=process_api_client_command(recv_buffer);
777 send(fd, msg, strlen(msg),0);
778 free(msg);
akmhoque1771c412012-11-09 13:06:08 -0600779 close(fd);
780 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -0600781 }
782 }
783 }
784 }
785
786 return 0;
787}
788
akmhoque386081b2012-08-10 10:53:21 -0500789void
790nlsr_destroy( void )
791{
akmhoque7b791452012-10-30 11:24:56 -0500792 if ( nlsr->debugging )
793 {
794 printf("Freeing Allocated Memory....\n");
795 }
akmhoque9e9fc722012-09-26 14:03:25 -0500796 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500797 /* Destroying all face created by nlsr in CCND */
798 destroy_all_face_by_nlsr();
799
akmhoque386081b2012-08-10 10:53:21 -0500800 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500801 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500802 hashtb_destroy(&nlsr->adl);
803 hashtb_destroy(&nlsr->lsdb->name_lsdb);
804 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500805 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500806
807 //To Do: has to destroy the face_list one by one
808
akmhoque3560cb62012-09-09 10:52:30 -0500809 hashtb_destroy(&nlsr->routing_table);
810
811
812 int i, npt_element;
813 struct npt_entry *ne;
814 struct hashtb_enumerator ee;
815 struct hashtb_enumerator *e = &ee;
816 hashtb_start(nlsr->npt, e);
817 npt_element=hashtb_n(nlsr->npt);
818 for(i=0;i<npt_element;i++)
819 {
820 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500821 hashtb_destroy(&ne->name_list);
822 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500823 hashtb_next(e);
824 }
825
826 hashtb_end(e);
827 hashtb_destroy(&nlsr->npt);
828
akmhoque95041802012-11-16 09:18:02 -0600829
830 close(nlsr->nlsr_api_server_sock_fd);
831
akmhoque386081b2012-08-10 10:53:21 -0500832 ccn_schedule_destroy(&nlsr->sched);
833 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500834
835 free(nlsr->lsdb->lsdb_version);
836 free(nlsr->lsdb);
837 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500838 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -0500839 if ( nlsr->debugging )
840 {
841 printf("Finished freeing allocated memory\n");
842 }
akmhoque9e9fc722012-09-26 14:03:25 -0500843 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -0500844
akmhoque386081b2012-08-10 10:53:21 -0500845}
846
akmhoque03004e62012-09-06 01:12:28 -0500847
akmhoque1771c412012-11-09 13:06:08 -0600848void
849init_api_server(int ccn_fd)
850{
851 int server_sockfd;
852 int server_len;
akmhoque95041802012-11-16 09:18:02 -0600853 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -0600854 unsigned int yes=1;
855
akmhoque95041802012-11-16 09:18:02 -0600856 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -0600857
858 int flags = fcntl(server_sockfd, F_GETFL, 0);
859 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
860
akmhoquef31f13b2012-11-16 09:42:24 -0600861 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
862 {
863 ON_ERROR_DESTROY(-1);
864 }
akmhoque95041802012-11-16 09:18:02 -0600865
866 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -0600867 //server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
868 server_address.sin_addr.s_addr = INADDR_ANY;
869 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -0600870
akmhoque1771c412012-11-09 13:06:08 -0600871 server_len = sizeof(server_address);
872 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
Adam Alyyanb5fff372013-01-09 14:32:52 -0600873 //printf("port number %d\n", ntohs(server_address.sin_port));
akmhoque1771c412012-11-09 13:06:08 -0600874 listen(server_sockfd, 100);
875 FD_ZERO(&nlsr->readfds);
876 FD_SET(server_sockfd, &nlsr->readfds);
877 FD_SET(ccn_fd, &nlsr->readfds);
878 nlsr->nlsr_api_server_sock_fd=server_sockfd;
879
880}
881
akmhoque81c25e02012-09-10 14:50:33 -0500882int
akmhoque902d57e2012-08-17 09:24:38 -0500883init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500884{
akmhoque03004e62012-09-06 01:12:28 -0500885 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
886 {
887 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500888 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500889 }
890 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
891 {
892 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500893 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500894 }
895 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
896 {
897 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500898 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500899 }
akmhoque902d57e2012-08-17 09:24:38 -0500900
akmhoque59980a52012-08-09 12:36:09 -0500901 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500902
903 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500904 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500905 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -0600906 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500907 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500908 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500909 struct hashtb_param param_npt = {0};
910 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
911 struct hashtb_param param_rte = {0};
912 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500913
akmhoque59980a52012-08-09 12:36:09 -0500914 nlsr->in_interest.p = &incoming_interest;
915 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500916
akmhoque03004e62012-09-06 01:12:28 -0500917 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500918
akmhoque03004e62012-09-06 01:12:28 -0500919 char *time_stamp=(char *)malloc(20);
920 memset(time_stamp,0,20);
921 get_current_timestamp_micro(time_stamp);
922 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
923 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
924 free(time_stamp);
925
926 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400927 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500928 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400929 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500930
931
akmhoque902d57e2012-08-17 09:24:38 -0500932
akmhoque53f64222012-09-05 13:57:51 -0500933
akmhoque59980a52012-08-09 12:36:09 -0500934 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500935 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500936 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500937 nlsr->adj_build_count=0;
938 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500939 nlsr->is_send_lsdb_interest_scheduled=0;
940 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500941
akmhoque7b791452012-10-30 11:24:56 -0500942 nlsr->detailed_logging=0;
943 nlsr->debugging=0;
944
akmhoqued79438d2012-08-27 13:31:42 -0500945 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
946 nlsr->interest_retry = INTEREST_RETRY;
947 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500948 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
949 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500950 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueffacaa82012-09-13 17:48:30 -0500951 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500952
akmhoque95041802012-11-16 09:18:02 -0600953 nlsr->api_port=API_PORT;
954
akmhoque81c25e02012-09-10 14:50:33 -0500955 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500956}
957
akmhoque03004e62012-09-06 01:12:28 -0500958
akmhoque902d57e2012-08-17 09:24:38 -0500959int
960main(int argc, char *argv[])
961{
akmhoque81c25e02012-09-10 14:50:33 -0500962 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500963 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500964 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -0600965 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -0500966
akmhoquebfefef22012-09-26 10:09:34 -0500967
akmhoque81c25e02012-09-10 14:50:33 -0500968
akmhoque95041802012-11-16 09:18:02 -0600969 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -0500970 {
971 switch (res)
972 {
973 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500974 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500975 break;
976 case 'f':
977 config_file = optarg;
978 break;
akmhoque95041802012-11-16 09:18:02 -0600979 case 'p':
980 port = atoi(optarg);
981 break;
akmhoque59980a52012-08-09 12:36:09 -0500982 case 'h':
983 default:
984 usage(argv[0]);
985 }
986 }
987
akmhoquebfefef22012-09-26 10:09:34 -0500988 ret=init_nlsr();
989 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -0600990
991 if ( port !=0 )
992 nlsr->api_port=port;
993
akmhoque59980a52012-08-09 12:36:09 -0500994 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500995 if ( daemon_mode == 1 )
996 {
997 daemonize_nlsr();
998 }
999
1000 startLogging(nlsr->logDir);
1001
akmhoque59980a52012-08-09 12:36:09 -05001002 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001003 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1004 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001005 {
1006 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001007 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001008 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001009 }
akmhoque1771c412012-11-09 13:06:08 -06001010
1011 init_api_server(ccn_fd);
1012
akmhoque53f64222012-09-05 13:57:51 -05001013 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001014 router_prefix=ccn_charbuf_create();
1015 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001016 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001017 {
1018 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001019 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001020 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001021 }
akmhoque59980a52012-08-09 12:36:09 -05001022
1023 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001024 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001025 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1026 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001027 {
1028 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001029 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001030 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001031 }
1032 ccn_charbuf_destroy(&router_prefix);
1033
akmhoque7b791452012-10-30 11:24:56 -05001034 if ( nlsr->debugging )
1035 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001036 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001037 if ( nlsr->debugging )
1038 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001039 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001040
akmhoque53f64222012-09-05 13:57:51 -05001041 print_name_prefix_from_npl();
1042 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -05001043 build_and_install_name_lsas();
1044 print_name_lsdb();
1045
1046 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001047 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001048 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001049
akmhoque1771c412012-11-09 13:06:08 -06001050
akmhoque59980a52012-08-09 12:36:09 -05001051 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001052 {
akmhoqueffacaa82012-09-13 17:48:30 -05001053 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001054 {
akmhoqueffacaa82012-09-13 17:48:30 -05001055 if( nlsr->sched != NULL )
1056 {
akmhoque1771c412012-11-09 13:06:08 -06001057 long int micro_sec=ccn_schedule_run(nlsr->sched);
1058 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1059 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001060 }
1061 if(nlsr->ccn != NULL)
1062 {
akmhoque1771c412012-11-09 13:06:08 -06001063 res = ccn_run(nlsr->ccn, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001064 }
1065 if (!(nlsr->sched && nlsr->ccn))
1066 {
1067 break;
1068 }
akmhoque29c1db52012-09-07 14:47:43 -05001069 }
1070
akmhoque59980a52012-08-09 12:36:09 -05001071 }
akmhoque1771c412012-11-09 13:06:08 -06001072
akmhoque59980a52012-08-09 12:36:09 -05001073
akmhoque59980a52012-08-09 12:36:09 -05001074 return 0;
1075}
1076