Learn R Programming

ipaddress (version 0.3.0)

ip_network: Vector of IP networks

Description

ip_network() constructs a vector of IP networks.

is_ip_network() checks if an object is of class ip_network.

as_ip_network() casts an object to ip_network.

Usage

ip_network(...)

# S3 method for default ip_network(x = character(), strict = TRUE, ...)

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

is_ip_network(x)

as_ip_network(x)

# S3 method for character as_ip_network(x)

# S3 method for ip_interface as_ip_network(x)

# S3 method for ip_network as.character(x, ...)

# S3 method for ip_network format(x, ...)

Arguments

...

Included for S3 generic consistency

x
  • For ip_network(): A character vector of IP networks, in CIDR notation (IPv4 or IPv6)

  • For is_ip_network(): An object to test

  • For as_ip_network(): An object to cast

  • For as.character(): An ip_network vector

strict

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.

address

An ip_address vector

prefix_length

An integer vector

Value

An S3 vector of class ip_network

Details

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.

See Also

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

Examples

Run this code
# NOT RUN {
# construct from character vector
ip_network(c("192.168.0.0/24", "2001:db8::/48"))

# construct from address + prefix length objects
ip_network(ip_address(c("192.168.0.0", "2001:db8::")), c(24L, 48L))

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

# IP networks should not have any host bits set
ip_network("192.168.0.1/22")

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

Run the code above in your browser using DataLab