In Files

  • resolv.rb

Resolv::IPv6

Attributes

address[R]

Public Class Methods

create(arg) click to toggle source
 
               # File resolv.rb, line 1843
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 << "\00"" * 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 << "\00"" * 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
            
new(address) click to toggle source
 
               # File resolv.rb, line 1889
def initialize(address)
  unless address.kind_of?(String) && address.length == 16
    raise ArgumentError.new('IPv6 address must be 16 bytes')
  end
  @address = address
end
            

Public Instance Methods

==(other) click to toggle source
 
               # File resolv.rb, line 1915
def ==(other)
  return @address == other.address
end
            
eql?(other) click to toggle source
 
               # File resolv.rb, line 1919
def eql?(other)
  return self == other
end
            
hash() click to toggle source
 
               # File resolv.rb, line 1923
def hash
  return @address.hash
end
            
inspect() click to toggle source
 
               # File resolv.rb, line 1905
def inspect
  return "#<#{self.class} #{self.to_s}>"
end
            
to_name() click to toggle source
 
               # File resolv.rb, line 1909
def to_name
  # ip6.arpa should be searched too. [RFC3152]
  return DNS::Name.new(
    @address.unpack("H32")[0].split(//).reverse + ['ip6', 'int'])
end
            
to_s() click to toggle source
 
               # File resolv.rb, line 1897
def to_s
  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