From 6284a95267d97b45185ee92c9d2ec2d4fce1aee4 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Sat, 25 Apr 2026 00:59:50 +0300 Subject: [PATCH] Fix: Define tc_htonl/tc_htons as macros before winsock2.h to avoid struct-in_addr redefinition --- lib/platform_compat.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/platform_compat.h b/lib/platform_compat.h index eba19429..821b03de 100644 --- a/lib/platform_compat.h +++ b/lib/platform_compat.h @@ -6,6 +6,12 @@ #define PLATFORM_COMPAT_H #ifdef _WIN32 + // Portable byte-order: must be first, before winsock2.h defines anything + #define tc_htons(x) ((uint16_t)(((uint16_t)(x) & 0xFFU) << 8) | (((uint16_t)(x) & 0xFF00U) >> 8)) + #define tc_htonl(x) ((((uint32_t)(x) & 0x000000FFU) << 24) | (((uint32_t)(x) & 0x0000FF00U) << 8) | (((uint32_t)(x) & 0x00FF0000U) >> 8) | (((uint32_t)(x) & 0xFF000000U) >> 24)) + #define tc_ntohs(x) tc_htons(x) + #define tc_ntohl(x) tc_htonl(x) + #include #include #include @@ -153,11 +159,12 @@ #include -// Portable byte-order: no winsock2, no struct in_addr, pure C +#ifndef tc_htons static inline uint16_t tc_htons(uint16_t x) { return (uint16_t)(((x & 0xFFU) << 8) | ((x & 0xFF00U) >> 8)); } static inline uint32_t tc_htonl(uint32_t x) { return (((x & 0x000000FFU) << 24) | ((x & 0x0000FF00U) << 8) | ((x & 0x00FF0000U) >> 8) | ((x & 0xFF000000U) >> 24)); } #define tc_ntohs(x) tc_htons(x) #define tc_ntohl(x) tc_htonl(x) +#endif // Generate cryptographically secure random bytes // Returns 0 on success, -1 on error