diff options
-rw-r--r-- | CHANGELOG.rdoc | 4 | ||||
-rw-r--r-- | README.rdoc | 3 | ||||
-rw-r--r-- | lib/ipaddress/ipv4.rb | 24 | ||||
-rw-r--r-- | lib/ipaddress/ipv6.rb | 62 | ||||
-rw-r--r-- | lib/ipaddress/prefix.rb | 13 | ||||
-rw-r--r-- | test/ipaddress/ipv6_test.rb | 27 |
6 files changed, 105 insertions, 28 deletions
diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 24f8398..254c00a 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,7 +1,9 @@ == ipaddress 0.8.0 NEW:: IPv6#network - +NEW:: Prefix128#host_prefix +NEW:: IPv6#broadcast_u128 +NEW:: IPv6#each == ipaddress 0.7.5 diff --git a/README.rdoc b/README.rdoc index 411526e..24e1a90 100644 --- a/README.rdoc +++ b/README.rdoc @@ -925,7 +925,8 @@ Thanks to Luca Russo (vargolo) and Simone Carletti (weppos) for all the support and technical review. Thanks to Marco Beri, Bryan T. Richardson, Nicolas Fevrier, jdpace, Daniele Alessandri, jrdioko, Ghislain Charrier, Pawel Krzesniak, Mark Sullivan, Leif Gensert, -Erik Ahlström and Steve Rawlinson for their support, feedback and bug reports. +Erik Ahlström, Peter Vandenberk and Steve Rawlinson for their support, +feedback and bug reports. == Copyright diff --git a/lib/ipaddress/ipv4.rb b/lib/ipaddress/ipv4.rb index 2a0ca65..d895cff 100644 --- a/lib/ipaddress/ipv4.rb +++ b/lib/ipaddress/ipv4.rb @@ -553,30 +553,6 @@ module IPAddress; end # - # True if the object is an IPv4 address - # - # ip = IPAddress("192.168.10.100/24") - # - # ip.ipv4? - # #-> true - # -# def ipv4? -# true -# end - - # - # True if the object is an IPv6 address - # - # ip = IPAddress("192.168.10.100/24") - # - # ip.ipv6? - # #-> false - # -# def ipv6? -# false -# end - - # # Checks if an IPv4 address objects belongs # to a private network RFC1918 # diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb index c0823ea..eb4b13c 100644 --- a/lib/ipaddress/ipv6.rb +++ b/lib/ipaddress/ipv6.rb @@ -329,6 +329,36 @@ module IPAddress; end # + # Returns the broadcast address in Unsigned 128bits format + # + # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" + # + # ip6.broadcast_u128 + # #=> 42540766411282592875350729025363378175 + # + # Please note that there is no Broadcast concept in IPv6 + # addresses as in IPv4 addresses, and this method is just + # an helper to other functions. + # + def broadcast_u128 + network_u128 + size - 1 + end + + # + # Returns the number of IP addresses included + # in the network. It also counts the network + # address and the broadcast address. + # + # ip6 = IPAddress("2001:db8::8:800:200c:417a/64") + # + # ip6.size + # #=> 18446744073709551616 + # + def size + 2 ** @prefix.host_prefix + end + + # # Checks whether a subnet includes the given IP address. # # Example: @@ -384,7 +414,37 @@ module IPAddress; def mapped? to_u128 >> 32 == 0xffff end - + + # + # Iterates over all the IP addresses for the given + # network (or IP address). + # + # The object yielded is a new IPv6 object created + # from the iteration. + # + # ip6 = IPAddress("2001:db8::4/125") + # + # ip6.each do |i| + # p i.compressed + # end + # #=> "2001:db8::" + # #=> "2001:db8::1" + # #=> "2001:db8::2" + # #=> "2001:db8::3" + # #=> "2001:db8::4" + # #=> "2001:db8::5" + # #=> "2001:db8::6" + # #=> "2001:db8::7" + # + # WARNING: if the host portion is very large, this method + # can be very slow and possibly hang your system! + # + def each + (network_u128..broadcast_u128).each do |i| + yield self.class.parse_u128(i, @prefix) + end + end + # # Returns the address portion of an IP in binary format, # as a string containing a sequence of 0 and 1 diff --git a/lib/ipaddress/prefix.rb b/lib/ipaddress/prefix.rb index aac2090..688b962 100644 --- a/lib/ipaddress/prefix.rb +++ b/lib/ipaddress/prefix.rb @@ -247,6 +247,19 @@ module IPAddress bits.to_i(2) end + # + # Returns the length of the host portion + # of a netmask. + # + # prefix = Prefix128.new 96 + # + # prefix.host_prefix + # #=> 32 + # + def host_prefix + 128 - @prefix + end + end # class Prefix123 < Prefix end # module IPAddress diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index 4c84e4b..898938f 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -127,7 +127,22 @@ class IPv6Test < Test::Unit::TestCase def test_method_network_u128 assert_equal 42540766411282592856903984951653826560, @ip.network_u128 end - + + def test_method_broadcast_u128 + assert_equal 42540766411282592875350729025363378175, @ip.broadcast_u128 + end + + def test_method_size + ip = @klass.new("2001:db8::8:800:200c:417a/64") + assert_equal 2**64, ip.size + ip = @klass.new("2001:db8::8:800:200c:417a/32") + assert_equal 2**96, ip.size + ip = @klass.new("2001:db8::8:800:200c:417a/120") + assert_equal 2**8, ip.size + ip = @klass.new("2001:db8::8:800:200c:417a/124") + assert_equal 2**4, ip.size + end + def test_method_include? assert_equal true, @ip.include?(@ip) # test prefix on same address @@ -199,6 +214,16 @@ class IPv6Test < Test::Unit::TestCase assert_equal net, ip.network.to_string end end + + def test_method_each + ip = @klass.new("2001:db8::4/125") + arr = [] + ip.each {|i| arr << i.compressed} + expected = ["2001:db8::","2001:db8::1","2001:db8::2", + "2001:db8::3","2001:db8::4","2001:db8::5", + "2001:db8::6","2001:db8::7"] + assert_equal expected, arr + end def test_classmethod_expand compressed = "2001:db8:0:cd30::" |