extern "C"
int RTinet_lookup( const char * host, char * address, unsigned int port)
{
struct addrinfo hints, *res;
int errcode;
char addrstr[INET6_ADDRSTRLEN];
void *ptr;
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, "port", &hints, &res);
/*if (errcode != 0)
{
perror ("getaddrinfo");
return -1;
}*/
INFO(STR("Host: %s", host));
while (res)
{
inet_ntop (res->ai_family, res->ai_addr->sa_data, addrstr,INET6_ADDRSTRLEN);
INFO(STR("Inside while condition with res "));
switch (res->ai_family)
{
case AF_INET:
ptr = &((struct sockaddr_in *) res->ai_addr)->sin_addr;
break;
case AF_INET6:
ptr = &((struct sockaddr_in6 *) res->ai_addr)->sin6_addr;
break;
}
inet_ntop (res->ai_family, ptr, addrstr,INET6_ADDRSTRLEN);
INFO(STR("IPv%d address: %s (%s)\n", res->ai_family == AF_INET6 ? 6 : 4,
addrstr, res->ai_canonname));
res = res->ai_next;
}INFO(STR("IPv%d address: %s (%s)\n", res->ai_fa开发者_运维技巧mily == AF_INET6 ? 6 : 4,
addrstr, res->ai_canonname));
return 1;
}
In this code, it's not going in while
part as res
is NULL
maybe. Can anyone point out the error here or correct the way getaddrinfo
is used?
The one error I see in this code is the "port"
parameter being passed to getaddrinfo
. this is probably not what was intended, and will most likely fail unless your system does have a service named "port" defined.
Either pass NULL
as the service name and override the port in the resultant address, or pass a string containing the name or decimal representation of the port. (in the latter case, you may also want to set the AI_NUMERICSERV
flag in the hints).
As a side note, your code isn't calling freeaddrinfo
, so is leaking memory.
精彩评论