int main(int argc, char **argv) { static char *ifname = NULL; /* Name of interface to attach to */ fd_set rfds, readers; int n, nfds = 0, i; int daemonize = 0; struct timeval *timeout; struct timespec timeout_spec; struct sigaction sigact; sigset_t mask, origmask; /* Remember the name of the executable... */ progname = strrchr(argv[0], '/'); if (progname) progname++; else progname = argv[0]; /* Use debug output as default */ debug = 1; memset (&sigact, 0, sizeof(struct sigaction)); sigact.sa_handler = signal_handler; /* This server should shut down on these signals. */ sigaction(SIGTERM, &sigact, 0); sigaction(SIGHUP, &sigact, 0); sigaction(SIGINT, &sigact, 0); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGHUP); sigaddset(&mask, SIGINT); /* Only capture segmentation faults when we are not debugging... */ #ifndef DEBUG sigaddset(&mask, SIGSEGV); #endif /* Block the signals we are watching here so that we can * handle them in pselect instead. */ sigprocmask(SIG_BLOCK, &mask, &origmask); /* Parse command line: */ while (1) { int opt; opt = getopt_long(argc, argv, "i:fjln:dghoq:r:s:c:uwxDLRV", longopts, 0); if (opt == EOF) break; switch (opt) { case 0: break; case 'd': debug = 0; daemonize = 1; break; case 'f': llfeedback = 1; active_route_timeout = ACTIVE_ROUTE_TIMEOUT_LLF; break; case 'g': rreq_gratuitous = !rreq_gratuitous; break; case 'i': ifname = optarg; break; case 'j': hello_jittering = !hello_jittering; break; case 'l': log_to_file = !log_to_file; break; case 'n': if (optarg && isdigit(*optarg)) { receive_n_hellos = atoi(optarg); if (receive_n_hellos < 2) { fprintf(stderr, "-n should be at least 2!\n"); exit(-1); } } break; case 'o': optimized_hellos = !optimized_hellos; break; case 'q': if (optarg && isdigit(*optarg)) qual_threshold = atoi(optarg); break; case 'r': if (optarg && isdigit(*optarg)) rt_log_interval = atof(optarg) * 1000; break; case 's': if (optarg && isdigit(*optarg)) inet_aton(optarg, &server_addr); break; case 'c': if (optarg && isdigit(*optarg)) { node_discovery_mode = !node_discovery_mode; discovery_internal = atof(optarg) * 1000 * 60; } break; case 'u': unidir_hack = !unidir_hack; break; case 'w': internet_gw_mode = !internet_gw_mode; break; case 'x': expanding_ring_search = !expanding_ring_search; break; case 'L': local_repair = !local_repair; break; case 'D': wait_on_reboot = !wait_on_reboot; break; case 'R': ratelimit = !ratelimit; break; case 'V': printf ("\nAODV-UU v%s, %s © Uppsala University & Ericsson AB.\nAuthor: Erik Nordström, <*****@*****.**>\n\n", AODV_UU_VERSION, DRAFT_VERSION); exit(0); break; case '?': case ':': exit(0); default: usage(0); } } /* Check that we are running as root */ if (geteuid() != 0) { fprintf(stderr, "must be root\n"); exit(1); } /* Detach from terminal */ if (daemonize) { if (fork() != 0) exit(0); /* Close stdin, stdout and stderr... */ /* close(0); */ close(1); close(2); setsid(); } /* Make sure we cleanup at exit... */ atexit((void *) &cleanup); /* Initialize data structures and services... */ rt_table_init(); log_init(); /* packet_queue_init(); */ host_init(ifname); /* packet_input_init(); */ nl_init(); nl_send_conf_msg(); aodv_socket_init(); #ifdef LLFEEDBACK if (llfeedback) { llf_init(); } #endif /* Set sockets to watch... */ FD_ZERO(&readers); for (i = 0; i < nr_callbacks; i++) { FD_SET(callbacks[i].fd, &readers); if (callbacks[i].fd >= nfds) nfds = callbacks[i].fd + 1; } /* Set the wait on reboot timer... */ if (wait_on_reboot) { timer_init(&worb_timer, wait_on_reboot_timeout, &wait_on_reboot); timer_set_timeout(&worb_timer, DELETE_PERIOD); alog(LOG_NOTICE, 0, __FUNCTION__, "In wait on reboot for %d milliseconds. Disable with \"-D\".", DELETE_PERIOD); } if(node_discovery_mode && internet_gw_mode) { timer_init(&discovery_timer, node_discovery_timeout, NULL); timer_set_timeout(&discovery_timer, discovery_internal); DEBUG(LOG_DEBUG, 0, "This is gateway and set with node discovery\n"); } /* Schedule the first Hello */ if (!optimized_hellos && !llfeedback) hello_start(); if (rt_log_interval) log_rt_table_init(); while (1) { memcpy((char *) &rfds, (char *) &readers, sizeof(rfds)); timeout = timer_age_queue(); timeout_spec.tv_sec = timeout->tv_sec; timeout_spec.tv_nsec = timeout->tv_usec * 1000; if ((n = pselect(nfds, &rfds, NULL, NULL, &timeout_spec, &origmask)) < 0) { if (errno != EINTR) alog(LOG_WARNING, errno, __FUNCTION__, "Failed select (main loop)"); continue; } if (n > 0) { for (i = 0; i < nr_callbacks; i++) { if (FD_ISSET(callbacks[i].fd, &rfds)) { /* We don't want any timer SIGALRM's while executing the callback functions, therefore we block the timer... */ (*callbacks[i].func) (callbacks[i].fd); } } } } /* Main loop */ return 0; }
int main (int argc, char **argv) { static char *ifname = NULL; /* Name of interface to attach to */ fd_set rfds, readers; int n, nfds = 0, i; int daemonize = 0; /* Remember the name of the executable... */ progname = strrchr(argv[0], '/'); if (progname) progname++; else progname = argv[0]; /* Use debug output as default */ debug = 1; /* Parse command line: */ argc--; argv++; while(argc) { if(argv[0][0] == '-') { switch(argv[0][1]) { case 'i': if(argv[1] != NULL) { ifname = argv[1]; argc--; argv++; break; } print_usage(); exit(-1); case 'd': debug = 0; daemonize = 1; break; case 'l': log_to_file = 1; break; case 'u': unidir_hack = 1; break; case 'g': rreq_gratuitous = 1; break; case 't': log_rt_table = 1; if(argv[1] != NULL && argv[1][0] != '-') { printf("arg: %d\n", atoi(argv[1])); rt_log_interval = atoi(argv[1])*1000; argc--; argv++; } break; default: print_usage(); exit(-1); } } else { fprintf(stderr, "Unknown option: %s\n", argv[0]); print_usage(); exit(-1); } argc--; argv++; } /* Check that we are running as root */ if (geteuid() != 0) { fprintf(stderr, "aodvd: must be root\n"); exit(1); } /* Detach from terminal */ if(daemonize) { if (fork() != 0) exit(0); /* Close stdin, stdout and stderr... */ close(0); close(1); close(2); setsid(); } /* Initialize data structures and services... */ host_init(ifname); log_init(); timer_queue_init(); rt_table_init(); packet_input_init(); aodv_socket_init(); log(LOG_NOTICE, 0, "INIT: Attaching to interface %s, override with -i <interface>.",this_host->ifname); /* Make sure we cleanup at exit... */ atexit((void *)&cleanup); /* Make sure we run at high priority to make up for the user space packet processing... */ /* nice(-5); */ /* Catch SIGHUP, SIGINT and SIGTERM type signals */ signal(SIGHUP, signal_handler); signal(SIGINT, signal_handler); signal(SIGTERM, signal_handler); /* Only capture segmentation faults when we are not debugging... */ #ifndef DEBUG signal(SIGSEGV, signal_handler); #endif /* Set sockets to watch... */ FD_ZERO(&readers); for (i = 0; i < nr_callbacks; i++) { FD_SET(callbacks[i].fd, &readers); if (callbacks[i].fd >= nfds) nfds = callbacks[i].fd + 1; } /* Set bcast_time, which ensures that we don't send unnecessary hello msgs. */ this_host->bcast_time = get_currtime(); /* Schedule the first Hello */ timer_new(HELLO_INTERVAL, hello_send, NULL); if(log_rt_table) timer_new(rt_log_interval, print_rt_table, NULL); while(1) { memcpy((char *)&rfds, (char *)&readers, sizeof(rfds)); if ((n = select(nfds, &rfds, NULL, NULL, NULL)) < 0) { if (errno != EINTR) log(LOG_WARNING, errno, "main.c: Failed select (main loop)"); continue; } if (n > 0) { for (i = 0; i < nr_callbacks; i++) { if (FD_ISSET(callbacks[i].fd, &rfds)) { /* We don't want any timer SIGALRM's while executing the callback functions, therefore we block the timer... */ timer_block(); (*callbacks[i].func)(callbacks[i].fd); timer_unblock(); } } } } /* Main loop */ return 0; }