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