diff options
| author | bluemonk <ceresa@gmail.com> | 2010-07-24 23:35:20 +0200 |
|---|---|---|
| committer | bluemonk <ceresa@gmail.com> | 2010-07-24 23:35:20 +0200 |
| commit | 414fdc0f51f05c36fdf6ec1e3e6e518d28a72a02 (patch) | |
| tree | d92e0d0dbcd759961f1341307aba6585866af069 | |
| parent | c1311addff2340b2d5b043346d56d2855da9df17 (diff) | |
| download | ipaddress-414fdc0f51f05c36fdf6ec1e3e6e518d28a72a02.tar.gz | |
Added IPv6#network_u128 and IPv6#include?
| -rw-r--r-- | CHANGELOG.rdoc | 4 | ||||
| -rw-r--r-- | lib/ipaddress/ipv6.rb | 35 | ||||
| -rw-r--r-- | test/ipaddress/ipv6_test.rb | 24 |
3 files changed, 60 insertions, 3 deletions
diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index 65d800a..e446b83 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -1,3 +1,7 @@ +== master + +NEW:: IPAddress::IPv6#include? +NEW:: IPAddress::IPv6#network_u128 == ipaddress 0.6.0 diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb index 8424d53..889ca99 100644 --- a/lib/ipaddress/ipv6.rb +++ b/lib/ipaddress/ipv6.rb @@ -303,7 +303,7 @@ module IPAddress; # Returns the IPv6 address in a DNS reverse lookup # string, as per RFC3172 and RFC2874. # - # ip6 = IPAddress "3ffe:505:2::f") + # ip6 = IPAddress "3ffe:505:2::f" # # ip6.reverse # #=> "f.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.2.0.0.0.5.0.5.0.e.f.f.3.ip6.arpa" @@ -312,7 +312,38 @@ module IPAddress; to_hex.reverse.gsub(/./){|c| c+"."} + "ip6.arpa" end alias_method :arpa, :reverse - + + # + # Returns the network number in Unsigned 128bits format + # + # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" + # + # ip6.network_u128 + # #=> 42540766411282592856903984951653826560 + # + def network_u128 + to_u128 & @prefix.to_u128 + end + + # + # Checks whether a subnet includes the given IP address. + # + # Example: + # + # ip6 = IPAddress "2001:db8::8:800:200c:417a/64" + # + # addr = IPAddress "2001:db8::8:800:200c:1/128" + # + # + # ip6.include? addr + # #=> true + # + # ip.include? IPAddress("2001:db8:1::8:800:200c:417a/76") + # #=> false + # + def include?(oth) + @prefix <= oth.prefix and network_u128 == self.class.new(oth.address+"/#@prefix").network_u128 + end # # Compressed form of the IPv6 address diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb index 5137440..3d6d342 100644 --- a/test/ipaddress/ipv6_test.rb +++ b/test/ipaddress/ipv6_test.rb @@ -39,7 +39,6 @@ class IPv6Test < Test::Unit::TestCase @hex = "20010db80000000000080800200c417a" end - def test_attribute_address addr = "2001:0db8:0000:0000:0008:0800:200c:417a" assert_equal addr, @ip.address @@ -106,6 +105,29 @@ class IPv6Test < Test::Unit::TestCase assert_equal false, @ip.network? end + def test_method_network_u128 + assert_equal 42540766411282592856903984951653826560, @ip.network_u128 + end + + def test_method_include? + assert_equal true, @ip.include?(@ip) + # test prefix on same address + included = @klass.new "2001:db8::8:800:200c:417a/128" + not_included = @klass.new "2001:db8::8:800:200c:417a/46" + assert_equal true, @ip.include?(included) + assert_equal false, @ip.include?(not_included) + # test address on same prefix + included = @klass.new "2001:db8::8:800:200c:0/64" + not_included = @klass.new "2001:db8:1::8:800:200c:417a/64" + assert_equal true, @ip.include?(included) + assert_equal false, @ip.include?(not_included) + # general test + included = @klass.new "2001:db8::8:800:200c:1/128" + not_included = @klass.new "2001:db8:1::8:800:200c:417a/76" + assert_equal true, @ip.include?(included) + assert_equal false, @ip.include?(not_included) + end + def test_method_to_hex assert_equal @hex, @ip.to_hex end |
