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 | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 600 | |
| 601 | int |
| 602 | nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd) |
| 603 | { |
| 604 | struct timeval timeout; |
| 605 | if ( time_out_micro_sec < 0 ) |
| 606 | { |
| 607 | timeout.tv_sec=1; |
| 608 | timeout.tv_usec=0; |
| 609 | } |
| 610 | else |
| 611 | { |
| 612 | time_out_micro_sec=(long int)time_out_micro_sec*0.4; |
| 613 | timeout.tv_sec=time_out_micro_sec / 1000000; |
| 614 | timeout.tv_usec=time_out_micro_sec % 1000000; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | int fd; |
| 619 | int nread; |
| 620 | int result; |
| 621 | fd_set testfds; |
| 622 | unsigned int client_len; |
| 623 | int client_sockfd; |
| 624 | char recv_buffer[1024]; |
| 625 | bzero(recv_buffer,1024); |
| 626 | struct sockaddr_un client_address; |
| 627 | |
| 628 | testfds=nlsr->readfds; |
| 629 | result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout); |
| 630 | |
| 631 | for(fd = 0; fd < FD_SETSIZE; fd++) |
| 632 | { |
| 633 | if(FD_ISSET(fd,&testfds)) |
| 634 | { |
| 635 | if ( fd == ccn_fd ) |
| 636 | { |
| 637 | return 0; |
| 638 | } |
| 639 | else if(fd == nlsr->nlsr_api_server_sock_fd) |
| 640 | { |
| 641 | client_len = sizeof(client_address); |
| 642 | client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len); |
| 643 | FD_SET(client_sockfd, &nlsr->readfds); |
| 644 | } |
| 645 | else |
| 646 | { |
| 647 | |
| 648 | ioctl(fd, FIONREAD, &nread); |
| 649 | if(nread == 0) |
| 650 | { |
| 651 | close(fd); |
| 652 | FD_CLR(fd, &nlsr->readfds); |
| 653 | } |
| 654 | else |
| 655 | { |
| 656 | recv(fd, recv_buffer, 1024, 0); |
| 657 | printf("Received Data from NLSR API cleint: %s \n",recv_buffer); |
| 658 | close(fd); |
| 659 | FD_CLR(fd, &nlsr->readfds); |
| 660 | free(recv_buffer); |
| 661 | } |
| 662 | } |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | return 0; |
| 667 | } |
| 668 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 669 | void |
| 670 | nlsr_destroy( void ) |
| 671 | { |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 672 | if ( nlsr->debugging ) |
| 673 | { |
| 674 | printf("Freeing Allocated Memory....\n"); |
| 675 | } |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 676 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n"); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 677 | /* Destroying all face created by nlsr in CCND */ |
| 678 | destroy_all_face_by_nlsr(); |
| 679 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 680 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 681 | hashtb_destroy(&nlsr->npl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 682 | hashtb_destroy(&nlsr->adl); |
| 683 | hashtb_destroy(&nlsr->lsdb->name_lsdb); |
| 684 | hashtb_destroy(&nlsr->lsdb->adj_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 685 | hashtb_destroy(&nlsr->pit_alsa); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 686 | |
| 687 | //To Do: has to destroy the face_list one by one |
| 688 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 689 | hashtb_destroy(&nlsr->routing_table); |
| 690 | |
| 691 | |
| 692 | int i, npt_element; |
| 693 | struct npt_entry *ne; |
| 694 | struct hashtb_enumerator ee; |
| 695 | struct hashtb_enumerator *e = ⅇ |
| 696 | hashtb_start(nlsr->npt, e); |
| 697 | npt_element=hashtb_n(nlsr->npt); |
| 698 | for(i=0;i<npt_element;i++) |
| 699 | { |
| 700 | ne=e->data; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 701 | hashtb_destroy(&ne->name_list); |
| 702 | hashtb_destroy(&ne->face_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 703 | hashtb_next(e); |
| 704 | } |
| 705 | |
| 706 | hashtb_end(e); |
| 707 | hashtb_destroy(&nlsr->npt); |
| 708 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 709 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 710 | ccn_schedule_destroy(&nlsr->sched); |
| 711 | ccn_destroy(&nlsr->ccn); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 712 | |
| 713 | free(nlsr->lsdb->lsdb_version); |
| 714 | free(nlsr->lsdb); |
| 715 | free(nlsr->router_name); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 716 | free(nlsr); |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 717 | if ( nlsr->debugging ) |
| 718 | { |
| 719 | printf("Finished freeing allocated memory\n"); |
| 720 | } |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 721 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 722 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 723 | } |
| 724 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 725 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 726 | void |
| 727 | init_api_server(int ccn_fd) |
| 728 | { |
| 729 | int server_sockfd; |
| 730 | int server_len; |
| 731 | struct sockaddr_un server_address; |
| 732 | |
| 733 | unlink("/tmp/nlsr_api_server_socket"); |
| 734 | server_sockfd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 735 | |
| 736 | int flags = fcntl(server_sockfd, F_GETFL, 0); |
| 737 | fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags); |
| 738 | |
| 739 | server_address.sun_family = AF_UNIX; |
| 740 | strcpy(server_address.sun_path, "/tmp/nlsr_api_server_socket"); |
| 741 | server_len = sizeof(server_address); |
| 742 | bind(server_sockfd, (struct sockaddr *)&server_address, server_len); |
| 743 | listen(server_sockfd, 100); |
| 744 | FD_ZERO(&nlsr->readfds); |
| 745 | FD_SET(server_sockfd, &nlsr->readfds); |
| 746 | FD_SET(ccn_fd, &nlsr->readfds); |
| 747 | nlsr->nlsr_api_server_sock_fd=server_sockfd; |
| 748 | |
| 749 | } |
| 750 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 751 | int |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 752 | init_nlsr(void) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 753 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 754 | if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 755 | { |
| 756 | perror("SIGQUIT install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 757 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 758 | } |
| 759 | if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR) |
| 760 | { |
| 761 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 762 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 763 | } |
| 764 | if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 765 | { |
| 766 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 767 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 768 | } |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 769 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 770 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 771 | |
| 772 | struct hashtb_param param_adl = {0}; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 773 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 774 | struct hashtb_param param_npl = {0}; |
| 775 | nlsr->npl = hashtb_create(sizeof(struct name_prefix), ¶m_npl); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 776 | struct hashtb_param param_pit_alsa = {0}; |
akmhoque | 1ce7105 | 2012-09-13 22:51:32 -0500 | [diff] [blame] | 777 | nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 778 | struct hashtb_param param_npt = {0}; |
| 779 | nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt); |
| 780 | struct hashtb_param param_rte = {0}; |
| 781 | nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_rte); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 782 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 783 | nlsr->in_interest.p = &incoming_interest; |
| 784 | nlsr->in_content.p = &incoming_content; |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 785 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 786 | nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase)); |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 787 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 788 | char *time_stamp=(char *)malloc(20); |
| 789 | memset(time_stamp,0,20); |
| 790 | get_current_timestamp_micro(time_stamp); |
| 791 | nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1); |
| 792 | memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp)); |
| 793 | free(time_stamp); |
| 794 | |
| 795 | struct hashtb_param param_adj_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 796 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 797 | struct hashtb_param param_name_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 798 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 799 | |
| 800 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 801 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 802 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 803 | nlsr->is_synch_init=1; |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 804 | nlsr->nlsa_id=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 805 | nlsr->adj_build_flag=0; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 806 | nlsr->adj_build_count=0; |
| 807 | nlsr->is_build_adj_lsa_sheduled=0; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 808 | nlsr->is_send_lsdb_interest_scheduled=0; |
| 809 | nlsr->is_route_calculation_scheduled=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 810 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 811 | nlsr->detailed_logging=0; |
| 812 | nlsr->debugging=0; |
| 813 | |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 814 | nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL; |
| 815 | nlsr->interest_retry = INTEREST_RETRY; |
| 816 | nlsr->interest_resend_time = INTEREST_RESEND_TIME; |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 817 | nlsr->lsa_refresh_time=LSA_REFRESH_TIME; |
| 818 | nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 819 | nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 820 | nlsr->semaphor=NLSR_UNLOCKED; |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 821 | |
| 822 | return 0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 823 | } |
| 824 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 825 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 826 | int |
| 827 | main(int argc, char *argv[]) |
| 828 | { |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 829 | int res, ret; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 830 | char *config_file; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 831 | int daemon_mode=0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 832 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 833 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 834 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 835 | while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| 836 | { |
| 837 | switch (res) |
| 838 | { |
| 839 | case 'd': |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 840 | daemon_mode = 1; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 841 | break; |
| 842 | case 'f': |
| 843 | config_file = optarg; |
| 844 | break; |
| 845 | case 'h': |
| 846 | default: |
| 847 | usage(argv[0]); |
| 848 | } |
| 849 | } |
| 850 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 851 | ret=init_nlsr(); |
| 852 | ON_ERROR_EXIT(ret); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 853 | readConfigFile(config_file); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 854 | |
| 855 | if ( daemon_mode == 1 ) |
| 856 | { |
| 857 | daemonize_nlsr(); |
| 858 | } |
| 859 | |
| 860 | startLogging(nlsr->logDir); |
| 861 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 862 | nlsr->ccn=ccn_create(); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 863 | int ccn_fd=ccn_connect(nlsr->ccn, NULL); |
| 864 | if(ccn_fd == -1) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 865 | { |
| 866 | fprintf(stderr,"Could not connect to ccnd\n"); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 867 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 868 | ON_ERROR_DESTROY(-1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 869 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 870 | |
| 871 | init_api_server(ccn_fd); |
| 872 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 873 | struct ccn_charbuf *router_prefix; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 874 | router_prefix=ccn_charbuf_create(); |
| 875 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 876 | if(res<0) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 877 | { |
| 878 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 879 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 880 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 881 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 882 | |
| 883 | ccn_name_append_str(router_prefix,"nlsr"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 884 | nlsr->in_interest.data=nlsr->router_name; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 885 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 886 | if ( res < 0 ) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 887 | { |
| 888 | fprintf(stderr,"Failed to register interest for router\n"); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 889 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 890 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 891 | } |
| 892 | ccn_charbuf_destroy(&router_prefix); |
| 893 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 894 | if ( nlsr->debugging ) |
| 895 | printf("Router Name : %s\n",nlsr->router_name); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 896 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name); |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 897 | if ( nlsr->debugging ) |
| 898 | printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 899 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 900 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 901 | print_name_prefix_from_npl(); |
| 902 | print_adjacent_from_adl(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 903 | build_and_install_name_lsas(); |
| 904 | print_name_lsdb(); |
| 905 | |
| 906 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 907 | 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] | 908 | nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 909 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 910 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 911 | while(1) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 912 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 913 | if ( nlsr->semaphor == NLSR_UNLOCKED ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 914 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 915 | if( nlsr->sched != NULL ) |
| 916 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 917 | long int micro_sec=ccn_schedule_run(nlsr->sched); |
| 918 | res=nlsr_api_server_poll(micro_sec,ccn_fd); |
| 919 | ON_ERROR_DESTROY(res); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 920 | } |
| 921 | if(nlsr->ccn != NULL) |
| 922 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 923 | res = ccn_run(nlsr->ccn, 0); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 924 | } |
| 925 | if (!(nlsr->sched && nlsr->ccn)) |
| 926 | { |
| 927 | break; |
| 928 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 929 | } |
| 930 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 931 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame^] | 932 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 933 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 934 | return 0; |
| 935 | } |
| 936 | |