extern "C" int RTinet_lookup( const char * host,RTinet_address *address,RTinet_port port)
{
struct addrinfo,*res;
int errcode;
char addrstr[INET6_ADDRSTRLEN];
memset (&hints, 0, sizeof (hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags |= AI_CANONNAME;
errcode = getaddrinfo (host, NULL , &hints, &res);
if (errcode != 0)
{
INFO("getaddrinfo");
return -1;
}
INFO(STR("Host: %s", host));
if(res == NULL)
{
INFO(STR("NULL VALUE OF RES"));
}
else
{INFO(STR("IN ELSE PART"));
while (res)
{
INFO(STR("Inside while condition with res "));
switch (res->ai_family)
{
case AF_INET:
inet_ntop(AF_INET, &((str开发者_运维问答uct sockaddr_in *) res->ai_addr)->sin_addr,addrstr, INET_ADDRSTRLEN);
break;
case AF_INET6:
inet_ntop(AF_INET6, &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr ,addrstr, INET6_ADDRSTRLEN);
break;
}
res = res->ai_next;
}
}
INFO(STR("IPv%d address: %s ", res->ai_family == AF_INET6 ? 6 : 4, addrstr));
return 1;
}
Here I am passing "::1"
as the host address, which is a IPV6 form. But in trace I am getting
Host: ::1
IPv4 address: ::1
Why this is taking IPv4 ??? It should have taken IPv6 as the address family should be AF_INET6
in this case
res
is pointing to NULL and hence your last print statement is invalid as you assume ai_family != AF_INET6
must be AF_INET
.
edit: To explain further here is your code problem in point, with the non-relevant points removed:
while (res)
{
res = res->ai_next;
}
INFO(STR("IPv%d address: %s ", res->ai_family == AF_INET6 ? 6 : 4, addrstr));
Ask yourself what is the value of res
when the while
loop as finished.
精彩评论