* Ruby >= 1.8.7 (not tested with previous versions)
* Ruby >= 1.8.7 (not tested with previous versions)
* Ruby 1.9.2 or later is strongly recommended
* Ruby 1.9.2 or later is strongly recommended
IPAddress 0.8.0 has been tested on:
IPAddress 0.8.2 has been tested on:
* ruby-1.8.7-p334 [ i386 ]
* ruby-1.8.7-p334 [ i386 ]
* ree-1.8.7-2011.03 [ i386 ]
* ree-1.8.7-2011.03 [ i386 ]
...
@@ -69,14 +69,14 @@ The latest documentation can be found online at
...
@@ -69,14 +69,14 @@ The latest documentation can be found online at
== IPv4
== IPv4
Class IPAddress::IPv4 is used to handle IPv4 type addresses. IPAddress
Class `IPAddress::IPv4` is used to handle IPv4 type addresses. IPAddress
is similar to other IP Addresses libraries, like Ruby's own
is similar to other IP Addresses libraries, like Ruby's own
IPAddr. However it works slightly different, as we will see.
IPAddr. However it works slightly different, as we will see.
=== Create a new IPv4 address
=== Create a new IPv4 address
The usual way to express an IP Address is using its dotted decimal
The usual way to express an IP Address is using its dotted decimal
form, such as 172.16.10.1, and a prefix, such as 24, separated by a
form, such as `172.16.10.1`, and a prefix, such as `24`, separated by a
slash.
slash.
172.16.10.1/24
172.16.10.1/24
...
@@ -93,7 +93,7 @@ which accepts and parses any kind of IP (uint32, IPv4, IPV6 and
...
@@ -93,7 +93,7 @@ which accepts and parses any kind of IP (uint32, IPv4, IPV6 and
IPv4 IPv6 Mapped addresses).
IPv4 IPv6 Mapped addresses).
If you like syntactic sugar, you can use the wrapper method
If you like syntactic sugar, you can use the wrapper method
IPAddress(), which is built around IPAddress::parse:
`IPAddress()`, which is built around `IPAddress::parse`:
ip = IPAddress "172.16.10.1/24"
ip = IPAddress "172.16.10.1/24"
...
@@ -102,13 +102,13 @@ You can specify an IPv4 address in any of two ways:
...
@@ -102,13 +102,13 @@ You can specify an IPv4 address in any of two ways:
IPAddress "172.16.10.1/24"
IPAddress "172.16.10.1/24"
IPAddress "172.16.10.1/255.255.255.0"
IPAddress "172.16.10.1/255.255.255.0"
In this example, prefix /24 and netmask 255.255.255.0 are the same and
In this example, prefix `/24` and netmask `255.255.255.0` are the same and
you have the flexibility to use either one of them.
you have the flexibility to use either one of them.
If you don't explicitly specify the prefix (or the subnet mask),
If you don't explicitly specify the prefix (or the subnet mask),
IPAddress thinks you're dealing with host addresses and not with
IPAddress thinks you're dealing with host addresses and not with
networks. Therefore, the default prefix will be /32, or
networks. Therefore, the default prefix will be `/32`, or
255.255.255.255. For example:
`255.255.255.255`. For example:
# let's declare an host address
# let's declare an host address
host = IPAddress::IPv4.new "10.1.1.1"
host = IPAddress::IPv4.new "10.1.1.1"
...
@@ -116,12 +116,12 @@ networks. Therefore, the default prefix will be /32, or
...
@@ -116,12 +116,12 @@ networks. Therefore, the default prefix will be /32, or
puts host.to_string
puts host.to_string
#=> "10.1.1.1/32"
#=> "10.1.1.1/32"
The new created object has prefix /32, which is the same
The new created object has prefix `/32`, which is the same
as we created the following:
as we created the following:
host = IPAddress::IPv4.new "10.1.1.1/32"
host = IPAddress::IPv4.new "10.1.1.1/32"
You can also pass a uint32 to obtain an IPAddress::IPv4 object:
You can also pass a `uint32` to obtain an `IPAddress::IPv4` object:
# Create host object
# Create host object
ip = IPAddress 167837953
ip = IPAddress 167837953
...
@@ -140,25 +140,25 @@ Once created, you can obtain the attributes for an IPv4 object:
...
@@ -140,25 +140,25 @@ Once created, you can obtain the attributes for an IPv4 object:
#=> 24
#=> 24
In case you need to retrieve the netmask in IPv4 format, you can use
In case you need to retrieve the netmask in IPv4 format, you can use
the IPv4#netmask method:
the `IPv4#netmask` method:
ip.netmask
ip.netmask
#=> "255.255.255.0"
#=> "255.255.255.0"
A special attribute, IPv4#octets, is available to get the four
A special attribute, `IPv4#octets`, is available to get the four
decimal octets from the IP address:
decimal octets from the IP address:
ip.octets
ip.octets
#=> [172,16,10,1]
#=> [172,16,10,1]
Shortcut method IPv4#[], provides access to a given octet whithin the
Shortcut method `IPv4#[]`, provides access to a given octet whithin the
range:
range:
ip[1]
ip[1]
#=> 16
#=> 16
If you need to print out the IPv4 address in a canonical form, you can
If you need to print out the IPv4 address in a canonical form, you can
use IPv4#to_string
use `IPv4#to_string`:
ip.to_string
ip.to_string
#=> "172.16.10.l/24"
#=> "172.16.10.l/24"
...
@@ -174,7 +174,7 @@ object. For example:
...
@@ -174,7 +174,7 @@ object. For example:
#=> "172.16.10.l/25"
#=> "172.16.10.l/25"
If you need to use a netmask in IPv4 format, you can achive so by
If you need to use a netmask in IPv4 format, you can achive so by
using the IPv4#netmask= method
using the `IPv4#netmask=` method:
ip.netmask = "255.255.255.252"
ip.netmask = "255.255.255.252"
...
@@ -187,7 +187,7 @@ Some very important topics in dealing with IP addresses are the
...
@@ -187,7 +187,7 @@ Some very important topics in dealing with IP addresses are the
concepts of +network+ and +broadcast+, as well as the addresses
concepts of +network+ and +broadcast+, as well as the addresses
included in a range.
included in a range.
When you specify an IPv4 address such as "172.16.10.1/24", you are
When you specify an IPv4 address such as `172.16.10.1/24`, you are
actually handling two different information:
actually handling two different information:
* The IP address itself, "172.16.10.1"
* The IP address itself, "172.16.10.1"
...
@@ -196,11 +196,11 @@ actually handling two different information:
...
@@ -196,11 +196,11 @@ actually handling two different information:
The network number is the IP which has all zeroes in the host
The network number is the IP which has all zeroes in the host
portion. In our example, because the prefix is 24, we identify our
portion. In our example, because the prefix is 24, we identify our
network number to have the last 8 (32-24) bits all zeroes. Thus, IP
network number to have the last 8 (32-24) bits all zeroes. Thus, IP
address "172.16.10.1/24" belongs to network "172.16.10.0/24".
address `172.16.10.1/24` belongs to network `172.16.10.0/24`.
This is very important because, for instance, IP "172.16.10.1/16" is
This is very important because, for instance, IP `172.16.10.1/16` is
very different to the previous one, belonging to the very different
very different to the previous one, belonging to the very different
network "172.16.0.0/16".
network `172.16.0.0/16`.
==== Networks
==== Networks
...
@@ -221,7 +221,7 @@ number, calculated after the original object. We want to outline here
...
@@ -221,7 +221,7 @@ number, calculated after the original object. We want to outline here
that the network address is a perfect legitimate IPv4 address, which
that the network address is a perfect legitimate IPv4 address, which
just happen to have all zeroes in the host portion.
just happen to have all zeroes in the host portion.
You can use method IPv4#network? to check whether an IP address is a
You can use method `IPv4#network?` to check whether an IP address is a
network or not:
network or not:
ip1 = IPAddress "172.16.10.1/24"
ip1 = IPAddress "172.16.10.1/24"
...
@@ -236,11 +236,11 @@ network or not:
...
@@ -236,11 +236,11 @@ network or not:
The broadcast address is the contrary than the network number: where
The broadcast address is the contrary than the network number: where
the network number has all zeroes in the host portion, the broadcast
the network number has all zeroes in the host portion, the broadcast
address has all one's. For example, ip "172.16.10.1/24" has broadcast
address has all one's. For example, ip `172.16.10.1/24` has broadcast
"172.16.10.255/24", where ip "172.16.10.1/16" has broadcast
`172.16.10.255/24`, where ip `172.16.10.1/16` has broadcast
"172.16.255.255/16".
`172.16.255.255/16`.
Method IPv4#broadcast has the same behavior as is #network
Method `IPv4#broadcast` has the same behavior as is `#network`
counterpart: it creates a new IPv4 object to handle the broadcast
counterpart: it creates a new IPv4 object to handle the broadcast
address:
address:
...
@@ -258,7 +258,7 @@ address:
...
@@ -258,7 +258,7 @@ address:
So we see that the netmask essentially specifies a range for IP
So we see that the netmask essentially specifies a range for IP
addresses that are included in a network: all the addresses between
addresses that are included in a network: all the addresses between
the network number and the broadcast. IPAddress has many methods to
the network number and the broadcast. IPAddress has many methods to
iterate between those addresses. Let's start with IPv4#each, which
iterate between those addresses. Let's start with `IPv4#each`, which
iterates over all addresses in a range
iterates over all addresses in a range
ip = IPAddress "172.16.10.1/24"
ip = IPAddress "172.16.10.1/24"
...
@@ -271,7 +271,7 @@ It is important to note that it doesn't matter if the original IP is a
...
@@ -271,7 +271,7 @@ It is important to note that it doesn't matter if the original IP is a
host IP or a network number (or a broadcast address): the #each method
host IP or a network number (or a broadcast address): the #each method
only considers the range that the original IP specifies.
only considers the range that the original IP specifies.
If you only want to iterate over hosts IP, use the IPv4#each_host
If you only want to iterate over hosts IP, use the `IPv4#each_host`
method:
method:
ip = IPAddress "172.16.10.1/24"
ip = IPAddress "172.16.10.1/24"
...
@@ -280,7 +280,7 @@ method:
...
@@ -280,7 +280,7 @@ method:
puts host
puts host
end
end
Methods IPv4#first and IPv4#last return a new object containing
Methods `IPv4#first` and `IPv4#last` return a new object containing
respectively the first and the last host address in the range
respectively the first and the last host address in the range
ip = IPAddress "172.16.10.100/24"
ip = IPAddress "172.16.10.100/24"
...
@@ -291,6 +291,28 @@ respectively the first and the last host address in the range
...
@@ -291,6 +291,28 @@ respectively the first and the last host address in the range
ip.last.to_string
ip.last.to_string
#=> "172.16.10.254/24"
#=> "172.16.10.254/24"
Checking if an address is loopback is easy with the `IPv4#loopback?`
method:
ip = IPAddress "127.0.0.1"
ip.loopback?
#=> true
Checking if an address is in the multicast range can be done using the `IPv4#multicast?`
method:
ip = IPAddress "224.0.0.1/32"
ip.multicast?
#=> true
The ability to generate a range also exists by using the `IPv4#to()` method. This allows you to create a subnet agnostic range based off a fixed amount.
ip = IPAddress "172.16.10.100/24"
ip.to('172.16.10.110')
#=> ["172.16.10.100", ..., "172.16.10.110"]
=== IP special formats
=== IP special formats
The IPAddress library provides a complete set of methods to access an
The IPAddress library provides a complete set of methods to access an
...
@@ -308,21 +330,21 @@ The first thing to highlight here is that all these conversion methods
...
@@ -308,21 +330,21 @@ The first thing to highlight here is that all these conversion methods
only take into consideration the address portion of an IPv4 object and
only take into consideration the address portion of an IPv4 object and
not the prefix (netmask).
not the prefix (netmask).
So, to express the address in binary format, use the IPv4#bits method:
So, to express the address in binary format, use the `IPv4#bits` method:
ip.bits
ip.bits
#=> "10101100000100000000101000000001"
#=> "10101100000100000000101000000001"
To calculate the 32 bits unsigned int format of the ip address, use
To calculate the 32 bits unsigned int format of the ip address, use
the IPv4#to_u32 method
the `IPv4#to_u32` method
ip.to_u32
ip.to_u32
#=> 2886732289
#=> 2886732289
This method is the equivalent of the Unix call pton(), expressing an
This method is the equivalent of the Unix call `pton()`, expressing an
IP address in the so called +network byte order+ notation. However, if
IP address in the so called +network byte order+ notation. However, if
you want to transmit your IP over a network socket, you might need to
you want to transmit your IP over a network socket, you might need to
transform it in data format using the IPv4#data method:
transform it in data format using the `IPv4#data` method:
ip.data
ip.data
#=> "\254\020\n\001"
#=> "\254\020\n\001"
...
@@ -333,7 +355,7 @@ suitable to use in IPv4-IPv6 mapped addresses:
...
@@ -333,7 +355,7 @@ suitable to use in IPv4-IPv6 mapped addresses:
ip.to_ipv6
ip.to_ipv6
#=> "ac10:0a01"
#=> "ac10:0a01"
Finally, much like IPv4#to_ipv6 you can use to IPv4#to_h method to return a non-semicolon delineated string (useful with pcap/byte level usage):
Finally, much like `IPv4#to_ipv6` you can use to `IPv4#to_h` method to return a non-semicolon delineated string (useful with pcap/byte level usage):
ip.to_h
ip.to_h
#=> "ac100a01"
#=> "ac100a01"
...
@@ -344,7 +366,7 @@ IPAddress allows you to create and manipulate objects using the old
...
@@ -344,7 +366,7 @@ IPAddress allows you to create and manipulate objects using the old
and deprecated (but apparently still popular) classful networks concept.
and deprecated (but apparently still popular) classful networks concept.
Classful networks and addresses don't have a prefix: their subnet mask
Classful networks and addresses don't have a prefix: their subnet mask
is univocally identified by their address, and therefore diveded in classes.
is univocally identified by their address, and therefore divided in classes.
As per RFC 791, these classes are:
As per RFC 791, these classes are:
* Class A, from 0.0.0.0 to 127.255.255.255
* Class A, from 0.0.0.0 to 127.255.255.255
...
@@ -406,7 +428,7 @@ Subnetting is easy with IPAddress. You actually have two options:
...
@@ -406,7 +428,7 @@ Subnetting is easy with IPAddress. You actually have two options:
* IPv4#subnet: specify a new prefix
* IPv4#subnet: specify a new prefix
* IPv4#split: tell IPAddress how many subnets you want to create.
* IPv4#split: tell IPAddress how many subnets you want to create.
Let's examine IPv4#subnet first. Say you have network "172.16.10.0/24"
Let's examine `IPv4#subnet` first. Say you have network "172.16.10.0/24"
and you want to subnet it into /26 networks. With IPAddress it's very
and you want to subnet it into /26 networks. With IPAddress it's very
easy:
easy:
...
@@ -426,7 +448,7 @@ representing the new subnets.
...
@@ -426,7 +448,7 @@ representing the new subnets.
Another way to create subnets is to tell IPAddress how many subnets you'd
Another way to create subnets is to tell IPAddress how many subnets you'd
like to have, and letting the library calculate the new prefix for you.
like to have, and letting the library calculate the new prefix for you.
Let's see how it works, using IPv4#split method. Say you want 4 new subnets:
Let's see how it works, using `IPv4#split` method. Say you want 4 new subnets:
network = IPAddress("172.16.10.0/24")
network = IPAddress("172.16.10.0/24")
...
@@ -439,15 +461,15 @@ Let's see how it works, using IPv4#split method. Say you want 4 new subnets:
...
@@ -439,15 +461,15 @@ Let's see how it works, using IPv4#split method. Say you want 4 new subnets:
"172.16.10.192/26"]
"172.16.10.192/26"]
Hey, that's the same result as before! This actually makes sense, as the
Hey, that's the same result as before! This actually makes sense, as the
two operations are complementary. When you use IPv4#subnet with the new
two operations are complementary. When you use `IPv4#subnet` with the new
prefix, IPAddress will always create a number of subnets that is a power
prefix, IPAddress will always create a number of subnets that is a power
of two. This is equivalent to use IPv4#split with a power of 2.
of two. This is equivalent to use IPv4#split with a power of 2.
Where IPv4#split really shines is with the so called "uneven subnetting".
Where `IPv4#split` really shines is with the so called "uneven subnetting".
You are not limited to split a network into a power-of-two numbers of
You are not limited to split a network into a power-of-two numbers of
subnets: IPAddress lets you create any number of subnets, and it will
subnets: IPAddress lets you create any number of subnets, and it will
try to organize the new created network in the best possible way, making
try to organize the new created network in the best possible way, making
an efficent allocation of the space.
an efficient allocation of the space.
An example here is worth a thousand words. Let's use the same network
An example here is worth a thousand words. Let's use the same network
as the previous examples:
as the previous examples:
...
@@ -465,8 +487,8 @@ How do we split this network into 3 subnets? Very easy:
...
@@ -465,8 +487,8 @@ How do we split this network into 3 subnets? Very easy:
As you can see, IPAddress tried to perform a good allocation by filling up
As you can see, IPAddress tried to perform a good allocation by filling up
all the address space from the original network. There is no point in splitting
all the address space from the original network. There is no point in splitting
a network into 3 subnets like "172.16.10.0/26", "172.16.10.64/26" and
a network into 3 subnets like `172.16.10.0/26`, `172.16.10.64/26` and
"172.16.10.128/26", as you would end up having "172.16.10.192/26" wasted (plus,
`172.16.10.128/26`, as you would end up having `172.16.10.192/26` wasted (plus,
I suppose I wouldn't need a Ruby library to perform un-efficient IP
I suppose I wouldn't need a Ruby library to perform un-efficient IP
allocation, as I do that myself very well ;) ).
allocation, as I do that myself very well ;) ).
...
@@ -478,8 +500,8 @@ We can go even further and split into 11 subnets:
...
@@ -478,8 +500,8 @@ We can go even further and split into 11 subnets: