blob: 587e015e70dac554558b84a31b4ed503b50d1d90 [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 memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1);
556 puts(topo_prefix);
akmhoque09c0afa2012-12-14 09:27:00 -0600557 memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix));
558
akmhoque09c0afa2012-12-14 09:27:00 -0600559 }
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);
akmhoqueb29edd82013-01-14 20:54:11 -0600585 if ( slice_prefix[strlen(slice_prefix)-1] == '/' )
586 slice_prefix[strlen(slice_prefix)-1]='\0';
587
akmhoque09c0afa2012-12-14 09:27:00 -0600588 nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600589 memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1);
akmhoque09c0afa2012-12-14 09:27:00 -0600590 memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix));
akmhoque09c0afa2012-12-14 09:27:00 -0600591 }
592}
593
594void
595process_command_hyperbolic(char *command)
596{
597 if(command==NULL)
598 {
599 printf(" Wrong Command Format ( hyperbolic on/off )\n");
600 return;
601 }
602 char *rem;
603 const char *sep=" \t\n";
604 char *on_off;
605
606 on_off=strtok_r(command,sep,&rem);
607 if(on_off==NULL)
608 {
609 printf(" Wrong Command Format ( hyperbolic on/off )\n");
610 return;
611 }
612
613 if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 )
614 {
615 nlsr->is_hyperbolic_calc=1;
616 }
617}
618
akmhoqueed418f32013-01-30 12:25:04 -0600619void
620process_command_hyperbolic_cordinate(char *command)
621{
622 if(command==NULL)
623 {
624 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
625 return;
626 }
627
628 char *rem;
629 const char *sep=" \t\n\r";
630 char *radious;
631 char *theta;
632
633 radious=strtok_r(command,sep,&rem);
634 if (radious == NULL )
635 {
636 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
637 return;
638 }
639
640 theta=strtok_r(NULL,sep,&rem);
641 if (theta == NULL )
642 {
643 printf(" Wrong Command Format ( hyperbolic r 0 )\n");
644 return;
645 }
646
647 nlsr->cor_r=strtof(radious,NULL);
648 nlsr->cor_theta=strtof(theta,NULL);
649
650}
651
akmhoque8fdd6412012-12-04 15:05:33 -0600652void
653process_conf_command(char *command)
654{
655 const char *separators=" \t\n";
656 char *remainder=NULL;
657 char *cmd_type=NULL;
658
659 if(command==NULL || strlen(command)==0 || command[0]=='!')
660 return;
661
662 cmd_type=strtok_r(command,separators,&remainder);
663
664 if(!strcmp(cmd_type,"router-name") )
665 {
666 process_command_router_name(remainder);
667 }
668 else if(!strcmp(cmd_type,"ccnneighbor") )
669 {
670 process_command_ccnneighbor(remainder);
671 }
672 else if(!strcmp(cmd_type,"ccnname") )
673 {
674 process_command_ccnname(remainder);
675 }
676 else if(!strcmp(cmd_type,"lsdb-synch-interval") )
677 {
678 process_command_lsdb_synch_interval(remainder);
679 }
680 else if(!strcmp(cmd_type,"interest-retry") )
681 {
682 process_command_interest_retry(remainder);
683 }
684 else if(!strcmp(cmd_type,"interest-resend-time") )
685 {
686 process_command_interest_resend_time(remainder);
687 }
688 else if(!strcmp(cmd_type,"lsa-refresh-time") )
689 {
690 process_command_lsa_refresh_time(remainder);
691 }
692 else if(!strcmp(cmd_type,"router-dead-interval") )
693 {
694 process_command_router_dead_interval(remainder);
695 }
696 else if(!strcmp(cmd_type,"multi-path-face-num") )
697 {
698 process_command_multi_path_face_num(remainder);
699 }
700 else if(!strcmp(cmd_type,"logdir") )
701 {
702 process_command_logdir(remainder);
703 }
704 else if(!strcmp(cmd_type,"detailed-log") )
705 {
706 process_command_detailed_log(remainder);
707 }
708 else if(!strcmp(cmd_type,"debug") )
709 {
710 process_command_debug(remainder);
711 }
akmhoque09c0afa2012-12-14 09:27:00 -0600712 else if(!strcmp(cmd_type,"topo-prefix") )
713 {
714 process_command_topo_prefix(remainder);
715 }
716 else if(!strcmp(cmd_type,"slice-prefix") )
717 {
718 process_command_slice_prefix(remainder);
719 }
akmhoqueed418f32013-01-30 12:25:04 -0600720 else if(!strcmp(cmd_type,"hyperbolic-cordinate") )
721 {
722 process_command_hyperbolic_cordinate(remainder);
723 }
akmhoque8fdd6412012-12-04 15:05:33 -0600724 else
725 {
726 printf("Wrong configuration Command %s \n",cmd_type);
727 }
728}
729
730
731int
732readConfigFile(const char *filename)
733{
734 FILE *cfg;
735 char buf[1024];
736 int len;
737
738 cfg=fopen(filename, "r");
739
740 if(cfg == NULL)
741 {
742 printf("\nConfiguration File does not exists\n");
743 exit(1);
744 }
745
746 while(fgets((char *)buf, sizeof(buf), cfg))
747 {
748 len=strlen(buf);
749 if(buf[len-1] == '\n')
750 buf[len-1]='\0';
751 if ( buf[0] != '#' && buf[0] != '!')
752 process_conf_command(buf);
753 }
754
755 fclose(cfg);
756
757 return 0;
758}
759
akmhoquea37b52c2012-12-14 11:16:36 -0600760
761void
762add_faces_for_nbrs(void)
763{
764 int i, adl_element;
765 struct ndn_neighbor *nbr;
766
767 struct hashtb_enumerator ee;
768 struct hashtb_enumerator *e = &ee;
769
770 hashtb_start(nlsr->adl, e);
771 adl_element=hashtb_n(nlsr->adl);
772
773 for(i=0;i<adl_element;i++)
774 {
775 nbr=e->data;
776 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695);
777 update_face_to_adl_for_nbr(nbr->neighbor->name, face_id);
778 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
akmhoquea37b52c2012-12-14 11:16:36 -0600779 hashtb_next(e);
780 }
781
782 hashtb_end(e);
783
784}
785
akmhoque61a57f22013-01-18 07:44:05 -0600786void
787destroy_faces_for_nbrs(void)
788{
789 int i, adl_element;
790 struct ndn_neighbor *nbr;
791
792 struct hashtb_enumerator ee;
793 struct hashtb_enumerator *e = &ee;
794
795 hashtb_start(nlsr->adl, e);
796 adl_element=hashtb_n(nlsr->adl);
797
798 for(i=0;i<adl_element;i++)
799 {
akmhoqueed418f32013-01-30 12:25:04 -0600800 nbr=e->data;
801 if ( nbr->face > 0 )
802 {
803 add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face);
804 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face);
805 }
akmhoque61a57f22013-01-18 07:44:05 -0600806 hashtb_next(e);
807 }
808
809 hashtb_end(e);
810
811}
812
akmhoque8fdd6412012-12-04 15:05:33 -0600813char *
814process_api_client_command(char *command)
815{
816 char *msg;
817 msg=(char *)malloc(100);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600818 memset(msg,0,100);
akmhoque8fdd6412012-12-04 15:05:33 -0600819
820 const char *sep=" \t\n";
821 char *rem=NULL;
822 char *cmd_type=NULL;
823 char *op_type=NULL;
824 char *name=NULL;
825 char *face=NULL;
826 int face_id;
827 int res;
828
829 op_type=strtok_r(command,sep,&rem);
830 cmd_type=strtok_r(NULL,sep,&rem);
831 name=strtok_r(NULL,sep,&rem);
832 if ( name[strlen(name)-1] == '/' )
833 name[strlen(name)-1]='\0';
834
835 struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
836 np->name=(char *)malloc(strlen(name)+1);
837 memset(np->name,0,strlen(name)+1);
838 memcpy(np->name,name,strlen(name)+1);
839 np->length=strlen(name)+1;
840
841 if ( strcmp(cmd_type,"name")!= 0 )
842 {
843 face=strtok_r(NULL,sep,&rem);
844 sscanf(face,"face%d",&face_id);
845 }
846
847 if ( strcmp(cmd_type,"name")== 0 )
848 {
849 if ( strcmp(op_type,"del") == 0 )
850 {
851 res=does_name_exist_in_npl(np);
852 if ( res == 0)
853 {
854 sprintf(msg,"Name %s does not exist !!",name);
855 }
856 else
857 {
858 long int ls_id=get_lsa_id_from_npl(np);
859 if ( ls_id != 0 )
860 {
861 make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id);
862 sprintf(msg,"Name %s has been deleted and Advertised.",name);
863 }
864 else
865 {
866 sprintf(msg,"Name %s does not have an Name LSA yet !!",name);
867 }
868 }
869 }
870 else if ( strcmp(op_type,"add") == 0 )
871 {
872 res=does_name_exist_in_npl(np);
873 if ( res == 0)
874 {
875 add_name_to_npl(np);
876 build_and_install_single_name_lsa(np);
877 sprintf(msg,"Name %s has been added to advertise.",name);
878 }
879 else
880 {
881 sprintf(msg,"Name %s has already been advertised from this router !!",name);
882 }
883 }
884 }
885 else if ( strcmp(cmd_type,"neighbor") == 0 )
886 {
887 if ( strcmp(op_type,"del") == 0 )
888 {
889 res=is_neighbor(np->name);
890 if ( res == 0)
891 {
892 sprintf(msg,"Neighbor %s does not exist !!",name);
893 }
894 else
895 {
896 update_adjacent_status_to_adl(np,NBR_DOWN);
897 delete_nbr_from_adl(np);
898 if(!nlsr->is_build_adj_lsa_sheduled)
899 {
900 nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0);
901 nlsr->is_build_adj_lsa_sheduled=1;
902 }
903 sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name);
904 }
905 }
906 else if ( strcmp(op_type,"add") == 0 )
907 {
908 res=is_neighbor(np->name);
909 if ( res == 0 )
910 {
akmhoque09c0afa2012-12-14 09:27:00 -0600911 struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix ));
912 get_host_name_from_command_string(nbr_name,np->name,0);
913 printf("Hostname of neighbor: %s ",nbr_name->name);
914
akmhoquea37b52c2012-12-14 11:16:36 -0600915 char *ip_addr=(char *)malloc(13);
Syed Obaid Amin4c959562012-12-21 16:43:21 -0600916 memset(ip_addr,0,13);
akmhoquea37b52c2012-12-14 11:16:36 -0600917 get_ip_from_hostname_02(nbr_name->name,ip_addr);
918 printf("IP Address: %s \n",ip_addr);
919 int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695);
920 update_face_to_adl_for_nbr(nbr_name->name, face_id);
921 add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id);
922
923 add_nbr_to_adl(np,face_id,ip_addr);
924
akmhoque8fdd6412012-12-04 15:05:33 -0600925 sprintf(msg,"Neighbor %s has been added to adjacency list.",name);
akmhoquea37b52c2012-12-14 11:16:36 -0600926
akmhoque8fdd6412012-12-04 15:05:33 -0600927 }
928 else
929 {
930 sprintf(msg,"Neighbor %s already exists in adjacency list.",name);
931 }
932 }
933 }
934
935
936 return msg;
937}
938
939int
940nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd)
941{
942 struct timeval timeout;
akmhoque21ffec22013-01-31 18:22:10 -0600943 if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 )
akmhoque8fdd6412012-12-04 15:05:33 -0600944 {
akmhoque21ffec22013-01-31 18:22:10 -0600945 timeout.tv_sec=0;
946 timeout.tv_usec=time_out_micro_sec;
akmhoque8fdd6412012-12-04 15:05:33 -0600947 }
akmhoque21ffec22013-01-31 18:22:10 -0600948 else
akmhoque8fdd6412012-12-04 15:05:33 -0600949 {
akmhoque21ffec22013-01-31 18:22:10 -0600950 timeout.tv_sec = 0;
951 timeout.tv_usec = 500000;
akmhoque8fdd6412012-12-04 15:05:33 -0600952 }
akmhoque8fdd6412012-12-04 15:05:33 -0600953
954 int fd;
955 int nread;
956 int result;
957 fd_set testfds;
958 unsigned int client_len;
959 int client_sockfd;
960 char recv_buffer[1024];
961 bzero(recv_buffer,1024);
962 struct sockaddr_in client_address;
963
964 testfds=nlsr->readfds;
965 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
966
soamin29b8e392013-01-22 17:12:07 -0600967 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque8fdd6412012-12-04 15:05:33 -0600968 {
969 if(FD_ISSET(fd,&testfds))
970 {
971 if ( fd == ccn_fd )
972 {
973 return 0;
974 }
975 else if(fd == nlsr->nlsr_api_server_sock_fd)
976 {
977 client_len = sizeof(client_address);
978 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
979 FD_SET(client_sockfd, &nlsr->readfds);
980 }
981 else
982 {
983
984 ioctl(fd, FIONREAD, &nread);
985 if(nread == 0)
986 {
987 close(fd);
988 FD_CLR(fd, &nlsr->readfds);
989 }
990 else
991 {
992 recv(fd, recv_buffer, 1024, 0);
993 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
994 char *msg=process_api_client_command(recv_buffer);
995 send(fd, msg, strlen(msg),0);
996 free(msg);
997 close(fd);
998 FD_CLR(fd, &nlsr->readfds);
999 }
1000 }
1001 }
1002 }
1003
1004 return 0;
1005}
1006
1007void
1008nlsr_destroy( void )
1009{
1010 if ( nlsr->debugging )
1011 {
1012 printf("Freeing Allocated Memory....\n");
1013 }
1014 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
1015 /* Destroying all face created by nlsr in CCND */
1016 destroy_all_face_by_nlsr();
akmhoque61a57f22013-01-18 07:44:05 -06001017 destroy_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001018 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
1019 hashtb_destroy(&nlsr->npl);
1020 hashtb_destroy(&nlsr->adl);
1021 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1022 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
1023 hashtb_destroy(&nlsr->pit_alsa);
1024
akmhoqueed418f32013-01-30 12:25:04 -06001025
akmhoque8fdd6412012-12-04 15:05:33 -06001026
1027 hashtb_destroy(&nlsr->routing_table);
1028
1029
1030 int i, npt_element;
1031 struct npt_entry *ne;
1032 struct hashtb_enumerator ee;
1033 struct hashtb_enumerator *e = &ee;
1034 hashtb_start(nlsr->npt, e);
1035 npt_element=hashtb_n(nlsr->npt);
1036 for(i=0;i<npt_element;i++)
1037 {
1038 ne=e->data;
1039 hashtb_destroy(&ne->name_list);
1040 hashtb_destroy(&ne->face_list);
1041 hashtb_next(e);
1042 }
1043
1044 hashtb_end(e);
1045 hashtb_destroy(&nlsr->npt);
1046
1047
1048 ccns_close(&nlsr->ccns, NULL, NULL);
1049 ccns_slice_destroy(&nlsr->slice);
1050
1051
1052
1053 close(nlsr->nlsr_api_server_sock_fd);
1054
1055 ccn_schedule_destroy(&nlsr->sched);
1056 ccn_destroy(&nlsr->ccn);
1057
1058 free(nlsr->lsdb->lsdb_version);
1059 free(nlsr->lsdb);
1060 free(nlsr->router_name);
1061 free(nlsr);
1062 if ( nlsr->debugging )
1063 {
1064 printf("Finished freeing allocated memory\n");
1065 }
1066 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1067
1068}
1069
1070
1071
1072void
1073init_api_server(int ccn_fd)
1074{
1075 int server_sockfd;
1076 int server_len;
1077 struct sockaddr_in server_address;
1078 unsigned int yes=1;
1079
1080 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1081
1082 int flags = fcntl(server_sockfd, F_GETFL, 0);
1083 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1084
1085 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1086 {
1087 ON_ERROR_DESTROY(-1);
1088 }
1089
1090 server_address.sin_family = AF_INET;
Adam Alyyanc350e3a2013-01-14 14:35:57 -06001091 server_address.sin_addr.s_addr = INADDR_ANY;
1092 server_address.sin_port = htons(nlsr->api_port);
akmhoque8fdd6412012-12-04 15:05:33 -06001093
1094 server_len = sizeof(server_address);
1095 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1096 listen(server_sockfd, 100);
1097 FD_ZERO(&nlsr->readfds);
1098 FD_SET(server_sockfd, &nlsr->readfds);
1099 FD_SET(ccn_fd, &nlsr->readfds);
1100 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1101
1102}
1103
1104int
1105init_nlsr(void)
1106{
1107 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1108 {
1109 perror("SIGQUIT install error\n");
1110 return -1;
1111 }
1112 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1113 {
1114 perror("SIGTERM install error\n");
1115 return -1;
1116 }
1117 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1118 {
1119 perror("SIGTERM install error\n");
1120 return -1;
1121 }
1122
1123 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1124
1125 struct hashtb_param param_adl = {0};
1126 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1127 struct hashtb_param param_npl = {0};
1128 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1129 struct hashtb_param param_pit_alsa = {0};
1130 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1131 struct hashtb_param param_npt = {0};
1132 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1133 struct hashtb_param param_rte = {0};
1134 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1135
1136 nlsr->in_interest.p = &incoming_interest;
1137 nlsr->in_content.p = &incoming_content;
1138
1139 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1140
1141 char *time_stamp=(char *)malloc(20);
1142 memset(time_stamp,0,20);
1143 get_current_timestamp_micro(time_stamp);
1144 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueed418f32013-01-30 12:25:04 -06001145 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque8fdd6412012-12-04 15:05:33 -06001146 free(time_stamp);
1147
1148 struct hashtb_param param_adj_lsdb = {0};
1149 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1150 struct hashtb_param param_name_lsdb = {0};
1151 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueed418f32013-01-30 12:25:04 -06001152 struct hashtb_param param_cor_lsdb = {0};
1153 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque8fdd6412012-12-04 15:05:33 -06001154
1155
1156
1157
1158 nlsr->is_synch_init=1;
1159 nlsr->nlsa_id=0;
1160 nlsr->adj_build_flag=0;
1161 nlsr->adj_build_count=0;
1162 nlsr->is_build_adj_lsa_sheduled=0;
1163 nlsr->is_send_lsdb_interest_scheduled=0;
1164 nlsr->is_route_calculation_scheduled=0;
1165
1166 nlsr->detailed_logging=0;
1167 nlsr->debugging=0;
1168
1169 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
1170 nlsr->interest_retry = INTEREST_RETRY;
1171 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1172 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1173 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
1174 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
1175 nlsr->semaphor=NLSR_UNLOCKED;
1176
1177 nlsr->api_port=API_PORT;
1178
akmhoque8fdd6412012-12-04 15:05:33 -06001179 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001180 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001181 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1182
1183 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001184 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001185 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1186
akmhoque09c0afa2012-12-14 09:27:00 -06001187 nlsr->is_hyperbolic_calc=0;
akmhoqueed418f32013-01-30 12:25:04 -06001188 nlsr->cor_r=-1.0;
1189 nlsr->cor_theta=-1.0;
akmhoque09c0afa2012-12-14 09:27:00 -06001190
akmhoque8fdd6412012-12-04 15:05:33 -06001191 return 0;
1192}
1193
1194
1195int
1196main(int argc, char *argv[])
1197{
1198 int res, ret;
1199 char *config_file;
1200 int daemon_mode=0;
1201 int port=0;
1202
1203
1204
1205 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1206 {
1207 switch (res)
1208 {
1209 case 'd':
1210 daemon_mode = 1;
1211 break;
1212 case 'f':
1213 config_file = optarg;
1214 break;
1215 case 'p':
1216 port = atoi(optarg);
1217 break;
1218 case 'h':
1219 default:
1220 usage(argv[0]);
1221 }
1222 }
1223
1224 ret=init_nlsr();
1225 ON_ERROR_EXIT(ret);
1226
1227 if ( port !=0 )
1228 nlsr->api_port=port;
1229
1230 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001231
akmhoqueed418f32013-01-30 12:25:04 -06001232 if ( nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0 )
1233 {
1234 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1235 ON_ERROR_DESTROY(-1);
1236 }
akmhoquea37b52c2012-12-14 11:16:36 -06001237 print_adjacent_from_adl();
1238
akmhoque8fdd6412012-12-04 15:05:33 -06001239 if ( daemon_mode == 1 )
1240 {
akmhoquebaf53f12013-01-24 11:06:18 -06001241 nlsr->debugging=0;
akmhoque8fdd6412012-12-04 15:05:33 -06001242 daemonize_nlsr();
1243 }
1244
1245 startLogging(nlsr->logDir);
1246
1247 nlsr->ccn=ccn_create();
1248 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1249 if(ccn_fd == -1)
1250 {
1251 fprintf(stderr,"Could not connect to ccnd\n");
1252 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1253 ON_ERROR_DESTROY(-1);
1254 }
1255
1256 init_api_server(ccn_fd);
1257
akmhoquefc5176d2013-01-18 09:50:12 -06001258 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1259 if(res<0)
1260 {
1261 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1262 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1263 ON_ERROR_DESTROY(res);
1264 }
akmhoque8fdd6412012-12-04 15:05:33 -06001265 struct ccn_charbuf *router_prefix;
1266 router_prefix=ccn_charbuf_create();
1267 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1268 if(res<0)
1269 {
1270 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1271 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1272 ON_ERROR_DESTROY(res);
1273 }
1274
1275 ccn_name_append_str(router_prefix,"nlsr");
1276 nlsr->in_interest.data=nlsr->router_name;
1277 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1278 if ( res < 0 )
1279 {
1280 fprintf(stderr,"Failed to register interest for router\n");
1281 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1282 ON_ERROR_DESTROY(res);
1283 }
1284 ccn_charbuf_destroy(&router_prefix);
1285
1286 if ( nlsr->debugging )
1287 printf("Router Name : %s\n",nlsr->router_name);
1288 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1289 if ( nlsr->debugging )
1290 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1291 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1292
akmhoquea37b52c2012-12-14 11:16:36 -06001293 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001294 print_name_prefix_from_npl();
1295 print_adjacent_from_adl();
1296 build_and_install_name_lsas();
akmhoqueed418f32013-01-30 12:25:04 -06001297 print_name_lsdb();
1298
akmhoque3c0c95d2013-01-31 16:46:24 -06001299 build_and_install_cor_lsa();
akmhoque8fdd6412012-12-04 15:05:33 -06001300
1301 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1302
1303 write_name_lsdb_to_repo(nlsr->slice_prefix);
1304
akmhoque8fdd6412012-12-04 15:05:33 -06001305 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001306 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1307 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001308
1309
1310 while(1)
1311 {
1312 if ( nlsr->semaphor == NLSR_UNLOCKED )
1313 {
1314 if( nlsr->sched != NULL )
1315 {
1316 long int micro_sec=ccn_schedule_run(nlsr->sched);
1317 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1318 ON_ERROR_DESTROY(res);
1319 }
1320 if(nlsr->ccn != NULL)
1321 {
akmhoque719a4fc2013-01-31 16:11:52 -06001322 res = ccn_run(nlsr->ccn, 1);
akmhoque8fdd6412012-12-04 15:05:33 -06001323 }
1324 if (!(nlsr->sched && nlsr->ccn))
1325 {
1326 break;
1327 }
1328 }
1329
1330 }
1331
1332
1333 return 0;
1334}
1335