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 | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 367 | process_conf_command(char *command) |
| 368 | { |
| 369 | const char *separators=" \t\n"; |
| 370 | char *remainder=NULL; |
| 371 | char *cmd_type=NULL; |
| 372 | |
| 373 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 374 | return; |
| 375 | |
| 376 | cmd_type=strtok_r(command,separators,&remainder); |
| 377 | |
| 378 | if(!strcmp(cmd_type,"router-name") ) |
| 379 | { |
| 380 | process_command_router_name(remainder); |
| 381 | } |
| 382 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 383 | { |
| 384 | process_command_ccnneighbor(remainder); |
| 385 | } |
| 386 | else if(!strcmp(cmd_type,"ccnname") ) |
| 387 | { |
| 388 | process_command_ccnname(remainder); |
| 389 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 390 | else if(!strcmp(cmd_type,"lsdb-synch-interval") ) |
| 391 | { |
| 392 | process_command_lsdb_synch_interval(remainder); |
| 393 | } |
| 394 | else if(!strcmp(cmd_type,"interest-retry") ) |
| 395 | { |
| 396 | process_command_interest_retry(remainder); |
| 397 | } |
| 398 | else if(!strcmp(cmd_type,"interest-resend-time") ) |
| 399 | { |
| 400 | process_command_interest_resend_time(remainder); |
| 401 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 402 | else if(!strcmp(cmd_type,"lsa-refresh-time") ) |
| 403 | { |
| 404 | process_command_lsa_refresh_time(remainder); |
| 405 | } |
| 406 | else if(!strcmp(cmd_type,"router-dead-interval") ) |
| 407 | { |
| 408 | process_command_router_dead_interval(remainder); |
| 409 | } |
| 410 | else |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 411 | { |
| 412 | printf("Wrong configuration Command %s \n",cmd_type); |
| 413 | } |
| 414 | } |
| 415 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 416 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 417 | int |
| 418 | readConfigFile(const char *filename) |
| 419 | { |
| 420 | FILE *cfg; |
| 421 | char buf[1024]; |
| 422 | int len; |
| 423 | |
| 424 | cfg=fopen(filename, "r"); |
| 425 | |
| 426 | if(cfg == NULL) |
| 427 | { |
| 428 | printf("\nConfiguration File does not exists\n"); |
| 429 | exit(1); |
| 430 | } |
| 431 | |
| 432 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 433 | { |
| 434 | len=strlen(buf); |
| 435 | if(buf[len-1] == '\n') |
| 436 | buf[len-1]='\0'; |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 437 | if ( buf[0] != '#' && buf[0] != '!') |
| 438 | process_conf_command(buf); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | fclose(cfg); |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 446 | void |
| 447 | nlsr_destroy( void ) |
| 448 | { |
| 449 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 450 | printf("Freeing Allocated Memory....\n"); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 451 | /* Destroying all face created by nlsr in CCND */ |
| 452 | destroy_all_face_by_nlsr(); |
| 453 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 454 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 455 | hashtb_destroy(&nlsr->npl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 456 | hashtb_destroy(&nlsr->adl); |
| 457 | hashtb_destroy(&nlsr->lsdb->name_lsdb); |
| 458 | hashtb_destroy(&nlsr->lsdb->adj_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 459 | hashtb_destroy(&nlsr->pit_alsa); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 460 | hashtb_destroy(&nlsr->routing_table); |
| 461 | |
| 462 | |
| 463 | int i, npt_element; |
| 464 | struct npt_entry *ne; |
| 465 | struct hashtb_enumerator ee; |
| 466 | struct hashtb_enumerator *e = ⅇ |
| 467 | hashtb_start(nlsr->npt, e); |
| 468 | npt_element=hashtb_n(nlsr->npt); |
| 469 | for(i=0;i<npt_element;i++) |
| 470 | { |
| 471 | ne=e->data; |
akmhoque | 810a5b5 | 2012-09-09 16:53:14 -0500 | [diff] [blame] | 472 | hashtb_destroy(&ne->name_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 473 | hashtb_next(e); |
| 474 | } |
| 475 | |
| 476 | hashtb_end(e); |
| 477 | hashtb_destroy(&nlsr->npt); |
| 478 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 479 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 480 | ccn_schedule_destroy(&nlsr->sched); |
| 481 | ccn_destroy(&nlsr->ccn); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 482 | |
| 483 | free(nlsr->lsdb->lsdb_version); |
| 484 | free(nlsr->lsdb); |
| 485 | free(nlsr->router_name); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 486 | free(nlsr); |
| 487 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 488 | printf("Finished freeing allocated memory\n"); |
| 489 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 490 | } |
| 491 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 492 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 493 | int |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 494 | init_nlsr(void) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 495 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 496 | if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 497 | { |
| 498 | perror("SIGQUIT install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 499 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 500 | } |
| 501 | if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR) |
| 502 | { |
| 503 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 504 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 505 | } |
| 506 | if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 507 | { |
| 508 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 509 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 510 | } |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 511 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 512 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 513 | |
| 514 | struct hashtb_param param_adl = {0}; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 515 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 516 | struct hashtb_param param_npl = {0}; |
| 517 | nlsr->npl = hashtb_create(sizeof(struct name_prefix), ¶m_npl); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 518 | struct hashtb_param param_pit_alsa = {0}; |
akmhoque | 1ce7105 | 2012-09-13 22:51:32 -0500 | [diff] [blame] | 519 | nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 520 | struct hashtb_param param_npt = {0}; |
| 521 | nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt); |
| 522 | struct hashtb_param param_rte = {0}; |
| 523 | nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_rte); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 524 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 525 | nlsr->in_interest.p = &incoming_interest; |
| 526 | nlsr->in_content.p = &incoming_content; |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 527 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 528 | nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase)); |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 529 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 530 | char *time_stamp=(char *)malloc(20); |
| 531 | memset(time_stamp,0,20); |
| 532 | get_current_timestamp_micro(time_stamp); |
| 533 | nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1); |
| 534 | memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp)); |
| 535 | free(time_stamp); |
| 536 | |
| 537 | struct hashtb_param param_adj_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 538 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 539 | struct hashtb_param param_name_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 540 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 541 | |
| 542 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 543 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 544 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 545 | nlsr->is_synch_init=1; |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 546 | nlsr->nlsa_id=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 547 | nlsr->adj_build_flag=0; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 548 | nlsr->adj_build_count=0; |
| 549 | nlsr->is_build_adj_lsa_sheduled=0; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 550 | nlsr->is_send_lsdb_interest_scheduled=0; |
| 551 | nlsr->is_route_calculation_scheduled=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 552 | |
| 553 | nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL; |
| 554 | nlsr->interest_retry = INTEREST_RETRY; |
| 555 | nlsr->interest_resend_time = INTEREST_RESEND_TIME; |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 556 | nlsr->lsa_refresh_time=LSA_REFRESH_TIME; |
| 557 | nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL; |
| 558 | |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 559 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 560 | nlsr->semaphor=NLSR_UNLOCKED; |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 561 | |
| 562 | return 0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 563 | } |
| 564 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 565 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 566 | int |
| 567 | main(int argc, char *argv[]) |
| 568 | { |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 569 | int res, ret; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 570 | char *config_file; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 571 | //int daemon_mode; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 572 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 573 | ret=init_nlsr(); |
| 574 | ON_ERROR_EXIT(ret); |
| 575 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 576 | while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| 577 | { |
| 578 | switch (res) |
| 579 | { |
| 580 | case 'd': |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 581 | //daemon_mode = 1; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 582 | break; |
| 583 | case 'f': |
| 584 | config_file = optarg; |
| 585 | break; |
| 586 | case 'h': |
| 587 | default: |
| 588 | usage(argv[0]); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | readConfigFile(config_file); |
| 593 | |
| 594 | nlsr->ccn=ccn_create(); |
| 595 | if(ccn_connect(nlsr->ccn, NULL) == -1) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 596 | { |
| 597 | fprintf(stderr,"Could not connect to ccnd\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 598 | ON_ERROR_DESTROY(-1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 599 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 600 | struct ccn_charbuf *router_prefix; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 601 | router_prefix=ccn_charbuf_create(); |
| 602 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 603 | if(res<0) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 604 | { |
| 605 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 606 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 607 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 608 | |
| 609 | ccn_name_append_str(router_prefix,"nlsr"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 610 | nlsr->in_interest.data=nlsr->router_name; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 611 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 612 | if ( res < 0 ) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 613 | { |
| 614 | fprintf(stderr,"Failed to register interest for router\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 615 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 616 | } |
| 617 | ccn_charbuf_destroy(&router_prefix); |
| 618 | |
| 619 | printf("Router Name : %s\n",nlsr->router_name); |
| 620 | printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 621 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 622 | print_name_prefix_from_npl(); |
| 623 | print_adjacent_from_adl(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 624 | build_and_install_name_lsas(); |
| 625 | print_name_lsdb(); |
| 626 | |
| 627 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 628 | 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] | 629 | nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 630 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 631 | while(1) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 632 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 633 | if ( nlsr->semaphor == NLSR_UNLOCKED ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 634 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 635 | if( nlsr->sched != NULL ) |
| 636 | { |
| 637 | ccn_schedule_run(nlsr->sched); |
| 638 | } |
| 639 | if(nlsr->ccn != NULL) |
| 640 | { |
| 641 | res = ccn_run(nlsr->ccn, 500); |
| 642 | } |
| 643 | if (!(nlsr->sched && nlsr->ccn)) |
| 644 | { |
| 645 | break; |
| 646 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 647 | } |
| 648 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 649 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 650 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |