An IP network corresponds to a contiguous range of IP addresses
(also known as an IP block). CIDR notation represents an
IP network as the routing prefix address (which denotes the start
of the range) and the prefix length (which indicates the size of
the range) separated by a forward slash. For example, 192.168.0.0/24
represents addresses from 192.168.0.0
to 192.168.0.255
.
The prefix length indicates the number of bits reserved by the
routing prefix. This means that larger prefix lengths indicate
smaller networks. The maximum prefix length is 32 for IPv4 and
128 for IPv6. These would correspond to an IP network of a single
IP address.
The ip_network()
constructor accepts a character vector of IP networks
in CIDR notation. It checks whether each string is a valid IPv4 or IPv6
network, and converts it to an ip_network
object. If the input is invalid,
a warning is emitted and NA
is stored instead.
An alternative constructor accepts an ip_address
vector and an
integer vector containing the network address and prefix length, respectively.
When casting an ip_network
object back to a character vector using
as.character()
, IPv6 addresses are reduced to their compressed representation.