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
This commit is contained in:
Guy Harris 2010-05-07 19:24:32 +00:00
parent 66f101f10f
commit 077ff72ac1
5 changed files with 143 additions and 88 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -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;

View File

@ -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;

View File

@ -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;