Class: Resolv::IPv6

Inherits:
Object
  • Object
show all
Defined in:
lib/async/dns/extensions/resolv.rb

Overview

Clearly, the Ruby 2.3.0 release was throughly tested.

Class Method Summary collapse

Class Method Details

.create(arg) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/async/dns/extensions/resolv.rb', line 88

def self.create(arg)
	case arg
	when IPv6
		return arg
	when String
		address = ''.b
		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 = ''.b
			a2 = ''.b
			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 = ''.b
				a2 = ''.b
				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