1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

Windows build fixes for libdnet-stripped

This commit is contained in:
dmiller
2025-04-11 16:39:12 +00:00
parent a62aa864eb
commit 21e5cc67b4
21 changed files with 189 additions and 24 deletions

View File

@@ -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 <windows.h>
#include <Iphlpapi.h>
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}

View File

@@ -0,0 +1,94 @@
#
# Copyright (C) 2017 Ali Abdulkadir <autostart.ini@gmail.com>.
#
# 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})

View File

@@ -36,6 +36,9 @@
/* Define to 1 if you have the `iphlpapi' library (-liphlpapi). */
#cmakedefine HAVE_LIBIPHLPAPI
/* Define to 1 if you have the <pcap.h> header file. */
#cmakedefine HAVE_PCAP_H
/* Define to 1 if you have the `nm' library (-lnm). */
#cmakedefine HAVE_LIBNM

View File

@@ -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 */

View File

@@ -235,7 +235,7 @@
<ClCompile Include="src\strsep.c" />
<ClCompile Include="src\arp-win32.c" />
<ClCompile Include="src\eth-win32.c" />
<ClCompile Include="src\fw-pktfilter.c" />
<ClCompile Include="src\fw-none.c" />
<ClCompile Include="src\intf-win32.c" />
<ClCompile Include="src\ip-win32.c" />
<ClCompile Include="src\route-win32.c" />

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>

View File

@@ -8,7 +8,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <sys/types.h>
#ifdef HAVE_NET_IF_H

View File

@@ -6,7 +6,7 @@
* $Id$
*/
#include "config.h"
#include "dnet_winconfig.h"
#include <ws2tcpip.h>
#include <iphlpapi.h>

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <ctype.h>
#include <stdarg.h>

View File

@@ -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 <winsock2.h>
#include "pcap.h"
/* From Npcap's Loopback.h */
/*

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <errno.h>
#include <stdio.h>

View File

@@ -5,10 +5,12 @@
*
*/
#include "config.h"
#include "dnet_winconfig.h"
#include <iphlpapi.h>
#ifdef HAVE_PCAP_H
#include <pcap.h>
#endif
#include <ctype.h>
#include <errno.h>
@@ -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
}

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <errno.h>
#include <stdlib.h>

View File

@@ -6,7 +6,7 @@
* $Id$
*/
#include "config.h"
#include "dnet_winconfig.h"
#include <ws2tcpip.h>

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <netinet/in.h>

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include "dnet.h"
#include <string.h>

View File

@@ -5,7 +5,11 @@
*
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <errno.h>
#include <stdio.h>

View File

@@ -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;

View File

@@ -6,7 +6,7 @@
* $Id$
*/
#include "config.h"
#include "dnet_winconfig.h"
#include <ws2tcpip.h>
#include <iphlpapi.h>

View File

@@ -6,7 +6,11 @@
* $Id$
*/
#ifdef _WIN32
#include "dnet_winconfig.h"
#else
#include "config.h"
#endif
#include <sys/types.h>

View File

@@ -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;