extcap: add local_interfaces_to_list() to wsutil.

This new function abstracts the creation of a list of the local
interfaces that will be used by future extcaps to generate specific
filters. Sshdump now uses it to create a pcap filter.

Change-Id: I5b75a266f81104b3c9bcb3e51de246b7cc8785ce
Reviewed-on: https://code.wireshark.org/review/14092
Reviewed-by: João Valverde <j@v6e.pt>
Petri-Dish: João Valverde <j@v6e.pt>
Tested-by: Petri Dish Buildbot <buildbot-no-reply@wireshark.org>
Reviewed-by: Roland Knall <rknall@gmail.com>
This commit is contained in:
Dario Lombardo 2016-02-23 14:33:54 +01:00 committed by Roland Knall
parent 5a66318272
commit 68d4ba212c
10 changed files with 198 additions and 74 deletions

View File

@ -2380,6 +2380,7 @@ endif()
if(BUILD_sshdump AND LIBSSH_FOUND)
set(sshdump_LIBS
wsutil
${GLIB2_LIBRARIES}
${CMAKE_DL_LIBS}
${LIBSSH_LIBRARIES}

View File

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

View File

@ -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 <ifaddrs.h> 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

View File

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

View File

@ -24,6 +24,8 @@
#ifndef __EXTCAP_BASE_H__
#define __EXTCAP_BASE_H__
#include "config.h"
#include <glib.h>
#include <glib/gprintf.h>
#include <stdlib.h>
@ -65,6 +67,10 @@
#define SOCKET_ERROR (-1)
#endif
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#define EXTCAP_BASE_OPTIONS_ENUM \
EXTCAP_OPT_LIST_INTERFACES, \
EXTCAP_OPT_VERSION, \

View File

@ -24,13 +24,13 @@
#include "config.h"
#include "extcap-base.h"
#include <extcap/extcap-base.h>
#include <wsutil/interface.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <string.h>
@ -39,17 +39,6 @@
#include <fcntl.h>
#include <libssh/libssh.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#include "log.h"
#if defined(__FreeBSD__) || defined(BSD) || defined(__APPLE__) || defined(__linux__)
#define USE_GETIFADDRS 1
#include <ifaddrs.h>
#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 <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)

View File

@ -54,6 +54,7 @@ set(WSUTIL_FILES
frequency-utils.c
g711.c
inet_addr.c
interface.c
jsmn.c
md4.c
md5.c

View File

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

106
wsutil/interface.c Normal file
View File

@ -0,0 +1,106 @@
/* interface.c
* Utility functions to get infos from interfaces
*
* Copyright 2016, Dario Lombardo
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* 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 <string.h>
#include <wsutil/inet_addr.h>
#ifdef HAVE_ARPA_INET_H
#include <arpa/inet.h>
#endif
#ifdef HAVE_IFADDRS_H
#include <ifaddrs.h>
#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:
*/

48
wsutil/interface.h Normal file
View File

@ -0,0 +1,48 @@
/* interface.c
* Utility functions to get infos from interfaces
*
* Copyright 2016, Dario Lombardo
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* 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 <glib.h>
#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:
*/