Force AF_INET6 when binding to a NULL address #85
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Note: This probably needs a fallback to support IPv4-only systems before merging. I've also only tested it on Linux, on exactly one network so far. So it could easily cause as many problems as it solves.
Currently, to listen on 'all' addresses, NULL is used as the
SDLNet_Address
pointer. This results in NULL being passed togetaddrinfo()
inMakeAddrInfoWithPort()
, as well as the hintAI_PASSIVE
and address familyAF_UNSPEC
being used.It's the latter which can cause problems: on a dual-stack IPv4/IPv6 setup,
AF_UNSPEC
can resolve toAF_INET
, resulting in the socket only binding to IPv4 addresses. This means that any attempt to connect to such a socket using the destination's IPv6 address (which may be the only address returned bySDLNet_ResolveHostname()
) will fail.There are two possible solutions here:
AF_INET6
, and enable v4 address mapping, which will allow IPv4 addresses to be treated as IPv6 addresses of the form::ffff:<address>
. Also set theAI_V4MAPPED
hint, which is enabled by default on Linux, but may not be everywhere.This implements the latter: it has the advantage of being much simpler (only one socket is required), but the disadvantages of not working as-is on IPv4-only systems (which are rarer, but still exist), and not supporting non-IP protocols at all (I assume we're not planning to support IPX).
With this patch, the example programs all now function properly on my network (without it, I cannot use hostnames — even localhost — with voipchat, only explicit IPv4 addresses).
There are still some other potential pitfalls:
SDLNet_GetLocalAddresses()
still lists IPv4 addresses first, so if users bind explicitly to that, rather than useNULL
, they'll be in trouble. Of course, it also returns loopback addresses first, so they'd be in trouble anyway…