blob: 189e11fb651ba82238a74a7c8e4829890f81fd7d [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
akmhoque1771c412012-11-09 13:06:08 -0600600
601int
602nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
603{
604 struct timeval timeout;
605 if ( time_out_micro_sec < 0 )
606 {
607 timeout.tv_sec=1;
608 timeout.tv_usec=0;
609 }
610 else
611 {
612 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
613 timeout.tv_sec=time_out_micro_sec / 1000000;
614 timeout.tv_usec=time_out_micro_sec % 1000000;
615 }
616
617
618 int fd;
619 int nread;
620 int result;
621 fd_set testfds;
622 unsigned int client_len;
623 int client_sockfd;
624 char recv_buffer[1024];
625 bzero(recv_buffer,1024);
626 struct sockaddr_un client_address;
627
628 testfds=nlsr->readfds;
629 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
630
631 for(fd = 0; fd < FD_SETSIZE; fd++)
632 {
633 if(FD_ISSET(fd,&testfds))
634 {
635 if ( fd == ccn_fd )
636 {
637 return 0;
638 }
639 else if(fd == nlsr->nlsr_api_server_sock_fd)
640 {
641 client_len = sizeof(client_address);
642 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
643 FD_SET(client_sockfd, &nlsr->readfds);
644 }
645 else
646 {
647
648 ioctl(fd, FIONREAD, &nread);
649 if(nread == 0)
650 {
651 close(fd);
652 FD_CLR(fd, &nlsr->readfds);
653 }
654 else
655 {
656 recv(fd, recv_buffer, 1024, 0);
657 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
658 close(fd);
659 FD_CLR(fd, &nlsr->readfds);
660 free(recv_buffer);
661 }
662 }
663 }
664 }
665
666 return 0;
667}
668
akmhoque386081b2012-08-10 10:53:21 -0500669void
670nlsr_destroy( void )
671{
akmhoque7b791452012-10-30 11:24:56 -0500672 if ( nlsr->debugging )
673 {
674 printf("Freeing Allocated Memory....\n");
675 }
akmhoque9e9fc722012-09-26 14:03:25 -0500676 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -0500677 /* Destroying all face created by nlsr in CCND */
678 destroy_all_face_by_nlsr();
679
akmhoque386081b2012-08-10 10:53:21 -0500680 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -0500681 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -0500682 hashtb_destroy(&nlsr->adl);
683 hashtb_destroy(&nlsr->lsdb->name_lsdb);
684 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500685 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -0500686
687 //To Do: has to destroy the face_list one by one
688
akmhoque3560cb62012-09-09 10:52:30 -0500689 hashtb_destroy(&nlsr->routing_table);
690
691
692 int i, npt_element;
693 struct npt_entry *ne;
694 struct hashtb_enumerator ee;
695 struct hashtb_enumerator *e = &ee;
696 hashtb_start(nlsr->npt, e);
697 npt_element=hashtb_n(nlsr->npt);
698 for(i=0;i<npt_element;i++)
699 {
700 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -0500701 hashtb_destroy(&ne->name_list);
702 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -0500703 hashtb_next(e);
704 }
705
706 hashtb_end(e);
707 hashtb_destroy(&nlsr->npt);
708
akmhoque03004e62012-09-06 01:12:28 -0500709
akmhoque386081b2012-08-10 10:53:21 -0500710 ccn_schedule_destroy(&nlsr->sched);
711 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -0500712
713 free(nlsr->lsdb->lsdb_version);
714 free(nlsr->lsdb);
715 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -0500716 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -0500717 if ( nlsr->debugging )
718 {
719 printf("Finished freeing allocated memory\n");
720 }
akmhoque9e9fc722012-09-26 14:03:25 -0500721 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -0500722
akmhoque386081b2012-08-10 10:53:21 -0500723}
724
akmhoque03004e62012-09-06 01:12:28 -0500725
akmhoque1771c412012-11-09 13:06:08 -0600726void
727init_api_server(int ccn_fd)
728{
729 int server_sockfd;
730 int server_len;
731 struct sockaddr_un server_address;
732
733 unlink("/tmp/nlsr_api_server_socket");
734 server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
735
736 int flags = fcntl(server_sockfd, F_GETFL, 0);
737 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
738
739 server_address.sun_family = AF_UNIX;
740 strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket");
741 server_len = sizeof(server_address);
742 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
743 listen(server_sockfd, 100);
744 FD_ZERO(&nlsr->readfds);
745 FD_SET(server_sockfd, &nlsr->readfds);
746 FD_SET(ccn_fd, &nlsr->readfds);
747 nlsr->nlsr_api_server_sock_fd=server_sockfd;
748
749}
750
akmhoque81c25e02012-09-10 14:50:33 -0500751int
akmhoque902d57e2012-08-17 09:24:38 -0500752init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -0500753{
akmhoque03004e62012-09-06 01:12:28 -0500754 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
755 {
756 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500757 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500758 }
759 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
760 {
761 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500762 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500763 }
764 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
765 {
766 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -0500767 return -1;
akmhoque03004e62012-09-06 01:12:28 -0500768 }
akmhoque902d57e2012-08-17 09:24:38 -0500769
akmhoque59980a52012-08-09 12:36:09 -0500770 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -0500771
772 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -0500773 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -0500774 struct hashtb_param param_npl = {0};
775 nlsr->npl = hashtb_create(sizeof(struct name_prefix), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -0500776 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -0500777 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -0500778 struct hashtb_param param_npt = {0};
779 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
780 struct hashtb_param param_rte = {0};
781 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -0500782
akmhoque59980a52012-08-09 12:36:09 -0500783 nlsr->in_interest.p = &incoming_interest;
784 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -0500785
akmhoque03004e62012-09-06 01:12:28 -0500786 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -0500787
akmhoque03004e62012-09-06 01:12:28 -0500788 char *time_stamp=(char *)malloc(20);
789 memset(time_stamp,0,20);
790 get_current_timestamp_micro(time_stamp);
791 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
792 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
793 free(time_stamp);
794
795 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400796 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -0500797 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -0400798 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -0500799
800
akmhoque902d57e2012-08-17 09:24:38 -0500801
akmhoque53f64222012-09-05 13:57:51 -0500802
akmhoque59980a52012-08-09 12:36:09 -0500803 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -0500804 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -0500805 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -0500806 nlsr->adj_build_count=0;
807 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -0500808 nlsr->is_send_lsdb_interest_scheduled=0;
809 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -0500810
akmhoque7b791452012-10-30 11:24:56 -0500811 nlsr->detailed_logging=0;
812 nlsr->debugging=0;
813
akmhoqued79438d2012-08-27 13:31:42 -0500814 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
815 nlsr->interest_retry = INTEREST_RETRY;
816 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -0500817 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
818 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque3cced642012-09-24 16:20:20 -0500819 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
akmhoqueffacaa82012-09-13 17:48:30 -0500820 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -0500821
822 return 0;
akmhoque902d57e2012-08-17 09:24:38 -0500823}
824
akmhoque03004e62012-09-06 01:12:28 -0500825
akmhoque902d57e2012-08-17 09:24:38 -0500826int
827main(int argc, char *argv[])
828{
akmhoque81c25e02012-09-10 14:50:33 -0500829 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -0500830 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -0500831 int daemon_mode=0;
akmhoque902d57e2012-08-17 09:24:38 -0500832
akmhoquebfefef22012-09-26 10:09:34 -0500833
akmhoque81c25e02012-09-10 14:50:33 -0500834
akmhoque59980a52012-08-09 12:36:09 -0500835 while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1)
836 {
837 switch (res)
838 {
839 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -0500840 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -0500841 break;
842 case 'f':
843 config_file = optarg;
844 break;
845 case 'h':
846 default:
847 usage(argv[0]);
848 }
849 }
850
akmhoquebfefef22012-09-26 10:09:34 -0500851 ret=init_nlsr();
852 ON_ERROR_EXIT(ret);
akmhoque59980a52012-08-09 12:36:09 -0500853 readConfigFile(config_file);
akmhoquebfefef22012-09-26 10:09:34 -0500854
855 if ( daemon_mode == 1 )
856 {
857 daemonize_nlsr();
858 }
859
860 startLogging(nlsr->logDir);
861
akmhoque59980a52012-08-09 12:36:09 -0500862 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -0600863 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
864 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -0500865 {
866 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -0500867 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -0500868 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -0500869 }
akmhoque1771c412012-11-09 13:06:08 -0600870
871 init_api_server(ccn_fd);
872
akmhoque53f64222012-09-05 13:57:51 -0500873 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -0500874 router_prefix=ccn_charbuf_create();
875 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -0500876 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -0500877 {
878 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -0500879 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -0500880 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500881 }
akmhoque59980a52012-08-09 12:36:09 -0500882
883 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -0500884 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -0500885 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
886 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -0500887 {
888 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -0500889 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -0500890 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -0500891 }
892 ccn_charbuf_destroy(&router_prefix);
893
akmhoque7b791452012-10-30 11:24:56 -0500894 if ( nlsr->debugging )
895 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -0500896 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -0500897 if ( nlsr->debugging )
898 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -0500899 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -0500900
akmhoque53f64222012-09-05 13:57:51 -0500901 print_name_prefix_from_npl();
902 print_adjacent_from_adl();
akmhoque53f64222012-09-05 13:57:51 -0500903 build_and_install_name_lsas();
904 print_name_lsdb();
905
906 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -0500907 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500908 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -0500909
akmhoque1771c412012-11-09 13:06:08 -0600910
akmhoque59980a52012-08-09 12:36:09 -0500911 while(1)
akmhoque29c1db52012-09-07 14:47:43 -0500912 {
akmhoqueffacaa82012-09-13 17:48:30 -0500913 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -0500914 {
akmhoqueffacaa82012-09-13 17:48:30 -0500915 if( nlsr->sched != NULL )
916 {
akmhoque1771c412012-11-09 13:06:08 -0600917 long int micro_sec=ccn_schedule_run(nlsr->sched);
918 res=nlsr_api_server_poll(micro_sec,ccn_fd);
919 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -0500920 }
921 if(nlsr->ccn != NULL)
922 {
akmhoque1771c412012-11-09 13:06:08 -0600923 res = ccn_run(nlsr->ccn, 0);
akmhoqueffacaa82012-09-13 17:48:30 -0500924 }
925 if (!(nlsr->sched && nlsr->ccn))
926 {
927 break;
928 }
akmhoque29c1db52012-09-07 14:47:43 -0500929 }
930
akmhoque59980a52012-08-09 12:36:09 -0500931 }
akmhoque1771c412012-11-09 13:06:08 -0600932
akmhoque59980a52012-08-09 12:36:09 -0500933
akmhoque59980a52012-08-09 12:36:09 -0500934 return 0;
935}
936