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 |
| 11 | |
| 12 | |
| 13 | #include <ccn/ccn.h> |
| 14 | #include <ccn/uri.h> |
| 15 | #include <ccn/keystore.h> |
| 16 | #include <ccn/signing.h> |
| 17 | #include <ccn/schedule.h> |
| 18 | #include <ccn/hashtb.h> |
| 19 | |
| 20 | #include "nlsr.h" |
| 21 | #include "nlsr_ndn.h" |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 22 | #include "nlsr_lsdb.h" |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 23 | #include "nlsr_adl.h" |
| 24 | #include "utility.h" |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 25 | |
| 26 | |
| 27 | struct option longopts[] = |
| 28 | { |
| 29 | { "daemon", no_argument, NULL, 'd'}, |
| 30 | { "config_file", required_argument, NULL, 'f'}, |
| 31 | { "help", no_argument, NULL, 'h'}, |
| 32 | { 0 } |
| 33 | }; |
| 34 | |
| 35 | static int |
| 36 | usage(char *progname) |
| 37 | { |
| 38 | |
| 39 | printf("Usage: %s [OPTIONS...]\n\ |
| 40 | NDN routing....\n\ |
| 41 | -d, --daemon Run in daemon mode\n\ |
| 42 | -f, --config_file Specify configuration file name\n\ |
| 43 | -h, --help Display this help message\n", progname); |
| 44 | |
| 45 | exit(1); |
| 46 | } |
| 47 | |
| 48 | void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result) |
| 49 | { |
| 50 | struct timeval now = {0}; |
| 51 | gettimeofday(&now, 0); |
| 52 | result->s = now.tv_sec; |
| 53 | result->micros = now.tv_usec; |
| 54 | } |
| 55 | |
| 56 | static struct ccn_gettime ndn_rtr_ticker = { |
| 57 | "timer", |
| 58 | &ndn_rtr_gettime, |
| 59 | 1000000, |
| 60 | NULL |
| 61 | }; |
| 62 | |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 63 | void |
| 64 | my_lock(void) |
| 65 | { |
| 66 | nlsr->semaphor=1; |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | my_unlock(void) |
| 71 | { |
| 72 | nlsr->semaphor=0; |
| 73 | } |
| 74 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 75 | void |
| 76 | process_command_router_name(char *command) |
| 77 | { |
| 78 | if(command==NULL) |
| 79 | { |
| 80 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 81 | return; |
| 82 | } |
| 83 | char *rem; |
| 84 | const char *sep=" \t\n"; |
| 85 | char *rtr_name; |
| 86 | |
| 87 | rtr_name=strtok_r(command,sep,&rem); |
| 88 | if(rtr_name==NULL) |
| 89 | { |
| 90 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 91 | return; |
| 92 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 93 | |
| 94 | nlsr->router_name->name=(char *)malloc(strlen(rtr_name)+1); |
| 95 | memcpy(nlsr->router_name->name,rtr_name,strlen(rtr_name)+1); |
| 96 | nlsr->router_name->length=strlen(rtr_name)+1; |
| 97 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void |
| 101 | process_command_ccnname(char *command) |
| 102 | { |
| 103 | |
| 104 | if(command==NULL) |
| 105 | { |
| 106 | printf(" Wrong Command Format ( ccnname /name/prefix)\n"); |
| 107 | return; |
| 108 | } |
| 109 | char *rem; |
| 110 | const char *sep=" \t\n"; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 111 | char *name; |
| 112 | name=strtok_r(command,sep,&rem); |
| 113 | if(name==NULL) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 114 | { |
| 115 | printf(" Wrong Command Format ( ccnname /name/prefix/ )\n"); |
| 116 | return; |
| 117 | } |
| 118 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 119 | printf("Name Prefix: %s \n",name); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 120 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 121 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 122 | np->name=(char *)malloc(strlen(name)+1); |
| 123 | memcpy(np->name,name,strlen(name)+1); |
| 124 | np->length=strlen(name)+1; |
| 125 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 126 | add_name_prefix_to_npl(np); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 127 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 128 | free(np); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 129 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 130 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | process_command_ccnneighbor(char *command) |
| 135 | { |
| 136 | if(command==NULL) |
| 137 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 138 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | char *rem; |
| 142 | const char *sep=" \t\n"; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 143 | char *rtr_name,*face; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 144 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 145 | rtr_name=strtok_r(command,sep,&rem); |
| 146 | if(rtr_name==NULL) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 147 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 148 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 149 | return; |
| 150 | } |
| 151 | |
| 152 | face=strtok_r(NULL,sep,&rem); |
| 153 | if(face==NULL) |
| 154 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 155 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 156 | return; |
| 157 | } |
| 158 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 159 | printf("Router: %s face: %s\n",rtr_name,face); |
| 160 | int face_id; |
| 161 | int res; |
| 162 | res=sscanf(face,"face%d",&face_id); |
| 163 | |
| 164 | if(res != 1 ) |
| 165 | { |
| 166 | printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n"); |
| 167 | return; |
| 168 | } |
| 169 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 170 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 171 | np->name=(char *)malloc(strlen(rtr_name)+1); |
| 172 | memcpy(np->name,rtr_name,strlen(rtr_name)+1); |
| 173 | np->length=strlen(rtr_name)+1; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 174 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 175 | add_adjacent_to_adl(np,face_id); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 176 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 177 | free(np); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 181 | process_command_lsdb_synch_interval(char *command) |
| 182 | { |
| 183 | if(command==NULL) |
| 184 | { |
| 185 | printf(" Wrong Command Format ( lsdb-synch-interval secs )\n"); |
| 186 | return; |
| 187 | } |
| 188 | char *rem; |
| 189 | const char *sep=" \t\n"; |
| 190 | char *secs; |
| 191 | long int seconds; |
| 192 | |
| 193 | secs=strtok_r(command,sep,&rem); |
| 194 | if(secs==NULL) |
| 195 | { |
| 196 | printf(" Wrong Command Format ( lsdb-synch-interval secs)\n"); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | seconds=atoi(secs); |
| 201 | nlsr->lsdb_synch_interval=seconds; |
| 202 | |
| 203 | } |
| 204 | |
| 205 | |
| 206 | void |
| 207 | process_command_interest_retry(char *command) |
| 208 | { |
| 209 | if(command==NULL) |
| 210 | { |
| 211 | printf(" Wrong Command Format ( interest-retry number )\n"); |
| 212 | return; |
| 213 | } |
| 214 | char *rem; |
| 215 | const char *sep=" \t\n"; |
| 216 | char *secs; |
| 217 | long int seconds; |
| 218 | |
| 219 | secs=strtok_r(command,sep,&rem); |
| 220 | if(secs==NULL) |
| 221 | { |
| 222 | printf(" Wrong Command Format ( interest-retry number)\n"); |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | seconds=atoi(secs); |
| 227 | nlsr->interest_retry=seconds; |
| 228 | |
| 229 | } |
| 230 | |
| 231 | void |
| 232 | process_command_interest_resend_time(char *command) |
| 233 | { |
| 234 | if(command==NULL) |
| 235 | { |
| 236 | printf(" Wrong Command Format ( interest-resend-time secs )\n"); |
| 237 | return; |
| 238 | } |
| 239 | char *rem; |
| 240 | const char *sep=" \t\n"; |
| 241 | char *secs; |
| 242 | long int seconds; |
| 243 | |
| 244 | secs=strtok_r(command,sep,&rem); |
| 245 | if(secs==NULL) |
| 246 | { |
| 247 | printf(" Wrong Command Format ( interest-resend-time secs)\n"); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | seconds=atoi(secs); |
| 252 | nlsr->interest_resend_time=seconds; |
| 253 | |
| 254 | } |
| 255 | |
| 256 | void |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 257 | process_conf_command(char *command) |
| 258 | { |
| 259 | const char *separators=" \t\n"; |
| 260 | char *remainder=NULL; |
| 261 | char *cmd_type=NULL; |
| 262 | |
| 263 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 264 | return; |
| 265 | |
| 266 | cmd_type=strtok_r(command,separators,&remainder); |
| 267 | |
| 268 | if(!strcmp(cmd_type,"router-name") ) |
| 269 | { |
| 270 | process_command_router_name(remainder); |
| 271 | } |
| 272 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 273 | { |
| 274 | process_command_ccnneighbor(remainder); |
| 275 | } |
| 276 | else if(!strcmp(cmd_type,"ccnname") ) |
| 277 | { |
| 278 | process_command_ccnname(remainder); |
| 279 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 280 | else if(!strcmp(cmd_type,"lsdb-synch-interval") ) |
| 281 | { |
| 282 | process_command_lsdb_synch_interval(remainder); |
| 283 | } |
| 284 | else if(!strcmp(cmd_type,"interest-retry") ) |
| 285 | { |
| 286 | process_command_interest_retry(remainder); |
| 287 | } |
| 288 | else if(!strcmp(cmd_type,"interest-resend-time") ) |
| 289 | { |
| 290 | process_command_interest_resend_time(remainder); |
| 291 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 292 | else |
| 293 | { |
| 294 | printf("Wrong configuration Command %s \n",cmd_type); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | int |
| 299 | readConfigFile(const char *filename) |
| 300 | { |
| 301 | FILE *cfg; |
| 302 | char buf[1024]; |
| 303 | int len; |
| 304 | |
| 305 | cfg=fopen(filename, "r"); |
| 306 | |
| 307 | if(cfg == NULL) |
| 308 | { |
| 309 | printf("\nConfiguration File does not exists\n"); |
| 310 | exit(1); |
| 311 | } |
| 312 | |
| 313 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 314 | { |
| 315 | len=strlen(buf); |
| 316 | if(buf[len-1] == '\n') |
| 317 | buf[len-1]='\0'; |
| 318 | process_conf_command(buf); |
| 319 | } |
| 320 | |
| 321 | fclose(cfg); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 326 | void |
| 327 | add_name_prefix_to_npl(struct name_prefix *np) |
| 328 | { |
| 329 | |
| 330 | |
| 331 | printf("\nadd_name_prefix called\n"); |
| 332 | printf("Name Prefix: %s and length: %d \n",np->name,np->length); |
| 333 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 334 | struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 335 | |
| 336 | struct hashtb_enumerator ee; |
| 337 | struct hashtb_enumerator *e = ⅇ |
| 338 | int res; |
| 339 | |
| 340 | hashtb_start(nlsr->npl, e); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 341 | res = hashtb_seek(e, np->name, np->length, 0); |
akmhoque | 2852a22 | 2012-08-21 12:09:00 -0400 | [diff] [blame] | 342 | |
akmhoque | e7c4b6d | 2012-08-21 12:30:25 -0400 | [diff] [blame] | 343 | if(res == HT_NEW_ENTRY) |
| 344 | { |
akmhoque | 2852a22 | 2012-08-21 12:09:00 -0400 | [diff] [blame] | 345 | |
akmhoque | e7c4b6d | 2012-08-21 12:30:25 -0400 | [diff] [blame] | 346 | hnp = e->data; |
akmhoque | e7c4b6d | 2012-08-21 12:30:25 -0400 | [diff] [blame] | 347 | hnp->length=np->length; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 348 | hnp->name=(char *)malloc(np->length); |
| 349 | memcpy(hnp->name,np->name,np->length); |
akmhoque | e7c4b6d | 2012-08-21 12:30:25 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | hashtb_end(e); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 353 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 354 | |
| 355 | printf("\n"); |
| 356 | |
| 357 | } |
| 358 | |
| 359 | void |
| 360 | print_name_prefix_from_npl(void) |
| 361 | { |
| 362 | printf("print_name_prefix_from_npl called \n"); |
| 363 | int i, npl_element; |
| 364 | struct name_prefix *np; |
| 365 | |
| 366 | struct hashtb_enumerator ee; |
| 367 | struct hashtb_enumerator *e = ⅇ |
| 368 | |
| 369 | hashtb_start(nlsr->npl, e); |
| 370 | npl_element=hashtb_n(nlsr->npl); |
| 371 | |
| 372 | for(i=0;i<npl_element;i++) |
| 373 | { |
| 374 | np=e->data; |
| 375 | printf("Name Prefix: %s and Length: %d \n",np->name,np->length); |
| 376 | hashtb_next(e); |
| 377 | } |
| 378 | |
| 379 | hashtb_end(e); |
| 380 | |
| 381 | printf("\n"); |
| 382 | } |
| 383 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 384 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 385 | void |
| 386 | nlsr_destroy( void ) |
| 387 | { |
| 388 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 389 | printf("Freeing Allocated Memory....\n"); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 390 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
| 391 | |
| 392 | int i, element; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 393 | struct hashtb_enumerator ee; |
| 394 | struct hashtb_enumerator *e = ⅇ |
| 395 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 396 | /*destroying NAME LSDB */ |
| 397 | struct nlsr *name_lsa; |
| 398 | hashtb_start(nlsr->lsdb->name_lsdb, e); |
| 399 | element=hashtb_n(nlsr->lsdb->name_lsdb); |
| 400 | |
| 401 | for(i=0;i<element;i++) |
| 402 | { |
| 403 | name_lsa=e->data; |
| 404 | free(name_lsa); |
| 405 | hashtb_next(e); |
| 406 | } |
| 407 | |
| 408 | hashtb_end(e); |
| 409 | hashtb_destroy(&nlsr->lsdb->name_lsdb); |
| 410 | |
| 411 | |
| 412 | /*destroying ADJ LSDB */ |
| 413 | struct alsr *adj_lsa; |
| 414 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 415 | element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 416 | |
| 417 | for(i=0;i<element;i++) |
| 418 | { |
| 419 | adj_lsa=e->data; |
| 420 | free(adj_lsa); |
| 421 | hashtb_next(e); |
| 422 | } |
| 423 | |
| 424 | hashtb_end(e); |
| 425 | |
| 426 | |
| 427 | hashtb_destroy(&nlsr->lsdb->adj_lsdb); |
| 428 | |
| 429 | /* Destroying NPL */ |
| 430 | struct ccn_charbuf *np; |
| 431 | hashtb_start(nlsr->npl, e); |
| 432 | element=hashtb_n(nlsr->npl); |
| 433 | |
| 434 | for(i=0;i<element;i++) |
| 435 | { |
| 436 | np=e->data; |
| 437 | free(np); |
| 438 | hashtb_next(e); |
| 439 | } |
| 440 | hashtb_end(e); |
| 441 | hashtb_destroy(&nlsr->npl); |
| 442 | |
| 443 | /* Destroying ADL */ |
| 444 | struct ndn_neighbor *nbr; |
| 445 | hashtb_start(nlsr->adl, e); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 446 | element=hashtb_n(nlsr->adl); |
| 447 | |
| 448 | for(i=0;i<element;i++) |
| 449 | { |
| 450 | nbr=e->data; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 451 | free(nbr); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 452 | hashtb_next(e); |
| 453 | } |
| 454 | hashtb_end(e); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 455 | hashtb_destroy(&nlsr->adl); |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 456 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 457 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 458 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 459 | ccn_schedule_destroy(&nlsr->sched); |
| 460 | ccn_destroy(&nlsr->ccn); |
| 461 | free(nlsr); |
| 462 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 463 | printf("Finished freeing allocated memory\n"); |
| 464 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 465 | } |
| 466 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 467 | void |
| 468 | init_nlsr(void) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 469 | { |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 470 | struct hashtb_param param_adl = {0}; |
| 471 | struct hashtb_param param_npl = {0}; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 472 | |
| 473 | struct hashtb_param param_adj_lsdb = {0}; |
| 474 | struct hashtb_param param_name_lsdb = {0}; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 475 | |
| 476 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
| 477 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 478 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 479 | nlsr->npl = hashtb_create(sizeof(struct name_prefix ), ¶m_npl); |
| 480 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 481 | nlsr->in_interest.p = &incoming_interest; |
| 482 | nlsr->in_content.p = &incoming_content; |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 483 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 484 | nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase )); |
| 485 | char *time_stamp=get_current_timestamp_micro(); |
| 486 | nlsr->lsdb->version=(char *)malloc(strlen(time_stamp)+1); |
| 487 | memcpy(nlsr->lsdb->version,time_stamp,strlen(time_stamp)+1); |
| 488 | memset(nlsr->lsdb->version,'0',strlen(time_stamp)); |
| 489 | |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 490 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 491 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
| 492 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 493 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 494 | nlsr->router_name=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 495 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 496 | nlsr->is_synch_init=1; |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 497 | nlsr->nlsa_id=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 498 | nlsr->adj_build_flag=0; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 499 | nlsr->adj_build_count=0; |
| 500 | nlsr->is_build_adj_lsa_sheduled=0; |
| 501 | nlsr->is_send_lsdb_interest_scheduled=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 502 | |
| 503 | nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL; |
| 504 | nlsr->interest_retry = INTEREST_RETRY; |
| 505 | nlsr->interest_resend_time = INTEREST_RESEND_TIME; |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 506 | |
| 507 | nlsr->semaphor=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 508 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 509 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | int |
| 513 | main(int argc, char *argv[]) |
| 514 | { |
| 515 | int res; |
| 516 | char *config_file; |
akmhoque | 2852a22 | 2012-08-21 12:09:00 -0400 | [diff] [blame] | 517 | //int daemon_mode; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 518 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 519 | |
| 520 | init_nlsr(); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 521 | |
| 522 | while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| 523 | { |
| 524 | switch (res) |
| 525 | { |
| 526 | case 'd': |
akmhoque | 2852a22 | 2012-08-21 12:09:00 -0400 | [diff] [blame] | 527 | //daemon_mode = 1; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 528 | break; |
| 529 | case 'f': |
| 530 | config_file = optarg; |
| 531 | break; |
| 532 | case 'h': |
| 533 | default: |
| 534 | usage(argv[0]); |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | readConfigFile(config_file); |
| 539 | |
| 540 | nlsr->ccn=ccn_create(); |
| 541 | if(ccn_connect(nlsr->ccn, NULL) == -1) |
| 542 | { |
| 543 | fprintf(stderr,"Could not connect to ccnd\n"); |
| 544 | exit(1); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 545 | } |
| 546 | struct ccn_charbuf *router_prefix; |
| 547 | router_prefix=ccn_charbuf_create(); |
| 548 | res=ccn_name_from_uri(router_prefix,nlsr->router_name->name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 549 | if(res<0) |
| 550 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 551 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name->name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 552 | exit(1); |
| 553 | } |
| 554 | |
| 555 | ccn_name_append_str(router_prefix,"nlsr"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 556 | nlsr->in_interest.data=nlsr->router_name->name; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 557 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 558 | if ( res < 0 ) |
| 559 | { |
| 560 | fprintf(stderr,"Failed to register interest for router\n"); |
| 561 | exit(1); |
| 562 | } |
| 563 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 564 | /* Debugging purpose */ |
| 565 | print_name_prefix_from_npl(); |
| 566 | print_adjacent_from_adl(); |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 567 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 568 | printf("\n"); |
| 569 | printf("Router Name: %s\n",nlsr->router_name->name); |
| 570 | printf("Time in MicroSec: %s \n",get_current_timestamp_micro()); |
| 571 | printf("LSDB Version: %s\n",nlsr->lsdb->version); |
| 572 | printf("\n"); |
| 573 | |
| 574 | build_and_install_name_lsas(); |
| 575 | print_name_lsdb(); |
| 576 | |
| 577 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
| 578 | nlsr->event_send_info_interest = ccn_schedule_event(nlsr->sched, 500, &send_info_interest, NULL, 0); |
| 579 | |
| 580 | |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 581 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 582 | while(1) |
| 583 | { |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame^] | 584 | if(nlsr->semaphor == 0) |
| 585 | { |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 586 | ccn_schedule_run(nlsr->sched); |
| 587 | res = ccn_run(nlsr->ccn, 500); |
| 588 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 589 | |
| 590 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 591 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 592 | nlsr_destroy(); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | |