inet_ntoa#

Turn a 4-byte packed IPv4 address back into a dotted-quad string.

Synopsis#

my $str = inet_ntoa($packed_ipv4);

What you get back#

A string like "192.0.2.1", or undef if the input is shorter than 4 bytes.

Examples#

print inet_ntoa(INADDR_LOOPBACK);    # 127.0.0.1
my ($port, $ip) = unpack_sockaddr_in($peer);
printf "%s:%d\n", inet_ntoa($ip), $port;

Edge cases#

  • Input shorter than 4 bytes returns undef.

  • Input longer than 4 bytes: the extra bytes are ignored.

Differences from upstream#

Fully compatible with upstream Socket.

See also#

  • inet_aton — reverse direction, string to packed bytes.

  • inet_ntop — same idea for IPv4 or IPv6, parameterised by family.

  • unpack_sockaddr_in — usually the producer of the packed address.