summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbluemonk <ceresa@gmail.com>2010-07-27 21:04:29 +0200
committerbluemonk <ceresa@gmail.com>2010-07-27 21:04:29 +0200
commitf990bb7457d6f5500a3b3ecbc846fe13369661be (patch)
treed2bd3bec2da54ea2d874373742236b5e3361d819
parentcb4a7e147ac189572a5055e42b63cab4b6ded220 (diff)
downloadipaddress-f990bb7457d6f5500a3b3ecbc846fe13369661be.tar.gz
Rewrote IPv6::Mapped to accept mapped IPv4 in IPv6 format
-rw-r--r--CHANGELOG.rdoc1
-rw-r--r--lib/ipaddress/ipv6.rb34
-rw-r--r--test/ipaddress/ipv6_test.rb34
3 files changed, 52 insertions, 17 deletions
diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc
index e446b83..c47d211 100644
--- a/CHANGELOG.rdoc
+++ b/CHANGELOG.rdoc
@@ -2,6 +2,7 @@
NEW:: IPAddress::IPv6#include?
NEW:: IPAddress::IPv6#network_u128
+NEW:: Modified IPAddress::IPv6::Mapped to accept IPv4 mapped addresses in IPv6 format
== ipaddress 0.6.0
diff --git a/lib/ipaddress/ipv6.rb b/lib/ipaddress/ipv6.rb
index 339fe05..78ebe3d 100644
--- a/lib/ipaddress/ipv6.rb
+++ b/lib/ipaddress/ipv6.rb
@@ -88,6 +88,10 @@ module IPAddress;
#
def initialize(str)
ip, netmask = str.split("/")
+
+ if str =~ /:.+\./
+ raise ArgumentError, "Please use #{self.class}::Mapped for IPv4 mapped addresses"
+ end
if IPAddress.valid_ipv6?(ip)
@groups = self.class.groups(ip)
@@ -101,7 +105,6 @@ module IPAddress;
end # def initialize
-
#
# Returns the IPv6 address in uncompressed form:
#
@@ -379,7 +382,7 @@ module IPAddress;
# See IPAddress::IPv6::Mapped for more information
#
def mapped?
- false
+ to_u128 >> 32 == 0xffff
end
#
@@ -703,13 +706,29 @@ module IPAddress;
attr_reader :ipv4
#
- # Creates a new IPv6 unspecified address
+ # Creates a new IPv6 IPv4-mapped address
#
# ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
#
+ # ipv6.ipv4.class
+ # #=> IPAddress::IPv4
+ #
+ # An IPv6 IPv4-mapped address can also be created using the
+ # IPv6 only format of the address:
+ #
+ # ip6 = IPAddress::IPv6::Mapped.new "::0d01:4403"
+ #
+ # ip6.to_s
+ # #=> "::ffff:13.1.68.3"
+ #
def initialize(str)
string, netmask = str.split("/")
- @ipv4 = IPAddress::IPv4.extract(string)
+ if string =~ /\./ # IPv4 in dotted decimal form
+ @ipv4 = IPAddress::IPv4.extract(string)
+ else # IPv4 in hex form
+ groups = IPAddress::IPv6.groups(string)
+ @ipv4 = IPAddress::IPv4.parse_u32((groups[-2]<< 16)+groups[-1])
+ end
super("::ffff:#{@ipv4.to_ipv6}/#{netmask}")
end
@@ -717,11 +736,10 @@ module IPAddress;
# Similar to IPv6#to_s, but prints out the IPv4 address
# in dotted decimal format
#
- #
# ip6 = IPAddress "::ffff:172.16.10.1/128"
#
# ip6.to_s
- # #=> "::FFFF:172.16.10.1"
+ # #=> "::ffff:172.16.10.1"
#
def to_s
"::ffff:#{@ipv4.address}"
@@ -734,8 +752,8 @@ module IPAddress;
#
# ip6 = IPAddress "::ffff:172.16.10.1/128"
#
- # ip6.to_s
- # #=> "::FFFF:172.16.10.1/128"
+ # ip6.to_string
+ # #=> "::ffff:172.16.10.1/128"
#
def to_string
"::ffff:#{@ipv4.address}/#@prefix"
diff --git a/test/ipaddress/ipv6_test.rb b/test/ipaddress/ipv6_test.rb
index 3d6d342..e46a6b2 100644
--- a/test/ipaddress/ipv6_test.rb
+++ b/test/ipaddress/ipv6_test.rb
@@ -53,6 +53,10 @@ class IPv6Test < Test::Unit::TestCase
assert_raise(ArgumentError) {@klass.new ip}
end
assert_equal 64, @ip.prefix
+
+ assert_raise(ArgumentError) {
+ @klass.new "::10.1.1.1"
+ }
end
def test_attribute_groups
@@ -87,6 +91,8 @@ class IPv6Test < Test::Unit::TestCase
def test_method_mapped?
assert_equal false, @ip.mapped?
+ ip6 = @klass.new "::ffff:1234:5678"
+ assert_equal true, ip6.mapped?
end
def test_method_literal
@@ -150,20 +156,11 @@ class IPv6Test < Test::Unit::TestCase
assert_equal str, @ip.data
end
-
def test_method_reverse
str = "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"
assert_equal str, @klass.new("3ffe:505:2::f").reverse
end
-# def test_ip6_int
-# assert_equal("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.int", IPAddress("3ffe:505:2::f").ip6_int)
- # assert_raises(ArgumentError) {
-# IPAddress("192.168.2.1").ip6_int
- # }
-# end
-
-
def test_method_compressed
assert_equal "1:1:1::1", @klass.new("1:1:1:0:0:0:0:1").compressed
assert_equal "1:0:1::1", @klass.new("1:0:1:0:0:0:0:1").compressed
@@ -309,6 +306,15 @@ class IPv6MappedTest < Test::Unit::TestCase
@valid_mapped = {'::13.1.68.3' => 281470899930115,
'0:0:0:0:0:ffff:129.144.52.38' => 281472855454758,
'::ffff:129.144.52.38' => 281472855454758}
+
+ @valid_mapped_ipv6 = {'::0d01:4403' => 281470899930115,
+ '0:0:0:0:0:ffff:8190:3426' => 281472855454758,
+ '::ffff:8190:3426' => 281472855454758}
+
+ @valid_mapped_ipv6_conversion = {'::0d01:4403' => "13.1.68.3",
+ '0:0:0:0:0:ffff:8190:3426' => "129.144.52.38",
+ '::ffff:8190:3426' => "129.144.52.38"}
+
end
def test_initialize
@@ -318,6 +324,16 @@ class IPv6MappedTest < Test::Unit::TestCase
assert_nothing_raised {@klass.new ip}
assert_equal u128, @klass.new(ip).to_u128
end
+ @valid_mapped_ipv6.each do |ip, u128|
+ assert_nothing_raised {@klass.new ip}
+ assert_equal u128, @klass.new(ip).to_u128
+ end
+ end
+
+ def test_mapped_from_ipv6_conversion
+ @valid_mapped_ipv6_conversion.each do |ip6,ip4|
+ assert_equal ip4, @klass.new(ip6).ipv4.to_s
+ end
end
def test_attributes