Class: Resolv::IPv6
- Inherits:
-
Object
- Object
- Resolv::IPv6
- Defined in:
- lib/rubysl/resolv/resolv.rb
Overview
A Resolv::DNS IPv6 address.
Constant Summary collapse
- Regex_8Hex =
IPv6 address format a:b:c:d:e:f:g:h
/\A (?:[0-9A-Fa-f]{1,4}:){7} [0-9A-Fa-f]{1,4} \z/x
- Regex_CompressedHex =
Compressed IPv6 address format a::b
/\A ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) \z/x
- Regex_6Hex4Dec =
IPv4 mapped IPv6 address format a:b:c:d:e:f:w.x.y.z
/\A ((?:[0-9A-Fa-f]{1,4}:){6,6}) (\d+)\.(\d+)\.(\d+)\.(\d+) \z/x
- Regex_CompressedHex4Dec =
Compressed IPv4 mapped IPv6 address format a::b:w.x.y.z
/\A ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) :: ((?:[0-9A-Fa-f]{1,4}:)*) (\d+)\.(\d+)\.(\d+)\.(\d+) \z/x
- Regex =
A composite IPv6 address Regexp.
/ (?:#{Regex_8Hex}) | (?:#{Regex_CompressedHex}) | (?:#{Regex_6Hex4Dec}) | (?:#{Regex_CompressedHex4Dec})/x
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
The raw IPv6 address as a String.
Class Method Summary collapse
-
.create(arg) ⇒ Object
Creates a new IPv6 address from
arg
which may be:.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#eql?(other) ⇒ Boolean
:nodoc:.
-
#hash ⇒ Object
:nodoc:.
-
#initialize(address) ⇒ IPv6
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#to_name ⇒ Object
Turns this IPv6 address into a Resolv::DNS::Name.
-
#to_s ⇒ Object
:nodoc:.
Constructor Details
#initialize(address) ⇒ IPv6
:nodoc:
2198 2199 2200 2201 2202 2203 |
# File 'lib/rubysl/resolv/resolv.rb', line 2198 def initialize(address) # :nodoc: unless address.kind_of?(String) && address.length == 16 raise ArgumentError.new('IPv6 address must be 16 bytes') end @address = address end |
Instance Attribute Details
#address ⇒ Object (readonly)
The raw IPv6 address as a String.
2208 2209 2210 |
# File 'lib/rubysl/resolv/resolv.rb', line 2208 def address @address end |
Class Method Details
.create(arg) ⇒ Object
Creates a new IPv6 address from arg
which may be:
- IPv6
-
returns
arg
. - String
-
arg
must match one of the IPv6::Regex* constants
2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 |
# File 'lib/rubysl/resolv/resolv.rb', line 2152 def self.create(arg) case arg when IPv6 return arg when String address = '' if Regex_8Hex =~ arg arg.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')} elsif Regex_CompressedHex =~ arg prefix = $1 suffix = $2 a1 = '' a2 = '' prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')} suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')} omitlen = 16 - a1.length - a2.length address << a1 << "\0" * omitlen << a2 elsif Regex_6Hex4Dec =~ arg prefix, a, b, c, d = $1, $2.to_i, $3.to_i, $4.to_i, $5.to_i if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d prefix.scan(/[0-9A-Fa-f]+/) {|hex| address << [hex.hex].pack('n')} address << [a, b, c, d].pack('CCCC') else raise ArgumentError.new("not numeric IPv6 address: " + arg) end elsif Regex_CompressedHex4Dec =~ arg prefix, suffix, a, b, c, d = $1, $2, $3.to_i, $4.to_i, $5.to_i, $6.to_i if (0..255) === a && (0..255) === b && (0..255) === c && (0..255) === d a1 = '' a2 = '' prefix.scan(/[0-9A-Fa-f]+/) {|hex| a1 << [hex.hex].pack('n')} suffix.scan(/[0-9A-Fa-f]+/) {|hex| a2 << [hex.hex].pack('n')} omitlen = 12 - a1.length - a2.length address << a1 << "\0" * omitlen << a2 << [a, b, c, d].pack('CCCC') else raise ArgumentError.new("not numeric IPv6 address: " + arg) end else raise ArgumentError.new("not numeric IPv6 address: " + arg) end return IPv6.new(address) else raise ArgumentError.new("cannot interpret as IPv6 address: #{arg.inspect}") end end |
Instance Method Details
#==(other) ⇒ Object
:nodoc:
2232 2233 2234 |
# File 'lib/rubysl/resolv/resolv.rb', line 2232 def ==(other) # :nodoc: return @address == other.address end |
#eql?(other) ⇒ Boolean
:nodoc:
2236 2237 2238 |
# File 'lib/rubysl/resolv/resolv.rb', line 2236 def eql?(other) # :nodoc: return self == other end |
#hash ⇒ Object
:nodoc:
2240 2241 2242 |
# File 'lib/rubysl/resolv/resolv.rb', line 2240 def hash # :nodoc: return @address.hash end |
#inspect ⇒ Object
:nodoc:
2218 2219 2220 |
# File 'lib/rubysl/resolv/resolv.rb', line 2218 def inspect # :nodoc: return "#<#{self.class} #{self.to_s}>" end |
#to_name ⇒ Object
Turns this IPv6 address into a Resolv::DNS::Name. – ip6.arpa should be searched too. [RFC3152]
2227 2228 2229 2230 |
# File 'lib/rubysl/resolv/resolv.rb', line 2227 def to_name return DNS::Name.new( @address.unpack("H32")[0].split(//).reverse + ['ip6', 'arpa']) end |
#to_s ⇒ Object
:nodoc:
2210 2211 2212 2213 2214 2215 2216 |
# File 'lib/rubysl/resolv/resolv.rb', line 2210 def to_s # :nodoc: address = sprintf("%X:%X:%X:%X:%X:%X:%X:%X", *@address.unpack("nnnnnnnn")) unless address.sub!(/(^|:)0(:0)+(:|$)/, '::') address.sub!(/(^|:)0(:|$)/, '::') end return address end |