Skip to content

Add hint SDL_NET_HINT_IP_DEFAULT_VERSION to select IPv6 or IPv4 #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions include/SDL3_net/SDL_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ 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
* and before resolving a hostname.
*
* \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.
Expand Down
31 changes: 28 additions & 3 deletions src/SDL_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -279,7 +294,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");
Expand Down Expand Up @@ -779,6 +794,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);

Expand Down
Loading