Introduction to iptools

Oliver Keyes


IP handling, conversion and validation

iptools is a package to make IP addresses convenient to deal with, parse and validate. It is heavily influenced by the Python iptools module, and will hopefully make users’ lives a heck of a lot easier if they have to deal with IP data. Much of it is currently IPv4-specific, out of necessity, but as R’s support for bigger numbers increases, we’ll hopefully make as much of it support IPv6 as possible!

Validating, converting and classifying IP addresses

How do you know an IP address is an IP address? How do you know what type of IP address it is? Most of the time the answer is a complicated regular expression, made more complicated by the need to check for things that are syntactically valid IP addresses, but aren’t actually possible. iptools contains ip_classify, which accepts a vector of IP addresses (or things that might be IP addresses) and identifies whether they’re valid - and if they are valid, what type they are.

Once you’ve validated and classified the IPs, you might want to look at them in their numeric form, rather than the “dotted-decimal” form - or you might have numeric forms, and need dotted-decimal. Either way, there’s a function for it.

These functions only currently work for IPv4 IPs - we’ll have IPv6 support as soon as R can handle numbers that big!

Resolving hostnames

The iptools package has integrated the AsioHeaders package which includes the asio networking library. Thanks to this we can take a hostname and work out what IP address(es) are associated with it:

This works in reverse, too:

Both operations are fully recognised, but aren’t particularly fast - in fact, they’re incredibly slow compared to the rest of the package - since they need to call out of the system to work. For the same reason, they require a net connection to work, and may slow down said connection while running. You’ve been warned.

Handling IP ranges

As well as specific, unique IP addresses, you may also encounter IP ranges - subsets of the IP address space, looking something like “”. iptools provides a variety of functions to manipulate and expand on these.

range_boundaries takes an IP range (or as many IP ranges as you want!) and produces a data.frame containing the smallest-valued and largest-valued IP addresses in that range:

If you want all the values within a certain range, rather than just the smallest and largest, you can use range_generate, which provides you with all of the valid addresses within a specific IP range:

On the other hand, if you simply want to check if an IP address is within a certain range, without caring about the range itself, you can use ip_in_range. This accepts a vector of IP addresses and either one range, or an equally-sized vector of ranges, to check them against. It then tells you which of the provided IPs are within the range (TRUE) or not (FALSE):

Generating IP addresses

Sometimes you need spoof data - IPs that are valid, but aren’t sourced from anywhere in particular. iptools contains ip_random, which lets you generate a set of pseudo-random and totally valid IPv4 addresses: