summaryrefslogtreecommitdiff
path: root/lib
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 /lib
parentcb4a7e147ac189572a5055e42b63cab4b6ded220 (diff)
downloadipaddress-f990bb7457d6f5500a3b3ecbc846fe13369661be.tar.gz
Rewrote IPv6::Mapped to accept mapped IPv4 in IPv6 format
Diffstat (limited to 'lib')
-rw-r--r--lib/ipaddress/ipv6.rb34
1 files changed, 26 insertions, 8 deletions
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"