我不明白为什么getaddrinfo
没有返回有效的IPv6地址。
在我的系统上,下面的代码正在打印22:B8:00:00:00:00:00:00:00:00:00:00:00:00
,但我预计某处会01
,因为localhost
应该解析为::1
。
同时,sa_data
只有 14 个字节,而 IPv6 地址是 16 个字节,所以似乎最后几个字节总是被砍掉,函数无法返回 IPv6 地址?
有人可以解释一下发生了什么吗?我应该如何在IPv6中使用此功能?
#include <stdio.h>
#include <WinSock2.h>
#include <WS2TCPIP.h>
#pragma comment(lib, "WS2_32")
int main(int argc, char *argv[])
{
WSADATA wsadata;
WSAStartup(0x0002, &wsadata);
addrinfo addr_hints = { 0, PF_INET6, SOCK_DGRAM, IPPROTO_UDP }, *addrs_out;
getaddrinfo("localhost", "8888", &addr_hints, &addrs_out);
fprintf(stderr,
"%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02Xn",
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 0]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 1]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 2]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 3]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 4]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 5]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 6]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 7]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 8]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[ 9]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[10]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[11]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[12]),
static_cast<unsigned char>(addrs_out->ai_addr->sa_data[13]));
freeaddrinfo(addrs_out);
return 0;
}
sockaddr
结构定义以供参考:
struct sockaddr {
ushort sa_family;
char sa_data[14];
};
struct sockaddr_in6 {
short sin6_family;
u_short sin6_port;
u_long sin6_flowinfo;
struct in6_addr sin6_addr;
u_long sin6_scope_id;
};
当ai_family == AF_INET6
ai_addr
实际上指向一个struct sockaddr_in6
.您正在打印的前几个字节是sin6_port
和sin6_flowinfo
。IPv6地址紧随其后。
编辑以添加:
您可以直接将ai_addr
与bind()
和getnameinfo()
等功能一起使用。通常不需要深入了解结构定义的详细信息。例如,我会将getnameinfo()
与NI_NUMERICHOST
一起使用来获取可打印的地址。
>sockaddr并没有被严格解释为指向 袜子结构式。结构在 不同地址系列的上下文。
因此,我们需要首先检查addrinfo
sockaddr
或ai_family
sa_family
(在此基础上它必须相等(,并基于此需要"重新解释"从sockaddr
(这就像void*
指针(到真实结构的指针。 说使用联合
addrinfo *addrs_out, *addr;
if (getaddrinfo("localhost", "8888", 0, &addrs_out) == NOERROR)
{
addr = addrs_out;
CHAR buf[256], *sz, srv[128];
ULONG n;
PUCHAR Byte;
do
{
union {
sockaddr* ai_addr;
SOCKADDR_IN* pa;
SOCKADDR_IN6* pa6;
};
ai_addr = addr->ai_addr;
if (addr->ai_family != ai_addr->sa_family)
{
__debugbreak();
}
switch (addr->ai_family)
{
case AF_INET6:
Byte = pa6->sin6_addr.u.Byte, n = RTL_NUMBER_OF(pa6->sin6_addr.u.Byte), sz = buf;
do
{
sz += sprintf(sz, "%02X:", *Byte++);
} while (--n);
sz[-1] = 0;
DbgPrint("AF_INET6: %sn", buf);
break;
case AF_INET:
if (0 <= RtlIpv4AddressToStringExA(&pa->sin_addr.S_un.S_addr, pa->sin_port, buf, &(n = RTL_NUMBER_OF(buf))))
{
DbgPrint("AF_INET: %sn", buf);
}
break;
}
// alt print
if (getnameinfo(ai_addr, (socklen_t)addr->ai_addrlen, buf, RTL_NUMBER_OF(buf), srv, RTL_NUMBER_OF(srv), NI_NUMERICHOST ) == NOERROR)
{
DbgPrint("%s:%sn", buf, srv);
}
} while (addr = addr->ai_next);
freeaddrinfo(addrs_out);
}