]> The Tcpdump Group git mirrors - libpcap/commitdiff
Make it possible to build on Windows without packet.dll.
authorGuy Harris <[email protected]>
Fri, 8 Sep 2017 00:46:28 +0000 (17:46 -0700)
committerGuy Harris <[email protected]>
Fri, 8 Sep 2017 00:46:28 +0000 (17:46 -0700)
Rename pcap-win32.c to pcap-npf.c, in case other capture mechanisms are
available on Windows in the future.

If we don't find packet.dll, build with pcap-null.c, to make a libpcap
that can read and write capture files but can't capture.

Use the generic pcap_lookupdev() if we don't have packet.dll and aren't
building on MS-DOS.

CMakeLists.txt
INSTALL.txt
Makefile.in
cmakeconfig.h.in
pcap-npf.c [moved from pcap-win32.c with 100% similarity]
pcap.c

index 32dc198cb73a2236ddc4c01563423f34ae229e1f..d57aacc9bc233258c8a5c3162f978b07f5d1d30c 100644 (file)
@@ -64,6 +64,17 @@ if( WIN32 )
         else( CMAKE_CL_64 )
             link_directories("${PACKET_DLL_DIR}/Lib")
         endif( CMAKE_CL_64 )
+
+       #
+       # We want to build with packet.dll, either from WinPcap or NPcap.
+       #
+       set(HAVE_PACKET32)
+
+       #
+       # Check whether we have the NPcap PacketIsLoopbackAdapter()
+       # function.
+       #
+       check_function_exists( PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER )
     endif()
     include_directories(
         ../Common/
@@ -143,13 +154,6 @@ check_function_exists( snprintf HAVE_SNPRINTF )
 check_function_exists( vsnprintf HAVE_VSNPRINTF )
 check_function_exists( strtok_r HAVE_STRTOK_R )
 
-if (WIN32)
-    #
-    # Check for Windows-only functions, such as packet.dll functions.
-    #
-    check_function_exists( PacketIsLoopbackAdapter HAVE_PACKET_IS_LOOPBACK_ADAPTER )
-endif()
-
 #
 # Data types.
 #
@@ -215,19 +219,42 @@ if( HAVE_REMOTE )
 endif( HAVE_REMOTE )
 
 #
-# Determine the main pcap-XXX.c file to use.
+# Determine the main pcap-XXX.c file to use, and the libraries with
+# which we need to link libpcap, if any.
 #
 if( WIN32 )
     #
-    # WinPcap.
+    # Windows; we need to link with WinSock2.
+    #
+    set(PCAP_LINK_LIBRARIES ws2_32)
+
     #
-    set( PCAP_TYPE win32 )
+    # Check whether we have packet.dll.
+    #
+    if (HAVE_PACKET32)
+       #
+       # We have packet.dll.
+       # Set the capture type to NPF, and link with packet.dll before
+       # WinSock2.
+       #
+       set( PCAP_TYPE npf )
+       set(PCAP_LINK_LIBRARIES packet ${PCAP_LINK_LIBRARIES})
+    else()
+       #
+       # We don't have any capture type we know about, so just use
+       # the null capture type, and only support reading (and writing)
+       # capture files.
+       #
+       set( PCAP_TYPE null )
+    endif()
 else()
     #
     # UN*X.
     #
-    # Figure out what type of packet capture mechanism we have.
+    # Figure out what type of packet capture mechanism we have, and
+    # what libraries we'd need to link libpcap with, if any.
     #
+    set(PCAP_LINK_LIBRARIES "")
     if( EXISTS /dev/bpf )
        #
        # Cloning BPF device.
@@ -545,12 +572,7 @@ add_library(${LIBRARY_NAME}_static STATIC
 add_dependencies(${LIBRARY_NAME}_static SerializeTarget)
 set_target_properties(${LIBRARY_NAME}_static PROPERTIES OUTPUT_NAME "${LIBRARY_NAME}")
 
-if( WIN32 )
-    target_link_libraries ( ${LIBRARY_NAME}
-        packet
-        ws2_32
-    )
-endif( WIN32 )
+target_link_libraries(${LIBRARY_NAME} ${PCAP_LINK_LIBRARIES})
 
 ######################################
 # Write out the config.h file
index 74aa01d3f574156f2daf035dd18cbae4cd47ba59..e48353d295765b7c67338f60cc9cabd30b51801b 100644 (file)
@@ -347,6 +347,7 @@ pcap-linux.c        - Linux packet socket support
 pcap-namedb.h  - header for backwards compatibility
 pcap-nit.c     - SunOS Network Interface Tap support
 pcap-nit.h     - SunOS Network Interface Tap definitions
+pcap-npf.c     - WinPcap capture support
 pcap-null.c    - dummy monitor support (allows offline use of libpcap)
 pcap-pf.c      - Ultrix and Digital/Tru64 UNIX Packet Filter support
 pcap-pf.h      - Ultrix and Digital/Tru64 UNIX Packet Filter definitions
@@ -360,7 +361,6 @@ pcap-snit.c - SunOS 4.x STREAMS-based Network Interface Tap support
 pcap-snoop.c   - IRIX Snoop network monitoring support
 pcap-usb-linux.c - USB capture support for Linux
 pcap-usb-linux.h - USB capture support for Linux
-pcap-win32.c   - WinPcap capture support
 pcap.3pcap     - manual entry for the library
 pcap.c         - pcap utility routines
 pcap.h         - header for backwards compatibility
index 66f7de35d767969de97c7403b6ad582d0fac62ed..388f16040ef86283c69ccdd0d17800e86327635e 100644 (file)
@@ -349,6 +349,7 @@ EXTRA_DIST = \
        pcap-netmap.c \
        pcap-netmap.h \
        pcap-nit.c \
+       pcap-npf.c \
        pcap-null.c \
        pcap-pf.c \
        pcap-rdmasniff.c \
@@ -368,7 +369,6 @@ EXTRA_DIST = \
        pcap-tc.h \
        pcap-usb-linux.c \
        pcap-usb-linux.h \
-       pcap-win32.c \
        sockutils.c \
        sockutils.h \
        scanner.l \
index 566afde3d518a0d9301705d700be036df3f36a0e..699d6bf6b12ac673348b6a83bfed47010175d986 100644 (file)
 /* Define to 1 if remote packet capture is to be supported */
 #cmakedefine HAVE_REMOTE 1
 
+/* Define to 1 if Packet32 API (WinPcap NPF driver) is available */
+#cmakedefine HAVE_PACKET32 1
+
 /* Define to 1 if you have the <paths.h> header file. */
 #cmakedefine HAVE_PATHS_H 1
 
similarity index 100%
rename from pcap-win32.c
rename to pcap-npf.c
diff --git a/pcap.c b/pcap.c
index c6c1b609934d20b2a5087389db2198be9fcc036f..176762adf169f8a85b98b2eb207ab7e7d697c6fb 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -1155,14 +1155,17 @@ pcap_freealldevs(pcap_if_t *alldevs)
 }
 
 /*
- * Windows has its own pcap_lookupdev(), for compatibility reasons, as
+ * pcap-npf.c has its own pcap_lookupdev(), for compatibility reasons, as
  * it actually returns the names of all interfaces, with a NUL separator
  * between them; some callers may depend on that.
  *
  * MS-DOS has its own pcap_lookupdev(), but that might be useful only
  * as an optimization.
+ *
+ * In all other cases, we just use pcap_findalldevs() to get a list of
+ * devices, and pick from that list.
  */
-#if !defined(_WIN32) && !defined(MSDOS)
+#if !defined(HAVE_PACKET32) && !defined(MSDOS)
 /*
  * Return the name of a network interface attached to the system, or NULL
  * if none can be found.  The interface must be configured up; the