Class: Resolv::IPv6

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

Overview

Obsoleted by ipaddr.rb?

Constant Summary collapse

Regex_8Hex =

:nodoc:

/\A
(?:[0-9A-Fa-f]{1,4}:){7}
   [0-9A-Fa-f]{1,4}
\z/x
Regex_CompressedHex =
/\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 =
/\A
((?:[0-9A-Fa-f]{1,4}:){6,6})
(\d+)\.(\d+)\.(\d+)\.(\d+)
\z/x
Regex_CompressedHex4Dec =
/\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 =
/
(?:#{Regex_8Hex}) |
(?:#{Regex_CompressedHex}) |
(?:#{Regex_6Hex4Dec}) |
(?:#{Regex_CompressedHex4Dec})/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv6

Returns a new instance of IPv6.



1971
1972
1973
1974
1975
1976
# File 'lib/net/dns/resolv.rb', line 1971

def initialize(address)
  unless address.kind_of?(String) && address.length == 16
    raise ArgumentError.new('IPv6 address must be 16 bytes')
  end
  @address = address
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



1977
1978
1979
# File 'lib/net/dns/resolv.rb', line 1977

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object



1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
# File 'lib/net/dns/resolv.rb', line 1925

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



1997
1998
1999
# File 'lib/net/dns/resolv.rb', line 1997

def ==(other)
  return @address == other.address
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


2001
2002
2003
# File 'lib/net/dns/resolv.rb', line 2001

def eql?(other)
  return self == other
end

#hashObject



2005
2006
2007
# File 'lib/net/dns/resolv.rb', line 2005

def hash
  return @address.hash
end

#inspectObject



1987
1988
1989
# File 'lib/net/dns/resolv.rb', line 1987

def inspect
  return "#<#{self.class} #{self.to_s}>"
end

#to_nameObject



1991
1992
1993
1994
1995
# File 'lib/net/dns/resolv.rb', line 1991

def to_name
  # ip6.arpa should be searched too. [RFC3152]
  return DNS::Name.new(
    @address.unpack("H32")[0].split(//).reverse + ['ip6', 'int'])
end

#to_sObject



1979
1980
1981
1982
1983
1984
1985
# File 'lib/net/dns/resolv.rb', line 1979

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