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.

תקציר#

my ($err, $host, $service) = getnameinfo($sockaddr);
my ($err, $host, $service) = getnameinfo($sockaddr, $flags);

מה מוחזר#

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.

דוגמאות#

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);

מקרי קצה#

  • Input shorter than 2 bytes returns the error string "Invalid sockaddr" and two undefs.

  • Internally the packed sockaddr is capped at 128 bytes; this is enough for every standard family.

הבדלים מן ה-upstream#

Fully compatible with upstream Socket.

ראו גם#

  • 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.