类 Resolv::IPv6

一个 Resolv::DNS IPv6 地址。

常量

Regex

一个组合的 IPv6 地址 Regexp

Regex_6Hex4Dec

IPv4 映射的 IPv6 地址格式 a:b:c:d:e:f:w.x.y.z

Regex_8Hex

IPv6 地址格式 a:b:c:d:e:f:g:h

Regex_8HexLinkLocal

IPv6 链接本地地址格式 fe80:b:c:d:e:f:g:h%em1

Regex_CompressedHex

压缩的 IPv6 地址格式 a::b

Regex_CompressedHex4Dec

压缩的 IPv4 映射的 IPv6 地址格式 a::b:w.x.y.z

Regex_CompressedHexLinkLocal

压缩的 IPv6 链接本地地址格式 fe80::b%em1

属性

address[R]

原始的 IPv6 地址,以 String 格式。

公共类方法

create(arg) 点击切换源代码

arg 创建一个新的 IPv6 地址,arg 可以是

IPv6

返回 arg

String

arg 必须匹配 IPv6::Regex* 常量之一

# File lib/resolv.rb, line 2983
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

公共实例方法

to_name() 点击切换源代码

将此 IPv6 地址转换为 Resolv::DNS::Name

# File lib/resolv.rb, line 3054
def to_name
  return DNS::Name.new(
    @address.unpack("H32")[0].split(//).reverse + ['ip6', 'arpa'])
end