blob: 20c7d4581abfc0b814d7faed29978e31d8e00600 [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);
akmhoque958ccf72013-02-11 10:42:03 -0600182 if ( nbr_ip_addr != NULL)
183 is_ip_configured=1;
akmhoqueb77b95f2013-02-08 12:28:47 -0600184 }
akmhoque03004e62012-09-06 01:12:28 -0500185 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
186 nbr->name=(char *)malloc(strlen(rtr_name)+1);
187 memset(nbr->name,0,strlen(rtr_name)+1);
188 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
189 nbr->length=strlen(rtr_name)+1;
190
akmhoqueb77b95f2013-02-08 12:28:47 -0600191
192 if ( !is_ip_configured )
193 {
194 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
195 get_host_name_from_command_string(nbr_name,nbr->name,0);
196 printf("Hostname of neighbor: %s ",nbr_name->name);
197 get_ip_from_hostname_02(nbr_name->name,ip_addr);
198 printf("IP Address: %s \n",ip_addr);
199 free(nbr_name->name);
200 free(nbr_name);
201 }
202 else
203 {
204 memcpy(ip_addr,nbr_ip_addr,strlen(nbr_ip_addr));
205 printf("Name of neighbor: %s ",nbr->name);
206 printf("IP Address: %s \n",ip_addr);
207 }
208 add_nbr_to_adl(nbr,0,ip_addr);
209
akmhoque03004e62012-09-06 01:12:28 -0500210
211 free(nbr->name);
212 free(nbr);
213}
214
215void
216process_command_ccnname(char *command)
217{
218
219 if(command==NULL)
220 {
221 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
222 return;
223 }
224 char *rem;
225 const char *sep=" \t\n";
226 char *name;
227 name=strtok_r(command,sep,&rem);
228 if(name==NULL)
229 {
230 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
231 return;
232 }
233
234 printf("Name Prefix: %s \n",name);
235
akmhoqued5152122012-09-19 06:44:23 -0500236 if ( name[strlen(name)-1] == '/' )
237 name[strlen(name)-1]='\0';
238
akmhoque53f64222012-09-05 13:57:51 -0500239 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500240 np->name=(char *)malloc(strlen(name)+1);
241 memset(np->name,0,strlen(name)+1);
242 memcpy(np->name,name,strlen(name)+1);
243 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500244
akmhoque03004e62012-09-06 01:12:28 -0500245 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500246
akmhoque03004e62012-09-06 01:12:28 -0500247 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500248 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500249}
250
akmhoque03004e62012-09-06 01:12:28 -0500251
252void
253process_command_router_name(char *command)
254{
255 if(command==NULL)
256 {
257 printf(" Wrong Command Format ( router-name /router/name )\n");
258 return;
259 }
260 char *rem;
261 const char *sep=" \t\n";
262 char *rtr_name;
263
264 rtr_name=strtok_r(command,sep,&rem);
265 if(rtr_name==NULL)
266 {
267 printf(" Wrong Command Format ( router-name /router/name )\n");
268 return;
269 }
270
271
akmhoqued5152122012-09-19 06:44:23 -0500272 if ( rtr_name[strlen(rtr_name)-1] == '/' )
273 rtr_name[strlen(rtr_name)-1]='\0';
274
akmhoque03004e62012-09-06 01:12:28 -0500275 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
276 memset(nlsr->router_name,0,strlen(rtr_name)+1);
277 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
278
279
280}
281
akmhoqueb77b95f2013-02-08 12:28:47 -0600282/*
akmhoque59980a52012-08-09 12:36:09 -0500283void
akmhoqued79438d2012-08-27 13:31:42 -0500284process_command_lsdb_synch_interval(char *command)
285{
286 if(command==NULL)
287 {
288 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
289 return;
290 }
291 char *rem;
292 const char *sep=" \t\n";
293 char *secs;
294 long int seconds;
295
296 secs=strtok_r(command,sep,&rem);
297 if(secs==NULL)
298 {
299 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
300 return;
301 }
302
303 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500304 if ( seconds >= 120 && seconds <= 3600 )
305 {
306 nlsr->lsdb_synch_interval=seconds;
307 }
akmhoqued79438d2012-08-27 13:31:42 -0500308
309}
akmhoqueb77b95f2013-02-08 12:28:47 -0600310*/
akmhoqued79438d2012-08-27 13:31:42 -0500311
312void
313process_command_interest_retry(char *command)
314{
315 if(command==NULL)
316 {
317 printf(" Wrong Command Format ( interest-retry number )\n");
318 return;
319 }
320 char *rem;
321 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500322 char *retry;
323 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500324
akmhoqueffacaa82012-09-13 17:48:30 -0500325 retry=strtok_r(command,sep,&rem);
326 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500327 {
328 printf(" Wrong Command Format ( interest-retry number)\n");
329 return;
330 }
331
akmhoqueffacaa82012-09-13 17:48:30 -0500332 retry_number=atoi(retry);
333 if ( retry_number >= 1 && retry_number<=10 )
334 {
335 nlsr->interest_retry=retry_number;
336 }
akmhoqued79438d2012-08-27 13:31:42 -0500337
338}
339
340void
341process_command_interest_resend_time(char *command)
342{
343 if(command==NULL)
344 {
345 printf(" Wrong Command Format ( interest-resend-time secs )\n");
346 return;
347 }
348 char *rem;
349 const char *sep=" \t\n";
350 char *secs;
351 long int seconds;
352
353 secs=strtok_r(command,sep,&rem);
354 if(secs==NULL)
355 {
356 printf(" Wrong Command Format ( interest-resend-time secs)\n");
357 return;
358 }
359
360 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500361 if ( seconds <= 60 && seconds >= 1 )
362 {
363 nlsr->interest_resend_time=seconds;
364 }
akmhoqued79438d2012-08-27 13:31:42 -0500365}
366
akmhoque03004e62012-09-06 01:12:28 -0500367
akmhoqued5152122012-09-19 06:44:23 -0500368void
369process_command_lsa_refresh_time(char *command)
370{
371 if(command==NULL)
372 {
373 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
374 return;
375 }
376 char *rem;
377 const char *sep=" \t\n";
378 char *secs;
379 long int seconds;
380
381 secs=strtok_r(command,sep,&rem);
382 if(secs==NULL)
383 {
384 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
385 return;
386 }
387
388 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600389 if ( seconds >= 240)
akmhoqued5152122012-09-19 06:44:23 -0500390 {
391 nlsr->lsa_refresh_time=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600392 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
393 {
394 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
395 }
akmhoqued5152122012-09-19 06:44:23 -0500396 }
397
398}
399
400void
401process_command_router_dead_interval(char *command)
402{
403 if(command==NULL)
404 {
405 printf(" Wrong Command Format ( router-dead-interval secs )\n");
406 return;
407 }
408 char *rem;
409 const char *sep=" \t\n";
410 char *secs;
411 long int seconds;
412
413 secs=strtok_r(command,sep,&rem);
414 if(secs==NULL)
415 {
416 printf(" Wrong Command Format ( router-dead-interval secs)\n");
417 return;
418 }
419
420 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600421 if ( seconds >= 480 )
akmhoqued5152122012-09-19 06:44:23 -0500422 {
423 nlsr->router_dead_interval=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600424 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
425 {
426 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
427 }
akmhoqued5152122012-09-19 06:44:23 -0500428 }
429
430}
akmhoque03004e62012-09-06 01:12:28 -0500431
akmhoqued79438d2012-08-27 13:31:42 -0500432void
akmhoqueb77b95f2013-02-08 12:28:47 -0600433process_command_max_faces_per_prefix(char *command)
akmhoque3cced642012-09-24 16:20:20 -0500434{
435 if(command==NULL)
436 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600437 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque3cced642012-09-24 16:20:20 -0500438 return;
439 }
440 char *rem;
441 const char *sep=" \t\n";
442 char *num;
443 long int number;
444
445 num=strtok_r(command,sep,&rem);
446 if(num==NULL)
447 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600448 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque3cced642012-09-24 16:20:20 -0500449 return;
450 }
451
452 number=atoi(num);
453 if ( number >= 0 && number <= 60 )
454 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600455 nlsr->max_faces_per_prefix=number;
akmhoque3cced642012-09-24 16:20:20 -0500456 }
457
458}
459
460void
akmhoquebfefef22012-09-26 10:09:34 -0500461process_command_logdir(char *command)
462{
463 if(command==NULL)
464 {
465 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
466 return;
467 }
468 char *rem;
469 const char *sep=" \t\n";
470 char *dir;
471
472 dir=strtok_r(command,sep,&rem);
473 if(dir==NULL)
474 {
475 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
476 return;
477 }
478
479 nlsr->logDir=(char *)malloc(strlen(dir)+1);
480 memset(nlsr->logDir,0,strlen(dir)+1);
481 memcpy(nlsr->logDir,dir,strlen(dir));
482}
483
484void
akmhoque7b791452012-10-30 11:24:56 -0500485process_command_detailed_log(char *command)
486{
487 if(command==NULL)
488 {
489 printf(" Wrong Command Format ( detailed-log on/off )\n");
490 return;
491 }
492 char *rem;
493 const char *sep=" \t\n";
494 char *on_off;
495
496 on_off=strtok_r(command,sep,&rem);
497 if(on_off==NULL)
498 {
499 printf(" Wrong Command Format ( detailed-log on/off )\n");
500 return;
501 }
502
503 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
504 {
505 nlsr->detailed_logging=1;
506 }
507}
508
509void
510process_command_debug(char *command)
511{
512 if(command==NULL)
513 {
514 printf(" Wrong Command Format ( debug on/off )\n");
515 return;
516 }
517 char *rem;
518 const char *sep=" \t\n";
519 char *on_off;
520
521 on_off=strtok_r(command,sep,&rem);
522 if(on_off==NULL)
523 {
524 printf(" Wrong Command Format ( debug on/off )\n");
525 return;
526 }
527
528 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
529 {
530 nlsr->debugging=1;
531 }
532}
533
akmhoqueb77b95f2013-02-08 12:28:47 -0600534
535void
536process_command_topo_prefix(char *command)
537{
538 if(command==NULL)
539 {
540 printf(" Wrong Command Format ( topo-prefix )\n");
541 return;
542 }
543 char *rem;
544 const char *sep=" \t\n";
545 char *topo_prefix;
546
547 topo_prefix=strtok_r(command,sep,&rem);
548 if(topo_prefix==NULL)
549 {
550 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
551 return;
552 }
553 else
554 {
555 if( nlsr->topo_prefix != NULL)
556 free(nlsr->topo_prefix);
557 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
558 topo_prefix[strlen(topo_prefix)-1]='\0';
559
560 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
561 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
562 puts(topo_prefix);
563 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
564
565 }
566}
567
568
569void
570process_command_slice_prefix(char *command)
571{
572 if(command==NULL)
573 {
574 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
575 return;
576 }
577 char *rem;
578 const char *sep=" \t\n";
579 char *slice_prefix;
580
581 slice_prefix=strtok_r(command,sep,&rem);
582 if(slice_prefix==NULL)
583 {
584 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
585 return;
586 }
587 else
588 {
589 if ( nlsr->slice_prefix != NULL)
590 free(nlsr->slice_prefix);
591 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
592 slice_prefix[strlen(slice_prefix)-1]='\0';
593
594 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
595 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
596 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
597 }
598}
599
600void
601process_command_hyperbolic_routing(char *command)
602{
603 if(command==NULL)
604 {
605 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
606 return;
607 }
608 char *rem;
609 const char *sep=" \t\n";
610 char *on_off;
611
612 on_off=strtok_r(command,sep,&rem);
613 if(on_off==NULL)
614 {
615 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
616 return;
617 }
618
619 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
620 {
621 nlsr->is_hyperbolic_calc=1;
622 }
623}
624
625void
626process_command_hyperbolic_cordinate(char *command)
627{
628 if(command==NULL)
629 {
630 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
631 return;
632 }
633
634 char *rem;
635 const char *sep=" \t\n\r";
636 char *radious;
637 char *theta;
638
639 radious=strtok_r(command,sep,&rem);
640 if (radious == NULL )
641 {
642 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
643 return;
644 }
645
646 theta=strtok_r(NULL,sep,&rem);
647 if (theta == NULL )
648 {
649 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
650 return;
651 }
652
653 nlsr->cor_r=strtof(radious,NULL);
654 nlsr->cor_theta=strtof(theta,NULL);
655
656}
657
658void
659process_command_tunnel_type(char *command)
660{
661 if(command==NULL)
662 {
663 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
664 return;
665 }
666 char *rem;
667 const char *sep=" \t\n";
668 char *on_off;
669
670 on_off=strtok_r(command,sep,&rem);
671 if(on_off==NULL)
672 {
673 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
674 return;
675 }
676
677 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
678 {
679 nlsr->tunnel_type=IPPROTO_TCP;
680 }
681 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
682 {
683 nlsr->tunnel_type=IPPROTO_UDP;
684 }
685}
686
akmhoque7b791452012-10-30 11:24:56 -0500687void
akmhoque59980a52012-08-09 12:36:09 -0500688process_conf_command(char *command)
689{
690 const char *separators=" \t\n";
691 char *remainder=NULL;
692 char *cmd_type=NULL;
693
694 if(command==NULL || strlen(command)==0 || command[0]=='!')
695 return;
696
697 cmd_type=strtok_r(command,separators,&remainder);
698
699 if(!strcmp(cmd_type,"router-name") )
700 {
701 process_command_router_name(remainder);
702 }
703 else if(!strcmp(cmd_type,"ccnneighbor") )
704 {
705 process_command_ccnneighbor(remainder);
706 }
707 else if(!strcmp(cmd_type,"ccnname") )
708 {
709 process_command_ccnname(remainder);
710 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600711 /*else if(!strcmp(cmd_type,"lsdb-synch-interval") )
akmhoqued79438d2012-08-27 13:31:42 -0500712 {
713 process_command_lsdb_synch_interval(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600714 }*/
akmhoqued79438d2012-08-27 13:31:42 -0500715 else if(!strcmp(cmd_type,"interest-retry") )
716 {
717 process_command_interest_retry(remainder);
718 }
719 else if(!strcmp(cmd_type,"interest-resend-time") )
720 {
721 process_command_interest_resend_time(remainder);
722 }
akmhoqued5152122012-09-19 06:44:23 -0500723 else if(!strcmp(cmd_type,"lsa-refresh-time") )
724 {
725 process_command_lsa_refresh_time(remainder);
726 }
727 else if(!strcmp(cmd_type,"router-dead-interval") )
728 {
729 process_command_router_dead_interval(remainder);
730 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600731 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque3cced642012-09-24 16:20:20 -0500732 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600733 process_command_max_faces_per_prefix(remainder);
akmhoque3cced642012-09-24 16:20:20 -0500734 }
akmhoquebfefef22012-09-26 10:09:34 -0500735 else if(!strcmp(cmd_type,"logdir") )
736 {
737 process_command_logdir(remainder);
738 }
akmhoque7b791452012-10-30 11:24:56 -0500739 else if(!strcmp(cmd_type,"detailed-log") )
740 {
741 process_command_detailed_log(remainder);
742 }
743 else if(!strcmp(cmd_type,"debug") )
744 {
745 process_command_debug(remainder);
746 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600747 else if(!strcmp(cmd_type,"topo-prefix") )
748 {
749 process_command_topo_prefix(remainder);
750 }
751 else if(!strcmp(cmd_type,"slice-prefix") )
752 {
753 process_command_slice_prefix(remainder);
754 }
755 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
756 {
757 process_command_hyperbolic_cordinate(remainder);
758 }
759 else if(!strcmp(cmd_type,"hyperbolic-routing") )
760 {
761 process_command_hyperbolic_routing(remainder);
762 }
763 else if(!strcmp(cmd_type,"tunnel-type") )
764 {
765 process_command_tunnel_type(remainder);
766 }
akmhoqued5152122012-09-19 06:44:23 -0500767 else
akmhoque59980a52012-08-09 12:36:09 -0500768 {
769 printf("Wrong configuration Command %s \n",cmd_type);
770 }
771}
772
akmhoque03004e62012-09-06 01:12:28 -0500773
akmhoque59980a52012-08-09 12:36:09 -0500774int
775readConfigFile(const char *filename)
776{
777 FILE *cfg;
778 char buf[1024];
779 int len;
780
781 cfg=fopen(filename, "r");
782
783 if(cfg == NULL)
784 {
785 printf("\nConfiguration File does not exists\n");
786 exit(1);
787 }
788
789 while(fgets((char *)buf, sizeof(buf), cfg))
790 {
791 len=strlen(buf);
792 if(buf[len-1] == '\n')
793 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500794 if ( buf[0] != '#' && buf[0] != '!')
795 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500796 }
797
798 fclose(cfg);
799
800 return 0;
801}
802
akmhoqueb77b95f2013-02-08 12:28:47 -0600803
804void
805add_faces_for_nbrs(void)
806{
807 int i, adl_element;
808 struct ndn_neighbor *nbr;
809
810 struct hashtb_enumerator ee;
811 struct hashtb_enumerator *e = &ee;
812
813 hashtb_start(nlsr->adl, e);
814 adl_element=hashtb_n(nlsr->adl);
815
816 for(i=0;i<adl_element;i++)
817 {
818 nbr=e->data;
819 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
820 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
821 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
822 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
823 hashtb_next(e);
824 }
825
826 hashtb_end(e);
827
828}
829
830void
831destroy_faces_for_nbrs(void)
832{
833 int i, adl_element;
834 struct ndn_neighbor *nbr;
835
836 struct hashtb_enumerator ee;
837 struct hashtb_enumerator *e = &ee;
838
839 hashtb_start(nlsr->adl, e);
840 adl_element=hashtb_n(nlsr->adl);
841
842 for(i=0;i<adl_element;i++)
843 {
844 nbr=e->data;
845 if ( nbr->face > 0 )
846 {
847 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
848 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
849 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
850 }
851 hashtb_next(e);
852 }
853
854 hashtb_end(e);
855
856}
857
akmhoque562caef2012-11-09 13:29:06 -0600858char *
859process_api_client_command(char *command)
860{
861 char *msg;
862 msg=(char *)malloc(100);
akmhoqueb77b95f2013-02-08 12:28:47 -0600863 memset(msg,0,100);
864
akmhoque3171d652012-11-13 11:44:33 -0600865 const char *sep=" \t\n";
866 char *rem=NULL;
867 char *cmd_type=NULL;
868 char *op_type=NULL;
869 char *name=NULL;
870 char *face=NULL;
871 int face_id;
872 int res;
873
874 op_type=strtok_r(command,sep,&rem);
875 cmd_type=strtok_r(NULL,sep,&rem);
876 name=strtok_r(NULL,sep,&rem);
877 if ( name[strlen(name)-1] == '/' )
878 name[strlen(name)-1]='\0';
879
880 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
881 np->name=(char *)malloc(strlen(name)+1);
882 memset(np->name,0,strlen(name)+1);
883 memcpy(np->name,name,strlen(name)+1);
884 np->length=strlen(name)+1;
885
886 if ( strcmp(cmd_type,"name")!= 0 )
887 {
888 face=strtok_r(NULL,sep,&rem);
889 sscanf(face,"face%d",&face_id);
890 }
akmhoque562caef2012-11-09 13:29:06 -0600891
akmhoque3171d652012-11-13 11:44:33 -0600892 if ( strcmp(cmd_type,"name")== 0 )
893 {
894 if ( strcmp(op_type,"del") == 0 )
895 {
896 res=does_name_exist_in_npl(np);
897 if ( res == 0)
898 {
899 sprintf(msg,"Name %s does not exist !!",name);
900 }
901 else
902 {
903 long int ls_id=get_lsa_id_from_npl(np);
904 if ( ls_id != 0 )
905 {
906 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
907 sprintf(msg,"Name %s has been deleted and Advertised.",name);
908 }
909 else
910 {
911 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
912 }
913 }
914 }
915 else if ( strcmp(op_type,"add") == 0 )
916 {
917 res=does_name_exist_in_npl(np);
918 if ( res == 0)
919 {
920 add_name_to_npl(np);
921 build_and_install_single_name_lsa(np);
922 sprintf(msg,"Name %s has been added to advertise.",name);
923 }
924 else
925 {
926 sprintf(msg,"Name %s has already been advertised from this router !!",name);
927 }
928 }
929 }
930 else if ( strcmp(cmd_type,"neighbor") == 0 )
931 {
932 if ( strcmp(op_type,"del") == 0 )
933 {
934 res=is_neighbor(np->name);
935 if ( res == 0)
936 {
937 sprintf(msg,"Neighbor %s does not exist !!",name);
938 }
939 else
940 {
941 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoqueb77b95f2013-02-08 12:28:47 -0600942 int face_id=get_next_hop_face_from_adl(np->name);
943 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
944 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
945 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque3171d652012-11-13 11:44:33 -0600946 delete_nbr_from_adl(np);
947 if(!nlsr->is_build_adj_lsa_sheduled)
948 {
949 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
950 nlsr->is_build_adj_lsa_sheduled=1;
951 }
952 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
953 }
954 }
955 else if ( strcmp(op_type,"add") == 0 )
956 {
957 res=is_neighbor(np->name);
958 if ( res == 0 )
959 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600960 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
961 get_host_name_from_command_string(nbr_name,np->name,0);
962 printf("Hostname of neighbor: %s ",nbr_name->name);
963
964 char *ip_addr=(char *)malloc(13);
965 memset(ip_addr,0,13);
966 get_ip_from_hostname_02(nbr_name->name,ip_addr);
967 printf("IP Address: %s \n",ip_addr);
968 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
969 update_face_to_adl_for_nbr(nbr_name->name, face_id);
970 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
971 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
972
973 add_nbr_to_adl(np,face_id,ip_addr);
974
akmhoque3171d652012-11-13 11:44:33 -0600975 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoqueb77b95f2013-02-08 12:28:47 -0600976
akmhoque3171d652012-11-13 11:44:33 -0600977 }
978 else
979 {
980 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
981 }
982 }
983 }
984
akmhoque562caef2012-11-09 13:29:06 -0600985
986 return msg;
987}
akmhoque1771c412012-11-09 13:06:08 -0600988
989int
990nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
991{
992 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600993 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -0600994 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600995 timeout.tv_sec=0;
996 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -0600997 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600998 else
akmhoque1771c412012-11-09 13:06:08 -0600999 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001000 timeout.tv_sec = 0;
1001 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001002 }
akmhoque1771c412012-11-09 13:06:08 -06001003
1004 int fd;
1005 int nread;
1006 int result;
1007 fd_set testfds;
1008 unsigned int client_len;
1009 int client_sockfd;
1010 char recv_buffer[1024];
1011 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -06001012 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001013
1014 testfds=nlsr->readfds;
1015 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1016
akmhoqueb77b95f2013-02-08 12:28:47 -06001017 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -06001018 {
1019 if(FD_ISSET(fd,&testfds))
1020 {
1021 if ( fd == ccn_fd )
1022 {
1023 return 0;
1024 }
1025 else if(fd == nlsr->nlsr_api_server_sock_fd)
1026 {
1027 client_len = sizeof(client_address);
1028 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1029 FD_SET(client_sockfd, &nlsr->readfds);
1030 }
1031 else
1032 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001033
akmhoque1771c412012-11-09 13:06:08 -06001034 ioctl(fd, FIONREAD, &nread);
1035 if(nread == 0)
1036 {
1037 close(fd);
1038 FD_CLR(fd, &nlsr->readfds);
1039 }
1040 else
1041 {
1042 recv(fd, recv_buffer, 1024, 0);
akmhoqueb77b95f2013-02-08 12:28:47 -06001043 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001044 char *msg=process_api_client_command(recv_buffer);
1045 send(fd, msg, strlen(msg),0);
1046 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001047 close(fd);
1048 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001049 }
1050 }
1051 }
1052 }
1053
1054 return 0;
1055}
1056
akmhoqueb77b95f2013-02-08 12:28:47 -06001057int
1058check_config_validity()
1059{
1060 if (nlsr->router_name == NULL )
1061 {
1062 fprintf(stderr,"Router name has not been configured :(\n");
1063 return -1;
1064 }
1065 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1066 {
1067 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1068 return -1;
1069 }
1070
1071 return 0;
1072}
1073
akmhoque386081b2012-08-10 10:53:21 -05001074void
1075nlsr_destroy( void )
1076{
akmhoque7b791452012-10-30 11:24:56 -05001077 if ( nlsr->debugging )
1078 {
1079 printf("Freeing Allocated Memory....\n");
1080 }
akmhoque9e9fc722012-09-26 14:03:25 -05001081 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001082 /* Destroying all face created by nlsr in CCND */
1083 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001084 destroy_faces_for_nbrs();
akmhoque386081b2012-08-10 10:53:21 -05001085 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -05001086 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -05001087 hashtb_destroy(&nlsr->adl);
1088 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1089 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001090 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -05001091
akmhoqueb77b95f2013-02-08 12:28:47 -06001092
akmhoque3cced642012-09-24 16:20:20 -05001093
akmhoque3560cb62012-09-09 10:52:30 -05001094 hashtb_destroy(&nlsr->routing_table);
1095
1096
1097 int i, npt_element;
1098 struct npt_entry *ne;
1099 struct hashtb_enumerator ee;
1100 struct hashtb_enumerator *e = &ee;
1101 hashtb_start(nlsr->npt, e);
1102 npt_element=hashtb_n(nlsr->npt);
1103 for(i=0;i<npt_element;i++)
1104 {
1105 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001106 hashtb_destroy(&ne->name_list);
1107 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -05001108 hashtb_next(e);
1109 }
1110
1111 hashtb_end(e);
1112 hashtb_destroy(&nlsr->npt);
1113
akmhoque95041802012-11-16 09:18:02 -06001114
akmhoqueb77b95f2013-02-08 12:28:47 -06001115 ccns_close(&nlsr->ccns, NULL, NULL);
1116 ccns_slice_destroy(&nlsr->slice);
1117
1118
1119
akmhoque95041802012-11-16 09:18:02 -06001120 close(nlsr->nlsr_api_server_sock_fd);
1121
akmhoque386081b2012-08-10 10:53:21 -05001122 ccn_schedule_destroy(&nlsr->sched);
1123 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001124
1125 free(nlsr->lsdb->lsdb_version);
1126 free(nlsr->lsdb);
1127 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -05001128 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -05001129 if ( nlsr->debugging )
1130 {
1131 printf("Finished freeing allocated memory\n");
1132 }
akmhoque9e9fc722012-09-26 14:03:25 -05001133 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -05001134
akmhoque386081b2012-08-10 10:53:21 -05001135}
1136
akmhoque03004e62012-09-06 01:12:28 -05001137
akmhoqueb77b95f2013-02-08 12:28:47 -06001138
akmhoque1771c412012-11-09 13:06:08 -06001139void
1140init_api_server(int ccn_fd)
1141{
1142 int server_sockfd;
1143 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001144 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001145 unsigned int yes=1;
1146
akmhoque95041802012-11-16 09:18:02 -06001147 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001148
1149 int flags = fcntl(server_sockfd, F_GETFL, 0);
1150 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1151
akmhoquef31f13b2012-11-16 09:42:24 -06001152 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1153 {
1154 ON_ERROR_DESTROY(-1);
1155 }
akmhoque95041802012-11-16 09:18:02 -06001156
1157 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001158 server_address.sin_addr.s_addr = INADDR_ANY;
1159 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001160
akmhoque1771c412012-11-09 13:06:08 -06001161 server_len = sizeof(server_address);
1162 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1163 listen(server_sockfd, 100);
1164 FD_ZERO(&nlsr->readfds);
1165 FD_SET(server_sockfd, &nlsr->readfds);
1166 FD_SET(ccn_fd, &nlsr->readfds);
1167 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1168
1169}
1170
akmhoque81c25e02012-09-10 14:50:33 -05001171int
akmhoque902d57e2012-08-17 09:24:38 -05001172init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001173{
akmhoque03004e62012-09-06 01:12:28 -05001174 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1175 {
1176 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001177 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001178 }
1179 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1180 {
1181 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001182 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001183 }
1184 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1185 {
1186 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001187 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001188 }
akmhoque902d57e2012-08-17 09:24:38 -05001189
akmhoque59980a52012-08-09 12:36:09 -05001190 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -05001191
1192 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -05001193 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -05001194 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -06001195 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -05001196 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -05001197 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -05001198 struct hashtb_param param_npt = {0};
1199 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1200 struct hashtb_param param_rte = {0};
1201 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -05001202
akmhoque59980a52012-08-09 12:36:09 -05001203 nlsr->in_interest.p = &incoming_interest;
1204 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -05001205
akmhoque03004e62012-09-06 01:12:28 -05001206 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -05001207
akmhoque03004e62012-09-06 01:12:28 -05001208 char *time_stamp=(char *)malloc(20);
1209 memset(time_stamp,0,20);
1210 get_current_timestamp_micro(time_stamp);
1211 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001212 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001213 free(time_stamp);
1214
1215 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001216 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -05001217 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001218 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueb77b95f2013-02-08 12:28:47 -06001219 struct hashtb_param param_cor_lsdb = {0};
1220 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001221
1222
akmhoque902d57e2012-08-17 09:24:38 -05001223
akmhoque53f64222012-09-05 13:57:51 -05001224
akmhoque59980a52012-08-09 12:36:09 -05001225 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001226 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001227 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001228 nlsr->adj_build_count=0;
1229 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001230 nlsr->is_send_lsdb_interest_scheduled=0;
1231 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001232
akmhoque7b791452012-10-30 11:24:56 -05001233 nlsr->detailed_logging=0;
1234 nlsr->debugging=0;
1235
akmhoqueb77b95f2013-02-08 12:28:47 -06001236 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoqued79438d2012-08-27 13:31:42 -05001237 nlsr->interest_retry = INTEREST_RETRY;
1238 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001239 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1240 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001241 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001242 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001243
akmhoque95041802012-11-16 09:18:02 -06001244 nlsr->api_port=API_PORT;
1245
akmhoqueb77b95f2013-02-08 12:28:47 -06001246 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
1247 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
1248 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1249
1250 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
1251 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
1252 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1253
1254 nlsr->is_hyperbolic_calc=0;
1255 nlsr->cor_r=-1.0;
1256 nlsr->cor_theta=-1.0;
1257
1258 nlsr->tunnel_type=IPPROTO_UDP;
1259
akmhoque81c25e02012-09-10 14:50:33 -05001260 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001261}
1262
akmhoque03004e62012-09-06 01:12:28 -05001263
akmhoque902d57e2012-08-17 09:24:38 -05001264int
1265main(int argc, char *argv[])
1266{
akmhoque81c25e02012-09-10 14:50:33 -05001267 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -05001268 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001269 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001270 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001271
akmhoquebfefef22012-09-26 10:09:34 -05001272
akmhoque81c25e02012-09-10 14:50:33 -05001273
akmhoque95041802012-11-16 09:18:02 -06001274 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001275 {
1276 switch (res)
1277 {
1278 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001279 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001280 break;
1281 case 'f':
1282 config_file = optarg;
1283 break;
akmhoque95041802012-11-16 09:18:02 -06001284 case 'p':
1285 port = atoi(optarg);
1286 break;
akmhoque59980a52012-08-09 12:36:09 -05001287 case 'h':
1288 default:
1289 usage(argv[0]);
1290 }
1291 }
1292
akmhoquebfefef22012-09-26 10:09:34 -05001293 ret=init_nlsr();
1294 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001295
1296 if ( port !=0 )
1297 nlsr->api_port=port;
1298
akmhoque59980a52012-08-09 12:36:09 -05001299 readConfigFile(config_file);
akmhoqueb77b95f2013-02-08 12:28:47 -06001300
1301 ON_ERROR_DESTROY(check_config_validity());
1302
1303 print_adjacent_from_adl();
1304
akmhoquebfefef22012-09-26 10:09:34 -05001305 if ( daemon_mode == 1 )
1306 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001307 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001308 daemonize_nlsr();
1309 }
1310
1311 startLogging(nlsr->logDir);
1312
akmhoque59980a52012-08-09 12:36:09 -05001313 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001314 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1315 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001316 {
1317 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001318 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001319 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001320 }
akmhoque1771c412012-11-09 13:06:08 -06001321
1322 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001323
1324 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1325 if(res<0)
1326 {
1327 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1328 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1329 ON_ERROR_DESTROY(res);
1330 }
akmhoque53f64222012-09-05 13:57:51 -05001331 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001332 router_prefix=ccn_charbuf_create();
1333 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001334 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001335 {
1336 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001337 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001338 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001339 }
akmhoque59980a52012-08-09 12:36:09 -05001340
1341 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001342 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001343 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1344 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001345 {
1346 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001347 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001348 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001349 }
1350 ccn_charbuf_destroy(&router_prefix);
1351
akmhoque7b791452012-10-30 11:24:56 -05001352 if ( nlsr->debugging )
1353 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001354 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001355 if ( nlsr->debugging )
1356 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001357 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001358
akmhoqueb77b95f2013-02-08 12:28:47 -06001359 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001360 print_name_prefix_from_npl();
1361 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001362 build_and_install_name_lsas();
1363
1364 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1365
1366
1367 print_name_lsdb();
1368 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1369 {
1370 build_and_install_cor_lsa();
1371 }
1372 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001373
1374 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001375 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001376 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001377
akmhoque1771c412012-11-09 13:06:08 -06001378
akmhoque59980a52012-08-09 12:36:09 -05001379 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001380 {
akmhoqueffacaa82012-09-13 17:48:30 -05001381 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001382 {
akmhoqueffacaa82012-09-13 17:48:30 -05001383 if( nlsr->sched != NULL )
1384 {
akmhoque1771c412012-11-09 13:06:08 -06001385 long int micro_sec=ccn_schedule_run(nlsr->sched);
1386 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1387 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001388 }
1389 if(nlsr->ccn != NULL)
1390 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001391 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001392 }
1393 if (!(nlsr->sched && nlsr->ccn))
1394 {
1395 break;
1396 }
akmhoque29c1db52012-09-07 14:47:43 -05001397 }
1398
akmhoque59980a52012-08-09 12:36:09 -05001399 }
akmhoque1771c412012-11-09 13:06:08 -06001400
akmhoque59980a52012-08-09 12:36:09 -05001401
akmhoque59980a52012-08-09 12:36:09 -05001402 return 0;
1403}
1404