I’m calling getaddrinfo
on Windows to query the IP address(es) of a Mac on my local network using mdns. The Mac has both IPv4 and IPv6 addresses.
static struct addrinfo* transport_resolve_host(const char* hostname, int port)
{
int status = 0;
char* service = NULL;
char portStr[16];
struct addrinfo hints = { 0 };
struct addrinfo* result = NULL;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if (port >= 0)
{
sprintf_s(portStr, sizeof(portStr) - 1, "%d", port);
service = portStr;
hints.ai_flags |= AI_NUMERICSERV;
}
status = getaddrinfo(hostname, service, &hints, &result);
// check status and iterate results
// ...
}
struct addrinfo* addr = NULL;
result = freevnc_transport_resolve_host("richards-macbook-pro.local", 5900);
With the given code and parameters, result
only contains the IPv4 address of the machine. This is expected.
However, if I change hints.ai_family
to AF_UNSPEC
, results
only contains the IPv6 address. Why?
The documentation says:
A value of AF_UNSPEC for ai_family indicates the caller will accept any protocol family. This value can be used to return both IPv4 and IPv6 addresses for the host name pointed to by the pNodeName parameter.
So why do I only get IPv6 in this case? What am I doing wrong?
Edited to add There is no corresponding entry in my %systemroot%system32driversetchosts file for the destination machine