--- name: getnetbyaddr signature: 'getnetbyaddr ADDR,ADDRTYPE' since: 5.0 status: documented categories: ["Network info"] --- ```{index} single: getnetbyaddr; Perl built-in ``` *[Network info](../perlfunc-by-category)* # 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`](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 ```perl 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`](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, typically `AF_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 prior `getnetbyname` call, or that you build with `Socket::inet_aton` followed by `unpack("N", ...)` to convert from network to host order. - `ADDRTYPE` — the address family, almost always `AF_INET` from the [`Socket`](../../Socket) module. ## Global state it touches - The network database iteration cursor shared with [`getnetent`](getnetent), [`setnetent`](setnetent), and [`endnetent`](endnetent). A call to `getnetbyaddr` may reset a cursor left open by `getnetent`. - [`$!`](../perlvar) — the C `errno` value on failure, where the platform sets it. Most `/etc/networks` lookups fail silently with `errno` untouched, so treat [`undef`](undef) as the primary failure signal. ## Examples Look up the loopback network by its integer number: ```perl use Socket; my $name = getnetbyaddr(127 << 24, AF_INET); print "$name\n"; # loopback ``` List context — the whole record: ```perl 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`](../../Socket): ```perl 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: ```perl 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: ```perl use Socket; my $net = (getnetbyname("loopback"))[3]; my $back = getnetbyaddr($net, AF_INET); print "$back\n"; # loopback ``` ## Edge cases - **Integer, not packed string.** `ADDR` is a plain integer in host byte order. Passing the four-byte packed form that [`gethostbyaddr`](gethostbyaddr) consumes 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/networks` database 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 use [`Socket`](../../Socket) or a CPAN module like `NetAddr::IP`. - **Empty `/etc/networks`.** Many Linux distributions ship a nearly empty `/etc/networks`; expect most lookups to return [`undef`](undef) on 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 with `defined` and element count if you care. - **`ADDRTYPE` other than `AF_INET`.** The C library accepts the argument but has no defined behavior for non-Internet families here. Pass `AF_INET` unless 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, `getnetbyaddr` parses as a list operator. `getnetbyaddr $n, AF_INET or die` binds the `or die` to 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`](getnetbyname) — the forward lookup: canonical or alias name → network record - [`getnetent`](getnetent) — iterate every entry in the network database instead of looking up one address - [`setnetent`](setnetent) — rewind the network iterator and control whether the database stays open across lookups - [`endnetent`](endnetent) — close the network database when done iterating - [`gethostbyaddr`](gethostbyaddr) — the host-level analogue; takes a packed address, not an integer - `Net::netent` — by-name wrapper that returns an object with `name` / `aliases` / `addrtype` / `net` accessors