int main(int argc, char *argv[]) { /* update progname from argv[0] */ set_progname(argv[0]); const struct ap_Option options[] = { {'h', "help", ap_no, 0, 0}, {'v', "version", ap_no, 0, 0}, {0, 0, ap_no, 0, 0} }; if (!ap_init(&parser, argc, (const char* const*) argv, options, 0)) critx("could not allocate memory for option parser"); if (ap_error(&parser)) { errx("%s", ap_error(&parser)); usage(EXIT_FAILURE); } /* parse command line */ for (int argind = 0; argind < ap_arguments(&parser); argind++) { const int code = ap_code(&parser, argind); switch (code) { case 0: break; case 'h': usage(EXIT_SUCCESS); break; case 'v': fprintf(stderr, "%s %s\n%s\n%s\n\n%s\n", progname, FLOWGRIND_VERSION, FLOWGRIND_COPYRIGHT, FLOWGRIND_COPYING, FLOWGRIND_AUTHORS); exit(EXIT_SUCCESS); break; default: errx("uncaught option: %s", ap_argument(&parser, argind)); usage(EXIT_FAILURE); break; } } if (!ap_arguments(&parser)) { errx("no address given"); usage(EXIT_FAILURE); } xmlrpc_env rpc_env; xmlrpc_env_init(&rpc_env); xmlrpc_client_setup_global_const(&rpc_env); for (int argind = 0; argind < ap_arguments(&parser); argind++) /* if non-option, it is an address */ if (!ap_code(&parser, argind)) stop_flows(ap_argument(&parser, argind)); xmlrpc_env_clean(&rpc_env); xmlrpc_client_teardown_global_const(); ap_free(&parser); }
static void stop_flows(const char* address) { xmlrpc_env env; xmlrpc_client *client = 0; xmlrpc_value * resultP = 0; int port = DEFAULT_LISTEN_PORT; bool is_ipv6 = false; char *arg, *url = 0; int rc; char *rpc_address = arg = strdup(address); struct sockaddr_in6 source_in6; source_in6.sin6_family = AF_INET6; parse_rpc_address(&rpc_address, &port, &is_ipv6); if (is_ipv6 && (inet_pton(AF_INET6, rpc_address, (char*)&source_in6.sin6_addr) <= 0)) errx("invalid IPv6 address '%s' for RPC", rpc_address); if (port < 1 || port > 65535) errx("invalid port for RPC"); if (is_ipv6) rc = asprintf(&url, "http://[%s]:%d/RPC2", rpc_address, port); else rc = asprintf(&url, "http://%s:%d/RPC2", rpc_address, port); if (rc==-1) critx("failed to build RPC URL"); printf("Stopping all flows on %s\n", url); /* Stop the flows */ xmlrpc_env_init(&env); xmlrpc_client_create(&env, XMLRPC_CLIENT_NO_FLAGS, "Flowgrind", FLOWGRIND_VERSION, NULL, 0, &client); if (env.fault_occurred) goto cleanup; xmlrpc_client_call2f(&env, client, url, "stop_flow", &resultP, "({s:i})", "flow_id", -1); /* -1 stops all flows */ if (resultP) xmlrpc_DECREF(resultP); cleanup: if (env.fault_occurred) { warnx("could not stop flows on %s: %s (%d)", url, env.fault_string, env.fault_code); } if (client) xmlrpc_client_destroy(client); xmlrpc_env_clean(&env); free_all(arg, url); }
void logging_init (void) { logstr = malloc(LOGGING_MAXLEN); if (logstr == NULL) critx("unable to allocate memory for logging string"); switch (log_type) { case LOGTYPE_SYSLOG: openlog("flowgrind_daemon", LOG_NDELAY | LOG_CONS | LOG_PID, LOG_DAEMON); break; case LOGTYPE_STDERR: break; } }
int main(int argc, char *argv[]) { struct sigaction sa; xmlrpc_env env; if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) crit("could not ignore SIGPIPE"); sa.sa_handler = sighandler; sa.sa_flags = 0; sigemptyset (&sa.sa_mask); sigaction (SIGHUP, &sa, NULL); sigaction (SIGALRM, &sa, NULL); sigaction (SIGCHLD, &sa, NULL); set_progname(argv[0]); parse_cmdline(argc, argv); logging_init(); #ifdef HAVE_LIBPCAP fg_pcap_init(); #endif /* HAVE_LIBPCAP */ if (log_type == LOGTYPE_SYSLOG) { /* Need to call daemon() before creating the thread because * it internally calls fork() which does not copy threads. */ if (daemon(0, 0) == -1) crit("daemon() failed"); logging_log(LOG_NOTICE, "flowgrindd daemonized"); } if (cpu >= 0) set_affinity(cpu); create_daemon_thread(); xmlrpc_env_init(&env); run_rpc_server(&env, port); critx("control should never reach end of main()"); }