The IP package provides vectorized classes and methods to work with IP addresses in R. Both IPv4 and IPv6 are supported as well as arbitrary IP ranges. It is based on the ip4r PostgreSQL extension. The IP package defines vectors-like objects that provides for
The following code snippet shows how to create IPv4 addresses and ranges in addition to arithmetic, compare and bit manipulation methods usage as well as address lookup :
##
library(IP)
## low part of the range
<- ipv4("192.168.0.0")
l ## high part of the range
<- l + (2^16 -1)
h ## range
<-ipv4r(l, h)
pn ## build range using CIDR notation
==ipv4r("192.168.0.0/16")
pn## build range using dash notation
==ipv4r("192.168.0.0-192.168.255.255")
pn## build h from l with the appropriate mask using bitwise OR
==ipv4r( l, l | ipv4.hostmask( ceiling(log2(ip.range(pn))) ) )
pn## same thing as l + (2^16 -1) using left shift bitwise operator
==ipv4r( l, l + ( ipv4(1L) %<<% 16L ) - 1)
pn##
## IP lookup
##
## \in
ipv4.reserved()[ ip.index(ipv4.reserved())( l + 0:9 )]
## ==
ipv4.reserved()[ ip.match(pn, ipv4.reserved() ) ]
## \in
ipv4.reserved()[ip.index(ipv4.reserved())(
ipv4r("192.168.0.0/24")
)]
The same will work IPv6 address and IPv6 ranges. Simply substitute
ipv4 with ipv6 and change the address to, eg, “fe80::/10”
(ipv6r("fe80::/10")
) and host mask
(hi(ipv6r("fe80::/10"))==(ipv6("fe80::")|ipv6.hostmask(10))
).
Also use ip()
and ipr()
to mix both
protocols.
What follows shows how to get (confusing) informations about hosts (note : the host() is still experimental under Windows and localhost.ip() is not yet avalaible)
## DNS lookup
<- host('r-project.org')
rhost ## Reverse DNS lookup : "cran.wu-wien.ac.at"
<- host.info(ipv4(rhost))
rhost.hnm ## primary domain : "ac.at"
fqdn(rhost.hnm)
## RIR : ARIN (echt?)
ipv4.rir()[ip.match(ipv4(rhost), ipv4.rir())]
## address was not recovered
ip.match(ipv4(rhost), ipv4.recovered())
## domain name info
<- whois('r-project.org', output=1)
rdom.whois ## "AT" (Österreich, alles klar)
'r-project.org']]['Registrant Country']
rdom.whois[[## host info
<- whois(ipv4(rhost),verbose = 2, output=1)
rhost.whois ## "RIPE Network Coordination Centre (RIPE)"
'r-project.org']]['Organization'] rhost.whois[[
The results of those queries may look a little bit confusing at first. The whois queries tells us that r-project.org site is hosted by the Wirtschaftsuniversität Wien in Austria (and so does the extension of the primary domain — “.at”) and that its address is accordingly managed by the RIPE-NCC. But RIR lookup on the address of the server tells us that its address falls within a range managed by ARIN which serves North America. What’s happening here is that some address ranges were assigned by ARIN in the 80’s to European organizations such as universities before RIPE NCC began its operations in 1992. Those ranges were later transferred to the RIPE NCC as shown by
## "Early Registrations, Transferred to RIPE NCC"
'r-project.org']]['NetType'] rhost.whois[[
but this range still belongs to the ARIN address space.
Please look here for more examples.