ip_network: Vector of IP networks

View source: R/ip_network.R

ip_networkR Documentation

Vector of IP networks


Construct a vector of IP networks.



## Default S3 method:
ip_network(x = character(), ..., strict = TRUE)

## S3 method for class 'ip_address'
ip_network(address, prefix_length, ..., strict = TRUE)



Arguments passed to methods.


A character vector of IP networks, in CIDR notation (IPv4 or IPv6)


If TRUE (the default) and the input has host bits set, then a warning is emitted and NA is returned. If FALSE, the host bits are set to zero and a valid IP network is returned. If you need to retain the host bits, consider using ip_interface() instead.


An ip_address vector


An integer vector


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, ⁠⁠ represents addresses from ⁠⁠ to ⁠⁠.

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.

When comparing and sorting ip_network vectors, the network address is compared before the prefix length.


An S3 vector of class ip_network

See Also

prefix_length(), network_address(), netmask(), hostmask()



# construct from character vector
ip_network(c("", "2001:db8::/48"))

# validates inputs and replaces with NA
ip_network(c("", ""))

# IP networks should not have any host bits set

# but we can mask the host bits if desired
ip_network("", strict = FALSE)

# construct from address + prefix length
ip_network(ip_address(""), 24L)

# construct from address + netmask
ip_network(ip_address(""), prefix_length(ip_address("")))

# construct from address + hostmask
ip_network(ip_address(""), prefix_length(ip_address("")))

ipaddress documentation built on April 4, 2023, 9:09 a.m.