blob: 04fb8cfd41d0046e2b92b765c9208414bc7bd45c [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;
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);
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
akmhoque53f64222012-09-05 13:57:51 -0500244 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
akmhoque03004e62012-09-06 01:12:28 -0500245 np->name=(char *)malloc(strlen(name)+1);
246 memset(np->name,0,strlen(name)+1);
247 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
akmhoque03004e62012-09-06 01:12:28 -0500280 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
281 memset(nlsr->router_name,0,strlen(rtr_name)+1);
282 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
484 nlsr->logDir=(char *)malloc(strlen(dir)+1);
485 memset(nlsr->logDir,0,strlen(dir)+1);
486 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
565 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
566 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
567 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
599 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
600 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
601 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
885 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
886 np->name=(char *)malloc(strlen(name)+1);
887 memset(np->name,0,strlen(name)+1);
888 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 {
akmhoqueb77b95f2013-02-08 12:28:47 -0600965 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
966 get_host_name_from_command_string(nbr_name,np->name,0);
967 printf("Hostname of neighbor: %s ",nbr_name->name);
968
969 char *ip_addr=(char *)malloc(13);
970 memset(ip_addr,0,13);
971 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);
akmhoqueb77b95f2013-02-08 12:28:47 -0600981
akmhoque3171d652012-11-13 11:44:33 -0600982 }
983 else
984 {
985 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
986 }
987 }
988 }
989
akmhoque562caef2012-11-09 13:29:06 -0600990
991 return msg;
992}
akmhoque1771c412012-11-09 13:06:08 -0600993
994int
995nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
996{
997 struct timeval timeout;
akmhoqueb77b95f2013-02-08 12:28:47 -0600998 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque1771c412012-11-09 13:06:08 -0600999 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001000 timeout.tv_sec=0;
1001 timeout.tv_usec=time_out_micro_sec;
akmhoque1771c412012-11-09 13:06:08 -06001002 }
akmhoqueb77b95f2013-02-08 12:28:47 -06001003 else
akmhoque1771c412012-11-09 13:06:08 -06001004 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001005 timeout.tv_sec = 0;
1006 timeout.tv_usec = 500000;
akmhoque1771c412012-11-09 13:06:08 -06001007 }
akmhoque1771c412012-11-09 13:06:08 -06001008
1009 int fd;
1010 int nread;
1011 int result;
1012 fd_set testfds;
1013 unsigned int client_len;
1014 int client_sockfd;
1015 char recv_buffer[1024];
1016 bzero(recv_buffer,1024);
akmhoque95041802012-11-16 09:18:02 -06001017 struct sockaddr_in client_address;
akmhoque1771c412012-11-09 13:06:08 -06001018
1019 testfds=nlsr->readfds;
1020 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
1021
akmhoqueb77b95f2013-02-08 12:28:47 -06001022 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque1771c412012-11-09 13:06:08 -06001023 {
1024 if(FD_ISSET(fd,&testfds))
1025 {
1026 if ( fd == ccn_fd )
1027 {
1028 return 0;
1029 }
1030 else if(fd == nlsr->nlsr_api_server_sock_fd)
1031 {
1032 client_len = sizeof(client_address);
1033 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
1034 FD_SET(client_sockfd, &nlsr->readfds);
1035 }
1036 else
1037 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001038
akmhoque1771c412012-11-09 13:06:08 -06001039 ioctl(fd, FIONREAD, &nread);
1040 if(nread == 0)
1041 {
1042 close(fd);
1043 FD_CLR(fd, &nlsr->readfds);
1044 }
1045 else
1046 {
1047 recv(fd, recv_buffer, 1024, 0);
akmhoqueb77b95f2013-02-08 12:28:47 -06001048 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
akmhoque562caef2012-11-09 13:29:06 -06001049 char *msg=process_api_client_command(recv_buffer);
1050 send(fd, msg, strlen(msg),0);
1051 free(msg);
akmhoque1771c412012-11-09 13:06:08 -06001052 close(fd);
1053 FD_CLR(fd, &nlsr->readfds);
akmhoque1771c412012-11-09 13:06:08 -06001054 }
1055 }
1056 }
1057 }
1058
1059 return 0;
1060}
1061
akmhoqueb77b95f2013-02-08 12:28:47 -06001062int
1063check_config_validity()
1064{
1065 if (nlsr->router_name == NULL )
1066 {
1067 fprintf(stderr,"Router name has not been configured :(\n");
1068 return -1;
1069 }
1070 if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) )
1071 {
1072 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1073 return -1;
1074 }
1075
1076 return 0;
1077}
1078
akmhoque386081b2012-08-10 10:53:21 -05001079void
1080nlsr_destroy( void )
1081{
akmhoque7b791452012-10-30 11:24:56 -05001082 if ( nlsr->debugging )
1083 {
1084 printf("Freeing Allocated Memory....\n");
1085 }
akmhoque9e9fc722012-09-26 14:03:25 -05001086 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
akmhoquefbfd0982012-09-09 20:59:03 -05001087 /* Destroying all face created by nlsr in CCND */
1088 destroy_all_face_by_nlsr();
akmhoqueb77b95f2013-02-08 12:28:47 -06001089 destroy_faces_for_nbrs();
akmhoque386081b2012-08-10 10:53:21 -05001090 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
akmhoque53f64222012-09-05 13:57:51 -05001091 hashtb_destroy(&nlsr->npl);
akmhoque03004e62012-09-06 01:12:28 -05001092 hashtb_destroy(&nlsr->adl);
1093 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1094 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001095 hashtb_destroy(&nlsr->pit_alsa);
akmhoque3cced642012-09-24 16:20:20 -05001096
akmhoqueb77b95f2013-02-08 12:28:47 -06001097
akmhoque3cced642012-09-24 16:20:20 -05001098
akmhoque3560cb62012-09-09 10:52:30 -05001099 hashtb_destroy(&nlsr->routing_table);
1100
1101
1102 int i, npt_element;
1103 struct npt_entry *ne;
1104 struct hashtb_enumerator ee;
1105 struct hashtb_enumerator *e = &ee;
1106 hashtb_start(nlsr->npt, e);
1107 npt_element=hashtb_n(nlsr->npt);
1108 for(i=0;i<npt_element;i++)
1109 {
1110 ne=e->data;
akmhoque3cced642012-09-24 16:20:20 -05001111 hashtb_destroy(&ne->name_list);
1112 hashtb_destroy(&ne->face_list);
akmhoque3560cb62012-09-09 10:52:30 -05001113 hashtb_next(e);
1114 }
1115
1116 hashtb_end(e);
1117 hashtb_destroy(&nlsr->npt);
1118
akmhoque95041802012-11-16 09:18:02 -06001119
akmhoqueb77b95f2013-02-08 12:28:47 -06001120 ccns_close(&nlsr->ccns, NULL, NULL);
1121 ccns_slice_destroy(&nlsr->slice);
1122
1123
1124
akmhoque95041802012-11-16 09:18:02 -06001125 close(nlsr->nlsr_api_server_sock_fd);
1126
akmhoque386081b2012-08-10 10:53:21 -05001127 ccn_schedule_destroy(&nlsr->sched);
akmhoque03004e62012-09-06 01:12:28 -05001128
akmhoque866c2222013-02-12 10:49:33 -06001129 ccns_close(&nlsr->ccns,NULL, NULL);
1130 ccns_slice_destroy(&nlsr->slice);
1131
1132 ccn_destroy(&nlsr->ccn);
akmhoque03004e62012-09-06 01:12:28 -05001133 free(nlsr->lsdb->lsdb_version);
1134 free(nlsr->lsdb);
1135 free(nlsr->router_name);
akmhoque386081b2012-08-10 10:53:21 -05001136 free(nlsr);
akmhoque7b791452012-10-30 11:24:56 -05001137 if ( nlsr->debugging )
1138 {
1139 printf("Finished freeing allocated memory\n");
1140 }
akmhoque9e9fc722012-09-26 14:03:25 -05001141 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
akmhoque53f64222012-09-05 13:57:51 -05001142
akmhoque386081b2012-08-10 10:53:21 -05001143}
1144
akmhoque03004e62012-09-06 01:12:28 -05001145
akmhoqueb77b95f2013-02-08 12:28:47 -06001146
akmhoque1771c412012-11-09 13:06:08 -06001147void
1148init_api_server(int ccn_fd)
1149{
1150 int server_sockfd;
1151 int server_len;
akmhoque95041802012-11-16 09:18:02 -06001152 struct sockaddr_in server_address;
akmhoquef31f13b2012-11-16 09:42:24 -06001153 unsigned int yes=1;
1154
akmhoque95041802012-11-16 09:18:02 -06001155 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
akmhoque1771c412012-11-09 13:06:08 -06001156
1157 int flags = fcntl(server_sockfd, F_GETFL, 0);
1158 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1159
akmhoquef31f13b2012-11-16 09:42:24 -06001160 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1161 {
1162 ON_ERROR_DESTROY(-1);
1163 }
akmhoque95041802012-11-16 09:18:02 -06001164
1165 server_address.sin_family = AF_INET;
Adam Alyyanb5fff372013-01-09 14:32:52 -06001166 server_address.sin_addr.s_addr = INADDR_ANY;
1167 server_address.sin_port = htons(nlsr->api_port);
akmhoque95041802012-11-16 09:18:02 -06001168
akmhoque1771c412012-11-09 13:06:08 -06001169 server_len = sizeof(server_address);
1170 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1171 listen(server_sockfd, 100);
1172 FD_ZERO(&nlsr->readfds);
1173 FD_SET(server_sockfd, &nlsr->readfds);
1174 FD_SET(ccn_fd, &nlsr->readfds);
1175 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1176
1177}
1178
akmhoque81c25e02012-09-10 14:50:33 -05001179int
akmhoque902d57e2012-08-17 09:24:38 -05001180init_nlsr(void)
akmhoque59980a52012-08-09 12:36:09 -05001181{
akmhoque03004e62012-09-06 01:12:28 -05001182 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1183 {
1184 perror("SIGQUIT install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001185 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001186 }
1187 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1188 {
1189 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001190 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001191 }
1192 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1193 {
1194 perror("SIGTERM install error\n");
akmhoque81c25e02012-09-10 14:50:33 -05001195 return -1;
akmhoque03004e62012-09-06 01:12:28 -05001196 }
akmhoque902d57e2012-08-17 09:24:38 -05001197
akmhoque59980a52012-08-09 12:36:09 -05001198 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
akmhoque03004e62012-09-06 01:12:28 -05001199
1200 struct hashtb_param param_adl = {0};
akmhoque28c45022012-08-09 15:38:02 -05001201 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
akmhoque03004e62012-09-06 01:12:28 -05001202 struct hashtb_param param_npl = {0};
akmhoque3171d652012-11-13 11:44:33 -06001203 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
akmhoque29c1db52012-09-07 14:47:43 -05001204 struct hashtb_param param_pit_alsa = {0};
akmhoque1ce71052012-09-13 22:51:32 -05001205 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
akmhoque3560cb62012-09-09 10:52:30 -05001206 struct hashtb_param param_npt = {0};
1207 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1208 struct hashtb_param param_rte = {0};
1209 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
akmhoque29c1db52012-09-07 14:47:43 -05001210
akmhoque59980a52012-08-09 12:36:09 -05001211 nlsr->in_interest.p = &incoming_interest;
1212 nlsr->in_content.p = &incoming_content;
akmhoque07dd8cc2012-08-16 10:23:01 -05001213
akmhoque03004e62012-09-06 01:12:28 -05001214 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
akmhoque07dd8cc2012-08-16 10:23:01 -05001215
akmhoque03004e62012-09-06 01:12:28 -05001216 char *time_stamp=(char *)malloc(20);
1217 memset(time_stamp,0,20);
1218 get_current_timestamp_micro(time_stamp);
1219 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueb77b95f2013-02-08 12:28:47 -06001220 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque03004e62012-09-06 01:12:28 -05001221 free(time_stamp);
1222
1223 struct hashtb_param param_adj_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001224 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
akmhoque03004e62012-09-06 01:12:28 -05001225 struct hashtb_param param_name_lsdb = {0};
akmhoquef71d9082012-08-22 12:51:53 -04001226 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueb77b95f2013-02-08 12:28:47 -06001227 struct hashtb_param param_cor_lsdb = {0};
1228 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque29c1db52012-09-07 14:47:43 -05001229
1230
akmhoque902d57e2012-08-17 09:24:38 -05001231
akmhoque53f64222012-09-05 13:57:51 -05001232
akmhoque59980a52012-08-09 12:36:09 -05001233 nlsr->is_synch_init=1;
akmhoquec9286692012-08-16 09:57:58 -05001234 nlsr->nlsa_id=0;
akmhoqued79438d2012-08-27 13:31:42 -05001235 nlsr->adj_build_flag=0;
akmhoque53f64222012-09-05 13:57:51 -05001236 nlsr->adj_build_count=0;
1237 nlsr->is_build_adj_lsa_sheduled=0;
akmhoque29c1db52012-09-07 14:47:43 -05001238 nlsr->is_send_lsdb_interest_scheduled=0;
1239 nlsr->is_route_calculation_scheduled=0;
akmhoqued79438d2012-08-27 13:31:42 -05001240
akmhoque7b791452012-10-30 11:24:56 -05001241 nlsr->detailed_logging=0;
1242 nlsr->debugging=0;
1243
akmhoqueb77b95f2013-02-08 12:28:47 -06001244 //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
akmhoqued79438d2012-08-27 13:31:42 -05001245 nlsr->interest_retry = INTEREST_RETRY;
1246 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
akmhoqueda5b6832012-09-13 22:33:55 -05001247 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1248 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
akmhoqueb77b95f2013-02-08 12:28:47 -06001249 nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX;
akmhoqueffacaa82012-09-13 17:48:30 -05001250 nlsr->semaphor=NLSR_UNLOCKED;
akmhoque81c25e02012-09-10 14:50:33 -05001251
akmhoque95041802012-11-16 09:18:02 -06001252 nlsr->api_port=API_PORT;
1253
akmhoqueb77b95f2013-02-08 12:28:47 -06001254 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
1255 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
1256 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1257
1258 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
1259 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
1260 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1261
1262 nlsr->is_hyperbolic_calc=0;
1263 nlsr->cor_r=-1.0;
1264 nlsr->cor_theta=-1.0;
1265
1266 nlsr->tunnel_type=IPPROTO_UDP;
1267
akmhoque81c25e02012-09-10 14:50:33 -05001268 return 0;
akmhoque902d57e2012-08-17 09:24:38 -05001269}
1270
akmhoque03004e62012-09-06 01:12:28 -05001271
akmhoque902d57e2012-08-17 09:24:38 -05001272int
1273main(int argc, char *argv[])
1274{
akmhoque81c25e02012-09-10 14:50:33 -05001275 int res, ret;
akmhoque902d57e2012-08-17 09:24:38 -05001276 char *config_file;
akmhoquebfefef22012-09-26 10:09:34 -05001277 int daemon_mode=0;
akmhoque95041802012-11-16 09:18:02 -06001278 int port=0;
akmhoque902d57e2012-08-17 09:24:38 -05001279
akmhoquebfefef22012-09-26 10:09:34 -05001280
akmhoque81c25e02012-09-10 14:50:33 -05001281
akmhoque95041802012-11-16 09:18:02 -06001282 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
akmhoque59980a52012-08-09 12:36:09 -05001283 {
1284 switch (res)
1285 {
1286 case 'd':
akmhoquebfefef22012-09-26 10:09:34 -05001287 daemon_mode = 1;
akmhoque59980a52012-08-09 12:36:09 -05001288 break;
1289 case 'f':
1290 config_file = optarg;
1291 break;
akmhoque95041802012-11-16 09:18:02 -06001292 case 'p':
1293 port = atoi(optarg);
1294 break;
akmhoque59980a52012-08-09 12:36:09 -05001295 case 'h':
1296 default:
1297 usage(argv[0]);
1298 }
1299 }
1300
akmhoquebfefef22012-09-26 10:09:34 -05001301 ret=init_nlsr();
1302 ON_ERROR_EXIT(ret);
akmhoque95041802012-11-16 09:18:02 -06001303
1304 if ( port !=0 )
1305 nlsr->api_port=port;
1306
akmhoque59980a52012-08-09 12:36:09 -05001307 readConfigFile(config_file);
akmhoqueb77b95f2013-02-08 12:28:47 -06001308
1309 ON_ERROR_DESTROY(check_config_validity());
1310
1311 print_adjacent_from_adl();
1312
akmhoquebfefef22012-09-26 10:09:34 -05001313 if ( daemon_mode == 1 )
1314 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001315 nlsr->debugging=0;
akmhoquebfefef22012-09-26 10:09:34 -05001316 daemonize_nlsr();
1317 }
1318
1319 startLogging(nlsr->logDir);
1320
akmhoque59980a52012-08-09 12:36:09 -05001321 nlsr->ccn=ccn_create();
akmhoque1771c412012-11-09 13:06:08 -06001322 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1323 if(ccn_fd == -1)
akmhoque03004e62012-09-06 01:12:28 -05001324 {
1325 fprintf(stderr,"Could not connect to ccnd\n");
akmhoquebfefef22012-09-26 10:09:34 -05001326 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
akmhoque81c25e02012-09-10 14:50:33 -05001327 ON_ERROR_DESTROY(-1);
akmhoque03004e62012-09-06 01:12:28 -05001328 }
akmhoque1771c412012-11-09 13:06:08 -06001329
1330 init_api_server(ccn_fd);
akmhoqueb77b95f2013-02-08 12:28:47 -06001331
1332 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1333 if(res<0)
1334 {
1335 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1336 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1337 ON_ERROR_DESTROY(res);
1338 }
akmhoque53f64222012-09-05 13:57:51 -05001339 struct ccn_charbuf *router_prefix;
akmhoque03004e62012-09-06 01:12:28 -05001340 router_prefix=ccn_charbuf_create();
1341 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
akmhoque59980a52012-08-09 12:36:09 -05001342 if(res<0)
akmhoque03004e62012-09-06 01:12:28 -05001343 {
1344 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
akmhoquebfefef22012-09-26 10:09:34 -05001345 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
akmhoque81c25e02012-09-10 14:50:33 -05001346 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001347 }
akmhoque59980a52012-08-09 12:36:09 -05001348
1349 ccn_name_append_str(router_prefix,"nlsr");
akmhoque03004e62012-09-06 01:12:28 -05001350 nlsr->in_interest.data=nlsr->router_name;
akmhoque59980a52012-08-09 12:36:09 -05001351 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1352 if ( res < 0 )
akmhoque03004e62012-09-06 01:12:28 -05001353 {
1354 fprintf(stderr,"Failed to register interest for router\n");
akmhoque9e9fc722012-09-26 14:03:25 -05001355 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
akmhoque81c25e02012-09-10 14:50:33 -05001356 ON_ERROR_DESTROY(res);
akmhoque03004e62012-09-06 01:12:28 -05001357 }
1358 ccn_charbuf_destroy(&router_prefix);
1359
akmhoque7b791452012-10-30 11:24:56 -05001360 if ( nlsr->debugging )
1361 printf("Router Name : %s\n",nlsr->router_name);
akmhoque9e9fc722012-09-26 14:03:25 -05001362 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
akmhoque7b791452012-10-30 11:24:56 -05001363 if ( nlsr->debugging )
1364 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque9e9fc722012-09-26 14:03:25 -05001365 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
akmhoque59980a52012-08-09 12:36:09 -05001366
akmhoqueb77b95f2013-02-08 12:28:47 -06001367 add_faces_for_nbrs();
akmhoque53f64222012-09-05 13:57:51 -05001368 print_name_prefix_from_npl();
1369 print_adjacent_from_adl();
akmhoqueb77b95f2013-02-08 12:28:47 -06001370 build_and_install_name_lsas();
1371
akmhoque866c2222013-02-12 10:49:33 -06001372 res=sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1373 ON_ERROR_DESTROY(res);
akmhoqueb77b95f2013-02-08 12:28:47 -06001374
1375 print_name_lsdb();
1376 if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0)
1377 {
1378 build_and_install_cor_lsa();
1379 }
1380 write_name_lsdb_to_repo(nlsr->slice_prefix);
akmhoque53f64222012-09-05 13:57:51 -05001381
1382 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque03004e62012-09-06 01:12:28 -05001383 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
akmhoqueffacaa82012-09-13 17:48:30 -05001384 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque1c9b92f2012-08-13 10:57:50 -05001385
akmhoque1771c412012-11-09 13:06:08 -06001386
akmhoque59980a52012-08-09 12:36:09 -05001387 while(1)
akmhoque29c1db52012-09-07 14:47:43 -05001388 {
akmhoqueffacaa82012-09-13 17:48:30 -05001389 if ( nlsr->semaphor == NLSR_UNLOCKED )
akmhoque29c1db52012-09-07 14:47:43 -05001390 {
akmhoqueffacaa82012-09-13 17:48:30 -05001391 if( nlsr->sched != NULL )
1392 {
akmhoque1771c412012-11-09 13:06:08 -06001393 long int micro_sec=ccn_schedule_run(nlsr->sched);
1394 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1395 ON_ERROR_DESTROY(res);
akmhoqueffacaa82012-09-13 17:48:30 -05001396 }
1397 if(nlsr->ccn != NULL)
1398 {
akmhoqueb77b95f2013-02-08 12:28:47 -06001399 res = ccn_run(nlsr->ccn, 1);
akmhoqueffacaa82012-09-13 17:48:30 -05001400 }
1401 if (!(nlsr->sched && nlsr->ccn))
1402 {
1403 break;
1404 }
akmhoque29c1db52012-09-07 14:47:43 -05001405 }
1406
akmhoque59980a52012-08-09 12:36:09 -05001407 }
akmhoque1771c412012-11-09 13:06:08 -06001408
akmhoque59980a52012-08-09 12:36:09 -05001409
akmhoque59980a52012-08-09 12:36:09 -05001410 return 0;
1411}
1412