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" |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 41 | #include "nlsr_face.h" |
| 42 | #include "nlsr_fib.h" |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 43 | |
| 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 | |
| 61 | struct 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 | |
| 70 | static int |
| 71 | usage(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 | |
| 84 | void 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 | |
| 92 | static struct ccn_gettime ndn_rtr_ticker = { |
| 93 | "timer", |
| 94 | &ndn_rtr_gettime, |
| 95 | 1000000, |
| 96 | NULL |
| 97 | }; |
| 98 | |
| 99 | void |
| 100 | nlsr_lock(void) |
| 101 | { |
| 102 | nlsr->semaphor=NLSR_LOCKED; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | nlsr_unlock(void) |
| 107 | { |
| 108 | nlsr->semaphor=NLSR_UNLOCKED; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | nlsr_stop_signal_handler(int sig) |
| 113 | { |
| 114 | signal(sig, SIG_IGN); |
| 115 | nlsr_destroy(); |
| 116 | exit(0); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | daemonize_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 | |
| 151 | void |
| 152 | process_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"; |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 161 | char *rtr_name; |
| 162 | //char *face; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 163 | |
| 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 | |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 171 | /*face=strtok_r(NULL,sep,&rem); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 172 | 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; |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 187 | }*/ |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 188 | |
| 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 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 199 | //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 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 205 | |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 206 | char *ip_addr=(char *)malloc(13); |
Syed Obaid Amin | 4c95956 | 2012-12-21 16:43:21 -0600 | [diff] [blame] | 207 | memset(ip_addr,0,13); |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 208 | 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); |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 212 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 213 | |
| 214 | free(nbr->name); |
| 215 | free(nbr); |
| 216 | } |
| 217 | |
| 218 | void |
| 219 | process_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 | |
| 255 | void |
| 256 | process_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 | |
| 285 | void |
| 286 | process_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 | |
| 314 | void |
| 315 | process_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 | |
| 342 | void |
| 343 | process_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 | |
| 370 | void |
| 371 | process_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 | |
| 398 | void |
| 399 | process_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 | |
| 426 | void |
| 427 | process_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 | |
| 454 | void |
| 455 | process_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 | |
| 478 | void |
| 479 | process_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 | |
| 503 | void |
| 504 | process_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 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 528 | |
| 529 | void |
| 530 | process_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); |
akmhoque | b29edd8 | 2013-01-14 20:54:11 -0600 | [diff] [blame] | 551 | if ( topo_prefix[strlen(topo_prefix)-1] == '/' ) |
| 552 | topo_prefix[strlen(topo_prefix)-1]='\0'; |
| 553 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 554 | nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1); |
Syed Obaid Amin | 7a176c6 | 2012-12-21 15:56:00 -0600 | [diff] [blame] | 555 | memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1); |
| 556 | puts(topo_prefix); |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 557 | memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix)); |
| 558 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
| 562 | |
| 563 | void |
| 564 | process_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); |
akmhoque | b29edd8 | 2013-01-14 20:54:11 -0600 | [diff] [blame] | 585 | if ( slice_prefix[strlen(slice_prefix)-1] == '/' ) |
| 586 | slice_prefix[strlen(slice_prefix)-1]='\0'; |
| 587 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 588 | nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1); |
Syed Obaid Amin | 4c95956 | 2012-12-21 16:43:21 -0600 | [diff] [blame] | 589 | memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1); |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 590 | memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix)); |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | void |
| 595 | process_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 | |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 619 | void |
| 620 | process_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 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 652 | void |
| 653 | process_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 | } |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 712 | 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 | } |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 720 | else if(!strcmp(cmd_type,"hyperbolic-cordinate") ) |
| 721 | { |
| 722 | process_command_hyperbolic_cordinate(remainder); |
| 723 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 724 | else |
| 725 | { |
| 726 | printf("Wrong configuration Command %s \n",cmd_type); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | |
| 731 | int |
| 732 | readConfigFile(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 | |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 760 | |
| 761 | void |
| 762 | add_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 = ⅇ |
| 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); |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 779 | hashtb_next(e); |
| 780 | } |
| 781 | |
| 782 | hashtb_end(e); |
| 783 | |
| 784 | } |
| 785 | |
akmhoque | 61a57f2 | 2013-01-18 07:44:05 -0600 | [diff] [blame] | 786 | void |
| 787 | destroy_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 = ⅇ |
| 794 | |
| 795 | hashtb_start(nlsr->adl, e); |
| 796 | adl_element=hashtb_n(nlsr->adl); |
| 797 | |
| 798 | for(i=0;i<adl_element;i++) |
| 799 | { |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 800 | 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 | } |
akmhoque | 61a57f2 | 2013-01-18 07:44:05 -0600 | [diff] [blame] | 806 | hashtb_next(e); |
| 807 | } |
| 808 | |
| 809 | hashtb_end(e); |
| 810 | |
| 811 | } |
| 812 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 813 | char * |
| 814 | process_api_client_command(char *command) |
| 815 | { |
| 816 | char *msg; |
| 817 | msg=(char *)malloc(100); |
Syed Obaid Amin | 4c95956 | 2012-12-21 16:43:21 -0600 | [diff] [blame] | 818 | memset(msg,0,100); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 819 | |
| 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 | { |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 911 | 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 | |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 915 | char *ip_addr=(char *)malloc(13); |
Syed Obaid Amin | 4c95956 | 2012-12-21 16:43:21 -0600 | [diff] [blame] | 916 | memset(ip_addr,0,13); |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 917 | 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 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 925 | sprintf(msg,"Neighbor %s has been added to adjacency list.",name); |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 926 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 927 | } |
| 928 | else |
| 929 | { |
| 930 | sprintf(msg,"Neighbor %s already exists in adjacency list.",name); |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | |
| 936 | return msg; |
| 937 | } |
| 938 | |
| 939 | int |
| 940 | nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd) |
| 941 | { |
| 942 | struct timeval timeout; |
akmhoque | 21ffec2 | 2013-01-31 18:22:10 -0600 | [diff] [blame^] | 943 | if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 ) |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 944 | { |
akmhoque | 21ffec2 | 2013-01-31 18:22:10 -0600 | [diff] [blame^] | 945 | timeout.tv_sec=0; |
| 946 | timeout.tv_usec=time_out_micro_sec; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 947 | } |
akmhoque | 21ffec2 | 2013-01-31 18:22:10 -0600 | [diff] [blame^] | 948 | else |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 949 | { |
akmhoque | 21ffec2 | 2013-01-31 18:22:10 -0600 | [diff] [blame^] | 950 | timeout.tv_sec = 0; |
| 951 | timeout.tv_usec = 500000; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 952 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 953 | |
| 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 | |
soamin | 29b8e39 | 2013-01-22 17:12:07 -0600 | [diff] [blame] | 967 | for(fd = 0; fd < FD_SETSIZE && result > 0; fd++) |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 968 | { |
| 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 | |
| 1007 | void |
| 1008 | nlsr_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(); |
akmhoque | 61a57f2 | 2013-01-18 07:44:05 -0600 | [diff] [blame] | 1017 | destroy_faces_for_nbrs(); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1018 | /* 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 | |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 1025 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1026 | |
| 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 = ⅇ |
| 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 | |
| 1072 | void |
| 1073 | init_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 Alyyan | c350e3a | 2013-01-14 14:35:57 -0600 | [diff] [blame] | 1091 | server_address.sin_addr.s_addr = INADDR_ANY; |
| 1092 | server_address.sin_port = htons(nlsr->api_port); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1093 | |
| 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 | |
| 1104 | int |
| 1105 | init_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), ¶m_adl); |
| 1127 | struct hashtb_param param_npl = {0}; |
| 1128 | nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), ¶m_npl); |
| 1129 | struct hashtb_param param_pit_alsa = {0}; |
| 1130 | nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa); |
| 1131 | struct hashtb_param param_npt = {0}; |
| 1132 | nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt); |
| 1133 | struct hashtb_param param_rte = {0}; |
| 1134 | nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_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); |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 1145 | memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp)); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1146 | free(time_stamp); |
| 1147 | |
| 1148 | struct hashtb_param param_adj_lsdb = {0}; |
| 1149 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
| 1150 | struct hashtb_param param_name_lsdb = {0}; |
| 1151 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 1152 | struct hashtb_param param_cor_lsdb = {0}; |
| 1153 | nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), ¶m_cor_lsdb); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1154 | |
| 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 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1179 | nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1); |
Syed Obaid Amin | 4c95956 | 2012-12-21 16:43:21 -0600 | [diff] [blame] | 1180 | memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1181 | 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 Amin | 4c95956 | 2012-12-21 16:43:21 -0600 | [diff] [blame] | 1184 | memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1185 | memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA")); |
| 1186 | |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 1187 | nlsr->is_hyperbolic_calc=0; |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 1188 | nlsr->cor_r=-1.0; |
| 1189 | nlsr->cor_theta=-1.0; |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 1190 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1191 | return 0; |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | int |
| 1196 | main(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); |
akmhoque | 09c0afa | 2012-12-14 09:27:00 -0600 | [diff] [blame] | 1231 | |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 1232 | 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 | } |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 1237 | print_adjacent_from_adl(); |
| 1238 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1239 | if ( daemon_mode == 1 ) |
| 1240 | { |
akmhoque | baf53f1 | 2013-01-24 11:06:18 -0600 | [diff] [blame] | 1241 | nlsr->debugging=0; |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1242 | 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 | |
akmhoque | fc5176d | 2013-01-18 09:50:12 -0600 | [diff] [blame] | 1258 | 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 | } |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1265 | 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 | |
akmhoque | a37b52c | 2012-12-14 11:16:36 -0600 | [diff] [blame] | 1293 | add_faces_for_nbrs(); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1294 | print_name_prefix_from_npl(); |
| 1295 | print_adjacent_from_adl(); |
| 1296 | build_and_install_name_lsas(); |
akmhoque | ed418f3 | 2013-01-30 12:25:04 -0600 | [diff] [blame] | 1297 | print_name_lsdb(); |
| 1298 | |
akmhoque | 3c0c95d | 2013-01-31 16:46:24 -0600 | [diff] [blame] | 1299 | build_and_install_cor_lsa(); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1300 | |
| 1301 | sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix); |
| 1302 | |
| 1303 | write_name_lsdb_to_repo(nlsr->slice_prefix); |
| 1304 | |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1305 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
akmhoque | 4ae1694 | 2012-12-10 11:50:43 -0600 | [diff] [blame] | 1306 | 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); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1308 | |
| 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 | { |
akmhoque | 719a4fc | 2013-01-31 16:11:52 -0600 | [diff] [blame] | 1322 | res = ccn_run(nlsr->ccn, 1); |
akmhoque | 8fdd641 | 2012-12-04 15:05:33 -0600 | [diff] [blame] | 1323 | } |
| 1324 | if (!(nlsr->sched && nlsr->ccn)) |
| 1325 | { |
| 1326 | break; |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | } |
| 1331 | |
| 1332 | |
| 1333 | return 0; |
| 1334 | } |
| 1335 | |