Learn R Programming

⚠️There's a newer version (1.0.2) of this package.Take me there.

ipaddress

ipaddress provides data classes and functions for working with IP addresses and networks. Its interface is inspired by the Python ipaddress module.

Here are some of the features:

  • Functions for generation and analysis of IP data
  • Full support for both IPv4 and IPv6 address spaces
  • Memory footprint: data stored in native format
  • Performance: calculations performed in C++
  • Compatible with the tidyverse

For data visualization of IP addresses and networks, check out the ggip package.

Installation

You can install the released version of ipaddress from CRAN with:

install.packages("ipaddress")

Or you can install the development version from GitHub:

# install.packages("remotes")
remotes::install_github("davidchall/ipaddress")

Usage

Use ip_address() and ip_network() vectors either standalone or as columns in a data frame.

library(tidyverse)
library(ipaddress)

tibble(
  address = ip_address(c("192.168.0.1", "2001:db8::8a2e:370:7334")),
  network = ip_network(c("192.168.100.0/22", "2001:db8::/80"))
)
#> # A tibble: 2 x 2
#>                   address          network
#>                 <ip_addr>       <ip_netwk>
#> 1             192.168.0.1 192.168.100.0/22
#> 2 2001:db8::8a2e:370:7334    2001:db8::/80

Input character vectors are validated as they are parsed. Invalid inputs raise a warning and are replaced with NA.

ip_address(c("255.255.255.255", "255.255.255.256"))
#> Warning: Problem on row 2: 255.255.255.256
#> <ip_address[2]>
#> [1] 255.255.255.255 <NA>

A variety of functions are provided to enable common tasks.

tibble(network = ip_network(c("192.168.100.0/22", "2001:db8::/80"))) %>%
  mutate(
    first = network_address(network),
    last = broadcast_address(network),
    ipv6 = is_ipv6(network)
  )
#> # A tibble: 2 x 4
#>            network         first                     last ipv6 
#>         <ip_netwk>     <ip_addr>                <ip_addr> <lgl>
#> 1 192.168.100.0/22 192.168.100.0          192.168.103.255 FALSE
#> 2    2001:db8::/80    2001:db8:: 2001:db8::ffff:ffff:ffff TRUE

Related work

  • iptools – A well established R package for working with IP addresses and networks. Unfortunately IPv6 support is severely limited. Also, addresses and networks are stored as character vectors, so they must be parsed to their native bit representation for every operation. It served as an excellent guide and motivation for ipaddress.
  • cyberpandas – A Python package for using IP addresses in a pandas DataFrame. This offers full support for IPv6 and stores addresses in the native bit representation. However, most “interesting” operations must deserialize each address to a Python ipaddress object, which is slow. It also doesn’t support IP networks.

Please note that the ipaddress project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('ipaddress')

Monthly Downloads

388

Version

0.5.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

David Hall

Last Published

August 26th, 2020

Functions in ipaddress (0.5.0)

ip_network

Vector of IP networks
collapse_networks

Collapse contiguous and overlapping networks
common_network

Find the common network of two addresses
ip_interface

Vector of IP interfaces
ip_operators

Operators for IP addresses
iana_ipv4

IPv4 address space allocation
ip_address

Vector of IP addresses
address_in_network

Network membership of addresses
exclude_networks

Remove networks from others
iana_ipv6

IPv6 address space allocation
ipaddress-package

ipaddress: Tidy IP Addresses
ipaddress-vctrs

Internal vctrs methods
ip_to_bytes

Represent address as raw bytes
ip_to_binary

Represent address as binary
ipv6-transition

IPv6 transition mechanisms
is_ipv6

Version of the address space
ip_to_integer

Represent address as integer
ip_to_hostname

Translate address to/from hostname
network_size

Network size
is_reserved

Reserved addresses
max_prefix_length

Size of the address space
reverse_pointer

Reverse DNS pointer
sequence

List addresses within a network
sample

Sample random addresses
netmask

Network mask
network_in_network

Network membership of other networks
traverse_hierarchy

Traverse the network hierarchy
summarize_address_range

List constituent networks of an address range