blob: f9835578ec30db3ab6fafacc6e7a8de72f1f584d [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);
606 strcpy(msg,"Action Carried Out for NLSR Api Client");
607
608
609
610 return msg;
611}
akmhoque1771c412012-11-09 13:06:08 -0600612
613int
614nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
615{
616 struct timeval timeout;
617 if ( time_out_micro_sec < 0 )
618 {
619 timeout.tv_sec=1;
620 timeout.tv_usec=0;
621 }
622 else
623 {
624 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
625 timeout.tv_sec=time_out_micro_sec / 1000000;
626 timeout.tv_usec=time_out_micro_sec % 1000000;
627 }
628
629
630 int fd;
631 int nread;
632 int result;
633 fd_set testfds;
634 unsigned int client_len;
635 int client_sockfd;
636 char recv_buffer[1024];
637 bzero(recv_buffer,1024);
638 struct sockaddr_un client_address;
639
640 testfds=nlsr->readfds;
641 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
642
643 for(fd = 0; fd < FD_SETSIZE; fd++)
644 {
645 if(FD_ISSET(fd,&testfds))
646 {
647 if ( fd == ccn_fd )
648 {
649 return 0;
650 }
651 else if(fd == nlsr->nlsr_api_server_sock_fd)
652 {
653 client_len = sizeof(client_address);
654 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
655 FD_SET(client_sockfd, &nlsr->readfds);
656 }
657 else
658 {
659
660 ioctl(fd, FIONREAD, &nread);
661 if(nread == 0)
662 {
663 close(fd);
664 FD_CLR(fd, &nlsr->readfds);
665 }
666 else
667 {
668 recv(fd, recv_buffer, 1024, 0);
669 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -0600670 char *msg=process_api_client_command(recv_buffer);
671 send(fd, msg, strlen(msg),0);
672 free(msg);
akmhoque1771c412012-11-09 13:06:08 -0600673 close(fd);
674 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -0600675 }
676 }
677 }
678 }
679
680 return 0;
681}
682
akmhoque386081b2012-08-10 10:53:21 -0500683void
684nlsr_destroy( void )
685{
akmhoque7b791452012-10-30 11:24:56 -0500686 if ( nlsr->debugging )
687 {
688 printf("Freeing Allocated Memory....\n");
689 }
akmhoque9e9fc722012-09-26 14:03:25 -0500690 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500691 /* Destroying all face created by nlsr in CCND */
692 destroy_all_face_by_nlsr();
693
akmhoque386081b2012-08-10 10:53:21 -0500694 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500695 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500696 hashtb_destroy(&nlsr->adl);
697 hashtb_destroy(&nlsr->lsdb->name_lsdb);
698 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500699 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500700
701 //To Do: has to destroy the face_list one by one
702
akmhoque3560cb62012-09-09 10:52:30 -0500703 hashtb_destroy(&nlsr->routing_table);
704
705
706 int i, npt_element;
707 struct npt_entry *ne;
708 struct hashtb_enumerator ee;
709 struct hashtb_enumerator *e = &ee;
710 hashtb_start(nlsr->npt, e);
711 npt_element=hashtb_n(nlsr->npt);
712 for(i=0;i<npt_element;i++)
713 {
714 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500715 hashtb_destroy(&ne->name_list);
716 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500717 hashtb_next(e);
718 }
719
720 hashtb_end(e);
721 hashtb_destroy(&nlsr->npt);
722
akmhoque03004e62012-09-06 01:12:28 -0500723
akmhoque386081b2012-08-10 10:53:21 -0500724 ccn_schedule_destroy(&nlsr->sched);
725 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500726
727 free(nlsr->lsdb->lsdb_version);
728 free(nlsr->lsdb);
729 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500730 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -0500731 if ( nlsr->debugging )
732 {
733 printf("Finished freeing allocated memory\n");
734 }
akmhoque9e9fc722012-09-26 14:03:25 -0500735 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -0500736
akmhoque386081b2012-08-10 10:53:21 -0500737}
738
akmhoque03004e62012-09-06 01:12:28 -0500739
akmhoque1771c412012-11-09 13:06:08 -0600740void
741init_api_server(int ccn_fd)
742{
743 int server_sockfd;
744 int server_len;
745 struct sockaddr_un server_address;
746
747 unlink("/tmp/nlsr_api_server_socket");
748 server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
749
750 int flags = fcntl(server_sockfd, F_GETFL, 0);
751 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
752
753 server_address.sun_family = AF_UNIX;
754 strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket");
755 server_len = sizeof(server_address);
756 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
757 listen(server_sockfd, 100);
758 FD_ZERO(&nlsr->readfds);
759 FD_SET(server_sockfd, &nlsr->readfds);
760 FD_SET(ccn_fd, &nlsr->readfds);
761 nlsr->nlsr_api_server_sock_fd=server_sockfd;
762
763}
764
akmhoque81c25e02012-09-10 14:50:33 -0500765int
akmhoque902d57e2012-08-17 09:24:38 -0500766init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500767{
akmhoque03004e62012-09-06 01:12:28 -0500768 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
769 {
770 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500771 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500772 }
773 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
774 {
775 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500776 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500777 }
778 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
779 {
780 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500781 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500782 }
akmhoque902d57e2012-08-17 09:24:38 -0500783
akmhoque59980a52012-08-09 12:36:09 -0500784 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500785
786 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500787 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500788 struct hashtb_param param_npl = {0};
789 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500790 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500791 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500792 struct hashtb_param param_npt = {0};
793 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
794 struct hashtb_param param_rte = {0};
795 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500796
akmhoque59980a52012-08-09 12:36:09 -0500797 nlsr->in_interest.p = &incoming_interest;
798 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500799
akmhoque03004e62012-09-06 01:12:28 -0500800 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500801
akmhoque03004e62012-09-06 01:12:28 -0500802 char *time_stamp=(char *)malloc(20);
803 memset(time_stamp,0,20);
804 get_current_timestamp_micro(time_stamp);
805 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
806 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
807 free(time_stamp);
808
809 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400810 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500811 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400812 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500813
814
akmhoque902d57e2012-08-17 09:24:38 -0500815
akmhoque53f64222012-09-05 13:57:51 -0500816
akmhoque59980a52012-08-09 12:36:09 -0500817 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500818 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500819 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500820 nlsr->adj_build_count=0;
821 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500822 nlsr->is_send_lsdb_interest_scheduled=0;
823 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500824
akmhoque7b791452012-10-30 11:24:56 -0500825 nlsr->detailed_logging=0;
826 nlsr->debugging=0;
827
akmhoqued79438d2012-08-27 13:31:42 -0500828 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
829 nlsr->interest_retry = INTEREST_RETRY;
830 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500831 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
832 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500833 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueffacaa82012-09-13 17:48:30 -0500834 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500835
836 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500837}
838
akmhoque03004e62012-09-06 01:12:28 -0500839
akmhoque902d57e2012-08-17 09:24:38 -0500840int
841main(int argc, char *argv[])
842{
akmhoque81c25e02012-09-10 14:50:33 -0500843 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500844 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500845 int daemon_mode=0;
akmhoque902d57e2012-08-17 09:24:38 -0500846
akmhoquebfefef22012-09-26 10:09:34 -0500847
akmhoque81c25e02012-09-10 14:50:33 -0500848
akmhoque59980a52012-08-09 12:36:09 -0500849 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
850 {
851 switch (res)
852 {
853 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500854 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500855 break;
856 case 'f':
857 config_file = optarg;
858 break;
859 case 'h':
860 default:
861 usage(argv[0]);
862 }
863 }
864
akmhoquebfefef22012-09-26 10:09:34 -0500865 ret=init_nlsr();
866 ON_ERROR_EXIT(ret);
akmhoque59980a52012-08-09 12:36:09 -0500867 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500868
869 if ( daemon_mode == 1 )
870 {
871 daemonize_nlsr();
872 }
873
874 startLogging(nlsr->logDir);
875
akmhoque59980a52012-08-09 12:36:09 -0500876 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -0600877 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
878 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -0500879 {
880 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -0500881 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -0500882 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -0500883 }
akmhoque1771c412012-11-09 13:06:08 -0600884
885 init_api_server(ccn_fd);
886
akmhoque53f64222012-09-05 13:57:51 -0500887 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500888 router_prefix=ccn_charbuf_create();
889 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500890 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500891 {
892 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -0500893 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -0500894 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500895 }
akmhoque59980a52012-08-09 12:36:09 -0500896
897 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500898 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500899 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
900 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500901 {
902 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -0500903 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -0500904 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500905 }
906 ccn_charbuf_destroy(&router_prefix);
907
akmhoque7b791452012-10-30 11:24:56 -0500908 if ( nlsr->debugging )
909 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -0500910 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -0500911 if ( nlsr->debugging )
912 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -0500913 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500914
akmhoque53f64222012-09-05 13:57:51 -0500915 print_name_prefix_from_npl();
916 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500917 build_and_install_name_lsas();
918 print_name_lsdb();
919
920 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500921 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500922 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500923
akmhoque1771c412012-11-09 13:06:08 -0600924
akmhoque59980a52012-08-09 12:36:09 -0500925 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500926 {
akmhoqueffacaa82012-09-13 17:48:30 -0500927 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -0500928 {
akmhoqueffacaa82012-09-13 17:48:30 -0500929 if( nlsr->sched != NULL )
930 {
akmhoque1771c412012-11-09 13:06:08 -0600931 long int micro_sec=ccn_schedule_run(nlsr->sched);
932 res=nlsr_api_server_poll(micro_sec,ccn_fd);
933 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -0500934 }
935 if(nlsr->ccn != NULL)
936 {
akmhoque1771c412012-11-09 13:06:08 -0600937 res = ccn_run(nlsr->ccn, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500938 }
939 if (!(nlsr->sched && nlsr->ccn))
940 {
941 break;
942 }
akmhoque29c1db52012-09-07 14:47:43 -0500943 }
944
akmhoque59980a52012-08-09 12:36:09 -0500945 }
akmhoque1771c412012-11-09 13:06:08 -0600946
akmhoque59980a52012-08-09 12:36:09 -0500947
akmhoque59980a52012-08-09 12:36:09 -0500948 return 0;
949}
950