getnetbyaddr#
Look up a network record by its numeric network address.
getnetbyaddr translates a numeric network number and address family
into a network database entry by calling the system’s C
getnetbyaddr(3) routine. It is the reverse of
getnetbyname and consults the same system network
database (typically /etc/networks or an NSS backend). In scalar
context it returns just the canonical network name; in list context it
returns the full record.
Synopsis#
my $name = getnetbyaddr $addr, $addrtype;
my ($name, $aliases, $addrtype, $net) = getnetbyaddr $addr, $addrtype;
What you get back#
Scalar context — the canonical name of the network, or
undef if no entry matches.
List context — a four-element list mirroring the C struct netent:
$name— the canonical network name.$aliases— a space-separated string of alternate names.$addrtype— the address family of the record, typicallyAF_INET(2).$net— the network number as an unsigned integer, in host byte order.
On lookup failure in list context, the return is the empty list. If
the entry exists but is empty, the return is a single meaningless true
value per the shared get* family convention.
Arguments#
ADDR— the network number as an integer in host byte order, not a packed string. This is the native-integer form you get back from a priorgetnetbynamecall, or that you build withSocket::inet_atonfollowed byunpack("N", ...)to convert from network to host order.ADDRTYPE— the address family, almost alwaysAF_INETfrom theSocketmodule.
Global state it touches#
The network database iteration cursor shared with
getnetent,setnetent, andendnetent. A call togetnetbyaddrmay reset a cursor left open bygetnetent.$!— the Cerrnovalue on failure, where the platform sets it. Most/etc/networkslookups fail silently witherrnountouched, so treatundefas the primary failure signal.
Examples#
Look up the loopback network by its integer number:
use Socket;
my $name = getnetbyaddr(127 << 24, AF_INET);
print "$name\n"; # loopback
List context — the whole record:
use Socket;
my ($name, $aliases, $type, $net)
= getnetbyaddr(127 << 24, AF_INET);
printf "%s (net=%08x)\n", $name, $net;
Round-trip from a dotted string, via Socket:
use Socket;
my $packed = inet_aton("127.0.0.0"); # packed, network order
my $net = unpack("N", $packed); # integer, host order
my $name = getnetbyaddr($net, AF_INET);
By-name interface via the Net::netent override —
accessor methods instead of positional unpacking:
use Net::netent;
my $n = getnetbyaddr(127 << 24, AF_INET);
print $n->name, "\n" if defined $n;
Forward then reverse — resolve a name, then look the address back up:
use Socket;
my $net = (getnetbyname("loopback"))[3];
my $back = getnetbyaddr($net, AF_INET);
print "$back\n"; # loopback
Edge cases#
Integer, not packed string.
ADDRis a plain integer in host byte order. Passing the four-byte packed form thatgethostbyaddrconsumes here will silently look up the wrong number. This is the most common mistake with this function.Classful only, on most systems. The underlying
/etc/networksdatabase predates CIDR. Entries are classful network numbers (e.g.127,10,192.168), and the system call is not useful for arbitrary CIDR subnets. For modern subnet reasoning useSocketor a CPAN module likeNetAddr::IP.Empty
/etc/networks. Many Linux distributions ship a nearly empty/etc/networks; expect most lookups to returnundefon such a host. This is a configuration fact, not a bug in the call.List-context empty database entry. Per the shared
get*family rule, an entry that exists but has no fields returns a single meaningless true value, not the five-element list. In practice no platform produces such an entry for networks; guard withdefinedand element count if you care.ADDRTYPEother thanAF_INET. The C library accepts the argument but has no defined behavior for non-Internet families here. PassAF_INETunless the target system documents otherwise.Thread safety. The underlying C routine keeps per-process static state for the network cursor. Concurrent calls from multiple threads may interleave results. Where the platform offers
getnetbyaddr_r, Perl substitutes it automatically.List-operator precedence. Like the rest of the
get*family,getnetbyaddrparses as a list operator.getnetbyaddr $n, AF_INET or diebinds theor dieto the whole argument list, which is usually what you want; add parens when in doubt.
Differences from upstream#
Fully compatible with upstream Perl 5.42.
See also#
getnetbyname— the forward lookup: canonical or alias name → network recordgetnetent— iterate every entry in the network database instead of looking up one addresssetnetent— rewind the network iterator and control whether the database stays open across lookupsendnetent— close the network database when done iteratinggethostbyaddr— the host-level analogue; takes a packed address, not an integerNet::netent— by-name wrapper that returns an object withname/aliases/addrtype/netaccessors