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