Windows 上的 GetAddrInfo (C++) 未正确处理 IPv6,对于通过其他方式正确解析的域返回错误代码 11004

Nic*_*ams 5 c++ windows ipv6 visual-c++ getaddrinfo

注意:我在这里广泛查看了一个类似(但不重复)的问题:GetAddrInfo cannot resolve ipv6.google.com (but nslookup can)。这个问题已有 9 年历史,OP 没有遇到与我完全相同的行为,并且已经接受的答案并没有解决我的问题——事实上,我特意遵循了那个答案的建议,但在样本中没有用下面的代码,我得到的错误与那个问题不同。此外,我仅仅通过评论就成功地让任何人参与到一个古老的问题中的可能性为零。这与我今天遇到的问题不同,所以我创建了一个新的、更详细的问题。

我有一台运行 Visual Studio 2017 的 Windows 10 机器和一台运行 Visual Studio 2019 的 Windows Server 2016 机器。两台机器都配置了 IPv4 和 IPv6 地址。在两台机器上,nslookup.exe返回三个不同域的预期结果:

> nslookup www.google.com
...
Addresses:  2607:f8b0:4002:80a::2004
          172.217.3.228
...
> nslookup ipv4.google.com
...
Addresses:  74.125.136.102
...
> nslookup ipv6.google.com
...
Addresses:  2607:f8b0:4002:812::200e
...
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试编写一个示例程序,用于GetAddrInfo查找这三个相同的域:

#include <iostream>
#include <sstream>
#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib,"WS2_32.Lib")

int main(int argc, char* argv[])
{
   WORD wVersionRequested = MAKEWORD(2, 2);
   WSADATA wsaData;
   int err( WSAStartup(wVersionRequested, &wsaData) );
   if (err != 0)
   {
       std::cout << "WSAStartup failed with error " << err << std::endl << std::flush;
       return 1;
   }

   struct addrinfo hints;
   memset(&hints, 0, sizeof(hints));
   hints.ai_socktype = SOCK_STREAM; // so that we get each IP address once, instead of once per socket type
   hints.ai_protocol = 0; // we don't care about the protocol
   hints.ai_canonname = NULL; // should be NULL for hints
   hints.ai_addr = NULL; // should be NULL for hints
   hints.ai_next = NULL; // should be NULL for hints
   hints.ai_flags = 0;

   hints.ai_family = AF_UNSPEC; // I vary this and the hostname below

   struct addrinfo* results;

   int error = getaddrinfo("ipv6.google.com", NULL, &hints, &results);
   if (error != 0)
   {
       std::ostringstream msg;
       std::cout << "Could not resolve hostname due to ";
       std::cout << "the following error in getaddrinfo: " << error << ": ";
       if (error == -11)
       {
           std::cout << "EAI_SYSTEM";
       }
       else
       {
           std::cout << gai_strerrorA(error);
       }
       std::cout << std::endl << std::flush;
       return 1;
   }

   for (struct addrinfo* result = results; result != NULL; result = result->ai_next)
   {
       if (result->ai_family == AF_INET6)
       {
           char buffer[INET6_ADDRSTRLEN];
           std::string ipv6Str(
               inet_ntop(
                   AF_INET6,
                   &((struct sockaddr_in6*)result->ai_addr)->sin6_addr,
                   buffer,
                   sizeof(buffer)));
           std::cout << ipv6Str << std::endl << std::flush;
       }
       else
       {
           char buffer[INET_ADDRSTRLEN];
           std::string ipv4Str(
               inet_ntop(
                   AF_INET,
                   &((struct sockaddr_in*)result->ai_addr)->sin_addr,
                   buffer,
                   sizeof(buffer)));
           std::cout << ipv4Str << std::endl << std::flush;
       }
   }

   freeaddrinfo(results);

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

为简单起见,我只是更改了hints.ai_family硬编码的主机名,然后重新编译并重新运行(使用 Visual Studio)。这些是我得到的结果:

主机名 提示.ai_family 结果 预期的?
www.google.com AF_UNSPEC 一个 IPv4 和一个 IPv6 地址 ?
www.google.com AF_INET 仅一个 IPv4 地址 ?
www.google.com AF_INET6 Err 11004:名称有效,无数据 ??
ipv4.google.com AF_UNSPEC 仅一个 IPv4 地址 ?
ipv4.google.com AF_INET 仅一个 IPv4 地址 ?
ipv4.google.com AF_INET6 Err 11004:名称有效,无数据 ?
ipv6.google.com AF_UNSPEC Err 11004:名称有效,无数据 ??
ipv6.google.com AF_INET Err 11004:名称有效,无数据 ?
ipv6.google.com AF_INET6 Err 11004:名称有效,无数据 ??

我已经为此苦恼了一整天,但无法getaddrinfo返回预期的结果。为什么这不起作用?似乎nslookup.exe使用除getaddrinfo(可能是根和胶水名称服务器的更底层查询)以外的其他东西,因为它在那里工作正常。另请注意,此代码(包含不同的包含和不包含 WSA 启动程序)在 RedHat、CentOS、Ubuntu 和 macOS High Sierra 中运行良好。

除了改变 之外,我还尝试了一些事情ai_family,所有这些都会产生与下表中相同的结果:

ai_flags = AI_NUMERICSERV
ai_flags = AI_ALL
ai_flags = AI_ALL | AI_NUMERICSERV
ai_socktype = SOCK_DGRAM
ai_socktype = 0
ai_protocol = IPPROTO_TCP
Run Code Online (Sandbox Code Playgroud)

此外,事实证明这ipv6.google.com实际上是一个CNAMEfor ipv6.l.google.com,所以我尝试使用该域,但无济于事。