如何解决Windows 上的 GetAddrInfo (C++) 未正确处理 IPv6,对于通过其他方式可以正常解析的域返回错误代码 11004
注意:我在这里广泛查看了一个类似(但不重复)的问题: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
...
现在我正在尝试编写一个示例程序,它使用 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,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,sizeof(buffer)));
std::cout << ipv4Str << std::endl << std::flush;
}
}
freeaddrinfo(results);
return 0;
}
为简单起见,我只是更改了 hints.ai_family
和硬编码的主机名,然后重新编译并重新运行(使用 Visual Studio)。这些是我得到的结果:
主机名 | hints.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
另外,事实证明 ipv6.google.com
实际上是 CNAME
的 ipv6.l.google.com
,所以我尝试使用该域,但无济于事。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。