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