inet_pton#

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

תקציר#

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

מה מוחזר#

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.

דוגמאות#

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

מקרי קצה#

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

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

Fully compatible with upstream Socket.

ראו גם#

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