Title: | Data Analysis for IP Addresses and Networks |
---|---|
Description: | Classes and functions for working with IP (Internet Protocol) addresses and networks, inspired by the Python 'ipaddress' module. Offers full support for both IPv4 and IPv6 (Internet Protocol versions 4 and 6) address spaces. It is specifically designed to work well with the 'tidyverse'. |
Authors: | David Hall [aut, cre] |
Maintainer: | David Hall <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.2.9000 |
Built: | 2024-12-07 04:25:05 UTC |
Source: | https://github.com/davidchall/ipaddress |
These functions check whether an address falls within a network.
is_within()
performs a one-to-one matching between addresses and networks.
is_within_any()
checks if each address falls within any of the networks.
is_within(address, network) is_within_any(address, network)
is_within(address, network) is_within_any(address, network)
address |
An |
network |
An |
A logical vector
Use is_subnet()
to check if an ip_network
is within another
ip_network
.
is_within(ip_address("192.168.2.6"), ip_network("192.168.2.0/28")) is_within(ip_address("192.168.3.6"), ip_network("192.168.2.0/28")) is_within_any(ip_address("192.168.3.6"), ip_network(c("192.168.2.0/28", "192.168.3.0/28")))
is_within(ip_address("192.168.2.6"), ip_network("192.168.2.0/28")) is_within(ip_address("192.168.3.6"), ip_network("192.168.2.0/28")) is_within_any(ip_address("192.168.3.6"), ip_network(c("192.168.2.0/28", "192.168.3.0/28")))
Given a vector of networks, this returns the minimal set of networks required to represent the same range of addresses.
collapse_networks(network)
collapse_networks(network)
network |
An |
An ip_network
vector (potentially shorter than the input)
collapse_networks(ip_network(c("192.168.0.0/24", "192.168.1.0/24")))
collapse_networks(ip_network(c("192.168.0.0/24", "192.168.1.0/24")))
Returns the smallest network that contains both addresses.
This can construct a network from its first and last addresses. However, if
the address range does not match the network boundaries, then the result
extends beyond the original address range. Use summarize_address_range()
to receive a list of networks that exactly match the address range.
common_network(address1, address2)
common_network(address1, address2)
address1 |
An |
address2 |
An |
An ip_network
vector
# address range matches network boundaries common_network(ip_address("192.168.0.0"), ip_address("192.168.0.15")) # address range does not match network boundaries common_network(ip_address("192.167.255.255"), ip_address("192.168.0.16"))
# address range matches network boundaries common_network(ip_address("192.168.0.0"), ip_address("192.168.0.15")) # address range does not match network boundaries common_network(ip_address("192.167.255.255"), ip_address("192.168.0.16"))
Retrieve lists of IP networks registered to specific countries.
country_networks(country, ..., collapse = TRUE)
country_networks(country, ..., collapse = TRUE)
country |
Character vector of two-letter country codes (ISO 3166-1 alpha-2) |
... |
These dots are for future extensions and must be empty. |
collapse |
If |
This function requires an internet connection to download network lists.
A data frame with 2 variables:
country
: A character vector
network
: A list of ip_network
vectors
Each row represents a single country associated with a vector of IP networks.
https://www.iwik.org/ipcountry/ (updated daily)
## Not run: country_networks(c("GB", "US")) country_networks(c("GB", "US"), collapse = FALSE) # retrieve networks for a single country country_networks("TO")$networks[[1]] # expand networks for multiple countries tidyr::unchop(country_networks(c("GB", "US")), networks) ## End(Not run)
## Not run: country_networks(c("GB", "US")) country_networks(c("GB", "US"), collapse = FALSE) # retrieve networks for a single country country_networks("TO")$networks[[1]] # expand networks for multiple countries tidyr::unchop(country_networks(c("GB", "US")), networks) ## End(Not run)
exclude_networks()
takes lists of networks to include and exclude. It then
calculates the address ranges that are included but not excluded (similar to
setdiff()
), and finally returns the minimal set of networks needed to
describe the remaining address ranges.
exclude_networks(include, exclude)
exclude_networks(include, exclude)
include |
An |
exclude |
An |
An ip_network
vector
collapse_networks()
, setdiff()
exclude_networks(ip_network("192.0.2.0/28"), ip_network("192.0.2.1/32")) exclude_networks(ip_network("192.0.2.0/28"), ip_network("192.0.2.15/32"))
exclude_networks(ip_network("192.0.2.0/28"), ip_network("192.0.2.1/32")) exclude_networks(ip_network("192.0.2.0/28"), ip_network("192.0.2.15/32"))
A dataset containing the registry of allocated blocks in IPv4 address space.
iana_ipv4
iana_ipv4
A data frame with 121 rows and 3 variables:
Address block (an ip_network
vector)
There are three types of allocation:
reserved
managed by regional Internet registry (RIR)
assigned to organization
The RIR, organization or purpose for reservation
Last updated 2022-12-12
https://www.iana.org/assignments/ipv4-address-space
iana_ipv4
iana_ipv4
A dataset containing the registry of allocated blocks in IPv6 address space.
iana_ipv6
iana_ipv6
A data frame with 47 rows and 3 variables:
Address block (an ip_network
vector)
There are two types of allocation:
reserved
managed by regional Internet registry (RIR)
The RIR or purpose for reservation
Last updated 2020-08-18
https://www.iana.org/assignments/ipv6-address-space
https://www.iana.org/assignments/ipv6-unicast-address-assignments
iana_ipv6
iana_ipv6
Construct a vector of IP addresses.
ip_address(x = character())
ip_address(x = character())
x |
A character vector of IP addresses, in dot-decimal notation (IPv4) or hexadecimal notation (IPv6) |
An address in IPv4 space uses 32-bits. It is usually represented
as 4 groups of 8 bits, each shown as decimal digits (e.g. 192.168.0.1
).
This is known as dot-decimal notation.
An address in IPv6 space uses 128-bits. It is usually represented
as 8 groups of 16 bits, each shown as hexadecimal digits
(e.g. 2001:0db8:85a3:0000:0000:8a2e:0370:7334
). This representation can
also be compressed by removing leading zeros and replacing consecutive
groups of zeros with double-colon (e.g. 2001:db8:85a3::8a2e:370:7334
).
Finally, there is also the dual representation. This expresses the final
two groups as an IPv4 address (e.g. 2001:db8:85a3::8a2e:3.112.115.52
).
The ip_address()
constructor accepts a character vector of IP addresses
in these two formats. It checks whether each string is a valid IPv4 or IPv6
address, and converts it to an ip_address
object. If the input is invalid,
a warning is emitted and NA
is stored instead.
When casting an ip_address
object back to a character vector using
as.character()
, IPv6 addresses are reduced to their compressed representation.
A special case is IPv4-mapped IPv6 addresses (see is_ipv4_mapped()
), which
are returned in the dual representation (e.g. ::ffff:192.168.0.1
).
ip_address
vectors support a number of operators.
An S3 vector of class ip_address
ip_operators
, vignette("ip-data")
# supports IPv4 and IPv6 simultaneously ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334")) # validates inputs and replaces with NA ip_address(c("255.255.255.256", "192.168.0.1/32"))
# supports IPv4 and IPv6 simultaneously ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334")) # validates inputs and replaces with NA ip_address(c("255.255.255.256", "192.168.0.1/32"))
Methods for converting character vectors and ip_interface
vectors to
ip_address
and ip_network
vectors.
as_ip_address(x) as_ip_interface(x) as_ip_network(x)
as_ip_address(x) as_ip_interface(x) as_ip_network(x)
x |
An object to cast |
as_ip_address()
: An ip_address
vector
as_ip_network()
: An ip_network
vector
as_ip_interface()
: An ip_interface
vector
as_ip_address(ip_interface("192.168.0.1/10")) as_ip_network(ip_interface("192.168.0.1/10"))
as_ip_address(ip_interface("192.168.0.1/10")) as_ip_network(ip_interface("192.168.0.1/10"))
Format vector of IP data using compressed or exploded representation.
## S3 method for class 'ip_address' format(x, ..., exploded = FALSE) ## S3 method for class 'ip_interface' format(x, ..., exploded = FALSE) ## S3 method for class 'ip_network' format(x, ..., exploded = FALSE)
## S3 method for class 'ip_address' format(x, ..., exploded = FALSE) ## S3 method for class 'ip_interface' format(x, ..., exploded = FALSE) ## S3 method for class 'ip_network' format(x, ..., exploded = FALSE)
x |
An object to format |
... |
These dots are for future extensions and must be empty. |
exploded |
Logical scalar. Should IPv6 addresses display leading zeros?
(default: |
A character vector
format(ip_address("2001:db8::8a2e:370:7334")) format(ip_address("2001:db8::8a2e:370:7334"), exploded = TRUE)
format(ip_address("2001:db8::8a2e:370:7334")) format(ip_address("2001:db8::8a2e:370:7334"), exploded = TRUE)
Construct a vector of IP interfaces.
This hybrid class stores both the host address and the network it is on.
ip_interface(...) ## Default S3 method: ip_interface(x = character(), ...) ## S3 method for class 'ip_address' ip_interface(address, prefix_length, ...)
ip_interface(...) ## Default S3 method: ip_interface(x = character(), ...) ## S3 method for class 'ip_address' ip_interface(address, prefix_length, ...)
... |
Arguments passed to methods. |
x |
A character vector of IP interfaces, in CIDR notation (IPv4 or IPv6) |
address |
An |
prefix_length |
An integer vector |
Constructing an ip_interface
vector is conceptually like constructing an
ip_network
vector, except the host bits are retained.
The ip_interface
class inherits from the ip_address
class.
This means it can generally be used in places where an ip_address
vector is expected. A few exceptions to this rule are:
It does not support addition and subtraction of integers
It does not support bitwise operations
It cannot be compared to ip_address
vectors
The ip_interface
class additionally supports a few functions typically
reserved for ip_network
vectors: prefix_length()
, netmask()
and
hostmask()
.
For other purposes, you can extract the address and network components using
as_ip_address()
and as_ip_network()
.
When comparing and sorting ip_interface
vectors, the network is compared
before the host address.
An S3 vector of class ip_interface
vignette("ip-data")
# construct from character vector ip_interface(c("192.168.0.1/10", "2001:db8:c3::abcd/45")) # construct from address + prefix length objects ip_interface(ip_address(c("192.168.0.1", "2001:db8:c3::abcd")), c(10L, 45L)) # extract IP address x <- ip_interface(c("192.168.0.1/10", "2001:db8:c3::abcd/45")) as_ip_address(x) # extract IP network (with host bits masked) as_ip_network(x)
# construct from character vector ip_interface(c("192.168.0.1/10", "2001:db8:c3::abcd/45")) # construct from address + prefix length objects ip_interface(ip_address(c("192.168.0.1", "2001:db8:c3::abcd")), c(10L, 45L)) # extract IP address x <- ip_interface(c("192.168.0.1/10", "2001:db8:c3::abcd/45")) as_ip_address(x) # extract IP network (with host bits masked) as_ip_network(x)
Construct a vector of IP networks.
ip_network(...) ## Default S3 method: ip_network(x = character(), ..., strict = TRUE) ## S3 method for class 'ip_address' ip_network(address, prefix_length, ..., strict = TRUE)
ip_network(...) ## 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. |
x |
A character vector of IP networks, in CIDR notation (IPv4 or IPv6) |
strict |
If |
address |
An |
prefix_length |
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, 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.
When comparing and sorting ip_network
vectors, the network address is
compared before the prefix length.
An S3 vector of class ip_network
prefix_length()
, network_address()
, netmask()
, hostmask()
vignette("ip-data")
# construct from character vector ip_network(c("192.168.0.0/24", "2001:db8::/48")) # 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) # construct from address + prefix length ip_network(ip_address("192.168.0.0"), 24L) # construct from address + netmask ip_network(ip_address("192.168.0.0"), prefix_length(ip_address("255.255.255.0"))) # construct from address + hostmask ip_network(ip_address("192.168.0.0"), prefix_length(ip_address("0.0.0.255")))
# construct from character vector ip_network(c("192.168.0.0/24", "2001:db8::/48")) # 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) # construct from address + prefix length ip_network(ip_address("192.168.0.0"), 24L) # construct from address + netmask ip_network(ip_address("192.168.0.0"), prefix_length(ip_address("255.255.255.0"))) # construct from address + hostmask ip_network(ip_address("192.168.0.0"), prefix_length(ip_address("0.0.0.255")))
ip_address
vectors support the following operators:
bitwise logic operators: !
(NOT), &
(AND), |
(OR), ^
(XOR)
bitwise shift operators: %<<%
(left shift), %>>%
(right shift)
arithmetic operators: +
(addition), -
(subtraction)
# use ip_to_binary() to understand these examples better # bitwise NOT !ip_address("192.168.0.1") # bitwise AND ip_address("192.168.0.1") & ip_address("255.0.0.255") # bitwise OR ip_address("192.168.0.0") | ip_address("255.0.0.255") # bitwise XOR ip_address("192.168.0.0") ^ ip_address("255.0.0.255") # bitwise shift left ip_address("192.168.0.1") %<<% 1 # bitwise shift right ip_address("192.168.0.1") %>>% 1 # addition of integers ip_address("192.168.0.1") + 10 # subtraction of integers ip_address("192.168.0.1") - 10
# use ip_to_binary() to understand these examples better # bitwise NOT !ip_address("192.168.0.1") # bitwise AND ip_address("192.168.0.1") & ip_address("255.0.0.255") # bitwise OR ip_address("192.168.0.0") | ip_address("255.0.0.255") # bitwise XOR ip_address("192.168.0.0") ^ ip_address("255.0.0.255") # bitwise shift left ip_address("192.168.0.1") %<<% 1 # bitwise shift right ip_address("192.168.0.1") %>>% 1 # addition of integers ip_address("192.168.0.1") + 10 # subtraction of integers ip_address("192.168.0.1") - 10
Check if an object is an ip_address
, ip_network
or ip_interface
vector.
is_ip_address(x) is_ip_interface(x) is_ip_network(x)
is_ip_address(x) is_ip_interface(x) is_ip_network(x)
x |
An object to test |
A logical scalar
is_ip_address(ip_address("192.168.0.1")) is_ip_interface(ip_interface("192.168.0.1/10")) is_ip_network(ip_network("192.168.0.0/24"))
is_ip_address(ip_address("192.168.0.1")) is_ip_interface(ip_interface("192.168.0.1/10")) is_ip_network(ip_network("192.168.0.0/24"))
Encode or decode an ip_address
as a binary bit string.
ip_to_binary(x) binary_to_ip(x)
ip_to_binary(x) binary_to_ip(x)
x |
|
The bits are stored in network order (also known as big-endian order), which is part of the IP standard.
IPv4 addresses use 32 bits, IPv6 addresses use 128 bits, and missing values
are encoded as NA
.
ip_to_binary()
: A character vector
binary_to_ip()
: An ip_address
vector
Other address representations:
ip_to_bytes()
,
ip_to_hex()
,
ip_to_integer()
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_binary(x) binary_to_ip(ip_to_binary(x))
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_binary(x) binary_to_ip(ip_to_binary(x))
Encode or decode an ip_address
as a list of raw bytes.
ip_to_bytes(x) bytes_to_ip(x)
ip_to_bytes(x) bytes_to_ip(x)
x |
|
The bytes are stored in network order (also known as big-endian order), which is part of the IP standard.
IPv4 addresses use 4 bytes, IPv6 addresses use 16 bytes,
and missing values are encoded as NULL
.
ip_to_bytes()
: A list of raw vectors
bytes_to_ip()
: An ip_address
vector
Use blob::as_blob()
to cast result to a blob object
Other address representations:
ip_to_binary()
,
ip_to_hex()
,
ip_to_integer()
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_bytes(x) bytes <- list( as.raw(c(0xc0, 0xa8, 0x00, 0x01)), as.raw(c( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 )), NULL ) bytes_to_ip(bytes)
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_bytes(x) bytes <- list( as.raw(c(0xc0, 0xa8, 0x00, 0x01)), as.raw(c( 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 )), NULL ) bytes_to_ip(bytes)
Encode or decode an ip_address
as a hexadecimal string.
ip_to_hex(x) hex_to_ip(x, is_ipv6 = NULL)
ip_to_hex(x) hex_to_ip(x, is_ipv6 = NULL)
x |
|
is_ipv6 |
A logical vector indicating whether to construct an IPv4 or
IPv6 address. If |
ip_to_hex()
: A character vector
hex_to_ip()
: An ip_address
vector
Other address representations:
ip_to_binary()
,
ip_to_bytes()
,
ip_to_integer()
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_hex(x) hex_to_ip(ip_to_hex(x))
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_hex(x) hex_to_ip(ip_to_hex(x))
Perform reverse and forward DNS resolution.
Note: These functions are significantly slower than others in the ipaddress package.
ip_to_hostname(x) ip_to_hostname_all(x) hostname_to_ip(x) hostname_to_ip_all(x)
ip_to_hostname(x) ip_to_hostname_all(x) hostname_to_ip(x) hostname_to_ip_all(x)
x |
|
These functions require an internet connection. Before processing the input vector, we first check that a known hostname can be resolved. If this fails, an error is raised.
If DNS lookup cannot resolve an input, then NA
is returned for that input.
If an error occurs during DNS lookup, then a warning is emitted and NA
is
returned for that input.
DNS resolution performs a many-to-many mapping between IP addresses and
hostnames. For this reason, there are two versions of each function. The
regular version returns just the first value and the _all()
suffix version
returns all values.
ip_to_hostname()
: A character vector
ip_to_hostname_all()
: A list of character vectors
hostname_to_ip()
: An ip_address
vector
hostname_to_ip_all()
: A list of ip_address
vectors
The base function nsl()
provides forward DNS resolution to IPv4 addresses,
but only on Unix-like systems.
## Not run: hostname_to_ip("r-project.org") ip_to_hostname(hostname_to_ip("r-project.org")) ## End(Not run)
## Not run: hostname_to_ip("r-project.org") ip_to_hostname(hostname_to_ip("r-project.org")) ## End(Not run)
Encode or decode an ip_address
as an integer.
ip_to_integer(x) integer_to_ip(x, is_ipv6 = NULL)
ip_to_integer(x) integer_to_ip(x, is_ipv6 = NULL)
x |
|
is_ipv6 |
A logical vector indicating whether to construct an IPv4 or
IPv6 address. If |
It is common to represent an IP address as an integer, by reinterpreting
the bit sequence as a big-endian unsigned integer. This means IPv4 and IPv6
addresses can be represented by 32-bit and 128-bit unsigned integers.
In this way, the IPv4 addresses 0.0.0.0
and 255.255.255.255
would be
represented as 0 and 4,294,967,295.
The numeric data types within base R (integer
and double
) have
insufficient precision to cover the IPv6 address space. Instead we return a
bignum::biginteger
vector, which supports arbitrary precision integers.
ip_to_integer()
: A bignum::biginteger
vector
integer_to_ip()
: An ip_address
vector
Other address representations:
ip_to_binary()
,
ip_to_bytes()
,
ip_to_hex()
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_integer(x) integer_to_ip(ip_to_integer(x)) # with IPv4 only, we can use numeric data type as.numeric(ip_to_integer(ip_address("192.168.0.1"))) integer_to_ip(3232235521)
x <- ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334", NA)) ip_to_integer(x) integer_to_ip(ip_to_integer(x)) # with IPv4 only, we can use numeric data type as.numeric(ip_to_integer(ip_address("192.168.0.1"))) integer_to_ip(3232235521)
There are multiple mechanisms designed to help with the transition from IPv4 to IPv6. These functions make it possible to extract the embedded IPv4 address from an IPv6 address.
is_ipv4_mapped(x) is_6to4(x) is_teredo(x) extract_ipv4_mapped(x) extract_6to4(x) extract_teredo_server(x) extract_teredo_client(x)
is_ipv4_mapped(x) is_6to4(x) is_teredo(x) extract_ipv4_mapped(x) extract_6to4(x) extract_teredo_server(x) extract_teredo_client(x)
x |
An |
The IPv6 transition mechanisms are described in the IETF memos:
is_xxx()
: A logical vector
extract_xxx()
: An ip_address
vector
# these examples show the reserved networks is_ipv4_mapped(ip_network("::ffff:0.0.0.0/96")) is_6to4(ip_network("2002::/16")) is_teredo(ip_network("2001::/32")) # these examples show embedded IPv4 addresses extract_ipv4_mapped(ip_address("::ffff:192.168.0.1")) extract_6to4(ip_address("2002:c000:0204::")) extract_teredo_server(ip_address("2001:0000:4136:e378:8000:63bf:3fff:fdd2")) extract_teredo_client(ip_address("2001:0000:4136:e378:8000:63bf:3fff:fdd2"))
# these examples show the reserved networks is_ipv4_mapped(ip_network("::ffff:0.0.0.0/96")) is_6to4(ip_network("2002::/16")) is_teredo(ip_network("2001::/32")) # these examples show embedded IPv4 addresses extract_ipv4_mapped(ip_address("::ffff:192.168.0.1")) extract_6to4(ip_address("2002:c000:0204::")) extract_teredo_server(ip_address("2001:0000:4136:e378:8000:63bf:3fff:fdd2")) extract_teredo_client(ip_address("2001:0000:4136:e378:8000:63bf:3fff:fdd2"))
Version of the address space
is_ipv4(x) is_ipv6(x)
is_ipv4(x) is_ipv6(x)
x |
An |
A logical vector
ip <- ip_address(c("192.168.0.1", "2001:db8::7334")) is_ipv4(ip) is_ipv6(ip)
ip <- ip_address(c("192.168.0.1", "2001:db8::7334")) is_ipv4(ip) is_ipv6(ip)
Most of these functions check if an address or network is reserved for
special use. The exception is is_global()
, which checks if it is not
reserved.
A network is considered reserved if both the network_address()
and broadcast_address()
are reserved.
is_private(x) is_global(x) is_multicast(x) is_unspecified(x) is_reserved(x) is_loopback(x) is_link_local(x) is_site_local(x)
is_private(x) is_global(x) is_multicast(x) is_unspecified(x) is_reserved(x) is_loopback(x) is_link_local(x) is_site_local(x)
x |
An |
Here are hyperlinks to the IANA registries of allocated address space:
IPv4: allocations, special purpose
IPv6: allocations, special purpose
A logical vector
Addresses reserved by IPv6 transition mechanisms can be identified by functions described in ipv6-transition.
is_private(ip_network(c("192.168.0.0/16", "2001:db8::/32"))) is_global(ip_network(c("1.0.0.0/8", "2002::/32"))) is_multicast(ip_network(c("224.0.0.0/4", "ff00::/8"))) is_unspecified(ip_network(c("0.0.0.0/32", "::/128"))) is_reserved(ip_network(c("240.0.0.0/4", "f000::/5"))) is_loopback(ip_network(c("127.0.0.0/8", "::1/128"))) is_link_local(ip_network(c("169.254.0.0/16", "fe80::/10"))) is_site_local(ip_network("fec0::/10"))
is_private(ip_network(c("192.168.0.0/16", "2001:db8::/32"))) is_global(ip_network(c("1.0.0.0/8", "2002::/32"))) is_multicast(ip_network(c("224.0.0.0/4", "ff00::/8"))) is_unspecified(ip_network(c("0.0.0.0/32", "::/128"))) is_reserved(ip_network(c("240.0.0.0/4", "f000::/5"))) is_loopback(ip_network(c("127.0.0.0/8", "::1/128"))) is_link_local(ip_network(c("169.254.0.0/16", "fe80::/10"))) is_site_local(ip_network("fec0::/10"))
The total number of bits available in the address space. IPv4 uses 32-bit addresses and IPv6 uses 128-bit addresses.
max_prefix_length(x)
max_prefix_length(x)
x |
An |
An integer vector
is_ipv4()
, is_ipv6()
, prefix_length()
x <- ip_address(c("192.168.0.1", "2001:db8::7334")) max_prefix_length(x)
x <- ip_address(c("192.168.0.1", "2001:db8::7334")) max_prefix_length(x)
prefix_length()
, netmask()
and hostmask()
extract different (but
equivalent) representations of the network mask. They accept an
ip_network
or ip_interface
vector.
The functions can also convert between these alternative representations.
For example, prefix_length()
can infer the prefix length from an
ip_address
vector of netmasks and/or hostmasks, while netmask()
and
hostmask()
can accept a vector of prefix lengths.
prefix_length(x) netmask(x, ...) ## S3 method for class 'numeric' netmask(x, is_ipv6 = NULL, ...) hostmask(x, ...) ## S3 method for class 'numeric' hostmask(x, is_ipv6 = NULL, ...)
prefix_length(x) netmask(x, ...) ## S3 method for class 'numeric' netmask(x, is_ipv6 = NULL, ...) hostmask(x, ...) ## S3 method for class 'numeric' hostmask(x, is_ipv6 = NULL, ...)
x |
|
... |
Arguments passed to methods. |
is_ipv6 |
A logical vector indicating whether to construct an IPv4 or
IPv6 address. If |
prefix_length()
: An integer vector
netmask()
: An ip_address
vector
hostmask()
: An ip_address
vector
x <- ip_network(c("192.168.0.0/22", "2001:db00::0/26")) prefix_length(x) netmask(x) hostmask(x) # construct netmask/hostmask from prefix length netmask(c(22L, 26L), c(FALSE, TRUE)) hostmask(c(22L, 26L), c(FALSE, TRUE)) # extract prefix length from netmask/hostmask prefix_length(ip_address(c("255.255.255.0", "0.255.255.255"))) # invalid netmask/hostmask raise a warning and return NA prefix_length(ip_address("255.255.255.1"))
x <- ip_network(c("192.168.0.0/22", "2001:db00::0/26")) prefix_length(x) netmask(x) hostmask(x) # construct netmask/hostmask from prefix length netmask(c(22L, 26L), c(FALSE, TRUE)) hostmask(c(22L, 26L), c(FALSE, TRUE)) # extract prefix length from netmask/hostmask prefix_length(ip_address(c("255.255.255.0", "0.255.255.255"))) # invalid netmask/hostmask raise a warning and return NA prefix_length(ip_address("255.255.255.1"))
is_supernet()
and is_subnet()
check if one network is a true supernet or
subnet of another network; overlaps()
checks for any overlap between two
networks.
is_supernet(network, other) is_subnet(network, other) overlaps(network, other)
is_supernet(network, other) is_subnet(network, other) overlaps(network, other)
network |
An |
other |
An |
A logical vector
Use is_within()
to check if an ip_address
is within an ip_network
.
Use supernet()
and subnets()
to traverse the network hierarchy.
net1 <- ip_network("192.168.1.128/30") net2 <- ip_network("192.168.1.0/24") is_supernet(net1, net2) is_subnet(net1, net2) overlaps(net1, net2)
net1 <- ip_network("192.168.1.128/30") net2 <- ip_network("192.168.1.0/24") is_supernet(net1, net2) is_subnet(net1, net2) overlaps(net1, net2)
network_address()
and broadcast_address()
yield the first and last
addresses of the network; num_addresses()
gives the total number of
addresses in the network.
network_address(x) broadcast_address(x) num_addresses(x)
network_address(x) broadcast_address(x) num_addresses(x)
x |
An |
The broadcast address is a special address at which any host connected
to the network can receive messages. That is, packets sent to this address
are received by all hosts on the network.
In IPv4, the last address of a network is the broadcast address.
Although IPv6 does not follow this approach to broadcast addresses, the
broadcast_address()
function still returns the last address of the network.
network_address()
: An ip_address
vector
broadcast_address()
: An ip_address
vector
num_addresses()
: A numeric vector
Use seq.ip_network()
to generate all addresses in a network.
x <- ip_network(c("192.168.0.0/22", "2001:db8::/33")) network_address(x) broadcast_address(x) num_addresses(x)
x <- ip_network(c("192.168.0.0/22", "2001:db8::/33")) network_address(x) broadcast_address(x) num_addresses(x)
Returns the PTR record used by reverse DNS.
reverse_pointer(x)
reverse_pointer(x)
x |
An |
These documents describe reverse DNS lookup in more detail:
IPv4: RFC-1035 Section 3.5
IPv6: RFC-3596 Section 2.5
A character vector
reverse_pointer(ip_address("127.0.0.1")) reverse_pointer(ip_address("2001:db8::1"))
reverse_pointer(ip_address("127.0.0.1")) reverse_pointer(ip_address("2001:db8::1"))
sample_ipv4()
and sample_ipv6()
sample from the entire address space;
sample_network()
samples from a specific network.
sample_ipv4(size, ..., replace = FALSE) sample_ipv6(size, ..., replace = FALSE) sample_network(x, size, ..., replace = FALSE)
sample_ipv4(size, ..., replace = FALSE) sample_ipv6(size, ..., replace = FALSE) sample_network(x, size, ..., replace = FALSE)
size |
Integer specifying the number of addresses to return |
... |
These dots are for future extensions and must be empty. |
replace |
Should sampling be with replacement? (default: |
x |
An |
An ip_address
vector
Use seq.ip_network()
to generate all addresses in a network.
sample_ipv4(5) sample_ipv6(5) sample_network(ip_network("192.168.0.0/16"), 5) sample_network(ip_network("2001:db8::/48"), 5)
sample_ipv4(5) sample_ipv6(5) sample_network(ip_network("192.168.0.0/16"), 5) sample_network(ip_network("2001:db8::/48"), 5)
seq()
returns all hosts
hosts()
returns only usable hosts
## S3 method for class 'ip_network' seq(x, ...) hosts(x)
## S3 method for class 'ip_network' seq(x, ...) hosts(x)
x |
An |
... |
These dots are for future extensions and must be empty. |
In IPv4, the unusable hosts are the network address and the broadcast address (i.e. the first and last addresses in the network). In IPv6, the only unusable host is the subnet router anycast address (i.e. the first address in the network).
For networks with a prefix length of 31 (for IPv4) or 127 (for IPv6),
the unusable hosts are included in the results of hosts()
.
The ipaddress package does not support long vectors (i.e. vectors with more than 2^31 - 1 elements). As a result, these two functions do not support networks larger than this size. This corresponds to prefix lengths less than 2 (for IPv4) or 98 (for IPv6). However, you might find that machine memory imposes stricter limitations.
An ip_address
vector
Use network_address()
and broadcast_address()
to get the first and last
address of a network.
Use sample_network()
to randomly sample addresses from a network.
Use subnets()
to list the subnetworks within a network.
seq(ip_network("192.168.0.0/30")) seq(ip_network("2001:db8::/126")) hosts(ip_network("192.168.0.0/30")) hosts(ip_network("2001:db8::/126"))
seq(ip_network("192.168.0.0/30")) seq(ip_network("2001:db8::/126")) hosts(ip_network("192.168.0.0/30")) hosts(ip_network("2001:db8::/126"))
Given an address range, this returns the list of constituent networks.
If you know the address range matches the boundaries of a single network, it
might be preferable to use common_network()
. This returns an ip_network
vector instead of a list of ip_network
vectors.
summarize_address_range(address1, address2)
summarize_address_range(address1, address2)
address1 |
An |
address2 |
An |
A list of ip_network
vectors
# address range matches network boundaries summarize_address_range(ip_address("192.168.0.0"), ip_address("192.168.0.15")) # address range does not match network boundaries summarize_address_range(ip_address("192.167.255.255"), ip_address("192.168.0.16"))
# address range matches network boundaries summarize_address_range(ip_address("192.168.0.0"), ip_address("192.168.0.15")) # address range does not match network boundaries summarize_address_range(ip_address("192.167.255.255"), ip_address("192.168.0.16"))
These functions step up and down the network hierarchy. supernet()
returns
the supernetwork containing the given network. subnets()
returns the list
of subnetworks which join to make the given network.
supernet(x, new_prefix = prefix_length(x) - 1L) subnets(x, new_prefix = prefix_length(x) + 1L)
supernet(x, new_prefix = prefix_length(x) - 1L) subnets(x, new_prefix = prefix_length(x) + 1L)
x |
An |
new_prefix |
An integer vector indicating the desired prefix length. By default, this steps a single level through the hierarchy. |
The ipaddress package does not support long vectors
(i.e. vectors with more than 2^31 - 1 elements). The limits the number of
subnetworks that subnets()
can return. However, you might find that machine
memory imposes stricter limitations.
supernet()
: An ip_network
vector
subnets()
: A list of ip_network
vectors
Use seq.ip_network()
to list the addresses within a network.
Use is_supernet()
and is_subnet()
to check if one network is contained
within another.
supernet(ip_network("192.168.0.0/24")) supernet(ip_network("192.168.0.0/24"), new_prefix = 10L) subnets(ip_network("192.168.0.0/24")) subnets(ip_network("192.168.0.0/24"), new_prefix = 27L)
supernet(ip_network("192.168.0.0/24")) supernet(ip_network("192.168.0.0/24"), new_prefix = 10L) subnets(ip_network("192.168.0.0/24")) subnets(ip_network("192.168.0.0/24"), new_prefix = 27L)