blob: c65eccd75d4c7954ac471f267606fb5a0d86b10e [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;
943 if ( time_out_micro_sec < 0 )
944 {
945 timeout.tv_sec=1;
946 timeout.tv_usec=0;
947 }
948 else
949 {
950 time_out_micro_sec=(long int)time_out_micro_sec*0.4;
951 timeout.tv_sec=time_out_micro_sec / 1000000;
952 timeout.tv_usec=time_out_micro_sec % 1000000;
953 }
954
Adam Alyyanf1f188f2013-01-14 11:57:08 -0600955 timeout.tv_sec = 0;
956 timeout.tv_usec = 499000;
akmhoque8fdd6412012-12-04 15:05:33 -0600957
958 int fd;
959 int nread;
960 int result;
961 fd_set testfds;
962 unsigned int client_len;
963 int client_sockfd;
964 char recv_buffer[1024];
965 bzero(recv_buffer,1024);
966 struct sockaddr_in client_address;
967
968 testfds=nlsr->readfds;
969 result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout);
970
soamin29b8e392013-01-22 17:12:07 -0600971 for(fd = 0; fd < FD_SETSIZE && result > 0; fd++)
akmhoque8fdd6412012-12-04 15:05:33 -0600972 {
973 if(FD_ISSET(fd,&testfds))
974 {
975 if ( fd == ccn_fd )
976 {
977 return 0;
978 }
979 else if(fd == nlsr->nlsr_api_server_sock_fd)
980 {
981 client_len = sizeof(client_address);
982 client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len);
983 FD_SET(client_sockfd, &nlsr->readfds);
984 }
985 else
986 {
987
988 ioctl(fd, FIONREAD, &nread);
989 if(nread == 0)
990 {
991 close(fd);
992 FD_CLR(fd, &nlsr->readfds);
993 }
994 else
995 {
996 recv(fd, recv_buffer, 1024, 0);
997 printf("Received Data from NLSR API cleint: %s \n",recv_buffer);
998 char *msg=process_api_client_command(recv_buffer);
999 send(fd, msg, strlen(msg),0);
1000 free(msg);
1001 close(fd);
1002 FD_CLR(fd, &nlsr->readfds);
1003 }
1004 }
1005 }
1006 }
1007
1008 return 0;
1009}
1010
1011void
1012nlsr_destroy( void )
1013{
1014 if ( nlsr->debugging )
1015 {
1016 printf("Freeing Allocated Memory....\n");
1017 }
1018 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n");
1019 /* Destroying all face created by nlsr in CCND */
1020 destroy_all_face_by_nlsr();
akmhoque61a57f22013-01-18 07:44:05 -06001021 destroy_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001022 /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */
1023 hashtb_destroy(&nlsr->npl);
1024 hashtb_destroy(&nlsr->adl);
1025 hashtb_destroy(&nlsr->lsdb->name_lsdb);
1026 hashtb_destroy(&nlsr->lsdb->adj_lsdb);
1027 hashtb_destroy(&nlsr->pit_alsa);
1028
akmhoqueed418f32013-01-30 12:25:04 -06001029
akmhoque8fdd6412012-12-04 15:05:33 -06001030
1031 hashtb_destroy(&nlsr->routing_table);
1032
1033
1034 int i, npt_element;
1035 struct npt_entry *ne;
1036 struct hashtb_enumerator ee;
1037 struct hashtb_enumerator *e = &ee;
1038 hashtb_start(nlsr->npt, e);
1039 npt_element=hashtb_n(nlsr->npt);
1040 for(i=0;i<npt_element;i++)
1041 {
1042 ne=e->data;
1043 hashtb_destroy(&ne->name_list);
1044 hashtb_destroy(&ne->face_list);
1045 hashtb_next(e);
1046 }
1047
1048 hashtb_end(e);
1049 hashtb_destroy(&nlsr->npt);
1050
1051
1052 ccns_close(&nlsr->ccns, NULL, NULL);
1053 ccns_slice_destroy(&nlsr->slice);
1054
1055
1056
1057 close(nlsr->nlsr_api_server_sock_fd);
1058
1059 ccn_schedule_destroy(&nlsr->sched);
1060 ccn_destroy(&nlsr->ccn);
1061
1062 free(nlsr->lsdb->lsdb_version);
1063 free(nlsr->lsdb);
1064 free(nlsr->router_name);
1065 free(nlsr);
1066 if ( nlsr->debugging )
1067 {
1068 printf("Finished freeing allocated memory\n");
1069 }
1070 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n");
1071
1072}
1073
1074
1075
1076void
1077init_api_server(int ccn_fd)
1078{
1079 int server_sockfd;
1080 int server_len;
1081 struct sockaddr_in server_address;
1082 unsigned int yes=1;
1083
1084 server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
1085
1086 int flags = fcntl(server_sockfd, F_GETFL, 0);
1087 fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags);
1088
1089 if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0)
1090 {
1091 ON_ERROR_DESTROY(-1);
1092 }
1093
1094 server_address.sin_family = AF_INET;
Adam Alyyanc350e3a2013-01-14 14:35:57 -06001095 server_address.sin_addr.s_addr = INADDR_ANY;
1096 server_address.sin_port = htons(nlsr->api_port);
akmhoque8fdd6412012-12-04 15:05:33 -06001097
1098 server_len = sizeof(server_address);
1099 bind(server_sockfd, (struct sockaddr *)&server_address, server_len);
1100 listen(server_sockfd, 100);
1101 FD_ZERO(&nlsr->readfds);
1102 FD_SET(server_sockfd, &nlsr->readfds);
1103 FD_SET(ccn_fd, &nlsr->readfds);
1104 nlsr->nlsr_api_server_sock_fd=server_sockfd;
1105
1106}
1107
1108int
1109init_nlsr(void)
1110{
1111 if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR)
1112 {
1113 perror("SIGQUIT install error\n");
1114 return -1;
1115 }
1116 if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR)
1117 {
1118 perror("SIGTERM install error\n");
1119 return -1;
1120 }
1121 if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR)
1122 {
1123 perror("SIGTERM install error\n");
1124 return -1;
1125 }
1126
1127 nlsr=(struct nlsr *)malloc(sizeof(struct nlsr));
1128
1129 struct hashtb_param param_adl = {0};
1130 nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), &param_adl);
1131 struct hashtb_param param_npl = {0};
1132 nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), &param_npl);
1133 struct hashtb_param param_pit_alsa = {0};
1134 nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), &param_pit_alsa);
1135 struct hashtb_param param_npt = {0};
1136 nlsr->npt = hashtb_create(sizeof(struct npt_entry), &param_npt);
1137 struct hashtb_param param_rte = {0};
1138 nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), &param_rte);
1139
1140 nlsr->in_interest.p = &incoming_interest;
1141 nlsr->in_content.p = &incoming_content;
1142
1143 nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase));
1144
1145 char *time_stamp=(char *)malloc(20);
1146 memset(time_stamp,0,20);
1147 get_current_timestamp_micro(time_stamp);
1148 nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1);
akmhoqueed418f32013-01-30 12:25:04 -06001149 memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp));
akmhoque8fdd6412012-12-04 15:05:33 -06001150 free(time_stamp);
1151
1152 struct hashtb_param param_adj_lsdb = {0};
1153 nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), &param_adj_lsdb);
1154 struct hashtb_param param_name_lsdb = {0};
1155 nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), &param_name_lsdb);
akmhoqueed418f32013-01-30 12:25:04 -06001156 struct hashtb_param param_cor_lsdb = {0};
1157 nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), &param_cor_lsdb);
akmhoque8fdd6412012-12-04 15:05:33 -06001158
1159
1160
1161
1162 nlsr->is_synch_init=1;
1163 nlsr->nlsa_id=0;
1164 nlsr->adj_build_flag=0;
1165 nlsr->adj_build_count=0;
1166 nlsr->is_build_adj_lsa_sheduled=0;
1167 nlsr->is_send_lsdb_interest_scheduled=0;
1168 nlsr->is_route_calculation_scheduled=0;
1169
1170 nlsr->detailed_logging=0;
1171 nlsr->debugging=0;
1172
1173 nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL;
1174 nlsr->interest_retry = INTEREST_RETRY;
1175 nlsr->interest_resend_time = INTEREST_RESEND_TIME;
1176 nlsr->lsa_refresh_time=LSA_REFRESH_TIME;
1177 nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL;
1178 nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM;
1179 nlsr->semaphor=NLSR_UNLOCKED;
1180
1181 nlsr->api_port=API_PORT;
1182
akmhoque8fdd6412012-12-04 15:05:33 -06001183 nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001184 memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001185 memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr"));
1186
1187 nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1);
Syed Obaid Amin4c959562012-12-21 16:43:21 -06001188 memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1);
akmhoque8fdd6412012-12-04 15:05:33 -06001189 memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA"));
1190
akmhoque09c0afa2012-12-14 09:27:00 -06001191 nlsr->is_hyperbolic_calc=0;
akmhoqueed418f32013-01-30 12:25:04 -06001192 nlsr->cor_r=-1.0;
1193 nlsr->cor_theta=-1.0;
akmhoque09c0afa2012-12-14 09:27:00 -06001194
akmhoque8fdd6412012-12-04 15:05:33 -06001195 return 0;
1196}
1197
1198
1199int
1200main(int argc, char *argv[])
1201{
1202 int res, ret;
1203 char *config_file;
1204 int daemon_mode=0;
1205 int port=0;
1206
1207
1208
1209 while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1)
1210 {
1211 switch (res)
1212 {
1213 case 'd':
1214 daemon_mode = 1;
1215 break;
1216 case 'f':
1217 config_file = optarg;
1218 break;
1219 case 'p':
1220 port = atoi(optarg);
1221 break;
1222 case 'h':
1223 default:
1224 usage(argv[0]);
1225 }
1226 }
1227
1228 ret=init_nlsr();
1229 ON_ERROR_EXIT(ret);
1230
1231 if ( port !=0 )
1232 nlsr->api_port=port;
1233
1234 readConfigFile(config_file);
akmhoque09c0afa2012-12-14 09:27:00 -06001235
akmhoqueed418f32013-01-30 12:25:04 -06001236 if ( nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0 )
1237 {
1238 fprintf(stderr,"Hyperbolic codinate has not been defined :(\n");
1239 ON_ERROR_DESTROY(-1);
1240 }
akmhoquea37b52c2012-12-14 11:16:36 -06001241 print_adjacent_from_adl();
1242
akmhoque8fdd6412012-12-04 15:05:33 -06001243 if ( daemon_mode == 1 )
1244 {
akmhoquebaf53f12013-01-24 11:06:18 -06001245 nlsr->debugging=0;
akmhoque8fdd6412012-12-04 15:05:33 -06001246 daemonize_nlsr();
1247 }
1248
1249 startLogging(nlsr->logDir);
1250
1251 nlsr->ccn=ccn_create();
1252 int ccn_fd=ccn_connect(nlsr->ccn, NULL);
1253 if(ccn_fd == -1)
1254 {
1255 fprintf(stderr,"Could not connect to ccnd\n");
1256 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n");
1257 ON_ERROR_DESTROY(-1);
1258 }
1259
1260 init_api_server(ccn_fd);
1261
akmhoquefc5176d2013-01-18 09:50:12 -06001262 res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix);
1263 if(res<0)
1264 {
1265 fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix);
1266 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix);
1267 ON_ERROR_DESTROY(res);
1268 }
akmhoque8fdd6412012-12-04 15:05:33 -06001269 struct ccn_charbuf *router_prefix;
1270 router_prefix=ccn_charbuf_create();
1271 res=ccn_name_from_uri(router_prefix,nlsr->router_name);
1272 if(res<0)
1273 {
1274 fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name);
1275 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name);
1276 ON_ERROR_DESTROY(res);
1277 }
1278
1279 ccn_name_append_str(router_prefix,"nlsr");
1280 nlsr->in_interest.data=nlsr->router_name;
1281 res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest);
1282 if ( res < 0 )
1283 {
1284 fprintf(stderr,"Failed to register interest for router\n");
1285 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n");
1286 ON_ERROR_DESTROY(res);
1287 }
1288 ccn_charbuf_destroy(&router_prefix);
1289
1290 if ( nlsr->debugging )
1291 printf("Router Name : %s\n",nlsr->router_name);
1292 writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name);
1293 if ( nlsr->debugging )
1294 printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1295 writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version);
1296
akmhoquea37b52c2012-12-14 11:16:36 -06001297 add_faces_for_nbrs();
akmhoque8fdd6412012-12-04 15:05:33 -06001298 print_name_prefix_from_npl();
1299 print_adjacent_from_adl();
1300 build_and_install_name_lsas();
akmhoqueed418f32013-01-30 12:25:04 -06001301 print_name_lsdb();
1302
1303 build_and_install_cor_lsa();
1304 //print_cor_lsdb();
akmhoque8fdd6412012-12-04 15:05:33 -06001305
1306 sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix);
1307
1308 write_name_lsdb_to_repo(nlsr->slice_prefix);
1309
akmhoque8fdd6412012-12-04 15:05:33 -06001310 nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker);
akmhoque4ae16942012-12-10 11:50:43 -06001311 nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0);
1312 nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0);
akmhoque8fdd6412012-12-04 15:05:33 -06001313
1314
1315 while(1)
1316 {
1317 if ( nlsr->semaphor == NLSR_UNLOCKED )
1318 {
1319 if( nlsr->sched != NULL )
1320 {
1321 long int micro_sec=ccn_schedule_run(nlsr->sched);
1322 res=nlsr_api_server_poll(micro_sec,ccn_fd);
1323 ON_ERROR_DESTROY(res);
1324 }
1325 if(nlsr->ccn != NULL)
1326 {
1327 res = ccn_run(nlsr->ccn, 0);
1328 }
1329 if (!(nlsr->sched && nlsr->ccn))
1330 {
1331 break;
1332 }
1333 }
1334
1335 }
1336
1337
1338 return 0;
1339}
1340