diff --git a/libdnet-stripped/CMakeLists.txt b/libdnet-stripped/CMakeLists.txt index c20bcde85..aae89074a 100644 --- a/libdnet-stripped/CMakeLists.txt +++ b/libdnet-stripped/CMakeLists.txt @@ -12,30 +12,51 @@ include(CheckSymbolExists) include(CheckTypeSize) include(CheckCSourceCompiles) include(GNUInstallDirs) +include(CMakePushCheckState) +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS True) option(BUILD_SHARED_LIBS "Build in shared lib mode" OFF) +set(DNET_LINK_LIBS "") + foreach (header stdio.h stdlib.h string.h inttypes.h) string(TOUPPER HAVE_${header} var) string(REGEX REPLACE "\\.|/" "_" var ${var}) check_include_file(${header} ${var}) endforeach () +if(WIN32) + find_package(Packet) + if(Packet_FOUND) + set(HAVE_PACKET32 TRUE) + set(HAVE_PCAP_H TRUE) + include_directories(${Packet_INCLUDE_DIRS}) + set(DNET_LINK_LIBS ${Packet_LIBRARIES} ${DNET_LINK_LIBS}) + endif(Packet_FOUND) +endif(WIN32) + if (MSVC) add_definitions(-DWIN32_LEAN_AND_MEAN) check_include_file(winsock2.h HAVE_WINSOCK2_H) - set(HAVE_LIBWS2_32 ${HAVE_WINSOCK2_H}) + if(HAVE_WINSOCK2_H) + set(HAVE_LIBWS2_32 TRUE) + set(DNET_LINK_LIBS ws2_32 ${DNET_LINK_LIBS}) + endif() check_c_source_compiles(" #define WIN32_LEAN_AND_MEAN #include #include int main() { return 0; }" HAVE_IPHLPAPI_H) - set(HAVE_LIBIPHLPAPI ${HAVE_IPHLPAPI_H}) + if(HAVE_IPHLPAPI_H) + set(HAVE_LIBIPHLPAPI TRUE) + set(DNET_LINK_LIBS Iphlpapi ${DNET_LINK_LIBS}) + endif() + cmake_push_check_state() set(CMAKE_REQUIRED_LIBRARIES "ws2_32") check_symbol_exists(inet_pton WS2tcpip.h HAVE_INET_PTON) - set(CMAKE_REQUIRED_LIBRARIES ) + cmake_pop_check_state() endif() if(UNIX) foreach (header strings.h @@ -190,9 +211,8 @@ else() list(APPEND PLATFORM_SOURCES src/arp-none.c) endif() -if (HAVE_IPHLPAPI_H) -# no npcap support -# list(APPEND PLATFORM_SOURCES src/eth-win32.c) +if (HAVE_PACKET32) + list(APPEND PLATFORM_SOURCES src/eth-win32.c) elseif(HAVE_NET_PFILT_H) list(APPEND PLATFORM_SOURCES src/eth-pfilt.c) elseif(HAVE_LINUX_PF_PACKET) @@ -301,6 +321,7 @@ set(DNET_HEADERS include/dnet/eth.h include/dnet/fw.h include/dnet/icmp.h + include/dnet/icmpv6.h include/dnet/intf.h include/dnet/ip.h include/dnet/ip6.h @@ -325,8 +346,8 @@ set_target_properties( PUBLIC_HEADER "${DNET_HEADERS}" ) -if (MSVC) - target_link_libraries(${PROJECT_NAME} PUBLIC Iphlpapi ws2_32) +if(NOT(DNET_LINK_LIBS STREQUAL "")) + target_link_libraries(${PROJECT_NAME} PUBLIC ${DNET_LINK_LIBS}) endif() install(TARGETS ${PROJECT_NAME} diff --git a/libdnet-stripped/cmake/Modules/FindPacket.cmake b/libdnet-stripped/cmake/Modules/FindPacket.cmake new file mode 100644 index 000000000..3d31cba83 --- /dev/null +++ b/libdnet-stripped/cmake/Modules/FindPacket.cmake @@ -0,0 +1,94 @@ +# +# Copyright (C) 2017 Ali Abdulkadir . +# +# Permission is hereby granted, free of charge, to any person +# obtaining a copy of this software and associated documentation files +# (the "Software"), to deal in the Software without restriction, +# including without limitation the rights to use, copy, modify, merge, +# publish, distribute, sub-license, and/or sell copies of the Software, +# and to permit persons to whom the Software is furnished to do so, +# subject to the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS +# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. +# +# FindPacket +# ========== +# +# Find the Packet library and include files. +# +# This module defines the following variables: +# +# Packet_INCLUDE_DIR - absolute path to the directory containing Packet32.h. +# +# Packet_LIBRARY - relative or absolute path to the Packet library to +# link with. An absolute path is will be used if the +# Packet library is not located in the compiler's +# default search path. + +# Packet_FOUND - TRUE if the Packet library *and* header are found. +# +# Hints and Backward Compatibility +# ================================ +# +# To tell this module where to look, a user may set the environment variable +# Packet_ROOT to point cmake to the *root* of a directory with include and +# lib subdirectories for packet.dll (e.g WpdPack or npcap-sdk). +# Alternatively, Packet_ROOT may also be set from cmake command line or GUI +# (e.g cmake -DPacket_ROOT=C:\path\to\packet [...]) +# + +if(CMAKE_GENERATOR_PLATFORM STREQUAL "Win32") + # + # 32-bit x86; no need to look in subdirectories of the SDK's + # Lib directory for the libraries, as the libraries are in + # the Lib directory + # +else() + # + # Platform other than 32-bit x86. + # + # For the WinPcap and Npcap SDKs, the Lib subdirectory of the top-level + # directory contains 32-bit x86 libraries; the libraries for other + # platforms are in subdirectories of the Lib directory whose names + # are the names of the supported platforms. + # + # The only way to *FORCE* CMake to look in the appropriate + # subdirectory of Lib for libraries without searching in the + # Lib directory first appears to be to set + # CMAKE_LIBRARY_ARCHITECTURE to the name of the subdirectory. + # + set(CMAKE_LIBRARY_ARCHITECTURE "${CMAKE_GENERATOR_PLATFORM}") +endif() + +# Find the header +find_path(Packet_INCLUDE_DIR Packet32.h + PATH_SUFFIXES include Include +) + +# Find the library +find_library(Packet_LIBRARY + NAMES Packet packet +) + +# Set Packet_FOUND to TRUE if Packet_INCLUDE_DIR and Packet_LIBRARY are TRUE. +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(Packet + DEFAULT_MSG + Packet_INCLUDE_DIR + Packet_LIBRARY +) + +mark_as_advanced(Packet_INCLUDE_DIR Packet_LIBRARY) + +set(Packet_INCLUDE_DIRS ${Packet_INCLUDE_DIR}) +set(Packet_LIBRARIES ${Packet_LIBRARY}) diff --git a/libdnet-stripped/config.h.cmake.in b/libdnet-stripped/config.h.cmake.in index a9ed5ad00..9af419757 100644 --- a/libdnet-stripped/config.h.cmake.in +++ b/libdnet-stripped/config.h.cmake.in @@ -36,6 +36,9 @@ /* Define to 1 if you have the `iphlpapi' library (-liphlpapi). */ #cmakedefine HAVE_LIBIPHLPAPI +/* Define to 1 if you have the header file. */ +#cmakedefine HAVE_PCAP_H + /* Define to 1 if you have the `nm' library (-lnm). */ #cmakedefine HAVE_LIBNM diff --git a/libdnet-stripped/include/dnet_winconfig.h b/libdnet-stripped/include/dnet_winconfig.h index 9fc6b067e..5df6310b3 100644 --- a/libdnet-stripped/include/dnet_winconfig.h +++ b/libdnet-stripped/include/dnet_winconfig.h @@ -250,7 +250,7 @@ #define VERSION "1.18.0" /* Define for faster code generation. */ -#define WIN32_LEAN_AND_MEAN +/* #define WIN32_LEAN_AND_MEAN */ /* Define to empty if `const' does not conform to ANSI C. */ /* #undef const */ diff --git a/libdnet-stripped/libdnet-stripped.vcxproj b/libdnet-stripped/libdnet-stripped.vcxproj index 04c152c5c..ccfdc36c7 100644 --- a/libdnet-stripped/libdnet-stripped.vcxproj +++ b/libdnet-stripped/libdnet-stripped.vcxproj @@ -235,7 +235,7 @@ - + diff --git a/libdnet-stripped/src/addr-util.c b/libdnet-stripped/src/addr-util.c index 43cad7b1f..bca82cfb7 100644 --- a/libdnet-stripped/src/addr-util.c +++ b/libdnet-stripped/src/addr-util.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include #include diff --git a/libdnet-stripped/src/addr.c b/libdnet-stripped/src/addr.c index 4baeef785..d5cd06fcd 100644 --- a/libdnet-stripped/src/addr.c +++ b/libdnet-stripped/src/addr.c @@ -8,7 +8,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include #ifdef HAVE_NET_IF_H diff --git a/libdnet-stripped/src/arp-win32.c b/libdnet-stripped/src/arp-win32.c index 8cf50ec81..c17abf0af 100644 --- a/libdnet-stripped/src/arp-win32.c +++ b/libdnet-stripped/src/arp-win32.c @@ -6,7 +6,7 @@ * $Id$ */ -#include "config.h" +#include "dnet_winconfig.h" #include #include diff --git a/libdnet-stripped/src/blob.c b/libdnet-stripped/src/blob.c index c0d8450e4..669f0fc88 100644 --- a/libdnet-stripped/src/blob.c +++ b/libdnet-stripped/src/blob.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include #include diff --git a/libdnet-stripped/src/eth-win32.c b/libdnet-stripped/src/eth-win32.c index 67790d7d9..2b9c15da7 100644 --- a/libdnet-stripped/src/eth-win32.c +++ b/libdnet-stripped/src/eth-win32.c @@ -6,7 +6,7 @@ * $Id$ */ -#include "config.h" +#include "dnet_winconfig.h" /* XXX - VC++ 6.0 bogosity */ #define sockaddr_storage sockaddr @@ -19,7 +19,6 @@ #include "dnet.h" #include -#include "pcap.h" /* From Npcap's Loopback.h */ /* diff --git a/libdnet-stripped/src/fw-none.c b/libdnet-stripped/src/fw-none.c index e6b9fa39f..eaf8a8a46 100644 --- a/libdnet-stripped/src/fw-none.c +++ b/libdnet-stripped/src/fw-none.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include #include diff --git a/libdnet-stripped/src/intf-win32.c b/libdnet-stripped/src/intf-win32.c index 901b619be..650333f4e 100644 --- a/libdnet-stripped/src/intf-win32.c +++ b/libdnet-stripped/src/intf-win32.c @@ -5,10 +5,12 @@ * */ -#include "config.h" +#include "dnet_winconfig.h" #include +#ifdef HAVE_PCAP_H #include +#endif #include #include @@ -152,7 +154,7 @@ _adapter_address_to_entry(intf_t *intf, IP_ADAPTER_ADDRESSES *a, type = _if_type_canonicalize(a->IfType); for (i = 0; i < intf->ifcombo[type].cnt; i++) { - if (intf->ifcombo[type].idx[i] == a->IfIndex && + if (intf->ifcombo[type].idx[i].ipv4 == a->IfIndex && intf->ifcombo[type].idx[i].ipv6 == a->Ipv6IfIndex) { break; } @@ -230,17 +232,16 @@ _adapter_address_to_entry(intf_t *intf, IP_ADAPTER_ADDRESSES *a, #define NPCAP_SERVICE_REGISTRY_KEY "SYSTEM\\CurrentControlSet\\Services\\npcap" +static int _intf_has_npcap_loopback(void) { HKEY hKey; - DWORD type, value; + DWORD type, value, size=sizeof(DWORD); int res = 0; - memset(buffer, 0, buf_size); - if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, NPCAP_SERVICE_REGISTRY_KEY "\\Parameters", 0, KEY_READ, &hKey) == ERROR_SUCCESS) { - if (RegQueryValueExA(hKey, "LoopbackAdapter", 0, &type, (LPBYTE)&value, sizeof(value)) == ERROR_SUCCESS && type == REG_DWORD) + if (RegQueryValueExA(hKey, "LoopbackSupport", 0, &type, (LPBYTE)&value, &size) == ERROR_SUCCESS && type == REG_DWORD) { res = value ? 1 : 0; } @@ -257,7 +258,7 @@ _update_tables_for_npcap_loopback(IP_ADAPTER_ADDRESSES *p) IP_ADAPTER_ADDRESSES *a; static int has_npcap_loopback = -1; - if (has_npcap_loopback < 0) { + if (has_npcap_loopback < 0) has_npcap_loopback = _intf_has_npcap_loopback(); if (!has_npcap_loopback) @@ -530,6 +531,8 @@ intf_close(intf_t *intf) return (NULL); } +#ifdef HAVE_PCAP_H +#define _DEVICE_PREFIX "\\Device\\" /* Converts a libdnet interface name to its pcap equivalent. The pcap name is stored in pcapdev up to a length of pcapdevlen, including the terminating '\0'. Returns -1 on error. */ @@ -593,8 +596,13 @@ intf_get_pcap_devname_cached(const char *intf_name, char *pcapdev, int pcapdevle else return 0; } +#endif /* HAVE_PCAP_H */ int intf_get_pcap_devname(const char *intf_name, char *pcapdev, int pcapdevlen) { +#ifdef HAVE_PCAP_H return intf_get_pcap_devname_cached(intf_name, pcapdev, pcapdevlen, 0); +#else + return -1; +#endif } diff --git a/libdnet-stripped/src/ip-util.c b/libdnet-stripped/src/ip-util.c index 7cbe44d3a..b646ec289 100644 --- a/libdnet-stripped/src/ip-util.c +++ b/libdnet-stripped/src/ip-util.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include #include diff --git a/libdnet-stripped/src/ip-win32.c b/libdnet-stripped/src/ip-win32.c index f1bba0762..79f8ed9c1 100644 --- a/libdnet-stripped/src/ip-win32.c +++ b/libdnet-stripped/src/ip-win32.c @@ -6,7 +6,7 @@ * $Id$ */ -#include "config.h" +#include "dnet_winconfig.h" #include diff --git a/libdnet-stripped/src/ip.c b/libdnet-stripped/src/ip.c index d5940a653..ffde2ac06 100644 --- a/libdnet-stripped/src/ip.c +++ b/libdnet-stripped/src/ip.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include diff --git a/libdnet-stripped/src/ip6.c b/libdnet-stripped/src/ip6.c index 2618a0b71..c342e7ab4 100644 --- a/libdnet-stripped/src/ip6.c +++ b/libdnet-stripped/src/ip6.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include "dnet.h" #include diff --git a/libdnet-stripped/src/ndisc-none.c b/libdnet-stripped/src/ndisc-none.c index de5700545..1f199a0c4 100644 --- a/libdnet-stripped/src/ndisc-none.c +++ b/libdnet-stripped/src/ndisc-none.c @@ -5,7 +5,11 @@ * */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include #include diff --git a/libdnet-stripped/src/rand.c b/libdnet-stripped/src/rand.c index 146432838..014a56475 100644 --- a/libdnet-stripped/src/rand.c +++ b/libdnet-stripped/src/rand.c @@ -9,7 +9,11 @@ * */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #ifdef _WIN32 # ifndef _WIN32_WINNT @@ -75,7 +79,7 @@ rand_open(void) u_char seed[256]; #ifdef _WIN32 # if _WIN32_WINNT >= _WIN32_WINNT_VISTA - if (STATUS_SUCCESS != BCryptGenRandom(NULL, seed, sizeof(seed), BCRYPT_USE_SYSTEM_PREFERRED_RNG)) + if (0 != BCryptGenRandom(NULL, seed, sizeof(seed), BCRYPT_USE_SYSTEM_PREFERRED_RNG)) return NULL; # else HCRYPTPROV hcrypt = 0; diff --git a/libdnet-stripped/src/route-win32.c b/libdnet-stripped/src/route-win32.c index 58f86f812..f22deca9d 100644 --- a/libdnet-stripped/src/route-win32.c +++ b/libdnet-stripped/src/route-win32.c @@ -6,7 +6,7 @@ * $Id$ */ -#include "config.h" +#include "dnet_winconfig.h" #include #include diff --git a/libdnet-stripped/src/tun-none.c b/libdnet-stripped/src/tun-none.c index e9ab12abe..9b4ea42fe 100644 --- a/libdnet-stripped/src/tun-none.c +++ b/libdnet-stripped/src/tun-none.c @@ -6,7 +6,11 @@ * $Id$ */ +#ifdef _WIN32 +#include "dnet_winconfig.h" +#else #include "config.h" +#endif #include diff --git a/libnetutil/netutil.cc b/libnetutil/netutil.cc index 5cedf5baf..e80d2cc4e 100644 --- a/libnetutil/netutil.cc +++ b/libnetutil/netutil.cc @@ -3908,7 +3908,7 @@ int DnetName2PcapName(const char *dnetdev, char *pcapdev, int pcapdevlen) { // OK, so it isn't in the cache. Let's ask dnet for it. /* Converts a dnet interface name (ifname) to its pcap equivalent, which is stored in pcapdev (up to a length of pcapdevlen). Returns 1 and fills in pcapdev if successful. */ - if (eth_get_pcap_devname(dnetdev, tmpdev, sizeof(tmpdev)) != 0) { + if (intf_get_pcap_devname(dnetdev, tmpdev, sizeof(tmpdev)) != 0) { // We've got it. Let's add it to the not found cache if (NNFCsz >= NNFCcapacity) { NNFCcapacity <<= 2;