Commit a654219f authored by jan.koester's avatar jan.koester
Browse files

tcp/udp: empty bind address listens dual-stack on ::

getaddrinfo(NULL, AI_PASSIVE) lists 0.0.0.0 first (glibc and systempp),
so a server with no bind address was IPv4-only. Prefer the IPv6 wildcard,
which IPV6_V6ONLY=0 already makes dual-stack; fall back to the plain lookup
when the host has no IPv6. OpenBSD keeps the old behaviour (no IPv4-mapped
sockets there).

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent edf1714a
Loading
Loading
Loading
Loading
+26 −1
Changes for src/posix/tcp.cpp: 26 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -169,7 +169,32 @@ tcp::tcp(const std::string& addr, int port, int maxconnections, int sockopts)
    char serv[32];
    std::snprintf(serv, sizeof(serv), "%d", port);

    int gai = ::getaddrinfo(addr.empty() ? nullptr : addr.c_str(), serv, &hints, &result);
    // No bind address: prefer the IPv6 wildcard "::" (IPV6_V6ONLY is cleared
    // below), so one listener takes IPv4 and IPv6 alike. getaddrinfo(NULL,
    // AI_PASSIVE) commonly lists 0.0.0.0 first, which would leave the server
    // IPv4-only. Hosts without IPv6 support fall back to the plain lookup.
    // OpenBSD has no IPv4-mapped IPv6 sockets (IPV6_V6ONLY is always on),
    // so there an IPv6 wildcard would drop IPv4; keep the plain lookup.
    int gai = -1;
#ifndef __OpenBSD__
    if (addr.empty()) {
        hints.ai_family = AF_INET6;
        gai = ::getaddrinfo(nullptr, serv, &hints, &result);
        if (gai == 0) {
            int probe = ::socket(AF_INET6, hints.ai_socktype, 0);
            if (probe < 0) {
                ::freeaddrinfo(result);
                result = nullptr;
                gai = -1;
            } else {
                ::close(probe);
            }
        }
        hints.ai_family = AF_UNSPEC;
    }
#endif
    if (gai != 0)
        gai = ::getaddrinfo(addr.empty() ? nullptr : addr.c_str(), serv, &hints, &result);
    if (gai != 0) {
        exception[NetException::Critical] << "getaddrinfo: " << gai_strerror(gai);
        throw exception;
+26 −1
Changes for src/posix/udp.cpp: 26 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -106,7 +106,32 @@ udp::udp(const std::string& addr, int port, int maxconnections, int sockopts)
    char serv[32];
    std::snprintf(serv, sizeof(serv), "%d", port);

    int gai = ::getaddrinfo(addr.empty() ? nullptr : addr.c_str(), serv, &hints, &result);
    // No bind address: prefer the IPv6 wildcard "::" (IPV6_V6ONLY is cleared
    // below), so one listener takes IPv4 and IPv6 alike. getaddrinfo(NULL,
    // AI_PASSIVE) commonly lists 0.0.0.0 first, which would leave the server
    // IPv4-only. Hosts without IPv6 support fall back to the plain lookup.
    // OpenBSD has no IPv4-mapped IPv6 sockets (IPV6_V6ONLY is always on),
    // so there an IPv6 wildcard would drop IPv4; keep the plain lookup.
    int gai = -1;
#ifndef __OpenBSD__
    if (addr.empty()) {
        hints.ai_family = AF_INET6;
        gai = ::getaddrinfo(nullptr, serv, &hints, &result);
        if (gai == 0) {
            int probe = ::socket(AF_INET6, hints.ai_socktype, 0);
            if (probe < 0) {
                ::freeaddrinfo(result);
                result = nullptr;
                gai = -1;
            } else {
                ::close(probe);
            }
        }
        hints.ai_family = AF_UNSPEC;
    }
#endif
    if (gai != 0)
        gai = ::getaddrinfo(addr.empty() ? nullptr : addr.c_str(), serv, &hints, &result);
    if (gai != 0) {
        NetException e;
        e[NetException::Critical] << "udp getaddrinfo: " << gai_strerror(gai);