Class: IPAddress::IPv6::Mapped
- Inherits:
-
IPAddress::IPv6
- Object
- IPAddress::IPv6
- IPAddress::IPv6::Mapped
- Defined in:
- lib/ipaddress/ipv6.rb
Overview
It is usually identified as a IPv4 mapped IPv6 address, a particular IPv6 address which aids the transition from IPv4 to IPv6. The 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 a mapped IPv6 address:
::ffff:192.168.100.1
IPAddress is very powerful in handling mapped IPv6 addresses, as the IPv4 portion is stored internally as a normal IPv4 object. Let’s have a look at some examples. To create a new mapped address, just use the class builder itself
ip6 = IPAddress::IPv6::Mapped.new "::ffff:172.16.10.1/128"
or just use the wrapper method
ip6 = IPAddress "::ffff:172.16.10.1/128"
Let’s check it’s really a mapped address:
ip6.mapped?
#=> true
ip6.to_string
#=> "::FFFF:172.16.10.1/128"
Now with the ipv4
attribute, we can easily access the IPv4 portion of the mapped IPv6 address:
ip6.ipv4.address
#=> "172.16.10.1"
Internally, the IPv4 address is stored as two 16 bits groups. Therefore all the usual methods for an IPv6 address are working perfectly fine:
ip6.to_hex
#=> "00000000000000000000ffffac100a01"
ip6.address
#=> "0000:0000:0000:0000:0000:ffff:ac10:0a01"
A mapped IPv6 can also be created just by specify the address in the following format:
ip6 = IPAddress "::172.16.10.1"
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
=> "::ffff:172.16.10.1/128"
making it a mapped IPv6 compatible address.
Constant Summary
Constants inherited from IPAddress::IPv6
Constants included from IPAddress
Instance Attribute Summary collapse
-
#ipv4 ⇒ Object
readonly
Access the internal IPv4 address.
Instance Method Summary collapse
-
#initialize(str) ⇒ Mapped
constructor
Creates a new IPv6 IPv4-mapped address.
-
#mapped? ⇒ Boolean
Checks if the IPv6 address is IPv4 mapped.
-
#to_s ⇒ Object
Similar to IPv6#to_s, but prints out the IPv4 address in dotted decimal format.
-
#to_string ⇒ Object
Similar to IPv6#to_string, but prints out the IPv4 address in dotted decimal format.
Methods inherited from IPAddress::IPv6
#<=>, #[], #address, #bits, #broadcast_u128, compress, #compressed, #data, #each, expand, #groups, groups, #hexs, #include?, #literal, #loopback?, #network, #network?, #network_u128, parse_data, parse_hex, parse_u128, #prefix, #prefix=, #reverse, #size, #to_hex, #to_i, #to_string_uncompressed, #unspecified?
Methods included from IPAddress
deprecate, #ipv4?, #ipv6?, parse, valid?, valid_ipv4?, valid_ipv4_netmask?, valid_ipv6?
Constructor Details
#initialize(str) ⇒ Mapped
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_string
#=> "::ffff:13.1.68.3"
834 835 836 837 838 839 840 841 842 843 |
# File 'lib/ipaddress/ipv6.rb', line 834 def initialize(str) string, netmask = str.split("/") 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 |
Instance Attribute Details
#ipv4 ⇒ Object (readonly)
Access the internal IPv4 address
816 817 818 |
# File 'lib/ipaddress/ipv6.rb', line 816 def ipv4 @ipv4 end |
Instance Method Details
#mapped? ⇒ Boolean
Checks if the IPv6 address is IPv4 mapped
ip6 = IPAddress "::ffff:172.16.10.1/128"
ip6.mapped?
#=> true
880 881 882 |
# File 'lib/ipaddress/ipv6.rb', line 880 def mapped? true end |
#to_s ⇒ Object
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"
854 855 856 |
# File 'lib/ipaddress/ipv6.rb', line 854 def to_s "::ffff:#{@ipv4.address}" end |
#to_string ⇒ Object
Similar to IPv6#to_string, but prints out the IPv4 address in dotted decimal format
ip6 = IPAddress "::ffff:172.16.10.1/128"
ip6.to_string
#=> "::ffff:172.16.10.1/128"
868 869 870 |
# File 'lib/ipaddress/ipv6.rb', line 868 def to_string "::ffff:#{@ipv4.address}/#@prefix" end |