akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <stdlib.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
| 6 | #include <sys/time.h> |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 7 | #include <sys/stat.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 8 | #include <assert.h> |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <signal.h> |
| 11 | #include <sys/socket.h> |
| 12 | #include <sys/un.h> |
| 13 | #include <fcntl.h> |
| 14 | #include <sys/ioctl.h> |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 15 | #include <netinet/in.h> |
| 16 | #include <netdb.h> |
| 17 | #include <arpa/inet.h> |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 18 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 22 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 23 | #include <ccn/ccn.h> |
| 24 | #include <ccn/uri.h> |
| 25 | #include <ccn/keystore.h> |
| 26 | #include <ccn/signing.h> |
| 27 | #include <ccn/schedule.h> |
| 28 | #include <ccn/hashtb.h> |
| 29 | |
| 30 | #include "nlsr.h" |
| 31 | #include "nlsr_ndn.h" |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 32 | #include "nlsr_lsdb.h" |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 33 | #include "utility.h" |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 34 | #include "nlsr_npl.h" |
| 35 | #include "nlsr_adl.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 36 | #include "nlsr_npt.h" |
| 37 | #include "nlsr_route.h" |
| 38 | |
| 39 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 40 | #define ON_ERROR_DESTROY(resval) \ |
| 41 | { \ |
| 42 | if ((resval) < 0) { \ |
| 43 | nlsr_destroy(); \ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 44 | exit(1);\ |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 45 | } \ |
| 46 | } |
| 47 | |
| 48 | |
| 49 | #define ON_ERROR_EXIT(resval) \ |
| 50 | { \ |
| 51 | if ((resval) < 0) { \ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 52 | exit(1); \ |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 53 | } \ |
| 54 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 55 | |
| 56 | struct option longopts[] = |
| 57 | { |
| 58 | { "daemon", no_argument, NULL, 'd'}, |
| 59 | { "config_file", required_argument, NULL, 'f'}, |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 60 | { "api_port", required_argument, NULL, 'p'}, |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 61 | { "help", no_argument, NULL, 'h'}, |
| 62 | { 0 } |
| 63 | }; |
| 64 | |
| 65 | static int |
| 66 | usage(char *progname) |
| 67 | { |
| 68 | |
| 69 | printf("Usage: %s [OPTIONS...]\n\ |
| 70 | NDN routing....\n\ |
| 71 | -d, --daemon Run in daemon mode\n\ |
| 72 | -f, --config_file Specify configuration file name\n\ |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 73 | -p, --api_port port where api client will connect\n\ |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 74 | -h, --help Display this help message\n", progname); |
| 75 | |
| 76 | exit(1); |
| 77 | } |
| 78 | |
| 79 | void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result) |
| 80 | { |
| 81 | struct timeval now = {0}; |
| 82 | gettimeofday(&now, 0); |
| 83 | result->s = now.tv_sec; |
| 84 | result->micros = now.tv_usec; |
| 85 | } |
| 86 | |
| 87 | static struct ccn_gettime ndn_rtr_ticker = { |
| 88 | "timer", |
| 89 | &ndn_rtr_gettime, |
| 90 | 1000000, |
| 91 | NULL |
| 92 | }; |
| 93 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 94 | void |
| 95 | nlsr_lock(void) |
| 96 | { |
| 97 | nlsr->semaphor=NLSR_LOCKED; |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | nlsr_unlock(void) |
| 102 | { |
| 103 | nlsr->semaphor=NLSR_UNLOCKED; |
| 104 | } |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 105 | |
| 106 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 107 | nlsr_stop_signal_handler(int sig) |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 108 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 109 | signal(sig, SIG_IGN); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 110 | nlsr_destroy(); |
| 111 | exit(0); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 112 | } |
| 113 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 114 | void |
| 115 | daemonize_nlsr(void) |
| 116 | { |
| 117 | int ret; |
| 118 | pid_t process_id = 0; |
| 119 | pid_t sid = 0; |
| 120 | process_id = fork(); |
| 121 | if (process_id < 0) |
| 122 | { |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 123 | printf("Daemonization failed!\n"); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 124 | ON_ERROR_DESTROY(process_id); |
| 125 | } |
| 126 | if (process_id > 0) |
| 127 | { |
| 128 | printf("Process daemonized. Process id: %d \n", process_id); |
| 129 | ret=process_id; |
| 130 | exit(0); |
| 131 | } |
| 132 | |
| 133 | umask(0); |
| 134 | sid = setsid(); |
| 135 | if(sid < 0) |
| 136 | { |
| 137 | ON_ERROR_DESTROY(sid); |
| 138 | } |
| 139 | |
| 140 | chdir("/"); |
| 141 | close(STDIN_FILENO); |
| 142 | close(STDOUT_FILENO); |
| 143 | close(STDERR_FILENO); |
| 144 | } |
| 145 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 146 | void |
| 147 | process_command_ccnneighbor(char *command) |
| 148 | { |
| 149 | if(command==NULL) |
| 150 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 151 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | char *rem; |
| 155 | const char *sep=" \t\n"; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 156 | char *rtr_name,*face; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 157 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 158 | rtr_name=strtok_r(command,sep,&rem); |
| 159 | if(rtr_name==NULL) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 160 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 161 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 162 | return; |
| 163 | } |
| 164 | |
| 165 | face=strtok_r(NULL,sep,&rem); |
| 166 | if(face==NULL) |
| 167 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 168 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 172 | printf("Router: %s face: %s\n",rtr_name,face); |
| 173 | int face_id; |
| 174 | int res; |
| 175 | res=sscanf(face,"face%d",&face_id); |
| 176 | |
| 177 | if(res != 1 ) |
| 178 | { |
| 179 | printf(" Wrong Command Format ( ccnneighbor router_name faceX) where X is integer\n"); |
| 180 | return; |
| 181 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 182 | if ( rtr_name[strlen(rtr_name)-1] == '/' ) |
| 183 | { |
| 184 | rtr_name[strlen(rtr_name)-1]='\0'; |
| 185 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 186 | struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 187 | nbr->name=(char *)malloc(strlen(rtr_name)+1); |
| 188 | memset(nbr->name,0,strlen(rtr_name)+1); |
| 189 | memcpy(nbr->name,rtr_name,strlen(rtr_name)+1); |
| 190 | nbr->length=strlen(rtr_name)+1; |
| 191 | |
| 192 | add_nbr_to_adl(nbr,face_id); |
| 193 | |
| 194 | free(nbr->name); |
| 195 | free(nbr); |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | process_command_ccnname(char *command) |
| 200 | { |
| 201 | |
| 202 | if(command==NULL) |
| 203 | { |
| 204 | printf(" Wrong Command Format ( ccnname /name/prefix)\n"); |
| 205 | return; |
| 206 | } |
| 207 | char *rem; |
| 208 | const char *sep=" \t\n"; |
| 209 | char *name; |
| 210 | name=strtok_r(command,sep,&rem); |
| 211 | if(name==NULL) |
| 212 | { |
| 213 | printf(" Wrong Command Format ( ccnname /name/prefix/ )\n"); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | printf("Name Prefix: %s \n",name); |
| 218 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 219 | if ( name[strlen(name)-1] == '/' ) |
| 220 | name[strlen(name)-1]='\0'; |
| 221 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 222 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 223 | np->name=(char *)malloc(strlen(name)+1); |
| 224 | memset(np->name,0,strlen(name)+1); |
| 225 | memcpy(np->name,name,strlen(name)+1); |
| 226 | np->length=strlen(name)+1; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 227 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 228 | add_name_to_npl(np); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 229 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 230 | free(np->name); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 231 | free(np); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 232 | } |
| 233 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 234 | |
| 235 | void |
| 236 | process_command_router_name(char *command) |
| 237 | { |
| 238 | if(command==NULL) |
| 239 | { |
| 240 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 241 | return; |
| 242 | } |
| 243 | char *rem; |
| 244 | const char *sep=" \t\n"; |
| 245 | char *rtr_name; |
| 246 | |
| 247 | rtr_name=strtok_r(command,sep,&rem); |
| 248 | if(rtr_name==NULL) |
| 249 | { |
| 250 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 255 | if ( rtr_name[strlen(rtr_name)-1] == '/' ) |
| 256 | rtr_name[strlen(rtr_name)-1]='\0'; |
| 257 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 258 | nlsr->router_name=(char *)malloc(strlen(rtr_name)+1); |
| 259 | memset(nlsr->router_name,0,strlen(rtr_name)+1); |
| 260 | memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1); |
| 261 | |
| 262 | |
| 263 | } |
| 264 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 265 | void |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 266 | process_command_lsdb_synch_interval(char *command) |
| 267 | { |
| 268 | if(command==NULL) |
| 269 | { |
| 270 | printf(" Wrong Command Format ( lsdb-synch-interval secs )\n"); |
| 271 | return; |
| 272 | } |
| 273 | char *rem; |
| 274 | const char *sep=" \t\n"; |
| 275 | char *secs; |
| 276 | long int seconds; |
| 277 | |
| 278 | secs=strtok_r(command,sep,&rem); |
| 279 | if(secs==NULL) |
| 280 | { |
| 281 | printf(" Wrong Command Format ( lsdb-synch-interval secs)\n"); |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | seconds=atoi(secs); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 286 | if ( seconds >= 120 && seconds <= 3600 ) |
| 287 | { |
| 288 | nlsr->lsdb_synch_interval=seconds; |
| 289 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 290 | |
| 291 | } |
| 292 | |
| 293 | |
| 294 | void |
| 295 | process_command_interest_retry(char *command) |
| 296 | { |
| 297 | if(command==NULL) |
| 298 | { |
| 299 | printf(" Wrong Command Format ( interest-retry number )\n"); |
| 300 | return; |
| 301 | } |
| 302 | char *rem; |
| 303 | const char *sep=" \t\n"; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 304 | char *retry; |
| 305 | long int retry_number; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 306 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 307 | retry=strtok_r(command,sep,&rem); |
| 308 | if(retry==NULL) |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 309 | { |
| 310 | printf(" Wrong Command Format ( interest-retry number)\n"); |
| 311 | return; |
| 312 | } |
| 313 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 314 | retry_number=atoi(retry); |
| 315 | if ( retry_number >= 1 && retry_number<=10 ) |
| 316 | { |
| 317 | nlsr->interest_retry=retry_number; |
| 318 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 319 | |
| 320 | } |
| 321 | |
| 322 | void |
| 323 | process_command_interest_resend_time(char *command) |
| 324 | { |
| 325 | if(command==NULL) |
| 326 | { |
| 327 | printf(" Wrong Command Format ( interest-resend-time secs )\n"); |
| 328 | return; |
| 329 | } |
| 330 | char *rem; |
| 331 | const char *sep=" \t\n"; |
| 332 | char *secs; |
| 333 | long int seconds; |
| 334 | |
| 335 | secs=strtok_r(command,sep,&rem); |
| 336 | if(secs==NULL) |
| 337 | { |
| 338 | printf(" Wrong Command Format ( interest-resend-time secs)\n"); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | seconds=atoi(secs); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 343 | if ( seconds <= 60 && seconds >= 1 ) |
| 344 | { |
| 345 | nlsr->interest_resend_time=seconds; |
| 346 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 347 | } |
| 348 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 349 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 350 | void |
| 351 | process_command_lsa_refresh_time(char *command) |
| 352 | { |
| 353 | if(command==NULL) |
| 354 | { |
| 355 | printf(" Wrong Command Format ( lsa-refresh-time secs )\n"); |
| 356 | return; |
| 357 | } |
| 358 | char *rem; |
| 359 | const char *sep=" \t\n"; |
| 360 | char *secs; |
| 361 | long int seconds; |
| 362 | |
| 363 | secs=strtok_r(command,sep,&rem); |
| 364 | if(secs==NULL) |
| 365 | { |
| 366 | printf(" Wrong Command Format ( lsa-refresh-time secs)\n"); |
| 367 | return; |
| 368 | } |
| 369 | |
| 370 | seconds=atoi(secs); |
| 371 | if ( seconds >= 240 && seconds <= 3600 ) |
| 372 | { |
| 373 | nlsr->lsa_refresh_time=seconds; |
| 374 | } |
| 375 | |
| 376 | } |
| 377 | |
| 378 | void |
| 379 | process_command_router_dead_interval(char *command) |
| 380 | { |
| 381 | if(command==NULL) |
| 382 | { |
| 383 | printf(" Wrong Command Format ( router-dead-interval secs )\n"); |
| 384 | return; |
| 385 | } |
| 386 | char *rem; |
| 387 | const char *sep=" \t\n"; |
| 388 | char *secs; |
| 389 | long int seconds; |
| 390 | |
| 391 | secs=strtok_r(command,sep,&rem); |
| 392 | if(secs==NULL) |
| 393 | { |
| 394 | printf(" Wrong Command Format ( router-dead-interval secs)\n"); |
| 395 | return; |
| 396 | } |
| 397 | |
| 398 | seconds=atoi(secs); |
| 399 | if ( seconds >= 360 && seconds <= 5400 ) |
| 400 | { |
| 401 | nlsr->router_dead_interval=seconds; |
| 402 | } |
| 403 | |
| 404 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 405 | |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 406 | void |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 407 | process_command_multi_path_face_num(char *command) |
| 408 | { |
| 409 | if(command==NULL) |
| 410 | { |
| 411 | printf(" Wrong Command Format ( multi-path-face-num n )\n"); |
| 412 | return; |
| 413 | } |
| 414 | char *rem; |
| 415 | const char *sep=" \t\n"; |
| 416 | char *num; |
| 417 | long int number; |
| 418 | |
| 419 | num=strtok_r(command,sep,&rem); |
| 420 | if(num==NULL) |
| 421 | { |
| 422 | printf(" Wrong Command Format ( multi-path-face-num n)\n"); |
| 423 | return; |
| 424 | } |
| 425 | |
| 426 | number=atoi(num); |
| 427 | if ( number >= 0 && number <= 60 ) |
| 428 | { |
| 429 | nlsr->multi_path_face_num=number; |
| 430 | } |
| 431 | |
| 432 | } |
| 433 | |
| 434 | void |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 435 | process_command_logdir(char *command) |
| 436 | { |
| 437 | if(command==NULL) |
| 438 | { |
| 439 | printf(" Wrong Command Format ( logdir /path/to/logdir )\n"); |
| 440 | return; |
| 441 | } |
| 442 | char *rem; |
| 443 | const char *sep=" \t\n"; |
| 444 | char *dir; |
| 445 | |
| 446 | dir=strtok_r(command,sep,&rem); |
| 447 | if(dir==NULL) |
| 448 | { |
| 449 | printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n"); |
| 450 | return; |
| 451 | } |
| 452 | |
| 453 | nlsr->logDir=(char *)malloc(strlen(dir)+1); |
| 454 | memset(nlsr->logDir,0,strlen(dir)+1); |
| 455 | memcpy(nlsr->logDir,dir,strlen(dir)); |
| 456 | } |
| 457 | |
| 458 | void |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 459 | process_command_detailed_log(char *command) |
| 460 | { |
| 461 | if(command==NULL) |
| 462 | { |
| 463 | printf(" Wrong Command Format ( detailed-log on/off )\n"); |
| 464 | return; |
| 465 | } |
| 466 | char *rem; |
| 467 | const char *sep=" \t\n"; |
| 468 | char *on_off; |
| 469 | |
| 470 | on_off=strtok_r(command,sep,&rem); |
| 471 | if(on_off==NULL) |
| 472 | { |
| 473 | printf(" Wrong Command Format ( detailed-log on/off )\n"); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0) |
| 478 | { |
| 479 | nlsr->detailed_logging=1; |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | void |
| 484 | process_command_debug(char *command) |
| 485 | { |
| 486 | if(command==NULL) |
| 487 | { |
| 488 | printf(" Wrong Command Format ( debug on/off )\n"); |
| 489 | return; |
| 490 | } |
| 491 | char *rem; |
| 492 | const char *sep=" \t\n"; |
| 493 | char *on_off; |
| 494 | |
| 495 | on_off=strtok_r(command,sep,&rem); |
| 496 | if(on_off==NULL) |
| 497 | { |
| 498 | printf(" Wrong Command Format ( debug on/off )\n"); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 ) |
| 503 | { |
| 504 | nlsr->debugging=1; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 509 | process_conf_command(char *command) |
| 510 | { |
| 511 | const char *separators=" \t\n"; |
| 512 | char *remainder=NULL; |
| 513 | char *cmd_type=NULL; |
| 514 | |
| 515 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 516 | return; |
| 517 | |
| 518 | cmd_type=strtok_r(command,separators,&remainder); |
| 519 | |
| 520 | if(!strcmp(cmd_type,"router-name") ) |
| 521 | { |
| 522 | process_command_router_name(remainder); |
| 523 | } |
| 524 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 525 | { |
| 526 | process_command_ccnneighbor(remainder); |
| 527 | } |
| 528 | else if(!strcmp(cmd_type,"ccnname") ) |
| 529 | { |
| 530 | process_command_ccnname(remainder); |
| 531 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 532 | else if(!strcmp(cmd_type,"lsdb-synch-interval") ) |
| 533 | { |
| 534 | process_command_lsdb_synch_interval(remainder); |
| 535 | } |
| 536 | else if(!strcmp(cmd_type,"interest-retry") ) |
| 537 | { |
| 538 | process_command_interest_retry(remainder); |
| 539 | } |
| 540 | else if(!strcmp(cmd_type,"interest-resend-time") ) |
| 541 | { |
| 542 | process_command_interest_resend_time(remainder); |
| 543 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 544 | else if(!strcmp(cmd_type,"lsa-refresh-time") ) |
| 545 | { |
| 546 | process_command_lsa_refresh_time(remainder); |
| 547 | } |
| 548 | else if(!strcmp(cmd_type,"router-dead-interval") ) |
| 549 | { |
| 550 | process_command_router_dead_interval(remainder); |
| 551 | } |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 552 | else if(!strcmp(cmd_type,"multi-path-face-num") ) |
| 553 | { |
| 554 | process_command_multi_path_face_num(remainder); |
| 555 | } |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 556 | else if(!strcmp(cmd_type,"logdir") ) |
| 557 | { |
| 558 | process_command_logdir(remainder); |
| 559 | } |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 560 | else if(!strcmp(cmd_type,"detailed-log") ) |
| 561 | { |
| 562 | process_command_detailed_log(remainder); |
| 563 | } |
| 564 | else if(!strcmp(cmd_type,"debug") ) |
| 565 | { |
| 566 | process_command_debug(remainder); |
| 567 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 568 | else |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 569 | { |
| 570 | printf("Wrong configuration Command %s \n",cmd_type); |
| 571 | } |
| 572 | } |
| 573 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 574 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 575 | int |
| 576 | readConfigFile(const char *filename) |
| 577 | { |
| 578 | FILE *cfg; |
| 579 | char buf[1024]; |
| 580 | int len; |
| 581 | |
| 582 | cfg=fopen(filename, "r"); |
| 583 | |
| 584 | if(cfg == NULL) |
| 585 | { |
| 586 | printf("\nConfiguration File does not exists\n"); |
| 587 | exit(1); |
| 588 | } |
| 589 | |
| 590 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 591 | { |
| 592 | len=strlen(buf); |
| 593 | if(buf[len-1] == '\n') |
| 594 | buf[len-1]='\0'; |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 595 | if ( buf[0] != '#' && buf[0] != '!') |
| 596 | process_conf_command(buf); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | fclose(cfg); |
| 600 | |
| 601 | return 0; |
| 602 | } |
| 603 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 604 | char * |
| 605 | process_api_client_command(char *command) |
| 606 | { |
| 607 | char *msg; |
| 608 | msg=(char *)malloc(100); |
| 609 | memset(msg,100,0); |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 610 | //strcpy(msg,"Action Carried Out for NLSR Api Client"); |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 611 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 612 | const char *sep=" \t\n"; |
| 613 | char *rem=NULL; |
| 614 | char *cmd_type=NULL; |
| 615 | char *op_type=NULL; |
| 616 | char *name=NULL; |
| 617 | char *face=NULL; |
| 618 | int face_id; |
| 619 | int res; |
| 620 | |
| 621 | op_type=strtok_r(command,sep,&rem); |
| 622 | cmd_type=strtok_r(NULL,sep,&rem); |
| 623 | name=strtok_r(NULL,sep,&rem); |
| 624 | if ( name[strlen(name)-1] == '/' ) |
| 625 | name[strlen(name)-1]='\0'; |
| 626 | |
| 627 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 628 | np->name=(char *)malloc(strlen(name)+1); |
| 629 | memset(np->name,0,strlen(name)+1); |
| 630 | memcpy(np->name,name,strlen(name)+1); |
| 631 | np->length=strlen(name)+1; |
| 632 | |
| 633 | if ( strcmp(cmd_type,"name")!= 0 ) |
| 634 | { |
| 635 | face=strtok_r(NULL,sep,&rem); |
| 636 | sscanf(face,"face%d",&face_id); |
| 637 | } |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 638 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 639 | if ( strcmp(cmd_type,"name")== 0 ) |
| 640 | { |
| 641 | if ( strcmp(op_type,"del") == 0 ) |
| 642 | { |
| 643 | res=does_name_exist_in_npl(np); |
| 644 | if ( res == 0) |
| 645 | { |
| 646 | sprintf(msg,"Name %s does not exist !!",name); |
| 647 | } |
| 648 | else |
| 649 | { |
| 650 | long int ls_id=get_lsa_id_from_npl(np); |
| 651 | if ( ls_id != 0 ) |
| 652 | { |
| 653 | make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id); |
| 654 | sprintf(msg,"Name %s has been deleted and Advertised.",name); |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | sprintf(msg,"Name %s does not have an Name LSA yet !!",name); |
| 659 | } |
| 660 | } |
| 661 | } |
| 662 | else if ( strcmp(op_type,"add") == 0 ) |
| 663 | { |
| 664 | res=does_name_exist_in_npl(np); |
| 665 | if ( res == 0) |
| 666 | { |
| 667 | add_name_to_npl(np); |
| 668 | build_and_install_single_name_lsa(np); |
| 669 | sprintf(msg,"Name %s has been added to advertise.",name); |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | sprintf(msg,"Name %s has already been advertised from this router !!",name); |
| 674 | } |
| 675 | } |
| 676 | } |
| 677 | else if ( strcmp(cmd_type,"neighbor") == 0 ) |
| 678 | { |
| 679 | if ( strcmp(op_type,"del") == 0 ) |
| 680 | { |
| 681 | res=is_neighbor(np->name); |
| 682 | if ( res == 0) |
| 683 | { |
| 684 | sprintf(msg,"Neighbor %s does not exist !!",name); |
| 685 | } |
| 686 | else |
| 687 | { |
| 688 | update_adjacent_status_to_adl(np,NBR_DOWN); |
| 689 | delete_nbr_from_adl(np); |
| 690 | if(!nlsr->is_build_adj_lsa_sheduled) |
| 691 | { |
| 692 | nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0); |
| 693 | nlsr->is_build_adj_lsa_sheduled=1; |
| 694 | } |
| 695 | sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name); |
| 696 | } |
| 697 | } |
| 698 | else if ( strcmp(op_type,"add") == 0 ) |
| 699 | { |
| 700 | res=is_neighbor(np->name); |
| 701 | if ( res == 0 ) |
| 702 | { |
| 703 | add_nbr_to_adl(np,face_id); |
| 704 | sprintf(msg,"Neighbor %s has been added to adjacency list.",name); |
| 705 | } |
| 706 | else |
| 707 | { |
| 708 | sprintf(msg,"Neighbor %s already exists in adjacency list.",name); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 713 | |
| 714 | return msg; |
| 715 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 716 | |
| 717 | int |
| 718 | nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd) |
| 719 | { |
| 720 | struct timeval timeout; |
| 721 | if ( time_out_micro_sec < 0 ) |
| 722 | { |
| 723 | timeout.tv_sec=1; |
| 724 | timeout.tv_usec=0; |
| 725 | } |
| 726 | else |
| 727 | { |
| 728 | time_out_micro_sec=(long int)time_out_micro_sec*0.4; |
| 729 | timeout.tv_sec=time_out_micro_sec / 1000000; |
| 730 | timeout.tv_usec=time_out_micro_sec % 1000000; |
| 731 | } |
| 732 | |
| 733 | |
| 734 | int fd; |
| 735 | int nread; |
| 736 | int result; |
| 737 | fd_set testfds; |
| 738 | unsigned int client_len; |
| 739 | int client_sockfd; |
| 740 | char recv_buffer[1024]; |
| 741 | bzero(recv_buffer,1024); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 742 | struct sockaddr_in client_address; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 743 | |
| 744 | testfds=nlsr->readfds; |
| 745 | result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout); |
| 746 | |
| 747 | for(fd = 0; fd < FD_SETSIZE; fd++) |
| 748 | { |
| 749 | if(FD_ISSET(fd,&testfds)) |
| 750 | { |
| 751 | if ( fd == ccn_fd ) |
| 752 | { |
| 753 | return 0; |
| 754 | } |
| 755 | else if(fd == nlsr->nlsr_api_server_sock_fd) |
| 756 | { |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame] | 757 | printf("Setting up socket....\n"); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 758 | client_len = sizeof(client_address); |
| 759 | client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len); |
| 760 | FD_SET(client_sockfd, &nlsr->readfds); |
| 761 | } |
| 762 | else |
| 763 | { |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame] | 764 | printf("Else...\n"); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 765 | ioctl(fd, FIONREAD, &nread); |
| 766 | if(nread == 0) |
| 767 | { |
| 768 | close(fd); |
| 769 | FD_CLR(fd, &nlsr->readfds); |
| 770 | } |
| 771 | else |
| 772 | { |
| 773 | recv(fd, recv_buffer, 1024, 0); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame] | 774 | printf("Test Received Data from NLSR API cleint: %s \n",recv_buffer); |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 775 | char *msg=process_api_client_command(recv_buffer); |
| 776 | send(fd, msg, strlen(msg),0); |
| 777 | free(msg); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 778 | close(fd); |
| 779 | FD_CLR(fd, &nlsr->readfds); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 788 | void |
| 789 | nlsr_destroy( void ) |
| 790 | { |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 791 | if ( nlsr->debugging ) |
| 792 | { |
| 793 | printf("Freeing Allocated Memory....\n"); |
| 794 | } |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 795 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n"); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 796 | /* Destroying all face created by nlsr in CCND */ |
| 797 | destroy_all_face_by_nlsr(); |
| 798 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 799 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 800 | hashtb_destroy(&nlsr->npl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 801 | hashtb_destroy(&nlsr->adl); |
| 802 | hashtb_destroy(&nlsr->lsdb->name_lsdb); |
| 803 | hashtb_destroy(&nlsr->lsdb->adj_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 804 | hashtb_destroy(&nlsr->pit_alsa); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 805 | |
| 806 | //To Do: has to destroy the face_list one by one |
| 807 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 808 | hashtb_destroy(&nlsr->routing_table); |
| 809 | |
| 810 | |
| 811 | int i, npt_element; |
| 812 | struct npt_entry *ne; |
| 813 | struct hashtb_enumerator ee; |
| 814 | struct hashtb_enumerator *e = ⅇ |
| 815 | hashtb_start(nlsr->npt, e); |
| 816 | npt_element=hashtb_n(nlsr->npt); |
| 817 | for(i=0;i<npt_element;i++) |
| 818 | { |
| 819 | ne=e->data; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 820 | hashtb_destroy(&ne->name_list); |
| 821 | hashtb_destroy(&ne->face_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 822 | hashtb_next(e); |
| 823 | } |
| 824 | |
| 825 | hashtb_end(e); |
| 826 | hashtb_destroy(&nlsr->npt); |
| 827 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 828 | |
| 829 | close(nlsr->nlsr_api_server_sock_fd); |
| 830 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 831 | ccn_schedule_destroy(&nlsr->sched); |
| 832 | ccn_destroy(&nlsr->ccn); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 833 | |
| 834 | free(nlsr->lsdb->lsdb_version); |
| 835 | free(nlsr->lsdb); |
| 836 | free(nlsr->router_name); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 837 | free(nlsr); |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 838 | if ( nlsr->debugging ) |
| 839 | { |
| 840 | printf("Finished freeing allocated memory\n"); |
| 841 | } |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 842 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 843 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 844 | } |
| 845 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 846 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 847 | void |
| 848 | init_api_server(int ccn_fd) |
| 849 | { |
| 850 | int server_sockfd; |
| 851 | int server_len; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 852 | struct sockaddr_in server_address; |
akmhoque | f31f13b | 2012-11-16 09:42:24 -0600 | [diff] [blame] | 853 | unsigned int yes=1; |
| 854 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 855 | server_sockfd = socket(AF_INET, SOCK_STREAM, 0); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 856 | |
| 857 | int flags = fcntl(server_sockfd, F_GETFL, 0); |
| 858 | fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags); |
| 859 | |
akmhoque | f31f13b | 2012-11-16 09:42:24 -0600 | [diff] [blame] | 860 | if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) |
| 861 | { |
| 862 | ON_ERROR_DESTROY(-1); |
| 863 | } |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 864 | |
| 865 | server_address.sin_family = AF_INET; |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame] | 866 | //server_address.sin_addr.s_addr = inet_addr("127.0.0.1"); |
| 867 | server_address.sin_addr.s_addr = INADDR_ANY; |
| 868 | server_address.sin_port = htons(nlsr->api_port); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 869 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 870 | server_len = sizeof(server_address); |
| 871 | bind(server_sockfd, (struct sockaddr *)&server_address, server_len); |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame] | 872 | //printf("port number %d\n", ntohs(server_address.sin_port)); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 873 | listen(server_sockfd, 100); |
| 874 | FD_ZERO(&nlsr->readfds); |
| 875 | FD_SET(server_sockfd, &nlsr->readfds); |
| 876 | FD_SET(ccn_fd, &nlsr->readfds); |
| 877 | nlsr->nlsr_api_server_sock_fd=server_sockfd; |
| 878 | |
| 879 | } |
| 880 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 881 | int |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 882 | init_nlsr(void) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 883 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 884 | if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 885 | { |
| 886 | perror("SIGQUIT install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 887 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 888 | } |
| 889 | if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR) |
| 890 | { |
| 891 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 892 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 893 | } |
| 894 | if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 895 | { |
| 896 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 897 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 898 | } |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 899 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 900 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 901 | |
| 902 | struct hashtb_param param_adl = {0}; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 903 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 904 | struct hashtb_param param_npl = {0}; |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 905 | nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), ¶m_npl); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 906 | struct hashtb_param param_pit_alsa = {0}; |
akmhoque | 1ce7105 | 2012-09-13 22:51:32 -0500 | [diff] [blame] | 907 | nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 908 | struct hashtb_param param_npt = {0}; |
| 909 | nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt); |
| 910 | struct hashtb_param param_rte = {0}; |
| 911 | nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_rte); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 912 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 913 | nlsr->in_interest.p = &incoming_interest; |
| 914 | nlsr->in_content.p = &incoming_content; |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 915 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 916 | nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase)); |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 917 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 918 | char *time_stamp=(char *)malloc(20); |
| 919 | memset(time_stamp,0,20); |
| 920 | get_current_timestamp_micro(time_stamp); |
| 921 | nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1); |
| 922 | memset(nlsr->lsdb->lsdb_version,'0',strlen(time_stamp)); |
| 923 | free(time_stamp); |
| 924 | |
| 925 | struct hashtb_param param_adj_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 926 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 927 | struct hashtb_param param_name_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 928 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 929 | |
| 930 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 931 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 932 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 933 | nlsr->is_synch_init=1; |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 934 | nlsr->nlsa_id=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 935 | nlsr->adj_build_flag=0; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 936 | nlsr->adj_build_count=0; |
| 937 | nlsr->is_build_adj_lsa_sheduled=0; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 938 | nlsr->is_send_lsdb_interest_scheduled=0; |
| 939 | nlsr->is_route_calculation_scheduled=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 940 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 941 | nlsr->detailed_logging=0; |
| 942 | nlsr->debugging=0; |
| 943 | |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 944 | nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL; |
| 945 | nlsr->interest_retry = INTEREST_RETRY; |
| 946 | nlsr->interest_resend_time = INTEREST_RESEND_TIME; |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 947 | nlsr->lsa_refresh_time=LSA_REFRESH_TIME; |
| 948 | nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 949 | nlsr->multi_path_face_num=MULTI_PATH_FACE_NUM; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 950 | nlsr->semaphor=NLSR_UNLOCKED; |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 951 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 952 | nlsr->api_port=API_PORT; |
| 953 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 954 | return 0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 955 | } |
| 956 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 957 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 958 | int |
| 959 | main(int argc, char *argv[]) |
| 960 | { |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 961 | int res, ret; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 962 | char *config_file; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 963 | int daemon_mode=0; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 964 | int port=0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 965 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 966 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 967 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 968 | while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 969 | { |
| 970 | switch (res) |
| 971 | { |
| 972 | case 'd': |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 973 | daemon_mode = 1; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 974 | break; |
| 975 | case 'f': |
| 976 | config_file = optarg; |
| 977 | break; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 978 | case 'p': |
| 979 | port = atoi(optarg); |
| 980 | break; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 981 | case 'h': |
| 982 | default: |
| 983 | usage(argv[0]); |
| 984 | } |
| 985 | } |
| 986 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 987 | ret=init_nlsr(); |
| 988 | ON_ERROR_EXIT(ret); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 989 | |
| 990 | if ( port !=0 ) |
| 991 | nlsr->api_port=port; |
| 992 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 993 | readConfigFile(config_file); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 994 | if ( daemon_mode == 1 ) |
| 995 | { |
| 996 | daemonize_nlsr(); |
| 997 | } |
| 998 | |
| 999 | startLogging(nlsr->logDir); |
| 1000 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1001 | nlsr->ccn=ccn_create(); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1002 | int ccn_fd=ccn_connect(nlsr->ccn, NULL); |
| 1003 | if(ccn_fd == -1) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1004 | { |
| 1005 | fprintf(stderr,"Could not connect to ccnd\n"); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1006 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1007 | ON_ERROR_DESTROY(-1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1008 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1009 | |
| 1010 | init_api_server(ccn_fd); |
| 1011 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1012 | struct ccn_charbuf *router_prefix; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1013 | router_prefix=ccn_charbuf_create(); |
| 1014 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1015 | if(res<0) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1016 | { |
| 1017 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1018 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1019 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1020 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1021 | |
| 1022 | ccn_name_append_str(router_prefix,"nlsr"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1023 | nlsr->in_interest.data=nlsr->router_name; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1024 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 1025 | if ( res < 0 ) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1026 | { |
| 1027 | fprintf(stderr,"Failed to register interest for router\n"); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1028 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1029 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1030 | } |
| 1031 | ccn_charbuf_destroy(&router_prefix); |
| 1032 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1033 | if ( nlsr->debugging ) |
| 1034 | printf("Router Name : %s\n",nlsr->router_name); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1035 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name); |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1036 | if ( nlsr->debugging ) |
| 1037 | printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1038 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1039 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1040 | print_name_prefix_from_npl(); |
| 1041 | print_adjacent_from_adl(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1042 | build_and_install_name_lsas(); |
| 1043 | print_name_lsdb(); |
| 1044 | |
| 1045 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1046 | 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] | 1047 | nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 1048 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1049 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1050 | while(1) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1051 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1052 | if ( nlsr->semaphor == NLSR_UNLOCKED ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1053 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1054 | if( nlsr->sched != NULL ) |
| 1055 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1056 | long int micro_sec=ccn_schedule_run(nlsr->sched); |
| 1057 | res=nlsr_api_server_poll(micro_sec,ccn_fd); |
| 1058 | ON_ERROR_DESTROY(res); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1059 | } |
| 1060 | if(nlsr->ccn != NULL) |
| 1061 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1062 | res = ccn_run(nlsr->ccn, 0); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1063 | } |
| 1064 | if (!(nlsr->sched && nlsr->ccn)) |
| 1065 | { |
| 1066 | break; |
| 1067 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1068 | } |
| 1069 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1070 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1071 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1072 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1073 | return 0; |
| 1074 | } |
| 1075 | |