From d3e2fd10991c8b283a0113898ce7fad20e286200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20B=C3=A9raud?= Date: Tue, 10 Jun 2025 10:24:28 -0400 Subject: [PATCH] fixes for msvc --- CMakeLists.txt | 1 - include/opendht/default_types.h | 4 ++++ include/opendht/infohash.h | 10 ++++++++-- include/opendht/value.h | 4 ++-- meson.build | 1 - src/Makefile.am | 1 - src/crypto.cpp | 4 ++-- src/infohash.cpp | 12 ------------ src/utils.cpp | 2 -- tests/tests_runner.cpp | 11 +++++++++++ tools/dhtchat.cpp | 5 ++++- tools/dhtnode.cpp | 5 ++++- tools/dhtscanner.cpp | 5 ++++- tools/perftest.cpp | 5 ++++- tools/tools_common.h | 2 ++ 15 files changed, 45 insertions(+), 27 deletions(-) delete mode 100644 src/infohash.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 7639c832..e20d0a17 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -305,7 +305,6 @@ set (top_srcdir "${CMAKE_CURRENT_SOURCE_DIR}") # Sources list (APPEND opendht_SOURCES - src/infohash.cpp src/utils.cpp src/crypto.cpp src/default_types.cpp diff --git a/include/opendht/default_types.h b/include/opendht/default_types.h index f3c25873..4b34152d 100644 --- a/include/opendht/default_types.h +++ b/include/opendht/default_types.h @@ -127,6 +127,10 @@ public: SignedValue::unpackValue(v); } + static Value::Filter getFilter() { + return SignedValue::getFilter(); + } + dht::InfoHash to; dht::Value::Id id {0}; std::string msg; diff --git a/include/opendht/infohash.h b/include/opendht/infohash.h index 7cfc0e1b..318535f1 100644 --- a/include/opendht/infohash.h +++ b/include/opendht/infohash.h @@ -345,12 +345,18 @@ Hash::getRandom(Rd& rdev) } struct alignas(std::max_align_t) HexMap : public std::array, 256> { - HexMap(); + constexpr HexMap(): std::array, 256>() { + for (size_t i = 0; i < size(); i++) { + auto& e = (*this)[i]; + e[0] = hex_digits[(i >> 4) & 0x0F]; + e[1] = hex_digits[i & 0x0F]; + } + } private: static constexpr const char* hex_digits = "0123456789abcdef"; }; -OPENDHT_PUBLIC extern const HexMap hex_map; +inline constexpr HexMap hex_map {}; inline std::string toHex(const uint8_t* data, size_t size) { diff --git a/include/opendht/value.h b/include/opendht/value.h index 8417fcbc..b42fa4e4 100644 --- a/include/opendht/value.h +++ b/include/opendht/value.h @@ -91,7 +91,7 @@ using EditPolicy = std::function static constexpr const size_t MAX_VALUE_SIZE {1024 * 64}; -struct OPENDHT_PUBLIC ValueType { +struct ValueType { typedef uint16_t Id; static bool DEFAULT_STORE_POLICY(InfoHash, const std::shared_ptr& v, const InfoHash&, const SockAddr&); @@ -114,7 +114,7 @@ struct OPENDHT_PUBLIC ValueType { } // Generic value type - static const ValueType USER_DATA; + OPENDHT_PUBLIC static const ValueType USER_DATA; Id id {0}; diff --git a/meson.build b/meson.build index 28ba5d54..c2bd62ff 100644 --- a/meson.build +++ b/meson.build @@ -28,7 +28,6 @@ add_project_arguments(['-Wno-return-type','-Wno-deprecated','-Wnon-virtual-dtor' opendht_inc = include_directories('include/opendht') opendht_interface_inc = include_directories('include', is_system: true) opendht_src = [ - 'src/infohash.cpp', 'src/utils.cpp', 'src/crypto.cpp', 'src/default_types.cpp', diff --git a/src/Makefile.am b/src/Makefile.am index e63782a9..31f66f3a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,7 +27,6 @@ libopendht_la_SOURCES = \ default_types.cpp \ log.cpp \ network_utils.cpp \ - infohash.cpp \ thread_pool.cpp nobase_include_HEADERS = \ diff --git a/src/crypto.cpp b/src/crypto.cpp index 192d974b..1901f575 100644 --- a/src/crypto.cpp +++ b/src/crypto.cpp @@ -1502,7 +1502,7 @@ OcspResponse::getCertificateStatus() const int ret; unsigned int status; // MSVC/vcpkg GnuTLS cast workaround -#if _WIN32 && (GNUTLS_VERSION_NUMBER >= 0x030807) +#if _WIN32 && (GNUTLS_VERSION_NUMBER == 0x030807) ret = gnutls_ocsp_resp_get_single(response, 0, NULL, @@ -1576,7 +1576,7 @@ OcspResponse::verifyDirect(const Certificate& crt, const Blob& nonce) // Check certificate revocation status unsigned int status_ocsp; // MSVC/vcpkg GnuTLS cast workaround -#if _WIN32 && (GNUTLS_VERSION_NUMBER >= 0x030807) +#if _WIN32 && (GNUTLS_VERSION_NUMBER == 0x030807) ret = gnutls_ocsp_resp_get_single(response, 0, NULL, diff --git a/src/infohash.cpp b/src/infohash.cpp deleted file mode 100644 index e4fd0fe9..00000000 --- a/src/infohash.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "def.h" -#include "infohash.h" - -OPENDHT_PUBLIC extern const dht::HexMap hex_map; - -dht::HexMap::HexMap() { - for (size_t i = 0; i < size(); i++) { - auto& e = (*this)[i]; - e[0] = hex_digits[(i >> 4) & 0x0F]; - e[1] = hex_digits[i & 0x0F]; - } -} diff --git a/src/utils.cpp b/src/utils.cpp index b25062a2..5e6a96bd 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -39,8 +39,6 @@ const char* version() { return PACKAGE_VERSION; } -const HexMap hex_map = {}; - static constexpr std::array MAPPED_IPV4_PREFIX {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}}; std::pair diff --git a/tests/tests_runner.cpp b/tests/tests_runner.cpp index 15f6cf17..f2c7fcdd 100644 --- a/tests/tests_runner.cpp +++ b/tests/tests_runner.cpp @@ -21,7 +21,18 @@ #include #include +extern "C" { +#include +} + int main(int /*argc*/, char** /*argv*/) { +#ifdef _MSC_VER + if (auto err = gnutls_global_init()) { + std::cerr << "Failed to initialize GnuTLS: " << gnutls_strerror(err) << std::endl; + return EXIT_FAILURE; + } +#endif + CppUnit::TestFactoryRegistry ®istry = CppUnit::TestFactoryRegistry::getRegistry(); CppUnit::Test *suite = registry.makeTest(); if (suite->countTestCases() == 0) { diff --git a/tools/dhtchat.cpp b/tools/dhtchat.cpp index 1aa54dd2..007f4ebd 100644 --- a/tools/dhtchat.cpp +++ b/tools/dhtchat.cpp @@ -55,7 +55,10 @@ main(int argc, char **argv) return 0; } #ifdef _MSC_VER - gnutls_global_init(); + if (auto err = gnutls_global_init()) { + std::cerr << "Failed to initialize GnuTLS: " << gnutls_strerror(err) << std::endl; + return EXIT_FAILURE; + } #endif DhtRunner dht; diff --git a/tools/dhtnode.cpp b/tools/dhtnode.cpp index 27ee6a16..1267b887 100644 --- a/tools/dhtnode.cpp +++ b/tools/dhtnode.cpp @@ -516,7 +516,10 @@ int main(int argc, char **argv) { #ifdef _MSC_VER - gnutls_global_init(); + if (auto err = gnutls_global_init()) { + std::cerr << "Failed to initialize GnuTLS: " << gnutls_strerror(err) << std::endl; + return EXIT_FAILURE; + } #endif auto params = parseArgs(argc, argv); if (params.help) { diff --git a/tools/dhtscanner.cpp b/tools/dhtscanner.cpp index e9f62987..81b06023 100644 --- a/tools/dhtscanner.cpp +++ b/tools/dhtscanner.cpp @@ -78,7 +78,10 @@ int main(int argc, char **argv) { #ifdef _MSC_VER - gnutls_global_init(); + if (auto err = gnutls_global_init()) { + std::cerr << "Failed to initialize GnuTLS: " << gnutls_strerror(err) << std::endl; + return EXIT_FAILURE; + } #endif auto params = parseArgs(argc, argv); if (params.help) { diff --git a/tools/perftest.cpp b/tools/perftest.cpp index 59d639ee..be5198ce 100644 --- a/tools/perftest.cpp +++ b/tools/perftest.cpp @@ -133,7 +133,10 @@ int main(int argc, char **argv) { #ifdef _MSC_VER - gnutls_global_init(); + if (auto err = gnutls_global_init()) { + std::cerr << "Failed to initialize GnuTLS: " << gnutls_strerror(err) << std::endl; + return EXIT_FAILURE; + } #endif auto params = parseArgs(argc, argv); if (params.help) { diff --git a/tools/tools_common.h b/tools/tools_common.h index f3d3c349..7d8e14d2 100644 --- a/tools/tools_common.h +++ b/tools/tools_common.h @@ -437,6 +437,7 @@ private: ServiceRunner runner; +#ifndef DISABLE_SIGNALS void signal_handler(int sig) { switch(sig) { @@ -450,6 +451,7 @@ void signal_handler(int sig) break; } } +#endif void setupSignals() {