blob: 908a227576e96efe80a9f5bb20405b91ba95f039 [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);
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);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600586 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoque09c0afa2012-12-14 09:27:00 -0600587 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);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600754 memset(msg,0,100);
akmhoque8fdd6412012-12-04 15:05:33 -0600755
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);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600852 memset(ip_addr,0,13);
akmhoquea37b52c2012-12-14 11:16:36 -0600853 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
Adam Alyyanf1f188f2013-01-14 11:57:08 -0600891 timeout.tv_sec = 0;
892 timeout.tv_usec = 499000;
akmhoque8fdd6412012-12-04 15:05:33 -0600893
894 int fd;
895 int nread;
896 int result;
897 fd_set testfds;
898 unsigned int client_len;
899 int client_sockfd;
900 char recv_buffer[1024];
901 bzero(recv_buffer,1024);
902 struct sockaddr_in client_address;
903
904 testfds=nlsr->readfds;
905 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
906
907 for(fd = 0; fd < FD_SETSIZE; fd++)
908 {
909 if(FD_ISSET(fd,&testfds))
910 {
911 if ( fd == ccn_fd )
912 {
913 return 0;
914 }
915 else if(fd == nlsr->nlsr_api_server_sock_fd)
916 {
917 client_len = sizeof(client_address);
918 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
919 FD_SET(client_sockfd, &nlsr->readfds);
920 }
921 else
922 {
923
924 ioctl(fd, FIONREAD, &nread);
925 if(nread == 0)
926 {
927 close(fd);
928 FD_CLR(fd, &nlsr->readfds);
929 }
930 else
931 {
932 recv(fd, recv_buffer, 1024, 0);
933 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
934 char *msg=process_api_client_command(recv_buffer);
935 send(fd, msg, strlen(msg),0);
936 free(msg);
937 close(fd);
938 FD_CLR(fd, &nlsr->readfds);
939 }
940 }
941 }
942 }
943
944 return 0;
945}
946
947void
948nlsr_destroy( void )
949{
950 if ( nlsr->debugging )
951 {
952 printf("Freeing Allocated Memory....\n");
953 }
954 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
955 /* Destroying all face created by nlsr in CCND */
956 destroy_all_face_by_nlsr();
957
958 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
959 hashtb_destroy(&nlsr->npl);
960 hashtb_destroy(&nlsr->adl);
961 hashtb_destroy(&nlsr->lsdb->name_lsdb);
962 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
963 hashtb_destroy(&nlsr->pit_alsa);
964
965 //To Do: has to destroy the face_list one by one
966
967 hashtb_destroy(&nlsr->routing_table);
968
969
970 int i, npt_element;
971 struct npt_entry *ne;
972 struct hashtb_enumerator ee;
973 struct hashtb_enumerator *e = &ee;
974 hashtb_start(nlsr->npt, e);
975 npt_element=hashtb_n(nlsr->npt);
976 for(i=0;i<npt_element;i++)
977 {
978 ne=e->data;
979 hashtb_destroy(&ne->name_list);
980 hashtb_destroy(&ne->face_list);
981 hashtb_next(e);
982 }
983
984 hashtb_end(e);
985 hashtb_destroy(&nlsr->npt);
986
987
988 ccns_close(&nlsr->ccns, NULL, NULL);
989 ccns_slice_destroy(&nlsr->slice);
990
991
992
993 close(nlsr->nlsr_api_server_sock_fd);
994
995 ccn_schedule_destroy(&nlsr->sched);
996 ccn_destroy(&nlsr->ccn);
997
998 free(nlsr->lsdb->lsdb_version);
999 free(nlsr->lsdb);
1000 free(nlsr->router_name);
1001 free(nlsr);
1002 if ( nlsr->debugging )
1003 {
1004 printf("Finished freeing allocated memory\n");
1005 }
1006 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1007
1008}
1009
1010
1011
1012void
1013init_api_server(int ccn_fd)
1014{
1015 int server_sockfd;
1016 int server_len;
1017 struct sockaddr_in server_address;
1018 unsigned int yes=1;
1019
1020 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1021
1022 int flags = fcntl(server_sockfd, F_GETFL, 0);
1023 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1024
1025 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1026 {
1027 ON_ERROR_DESTROY(-1);
1028 }
1029
1030 server_address.sin_family = AF_INET;
1031 server_address.sin_addr.s_addr = inet_addr("127.0.0.1");
1032 server_address.sin_port = nlsr->api_port;
1033
1034 server_len = sizeof(server_address);
1035 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1036 listen(server_sockfd, 100);
1037 FD_ZERO(&nlsr->readfds);
1038 FD_SET(server_sockfd, &nlsr->readfds);
1039 FD_SET(ccn_fd, &nlsr->readfds);
1040 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1041
1042}
1043
1044int
1045init_nlsr(void)
1046{
1047 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1048 {
1049 perror("SIGQUIT install error\n");
1050 return -1;
1051 }
1052 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1053 {
1054 perror("SIGTERM install error\n");
1055 return -1;
1056 }
1057 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1058 {
1059 perror("SIGTERM install error\n");
1060 return -1;
1061 }
1062
1063 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1064
1065 struct hashtb_param param_adl = {0};
1066 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1067 struct hashtb_param param_npl = {0};
1068 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1069 struct hashtb_param param_pit_alsa = {0};
1070 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1071 struct hashtb_param param_npt = {0};
1072 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1073 struct hashtb_param param_rte = {0};
1074 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1075
1076 nlsr->in_interest.p = &incoming_interest;
1077 nlsr->in_content.p = &incoming_content;
1078
1079 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1080
1081 char *time_stamp=(char *)malloc(20);
1082 memset(time_stamp,0,20);
1083 get_current_timestamp_micro(time_stamp);
1084 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
1085 memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp));
1086 free(time_stamp);
1087
1088 struct hashtb_param param_adj_lsdb = {0};
1089 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1090 struct hashtb_param param_name_lsdb = {0};
1091 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
1092
1093
1094
1095
1096 nlsr->is_synch_init=1;
1097 nlsr->nlsa_id=0;
1098 nlsr->adj_build_flag=0;
1099 nlsr->adj_build_count=0;
1100 nlsr->is_build_adj_lsa_sheduled=0;
1101 nlsr->is_send_lsdb_interest_scheduled=0;
1102 nlsr->is_route_calculation_scheduled=0;
1103
1104 nlsr->detailed_logging=0;
1105 nlsr->debugging=0;
1106
1107 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
1108 nlsr->interest_retry = INTEREST_RETRY;
1109 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1110 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1111 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
1112 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
1113 nlsr->semaphor=NLSR_UNLOCKED;
1114
1115 nlsr->api_port=API_PORT;
1116
akmhoque8fdd6412012-12-04 15:05:33 -06001117 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001118 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001119 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1120
1121 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001122 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001123 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1124
akmhoque09c0afa2012-12-14 09:27:00 -06001125 nlsr->is_hyperbolic_calc=0;
1126
akmhoque8fdd6412012-12-04 15:05:33 -06001127 return 0;
1128}
1129
1130
1131int
1132main(int argc, char *argv[])
1133{
1134 int res, ret;
1135 char *config_file;
1136 int daemon_mode=0;
1137 int port=0;
1138
1139
1140
1141 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1142 {
1143 switch (res)
1144 {
1145 case 'd':
1146 daemon_mode = 1;
1147 break;
1148 case 'f':
1149 config_file = optarg;
1150 break;
1151 case 'p':
1152 port = atoi(optarg);
1153 break;
1154 case 'h':
1155 default:
1156 usage(argv[0]);
1157 }
1158 }
1159
1160 ret=init_nlsr();
1161 ON_ERROR_EXIT(ret);
1162
1163 if ( port !=0 )
1164 nlsr->api_port=port;
1165
1166 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001167
akmhoquea37b52c2012-12-14 11:16:36 -06001168 print_adjacent_from_adl();
1169
akmhoque8fdd6412012-12-04 15:05:33 -06001170 if ( daemon_mode == 1 )
1171 {
1172 daemonize_nlsr();
1173 }
1174
1175 startLogging(nlsr->logDir);
1176
1177 nlsr->ccn=ccn_create();
1178 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1179 if(ccn_fd == -1)
1180 {
1181 fprintf(stderr,"Could not connect to ccnd\n");
1182 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1183 ON_ERROR_DESTROY(-1);
1184 }
1185
1186 init_api_server(ccn_fd);
1187
1188 create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1189
1190 struct ccn_charbuf *router_prefix;
1191 router_prefix=ccn_charbuf_create();
1192 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1193 if(res<0)
1194 {
1195 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1196 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1197 ON_ERROR_DESTROY(res);
1198 }
1199
1200 ccn_name_append_str(router_prefix,"nlsr");
1201 nlsr->in_interest.data=nlsr->router_name;
1202 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1203 if ( res < 0 )
1204 {
1205 fprintf(stderr,"Failed to register interest for router\n");
1206 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1207 ON_ERROR_DESTROY(res);
1208 }
1209 ccn_charbuf_destroy(&router_prefix);
1210
1211 if ( nlsr->debugging )
1212 printf("Router Name : %s\n",nlsr->router_name);
1213 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1214 if ( nlsr->debugging )
1215 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1216 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1217
akmhoquea37b52c2012-12-14 11:16:36 -06001218 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001219 print_name_prefix_from_npl();
1220 print_adjacent_from_adl();
1221 build_and_install_name_lsas();
1222 print_name_lsdb();
1223
1224 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1225
1226 write_name_lsdb_to_repo(nlsr->slice_prefix);
1227
akmhoque8fdd6412012-12-04 15:05:33 -06001228 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001229 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1230 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001231
1232
1233 while(1)
1234 {
1235 if ( nlsr->semaphor == NLSR_UNLOCKED )
1236 {
1237 if( nlsr->sched != NULL )
1238 {
1239 long int micro_sec=ccn_schedule_run(nlsr->sched);
1240 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1241 ON_ERROR_DESTROY(res);
1242 }
1243 if(nlsr->ccn != NULL)
1244 {
1245 res = ccn_run(nlsr->ccn, 0);
1246 }
1247 if (!(nlsr->sched && nlsr->ccn))
1248 {
1249 break;
1250 }
1251 }
1252
1253 }
1254
1255
1256 return 0;
1257}
1258