0
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, &((struct 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;
}
在这里我通过"::1"
作为主机地址,这是一个IPV6形式。但在跟踪我越来越没有得到想要的输出
Host: ::1
IPv4 address: ::1
为什么这是采取IPv4?它应该采取的IPv6的地址族应AF_INET6
在这种情况下
@ Steve-o ...你能否提出为什么它指向null以及如何纠正它,以便获得所需的输出。谢谢 – 2011-05-13 05:05:48
@ Steve-o ...但是因为我保留了一个条件,如果res == NULL,那么INFO(STR(“NULL VALUE OF RES”)); 应该打印出来,但是这条语句不会出现在日志中。那么你对此有什么建议?谢谢 – 2011-05-13 05:15:51
@Kundan,你在'res'上有一个'while'循环,直到它是'NULL',然后你试着读'res-> ai_family'。 – 2011-05-13 12:51:50