blob: df637b72fe004258b61f8d81f303226378c5cdcd [file] [log] [blame]
akmhoque1771c412012-11-09 13:06:08 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
akmhoque59980a52012-08-09 12:36:09 -05004#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
akmhoquebfefef22012-09-26 10:09:34 -05007#include <sys/stat.h>
akmhoque59980a52012-08-09 12:36:09 -05008#include <assert.h>
akmhoque1771c412012-11-09 13:06:08 -06009#include <sys/types.h>
10#include <signal.h>
11#include <sys/socket.h>
12#include <sys/un.h>
13#include <fcntl.h>
14#include <sys/ioctl.h>
akmhoque95041802012-11-16 09:18:02 -060015#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
akmhoque1771c412012-11-09 13:06:08 -060018
akmhoque59980a52012-08-09 12:36:09 -050019#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
akmhoquebfefef22012-09-26 10:09:34 -050022
akmhoque59980a52012-08-09 12:36:09 -050023#include <ccn/ccn.h>
24#include <ccn/uri.h>
25#include <ccn/keystore.h>
26#include <ccn/signing.h>
27#include <ccn/schedule.h>
28#include <ccn/hashtb.h>
akmhoqueb77b95f2013-02-08 12:28:47 -060029#include <ccn/sync.h>
30#include <ccn/seqwriter.h>
akmhoque59980a52012-08-09 12:36:09 -050031
32#include "nlsr.h"
33#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050034#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050035#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050036#include "nlsr_npl.h"
37#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050038#include "nlsr_npt.h"
39#include "nlsr_route.h"
akmhoqueb77b95f2013-02-08 12:28:47 -060040#include "nlsr_sync.h"
41#include "nlsr_face.h"
42#include "nlsr_fib.h"
akmhoque3560cb62012-09-09 10:52:30 -050043
44
akmhoque81c25e02012-09-10 14:50:33 -050045#define ON_ERROR_DESTROY(resval) \
46{ \
47 if ((resval) < 0) { \
48 nlsr_destroy(); \
akmhoqueffacaa82012-09-13 17:48:30 -050049 exit(1);\
akmhoque81c25e02012-09-10 14:50:33 -050050 } \
51}
52
53
54#define ON_ERROR_EXIT(resval) \
55{ \
56 if ((resval) < 0) { \
akmhoqueffacaa82012-09-13 17:48:30 -050057 exit(1); \
akmhoque81c25e02012-09-10 14:50:33 -050058 } \
59}
akmhoque59980a52012-08-09 12:36:09 -050060
61struct option longopts[] =
62{
63 { "daemon", no_argument, NULL, 'd'},
64 { "config_file", required_argument, NULL, 'f'},
akmhoque95041802012-11-16 09:18:02 -060065 { "api_port", required_argument, NULL, 'p'},
akmhoque59980a52012-08-09 12:36:09 -050066 { "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\
akmhoque95041802012-11-16 09:18:02 -060078 -p, --api_port port where api client will connect\n\
akmhoque59980a52012-08-09 12:36:09 -050079 -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
akmhoqueffacaa82012-09-13 17:48:30 -050099void
100nlsr_lock(void)
101{
102 nlsr->semaphor=NLSR_LOCKED;
103}
104
105void
106nlsr_unlock(void)
107{
108 nlsr->semaphor=NLSR_UNLOCKED;
109}
akmhoque42098b12012-08-27 22:54:23 -0500110
111void
akmhoque03004e62012-09-06 01:12:28 -0500112nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -0500113{
akmhoque03004e62012-09-06 01:12:28 -0500114 signal(sig, SIG_IGN);
akmhoqueffacaa82012-09-13 17:48:30 -0500115 nlsr_destroy();
116 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500117}
118
akmhoquebfefef22012-09-26 10:09:34 -0500119void
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 {
akmhoque7b791452012-10-30 11:24:56 -0500128 printf("Daemonization failed!\n");
akmhoquebfefef22012-09-26 10:09:34 -0500129 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
akmhoque59980a52012-08-09 12:36:09 -0500151void
152process_command_ccnneighbor(char *command)
153{
154 if(command==NULL)
155 {
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 char *rem;
160 const char *sep=" \t\n";
akmhoqueb77b95f2013-02-08 12:28:47 -0600161 char *rtr_name;
162 char *nbr_ip_addr;
163 int is_ip_configured=0;
164 //char *face;
165 char *ip_addr=(char *)malloc(13);
166 memset(ip_addr,0,13);
akmhoque59980a52012-08-09 12:36:09 -0500167
akmhoque28c45022012-08-09 15:38:02 -0500168 rtr_name=strtok_r(command,sep,&rem);
169 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500170 {
akmhoque28c45022012-08-09 15:38:02 -0500171 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500172 return;
173 }
akmhoqued5152122012-09-19 06:44:23 -0500174 if ( rtr_name[strlen(rtr_name)-1] == '/' )
175 {
176 rtr_name[strlen(rtr_name)-1]='\0';
177 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600178
179 if (rem != NULL )
180 {
181 nbr_ip_addr=strtok_r(NULL,sep,&rem);
182 is_ip_configured=1;
183 }
akmhoque03004e62012-09-06 01:12:28 -0500184 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
akmhoqueb77b95f2013-02-08 12:28:47 -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 }
207 add_nbr_to_adl(nbr,0,ip_addr);
208
akmhoque03004e62012-09-06 01:12:28 -0500209
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
akmhoqued5152122012-09-19 06:44:23 -0500235 if ( name[strlen(name)-1] == '/' )
236 name[strlen(name)-1]='\0';
237
akmhoque53f64222012-09-05 13:57:51 -0500238 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500239 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;
akmhoque386081b2012-08-10 10:53:21 -0500243
akmhoque03004e62012-09-06 01:12:28 -0500244 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500245
akmhoque03004e62012-09-06 01:12:28 -0500246 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500247 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500248}
249
akmhoque03004e62012-09-06 01:12:28 -0500250
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
akmhoqued5152122012-09-19 06:44:23 -0500271 if ( rtr_name[strlen(rtr_name)-1] == '/' )
272 rtr_name[strlen(rtr_name)-1]='\0';
273
akmhoque03004e62012-09-06 01:12:28 -0500274 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
akmhoqueb77b95f2013-02-08 12:28:47 -0600281/*
akmhoque59980a52012-08-09 12:36:09 -0500282void
akmhoqued79438d2012-08-27 13:31:42 -0500283process_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);
akmhoqueffacaa82012-09-13 17:48:30 -0500303 if ( seconds >= 120 && seconds <= 3600 )
304 {
305 nlsr->lsdb_synch_interval=seconds;
306 }
akmhoqued79438d2012-08-27 13:31:42 -0500307
308}
akmhoqueb77b95f2013-02-08 12:28:47 -0600309*/
akmhoqued79438d2012-08-27 13:31:42 -0500310
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";
akmhoqueffacaa82012-09-13 17:48:30 -0500321 char *retry;
322 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500323
akmhoqueffacaa82012-09-13 17:48:30 -0500324 retry=strtok_r(command,sep,&rem);
325 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500326 {
327 printf(" Wrong Command Format ( interest-retry number)\n");
328 return;
329 }
330
akmhoqueffacaa82012-09-13 17:48:30 -0500331 retry_number=atoi(retry);
332 if ( retry_number >= 1 && retry_number<=10 )
333 {
334 nlsr->interest_retry=retry_number;
335 }
akmhoqued79438d2012-08-27 13:31:42 -0500336
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);
akmhoqueffacaa82012-09-13 17:48:30 -0500360 if ( seconds <= 60 && seconds >= 1 )
361 {
362 nlsr->interest_resend_time=seconds;
363 }
akmhoqued79438d2012-08-27 13:31:42 -0500364}
365
akmhoque03004e62012-09-06 01:12:28 -0500366
akmhoqued5152122012-09-19 06:44:23 -0500367void
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);
akmhoqueb77b95f2013-02-08 12:28:47 -0600388 if ( seconds >= 240)
akmhoqued5152122012-09-19 06:44:23 -0500389 {
390 nlsr->lsa_refresh_time=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600391 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
392 {
393 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
394 }
akmhoqued5152122012-09-19 06:44:23 -0500395 }
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);
akmhoqueb77b95f2013-02-08 12:28:47 -0600420 if ( seconds >= 480 )
akmhoqued5152122012-09-19 06:44:23 -0500421 {
422 nlsr->router_dead_interval=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600423 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
424 {
425 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
426 }
akmhoqued5152122012-09-19 06:44:23 -0500427 }
428
429}
akmhoque03004e62012-09-06 01:12:28 -0500430
akmhoqued79438d2012-08-27 13:31:42 -0500431void
akmhoqueb77b95f2013-02-08 12:28:47 -0600432process_command_max_faces_per_prefix(char *command)
akmhoque3cced642012-09-24 16:20:20 -0500433{
434 if(command==NULL)
435 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600436 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque3cced642012-09-24 16:20:20 -0500437 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 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600447 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque3cced642012-09-24 16:20:20 -0500448 return;
449 }
450
451 number=atoi(num);
452 if ( number >= 0 && number <= 60 )
453 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600454 nlsr->max_faces_per_prefix=number;
akmhoque3cced642012-09-24 16:20:20 -0500455 }
456
457}
458
459void
akmhoquebfefef22012-09-26 10:09:34 -0500460process_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
akmhoque7b791452012-10-30 11:24:56 -0500484process_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
akmhoqueb77b95f2013-02-08 12:28:47 -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);
556 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
557 topo_prefix[strlen(topo_prefix)-1]='\0';
558
559 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
560 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
561 puts(topo_prefix);
562 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
563
564 }
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);
590 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
591 slice_prefix[strlen(slice_prefix)-1]='\0';
592
593 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
594 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
595 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
596 }
597}
598
599void
600process_command_hyperbolic_routing(char *command)
601{
602 if(command==NULL)
603 {
604 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
605 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 {
614 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
615 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
624void
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
657void
658process_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 }
680 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
681 {
682 nlsr->tunnel_type=IPPROTO_UDP;
683 }
684}
685
akmhoque7b791452012-10-30 11:24:56 -0500686void
akmhoque59980a52012-08-09 12:36:09 -0500687process_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 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600710 /*else if(!strcmp(cmd_type,"lsdb-synch-interval") )
akmhoqued79438d2012-08-27 13:31:42 -0500711 {
712 process_command_lsdb_synch_interval(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600713 }*/
akmhoqued79438d2012-08-27 13:31:42 -0500714 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 }
akmhoqued5152122012-09-19 06:44:23 -0500722 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 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600730 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque3cced642012-09-24 16:20:20 -0500731 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600732 process_command_max_faces_per_prefix(remainder);
akmhoque3cced642012-09-24 16:20:20 -0500733 }
akmhoquebfefef22012-09-26 10:09:34 -0500734 else if(!strcmp(cmd_type,"logdir") )
735 {
736 process_command_logdir(remainder);
737 }
akmhoque7b791452012-10-30 11:24:56 -0500738 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 }
akmhoqueb77b95f2013-02-08 12:28:47 -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 }
754 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
755 {
756 process_command_hyperbolic_cordinate(remainder);
757 }
758 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 }
akmhoqued5152122012-09-19 06:44:23 -0500766 else
akmhoque59980a52012-08-09 12:36:09 -0500767 {
768 printf("Wrong configuration Command %s \n",cmd_type);
769 }
770}
771
akmhoque03004e62012-09-06 01:12:28 -0500772
akmhoque59980a52012-08-09 12:36:09 -0500773int
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';
akmhoqued5152122012-09-19 06:44:23 -0500793 if ( buf[0] != '#' && buf[0] != '!')
794 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500795 }
796
797 fclose(cfg);
798
799 return 0;
800}
801
akmhoqueb77b95f2013-02-08 12:28:47 -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;
818 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
819 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);
821 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
822 hashtb_next(e);
823 }
824
825 hashtb_end(e);
826
827}
828
829void
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 {
843 nbr=e->data;
844 if ( nbr->face > 0 )
845 {
846 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
847 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
848 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
849 }
850 hashtb_next(e);
851 }
852
853 hashtb_end(e);
854
855}
856
akmhoque562caef2012-11-09 13:29:06 -0600857char *
858process_api_client_command(char *command)
859{
860 char *msg;
861 msg=(char *)malloc(100);
akmhoqueb77b95f2013-02-08 12:28:47 -0600862 memset(msg,0,100);
863
akmhoque3171d652012-11-13 11:44:33 -0600864 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 }
akmhoque562caef2012-11-09 13:29:06 -0600890
akmhoque3171d652012-11-13 11:44:33 -0600891 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);
akmhoqueb77b95f2013-02-08 12:28:47 -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);
akmhoque3171d652012-11-13 11:44: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 {
akmhoqueb77b95f2013-02-08 12:28:47 -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
963 char *ip_addr=(char *)malloc(13);
964 memset(ip_addr,0,13);
965 get_ip_from_hostname_02(nbr_name->name,ip_addr);
966 printf("IP Address: %s \n",ip_addr);
967 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
968 update_face_to_adl_for_nbr(nbr_name->name, face_id);
969 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);
971
972 add_nbr_to_adl(np,face_id,ip_addr);
973
akmhoque3171d652012-11-13 11:44:33 -0600974 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoqueb77b95f2013-02-08 12:28:47 -0600975
akmhoque3171d652012-11-13 11:44:33 -0600976 }
977 else
978 {
979 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
980 }
981 }
982 }
983
akmhoque562caef2012-11-09 13:29:06 -0600984
985 return msg;
986}
akmhoque1771c412012-11-09 13:06:08 -0600987
988int
989nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
990{
991 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600992 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -0600993 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600994 timeout.tv_sec=0;
995 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -0600996 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600997 else
akmhoque1771c412012-11-09 13:06:08 -0600998 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600999 timeout.tv_sec = 0;
1000 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001001 }
akmhoque1771c412012-11-09 13:06:08 -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);
akmhoque95041802012-11-16 09:18:02 -06001011 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001012
1013 testfds=nlsr->readfds;
1014 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1015
akmhoqueb77b95f2013-02-08 12:28:47 -06001016 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -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 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001032
akmhoque1771c412012-11-09 13:06:08 -06001033 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);
akmhoqueb77b95f2013-02-08 12:28:47 -06001042 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001043 char *msg=process_api_client_command(recv_buffer);
1044 send(fd, msg, strlen(msg),0);
1045 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001046 close(fd);
1047 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001048 }
1049 }
1050 }
1051 }
1052
1053 return 0;
1054}
1055
akmhoqueb77b95f2013-02-08 12:28:47 -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
akmhoque386081b2012-08-10 10:53:21 -05001073void
1074nlsr_destroy( void )
1075{
akmhoque7b791452012-10-30 11:24:56 -05001076 if ( nlsr->debugging )
1077 {
1078 printf("Freeing Allocated Memory....\n");
1079 }
akmhoque9e9fc722012-09-26 14:03:25 -05001080 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001081 /* Destroying all face created by nlsr in CCND */
1082 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001083 destroy_faces_for_nbrs();
akmhoque386081b2012-08-10 10:53:21 -05001084 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -05001085 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -05001086 hashtb_destroy(&nlsr->adl);
1087 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1088 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001089 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -05001090
akmhoqueb77b95f2013-02-08 12:28:47 -06001091
akmhoque3cced642012-09-24 16:20:20 -05001092
akmhoque3560cb62012-09-09 10:52:30 -05001093 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;
akmhoque3cced642012-09-24 16:20:20 -05001105 hashtb_destroy(&ne->name_list);
1106 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -05001107 hashtb_next(e);
1108 }
1109
1110 hashtb_end(e);
1111 hashtb_destroy(&nlsr->npt);
1112
akmhoque95041802012-11-16 09:18:02 -06001113
akmhoqueb77b95f2013-02-08 12:28:47 -06001114 ccns_close(&nlsr->ccns, NULL, NULL);
1115 ccns_slice_destroy(&nlsr->slice);
1116
1117
1118
akmhoque95041802012-11-16 09:18:02 -06001119 close(nlsr->nlsr_api_server_sock_fd);
1120
akmhoque386081b2012-08-10 10:53:21 -05001121 ccn_schedule_destroy(&nlsr->sched);
1122 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001123
1124 free(nlsr->lsdb->lsdb_version);
1125 free(nlsr->lsdb);
1126 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -05001127 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -05001128 if ( nlsr->debugging )
1129 {
1130 printf("Finished freeing allocated memory\n");
1131 }
akmhoque9e9fc722012-09-26 14:03:25 -05001132 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -05001133
akmhoque386081b2012-08-10 10:53:21 -05001134}
1135
akmhoque03004e62012-09-06 01:12:28 -05001136
akmhoqueb77b95f2013-02-08 12:28:47 -06001137
akmhoque1771c412012-11-09 13:06:08 -06001138void
1139init_api_server(int ccn_fd)
1140{
1141 int server_sockfd;
1142 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001143 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001144 unsigned int yes=1;
1145
akmhoque95041802012-11-16 09:18:02 -06001146 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001147
1148 int flags = fcntl(server_sockfd, F_GETFL, 0);
1149 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1150
akmhoquef31f13b2012-11-16 09:42:24 -06001151 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1152 {
1153 ON_ERROR_DESTROY(-1);
1154 }
akmhoque95041802012-11-16 09:18:02 -06001155
1156 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001157 server_address.sin_addr.s_addr = INADDR_ANY;
1158 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001159
akmhoque1771c412012-11-09 13:06:08 -06001160 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
akmhoque81c25e02012-09-10 14:50:33 -05001170int
akmhoque902d57e2012-08-17 09:24:38 -05001171init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001172{
akmhoque03004e62012-09-06 01:12:28 -05001173 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1174 {
1175 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001176 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001177 }
1178 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1179 {
1180 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001181 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001182 }
1183 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1184 {
1185 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001186 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001187 }
akmhoque902d57e2012-08-17 09:24:38 -05001188
akmhoque59980a52012-08-09 12:36:09 -05001189 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -05001190
1191 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -05001192 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -05001193 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -06001194 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -05001195 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -05001196 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -05001197 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);
akmhoque29c1db52012-09-07 14:47:43 -05001201
akmhoque59980a52012-08-09 12:36:09 -05001202 nlsr->in_interest.p = &incoming_interest;
1203 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -05001204
akmhoque03004e62012-09-06 01:12:28 -05001205 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -05001206
akmhoque03004e62012-09-06 01:12:28 -05001207 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);
akmhoqueb77b95f2013-02-08 12:28:47 -06001211 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001212 free(time_stamp);
1213
1214 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001215 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -05001216 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001217 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueb77b95f2013-02-08 12:28:47 -06001218 struct hashtb_param param_cor_lsdb = {0};
1219 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001220
1221
akmhoque902d57e2012-08-17 09:24:38 -05001222
akmhoque53f64222012-09-05 13:57:51 -05001223
akmhoque59980a52012-08-09 12:36:09 -05001224 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001225 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001226 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001227 nlsr->adj_build_count=0;
1228 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001229 nlsr->is_send_lsdb_interest_scheduled=0;
1230 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001231
akmhoque7b791452012-10-30 11:24:56 -05001232 nlsr->detailed_logging=0;
1233 nlsr->debugging=0;
1234
akmhoqueb77b95f2013-02-08 12:28:47 -06001235 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoqued79438d2012-08-27 13:31:42 -05001236 nlsr->interest_retry = INTEREST_RETRY;
1237 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001238 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1239 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001240 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001241 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001242
akmhoque95041802012-11-16 09:18:02 -06001243 nlsr->api_port=API_PORT;
1244
akmhoqueb77b95f2013-02-08 12:28:47 -06001245 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
1246 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
1247 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1248
1249 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
1250 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
1251 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1252
1253 nlsr->is_hyperbolic_calc=0;
1254 nlsr->cor_r=-1.0;
1255 nlsr->cor_theta=-1.0;
1256
1257 nlsr->tunnel_type=IPPROTO_UDP;
1258
akmhoque81c25e02012-09-10 14:50:33 -05001259 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001260}
1261
akmhoque03004e62012-09-06 01:12:28 -05001262
akmhoque902d57e2012-08-17 09:24:38 -05001263int
1264main(int argc, char *argv[])
1265{
akmhoque81c25e02012-09-10 14:50:33 -05001266 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -05001267 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001268 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001269 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001270
akmhoquebfefef22012-09-26 10:09:34 -05001271
akmhoque81c25e02012-09-10 14:50:33 -05001272
akmhoque95041802012-11-16 09:18:02 -06001273 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001274 {
1275 switch (res)
1276 {
1277 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001278 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001279 break;
1280 case 'f':
1281 config_file = optarg;
1282 break;
akmhoque95041802012-11-16 09:18:02 -06001283 case 'p':
1284 port = atoi(optarg);
1285 break;
akmhoque59980a52012-08-09 12:36:09 -05001286 case 'h':
1287 default:
1288 usage(argv[0]);
1289 }
1290 }
1291
akmhoquebfefef22012-09-26 10:09:34 -05001292 ret=init_nlsr();
1293 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001294
1295 if ( port !=0 )
1296 nlsr->api_port=port;
1297
akmhoque59980a52012-08-09 12:36:09 -05001298 readConfigFile(config_file);
akmhoqueb77b95f2013-02-08 12:28:47 -06001299
1300 ON_ERROR_DESTROY(check_config_validity());
1301
1302 print_adjacent_from_adl();
1303
akmhoquebfefef22012-09-26 10:09:34 -05001304 if ( daemon_mode == 1 )
1305 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001306 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001307 daemonize_nlsr();
1308 }
1309
1310 startLogging(nlsr->logDir);
1311
akmhoque59980a52012-08-09 12:36:09 -05001312 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001313 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1314 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001315 {
1316 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001317 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001318 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001319 }
akmhoque1771c412012-11-09 13:06:08 -06001320
1321 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001322
1323 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 }
akmhoque53f64222012-09-05 13:57:51 -05001330 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001331 router_prefix=ccn_charbuf_create();
1332 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001333 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001334 {
1335 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001336 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001337 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001338 }
akmhoque59980a52012-08-09 12:36:09 -05001339
1340 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001341 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001342 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1343 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001344 {
1345 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001346 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001347 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001348 }
1349 ccn_charbuf_destroy(&router_prefix);
1350
akmhoque7b791452012-10-30 11:24:56 -05001351 if ( nlsr->debugging )
1352 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001353 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001354 if ( nlsr->debugging )
1355 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001356 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001357
akmhoqueb77b95f2013-02-08 12:28:47 -06001358 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001359 print_name_prefix_from_npl();
1360 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001361 build_and_install_name_lsas();
1362
1363 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1364
1365
1366 print_name_lsdb();
1367 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1368 {
1369 build_and_install_cor_lsa();
1370 }
1371 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001372
1373 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001374 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001375 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001376
akmhoque1771c412012-11-09 13:06:08 -06001377
akmhoque59980a52012-08-09 12:36:09 -05001378 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001379 {
akmhoqueffacaa82012-09-13 17:48:30 -05001380 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001381 {
akmhoqueffacaa82012-09-13 17:48:30 -05001382 if( nlsr->sched != NULL )
1383 {
akmhoque1771c412012-11-09 13:06:08 -06001384 long int micro_sec=ccn_schedule_run(nlsr->sched);
1385 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1386 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001387 }
1388 if(nlsr->ccn != NULL)
1389 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001390 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001391 }
1392 if (!(nlsr->sched && nlsr->ccn))
1393 {
1394 break;
1395 }
akmhoque29c1db52012-09-07 14:47:43 -05001396 }
1397
akmhoque59980a52012-08-09 12:36:09 -05001398 }
akmhoque1771c412012-11-09 13:06:08 -06001399
akmhoque59980a52012-08-09 12:36:09 -05001400
akmhoque59980a52012-08-09 12:36:09 -05001401 return 0;
1402}
1403