Class: Dnsruby::IPv6
- Inherits:
-
Object
- Object
- Dnsruby::IPv6
- Defined in:
- lib/dnsruby/ipv6.rb
Overview
Dnsruby::IPv6 class
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 =
Compresses IPv6 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
Created a new IPv6 address from
argwhich may be:.
Instance Method Summary collapse
-
#==(other) ⇒ Object
:nodoc:.
-
#eql?(other) ⇒ Boolean
:nodoc:.
- #hash ⇒ Object
-
#initialize(address) ⇒ IPv6
constructor
:nodoc:.
-
#inspect ⇒ Object
:nodoc:.
-
#to_name ⇒ Object
Turns this IPv6 address into a Dnsruby::Name -- ip6.arpa should be searched too.
-
#to_s ⇒ Object
The historical Dnsruby representation, retained for callers that compare address strings.
-
#to_s_canonical ⇒ Object
The RFC 5952 canonical text representation of the address, opt-in.
Constructor Details
#initialize(address) ⇒ IPv6
:nodoc:
103 104 105 106 107 108 |
# File 'lib/dnsruby/ipv6.rb', line 103 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
111 112 113 |
# File 'lib/dnsruby/ipv6.rb', line 111 def address @address end |
Class Method Details
.create(arg) ⇒ Object
Created a new IPv6 address from arg which may be:
- * IPv6
returns
arg- * String
argmust match one of the IPv6::Regex* constants
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dnsruby/ipv6.rb', line 55 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 raise ArgumentError, "not numeric IPv6 address: #{arg}" unless omitlen > 0 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 raise ArgumentError, "not numeric IPv6 address: #{arg}" unless omitlen > 0 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:
172 173 174 |
# File 'lib/dnsruby/ipv6.rb', line 172 def ==(other) #:nodoc: return @address == other.address end |
#eql?(other) ⇒ Boolean
:nodoc:
176 177 178 |
# File 'lib/dnsruby/ipv6.rb', line 176 def eql?(other) #:nodoc: return self == other end |
#hash ⇒ Object
180 181 182 |
# File 'lib/dnsruby/ipv6.rb', line 180 def hash return @address.hash end |
#inspect ⇒ Object
:nodoc:
159 160 161 |
# File 'lib/dnsruby/ipv6.rb', line 159 def inspect #:nodoc: return "#<#{self.class} #{self.to_s}>" end |
#to_name ⇒ Object
Turns this IPv6 address into a Dnsruby::Name
ip6.arpa should be searched too. [RFC3152]
166 167 168 169 170 |
# File 'lib/dnsruby/ipv6.rb', line 166 def to_name return Name.create( # @address.unpack("H32")[0].split(//).reverse + ['ip6', 'arpa']) @address.unpack("H32")[0].split(//).reverse.join(".") + ".ip6.arpa") end |
#to_s ⇒ Object
The historical Dnsruby representation, retained for callers that compare address strings. Use #to_s_canonical for RFC 5952 formatting.
115 116 117 118 119 120 121 |
# File 'lib/dnsruby/ipv6.rb', line 115 def to_s text = sprintf("%X:%X:%X:%X:%X:%X:%X:%X", *@address.unpack("n8")) unless text.sub!(/(^|:)0(:0)+(:|$)/, '::') text.sub!(/(^|:)0(:|$)/, '::') end text end |
#to_s_canonical ⇒ Object
The RFC 5952 canonical text representation of the address, opt-in.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/dnsruby/ipv6.rb', line 124 def to_s_canonical fields = @address.unpack("n8") # RFC 5952 section 5: an address carrying an embedded IPv4 address # under the well-known ::ffff:0:0/96 prefix is written in the mixed # hexadecimal / dotted decimal notation. if fields[0, 5].all?(&:zero?) && fields[5] == 0xffff return "::ffff:" + fields[6, 2].pack("n2").unpack("C4").join(".") end # RFC 5952 section 4.2.3: "::" replaces the longest run of consecutive # all-zero fields, and the first such run when several are equally long. # Section 4.2.2: a run of a single all-zero field is never replaced. run_start = nil best_start, best_length = nil, 1 fields.each_with_index do |field, i| if field == 0 run_start ||= i length = i - run_start + 1 best_start, best_length = run_start, length if length > best_length else run_start = nil end end # RFC 5952 sections 4.1 and 4.3: no leading zeros, and lowercase hex strings = fields.map {|field| field.to_s(16)} if best_start strings[best_start, best_length] = [''] strings.unshift('') if best_start == 0 strings.push('') if best_start + best_length == fields.length end return strings.join(':') end |