inet_pton#

Parse an address string into its packed binary form for either IPv4 or IPv6, selected by the first argument.

Synopsis#

my $v4 = inet_pton(AF_INET,  "192.0.2.1");
my $v6 = inet_pton(AF_INET6, "2001:db8::1");

What you get back#

A 4-byte string for AF_INET or a 16-byte string for AF_INET6, in network byte order. Returns undef when the string is not a valid address for the requested family.

Examples#

my $addr = inet_pton(AF_INET6, "::1");
my $sin6 = pack_sockaddr_in6(443, $addr);
defined(inet_pton(AF_INET, $input))
    or die "not a valid IPv4 address: $input\n";

Edge cases#

  • Unsupported family returns undef rather than croaking.

  • Unlike inet_aton, there is no DNS fallback — hostnames are rejected.

  • Scoped IPv6 literals like "fe80::1%eth0" are not accepted; use getaddrinfo to carry the zone index through.

Differences from upstream#

Fully compatible with upstream Socket.

See also#

  • inet_ntop — reverse direction, packed bytes to string.

  • inet_aton — IPv4-only convenience with DNS fallback.

  • pack_sockaddr_in6 — the usual consumer of an IPv6 packed address.