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