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); |
| 104 | } |
| 105 | |
| 106 | void |
| 107 | process_command_ccnneighbor(char *command) |
| 108 | { |
| 109 | if(command==NULL) |
| 110 | { |
| 111 | printf(" Wrong Command Format ( ccnneighbor router_id faceX)\n"); |
| 112 | return; |
| 113 | } |
| 114 | char *rem; |
| 115 | const char *sep=" \t\n"; |
| 116 | char *rtr_id,*face; |
| 117 | |
| 118 | rtr_id=strtok_r(command,sep,&rem); |
| 119 | if(rtr_id==NULL) |
| 120 | { |
| 121 | printf(" Wrong Command Format ( ccnneighbor router_id faceX)\n"); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | face=strtok_r(NULL,sep,&rem); |
| 126 | if(face==NULL) |
| 127 | { |
| 128 | printf(" Wrong Command Format ( ccnneighbor router_id faceX)\n"); |
| 129 | return; |
| 130 | } |
| 131 | |
| 132 | printf("Router: %s face: %s\n",rtr_id,face); |
| 133 | |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | process_conf_command(char *command) |
| 138 | { |
| 139 | const char *separators=" \t\n"; |
| 140 | char *remainder=NULL; |
| 141 | char *cmd_type=NULL; |
| 142 | |
| 143 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 144 | return; |
| 145 | |
| 146 | cmd_type=strtok_r(command,separators,&remainder); |
| 147 | |
| 148 | if(!strcmp(cmd_type,"router-name") ) |
| 149 | { |
| 150 | process_command_router_name(remainder); |
| 151 | } |
| 152 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 153 | { |
| 154 | process_command_ccnneighbor(remainder); |
| 155 | } |
| 156 | else if(!strcmp(cmd_type,"ccnname") ) |
| 157 | { |
| 158 | process_command_ccnname(remainder); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | printf("Wrong configuration Command %s \n",cmd_type); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | int |
| 167 | readConfigFile(const char *filename) |
| 168 | { |
| 169 | FILE *cfg; |
| 170 | char buf[1024]; |
| 171 | int len; |
| 172 | |
| 173 | cfg=fopen(filename, "r"); |
| 174 | |
| 175 | if(cfg == NULL) |
| 176 | { |
| 177 | printf("\nConfiguration File does not exists\n"); |
| 178 | exit(1); |
| 179 | } |
| 180 | |
| 181 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 182 | { |
| 183 | len=strlen(buf); |
| 184 | if(buf[len-1] == '\n') |
| 185 | buf[len-1]='\0'; |
| 186 | process_conf_command(buf); |
| 187 | } |
| 188 | |
| 189 | fclose(cfg); |
| 190 | |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | int |
| 195 | main(int argc, char *argv[]) |
| 196 | { |
| 197 | int res; |
| 198 | char *config_file; |
| 199 | int daemon_mode; |
| 200 | struct hashtb_param param_adl = {0}; |
| 201 | struct hashtb_param param_npl = {0}; |
| 202 | |
| 203 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
| 204 | |
| 205 | nlsr->adl=hashtb_create(200, ¶m_adl); |
| 206 | nlsr->npl = hashtb_create(200, ¶m_npl); |
| 207 | nlsr->in_interest.p = &incoming_interest; |
| 208 | nlsr->in_content.p = &incoming_content; |
| 209 | nlsr->is_synch_init=1; |
| 210 | |
| 211 | struct ccn_charbuf *router_prefix; |
| 212 | |
| 213 | while ((res = getopt_long(argc, argv, "df:h", longopts, 0)) != -1) |
| 214 | { |
| 215 | switch (res) |
| 216 | { |
| 217 | case 'd': |
| 218 | daemon_mode = 1; |
| 219 | break; |
| 220 | case 'f': |
| 221 | config_file = optarg; |
| 222 | break; |
| 223 | case 'h': |
| 224 | default: |
| 225 | usage(argv[0]); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | readConfigFile(config_file); |
| 230 | |
| 231 | nlsr->ccn=ccn_create(); |
| 232 | if(ccn_connect(nlsr->ccn, NULL) == -1) |
| 233 | { |
| 234 | fprintf(stderr,"Could not connect to ccnd\n"); |
| 235 | exit(1); |
| 236 | } |
| 237 | router_prefix=ccn_charbuf_create(); |
| 238 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
| 239 | if(res<0) |
| 240 | { |
| 241 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
| 242 | exit(1); |
| 243 | } |
| 244 | |
| 245 | ccn_name_append_str(router_prefix,"nlsr"); |
| 246 | nlsr->in_interest.data=nlsr->router_name; |
| 247 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 248 | if ( res < 0 ) |
| 249 | { |
| 250 | fprintf(stderr,"Failed to register interest for router\n"); |
| 251 | exit(1); |
| 252 | } |
| 253 | |
| 254 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
| 255 | |
| 256 | while(1) |
| 257 | { |
| 258 | ccn_schedule_run(nlsr->sched); |
| 259 | res = ccn_run(nlsr->ccn, 500); |
| 260 | |
| 261 | } |
| 262 | |
| 263 | |
| 264 | |
| 265 | hashtb_destroy(&nlsr->adl); |
| 266 | hashtb_destroy(&nlsr->npl); |
| 267 | ccn_schedule_destroy(&nlsr->sched); |
| 268 | ccn_destroy(&nlsr->ccn); |
| 269 | free(nlsr); |
| 270 | |
| 271 | return 0; |
| 272 | } |
| 273 | |