summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Mackintosh <mikemackintosh@users.noreply.github.com>2015-04-17 11:52:04 -0400
committerMike Mackintosh <mikemackintosh@users.noreply.github.com>2015-04-17 11:52:04 -0400
commit8fa088ba7d20ca2e13909265bc9b00c4f64c2edf (patch)
tree94336445957ba92d7adab5f2198d611d2befd800
parenta7daea80883b04cb7b5e0b306de100c69f2bdb8a (diff)
parent05bb1473c4e116ea939deb44928efcf094af788b (diff)
downloadipaddress-8fa088ba7d20ca2e13909265bc9b00c4f64c2edf.tar.gz
Merge pull request #61 from mikemackintosh/master
Version 0.8.2
-rw-r--r--.gitignore20
-rw-r--r--.rock.yml5
-rw-r--r--CHANGELOG.rdoc11
-rw-r--r--Gemfile3
-rw-r--r--LICENSE.txt (renamed from LICENSE)2
-rw-r--r--README.rdoc153
-rw-r--r--Rakefile19
-rw-r--r--VERSION1
-rw-r--r--ipaddress.gemspec72
-rw-r--r--lib/ipaddress/ipv4.rb121
-rw-r--r--lib/ipaddress/ipv6.rb9
-rw-r--r--lib/ipaddress/version.rb3
-rw-r--r--test/ipaddress/ipv4_test.rb59
-rw-r--r--test/ipaddress/ipv6_test.rb8
14 files changed, 324 insertions, 162 deletions
diff --git a/.gitignore b/.gitignore
index 8849c99..85d89b4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,18 @@
-*.sw?
+*.a
+/.bundle/
+*.bundle
+/coverage/
+/doc/
.DS_Store
-coverage
+/Gemfile.lock
+ipaddr.html
+mkmf.log
+*.o
+/pkg/
rdoc
-pkg
server.rb
-ipaddr.html
-Gemfile.lock
+*.so
+/spec/reports/
+*.sw?
+/tmp/
+/_yardoc/
diff --git a/.rock.yml b/.rock.yml
new file mode 100644
index 0000000..551b6e7
--- /dev/null
+++ b/.rock.yml
@@ -0,0 +1,5 @@
+runtime: ruby21
+doc: exec rake rdoc
+build: |
+ rock clean
+ {{parent}} \ No newline at end of file
diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc
index 650cafe..c386602 100644
--- a/CHANGELOG.rdoc
+++ b/CHANGELOG.rdoc
@@ -1,6 +1,15 @@
+== ipaddress 0.8.2
+
+CHANGED:: merged bundler branch to cleanup gemspec and Rakefiles
+FIXED:: IPAddress::IPv4.split handling (Issue #40)
+NEW:: Added #[]= method to IPv4/6 classes to add octet writing support. (Issue #24)
+NEW:: IPV4#multicast?
+NEW:: IPV4#loopback?
+NEW:: IPV4#to()
+
== ipaddress 0.8.1
-CHANGED:: ipaddress now uses minitest for testing, all tests passing
+CHANGED:: ipaddress now uses minitest for testing, all tests passing
== ipaddress 0.8.0
diff --git a/Gemfile b/Gemfile
new file mode 100644
index 0000000..b4e2a20
--- /dev/null
+++ b/Gemfile
@@ -0,0 +1,3 @@
+source "https://rubygems.org"
+
+gemspec
diff --git a/LICENSE b/LICENSE.txt
index 25def2b..943eee1 100644
--- a/LICENSE
+++ b/LICENSE.txt
@@ -1,4 +1,4 @@
-Copyright (c) 2009-2011 Marco Ceresa
+Copyright (c) 2009-2015 Marco Ceresa
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
diff --git a/README.rdoc b/README.rdoc
index b74c9b0..05698a0 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -21,7 +21,7 @@ examples of typical usage.
* Ruby >= 1.8.7 (not tested with previous versions)
* 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 ]
* ree-1.8.7-2011.03 [ i386 ]
@@ -69,14 +69,14 @@ The latest documentation can be found online at
== 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
IPAddr. However it works slightly different, as we will see.
=== Create a new IPv4 address
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.
172.16.10.1/24
@@ -93,7 +93,7 @@ which accepts and parses any kind of IP (uint32, IPv4, IPV6 and
IPv4 IPv6 Mapped addresses).
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"
@@ -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/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.
If you don't explicitly specify the prefix (or the subnet mask),
IPAddress thinks you're dealing with host addresses and not with
-networks. Therefore, the default prefix will be /32, or
-255.255.255.255. For example:
+networks. Therefore, the default prefix will be `/32`, or
+`255.255.255.255`. For example:
# let's declare an host address
host = IPAddress::IPv4.new "10.1.1.1"
@@ -116,12 +116,12 @@ networks. Therefore, the default prefix will be /32, or
puts host.to_string
#=> "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:
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
ip = IPAddress 167837953
@@ -140,25 +140,25 @@ Once created, you can obtain the attributes for an IPv4 object:
#=> 24
In case you need to retrieve the netmask in IPv4 format, you can use
-the IPv4#netmask method:
+the `IPv4#netmask` method:
ip.netmask
#=> "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:
ip.octets
#=> [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:
ip[1]
#=> 16
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
#=> "172.16.10.l/24"
@@ -174,7 +174,7 @@ object. For example:
#=> "172.16.10.l/25"
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"
@@ -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
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:
* The IP address itself, "172.16.10.1"
@@ -196,11 +196,11 @@ actually handling two different information:
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
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
-network "172.16.0.0/16".
+network `172.16.0.0/16`.
==== Networks
@@ -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
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:
ip1 = IPAddress "172.16.10.1/24"
@@ -236,11 +236,11 @@ network or not:
The broadcast address is the contrary than the network number: where
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
-"172.16.10.255/24", where ip "172.16.10.1/16" has broadcast
-"172.16.255.255/16".
+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.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
address:
@@ -258,7 +258,7 @@ address:
So we see that the netmask essentially specifies a range for IP
addresses that are included in a network: all the addresses between
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
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
host IP or a network number (or a broadcast address): the #each method
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:
ip = IPAddress "172.16.10.1/24"
@@ -280,7 +280,7 @@ method:
puts host
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
ip = IPAddress "172.16.10.100/24"
@@ -291,6 +291,28 @@ respectively the first and the last host address in the range
ip.last.to_string
#=> "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
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
only take into consideration the address portion of an IPv4 object and
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
#=> "10101100000100000000101000000001"
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
#=> 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
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
#=> "\254\020\n\001"
@@ -333,7 +355,7 @@ suitable to use in IPv4-IPv6 mapped addresses:
ip.to_ipv6
#=> "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
#=> "ac100a01"
@@ -344,7 +366,7 @@ IPAddress allows you to create and manipulate objects using the old
and deprecated (but apparently still popular) classful networks concept.
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:
* 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:
* IPv4#subnet: specify a new prefix
* 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
easy:
@@ -426,7 +448,7 @@ representing the new subnets.
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.
-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")
@@ -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"]
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
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
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
-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
as the previous examples:
@@ -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
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
-"172.16.10.128/26", as you would end up having "172.16.10.192/26" wasted (plus,
+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,
I suppose I wouldn't need a Ruby library to perform un-efficient IP
allocation, as I do that myself very well ;) ).
@@ -478,8 +500,8 @@ We can go even further and split into 11 subnets:
"172.16.10.96/28", "172.16.10.112/28", "172.16.10.128/27",
"172.16.10.160/27", "172.16.10.192/26"]
-As you can see, most of the networks are /28, with a few /27 and one
-/26 to fill up the remaining space.
+As you can see, most of the networks are `/28`, with a few `/27` and one
+`/26` to fill up the remaining space.
==== Summarization
@@ -510,7 +532,7 @@ network if we change the prefix. Let Ruby do the work:
IPAddress::IPv4::summarize(ip1,ip2).map(&:to_string)
#=> "172.16.10.0/23"
-We note how the network "172.16.10.0/23" includes all the
+We note how the network `172.16.10.0/23` includes all the
addresses specified in the above networks, and (more important) includes
ONLY those addresses.
@@ -527,7 +549,7 @@ is not an aggregate network for +ip1+ and +ip2+.
If it's not possible to compute a single aggregated network for
all the original networks, the method returns an array with all the
aggregate networks found. For example, the following four networks can be
-aggregated in a single /22:
+aggregated in a single `/22`:
ip1 = IPAddress("10.0.0.1/24")
ip2 = IPAddress("10.0.1.1/24")
@@ -549,7 +571,7 @@ network:
#=> ["10.0.1.0/24","10.0.2.0/23","10.0.4.0/24"]
In this case, the two summarizables networks have been aggregated into
-a single /23, while the other two networks have been left untouched.
+a single `/23`, while the other two networks have been left untouched.
==== Supernetting
@@ -568,13 +590,13 @@ you can supernet it with a new /23 prefix
ip.supernet(23).to_string
#=> "172.16.10.0/23"
-However if you supernet it with a /22 prefix, the network address will
+However if you supernet it with a `/22` prefix, the network address will
change:
ip.supernet(22).to_string
#=> "172.16.8.0/22"
-This is because "172.16.10.0/22" is not a network anymore, but an host
+This is because `172.16.10.0/22` is not a network anymore, but an host
address.
== IPv6
@@ -656,7 +678,7 @@ methods:
#=> 64
A compressed version of the IPv6 address can be obtained with the
-IPv6#compressed method:
+`IPv6#compressed` method:
ip6 = IPAddress "2001:0db8:0000:0000:0008:200c:417a:00ab/64"
@@ -666,7 +688,7 @@ IPv6#compressed method:
=== Handling the IPv6 address
Accessing the groups that form an IPv6 address is very easy with the
-IPv6#groups method:
+`IPv6#groups` method:
ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
@@ -674,7 +696,7 @@ IPv6#groups method:
#=> [8193, 3512, 0, 0, 8, 2048, 8204, 16762]
As with IPv4 addresses, each individual group can be accessed using
-the IPv6#[] shortcut method:
+the `IPv6#[]` shortcut method:
ip6[0]
#=> 8193
@@ -686,14 +708,14 @@ the IPv6#[] shortcut method:
#=> 0
Note that each 16 bits group is expressed in its decimal form. You can
-also obtain the groups into hexadecimal format using the IPv6#hexs
+also obtain the groups into hexadecimal format using the `IPv6#hexs`
method:
ip6.hexs
#=> => ["2001", "0db8", "0000", "0000", "0008", "0800", "200c", "417a"]
A few other methods are available to transform an IPv6 address into
-decimal representation, with IPv6.to_i
+decimal representation, with `IPv6.to_i`
ip6.to_i
#=> 42540766411282592856906245548098208122
@@ -703,8 +725,8 @@ or to hexadecimal representation
ip6.to_hex
#=> "20010db80000000000080800200c417a"
-To print out an IPv6 address in human readable form, use the IPv6#to_s, IPv6#to_string
-and IPv6#to_string_uncompressed methods
+To print out an IPv6 address in human readable form, use the `IPv6#to_s`, `IPv6#to_string`
+and `IPv6#to_string_uncompressed` methods
ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
@@ -714,8 +736,8 @@ and IPv6#to_string_uncompressed methods
ip6.to_string_uncompressed
#=> "2001:0db8:0000:0000:0008:0800:200c:417a/96"
-As you can see, IPv6.to_string prints out the compressed form, while
-IPv6.to_string_uncompressed uses the expanded version.
+As you can see, `IPv6.to_string` prints out the compressed form, while
+`IPv6.to_string_uncompressed` uses the expanded version.
==== Compressing and uncompressing
@@ -749,7 +771,7 @@ actually created internally).
You can create a new IPv6 address from different formats than just a
string representing the colon-hex groups.
-For instance, if you have a data stream, you can use IPv6::parse_data,
+For instance, if you have a data stream, you can use `IPv6::parse_data`,
like in the following example:
data = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
@@ -790,7 +812,7 @@ support for unspecified, loopback and mapped IPv6 addresses.
==== Unspecified address
The address with all zero bits is called the +unspecified+ address
-(corresponding to 0.0.0.0 in IPv4). It should be something like this:
+(corresponding to `0.0.0.0` in IPv4). It should be something like this:
0000:0000:0000:0000:0000:0000:0000:0000
@@ -832,7 +854,7 @@ packets with the unspecified address.
==== Loopback address
-The loopback address is a unicast localhost address. If an
+The loopback address is a unicast localhost address. If an
application in a host sends packets to this address, the IPv6 stack
will loop these packets back on the same virtual interface.
@@ -859,13 +881,13 @@ or by using the wrapper:
ip.to_string
#=> "::1/128"
-Checking if an address is loopback is easy with the IPv6#loopback?
+Checking if an address is loopback is easy with the `IPv6#loopback?`
method:
ip.loopback?
#=> true
-The IPv6 loopback address corresponds to 127.0.0.1 in IPv4.
+The IPv6 loopback address corresponds to `127.0.0.1` in IPv4.
==== Mapped address
@@ -875,7 +897,7 @@ structure of the address is
::ffff:w.y.x.z
-where w.x.y.z is a normal IPv4 address. For example, the following is
+where `w.x.y.z` is a normal IPv4 address. For example, the following is
a mapped IPv6 address:
::ffff:192.168.100.1
@@ -899,7 +921,7 @@ Let's check it's really a mapped address:
ip6.to_string
#=> "::ffff:172.16.10.1/128"
-Now with the +ipv4+ attribute, we can easily access the IPv4 portion
+Now with the `#ipv4` attribute, we can easily access the IPv4 portion
of the mapped IPv6 address:
ip6.ipv4.address
@@ -920,7 +942,7 @@ following format:
ip6 = IPAddress "::172.16.10.1"
-That is, two colons and the IPv4 address. However, as by RFC, the ffff
+That is, two colons and the IPv4 address. However, as by RFC, the `ffff`
group will be automatically added at the beginning
ip6.to_string
@@ -973,7 +995,4 @@ feedback and bug reports.
== Copyright
-Copyright (c) 2009-2011 Marco Ceresa. See LICENSE for details.
-
-
-
+Copyright (c) 2009-2015 Marco Ceresa and Mike Mackintosh. See LICENSE for details.
diff --git a/Rakefile b/Rakefile
index b7dbf64..792f86f 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,25 +1,8 @@
require 'rubygems'
require 'rake'
require 'rake/clean'
+require "bundler/gem_tasks"
-begin
- require 'jeweler'
- Jeweler::Tasks.new do |gem|
- gem.name = "ipaddress"
- gem.summary = %Q{IPv4/IPv6 addresses manipulation library}
- gem.email = "ceresa@gmail.com"
- gem.homepage = "http://github.com/bluemonk/ipaddress"
- gem.authors = ["Marco Ceresa"]
- gem.description = <<-EOD
- IPAddress is a Ruby library designed to make manipulation
- of IPv4 and IPv6 addresses both powerful and simple. It mantains
- a layer of compatibility with Ruby's own IPAddr, while
- addressing many of its issues.
- EOD
- end
-rescue LoadError
- puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
-end
require 'rake/testtask'
Rake::TestTask.new(:test) do |test|
diff --git a/VERSION b/VERSION
deleted file mode 100644
index c18d72b..0000000
--- a/VERSION
+++ /dev/null
@@ -1 +0,0 @@
-0.8.1 \ No newline at end of file
diff --git a/ipaddress.gemspec b/ipaddress.gemspec
index 67d3f9b..2f66ccf 100644
--- a/ipaddress.gemspec
+++ b/ipaddress.gemspec
@@ -1,58 +1,26 @@
-# Generated by jeweler
-# DO NOT EDIT THIS FILE DIRECTLY
-# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
-# -*- encoding: utf-8 -*-
+# coding: utf-8
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'ipaddress/version'
-Gem::Specification.new do |s|
- s.name = %q{ipaddress}
- s.version = "0.8.1"
-
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
- s.authors = ["Marco Ceresa"]
- s.date = %q{2011-05-17}
- s.description = %q{ IPAddress is a Ruby library designed to make manipulation
+Gem::Specification.new do |spec|
+ spec.name = "ipaddress"
+ spec.version = Ipaddress::VERSION
+ spec.authors = ["bluemonk", "mikemackintosh"]
+ spec.email = ["ceresa@gmail.com"]
+ spec.summary = %q{IPv4/IPv6 address manipulation library}
+ spec.description = %q{IPAddress is a Ruby library designed to make manipulation
of IPv4 and IPv6 addresses both powerful and simple. It mantains
a layer of compatibility with Ruby's own IPAddr, while
- addressing many of its issues.
-}
- s.email = %q{ceresa@gmail.com}
- s.extra_rdoc_files = [
- "LICENSE",
- "README.rdoc"
- ]
- s.files = [
- ".document",
- "CHANGELOG.rdoc",
- "LICENSE",
- "README.rdoc",
- "Rakefile",
- "VERSION",
- "ipaddress.gemspec",
- "lib/ipaddress.rb",
- "lib/ipaddress/ipv4.rb",
- "lib/ipaddress/ipv6.rb",
- "lib/ipaddress/prefix.rb",
- "lib/ipaddress/mongoid.rb",
- "test/ipaddress/ipv4_test.rb",
- "test/ipaddress/ipv6_test.rb",
- "test/ipaddress/prefix_test.rb",
- "test/ipaddress/mongoid_test.rb",
- "test/ipaddress_test.rb",
- "test/test_helper.rb"
- ]
- s.homepage = %q{http://github.com/bluemonk/ipaddress}
- s.license = 'MIT'
- s.require_paths = ["lib"]
- s.rubygems_version = %q{1.6.2}
- s.summary = %q{IPv4/IPv6 addresses manipulation library}
+ addressing many of its issues.}
+ spec.homepage = "https://github.com/bluemonk/ipaddress"
+ spec.license = "MIT"
- if s.respond_to? :specification_version then
- s.specification_version = 3
+ spec.files = `git ls-files -z`.split("\x0")
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
+ spec.require_paths = ["lib"]
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
- else
- end
- else
- end
+ spec.add_development_dependency "bundler"
+ spec.add_development_dependency "rake"
end
-
diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb
index a32a4a0..7d8e0d3 100644
--- a/lib/ipaddress/ipv4.rb
+++ b/lib/ipaddress/ipv4.rb
@@ -286,6 +286,21 @@ module IPAddress;
@octets[index]
end
alias_method :octet, :[]
+
+ #
+ # Updated the octet specified at index
+ #
+ # ip = IPAddress("172.16.100.50/24")
+ # ip[2] = 200
+ #
+ # #=> #<IPAddress::IPv4:0x00000000000000 @address="172.16.200.1",
+ # #=> @prefix=32, @octets=[172, 16, 200, 1], @u32=2886780929>
+ #
+ def []=(index, value)
+ @octets[index] = value.to_i
+ initialize("#{@octets.join('.')}/#{prefix}")
+ end
+ alias_method :octet=, :[]=
#
# Returns the address portion of an IP in binary format,
@@ -309,7 +324,14 @@ module IPAddress;
# #=> "172.16.10.255"
#
def broadcast
- self.class.parse_u32(broadcast_u32, @prefix)
+ case
+ when prefix <= 30
+ self.class.parse_u32(broadcast_u32, @prefix)
+ when prefix == 31
+ self.class.parse_u32(-1, @prefix)
+ when prefix == 32
+ return self
+ end
end
#
@@ -363,7 +385,14 @@ module IPAddress;
# #=> "192.168.100.1"
#
def first
- self.class.parse_u32(network_u32+1, @prefix)
+ case
+ when prefix <= 30
+ self.class.parse_u32(network_u32+1, @prefix)
+ when prefix == 31
+ self.class.parse_u32(network_u32, @prefix)
+ when prefix == 32
+ return self
+ end
end
#
@@ -388,7 +417,14 @@ module IPAddress;
# #=> "192.168.100.254"
#
def last
- self.class.parse_u32(broadcast_u32-1, @prefix)
+ case
+ when prefix <= 30
+ self.class.parse_u32(broadcast_u32-1, @prefix)
+ when prefix == 31
+ self.class.parse_u32(broadcast_u32, @prefix)
+ when prefix == 32
+ return self
+ end
end
#
@@ -585,6 +621,48 @@ module IPAddress;
end
#
+ # Checks if an IPv4 address objects belongs
+ # to a multicast network RFC3171
+ #
+ # Example:
+ #
+ # ip = IPAddress "224.0.0.0/4"
+ # ip.multicast?
+ # #=> true
+ #
+ def multicast?
+ [self.class.new("224.0.0.0/4")].any? {|i| i.include? self}
+ end
+
+ #
+ # Checks if an IPv4 address objects belongs
+ # to a multicast network RFC3171
+ #
+ # Example:
+ #
+ # ip = IPAddress "224.0.0.0/4"
+ # ip.multicast?
+ # #=> true
+ #
+ def multicast?
+ [self.class.new("224.0.0.0/4")].any? {|i| i.include? self}
+ end
+
+ #
+ # Checks if an IPv4 address objects belongs
+ # to a loopback network RFC1122
+ #
+ # Example:
+ #
+ # ip = IPAddress "127.0.0.1"
+ # ip.loopback?
+ # #=> true
+ #
+ def loopback?
+ [self.class.new("127.0.0.0/8")].any? {|i| i.include? self}
+ end
+
+ #
# Returns the IP address in in-addr.arpa format
# for DNS lookups
#
@@ -599,6 +677,26 @@ module IPAddress;
alias_method :arpa, :reverse
#
+ # Return a list of IP's between @address
+ # and the supplied IP
+ #
+ # ip = IPAddress("172.16.100.51/32")
+ #
+ # ip.to("172.16.100.100")
+ # #=> ["172.16.100.51",
+ # #=> "172.16.100.52",
+ # #=> ...
+ # #=> "172.16.100.99",
+ # #=> "172.16.100.100"]
+ #
+ def to(e)
+ unless e.is_a? IPAddress::IPv4
+ e = IPv4.new(e)
+ end
+
+ Range.new(@u32, e.to_u32).map{|i| IPAddress.ntoa(i) }
+ end
+ #
# Splits a network into different subnets
#
# If the IP Address is a network, it can be divided into
@@ -613,9 +711,9 @@ module IPAddress;
#
# network / 4 # implies map{|i| i.to_string}
# #=> ["172.16.10.0/26",
- # "172.16.10.64/26",
- # "172.16.10.128/26",
- # "172.16.10.192/26"]
+ # #=> "172.16.10.64/26",
+ # #=> "172.16.10.128/26",
+ # #=> "172.16.10.192/26"]
#
# If +num+ is any other number, the supernet will be
# divided into some networks with a even number of hosts and
@@ -625,8 +723,8 @@ module IPAddress;
#
# network / 3 # implies map{|i| i.to_string}
# #=> ["172.16.10.0/26",
- # "172.16.10.64/26",
- # "172.16.10.128/25"]
+ # #=> "172.16.10.64/26",
+ # #=> "172.16.10.128/25"]
#
# Returns an array of IPv4 objects
#
@@ -984,12 +1082,9 @@ module IPAddress;
#
private
+ # Tweaked to remove the #upto(32)
def newprefix(num)
- num.upto(32) do |i|
- if (a = Math::log2(i).to_i) == Math::log2(i)
- return @prefix + a
- end
- end
+ return @prefix + (Math::log2(num).ceil )
end
def sum_first_found(arr)
diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb
index 33d4d19..88098bc 100644
--- a/lib/ipaddress/ipv6.rb
+++ b/lib/ipaddress/ipv6.rb
@@ -252,6 +252,15 @@ module IPAddress;
end
alias_method :group, :[]
+ #
+ # Updated the octet specified at index
+ #
+ def []=(index, value)
+ @groups[index] = value
+ initialize("#{IN6FORMAT % @groups}/#{prefix}")
+ end
+ alias_method :group=, :[]=
+
#
# Returns a Base16 number representing the IPv6
# address
diff --git a/lib/ipaddress/version.rb b/lib/ipaddress/version.rb
new file mode 100644
index 0000000..6aa96de
--- /dev/null
+++ b/lib/ipaddress/version.rb
@@ -0,0 +1,3 @@
+module Ipaddress
+ VERSION = "0.8.2"
+end
diff --git a/test/ipaddress/ipv4_test.rb b/test/ipaddress/ipv4_test.rb
index bf969ad..19264e2 100644
--- a/test/ipaddress/ipv4_test.rb
+++ b/test/ipaddress/ipv4_test.rb
@@ -26,7 +26,8 @@ class IPv4Test < Minitest::Test
"10.0.0.0/8" => "255.0.0.0",
"172.16.0.0/16" => "255.255.0.0",
"192.168.0.0/24" => "255.255.255.0",
- "192.168.100.4/30" => "255.255.255.252"}
+ "192.168.100.4/30" => "255.255.255.252",
+ "192.168.12.4/32" => "255.255.255.255"}
@decimal_values ={
"0.0.0.0/0" => 0,
@@ -48,13 +49,17 @@ class IPv4Test < Minitest::Test
"10.0.0.0/8" => "10.255.255.255/8",
"172.16.0.0/16" => "172.16.255.255/16",
"192.168.0.0/24" => "192.168.0.255/24",
- "192.168.100.4/30" => "192.168.100.7/30"}
+ "192.168.100.4/30" => "192.168.100.7/30",
+ "192.168.12.3/31" => "255.255.255.255/31",
+ "10.0.0.1/32" => "10.0.0.1/32"}
@networks = {
"10.5.4.3/8" => "10.0.0.0/8",
"172.16.5.4/16" => "172.16.0.0/16",
"192.168.4.3/24" => "192.168.4.0/24",
- "192.168.100.5/30" => "192.168.100.4/30"}
+ "192.168.100.5/30" => "192.168.100.4/30",
+ "192.168.1.3/31" => "192.168.1.2/31",
+ "192.168.2.5/32" => "192.168.2.5/32"}
@class_a = @klass.new("10.0.0.1/8")
@class_b = @klass.new("172.16.0.1/16")
@@ -64,6 +69,11 @@ class IPv4Test < Minitest::Test
"10.1.1.1" => 8,
"150.1.1.1" => 16,
"200.1.1.1" => 24 }
+
+ @in_range = {
+ "10.32.0.1" => ["10.32.0.253", 253],
+ "192.0.0.0" => ["192.1.255.255", 131072]
+ }
end
@@ -189,6 +199,12 @@ class IPv4Test < Minitest::Test
ip = @klass.new("192.168.100.50/24")
assert_instance_of @klass, ip.first
assert_equal "192.168.100.1", ip.first.to_s
+ ip = @klass.new("192.168.100.50/32")
+ assert_instance_of @klass, ip.first
+ assert_equal "192.168.100.50", ip.first.to_s
+ ip = @klass.new("192.168.100.50/31")
+ assert_instance_of @klass, ip.first
+ assert_equal "192.168.100.50", ip.first.to_s
end
def test_method_last
@@ -198,6 +214,12 @@ class IPv4Test < Minitest::Test
ip = @klass.new("192.168.100.50/24")
assert_instance_of @klass, ip.last
assert_equal "192.168.100.254", ip.last.to_s
+ ip = @klass.new("192.168.100.50/32")
+ assert_instance_of @klass, ip.last
+ assert_equal "192.168.100.50", ip.last.to_s
+ ip = @klass.new("192.168.100.50/31")
+ assert_instance_of @klass, ip.last
+ assert_equal "192.168.100.51", ip.last.to_s
end
def test_method_each_host
@@ -542,6 +564,37 @@ class IPv4Test < Minitest::Test
assert_raises(ArgumentError){ @klass.parse_classful("192.168.256.257") }
end
+ def test_network_split
+ @classful.each do |ip,net|
+ x = @klass.new("#{ip}/#{net}")
+ assert_equal x.split(1).length, 1
+ assert_equal x.split(2).length, 2
+ assert_equal x.split(32).length, 32
+ assert_equal x.split(256).length, 256
+ end
+ end
+
+ def test_in_range
+ @in_range.each do |s,d|
+ ip = @klass.new(s)
+ assert_equal ip.to(d[0]).length, d[1]
+ end
+ end
+
+ def test_octect_updates
+ ip = @klass.new("10.0.1.15/32")
+ ip[1] = 15
+ assert_equal "10.15.1.15/32", ip.to_string
+
+ ip = @klass.new("172.16.100.1")
+ ip[3] = 200
+ assert_equal "172.16.100.200/32", ip.to_string
+
+ ip = @klass.new("192.168.199.0/24")
+ ip[2] = 200
+ assert_equal "192.168.200.0/24", ip.to_string
+ end
+
end # class IPv4Test
diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb
index f0932d7..dcfb601 100644
--- a/test/ipaddress/ipv6_test.rb
+++ b/test/ipaddress/ipv6_test.rb
@@ -292,6 +292,12 @@ class IPv6Test < Minitest::Test
assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
end
+ def test_group_updates
+ ip = @klass.new("2001:db8::8:800:200c:417a/64")
+ ip[2] = '1234'
+ assert_equal "2001:db8:4d2:0:8:800:200c:417a/64", ip.to_string
+ end
+
end # class IPv6Test
class IPv6UnspecifiedTest < Minitest::Test
@@ -416,5 +422,5 @@ class IPv6MappedTest < Minitest::Test
def test_mapped?
assert_equal true, @ip.mapped?
end
-
+
end # class IPv6MappedTest