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" |
| 22 | #include "utility.h" |
| 23 | |
| 24 | |
| 25 | struct option longopts[] = |
| 26 | { |
| 27 | { "daemon", no_argument, NULL, 'd'}, |
| 28 | { "config_file", required_argument, NULL, 'f'}, |
| 29 | { "help", no_argument, NULL, 'h'}, |
| 30 | { 0 } |
| 31 | }; |
| 32 | |
| 33 | static int |
| 34 | usage(char *progname) |
| 35 | { |
| 36 | |
| 37 | printf("Usage: %s [OPTIONS...]\n\ |
| 38 | NDN routing....\n\ |
| 39 | -d, --daemon Run in daemon mode\n\ |
| 40 | -f, --config_file Specify configuration file name\n\ |
| 41 | -h, --help Display this help message\n", progname); |
| 42 | |
| 43 | exit(1); |
| 44 | } |
| 45 | |
| 46 | void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result) |
| 47 | { |
| 48 | struct timeval now = {0}; |
| 49 | gettimeofday(&now, 0); |
| 50 | result->s = now.tv_sec; |
| 51 | result->micros = now.tv_usec; |
| 52 | } |
| 53 | |
| 54 | static struct ccn_gettime ndn_rtr_ticker = { |
| 55 | "timer", |
| 56 | &ndn_rtr_gettime, |
| 57 | 1000000, |
| 58 | NULL |
| 59 | }; |
| 60 | |
| 61 | void |
| 62 | process_command_router_name(char *command) |
| 63 | { |
| 64 | if(command==NULL) |
| 65 | { |
| 66 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 67 | return; |
| 68 | } |
| 69 | char *rem; |
| 70 | const char *sep=" \t\n"; |
| 71 | char *rtr_name; |
| 72 | |
| 73 | rtr_name=strtok_r(command,sep,&rem); |
| 74 | if(rtr_name==NULL) |
| 75 | { |
| 76 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 77 | return; |
| 78 | } |
| 79 | nlsr->router_name=(char *)malloc(strlen(rtr_name)+1); |
| 80 | memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1); |
| 81 | printf("Router Name: %s\n",nlsr->router_name); |
| 82 | } |
| 83 | |
| 84 | void |
| 85 | process_command_ccnname(char *command) |
| 86 | { |
| 87 | |
| 88 | if(command==NULL) |
| 89 | { |
| 90 | printf(" Wrong Command Format ( ccnname /name/prefix)\n"); |
| 91 | return; |
| 92 | } |
| 93 | char *rem; |
| 94 | const char *sep=" \t\n"; |
| 95 | char *name_prefix; |
| 96 | name_prefix=strtok_r(command,sep,&rem); |
| 97 | if(name_prefix==NULL) |
| 98 | { |
| 99 | printf(" Wrong Command Format ( ccnname /name/prefix/ )\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | printf("Name Prefix: %s \n",name_prefix); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 104 | |
| 105 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| 106 | np->name=(char *)malloc(strlen(name_prefix)+1); |
| 107 | memcpy(np->name,name_prefix,strlen(name_prefix)+1); |
| 108 | np->length=strlen(name_prefix)+1; |
| 109 | |
| 110 | add_name_prefix_to_npl(np); |
| 111 | /* Debugging Purpose */ |
| 112 | print_name_prefix_from_npl(); |
| 113 | |
| 114 | free(np->name); |
| 115 | free(np); |
| 116 | |
| 117 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | void |
| 121 | process_command_ccnneighbor(char *command) |
| 122 | { |
| 123 | if(command==NULL) |
| 124 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 125 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | char *rem; |
| 129 | const char *sep=" \t\n"; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 130 | char *rtr_name,*face; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 131 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 132 | rtr_name=strtok_r(command,sep,&rem); |
| 133 | if(rtr_name==NULL) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 134 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 135 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 136 | return; |
| 137 | } |
| 138 | |
| 139 | face=strtok_r(NULL,sep,&rem); |
| 140 | if(face==NULL) |
| 141 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 142 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 146 | printf("Router: %s face: %s\n",rtr_name,face); |
| 147 | int face_id; |
| 148 | int res; |
| 149 | res=sscanf(face,"face%d",&face_id); |
| 150 | |
| 151 | if(res != 1 ) |
| 152 | { |
| 153 | printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n"); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*)); |
| 158 | nbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| 159 | nbr->neighbor->name=(char *)malloc(strlen(rtr_name)+1); |
| 160 | memcpy(nbr->neighbor->name,rtr_name,strlen(rtr_name)+1); |
| 161 | nbr->neighbor->length=strlen(rtr_name)+1; |
| 162 | nbr->face=face_id; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 163 | nbr->status=0; |
| 164 | |
| 165 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 166 | |
| 167 | add_adjacent_to_adl(nbr); |
| 168 | print_adjacent_from_adl(); |
| 169 | |
| 170 | free(nbr->neighbor->name); |
| 171 | free(nbr->neighbor); |
| 172 | free(nbr); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 173 | |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | process_conf_command(char *command) |
| 178 | { |
| 179 | const char *separators=" \t\n"; |
| 180 | char *remainder=NULL; |
| 181 | char *cmd_type=NULL; |
| 182 | |
| 183 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 184 | return; |
| 185 | |
| 186 | cmd_type=strtok_r(command,separators,&remainder); |
| 187 | |
| 188 | if(!strcmp(cmd_type,"router-name") ) |
| 189 | { |
| 190 | process_command_router_name(remainder); |
| 191 | } |
| 192 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 193 | { |
| 194 | process_command_ccnneighbor(remainder); |
| 195 | } |
| 196 | else if(!strcmp(cmd_type,"ccnname") ) |
| 197 | { |
| 198 | process_command_ccnname(remainder); |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | printf("Wrong configuration Command %s \n",cmd_type); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | int |
| 207 | readConfigFile(const char *filename) |
| 208 | { |
| 209 | FILE *cfg; |
| 210 | char buf[1024]; |
| 211 | int len; |
| 212 | |
| 213 | cfg=fopen(filename, "r"); |
| 214 | |
| 215 | if(cfg == NULL) |
| 216 | { |
| 217 | printf("\nConfiguration File does not exists\n"); |
| 218 | exit(1); |
| 219 | } |
| 220 | |
| 221 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 222 | { |
| 223 | len=strlen(buf); |
| 224 | if(buf[len-1] == '\n') |
| 225 | buf[len-1]='\0'; |
| 226 | process_conf_command(buf); |
| 227 | } |
| 228 | |
| 229 | fclose(cfg); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 234 | void |
| 235 | add_name_prefix_to_npl(struct name_prefix *np) |
| 236 | { |
| 237 | |
| 238 | |
| 239 | printf("\nadd_name_prefix called\n"); |
| 240 | printf("Name Prefix: %s and length: %d \n",np->name,np->length); |
| 241 | |
| 242 | struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| 243 | |
| 244 | struct hashtb_enumerator ee; |
| 245 | struct hashtb_enumerator *e = ⅇ |
| 246 | int res; |
| 247 | |
| 248 | hashtb_start(nlsr->npl, e); |
| 249 | res = hashtb_seek(e, np->name, strlen(np->name), 0); |
| 250 | |
| 251 | hnp = e->data; |
| 252 | hnp->name=(char *)malloc(np->length); |
| 253 | memcpy(hnp->name,np->name,np->length); |
| 254 | hnp->length=np->length; |
| 255 | |
| 256 | hashtb_end(e); |
| 257 | |
| 258 | printf("\n"); |
| 259 | |
| 260 | } |
| 261 | |
| 262 | void |
| 263 | print_name_prefix_from_npl(void) |
| 264 | { |
| 265 | printf("print_name_prefix_from_npl called \n"); |
| 266 | int i, npl_element; |
| 267 | struct name_prefix *np; |
| 268 | |
| 269 | struct hashtb_enumerator ee; |
| 270 | struct hashtb_enumerator *e = ⅇ |
| 271 | |
| 272 | hashtb_start(nlsr->npl, e); |
| 273 | npl_element=hashtb_n(nlsr->npl); |
| 274 | |
| 275 | for(i=0;i<npl_element;i++) |
| 276 | { |
| 277 | np=e->data; |
| 278 | printf("Name Prefix: %s and Length: %d \n",np->name,np->length); |
| 279 | hashtb_next(e); |
| 280 | } |
| 281 | |
| 282 | hashtb_end(e); |
| 283 | |
| 284 | printf("\n"); |
| 285 | } |
| 286 | |
| 287 | void |
| 288 | add_adjacent_to_adl(struct ndn_neighbor *nbr) |
| 289 | { |
| 290 | printf("\nadd_adjacent_to_adl called\n"); |
| 291 | printf("Neighbor: %s Length: %d Face: %d Status: %d\n",nbr->neighbor->name,nbr->neighbor->length,nbr->face, nbr->status); |
| 292 | |
| 293 | struct ndn_neighbor *hnbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*)); |
| 294 | |
| 295 | struct hashtb_enumerator ee; |
| 296 | struct hashtb_enumerator *e = ⅇ |
| 297 | int res; |
| 298 | |
| 299 | hashtb_start(nlsr->adl, e); |
| 300 | res = hashtb_seek(e, nbr->neighbor->name , nbr->neighbor->length, 0); |
| 301 | |
| 302 | hnbr = e->data; |
| 303 | |
| 304 | hnbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| 305 | hnbr->neighbor->name=(char *)malloc(nbr->neighbor->length); |
| 306 | memcpy(hnbr->neighbor->name,nbr->neighbor->name,nbr->neighbor->length); |
| 307 | hnbr->neighbor->length=nbr->neighbor->length; |
| 308 | hnbr->face=nbr->face; |
| 309 | hnbr->status=nbr->status; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 310 | hnbr->last_lsdb_version=0; |
| 311 | |
| 312 | struct hashtb_param param_luq = {0}; |
| 313 | hnbr->lsa_update_queue=hashtb_create(200, ¶m_luq); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 314 | |
| 315 | hashtb_end(e); |
| 316 | |
| 317 | printf("\n"); |
| 318 | |
| 319 | |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | print_adjacent_from_adl(void) |
| 324 | { |
| 325 | printf("print_adjacent_from_adl called \n"); |
| 326 | int i, adl_element; |
| 327 | struct ndn_neighbor *nbr; |
| 328 | |
| 329 | struct hashtb_enumerator ee; |
| 330 | struct hashtb_enumerator *e = ⅇ |
| 331 | |
| 332 | hashtb_start(nlsr->adl, e); |
| 333 | adl_element=hashtb_n(nlsr->adl); |
| 334 | |
| 335 | for(i=0;i<adl_element;i++) |
| 336 | { |
| 337 | nbr=e->data; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 338 | printf("Neighbor: %s Length: %d Face: %d Status: %d LSDB Version: %d \n",nbr->neighbor->name,nbr->neighbor->length,nbr->face, nbr->status, nbr->last_lsdb_version); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 339 | hashtb_next(e); |
| 340 | } |
| 341 | |
| 342 | hashtb_end(e); |
| 343 | |
| 344 | printf("\n"); |
| 345 | } |
| 346 | |
| 347 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 348 | void |
| 349 | nlsr_destroy( void ) |
| 350 | { |
| 351 | |
| 352 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
| 353 | |
| 354 | int i, element; |
| 355 | struct ndn_neighbor *nbr; |
| 356 | |
| 357 | struct hashtb_enumerator ee; |
| 358 | struct hashtb_enumerator *e = ⅇ |
| 359 | |
| 360 | hashtb_start(nlsr->adl, e); |
| 361 | element=hashtb_n(nlsr->adl); |
| 362 | |
| 363 | for(i=0;i<element;i++) |
| 364 | { |
| 365 | nbr=e->data; |
| 366 | hashtb_destroy(&nbr->lsa_update_queue); |
| 367 | hashtb_next(e); |
| 368 | } |
| 369 | hashtb_end(e); |
| 370 | |
| 371 | |
| 372 | |
| 373 | hashtb_destroy(&nlsr->adl); |
| 374 | hashtb_destroy(&nlsr->npl); |
| 375 | ccn_schedule_destroy(&nlsr->sched); |
| 376 | ccn_destroy(&nlsr->ccn); |
| 377 | free(nlsr); |
| 378 | |
| 379 | } |
| 380 | |
| 381 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 382 | int |
| 383 | main(int argc, char *argv[]) |
| 384 | { |
| 385 | int res; |
| 386 | char *config_file; |
| 387 | int daemon_mode; |
| 388 | struct hashtb_param param_adl = {0}; |
| 389 | struct hashtb_param param_npl = {0}; |
| 390 | |
| 391 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
| 392 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 393 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 8c50d0d | 2012-08-09 13:38:03 -0500 | [diff] [blame] | 394 | nlsr->npl = hashtb_create(sizeof(struct name_prefix), ¶m_npl); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 395 | nlsr->in_interest.p = &incoming_interest; |
| 396 | nlsr->in_content.p = &incoming_content; |
| 397 | nlsr->is_synch_init=1; |
| 398 | |
| 399 | struct ccn_charbuf *router_prefix; |
| 400 | |
| 401 | while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| 402 | { |
| 403 | switch (res) |
| 404 | { |
| 405 | case 'd': |
| 406 | daemon_mode = 1; |
| 407 | break; |
| 408 | case 'f': |
| 409 | config_file = optarg; |
| 410 | break; |
| 411 | case 'h': |
| 412 | default: |
| 413 | usage(argv[0]); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | readConfigFile(config_file); |
| 418 | |
| 419 | nlsr->ccn=ccn_create(); |
| 420 | if(ccn_connect(nlsr->ccn, NULL) == -1) |
| 421 | { |
| 422 | fprintf(stderr,"Could not connect to ccnd\n"); |
| 423 | exit(1); |
| 424 | } |
| 425 | router_prefix=ccn_charbuf_create(); |
| 426 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
| 427 | if(res<0) |
| 428 | { |
| 429 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
| 430 | exit(1); |
| 431 | } |
| 432 | |
| 433 | ccn_name_append_str(router_prefix,"nlsr"); |
| 434 | nlsr->in_interest.data=nlsr->router_name; |
| 435 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 436 | if ( res < 0 ) |
| 437 | { |
| 438 | fprintf(stderr,"Failed to register interest for router\n"); |
| 439 | exit(1); |
| 440 | } |
| 441 | |
| 442 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
| 443 | |
| 444 | while(1) |
| 445 | { |
| 446 | ccn_schedule_run(nlsr->sched); |
| 447 | res = ccn_run(nlsr->ccn, 500); |
| 448 | |
| 449 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 450 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 451 | nlsr_destroy(); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |