The following functions manipulate IP network and/or host addresses;
most take inet
style argument(s).
This is an IPv4
address string, optionally followed by a netmask.
For IPv4, the format is dotted-decimal, i.e. N[.N[.[N.N]]] where N is a decimal, octal or hexadecimal integer from 0 to 255. If x < 4 values of N are given, the last N is taken as the last 5-x bytes instead of 1 byte, with missing bytes padded to the right. E.g. 192.258 is valid and equivalent to 192.1.2.0: the last N is 2 bytes in size, and covers 5 - 2 = 3 needed bytes, including 1 zero pad to the right. Conversely, 192.168.4.1027 is not valid: the last N is too large.
An IPv4 address may optionally be followed by a netmask, either of the form /B or :IPv4, where B is a decimal, octal or hexadecimal netmask integer from 0 to 32, and IPv4 is a dotted-decimal IPv4 address of the same format described above. If an :IPv4 netmask is given, only the largest contiguous set of most-significant 1 bits are used (because netmasks are contiguous). If no netmask is given, it will be calculated from standard IPv4 class A/B/C/D/E rules, but will be large enough to include all given bytes of the IP. E.g. 1.2.3.4 is Class A which has a netmask of 8, but the netmask will be extended to 32 to include all 4 given bytes.
In version 7.07.1554395000 20190404 and later, error messages are reported.
The inet
functions were added in version 5.01.1113268256 of
Apr 11 2005 and include the following. See also the Vortex
<urlutil>
equivalents:
inetabbrev(inet)
Returns a possibly shorter-than-canonical representation of
$inet
, where trailing zero byte(s) of an IPv4 address may
be omitted. All bytes of the network, and leading non-zero bytes
of the host, will be included. E.g. <urlutil inetabbrev
"192.100.0.0/24"> returns 192.100.0/24. The /B
netmask is included, except if (in version 7.07.1554840000
20190409 and later) the network is host-only (i.e. netmask is the
full size of the IP address). Empty string is returned on error.
inetcanon(inet)
Returns canonical representation of $inet
. For IPv4, this
is dotted-decimal with all 4 bytes.
The /B netmask is included, except if (in version
7.07.1554840000 20190409 and later) the network is host-only
(i.e. netmask is the full size of the IP address). Empty string
is returned on error.
inetnetwork(inet)
Returns string IP address with the network bits of inet
,
and the host bits set to 0. Empty string is returned on error.inethost(inet)
Returns string IP address with the host bits of inet
,
and the network bits set to 0. Empty string is returned on error.inetbroadcast(inet)
Returns string IP broadcast address for inet
, i.e. with
the network bits, and host bits set to 1. Empty string is
returned on error.inetnetmask(inet)
Returns string IP netmask for inet
, i.e. with the
network bits set to 1, and host bits set to 0. Empty string is
returned on error.inetnetmasklen(inet)
Returns integer netmask length of inet
. -1 is returned
on error.inetcontains(inetA, inetB)
Returns 1 if inetA
contains inetB
, i.e. every
address in inetB
occurs within the inetA
network.
0 is returned if not, or -1 on error.inetclass(inet)
Returns class of inet
, e.g. A, B, C,
D, E or classless if a different netmask is
used (or the address is IPv6). Empty string is returned on error.inet2int(inet)
Returns integer representation of IP network/host bits of
$inet
(i.e. without netmask); useful for compact storage of
address as integer(s) instead of string.
Returns -1 is returned on error (note that -1 may also be
returned for an all-ones IP address, e.g. 255.255.255.255).
int2inet(i)
Returns inet
string for
1- or 4-value varint $i
taken as an IP address. Since no netmask can be stored in the
integer form of an IP address, the returned IP string will not
have a netmask. Empty string is returned on error.