From 6c3c3b04637d5aec3faeb0162da58cf96f0d1d6b Mon Sep 17 00:00:00 2001 From: Mark Kunka Date: Sat, 15 Feb 2025 11:29:30 -0800 Subject: [PATCH 1/3] create hint SDL_NET_HINT_IP_DEFAULT_VERSION When creating a socket bound to a NULL address, the default is AF_UNSPEC. This is causing a problem in my mixed IPv4/IPv6 environment (Window 10). A hint will allow the user to specify what they want to use. --- include/SDL3_net/SDL_net.h | 8 ++++++++ src/SDL_net.c | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/SDL3_net/SDL_net.h b/include/SDL3_net/SDL_net.h index eefc96a..e3ba733 100644 --- a/include/SDL3_net/SDL_net.h +++ b/include/SDL3_net/SDL_net.h @@ -61,6 +61,14 @@ extern "C" { (SDL_NET_MAJOR_VERSION > X || SDL_NET_MINOR_VERSION >= Y) && \ (SDL_NET_MAJOR_VERSION > X || SDL_NET_MINOR_VERSION > Y || SDL_NET_MICRO_VERSION >= Z)) +/** + * A variable setting the default IP value to "IPv4" or "IPv6" + * + * This hint should be set before creating a socket bound to a NULL address + * + * \since This hint is available since SDL_net 3.0.0 + */ +#define SDL_NET_HINT_IP_DEFAULT_VERSION "SDL_NET_HINT_IP_DEFAULT_VERSION" /** * This function gets the version of the dynamically linked SDL_net library. diff --git a/src/SDL_net.c b/src/SDL_net.c index 8d1bf36..84b6f17 100644 --- a/src/SDL_net.c +++ b/src/SDL_net.c @@ -160,7 +160,7 @@ static char *CreateSocketErrorString(int rc) MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default language */ msgbuf, SDL_arraysize(msgbuf), - NULL + NULL ); if (bw == 0) { return SDL_strdup("Unknown error"); @@ -279,7 +279,7 @@ static int SDLCALL ResolverThread(void *data) int outcome; if (!simulated_loss || (RandomNumberBetween(0, 100) > simulated_loss)) { - outcome = ResolveAddress(addr); + outcome = ResolveAddress(addr); } else { outcome = -1; addr->errstr = SDL_strdup("simulated failure"); @@ -779,6 +779,16 @@ static struct addrinfo *MakeAddrInfoWithPort(const SDLNet_Address *addr, const i //hints.ai_protocol = ainfo ? ainfo->ai_protocol : 0; hints.ai_flags = AI_NUMERICHOST | AI_NUMERICSERV | (!ainfo ? AI_PASSIVE : 0); + const char *hint = SDL_GetHint(SDL_NET_HINT_IP_DEFAULT_VERSION); + if (!addr && hint) { + if (SDL_strcasecmp(hint, "ipv4") == 0) { + hints.ai_family = AF_INET; + } else if (SDL_strcasecmp(hint, "ipv6") == 0) { + hints.ai_family = AF_INET6; + hints.ai_flags |= AI_V4MAPPED; + } + } + char service[16]; SDL_snprintf(service, sizeof (service), "%d", (int) port); From 171a41845fa01613af18a6d22463156e4e70d701 Mon Sep 17 00:00:00 2001 From: Mark Kunka Date: Sat, 15 Feb 2025 12:24:33 -0800 Subject: [PATCH 2/3] Also use SDL_NET_HINT_IP_DEFAULT_VERSION in ResolveAddress So that the ip addresses will be in a consistent version. --- src/SDL_net.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/SDL_net.c b/src/SDL_net.c index 84b6f17..a95de06 100644 --- a/src/SDL_net.c +++ b/src/SDL_net.c @@ -220,8 +220,23 @@ static int ResolveAddress(SDLNet_Address *addr) struct addrinfo *ainfo = NULL; int rc; + struct addrinfo hints, *phints = &hints; + SDL_zero(hints); + + const char *hint = SDL_GetHint(SDL_NET_HINT_IP_DEFAULT_VERSION); + if (hint) { + if (SDL_strcasecmp(hint, "ipv4") == 0) { + hints.ai_family = AF_INET; + } else if (SDL_strcasecmp(hint, "ipv6") == 0) { + hints.ai_family = AF_INET6; + hints.ai_flags = AI_V4MAPPED; + } + } else { + phints = NULL; + } + //SDL_Log("getaddrinfo '%s'", addr->hostname); - rc = getaddrinfo(addr->hostname, NULL, NULL, &ainfo); + rc = getaddrinfo(addr->hostname, NULL, phints, &ainfo); //SDL_Log("rc=%d", rc); if (rc != 0) { addr->errstr = CreateGetAddrInfoErrorString(rc); From d515bff87f51ffcd40796f2206c0cf57dcf0f83d Mon Sep 17 00:00:00 2001 From: Mark Kunka Date: Sat, 15 Feb 2025 12:42:17 -0800 Subject: [PATCH 3/3] tweek comment --- include/SDL3_net/SDL_net.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/SDL3_net/SDL_net.h b/include/SDL3_net/SDL_net.h index e3ba733..09ea6d6 100644 --- a/include/SDL3_net/SDL_net.h +++ b/include/SDL3_net/SDL_net.h @@ -65,6 +65,7 @@ extern "C" { * A variable setting the default IP value to "IPv4" or "IPv6" * * This hint should be set before creating a socket bound to a NULL address + * and before resolving a hostname. * * \since This hint is available since SDL_net 3.0.0 */