From 89a7ebd63d48a237daa06aca97f88b70fe28da8c Mon Sep 17 00:00:00 2001 From: zoeyjodon <76182954+zoeyjodon@users.noreply.github.com> Date: Fri, 22 Dec 2023 13:48:34 -0500 Subject: [PATCH 1/3] Reduce 3DS MTU (#14) --- include/enet/protocol.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/enet/protocol.h b/include/enet/protocol.h index b3c8b03e..444919a8 100644 --- a/include/enet/protocol.h +++ b/include/enet/protocol.h @@ -1,4 +1,4 @@ -/** +/** @file protocol.h @brief ENet protocol */ @@ -10,7 +10,7 @@ enum { ENET_PROTOCOL_MINIMUM_MTU = 576, -#ifdef __WIIU__ +#if defined(__WIIU__) || defined(__3DS__) ENET_PROTOCOL_MAXIMUM_MTU = 1400, #else ENET_PROTOCOL_MAXIMUM_MTU = 4096, From 43218d4de23b46d7da54e32f7a5645eb4ca68e92 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 28 Dec 2023 00:35:46 -0600 Subject: [PATCH 2/3] Add warning when IPv6 options are not defined on Apple platforms --- unix.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/unix.c b/unix.c index 5f9b6584..6ee5ddd1 100644 --- a/unix.c +++ b/unix.c @@ -29,6 +29,9 @@ #include "enet/enet.h" #if defined(__APPLE__) +#if !defined(IPV6_RECVPKTINFO) || !defined(IPV6_PKTINFO) +#warning Missing IPv6 socket option definitions. Is __APPLE_USE_RFC_3542 defined? +#endif #ifndef HAS_POLL #define HAS_POLL 1 #endif From c6bb0e50118d08252eee308de8412751218442d6 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Thu, 28 Dec 2023 16:07:39 -0600 Subject: [PATCH 3/3] Add workaround for Apple kernel bug --- unix.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/unix.c b/unix.c index 6ee5ddd1..0903dadc 100644 --- a/unix.c +++ b/unix.c @@ -219,6 +219,27 @@ enet_time_set (enet_uint32 newTimeBase) timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase; } +#ifdef __APPLE__ +void +enet_address_make_v4mapped (ENetAddress * address) +{ + ENetAddress oldAddress = *address; + struct sockaddr_in *sin = ((struct sockaddr_in *)&oldAddress.address); + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&address->address; + + memset(sin6, 0, sizeof(*sin6)); + sin6->sin6_family = AF_INET6; + sin6->sin6_len = sizeof(*sin6); + sin6->sin6_port = sin->sin_port; + + sin6->sin6_addr.s6_addr[10] = 0xFF; + sin6->sin6_addr.s6_addr[11] = 0xFF; + memcpy(&sin6->sin6_addr.s6_addr[12], &sin->sin_addr, 4); + + address->addressLength = sizeof(*sin6); +} +#endif + int enet_address_equal (ENetAddress * address1, ENetAddress * address2) { @@ -729,8 +750,19 @@ enet_socket_receive (ENetSocket socket, } } - if (peerAddress != NULL) - peerAddress -> addressLength = msgHdr.msg_namelen; + if (peerAddress != NULL) { + peerAddress -> addressLength = msgHdr.msg_namelen; + +#ifdef __APPLE__ + // HACK: Apple platforms return AF_INET addresses in msg_name from recvmsg() on dual-stack sockets + // instead of AF_INET6 addresses then rejects those same addresses when they are passed to sendmsg(). + // Strangely, this only happens when the socket is bound, and IPV6_PKTINFO properly returns v4-mapped + // addresses in the same call. This is probably a kernel bug, so we fix it up here. + if (peerAddress -> address.ss_family == AF_INET && localAddress -> address.ss_family == AF_INET6) { + enet_address_make_v4mapped(peerAddress); + } +#endif + } return recvLength; #endif