IPV6 套接字连接()returns EADDRNOTAVAIL

IPV6 socket connect() returns EADDRNOTAVAIL

我正在尝试通过 Appstore 的 IPv6 支持,但遇到了一些问题。

我关注了 Apple's Guild Supporting IPv6 DNS64/NAT64 Networks,我的代码:

const char sAddr[] = "123.123.123.123"; //my IPv4 gamesvr, using boost asio
const char sPort[] = "9899";
const char *cause = NULL;
int sock_;
struct addrinfo hints, *res, *res0;

memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_DEFAULT;

int error = getaddrinfo(sAddr, sPort, &hints, &res0);
if (error) {
    return false;
}
sock_ = -1;
for (res = res0; res; res = res->ai_next) {
    sock_ = socket(res->ai_family, res->ai_socktype,res->ai_protocol);
    if (sock_ < 0) {
        cause = "socket";
        continue;
    }
    if (::connect(sock_, res->ai_addr, res->ai_addrlen) < 0) {
        error = errno; // error = 49, EADDRNOTAVAIL
        break;
    }
    break;  /* okay we got one */
}
if (sock_ < 0) {
    freeaddrinfo(res0);
    return false;
}
freeaddrinfo(res0);
return true;

在IPv4 WIFI下,我的游戏运行很好

当切换到 IPv6-ONLY WIFI(运行 by mac )时,使用 CURL 运行s 的功能很好,我可以获取网页内容,但是 socket connect() 失败了errno 49(EADDRNOTAVAIL), res->ai_family = 30 (AF_INET6)

有哪位帮帮我,非常感谢!

尝试将 error = errno; 之后的 break 更改为 continue。如果 connect 失败,您应该尝试下一个地址。

解析器可能仍会为您提供原始 IPv4 结果 NAT64 的合成 IPv6 结果。如果您收到的错误是由于尝试在纯 IPv6 网络上创建 IPv4 套接字,我不会感到惊讶。

我解决了,虽然我不知道为什么...

令人惊讶的是,当我尝试使用域名而不是数字 ip 地址时,连接正常!

const char sAddr[] = "123.123.123.123"; --> const char sAddr[] = "gs1.mydomain.com";

我的游戏在 IPv6-ONLY WIFI 下运行正常!


现在我很困惑为什么 Apple 将这些放入 Supporting IPv6 DNS64/NAT64 Networks :

uint8_t ipv4[4] = {192, 0, 2, 1};
struct addrinfo hints, *res, *res0;
int error, s;
const char *cause = NULL;

char ipv4_str_buf[INET_ADDRSTRLEN] = { 0 };
const char *ipv4_str = inet_ntop(AF_INET, &ipv4, ipv4_str_buf, sizeof(ipv4_str_buf));

memset(&hints, 0, sizeof(hints));
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_DEFAULT;
error = getaddrinfo(ipv4_str, "http", &hints, &res0);

Listing 10-1 shows how to resolve an IPv4 literal using getaddrinfo. Assuming you have an IPv4 address stored in memory as four bytes (such as {192, 0, 2, 1}), this example code converts it to a string (such as "192.0.2.1"), uses getaddrinfo to synthesize an IPv6 address (such as a struct sockaddr_in6 containing the IPv6 address "64:ff9b::192.0.2.1") and tries to connect to that IPv6 address.

-_-||