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