9.12. Network Address Functions and Operators

Table 9-33 shows the operators available for the cidr and inet types. The operators <<, <<=, >>, and >>= test for subnet inclusion. They consider only the network parts of the two addresses (ignoring any host part) and determine whether one network is identical to or a subnet of the other.

Table 9-33. cidr and inet Operators

< is less thaninet '' < inet ''
<= is less than or equalinet '' <= inet ''
= equalsinet '' = inet ''
>= is greater or equalinet '' >= inet ''
> is greater thaninet '' > inet ''
<> is not equalinet '' <> inet ''
<< is contained withininet '' << inet '192.168.1/24'
<<= is contained within or equalsinet '192.168.1/24' <<= inet '192.168.1/24'
>> containsinet '192.168.1/24' >> inet ''
>>= contains or equalsinet '192.168.1/24' >>= inet '192.168.1/24'
~ bitwise NOT~ inet ''
& bitwise ANDinet '' & inet ''
| bitwise ORinet '' | inet ''
+ additioninet '' + 25
- subtractioninet '' - 36
- subtractioninet '' - inet ''

Table 9-34 shows the functions available for use with the cidr and inet types. The abbrev, host, and text functions are primarily intended to offer alternative display formats.

Table 9-34. cidr and inet Functions

FunctionReturn TypeDescriptionExampleResult
abbrev(inet)textabbreviated display format as textabbrev(inet '')
abbrev(cidr)textabbreviated display format as textabbrev(cidr '')10.1/16
broadcast(inet)inetbroadcast address for networkbroadcast('')
family(inet)intextract family of address; 4 for IPv4, 6 for IPv6family('::1')6
host(inet)textextract IP address as texthost('')
hostmask(inet)inetconstruct host mask for networkhostmask('')
masklen(inet)intextract netmask lengthmasklen('')24
netmask(inet)inetconstruct netmask for networknetmask('')
network(inet)cidrextract network part of addressnetwork('')
set_masklen(inet, int)inetset netmask length for inet valueset_masklen('', 16)
set_masklen(cidr, int)cidrset netmask length for cidr valueset_masklen(''::cidr, 16)
text(inet)textextract IP address and netmask length as texttext(inet '')

Any cidr value can be cast to inet implicitly or explicitly; therefore, the functions shown above as operating on inet also work on cidr values. (Where there are separate functions for inet and cidr, it is because the behavior should be different for the two cases.) Also, it is permitted to cast an inet value to cidr. When this is done, any bits to the right of the netmask are silently zeroed to create a valid cidr value. In addition, you can cast a text value to inet or cidr using normal casting syntax: for example, inet(expression) or colname::cidr.

Table 9-35 shows the functions available for use with the macaddr type. The function trunc(macaddr) returns a MAC address with the last 3 bytes set to zero. This can be used to associate the remaining prefix with a manufacturer.

Table 9-35. macaddr Functions

FunctionReturn TypeDescriptionExampleResult
trunc(macaddr)macaddrset last 3 bytes to zerotrunc(macaddr '12:34:56:78:90:ab')12:34:56:00:00:00

The macaddr type also supports the standard relational operators (>, <=, etc.) for lexicographical ordering.