Skip to content

Commit

Permalink
Require at least libpcap 0.8/WinPcap 3.1.
Browse files Browse the repository at this point in the history
2004 called, they want their libpcap/WinPcap back.

RHEL 6 initially shipped with libpcap 1.0; even old Enterprise(TM)
versions of OSes ship with something shinier than 0.7.x these days.

This lets us get rid of a bunch of #ifdefs and workaround code for
missing APIs.

Change-Id: I862cb027418b0a0c0f45a26979acea82f93f833b
Reviewed-on: https://code.wireshark.org/review/36383
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
  • Loading branch information
guyharris committed Mar 13, 2020
1 parent f77c677 commit 0c889d6
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 955 deletions.
2 changes: 0 additions & 2 deletions caputils/capture-pcap-util-int.h
Expand Up @@ -14,13 +14,11 @@
extern if_info_t *if_info_new(const char *name, const char *description,
gboolean loopback);
extern void if_info_add_address(if_info_t *if_info, struct sockaddr *addr);
#ifdef HAVE_PCAP_FINDALLDEVS
#ifdef HAVE_PCAP_REMOTE
extern GList *get_interface_list_findalldevs_ex(const char *source,
struct pcap_rmtauth *auth, int *err, char **err_str);
#endif /* HAVE_PCAP_REMOTE */
extern GList *get_interface_list_findalldevs(int *err, char **err_str);
#endif /* HAVE_PCAP_FINDALLDEVS */

#ifdef HAVE_PCAP_SET_TSTAMP_PRECISION
/*
Expand Down
256 changes: 0 additions & 256 deletions caputils/capture-pcap-util-unix.c
Expand Up @@ -22,38 +22,6 @@
#include <dlfcn.h>
#endif

#ifndef HAVE_PCAP_FINDALLDEVS

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif

/*
* Keep Digital UNIX happy when including <net/if.h>.
*/
struct mbuf;
struct rtentry;
#include <net/if.h>

#ifdef HAVE_SYS_SOCKIO_H
# include <sys/sockio.h>
#endif

#endif /* HAVE_PCAP_FINDALLDEVS */

#ifdef HAVE_LIBCAP
# include <sys/capability.h>
#endif
Expand Down Expand Up @@ -93,231 +61,11 @@ get_remote_interface_list(const char *hostname, const char *port,
}
#endif

#ifdef HAVE_PCAP_FINDALLDEVS
GList *
get_interface_list(int *err, char **err_str)
{
return get_interface_list_findalldevs(err, err_str);
}
#else /* HAVE_PCAP_FINDALLDEVS */
struct search_user_data {
char *name;
if_info_t *if_info;
};

static void
search_for_if_cb(gpointer data, gpointer user_data)
{
struct search_user_data *search_user_data = (struct search_user_data*)user_data;
if_info_t *if_info = (if_info_t *)data;

if (strcmp(if_info->name, search_user_data->name) == 0)
search_user_data->if_info = if_info;
}

GList *
get_interface_list(cap_device_open_err *err, char **err_str)
{
GList *il = NULL;
gint nonloopback_pos = 0;
struct ifreq *ifr, *last;
struct ifconf ifc;
struct ifreq ifrflags;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
struct search_user_data user_data;
pcap_t *pch;
int len, lastlen;
char *buf;
if_info_t *if_info;
char errbuf[PCAP_ERRBUF_SIZE];
gboolean loopback;

if (sock < 0) {
*err = CANT_GET_INTERFACE_LIST;
if (err_str != NULL) {
*err_str = g_strdup_printf(
"Can't get list of interfaces: error opening socket: %s",
g_strerror(errno));
}
return NULL;
}

/*
* This code came from: W. Richard Stevens: "UNIX Network Programming",
* Networking APIs: Sockets and XTI, Vol 1, page 434.
*/
lastlen = 0;
len = 100 * sizeof(struct ifreq);
for ( ; ; ) {
buf = (char *)g_malloc0(len);
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) < 0) {
if (errno != EINVAL || lastlen != 0) {
if (err_str != NULL) {
*err_str = g_strdup_printf(
"Can't get list of interfaces: SIOCGIFCONF ioctl error: %s",
g_strerror(errno));
}
goto fail;
}
} else {
if ((unsigned int) ifc.ifc_len < sizeof(struct ifreq)) {
if (err_str != NULL) {
*err_str = g_strdup(
"Can't get list of interfaces: SIOCGIFCONF ioctl gave too small return buffer");
}
goto fail;
}
if (ifc.ifc_len == lastlen)
break; /* success, len has not changed */
lastlen = ifc.ifc_len;
}
len += 10 * sizeof(struct ifreq); /* increment */
g_free(buf);
}
ifr = (struct ifreq *) ifc.ifc_req;
last = (struct ifreq *) ((char *) ifr + ifc.ifc_len);
while (ifr < last) {
/*
* Skip entries that begin with "dummy", or that include
* a ":" (the latter are Solaris virtuals).
*/
if (strncmp(ifr->ifr_name, "dummy", 5) == 0 ||
strchr(ifr->ifr_name, ':') != NULL)
goto next;

/*
* If we already have this interface name on the list,
* don't add it, but, if we don't already have an IP
* address for it, add that address (SIOCGIFCONF returns,
* at least on BSD-flavored systems, one entry per
* interface *address*; if an interface has multiple
* addresses, we get multiple entries for it).
*/
user_data.name = ifr->ifr_name;
user_data.if_info = NULL;
g_list_foreach(il, search_for_if_cb, &user_data);
if (user_data.if_info != NULL) {
if_info_add_address(user_data.if_info, &ifr->ifr_addr);
if (user_data.if_info->addrs) {
g_slist_reverse(user_data.if_info->addrs);
}
goto next;
}

/*
* Get the interface flags.
*/
memset(&ifrflags, 0, sizeof ifrflags);
g_strlcpy(ifrflags.ifr_name, ifr->ifr_name,
sizeof ifrflags.ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, (char *)&ifrflags) < 0) {
if (errno == ENXIO)
goto next;
if (err_str != NULL) {
*err_str = g_strdup_printf(
"Can't get list of interfaces: SIOCGIFFLAGS error getting flags for interface %s: %s",
ifr->ifr_name, g_strerror(errno));
}
goto fail;
}

/*
* Skip interfaces that aren't up.
*/
if (!(ifrflags.ifr_flags & IFF_UP))
goto next;

/*
* Skip interfaces that we can't open with "libpcap".
* Open with the minimum packet size - it appears that the
* IRIX SIOCSNOOPLEN "ioctl" may fail if the capture length
* supplied is too large, rather than just truncating it.
*/
pch = pcap_open_live(ifr->ifr_name, MIN_PACKET_SIZE, 0, 0,
errbuf);
if (pch == NULL)
goto next;
pcap_close(pch);

/*
* If it's a loopback interface, add it at the end of the
* list, otherwise add it after the last non-loopback
* interface, so all loopback interfaces go at the end - we
* don't want a loopback interface to be the default capture
* device unless there are no non-loopback devices.
*/
loopback = ((ifrflags.ifr_flags & IFF_LOOPBACK) ||
strncmp(ifr->ifr_name, "lo", 2) == 0);
if_info = if_info_new(ifr->ifr_name, NULL, loopback);
if_info_add_address(if_info, &ifr->ifr_addr);
if (if_info->addrs) {
g_slist_reverse(if_info->addrs);
}
if (loopback)
il = g_list_append(il, if_info);
else {
il = g_list_insert(il, if_info, nonloopback_pos);
/*
* Insert the next non-loopback interface after this
* one.
*/
nonloopback_pos++;
}

next:
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
ifr = (struct ifreq *) ((char *) ifr +
(ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr) ?
ifr->ifr_addr.sa_len : sizeof(ifr->ifr_addr)) +
IFNAMSIZ);
#else
ifr = (struct ifreq *) ((char *) ifr + sizeof(struct ifreq));
#endif
}

#ifdef linux
/*
* OK, maybe we have support for the "any" device, to do a cooked
* capture on all interfaces at once.
* Try opening it and, if that succeeds, add it to the end of
* the list of interfaces.
*/
pch = pcap_open_live("any", MIN_PACKET_SIZE, 0, 0, errbuf);
if (pch != NULL) {
/*
* It worked; we can use the "any" device.
*/
if_info = if_info_new("any",
"Pseudo-device that captures on all interfaces", FALSE);
il = g_list_insert(il, if_info, -1);
pcap_close(pch);
}
#endif

g_free(ifc.ifc_buf);
close(sock);

if (il == NULL) {
/*
* No interfaces found.
*/
*err = 0;
if (err_str != NULL)
*err_str = NULL;
}
return il;

fail:
if (il != NULL)
free_interface_list(il);
g_free(ifc.ifc_buf);
close(sock);
*err = CANT_GET_INTERFACE_LIST;
return NULL;
}
#endif /* HAVE_PCAP_FINDALLDEVS */

/*
* Get an error message string for a CANT_GET_INTERFACE_LIST error from
Expand Down Expand Up @@ -526,11 +274,7 @@ void
get_runtime_caplibs_version(GString *str)
{
g_string_append_printf(str, "with ");
#ifdef HAVE_PCAP_LIB_VERSION
g_string_append(str, pcap_lib_version());
#else
g_string_append(str, "libpcap (version unknown)");
#endif
}

#else /* HAVE_LIBPCAP */
Expand Down

0 comments on commit 0c889d6

Please sign in to comment.