akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [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> |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 7 | #include <sys/stat.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #ifdef HAVE_CONFIG_H |
| 10 | #include <config.h> |
| 11 | #endif |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 12 | #include <sys/types.h> |
| 13 | #include <signal.h> |
| 14 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 15 | |
| 16 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 17 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 18 | #include <ccn/ccn.h> |
| 19 | #include <ccn/uri.h> |
| 20 | #include <ccn/keystore.h> |
| 21 | #include <ccn/signing.h> |
| 22 | #include <ccn/schedule.h> |
| 23 | #include <ccn/hashtb.h> |
| 24 | |
| 25 | #include "nlsr.h" |
| 26 | #include "nlsr_ndn.h" |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 27 | #include "nlsr_lsdb.h" |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 28 | #include "utility.h" |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 29 | #include "nlsr_npl.h" |
| 30 | #include "nlsr_adl.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 31 | #include "nlsr_npt.h" |
| 32 | #include "nlsr_route.h" |
| 33 | |
| 34 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 35 | #define ON_ERROR_DESTROY(resval) \ |
| 36 | { \ |
| 37 | if ((resval) < 0) { \ |
| 38 | nlsr_destroy(); \ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 39 | exit(1);\ |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 40 | } \ |
| 41 | } |
| 42 | |
| 43 | |
| 44 | #define ON_ERROR_EXIT(resval) \ |
| 45 | { \ |
| 46 | if ((resval) < 0) { \ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 47 | exit(1); \ |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 48 | } \ |
| 49 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 50 | |
| 51 | struct option longopts[] = |
| 52 | { |
| 53 | { "daemon", no_argument, NULL, 'd'}, |
| 54 | { "config_file", required_argument, NULL, 'f'}, |
| 55 | { "help", no_argument, NULL, 'h'}, |
| 56 | { 0 } |
| 57 | }; |
| 58 | |
| 59 | static int |
| 60 | usage(char *progname) |
| 61 | { |
| 62 | |
| 63 | printf("Usage: %s [OPTIONS...]\n\ |
| 64 | NDN routing....\n\ |
| 65 | -d, --daemon Run in daemon mode\n\ |
| 66 | -f, --config_file Specify configuration file name\n\ |
| 67 | -h, --help Display this help message\n", progname); |
| 68 | |
| 69 | exit(1); |
| 70 | } |
| 71 | |
| 72 | void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result) |
| 73 | { |
| 74 | struct timeval now = {0}; |
| 75 | gettimeofday(&now, 0); |
| 76 | result->s = now.tv_sec; |
| 77 | result->micros = now.tv_usec; |
| 78 | } |
| 79 | |
| 80 | static struct ccn_gettime ndn_rtr_ticker = { |
| 81 | "timer", |
| 82 | &ndn_rtr_gettime, |
| 83 | 1000000, |
| 84 | NULL |
| 85 | }; |
| 86 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 87 | void |
| 88 | nlsr_lock(void) |
| 89 | { |
| 90 | nlsr->semaphor=NLSR_LOCKED; |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | nlsr_unlock(void) |
| 95 | { |
| 96 | nlsr->semaphor=NLSR_UNLOCKED; |
| 97 | } |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 98 | |
| 99 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 100 | nlsr_stop_signal_handler(int sig) |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 101 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 102 | signal(sig, SIG_IGN); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 103 | nlsr_destroy(); |
| 104 | exit(0); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 105 | } |
| 106 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 107 | void |
| 108 | daemonize_nlsr(void) |
| 109 | { |
| 110 | int ret; |
| 111 | pid_t process_id = 0; |
| 112 | pid_t sid = 0; |
| 113 | process_id = fork(); |
| 114 | if (process_id < 0) |
| 115 | { |
| 116 | printf("fork failed!\n"); |
| 117 | ON_ERROR_DESTROY(process_id); |
| 118 | } |
| 119 | if (process_id > 0) |
| 120 | { |
| 121 | printf("Process daemonized. Process id: %d \n", process_id); |
| 122 | ret=process_id; |
| 123 | exit(0); |
| 124 | } |
| 125 | |
| 126 | umask(0); |
| 127 | sid = setsid(); |
| 128 | if(sid < 0) |
| 129 | { |
| 130 | ON_ERROR_DESTROY(sid); |
| 131 | } |
| 132 | |
| 133 | chdir("/"); |
| 134 | close(STDIN_FILENO); |
| 135 | close(STDOUT_FILENO); |
| 136 | close(STDERR_FILENO); |
| 137 | } |
| 138 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 139 | void |
| 140 | process_command_ccnneighbor(char *command) |
| 141 | { |
| 142 | if(command==NULL) |
| 143 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 144 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 145 | return; |
| 146 | } |
| 147 | char *rem; |
| 148 | const char *sep=" \t\n"; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 149 | char *rtr_name,*face; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 150 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 151 | rtr_name=strtok_r(command,sep,&rem); |
| 152 | if(rtr_name==NULL) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 153 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 154 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
| 158 | face=strtok_r(NULL,sep,&rem); |
| 159 | if(face==NULL) |
| 160 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 161 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 165 | printf("Router: %s face: %s\n",rtr_name,face); |
| 166 | int face_id; |
| 167 | int res; |
| 168 | res=sscanf(face,"face%d",&face_id); |
| 169 | |
| 170 | if(res != 1 ) |
| 171 | { |
| 172 | printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n"); |
| 173 | return; |
| 174 | } |
| 175 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 176 | if ( rtr_name[strlen(rtr_name)-1] == '/' ) |
| 177 | { |
| 178 | rtr_name[strlen(rtr_name)-1]='\0'; |
| 179 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 180 | struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 181 | nbr->name=(char *)malloc(strlen(rtr_name)+1); |
| 182 | memset(nbr->name,0,strlen(rtr_name)+1); |
| 183 | memcpy(nbr->name,rtr_name,strlen(rtr_name)+1); |
| 184 | nbr->length=strlen(rtr_name)+1; |
| 185 | |
| 186 | add_nbr_to_adl(nbr,face_id); |
| 187 | |
| 188 | free(nbr->name); |
| 189 | free(nbr); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | process_command_ccnname(char *command) |
| 194 | { |
| 195 | |
| 196 | if(command==NULL) |
| 197 | { |
| 198 | printf(" Wrong Command Format ( ccnname /name/prefix)\n"); |
| 199 | return; |
| 200 | } |
| 201 | char *rem; |
| 202 | const char *sep=" \t\n"; |
| 203 | char *name; |
| 204 | name=strtok_r(command,sep,&rem); |
| 205 | if(name==NULL) |
| 206 | { |
| 207 | printf(" Wrong Command Format ( ccnname /name/prefix/ )\n"); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | printf("Name Prefix: %s \n",name); |
| 212 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 213 | if ( name[strlen(name)-1] == '/' ) |
| 214 | name[strlen(name)-1]='\0'; |
| 215 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 216 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 217 | np->name=(char *)malloc(strlen(name)+1); |
| 218 | memset(np->name,0,strlen(name)+1); |
| 219 | memcpy(np->name,name,strlen(name)+1); |
| 220 | np->length=strlen(name)+1; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 221 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 222 | add_name_to_npl(np); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 223 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 224 | free(np->name); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 225 | free(np); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 226 | } |
| 227 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 228 | |
| 229 | void |
| 230 | process_command_router_name(char *command) |
| 231 | { |
| 232 | if(command==NULL) |
| 233 | { |
| 234 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 235 | return; |
| 236 | } |
| 237 | char *rem; |
| 238 | const char *sep=" \t\n"; |
| 239 | char *rtr_name; |
| 240 | |
| 241 | rtr_name=strtok_r(command,sep,&rem); |
| 242 | if(rtr_name==NULL) |
| 243 | { |
| 244 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 249 | if ( rtr_name[strlen(rtr_name)-1] == '/' ) |
| 250 | rtr_name[strlen(rtr_name)-1]='\0'; |
| 251 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 252 | nlsr->router_name=(char *)malloc(strlen(rtr_name)+1); |
| 253 | memset(nlsr->router_name,0,strlen(rtr_name)+1); |
| 254 | memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1); |
| 255 | |
| 256 | |
| 257 | } |
| 258 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 259 | void |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 260 | process_command_lsdb_synch_interval(char *command) |
| 261 | { |
| 262 | if(command==NULL) |
| 263 | { |
| 264 | printf(" Wrong Command Format ( lsdb-synch-interval secs )\n"); |
| 265 | return; |
| 266 | } |
| 267 | char *rem; |
| 268 | const char *sep=" \t\n"; |
| 269 | char *secs; |
| 270 | long int seconds; |
| 271 | |
| 272 | secs=strtok_r(command,sep,&rem); |
| 273 | if(secs==NULL) |
| 274 | { |
| 275 | printf(" Wrong Command Format ( lsdb-synch-interval secs)\n"); |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | seconds=atoi(secs); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 280 | if ( seconds >= 120 && seconds <= 3600 ) |
| 281 | { |
| 282 | nlsr->lsdb_synch_interval=seconds; |
| 283 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 284 | |
| 285 | } |
| 286 | |
| 287 | |
| 288 | void |
| 289 | process_command_interest_retry(char *command) |
| 290 | { |
| 291 | if(command==NULL) |
| 292 | { |
| 293 | printf(" Wrong Command Format ( interest-retry number )\n"); |
| 294 | return; |
| 295 | } |
| 296 | char *rem; |
| 297 | const char *sep=" \t\n"; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 298 | char *retry; |
| 299 | long int retry_number; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 300 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 301 | retry=strtok_r(command,sep,&rem); |
| 302 | if(retry==NULL) |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 303 | { |
| 304 | printf(" Wrong Command Format ( interest-retry number)\n"); |
| 305 | return; |
| 306 | } |
| 307 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 308 | retry_number=atoi(retry); |
| 309 | if ( retry_number >= 1 && retry_number<=10 ) |
| 310 | { |
| 311 | nlsr->interest_retry=retry_number; |
| 312 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 313 | |
| 314 | } |
| 315 | |
| 316 | void |
| 317 | process_command_interest_resend_time(char *command) |
| 318 | { |
| 319 | if(command==NULL) |
| 320 | { |
| 321 | printf(" Wrong Command Format ( interest-resend-time secs )\n"); |
| 322 | return; |
| 323 | } |
| 324 | char *rem; |
| 325 | const char *sep=" \t\n"; |
| 326 | char *secs; |
| 327 | long int seconds; |
| 328 | |
| 329 | secs=strtok_r(command,sep,&rem); |
| 330 | if(secs==NULL) |
| 331 | { |
| 332 | printf(" Wrong Command Format ( interest-resend-time secs)\n"); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | seconds=atoi(secs); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 337 | if ( seconds <= 60 && seconds >= 1 ) |
| 338 | { |
| 339 | nlsr->interest_resend_time=seconds; |
| 340 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 341 | } |
| 342 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 343 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 344 | void |
| 345 | process_command_lsa_refresh_time(char *command) |
| 346 | { |
| 347 | if(command==NULL) |
| 348 | { |
| 349 | printf(" Wrong Command Format ( lsa-refresh-time secs )\n"); |
| 350 | return; |
| 351 | } |
| 352 | char *rem; |
| 353 | const char *sep=" \t\n"; |
| 354 | char *secs; |
| 355 | long int seconds; |
| 356 | |
| 357 | secs=strtok_r(command,sep,&rem); |
| 358 | if(secs==NULL) |
| 359 | { |
| 360 | printf(" Wrong Command Format ( lsa-refresh-time secs)\n"); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | seconds=atoi(secs); |
| 365 | if ( seconds >= 240 && seconds <= 3600 ) |
| 366 | { |
| 367 | nlsr->lsa_refresh_time=seconds; |
| 368 | } |
| 369 | |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | process_command_router_dead_interval(char *command) |
| 374 | { |
| 375 | if(command==NULL) |
| 376 | { |
| 377 | printf(" Wrong Command Format ( router-dead-interval secs )\n"); |
| 378 | return; |
| 379 | } |
| 380 | char *rem; |
| 381 | const char *sep=" \t\n"; |
| 382 | char *secs; |
| 383 | long int seconds; |
| 384 | |
| 385 | secs=strtok_r(command,sep,&rem); |
| 386 | if(secs==NULL) |
| 387 | { |
| 388 | printf(" Wrong Command Format ( router-dead-interval secs)\n"); |
| 389 | return; |
| 390 | } |
| 391 | |
| 392 | seconds=atoi(secs); |
| 393 | if ( seconds >= 360 && seconds <= 5400 ) |
| 394 | { |
| 395 | nlsr->router_dead_interval=seconds; |
| 396 | } |
| 397 | |
| 398 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 399 | |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 400 | void |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 401 | process_command_multi_path_face_num(char *command) |
| 402 | { |
| 403 | if(command==NULL) |
| 404 | { |
| 405 | printf(" Wrong Command Format ( multi-path-face-num n )\n"); |
| 406 | return; |
| 407 | } |
| 408 | char *rem; |
| 409 | const char *sep=" \t\n"; |
| 410 | char *num; |
| 411 | long int number; |
| 412 | |
| 413 | num=strtok_r(command,sep,&rem); |
| 414 | if(num==NULL) |
| 415 | { |
| 416 | printf(" Wrong Command Format ( multi-path-face-num n)\n"); |
| 417 | return; |
| 418 | } |
| 419 | |
| 420 | number=atoi(num); |
| 421 | if ( number >= 0 && number <= 60 ) |
| 422 | { |
| 423 | nlsr->multi_path_face_num=number; |
| 424 | } |
| 425 | |
| 426 | } |
| 427 | |
| 428 | void |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 429 | process_command_logdir(char *command) |
| 430 | { |
| 431 | if(command==NULL) |
| 432 | { |
| 433 | printf(" Wrong Command Format ( logdir /path/to/logdir )\n"); |
| 434 | return; |
| 435 | } |
| 436 | char *rem; |
| 437 | const char *sep=" \t\n"; |
| 438 | char *dir; |
| 439 | |
| 440 | dir=strtok_r(command,sep,&rem); |
| 441 | if(dir==NULL) |
| 442 | { |
| 443 | printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n"); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | nlsr->logDir=(char *)malloc(strlen(dir)+1); |
| 448 | memset(nlsr->logDir,0,strlen(dir)+1); |
| 449 | memcpy(nlsr->logDir,dir,strlen(dir)); |
| 450 | } |
| 451 | |
| 452 | void |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 453 | process_conf_command(char *command) |
| 454 | { |
| 455 | const char *separators=" \t\n"; |
| 456 | char *remainder=NULL; |
| 457 | char *cmd_type=NULL; |
| 458 | |
| 459 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 460 | return; |
| 461 | |
| 462 | cmd_type=strtok_r(command,separators,&remainder); |
| 463 | |
| 464 | if(!strcmp(cmd_type,"router-name") ) |
| 465 | { |
| 466 | process_command_router_name(remainder); |
| 467 | } |
| 468 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 469 | { |
| 470 | process_command_ccnneighbor(remainder); |
| 471 | } |
| 472 | else if(!strcmp(cmd_type,"ccnname") ) |
| 473 | { |
| 474 | process_command_ccnname(remainder); |
| 475 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 476 | else if(!strcmp(cmd_type,"lsdb-synch-interval") ) |
| 477 | { |
| 478 | process_command_lsdb_synch_interval(remainder); |
| 479 | } |
| 480 | else if(!strcmp(cmd_type,"interest-retry") ) |
| 481 | { |
| 482 | process_command_interest_retry(remainder); |
| 483 | } |
| 484 | else if(!strcmp(cmd_type,"interest-resend-time") ) |
| 485 | { |
| 486 | process_command_interest_resend_time(remainder); |
| 487 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 488 | else if(!strcmp(cmd_type,"lsa-refresh-time") ) |
| 489 | { |
| 490 | process_command_lsa_refresh_time(remainder); |
| 491 | } |
| 492 | else if(!strcmp(cmd_type,"router-dead-interval") ) |
| 493 | { |
| 494 | process_command_router_dead_interval(remainder); |
| 495 | } |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 496 | else if(!strcmp(cmd_type,"multi-path-face-num") ) |
| 497 | { |
| 498 | process_command_multi_path_face_num(remainder); |
| 499 | } |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 500 | else if(!strcmp(cmd_type,"logdir") ) |
| 501 | { |
| 502 | process_command_logdir(remainder); |
| 503 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 504 | else |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 505 | { |
| 506 | printf("Wrong configuration Command %s \n",cmd_type); |
| 507 | } |
| 508 | } |
| 509 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 510 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 511 | int |
| 512 | readConfigFile(const char *filename) |
| 513 | { |
| 514 | FILE *cfg; |
| 515 | char buf[1024]; |
| 516 | int len; |
| 517 | |
| 518 | cfg=fopen(filename, "r"); |
| 519 | |
| 520 | if(cfg == NULL) |
| 521 | { |
| 522 | printf("\nConfiguration File does not exists\n"); |
| 523 | exit(1); |
| 524 | } |
| 525 | |
| 526 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 527 | { |
| 528 | len=strlen(buf); |
| 529 | if(buf[len-1] == '\n') |
| 530 | buf[len-1]='\0'; |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 531 | if ( buf[0] != '#' && buf[0] != '!') |
| 532 | process_conf_command(buf); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | fclose(cfg); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 540 | void |
| 541 | nlsr_destroy( void ) |
| 542 | { |
| 543 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 544 | printf("Freeing Allocated Memory....\n"); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 545 | /* Destroying all face created by nlsr in CCND */ |
| 546 | destroy_all_face_by_nlsr(); |
| 547 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 548 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 549 | hashtb_destroy(&nlsr->npl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 550 | hashtb_destroy(&nlsr->adl); |
| 551 | hashtb_destroy(&nlsr->lsdb->name_lsdb); |
| 552 | hashtb_destroy(&nlsr->lsdb->adj_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 553 | hashtb_destroy(&nlsr->pit_alsa); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 554 | |
| 555 | //To Do: has to destroy the face_list one by one |
| 556 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 557 | hashtb_destroy(&nlsr->routing_table); |
| 558 | |
| 559 | |
| 560 | int i, npt_element; |
| 561 | struct npt_entry *ne; |
| 562 | struct hashtb_enumerator ee; |
| 563 | struct hashtb_enumerator *e = ⅇ |
| 564 | hashtb_start(nlsr->npt, e); |
| 565 | npt_element=hashtb_n(nlsr->npt); |
| 566 | for(i=0;i<npt_element;i++) |
| 567 | { |
| 568 | ne=e->data; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 569 | hashtb_destroy(&ne->name_list); |
| 570 | hashtb_destroy(&ne->face_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 571 | hashtb_next(e); |
| 572 | } |
| 573 | |
| 574 | hashtb_end(e); |
| 575 | hashtb_destroy(&nlsr->npt); |
| 576 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 577 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 578 | ccn_schedule_destroy(&nlsr->sched); |
| 579 | ccn_destroy(&nlsr->ccn); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 580 | |
| 581 | free(nlsr->lsdb->lsdb_version); |
| 582 | free(nlsr->lsdb); |
| 583 | free(nlsr->router_name); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 584 | free(nlsr); |
| 585 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 586 | printf("Finished freeing allocated memory\n"); |
| 587 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 588 | } |
| 589 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 590 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 591 | int |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 592 | init_nlsr(void) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 593 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 594 | if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 595 | { |
| 596 | perror("SIGQUIT install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 597 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 598 | } |
| 599 | if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR) |
| 600 | { |
| 601 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 602 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 603 | } |
| 604 | if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 605 | { |
| 606 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 607 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 608 | } |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 609 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 610 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 611 | |
| 612 | struct hashtb_param param_adl = {0}; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 613 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 614 | struct hashtb_param param_npl = {0}; |
| 615 | nlsr->npl = hashtb_create(sizeof(struct name_prefix), ¶m_npl); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 616 | struct hashtb_param param_pit_alsa = {0}; |
akmhoque | 1ce7105 | 2012-09-13 22:51:32 -0500 | [diff] [blame] | 617 | nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 618 | struct hashtb_param param_npt = {0}; |
| 619 | nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt); |
| 620 | struct hashtb_param param_rte = {0}; |
| 621 | nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_rte); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 622 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 623 | nlsr->in_interest.p = &incoming_interest; |
| 624 | nlsr->in_content.p = &incoming_content; |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 625 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 626 | nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase)); |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 627 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 628 | char *time_stamp=(char *)malloc(20); |
| 629 | memset(time_stamp,0,20); |
| 630 | get_current_timestamp_micro(time_stamp); |
| 631 | nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1); |
| 632 | memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp)); |
| 633 | free(time_stamp); |
| 634 | |
| 635 | struct hashtb_param param_adj_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 636 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 637 | struct hashtb_param param_name_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 638 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 639 | |
| 640 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 641 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 642 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 643 | nlsr->is_synch_init=1; |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 644 | nlsr->nlsa_id=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 645 | nlsr->adj_build_flag=0; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 646 | nlsr->adj_build_count=0; |
| 647 | nlsr->is_build_adj_lsa_sheduled=0; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 648 | nlsr->is_send_lsdb_interest_scheduled=0; |
| 649 | nlsr->is_route_calculation_scheduled=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 650 | |
| 651 | nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL; |
| 652 | nlsr->interest_retry = INTEREST_RETRY; |
| 653 | nlsr->interest_resend_time = INTEREST_RESEND_TIME; |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 654 | nlsr->lsa_refresh_time=LSA_REFRESH_TIME; |
| 655 | nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 656 | nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM; |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 657 | |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 658 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 659 | nlsr->semaphor=NLSR_UNLOCKED; |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 660 | |
| 661 | return 0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 662 | } |
| 663 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 664 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 665 | int |
| 666 | main(int argc, char *argv[]) |
| 667 | { |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 668 | int res, ret; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 669 | char *config_file; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 670 | int daemon_mode=0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 671 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 672 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 673 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 674 | while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| 675 | { |
| 676 | switch (res) |
| 677 | { |
| 678 | case 'd': |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 679 | daemon_mode = 1; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 680 | break; |
| 681 | case 'f': |
| 682 | config_file = optarg; |
| 683 | break; |
| 684 | case 'h': |
| 685 | default: |
| 686 | usage(argv[0]); |
| 687 | } |
| 688 | } |
| 689 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 690 | ret=init_nlsr(); |
| 691 | ON_ERROR_EXIT(ret); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 692 | readConfigFile(config_file); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 693 | |
| 694 | if ( daemon_mode == 1 ) |
| 695 | { |
| 696 | daemonize_nlsr(); |
| 697 | } |
| 698 | |
| 699 | startLogging(nlsr->logDir); |
| 700 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 701 | nlsr->ccn=ccn_create(); |
| 702 | if(ccn_connect(nlsr->ccn, NULL) == -1) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 703 | { |
| 704 | fprintf(stderr,"Could not connect to ccnd\n"); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 705 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 706 | ON_ERROR_DESTROY(-1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 707 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 708 | struct ccn_charbuf *router_prefix; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 709 | router_prefix=ccn_charbuf_create(); |
| 710 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 711 | if(res<0) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 712 | { |
| 713 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 714 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 715 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 716 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 717 | |
| 718 | ccn_name_append_str(router_prefix,"nlsr"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 719 | nlsr->in_interest.data=nlsr->router_name; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 720 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 721 | if ( res < 0 ) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 722 | { |
| 723 | fprintf(stderr,"Failed to register interest for router\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 724 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 725 | } |
| 726 | ccn_charbuf_destroy(&router_prefix); |
| 727 | |
| 728 | printf("Router Name : %s\n",nlsr->router_name); |
| 729 | printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 730 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 731 | print_name_prefix_from_npl(); |
| 732 | print_adjacent_from_adl(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 733 | build_and_install_name_lsas(); |
| 734 | print_name_lsdb(); |
| 735 | |
| 736 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 737 | nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 1, &send_info_interest, NULL, 0); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 738 | nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 739 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 740 | while(1) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 741 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 742 | if ( nlsr->semaphor == NLSR_UNLOCKED ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 743 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 744 | if( nlsr->sched != NULL ) |
| 745 | { |
| 746 | ccn_schedule_run(nlsr->sched); |
| 747 | } |
| 748 | if(nlsr->ccn != NULL) |
| 749 | { |
| 750 | res = ccn_run(nlsr->ccn, 500); |
| 751 | } |
| 752 | if (!(nlsr->sched && nlsr->ccn)) |
| 753 | { |
| 754 | break; |
| 755 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 756 | } |
| 757 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 758 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 759 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 760 | return 0; |
| 761 | } |
| 762 | |