From 78cc9b4b03bdd4f95c277391cac6e0951407072b Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Fri, 6 Feb 2026 23:20:39 -0600 Subject: [PATCH] Only pass the peer's local address if the host was wildcard bound --- host.c | 2 ++ include/enet/enet.h | 2 ++ protocol.c | 4 +++- unix.c | 24 ++++++++++++++++++++++++ win32.c | 22 ++++++++++++++++++++++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/host.c b/host.c index 1cf0c05..7668a4e 100644 --- a/host.c +++ b/host.c @@ -61,6 +61,8 @@ enet_host_create (int addressFamily, const ENetAddress * address, size_t peerCou return NULL; } + host -> wildcardBind = address && enet_address_wildcard (address); + enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1); enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); diff --git a/include/enet/enet.h b/include/enet/enet.h index 791e303..985b883 100644 --- a/include/enet/enet.h +++ b/include/enet/enet.h @@ -364,6 +364,7 @@ typedef int (ENET_CALLBACK * ENetInterceptCallback) (struct _ENetHost * host, st typedef struct _ENetHost { ENetSocket socket; + int wildcardBind; ENetAddress address; /**< Internet address of the host */ enet_uint32 incomingBandwidth; /**< downstream bandwidth of the host */ enet_uint32 outgoingBandwidth; /**< upstream bandwidth of the host */ @@ -527,6 +528,7 @@ ENET_API int enet_socketset_select (ENetSocket, ENetSocketSet *, ENetSock ENET_API int enet_address_set_host (ENetAddress * address, const char * hostName); ENET_API int enet_address_set_address (ENetAddress * address, struct sockaddr * addr, socklen_t addrlen); ENET_API int enet_address_set_port (ENetAddress * address, enet_uint16 port); +ENET_API int enet_address_wildcard (const ENetAddress * address); ENET_API int enet_address_equal (ENetAddress * address1, ENetAddress * address2); /** @} */ diff --git a/protocol.c b/protocol.c index f5301fc..1fa4de0 100644 --- a/protocol.c +++ b/protocol.c @@ -1731,7 +1731,9 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch enet_socket_set_option (host -> socket, ENET_SOCKOPT_QOS, 0); } - sentLength = enet_socket_send (host -> socket, & currentPeer -> address, & currentPeer -> localAddress, host -> buffers, host -> bufferCount); + sentLength = enet_socket_send (host -> socket, & currentPeer -> address, + host -> wildcardBind ? (& currentPeer -> localAddress) : NULL, + host -> buffers, host -> bufferCount); enet_protocol_remove_sent_unreliable_commands (currentPeer, & sentUnreliableCommands); diff --git a/unix.c b/unix.c index f5563c0..251e144 100644 --- a/unix.c +++ b/unix.c @@ -269,6 +269,30 @@ enet_address_equal (ENetAddress * address1, ENetAddress * address2) } } +int +enet_address_wildcard (const ENetAddress * address) +{ + switch (address -> address.ss_family) + { + case AF_INET: + { + struct sockaddr_in *sin = (struct sockaddr_in *) & address -> address; + return sin -> sin_addr.s_addr == INADDR_ANY; + } +#ifdef AF_INET6 + case AF_INET6: + { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) & address -> address; + return ! memcmp (& sin6 -> sin6_addr, & in6addr_any, sizeof (in6addr_any)); + } +#endif + default: + { + return 0; + } + } +} + int enet_address_set_port (ENetAddress * address, enet_uint16 port) { diff --git a/win32.c b/win32.c index b1b707c..a93594b 100644 --- a/win32.c +++ b/win32.c @@ -190,6 +190,28 @@ enet_address_equal (ENetAddress * address1, ENetAddress * address2) } } +int +enet_address_wildcard (const ENetAddress * address) +{ + switch (address -> address.ss_family) + { + case AF_INET: + { + struct sockaddr_in *sin = (struct sockaddr_in *) & address -> address; + return sin -> sin_addr.S_un.S_addr == INADDR_ANY; + } + case AF_INET6: + { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) & address -> address; + return ! memcmp (& sin6 -> sin6_addr, & in6addr_any, sizeof (in6addr_any)); + } + default: + { + return 0; + } + } +} + int enet_address_set_host (ENetAddress * address, const char * name) {