]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Make tcpdump find wpcap.dll on Windows if it's not in the system library.
authorGuy Harris <[email protected]>
Tue, 8 Oct 2024 19:40:21 +0000 (12:40 -0700)
committerGuy Harris <[email protected]>
Wed, 9 Oct 2024 04:39:41 +0000 (21:39 -0700)
See https://npcap.com/guide/npcap-devguide.html#npcap-feature-native-dll-implicitly
for details on what's being done.

Fix #1226.

CMakeLists.txt
cmake/Modules/FindPCAP.cmake
tcpdump.c

index 0b6c07c3bec0e1095aa8665111d0c5000ea8cc7a..a501afdc048a391fa3a08ae210c25e903f275f7a 100644 (file)
@@ -1418,6 +1418,9 @@ add_executable(tcpdump ${TCPDUMP_SOURCE_LIST_C})
 if(NOT C_ADDITIONAL_FLAGS STREQUAL "")
     set_target_properties(tcpdump PROPERTIES COMPILE_FLAGS ${C_ADDITIONAL_FLAGS})
 endif()
+if(NOT "${PCAP_LINK_FLAGS}" STREQUAL "")
+    set_target_properties(tcpdump PROPERTIES LINK_FLAGS ${PCAP_LINK_FLAGS})
+endif()
 target_link_libraries(tcpdump netdissect ${TCPDUMP_LINK_LIBRARIES})
 
 ######################################
index 11074655f45763c7e4c1b468fd157e487a21e170..596f2909ee03a1f4b182592abb083026d43814dc 100644 (file)
@@ -61,6 +61,42 @@ if(WIN32)
   if(PCAP_FOUND)
     set(PCAP_LIBRARIES ${PCAP_LIBRARY})
     set(PCAP_INCLUDE_DIRS ${PCAP_INCLUDE_DIR})
+
+    #
+    # We need to look for wpcap.dll in \Windows\System32\Npcap first,
+    # as either:
+    #
+    #  1) WinPcap isn't installed and Npcap isn't installed in "WinPcap
+    #     API-compatible Mode", so there's no wpcap.dll in
+    #     \Windows\System32, only in \Windows\System32\Npcap;
+    #
+    #  2) WinPcap is installed and Npcap isn't installed in "WinPcap
+    #     API-compatible Mode", so the wpcap.dll in \Windows\System32
+    #     is a WinPcap DLL, but we'd prefer an Npcap DLL (we should
+    #     work with either one if we're configured against WinPcap,
+    #     and we'll probably require Npcap if we're configured againt
+    #     it), and that's in \Windows\System32\Npcap;
+    #
+    #  3) Npcap is installed in "WinPcap API-compatible Mode", so both
+    #     \Windows\System32 and \Windows\System32\Npcap have an Npcap
+    #     wpcap.dll.
+    #
+    # Unfortunately, Windows has no notion of an rpath, so we can't
+    # set the rpath to include \Windows\System32\Npcap at link time;
+    # what we need to do is to link wpcap as a delay-load DLL and
+    # add \Windows\System32\Npcap to the DLL search path early in
+    # main() with a call to SetDllDirectory().
+    #
+    # We add /delayload:wpcap.dll to the linker options here.
+    #
+    # See https://npcap.com/guide/npcap-devguide.html#npcap-feature-native-dll-implicitly
+    #
+    set(PCAP_LINK_FLAGS /delayload:wpcap.dll)
+
+    #
+    # Delay-loading libraries means we need to link with delayimp.lib.
+    #
+    set(PCAP_LIBRARIES ${PCAP_LIBRARIES} delayimp.lib)
   endif()
 else(WIN32)
   #
index ca4babf221575e871f1b65ccbe8b011867f5e609..7484ae4c4bc3c3e8b2cff20ef76cc160f14ee1fb 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -1437,6 +1437,42 @@ main(int argc, char **argv)
        netdissect_options Ndo;
        netdissect_options *ndo = &Ndo;
 
+#ifdef _WIN32
+       /*
+        * We need to look for wpcap.dll in \Windows\System32\Npcap first,
+        * as either:
+        *
+        *  1) WinPcap isn't installed and Npcap isn't installed in "WinPcap
+        *     API-compatible Mode", so there's no wpcap.dll in
+        *     \Windows\System32, only in \Windows\System32\Npcap;
+        *
+        *  2) WinPcap is installed and Npcap isn't installed in "WinPcap
+        *     API-compatible Mode", so the wpcap.dll in \Windows\System32
+        *     is a WinPcap DLL, but we'd prefer an Npcap DLL (we should
+        *     work with either one if we're configured against WinPcap,
+        *     and we'll probably require Npcap if we're configured againt
+        *     it), and that's in \Windows\System32\Npcap;
+        *
+        *  3) Npcap is installed in "WinPcap API-compatible Mode", so both
+        *     \Windows\System32 and \Windows\System32\Npcap have an Npcap
+        *     wpcap.dll.
+        *
+        * Unfortunately, Windows has no notion of an rpath, so we can't
+        * set the rpath to include \Windows\System32\Npcap at link time;
+        * what we need to do is to link wpcap as a delay-load DLL and
+        * add \Windows\System32\Npcap to the DLL search path early in
+        * main() with a call to SetDllDirectory().
+        *
+        * The same applies to packet.dll.
+        *
+        * We add \Windows\System32\Npcap here.
+        *
+        * See https://npcap.com/guide/npcap-devguide.html#npcap-feature-native-dll-implicitly
+        */
+       if (!SetDllDirectoryA("C:\\Windows\\System32\\Npcap"))
+               error("SetDllDirectory failed");
+#endif
+
        /*
         * Initialize the netdissect code.
         */