[ds6-devel] gethostbyaddr() problem

Chris Leishman chris at leishman.org
Wed May 28 23:48:32 CEST 2003


On Wednesday, May 28, 2003, at 10:30 PM, Kirk Bollinger wrote:
<snip>
> now, I have one more thing I can't get to work. I want to print out the
> addresses in ascii. I know that inet_ntop() is discouraged but it 
> should
> work.
>
> here's my code:
> if (res->ai_family == AF_INET6)
>       {
>         printf("\nAddress family: AF_INET6");
>         //printf("\nThe address is: %s", ((struct sockaddr_in6
> *)res->ai_addr)->sin6_addr);
>
>         printf("\nnet_byte_order in hex is: %x", ((struct sockaddr_in6
> *)res->ai_addr)->sin6_addr);
>         memset(&sin6, 0, sizeof(struct sockaddr_in6));
>         memcpy(&sin6, (struct sockaddr_in6 *)res->ai_addr, 
> sizeof(struct
> sockaddr_in6));
>         inet_ntop(AF_INET6, &sin6.sin6_addr, buf, 16);
>
>         printf("\nAddress is: %s", buf);


The function your looking for is 'getnameinfo'.  If you want only the 
numeric name (IP address), then give it the AI_NUMERICHOST and/or the 
AI_NUMERICSERV flags.

With the getaddrinfo family of functions you should never need to 
actually do any manipulations of the actual sockets, including doing 
any switch like statements on the family (unless your doing really 
esoteric stuff like comparing sockets).

So your code would become something like:

char hbuf[NI_MAXHOST+1];
char sbuf[NI_MAXSERV+1];

err = getnameinfo(socket, socklen, hbuf, sizeof(hbuf), sbuf, 
sizeof(sbuf), 0);
/* handle err */

printf("address is %s %s\n", hbuf, sbuf);

err = getnameinfo(socket, socklen, buf, sizeof(hbuf), sbuf, 
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
/* handle err */

printf("ip address is %s %s\n", hbuf, sbuf);


If your trying to print out the results of getaddrinfo, then use 
'res->ai_addr' for socket and 'res->ai_addrlen' for socklen.

Regards,
Chris



More information about the ds6-devel mailing list