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> |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 29 | #include <ccn/sync.h> |
| 30 | #include <ccn/seqwriter.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 31 | |
| 32 | #include "nlsr.h" |
| 33 | #include "nlsr_ndn.h" |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 34 | #include "nlsr_lsdb.h" |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 35 | #include "utility.h" |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 36 | #include "nlsr_npl.h" |
| 37 | #include "nlsr_adl.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 38 | #include "nlsr_npt.h" |
| 39 | #include "nlsr_route.h" |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 40 | #include "nlsr_sync.h" |
| 41 | #include "nlsr_face.h" |
| 42 | #include "nlsr_fib.h" |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 43 | |
| 44 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 45 | #define ON_ERROR_DESTROY(resval) \ |
| 46 | { \ |
| 47 | if ((resval) < 0) { \ |
| 48 | nlsr_destroy(); \ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 49 | exit(1);\ |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 50 | } \ |
| 51 | } |
| 52 | |
| 53 | |
| 54 | #define ON_ERROR_EXIT(resval) \ |
| 55 | { \ |
| 56 | if ((resval) < 0) { \ |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 57 | exit(1); \ |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 58 | } \ |
| 59 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 60 | |
| 61 | struct option longopts[] = |
| 62 | { |
| 63 | { "daemon", no_argument, NULL, 'd'}, |
| 64 | { "config_file", required_argument, NULL, 'f'}, |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 65 | { "api_port", required_argument, NULL, 'p'}, |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 66 | { "help", no_argument, NULL, 'h'}, |
| 67 | { 0 } |
| 68 | }; |
| 69 | |
| 70 | static int |
| 71 | usage(char *progname) |
| 72 | { |
| 73 | |
| 74 | printf("Usage: %s [OPTIONS...]\n\ |
| 75 | NDN routing....\n\ |
| 76 | -d, --daemon Run in daemon mode\n\ |
| 77 | -f, --config_file Specify configuration file name\n\ |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 78 | -p, --api_port port where api client will connect\n\ |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 79 | -h, --help Display this help message\n", progname); |
| 80 | |
| 81 | exit(1); |
| 82 | } |
| 83 | |
| 84 | void ndn_rtr_gettime(const struct ccn_gettime *self, struct ccn_timeval *result) |
| 85 | { |
| 86 | struct timeval now = {0}; |
| 87 | gettimeofday(&now, 0); |
| 88 | result->s = now.tv_sec; |
| 89 | result->micros = now.tv_usec; |
| 90 | } |
| 91 | |
| 92 | static struct ccn_gettime ndn_rtr_ticker = { |
| 93 | "timer", |
| 94 | &ndn_rtr_gettime, |
| 95 | 1000000, |
| 96 | NULL |
| 97 | }; |
| 98 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 99 | void |
| 100 | nlsr_lock(void) |
| 101 | { |
| 102 | nlsr->semaphor=NLSR_LOCKED; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | nlsr_unlock(void) |
| 107 | { |
| 108 | nlsr->semaphor=NLSR_UNLOCKED; |
| 109 | } |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 110 | |
| 111 | void |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 112 | nlsr_stop_signal_handler(int sig) |
akmhoque | 42098b1 | 2012-08-27 22:54:23 -0500 | [diff] [blame] | 113 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 114 | signal(sig, SIG_IGN); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 115 | nlsr_destroy(); |
| 116 | exit(0); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 117 | } |
| 118 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 119 | void |
| 120 | daemonize_nlsr(void) |
| 121 | { |
| 122 | int ret; |
| 123 | pid_t process_id = 0; |
| 124 | pid_t sid = 0; |
| 125 | process_id = fork(); |
| 126 | if (process_id < 0) |
| 127 | { |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 128 | printf("Daemonization failed!\n"); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 129 | ON_ERROR_DESTROY(process_id); |
| 130 | } |
| 131 | if (process_id > 0) |
| 132 | { |
| 133 | printf("Process daemonized. Process id: %d \n", process_id); |
| 134 | ret=process_id; |
| 135 | exit(0); |
| 136 | } |
| 137 | |
| 138 | umask(0); |
| 139 | sid = setsid(); |
| 140 | if(sid < 0) |
| 141 | { |
| 142 | ON_ERROR_DESTROY(sid); |
| 143 | } |
| 144 | |
| 145 | chdir("/"); |
| 146 | close(STDIN_FILENO); |
| 147 | close(STDOUT_FILENO); |
| 148 | close(STDERR_FILENO); |
| 149 | } |
| 150 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 151 | void |
| 152 | process_command_ccnneighbor(char *command) |
| 153 | { |
| 154 | if(command==NULL) |
| 155 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 156 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 157 | return; |
| 158 | } |
| 159 | char *rem; |
| 160 | const char *sep=" \t\n"; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 161 | char *rtr_name; |
| 162 | char *nbr_ip_addr; |
| 163 | int is_ip_configured=0; |
| 164 | //char *face; |
| 165 | char *ip_addr=(char *)malloc(13); |
| 166 | memset(ip_addr,0,13); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 167 | |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 168 | rtr_name=strtok_r(command,sep,&rem); |
| 169 | if(rtr_name==NULL) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 170 | { |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 171 | printf(" Wrong Command Format ( ccnneighbor router_name faceX)\n"); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 172 | return; |
| 173 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 174 | if ( rtr_name[strlen(rtr_name)-1] == '/' ) |
| 175 | { |
| 176 | rtr_name[strlen(rtr_name)-1]='\0'; |
| 177 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 178 | |
| 179 | if (rem != NULL ) |
| 180 | { |
| 181 | nbr_ip_addr=strtok_r(NULL,sep,&rem); |
| 182 | is_ip_configured=1; |
| 183 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 184 | struct name_prefix *nbr=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 185 | nbr->name=(char *)malloc(strlen(rtr_name)+1); |
| 186 | memset(nbr->name,0,strlen(rtr_name)+1); |
| 187 | memcpy(nbr->name,rtr_name,strlen(rtr_name)+1); |
| 188 | nbr->length=strlen(rtr_name)+1; |
| 189 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 190 | |
| 191 | if ( !is_ip_configured ) |
| 192 | { |
| 193 | struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 194 | get_host_name_from_command_string(nbr_name,nbr->name,0); |
| 195 | printf("Hostname of neighbor: %s ",nbr_name->name); |
| 196 | get_ip_from_hostname_02(nbr_name->name,ip_addr); |
| 197 | printf("IP Address: %s \n",ip_addr); |
| 198 | free(nbr_name->name); |
| 199 | free(nbr_name); |
| 200 | } |
| 201 | else |
| 202 | { |
| 203 | memcpy(ip_addr,nbr_ip_addr,strlen(nbr_ip_addr)); |
| 204 | printf("Name of neighbor: %s ",nbr->name); |
| 205 | printf("IP Address: %s \n",ip_addr); |
| 206 | } |
| 207 | add_nbr_to_adl(nbr,0,ip_addr); |
| 208 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 209 | |
| 210 | free(nbr->name); |
| 211 | free(nbr); |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | process_command_ccnname(char *command) |
| 216 | { |
| 217 | |
| 218 | if(command==NULL) |
| 219 | { |
| 220 | printf(" Wrong Command Format ( ccnname /name/prefix)\n"); |
| 221 | return; |
| 222 | } |
| 223 | char *rem; |
| 224 | const char *sep=" \t\n"; |
| 225 | char *name; |
| 226 | name=strtok_r(command,sep,&rem); |
| 227 | if(name==NULL) |
| 228 | { |
| 229 | printf(" Wrong Command Format ( ccnname /name/prefix/ )\n"); |
| 230 | return; |
| 231 | } |
| 232 | |
| 233 | printf("Name Prefix: %s \n",name); |
| 234 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 235 | if ( name[strlen(name)-1] == '/' ) |
| 236 | name[strlen(name)-1]='\0'; |
| 237 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 238 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 239 | np->name=(char *)malloc(strlen(name)+1); |
| 240 | memset(np->name,0,strlen(name)+1); |
| 241 | memcpy(np->name,name,strlen(name)+1); |
| 242 | np->length=strlen(name)+1; |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 243 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 244 | add_name_to_npl(np); |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 245 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 246 | free(np->name); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 247 | free(np); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 248 | } |
| 249 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 250 | |
| 251 | void |
| 252 | process_command_router_name(char *command) |
| 253 | { |
| 254 | if(command==NULL) |
| 255 | { |
| 256 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 257 | return; |
| 258 | } |
| 259 | char *rem; |
| 260 | const char *sep=" \t\n"; |
| 261 | char *rtr_name; |
| 262 | |
| 263 | rtr_name=strtok_r(command,sep,&rem); |
| 264 | if(rtr_name==NULL) |
| 265 | { |
| 266 | printf(" Wrong Command Format ( router-name /router/name )\n"); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 271 | if ( rtr_name[strlen(rtr_name)-1] == '/' ) |
| 272 | rtr_name[strlen(rtr_name)-1]='\0'; |
| 273 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 274 | nlsr->router_name=(char *)malloc(strlen(rtr_name)+1); |
| 275 | memset(nlsr->router_name,0,strlen(rtr_name)+1); |
| 276 | memcpy(nlsr->router_name,rtr_name,strlen(rtr_name)+1); |
| 277 | |
| 278 | |
| 279 | } |
| 280 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 281 | /* |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 282 | void |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 283 | process_command_lsdb_synch_interval(char *command) |
| 284 | { |
| 285 | if(command==NULL) |
| 286 | { |
| 287 | printf(" Wrong Command Format ( lsdb-synch-interval secs )\n"); |
| 288 | return; |
| 289 | } |
| 290 | char *rem; |
| 291 | const char *sep=" \t\n"; |
| 292 | char *secs; |
| 293 | long int seconds; |
| 294 | |
| 295 | secs=strtok_r(command,sep,&rem); |
| 296 | if(secs==NULL) |
| 297 | { |
| 298 | printf(" Wrong Command Format ( lsdb-synch-interval secs)\n"); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | seconds=atoi(secs); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 303 | if ( seconds >= 120 && seconds <= 3600 ) |
| 304 | { |
| 305 | nlsr->lsdb_synch_interval=seconds; |
| 306 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 307 | |
| 308 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 309 | */ |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 310 | |
| 311 | void |
| 312 | process_command_interest_retry(char *command) |
| 313 | { |
| 314 | if(command==NULL) |
| 315 | { |
| 316 | printf(" Wrong Command Format ( interest-retry number )\n"); |
| 317 | return; |
| 318 | } |
| 319 | char *rem; |
| 320 | const char *sep=" \t\n"; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 321 | char *retry; |
| 322 | long int retry_number; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 323 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 324 | retry=strtok_r(command,sep,&rem); |
| 325 | if(retry==NULL) |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 326 | { |
| 327 | printf(" Wrong Command Format ( interest-retry number)\n"); |
| 328 | return; |
| 329 | } |
| 330 | |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 331 | retry_number=atoi(retry); |
| 332 | if ( retry_number >= 1 && retry_number<=10 ) |
| 333 | { |
| 334 | nlsr->interest_retry=retry_number; |
| 335 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 336 | |
| 337 | } |
| 338 | |
| 339 | void |
| 340 | process_command_interest_resend_time(char *command) |
| 341 | { |
| 342 | if(command==NULL) |
| 343 | { |
| 344 | printf(" Wrong Command Format ( interest-resend-time secs )\n"); |
| 345 | return; |
| 346 | } |
| 347 | char *rem; |
| 348 | const char *sep=" \t\n"; |
| 349 | char *secs; |
| 350 | long int seconds; |
| 351 | |
| 352 | secs=strtok_r(command,sep,&rem); |
| 353 | if(secs==NULL) |
| 354 | { |
| 355 | printf(" Wrong Command Format ( interest-resend-time secs)\n"); |
| 356 | return; |
| 357 | } |
| 358 | |
| 359 | seconds=atoi(secs); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 360 | if ( seconds <= 60 && seconds >= 1 ) |
| 361 | { |
| 362 | nlsr->interest_resend_time=seconds; |
| 363 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 364 | } |
| 365 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 366 | |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 367 | void |
| 368 | process_command_lsa_refresh_time(char *command) |
| 369 | { |
| 370 | if(command==NULL) |
| 371 | { |
| 372 | printf(" Wrong Command Format ( lsa-refresh-time secs )\n"); |
| 373 | return; |
| 374 | } |
| 375 | char *rem; |
| 376 | const char *sep=" \t\n"; |
| 377 | char *secs; |
| 378 | long int seconds; |
| 379 | |
| 380 | secs=strtok_r(command,sep,&rem); |
| 381 | if(secs==NULL) |
| 382 | { |
| 383 | printf(" Wrong Command Format ( lsa-refresh-time secs)\n"); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | seconds=atoi(secs); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 388 | if ( seconds >= 240) |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 389 | { |
| 390 | nlsr->lsa_refresh_time=seconds; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 391 | if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 ) |
| 392 | { |
| 393 | nlsr->router_dead_interval=2*nlsr->lsa_refresh_time; |
| 394 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | process_command_router_dead_interval(char *command) |
| 401 | { |
| 402 | if(command==NULL) |
| 403 | { |
| 404 | printf(" Wrong Command Format ( router-dead-interval secs )\n"); |
| 405 | return; |
| 406 | } |
| 407 | char *rem; |
| 408 | const char *sep=" \t\n"; |
| 409 | char *secs; |
| 410 | long int seconds; |
| 411 | |
| 412 | secs=strtok_r(command,sep,&rem); |
| 413 | if(secs==NULL) |
| 414 | { |
| 415 | printf(" Wrong Command Format ( router-dead-interval secs)\n"); |
| 416 | return; |
| 417 | } |
| 418 | |
| 419 | seconds=atoi(secs); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 420 | if ( seconds >= 480 ) |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 421 | { |
| 422 | nlsr->router_dead_interval=seconds; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 423 | if ( nlsr->router_dead_interval < nlsr->lsa_refresh_time * 2 ) |
| 424 | { |
| 425 | nlsr->router_dead_interval=2*nlsr->lsa_refresh_time; |
| 426 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | } |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 430 | |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 431 | void |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 432 | process_command_max_faces_per_prefix(char *command) |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 433 | { |
| 434 | if(command==NULL) |
| 435 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 436 | printf(" Wrong Command Format ( max-faces-per-prefix n )\n"); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | char *rem; |
| 440 | const char *sep=" \t\n"; |
| 441 | char *num; |
| 442 | long int number; |
| 443 | |
| 444 | num=strtok_r(command,sep,&rem); |
| 445 | if(num==NULL) |
| 446 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 447 | printf(" Wrong Command Format ( max-faces-per-prefix n)\n"); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 448 | return; |
| 449 | } |
| 450 | |
| 451 | number=atoi(num); |
| 452 | if ( number >= 0 && number <= 60 ) |
| 453 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 454 | nlsr->max_faces_per_prefix=number; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | } |
| 458 | |
| 459 | void |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 460 | process_command_logdir(char *command) |
| 461 | { |
| 462 | if(command==NULL) |
| 463 | { |
| 464 | printf(" Wrong Command Format ( logdir /path/to/logdir )\n"); |
| 465 | return; |
| 466 | } |
| 467 | char *rem; |
| 468 | const char *sep=" \t\n"; |
| 469 | char *dir; |
| 470 | |
| 471 | dir=strtok_r(command,sep,&rem); |
| 472 | if(dir==NULL) |
| 473 | { |
| 474 | printf(" Wrong Command Format ( logdir /path/to/logdir/ )\n"); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | nlsr->logDir=(char *)malloc(strlen(dir)+1); |
| 479 | memset(nlsr->logDir,0,strlen(dir)+1); |
| 480 | memcpy(nlsr->logDir,dir,strlen(dir)); |
| 481 | } |
| 482 | |
| 483 | void |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 484 | process_command_detailed_log(char *command) |
| 485 | { |
| 486 | if(command==NULL) |
| 487 | { |
| 488 | printf(" Wrong Command Format ( detailed-log 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 ( detailed-log on/off )\n"); |
| 499 | return; |
| 500 | } |
| 501 | |
| 502 | if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0) |
| 503 | { |
| 504 | nlsr->detailed_logging=1; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void |
| 509 | process_command_debug(char *command) |
| 510 | { |
| 511 | if(command==NULL) |
| 512 | { |
| 513 | printf(" Wrong Command Format ( debug on/off )\n"); |
| 514 | return; |
| 515 | } |
| 516 | char *rem; |
| 517 | const char *sep=" \t\n"; |
| 518 | char *on_off; |
| 519 | |
| 520 | on_off=strtok_r(command,sep,&rem); |
| 521 | if(on_off==NULL) |
| 522 | { |
| 523 | printf(" Wrong Command Format ( debug on/off )\n"); |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 ) |
| 528 | { |
| 529 | nlsr->debugging=1; |
| 530 | } |
| 531 | } |
| 532 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 533 | |
| 534 | void |
| 535 | process_command_topo_prefix(char *command) |
| 536 | { |
| 537 | if(command==NULL) |
| 538 | { |
| 539 | printf(" Wrong Command Format ( topo-prefix )\n"); |
| 540 | return; |
| 541 | } |
| 542 | char *rem; |
| 543 | const char *sep=" \t\n"; |
| 544 | char *topo_prefix; |
| 545 | |
| 546 | topo_prefix=strtok_r(command,sep,&rem); |
| 547 | if(topo_prefix==NULL) |
| 548 | { |
| 549 | printf(" Wrong Command Format ( topo-prefix /name/prefix )\n"); |
| 550 | return; |
| 551 | } |
| 552 | else |
| 553 | { |
| 554 | if( nlsr->topo_prefix != NULL) |
| 555 | free(nlsr->topo_prefix); |
| 556 | if ( topo_prefix[strlen(topo_prefix)-1] == '/' ) |
| 557 | topo_prefix[strlen(topo_prefix)-1]='\0'; |
| 558 | |
| 559 | nlsr->topo_prefix=(char *)malloc(strlen(topo_prefix)+1); |
| 560 | memset(nlsr->topo_prefix,0,strlen(topo_prefix)+1); |
| 561 | puts(topo_prefix); |
| 562 | memcpy(nlsr->topo_prefix,topo_prefix,strlen(topo_prefix)); |
| 563 | |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | |
| 568 | void |
| 569 | process_command_slice_prefix(char *command) |
| 570 | { |
| 571 | if(command==NULL) |
| 572 | { |
| 573 | printf(" Wrong Command Format ( slice-prefix /name/prefix )\n"); |
| 574 | return; |
| 575 | } |
| 576 | char *rem; |
| 577 | const char *sep=" \t\n"; |
| 578 | char *slice_prefix; |
| 579 | |
| 580 | slice_prefix=strtok_r(command,sep,&rem); |
| 581 | if(slice_prefix==NULL) |
| 582 | { |
| 583 | printf(" Wrong Command Format ( slice-prefix /name/prefix )\n"); |
| 584 | return; |
| 585 | } |
| 586 | else |
| 587 | { |
| 588 | if ( nlsr->slice_prefix != NULL) |
| 589 | free(nlsr->slice_prefix); |
| 590 | if ( slice_prefix[strlen(slice_prefix)-1] == '/' ) |
| 591 | slice_prefix[strlen(slice_prefix)-1]='\0'; |
| 592 | |
| 593 | nlsr->slice_prefix=(char *)malloc(strlen(slice_prefix)+1); |
| 594 | memset(nlsr->slice_prefix,0,strlen(slice_prefix)+1); |
| 595 | memcpy(nlsr->slice_prefix,slice_prefix,strlen(slice_prefix)); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | void |
| 600 | process_command_hyperbolic_routing(char *command) |
| 601 | { |
| 602 | if(command==NULL) |
| 603 | { |
| 604 | printf(" Wrong Command Format ( hyperbolic-routing on)\n"); |
| 605 | return; |
| 606 | } |
| 607 | char *rem; |
| 608 | const char *sep=" \t\n"; |
| 609 | char *on_off; |
| 610 | |
| 611 | on_off=strtok_r(command,sep,&rem); |
| 612 | if(on_off==NULL) |
| 613 | { |
| 614 | printf(" Wrong Command Format ( hyperbolic-routing on )\n"); |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | if ( strcmp(on_off,"ON") == 0 || strcmp(on_off,"on") == 0 ) |
| 619 | { |
| 620 | nlsr->is_hyperbolic_calc=1; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | process_command_hyperbolic_cordinate(char *command) |
| 626 | { |
| 627 | if(command==NULL) |
| 628 | { |
| 629 | printf(" Wrong Command Format ( hyperbolic r 0 )\n"); |
| 630 | return; |
| 631 | } |
| 632 | |
| 633 | char *rem; |
| 634 | const char *sep=" \t\n\r"; |
| 635 | char *radious; |
| 636 | char *theta; |
| 637 | |
| 638 | radious=strtok_r(command,sep,&rem); |
| 639 | if (radious == NULL ) |
| 640 | { |
| 641 | printf(" Wrong Command Format ( hyperbolic r 0 )\n"); |
| 642 | return; |
| 643 | } |
| 644 | |
| 645 | theta=strtok_r(NULL,sep,&rem); |
| 646 | if (theta == NULL ) |
| 647 | { |
| 648 | printf(" Wrong Command Format ( hyperbolic r 0 )\n"); |
| 649 | return; |
| 650 | } |
| 651 | |
| 652 | nlsr->cor_r=strtof(radious,NULL); |
| 653 | nlsr->cor_theta=strtof(theta,NULL); |
| 654 | |
| 655 | } |
| 656 | |
| 657 | void |
| 658 | process_command_tunnel_type(char *command) |
| 659 | { |
| 660 | if(command==NULL) |
| 661 | { |
| 662 | printf(" Wrong Command Format ( tunnel-type udp/tcp)\n"); |
| 663 | return; |
| 664 | } |
| 665 | char *rem; |
| 666 | const char *sep=" \t\n"; |
| 667 | char *on_off; |
| 668 | |
| 669 | on_off=strtok_r(command,sep,&rem); |
| 670 | if(on_off==NULL) |
| 671 | { |
| 672 | printf(" Wrong Command Format ( tunnel-type udp/tcp )\n"); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | if ( strcmp(on_off,"TCP") == 0 || strcmp(on_off,"tcp") == 0 ) |
| 677 | { |
| 678 | nlsr->tunnel_type=IPPROTO_TCP; |
| 679 | } |
| 680 | else if ( strcmp(on_off,"UDP") == 0 || strcmp(on_off,"udp") == 0 ) |
| 681 | { |
| 682 | nlsr->tunnel_type=IPPROTO_UDP; |
| 683 | } |
| 684 | } |
| 685 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 686 | void |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 687 | process_conf_command(char *command) |
| 688 | { |
| 689 | const char *separators=" \t\n"; |
| 690 | char *remainder=NULL; |
| 691 | char *cmd_type=NULL; |
| 692 | |
| 693 | if(command==NULL || strlen(command)==0 || command[0]=='!') |
| 694 | return; |
| 695 | |
| 696 | cmd_type=strtok_r(command,separators,&remainder); |
| 697 | |
| 698 | if(!strcmp(cmd_type,"router-name") ) |
| 699 | { |
| 700 | process_command_router_name(remainder); |
| 701 | } |
| 702 | else if(!strcmp(cmd_type,"ccnneighbor") ) |
| 703 | { |
| 704 | process_command_ccnneighbor(remainder); |
| 705 | } |
| 706 | else if(!strcmp(cmd_type,"ccnname") ) |
| 707 | { |
| 708 | process_command_ccnname(remainder); |
| 709 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 710 | /*else if(!strcmp(cmd_type,"lsdb-synch-interval") ) |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 711 | { |
| 712 | process_command_lsdb_synch_interval(remainder); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 713 | }*/ |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 714 | else if(!strcmp(cmd_type,"interest-retry") ) |
| 715 | { |
| 716 | process_command_interest_retry(remainder); |
| 717 | } |
| 718 | else if(!strcmp(cmd_type,"interest-resend-time") ) |
| 719 | { |
| 720 | process_command_interest_resend_time(remainder); |
| 721 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 722 | else if(!strcmp(cmd_type,"lsa-refresh-time") ) |
| 723 | { |
| 724 | process_command_lsa_refresh_time(remainder); |
| 725 | } |
| 726 | else if(!strcmp(cmd_type,"router-dead-interval") ) |
| 727 | { |
| 728 | process_command_router_dead_interval(remainder); |
| 729 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 730 | else if(!strcmp(cmd_type,"max-faces-per-prefix") ) |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 731 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 732 | process_command_max_faces_per_prefix(remainder); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 733 | } |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 734 | else if(!strcmp(cmd_type,"logdir") ) |
| 735 | { |
| 736 | process_command_logdir(remainder); |
| 737 | } |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 738 | else if(!strcmp(cmd_type,"detailed-log") ) |
| 739 | { |
| 740 | process_command_detailed_log(remainder); |
| 741 | } |
| 742 | else if(!strcmp(cmd_type,"debug") ) |
| 743 | { |
| 744 | process_command_debug(remainder); |
| 745 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 746 | else if(!strcmp(cmd_type,"topo-prefix") ) |
| 747 | { |
| 748 | process_command_topo_prefix(remainder); |
| 749 | } |
| 750 | else if(!strcmp(cmd_type,"slice-prefix") ) |
| 751 | { |
| 752 | process_command_slice_prefix(remainder); |
| 753 | } |
| 754 | else if(!strcmp(cmd_type,"hyperbolic-cordinate") ) |
| 755 | { |
| 756 | process_command_hyperbolic_cordinate(remainder); |
| 757 | } |
| 758 | else if(!strcmp(cmd_type,"hyperbolic-routing") ) |
| 759 | { |
| 760 | process_command_hyperbolic_routing(remainder); |
| 761 | } |
| 762 | else if(!strcmp(cmd_type,"tunnel-type") ) |
| 763 | { |
| 764 | process_command_tunnel_type(remainder); |
| 765 | } |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 766 | else |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 767 | { |
| 768 | printf("Wrong configuration Command %s \n",cmd_type); |
| 769 | } |
| 770 | } |
| 771 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 772 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 773 | int |
| 774 | readConfigFile(const char *filename) |
| 775 | { |
| 776 | FILE *cfg; |
| 777 | char buf[1024]; |
| 778 | int len; |
| 779 | |
| 780 | cfg=fopen(filename, "r"); |
| 781 | |
| 782 | if(cfg == NULL) |
| 783 | { |
| 784 | printf("\nConfiguration File does not exists\n"); |
| 785 | exit(1); |
| 786 | } |
| 787 | |
| 788 | while(fgets((char *)buf, sizeof(buf), cfg)) |
| 789 | { |
| 790 | len=strlen(buf); |
| 791 | if(buf[len-1] == '\n') |
| 792 | buf[len-1]='\0'; |
akmhoque | d515212 | 2012-09-19 06:44:23 -0500 | [diff] [blame] | 793 | if ( buf[0] != '#' && buf[0] != '!') |
| 794 | process_conf_command(buf); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | fclose(cfg); |
| 798 | |
| 799 | return 0; |
| 800 | } |
| 801 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 802 | |
| 803 | void |
| 804 | add_faces_for_nbrs(void) |
| 805 | { |
| 806 | int i, adl_element; |
| 807 | struct ndn_neighbor *nbr; |
| 808 | |
| 809 | struct hashtb_enumerator ee; |
| 810 | struct hashtb_enumerator *e = ⅇ |
| 811 | |
| 812 | hashtb_start(nlsr->adl, e); |
| 813 | adl_element=hashtb_n(nlsr->adl); |
| 814 | |
| 815 | for(i=0;i<adl_element;i++) |
| 816 | { |
| 817 | nbr=e->data; |
| 818 | int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr->neighbor->name, (const char *)nbr->ip_address, 9695,nlsr->tunnel_type); |
| 819 | update_face_to_adl_for_nbr(nbr->neighbor->name, face_id); |
| 820 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id); |
| 821 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id); |
| 822 | hashtb_next(e); |
| 823 | } |
| 824 | |
| 825 | hashtb_end(e); |
| 826 | |
| 827 | } |
| 828 | |
| 829 | void |
| 830 | destroy_faces_for_nbrs(void) |
| 831 | { |
| 832 | int i, adl_element; |
| 833 | struct ndn_neighbor *nbr; |
| 834 | |
| 835 | struct hashtb_enumerator ee; |
| 836 | struct hashtb_enumerator *e = ⅇ |
| 837 | |
| 838 | hashtb_start(nlsr->adl, e); |
| 839 | adl_element=hashtb_n(nlsr->adl); |
| 840 | |
| 841 | for(i=0;i<adl_element;i++) |
| 842 | { |
| 843 | nbr=e->data; |
| 844 | if ( nbr->face > 0 ) |
| 845 | { |
| 846 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, nbr->face); |
| 847 | add_delete_ccn_face_by_face_id(nlsr->ccn,(const char *)nbr->neighbor->name,OP_UNREG,nbr->face); |
| 848 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, nbr->face); |
| 849 | } |
| 850 | hashtb_next(e); |
| 851 | } |
| 852 | |
| 853 | hashtb_end(e); |
| 854 | |
| 855 | } |
| 856 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 857 | char * |
| 858 | process_api_client_command(char *command) |
| 859 | { |
| 860 | char *msg; |
| 861 | msg=(char *)malloc(100); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 862 | memset(msg,0,100); |
| 863 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 864 | const char *sep=" \t\n"; |
| 865 | char *rem=NULL; |
| 866 | char *cmd_type=NULL; |
| 867 | char *op_type=NULL; |
| 868 | char *name=NULL; |
| 869 | char *face=NULL; |
| 870 | int face_id; |
| 871 | int res; |
| 872 | |
| 873 | op_type=strtok_r(command,sep,&rem); |
| 874 | cmd_type=strtok_r(NULL,sep,&rem); |
| 875 | name=strtok_r(NULL,sep,&rem); |
| 876 | if ( name[strlen(name)-1] == '/' ) |
| 877 | name[strlen(name)-1]='\0'; |
| 878 | |
| 879 | struct name_prefix *np=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 880 | np->name=(char *)malloc(strlen(name)+1); |
| 881 | memset(np->name,0,strlen(name)+1); |
| 882 | memcpy(np->name,name,strlen(name)+1); |
| 883 | np->length=strlen(name)+1; |
| 884 | |
| 885 | if ( strcmp(cmd_type,"name")!= 0 ) |
| 886 | { |
| 887 | face=strtok_r(NULL,sep,&rem); |
| 888 | sscanf(face,"face%d",&face_id); |
| 889 | } |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 890 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 891 | if ( strcmp(cmd_type,"name")== 0 ) |
| 892 | { |
| 893 | if ( strcmp(op_type,"del") == 0 ) |
| 894 | { |
| 895 | res=does_name_exist_in_npl(np); |
| 896 | if ( res == 0) |
| 897 | { |
| 898 | sprintf(msg,"Name %s does not exist !!",name); |
| 899 | } |
| 900 | else |
| 901 | { |
| 902 | long int ls_id=get_lsa_id_from_npl(np); |
| 903 | if ( ls_id != 0 ) |
| 904 | { |
| 905 | make_name_lsa_invalid(np,LS_TYPE_NAME,ls_id); |
| 906 | sprintf(msg,"Name %s has been deleted and Advertised.",name); |
| 907 | } |
| 908 | else |
| 909 | { |
| 910 | sprintf(msg,"Name %s does not have an Name LSA yet !!",name); |
| 911 | } |
| 912 | } |
| 913 | } |
| 914 | else if ( strcmp(op_type,"add") == 0 ) |
| 915 | { |
| 916 | res=does_name_exist_in_npl(np); |
| 917 | if ( res == 0) |
| 918 | { |
| 919 | add_name_to_npl(np); |
| 920 | build_and_install_single_name_lsa(np); |
| 921 | sprintf(msg,"Name %s has been added to advertise.",name); |
| 922 | } |
| 923 | else |
| 924 | { |
| 925 | sprintf(msg,"Name %s has already been advertised from this router !!",name); |
| 926 | } |
| 927 | } |
| 928 | } |
| 929 | else if ( strcmp(cmd_type,"neighbor") == 0 ) |
| 930 | { |
| 931 | if ( strcmp(op_type,"del") == 0 ) |
| 932 | { |
| 933 | res=is_neighbor(np->name); |
| 934 | if ( res == 0) |
| 935 | { |
| 936 | sprintf(msg,"Neighbor %s does not exist !!",name); |
| 937 | } |
| 938 | else |
| 939 | { |
| 940 | update_adjacent_status_to_adl(np,NBR_DOWN); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 941 | int face_id=get_next_hop_face_from_adl(np->name); |
| 942 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)np->name, OP_UNREG, face_id); |
| 943 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_UNREG, face_id); |
| 944 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_UNREG, face_id); |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 945 | delete_nbr_from_adl(np); |
| 946 | if(!nlsr->is_build_adj_lsa_sheduled) |
| 947 | { |
| 948 | nlsr->event_build_adj_lsa = ccn_schedule_event(nlsr->sched, 1000, &build_and_install_adj_lsa, NULL, 0); |
| 949 | nlsr->is_build_adj_lsa_sheduled=1; |
| 950 | } |
| 951 | sprintf(msg,"Neighbor %s has been deleted from adjacency list.",name); |
| 952 | } |
| 953 | } |
| 954 | else if ( strcmp(op_type,"add") == 0 ) |
| 955 | { |
| 956 | res=is_neighbor(np->name); |
| 957 | if ( res == 0 ) |
| 958 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 959 | struct name_prefix *nbr_name=(struct name_prefix *)malloc(sizeof(struct name_prefix )); |
| 960 | get_host_name_from_command_string(nbr_name,np->name,0); |
| 961 | printf("Hostname of neighbor: %s ",nbr_name->name); |
| 962 | |
| 963 | char *ip_addr=(char *)malloc(13); |
| 964 | memset(ip_addr,0,13); |
| 965 | get_ip_from_hostname_02(nbr_name->name,ip_addr); |
| 966 | printf("IP Address: %s \n",ip_addr); |
| 967 | int face_id=add_ccn_face(nlsr->ccn, (const char *)nbr_name->name, (const char *)ip_addr, 9695,nlsr->tunnel_type); |
| 968 | update_face_to_adl_for_nbr(nbr_name->name, face_id); |
| 969 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->topo_prefix, OP_REG, face_id); |
| 970 | add_delete_ccn_face_by_face_id(nlsr->ccn, (const char *)nlsr->slice_prefix, OP_REG, face_id); |
| 971 | |
| 972 | add_nbr_to_adl(np,face_id,ip_addr); |
| 973 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 974 | sprintf(msg,"Neighbor %s has been added to adjacency list.",name); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 975 | |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 976 | } |
| 977 | else |
| 978 | { |
| 979 | sprintf(msg,"Neighbor %s already exists in adjacency list.",name); |
| 980 | } |
| 981 | } |
| 982 | } |
| 983 | |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 984 | |
| 985 | return msg; |
| 986 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 987 | |
| 988 | int |
| 989 | nlsr_api_server_poll(long int time_out_micro_sec, int ccn_fd) |
| 990 | { |
| 991 | struct timeval timeout; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 992 | if (time_out_micro_sec< 500000 && time_out_micro_sec> 0 ) |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 993 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 994 | timeout.tv_sec=0; |
| 995 | timeout.tv_usec=time_out_micro_sec; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 996 | } |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 997 | else |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 998 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 999 | timeout.tv_sec = 0; |
| 1000 | timeout.tv_usec = 500000; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1001 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1002 | |
| 1003 | int fd; |
| 1004 | int nread; |
| 1005 | int result; |
| 1006 | fd_set testfds; |
| 1007 | unsigned int client_len; |
| 1008 | int client_sockfd; |
| 1009 | char recv_buffer[1024]; |
| 1010 | bzero(recv_buffer,1024); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1011 | struct sockaddr_in client_address; |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1012 | |
| 1013 | testfds=nlsr->readfds; |
| 1014 | result = select(FD_SETSIZE, &testfds, NULL,NULL, &timeout); |
| 1015 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1016 | for(fd = 0; fd < FD_SETSIZE && result > 0; fd++) |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1017 | { |
| 1018 | if(FD_ISSET(fd,&testfds)) |
| 1019 | { |
| 1020 | if ( fd == ccn_fd ) |
| 1021 | { |
| 1022 | return 0; |
| 1023 | } |
| 1024 | else if(fd == nlsr->nlsr_api_server_sock_fd) |
| 1025 | { |
| 1026 | client_len = sizeof(client_address); |
| 1027 | client_sockfd = accept(nlsr->nlsr_api_server_sock_fd,(struct sockaddr *)&client_address, &client_len); |
| 1028 | FD_SET(client_sockfd, &nlsr->readfds); |
| 1029 | } |
| 1030 | else |
| 1031 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1032 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1033 | ioctl(fd, FIONREAD, &nread); |
| 1034 | if(nread == 0) |
| 1035 | { |
| 1036 | close(fd); |
| 1037 | FD_CLR(fd, &nlsr->readfds); |
| 1038 | } |
| 1039 | else |
| 1040 | { |
| 1041 | recv(fd, recv_buffer, 1024, 0); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1042 | printf("Received Data from NLSR API cleint: %s \n",recv_buffer); |
akmhoque | 562caef | 2012-11-09 13:29:06 -0600 | [diff] [blame] | 1043 | char *msg=process_api_client_command(recv_buffer); |
| 1044 | send(fd, msg, strlen(msg),0); |
| 1045 | free(msg); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1046 | close(fd); |
| 1047 | FD_CLR(fd, &nlsr->readfds); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1056 | int |
| 1057 | check_config_validity() |
| 1058 | { |
| 1059 | if (nlsr->router_name == NULL ) |
| 1060 | { |
| 1061 | fprintf(stderr,"Router name has not been configured :(\n"); |
| 1062 | return -1; |
| 1063 | } |
| 1064 | if ( nlsr->is_hyperbolic_calc == 1 && (nlsr->cor_r == -1.0 && nlsr->cor_theta== -1.0) ) |
| 1065 | { |
| 1066 | fprintf(stderr,"Hyperbolic codinate has not been defined :(\n"); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 1073 | void |
| 1074 | nlsr_destroy( void ) |
| 1075 | { |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1076 | if ( nlsr->debugging ) |
| 1077 | { |
| 1078 | printf("Freeing Allocated Memory....\n"); |
| 1079 | } |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1080 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Freeing Allocated Memory....\n"); |
akmhoque | fbfd098 | 2012-09-09 20:59:03 -0500 | [diff] [blame] | 1081 | /* Destroying all face created by nlsr in CCND */ |
| 1082 | destroy_all_face_by_nlsr(); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1083 | destroy_faces_for_nbrs(); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 1084 | /* Destroying every hash table attached to each neighbor in ADL before destorying ADL */ |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1085 | hashtb_destroy(&nlsr->npl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1086 | hashtb_destroy(&nlsr->adl); |
| 1087 | hashtb_destroy(&nlsr->lsdb->name_lsdb); |
| 1088 | hashtb_destroy(&nlsr->lsdb->adj_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1089 | hashtb_destroy(&nlsr->pit_alsa); |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 1090 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1091 | |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 1092 | |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1093 | hashtb_destroy(&nlsr->routing_table); |
| 1094 | |
| 1095 | |
| 1096 | int i, npt_element; |
| 1097 | struct npt_entry *ne; |
| 1098 | struct hashtb_enumerator ee; |
| 1099 | struct hashtb_enumerator *e = ⅇ |
| 1100 | hashtb_start(nlsr->npt, e); |
| 1101 | npt_element=hashtb_n(nlsr->npt); |
| 1102 | for(i=0;i<npt_element;i++) |
| 1103 | { |
| 1104 | ne=e->data; |
akmhoque | 3cced64 | 2012-09-24 16:20:20 -0500 | [diff] [blame] | 1105 | hashtb_destroy(&ne->name_list); |
| 1106 | hashtb_destroy(&ne->face_list); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1107 | hashtb_next(e); |
| 1108 | } |
| 1109 | |
| 1110 | hashtb_end(e); |
| 1111 | hashtb_destroy(&nlsr->npt); |
| 1112 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1113 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1114 | ccns_close(&nlsr->ccns, NULL, NULL); |
| 1115 | ccns_slice_destroy(&nlsr->slice); |
| 1116 | |
| 1117 | |
| 1118 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1119 | close(nlsr->nlsr_api_server_sock_fd); |
| 1120 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 1121 | ccn_schedule_destroy(&nlsr->sched); |
| 1122 | ccn_destroy(&nlsr->ccn); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1123 | |
| 1124 | free(nlsr->lsdb->lsdb_version); |
| 1125 | free(nlsr->lsdb); |
| 1126 | free(nlsr->router_name); |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 1127 | free(nlsr); |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1128 | if ( nlsr->debugging ) |
| 1129 | { |
| 1130 | printf("Finished freeing allocated memory\n"); |
| 1131 | } |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1132 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Finished freeing allocated memory\n"); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1133 | |
akmhoque | 386081b | 2012-08-10 10:53:21 -0500 | [diff] [blame] | 1134 | } |
| 1135 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1136 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1137 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1138 | void |
| 1139 | init_api_server(int ccn_fd) |
| 1140 | { |
| 1141 | int server_sockfd; |
| 1142 | int server_len; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1143 | struct sockaddr_in server_address; |
akmhoque | f31f13b | 2012-11-16 09:42:24 -0600 | [diff] [blame] | 1144 | unsigned int yes=1; |
| 1145 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1146 | server_sockfd = socket(AF_INET, SOCK_STREAM, 0); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1147 | |
| 1148 | int flags = fcntl(server_sockfd, F_GETFL, 0); |
| 1149 | fcntl(server_sockfd, F_SETFL, O_NONBLOCK|flags); |
| 1150 | |
akmhoque | f31f13b | 2012-11-16 09:42:24 -0600 | [diff] [blame] | 1151 | if (setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) |
| 1152 | { |
| 1153 | ON_ERROR_DESTROY(-1); |
| 1154 | } |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1155 | |
| 1156 | server_address.sin_family = AF_INET; |
Adam Alyyan | b5fff37 | 2013-01-09 14:32:52 -0600 | [diff] [blame] | 1157 | server_address.sin_addr.s_addr = INADDR_ANY; |
| 1158 | server_address.sin_port = htons(nlsr->api_port); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1159 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1160 | server_len = sizeof(server_address); |
| 1161 | bind(server_sockfd, (struct sockaddr *)&server_address, server_len); |
| 1162 | listen(server_sockfd, 100); |
| 1163 | FD_ZERO(&nlsr->readfds); |
| 1164 | FD_SET(server_sockfd, &nlsr->readfds); |
| 1165 | FD_SET(ccn_fd, &nlsr->readfds); |
| 1166 | nlsr->nlsr_api_server_sock_fd=server_sockfd; |
| 1167 | |
| 1168 | } |
| 1169 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1170 | int |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1171 | init_nlsr(void) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1172 | { |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1173 | if (signal(SIGQUIT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 1174 | { |
| 1175 | perror("SIGQUIT install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1176 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1177 | } |
| 1178 | if (signal(SIGTERM, nlsr_stop_signal_handler ) == SIG_ERR) |
| 1179 | { |
| 1180 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1181 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1182 | } |
| 1183 | if (signal(SIGINT, nlsr_stop_signal_handler ) == SIG_ERR) |
| 1184 | { |
| 1185 | perror("SIGTERM install error\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1186 | return -1; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1187 | } |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1188 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1189 | nlsr=(struct nlsr *)malloc(sizeof(struct nlsr)); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1190 | |
| 1191 | struct hashtb_param param_adl = {0}; |
akmhoque | 28c4502 | 2012-08-09 15:38:02 -0500 | [diff] [blame] | 1192 | nlsr->adl=hashtb_create(sizeof(struct ndn_neighbor), ¶m_adl); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1193 | struct hashtb_param param_npl = {0}; |
akmhoque | 3171d65 | 2012-11-13 11:44:33 -0600 | [diff] [blame] | 1194 | nlsr->npl = hashtb_create(sizeof(struct name_prefix_list_entry), ¶m_npl); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1195 | struct hashtb_param param_pit_alsa = {0}; |
akmhoque | 1ce7105 | 2012-09-13 22:51:32 -0500 | [diff] [blame] | 1196 | nlsr->pit_alsa = hashtb_create(sizeof(struct pending_interest), ¶m_pit_alsa); |
akmhoque | 3560cb6 | 2012-09-09 10:52:30 -0500 | [diff] [blame] | 1197 | struct hashtb_param param_npt = {0}; |
| 1198 | nlsr->npt = hashtb_create(sizeof(struct npt_entry), ¶m_npt); |
| 1199 | struct hashtb_param param_rte = {0}; |
| 1200 | nlsr->routing_table = hashtb_create(sizeof(struct routing_table_entry), ¶m_rte); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1201 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1202 | nlsr->in_interest.p = &incoming_interest; |
| 1203 | nlsr->in_content.p = &incoming_content; |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 1204 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1205 | nlsr->lsdb=(struct linkStateDatabase *)malloc(sizeof(struct linkStateDatabase)); |
akmhoque | 07dd8cc | 2012-08-16 10:23:01 -0500 | [diff] [blame] | 1206 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1207 | char *time_stamp=(char *)malloc(20); |
| 1208 | memset(time_stamp,0,20); |
| 1209 | get_current_timestamp_micro(time_stamp); |
| 1210 | nlsr->lsdb->lsdb_version=(char *)malloc(strlen(time_stamp)+1); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1211 | memset(nlsr->lsdb->lsdb_version,0,strlen(time_stamp)); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1212 | free(time_stamp); |
| 1213 | |
| 1214 | struct hashtb_param param_adj_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 1215 | nlsr->lsdb->adj_lsdb = hashtb_create(sizeof(struct alsa), ¶m_adj_lsdb); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1216 | struct hashtb_param param_name_lsdb = {0}; |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 1217 | nlsr->lsdb->name_lsdb = hashtb_create(sizeof(struct nlsa), ¶m_name_lsdb); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1218 | struct hashtb_param param_cor_lsdb = {0}; |
| 1219 | nlsr->lsdb->cor_lsdb = hashtb_create(sizeof(struct clsa), ¶m_cor_lsdb); |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1220 | |
| 1221 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1222 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1223 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1224 | nlsr->is_synch_init=1; |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 1225 | nlsr->nlsa_id=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 1226 | nlsr->adj_build_flag=0; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1227 | nlsr->adj_build_count=0; |
| 1228 | nlsr->is_build_adj_lsa_sheduled=0; |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1229 | nlsr->is_send_lsdb_interest_scheduled=0; |
| 1230 | nlsr->is_route_calculation_scheduled=0; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 1231 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1232 | nlsr->detailed_logging=0; |
| 1233 | nlsr->debugging=0; |
| 1234 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1235 | //nlsr->lsdb_synch_interval = LSDB_SYNCH_INTERVAL; |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 1236 | nlsr->interest_retry = INTEREST_RETRY; |
| 1237 | nlsr->interest_resend_time = INTEREST_RESEND_TIME; |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 1238 | nlsr->lsa_refresh_time=LSA_REFRESH_TIME; |
| 1239 | nlsr->router_dead_interval=ROUTER_DEAD_INTERVAL; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1240 | nlsr->max_faces_per_prefix=MAX_FACES_PER_PREFIX; |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1241 | nlsr->semaphor=NLSR_UNLOCKED; |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1242 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1243 | nlsr->api_port=API_PORT; |
| 1244 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1245 | nlsr->topo_prefix=(char *)malloc(strlen("/ndn/routing/nlsr")+1); |
| 1246 | memset(nlsr->topo_prefix,0,strlen("/ndn/routing/nlsr")+1); |
| 1247 | memcpy(nlsr->topo_prefix,"/ndn/routing/nlsr",strlen("/ndn/routing/nlsr")); |
| 1248 | |
| 1249 | nlsr->slice_prefix=(char *)malloc(strlen("/ndn/routing/nlsr/LSA")+1); |
| 1250 | memset(nlsr->slice_prefix, 0, strlen("/ndn/routing/nlsr/LSA")+1); |
| 1251 | memcpy(nlsr->slice_prefix,"/ndn/routing/nlsr/LSA",strlen("/ndn/routing/nlsr/LSA")); |
| 1252 | |
| 1253 | nlsr->is_hyperbolic_calc=0; |
| 1254 | nlsr->cor_r=-1.0; |
| 1255 | nlsr->cor_theta=-1.0; |
| 1256 | |
| 1257 | nlsr->tunnel_type=IPPROTO_UDP; |
| 1258 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1259 | return 0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1260 | } |
| 1261 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1262 | |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1263 | int |
| 1264 | main(int argc, char *argv[]) |
| 1265 | { |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1266 | int res, ret; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1267 | char *config_file; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1268 | int daemon_mode=0; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1269 | int port=0; |
akmhoque | 902d57e | 2012-08-17 09:24:38 -0500 | [diff] [blame] | 1270 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1271 | |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1272 | |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1273 | while ((res = getopt_long(argc, argv, "df:p:h", longopts, 0)) != -1) |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1274 | { |
| 1275 | switch (res) |
| 1276 | { |
| 1277 | case 'd': |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1278 | daemon_mode = 1; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1279 | break; |
| 1280 | case 'f': |
| 1281 | config_file = optarg; |
| 1282 | break; |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1283 | case 'p': |
| 1284 | port = atoi(optarg); |
| 1285 | break; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1286 | case 'h': |
| 1287 | default: |
| 1288 | usage(argv[0]); |
| 1289 | } |
| 1290 | } |
| 1291 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1292 | ret=init_nlsr(); |
| 1293 | ON_ERROR_EXIT(ret); |
akmhoque | 9504180 | 2012-11-16 09:18:02 -0600 | [diff] [blame] | 1294 | |
| 1295 | if ( port !=0 ) |
| 1296 | nlsr->api_port=port; |
| 1297 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1298 | readConfigFile(config_file); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1299 | |
| 1300 | ON_ERROR_DESTROY(check_config_validity()); |
| 1301 | |
| 1302 | print_adjacent_from_adl(); |
| 1303 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1304 | if ( daemon_mode == 1 ) |
| 1305 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1306 | nlsr->debugging=0; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1307 | daemonize_nlsr(); |
| 1308 | } |
| 1309 | |
| 1310 | startLogging(nlsr->logDir); |
| 1311 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1312 | nlsr->ccn=ccn_create(); |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1313 | int ccn_fd=ccn_connect(nlsr->ccn, NULL); |
| 1314 | if(ccn_fd == -1) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1315 | { |
| 1316 | fprintf(stderr,"Could not connect to ccnd\n"); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1317 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Could not connect to ccnd\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1318 | ON_ERROR_DESTROY(-1); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1319 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1320 | |
| 1321 | init_api_server(ccn_fd); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1322 | |
| 1323 | res=create_sync_slice(nlsr->topo_prefix, nlsr->slice_prefix); |
| 1324 | if(res<0) |
| 1325 | { |
| 1326 | fprintf(stderr, "Can not create slice for prefix %s\n",nlsr->slice_prefix); |
| 1327 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Can not create slice for prefix %s\n",nlsr->slice_prefix); |
| 1328 | ON_ERROR_DESTROY(res); |
| 1329 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1330 | struct ccn_charbuf *router_prefix; |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1331 | router_prefix=ccn_charbuf_create(); |
| 1332 | res=ccn_name_from_uri(router_prefix,nlsr->router_name); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1333 | if(res<0) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1334 | { |
| 1335 | fprintf(stderr, "Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 1336 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Bad ccn URI: %s\n",nlsr->router_name); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1337 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1338 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1339 | |
| 1340 | ccn_name_append_str(router_prefix,"nlsr"); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1341 | nlsr->in_interest.data=nlsr->router_name; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1342 | res=ccn_set_interest_filter(nlsr->ccn,router_prefix,&nlsr->in_interest); |
| 1343 | if ( res < 0 ) |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1344 | { |
| 1345 | fprintf(stderr,"Failed to register interest for router\n"); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1346 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Failed to register interest for router\n"); |
akmhoque | 81c25e0 | 2012-09-10 14:50:33 -0500 | [diff] [blame] | 1347 | ON_ERROR_DESTROY(res); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1348 | } |
| 1349 | ccn_charbuf_destroy(&router_prefix); |
| 1350 | |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1351 | if ( nlsr->debugging ) |
| 1352 | printf("Router Name : %s\n",nlsr->router_name); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1353 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"Router Name : %s\n",nlsr->router_name); |
akmhoque | 7b79145 | 2012-10-30 11:24:56 -0500 | [diff] [blame] | 1354 | if ( nlsr->debugging ) |
| 1355 | printf("lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 1356 | writeLogg(__FILE__,__FUNCTION__,__LINE__,"lsdb_version: %s\n",nlsr->lsdb->lsdb_version); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1357 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1358 | add_faces_for_nbrs(); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1359 | print_name_prefix_from_npl(); |
| 1360 | print_adjacent_from_adl(); |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1361 | build_and_install_name_lsas(); |
| 1362 | |
| 1363 | sync_monitor(nlsr->topo_prefix,nlsr->slice_prefix); |
| 1364 | |
| 1365 | |
| 1366 | print_name_lsdb(); |
| 1367 | if ( nlsr->cor_r != -1.0 && nlsr->cor_theta != -1.0) |
| 1368 | { |
| 1369 | build_and_install_cor_lsa(); |
| 1370 | } |
| 1371 | write_name_lsdb_to_repo(nlsr->slice_prefix); |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 1372 | |
| 1373 | nlsr->sched = ccn_schedule_create(nlsr, &ndn_rtr_ticker); |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 1374 | 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] | 1375 | nlsr->event = ccn_schedule_event(nlsr->sched, 60000000, &refresh_lsdb, NULL, 0); |
akmhoque | 1c9b92f | 2012-08-13 10:57:50 -0500 | [diff] [blame] | 1376 | |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1377 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1378 | while(1) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1379 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1380 | if ( nlsr->semaphor == NLSR_UNLOCKED ) |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1381 | { |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1382 | if( nlsr->sched != NULL ) |
| 1383 | { |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1384 | long int micro_sec=ccn_schedule_run(nlsr->sched); |
| 1385 | res=nlsr_api_server_poll(micro_sec,ccn_fd); |
| 1386 | ON_ERROR_DESTROY(res); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1387 | } |
| 1388 | if(nlsr->ccn != NULL) |
| 1389 | { |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame^] | 1390 | res = ccn_run(nlsr->ccn, 1); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 1391 | } |
| 1392 | if (!(nlsr->sched && nlsr->ccn)) |
| 1393 | { |
| 1394 | break; |
| 1395 | } |
akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame] | 1396 | } |
| 1397 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1398 | } |
akmhoque | 1771c41 | 2012-11-09 13:06:08 -0600 | [diff] [blame] | 1399 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1400 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1401 | return 0; |
| 1402 | } |
| 1403 | |