Skip to content

Commit

Permalink
Check for loopback adapters on Windows.
Browse files Browse the repository at this point in the history
  • Loading branch information
guyharris committed Jun 28, 2016
1 parent 01dabcd commit 84ee6a9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ check_function_exists( strlcpy HAVE_STRLCPY )
check_function_exists( snprintf HAVE_SNPRINTF )
check_function_exists( vsnprintf HAVE_VSNPRINTF )

if (WIN32)
#
# Check for Windows-only functions, such as packet.dll functions.
#
check_function_exits( PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER )
endif()

#
# Data types.
#
Expand Down
3 changes: 3 additions & 0 deletions cmakeconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@
/* Define to 1 if you have the `vsnprintf' function. */
#cmakedefine HAVE_VSNPRINTF 1

/* Define to 1 if you have the `PacketIsLoopbackAdapter' function. */
#cmakedefine HAVE_PACKET_IS_LOOPBACK_ADAPTER 1

/* define if the system supports zerocopy BPF */
#cmakedefine HAVE_ZEROCOPY_BPF 1

Expand Down
31 changes: 27 additions & 4 deletions pcap-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,8 @@ pcap_setnonblock_win32(pcap_t *p, int nonblock, char *errbuf)
}

static int
pcap_add_if_win32(pcap_if_t **devlist, char *name, const char *desc,
char *errbuf)
pcap_add_if_win32(pcap_if_t **devlist, char *name, bpf_u_int32 flags,
const char *description, char *errbuf)
{
pcap_if_t *curdev;
npf_if_addr if_addrs[MAX_NETWORK_ADDRESSES];
Expand All @@ -1246,7 +1246,8 @@ pcap_add_if_win32(pcap_if_t **devlist, char *name, const char *desc,
/*
* Add an entry for this interface, with no addresses.
*/
if (add_or_find_if(&curdev, devlist, name, 0, desc, errbuf) == -1) {
if (add_or_find_if(&curdev, devlist, name, flags, description,
errbuf) == -1) {
/*
* Failure.
*/
Expand Down Expand Up @@ -1392,10 +1393,32 @@ pcap_platform_finddevs(pcap_if_t **alldevsp, char *errbuf)
*/
name = &AdaptersName[0];
while (*name != '\0') {
#ifdef HAVE_PACKET_IS_LOOPBACK_ADAPTER
bpf_u_int32 flags = 0;
pcap_t p;

/*
* Is this a loopback interface?
* XXX - it'd be best if there were a way to get a list
* of interfaces *and* flags, including "Up" and "Running"
* as well as "loopback", without having to open the
* interfaces.
*/
p = pcap_open_live(name, 68, 0, 0, errbuf);
if (p != NULL) {
if (PacketIsLoopbackAdapter(p->adapter)) {
/* Yes */
flags |= PCAP_IF_LOOPBACK;
}
pcap_close(p);
}
#endif

/*
* Add an entry for this interface.
*/
if (pcap_add_if_win32(&devlist, name, desc, errbuf) == -1) {
if (pcap_add_if_win32(&devlist, name, flags, desc,
errbuf) == -1) {
/*
* Failure.
*/
Expand Down

0 comments on commit 84ee6a9

Please sign in to comment.