void set_mode(struct shared_data *data, char value[20]){ if (strcmp(value, "play") == 0){ data->recording = 0; } else if (strcmp(value, "record") == 0){ data->recording = 1; } else { error("ERROR unknown mode", NULL); } data->update_time = time(NULL); list_config(data); }
static int config_handler(TSCont cont, TSEvent event, void *edata) { plugin_state_t *pstate; invalidate_t *i, *iptr; TSCont free_cont; bool updated; TSMutex mutex; mutex = TSContMutexGet(cont); TSMutexLock(mutex); TSDebug(LOG_PREFIX, "In config Handler"); pstate = (plugin_state_t *)TSContDataGet(cont); i = copy_config(pstate->invalidate_list); updated = prune_config(&i); updated = load_config(pstate, &i) || updated; if (updated) { list_config(pstate, i); iptr = __sync_val_compare_and_swap(&(pstate->invalidate_list), pstate->invalidate_list, i); if (iptr) { free_cont = TSContCreate(free_handler, TSMutexCreate()); TSContDataSet(free_cont, (void *)iptr); TSContScheduleOnPool(free_cont, FREE_TMOUT, TS_THREAD_POOL_TASK); } } else { TSDebug(LOG_PREFIX, "No Changes"); if (i) { free_invalidate_t_list(i); } } TSMutexUnlock(mutex); // Don't reschedule for TS_EVENT_MGMT_UPDATE if (event == TS_EVENT_TIMEOUT) { TSContScheduleOnPool(cont, CONFIG_TMOUT, TS_THREAD_POOL_TASK); } return 0; }
int main(int argc, char **argv) { int result; int option_idx = 0; int do_list_interfaces = 0; int do_config = 0; int do_capture = 0; int i; char* interface = NULL; char* remote_host = NULL; unsigned int remote_port = 22; char* remote_username = NULL; char* remote_password = NULL; int do_dlts = 0; char* fifo = NULL; char* remote_interface = NULL; char* remote_capture_bin = NULL; char* extcap_filter = NULL; char* sshkey = NULL; char* sshkey_passphrase = NULL; char* remote_filter = NULL; unsigned long int count = 0; #ifdef _WIN32 WSADATA wsaData; attach_parent_console(); #endif /* _WIN32 */ opterr = 0; optind = 0; if (argc == 1) { help(argv[0]); return EXIT_FAILURE; } for (i = 0; i < argc; i++) { verbose_print("%s ", argv[i]); } verbose_print("\n"); while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: help(argv[0]); return EXIT_SUCCESS; case OPT_VERBOSE: verbose = TRUE; break; case OPT_VERSION: printf("%u.%u.%u\n", SSHDUMP_VERSION_MAJOR, SSHDUMP_VERSION_MINOR, SSHDUMP_VERSION_RELEASE); return EXIT_SUCCESS; case OPT_LIST_INTERFACES: do_list_interfaces = 1; break; case OPT_LIST_DLTS: do_dlts = 1; break; case OPT_INTERFACE: if (interface) g_free(interface); interface = g_strdup(optarg); break; case OPT_CONFIG: do_config = 1; break; case OPT_CAPTURE: do_capture = 1; break; case OPT_FIFO: if (fifo) g_free(fifo); fifo = g_strdup(optarg); break; case OPT_REMOTE_HOST: if (remote_host) g_free(remote_host); remote_host = g_strdup(optarg); break; case OPT_REMOTE_PORT: remote_port = (unsigned int)strtoul(optarg, NULL, 10); if (remote_port > 65535 || remote_port == 0) { printf("Invalid port: %s\n", optarg); return EXIT_FAILURE; } break; case OPT_REMOTE_USERNAME: if (remote_username) g_free(remote_username); remote_username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: if (remote_password) g_free(remote_password); remote_password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: if (sshkey) g_free(sshkey); sshkey = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: if (sshkey_passphrase) g_free(sshkey_passphrase); sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_REMOTE_INTERFACE: if (remote_interface) g_free(remote_interface); remote_interface = g_strdup(optarg); break; case OPT_REMOTE_CAPTURE_BIN: if (remote_capture_bin) g_free(remote_capture_bin); remote_capture_bin = g_strdup(optarg); break; case OPT_EXTCAP_FILTER: if (extcap_filter) g_free(extcap_filter); extcap_filter = g_strdup(optarg); break; case OPT_REMOTE_FILTER: if (remote_filter) g_free(remote_filter); remote_filter = g_strdup(optarg); break; case OPT_REMOTE_COUNT: count = strtoul(optarg, NULL, 10); break; case ':': /* missing option argument */ printf("Option '%s' requires an argument\n", argv[optind - 1]); break; default: printf("Invalid option: %s\n", argv[optind - 1]); return EXIT_FAILURE; } } if (optind != argc) { printf("Unexpected extra option: %s\n", argv[optind]); return EXIT_FAILURE; } if (do_list_interfaces) return list_interfaces(); if (do_config) return list_config(interface, remote_port); if (do_dlts) return list_dlts(interface); #ifdef _WIN32 result = WSAStartup(MAKEWORD(1,1), &wsaData); if (result != 0) { if (verbose) errmsg_print("ERROR: WSAStartup failed with error: %d\n", result); return 1; } #endif /* _WIN32 */ if (do_capture) { char* filter; int ret = 0; if (!fifo) { errmsg_print("ERROR: No FIFO or file specified\n"); return 1; } if (g_strcmp0(interface, SSH_EXTCAP_INTERFACE)) { errmsg_print("ERROR: invalid interface\n"); return 1; } if (!remote_host) { errmsg_print("Missing parameter: --remote-host"); return 1; } filter = concat_filters(extcap_filter, remote_filter); ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, remote_password, sshkey, sshkey_passphrase, remote_interface, filter, remote_capture_bin, count, fifo); g_free(filter); return ret; } verbose_print("You should not come here... maybe some parameter missing?\n"); return 1; }
int main(int argc, char **argv) { int result; int option_idx = 0; int i; char* remote_host = NULL; unsigned int remote_port = 22; char* remote_username = NULL; char* remote_password = NULL; char* remote_interface = NULL; char* remote_capture_bin = NULL; char* sshkey = NULL; char* sshkey_passphrase = NULL; char* remote_filter = NULL; unsigned long int count = 0; int ret = EXIT_FAILURE; extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1); #ifdef _WIN32 WSADATA wsaData; attach_parent_console(); #endif /* _WIN32 */ extcap_base_set_util_info(extcap_conf, SSHDUMP_VERSION_MAJOR, SSHDUMP_VERSION_MINOR, SSHDUMP_VERSION_RELEASE, NULL); extcap_base_register_interface(extcap_conf, SSH_EXTCAP_INTERFACE, "SSH remote capture", 147, "Remote capture dependent DLT"); opterr = 0; optind = 0; if (argc == 1) { help(argv[0]); goto end; } for (i = 0; i < argc; i++) { verbose_print("%s ", argv[i]); } verbose_print("\n"); while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: help(argv[0]); ret = EXIT_SUCCESS; goto end; case OPT_VERBOSE: verbose = TRUE; break; case OPT_VERSION: printf("%s.%s.%s\n", SSHDUMP_VERSION_MAJOR, SSHDUMP_VERSION_MINOR, SSHDUMP_VERSION_RELEASE); ret = EXIT_SUCCESS; goto end; case OPT_REMOTE_HOST: g_free(remote_host); remote_host = g_strdup(optarg); break; case OPT_REMOTE_PORT: remote_port = (unsigned int)strtoul(optarg, NULL, 10); if (remote_port > 65535 || remote_port == 0) { errmsg_print("Invalid port: %s", optarg); goto end; } break; case OPT_REMOTE_USERNAME: g_free(remote_username); remote_username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: g_free(remote_password); remote_password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: g_free(sshkey); sshkey = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: g_free(sshkey_passphrase); sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_REMOTE_INTERFACE: g_free(remote_interface); remote_interface = g_strdup(optarg); break; case OPT_REMOTE_CAPTURE_BIN: g_free(remote_capture_bin); remote_capture_bin = g_strdup(optarg); break; case OPT_REMOTE_FILTER: g_free(remote_filter); remote_filter = g_strdup(optarg); break; case OPT_REMOTE_COUNT: count = strtoul(optarg, NULL, 10); break; case ':': /* missing option argument */ errmsg_print("Option '%s' requires an argument", argv[optind - 1]); break; default: if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { errmsg_print("Invalid option: %s", argv[optind - 1]); goto end; } } } if (optind != argc) { errmsg_print("Unexpected extra option: %s", argv[optind]); goto end; } if (extcap_base_handle_interface(extcap_conf)) { ret = EXIT_SUCCESS; goto end; } if (extcap_conf->show_config) { ret = list_config(extcap_conf->interface, remote_port); goto end; } #ifdef _WIN32 result = WSAStartup(MAKEWORD(1,1), &wsaData); if (result != 0) { if (verbose) errmsg_print("ERROR: WSAStartup failed with error: %d", result); goto end; } #endif /* _WIN32 */ if (extcap_conf->capture) { char* filter; if (!remote_host) { errmsg_print("Missing parameter: --remote-host"); goto end; } filter = concat_filters(extcap_conf->capture_filter, remote_filter); ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, remote_password, sshkey, sshkey_passphrase, remote_interface, filter, remote_capture_bin, count, extcap_conf->fifo); g_free(filter); } else { verbose_print("You should not come here... maybe some parameter missing?\n"); ret = EXIT_FAILURE; } end: /* clean up stuff */ g_free(remote_host); g_free(remote_username); g_free(remote_password); g_free(remote_interface); g_free(remote_capture_bin); g_free(sshkey); g_free(sshkey_passphrase); g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; }
int main(int argc, char **argv) { int result; int option_idx = 0; char* remote_host = NULL; guint16 remote_port = 22; char* remote_username = NULL; char* remote_password = NULL; char* remote_interface = NULL; char* sshkey = NULL; char* sshkey_passphrase = NULL; char* remote_filter = NULL; guint32 count = 0; int ret = EXIT_FAILURE; extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1); char* help_url; char* help_header = NULL; #ifdef _WIN32 WSADATA wsaData; attach_parent_console(); #endif /* _WIN32 */ help_url = data_file_url("ciscodump.html"); extcap_base_set_util_info(extcap_conf, argv[0], CISCODUMP_VERSION_MAJOR, CISCODUMP_VERSION_MINOR, CISCODUMP_VERSION_RELEASE, help_url); g_free(help_url); extcap_base_register_interface(extcap_conf, CISCODUMP_EXTCAP_INTERFACE, "Cisco remote capture", 147, "Remote capture dependent DLT"); help_header = g_strdup_printf( " %s --extcap-interfaces\n" " %s --extcap-interface=%s --extcap-dlts\n" " %s --extcap-interface=%s --extcap-config\n" " %s --extcap-interface=%s --remote-host myhost --remote-port 22222 " "--remote-username myuser --remote-interface gigabit0/0 " "--fifo=FILENAME --capture\n", argv[0], argv[0], CISCODUMP_EXTCAP_INTERFACE, argv[0], CISCODUMP_EXTCAP_INTERFACE, argv[0], CISCODUMP_EXTCAP_INTERFACE); extcap_help_add_header(extcap_conf, help_header); g_free(help_header); extcap_help_add_option(extcap_conf, "--help", "print this help"); extcap_help_add_option(extcap_conf, "--version", "print the version"); extcap_help_add_option(extcap_conf, "--remote-host <host>", "the remote SSH host"); extcap_help_add_option(extcap_conf, "--remote-port <port>", "the remote SSH port (default: 22)"); extcap_help_add_option(extcap_conf, "--remote-username <username>", "the remote SSH username (default: the current user)"); extcap_help_add_option(extcap_conf, "--remote-password <password>", "the remote SSH password. " "If not specified, ssh-agent and ssh-key are used"); extcap_help_add_option(extcap_conf, "--sshkey <public key path>", "the path of the ssh key"); extcap_help_add_option(extcap_conf, "--sshkey-passphrase <public key passphrase>", "the passphrase to unlock public ssh"); extcap_help_add_option(extcap_conf, "--remote-interface <iface>", "the remote capture interface"); extcap_help_add_option(extcap_conf, "--remote-filter <filter>", "a filter for remote capture " "(default: don't capture data for lal interfaces IPs)"); opterr = 0; optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: extcap_help_print(extcap_conf); ret = EXIT_SUCCESS; goto end; case OPT_VERSION: printf("%s\n", extcap_conf->version); goto end; case OPT_REMOTE_HOST: g_free(remote_host); remote_host = g_strdup(optarg); break; case OPT_REMOTE_PORT: if (!ws_strtou16(optarg, NULL, &remote_port) || remote_port == 0) { g_warning("Invalid port: %s", optarg); goto end; } break; case OPT_REMOTE_USERNAME: g_free(remote_username); remote_username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: g_free(remote_password); remote_password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: g_free(sshkey); sshkey = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: g_free(sshkey_passphrase); sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_REMOTE_INTERFACE: g_free(remote_interface); remote_interface = g_strdup(optarg); break; case OPT_REMOTE_FILTER: g_free(remote_filter); remote_filter = g_strdup(optarg); break; case OPT_REMOTE_COUNT: if (!ws_strtou32(optarg, NULL, &count)) { g_warning("Invalid packet count: %s", optarg); goto end; } break; case ':': /* missing option argument */ g_warning("Option '%s' requires an argument", argv[optind - 1]); break; default: if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { g_warning("Invalid option: %s", argv[optind - 1]); goto end; } } } extcap_cmdline_debug(argv, argc); if (optind != argc) { g_warning("Unexpected extra option: %s", argv[optind]); goto end; } if (extcap_base_handle_interface(extcap_conf)) { ret = EXIT_SUCCESS; goto end; } if (extcap_conf->show_config) { ret = list_config(extcap_conf->interface, remote_port); goto end; } #ifdef _WIN32 result = WSAStartup(MAKEWORD(1,1), &wsaData); if (result != 0) { g_warning("ERROR: WSAStartup failed with error: %d", result); goto end; } #endif /* _WIN32 */ if (extcap_conf->capture) { if (!remote_host) { g_warning("Missing parameter: --remote-host"); goto end; } if (!remote_interface) { g_warning("ERROR: No interface specified (--remote-interface)"); goto end; } if (count == 0) { g_warning("ERROR: count of packets must be specified (--remote-count)"); goto end; } ret = ssh_open_remote_connection(remote_host, remote_port, remote_username, remote_password, sshkey, sshkey_passphrase, remote_interface, remote_filter, count, extcap_conf->fifo); } else { g_debug("You should not come here... maybe some parameter missing?"); ret = EXIT_FAILURE; } end: g_free(remote_host); g_free(remote_username); g_free(remote_password); g_free(remote_interface); g_free(sshkey); g_free(sshkey_passphrase); g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; }
int main(int argc, char *argv[]) { int option_idx = 0; int result; guint16 port = 0; int ret = EXIT_FAILURE; extcap_parameters* extcap_conf = g_new0(extcap_parameters, 1); char* help_url; char* help_header = NULL; char* payload = NULL; char* port_msg = NULL; #ifdef _WIN32 WSADATA wsaData; attach_parent_console(); #endif /* _WIN32 */ help_url = data_file_url("udpdump.html"); extcap_base_set_util_info(extcap_conf, argv[0], UDPDUMP_VERSION_MAJOR, UDPDUMP_VERSION_MINOR, UDPDUMP_VERSION_RELEASE, help_url); g_free(help_url); extcap_base_register_interface(extcap_conf, UDPDUMP_EXTCAP_INTERFACE, "UDP Listener remote capture", 252, "Exported PDUs"); help_header = g_strdup_printf( " %s --extcap-interfaces\n" " %s --extcap-interface=%s --extcap-dlts\n" " %s --extcap-interface=%s --extcap-config\n" " %s --extcap-interface=%s --port 5555 --fifo myfifo --capture", argv[0], argv[0], UDPDUMP_EXTCAP_INTERFACE, argv[0], UDPDUMP_EXTCAP_INTERFACE, argv[0], UDPDUMP_EXTCAP_INTERFACE); extcap_help_add_header(extcap_conf, help_header); g_free(help_header); extcap_help_add_option(extcap_conf, "--help", "print this help"); extcap_help_add_option(extcap_conf, "--version", "print the version"); port_msg = g_strdup_printf("the port to listens on. Default: %u", UDPDUMP_DEFAULT_PORT); extcap_help_add_option(extcap_conf, "--port <port>", port_msg); g_free(port_msg); opterr = 0; optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: extcap_help_print(extcap_conf); ret = EXIT_SUCCESS; goto end; case OPT_VERSION: printf("%s\n", extcap_conf->version); goto end; case OPT_PORT: if (!ws_strtou16(optarg, NULL, &port)) { g_warning("Invalid port: %s", optarg); goto end; } break; case OPT_PAYLOAD: g_free(payload); payload = g_strdup(optarg); break; case ':': /* missing option argument */ g_warning("Option '%s' requires an argument", argv[optind - 1]); break; default: if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { g_warning("Invalid option: %s", argv[optind - 1]); goto end; } } } extcap_cmdline_debug(argv, argc); if (optind != argc) { g_warning("Unexpected extra option: %s", argv[optind]); goto end; } if (extcap_base_handle_interface(extcap_conf)) { ret = EXIT_SUCCESS; goto end; } if (extcap_conf->show_config) { ret = list_config(extcap_conf->interface); goto end; } if (!payload) payload = g_strdup("data"); #ifdef _WIN32 result = WSAStartup(MAKEWORD(1,1), &wsaData); if (result != 0) { g_warning("Error: WSAStartup failed with error: %d", result); goto end; } #endif /* _WIN32 */ if (port == 0) port = UDPDUMP_DEFAULT_PORT; if (extcap_conf->capture) run_listener(extcap_conf->fifo, port, payload); end: /* clean up stuff */ extcap_base_cleanup(&extcap_conf); g_free(payload); return ret; }
int main(int argc, char *argv[]) { char* err_msg; int result; int option_idx = 0; ssh_params_t* ssh_params = ssh_params_new(); char* remote_interface = NULL; char* remote_filter = NULL; guint32 count = 0; int ret = EXIT_FAILURE; extcap_parameters * extcap_conf = g_new0(extcap_parameters, 1); char* help_url; char* help_header = NULL; /* * Get credential information for later use. */ init_process_policies(); /* * Attempt to get the pathname of the directory containing the * executable file. */ err_msg = init_progfile_dir(argv[0]); if (err_msg != NULL) { g_warning("Can't get pathname of directory containing the captype program: %s.", err_msg); g_free(err_msg); } help_url = data_file_url("ciscodump.html"); extcap_base_set_util_info(extcap_conf, argv[0], CISCODUMP_VERSION_MAJOR, CISCODUMP_VERSION_MINOR, CISCODUMP_VERSION_RELEASE, help_url); g_free(help_url); extcap_base_register_interface(extcap_conf, CISCODUMP_EXTCAP_INTERFACE, "Cisco remote capture", 147, "Remote capture dependent DLT"); help_header = g_strdup_printf( " %s --extcap-interfaces\n" " %s --extcap-interface=%s --extcap-dlts\n" " %s --extcap-interface=%s --extcap-config\n" " %s --extcap-interface=%s --remote-host myhost --remote-port 22222 " "--remote-username myuser --remote-interface gigabit0/0 " "--fifo=FILENAME --capture\n", argv[0], argv[0], CISCODUMP_EXTCAP_INTERFACE, argv[0], CISCODUMP_EXTCAP_INTERFACE, argv[0], CISCODUMP_EXTCAP_INTERFACE); extcap_help_add_header(extcap_conf, help_header); g_free(help_header); extcap_help_add_option(extcap_conf, "--help", "print this help"); extcap_help_add_option(extcap_conf, "--version", "print the version"); extcap_help_add_option(extcap_conf, "--remote-host <host>", "the remote SSH host"); extcap_help_add_option(extcap_conf, "--remote-port <port>", "the remote SSH port (default: 22)"); extcap_help_add_option(extcap_conf, "--remote-username <username>", "the remote SSH username (default: the current user)"); extcap_help_add_option(extcap_conf, "--remote-password <password>", "the remote SSH password. " "If not specified, ssh-agent and ssh-key are used"); extcap_help_add_option(extcap_conf, "--sshkey <public key path>", "the path of the ssh key"); extcap_help_add_option(extcap_conf, "--sshkey-passphrase <public key passphrase>", "the passphrase to unlock public ssh"); extcap_help_add_option(extcap_conf, "--proxycommand <proxy command>", "the command to use as proxy the the ssh connection"); extcap_help_add_option(extcap_conf, "--remote-interface <iface>", "the remote capture interface"); extcap_help_add_option(extcap_conf, "--remote-filter <filter>", "a filter for remote capture " "(default: don't capture data for lal interfaces IPs)"); opterr = 0; optind = 0; if (argc == 1) { extcap_help_print(extcap_conf); goto end; } while ((result = getopt_long(argc, argv, ":", longopts, &option_idx)) != -1) { switch (result) { case OPT_HELP: extcap_help_print(extcap_conf); ret = EXIT_SUCCESS; goto end; case OPT_VERSION: printf("%s\n", extcap_conf->version); goto end; case OPT_REMOTE_HOST: g_free(ssh_params->host); ssh_params->host = g_strdup(optarg); break; case OPT_REMOTE_PORT: if (!ws_strtou16(optarg, NULL, &ssh_params->port) || ssh_params->port == 0) { g_warning("Invalid port: %s", optarg); goto end; } break; case OPT_REMOTE_USERNAME: g_free(ssh_params->username); ssh_params->username = g_strdup(optarg); break; case OPT_REMOTE_PASSWORD: g_free(ssh_params->password); ssh_params->password = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_SSHKEY: g_free(ssh_params->sshkey_path); ssh_params->sshkey_path = g_strdup(optarg); break; case OPT_SSHKEY_PASSPHRASE: g_free(ssh_params->sshkey_passphrase); ssh_params->sshkey_passphrase = g_strdup(optarg); memset(optarg, 'X', strlen(optarg)); break; case OPT_PROXYCOMMAND: g_free(ssh_params->proxycommand); ssh_params->proxycommand = g_strdup(optarg); break; case OPT_REMOTE_INTERFACE: g_free(remote_interface); remote_interface = g_strdup(optarg); break; case OPT_REMOTE_FILTER: g_free(remote_filter); remote_filter = g_strdup(optarg); break; case OPT_REMOTE_COUNT: if (!ws_strtou32(optarg, NULL, &count)) { g_warning("Invalid packet count: %s", optarg); goto end; } break; case ':': /* missing option argument */ g_warning("Option '%s' requires an argument", argv[optind - 1]); break; default: if (!extcap_base_parse_options(extcap_conf, result - EXTCAP_OPT_LIST_INTERFACES, optarg)) { g_warning("Invalid option: %s", argv[optind - 1]); goto end; } } } extcap_cmdline_debug(argv, argc); if (optind != argc) { g_warning("Unexpected extra option: %s", argv[optind]); goto end; } if (extcap_base_handle_interface(extcap_conf)) { ret = EXIT_SUCCESS; goto end; } if (extcap_conf->show_config) { ret = list_config(extcap_conf->interface, ssh_params->port); goto end; } err_msg = ws_init_sockets(); if (err_msg != NULL) { g_warning("ERROR: %s", err_msg); g_free(err_msg); g_warning("%s", please_report_bug()); goto end; } if (extcap_conf->capture) { if (!ssh_params->host) { g_warning("Missing parameter: --remote-host"); goto end; } if (!remote_interface) { g_warning("ERROR: No interface specified (--remote-interface)"); goto end; } if (count == 0) { g_warning("ERROR: count of packets must be specified (--remote-count)"); goto end; } ssh_params->debug = extcap_conf->debug; ret = ssh_open_remote_connection(ssh_params, remote_interface, remote_filter, count, extcap_conf->fifo); } else { g_debug("You should not come here... maybe some parameter missing?"); ret = EXIT_FAILURE; } end: ssh_params_free(ssh_params); g_free(remote_interface); g_free(remote_filter); extcap_base_cleanup(&extcap_conf); return ret; }
void TSPluginInit(int argc, const char *argv[]) { TSPluginRegistrationInfo info; TSCont main_cont, config_cont; plugin_state_t *pstate; invalidate_t *iptr = NULL; bool disable_timed_reload = false; TSDebug(LOG_PREFIX, "Starting plugin init"); pstate = (plugin_state_t *)TSmalloc(sizeof(plugin_state_t)); init_plugin_state_t(pstate); int c; static const struct option longopts[] = {{"config", required_argument, NULL, 'c'}, {"log", required_argument, NULL, 'l'}, {"disable-timed-reload", no_argument, NULL, 'd'}, {NULL, 0, NULL, 0}}; while ((c = getopt_long(argc, (char *const *)argv, "c:l:", longopts, NULL)) != -1) { switch (c) { case 'c': pstate->config_file = TSstrdup(optarg); break; case 'l': if (TS_SUCCESS == TSTextLogObjectCreate(optarg, TS_LOG_MODE_ADD_TIMESTAMP, &pstate->log)) { TSTextLogObjectRollingEnabledSet(pstate->log, 1); TSTextLogObjectRollingIntervalSecSet(pstate->log, LOG_ROLL_INTERVAL); TSTextLogObjectRollingOffsetHrSet(pstate->log, LOG_ROLL_OFFSET); } break; case 'd': disable_timed_reload = true; break; default: break; } } if (!pstate->config_file) { TSError("[regex_revalidate] Plugin requires a --config option along with a config file name"); free_plugin_state_t(pstate); return; } if (!load_config(pstate, &iptr)) { TSDebug(LOG_PREFIX, "Problem loading config from file %s", pstate->config_file); } else { pstate->invalidate_list = iptr; list_config(pstate, iptr); } info.plugin_name = LOG_PREFIX; info.vendor_name = "Apache Software Foundation"; info.support_email = "*****@*****.**"; if (TSPluginRegister(&info) != TS_SUCCESS) { TSError("[regex_revalidate] Plugin registration failed"); free_plugin_state_t(pstate); return; } else { TSDebug(LOG_PREFIX, "Plugin registration succeeded"); } if (!check_ts_version()) { TSError("[regex_revalidate] Plugin requires Traffic Server %d.%d.%d", TS_VERSION_MAJOR, TS_VERSION_MINOR, TS_VERSION_MICRO); free_plugin_state_t(pstate); return; } pcre_malloc = &ts_malloc; pcre_free = &ts_free; main_cont = TSContCreate(main_handler, NULL); TSContDataSet(main_cont, (void *)pstate); TSHttpHookAdd(TS_HTTP_CACHE_LOOKUP_COMPLETE_HOOK, main_cont); config_cont = TSContCreate(config_handler, TSMutexCreate()); TSContDataSet(config_cont, (void *)pstate); TSMgmtUpdateRegister(config_cont, LOG_PREFIX); if (!disable_timed_reload) { TSContScheduleOnPool(config_cont, CONFIG_TMOUT, TS_THREAD_POOL_TASK); } TSDebug(LOG_PREFIX, "Plugin Init Complete"); }
int main(int argc, char **argv) { int optval = 1, n, reading = 1, connection_counter; int fd_parent, fd_child, fd_trans, fd_dump, clientlen; off_t sendfile_offset; struct sockaddr_in server_addr, client_addr, trans_addr; struct hostent *hostp, *trans; struct stat stat_buf; char buffer_in[BUFFERSIZE], buffer_out[BUFFERSIZE], file_path[128]; char *hostaddrp, *trans_host; FILE *fd_in, *fd_out, *fd_log; int mock_port, trans_port; pid_t process_id = 0; const char *base_path=getenv("HOME"); struct shared_data *shmem_data = get_shmem(base_path); // arguments list // set stage "stage name" // set mode [play|record] // get config // server "port" "trans host" "trans port" // stop if (argc > 1){ if (strcmp(argv[1], "server") == 0){ if (argc == 5){ if (shmem_data->pid == 0){ mock_port = atoi(argv[2]); trans_host = argv[3]; trans_port = atoi(argv[4]); shmem_data->recording = 1; printf("0> tunnel: %i:%s:%i\n", mock_port, trans_host, trans_port); } else { error("ERROR server seems to be already running", NULL); } } else if (argc == 3){ mock_port = atoi(argv[2]); shmem_data->recording = 0; printf("0> player: %i\n", mock_port); } else { error("ERROR wrong number of parameter for server", NULL); } } else if (strcmp(argv[1], "set") == 0){ if (argc == 4){ if (strcmp(argv[2], "stage") == 0){ set_stage(shmem_data, argv[3]); } else if (strcmp(argv[2], "mode") == 0){ set_mode(shmem_data, argv[3]); } else { error("ERROR unknown variable to set", NULL); } } else { error("ERROR wrong number of parameter for set", NULL); } exit (0); } else if (strcmp(argv[1], "get") == 0){ if (argc == 3 && strcmp(argv[2], "config") == 0){ list_config(shmem_data); } else { error("ERROR unknown param for get", NULL); } exit (0); } else if (strcmp(argv[1], "stop") == 0){ stop_server(shmem_data); exit (0); } else { error("0> ERROR command not found", NULL); } } else { error("0> ERROR not enough parameters", NULL); } // XXX: fork and daemonization process_id = fork(); if (process_id < 0){ error("0> ERROR fork failed!", NULL); } if (process_id > 0){ printf ("Killing parent process\n"); exit(0); } // get child pid set_pid(shmem_data, getpid()); // create log file fd_log = fopen("mock.log", "a"); if (fd_log == NULL){ error("ERROR fail to open log file", NULL); } // daemonization stuff /* umask(0); setsid(); close(STDIN_FILENO); close(STDOUT_FILENO); close(STDERR_FILENO); */ fd_parent = socket(AF_INET, SOCK_STREAM, 0); if (fd_parent < 0) { error("0> ERROR opening socket", fd_log); } debug(fd_log, -1, "Starting!"); setsockopt(fd_parent, SOL_SOCKET, SO_REUSEADDR, (const void *)&optval , sizeof(int)); bzero((char *) &server_addr, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_addr.s_addr = htonl(INADDR_ANY); server_addr.sin_port = htons((unsigned short)mock_port); if (bind(fd_parent, (struct sockaddr *) &server_addr, sizeof(server_addr)) < 0) { error("0> ERROR on binding", fd_log); } if (listen(fd_parent, 5) < 0){ error("0> ERROR on listen", fd_log); } if (shmem_data->recording) { // setup and test trans connection fd_trans = socket(AF_INET, SOCK_STREAM, 0); if (fd_trans < 0) error ("0> ERROR creating trans socket", fd_log); trans = gethostbyname(trans_host); if (trans == NULL) error("0> ERROR trans host not found", fd_log); bzero((char *) &trans_addr, sizeof(trans_addr)); trans_addr.sin_family = AF_INET; bcopy((char *)trans->h_addr, (char *)&trans_addr.sin_addr.s_addr, trans->h_length); trans_addr.sin_port = htons(trans_port); n = connect(fd_trans, (struct sockaddr *)&trans_addr, sizeof(trans_addr)); if (n < 0) error("0> ERROR connecting to trans", fd_log); // read from trans bzero(buffer_out, BUFFERSIZE); read(fd_trans, buffer_out, BUFFERSIZE); shutdown(fd_trans, 2); if (strcmp(buffer_out, "220 Welcome.\n") != 0) error ("0> ERROR handshake with trans failed", fd_log); } clientlen = sizeof(client_addr); connection_counter = 0; while (1) { connection_counter++; fd_child = accept(fd_parent, (struct sockaddr *) &client_addr, &clientlen); if (fd_child < 0) { error("0> ERROR on accept", fd_log); } if (shmem_data->recording) { // creating file sprintf(file_path, "%s/%s/%i.in.dump", base_path, shmem_data->stage, connection_counter); debug(fd_log, connection_counter, "filepath: "); debug(fd_log, -1, file_path); ensure_file_path(file_path); fd_in = fopen(file_path, "w"); sprintf(file_path, "%s/%s/%i.out.dump", base_path, shmem_data->stage, connection_counter); fd_out = fopen(file_path, "w"); if (fd_in == NULL) error("ERROR could not create input log file", fd_log); if (fd_out == NULL) error("ERROR could not create output log file", fd_log); } hostp = gethostbyaddr((const char *)&client_addr.sin_addr.s_addr, sizeof(client_addr.sin_addr.s_addr), AF_INET); if (hostp == NULL){ error("ERROR on gethostbyaddr", fd_log); } hostaddrp = inet_ntoa(client_addr.sin_addr); if (hostaddrp == NULL){ error("ERROR on inet_ntoa\n", fd_log); } debug(fd_log, connection_counter, "server established connection with:"); debug(fd_log, -1, hostaddrp); if (shmem_data->recording) { // connect to trans fd_trans = socket(AF_INET, SOCK_STREAM, 0); n = connect(fd_trans, (struct sockaddr *) &trans_addr, sizeof(trans_addr)); if (n < 0){ debug(fd_log, connection_counter, "ERROR connecting to trans"); } } reading = 1; while (reading){ bzero(buffer_in, BUFFERSIZE); n = read(fd_child, buffer_in, BUFFERSIZE); if (n == 0){ debug(fd_log, connection_counter, "end of message"); reading = 0; continue; } if (n < 0){ debug(fd_log, connection_counter, "ERROR reading from socket"); reading = 0; continue; } debug(fd_log, connection_counter, "server received bytes:"); if (shmem_data->recording){ write(fd_trans, buffer_in, n); fwrite(buffer_in, 1, n, fd_in); } if (is_eom(buffer_in)){ debug(fd_log, connection_counter, "EOM"); if (shmem_data->recording){ // read from trans socket bzero(buffer_out, BUFFERSIZE); n = read(fd_trans, buffer_out, BUFFERSIZE); debug(fd_log, connection_counter, "readed:"); debug(fd_log, -1, buffer_out); while (n > 0){ debug(fd_log, connection_counter, "writting back:"); debug(fd_log, -1, buffer_out); write(fd_child, buffer_out, n); fwrite(buffer_out, 1, n, fd_out); bzero(buffer_out, BUFFERSIZE); n = read(fd_trans, buffer_out, BUFFERSIZE); if (n < 0){ error("ERROR couldn't read from trans", fd_log); } } } else { debug(fd_log, connection_counter, "EOM: retrieving file"); sprintf(file_path, "%s/%s/%i.out.dump", base_path, shmem_data->stage, connection_counter); debug(fd_log, connection_counter, "filepath:"); debug(fd_log, -1, file_path); fd_dump = open(file_path, O_RDONLY); if (fd_dump == -1){ debug(fd_log, connection_counter, "ERROR failed to open file"); continue; } fstat(fd_dump, &stat_buf); sendfile_offset = 0; sendfile(fd_child, fd_dump, &sendfile_offset, stat_buf.st_size); } reading = 0; } } debug(fd_log, connection_counter, "Closing connection"); if (shmem_data->recording){ fclose(fd_in); fclose(fd_out); close(fd_trans); } close(fd_child); debug(fd_log, -1, "- - - - - - - - - - - - - - - - - - -"); } }
void set_stage(struct shared_data *data, char value[100]){ strcpy(data->stage, str2md5(value, strlen(value))); data->update_time = time(NULL); list_config(data); }
void set_default_config (struct shared_data *data) { strcpy(data->stage, str2md5("default", 7)); data->recording = 0; data->update_time = time(NULL); list_config(data); }