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(""); - } - break; - case IF_AT_IPv6: - if (inet_ntop(AF_INET6, &if_addr->addr.ip6_addr, - addr_str, ADDRSTRLEN)) { - printf("%s", addr_str); - } else { - printf(""); - } - break; - default: - printf("", 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 #endif +#ifdef HAVE_ARPA_INET_H +#include +#endif + #if defined(__APPLE__) && defined(__LP64__) #include #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(""); + } + break; + case IF_AT_IPv6: + if (inet_ntop(AF_INET6, &if_addr->addr.ip6_addr, + addr_str, ADDRSTRLEN)) { + printf("%s", addr_str); + } else { + printf(""); + } + break; + default: + printf("", 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;