blob: 709924f98697260b121c0d010ab61579e5788001 [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{
akmhoqueb28579d2013-02-12 11:15:52 -0600122 //int ret;
akmhoquebfefef22012-09-26 10:09:34 -0500123 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);
akmhoqueb28579d2013-02-12 11:15:52 -0600134 //ret=process_id;
akmhoquebfefef22012-09-26 10:09:34 -0500135 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;
akmhoque7c234e02013-02-13 11:23:56 -0600165 char *ip_addr=(char *)calloc(20,sizeof(char));
166 //memset(ip_addr,0,12);
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 }
akmhoque7c234e02013-02-13 11:23:56 -0600185 struct name_prefix *nbr=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
186 nbr->name=(char *)calloc(strlen(rtr_name)+1,sizeof(char));
187 //memset(nbr->name,0,strlen(rtr_name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500188 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 {
akmhoque7c234e02013-02-13 11:23:56 -0600194 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600195 get_host_name_from_command_string(nbr_name,nbr->name,0);
akmhoque018692c2013-02-11 11:33:39 -0600196 if ( nlsr->debugging)
197 printf("Hostname of neighbor: %s ",nbr_name->name);
akmhoqueb77b95f2013-02-08 12:28:47 -0600198 get_ip_from_hostname_02(nbr_name->name,ip_addr);
akmhoque018692c2013-02-11 11:33:39 -0600199 if ( nlsr->debugging)
200 printf("IP Address: %s \n",ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600201 free(nbr_name->name);
202 free(nbr_name);
203 }
204 else
205 {
206 memcpy(ip_addr,nbr_ip_addr,strlen(nbr_ip_addr));
akmhoque018692c2013-02-11 11:33:39 -0600207 if (nlsr->debugging)
208 {
209 printf("Name of neighbor: %s ",nbr->name);
210 printf("IP Address: %s \n",ip_addr);
211 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600212 }
213 add_nbr_to_adl(nbr,0,ip_addr);
214
akmhoque03004e62012-09-06 01:12:28 -0500215
216 free(nbr->name);
217 free(nbr);
218}
219
220void
221process_command_ccnname(char *command)
222{
223
224 if(command==NULL)
225 {
226 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
227 return;
228 }
229 char *rem;
230 const char *sep=" \t\n";
231 char *name;
232 name=strtok_r(command,sep,&rem);
233 if(name==NULL)
234 {
235 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
236 return;
237 }
238
239 printf("Name Prefix: %s \n",name);
240
akmhoqued5152122012-09-19 06:44:23 -0500241 if ( name[strlen(name)-1] == '/' )
242 name[strlen(name)-1]='\0';
243
akmhoque7c234e02013-02-13 11:23:56 -0600244 struct name_prefix *np=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
245 np->name=(char *)calloc(strlen(name)+1,sizeof(char));
246 //memset(np->name,0,strlen(name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500247 memcpy(np->name,name,strlen(name)+1);
248 np->length=strlen(name)+1;
akmhoque386081b2012-08-10 10:53:21 -0500249
akmhoque03004e62012-09-06 01:12:28 -0500250 add_name_to_npl(np);
akmhoque28c45022012-08-09 15:38:02 -0500251
akmhoque03004e62012-09-06 01:12:28 -0500252 free(np->name);
akmhoque53f64222012-09-05 13:57:51 -0500253 free(np);
akmhoque59980a52012-08-09 12:36:09 -0500254}
255
akmhoque03004e62012-09-06 01:12:28 -0500256
257void
258process_command_router_name(char *command)
259{
260 if(command==NULL)
261 {
262 printf(" Wrong Command Format ( router-name /router/name )\n");
263 return;
264 }
265 char *rem;
266 const char *sep=" \t\n";
267 char *rtr_name;
268
269 rtr_name=strtok_r(command,sep,&rem);
270 if(rtr_name==NULL)
271 {
272 printf(" Wrong Command Format ( router-name /router/name )\n");
273 return;
274 }
275
276
akmhoqued5152122012-09-19 06:44:23 -0500277 if ( rtr_name[strlen(rtr_name)-1] == '/' )
278 rtr_name[strlen(rtr_name)-1]='\0';
279
akmhoque7c234e02013-02-13 11:23:56 -0600280 nlsr->router_name=(char *)calloc(strlen(rtr_name)+1,sizeof(char));
281 //memset(nlsr->router_name,0,strlen(rtr_name)+1);
akmhoque03004e62012-09-06 01:12:28 -0500282 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
283
284
285}
286
akmhoqueb77b95f2013-02-08 12:28:47 -0600287/*
akmhoque59980a52012-08-09 12:36:09 -0500288void
akmhoqued79438d2012-08-27 13:31:42 -0500289process_command_lsdb_synch_interval(char *command)
290{
291 if(command==NULL)
292 {
293 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
294 return;
295 }
296 char *rem;
297 const char *sep=" \t\n";
298 char *secs;
299 long int seconds;
300
301 secs=strtok_r(command,sep,&rem);
302 if(secs==NULL)
303 {
304 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
305 return;
306 }
307
308 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500309 if ( seconds >= 120 && seconds <= 3600 )
310 {
311 nlsr->lsdb_synch_interval=seconds;
312 }
akmhoqued79438d2012-08-27 13:31:42 -0500313
314}
akmhoqueb77b95f2013-02-08 12:28:47 -0600315*/
akmhoqued79438d2012-08-27 13:31:42 -0500316
317void
318process_command_interest_retry(char *command)
319{
320 if(command==NULL)
321 {
322 printf(" Wrong Command Format ( interest-retry number )\n");
323 return;
324 }
325 char *rem;
326 const char *sep=" \t\n";
akmhoqueffacaa82012-09-13 17:48:30 -0500327 char *retry;
328 long int retry_number;
akmhoqued79438d2012-08-27 13:31:42 -0500329
akmhoqueffacaa82012-09-13 17:48:30 -0500330 retry=strtok_r(command,sep,&rem);
331 if(retry==NULL)
akmhoqued79438d2012-08-27 13:31:42 -0500332 {
333 printf(" Wrong Command Format ( interest-retry number)\n");
334 return;
335 }
336
akmhoqueffacaa82012-09-13 17:48:30 -0500337 retry_number=atoi(retry);
338 if ( retry_number >= 1 && retry_number<=10 )
339 {
340 nlsr->interest_retry=retry_number;
341 }
akmhoqued79438d2012-08-27 13:31:42 -0500342
343}
344
345void
346process_command_interest_resend_time(char *command)
347{
348 if(command==NULL)
349 {
350 printf(" Wrong Command Format ( interest-resend-time secs )\n");
351 return;
352 }
353 char *rem;
354 const char *sep=" \t\n";
355 char *secs;
356 long int seconds;
357
358 secs=strtok_r(command,sep,&rem);
359 if(secs==NULL)
360 {
361 printf(" Wrong Command Format ( interest-resend-time secs)\n");
362 return;
363 }
364
365 seconds=atoi(secs);
akmhoqueffacaa82012-09-13 17:48:30 -0500366 if ( seconds <= 60 && seconds >= 1 )
367 {
368 nlsr->interest_resend_time=seconds;
369 }
akmhoqued79438d2012-08-27 13:31:42 -0500370}
371
akmhoque03004e62012-09-06 01:12:28 -0500372
akmhoqued5152122012-09-19 06:44:23 -0500373void
374process_command_lsa_refresh_time(char *command)
375{
376 if(command==NULL)
377 {
378 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
379 return;
380 }
381 char *rem;
382 const char *sep=" \t\n";
383 char *secs;
384 long int seconds;
385
386 secs=strtok_r(command,sep,&rem);
387 if(secs==NULL)
388 {
389 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
390 return;
391 }
392
393 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600394 if ( seconds >= 240)
akmhoqued5152122012-09-19 06:44:23 -0500395 {
396 nlsr->lsa_refresh_time=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600397 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
398 {
399 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
400 }
akmhoqued5152122012-09-19 06:44:23 -0500401 }
402
403}
404
405void
406process_command_router_dead_interval(char *command)
407{
408 if(command==NULL)
409 {
410 printf(" Wrong Command Format ( router-dead-interval secs )\n");
411 return;
412 }
413 char *rem;
414 const char *sep=" \t\n";
415 char *secs;
416 long int seconds;
417
418 secs=strtok_r(command,sep,&rem);
419 if(secs==NULL)
420 {
421 printf(" Wrong Command Format ( router-dead-interval secs)\n");
422 return;
423 }
424
425 seconds=atoi(secs);
akmhoqueb77b95f2013-02-08 12:28:47 -0600426 if ( seconds >= 480 )
akmhoqued5152122012-09-19 06:44:23 -0500427 {
428 nlsr->router_dead_interval=seconds;
akmhoqueb77b95f2013-02-08 12:28:47 -0600429 if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 )
430 {
431 nlsr->router_dead_interval=2*nlsr->lsa_refresh_time;
432 }
akmhoqued5152122012-09-19 06:44:23 -0500433 }
434
435}
akmhoque03004e62012-09-06 01:12:28 -0500436
akmhoqued79438d2012-08-27 13:31:42 -0500437void
akmhoqueb77b95f2013-02-08 12:28:47 -0600438process_command_max_faces_per_prefix(char *command)
akmhoque3cced642012-09-24 16:20:20 -0500439{
440 if(command==NULL)
441 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600442 printf(" Wrong Command Format ( max-faces-per-prefix n )\n");
akmhoque3cced642012-09-24 16:20:20 -0500443 return;
444 }
445 char *rem;
446 const char *sep=" \t\n";
447 char *num;
448 long int number;
449
450 num=strtok_r(command,sep,&rem);
451 if(num==NULL)
452 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600453 printf(" Wrong Command Format ( max-faces-per-prefix n)\n");
akmhoque3cced642012-09-24 16:20:20 -0500454 return;
455 }
456
457 number=atoi(num);
458 if ( number >= 0 && number <= 60 )
459 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600460 nlsr->max_faces_per_prefix=number;
akmhoque3cced642012-09-24 16:20:20 -0500461 }
462
463}
464
465void
akmhoquebfefef22012-09-26 10:09:34 -0500466process_command_logdir(char *command)
467{
468 if(command==NULL)
469 {
470 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
471 return;
472 }
473 char *rem;
474 const char *sep=" \t\n";
475 char *dir;
476
477 dir=strtok_r(command,sep,&rem);
478 if(dir==NULL)
479 {
480 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
481 return;
482 }
483
akmhoque7c234e02013-02-13 11:23:56 -0600484 nlsr->logDir=(char *)calloc(strlen(dir)+1,sizeof(char));
485 //memset(nlsr->logDir,0,strlen(dir)+1);
akmhoquebfefef22012-09-26 10:09:34 -0500486 memcpy(nlsr->logDir,dir,strlen(dir));
487}
488
489void
akmhoque7b791452012-10-30 11:24:56 -0500490process_command_detailed_log(char *command)
491{
492 if(command==NULL)
493 {
494 printf(" Wrong Command Format ( detailed-log on/off )\n");
495 return;
496 }
497 char *rem;
498 const char *sep=" \t\n";
499 char *on_off;
500
501 on_off=strtok_r(command,sep,&rem);
502 if(on_off==NULL)
503 {
504 printf(" Wrong Command Format ( detailed-log on/off )\n");
505 return;
506 }
507
508 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
509 {
510 nlsr->detailed_logging=1;
511 }
512}
513
514void
515process_command_debug(char *command)
516{
517 if(command==NULL)
518 {
519 printf(" Wrong Command Format ( debug on/off )\n");
520 return;
521 }
522 char *rem;
523 const char *sep=" \t\n";
524 char *on_off;
525
526 on_off=strtok_r(command,sep,&rem);
527 if(on_off==NULL)
528 {
529 printf(" Wrong Command Format ( debug on/off )\n");
530 return;
531 }
532
533 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
534 {
535 nlsr->debugging=1;
536 }
537}
538
akmhoqueb77b95f2013-02-08 12:28:47 -0600539
540void
541process_command_topo_prefix(char *command)
542{
543 if(command==NULL)
544 {
545 printf(" Wrong Command Format ( topo-prefix )\n");
546 return;
547 }
548 char *rem;
549 const char *sep=" \t\n";
550 char *topo_prefix;
551
552 topo_prefix=strtok_r(command,sep,&rem);
553 if(topo_prefix==NULL)
554 {
555 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
556 return;
557 }
558 else
559 {
560 if( nlsr->topo_prefix != NULL)
561 free(nlsr->topo_prefix);
562 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
563 topo_prefix[strlen(topo_prefix)-1]='\0';
564
akmhoque7c234e02013-02-13 11:23:56 -0600565 nlsr->topo_prefix=(char *)calloc(strlen(topo_prefix)+1,sizeof(char));
566 //memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600567 puts(topo_prefix);
568 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
569
570 }
571}
572
573
574void
575process_command_slice_prefix(char *command)
576{
577 if(command==NULL)
578 {
579 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
580 return;
581 }
582 char *rem;
583 const char *sep=" \t\n";
584 char *slice_prefix;
585
586 slice_prefix=strtok_r(command,sep,&rem);
587 if(slice_prefix==NULL)
588 {
589 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
590 return;
591 }
592 else
593 {
594 if ( nlsr->slice_prefix != NULL)
595 free(nlsr->slice_prefix);
596 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
597 slice_prefix[strlen(slice_prefix)-1]='\0';
598
akmhoque7c234e02013-02-13 11:23:56 -0600599 nlsr->slice_prefix=(char *)calloc(strlen(slice_prefix)+1,sizeof(char));
600 //memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -0600601 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
602 }
603}
604
605void
606process_command_hyperbolic_routing(char *command)
607{
608 if(command==NULL)
609 {
610 printf(" Wrong Command Format ( hyperbolic-routing on)\n");
611 return;
612 }
613 char *rem;
614 const char *sep=" \t\n";
615 char *on_off;
616
617 on_off=strtok_r(command,sep,&rem);
618 if(on_off==NULL)
619 {
620 printf(" Wrong Command Format ( hyperbolic-routing on )\n");
621 return;
622 }
623
624 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
625 {
626 nlsr->is_hyperbolic_calc=1;
627 }
628}
629
630void
631process_command_hyperbolic_cordinate(char *command)
632{
633 if(command==NULL)
634 {
635 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
636 return;
637 }
638
639 char *rem;
640 const char *sep=" \t\n\r";
641 char *radious;
642 char *theta;
643
644 radious=strtok_r(command,sep,&rem);
645 if (radious == NULL )
646 {
647 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
648 return;
649 }
650
651 theta=strtok_r(NULL,sep,&rem);
652 if (theta == NULL )
653 {
654 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
655 return;
656 }
657
658 nlsr->cor_r=strtof(radious,NULL);
659 nlsr->cor_theta=strtof(theta,NULL);
660
661}
662
663void
664process_command_tunnel_type(char *command)
665{
666 if(command==NULL)
667 {
668 printf(" Wrong Command Format ( tunnel-type udp/tcp)\n");
669 return;
670 }
671 char *rem;
672 const char *sep=" \t\n";
673 char *on_off;
674
675 on_off=strtok_r(command,sep,&rem);
676 if(on_off==NULL)
677 {
678 printf(" Wrong Command Format ( tunnel-type udp/tcp )\n");
679 return;
680 }
681
682 if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 )
683 {
684 nlsr->tunnel_type=IPPROTO_TCP;
685 }
686 else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 )
687 {
688 nlsr->tunnel_type=IPPROTO_UDP;
689 }
690}
691
akmhoque7b791452012-10-30 11:24:56 -0500692void
akmhoque59980a52012-08-09 12:36:09 -0500693process_conf_command(char *command)
694{
695 const char *separators=" \t\n";
696 char *remainder=NULL;
697 char *cmd_type=NULL;
698
699 if(command==NULL || strlen(command)==0 || command[0]=='!')
700 return;
701
702 cmd_type=strtok_r(command,separators,&remainder);
703
704 if(!strcmp(cmd_type,"router-name") )
705 {
706 process_command_router_name(remainder);
707 }
708 else if(!strcmp(cmd_type,"ccnneighbor") )
709 {
710 process_command_ccnneighbor(remainder);
711 }
712 else if(!strcmp(cmd_type,"ccnname") )
713 {
714 process_command_ccnname(remainder);
715 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600716 /*else if(!strcmp(cmd_type,"lsdb-synch-interval") )
akmhoqued79438d2012-08-27 13:31:42 -0500717 {
718 process_command_lsdb_synch_interval(remainder);
akmhoqueb77b95f2013-02-08 12:28:47 -0600719 }*/
akmhoqued79438d2012-08-27 13:31:42 -0500720 else if(!strcmp(cmd_type,"interest-retry") )
721 {
722 process_command_interest_retry(remainder);
723 }
724 else if(!strcmp(cmd_type,"interest-resend-time") )
725 {
726 process_command_interest_resend_time(remainder);
727 }
akmhoqued5152122012-09-19 06:44:23 -0500728 else if(!strcmp(cmd_type,"lsa-refresh-time") )
729 {
730 process_command_lsa_refresh_time(remainder);
731 }
732 else if(!strcmp(cmd_type,"router-dead-interval") )
733 {
734 process_command_router_dead_interval(remainder);
735 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600736 else if(!strcmp(cmd_type,"max-faces-per-prefix") )
akmhoque3cced642012-09-24 16:20:20 -0500737 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600738 process_command_max_faces_per_prefix(remainder);
akmhoque3cced642012-09-24 16:20:20 -0500739 }
akmhoquebfefef22012-09-26 10:09:34 -0500740 else if(!strcmp(cmd_type,"logdir") )
741 {
742 process_command_logdir(remainder);
743 }
akmhoque7b791452012-10-30 11:24:56 -0500744 else if(!strcmp(cmd_type,"detailed-log") )
745 {
746 process_command_detailed_log(remainder);
747 }
748 else if(!strcmp(cmd_type,"debug") )
749 {
750 process_command_debug(remainder);
751 }
akmhoqueb77b95f2013-02-08 12:28:47 -0600752 else if(!strcmp(cmd_type,"topo-prefix") )
753 {
754 process_command_topo_prefix(remainder);
755 }
756 else if(!strcmp(cmd_type,"slice-prefix") )
757 {
758 process_command_slice_prefix(remainder);
759 }
760 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
761 {
762 process_command_hyperbolic_cordinate(remainder);
763 }
764 else if(!strcmp(cmd_type,"hyperbolic-routing") )
765 {
766 process_command_hyperbolic_routing(remainder);
767 }
768 else if(!strcmp(cmd_type,"tunnel-type") )
769 {
770 process_command_tunnel_type(remainder);
771 }
akmhoqued5152122012-09-19 06:44:23 -0500772 else
akmhoque59980a52012-08-09 12:36:09 -0500773 {
774 printf("Wrong configuration Command %s \n",cmd_type);
775 }
776}
777
akmhoque03004e62012-09-06 01:12:28 -0500778
akmhoque59980a52012-08-09 12:36:09 -0500779int
780readConfigFile(const char *filename)
781{
782 FILE *cfg;
783 char buf[1024];
784 int len;
785
786 cfg=fopen(filename, "r");
787
788 if(cfg == NULL)
789 {
790 printf("\nConfiguration File does not exists\n");
791 exit(1);
792 }
793
794 while(fgets((char *)buf, sizeof(buf), cfg))
795 {
796 len=strlen(buf);
797 if(buf[len-1] == '\n')
798 buf[len-1]='\0';
akmhoqued5152122012-09-19 06:44:23 -0500799 if ( buf[0] != '#' && buf[0] != '!')
800 process_conf_command(buf);
akmhoque59980a52012-08-09 12:36:09 -0500801 }
802
803 fclose(cfg);
804
805 return 0;
806}
807
akmhoqueb77b95f2013-02-08 12:28:47 -0600808
809void
810add_faces_for_nbrs(void)
811{
812 int i, adl_element;
813 struct ndn_neighbor *nbr;
814
815 struct hashtb_enumerator ee;
816 struct hashtb_enumerator *e = &ee;
817
818 hashtb_start(nlsr->adl, e);
819 adl_element=hashtb_n(nlsr->adl);
820
821 for(i=0;i<adl_element;i++)
822 {
823 nbr=e->data;
824 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695,nlsr->tunnel_type);
825 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
826 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
827 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
828 hashtb_next(e);
829 }
830
831 hashtb_end(e);
832
833}
834
835void
836destroy_faces_for_nbrs(void)
837{
838 int i, adl_element;
839 struct ndn_neighbor *nbr;
840
841 struct hashtb_enumerator ee;
842 struct hashtb_enumerator *e = &ee;
843
844 hashtb_start(nlsr->adl, e);
845 adl_element=hashtb_n(nlsr->adl);
846
847 for(i=0;i<adl_element;i++)
848 {
849 nbr=e->data;
850 if ( nbr->face > 0 )
851 {
852 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
853 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
854 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face);
855 }
856 hashtb_next(e);
857 }
858
859 hashtb_end(e);
860
861}
862
akmhoque562caef2012-11-09 13:29:06 -0600863char *
864process_api_client_command(char *command)
865{
866 char *msg;
867 msg=(char *)malloc(100);
akmhoqueb77b95f2013-02-08 12:28:47 -0600868 memset(msg,0,100);
869
akmhoque3171d652012-11-13 11:44:33 -0600870 const char *sep=" \t\n";
871 char *rem=NULL;
872 char *cmd_type=NULL;
873 char *op_type=NULL;
874 char *name=NULL;
875 char *face=NULL;
876 int face_id;
877 int res;
878
879 op_type=strtok_r(command,sep,&rem);
880 cmd_type=strtok_r(NULL,sep,&rem);
881 name=strtok_r(NULL,sep,&rem);
882 if ( name[strlen(name)-1] == '/' )
883 name[strlen(name)-1]='\0';
884
akmhoque7c234e02013-02-13 11:23:56 -0600885 struct name_prefix *np=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
886 np->name=(char *)calloc(strlen(name)+1,sizeof(char));
887 //memset(np->name,0,strlen(name)+1);
akmhoque3171d652012-11-13 11:44:33 -0600888 memcpy(np->name,name,strlen(name)+1);
889 np->length=strlen(name)+1;
890
891 if ( strcmp(cmd_type,"name")!= 0 )
892 {
893 face=strtok_r(NULL,sep,&rem);
894 sscanf(face,"face%d",&face_id);
895 }
akmhoque562caef2012-11-09 13:29:06 -0600896
akmhoque3171d652012-11-13 11:44:33 -0600897 if ( strcmp(cmd_type,"name")== 0 )
898 {
899 if ( strcmp(op_type,"del") == 0 )
900 {
901 res=does_name_exist_in_npl(np);
902 if ( res == 0)
903 {
904 sprintf(msg,"Name %s does not exist !!",name);
905 }
906 else
907 {
908 long int ls_id=get_lsa_id_from_npl(np);
909 if ( ls_id != 0 )
910 {
911 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
912 sprintf(msg,"Name %s has been deleted and Advertised.",name);
913 }
914 else
915 {
916 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
917 }
918 }
919 }
920 else if ( strcmp(op_type,"add") == 0 )
921 {
922 res=does_name_exist_in_npl(np);
923 if ( res == 0)
924 {
925 add_name_to_npl(np);
926 build_and_install_single_name_lsa(np);
927 sprintf(msg,"Name %s has been added to advertise.",name);
928 }
929 else
930 {
931 sprintf(msg,"Name %s has already been advertised from this router !!",name);
932 }
933 }
934 }
935 else if ( strcmp(cmd_type,"neighbor") == 0 )
936 {
937 if ( strcmp(op_type,"del") == 0 )
938 {
939 res=is_neighbor(np->name);
940 if ( res == 0)
941 {
942 sprintf(msg,"Neighbor %s does not exist !!",name);
943 }
944 else
945 {
946 update_adjacent_status_to_adl(np,NBR_DOWN);
akmhoqueb77b95f2013-02-08 12:28:47 -0600947 int face_id=get_next_hop_face_from_adl(np->name);
948 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id);
949 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id);
950 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id);
akmhoque3171d652012-11-13 11:44:33 -0600951 delete_nbr_from_adl(np);
952 if(!nlsr->is_build_adj_lsa_sheduled)
953 {
954 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
955 nlsr->is_build_adj_lsa_sheduled=1;
956 }
957 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
958 }
959 }
960 else if ( strcmp(op_type,"add") == 0 )
961 {
962 res=is_neighbor(np->name);
963 if ( res == 0 )
964 {
akmhoque7c234e02013-02-13 11:23:56 -0600965 struct name_prefix *nbr_name=(struct name_prefix *)calloc(1,sizeof(struct name_prefix ));
akmhoqueb77b95f2013-02-08 12:28:47 -0600966 get_host_name_from_command_string(nbr_name,np->name,0);
967 printf("Hostname of neighbor: %s ",nbr_name->name);
968
akmhoque7c234e02013-02-13 11:23:56 -0600969 char *ip_addr=(char *)calloc(20,sizeof(char));
970 //memset(ip_addr,0,20);
akmhoqueb77b95f2013-02-08 12:28:47 -0600971 get_ip_from_hostname_02(nbr_name->name,ip_addr);
972 printf("IP Address: %s \n",ip_addr);
973 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type);
974 update_face_to_adl_for_nbr(nbr_name->name, face_id);
975 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
976 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
977
978 add_nbr_to_adl(np,face_id,ip_addr);
979
akmhoque3171d652012-11-13 11:44:33 -0600980 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoque7c234e02013-02-13 11:23:56 -0600981 free(ip_addr);
akmhoqueb77b95f2013-02-08 12:28:47 -0600982
akmhoque3171d652012-11-13 11:44:33 -0600983 }
984 else
985 {
986 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
987 }
988 }
989 }
990
akmhoque562caef2012-11-09 13:29:06 -0600991
992 return msg;
993}
akmhoque1771c412012-11-09 13:06:08 -0600994
995int
996nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
997{
998 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600999 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -06001000 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001001 timeout.tv_sec=0;
1002 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -06001003 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001004 else
akmhoque1771c412012-11-09 13:06:08 -06001005 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001006 timeout.tv_sec = 0;
1007 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001008 }
akmhoque1771c412012-11-09 13:06:08 -06001009
1010 int fd;
1011 int nread;
1012 int result;
1013 fd_set testfds;
1014 unsigned int client_len;
1015 int client_sockfd;
1016 char recv_buffer[1024];
1017 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -06001018 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001019
1020 testfds=nlsr->readfds;
1021 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1022
akmhoqueb77b95f2013-02-08 12:28:47 -06001023 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -06001024 {
1025 if(FD_ISSET(fd,&testfds))
1026 {
1027 if ( fd == ccn_fd )
1028 {
1029 return 0;
1030 }
1031 else if(fd == nlsr->nlsr_api_server_sock_fd)
1032 {
1033 client_len = sizeof(client_address);
1034 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1035 FD_SET(client_sockfd, &nlsr->readfds);
1036 }
1037 else
1038 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001039
akmhoque1771c412012-11-09 13:06:08 -06001040 ioctl(fd, FIONREAD, &nread);
1041 if(nread == 0)
1042 {
1043 close(fd);
1044 FD_CLR(fd, &nlsr->readfds);
1045 }
1046 else
1047 {
1048 recv(fd, recv_buffer, 1024, 0);
akmhoqueb77b95f2013-02-08 12:28:47 -06001049 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001050 char *msg=process_api_client_command(recv_buffer);
1051 send(fd, msg, strlen(msg),0);
1052 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001053 close(fd);
1054 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001055 }
1056 }
1057 }
1058 }
1059
1060 return 0;
1061}
1062
akmhoqueb77b95f2013-02-08 12:28:47 -06001063int
1064check_config_validity()
1065{
1066 if (nlsr->router_name == NULL )
1067 {
1068 fprintf(stderr,"Router name has not been configured :(\n");
1069 return -1;
1070 }
1071 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1072 {
1073 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1074 return -1;
1075 }
1076
1077 return 0;
1078}
1079
akmhoque386081b2012-08-10 10:53:21 -05001080void
1081nlsr_destroy( void )
1082{
akmhoque7b791452012-10-30 11:24:56 -05001083 if ( nlsr->debugging )
1084 {
1085 printf("Freeing Allocated Memory....\n");
1086 }
akmhoque9e9fc722012-09-26 14:03:25 -05001087 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001088 /* Destroying all face created by nlsr in CCND */
1089 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001090 destroy_faces_for_nbrs();
akmhoque386081b2012-08-10 10:53:21 -05001091 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque7c234e02013-02-13 11:23:56 -06001092 hashtb_destroy(&nlsr->adl);
1093 hashtb_destroy(&nlsr->npl);
1094 hashtb_destroy(&nlsr->pit_alsa);
akmhoque03004e62012-09-06 01:12:28 -05001095 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1096 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque7c234e02013-02-13 11:23:56 -06001097 hashtb_destroy(&nlsr->lsdb->cor_lsdb);
akmhoque3cced642012-09-24 16:20:20 -05001098
akmhoque7c234e02013-02-13 11:23:56 -06001099 int i, npt_element,rt_element;
akmhoque3560cb62012-09-09 10:52:30 -05001100 struct npt_entry *ne;
1101 struct hashtb_enumerator ee;
1102 struct hashtb_enumerator *e = &ee;
1103 hashtb_start(nlsr->npt, e);
1104 npt_element=hashtb_n(nlsr->npt);
1105 for(i=0;i<npt_element;i++)
1106 {
1107 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001108 hashtb_destroy(&ne->name_list);
1109 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -05001110 hashtb_next(e);
1111 }
1112
1113 hashtb_end(e);
1114 hashtb_destroy(&nlsr->npt);
akmhoque7c234e02013-02-13 11:23:56 -06001115
1116
1117 struct routing_table_entry *rte;
1118 hashtb_start(nlsr->routing_table, e);
1119 rt_element=hashtb_n(nlsr->routing_table);
1120 for(i=0;i<rt_element;i++)
1121 {
1122 rte=e->data;
1123 hashtb_destroy(&rte->face_list);
1124 hashtb_next(e);
1125 }
1126 hashtb_end(e);
1127 hashtb_destroy(&nlsr->routing_table);
1128
1129 if ( nlsr->ccns != NULL )
1130 ccns_close(&nlsr->ccns, NULL, NULL);
1131 if ( nlsr->slice != NULL )
1132 ccns_slice_destroy(&nlsr->slice);
akmhoque3560cb62012-09-09 10:52:30 -05001133
akmhoque7c234e02013-02-13 11:23:56 -06001134 close(nlsr->nlsr_api_server_sock_fd);
akmhoque95041802012-11-16 09:18:02 -06001135
akmhoque7c234e02013-02-13 11:23:56 -06001136 if ( nlsr->sched != NULL )
1137 ccn_schedule_destroy(&nlsr->sched);
akmhoque866c2222013-02-12 10:49:33 -06001138
1139 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001140 free(nlsr->lsdb->lsdb_version);
1141 free(nlsr->lsdb);
1142 free(nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001143 if ( nlsr->debugging )
1144 {
1145 printf("Finished freeing allocated memory\n");
1146 }
akmhoque9e9fc722012-09-26 14:03:25 -05001147 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque7c234e02013-02-13 11:23:56 -06001148
1149 free(nlsr);
akmhoque53f64222012-09-05 13:57:51 -05001150
akmhoque386081b2012-08-10 10:53:21 -05001151}
1152
akmhoque03004e62012-09-06 01:12:28 -05001153
akmhoqueb77b95f2013-02-08 12:28:47 -06001154
akmhoque1771c412012-11-09 13:06:08 -06001155void
1156init_api_server(int ccn_fd)
1157{
1158 int server_sockfd;
1159 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001160 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001161 unsigned int yes=1;
1162
akmhoque95041802012-11-16 09:18:02 -06001163 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001164
1165 int flags = fcntl(server_sockfd, F_GETFL, 0);
1166 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1167
akmhoquef31f13b2012-11-16 09:42:24 -06001168 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1169 {
1170 ON_ERROR_DESTROY(-1);
1171 }
akmhoque95041802012-11-16 09:18:02 -06001172
1173 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001174 server_address.sin_addr.s_addr = INADDR_ANY;
1175 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001176
akmhoque1771c412012-11-09 13:06:08 -06001177 server_len = sizeof(server_address);
1178 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1179 listen(server_sockfd, 100);
1180 FD_ZERO(&nlsr->readfds);
1181 FD_SET(server_sockfd, &nlsr->readfds);
1182 FD_SET(ccn_fd, &nlsr->readfds);
1183 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1184
1185}
1186
akmhoque81c25e02012-09-10 14:50:33 -05001187int
akmhoque902d57e2012-08-17 09:24:38 -05001188init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001189{
akmhoque03004e62012-09-06 01:12:28 -05001190 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1191 {
1192 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001193 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001194 }
1195 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1196 {
1197 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001198 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001199 }
1200 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1201 {
1202 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001203 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001204 }
akmhoque902d57e2012-08-17 09:24:38 -05001205
akmhoque7c234e02013-02-13 11:23:56 -06001206 nlsr=(struct nlsr *)calloc(1,sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -05001207
1208 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -05001209 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -05001210 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -06001211 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -05001212 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -05001213 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -05001214 struct hashtb_param param_npt = {0};
1215 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1216 struct hashtb_param param_rte = {0};
1217 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -05001218
akmhoque59980a52012-08-09 12:36:09 -05001219 nlsr->in_interest.p = &incoming_interest;
1220 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -05001221
akmhoque03004e62012-09-06 01:12:28 -05001222 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -05001223
akmhoque7c234e02013-02-13 11:23:56 -06001224 char *time_stamp=(char *)calloc(20,sizeof(char));
1225 //memset(time_stamp,0,20);
akmhoque03004e62012-09-06 01:12:28 -05001226 get_current_timestamp_micro(time_stamp);
1227 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001228 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001229 free(time_stamp);
1230
1231 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001232 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -05001233 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001234 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueb77b95f2013-02-08 12:28:47 -06001235 struct hashtb_param param_cor_lsdb = {0};
1236 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001237
1238
akmhoque902d57e2012-08-17 09:24:38 -05001239
akmhoque53f64222012-09-05 13:57:51 -05001240
akmhoque59980a52012-08-09 12:36:09 -05001241 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001242 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001243 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001244 nlsr->adj_build_count=0;
1245 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001246 nlsr->is_send_lsdb_interest_scheduled=0;
1247 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001248
akmhoque7b791452012-10-30 11:24:56 -05001249 nlsr->detailed_logging=0;
1250 nlsr->debugging=0;
1251
akmhoqueb77b95f2013-02-08 12:28:47 -06001252 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoqued79438d2012-08-27 13:31:42 -05001253 nlsr->interest_retry = INTEREST_RETRY;
1254 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001255 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1256 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001257 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001258 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001259
akmhoque95041802012-11-16 09:18:02 -06001260 nlsr->api_port=API_PORT;
1261
akmhoque7c234e02013-02-13 11:23:56 -06001262 nlsr->topo_prefix=(char *)calloc(strlen("/ndn/routing/nlsr")+1,sizeof(char));
1263 //memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001264 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1265
akmhoque7c234e02013-02-13 11:23:56 -06001266 nlsr->slice_prefix=(char *)calloc(strlen("/ndn/routing/nlsr/LSA")+1,sizeof(char));
1267 //memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001268 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1269
1270 nlsr->is_hyperbolic_calc=0;
1271 nlsr->cor_r=-1.0;
1272 nlsr->cor_theta=-1.0;
1273
1274 nlsr->tunnel_type=IPPROTO_UDP;
1275
akmhoque81c25e02012-09-10 14:50:33 -05001276 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001277}
1278
akmhoque03004e62012-09-06 01:12:28 -05001279
akmhoque902d57e2012-08-17 09:24:38 -05001280int
1281main(int argc, char *argv[])
1282{
akmhoque81c25e02012-09-10 14:50:33 -05001283 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -05001284 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001285 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001286 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001287
akmhoquebfefef22012-09-26 10:09:34 -05001288
akmhoque81c25e02012-09-10 14:50:33 -05001289
akmhoque95041802012-11-16 09:18:02 -06001290 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001291 {
1292 switch (res)
1293 {
1294 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001295 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001296 break;
1297 case 'f':
1298 config_file = optarg;
1299 break;
akmhoque95041802012-11-16 09:18:02 -06001300 case 'p':
1301 port = atoi(optarg);
1302 break;
akmhoque59980a52012-08-09 12:36:09 -05001303 case 'h':
1304 default:
1305 usage(argv[0]);
1306 }
1307 }
1308
akmhoquebfefef22012-09-26 10:09:34 -05001309 ret=init_nlsr();
1310 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001311
1312 if ( port !=0 )
1313 nlsr->api_port=port;
1314
akmhoque59980a52012-08-09 12:36:09 -05001315 readConfigFile(config_file);
akmhoqueb77b95f2013-02-08 12:28:47 -06001316
1317 ON_ERROR_DESTROY(check_config_validity());
1318
1319 print_adjacent_from_adl();
1320
akmhoquebfefef22012-09-26 10:09:34 -05001321 if ( daemon_mode == 1 )
1322 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001323 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001324 daemonize_nlsr();
1325 }
1326
1327 startLogging(nlsr->logDir);
1328
akmhoque59980a52012-08-09 12:36:09 -05001329 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001330 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1331 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001332 {
1333 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001334 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001335 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001336 }
akmhoque1771c412012-11-09 13:06:08 -06001337
1338 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001339
1340 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1341 if(res<0)
1342 {
1343 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1344 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1345 ON_ERROR_DESTROY(res);
1346 }
akmhoque53f64222012-09-05 13:57:51 -05001347 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001348 router_prefix=ccn_charbuf_create();
1349 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001350 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001351 {
1352 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001353 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001354 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001355 }
akmhoque59980a52012-08-09 12:36:09 -05001356
1357 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001358 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001359 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1360 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001361 {
1362 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001363 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001364 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001365 }
1366 ccn_charbuf_destroy(&router_prefix);
1367
akmhoque7b791452012-10-30 11:24:56 -05001368 if ( nlsr->debugging )
1369 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001370 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001371 if ( nlsr->debugging )
1372 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001373 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001374
akmhoqueb77b95f2013-02-08 12:28:47 -06001375 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001376 print_name_prefix_from_npl();
1377 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001378 build_and_install_name_lsas();
1379
akmhoque866c2222013-02-12 10:49:33 -06001380 res=sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1381 ON_ERROR_DESTROY(res);
akmhoqueb77b95f2013-02-08 12:28:47 -06001382
1383 print_name_lsdb();
1384 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1385 {
1386 build_and_install_cor_lsa();
1387 }
1388 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001389
1390 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001391 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001392 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001393
akmhoque1771c412012-11-09 13:06:08 -06001394
akmhoque59980a52012-08-09 12:36:09 -05001395 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001396 {
akmhoqueffacaa82012-09-13 17:48:30 -05001397 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001398 {
akmhoqueffacaa82012-09-13 17:48:30 -05001399 if( nlsr->sched != NULL )
1400 {
akmhoque1771c412012-11-09 13:06:08 -06001401 long int micro_sec=ccn_schedule_run(nlsr->sched);
1402 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1403 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001404 }
1405 if(nlsr->ccn != NULL)
1406 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001407 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001408 }
1409 if (!(nlsr->sched && nlsr->ccn))
1410 {
1411 break;
1412 }
akmhoque29c1db52012-09-07 14:47:43 -05001413 }
1414
akmhoque59980a52012-08-09 12:36:09 -05001415 }
akmhoque1771c412012-11-09 13:06:08 -06001416
akmhoque59980a52012-08-09 12:36:09 -05001417
akmhoque59980a52012-08-09 12:36:09 -05001418 return 0;
1419}
1420