blob: 351554f66ba65fd52c671b8643380fd6e8b4c932 [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <sys/stat.h>
8#include <assert.h>
9#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#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#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#include <ccn/sync.h>
30#include <ccn/seqwriter.h>
31
32#include "nlsr.h"
33#include "nlsr_ndn.h"
34#include "nlsr_lsdb.h"
35#include "utility.h"
36#include "nlsr_npl.h"
37#include "nlsr_adl.h"
38#include "nlsr_npt.h"
39#include "nlsr_route.h"
40#include "nlsr_sync.h"
akmhoquea37b52c2012-12-14 11:16:36 -060041#include "nlsr_face.h"
42#include "nlsr_fib.h"
akmhoque8fdd6412012-12-04 15:05:33 -060043
44
45#define ON_ERROR_DESTROY(resval) \
46{ \
47 if ((resval) < 0) { \
48 nlsr_destroy(); \
49 exit(1);\
50 } \
51}
52
53
54#define ON_ERROR_EXIT(resval) \
55{ \
56 if ((resval) < 0) { \
57 exit(1); \
58 } \
59}
60
61struct option longopts[] =
62{
63 { "daemon", no_argument, NULL, 'd'},
64 { "config_file", required_argument, NULL, 'f'},
65 { "api_port", required_argument, NULL, 'p'},
66 { "help", no_argument, NULL, 'h'},
67 { 0 }
68};
69
70static int
71usage(char *progname)
72{
73
74 printf("Usage: %s [OPTIONS...]\n\
75 NDN routing....\n\
76 -d, --daemon Run in daemon mode\n\
77 -f, --config_file Specify configuration file name\n\
78 -p, --api_port port where api client will connect\n\
79 -h, --help Display this help message\n", progname);
80
81 exit(1);
82}
83
84void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
85{
86 struct timeval now = {0};
87 gettimeofday(&now, 0);
88 result->s = now.tv_sec;
89 result->micros = now.tv_usec;
90}
91
92static struct ccn_gettime ndn_rtr_ticker = {
93 "timer",
94 &ndn_rtr_gettime,
95 1000000,
96 NULL
97};
98
99void
100nlsr_lock(void)
101{
102 nlsr->semaphor=NLSR_LOCKED;
103}
104
105void
106nlsr_unlock(void)
107{
108 nlsr->semaphor=NLSR_UNLOCKED;
109}
110
111void
112nlsr_stop_signal_handler(int sig)
113{
114 signal(sig, SIG_IGN);
115 nlsr_destroy();
116 exit(0);
117}
118
119void
120daemonize_nlsr(void)
121{
122 int ret;
123 pid_t process_id = 0;
124 pid_t sid = 0;
125 process_id = fork();
126 if (process_id < 0)
127 {
128 printf("Daemonization failed!\n");
129 ON_ERROR_DESTROY(process_id);
130 }
131 if (process_id > 0)
132 {
133 printf("Process daemonized. Process id: %d \n", process_id);
134 ret=process_id;
135 exit(0);
136 }
137
138 umask(0);
139 sid = setsid();
140 if(sid < 0)
141 {
142 ON_ERROR_DESTROY(sid);
143 }
144
145 chdir("/");
146 close(STDIN_FILENO);
147 close(STDOUT_FILENO);
148 close(STDERR_FILENO);
149}
150
151void
152process_command_ccnneighbor(char *command)
153{
154 if(command==NULL)
155 {
156 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
157 return;
158 }
159 char *rem;
160 const char *sep=" \t\n";
akmhoquea37b52c2012-12-14 11:16:36 -0600161 char *rtr_name;
akmhoque496c88f2013-02-08 05:24:18 -0600162 char *nbr_ip_addr;
163 int is_ip_configured=0;
akmhoquea37b52c2012-12-14 11:16:36 -0600164 //char *face;
akmhoque496c88f2013-02-08 05:24:18 -0600165 char *ip_addr=(char *)malloc(13);
166 memset(ip_addr,0,13);
akmhoque8fdd6412012-12-04 15:05:33 -0600167
168 rtr_name=strtok_r(command,sep,&rem);
169 if(rtr_name==NULL)
170 {
171 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
172 return;
173 }
akmhoque8fdd6412012-12-04 15:05:33 -0600174 if ( rtr_name[strlen(rtr_name)-1] == '/' )
175 {
176 rtr_name[strlen(rtr_name)-1]='\0';
177 }
akmhoque496c88f2013-02-08 05:24:18 -0600178
179 if (rem != NULL )
180 {
181 nbr_ip_addr=strtok_r(NULL,sep,&rem);
182 is_ip_configured=1;
183 }
akmhoque8fdd6412012-12-04 15:05:33 -0600184 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
185 nbr->name=(char *)malloc(strlen(rtr_name)+1);
186 memset(nbr->name,0,strlen(rtr_name)+1);
187 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
188 nbr->length=strlen(rtr_name)+1;
189
akmhoque496c88f2013-02-08 05:24:18 -0600190
191 if ( !is_ip_configured )
192 {
193 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
194 get_host_name_from_command_string(nbr_name,nbr->name,0);
195 printf("Hostname of neighbor: %s ",nbr_name->name);
196 get_ip_from_hostname_02(nbr_name->name,ip_addr);
197 printf("IP Address: %s \n",ip_addr);
198 free(nbr_name->name);
199 free(nbr_name);
200 }
201 else
202 {
203 memcpy(ip_addr,nbr_ip_addr,strlen(nbr_ip_addr));
204 printf("Name of neighbor: %s ",nbr->name);
205 printf("IP Address: %s \n",ip_addr);
206 }
akmhoquea37b52c2012-12-14 11:16:36 -0600207 add_nbr_to_adl(nbr,0,ip_addr);
akmhoque09c0afa2012-12-14 09:27:00 -0600208
akmhoque8fdd6412012-12-04 15:05:33 -0600209
210 free(nbr->name);
211 free(nbr);
212}
213
214void
215process_command_ccnname(char *command)
216{
217
218 if(command==NULL)
219 {
220 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
221 return;
222 }
223 char *rem;
224 const char *sep=" \t\n";
225 char *name;
226 name=strtok_r(command,sep,&rem);
227 if(name==NULL)
228 {
229 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
230 return;
231 }
232
233 printf("Name Prefix: %s \n",name);
234
235 if ( name[strlen(name)-1] == '/' )
236 name[strlen(name)-1]='\0';
237
238 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
239 np->name=(char *)malloc(strlen(name)+1);
240 memset(np->name,0,strlen(name)+1);
241 memcpy(np->name,name,strlen(name)+1);
242 np->length=strlen(name)+1;
243
244 add_name_to_npl(np);
245
246 free(np->name);
247 free(np);
248}
249
250
251void
252process_command_router_name(char *command)
253{
254 if(command==NULL)
255 {
256 printf(" Wrong Command Format ( router-name /router/name )\n");
257 return;
258 }
259 char *rem;
260 const char *sep=" \t\n";
261 char *rtr_name;
262
263 rtr_name=strtok_r(command,sep,&rem);
264 if(rtr_name==NULL)
265 {
266 printf(" Wrong Command Format ( router-name /router/name )\n");
267 return;
268 }
269
270
271 if ( rtr_name[strlen(rtr_name)-1] == '/' )
272 rtr_name[strlen(rtr_name)-1]='\0';
273
274 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
275 memset(nlsr->router_name,0,strlen(rtr_name)+1);
276 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
277
278
279}
280
akmhoque865d93c2013-02-08 06:00:10 -0600281/*
akmhoque8fdd6412012-12-04 15:05:33 -0600282void
283process_command_lsdb_synch_interval(char *command)
284{
285 if(command==NULL)
286 {
287 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
288 return;
289 }
290 char *rem;
291 const char *sep=" \t\n";
292 char *secs;
293 long int seconds;
294
295 secs=strtok_r(command,sep,&rem);
296 if(secs==NULL)
297 {
298 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
299 return;
300 }
301
302 seconds=atoi(secs);
303 if ( seconds >= 120 && seconds <= 3600 )
304 {
305 nlsr->lsdb_synch_interval=seconds;
306 }
307
308}
akmhoque865d93c2013-02-08 06:00:10 -0600309*/
akmhoque8fdd6412012-12-04 15:05:33 -0600310
311void
312process_command_interest_retry(char *command)
313{
314 if(command==NULL)
315 {
316 printf(" Wrong Command Format ( interest-retry number )\n");
317 return;
318 }
319 char *rem;
320 const char *sep=" \t\n";
321 char *retry;
322 long int retry_number;
323
324 retry=strtok_r(command,sep,&rem);
325 if(retry==NULL)
326 {
327 printf(" Wrong Command Format ( interest-retry number)\n");
328 return;
329 }
330
331 retry_number=atoi(retry);
332 if ( retry_number >= 1 && retry_number<=10 )
333 {
334 nlsr->interest_retry=retry_number;
335 }
336
337}
338
339void
340process_command_interest_resend_time(char *command)
341{
342 if(command==NULL)
343 {
344 printf(" Wrong Command Format ( interest-resend-time secs )\n");
345 return;
346 }
347 char *rem;
348 const char *sep=" \t\n";
349 char *secs;
350 long int seconds;
351
352 secs=strtok_r(command,sep,&rem);
353 if(secs==NULL)
354 {
355 printf(" Wrong Command Format ( interest-resend-time secs)\n");
356 return;
357 }
358
359 seconds=atoi(secs);
360 if ( seconds <= 60 && seconds >= 1 )
361 {
362 nlsr->interest_resend_time=seconds;
363 }
364}
365
366
367void
368process_command_lsa_refresh_time(char *command)
369{
370 if(command==NULL)
371 {
372 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
373 return;
374 }
375 char *rem;
376 const char *sep=" \t\n";
377 char *secs;
378 long int seconds;
379
380 secs=strtok_r(command,sep,&rem);
381 if(secs==NULL)
382 {
383 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
384 return;
385 }
386
387 seconds=atoi(secs);
akmhoque496c88f2013-02-08 05:24:18 -0600388 if ( seconds >= 240)
akmhoque8fdd6412012-12-04 15:05:33 -0600389 {
390 nlsr->lsa_refresh_time=seconds;
akmhoque496c88f2013-02-08 05:24:18 -0600391 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
392 {
393 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
394 }
akmhoque8fdd6412012-12-04 15:05:33 -0600395 }
396
397}
398
399void
400process_command_router_dead_interval(char *command)
401{
402 if(command==NULL)
403 {
404 printf(" Wrong Command Format ( router-dead-interval secs )\n");
405 return;
406 }
407 char *rem;
408 const char *sep=" \t\n";
409 char *secs;
410 long int seconds;
411
412 secs=strtok_r(command,sep,&rem);
413 if(secs==NULL)
414 {
415 printf(" Wrong Command Format ( router-dead-interval secs)\n");
416 return;
417 }
418
419 seconds=atoi(secs);
akmhoque496c88f2013-02-08 05:24:18 -0600420 if ( seconds >= 480 )
akmhoque8fdd6412012-12-04 15:05:33 -0600421 {
422 nlsr->router_dead_interval=seconds;
akmhoque496c88f2013-02-08 05:24:18 -0600423 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
424 {
425 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
426 }
akmhoque8fdd6412012-12-04 15:05:33 -0600427 }
428
429}
430
431void
akmhoque496c88f2013-02-08 05:24:18 -0600432process_command_max_faces_per_prefix(char *command)
akmhoque8fdd6412012-12-04 15:05:33 -0600433{
434 if(command==NULL)
435 {
akmhoque496c88f2013-02-08 05:24:18 -0600436 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600437 return;
438 }
439 char *rem;
440 const char *sep=" \t\n";
441 char *num;
442 long int number;
443
444 num=strtok_r(command,sep,&rem);
445 if(num==NULL)
446 {
akmhoque496c88f2013-02-08 05:24:18 -0600447 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600448 return;
449 }
450
451 number=atoi(num);
452 if ( number >= 0 && number <= 60 )
453 {
akmhoque496c88f2013-02-08 05:24:18 -0600454 nlsr->max_faces_per_prefix=number;
akmhoque8fdd6412012-12-04 15:05:33 -0600455 }
456
457}
458
459void
460process_command_logdir(char *command)
461{
462 if(command==NULL)
463 {
464 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
465 return;
466 }
467 char *rem;
468 const char *sep=" \t\n";
469 char *dir;
470
471 dir=strtok_r(command,sep,&rem);
472 if(dir==NULL)
473 {
474 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
475 return;
476 }
477
478 nlsr->logDir=(char *)malloc(strlen(dir)+1);
479 memset(nlsr->logDir,0,strlen(dir)+1);
480 memcpy(nlsr->logDir,dir,strlen(dir));
481}
482
483void
484process_command_detailed_log(char *command)
485{
486 if(command==NULL)
487 {
488 printf(" Wrong Command Format ( detailed-log on/off )\n");
489 return;
490 }
491 char *rem;
492 const char *sep=" \t\n";
493 char *on_off;
494
495 on_off=strtok_r(command,sep,&rem);
496 if(on_off==NULL)
497 {
498 printf(" Wrong Command Format ( detailed-log on/off )\n");
499 return;
500 }
501
502 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
503 {
504 nlsr->detailed_logging=1;
505 }
506}
507
508void
509process_command_debug(char *command)
510{
511 if(command==NULL)
512 {
513 printf(" Wrong Command Format ( debug on/off )\n");
514 return;
515 }
516 char *rem;
517 const char *sep=" \t\n";
518 char *on_off;
519
520 on_off=strtok_r(command,sep,&rem);
521 if(on_off==NULL)
522 {
523 printf(" Wrong Command Format ( debug on/off )\n");
524 return;
525 }
526
527 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
528 {
529 nlsr->debugging=1;
530 }
531}
532
akmhoque09c0afa2012-12-14 09:27:00 -0600533
534void
535process_command_topo_prefix(char *command)
536{
537 if(command==NULL)
538 {
539 printf(" Wrong Command Format ( topo-prefix )\n");
540 return;
541 }
542 char *rem;
543 const char *sep=" \t\n";
544 char *topo_prefix;
545
546 topo_prefix=strtok_r(command,sep,&rem);
547 if(topo_prefix==NULL)
548 {
549 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
550 return;
551 }
552 else
553 {
554 if( nlsr->topo_prefix != NULL)
555 free(nlsr->topo_prefix);
akmhoqueb29edd82013-01-14 20:54:11 -0600556 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
557 topo_prefix[strlen(topo_prefix)-1]='\0';
558
akmhoque09c0afa2012-12-14 09:27:00 -0600559 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
Syed Obaid Amin7a176c62012-12-21 15:56:00 -0600560 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
561 puts(topo_prefix);
akmhoque09c0afa2012-12-14 09:27:00 -0600562 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
563
akmhoque09c0afa2012-12-14 09:27:00 -0600564 }
565}
566
567
568void
569process_command_slice_prefix(char *command)
570{
571 if(command==NULL)
572 {
573 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
574 return;
575 }
576 char *rem;
577 const char *sep=" \t\n";
578 char *slice_prefix;
579
580 slice_prefix=strtok_r(command,sep,&rem);
581 if(slice_prefix==NULL)
582 {
583 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
584 return;
585 }
586 else
587 {
588 if ( nlsr->slice_prefix != NULL)
589 free(nlsr->slice_prefix);
akmhoqueb29edd82013-01-14 20:54:11 -0600590 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
591 slice_prefix[strlen(slice_prefix)-1]='\0';
592
akmhoque09c0afa2012-12-14 09:27:00 -0600593 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600594 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoque09c0afa2012-12-14 09:27:00 -0600595 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
akmhoque09c0afa2012-12-14 09:27:00 -0600596 }
597}
598
599void
akmhoque596f7082013-02-04 13:34:13 -0600600process_command_hyperbolic_routing(char *command)
akmhoque09c0afa2012-12-14 09:27:00 -0600601{
602 if(command==NULL)
603 {
akmhoque596f7082013-02-04 13:34:13 -0600604 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
akmhoque09c0afa2012-12-14 09:27:00 -0600605 return;
606 }
607 char *rem;
608 const char *sep=" \t\n";
609 char *on_off;
610
611 on_off=strtok_r(command,sep,&rem);
612 if(on_off==NULL)
613 {
akmhoque596f7082013-02-04 13:34:13 -0600614 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
akmhoque09c0afa2012-12-14 09:27:00 -0600615 return;
616 }
617
618 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
619 {
620 nlsr->is_hyperbolic_calc=1;
621 }
622}
623
akmhoqueed418f32013-01-30 12:25:04 -0600624void
625process_command_hyperbolic_cordinate(char *command)
626{
627 if(command==NULL)
628 {
629 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
630 return;
631 }
632
633 char *rem;
634 const char *sep=" \t\n\r";
635 char *radious;
636 char *theta;
637
638 radious=strtok_r(command,sep,&rem);
639 if (radious == NULL )
640 {
641 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
642 return;
643 }
644
645 theta=strtok_r(NULL,sep,&rem);
646 if (theta == NULL )
647 {
648 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
649 return;
650 }
651
652 nlsr->cor_r=strtof(radious,NULL);
653 nlsr->cor_theta=strtof(theta,NULL);
654
655}
656
akmhoque8fdd6412012-12-04 15:05:33 -0600657void
akmhoque596f7082013-02-04 13:34:13 -0600658process_command_tunnel_type(char *command)
659{
660 if(command==NULL)
661 {
662 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
663 return;
664 }
665 char *rem;
666 const char *sep=" \t\n";
667 char *on_off;
668
669 on_off=strtok_r(command,sep,&rem);
670 if(on_off==NULL)
671 {
672 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
673 return;
674 }
675
676 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
677 {
678 nlsr->tunnel_type=IPPROTO_TCP;
679 }
akmhoque496c88f2013-02-08 05:24:18 -0600680 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
681 {
682 nlsr->tunnel_type=IPPROTO_UDP;
683 }
akmhoque596f7082013-02-04 13:34:13 -0600684}
685
686void
akmhoque8fdd6412012-12-04 15:05:33 -0600687process_conf_command(char *command)
688{
689 const char *separators=" \t\n";
690 char *remainder=NULL;
691 char *cmd_type=NULL;
692
693 if(command==NULL || strlen(command)==0 || command[0]=='!')
694 return;
695
696 cmd_type=strtok_r(command,separators,&remainder);
697
698 if(!strcmp(cmd_type,"router-name") )
699 {
700 process_command_router_name(remainder);
701 }
702 else if(!strcmp(cmd_type,"ccnneighbor") )
703 {
704 process_command_ccnneighbor(remainder);
705 }
706 else if(!strcmp(cmd_type,"ccnname") )
707 {
708 process_command_ccnname(remainder);
709 }
akmhoque865d93c2013-02-08 06:00:10 -0600710 /*else if(!strcmp(cmd_type,"lsdb-synch-interval") )
akmhoque8fdd6412012-12-04 15:05:33 -0600711 {
712 process_command_lsdb_synch_interval(remainder);
akmhoque865d93c2013-02-08 06:00:10 -0600713 }*/
akmhoque8fdd6412012-12-04 15:05:33 -0600714 else if(!strcmp(cmd_type,"interest-retry") )
715 {
716 process_command_interest_retry(remainder);
717 }
718 else if(!strcmp(cmd_type,"interest-resend-time") )
719 {
720 process_command_interest_resend_time(remainder);
721 }
722 else if(!strcmp(cmd_type,"lsa-refresh-time") )
723 {
724 process_command_lsa_refresh_time(remainder);
725 }
726 else if(!strcmp(cmd_type,"router-dead-interval") )
727 {
728 process_command_router_dead_interval(remainder);
729 }
akmhoque496c88f2013-02-08 05:24:18 -0600730 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque8fdd6412012-12-04 15:05:33 -0600731 {
akmhoque496c88f2013-02-08 05:24:18 -0600732 process_command_max_faces_per_prefix(remainder);
akmhoque8fdd6412012-12-04 15:05:33 -0600733 }
734 else if(!strcmp(cmd_type,"logdir") )
735 {
736 process_command_logdir(remainder);
737 }
738 else if(!strcmp(cmd_type,"detailed-log") )
739 {
740 process_command_detailed_log(remainder);
741 }
742 else if(!strcmp(cmd_type,"debug") )
743 {
744 process_command_debug(remainder);
745 }
akmhoque09c0afa2012-12-14 09:27:00 -0600746 else if(!strcmp(cmd_type,"topo-prefix") )
747 {
748 process_command_topo_prefix(remainder);
749 }
750 else if(!strcmp(cmd_type,"slice-prefix") )
751 {
752 process_command_slice_prefix(remainder);
753 }
akmhoqueed418f32013-01-30 12:25:04 -0600754 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
755 {
756 process_command_hyperbolic_cordinate(remainder);
757 }
akmhoque596f7082013-02-04 13:34:13 -0600758 else if(!strcmp(cmd_type,"hyperbolic-routing") )
759 {
760 process_command_hyperbolic_routing(remainder);
761 }
762 else if(!strcmp(cmd_type,"tunnel-type") )
763 {
764 process_command_tunnel_type(remainder);
765 }
akmhoque8fdd6412012-12-04 15:05:33 -0600766 else
767 {
768 printf("Wrong configuration Command %s \n",cmd_type);
769 }
770}
771
772
773int
774readConfigFile(const char *filename)
775{
776 FILE *cfg;
777 char buf[1024];
778 int len;
779
780 cfg=fopen(filename, "r");
781
782 if(cfg == NULL)
783 {
784 printf("\nConfiguration File does not exists\n");
785 exit(1);
786 }
787
788 while(fgets((char *)buf, sizeof(buf), cfg))
789 {
790 len=strlen(buf);
791 if(buf[len-1] == '\n')
792 buf[len-1]='\0';
793 if ( buf[0] != '#' && buf[0] != '!')
794 process_conf_command(buf);
795 }
796
797 fclose(cfg);
798
799 return 0;
800}
801
akmhoquea37b52c2012-12-14 11:16:36 -0600802
803void
804add_faces_for_nbrs(void)
805{
806 int i, adl_element;
807 struct ndn_neighbor *nbr;
808
809 struct hashtb_enumerator ee;
810 struct hashtb_enumerator *e = &ee;
811
812 hashtb_start(nlsr->adl, e);
813 adl_element=hashtb_n(nlsr->adl);
814
815 for(i=0;i<adl_element;i++)
816 {
817 nbr=e->data;
akmhoque596f7082013-02-04 13:34:13 -0600818 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
akmhoquea37b52c2012-12-14 11:16:36 -0600819 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
820 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
akmhoque496c88f2013-02-08 05:24:18 -0600821 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
akmhoquea37b52c2012-12-14 11:16:36 -0600822 hashtb_next(e);
823 }
824
825 hashtb_end(e);
826
827}
828
akmhoque61a57f22013-01-18 07:44:05 -0600829void
830destroy_faces_for_nbrs(void)
831{
832 int i, adl_element;
833 struct ndn_neighbor *nbr;
834
835 struct hashtb_enumerator ee;
836 struct hashtb_enumerator *e = &ee;
837
838 hashtb_start(nlsr->adl, e);
839 adl_element=hashtb_n(nlsr->adl);
840
841 for(i=0;i<adl_element;i++)
842 {
akmhoqueed418f32013-01-30 12:25:04 -0600843 nbr=e->data;
844 if ( nbr->face > 0 )
845 {
akmhoqueed418f32013-01-30 12:25:04 -0600846 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
akmhoque596f7082013-02-04 13:34:13 -0600847 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
akmhoque496c88f2013-02-08 05:24:18 -0600848 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
akmhoqueed418f32013-01-30 12:25:04 -0600849 }
akmhoque61a57f22013-01-18 07:44:05 -0600850 hashtb_next(e);
851 }
852
853 hashtb_end(e);
854
855}
856
akmhoque8fdd6412012-12-04 15:05:33 -0600857char *
858process_api_client_command(char *command)
859{
860 char *msg;
861 msg=(char *)malloc(100);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600862 memset(msg,0,100);
akmhoque8fdd6412012-12-04 15:05:33 -0600863
864 const char *sep=" \t\n";
865 char *rem=NULL;
866 char *cmd_type=NULL;
867 char *op_type=NULL;
868 char *name=NULL;
869 char *face=NULL;
870 int face_id;
871 int res;
872
873 op_type=strtok_r(command,sep,&rem);
874 cmd_type=strtok_r(NULL,sep,&rem);
875 name=strtok_r(NULL,sep,&rem);
876 if ( name[strlen(name)-1] == '/' )
877 name[strlen(name)-1]='\0';
878
879 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
880 np->name=(char *)malloc(strlen(name)+1);
881 memset(np->name,0,strlen(name)+1);
882 memcpy(np->name,name,strlen(name)+1);
883 np->length=strlen(name)+1;
884
885 if ( strcmp(cmd_type,"name")!= 0 )
886 {
887 face=strtok_r(NULL,sep,&rem);
888 sscanf(face,"face%d",&face_id);
889 }
890
891 if ( strcmp(cmd_type,"name")== 0 )
892 {
893 if ( strcmp(op_type,"del") == 0 )
894 {
895 res=does_name_exist_in_npl(np);
896 if ( res == 0)
897 {
898 sprintf(msg,"Name %s does not exist !!",name);
899 }
900 else
901 {
902 long int ls_id=get_lsa_id_from_npl(np);
903 if ( ls_id != 0 )
904 {
905 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
906 sprintf(msg,"Name %s has been deleted and Advertised.",name);
907 }
908 else
909 {
910 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
911 }
912 }
913 }
914 else if ( strcmp(op_type,"add") == 0 )
915 {
916 res=does_name_exist_in_npl(np);
917 if ( res == 0)
918 {
919 add_name_to_npl(np);
920 build_and_install_single_name_lsa(np);
921 sprintf(msg,"Name %s has been added to advertise.",name);
922 }
923 else
924 {
925 sprintf(msg,"Name %s has already been advertised from this router !!",name);
926 }
927 }
928 }
929 else if ( strcmp(cmd_type,"neighbor") == 0 )
930 {
931 if ( strcmp(op_type,"del") == 0 )
932 {
933 res=is_neighbor(np->name);
934 if ( res == 0)
935 {
936 sprintf(msg,"Neighbor %s does not exist !!",name);
937 }
938 else
939 {
940 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoque496c88f2013-02-08 05:24:18 -0600941 int face_id=get_next_hop_face_from_adl(np->name);
942 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
943 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
944 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque8fdd6412012-12-04 15:05:33 -0600945 delete_nbr_from_adl(np);
946 if(!nlsr->is_build_adj_lsa_sheduled)
947 {
948 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
949 nlsr->is_build_adj_lsa_sheduled=1;
950 }
951 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
952 }
953 }
954 else if ( strcmp(op_type,"add") == 0 )
955 {
956 res=is_neighbor(np->name);
957 if ( res == 0 )
958 {
akmhoque09c0afa2012-12-14 09:27:00 -0600959 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
960 get_host_name_from_command_string(nbr_name,np->name,0);
961 printf("Hostname of neighbor: %s ",nbr_name->name);
962
akmhoquea37b52c2012-12-14 11:16:36 -0600963 char *ip_addr=(char *)malloc(13);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600964 memset(ip_addr,0,13);
akmhoquea37b52c2012-12-14 11:16:36 -0600965 get_ip_from_hostname_02(nbr_name->name,ip_addr);
966 printf("IP Address: %s \n",ip_addr);
akmhoque596f7082013-02-04 13:34:13 -0600967 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
akmhoquea37b52c2012-12-14 11:16:36 -0600968 update_face_to_adl_for_nbr(nbr_name->name, face_id);
akmhoque496c88f2013-02-08 05:24:18 -0600969 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
970 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
akmhoquea37b52c2012-12-14 11:16:36 -0600971
972 add_nbr_to_adl(np,face_id,ip_addr);
973
akmhoque8fdd6412012-12-04 15:05:33 -0600974 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoquea37b52c2012-12-14 11:16:36 -0600975
akmhoque8fdd6412012-12-04 15:05:33 -0600976 }
977 else
978 {
979 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
980 }
981 }
982 }
983
984
985 return msg;
986}
987
988int
989nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
990{
991 struct timeval timeout;
akmhoque21ffec22013-01-31 18:22:10 -0600992 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque8fdd6412012-12-04 15:05:33 -0600993 {
akmhoque21ffec22013-01-31 18:22:10 -0600994 timeout.tv_sec=0;
995 timeout.tv_usec=time_out_micro_sec;
akmhoque8fdd6412012-12-04 15:05:33 -0600996 }
akmhoque21ffec22013-01-31 18:22:10 -0600997 else
akmhoque8fdd6412012-12-04 15:05:33 -0600998 {
akmhoque21ffec22013-01-31 18:22:10 -0600999 timeout.tv_sec = 0;
1000 timeout.tv_usec = 500000;
akmhoque8fdd6412012-12-04 15:05:33 -06001001 }
akmhoque8fdd6412012-12-04 15:05:33 -06001002
1003 int fd;
1004 int nread;
1005 int result;
1006 fd_set testfds;
1007 unsigned int client_len;
1008 int client_sockfd;
1009 char recv_buffer[1024];
1010 bzero(recv_buffer,1024);
1011 struct sockaddr_in client_address;
1012
1013 testfds=nlsr->readfds;
1014 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1015
soamin29b8e392013-01-22 17:12:07 -06001016 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque8fdd6412012-12-04 15:05:33 -06001017 {
1018 if(FD_ISSET(fd,&testfds))
1019 {
1020 if ( fd == ccn_fd )
1021 {
1022 return 0;
1023 }
1024 else if(fd == nlsr->nlsr_api_server_sock_fd)
1025 {
1026 client_len = sizeof(client_address);
1027 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1028 FD_SET(client_sockfd, &nlsr->readfds);
1029 }
1030 else
1031 {
1032
1033 ioctl(fd, FIONREAD, &nread);
1034 if(nread == 0)
1035 {
1036 close(fd);
1037 FD_CLR(fd, &nlsr->readfds);
1038 }
1039 else
1040 {
1041 recv(fd, recv_buffer, 1024, 0);
1042 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
1043 char *msg=process_api_client_command(recv_buffer);
1044 send(fd, msg, strlen(msg),0);
1045 free(msg);
1046 close(fd);
1047 FD_CLR(fd, &nlsr->readfds);
1048 }
1049 }
1050 }
1051 }
1052
1053 return 0;
1054}
1055
akmhoque865d93c2013-02-08 06:00:10 -06001056int
1057check_config_validity()
1058{
1059 if (nlsr->router_name == NULL )
1060 {
1061 fprintf(stderr,"Router name has not been configured :(\n");
1062 return -1;
1063 }
1064 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1065 {
1066 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1067 return -1;
1068 }
1069
1070 return 0;
1071}
1072
akmhoque8fdd6412012-12-04 15:05:33 -06001073void
1074nlsr_destroy( void )
1075{
1076 if ( nlsr->debugging )
1077 {
1078 printf("Freeing Allocated Memory....\n");
1079 }
1080 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
1081 /* Destroying all face created by nlsr in CCND */
1082 destroy_all_face_by_nlsr();
akmhoque61a57f22013-01-18 07:44:05 -06001083 destroy_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001084 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
1085 hashtb_destroy(&nlsr->npl);
1086 hashtb_destroy(&nlsr->adl);
1087 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1088 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
1089 hashtb_destroy(&nlsr->pit_alsa);
1090
akmhoqueed418f32013-01-30 12:25:04 -06001091
akmhoque8fdd6412012-12-04 15:05:33 -06001092
1093 hashtb_destroy(&nlsr->routing_table);
1094
1095
1096 int i, npt_element;
1097 struct npt_entry *ne;
1098 struct hashtb_enumerator ee;
1099 struct hashtb_enumerator *e = &ee;
1100 hashtb_start(nlsr->npt, e);
1101 npt_element=hashtb_n(nlsr->npt);
1102 for(i=0;i<npt_element;i++)
1103 {
1104 ne=e->data;
1105 hashtb_destroy(&ne->name_list);
1106 hashtb_destroy(&ne->face_list);
1107 hashtb_next(e);
1108 }
1109
1110 hashtb_end(e);
1111 hashtb_destroy(&nlsr->npt);
1112
1113
1114 ccns_close(&nlsr->ccns, NULL, NULL);
1115 ccns_slice_destroy(&nlsr->slice);
1116
1117
1118
1119 close(nlsr->nlsr_api_server_sock_fd);
1120
1121 ccn_schedule_destroy(&nlsr->sched);
1122 ccn_destroy(&nlsr->ccn);
1123
1124 free(nlsr->lsdb->lsdb_version);
1125 free(nlsr->lsdb);
1126 free(nlsr->router_name);
1127 free(nlsr);
1128 if ( nlsr->debugging )
1129 {
1130 printf("Finished freeing allocated memory\n");
1131 }
1132 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1133
1134}
1135
1136
1137
1138void
1139init_api_server(int ccn_fd)
1140{
1141 int server_sockfd;
1142 int server_len;
1143 struct sockaddr_in server_address;
1144 unsigned int yes=1;
1145
1146 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1147
1148 int flags = fcntl(server_sockfd, F_GETFL, 0);
1149 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1150
1151 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1152 {
1153 ON_ERROR_DESTROY(-1);
1154 }
1155
1156 server_address.sin_family = AF_INET;
Adam Alyyanc350e3a2013-01-14 14:35:57 -06001157 server_address.sin_addr.s_addr = INADDR_ANY;
1158 server_address.sin_port = htons(nlsr->api_port);
akmhoque8fdd6412012-12-04 15:05:33 -06001159
1160 server_len = sizeof(server_address);
1161 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1162 listen(server_sockfd, 100);
1163 FD_ZERO(&nlsr->readfds);
1164 FD_SET(server_sockfd, &nlsr->readfds);
1165 FD_SET(ccn_fd, &nlsr->readfds);
1166 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1167
1168}
1169
1170int
1171init_nlsr(void)
1172{
1173 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1174 {
1175 perror("SIGQUIT install error\n");
1176 return -1;
1177 }
1178 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1179 {
1180 perror("SIGTERM install error\n");
1181 return -1;
1182 }
1183 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1184 {
1185 perror("SIGTERM install error\n");
1186 return -1;
1187 }
1188
1189 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1190
1191 struct hashtb_param param_adl = {0};
1192 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1193 struct hashtb_param param_npl = {0};
1194 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1195 struct hashtb_param param_pit_alsa = {0};
1196 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1197 struct hashtb_param param_npt = {0};
1198 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1199 struct hashtb_param param_rte = {0};
1200 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1201
1202 nlsr->in_interest.p = &incoming_interest;
1203 nlsr->in_content.p = &incoming_content;
1204
1205 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1206
1207 char *time_stamp=(char *)malloc(20);
1208 memset(time_stamp,0,20);
1209 get_current_timestamp_micro(time_stamp);
1210 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueed418f32013-01-30 12:25:04 -06001211 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque8fdd6412012-12-04 15:05:33 -06001212 free(time_stamp);
1213
1214 struct hashtb_param param_adj_lsdb = {0};
1215 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1216 struct hashtb_param param_name_lsdb = {0};
1217 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueed418f32013-01-30 12:25:04 -06001218 struct hashtb_param param_cor_lsdb = {0};
1219 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque8fdd6412012-12-04 15:05:33 -06001220
1221
1222
1223
1224 nlsr->is_synch_init=1;
1225 nlsr->nlsa_id=0;
1226 nlsr->adj_build_flag=0;
1227 nlsr->adj_build_count=0;
1228 nlsr->is_build_adj_lsa_sheduled=0;
1229 nlsr->is_send_lsdb_interest_scheduled=0;
1230 nlsr->is_route_calculation_scheduled=0;
1231
1232 nlsr->detailed_logging=0;
1233 nlsr->debugging=0;
1234
akmhoque865d93c2013-02-08 06:00:10 -06001235 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoque8fdd6412012-12-04 15:05:33 -06001236 nlsr->interest_retry = INTEREST_RETRY;
1237 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1238 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1239 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque496c88f2013-02-08 05:24:18 -06001240 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoque8fdd6412012-12-04 15:05:33 -06001241 nlsr->semaphor=NLSR_UNLOCKED;
1242
1243 nlsr->api_port=API_PORT;
1244
akmhoque8fdd6412012-12-04 15:05:33 -06001245 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001246 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001247 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1248
1249 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001250 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001251 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1252
akmhoque09c0afa2012-12-14 09:27:00 -06001253 nlsr->is_hyperbolic_calc=0;
akmhoqueed418f32013-01-30 12:25:04 -06001254 nlsr->cor_r=-1.0;
1255 nlsr->cor_theta=-1.0;
akmhoque09c0afa2012-12-14 09:27:00 -06001256
akmhoque596f7082013-02-04 13:34:13 -06001257 nlsr->tunnel_type=IPPROTO_UDP;
1258
akmhoque8fdd6412012-12-04 15:05:33 -06001259 return 0;
1260}
1261
1262
1263int
1264main(int argc, char *argv[])
1265{
1266 int res, ret;
1267 char *config_file;
1268 int daemon_mode=0;
1269 int port=0;
1270
1271
1272
1273 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1274 {
1275 switch (res)
1276 {
1277 case 'd':
1278 daemon_mode = 1;
1279 break;
1280 case 'f':
1281 config_file = optarg;
1282 break;
1283 case 'p':
1284 port = atoi(optarg);
1285 break;
1286 case 'h':
1287 default:
1288 usage(argv[0]);
1289 }
1290 }
1291
1292 ret=init_nlsr();
1293 ON_ERROR_EXIT(ret);
1294
1295 if ( port !=0 )
1296 nlsr->api_port=port;
1297
1298 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001299
akmhoque865d93c2013-02-08 06:00:10 -06001300 ON_ERROR_DESTROY(check_config_validity());
1301
akmhoquea37b52c2012-12-14 11:16:36 -06001302 print_adjacent_from_adl();
1303
akmhoque8fdd6412012-12-04 15:05:33 -06001304 if ( daemon_mode == 1 )
1305 {
akmhoquebaf53f12013-01-24 11:06:18 -06001306 nlsr->debugging=0;
akmhoque8fdd6412012-12-04 15:05:33 -06001307 daemonize_nlsr();
1308 }
1309
1310 startLogging(nlsr->logDir);
1311
1312 nlsr->ccn=ccn_create();
1313 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1314 if(ccn_fd == -1)
1315 {
1316 fprintf(stderr,"Could not connect to ccnd\n");
1317 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1318 ON_ERROR_DESTROY(-1);
1319 }
1320
1321 init_api_server(ccn_fd);
1322
akmhoquefc5176d2013-01-18 09:50:12 -06001323 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1324 if(res<0)
1325 {
1326 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1327 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1328 ON_ERROR_DESTROY(res);
1329 }
akmhoque8fdd6412012-12-04 15:05:33 -06001330 struct ccn_charbuf *router_prefix;
1331 router_prefix=ccn_charbuf_create();
1332 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1333 if(res<0)
1334 {
1335 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1336 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1337 ON_ERROR_DESTROY(res);
1338 }
1339
1340 ccn_name_append_str(router_prefix,"nlsr");
1341 nlsr->in_interest.data=nlsr->router_name;
1342 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1343 if ( res < 0 )
1344 {
1345 fprintf(stderr,"Failed to register interest for router\n");
1346 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1347 ON_ERROR_DESTROY(res);
1348 }
1349 ccn_charbuf_destroy(&router_prefix);
1350
1351 if ( nlsr->debugging )
1352 printf("Router Name : %s\n",nlsr->router_name);
1353 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1354 if ( nlsr->debugging )
1355 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1356 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1357
akmhoquea37b52c2012-12-14 11:16:36 -06001358 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001359 print_name_prefix_from_npl();
1360 print_adjacent_from_adl();
akmhoque596f7082013-02-04 13:34:13 -06001361 build_and_install_name_lsas();
akmhoque8fdd6412012-12-04 15:05:33 -06001362
1363 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1364
akmhoque596f7082013-02-04 13:34:13 -06001365
1366 print_name_lsdb();
akmhoque865d93c2013-02-08 06:00:10 -06001367 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta== -1.0)
1368 {
1369 build_and_install_cor_lsa();
1370 }
akmhoque8fdd6412012-12-04 15:05:33 -06001371 write_name_lsdb_to_repo(nlsr->slice_prefix);
1372
akmhoque8fdd6412012-12-04 15:05:33 -06001373 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001374 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1375 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001376
1377
1378 while(1)
1379 {
1380 if ( nlsr->semaphor == NLSR_UNLOCKED )
1381 {
1382 if( nlsr->sched != NULL )
1383 {
1384 long int micro_sec=ccn_schedule_run(nlsr->sched);
1385 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1386 ON_ERROR_DESTROY(res);
1387 }
1388 if(nlsr->ccn != NULL)
1389 {
akmhoque719a4fc2013-01-31 16:11:52 -06001390 res = ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -06001391 }
1392 if (!(nlsr->sched && nlsr->ccn))
1393 {
1394 break;
1395 }
1396 }
1397
1398 }
1399
1400
1401 return 0;
1402}
1403