最近遇到一个ipv6的问题,问题描述如下:
#ping6 ::1
unknown host
跟踪代码,发现函数返回gai=-1
gai = getaddrinfo(target, NULL, &hints, &ai);
进一步使用函数检查错误类型
gai_strerror(gai);
得知ai_flagsd值设置错误。
把 hints.ai_flags = AI_IDN;
改为 hints.ai_flags = AI_PASSIVE; 即可。
好了现在可以正常使用了。
-sh-3.2# ./ping6 -I eth0 fe80::280:45ff:fe51:1000 -c 3
PING fe80::280:45ff:fe51:1000(fe80::280:45ff:fe51:1000) from fe80::280:45ff:fe51:1000 eth0: 56 data bytes
64 bytes from fe80::280:45ff:fe51:1000: icmp_seq=1 ttl=64 time=0.572 ms
64 bytes from fe80::280:45ff:fe51:1000: icmp_seq=2 ttl=64 time=0.533 ms
64 bytes from fe80::280:45ff:fe51:1000: icmp_seq=3 ttl=64 time=0.530 ms
--- fe80::280:45ff:fe51:1000 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.530/0.545/0.572/0.019 ms
附 ai_flags 常用值
AI_PASSIVE
Setting the AI_PASSIVE flag indicates the caller intends to use the returned socket address structure in a call to the bind function. When the AI_PASSIVE flag is set and pNodeName is a NULL pointer, the IP address portion of the socket address structure is set to INADDR_ANY for IPv4 addresses and IN6ADDR_ANY_INIT for IPv6 addresses.
When the AI_PASSIVE flag is not set, the returned socket address structure is ready for a call to the connect function for a connection-oriented protocol, or ready for a call to either the connect, sendto, or send functions for a connectionless protocol. If the pNodeName parameter is a NULL pointer in this case, the IP address portion of the socket address structure is set to the loopback address.
AI_CANONNAME
If neither AI_CANONNAME nor AI_NUMERICHOST is used, the GetAddrInfoW function attempts resolution. If a literal string is passed GetAddrInfoW attempts to convert the string, and if a host name is passed the GetAddrInfoW function attempts to resolve the name to an address or multiple addresses.
When the AI_CANONNAME bit is set and the GetAddrInfoW function returns success, the ai_canonname member in the ppResult parameter points to a NULL-terminated string that contains the canonical name of the specified node.
The GetAddrInfoW function can return success when the AI_CANONNAME flag is set, yet the ai_canonname member in the associated addrinfoW structure is NULL. Therefore, the recommended use of the AI_CANONNAME flag includes testing whether the ai_canonname member in the associated addrinfoW structure is NULL.
AI_NUMERICHOST
When the AI_NUMERICHOST bit is set, the pNodeName parameter must contain a non-NULL numeric host address string, otherwise the EAI_NONAME error is returned. This flag prevents a name resolution service from being called.
AI_NUMERICSERV
When the AI_NUMERICSERV bit is set, the pServiceName parameter must contain a non-NULL numeric port number, otherwise the EAI_NONAME error is returned. This flag prevents a name resolution service from being called.
The AI_NUMERICSERV flag is defined on Windows SDK for Windows??Vista and later. The AI_NUMERICSERV flag is not supported by Microsoft providers.
AI_ALL
If the AI_ALL bit is set, a request is made for IPv6 addresses and IPv4 addressses with AI_V4MAPPED.
The AI_ALL flag is defined on the Windows SDK for Windows??Vista and later. The AI_ALL flag is supported on Windows??Vista and later.
AI_ADDRCONFIG
If the AI_ADDRCONFIG bit is set, GetAddrInfoW will resolve only if a global address is configured. If AI_ADDRCONFIG flag is specified, IPv4 addresses shall be returned only if an IPv4 address is configured on the local system, and IPv6 addresses shall be returned only if an IPv6 address is configured on the local system. The IPv4 or IPv6 loopback address is not considered a valid global address.
The AI_ADDRCONFIG flag is defined on the Windows SDK for Windows??Vista and later. The AI_ADDRCONFIG flag is supported on Windows??Vista and later.
AI_V4MAPPED
If the AI_V4MAPPED bit is set and a request for IPv6 addresses fails, a name service request is made for IPv4 addresses and these addresses are converted to IPv4-mapped IPv6 address format.
The AI_V4MAPPED flag is defined on the Windows SDK for Windows??Vista and later. The AI_V4MAPPED flag is supported on Windows??Vista and later.
AI_NON_AUTHORITATIVE
If the AI_NON_AUTHORITATIVE bit is set, the NS_EMAIL namespace provider returns both authoritative and non-authoritative results. If the AI_NON_AUTHORITATIVE bit is not set, the NS_EMAIL namespace provider returns only authoritative results.
The AI_NON_AUTHORITATIVE flag is defined on the Windows SDK for Windows??Vista and later. The AI_NON_AUTHORITATIVE flag is supported on Windows??Vista and later and applies only to the NS_EMAIL namespace.
AI_SECURE
If the AI_SECURE bit is set, the NS_EMAIL namespace provider will return results that were obtained with enhanced security to minimize possible spoofing.
The AI_SECURE flag is defined on the Windows SDK for Windows??Vista and later. The AI_SECURE flag is supported on Windows??Vista and later and applies only to the NS_EMAIL namespace.
AI_RETURN_PREFERRED_NAMES
If the AI_RETURN_PREFERRED_NAMES is set, then no name should be provided in the pNodeName parameter. The NS_EMAIL namespace provider will return preferred names for publication.
The AI_RETURN_PREFERRED_NAMES flag is defined on the Windows SDK for Windows??Vista and later. The AI_RETURN_PREFERRED_NAMES flag is supported on Windows??Vista and later and applies only to the NS_EMAIL namespace.
AI_FQDN
If the AI_FQDN is set and a flat name (single label) is specified, GetAddrInfoW will return the fully qualified domain name that the name eventually resolved to. The fully qualified domain name is returned in the ai_canonname member in the associated addrinfoW structure. This is different than AI_CANONNAME bit flag that returns the canonical name registered in DNS which may be different than the fully qualified domain name that the flat name resolved to. Only one of the AI_FQDN and AI_CANONNAME bits can be set. The GetAddrInfoW function will fail if both flags are present with EAI_BADFLAGS.
The AI_FQDN flag is defined on the Windows SDK for Windows??7 and later. The AI_FQDN flag is supported on Windows??7 and later.
AI_FILESERVER
If the AI_FILESERVER is set, this is a hint to the namespace provider that the hostname being queried is being used in file share scenario. The namespace provider may ignore this hint.
The AI_FILESERVER flag is defined on the Windows SDK for Windows??7 and later. The AI_FILESERVER flag is supported on Windows??7 and later.
AI_DISABLE_IDN_ENCODING
If the AI_DISABLE_IDN_ENCODING is set, this disables the automatic International Domain Name encoding using Punycode in the name resolution functions called by the GetAddrInfoW function.
Windows Developer Preview:????The AI_DISABLE_IDN_ENCODING flag is defined on the Windows SDK for Windows Developer Preview and later. The AI_DISABLE_IDN_ENCODING flag is supported on Windows Developer Preview and later.