#include <netinet/in.h>
#endif
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
+
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h> /* needed to define AF_ values on UNIX */
#endif
return 0;
}
-
-int capture_opts_list_link_layer_types(capture_options *capture_opts)
+/*
+ * If you change the output format of this function, you MUST update
+ * capture_sync.c:sync_linktype_list_open() accordingly!
+ */
+int
+capture_opts_list_link_layer_types(capture_options *capture_opts, gboolean machine_readable)
{
- gchar *err_str;
+ gchar *err_str, *desc_str;
GList *lt_list, *lt_entry;
data_link_info_t *data_link_info;
cmdarg_err("The capture device \"%s\" has no data link types.", capture_opts->iface);
return 2;
}
- cmdarg_err_cont("Data link types (use option -y to set):");
- for (lt_entry = lt_list; lt_entry != NULL;
- lt_entry = g_list_next(lt_entry)) {
- data_link_info = lt_entry->data;
- cmdarg_err_cont(" %s", data_link_info->name);
- if (data_link_info->description != NULL)
- cmdarg_err_cont(" (%s)", data_link_info->description);
- else
- cmdarg_err_cont(" (not supported)");
- putchar('\n');
+ if (machine_readable) { /* tab-separated values to stdout */
+ for (lt_entry = lt_list; lt_entry != NULL; lt_entry = g_list_next(lt_entry)) {
+ data_link_info = lt_entry->data;
+ if (data_link_info->description != NULL)
+ desc_str = data_link_info->description;
+ else
+ desc_str = "(not supported)";
+ printf("%d\t%s\t%s\n", data_link_info->dlt, data_link_info->name,
+ desc_str);
+ }
+ } else {
+ cmdarg_err_cont("Data link types (use option -y to set):");
+ for (lt_entry = lt_list; lt_entry != NULL;
+ lt_entry = g_list_next(lt_entry)) {
+ data_link_info = lt_entry->data;
+ cmdarg_err_cont(" %s", data_link_info->name);
+ if (data_link_info->description != NULL)
+ cmdarg_err_cont(" (%s)", data_link_info->description);
+ else
+ cmdarg_err_cont(" (not supported)");
+ putchar('\n');
+ }
}
free_pcap_linktype_list(lt_list);
/* Return an ASCII-formatted list of interfaces. */
#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
int
-capture_opts_list_interfaces(gboolean verbose)
+capture_opts_list_interfaces(gboolean machine_readable)
{
GList *if_list;
GList *if_entry;
if_info = if_entry->data;
printf("%d. %s", i++, if_info->name);
- if (!verbose) {
+ if (!machine_readable) {
/* Add the description if it exists */
if (if_info->description != NULL)
printf(" (%s)", if_info->description);
}
#endif
+/* Initialize an argument list and add dumpcap to it. */
+static const char **
+init_pipe_args(int *argc) {
+ const char **argv;
+ const char *progfile_dir;
+ char *exename;
+ progfile_dir = get_progfile_dir();
+ if (progfile_dir == NULL) {
+ return NULL;
+ }
-#define ARGV_NUMBER_LEN 24
+ /* Allocate the string pointer array with enough space for the
+ terminating NULL pointer. */
+ *argc = 0;
+ argv = g_malloc(sizeof (char *));
+ *argv = NULL;
+
+ /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
+ exename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "dumpcap", progfile_dir);
+
+ /* Make that the first argument in the argument list (argv[0]). */
+ argv = sync_pipe_add_arg(argv, argc, exename);
+
+ return argv;
+}
+
+#define ARGV_NUMBER_LEN 24
/* a new capture run: start a new dumpcap task and hand over parameters through command line */
gboolean
sync_pipe_start(capture_options *capture_opts) {
enum PIPES { PIPE_READ, PIPE_WRITE }; /* Constants 0 and 1 for PIPE_READ and PIPE_WRITE */
#endif
int sync_pipe_read_fd;
- const char *progfile_dir;
- char *exename;
int argc;
const char **argv;
capture_opts->fork_child = -1;
- progfile_dir = get_progfile_dir();
- if (progfile_dir == NULL) {
- /* We don't know where to find dumpcap. */
- simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "We don't know where to find dumpcap.");
- return FALSE;
+ argv = init_pipe_args(&argc);
+ if (!argv) {
+ /* We don't know where to find dumpcap. */
+ simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK, "We don't know where to find dumpcap.");
+ return FALSE;
}
- /* Allocate the string pointer array with enough space for the
- terminating NULL pointer. */
- argc = 0;
- argv = g_malloc(sizeof (char *));
- *argv = NULL;
-
- /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
- exename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "dumpcap", progfile_dir);
-
- /* Make that the first argument in the argument list (argv[0]). */
- argv = sync_pipe_add_arg(argv, &argc, exename);
-
argv = sync_pipe_add_arg(argv, &argc, "-i");
argv = sync_pipe_add_arg(argv, &argc, capture_opts->iface);
eth_close(1);
dup(sync_pipe[PIPE_WRITE]);
eth_close(sync_pipe[PIPE_READ]);
- execv(exename, (gpointer)argv);
+ execv(argv[0], (gpointer)argv);
g_snprintf(errmsg, sizeof errmsg, "Couldn't run %s in child process: %s",
- exename, strerror(errno));
+ argv[0], strerror(errno));
sync_pipe_errmsg_to_parent(errmsg, "");
/* Exit with "_exit()", so that we don't close the connection
sync_pipe_read_fd = sync_pipe[PIPE_READ];
#endif
- g_free(exename);
+ g_free(argv[0]); /* exename */
/* Parent process - read messages from the child process over the
sync pipe. */
}
/*
- * Get an interface list using dumpcap. On success, msg points to
+ * Run dumpcap with the supplied arguments. On success, msg points to
* a buffer containing the dumpcap output and returns 0. On failure, msg
* points to the error message returned by dumpcap, and returns dumpcap's
* exit value. In either case, msg must be freed with g_free().
*/
-/* XXX - This duplicates a lot of code in sync_pipe_start() and sync_interface_list_open() */
+/* XXX - This duplicates a lot of code in sync_pipe_start() */
#define PIPE_BUF_SIZE 5120
-int
-sync_interface_list_open(gchar **msg) {
+static int
+sync_pipe_run_command(const char** argv, gchar **msg) {
#ifdef _WIN32
HANDLE sync_pipe_read; /* pipe used to send messages from child to parent */
HANDLE sync_pipe_write; /* pipe used to send messages from parent to child */
#endif
int fork_child = -1, fork_child_status;
int sync_pipe_read_fd = -1;
- const char *progfile_dir;
- char *exename;
- int argc;
- const char **argv;
GString *msg_buf = NULL;
gchar buf[PIPE_BUF_SIZE+1];
int count;
- g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open");
+ g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_pipe_run_command");
if (!msg) {
/* We can't return anything */
return -1;
}
- progfile_dir = get_progfile_dir();
- if (progfile_dir == NULL) {
- /* We don't know where to find dumpcap. */
- *msg = g_strdup("We don't know where to find dumpcap.");
- return CANT_RUN_DUMPCAP;
- }
-
- /* Allocate the string pointer array with enough space for the
- terminating NULL pointer. */
- argc = 0;
- argv = g_malloc(sizeof (char *));
- *argv = NULL;
-
- /* take Wireshark's absolute program path and replace "Wireshark" with "dumpcap" */
- exename = g_strdup_printf("%s" G_DIR_SEPARATOR_S "dumpcap", progfile_dir);
-
- /* Make that the first argument in the argument list (argv[0]). */
- argv = sync_pipe_add_arg(argv, &argc, exename);
-
- /* Ask for the interface list */
- argv = sync_pipe_add_arg(argv, &argc, "-I");
- argv = sync_pipe_add_arg(argv, &argc, "l");
-
-
- /* dumpcap should be running in capture child mode (hidden feature) */
-#ifndef DEBUG_CHILD
- argv = sync_pipe_add_arg(argv, &argc, "-Z");
-#endif
-
-
#ifdef _WIN32
/* init SECURITY_ATTRIBUTES */
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
eth_close(1);
dup(sync_pipe[PIPE_WRITE]);
eth_close(sync_pipe[PIPE_READ]);
- execv(exename, (gpointer)argv);
+ execv(argv[0], (gpointer)argv);
*msg = g_strdup_printf("Couldn't run %s in child process: %s",
- exename, strerror(errno));
+ argv[0], strerror(errno));
return CANT_RUN_DUMPCAP;
}
sync_pipe_read_fd = sync_pipe[PIPE_READ];
#endif
- g_free(exename);
+ g_free(argv[0]); /* exename */
/* Parent process - read messages from the child process over the
sync pipe. */
return fork_child_status;
}
+/*
+ * Get an interface list using dumpcap. On success, msg points to
+ * a buffer containing the dumpcap output and returns 0. On failure, msg
+ * points to the error message returned by dumpcap, and returns dumpcap's
+ * exit value. In either case, msg must be freed with g_free().
+ */
+int
+sync_interface_list_open(gchar **msg) {
+ int argc;
+ const char **argv;
+
+ if (!msg) {
+ /* We can't return anything */
+#ifdef _WIN32
+ g_string_free(args, TRUE);
+#endif
+ return -1;
+ }
+
+ g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_list_open");
+
+ argv = init_pipe_args(&argc);
+
+ if (!argv) {
+ *msg = g_strdup_printf("We don't know where to find dumpcap.");
+ return CANT_RUN_DUMPCAP;
+ }
+
+ /* Ask for the interface list */
+ argv = sync_pipe_add_arg(argv, &argc, "-D");
+ argv = sync_pipe_add_arg(argv, &argc, "-M");
+
+ /* dumpcap should be running in capture child mode (hidden feature) */
+#ifndef DEBUG_CHILD
+ argv = sync_pipe_add_arg(argv, &argc, "-Z");
+#endif
+
+ return sync_pipe_run_command(argv, msg);
+}
+
+/*
+ * Get an linktype list using dumpcap. On success, msg points to
+ * a buffer containing the dumpcap output and returns 0. On failure, msg
+ * points to the error message returned by dumpcap, and returns dumpcap's
+ * exit value. In either case, msg must be freed with g_free().
+ */
+int
+sync_linktype_list_open(gchar *ifname, gchar **msg) {
+ int argc;
+ const char **argv;
+
+ if (!msg) {
+ /* We can't return anything */
+#ifdef _WIN32
+ g_string_free(args, TRUE);
+#endif
+ return -1;
+ }
+
+ g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_linktype_list_open");
+
+ argv = init_pipe_args(&argc);
+
+ if (!argv) {
+ *msg = g_strdup_printf("We don't know where to find dumpcap.");
+ return CANT_RUN_DUMPCAP;
+ }
+
+ /* Ask for the linktype list */
+ argv = sync_pipe_add_arg(argv, &argc, "-i");
+ argv = sync_pipe_add_arg(argv, &argc, ifname);
+ argv = sync_pipe_add_arg(argv, &argc, "-L");
+ argv = sync_pipe_add_arg(argv, &argc, "-M");
+
+ /* dumpcap should be running in capture child mode (hidden feature) */
+#ifndef DEBUG_CHILD
+ argv = sync_pipe_add_arg(argv, &argc, "-Z");
+#endif
+
+ return sync_pipe_run_command(argv, msg);
+}
+
+/*
+ * Get interface stats using dumpcap. On success, msg points to
+ * a buffer containing the dumpcap output and returns 0. On failure, msg
+ * points to the error message returned by dumpcap, and returns dumpcap's
+ * exit value. In either case, msg must be freed with g_free().
+ */
+int
+sync_interface_stats_open(gchar *ifname, gchar **msg) {
+ int argc;
+ const char **argv;
+
+ if (!msg) {
+ /* We can't return anything */
+#ifdef _WIN32
+ g_string_free(args, TRUE);
+#endif
+ return -1;
+ }
+
+ g_log(LOG_DOMAIN_CAPTURE, G_LOG_LEVEL_DEBUG, "sync_interface_stats_open");
+
+ argv = init_pipe_args(&argc);
+
+ if (!argv) {
+ *msg = g_strdup_printf("We don't know where to find dumpcap.");
+ return CANT_RUN_DUMPCAP;
+ }
+
+ /* Ask for the linktype list */
+ argv = sync_pipe_add_arg(argv, &argc, "-i");
+ argv = sync_pipe_add_arg(argv, &argc, ifname);
+ argv = sync_pipe_add_arg(argv, &argc, "-S");
+ argv = sync_pipe_add_arg(argv, &argc, "-M");
+
+ /* dumpcap should be running in capture child mode (hidden feature) */
+#ifndef DEBUG_CHILD
+ argv = sync_pipe_add_arg(argv, &argc, "-Z");
+#endif
+
+ return sync_pipe_run_command(argv, msg);
+}
/* read a number of bytes from a pipe */
/* (blocks until enough bytes read or an error occurs) */
fprintf(output, " -y <link type> link layer type (def: first appropriate)\n");
fprintf(output, " -D print list of interfaces and exit\n");
fprintf(output, " -L print list of link-layer types of iface and exit\n");
- fprintf(output, " -I [l|s] print a detailed interface list (l) or interface statistics (s).\n");
+ fprintf(output, " -M for -D and -L, produce machine-readable output\n");
fprintf(output, "\n");
fprintf(output, "Stop conditions:\n");
fprintf(output, " -c <packet count> stop after n packets (def: infinite)\n");
gboolean stats_known;
struct pcap_stat stats;
GLogLevelFlags log_flags;
+ gboolean list_interfaces = FALSE;
gboolean list_link_layer_types = FALSE;
+ gboolean machine_readable = FALSE;
int status;
-#define OPTSTRING_INIT "a:b:c:Df:hI:i:Lps:vw:y:Z"
+#define OPTSTRING_INIT "a:b:c:Df:hi:LMps:vw:y:Z"
#ifdef _WIN32
#define OPTSTRING_WIN32 "B:"
/*** all non capture option specific ***/
case 'D': /* Print a list of capture devices and exit */
- status = capture_opts_list_interfaces(FALSE);
- exit_main(status);
+ list_interfaces = TRUE;
break;
- /* XXX - We might want to use 'D' for this. Do we use GNU
- * getopt on every platform (which supports optional arguments)? */
- /* XXX - Implement interface stats */
- case 'I':
- status = capture_opts_list_interfaces(TRUE);
- exit_main(status);
case 'L': /* Print list of link-layer types and exit */
list_link_layer_types = TRUE;
break;
+ case 'M': /* For -D and -L, print machine-readable output */
+ machine_readable = TRUE;
+ break;
default:
case '?': /* Bad flag - print usage message */
cmdarg_err("Invalid Option: %s", argv[optind-1]);
exit_main(1);
}
- if (list_link_layer_types) {
+ if (list_interfaces && list_link_layer_types) {
+ cmdarg_err("Only one of -D or -L may be supplied.");
+ exit_main(1);
+ } else if (list_link_layer_types) {
/* We're supposed to list the link-layer types for an interface;
did the user also specify a capture file to be read? */
/* No - did they specify a ring buffer option? */
/* get_interface_descriptive_name() is not available! */
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", capture_opts->iface);
- if (list_link_layer_types) {
- status = capture_opts_list_link_layer_types(capture_opts);
+ if (list_interfaces) {
+ status = capture_opts_list_interfaces(machine_readable);
+ exit_main(status);
+ } else if (list_link_layer_types) {
+ status = capture_opts_list_link_layer_types(capture_opts, machine_readable);
exit_main(status);
}