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