blob: 2efd619f2c76614b7f787601def300b54afc4374 [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>
akmhoque184dde02013-02-14 15:53:24 -060031#include <ccn/ccn_private.h>
akmhoque59980a52012-08-09 12:36:09 -050032
33#include "nlsr.h"
34#include "nlsr_ndn.h"
akmhoque902d57e2012-08-17 09:24:38 -050035#include "nlsr_lsdb.h"
akmhoque53f64222012-09-05 13:57:51 -050036#include "utility.h"
akmhoque03004e62012-09-06 01:12:28 -050037#include "nlsr_npl.h"
38#include "nlsr_adl.h"
akmhoque3560cb62012-09-09 10:52:30 -050039#include "nlsr_npt.h"
40#include "nlsr_route.h"
akmhoqueb77b95f2013-02-08 12:28:47 -060041#include "nlsr_sync.h"
42#include "nlsr_face.h"
43#include "nlsr_fib.h"
akmhoque3560cb62012-09-09 10:52:30 -050044
45
akmhoque81c25e02012-09-10 14:50:33 -050046#define ON_ERROR_DESTROY(resval) \
47{ \
48 if ((resval) < 0) { \
49 nlsr_destroy(); \
akmhoqueffacaa82012-09-13 17:48:30 -050050 exit(1);\
akmhoque81c25e02012-09-10 14:50:33 -050051 } \
52}
53
54
55#define ON_ERROR_EXIT(resval) \
56{ \
57 if ((resval) < 0) { \
akmhoqueffacaa82012-09-13 17:48:30 -050058 exit(1); \
akmhoque81c25e02012-09-10 14:50:33 -050059 } \
60}
akmhoque59980a52012-08-09 12:36:09 -050061
62struct option longopts[] =
63{
64 { "daemon", no_argument, NULL, 'd'},
65 { "config_file", required_argument, NULL, 'f'},
akmhoque95041802012-11-16 09:18:02 -060066 { "api_port", required_argument, NULL, 'p'},
akmhoque59980a52012-08-09 12:36:09 -050067 { "help", no_argument, NULL, 'h'},
68 { 0 }
69};
70
71static int
72usage(char *progname)
73{
74
75 printf("Usage: %s [OPTIONS...]\n\
76 NDN routing....\n\
77 -d, --daemon Run in daemon mode\n\
78 -f, --config_file Specify configuration file name\n\
akmhoque95041802012-11-16 09:18:02 -060079 -p, --api_port port where api client will connect\n\
akmhoque59980a52012-08-09 12:36:09 -050080 -h, --help Display this help message\n", progname);
81
82 exit(1);
83}
84
85void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result)
86{
87 struct timeval now = {0};
88 gettimeofday(&now, 0);
89 result->s = now.tv_sec;
90 result->micros = now.tv_usec;
91}
92
93static struct ccn_gettime ndn_rtr_ticker = {
94 "timer",
95 &ndn_rtr_gettime,
96 1000000,
97 NULL
98};
99
akmhoqueffacaa82012-09-13 17:48:30 -0500100void
101nlsr_lock(void)
102{
103 nlsr->semaphor=NLSR_LOCKED;
104}
105
106void
107nlsr_unlock(void)
108{
109 nlsr->semaphor=NLSR_UNLOCKED;
110}
akmhoque42098b12012-08-27 22:54:23 -0500111
112void
akmhoque03004e62012-09-06 01:12:28 -0500113nlsr_stop_signal_handler(int sig)
akmhoque42098b12012-08-27 22:54:23 -0500114{
akmhoque03004e62012-09-06 01:12:28 -0500115 signal(sig, SIG_IGN);
akmhoqueffacaa82012-09-13 17:48:30 -0500116 nlsr_destroy();
117 exit(0);
akmhoque59980a52012-08-09 12:36:09 -0500118}
119
akmhoquebfefef22012-09-26 10:09:34 -0500120void
121daemonize_nlsr(void)
122{
akmhoqueb28579d2013-02-12 11:15:52 -0600123 //int ret;
akmhoquebfefef22012-09-26 10:09:34 -0500124 pid_t process_id = 0;
125 pid_t sid = 0;
126 process_id = fork();
127 if (process_id < 0)
128 {
akmhoque7b791452012-10-30 11:24:56 -0500129 printf("Daemonization failed!\n");
akmhoquebfefef22012-09-26 10:09:34 -0500130 ON_ERROR_DESTROY(process_id);
131 }
132 if (process_id > 0)
133 {
134 printf("Process daemonized. Process id: %d \n", process_id);
akmhoqueb28579d2013-02-12 11:15:52 -0600135 //ret=process_id;
akmhoquebfefef22012-09-26 10:09:34 -0500136 exit(0);
137 }
138
139 umask(0);
140 sid = setsid();
141 if(sid < 0)
142 {
143 ON_ERROR_DESTROY(sid);
144 }
145
146 chdir("/");
147 close(STDIN_FILENO);
148 close(STDOUT_FILENO);
149 close(STDERR_FILENO);
150}
151
akmhoque59980a52012-08-09 12:36:09 -0500152void
153process_command_ccnneighbor(char *command)
154{
155 if(command==NULL)
156 {
akmhoque28c45022012-08-09 15:38:02 -0500157 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500158 return;
159 }
160 char *rem;
161 const char *sep=" \t\n";
akmhoqueb77b95f2013-02-08 12:28:47 -0600162 char *rtr_name;
163 char *nbr_ip_addr;
164 int is_ip_configured=0;
165 //char *face;
akmhoque7c234e02013-02-13 11:23:56 -0600166 char *ip_addr=(char *)calloc(20,sizeof(char));
167 //memset(ip_addr,0,12);
akmhoque59980a52012-08-09 12:36:09 -0500168
akmhoque28c45022012-08-09 15:38:02 -0500169 rtr_name=strtok_r(command,sep,&rem);
170 if(rtr_name==NULL)
akmhoque59980a52012-08-09 12:36:09 -0500171 {
akmhoque28c45022012-08-09 15:38:02 -0500172 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
akmhoque59980a52012-08-09 12:36:09 -0500173 return;
174 }
akmhoqued5152122012-09-19 06:44:23 -0500175 if ( rtr_name[strlen(rtr_name)-1] == '/' )
176 {
177 rtr_name[strlen(rtr_name)-1]='\0';
178 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600179
180 if (rem != NULL )
181 {
182 nbr_ip_addr=strtok_r(NULL,sep,&rem);
akmhoque958ccf72013-02-11 10:42:03 -0600183 if ( nbr_ip_addr != NULL)
184 is_ip_configured=1;
akmhoqueb77b95f2013-02-08 12:28:47 -0600185 }
akmhoque7c234e02013-02-13 11:23:56 -0600186 struct name_prefix *nbr=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
187 nbr->name=(char *)calloc(strlen(rtr_name)+1,sizeof(char));
188 //memset(nbr->name,0,strlen(rtr_name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500189 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
190 nbr->length=strlen(rtr_name)+1;
191
akmhoqueb77b95f2013-02-08 12:28:47 -0600192
193 if ( !is_ip_configured )
194 {
akmhoque7c234e02013-02-13 11:23:56 -0600195 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600196 get_host_name_from_command_string(nbr_name,nbr->name,0);
akmhoque018692c2013-02-11 11:33:39 -0600197 if ( nlsr->debugging)
198 printf("Hostname of neighbor: %s ",nbr_name->name);
akmhoqueb77b95f2013-02-08 12:28:47 -0600199 get_ip_from_hostname_02(nbr_name->name,ip_addr);
akmhoque018692c2013-02-11 11:33:39 -0600200 if ( nlsr->debugging)
201 printf("IP Address: %s \n",ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600202 free(nbr_name->name);
203 free(nbr_name);
204 }
205 else
206 {
207 memcpy(ip_addr,nbr_ip_addr,strlen(nbr_ip_addr));
akmhoque018692c2013-02-11 11:33:39 -0600208 if (nlsr->debugging)
209 {
210 printf("Name of neighbor: %s ",nbr->name);
211 printf("IP Address: %s \n",ip_addr);
212 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600213 }
214 add_nbr_to_adl(nbr,0,ip_addr);
215
akmhoque03004e62012-09-06 01:12:28 -0500216
217 free(nbr->name);
218 free(nbr);
219}
220
221void
222process_command_ccnname(char *command)
223{
224
225 if(command==NULL)
226 {
227 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
228 return;
229 }
230 char *rem;
231 const char *sep=" \t\n";
232 char *name;
233 name=strtok_r(command,sep,&rem);
234 if(name==NULL)
235 {
236 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
237 return;
238 }
239
240 printf("Name Prefix: %s \n",name);
241
akmhoqued5152122012-09-19 06:44:23 -0500242 if ( name[strlen(name)-1] == '/' )
243 name[strlen(name)-1]='\0';
244
akmhoque7c234e02013-02-13 11:23:56 -0600245 struct name_prefix *np=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
246 np->name=(char *)calloc(strlen(name)+1,sizeof(char));
247 //memset(np->name,0,strlen(name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500248 memcpy(np->name,name,strlen(name)+1);
249 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500250
akmhoque03004e62012-09-06 01:12:28 -0500251 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500252
akmhoque03004e62012-09-06 01:12:28 -0500253 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500254 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500255}
256
akmhoque03004e62012-09-06 01:12:28 -0500257
258void
259process_command_router_name(char *command)
260{
261 if(command==NULL)
262 {
263 printf(" Wrong Command Format ( router-name /router/name )\n");
264 return;
265 }
266 char *rem;
267 const char *sep=" \t\n";
268 char *rtr_name;
269
270 rtr_name=strtok_r(command,sep,&rem);
271 if(rtr_name==NULL)
272 {
273 printf(" Wrong Command Format ( router-name /router/name )\n");
274 return;
275 }
276
277
akmhoqued5152122012-09-19 06:44:23 -0500278 if ( rtr_name[strlen(rtr_name)-1] == '/' )
279 rtr_name[strlen(rtr_name)-1]='\0';
280
akmhoque7c234e02013-02-13 11:23:56 -0600281 nlsr->router_name=(char *)calloc(strlen(rtr_name)+1,sizeof(char));
282 //memset(nlsr->router_name,0,strlen(rtr_name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500283 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
284
285
286}
287
akmhoqueb77b95f2013-02-08 12:28:47 -0600288/*
akmhoque59980a52012-08-09 12:36:09 -0500289void
akmhoqued79438d2012-08-27 13:31:42 -0500290process_command_lsdb_synch_interval(char *command)
291{
292 if(command==NULL)
293 {
294 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
295 return;
296 }
297 char *rem;
298 const char *sep=" \t\n";
299 char *secs;
300 long int seconds;
301
302 secs=strtok_r(command,sep,&rem);
303 if(secs==NULL)
304 {
305 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
306 return;
307 }
308
309 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500310 if ( seconds >= 120 && seconds <= 3600 )
311 {
312 nlsr->lsdb_synch_interval=seconds;
313 }
akmhoqued79438d2012-08-27 13:31:42 -0500314
315}
akmhoqueb77b95f2013-02-08 12:28:47 -0600316*/
akmhoqued79438d2012-08-27 13:31:42 -0500317
318void
319process_command_interest_retry(char *command)
320{
321 if(command==NULL)
322 {
323 printf(" Wrong Command Format ( interest-retry number )\n");
324 return;
325 }
326 char *rem;
327 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500328 char *retry;
329 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500330
akmhoqueffacaa82012-09-13 17:48:30 -0500331 retry=strtok_r(command,sep,&rem);
332 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500333 {
334 printf(" Wrong Command Format ( interest-retry number)\n");
335 return;
336 }
337
akmhoqueffacaa82012-09-13 17:48:30 -0500338 retry_number=atoi(retry);
339 if ( retry_number >= 1 && retry_number<=10 )
340 {
341 nlsr->interest_retry=retry_number;
342 }
akmhoqued79438d2012-08-27 13:31:42 -0500343
344}
345
346void
347process_command_interest_resend_time(char *command)
348{
349 if(command==NULL)
350 {
351 printf(" Wrong Command Format ( interest-resend-time secs )\n");
352 return;
353 }
354 char *rem;
355 const char *sep=" \t\n";
356 char *secs;
357 long int seconds;
358
359 secs=strtok_r(command,sep,&rem);
360 if(secs==NULL)
361 {
362 printf(" Wrong Command Format ( interest-resend-time secs)\n");
363 return;
364 }
365
366 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500367 if ( seconds <= 60 && seconds >= 1 )
368 {
369 nlsr->interest_resend_time=seconds;
370 }
akmhoqued79438d2012-08-27 13:31:42 -0500371}
372
akmhoque03004e62012-09-06 01:12:28 -0500373
akmhoqued5152122012-09-19 06:44:23 -0500374void
375process_command_lsa_refresh_time(char *command)
376{
377 if(command==NULL)
378 {
379 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
380 return;
381 }
382 char *rem;
383 const char *sep=" \t\n";
384 char *secs;
385 long int seconds;
386
387 secs=strtok_r(command,sep,&rem);
388 if(secs==NULL)
389 {
390 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
391 return;
392 }
393
394 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600395 if ( seconds >= 240)
akmhoqued5152122012-09-19 06:44:23 -0500396 {
397 nlsr->lsa_refresh_time=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600398 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
399 {
400 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
401 }
akmhoqued5152122012-09-19 06:44:23 -0500402 }
403
404}
405
406void
407process_command_router_dead_interval(char *command)
408{
409 if(command==NULL)
410 {
411 printf(" Wrong Command Format ( router-dead-interval secs )\n");
412 return;
413 }
414 char *rem;
415 const char *sep=" \t\n";
416 char *secs;
417 long int seconds;
418
419 secs=strtok_r(command,sep,&rem);
420 if(secs==NULL)
421 {
422 printf(" Wrong Command Format ( router-dead-interval secs)\n");
423 return;
424 }
425
426 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600427 if ( seconds >= 480 )
akmhoqued5152122012-09-19 06:44:23 -0500428 {
429 nlsr->router_dead_interval=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600430 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
431 {
432 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
433 }
akmhoqued5152122012-09-19 06:44:23 -0500434 }
435
436}
akmhoque03004e62012-09-06 01:12:28 -0500437
akmhoqued79438d2012-08-27 13:31:42 -0500438void
akmhoqueb77b95f2013-02-08 12:28:47 -0600439process_command_max_faces_per_prefix(char *command)
akmhoque3cced642012-09-24 16:20:20 -0500440{
441 if(command==NULL)
442 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600443 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque3cced642012-09-24 16:20:20 -0500444 return;
445 }
446 char *rem;
447 const char *sep=" \t\n";
448 char *num;
449 long int number;
450
451 num=strtok_r(command,sep,&rem);
452 if(num==NULL)
453 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600454 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque3cced642012-09-24 16:20:20 -0500455 return;
456 }
457
458 number=atoi(num);
459 if ( number >= 0 && number <= 60 )
460 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600461 nlsr->max_faces_per_prefix=number;
akmhoque3cced642012-09-24 16:20:20 -0500462 }
463
464}
465
466void
akmhoquebfefef22012-09-26 10:09:34 -0500467process_command_logdir(char *command)
468{
469 if(command==NULL)
470 {
471 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
472 return;
473 }
474 char *rem;
475 const char *sep=" \t\n";
476 char *dir;
477
478 dir=strtok_r(command,sep,&rem);
479 if(dir==NULL)
480 {
481 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
482 return;
483 }
484
akmhoque7c234e02013-02-13 11:23:56 -0600485 nlsr->logDir=(char *)calloc(strlen(dir)+1,sizeof(char));
486 //memset(nlsr->logDir,0,strlen(dir)+1);
akmhoquebfefef22012-09-26 10:09:34 -0500487 memcpy(nlsr->logDir,dir,strlen(dir));
488}
489
490void
akmhoque7b791452012-10-30 11:24:56 -0500491process_command_detailed_log(char *command)
492{
493 if(command==NULL)
494 {
495 printf(" Wrong Command Format ( detailed-log on/off )\n");
496 return;
497 }
498 char *rem;
499 const char *sep=" \t\n";
500 char *on_off;
501
502 on_off=strtok_r(command,sep,&rem);
503 if(on_off==NULL)
504 {
505 printf(" Wrong Command Format ( detailed-log on/off )\n");
506 return;
507 }
508
509 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
510 {
511 nlsr->detailed_logging=1;
512 }
513}
514
515void
516process_command_debug(char *command)
517{
518 if(command==NULL)
519 {
520 printf(" Wrong Command Format ( debug on/off )\n");
521 return;
522 }
523 char *rem;
524 const char *sep=" \t\n";
525 char *on_off;
526
527 on_off=strtok_r(command,sep,&rem);
528 if(on_off==NULL)
529 {
530 printf(" Wrong Command Format ( debug on/off )\n");
531 return;
532 }
533
534 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
535 {
536 nlsr->debugging=1;
537 }
538}
539
akmhoqueb77b95f2013-02-08 12:28:47 -0600540
541void
542process_command_topo_prefix(char *command)
543{
544 if(command==NULL)
545 {
546 printf(" Wrong Command Format ( topo-prefix )\n");
547 return;
548 }
549 char *rem;
550 const char *sep=" \t\n";
551 char *topo_prefix;
552
553 topo_prefix=strtok_r(command,sep,&rem);
554 if(topo_prefix==NULL)
555 {
556 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
557 return;
558 }
559 else
560 {
561 if( nlsr->topo_prefix != NULL)
562 free(nlsr->topo_prefix);
563 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
564 topo_prefix[strlen(topo_prefix)-1]='\0';
565
akmhoque7c234e02013-02-13 11:23:56 -0600566 nlsr->topo_prefix=(char *)calloc(strlen(topo_prefix)+1,sizeof(char));
567 //memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600568 puts(topo_prefix);
569 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
570
571 }
572}
573
574
575void
576process_command_slice_prefix(char *command)
577{
578 if(command==NULL)
579 {
580 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
581 return;
582 }
583 char *rem;
584 const char *sep=" \t\n";
585 char *slice_prefix;
586
587 slice_prefix=strtok_r(command,sep,&rem);
588 if(slice_prefix==NULL)
589 {
590 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
591 return;
592 }
593 else
594 {
595 if ( nlsr->slice_prefix != NULL)
596 free(nlsr->slice_prefix);
597 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
598 slice_prefix[strlen(slice_prefix)-1]='\0';
599
akmhoque7c234e02013-02-13 11:23:56 -0600600 nlsr->slice_prefix=(char *)calloc(strlen(slice_prefix)+1,sizeof(char));
601 //memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600602 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
603 }
604}
605
606void
607process_command_hyperbolic_routing(char *command)
608{
609 if(command==NULL)
610 {
611 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
612 return;
613 }
614 char *rem;
615 const char *sep=" \t\n";
616 char *on_off;
617
618 on_off=strtok_r(command,sep,&rem);
619 if(on_off==NULL)
620 {
621 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
622 return;
623 }
624
625 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
626 {
627 nlsr->is_hyperbolic_calc=1;
628 }
629}
630
631void
632process_command_hyperbolic_cordinate(char *command)
633{
634 if(command==NULL)
635 {
636 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
637 return;
638 }
639
640 char *rem;
641 const char *sep=" \t\n\r";
642 char *radious;
643 char *theta;
644
645 radious=strtok_r(command,sep,&rem);
646 if (radious == NULL )
647 {
648 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
649 return;
650 }
651
652 theta=strtok_r(NULL,sep,&rem);
653 if (theta == NULL )
654 {
655 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
656 return;
657 }
658
659 nlsr->cor_r=strtof(radious,NULL);
660 nlsr->cor_theta=strtof(theta,NULL);
661
662}
663
664void
665process_command_tunnel_type(char *command)
666{
667 if(command==NULL)
668 {
669 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
670 return;
671 }
672 char *rem;
673 const char *sep=" \t\n";
674 char *on_off;
675
676 on_off=strtok_r(command,sep,&rem);
677 if(on_off==NULL)
678 {
679 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
680 return;
681 }
682
683 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
684 {
685 nlsr->tunnel_type=IPPROTO_TCP;
686 }
687 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
688 {
689 nlsr->tunnel_type=IPPROTO_UDP;
690 }
691}
692
akmhoque7b791452012-10-30 11:24:56 -0500693void
akmhoque59980a52012-08-09 12:36:09 -0500694process_conf_command(char *command)
695{
696 const char *separators=" \t\n";
697 char *remainder=NULL;
698 char *cmd_type=NULL;
699
700 if(command==NULL || strlen(command)==0 || command[0]=='!')
701 return;
702
703 cmd_type=strtok_r(command,separators,&remainder);
704
705 if(!strcmp(cmd_type,"router-name") )
706 {
707 process_command_router_name(remainder);
708 }
709 else if(!strcmp(cmd_type,"ccnneighbor") )
710 {
711 process_command_ccnneighbor(remainder);
712 }
713 else if(!strcmp(cmd_type,"ccnname") )
714 {
715 process_command_ccnname(remainder);
716 }
akmhoqued79438d2012-08-27 13:31:42 -0500717 else if(!strcmp(cmd_type,"interest-retry") )
718 {
719 process_command_interest_retry(remainder);
720 }
721 else if(!strcmp(cmd_type,"interest-resend-time") )
722 {
723 process_command_interest_resend_time(remainder);
724 }
akmhoqued5152122012-09-19 06:44:23 -0500725 else if(!strcmp(cmd_type,"lsa-refresh-time") )
726 {
727 process_command_lsa_refresh_time(remainder);
728 }
729 else if(!strcmp(cmd_type,"router-dead-interval") )
730 {
731 process_command_router_dead_interval(remainder);
732 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600733 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque3cced642012-09-24 16:20:20 -0500734 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600735 process_command_max_faces_per_prefix(remainder);
akmhoque3cced642012-09-24 16:20:20 -0500736 }
akmhoquebfefef22012-09-26 10:09:34 -0500737 else if(!strcmp(cmd_type,"logdir") )
738 {
739 process_command_logdir(remainder);
740 }
akmhoque7b791452012-10-30 11:24:56 -0500741 else if(!strcmp(cmd_type,"detailed-log") )
742 {
743 process_command_detailed_log(remainder);
744 }
745 else if(!strcmp(cmd_type,"debug") )
746 {
747 process_command_debug(remainder);
748 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600749 else if(!strcmp(cmd_type,"topo-prefix") )
750 {
751 process_command_topo_prefix(remainder);
752 }
753 else if(!strcmp(cmd_type,"slice-prefix") )
754 {
755 process_command_slice_prefix(remainder);
756 }
757 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
758 {
759 process_command_hyperbolic_cordinate(remainder);
760 }
761 else if(!strcmp(cmd_type,"hyperbolic-routing") )
762 {
763 process_command_hyperbolic_routing(remainder);
764 }
765 else if(!strcmp(cmd_type,"tunnel-type") )
766 {
767 process_command_tunnel_type(remainder);
768 }
akmhoqued5152122012-09-19 06:44:23 -0500769 else
akmhoque59980a52012-08-09 12:36:09 -0500770 {
771 printf("Wrong configuration Command %s \n",cmd_type);
772 }
773}
774
akmhoque03004e62012-09-06 01:12:28 -0500775
akmhoque59980a52012-08-09 12:36:09 -0500776int
777readConfigFile(const char *filename)
778{
779 FILE *cfg;
780 char buf[1024];
781 int len;
782
783 cfg=fopen(filename, "r");
784
785 if(cfg == NULL)
786 {
787 printf("\nConfiguration File does not exists\n");
788 exit(1);
789 }
790
791 while(fgets((char *)buf, sizeof(buf), cfg))
792 {
793 len=strlen(buf);
794 if(buf[len-1] == '\n')
795 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500796 if ( buf[0] != '#' && buf[0] != '!')
797 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500798 }
799
800 fclose(cfg);
801
802 return 0;
803}
804
akmhoqueb77b95f2013-02-08 12:28:47 -0600805
806void
807add_faces_for_nbrs(void)
808{
809 int i, adl_element;
810 struct ndn_neighbor *nbr;
811
812 struct hashtb_enumerator ee;
813 struct hashtb_enumerator *e = &ee;
814
815 hashtb_start(nlsr->adl, e);
816 adl_element=hashtb_n(nlsr->adl);
817
818 for(i=0;i<adl_element;i++)
819 {
820 nbr=e->data;
821 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
822 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
823 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
824 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
825 hashtb_next(e);
826 }
827
828 hashtb_end(e);
829
830}
831
832void
833destroy_faces_for_nbrs(void)
834{
835 int i, adl_element;
836 struct ndn_neighbor *nbr;
837
838 struct hashtb_enumerator ee;
839 struct hashtb_enumerator *e = &ee;
840
841 hashtb_start(nlsr->adl, e);
842 adl_element=hashtb_n(nlsr->adl);
843
844 for(i=0;i<adl_element;i++)
845 {
846 nbr=e->data;
847 if ( nbr->face > 0 )
848 {
849 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
850 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
851 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
852 }
853 hashtb_next(e);
854 }
855
856 hashtb_end(e);
857
858}
859
akmhoque562caef2012-11-09 13:29:06 -0600860char *
861process_api_client_command(char *command)
862{
863 char *msg;
864 msg=(char *)malloc(100);
akmhoqueb77b95f2013-02-08 12:28:47 -0600865 memset(msg,0,100);
866
akmhoque3171d652012-11-13 11:44:33 -0600867 const char *sep=" \t\n";
868 char *rem=NULL;
869 char *cmd_type=NULL;
870 char *op_type=NULL;
871 char *name=NULL;
872 char *face=NULL;
873 int face_id;
874 int res;
875
876 op_type=strtok_r(command,sep,&rem);
877 cmd_type=strtok_r(NULL,sep,&rem);
878 name=strtok_r(NULL,sep,&rem);
879 if ( name[strlen(name)-1] == '/' )
880 name[strlen(name)-1]='\0';
881
akmhoque7c234e02013-02-13 11:23:56 -0600882 struct name_prefix *np=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
883 np->name=(char *)calloc(strlen(name)+1,sizeof(char));
884 //memset(np->name,0,strlen(name)+1);
akmhoque3171d652012-11-13 11:44:33 -0600885 memcpy(np->name,name,strlen(name)+1);
886 np->length=strlen(name)+1;
887
888 if ( strcmp(cmd_type,"name")!= 0 )
889 {
890 face=strtok_r(NULL,sep,&rem);
891 sscanf(face,"face%d",&face_id);
892 }
akmhoque562caef2012-11-09 13:29:06 -0600893
akmhoque3171d652012-11-13 11:44:33 -0600894 if ( strcmp(cmd_type,"name")== 0 )
895 {
896 if ( strcmp(op_type,"del") == 0 )
897 {
898 res=does_name_exist_in_npl(np);
899 if ( res == 0)
900 {
901 sprintf(msg,"Name %s does not exist !!",name);
902 }
903 else
904 {
905 long int ls_id=get_lsa_id_from_npl(np);
906 if ( ls_id != 0 )
907 {
908 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
909 sprintf(msg,"Name %s has been deleted and Advertised.",name);
910 }
911 else
912 {
913 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
914 }
915 }
916 }
917 else if ( strcmp(op_type,"add") == 0 )
918 {
919 res=does_name_exist_in_npl(np);
920 if ( res == 0)
921 {
922 add_name_to_npl(np);
923 build_and_install_single_name_lsa(np);
924 sprintf(msg,"Name %s has been added to advertise.",name);
925 }
926 else
927 {
928 sprintf(msg,"Name %s has already been advertised from this router !!",name);
929 }
930 }
931 }
932 else if ( strcmp(cmd_type,"neighbor") == 0 )
933 {
934 if ( strcmp(op_type,"del") == 0 )
935 {
936 res=is_neighbor(np->name);
937 if ( res == 0)
938 {
939 sprintf(msg,"Neighbor %s does not exist !!",name);
940 }
941 else
942 {
943 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoqueb77b95f2013-02-08 12:28:47 -0600944 int face_id=get_next_hop_face_from_adl(np->name);
945 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
946 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
947 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque3171d652012-11-13 11:44:33 -0600948 delete_nbr_from_adl(np);
949 if(!nlsr->is_build_adj_lsa_sheduled)
950 {
951 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
952 nlsr->is_build_adj_lsa_sheduled=1;
953 }
954 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
955 }
956 }
957 else if ( strcmp(op_type,"add") == 0 )
958 {
959 res=is_neighbor(np->name);
960 if ( res == 0 )
961 {
akmhoque7c234e02013-02-13 11:23:56 -0600962 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600963 get_host_name_from_command_string(nbr_name,np->name,0);
964 printf("Hostname of neighbor: %s ",nbr_name->name);
965
akmhoque7c234e02013-02-13 11:23:56 -0600966 char *ip_addr=(char *)calloc(20,sizeof(char));
967 //memset(ip_addr,0,20);
akmhoqueb77b95f2013-02-08 12:28:47 -0600968 get_ip_from_hostname_02(nbr_name->name,ip_addr);
969 printf("IP Address: %s \n",ip_addr);
970 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
971 update_face_to_adl_for_nbr(nbr_name->name, face_id);
972 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
973 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
974
975 add_nbr_to_adl(np,face_id,ip_addr);
976
akmhoque3171d652012-11-13 11:44:33 -0600977 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoque7c234e02013-02-13 11:23:56 -0600978 free(ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600979
akmhoque3171d652012-11-13 11:44:33 -0600980 }
981 else
982 {
983 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
984 }
985 }
986 }
987
akmhoque562caef2012-11-09 13:29:06 -0600988
989 return msg;
990}
akmhoque1771c412012-11-09 13:06:08 -0600991
992int
993nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
994{
995 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600996 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -0600997 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600998 timeout.tv_sec=0;
999 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -06001000 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001001 else
akmhoque1771c412012-11-09 13:06:08 -06001002 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001003 timeout.tv_sec = 0;
1004 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001005 }
akmhoque1771c412012-11-09 13:06:08 -06001006
1007 int fd;
1008 int nread;
1009 int result;
1010 fd_set testfds;
1011 unsigned int client_len;
1012 int client_sockfd;
1013 char recv_buffer[1024];
1014 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -06001015 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001016
1017 testfds=nlsr->readfds;
1018 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1019
akmhoqueb77b95f2013-02-08 12:28:47 -06001020 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -06001021 {
1022 if(FD_ISSET(fd,&testfds))
1023 {
1024 if ( fd == ccn_fd )
1025 {
1026 return 0;
1027 }
1028 else if(fd == nlsr->nlsr_api_server_sock_fd)
1029 {
1030 client_len = sizeof(client_address);
1031 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1032 FD_SET(client_sockfd, &nlsr->readfds);
1033 }
1034 else
1035 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001036
akmhoque1771c412012-11-09 13:06:08 -06001037 ioctl(fd, FIONREAD, &nread);
1038 if(nread == 0)
1039 {
1040 close(fd);
1041 FD_CLR(fd, &nlsr->readfds);
1042 }
1043 else
1044 {
1045 recv(fd, recv_buffer, 1024, 0);
akmhoqueb77b95f2013-02-08 12:28:47 -06001046 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001047 char *msg=process_api_client_command(recv_buffer);
1048 send(fd, msg, strlen(msg),0);
1049 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001050 close(fd);
1051 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001052 }
1053 }
1054 }
1055 }
1056
1057 return 0;
1058}
1059
akmhoqueb77b95f2013-02-08 12:28:47 -06001060int
1061check_config_validity()
1062{
1063 if (nlsr->router_name == NULL )
1064 {
1065 fprintf(stderr,"Router name has not been configured :(\n");
1066 return -1;
1067 }
1068 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1069 {
1070 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1071 return -1;
1072 }
1073
1074 return 0;
1075}
1076
akmhoque386081b2012-08-10 10:53:21 -05001077void
1078nlsr_destroy( void )
1079{
akmhoque7b791452012-10-30 11:24:56 -05001080 if ( nlsr->debugging )
1081 {
1082 printf("Freeing Allocated Memory....\n");
1083 }
akmhoque9e9fc722012-09-26 14:03:25 -05001084 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001085 /* Destroying all face created by nlsr in CCND */
1086 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001087 destroy_faces_for_nbrs();
akmhoque386081b2012-08-10 10:53:21 -05001088 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque7c234e02013-02-13 11:23:56 -06001089 hashtb_destroy(&nlsr->adl);
1090 hashtb_destroy(&nlsr->npl);
1091 hashtb_destroy(&nlsr->pit_alsa);
akmhoque03004e62012-09-06 01:12:28 -05001092 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1093 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque7c234e02013-02-13 11:23:56 -06001094 hashtb_destroy(&nlsr->lsdb->cor_lsdb);
akmhoque3cced642012-09-24 16:20:20 -05001095
akmhoque7c234e02013-02-13 11:23:56 -06001096 int i, npt_element,rt_element;
akmhoque3560cb62012-09-09 10:52:30 -05001097 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);
akmhoque7c234e02013-02-13 11:23:56 -06001112
1113
1114 struct routing_table_entry *rte;
1115 hashtb_start(nlsr->routing_table, e);
1116 rt_element=hashtb_n(nlsr->routing_table);
1117 for(i=0;i<rt_element;i++)
1118 {
1119 rte=e->data;
1120 hashtb_destroy(&rte->face_list);
1121 hashtb_next(e);
1122 }
1123 hashtb_end(e);
1124 hashtb_destroy(&nlsr->routing_table);
1125
1126 if ( nlsr->ccns != NULL )
1127 ccns_close(&nlsr->ccns, NULL, NULL);
1128 if ( nlsr->slice != NULL )
1129 ccns_slice_destroy(&nlsr->slice);
akmhoque3560cb62012-09-09 10:52:30 -05001130
akmhoque7c234e02013-02-13 11:23:56 -06001131 close(nlsr->nlsr_api_server_sock_fd);
akmhoque95041802012-11-16 09:18:02 -06001132
akmhoque866c2222013-02-12 10:49:33 -06001133 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001134 free(nlsr->lsdb->lsdb_version);
1135 free(nlsr->lsdb);
1136 free(nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001137 if ( nlsr->debugging )
1138 {
1139 printf("Finished freeing allocated memory\n");
1140 }
akmhoque9e9fc722012-09-26 14:03:25 -05001141 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque7c234e02013-02-13 11:23:56 -06001142
1143 free(nlsr);
akmhoque53f64222012-09-05 13:57:51 -05001144
akmhoque386081b2012-08-10 10:53:21 -05001145}
1146
akmhoque03004e62012-09-06 01:12:28 -05001147
akmhoqueb77b95f2013-02-08 12:28:47 -06001148
akmhoque1771c412012-11-09 13:06:08 -06001149void
1150init_api_server(int ccn_fd)
1151{
1152 int server_sockfd;
1153 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001154 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001155 unsigned int yes=1;
1156
akmhoque95041802012-11-16 09:18:02 -06001157 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001158
1159 int flags = fcntl(server_sockfd, F_GETFL, 0);
1160 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1161
akmhoquef31f13b2012-11-16 09:42:24 -06001162 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1163 {
1164 ON_ERROR_DESTROY(-1);
1165 }
akmhoque95041802012-11-16 09:18:02 -06001166
1167 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001168 server_address.sin_addr.s_addr = INADDR_ANY;
1169 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001170
akmhoque1771c412012-11-09 13:06:08 -06001171 server_len = sizeof(server_address);
1172 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1173 listen(server_sockfd, 100);
1174 FD_ZERO(&nlsr->readfds);
1175 FD_SET(server_sockfd, &nlsr->readfds);
1176 FD_SET(ccn_fd, &nlsr->readfds);
1177 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1178
1179}
1180
akmhoque81c25e02012-09-10 14:50:33 -05001181int
akmhoque902d57e2012-08-17 09:24:38 -05001182init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001183{
akmhoque03004e62012-09-06 01:12:28 -05001184 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1185 {
1186 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001187 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001188 }
1189 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1190 {
1191 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001192 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001193 }
1194 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1195 {
1196 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001197 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001198 }
akmhoque902d57e2012-08-17 09:24:38 -05001199
akmhoque7c234e02013-02-13 11:23:56 -06001200 nlsr=(struct nlsr *)calloc(1,sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -05001201
1202 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -05001203 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -05001204 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -06001205 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -05001206 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -05001207 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -05001208 struct hashtb_param param_npt = {0};
1209 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1210 struct hashtb_param param_rte = {0};
1211 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -05001212
akmhoque59980a52012-08-09 12:36:09 -05001213 nlsr->in_interest.p = &incoming_interest;
1214 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -05001215
akmhoque03004e62012-09-06 01:12:28 -05001216 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -05001217
akmhoque7c234e02013-02-13 11:23:56 -06001218 char *time_stamp=(char *)calloc(20,sizeof(char));
1219 //memset(time_stamp,0,20);
akmhoque03004e62012-09-06 01:12:28 -05001220 get_current_timestamp_micro(time_stamp);
1221 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001222 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001223 free(time_stamp);
1224
1225 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001226 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -05001227 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001228 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueb77b95f2013-02-08 12:28:47 -06001229 struct hashtb_param param_cor_lsdb = {0};
1230 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001231
1232
akmhoque902d57e2012-08-17 09:24:38 -05001233
akmhoque53f64222012-09-05 13:57:51 -05001234
akmhoque59980a52012-08-09 12:36:09 -05001235 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001236 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001237 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001238 nlsr->adj_build_count=0;
1239 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001240 nlsr->is_send_lsdb_interest_scheduled=0;
1241 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001242
akmhoque7b791452012-10-30 11:24:56 -05001243 nlsr->detailed_logging=0;
1244 nlsr->debugging=0;
1245
akmhoqueb77b95f2013-02-08 12:28:47 -06001246 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoqued79438d2012-08-27 13:31:42 -05001247 nlsr->interest_retry = INTEREST_RETRY;
1248 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001249 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1250 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001251 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001252 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001253
akmhoque95041802012-11-16 09:18:02 -06001254 nlsr->api_port=API_PORT;
1255
akmhoque7c234e02013-02-13 11:23:56 -06001256 nlsr->topo_prefix=(char *)calloc(strlen("/ndn/routing/nlsr")+1,sizeof(char));
1257 //memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001258 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1259
akmhoque7c234e02013-02-13 11:23:56 -06001260 nlsr->slice_prefix=(char *)calloc(strlen("/ndn/routing/nlsr/LSA")+1,sizeof(char));
1261 //memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001262 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1263
1264 nlsr->is_hyperbolic_calc=0;
1265 nlsr->cor_r=-1.0;
1266 nlsr->cor_theta=-1.0;
1267
1268 nlsr->tunnel_type=IPPROTO_UDP;
1269
akmhoque81c25e02012-09-10 14:50:33 -05001270 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001271}
1272
akmhoque03004e62012-09-06 01:12:28 -05001273
akmhoque902d57e2012-08-17 09:24:38 -05001274int
1275main(int argc, char *argv[])
1276{
akmhoque81c25e02012-09-10 14:50:33 -05001277 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -05001278 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001279 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001280 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001281
akmhoquebfefef22012-09-26 10:09:34 -05001282
akmhoque81c25e02012-09-10 14:50:33 -05001283
akmhoque95041802012-11-16 09:18:02 -06001284 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001285 {
1286 switch (res)
1287 {
1288 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001289 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001290 break;
1291 case 'f':
1292 config_file = optarg;
1293 break;
akmhoque95041802012-11-16 09:18:02 -06001294 case 'p':
1295 port = atoi(optarg);
1296 break;
akmhoque59980a52012-08-09 12:36:09 -05001297 case 'h':
1298 default:
1299 usage(argv[0]);
1300 }
1301 }
1302
akmhoquebfefef22012-09-26 10:09:34 -05001303 ret=init_nlsr();
1304 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001305
1306 if ( port !=0 )
1307 nlsr->api_port=port;
1308
akmhoque59980a52012-08-09 12:36:09 -05001309 readConfigFile(config_file);
akmhoqueb77b95f2013-02-08 12:28:47 -06001310
1311 ON_ERROR_DESTROY(check_config_validity());
1312
1313 print_adjacent_from_adl();
1314
akmhoquebfefef22012-09-26 10:09:34 -05001315 if ( daemon_mode == 1 )
1316 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001317 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001318 daemonize_nlsr();
1319 }
1320
1321 startLogging(nlsr->logDir);
1322
akmhoque59980a52012-08-09 12:36:09 -05001323 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001324 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1325 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001326 {
1327 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001328 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001329 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001330 }
akmhoque1771c412012-11-09 13:06:08 -06001331
1332 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001333
1334 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1335 if(res<0)
1336 {
1337 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1338 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1339 ON_ERROR_DESTROY(res);
1340 }
akmhoque53f64222012-09-05 13:57:51 -05001341 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001342 router_prefix=ccn_charbuf_create();
1343 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001344 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001345 {
1346 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001347 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001348 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001349 }
akmhoque59980a52012-08-09 12:36:09 -05001350
1351 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001352 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001353 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1354 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001355 {
1356 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001357 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001358 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001359 }
1360 ccn_charbuf_destroy(&router_prefix);
1361
akmhoque7b791452012-10-30 11:24:56 -05001362 if ( nlsr->debugging )
1363 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001364 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001365 if ( nlsr->debugging )
1366 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001367 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001368
akmhoqueb77b95f2013-02-08 12:28:47 -06001369 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001370 print_name_prefix_from_npl();
1371 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001372 build_and_install_name_lsas();
1373
akmhoqueb77b95f2013-02-08 12:28:47 -06001374 print_name_lsdb();
1375 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1376 {
1377 build_and_install_cor_lsa();
1378 }
1379 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001380
1381 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque184dde02013-02-14 15:53:24 -06001382 ccn_set_schedule(nlsr->ccn,nlsr->sched);
akmhoque03004e62012-09-06 01:12:28 -05001383 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001384 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque0678c5d2013-02-18 11:03:31 -06001385
1386 res=sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1387 ON_ERROR_DESTROY(res);
akmhoque1c9b92f2012-08-13 10:57:50 -05001388
akmhoque1771c412012-11-09 13:06:08 -06001389
akmhoque59980a52012-08-09 12:36:09 -05001390 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001391 {
akmhoqueffacaa82012-09-13 17:48:30 -05001392 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001393 {
akmhoqueffacaa82012-09-13 17:48:30 -05001394 if( nlsr->sched != NULL )
1395 {
akmhoque1771c412012-11-09 13:06:08 -06001396 long int micro_sec=ccn_schedule_run(nlsr->sched);
1397 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1398 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001399 }
1400 if(nlsr->ccn != NULL)
1401 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001402 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001403 }
1404 if (!(nlsr->sched && nlsr->ccn))
1405 {
1406 break;
1407 }
akmhoque29c1db52012-09-07 14:47:43 -05001408 }
1409
akmhoque59980a52012-08-09 12:36:09 -05001410 }
akmhoque1771c412012-11-09 13:06:08 -06001411
akmhoque59980a52012-08-09 12:36:09 -05001412
akmhoque59980a52012-08-09 12:36:09 -05001413 return 0;
1414}
1415