| #include<stdio.h> |
| #include<string.h> |
| #include<stdlib.h> |
| #include <unistd.h> |
| #include <getopt.h> |
| #include <sys/time.h> |
| #include <assert.h> |
| #ifdef HAVE_CONFIG_H |
| #include <config.h> |
| #endif |
| |
| |
| #include <ccn/ccn.h> |
| #include <ccn/uri.h> |
| #include <ccn/keystore.h> |
| #include <ccn/signing.h> |
| #include <ccn/schedule.h> |
| #include <ccn/hashtb.h> |
| |
| #include "nlsr.h" |
| #include "nlsr_ndn.h" |
| #include "utility.h" |
| #include "nlsr_adl.h" |
| #include "nlsr_lsdb.h" |
| |
| |
| struct option longopts[] = |
| { |
| { "daemon", no_argument, NULL, 'd'}, |
| { "config_file", required_argument, NULL, 'f'}, |
| { "help", no_argument, NULL, 'h'}, |
| { 0 } |
| }; |
| |
| static int |
| usage(char *progname) |
| { |
| |
| printf("Usage: %s [OPTIONS...]\n\ |
| NDN routing....\n\ |
| -d, --daemon Run in daemon mode\n\ |
| -f, --config_file Specify configuration file name\n\ |
| -h, --help Display this help message\n", progname); |
| |
| exit(1); |
| } |
| |
| void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result) |
| { |
| struct timeval now = {0}; |
| gettimeofday(&now, 0); |
| result->s = now.tv_sec; |
| result->micros = now.tv_usec; |
| } |
| |
| static struct ccn_gettime ndn_rtr_ticker = { |
| "timer", |
| &ndn_rtr_gettime, |
| 1000000, |
| NULL |
| }; |
| |
| void |
| process_command_router_name(char *command) |
| { |
| if(command==NULL) |
| { |
| printf(" Wrong Command Format ( router-name /router/name )\n"); |
| return; |
| } |
| char *rem; |
| const char *sep=" \t\n"; |
| char *rtr_name; |
| |
| rtr_name=strtok_r(command,sep,&rem); |
| if(rtr_name==NULL) |
| { |
| printf(" Wrong Command Format ( router-name /router/name )\n"); |
| return; |
| } |
| nlsr->router_name=(char *)malloc(strlen(rtr_name)+1); |
| memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1); |
| printf("Router Name: %s\n",nlsr->router_name); |
| } |
| |
| void |
| process_command_ccnname(char *command) |
| { |
| |
| if(command==NULL) |
| { |
| printf(" Wrong Command Format ( ccnname /name/prefix)\n"); |
| return; |
| } |
| char *rem; |
| const char *sep=" \t\n"; |
| char *name_prefix; |
| name_prefix=strtok_r(command,sep,&rem); |
| if(name_prefix==NULL) |
| { |
| printf(" Wrong Command Format ( ccnname /name/prefix/ )\n"); |
| return; |
| } |
| |
| printf("Name Prefix: %s \n",name_prefix); |
| |
| struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| np->name=(char *)malloc(strlen(name_prefix)+1); |
| memcpy(np->name,name_prefix,strlen(name_prefix)+1); |
| np->length=strlen(name_prefix)+1; |
| |
| add_name_prefix_to_npl(np); |
| /* Debugging Purpose */ |
| print_name_prefix_from_npl(); |
| |
| free(np->name); |
| free(np); |
| |
| |
| } |
| |
| void |
| process_command_ccnneighbor(char *command) |
| { |
| if(command==NULL) |
| { |
| printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
| return; |
| } |
| char *rem; |
| const char *sep=" \t\n"; |
| char *rtr_name,*face; |
| |
| rtr_name=strtok_r(command,sep,&rem); |
| if(rtr_name==NULL) |
| { |
| printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
| return; |
| } |
| |
| face=strtok_r(NULL,sep,&rem); |
| if(face==NULL) |
| { |
| printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
| return; |
| } |
| |
| printf("Router: %s face: %s\n",rtr_name,face); |
| int face_id; |
| int res; |
| res=sscanf(face,"face%d",&face_id); |
| |
| if(res != 1 ) |
| { |
| printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n"); |
| return; |
| } |
| |
| struct ndn_neighbor *nbr=(struct ndn_neighbor *)malloc(sizeof(struct ndn_neighbor*)); |
| nbr->neighbor=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| nbr->neighbor->name=(char *)malloc(strlen(rtr_name)+1); |
| memcpy(nbr->neighbor->name,rtr_name,strlen(rtr_name)+1); |
| nbr->neighbor->length=strlen(rtr_name); |
| nbr->face=face_id; |
| nbr->status=0; |
| |
| |
| |
| add_adjacent_to_adl(nbr); |
| print_adjacent_from_adl(); |
| |
| free(nbr->neighbor->name); |
| free(nbr->neighbor); |
| free(nbr); |
| |
| } |
| |
| void |
| process_conf_command(char *command) |
| { |
| const char *separators=" \t\n"; |
| char *remainder=NULL; |
| char *cmd_type=NULL; |
| |
| if(command==NULL || strlen(command)==0 || command[0]=='!') |
| return; |
| |
| cmd_type=strtok_r(command,separators,&remainder); |
| |
| if(!strcmp(cmd_type,"router-name") ) |
| { |
| process_command_router_name(remainder); |
| } |
| else if(!strcmp(cmd_type,"ccnneighbor") ) |
| { |
| process_command_ccnneighbor(remainder); |
| } |
| else if(!strcmp(cmd_type,"ccnname") ) |
| { |
| process_command_ccnname(remainder); |
| } |
| else |
| { |
| printf("Wrong configuration Command %s \n",cmd_type); |
| } |
| } |
| |
| int |
| readConfigFile(const char *filename) |
| { |
| FILE *cfg; |
| char buf[1024]; |
| int len; |
| |
| cfg=fopen(filename, "r"); |
| |
| if(cfg == NULL) |
| { |
| printf("\nConfiguration File does not exists\n"); |
| exit(1); |
| } |
| |
| while(fgets((char *)buf, sizeof(buf), cfg)) |
| { |
| len=strlen(buf); |
| if(buf[len-1] == '\n') |
| buf[len-1]='\0'; |
| process_conf_command(buf); |
| } |
| |
| fclose(cfg); |
| |
| return 0; |
| } |
| |
| void |
| add_name_prefix_to_npl(struct name_prefix *np) |
| { |
| |
| |
| printf("\nadd_name_prefix called\n"); |
| printf("Name Prefix: %s and length: %d \n",np->name,np->length); |
| |
| struct name_prefix *hnp=(struct name_prefix *)malloc(sizeof(struct name_prefix *)); |
| |
| struct hashtb_enumerator ee; |
| struct hashtb_enumerator *e = ⅇ |
| int res; |
| |
| hashtb_start(nlsr->npl, e); |
| res = hashtb_seek(e, np->name, strlen(np->name), 0); |
| |
| if(res == HT_NEW_ENTRY) |
| { |
| |
| hnp = e->data; |
| hnp->name=(char *)malloc(np->length); |
| memcpy(hnp->name,np->name,np->length); |
| hnp->length=np->length; |
| } |
| |
| hashtb_end(e); |
| |
| |
| printf("\n"); |
| |
| } |
| |
| void |
| print_name_prefix_from_npl(void) |
| { |
| printf("print_name_prefix_from_npl called \n"); |
| int i, npl_element; |
| struct name_prefix *np; |
| |
| struct hashtb_enumerator ee; |
| struct hashtb_enumerator *e = ⅇ |
| |
| hashtb_start(nlsr->npl, e); |
| npl_element=hashtb_n(nlsr->npl); |
| |
| for(i=0;i<npl_element;i++) |
| { |
| np=e->data; |
| printf("Name Prefix: %s and Length: %d \n",np->name,np->length); |
| hashtb_next(e); |
| } |
| |
| hashtb_end(e); |
| |
| printf("\n"); |
| } |
| |
| |
| void |
| nlsr_destroy( void ) |
| { |
| |
| /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
| |
| int i, element; |
| struct ndn_neighbor *nbr; |
| |
| struct hashtb_enumerator ee; |
| struct hashtb_enumerator *e = ⅇ |
| |
| hashtb_start(nlsr->adl, e); |
| element=hashtb_n(nlsr->adl); |
| |
| for(i=0;i<element;i++) |
| { |
| nbr=e->data; |
| hashtb_destroy(&nbr->lsa_update_queue); |
| hashtb_next(e); |
| } |
| hashtb_end(e); |
| |
| |
| |
| hashtb_destroy(&nlsr->adl); |
| hashtb_destroy(&nlsr->npl); |
| ccn_schedule_destroy(&nlsr->sched); |
| ccn_destroy(&nlsr->ccn); |
| free(nlsr); |
| |
| } |
| |
| void |
| init_nlsr(void) |
| { |
| struct hashtb_param param_adl = {0}; |
| struct hashtb_param param_npl = {0}; |
| |
| struct hashtb_param param_adj_lsdb = {0}; |
| struct hashtb_param param_name_lsdb = {0}; |
| |
| nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
| |
| nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
| nlsr->npl = hashtb_create(sizeof(struct name_prefix), ¶m_npl); |
| nlsr->in_interest.p = &incoming_interest; |
| nlsr->in_content.p = &incoming_content; |
| |
| nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase *)); |
| //nlsr->lsdb->version=(char *)malloc(16); |
| //nlsr->lsdb->version="0000000000000000"; |
| nlsr->lsdb->version=0; |
| |
| nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct adj_lsa), ¶m_adj_lsdb); |
| nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct name_lsa), ¶m_name_lsdb); |
| |
| nlsr->is_synch_init=1; |
| nlsr->nlsa_id=0; |
| |
| } |
| |
| int |
| main(int argc, char *argv[]) |
| { |
| int res; |
| char *config_file; |
| //int daemon_mode; |
| struct ccn_charbuf *router_prefix; |
| |
| init_nlsr(); |
| |
| while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| { |
| switch (res) |
| { |
| case 'd': |
| //daemon_mode = 1; |
| break; |
| case 'f': |
| config_file = optarg; |
| break; |
| case 'h': |
| default: |
| usage(argv[0]); |
| } |
| } |
| |
| readConfigFile(config_file); |
| |
| nlsr->ccn=ccn_create(); |
| if(ccn_connect(nlsr->ccn, NULL) == -1) |
| { |
| fprintf(stderr,"Could not connect to ccnd\n"); |
| exit(1); |
| } |
| router_prefix=ccn_charbuf_create(); |
| res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
| if(res<0) |
| { |
| fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
| exit(1); |
| } |
| |
| ccn_name_append_str(router_prefix,"nlsr"); |
| nlsr->in_interest.data=nlsr->router_name; |
| res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| if ( res < 0 ) |
| { |
| fprintf(stderr,"Failed to register interest for router\n"); |
| exit(1); |
| } |
| |
| nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
| |
| nlsr->event_send_lsdb_interest = ccn_schedule_event(nlsr->sched, 1000000, &send_lsdb_interest, NULL, 0); |
| |
| while(1) |
| { |
| ccn_schedule_run(nlsr->sched); |
| res = ccn_run(nlsr->ccn, 500); |
| |
| } |
| |
| nlsr_destroy(); |
| |
| return 0; |
| } |
| |