|
NAMEgetnameinfo —
socket address structure to hostname and service name
SYNOPSIS#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
int
DESCRIPTIONThegetnameinfo () function is used to convert a
sockaddr structure to a pair of host name and service
strings. It is a replacement for and provides more flexibility than the
gethostbyaddr(3)
and
getservbyport(3)
functions and is the converse of the
getaddrinfo(3)
function.
If a link-layer address or UNIX-domain address is passed to
The The host and service names associated with
sa are stored in host and
serv which have length parameters
hostlen and servlen. The maximum
value for hostlen is
The flags argument is formed by OR'ing the following values:
This implementation allows numeric IPv6 address notation with
scope identifier, as documented in chapter 11 of RFC 4007. IPv6 link-local
address will appear as a string like
“ RETURN VALUESgetnameinfo () returns zero on success or one of the
error codes listed in
gai_strerror(3)
if an error occurs.
EXAMPLESThe following code tries to get a numeric host name, and service name, for a given socket address. Observe that there is no hardcoded reference to a particular address family.struct sockaddr *sa; /* input */ char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) { errx(1, "could not get numeric hostname"); /* NOTREACHED */ } printf("host=%s, serv=%s\n", hbuf, sbuf); The following version checks if the socket address has a reverse address mapping: struct sockaddr *sa; /* input */ char hbuf[NI_MAXHOST]; if (getnameinfo(sa, sa->sa_len, hbuf, sizeof(hbuf), NULL, 0, NI_NAMEREQD)) { errx(1, "could not resolve hostname"); /* NOTREACHED */ } printf("host=%s\n", hbuf); SEE ALSOgai_strerror(3), getaddrinfo(3), gethostbyaddr(3), getservbyport(3), inet_ntop(3), link_ntoa(3), resolver(3), inet(4), inet6(4), unix(4), hosts(5), resolv.conf(5), services(5), hostname(7), named(8)R. Gilligan, S. Thomson, J. Bound, J. McCann, and W. Stevens, Basic Socket Interface Extensions for IPv6, RFC 3493, February 2003. S. Deering, B. Haberman, T. Jinmei, E. Nordmark, and B. Zill, IPv6 Scoped Address Architecture, RFC 4007, March 2005. Craig Metz, Protocol Independence Using the Sockets API, Proceedings of the freenix track: 2000 USENIX annual technical conference, June 2000. STANDARDSThegetnameinfo () function is defined by the
IEEE Std 1003.1-2004 (“POSIX.1”)
specification and documented in RFC 3493, “Basic Socket Interface
Extensions for IPv6”.
CAVEATSgetnameinfo () can return both numeric and FQDN forms of
the address specified in sa. There is no return value
that indicates whether the string returned in host is a
result of binary to numeric-text translation (like
inet_ntop(3)),
or is the result of a DNS reverse lookup. Because of this, malicious parties
could set up a PTR record as follows:
1.0.0.127.in-addr.arpa. IN PTR 10.1.1.1 and trick the caller of To prevent such attacks, the use of
struct sockaddr *sa; socklen_t salen; char addr[NI_MAXHOST]; struct addrinfo hints, *res; int error; error = getnameinfo(sa, salen, addr, sizeof(addr), NULL, 0, NI_NAMEREQD); if (error == 0) { memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; /*dummy*/ hints.ai_flags = AI_NUMERICHOST; if (getaddrinfo(addr, "0", &hints, &res) == 0) { /* malicious PTR record */ freeaddrinfo(res); printf("bogus PTR record\n"); return -1; } /* addr is FQDN as a result of PTR lookup */ } else { /* addr is numeric string */ error = getnameinfo(sa, salen, addr, sizeof(addr), NULL, 0, NI_NUMERICHOST); }
Visit the GSP FreeBSD Man Page Interface. |