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