The following functions manipulate IP network and/or host addresses.
The addresses are
varchar arguments in
where N is a decimal, octal or hexadecimal integer from 0 to 255,
B is a decimal, octal or hexadecimal netmask integer from 0 to 32,
and IP is an IP address netmask of the form
.N]]]. If only x Ns are
specified, the last N may be 5-x bytes in size instead of 1
byte. E.g. "
1.2.65535" is legal (last N is 2 bytes),
22.214.171.124535" is not. If no netmask
:IP) is specified, the netmask will be
calculated from standard class A/B/C/D/E rules, but it will be at
least large enough to include all specified bytes of the IP. (Thus,
to get the class A/B/C/D/E netmask of an IP address via
inetnetmask(), just give the first/highest N byte of the
IP, as this is the sole determiner of class.) If an IP netmask is
specified, only the largest contiguous set of most-significant 1
bits are used. Examples: "
126.96.36.199" (netmask is
because 4 bytes given), "
10" (netmask is
it is a Class A address), "
inet functions were added in version 5.01.1113268256 of
Apr 11 2005 and include the following. See also the Vortex
inetabbrev(inet)Returns the shortest canonical representation of
inet. This will include all contiguous most-significant bytes of the network, non-zero bytes of the host, and a netmask. Other trailing 0 bytes of the IP may be trimmed. Empty string is returned on error.
inetcanon(inet)Returns canonical representation of
inet. This is dotted-decimal with all 4 bytes, and a
/N netmask. Empty string is returned on error.
inetnetwork(inet)Returns 4-decimal IP address with the network bits of
inet, and the host bits set to 0. Empty string is returned on error.
inethost(inet)Returns 4-decimal IP address with the host bits of
inet, and the network bits set to 0. Empty string is returned on error.
inetbroadcast(inet)Returns 4-decimal 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 4-decimal 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
inetB, i.e. every address in
inetBoccurs within the
inetAnetwork. 0 is returned if not, or -1 on error.
inetclass(inet)Returns class of
classlessif a different netmask is used. 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 IPv4 address as integers instead of strings. -1 is returned on error (note that -1 may also be legitimately returned for an all-ones IP address, e.g. "
inetstring for integer
itaken 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.