1

I have following snippet of code, which is just trying to get address family of a server address. srv_addr is passed from other places to this piece of code.

    struct addrinfo dinfo, *dptr = NULL;
    int error;

    memset(&dinfo, 0, sizeof(dinfo));
    dinfo.ai_family = AF_UNSPEC;

    error = getaddrinfo(srv_addr, NULL, &dinfo, &dptr);

    if (error) {
        printf("error in validating server address: %s",
                gai_strerror(error));
       return -1;
    }

srv_addr can be hostname(string) or IPv4 or IPv6 address.

code works fine, if srv_addr is numeric address.

In case if srv_addr is hostname, sometimes getaddrinfo() fails although ping works fine at the same point of time.

Error: error in validating server address: hostname nor servname provided, or not known. error code: 8 (EAI_NONAME).

I am sure that hostname is valid, what can be the scenarios where getaddrinfo() may fail, even if hostname is valid and ping-able.

I know that getaddrinfo() returns list of addrinfo structures and hence there can be multiple address families to be returned. But I am interested only in failure case here.

Thanks

macfij
  • 3,093
  • 1
  • 19
  • 24
Ram
  • 1,153
  • 4
  • 16
  • 34
  • can you provide some more info? print out ``srv_addr`` before calling ``getaddrinfo``? what exactly is your ``srv_addr``? – macfij Jun 26 '14 at 14:00
  • As I said, it is just a hostname of an ubuntu server. which is ping-able from my machine where above program runs. – Ram Jun 26 '14 at 15:11
  • what i meant was that maybe ``srv_addr`` might differ from a hostname that you're pinging – macfij Jun 26 '14 at 15:30
  • aah. Nope. I am sure about that. – Ram Jun 26 '14 at 15:40

0 Answers0