getnameinfo#
Turn a packed sockaddr into a host name and service name, with optional flags to force numeric output or demand a successful DNS lookup.
Synopsis#
my ($err, $host, $service) = getnameinfo($sockaddr);
my ($err, $host, $service) = getnameinfo($sockaddr, $flags);
my ($err, $host, $service) = getnameinfo($sockaddr, $flags, $xflags);
What you get back#
($err, $host, $service); on error, $err alone. $err is the same dualvar getaddrinfo returns. $xflags takes NIx_NOHOST and NIx_NOSERV, which skip that half of the lookup and return it as undef.
Useful flag bits:
NI_NUMERICHOST- skip DNS, return a printable address.NI_NUMERICSERV- return the port number as a decimal string.NI_NAMEREQD- fail instead of falling back to numeric host.NI_NOFQDN- local hosts keep their short name.NI_DGRAM- look up the service as UDP rather than TCP.
Examples#
my ($err, $host, $svc) = getnameinfo(getpeername($sock));
die "reverse lookup: $err\n" if $err;
print "peer: $host:$svc\n";
# Numeric formatting only - no DNS, no /etc/services.
my ($err, $host, $svc) = getnameinfo($sockaddr,
NI_NUMERICHOST | NI_NUMERICSERV);
Edge cases#
A sockaddr that is not a string croaks “addr is not a string”.
A malformed sockaddr is reported through
$err(EAI_FAMILY).
See also#
getaddrinfo- reverse direction, name to sockaddr.inet_ntop- format a raw address without a resolver.unpack_sockaddr_in,unpack_sockaddr_in6- when you need the numeric port and address without any lookup.