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

如何解决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 实际上是 CNAMEipv6.l.google.com,所以我尝试使用该域,但无济于事。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-