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