Class | IPAddress::IPv6 |
In: |
lib/ipaddress/ipv6.rb
|
Parent: | Object |
IPAddress::IPv6 - IP version 6 address manipulation library
require 'ipaddress'
Class IPAddress::IPv6 is used to handle IPv6 type addresses.
IPv6 addresses are 128 bits long, in contrast with IPv4 addresses which are only 32 bits long. An IPv6 address is generally written as eight groups of four hexadecimal digits, each group representing 16 bits or two octect. For example, the following is a valid IPv6 address:
1080:0000:0000:0000:0008:0800:200c:417a
Letters in an IPv6 address are usually written downcase, as per RFC. You can create a new IPv6 object using uppercase letters, but they will be converted.
Since IPv6 addresses are very long to write, there are some semplifications and compressions that you can use to shorten them.
Using compression, the IPv6 address written above can be shorten into the following, equivalent, address
1080::8:800:200c:417a
This short version is often used in human representation.
As we used to do with IPv4 addresses, an IPv6 address can be written using the prefix notation to specify the subnet mask:
1080::8:800:200c:417a/64
The /64 part means that the first 64 bits of the address are representing the network portion, and the last 64 bits are the host portion.
IN6FORMAT | = | ("%.4x:"*8).chop | Format string to pretty print IPv6 addresses |
Compress an IPv6 address in its compressed form
IPAddress::IPv6.compress "2001:0DB8:0000:CD30:0000:0000:0000:0000" #=> "2001:db8:0:cd30::"
Creates a new IPv6 address object.
An IPv6 address can be expressed in any of the following forms:
In all these 3 cases, a new IPv6 address object will be created, using the default subnet mask /128
You can also specify the subnet mask as with IPv4 addresses:
ip6 = IPAddress "2001:db8::8:800:200c:417a/64"
Creates a new IPv6 object from binary data, like the one you get from a network stream.
For example, on a network stream the IP
"2001:db8::8:800:200c:417a"
is represented with the binary data
" \001\r\270\000\000\000\000\000\b\b\000 \fAz"
With that data you can create a new IPv6 object:
ip6 = IPAddress::IPv6::parse_data " \001\r\270\000\000\000\000\000\b\b\000 \fAz" ip6.prefix = 64 ip6.to_s #=> "2001:db8::8:800:200c:417a/64"
Creates a new IPv6 object from a number expressed in hexdecimal format:
ip6 = IPAddress::IPv6::parse_hex("20010db80000000000080800200c417a") ip6.prefix = 64 ip6.to_s #=> "2001:db8::8:800:200c:417a/64"
The prefix parameter is optional:
ip6 = IPAddress::IPv6::parse_hex("20010db80000000000080800200c417a", 64) ip6.to_s #=> "1080::8:800:200c:417a/64"
Creates a new IPv6 object from an unsigned 128 bits integer.
ip6 = IPAddress::IPv6::parse_u128(21932261930451111902915077091070067066) ip6.prefix = 64 ip6.to_s #=> "1080::8:800:200c:417a/64"
The prefix parameter is optional:
ip6 = IPAddress::IPv6::parse_u128(21932261930451111902915077091070067066, 64) ip6.to_s #=> "1080::8:800:200c:417a/64"
Returns the 16-bits value specified by index
ip = IPAddress("2001:db8::8:800:200c:417a/64") ip[0] #=> 8193 ip[1] #=> 3512 ip[2] #=> 0 ip[3] #=> 0
Returns the address portion of an IP in binary format, as a string containing a sequence of 0 and 1
ip6 = IPAddress("2001:db8::8:800:200c:417a") ip6.bits #=> "0010000000000001000011011011100000 [...] "
Returns the address portion of an IPv6 object in a network byte order format.
ip6 = IPAddress "2001:db8::8:800:200c:417a/64" ip6.data #=> " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
It is usually used to include an IP address in a data packet to be sent over a socket
a = Socket.open(params) # socket details here ip6 = IPAddress "2001:db8::8:800:200c:417a/64" binary_data = ["Address: "].pack("a*") + ip.data # Send binary data a.puts binary_data
Returns an array of the 16 bits groups in hexdecimal format:
ip6 = IPAddress "2001:db8::8:800:200c:417a/64" ip6.hexs #=> ["2001", "0db8", "0000", "0000", "0008", "0800", "200c", "417a"]
Not to be confused with the similar IPv6#to_hex method.
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 ip6.include? IPAddress("2001:db8:1::8:800:200c:417a/76") #=> false
Returns true if the address is a loopback address
See IPAddress::IPv6::Loopback for more information
Returns true if the address is a mapped address
See IPAddress::IPv6::Mapped for more information
Returns the network number in Unsigned 128bits format
ip6 = IPAddress "2001:db8::8:800:200c:417a/64" ip6.network_u128 #=> 42540766411282592856903984951653826560
Returns an instance of the prefix object
ip6 = IPAddress "2001:db8::8:800:200c:417a/64" ip6.prefix #=> 64
Set a new prefix number for the object
This is useful if you want to change the prefix to an object created with IPv6::parse_u128 or if the object was created using the default prefix of 128 bits.
ip6 = IPAddress("2001:db8::8:800:200c:417a") puts ip6.to_string #=> "2001:db8::8:800:200c:417a/128" ip6.prefix = 64 puts ip6.to_string #=> "2001:db8::8:800:200c:417a/64"
Returns the IPv6 address in a human readable form, using the compressed address.
ip6 = IPAddress "2001:db8::8:800:200c:417a/64" ip6.to_s #=> "2001:db8::8:800:200c:417a"
Returns the IPv6 address in a human readable form, using the compressed address.
ip6 = IPAddress "2001:0db8:0000:0000:0008:0800:200c:417a/64" ip6.to_string #=> "2001:db8::8:800:200c:417a/64"
Unlike its counterpart IPv6#to_string method, IPv6#to_string_uncompressed returns the whole IPv6 address and prefix in an uncompressed form
ip6 = IPAddress "2001:db8::8:800:200c:417a/64" ip6.to_string_uncompressed #=> "2001:0db8:0000:0000:0008:0800:200c:417a/64"
Returns true if the address is an unspecified address
See IPAddress::IPv6::Unspecified for more information