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