好文档 - 专业文书写作范文服务资料分享网站

【IT专家】使用getaddrinfo将IPv4地址转换为IPv6地址时,会丢失服务端口

天下 分享 时间: 加入收藏 我要投稿 点赞

本文由我司收集整编,推荐下载,如有疑问,请与我司联系

使用 getaddrinfo 将 IPv4 地址转换为 IPv6 地址时,会丢失服务端口

使用 getaddrinfo 将 IPv4 地址转换为 IPv6 地址时,会丢失服务端口[英]Service port is missed when using getaddrinfo to convert IPv4 addr to IPv6 addr I am testing IPv6

in Mac OS X 10.11.2 and I find a strange problem.

我在 Mac OS X 10.11.2 中测试 IPv6,我发现一个奇怪的问题。 I use getaddrinfo to resolve hostname to IPv6 addr : 我使用 getaddrinfo 将主机名解析为 IPv6 addr:

#include stdio.h #include netdb.h #include errno.h #include string.h #include arpa/inet.h int main(int argc, const char * argv[]) {struct addrinfo * res, * addr;struct addrinfo hints;char

buffer[128];struct

sockaddr_in6

=

*

sockaddr_v6;memset(

hints,

0, =

sizeof(hints));hints.ai_family PF_UNSPEC;hints.ai_socktype

SOCK_STREAM;hints.ai_flags = AI_DEFAULT;if (getaddrinfo(“google”, “80”, hints, res)) {//if (getaddrinfo(“216.58.199.4”, “80”, hints, res)) { printf(“getaddrinfo failed with errno(-%d)\\n”, errno); return 0;for (addr = res;addr;addr = addr- ai_next) if (addr- ai_family == AF_INET6) sockaddr_v6 = (struct sockaddr_in6 *)addr- ai_addr; printf(“ipv6 addr is %s %d)\\n”, inet_ntop(AF_INET6, sockaddr_v6- sin6_addr, buffer, sizeof(buffer)), ntohs(sockaddr_v6- sin6_port));freeaddrinfo(res);return 0; output is “ipv6 addr is 64:ff9b::d83a:c704 80”. everything is ok ! “google” is resolved to “64:ff9b::d83a:c704”, sin6_port is 80. but when I use “216.58.199.4” instead of “google”, “216.58.199.4” is IPv4 addr of “google”.

但是当我使用“216.58.199.4”代替“google”时,“216.58.199.4”是“google”的 IPv4 地 址。

//if (getaddrinfo(“google”, “80”, hints, res)) {if (getaddrinfo(“216.58.199.4”, “80”, hints, res)) { output is “ipv6 addr is 64:ff9b::d83a:c704 0”. it is ok that “216.58.199.4” is converted to “64:ff9b::d83a:c704”, but it is strange that the service port of 80 become 0. 输出是“ipv6 addr is 64:ff9b :: d83a:c704 0”。可以将“216.58.199.4”转换为“64:ff9b ::

本文由我司收集整编,推荐下载,如有疑问,请与我司联系

d83a:c704”,但奇怪的是 80 的服务端口变为 0。 Is anyone can explain it ? 有人能解释一下吗? 3

This is a bug impacting iOS 9 and Mac OS X 10.11. It has been fixed in iOS 10 and macOS 10.12, but here are workarounds that can be used to support devices running iOS 9

and Mac OS X 10.11:

这是影响 iOS 9 和 Mac OS X 10.11 的错误。它已在 iOS 10 和 macOS 10.12 中修 复,但这里有一些解决方法可用于支持运行 iOS 9 和 Mac OS X 10.11 的设备:

Workaround 1: Use a service name If you’re using a well known or registered port number, you can pass in the service name instead of the port number as a string. In this

example, simply replace “80” with “http”:

如果您使用的是已知或注册的端口号,则可以将服务名称而不是端口号作为字符串 传递。在此示例中,只需将“80”替换为“http”:

if (getaddrinfo(“216.58.199.4”, “http”, hints, res)) { Since the bug is limited to the number parsing, using service names still works. The full list of known services can be found in /etc/services.

由于错误仅限于数字解析,因此使用服务名称仍然有效。可以在/ etc / services 中找 到已知服务的完整列表。

Workaround 2: Manually write the port into the sockaddr If you don’t use a port number from /etc/services, you can manually write the correct port number into your struct sockaddr. If you do this, it’s important to:

如果不使用/ etc / services 中的端口号,则可以手动将正确的端口号写入 struct sockaddr。如果你这样做,重要的是:

Only write if the port is zero to make sure this code deactivates once the bug is fixed. 仅在端口为零时才写入,以确保在修复错误后此代码停用。

本文由我司收集整编,推荐下载,如有疑问,请与我司联系

Remember that the sockaddr uses network byte order, so you need to use htons() to

convert your port number. 请记住,sockaddr 使用网络字节顺序,因此您需要使用 htons() 来转换端口号。

Here is the workaround applied to your example: 以下是适用于您的示例的变通方法:

for (addr = res;addr;addr = addr- ai_next) if (addr- ai_family == AF_INET6) sockaddr_v6 = (struct sockaddr_in6 *)addr- ai_addr; // START WORKAROUND if (sockaddr_v6- sin6_port == 0) sockaddr_v6- sin6_port = htons(80); // END WORKAROUND printf(“ipv6 addr is %s %d)\\n”, inet_ntop(AF_INET6, sockaddr_v6- sin6_addr, buffer, sizeof(buffer)), ntohs(sockaddr_v6- sin6_port));tips:感谢大家的阅读, 本文由我司收集整编。仅供参阅!

【IT专家】使用getaddrinfo将IPv4地址转换为IPv6地址时,会丢失服务端口

本文由我司收集整编,推荐下载,如有疑问,请与我司联系使用getaddrinfo将IPv4地址转换为IPv6地址时,会丢失服务端口使用getaddrinfo将IPv4地址转换为IPv6地址时,会丢失服务端口[英]ServiceportismissedwhenusinggetaddrinfotoconvertIPv4ad
推荐度:
点击下载文档文档为doc格式
14gsq2nqwa52amw9lhr375cln2z0an008ek
领取福利

微信扫码领取福利

微信扫码分享