diff --git a/CMakeLists.txt b/CMakeLists.txt index 0365ff5a15..9f722b8234 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2380,6 +2380,7 @@ endif() if(BUILD_sshdump AND LIBSSH_FOUND) set(sshdump_LIBS + wsutil ${GLIB2_LIBRARIES} ${CMAKE_DL_LIBS} ${LIBSSH_LIBRARIES} diff --git a/ConfigureChecks.cmake b/ConfigureChecks.cmake index f7b2a7bb4a..0cf1ae0131 100644 --- a/ConfigureChecks.cmake +++ b/ConfigureChecks.cmake @@ -29,6 +29,7 @@ check_include_file("dlfcn.h" HAVE_DLFCN_H) check_include_file("fcntl.h" HAVE_FCNTL_H) check_include_file("getopt.h" HAVE_GETOPT_H) check_include_file("grp.h" HAVE_GRP_H) +check_include_file("ifaddrs.h" HAVE_IFADDRS_H) check_include_file("inttypes.h" HAVE_INTTYPES_H) check_include_file("netinet/in.h" HAVE_NETINET_IN_H) check_include_file("netdb.h" HAVE_NETDB_H) @@ -115,6 +116,7 @@ if(HAVE_GETOPT_LONG) endif() endif() check_function_exists("getprotobynumber" HAVE_GETPROTOBYNUMBER) +check_function_exists("getifaddrs" HAVE_GETIFADDRS) check_function_exists("inet_aton" HAVE_INET_ATON) check_function_exists("inet_ntop" HAVE_INET_NTOP_PROTO) check_function_exists("inet_pton" HAVE_INET_PTON) diff --git a/cmakeconfig.h.in b/cmakeconfig.h.in index aa4a2f9492..35cc3d06cd 100644 --- a/cmakeconfig.h.in +++ b/cmakeconfig.h.in @@ -64,6 +64,12 @@ /* Define if GeoIP supports IPv6 (GeoIP 1.4.5 and later) */ #cmakedefine HAVE_GEOIP_V6 1 +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_IFADDRS_H 1 + +/* Define to 1 if you have the `getifaddrs' function. */ +#cmakedefine HAVE_GETIFADDRS 1 + /* Define if LIBSSH support is enabled */ #cmakedefine HAVE_LIBSSH 1 diff --git a/configure.ac b/configure.ac index c917542472..c85651913c 100644 --- a/configure.ac +++ b/configure.ac @@ -2647,6 +2647,7 @@ AC_CHECK_HEADERS(fcntl.h getopt.h grp.h inttypes.h netdb.h pwd.h unistd.h) AC_CHECK_HEADERS(sys/ioctl.h sys/param.h sys/socket.h sys/sockio.h sys/stat.h sys/time.h sys/types.h sys/utsname.h sys/wait.h) AC_CHECK_HEADERS(netinet/in.h) AC_CHECK_HEADERS(arpa/inet.h arpa/nameser.h) +AC_CHECK_HEADERS(ifaddrs.h) # # On Linux, check for some additional headers, which we need as a @@ -2998,6 +2999,7 @@ AC_SUBST(POPCOUNT_LO) AC_CHECK_FUNCS(getprotobynumber) AC_CHECK_FUNCS(issetugid) AC_CHECK_FUNCS(sysconf) +AC_CHECK_FUNCS(getifaddrs) dnl blank for now, but will be used in future AC_SUBST(wireshark_SUBDIRS) diff --git a/extcap/extcap-base.h b/extcap/extcap-base.h index 3e2f847b84..2c6763a025 100644 --- a/extcap/extcap-base.h +++ b/extcap/extcap-base.h @@ -24,6 +24,8 @@ #ifndef __EXTCAP_BASE_H__ #define __EXTCAP_BASE_H__ +#include "config.h" + #include #include #include @@ -65,6 +67,10 @@ #define SOCKET_ERROR (-1) #endif +#ifdef HAVE_ARPA_INET_H + #include +#endif + #define EXTCAP_BASE_OPTIONS_ENUM \ EXTCAP_OPT_LIST_INTERFACES, \ EXTCAP_OPT_VERSION, \ diff --git a/extcap/sshdump.c b/extcap/sshdump.c index 8ed9cf6887..f0055fc40e 100644 --- a/extcap/sshdump.c +++ b/extcap/sshdump.c @@ -24,13 +24,13 @@ #include "config.h" -#include "extcap-base.h" +#include +#include #include #include #include #include -#include #include #include #include @@ -39,17 +39,6 @@ #include #include -#ifdef HAVE_ARPA_INET_H - #include -#endif - -#include "log.h" - -#if defined(__FreeBSD__) || defined(BSD) || defined(__APPLE__) || defined(__linux__) -#define USE_GETIFADDRS 1 -#include -#endif - #ifndef STDERR_FILENO #define STDERR_FILENO 2 #endif @@ -106,7 +95,7 @@ static struct option longopts[] = { { 0, 0, 0, 0} }; -static char* local_interfaces_to_filter(unsigned int remote_port); +static char* interfaces_list_to_filter(GSList* if_list, const unsigned int remote_port); static void ssh_cleanup(ssh_session sshs, ssh_channel channel) { @@ -249,6 +238,14 @@ static void ssh_loop_read(ssh_channel channel, int fd) return; } +static char* local_interfaces_to_filter(const unsigned int remote_port) +{ + GSList* interfaces = local_interfaces_to_list(); + char* filter = interfaces_list_to_filter(interfaces, remote_port); + g_slist_free_full(interfaces, g_free); + return filter; +} + static ssh_channel run_ssh_command(ssh_session sshs, const char* capture_bin, const char* iface, const char* cfilter, unsigned long int count) { @@ -384,70 +381,23 @@ static void help(const char* binname) printf(" --remote-filter : a filter for remote capture (default: don't listen on local local interfaces IPs)\n"); } -static char* local_interfaces_to_filter(unsigned int remote_port) +static char* interfaces_list_to_filter(GSList* interfaces, unsigned int remote_port) { - char* filter = NULL; -#ifdef USE_GETIFADDRS - struct ifaddrs* ifap; - struct ifaddrs* ifa; - GString* interfaces; - int family; - char ip[INET6_ADDRSTRLEN]; + GString* filter = g_string_new(NULL); + GSList* cur; - if (getifaddrs(&ifap)) { - goto end; - } - - interfaces = g_string_new(NULL); - - for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { - if (ifa->ifa_addr == NULL) - continue; - - family = ifa->ifa_addr->sa_family; - - memset(&ip, 0x0, INET6_ADDRSTRLEN); - - switch (family) { - case AF_INET: - { - struct sockaddr_in *addr4 = (struct sockaddr_in *)ifa->ifa_addr; - inet_ntop(family, (char *)&addr4->sin_addr, ip, sizeof(ip)); - break; - } - - case AF_INET6: - { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)ifa->ifa_addr; - inet_ntop(family, (char *)&addr6->sin6_addr, ip, sizeof(ip)); - break; - } - - default: - break; - } - - /* skip loopback addresses */ - if (!g_strcmp0(ip, "127.0.0.1") || !g_strcmp0(ip, "::1")) - continue; - - if (*ip) { - if (interfaces->len) - g_string_append(interfaces, " or "); - g_string_append_printf(interfaces, "host %s", ip); + if (!interfaces) { + g_string_append_printf(filter, "not port %u", remote_port); + } else { + g_string_append_printf(filter, "not ((host %s", (char*)interfaces->data); + cur = g_slist_next(interfaces); + while (cur->next != NULL) { + g_string_append_printf(filter, " or host %s", (char*)cur->data); + cur = cur->next; } + g_string_append_printf(filter, ") and port %u)", remote_port); } - freeifaddrs(ifap); - - if (interfaces->len) - filter = g_strdup_printf("not ((%s) and port %u)", interfaces->str, remote_port); - - g_string_free(interfaces, TRUE); -end: -#endif - if (!filter) - filter = g_strdup_printf("not port %u", remote_port); - return filter; + return g_string_free(filter, FALSE); } static int list_config(char *interface, unsigned int remote_port) diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt index 7d63757132..4a4d047c35 100644 --- a/wsutil/CMakeLists.txt +++ b/wsutil/CMakeLists.txt @@ -54,6 +54,7 @@ set(WSUTIL_FILES frequency-utils.c g711.c inet_addr.c + interface.c jsmn.c md4.c md5.c diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common index 6e93833de2..38256f5228 100644 --- a/wsutil/Makefile.common +++ b/wsutil/Makefile.common @@ -51,6 +51,7 @@ LIBWSUTIL_COMMON_SRC = \ frequency-utils.c \ g711.c \ inet_addr.c \ + interface.c \ jsmn.c \ md4.c \ md5.c \ @@ -102,6 +103,7 @@ libwsutil_nonrepl_INCLUDES = \ frequency-utils.h \ g711.h \ inet_addr.h \ + interface.h \ jsmn.h \ md4.h \ md5.h \ diff --git a/wsutil/interface.c b/wsutil/interface.c new file mode 100644 index 0000000000..07fdb318b5 --- /dev/null +++ b/wsutil/interface.c @@ -0,0 +1,106 @@ +/* interface.c + * Utility functions to get infos from interfaces + * + * Copyright 2016, Dario Lombardo + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "config.h" + +#include "interface.h" + +#include +#include + +#ifdef HAVE_ARPA_INET_H + #include +#endif + +#ifdef HAVE_IFADDRS_H + #include +#endif + +GSList *local_interfaces_to_list(void) +{ + GSList *interfaces = NULL; +#ifdef HAVE_GETIFADDRS + struct ifaddrs *ifap; + struct ifaddrs *ifa; + int family; + char ip[INET6_ADDRSTRLEN]; + + if (getifaddrs(&ifap)) { + goto end; + } + + interfaces = g_slist_alloc(); + + for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr == NULL) + continue; + + family = ifa->ifa_addr->sa_family; + + memset(ip, 0x0, INET6_ADDRSTRLEN); + + switch (family) { + case AF_INET: + { + struct sockaddr_in *addr4 = (struct sockaddr_in *)ifa->ifa_addr; + ws_inet_ntop4(&addr4->sin_addr, ip, sizeof(ip)); + break; + } + + case AF_INET6: + { + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)ifa->ifa_addr; + ws_inet_ntop6(&addr6->sin6_addr, ip, sizeof(ip)); + break; + } + + default: + break; + } + + /* skip loopback addresses */ + if (!g_strcmp0(ip, "127.0.0.1") || !g_strcmp0(ip, "::1")) + continue; + + if (*ip) { + interfaces = g_slist_prepend(interfaces, g_strdup(ip)); + } + } +end: +#endif /* HAVE_GETIFADDRS */ + return interfaces; +} + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: t + * End: + * + * vi: set shiftwidth=4 tabstop=4 noexpandtab: + * :indentSize=4:tabSize=4:noTabs=false: + */ diff --git a/wsutil/interface.h b/wsutil/interface.h new file mode 100644 index 0000000000..80205f4ea0 --- /dev/null +++ b/wsutil/interface.h @@ -0,0 +1,48 @@ +/* interface.c + * Utility functions to get infos from interfaces + * + * Copyright 2016, Dario Lombardo + * + * Wireshark - Network traffic analyzer + * By Gerald Combs + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _INTERFACE_H +#define _INTERFACE_H + +#include +#include "ws_symbol_export.h" + +/* Return a list of IPv4/IPv6 addresses for local interfaces */ +WS_DLL_PUBLIC +GSList* local_interfaces_to_list(void); + +#endif + +/* + * Editor modelines - https://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 4 + * indent-tabs-mode: t + * End: + * + * vi: set shiftwidth=4 tabstop=4 noexpandtab: + * :indentSize=4:tabSize=4:noTabs=false: + */