blob: 9ba67c66ed3714c9dfb6549137b70f5301fe6fc8 [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
281void
282process_command_lsdb_synch_interval(char *command)
283{
284 if(command==NULL)
285 {
286 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
287 return;
288 }
289 char *rem;
290 const char *sep=" \t\n";
291 char *secs;
292 long int seconds;
293
294 secs=strtok_r(command,sep,&rem);
295 if(secs==NULL)
296 {
297 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
298 return;
299 }
300
301 seconds=atoi(secs);
302 if ( seconds >= 120 && seconds <= 3600 )
303 {
304 nlsr->lsdb_synch_interval=seconds;
305 }
306
307}
308
309
310void
311process_command_interest_retry(char *command)
312{
313 if(command==NULL)
314 {
315 printf(" Wrong Command Format ( interest-retry number )\n");
316 return;
317 }
318 char *rem;
319 const char *sep=" \t\n";
320 char *retry;
321 long int retry_number;
322
323 retry=strtok_r(command,sep,&rem);
324 if(retry==NULL)
325 {
326 printf(" Wrong Command Format ( interest-retry number)\n");
327 return;
328 }
329
330 retry_number=atoi(retry);
331 if ( retry_number >= 1 && retry_number<=10 )
332 {
333 nlsr->interest_retry=retry_number;
334 }
335
336}
337
338void
339process_command_interest_resend_time(char *command)
340{
341 if(command==NULL)
342 {
343 printf(" Wrong Command Format ( interest-resend-time secs )\n");
344 return;
345 }
346 char *rem;
347 const char *sep=" \t\n";
348 char *secs;
349 long int seconds;
350
351 secs=strtok_r(command,sep,&rem);
352 if(secs==NULL)
353 {
354 printf(" Wrong Command Format ( interest-resend-time secs)\n");
355 return;
356 }
357
358 seconds=atoi(secs);
359 if ( seconds <= 60 && seconds >= 1 )
360 {
361 nlsr->interest_resend_time=seconds;
362 }
363}
364
365
366void
367process_command_lsa_refresh_time(char *command)
368{
369 if(command==NULL)
370 {
371 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
372 return;
373 }
374 char *rem;
375 const char *sep=" \t\n";
376 char *secs;
377 long int seconds;
378
379 secs=strtok_r(command,sep,&rem);
380 if(secs==NULL)
381 {
382 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
383 return;
384 }
385
386 seconds=atoi(secs);
akmhoque496c88f2013-02-08 05:24:18 -0600387 if ( seconds >= 240)
akmhoque8fdd6412012-12-04 15:05:33 -0600388 {
389 nlsr->lsa_refresh_time=seconds;
akmhoque496c88f2013-02-08 05:24:18 -0600390 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
391 {
392 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
393 }
akmhoque8fdd6412012-12-04 15:05:33 -0600394 }
395
396}
397
398void
399process_command_router_dead_interval(char *command)
400{
401 if(command==NULL)
402 {
403 printf(" Wrong Command Format ( router-dead-interval secs )\n");
404 return;
405 }
406 char *rem;
407 const char *sep=" \t\n";
408 char *secs;
409 long int seconds;
410
411 secs=strtok_r(command,sep,&rem);
412 if(secs==NULL)
413 {
414 printf(" Wrong Command Format ( router-dead-interval secs)\n");
415 return;
416 }
417
418 seconds=atoi(secs);
akmhoque496c88f2013-02-08 05:24:18 -0600419 if ( seconds >= 480 )
akmhoque8fdd6412012-12-04 15:05:33 -0600420 {
421 nlsr->router_dead_interval=seconds;
akmhoque496c88f2013-02-08 05:24:18 -0600422 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
423 {
424 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
425 }
akmhoque8fdd6412012-12-04 15:05:33 -0600426 }
427
428}
429
430void
akmhoque496c88f2013-02-08 05:24:18 -0600431process_command_max_faces_per_prefix(char *command)
akmhoque8fdd6412012-12-04 15:05:33 -0600432{
433 if(command==NULL)
434 {
akmhoque496c88f2013-02-08 05:24:18 -0600435 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600436 return;
437 }
438 char *rem;
439 const char *sep=" \t\n";
440 char *num;
441 long int number;
442
443 num=strtok_r(command,sep,&rem);
444 if(num==NULL)
445 {
akmhoque496c88f2013-02-08 05:24:18 -0600446 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque8fdd6412012-12-04 15:05:33 -0600447 return;
448 }
449
450 number=atoi(num);
451 if ( number >= 0 && number <= 60 )
452 {
akmhoque496c88f2013-02-08 05:24:18 -0600453 nlsr->max_faces_per_prefix=number;
akmhoque8fdd6412012-12-04 15:05:33 -0600454 }
455
456}
457
458void
459process_command_logdir(char *command)
460{
461 if(command==NULL)
462 {
463 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
464 return;
465 }
466 char *rem;
467 const char *sep=" \t\n";
468 char *dir;
469
470 dir=strtok_r(command,sep,&rem);
471 if(dir==NULL)
472 {
473 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
474 return;
475 }
476
477 nlsr->logDir=(char *)malloc(strlen(dir)+1);
478 memset(nlsr->logDir,0,strlen(dir)+1);
479 memcpy(nlsr->logDir,dir,strlen(dir));
480}
481
482void
483process_command_detailed_log(char *command)
484{
485 if(command==NULL)
486 {
487 printf(" Wrong Command Format ( detailed-log on/off )\n");
488 return;
489 }
490 char *rem;
491 const char *sep=" \t\n";
492 char *on_off;
493
494 on_off=strtok_r(command,sep,&rem);
495 if(on_off==NULL)
496 {
497 printf(" Wrong Command Format ( detailed-log on/off )\n");
498 return;
499 }
500
501 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
502 {
503 nlsr->detailed_logging=1;
504 }
505}
506
507void
508process_command_debug(char *command)
509{
510 if(command==NULL)
511 {
512 printf(" Wrong Command Format ( debug on/off )\n");
513 return;
514 }
515 char *rem;
516 const char *sep=" \t\n";
517 char *on_off;
518
519 on_off=strtok_r(command,sep,&rem);
520 if(on_off==NULL)
521 {
522 printf(" Wrong Command Format ( debug on/off )\n");
523 return;
524 }
525
526 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
527 {
528 nlsr->debugging=1;
529 }
530}
531
akmhoque09c0afa2012-12-14 09:27:00 -0600532
533void
534process_command_topo_prefix(char *command)
535{
536 if(command==NULL)
537 {
538 printf(" Wrong Command Format ( topo-prefix )\n");
539 return;
540 }
541 char *rem;
542 const char *sep=" \t\n";
543 char *topo_prefix;
544
545 topo_prefix=strtok_r(command,sep,&rem);
546 if(topo_prefix==NULL)
547 {
548 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
549 return;
550 }
551 else
552 {
553 if( nlsr->topo_prefix != NULL)
554 free(nlsr->topo_prefix);
akmhoqueb29edd82013-01-14 20:54:11 -0600555 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
556 topo_prefix[strlen(topo_prefix)-1]='\0';
557
akmhoque09c0afa2012-12-14 09:27:00 -0600558 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
Syed Obaid Amin7a176c62012-12-21 15:56:00 -0600559 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
560 puts(topo_prefix);
akmhoque09c0afa2012-12-14 09:27:00 -0600561 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
562
akmhoque09c0afa2012-12-14 09:27:00 -0600563 }
564}
565
566
567void
568process_command_slice_prefix(char *command)
569{
570 if(command==NULL)
571 {
572 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
573 return;
574 }
575 char *rem;
576 const char *sep=" \t\n";
577 char *slice_prefix;
578
579 slice_prefix=strtok_r(command,sep,&rem);
580 if(slice_prefix==NULL)
581 {
582 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
583 return;
584 }
585 else
586 {
587 if ( nlsr->slice_prefix != NULL)
588 free(nlsr->slice_prefix);
akmhoqueb29edd82013-01-14 20:54:11 -0600589 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
590 slice_prefix[strlen(slice_prefix)-1]='\0';
591
akmhoque09c0afa2012-12-14 09:27:00 -0600592 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600593 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoque09c0afa2012-12-14 09:27:00 -0600594 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
akmhoque09c0afa2012-12-14 09:27:00 -0600595 }
596}
597
598void
akmhoque596f7082013-02-04 13:34:13 -0600599process_command_hyperbolic_routing(char *command)
akmhoque09c0afa2012-12-14 09:27:00 -0600600{
601 if(command==NULL)
602 {
akmhoque596f7082013-02-04 13:34:13 -0600603 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
akmhoque09c0afa2012-12-14 09:27:00 -0600604 return;
605 }
606 char *rem;
607 const char *sep=" \t\n";
608 char *on_off;
609
610 on_off=strtok_r(command,sep,&rem);
611 if(on_off==NULL)
612 {
akmhoque596f7082013-02-04 13:34:13 -0600613 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
akmhoque09c0afa2012-12-14 09:27:00 -0600614 return;
615 }
616
617 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
618 {
619 nlsr->is_hyperbolic_calc=1;
620 }
621}
622
akmhoqueed418f32013-01-30 12:25:04 -0600623void
624process_command_hyperbolic_cordinate(char *command)
625{
626 if(command==NULL)
627 {
628 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
629 return;
630 }
631
632 char *rem;
633 const char *sep=" \t\n\r";
634 char *radious;
635 char *theta;
636
637 radious=strtok_r(command,sep,&rem);
638 if (radious == NULL )
639 {
640 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
641 return;
642 }
643
644 theta=strtok_r(NULL,sep,&rem);
645 if (theta == NULL )
646 {
647 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
648 return;
649 }
650
651 nlsr->cor_r=strtof(radious,NULL);
652 nlsr->cor_theta=strtof(theta,NULL);
653
654}
655
akmhoque8fdd6412012-12-04 15:05:33 -0600656void
akmhoque596f7082013-02-04 13:34:13 -0600657process_command_tunnel_type(char *command)
658{
659 if(command==NULL)
660 {
661 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
662 return;
663 }
664 char *rem;
665 const char *sep=" \t\n";
666 char *on_off;
667
668 on_off=strtok_r(command,sep,&rem);
669 if(on_off==NULL)
670 {
671 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
672 return;
673 }
674
675 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
676 {
677 nlsr->tunnel_type=IPPROTO_TCP;
678 }
akmhoque496c88f2013-02-08 05:24:18 -0600679 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
680 {
681 nlsr->tunnel_type=IPPROTO_UDP;
682 }
akmhoque596f7082013-02-04 13:34:13 -0600683}
684
685void
akmhoque8fdd6412012-12-04 15:05:33 -0600686process_conf_command(char *command)
687{
688 const char *separators=" \t\n";
689 char *remainder=NULL;
690 char *cmd_type=NULL;
691
692 if(command==NULL || strlen(command)==0 || command[0]=='!')
693 return;
694
695 cmd_type=strtok_r(command,separators,&remainder);
696
697 if(!strcmp(cmd_type,"router-name") )
698 {
699 process_command_router_name(remainder);
700 }
701 else if(!strcmp(cmd_type,"ccnneighbor") )
702 {
703 process_command_ccnneighbor(remainder);
704 }
705 else if(!strcmp(cmd_type,"ccnname") )
706 {
707 process_command_ccnname(remainder);
708 }
709 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
710 {
711 process_command_lsdb_synch_interval(remainder);
712 }
713 else if(!strcmp(cmd_type,"interest-retry") )
714 {
715 process_command_interest_retry(remainder);
716 }
717 else if(!strcmp(cmd_type,"interest-resend-time") )
718 {
719 process_command_interest_resend_time(remainder);
720 }
721 else if(!strcmp(cmd_type,"lsa-refresh-time") )
722 {
723 process_command_lsa_refresh_time(remainder);
724 }
725 else if(!strcmp(cmd_type,"router-dead-interval") )
726 {
727 process_command_router_dead_interval(remainder);
728 }
akmhoque496c88f2013-02-08 05:24:18 -0600729 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque8fdd6412012-12-04 15:05:33 -0600730 {
akmhoque496c88f2013-02-08 05:24:18 -0600731 process_command_max_faces_per_prefix(remainder);
akmhoque8fdd6412012-12-04 15:05:33 -0600732 }
733 else if(!strcmp(cmd_type,"logdir") )
734 {
735 process_command_logdir(remainder);
736 }
737 else if(!strcmp(cmd_type,"detailed-log") )
738 {
739 process_command_detailed_log(remainder);
740 }
741 else if(!strcmp(cmd_type,"debug") )
742 {
743 process_command_debug(remainder);
744 }
akmhoque09c0afa2012-12-14 09:27:00 -0600745 else if(!strcmp(cmd_type,"topo-prefix") )
746 {
747 process_command_topo_prefix(remainder);
748 }
749 else if(!strcmp(cmd_type,"slice-prefix") )
750 {
751 process_command_slice_prefix(remainder);
752 }
akmhoqueed418f32013-01-30 12:25:04 -0600753 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
754 {
755 process_command_hyperbolic_cordinate(remainder);
756 }
akmhoque596f7082013-02-04 13:34:13 -0600757 else if(!strcmp(cmd_type,"hyperbolic-routing") )
758 {
759 process_command_hyperbolic_routing(remainder);
760 }
761 else if(!strcmp(cmd_type,"tunnel-type") )
762 {
763 process_command_tunnel_type(remainder);
764 }
akmhoque8fdd6412012-12-04 15:05:33 -0600765 else
766 {
767 printf("Wrong configuration Command %s \n",cmd_type);
768 }
769}
770
771
772int
773readConfigFile(const char *filename)
774{
775 FILE *cfg;
776 char buf[1024];
777 int len;
778
779 cfg=fopen(filename, "r");
780
781 if(cfg == NULL)
782 {
783 printf("\nConfiguration File does not exists\n");
784 exit(1);
785 }
786
787 while(fgets((char *)buf, sizeof(buf), cfg))
788 {
789 len=strlen(buf);
790 if(buf[len-1] == '\n')
791 buf[len-1]='\0';
792 if ( buf[0] != '#' && buf[0] != '!')
793 process_conf_command(buf);
794 }
795
796 fclose(cfg);
797
798 return 0;
799}
800
akmhoquea37b52c2012-12-14 11:16:36 -0600801
802void
803add_faces_for_nbrs(void)
804{
805 int i, adl_element;
806 struct ndn_neighbor *nbr;
807
808 struct hashtb_enumerator ee;
809 struct hashtb_enumerator *e = &ee;
810
811 hashtb_start(nlsr->adl, e);
812 adl_element=hashtb_n(nlsr->adl);
813
814 for(i=0;i<adl_element;i++)
815 {
816 nbr=e->data;
akmhoque596f7082013-02-04 13:34:13 -0600817 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 -0600818 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
819 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
akmhoque496c88f2013-02-08 05:24:18 -0600820 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
akmhoquea37b52c2012-12-14 11:16:36 -0600821 hashtb_next(e);
822 }
823
824 hashtb_end(e);
825
826}
827
akmhoque61a57f22013-01-18 07:44:05 -0600828void
829destroy_faces_for_nbrs(void)
830{
831 int i, adl_element;
832 struct ndn_neighbor *nbr;
833
834 struct hashtb_enumerator ee;
835 struct hashtb_enumerator *e = &ee;
836
837 hashtb_start(nlsr->adl, e);
838 adl_element=hashtb_n(nlsr->adl);
839
840 for(i=0;i<adl_element;i++)
841 {
akmhoqueed418f32013-01-30 12:25:04 -0600842 nbr=e->data;
843 if ( nbr->face > 0 )
844 {
akmhoqueed418f32013-01-30 12:25:04 -0600845 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
akmhoque596f7082013-02-04 13:34:13 -0600846 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
akmhoque496c88f2013-02-08 05:24:18 -0600847 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
akmhoqueed418f32013-01-30 12:25:04 -0600848 }
akmhoque61a57f22013-01-18 07:44:05 -0600849 hashtb_next(e);
850 }
851
852 hashtb_end(e);
853
854}
855
akmhoque8fdd6412012-12-04 15:05:33 -0600856char *
857process_api_client_command(char *command)
858{
859 char *msg;
860 msg=(char *)malloc(100);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600861 memset(msg,0,100);
akmhoque8fdd6412012-12-04 15:05:33 -0600862
863 const char *sep=" \t\n";
864 char *rem=NULL;
865 char *cmd_type=NULL;
866 char *op_type=NULL;
867 char *name=NULL;
868 char *face=NULL;
869 int face_id;
870 int res;
871
872 op_type=strtok_r(command,sep,&rem);
873 cmd_type=strtok_r(NULL,sep,&rem);
874 name=strtok_r(NULL,sep,&rem);
875 if ( name[strlen(name)-1] == '/' )
876 name[strlen(name)-1]='\0';
877
878 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
879 np->name=(char *)malloc(strlen(name)+1);
880 memset(np->name,0,strlen(name)+1);
881 memcpy(np->name,name,strlen(name)+1);
882 np->length=strlen(name)+1;
883
884 if ( strcmp(cmd_type,"name")!= 0 )
885 {
886 face=strtok_r(NULL,sep,&rem);
887 sscanf(face,"face%d",&face_id);
888 }
889
890 if ( strcmp(cmd_type,"name")== 0 )
891 {
892 if ( strcmp(op_type,"del") == 0 )
893 {
894 res=does_name_exist_in_npl(np);
895 if ( res == 0)
896 {
897 sprintf(msg,"Name %s does not exist !!",name);
898 }
899 else
900 {
901 long int ls_id=get_lsa_id_from_npl(np);
902 if ( ls_id != 0 )
903 {
904 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
905 sprintf(msg,"Name %s has been deleted and Advertised.",name);
906 }
907 else
908 {
909 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
910 }
911 }
912 }
913 else if ( strcmp(op_type,"add") == 0 )
914 {
915 res=does_name_exist_in_npl(np);
916 if ( res == 0)
917 {
918 add_name_to_npl(np);
919 build_and_install_single_name_lsa(np);
920 sprintf(msg,"Name %s has been added to advertise.",name);
921 }
922 else
923 {
924 sprintf(msg,"Name %s has already been advertised from this router !!",name);
925 }
926 }
927 }
928 else if ( strcmp(cmd_type,"neighbor") == 0 )
929 {
930 if ( strcmp(op_type,"del") == 0 )
931 {
932 res=is_neighbor(np->name);
933 if ( res == 0)
934 {
935 sprintf(msg,"Neighbor %s does not exist !!",name);
936 }
937 else
938 {
939 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoque496c88f2013-02-08 05:24:18 -0600940 int face_id=get_next_hop_face_from_adl(np->name);
941 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
942 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
943 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque8fdd6412012-12-04 15:05:33 -0600944 delete_nbr_from_adl(np);
945 if(!nlsr->is_build_adj_lsa_sheduled)
946 {
947 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
948 nlsr->is_build_adj_lsa_sheduled=1;
949 }
950 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
951 }
952 }
953 else if ( strcmp(op_type,"add") == 0 )
954 {
955 res=is_neighbor(np->name);
956 if ( res == 0 )
957 {
akmhoque09c0afa2012-12-14 09:27:00 -0600958 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
959 get_host_name_from_command_string(nbr_name,np->name,0);
960 printf("Hostname of neighbor: %s ",nbr_name->name);
961
akmhoquea37b52c2012-12-14 11:16:36 -0600962 char *ip_addr=(char *)malloc(13);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600963 memset(ip_addr,0,13);
akmhoquea37b52c2012-12-14 11:16:36 -0600964 get_ip_from_hostname_02(nbr_name->name,ip_addr);
965 printf("IP Address: %s \n",ip_addr);
akmhoque596f7082013-02-04 13:34:13 -0600966 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 -0600967 update_face_to_adl_for_nbr(nbr_name->name, face_id);
akmhoque496c88f2013-02-08 05:24:18 -0600968 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
969 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
akmhoquea37b52c2012-12-14 11:16:36 -0600970
971 add_nbr_to_adl(np,face_id,ip_addr);
972
akmhoque8fdd6412012-12-04 15:05:33 -0600973 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoquea37b52c2012-12-14 11:16:36 -0600974
akmhoque8fdd6412012-12-04 15:05:33 -0600975 }
976 else
977 {
978 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
979 }
980 }
981 }
982
983
984 return msg;
985}
986
987int
988nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
989{
990 struct timeval timeout;
akmhoque21ffec22013-01-31 18:22:10 -0600991 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque8fdd6412012-12-04 15:05:33 -0600992 {
akmhoque21ffec22013-01-31 18:22:10 -0600993 timeout.tv_sec=0;
994 timeout.tv_usec=time_out_micro_sec;
akmhoque8fdd6412012-12-04 15:05:33 -0600995 }
akmhoque21ffec22013-01-31 18:22:10 -0600996 else
akmhoque8fdd6412012-12-04 15:05:33 -0600997 {
akmhoque21ffec22013-01-31 18:22:10 -0600998 timeout.tv_sec = 0;
999 timeout.tv_usec = 500000;
akmhoque8fdd6412012-12-04 15:05:33 -06001000 }
akmhoque8fdd6412012-12-04 15:05:33 -06001001
1002 int fd;
1003 int nread;
1004 int result;
1005 fd_set testfds;
1006 unsigned int client_len;
1007 int client_sockfd;
1008 char recv_buffer[1024];
1009 bzero(recv_buffer,1024);
1010 struct sockaddr_in client_address;
1011
1012 testfds=nlsr->readfds;
1013 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1014
soamin29b8e392013-01-22 17:12:07 -06001015 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque8fdd6412012-12-04 15:05:33 -06001016 {
1017 if(FD_ISSET(fd,&testfds))
1018 {
1019 if ( fd == ccn_fd )
1020 {
1021 return 0;
1022 }
1023 else if(fd == nlsr->nlsr_api_server_sock_fd)
1024 {
1025 client_len = sizeof(client_address);
1026 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1027 FD_SET(client_sockfd, &nlsr->readfds);
1028 }
1029 else
1030 {
1031
1032 ioctl(fd, FIONREAD, &nread);
1033 if(nread == 0)
1034 {
1035 close(fd);
1036 FD_CLR(fd, &nlsr->readfds);
1037 }
1038 else
1039 {
1040 recv(fd, recv_buffer, 1024, 0);
1041 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
1042 char *msg=process_api_client_command(recv_buffer);
1043 send(fd, msg, strlen(msg),0);
1044 free(msg);
1045 close(fd);
1046 FD_CLR(fd, &nlsr->readfds);
1047 }
1048 }
1049 }
1050 }
1051
1052 return 0;
1053}
1054
1055void
1056nlsr_destroy( void )
1057{
1058 if ( nlsr->debugging )
1059 {
1060 printf("Freeing Allocated Memory....\n");
1061 }
1062 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
1063 /* Destroying all face created by nlsr in CCND */
1064 destroy_all_face_by_nlsr();
akmhoque61a57f22013-01-18 07:44:05 -06001065 destroy_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001066 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
1067 hashtb_destroy(&nlsr->npl);
1068 hashtb_destroy(&nlsr->adl);
1069 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1070 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
1071 hashtb_destroy(&nlsr->pit_alsa);
1072
akmhoqueed418f32013-01-30 12:25:04 -06001073
akmhoque8fdd6412012-12-04 15:05:33 -06001074
1075 hashtb_destroy(&nlsr->routing_table);
1076
1077
1078 int i, npt_element;
1079 struct npt_entry *ne;
1080 struct hashtb_enumerator ee;
1081 struct hashtb_enumerator *e = &ee;
1082 hashtb_start(nlsr->npt, e);
1083 npt_element=hashtb_n(nlsr->npt);
1084 for(i=0;i<npt_element;i++)
1085 {
1086 ne=e->data;
1087 hashtb_destroy(&ne->name_list);
1088 hashtb_destroy(&ne->face_list);
1089 hashtb_next(e);
1090 }
1091
1092 hashtb_end(e);
1093 hashtb_destroy(&nlsr->npt);
1094
1095
1096 ccns_close(&nlsr->ccns, NULL, NULL);
1097 ccns_slice_destroy(&nlsr->slice);
1098
1099
1100
1101 close(nlsr->nlsr_api_server_sock_fd);
1102
1103 ccn_schedule_destroy(&nlsr->sched);
1104 ccn_destroy(&nlsr->ccn);
1105
1106 free(nlsr->lsdb->lsdb_version);
1107 free(nlsr->lsdb);
1108 free(nlsr->router_name);
1109 free(nlsr);
1110 if ( nlsr->debugging )
1111 {
1112 printf("Finished freeing allocated memory\n");
1113 }
1114 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1115
1116}
1117
1118
1119
1120void
1121init_api_server(int ccn_fd)
1122{
1123 int server_sockfd;
1124 int server_len;
1125 struct sockaddr_in server_address;
1126 unsigned int yes=1;
1127
1128 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1129
1130 int flags = fcntl(server_sockfd, F_GETFL, 0);
1131 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1132
1133 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1134 {
1135 ON_ERROR_DESTROY(-1);
1136 }
1137
1138 server_address.sin_family = AF_INET;
Adam Alyyanc350e3a2013-01-14 14:35:57 -06001139 server_address.sin_addr.s_addr = INADDR_ANY;
1140 server_address.sin_port = htons(nlsr->api_port);
akmhoque8fdd6412012-12-04 15:05:33 -06001141
1142 server_len = sizeof(server_address);
1143 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1144 listen(server_sockfd, 100);
1145 FD_ZERO(&nlsr->readfds);
1146 FD_SET(server_sockfd, &nlsr->readfds);
1147 FD_SET(ccn_fd, &nlsr->readfds);
1148 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1149
1150}
1151
1152int
1153init_nlsr(void)
1154{
1155 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1156 {
1157 perror("SIGQUIT install error\n");
1158 return -1;
1159 }
1160 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1161 {
1162 perror("SIGTERM install error\n");
1163 return -1;
1164 }
1165 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1166 {
1167 perror("SIGTERM install error\n");
1168 return -1;
1169 }
1170
1171 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1172
1173 struct hashtb_param param_adl = {0};
1174 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1175 struct hashtb_param param_npl = {0};
1176 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1177 struct hashtb_param param_pit_alsa = {0};
1178 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1179 struct hashtb_param param_npt = {0};
1180 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1181 struct hashtb_param param_rte = {0};
1182 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1183
1184 nlsr->in_interest.p = &incoming_interest;
1185 nlsr->in_content.p = &incoming_content;
1186
1187 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1188
1189 char *time_stamp=(char *)malloc(20);
1190 memset(time_stamp,0,20);
1191 get_current_timestamp_micro(time_stamp);
1192 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueed418f32013-01-30 12:25:04 -06001193 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque8fdd6412012-12-04 15:05:33 -06001194 free(time_stamp);
1195
1196 struct hashtb_param param_adj_lsdb = {0};
1197 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1198 struct hashtb_param param_name_lsdb = {0};
1199 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueed418f32013-01-30 12:25:04 -06001200 struct hashtb_param param_cor_lsdb = {0};
1201 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque8fdd6412012-12-04 15:05:33 -06001202
1203
1204
1205
1206 nlsr->is_synch_init=1;
1207 nlsr->nlsa_id=0;
1208 nlsr->adj_build_flag=0;
1209 nlsr->adj_build_count=0;
1210 nlsr->is_build_adj_lsa_sheduled=0;
1211 nlsr->is_send_lsdb_interest_scheduled=0;
1212 nlsr->is_route_calculation_scheduled=0;
1213
1214 nlsr->detailed_logging=0;
1215 nlsr->debugging=0;
1216
1217 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
1218 nlsr->interest_retry = INTEREST_RETRY;
1219 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1220 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1221 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoque496c88f2013-02-08 05:24:18 -06001222 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoque8fdd6412012-12-04 15:05:33 -06001223 nlsr->semaphor=NLSR_UNLOCKED;
1224
1225 nlsr->api_port=API_PORT;
1226
akmhoque8fdd6412012-12-04 15:05:33 -06001227 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001228 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001229 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1230
1231 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001232 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001233 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1234
akmhoque09c0afa2012-12-14 09:27:00 -06001235 nlsr->is_hyperbolic_calc=0;
akmhoqueed418f32013-01-30 12:25:04 -06001236 nlsr->cor_r=-1.0;
1237 nlsr->cor_theta=-1.0;
akmhoque09c0afa2012-12-14 09:27:00 -06001238
akmhoque596f7082013-02-04 13:34:13 -06001239 nlsr->tunnel_type=IPPROTO_UDP;
1240
akmhoque8fdd6412012-12-04 15:05:33 -06001241 return 0;
1242}
1243
1244
1245int
1246main(int argc, char *argv[])
1247{
1248 int res, ret;
1249 char *config_file;
1250 int daemon_mode=0;
1251 int port=0;
1252
1253
1254
1255 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1256 {
1257 switch (res)
1258 {
1259 case 'd':
1260 daemon_mode = 1;
1261 break;
1262 case 'f':
1263 config_file = optarg;
1264 break;
1265 case 'p':
1266 port = atoi(optarg);
1267 break;
1268 case 'h':
1269 default:
1270 usage(argv[0]);
1271 }
1272 }
1273
1274 ret=init_nlsr();
1275 ON_ERROR_EXIT(ret);
1276
1277 if ( port !=0 )
1278 nlsr->api_port=port;
1279
1280 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001281
akmhoque596f7082013-02-04 13:34:13 -06001282 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
akmhoqueed418f32013-01-30 12:25:04 -06001283 {
1284 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1285 ON_ERROR_DESTROY(-1);
1286 }
akmhoquea37b52c2012-12-14 11:16:36 -06001287 print_adjacent_from_adl();
1288
akmhoque8fdd6412012-12-04 15:05:33 -06001289 if ( daemon_mode == 1 )
1290 {
akmhoquebaf53f12013-01-24 11:06:18 -06001291 nlsr->debugging=0;
akmhoque8fdd6412012-12-04 15:05:33 -06001292 daemonize_nlsr();
1293 }
1294
1295 startLogging(nlsr->logDir);
1296
1297 nlsr->ccn=ccn_create();
1298 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1299 if(ccn_fd == -1)
1300 {
1301 fprintf(stderr,"Could not connect to ccnd\n");
1302 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1303 ON_ERROR_DESTROY(-1);
1304 }
1305
1306 init_api_server(ccn_fd);
1307
akmhoquefc5176d2013-01-18 09:50:12 -06001308 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1309 if(res<0)
1310 {
1311 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1312 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1313 ON_ERROR_DESTROY(res);
1314 }
akmhoque8fdd6412012-12-04 15:05:33 -06001315 struct ccn_charbuf *router_prefix;
1316 router_prefix=ccn_charbuf_create();
1317 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1318 if(res<0)
1319 {
1320 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1321 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1322 ON_ERROR_DESTROY(res);
1323 }
1324
1325 ccn_name_append_str(router_prefix,"nlsr");
1326 nlsr->in_interest.data=nlsr->router_name;
1327 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1328 if ( res < 0 )
1329 {
1330 fprintf(stderr,"Failed to register interest for router\n");
1331 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1332 ON_ERROR_DESTROY(res);
1333 }
1334 ccn_charbuf_destroy(&router_prefix);
1335
1336 if ( nlsr->debugging )
1337 printf("Router Name : %s\n",nlsr->router_name);
1338 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1339 if ( nlsr->debugging )
1340 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1341 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1342
akmhoquea37b52c2012-12-14 11:16:36 -06001343 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001344 print_name_prefix_from_npl();
1345 print_adjacent_from_adl();
akmhoque596f7082013-02-04 13:34:13 -06001346 build_and_install_name_lsas();
akmhoque8fdd6412012-12-04 15:05:33 -06001347
1348 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1349
akmhoque596f7082013-02-04 13:34:13 -06001350
1351 print_name_lsdb();
1352 build_and_install_cor_lsa();
akmhoque8fdd6412012-12-04 15:05:33 -06001353 write_name_lsdb_to_repo(nlsr->slice_prefix);
1354
akmhoque8fdd6412012-12-04 15:05:33 -06001355 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001356 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1357 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001358
1359
1360 while(1)
1361 {
1362 if ( nlsr->semaphor == NLSR_UNLOCKED )
1363 {
1364 if( nlsr->sched != NULL )
1365 {
1366 long int micro_sec=ccn_schedule_run(nlsr->sched);
1367 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1368 ON_ERROR_DESTROY(res);
1369 }
1370 if(nlsr->ccn != NULL)
1371 {
akmhoque719a4fc2013-01-31 16:11:52 -06001372 res = ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -06001373 }
1374 if (!(nlsr->sched && nlsr->ccn))
1375 {
1376 break;
1377 }
1378 }
1379
1380 }
1381
1382
1383 return 0;
1384}
1385