Search code examples
macosunixdnsposixgetaddrinfo

getaddrinfo error: nodename nor servname provided, or not known


When I tryed to get IP address for DNS name with getaddrinfo I got the following error

getaddrinfo error: nodename nor servname provided, or not known

What does this error means? Is it possible that getaddrinfo can't resole the DNS name? But nslookup works fine for this address. I tryed to call getaddrinfo without port, and without AI_NUMERICSERV flag with the same result.

What is wrong with my call? OS X 10.11 plaftorm.

This is the sample code

struct addrinfo hints, *servinfo;
int rv;

printf("hostname: %s port: %s ", hostname, port);

memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC; // use AF_INET6 to force IPv6
hints.ai_flags=AI_DEFAULT|AI_NUMERICSERV;
if ( (rv = getaddrinfo( hostname , port , &hints , &servinfo)) != 0)
{

}

Solution

  • The problem was connected with sandboxing mechanism on Mac OS X . I forget about the fact that my application is sandboxed. Make sure that you have the following lines in your entitlements file.

    <key>com.apple.security.app-sandbox</key>
    <true/>
    <key>com.apple.security.network.client</key>
    <true/>
    <key>com.apple.security.network.server</key>
    <true/>
    

    If you application is not sandboxed getaddrinfo() works as expected.