aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2010-05-07 19:24:32 +0000
committerGuy Harris <guy@alum.mit.edu>2010-05-07 19:24:32 +0000
commit077ff72ac19c4c48a47f914615f3ddc50b086c72 (patch)
tree2489e60d99c5ab28316218fa2841a88a89fe8a18
parent66f101f10fd9f48d543060f16f8f66de2de62a67 (diff)
As with the list of data link types, so with the list of interfaces; move
the code to print the machine-readable format into dumpcap, and have the code in capture_opts.c just print the human-readable format. svn path=/trunk/; revision=32714
-rw-r--r--capture_opts.c88
-rw-r--r--capture_opts.h6
-rw-r--r--dumpcap.c97
-rw-r--r--gtk/main.c19
-rw-r--r--tshark.c21
5 files changed, 143 insertions, 88 deletions
diff --git a/capture_opts.c b/capture_opts.c
index f69f791f08..54c328d070 100644
--- a/capture_opts.c
+++ b/capture_opts.c
@@ -591,35 +591,13 @@ capture_opts_print_link_layer_types(GList *lt_list)
}
}
-/* Return an ASCII-formatted list of interfaces. */
-#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
-int
-capture_opts_list_interfaces(gboolean machine_readable)
+/* Print an ASCII-formatted list of interfaces. */
+void
+capture_opts_print_interfaces(GList *if_list)
{
- GList *if_list;
+ int i;
GList *if_entry;
if_info_t *if_info;
- int err;
- gchar *err_str;
- int i;
- GSList *addr;
- if_addr_t *if_addr;
- char addr_str[ADDRSTRLEN];
-
- if_list = capture_interface_list(&err, &err_str);
- if (if_list == NULL) {
- switch (err) {
- case CANT_GET_INTERFACE_LIST:
- cmdarg_err("%s", err_str);
- g_free(err_str);
- break;
-
- case NO_INTERFACES_FOUND:
- cmdarg_err("There are no interfaces on which a capture can be done");
- break;
- }
- return err;
- }
i = 1; /* Interface id number */
for (if_entry = g_list_first(if_list); if_entry != NULL;
@@ -627,61 +605,11 @@ capture_opts_list_interfaces(gboolean machine_readable)
if_info = (if_info_t *)if_entry->data;
printf("%d. %s", i++, if_info->name);
- if (!machine_readable) {
- /* Add the description if it exists */
- if (if_info->description != NULL)
- printf(" (%s)", if_info->description);
- } else {
- /*
- * Add the contents of the if_entry struct in a parseable format.
- * Each if_entry element is tab-separated. Addresses are comma-
- * separated.
- */
- /* XXX - Make sure our description doesn't contain a tab */
- if (if_info->description != NULL)
- printf("\t%s\t", if_info->description);
- else
- printf("\t\t");
-
- for(addr = g_slist_nth(if_info->addrs, 0); addr != NULL;
- addr = g_slist_next(addr)) {
- if (addr != g_slist_nth(if_info->addrs, 0))
- printf(",");
-
- if_addr = (if_addr_t *)addr->data;
- switch(if_addr->ifat_type) {
- case IF_AT_IPv4:
- if (inet_ntop(AF_INET, &if_addr->addr.ip4_addr, addr_str,
- ADDRSTRLEN)) {
- printf("%s", addr_str);
- } else {
- printf("<unknown IPv4>");
- }
- break;
- case IF_AT_IPv6:
- if (inet_ntop(AF_INET6, &if_addr->addr.ip6_addr,
- addr_str, ADDRSTRLEN)) {
- printf("%s", addr_str);
- } else {
- printf("<unknown IPv6>");
- }
- break;
- default:
- printf("<type unknown %u>", if_addr->ifat_type);
- }
- }
-
- if (if_info->loopback)
- printf("\tloopback");
- else
- printf("\tnetwork");
-
- }
- printf("\n");
+ /* Print the description if it exists */
+ if (if_info->description != NULL)
+ printf(" (%s)", if_info->description);
+ printf("\n");
}
- free_interface_list(if_list);
-
- return 0;
}
diff --git a/capture_opts.h b/capture_opts.h
index 3b1b869398..fdf7672220 100644
--- a/capture_opts.h
+++ b/capture_opts.h
@@ -179,9 +179,9 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
extern void
capture_opts_print_link_layer_types(GList *lt_list);
-/* list interfaces */
-extern int
-capture_opts_list_interfaces(gboolean machine_readable);
+/* print list of interfaces */
+extern void
+capture_opts_print_interfaces(GList *if_list);
/* trim the snaplen entry */
extern void
diff --git a/dumpcap.c b/dumpcap.c
index e6de0d1fac..545ee17325 100644
--- a/dumpcap.c
+++ b/dumpcap.c
@@ -48,6 +48,10 @@
#include <unistd.h>
#endif
+#ifdef HAVE_ARPA_INET_H
+#include <arpa/inet.h>
+#endif
+
#if defined(__APPLE__) && defined(__LP64__)
#include <sys/utsname.h>
#endif
@@ -577,6 +581,71 @@ get_pcap_linktype_list(const char *devname, char **err_str)
return linktype_list;
}
+#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
+static void
+print_machine_readable_interfaces(GList *if_list)
+{
+ int i;
+ GList *if_entry;
+ if_info_t *if_info;
+ GSList *addr;
+ if_addr_t *if_addr;
+ char addr_str[ADDRSTRLEN];
+
+ i = 1; /* Interface id number */
+ for (if_entry = g_list_first(if_list); if_entry != NULL;
+ if_entry = g_list_next(if_entry)) {
+ if_info = (if_info_t *)if_entry->data;
+ printf("%d. %s", i++, if_info->name);
+
+ /*
+ * Print the contents of the if_entry struct in a parseable format.
+ * Each if_entry element is tab-separated. Addresses are comma-
+ * separated.
+ */
+ /* XXX - Make sure our description doesn't contain a tab */
+ if (if_info->description != NULL)
+ printf("\t%s\t", if_info->description);
+ else
+ printf("\t\t");
+
+ for(addr = g_slist_nth(if_info->addrs, 0); addr != NULL;
+ addr = g_slist_next(addr)) {
+ if (addr != g_slist_nth(if_info->addrs, 0))
+ printf(",");
+
+ if_addr = (if_addr_t *)addr->data;
+ switch(if_addr->ifat_type) {
+ case IF_AT_IPv4:
+ if (inet_ntop(AF_INET, &if_addr->addr.ip4_addr, addr_str,
+ ADDRSTRLEN)) {
+ printf("%s", addr_str);
+ } else {
+ printf("<unknown IPv4>");
+ }
+ break;
+ case IF_AT_IPv6:
+ if (inet_ntop(AF_INET6, &if_addr->addr.ip6_addr,
+ addr_str, ADDRSTRLEN)) {
+ printf("%s", addr_str);
+ } else {
+ printf("<unknown IPv6>");
+ }
+ break;
+ default:
+ printf("<type unknown %u>", if_addr->ifat_type);
+ }
+ }
+
+ if (if_info->loopback)
+ printf("\tloopback");
+ else
+ printf("\tnetwork");
+
+ printf("\n");
+ }
+}
+
/*
* If you change the machine-readable output format of this function,
* you MUST update capture_sync.c:sync_linktype_list_open() accordingly!
@@ -3270,8 +3339,32 @@ main(int argc, char *argv[])
g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", global_capture_opts.iface);
if (list_interfaces) {
- status = capture_opts_list_interfaces(machine_readable);
- exit_main(status);
+ /* Get the list of interfaces */
+ GList *if_list;
+ int err;
+ gchar *err_str;
+
+ if_list = capture_interface_list(&err, &err_str);
+ if (if_list == NULL) {
+ switch (err) {
+ case CANT_GET_INTERFACE_LIST:
+ cmdarg_err("%s", err_str);
+ g_free(err_str);
+ break;
+
+ case NO_INTERFACES_FOUND:
+ cmdarg_err("There are no interfaces on which a capture can be done");
+ break;
+ }
+ exit_main(2);
+ }
+
+ if (machine_readable) /* tab-separated values to stdout */
+ print_machine_readable_interfaces(if_list);
+ else
+ capture_opts_print_interfaces(if_list);
+ free_interface_list(if_list);
+ exit_main(0);
} else if (list_link_layer_types) {
/* Get the list of link-layer types for the capture device. */
GList *lt_list;
diff --git a/gtk/main.c b/gtk/main.c
index e230d3b5c6..2d075520da 100644
--- a/gtk/main.c
+++ b/gtk/main.c
@@ -2015,6 +2015,8 @@ main(int argc, char *argv[])
#ifdef HAVE_LIBPCAP
gboolean start_capture = FALSE;
gboolean list_link_layer_types = FALSE;
+ GList *if_list;
+ gchar *err_str;
#else
gboolean capture_option_specified = FALSE;
#endif
@@ -2451,7 +2453,22 @@ main(int argc, char *argv[])
break;
case 'D': /* Print a list of capture devices and exit */
#ifdef HAVE_LIBPCAP
- capture_opts_list_interfaces(FALSE);
+ if_list = capture_interface_list(&err, &err_str);
+ if (if_list == NULL) {
+ switch (err) {
+ case CANT_GET_INTERFACE_LIST:
+ cmdarg_err("%s", err_str);
+ g_free(err_str);
+ break;
+
+ case NO_INTERFACES_FOUND:
+ cmdarg_err("There are no interfaces on which a capture can be done");
+ break;
+ }
+ exit(2);
+ }
+ capture_opts_print_interfaces(if_list);
+ free_interface_list(if_list);
exit(0);
#else
capture_option_specified = TRUE;
diff --git a/tshark.c b/tshark.c
index 3ed559c92d..f4802bcb64 100644
--- a/tshark.c
+++ b/tshark.c
@@ -757,6 +757,8 @@ main(int argc, char *argv[])
gboolean list_link_layer_types = FALSE;
gboolean start_capture = FALSE;
int status;
+ GList *if_list;
+ gchar *err_str;
#else
gboolean capture_option_specified = FALSE;
#endif
@@ -1046,8 +1048,23 @@ main(int argc, char *argv[])
#endif
case 'D': /* Print a list of capture devices and exit */
#ifdef HAVE_LIBPCAP
- status = capture_opts_list_interfaces(FALSE);
- exit(status);
+ if_list = capture_interface_list(&err, &err_str);
+ if (if_list == NULL) {
+ switch (err) {
+ case CANT_GET_INTERFACE_LIST:
+ cmdarg_err("%s", err_str);
+ g_free(err_str);
+ break;
+
+ case NO_INTERFACES_FOUND:
+ cmdarg_err("There are no interfaces on which a capture can be done");
+ break;
+ }
+ exit(2);
+ }
+ capture_opts_print_interfaces(if_list);
+ free_interface_list(if_list);
+ exit(0);
#else
capture_option_specified = TRUE;
arg_error = TRUE;