C/C++ getnameinfo ai_family not supported
<p>I use a library that does some nework stuff and when a client connects, this library provides a "struct sockaddr *" which holds the client socket. I simply wanted to extract the IP and port and I do it currently this way:</p>

<pre><code>#include &lt;sys/socket.h&gt;
#include &lt;netdb.h&gt;

const std::string Client::prepareIPandPort(struct sockaddr *hostaddr) {
    assert(hostaddr != nullptr);

    std::string ipport;
    char clienthost[NI_MAXHOST];
    char clientport[NI_MAXSERV];
    int result = getnameinfo(hostaddr, sizeof(*hostaddr),
                             clienthost, sizeof(clienthost),
                             clientport, sizeof(clientport),
                             NI_NUMERICHOST | NI_NUMERICSERV);

    if (result != 0) {
        std::cerr &lt;&lt; "Error: " &lt;&lt; gai_strerror(result) &lt;&lt; std::endl;
        ipport = "unknown";
    } else {
        switch (hostaddr-&gt;sa_family) {
            case AF_INET:
                ipport = std::string {clienthost} + ":"
                         + std::string {clientport};
                break;
            case AF_INET6:
                ipport = "[" + std::string {clienthost} + "]:"
                         + std::string {clientport};
                break;
            default:
                ipport = "unknown";
        }
    }

    return ipport;
}
</code></pre>

<p>When using IPv4 on my Mac, it works. If I use this application on my Gentoo Linux server with full IPv6 support, I only get:</p>

<p>Error: ai_family not supported</p>

<p>The connecting client has AAAA and IP6 records in place.</p>

<p>I added some couts and printed hostaddr->sa_family, which returns 10, which is AF_INET6.</p>

<p>Why does this not work? :-)</p>