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