blob: 4c4bc3b1fc3e1296ba723dc2dc94d417447e656e [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include <sys/time.h>
7#include <sys/stat.h>
8#include <assert.h>
9#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>
15#include <netinet/in.h>
16#include <netdb.h>
17#include <arpa/inet.h>
18
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23#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>
29#include <ccn/sync.h>
30#include <ccn/seqwriter.h>
31
32#include "nlsr.h"
33#include "nlsr_ndn.h"
34#include "nlsr_lsdb.h"
35#include "utility.h"
36#include "nlsr_npl.h"
37#include "nlsr_adl.h"
38#include "nlsr_npt.h"
39#include "nlsr_route.h"
40#include "nlsr_sync.h"
akmhoquea37b52c2012-12-14 11:16:36 -060041#include "nlsr_face.h"
42#include "nlsr_fib.h"
akmhoque8fdd6412012-12-04 15:05:33 -060043
44
45#define ON_ERROR_DESTROY(resval) \
46{ \
47 if ((resval) < 0) { \
48 nlsr_destroy(); \
49 exit(1);\
50 } \
51}
52
53
54#define ON_ERROR_EXIT(resval) \
55{ \
56 if ((resval) < 0) { \
57 exit(1); \
58 } \
59}
60
61struct option longopts[] =
62{
63 { "daemon", no_argument, NULL, 'd'},
64 { "config_file", required_argument, NULL, 'f'},
65 { "api_port", required_argument, NULL, 'p'},
66 { "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\
78 -p, --api_port port where api client will connect\n\
79 -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
99void
100nlsr_lock(void)
101{
102 nlsr->semaphor=NLSR_LOCKED;
103}
104
105void
106nlsr_unlock(void)
107{
108 nlsr->semaphor=NLSR_UNLOCKED;
109}
110
111void
112nlsr_stop_signal_handler(int sig)
113{
114 signal(sig, SIG_IGN);
115 nlsr_destroy();
116 exit(0);
117}
118
119void
120daemonize_nlsr(void)
121{
122 int ret;
123 pid_t process_id = 0;
124 pid_t sid = 0;
125 process_id = fork();
126 if (process_id < 0)
127 {
128 printf("Daemonization failed!\n");
129 ON_ERROR_DESTROY(process_id);
130 }
131 if (process_id > 0)
132 {
133 printf("Process daemonized. Process id: %d \n", process_id);
134 ret=process_id;
135 exit(0);
136 }
137
138 umask(0);
139 sid = setsid();
140 if(sid < 0)
141 {
142 ON_ERROR_DESTROY(sid);
143 }
144
145 chdir("/");
146 close(STDIN_FILENO);
147 close(STDOUT_FILENO);
148 close(STDERR_FILENO);
149}
150
151void
152process_command_ccnneighbor(char *command)
153{
154 if(command==NULL)
155 {
156 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
157 return;
158 }
159 char *rem;
160 const char *sep=" \t\n";
akmhoquea37b52c2012-12-14 11:16:36 -0600161 char *rtr_name;
162 //char *face;
akmhoque8fdd6412012-12-04 15:05:33 -0600163
164 rtr_name=strtok_r(command,sep,&rem);
165 if(rtr_name==NULL)
166 {
167 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
168 return;
169 }
170
akmhoquea37b52c2012-12-14 11:16:36 -0600171 /*face=strtok_r(NULL,sep,&rem);
akmhoque8fdd6412012-12-04 15:05:33 -0600172 if(face==NULL)
173 {
174 printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n");
175 return;
176 }
177
178 printf("Router: %s face: %s\n",rtr_name,face);
179 int face_id;
180 int res;
181 res=sscanf(face,"face%d",&face_id);
182
183 if(res != 1 )
184 {
185 printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n");
186 return;
akmhoquea37b52c2012-12-14 11:16:36 -0600187 }*/
akmhoque8fdd6412012-12-04 15:05:33 -0600188
189 if ( rtr_name[strlen(rtr_name)-1] == '/' )
190 {
191 rtr_name[strlen(rtr_name)-1]='\0';
192 }
193 struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
194 nbr->name=(char *)malloc(strlen(rtr_name)+1);
195 memset(nbr->name,0,strlen(rtr_name)+1);
196 memcpy(nbr->name,rtr_name,strlen(rtr_name)+1);
197 nbr->length=strlen(rtr_name)+1;
198
akmhoque09c0afa2012-12-14 09:27:00 -0600199 //add_nbr_to_adl(nbr,face_id);
200
201 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
202 get_host_name_from_command_string(nbr_name,nbr->name,0);
203 printf("Hostname of neighbor: %s ",nbr_name->name);
204
akmhoque09c0afa2012-12-14 09:27:00 -0600205
akmhoquea37b52c2012-12-14 11:16:36 -0600206 char *ip_addr=(char *)malloc(13);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600207 memset(ip_addr,0,13);
akmhoquea37b52c2012-12-14 11:16:36 -0600208 get_ip_from_hostname_02(nbr_name->name,ip_addr);
209 printf("IP Address: %s \n",ip_addr);
210
211 add_nbr_to_adl(nbr,0,ip_addr);
akmhoque09c0afa2012-12-14 09:27:00 -0600212
akmhoque8fdd6412012-12-04 15:05:33 -0600213
214 free(nbr->name);
215 free(nbr);
216}
217
218void
219process_command_ccnname(char *command)
220{
221
222 if(command==NULL)
223 {
224 printf(" Wrong Command Format ( ccnname /name/prefix)\n");
225 return;
226 }
227 char *rem;
228 const char *sep=" \t\n";
229 char *name;
230 name=strtok_r(command,sep,&rem);
231 if(name==NULL)
232 {
233 printf(" Wrong Command Format ( ccnname /name/prefix/ )\n");
234 return;
235 }
236
237 printf("Name Prefix: %s \n",name);
238
239 if ( name[strlen(name)-1] == '/' )
240 name[strlen(name)-1]='\0';
241
242 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
243 np->name=(char *)malloc(strlen(name)+1);
244 memset(np->name,0,strlen(name)+1);
245 memcpy(np->name,name,strlen(name)+1);
246 np->length=strlen(name)+1;
247
248 add_name_to_npl(np);
249
250 free(np->name);
251 free(np);
252}
253
254
255void
256process_command_router_name(char *command)
257{
258 if(command==NULL)
259 {
260 printf(" Wrong Command Format ( router-name /router/name )\n");
261 return;
262 }
263 char *rem;
264 const char *sep=" \t\n";
265 char *rtr_name;
266
267 rtr_name=strtok_r(command,sep,&rem);
268 if(rtr_name==NULL)
269 {
270 printf(" Wrong Command Format ( router-name /router/name )\n");
271 return;
272 }
273
274
275 if ( rtr_name[strlen(rtr_name)-1] == '/' )
276 rtr_name[strlen(rtr_name)-1]='\0';
277
278 nlsr->router_name=(char *)malloc(strlen(rtr_name)+1);
279 memset(nlsr->router_name,0,strlen(rtr_name)+1);
280 memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1);
281
282
283}
284
285void
286process_command_lsdb_synch_interval(char *command)
287{
288 if(command==NULL)
289 {
290 printf(" Wrong Command Format ( lsdb-synch-interval secs )\n");
291 return;
292 }
293 char *rem;
294 const char *sep=" \t\n";
295 char *secs;
296 long int seconds;
297
298 secs=strtok_r(command,sep,&rem);
299 if(secs==NULL)
300 {
301 printf(" Wrong Command Format ( lsdb-synch-interval secs)\n");
302 return;
303 }
304
305 seconds=atoi(secs);
306 if ( seconds >= 120 && seconds <= 3600 )
307 {
308 nlsr->lsdb_synch_interval=seconds;
309 }
310
311}
312
313
314void
315process_command_interest_retry(char *command)
316{
317 if(command==NULL)
318 {
319 printf(" Wrong Command Format ( interest-retry number )\n");
320 return;
321 }
322 char *rem;
323 const char *sep=" \t\n";
324 char *retry;
325 long int retry_number;
326
327 retry=strtok_r(command,sep,&rem);
328 if(retry==NULL)
329 {
330 printf(" Wrong Command Format ( interest-retry number)\n");
331 return;
332 }
333
334 retry_number=atoi(retry);
335 if ( retry_number >= 1 && retry_number<=10 )
336 {
337 nlsr->interest_retry=retry_number;
338 }
339
340}
341
342void
343process_command_interest_resend_time(char *command)
344{
345 if(command==NULL)
346 {
347 printf(" Wrong Command Format ( interest-resend-time secs )\n");
348 return;
349 }
350 char *rem;
351 const char *sep=" \t\n";
352 char *secs;
353 long int seconds;
354
355 secs=strtok_r(command,sep,&rem);
356 if(secs==NULL)
357 {
358 printf(" Wrong Command Format ( interest-resend-time secs)\n");
359 return;
360 }
361
362 seconds=atoi(secs);
363 if ( seconds <= 60 && seconds >= 1 )
364 {
365 nlsr->interest_resend_time=seconds;
366 }
367}
368
369
370void
371process_command_lsa_refresh_time(char *command)
372{
373 if(command==NULL)
374 {
375 printf(" Wrong Command Format ( lsa-refresh-time secs )\n");
376 return;
377 }
378 char *rem;
379 const char *sep=" \t\n";
380 char *secs;
381 long int seconds;
382
383 secs=strtok_r(command,sep,&rem);
384 if(secs==NULL)
385 {
386 printf(" Wrong Command Format ( lsa-refresh-time secs)\n");
387 return;
388 }
389
390 seconds=atoi(secs);
391 if ( seconds >= 240 && seconds <= 3600 )
392 {
393 nlsr->lsa_refresh_time=seconds;
394 }
395
396}
397
398void
399process_command_router_dead_interval(char *command)
400{
401 if(command==NULL)
402 {
403 printf(" Wrong Command Format ( router-dead-interval secs )\n");
404 return;
405 }
406 char *rem;
407 const char *sep=" \t\n";
408 char *secs;
409 long int seconds;
410
411 secs=strtok_r(command,sep,&rem);
412 if(secs==NULL)
413 {
414 printf(" Wrong Command Format ( router-dead-interval secs)\n");
415 return;
416 }
417
418 seconds=atoi(secs);
419 if ( seconds >= 360 && seconds <= 5400 )
420 {
421 nlsr->router_dead_interval=seconds;
422 }
423
424}
425
426void
427process_command_multi_path_face_num(char *command)
428{
429 if(command==NULL)
430 {
431 printf(" Wrong Command Format ( multi-path-face-num n )\n");
432 return;
433 }
434 char *rem;
435 const char *sep=" \t\n";
436 char *num;
437 long int number;
438
439 num=strtok_r(command,sep,&rem);
440 if(num==NULL)
441 {
442 printf(" Wrong Command Format ( multi-path-face-num n)\n");
443 return;
444 }
445
446 number=atoi(num);
447 if ( number >= 0 && number <= 60 )
448 {
449 nlsr->multi_path_face_num=number;
450 }
451
452}
453
454void
455process_command_logdir(char *command)
456{
457 if(command==NULL)
458 {
459 printf(" Wrong Command Format ( logdir /path/to/logdir )\n");
460 return;
461 }
462 char *rem;
463 const char *sep=" \t\n";
464 char *dir;
465
466 dir=strtok_r(command,sep,&rem);
467 if(dir==NULL)
468 {
469 printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n");
470 return;
471 }
472
473 nlsr->logDir=(char *)malloc(strlen(dir)+1);
474 memset(nlsr->logDir,0,strlen(dir)+1);
475 memcpy(nlsr->logDir,dir,strlen(dir));
476}
477
478void
479process_command_detailed_log(char *command)
480{
481 if(command==NULL)
482 {
483 printf(" Wrong Command Format ( detailed-log on/off )\n");
484 return;
485 }
486 char *rem;
487 const char *sep=" \t\n";
488 char *on_off;
489
490 on_off=strtok_r(command,sep,&rem);
491 if(on_off==NULL)
492 {
493 printf(" Wrong Command Format ( detailed-log on/off )\n");
494 return;
495 }
496
497 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0)
498 {
499 nlsr->detailed_logging=1;
500 }
501}
502
503void
504process_command_debug(char *command)
505{
506 if(command==NULL)
507 {
508 printf(" Wrong Command Format ( debug on/off )\n");
509 return;
510 }
511 char *rem;
512 const char *sep=" \t\n";
513 char *on_off;
514
515 on_off=strtok_r(command,sep,&rem);
516 if(on_off==NULL)
517 {
518 printf(" Wrong Command Format ( debug on/off )\n");
519 return;
520 }
521
522 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
523 {
524 nlsr->debugging=1;
525 }
526}
527
akmhoque09c0afa2012-12-14 09:27:00 -0600528
529void
530process_command_topo_prefix(char *command)
531{
532 if(command==NULL)
533 {
534 printf(" Wrong Command Format ( topo-prefix )\n");
535 return;
536 }
537 char *rem;
538 const char *sep=" \t\n";
539 char *topo_prefix;
540
541 topo_prefix=strtok_r(command,sep,&rem);
542 if(topo_prefix==NULL)
543 {
544 printf(" Wrong Command Format ( topo-prefix /name/prefix )\n");
545 return;
546 }
547 else
548 {
549 if( nlsr->topo_prefix != NULL)
550 free(nlsr->topo_prefix);
akmhoqueb29edd82013-01-14 20:54:11 -0600551 if ( topo_prefix[strlen(topo_prefix)-1] == '/' )
552 topo_prefix[strlen(topo_prefix)-1]='\0';
553
akmhoque09c0afa2012-12-14 09:27:00 -0600554 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
Syed Obaid Amin7a176c62012-12-21 15:56:00 -0600555 //nlsr->topo_prefix=(char *)calloc(strlen(topo_prefix)+1, sizeof(char));
556 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
557 puts(topo_prefix);
akmhoque09c0afa2012-12-14 09:27:00 -0600558 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
559
560 //printf(" Topo Prefix: %s \n",nlsr->topo_prefix);
561
562 }
563}
564
565
566void
567process_command_slice_prefix(char *command)
568{
569 if(command==NULL)
570 {
571 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
572 return;
573 }
574 char *rem;
575 const char *sep=" \t\n";
576 char *slice_prefix;
577
578 slice_prefix=strtok_r(command,sep,&rem);
579 if(slice_prefix==NULL)
580 {
581 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
582 return;
583 }
584 else
585 {
586 if ( nlsr->slice_prefix != NULL)
587 free(nlsr->slice_prefix);
akmhoqueb29edd82013-01-14 20:54:11 -0600588 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
589 slice_prefix[strlen(slice_prefix)-1]='\0';
590
akmhoque09c0afa2012-12-14 09:27:00 -0600591 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600592 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoque09c0afa2012-12-14 09:27:00 -0600593 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
594
595 //printf(" Slice Prefix: %s \n",nlsr->slice_prefix);
596 }
597}
598
599void
600process_command_hyperbolic(char *command)
601{
602 if(command==NULL)
603 {
604 printf(" Wrong Command Format ( hyperbolic on/off )\n");
605 return;
606 }
607 char *rem;
608 const char *sep=" \t\n";
609 char *on_off;
610
611 on_off=strtok_r(command,sep,&rem);
612 if(on_off==NULL)
613 {
614 printf(" Wrong Command Format ( hyperbolic on/off )\n");
615 return;
616 }
617
618 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
619 {
620 nlsr->is_hyperbolic_calc=1;
621 }
622}
623
akmhoque8fdd6412012-12-04 15:05:33 -0600624void
625process_conf_command(char *command)
626{
627 const char *separators=" \t\n";
628 char *remainder=NULL;
629 char *cmd_type=NULL;
630
631 if(command==NULL || strlen(command)==0 || command[0]=='!')
632 return;
633
634 cmd_type=strtok_r(command,separators,&remainder);
635
636 if(!strcmp(cmd_type,"router-name") )
637 {
638 process_command_router_name(remainder);
639 }
640 else if(!strcmp(cmd_type,"ccnneighbor") )
641 {
642 process_command_ccnneighbor(remainder);
643 }
644 else if(!strcmp(cmd_type,"ccnname") )
645 {
646 process_command_ccnname(remainder);
647 }
648 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
649 {
650 process_command_lsdb_synch_interval(remainder);
651 }
652 else if(!strcmp(cmd_type,"interest-retry") )
653 {
654 process_command_interest_retry(remainder);
655 }
656 else if(!strcmp(cmd_type,"interest-resend-time") )
657 {
658 process_command_interest_resend_time(remainder);
659 }
660 else if(!strcmp(cmd_type,"lsa-refresh-time") )
661 {
662 process_command_lsa_refresh_time(remainder);
663 }
664 else if(!strcmp(cmd_type,"router-dead-interval") )
665 {
666 process_command_router_dead_interval(remainder);
667 }
668 else if(!strcmp(cmd_type,"multi-path-face-num") )
669 {
670 process_command_multi_path_face_num(remainder);
671 }
672 else if(!strcmp(cmd_type,"logdir") )
673 {
674 process_command_logdir(remainder);
675 }
676 else if(!strcmp(cmd_type,"detailed-log") )
677 {
678 process_command_detailed_log(remainder);
679 }
680 else if(!strcmp(cmd_type,"debug") )
681 {
682 process_command_debug(remainder);
683 }
akmhoque09c0afa2012-12-14 09:27:00 -0600684 else if(!strcmp(cmd_type,"topo-prefix") )
685 {
686 process_command_topo_prefix(remainder);
687 }
688 else if(!strcmp(cmd_type,"slice-prefix") )
689 {
690 process_command_slice_prefix(remainder);
691 }
akmhoque8fdd6412012-12-04 15:05:33 -0600692 else
693 {
694 printf("Wrong configuration Command %s \n",cmd_type);
695 }
696}
697
698
699int
700readConfigFile(const char *filename)
701{
702 FILE *cfg;
703 char buf[1024];
704 int len;
705
706 cfg=fopen(filename, "r");
707
708 if(cfg == NULL)
709 {
710 printf("\nConfiguration File does not exists\n");
711 exit(1);
712 }
713
714 while(fgets((char *)buf, sizeof(buf), cfg))
715 {
716 len=strlen(buf);
717 if(buf[len-1] == '\n')
718 buf[len-1]='\0';
719 if ( buf[0] != '#' && buf[0] != '!')
720 process_conf_command(buf);
721 }
722
723 fclose(cfg);
724
725 return 0;
726}
727
akmhoquea37b52c2012-12-14 11:16:36 -0600728
729void
730add_faces_for_nbrs(void)
731{
732 int i, adl_element;
733 struct ndn_neighbor *nbr;
734
735 struct hashtb_enumerator ee;
736 struct hashtb_enumerator *e = &ee;
737
738 hashtb_start(nlsr->adl, e);
739 adl_element=hashtb_n(nlsr->adl);
740
741 for(i=0;i<adl_element;i++)
742 {
743 nbr=e->data;
744 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695);
745 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
746 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
747 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
748 hashtb_next(e);
749 }
750
751 hashtb_end(e);
752
753}
754
akmhoque61a57f22013-01-18 07:44:05 -0600755void
756destroy_faces_for_nbrs(void)
757{
758 int i, adl_element;
759 struct ndn_neighbor *nbr;
760
761 struct hashtb_enumerator ee;
762 struct hashtb_enumerator *e = &ee;
763
764 hashtb_start(nlsr->adl, e);
765 adl_element=hashtb_n(nlsr->adl);
766
767 for(i=0;i<adl_element;i++)
768 {
769 nbr=e->data;
770 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
771 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
772 hashtb_next(e);
773 }
774
775 hashtb_end(e);
776
777}
778
akmhoque8fdd6412012-12-04 15:05:33 -0600779char *
780process_api_client_command(char *command)
781{
782 char *msg;
783 msg=(char *)malloc(100);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600784 memset(msg,0,100);
akmhoque8fdd6412012-12-04 15:05:33 -0600785
786 const char *sep=" \t\n";
787 char *rem=NULL;
788 char *cmd_type=NULL;
789 char *op_type=NULL;
790 char *name=NULL;
791 char *face=NULL;
792 int face_id;
793 int res;
794
795 op_type=strtok_r(command,sep,&rem);
796 cmd_type=strtok_r(NULL,sep,&rem);
797 name=strtok_r(NULL,sep,&rem);
798 if ( name[strlen(name)-1] == '/' )
799 name[strlen(name)-1]='\0';
800
801 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
802 np->name=(char *)malloc(strlen(name)+1);
803 memset(np->name,0,strlen(name)+1);
804 memcpy(np->name,name,strlen(name)+1);
805 np->length=strlen(name)+1;
806
807 if ( strcmp(cmd_type,"name")!= 0 )
808 {
809 face=strtok_r(NULL,sep,&rem);
810 sscanf(face,"face%d",&face_id);
811 }
812
813 if ( strcmp(cmd_type,"name")== 0 )
814 {
815 if ( strcmp(op_type,"del") == 0 )
816 {
817 res=does_name_exist_in_npl(np);
818 if ( res == 0)
819 {
820 sprintf(msg,"Name %s does not exist !!",name);
821 }
822 else
823 {
824 long int ls_id=get_lsa_id_from_npl(np);
825 if ( ls_id != 0 )
826 {
827 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
828 sprintf(msg,"Name %s has been deleted and Advertised.",name);
829 }
830 else
831 {
832 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
833 }
834 }
835 }
836 else if ( strcmp(op_type,"add") == 0 )
837 {
838 res=does_name_exist_in_npl(np);
839 if ( res == 0)
840 {
841 add_name_to_npl(np);
842 build_and_install_single_name_lsa(np);
843 sprintf(msg,"Name %s has been added to advertise.",name);
844 }
845 else
846 {
847 sprintf(msg,"Name %s has already been advertised from this router !!",name);
848 }
849 }
850 }
851 else if ( strcmp(cmd_type,"neighbor") == 0 )
852 {
853 if ( strcmp(op_type,"del") == 0 )
854 {
855 res=is_neighbor(np->name);
856 if ( res == 0)
857 {
858 sprintf(msg,"Neighbor %s does not exist !!",name);
859 }
860 else
861 {
862 update_adjacent_status_to_adl(np,NBR_DOWN);
863 delete_nbr_from_adl(np);
864 if(!nlsr->is_build_adj_lsa_sheduled)
865 {
866 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
867 nlsr->is_build_adj_lsa_sheduled=1;
868 }
869 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
870 }
871 }
872 else if ( strcmp(op_type,"add") == 0 )
873 {
874 res=is_neighbor(np->name);
875 if ( res == 0 )
876 {
akmhoque09c0afa2012-12-14 09:27:00 -0600877 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
878 get_host_name_from_command_string(nbr_name,np->name,0);
879 printf("Hostname of neighbor: %s ",nbr_name->name);
880
akmhoquea37b52c2012-12-14 11:16:36 -0600881 char *ip_addr=(char *)malloc(13);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600882 memset(ip_addr,0,13);
akmhoquea37b52c2012-12-14 11:16:36 -0600883 get_ip_from_hostname_02(nbr_name->name,ip_addr);
884 printf("IP Address: %s \n",ip_addr);
885 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695);
886 update_face_to_adl_for_nbr(nbr_name->name, face_id);
887 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
888
889 add_nbr_to_adl(np,face_id,ip_addr);
890
akmhoque8fdd6412012-12-04 15:05:33 -0600891 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoquea37b52c2012-12-14 11:16:36 -0600892
akmhoque8fdd6412012-12-04 15:05:33 -0600893 }
894 else
895 {
896 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
897 }
898 }
899 }
900
901
902 return msg;
903}
904
905int
906nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
907{
908 struct timeval timeout;
909 if ( time_out_micro_sec < 0 )
910 {
911 timeout.tv_sec=1;
912 timeout.tv_usec=0;
913 }
914 else
915 {
916 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
917 timeout.tv_sec=time_out_micro_sec / 1000000;
918 timeout.tv_usec=time_out_micro_sec % 1000000;
919 }
920
Adam Alyyanf1f188f2013-01-14 11:57:08 -0600921 timeout.tv_sec = 0;
922 timeout.tv_usec = 499000;
akmhoque8fdd6412012-12-04 15:05:33 -0600923
924 int fd;
925 int nread;
926 int result;
927 fd_set testfds;
928 unsigned int client_len;
929 int client_sockfd;
930 char recv_buffer[1024];
931 bzero(recv_buffer,1024);
932 struct sockaddr_in client_address;
933
934 testfds=nlsr->readfds;
935 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
936
soamin29b8e392013-01-22 17:12:07 -0600937 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque8fdd6412012-12-04 15:05:33 -0600938 {
939 if(FD_ISSET(fd,&testfds))
940 {
941 if ( fd == ccn_fd )
942 {
943 return 0;
944 }
945 else if(fd == nlsr->nlsr_api_server_sock_fd)
946 {
947 client_len = sizeof(client_address);
948 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
949 FD_SET(client_sockfd, &nlsr->readfds);
950 }
951 else
952 {
953
954 ioctl(fd, FIONREAD, &nread);
955 if(nread == 0)
956 {
957 close(fd);
958 FD_CLR(fd, &nlsr->readfds);
959 }
960 else
961 {
962 recv(fd, recv_buffer, 1024, 0);
963 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
964 char *msg=process_api_client_command(recv_buffer);
965 send(fd, msg, strlen(msg),0);
966 free(msg);
967 close(fd);
968 FD_CLR(fd, &nlsr->readfds);
969 }
970 }
971 }
972 }
973
974 return 0;
975}
976
977void
978nlsr_destroy( void )
979{
980 if ( nlsr->debugging )
981 {
982 printf("Freeing Allocated Memory....\n");
983 }
984 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
985 /* Destroying all face created by nlsr in CCND */
986 destroy_all_face_by_nlsr();
akmhoque61a57f22013-01-18 07:44:05 -0600987 destroy_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -0600988 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
989 hashtb_destroy(&nlsr->npl);
990 hashtb_destroy(&nlsr->adl);
991 hashtb_destroy(&nlsr->lsdb->name_lsdb);
992 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
993 hashtb_destroy(&nlsr->pit_alsa);
994
995 //To Do: has to destroy the face_list one by one
996
997 hashtb_destroy(&nlsr->routing_table);
998
999
1000 int i, npt_element;
1001 struct npt_entry *ne;
1002 struct hashtb_enumerator ee;
1003 struct hashtb_enumerator *e = &ee;
1004 hashtb_start(nlsr->npt, e);
1005 npt_element=hashtb_n(nlsr->npt);
1006 for(i=0;i<npt_element;i++)
1007 {
1008 ne=e->data;
1009 hashtb_destroy(&ne->name_list);
1010 hashtb_destroy(&ne->face_list);
1011 hashtb_next(e);
1012 }
1013
1014 hashtb_end(e);
1015 hashtb_destroy(&nlsr->npt);
1016
1017
1018 ccns_close(&nlsr->ccns, NULL, NULL);
1019 ccns_slice_destroy(&nlsr->slice);
1020
1021
1022
1023 close(nlsr->nlsr_api_server_sock_fd);
1024
1025 ccn_schedule_destroy(&nlsr->sched);
1026 ccn_destroy(&nlsr->ccn);
1027
1028 free(nlsr->lsdb->lsdb_version);
1029 free(nlsr->lsdb);
1030 free(nlsr->router_name);
1031 free(nlsr);
1032 if ( nlsr->debugging )
1033 {
1034 printf("Finished freeing allocated memory\n");
1035 }
1036 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1037
1038}
1039
1040
1041
1042void
1043init_api_server(int ccn_fd)
1044{
1045 int server_sockfd;
1046 int server_len;
1047 struct sockaddr_in server_address;
1048 unsigned int yes=1;
1049
1050 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1051
1052 int flags = fcntl(server_sockfd, F_GETFL, 0);
1053 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1054
1055 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1056 {
1057 ON_ERROR_DESTROY(-1);
1058 }
1059
1060 server_address.sin_family = AF_INET;
Adam Alyyanc350e3a2013-01-14 14:35:57 -06001061 //server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
1062 server_address.sin_addr.s_addr = INADDR_ANY;
1063 server_address.sin_port = htons(nlsr->api_port);
akmhoque8fdd6412012-12-04 15:05:33 -06001064
1065 server_len = sizeof(server_address);
1066 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1067 listen(server_sockfd, 100);
1068 FD_ZERO(&nlsr->readfds);
1069 FD_SET(server_sockfd, &nlsr->readfds);
1070 FD_SET(ccn_fd, &nlsr->readfds);
1071 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1072
1073}
1074
1075int
1076init_nlsr(void)
1077{
1078 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1079 {
1080 perror("SIGQUIT install error\n");
1081 return -1;
1082 }
1083 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1084 {
1085 perror("SIGTERM install error\n");
1086 return -1;
1087 }
1088 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1089 {
1090 perror("SIGTERM install error\n");
1091 return -1;
1092 }
1093
1094 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1095
1096 struct hashtb_param param_adl = {0};
1097 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1098 struct hashtb_param param_npl = {0};
1099 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1100 struct hashtb_param param_pit_alsa = {0};
1101 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1102 struct hashtb_param param_npt = {0};
1103 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1104 struct hashtb_param param_rte = {0};
1105 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1106
1107 nlsr->in_interest.p = &incoming_interest;
1108 nlsr->in_content.p = &incoming_content;
1109
1110 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1111
1112 char *time_stamp=(char *)malloc(20);
1113 memset(time_stamp,0,20);
1114 get_current_timestamp_micro(time_stamp);
1115 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
1116 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
1117 free(time_stamp);
1118
1119 struct hashtb_param param_adj_lsdb = {0};
1120 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1121 struct hashtb_param param_name_lsdb = {0};
1122 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
1123
1124
1125
1126
1127 nlsr->is_synch_init=1;
1128 nlsr->nlsa_id=0;
1129 nlsr->adj_build_flag=0;
1130 nlsr->adj_build_count=0;
1131 nlsr->is_build_adj_lsa_sheduled=0;
1132 nlsr->is_send_lsdb_interest_scheduled=0;
1133 nlsr->is_route_calculation_scheduled=0;
1134
1135 nlsr->detailed_logging=0;
1136 nlsr->debugging=0;
1137
1138 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
1139 nlsr->interest_retry = INTEREST_RETRY;
1140 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1141 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1142 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
1143 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
1144 nlsr->semaphor=NLSR_UNLOCKED;
1145
1146 nlsr->api_port=API_PORT;
1147
akmhoque8fdd6412012-12-04 15:05:33 -06001148 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001149 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001150 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1151
1152 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001153 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001154 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1155
akmhoque09c0afa2012-12-14 09:27:00 -06001156 nlsr->is_hyperbolic_calc=0;
1157
akmhoque8fdd6412012-12-04 15:05:33 -06001158 return 0;
1159}
1160
1161
1162int
1163main(int argc, char *argv[])
1164{
1165 int res, ret;
1166 char *config_file;
1167 int daemon_mode=0;
1168 int port=0;
1169
1170
1171
1172 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1173 {
1174 switch (res)
1175 {
1176 case 'd':
1177 daemon_mode = 1;
1178 break;
1179 case 'f':
1180 config_file = optarg;
1181 break;
1182 case 'p':
1183 port = atoi(optarg);
1184 break;
1185 case 'h':
1186 default:
1187 usage(argv[0]);
1188 }
1189 }
1190
1191 ret=init_nlsr();
1192 ON_ERROR_EXIT(ret);
1193
1194 if ( port !=0 )
1195 nlsr->api_port=port;
1196
1197 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001198
akmhoquea37b52c2012-12-14 11:16:36 -06001199 print_adjacent_from_adl();
1200
akmhoque8fdd6412012-12-04 15:05:33 -06001201 if ( daemon_mode == 1 )
1202 {
1203 daemonize_nlsr();
1204 }
1205
1206 startLogging(nlsr->logDir);
1207
1208 nlsr->ccn=ccn_create();
1209 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1210 if(ccn_fd == -1)
1211 {
1212 fprintf(stderr,"Could not connect to ccnd\n");
1213 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1214 ON_ERROR_DESTROY(-1);
1215 }
1216
1217 init_api_server(ccn_fd);
1218
akmhoquefc5176d2013-01-18 09:50:12 -06001219 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1220 if(res<0)
1221 {
1222 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1223 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1224 ON_ERROR_DESTROY(res);
1225 }
akmhoque8fdd6412012-12-04 15:05:33 -06001226 struct ccn_charbuf *router_prefix;
1227 router_prefix=ccn_charbuf_create();
1228 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1229 if(res<0)
1230 {
1231 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1232 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1233 ON_ERROR_DESTROY(res);
1234 }
1235
1236 ccn_name_append_str(router_prefix,"nlsr");
1237 nlsr->in_interest.data=nlsr->router_name;
1238 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1239 if ( res < 0 )
1240 {
1241 fprintf(stderr,"Failed to register interest for router\n");
1242 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1243 ON_ERROR_DESTROY(res);
1244 }
1245 ccn_charbuf_destroy(&router_prefix);
1246
1247 if ( nlsr->debugging )
1248 printf("Router Name : %s\n",nlsr->router_name);
1249 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1250 if ( nlsr->debugging )
1251 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1252 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1253
akmhoquea37b52c2012-12-14 11:16:36 -06001254 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001255 print_name_prefix_from_npl();
1256 print_adjacent_from_adl();
1257 build_and_install_name_lsas();
1258 print_name_lsdb();
1259
1260 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1261
1262 write_name_lsdb_to_repo(nlsr->slice_prefix);
1263
akmhoque8fdd6412012-12-04 15:05:33 -06001264 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001265 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1266 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001267
1268
1269 while(1)
1270 {
1271 if ( nlsr->semaphor == NLSR_UNLOCKED )
1272 {
1273 if( nlsr->sched != NULL )
1274 {
1275 long int micro_sec=ccn_schedule_run(nlsr->sched);
1276 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1277 ON_ERROR_DESTROY(res);
1278 }
1279 if(nlsr->ccn != NULL)
1280 {
1281 res = ccn_run(nlsr->ccn, 0);
1282 }
1283 if (!(nlsr->sched && nlsr->ccn))
1284 {
1285 break;
1286 }
1287 }
1288
1289 }
1290
1291
1292 return 0;
1293}
1294