blob: 9e63703387345315e19a2df876bbc06f5cc42efc [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);
207 memset(ip_addr,13,0);
208 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);
551 nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1);
Syed Obaid Amin7a176c62012-12-21 15:56:00 -0600552 //nlsr->topo_prefix=(char *)calloc(strlen(topo_prefix)+1, sizeof(char));
553 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
554 puts(topo_prefix);
akmhoque09c0afa2012-12-14 09:27:00 -0600555 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
556
557 //printf(" Topo Prefix: %s \n",nlsr->topo_prefix);
558
559 }
560}
561
562
563void
564process_command_slice_prefix(char *command)
565{
566 if(command==NULL)
567 {
568 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
569 return;
570 }
571 char *rem;
572 const char *sep=" \t\n";
573 char *slice_prefix;
574
575 slice_prefix=strtok_r(command,sep,&rem);
576 if(slice_prefix==NULL)
577 {
578 printf(" Wrong Command Format ( slice-prefix /name/prefix )\n");
579 return;
580 }
581 else
582 {
583 if ( nlsr->slice_prefix != NULL)
584 free(nlsr->slice_prefix);
585 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
586 memset(nlsr->slice_prefix,strlen(slice_prefix)+1,0);
587 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
588
589 //printf(" Slice Prefix: %s \n",nlsr->slice_prefix);
590 }
591}
592
593void
594process_command_hyperbolic(char *command)
595{
596 if(command==NULL)
597 {
598 printf(" Wrong Command Format ( hyperbolic on/off )\n");
599 return;
600 }
601 char *rem;
602 const char *sep=" \t\n";
603 char *on_off;
604
605 on_off=strtok_r(command,sep,&rem);
606 if(on_off==NULL)
607 {
608 printf(" Wrong Command Format ( hyperbolic on/off )\n");
609 return;
610 }
611
612 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
613 {
614 nlsr->is_hyperbolic_calc=1;
615 }
616}
617
akmhoque8fdd6412012-12-04 15:05:33 -0600618void
619process_conf_command(char *command)
620{
621 const char *separators=" \t\n";
622 char *remainder=NULL;
623 char *cmd_type=NULL;
624
625 if(command==NULL || strlen(command)==0 || command[0]=='!')
626 return;
627
628 cmd_type=strtok_r(command,separators,&remainder);
629
630 if(!strcmp(cmd_type,"router-name") )
631 {
632 process_command_router_name(remainder);
633 }
634 else if(!strcmp(cmd_type,"ccnneighbor") )
635 {
636 process_command_ccnneighbor(remainder);
637 }
638 else if(!strcmp(cmd_type,"ccnname") )
639 {
640 process_command_ccnname(remainder);
641 }
642 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
643 {
644 process_command_lsdb_synch_interval(remainder);
645 }
646 else if(!strcmp(cmd_type,"interest-retry") )
647 {
648 process_command_interest_retry(remainder);
649 }
650 else if(!strcmp(cmd_type,"interest-resend-time") )
651 {
652 process_command_interest_resend_time(remainder);
653 }
654 else if(!strcmp(cmd_type,"lsa-refresh-time") )
655 {
656 process_command_lsa_refresh_time(remainder);
657 }
658 else if(!strcmp(cmd_type,"router-dead-interval") )
659 {
660 process_command_router_dead_interval(remainder);
661 }
662 else if(!strcmp(cmd_type,"multi-path-face-num") )
663 {
664 process_command_multi_path_face_num(remainder);
665 }
666 else if(!strcmp(cmd_type,"logdir") )
667 {
668 process_command_logdir(remainder);
669 }
670 else if(!strcmp(cmd_type,"detailed-log") )
671 {
672 process_command_detailed_log(remainder);
673 }
674 else if(!strcmp(cmd_type,"debug") )
675 {
676 process_command_debug(remainder);
677 }
akmhoque09c0afa2012-12-14 09:27:00 -0600678 else if(!strcmp(cmd_type,"topo-prefix") )
679 {
680 process_command_topo_prefix(remainder);
681 }
682 else if(!strcmp(cmd_type,"slice-prefix") )
683 {
684 process_command_slice_prefix(remainder);
685 }
akmhoque8fdd6412012-12-04 15:05:33 -0600686 else
687 {
688 printf("Wrong configuration Command %s \n",cmd_type);
689 }
690}
691
692
693int
694readConfigFile(const char *filename)
695{
696 FILE *cfg;
697 char buf[1024];
698 int len;
699
700 cfg=fopen(filename, "r");
701
702 if(cfg == NULL)
703 {
704 printf("\nConfiguration File does not exists\n");
705 exit(1);
706 }
707
708 while(fgets((char *)buf, sizeof(buf), cfg))
709 {
710 len=strlen(buf);
711 if(buf[len-1] == '\n')
712 buf[len-1]='\0';
713 if ( buf[0] != '#' && buf[0] != '!')
714 process_conf_command(buf);
715 }
716
717 fclose(cfg);
718
719 return 0;
720}
721
akmhoquea37b52c2012-12-14 11:16:36 -0600722
723void
724add_faces_for_nbrs(void)
725{
726 int i, adl_element;
727 struct ndn_neighbor *nbr;
728
729 struct hashtb_enumerator ee;
730 struct hashtb_enumerator *e = &ee;
731
732 hashtb_start(nlsr->adl, e);
733 adl_element=hashtb_n(nlsr->adl);
734
735 for(i=0;i<adl_element;i++)
736 {
737 nbr=e->data;
738 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695);
739 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
740 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
741 //add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id);
742 hashtb_next(e);
743 }
744
745 hashtb_end(e);
746
747}
748
akmhoque8fdd6412012-12-04 15:05:33 -0600749char *
750process_api_client_command(char *command)
751{
752 char *msg;
753 msg=(char *)malloc(100);
754 memset(msg,100,0);
755
756 const char *sep=" \t\n";
757 char *rem=NULL;
758 char *cmd_type=NULL;
759 char *op_type=NULL;
760 char *name=NULL;
761 char *face=NULL;
762 int face_id;
763 int res;
764
765 op_type=strtok_r(command,sep,&rem);
766 cmd_type=strtok_r(NULL,sep,&rem);
767 name=strtok_r(NULL,sep,&rem);
768 if ( name[strlen(name)-1] == '/' )
769 name[strlen(name)-1]='\0';
770
771 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
772 np->name=(char *)malloc(strlen(name)+1);
773 memset(np->name,0,strlen(name)+1);
774 memcpy(np->name,name,strlen(name)+1);
775 np->length=strlen(name)+1;
776
777 if ( strcmp(cmd_type,"name")!= 0 )
778 {
779 face=strtok_r(NULL,sep,&rem);
780 sscanf(face,"face%d",&face_id);
781 }
782
783 if ( strcmp(cmd_type,"name")== 0 )
784 {
785 if ( strcmp(op_type,"del") == 0 )
786 {
787 res=does_name_exist_in_npl(np);
788 if ( res == 0)
789 {
790 sprintf(msg,"Name %s does not exist !!",name);
791 }
792 else
793 {
794 long int ls_id=get_lsa_id_from_npl(np);
795 if ( ls_id != 0 )
796 {
797 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
798 sprintf(msg,"Name %s has been deleted and Advertised.",name);
799 }
800 else
801 {
802 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
803 }
804 }
805 }
806 else if ( strcmp(op_type,"add") == 0 )
807 {
808 res=does_name_exist_in_npl(np);
809 if ( res == 0)
810 {
811 add_name_to_npl(np);
812 build_and_install_single_name_lsa(np);
813 sprintf(msg,"Name %s has been added to advertise.",name);
814 }
815 else
816 {
817 sprintf(msg,"Name %s has already been advertised from this router !!",name);
818 }
819 }
820 }
821 else if ( strcmp(cmd_type,"neighbor") == 0 )
822 {
823 if ( strcmp(op_type,"del") == 0 )
824 {
825 res=is_neighbor(np->name);
826 if ( res == 0)
827 {
828 sprintf(msg,"Neighbor %s does not exist !!",name);
829 }
830 else
831 {
832 update_adjacent_status_to_adl(np,NBR_DOWN);
833 delete_nbr_from_adl(np);
834 if(!nlsr->is_build_adj_lsa_sheduled)
835 {
836 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
837 nlsr->is_build_adj_lsa_sheduled=1;
838 }
839 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
840 }
841 }
842 else if ( strcmp(op_type,"add") == 0 )
843 {
844 res=is_neighbor(np->name);
845 if ( res == 0 )
846 {
akmhoque09c0afa2012-12-14 09:27:00 -0600847 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
848 get_host_name_from_command_string(nbr_name,np->name,0);
849 printf("Hostname of neighbor: %s ",nbr_name->name);
850
akmhoquea37b52c2012-12-14 11:16:36 -0600851 char *ip_addr=(char *)malloc(13);
852 memset(ip_addr,13,0);
853 get_ip_from_hostname_02(nbr_name->name,ip_addr);
854 printf("IP Address: %s \n",ip_addr);
855 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695);
856 update_face_to_adl_for_nbr(nbr_name->name, face_id);
857 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
858
859 add_nbr_to_adl(np,face_id,ip_addr);
860
akmhoque8fdd6412012-12-04 15:05:33 -0600861 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoquea37b52c2012-12-14 11:16:36 -0600862
akmhoque8fdd6412012-12-04 15:05:33 -0600863 }
864 else
865 {
866 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
867 }
868 }
869 }
870
871
872 return msg;
873}
874
875int
876nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
877{
878 struct timeval timeout;
879 if ( time_out_micro_sec < 0 )
880 {
881 timeout.tv_sec=1;
882 timeout.tv_usec=0;
883 }
884 else
885 {
886 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
887 timeout.tv_sec=time_out_micro_sec / 1000000;
888 timeout.tv_usec=time_out_micro_sec % 1000000;
889 }
890
891
892 int fd;
893 int nread;
894 int result;
895 fd_set testfds;
896 unsigned int client_len;
897 int client_sockfd;
898 char recv_buffer[1024];
899 bzero(recv_buffer,1024);
900 struct sockaddr_in client_address;
901
902 testfds=nlsr->readfds;
903 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
904
905 for(fd = 0; fd < FD_SETSIZE; fd++)
906 {
907 if(FD_ISSET(fd,&testfds))
908 {
909 if ( fd == ccn_fd )
910 {
911 return 0;
912 }
913 else if(fd == nlsr->nlsr_api_server_sock_fd)
914 {
915 client_len = sizeof(client_address);
916 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
917 FD_SET(client_sockfd, &nlsr->readfds);
918 }
919 else
920 {
921
922 ioctl(fd, FIONREAD, &nread);
923 if(nread == 0)
924 {
925 close(fd);
926 FD_CLR(fd, &nlsr->readfds);
927 }
928 else
929 {
930 recv(fd, recv_buffer, 1024, 0);
931 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
932 char *msg=process_api_client_command(recv_buffer);
933 send(fd, msg, strlen(msg),0);
934 free(msg);
935 close(fd);
936 FD_CLR(fd, &nlsr->readfds);
937 }
938 }
939 }
940 }
941
942 return 0;
943}
944
945void
946nlsr_destroy( void )
947{
948 if ( nlsr->debugging )
949 {
950 printf("Freeing Allocated Memory....\n");
951 }
952 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
953 /* Destroying all face created by nlsr in CCND */
954 destroy_all_face_by_nlsr();
955
956 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
957 hashtb_destroy(&nlsr->npl);
958 hashtb_destroy(&nlsr->adl);
959 hashtb_destroy(&nlsr->lsdb->name_lsdb);
960 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
961 hashtb_destroy(&nlsr->pit_alsa);
962
963 //To Do: has to destroy the face_list one by one
964
965 hashtb_destroy(&nlsr->routing_table);
966
967
968 int i, npt_element;
969 struct npt_entry *ne;
970 struct hashtb_enumerator ee;
971 struct hashtb_enumerator *e = &ee;
972 hashtb_start(nlsr->npt, e);
973 npt_element=hashtb_n(nlsr->npt);
974 for(i=0;i<npt_element;i++)
975 {
976 ne=e->data;
977 hashtb_destroy(&ne->name_list);
978 hashtb_destroy(&ne->face_list);
979 hashtb_next(e);
980 }
981
982 hashtb_end(e);
983 hashtb_destroy(&nlsr->npt);
984
985
986 ccns_close(&nlsr->ccns, NULL, NULL);
987 ccns_slice_destroy(&nlsr->slice);
988
989
990
991 close(nlsr->nlsr_api_server_sock_fd);
992
993 ccn_schedule_destroy(&nlsr->sched);
994 ccn_destroy(&nlsr->ccn);
995
996 free(nlsr->lsdb->lsdb_version);
997 free(nlsr->lsdb);
998 free(nlsr->router_name);
999 free(nlsr);
1000 if ( nlsr->debugging )
1001 {
1002 printf("Finished freeing allocated memory\n");
1003 }
1004 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1005
1006}
1007
1008
1009
1010void
1011init_api_server(int ccn_fd)
1012{
1013 int server_sockfd;
1014 int server_len;
1015 struct sockaddr_in server_address;
1016 unsigned int yes=1;
1017
1018 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1019
1020 int flags = fcntl(server_sockfd, F_GETFL, 0);
1021 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1022
1023 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1024 {
1025 ON_ERROR_DESTROY(-1);
1026 }
1027
1028 server_address.sin_family = AF_INET;
1029 server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
1030 server_address.sin_port = nlsr->api_port;
1031
1032 server_len = sizeof(server_address);
1033 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1034 listen(server_sockfd, 100);
1035 FD_ZERO(&nlsr->readfds);
1036 FD_SET(server_sockfd, &nlsr->readfds);
1037 FD_SET(ccn_fd, &nlsr->readfds);
1038 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1039
1040}
1041
1042int
1043init_nlsr(void)
1044{
1045 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1046 {
1047 perror("SIGQUIT install error\n");
1048 return -1;
1049 }
1050 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1051 {
1052 perror("SIGTERM install error\n");
1053 return -1;
1054 }
1055 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1056 {
1057 perror("SIGTERM install error\n");
1058 return -1;
1059 }
1060
1061 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1062
1063 struct hashtb_param param_adl = {0};
1064 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1065 struct hashtb_param param_npl = {0};
1066 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1067 struct hashtb_param param_pit_alsa = {0};
1068 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1069 struct hashtb_param param_npt = {0};
1070 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1071 struct hashtb_param param_rte = {0};
1072 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1073
1074 nlsr->in_interest.p = &incoming_interest;
1075 nlsr->in_content.p = &incoming_content;
1076
1077 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1078
1079 char *time_stamp=(char *)malloc(20);
1080 memset(time_stamp,0,20);
1081 get_current_timestamp_micro(time_stamp);
1082 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
1083 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
1084 free(time_stamp);
1085
1086 struct hashtb_param param_adj_lsdb = {0};
1087 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1088 struct hashtb_param param_name_lsdb = {0};
1089 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
1090
1091
1092
1093
1094 nlsr->is_synch_init=1;
1095 nlsr->nlsa_id=0;
1096 nlsr->adj_build_flag=0;
1097 nlsr->adj_build_count=0;
1098 nlsr->is_build_adj_lsa_sheduled=0;
1099 nlsr->is_send_lsdb_interest_scheduled=0;
1100 nlsr->is_route_calculation_scheduled=0;
1101
1102 nlsr->detailed_logging=0;
1103 nlsr->debugging=0;
1104
1105 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
1106 nlsr->interest_retry = INTEREST_RETRY;
1107 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1108 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1109 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
1110 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
1111 nlsr->semaphor=NLSR_UNLOCKED;
1112
1113 nlsr->api_port=API_PORT;
1114
akmhoque8fdd6412012-12-04 15:05:33 -06001115 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
1116 memset(nlsr->topo_prefix,strlen("/ndn/routing/nlsr")+1,0);
1117 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1118
1119 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
1120 memset(nlsr->slice_prefix,strlen("/ndn/routing/nlsr/LSA")+1,0);
1121 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1122
akmhoque09c0afa2012-12-14 09:27:00 -06001123 nlsr->is_hyperbolic_calc=0;
1124
akmhoque8fdd6412012-12-04 15:05:33 -06001125 return 0;
1126}
1127
1128
1129int
1130main(int argc, char *argv[])
1131{
1132 int res, ret;
1133 char *config_file;
1134 int daemon_mode=0;
1135 int port=0;
1136
1137
1138
1139 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1140 {
1141 switch (res)
1142 {
1143 case 'd':
1144 daemon_mode = 1;
1145 break;
1146 case 'f':
1147 config_file = optarg;
1148 break;
1149 case 'p':
1150 port = atoi(optarg);
1151 break;
1152 case 'h':
1153 default:
1154 usage(argv[0]);
1155 }
1156 }
1157
1158 ret=init_nlsr();
1159 ON_ERROR_EXIT(ret);
1160
1161 if ( port !=0 )
1162 nlsr->api_port=port;
1163
1164 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001165
akmhoquea37b52c2012-12-14 11:16:36 -06001166 print_adjacent_from_adl();
1167
akmhoque8fdd6412012-12-04 15:05:33 -06001168 if ( daemon_mode == 1 )
1169 {
1170 daemonize_nlsr();
1171 }
1172
1173 startLogging(nlsr->logDir);
1174
1175 nlsr->ccn=ccn_create();
1176 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1177 if(ccn_fd == -1)
1178 {
1179 fprintf(stderr,"Could not connect to ccnd\n");
1180 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1181 ON_ERROR_DESTROY(-1);
1182 }
1183
1184 init_api_server(ccn_fd);
1185
1186 create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1187
1188 struct ccn_charbuf *router_prefix;
1189 router_prefix=ccn_charbuf_create();
1190 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1191 if(res<0)
1192 {
1193 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1194 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1195 ON_ERROR_DESTROY(res);
1196 }
1197
1198 ccn_name_append_str(router_prefix,"nlsr");
1199 nlsr->in_interest.data=nlsr->router_name;
1200 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1201 if ( res < 0 )
1202 {
1203 fprintf(stderr,"Failed to register interest for router\n");
1204 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1205 ON_ERROR_DESTROY(res);
1206 }
1207 ccn_charbuf_destroy(&router_prefix);
1208
1209 if ( nlsr->debugging )
1210 printf("Router Name : %s\n",nlsr->router_name);
1211 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1212 if ( nlsr->debugging )
1213 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1214 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1215
akmhoquea37b52c2012-12-14 11:16:36 -06001216 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001217 print_name_prefix_from_npl();
1218 print_adjacent_from_adl();
1219 build_and_install_name_lsas();
1220 print_name_lsdb();
1221
1222 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1223
1224 write_name_lsdb_to_repo(nlsr->slice_prefix);
1225
akmhoque8fdd6412012-12-04 15:05:33 -06001226 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001227 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1228 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001229
1230
1231 while(1)
1232 {
1233 if ( nlsr->semaphor == NLSR_UNLOCKED )
1234 {
1235 if( nlsr->sched != NULL )
1236 {
1237 long int micro_sec=ccn_schedule_run(nlsr->sched);
1238 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1239 ON_ERROR_DESTROY(res);
1240 }
1241 if(nlsr->ccn != NULL)
1242 {
1243 res = ccn_run(nlsr->ccn, 0);
1244 }
1245 if (!(nlsr->sched && nlsr->ccn))
1246 {
1247 break;
1248 }
1249 }
1250
1251 }
1252
1253
1254 return 0;
1255}
1256