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);
What you get back#
A three-element list: error string (empty on success), host, and
service. On error, host and service come back 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#
Input shorter than 2 bytes returns the error string
"Invalid sockaddr"and twoundefs.Internally the packed sockaddr is capped at 128 bytes; this is enough for every standard family.
Differences from upstream#
Fully compatible with upstream Socket.
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.