class Subnet::IPv6::Mapped

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

Subnet 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 = Subnet::IPv6::Mapped.new "::ffff:172.16.10.1/128"

or just use the wrapper method

ip6 = Subnet.parse "::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.hexstring
# => "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 = Subnet.parse "::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.

Defined in:

subnet/ipv6.cr

Constructors

Instance Method Summary

Instance methods inherited from class Subnet::IPv6

<=>(oth) <=>, [](index) [], []=(index, value) []=, address : String address, allocate(skip = 0) allocate, arpa arpa, bits bits, broadcast_u128 broadcast_u128, compressed : String compressed, data data, each(&block) each, group(index) group, groups : Array(Int32) groups, hex_groups hex_groups, hexstring hexstring, includes?(oth) includes?, link_local? link_local?, literal literal, loopback? loopback?, mapped? mapped?, network network, network? network?, network_u128 network_u128, next next, pred pred, prefix : Prefix128 prefix, prefix=(num) prefix=, reverse reverse, size size, succ succ, to_big_i to_big_i, to_i to_i, to_json(json : JSON::Builder) to_json, to_s : String to_s, to_string to_string, to_string_uncompressed to_string_uncompressed, to_u128 to_u128, unique_local? unique_local?, unspecified? unspecified?

Constructor methods inherited from class Subnet::IPv6

new(str : String)
new(value : JSON::PullParser) : Subnet::IPv6
new

Class methods inherited from class Subnet::IPv6

compress(str) compress, expand(str) expand, groups(str) groups, parse_data(data) parse_data, parse_hex(hexstring, prefix = 128) parse_hex, parse_u128(u128, prefix = 128) parse_u128

Instance methods inherited from module Subnet

ipv4? ipv4?, ipv6? ipv6?, to_json(json : JSON::Builder) to_json

Constructor methods inherited from module Subnet

new(value : JSON::PullParser) : Subnet new, parse(str) : Subnet parse

Class methods inherited from module Subnet

deprecate(message = nil) deprecate, ntoa(uint) ntoa, valid?(addr) valid?, valid_ip?(addr) valid_ip?, valid_ipv4?(addr) valid_ipv4?, valid_ipv4_netmask?(addr) valid_ipv4_netmask?, valid_ipv4_subnet?(addr) valid_ipv4_subnet?, valid_ipv6?(addr) valid_ipv6?, valid_ipv6_subnet?(addr) valid_ipv6_subnet?

Constructor Detail

def self.new(str) #

Creates a new IPv6 IPv4-mapped address

ip6 = Subnet::IPv6::Mapped.new "::ffff:172.16.10.1/128"

ipv6.ipv4.class
# => Subnet::IPv4

An IPv6 IPv4-mapped address can also be created using the IPv6 only format of the address:

ip6 = Subnet::IPv6::Mapped.new "::0d01:4403"

ip6.to_string
# => "::ffff:13.1.68.3"

[View source]

Instance Method Detail

def ipv4 : IPv4 #

The internal IPv4 address.


[View source]
def mapped? #

Checks if the IPv6 address is IPv4 mapped

ip6 = Subnet.parse "::ffff:172.16.10.1/128"

ip6.mapped?
# => true

[View source]
def to_s #

Similar to IPv6#to_s, but prints out the IPv4 address in dotted decimal format

ip6 = Subnet.parse "::ffff:172.16.10.1/128"

ip6.to_s
# => "::ffff:172.16.10.1"

[View source]
def to_string #

Similar to IPv6#to_string, but prints out the IPv4 address in dotted decimal format

ip6 = Subnet.parse "::ffff:172.16.10.1/128"

ip6.to_string
# => "::ffff:172.16.10.1/128"

[View source]