Class: Gem::Resolv::IPv6

Inherits:
Object
  • Object
show all
Defined in:
lib/rubygems/vendor/resolv/lib/resolv.rb

Overview

A Gem::Resolv::DNS IPv6 address.

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 =

Compressed IPv6 address 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_8HexLinkLocal =

IPv6 link local address format fe80:b:c:d:e:f:g:h%em1

/\A
[Ff][Ee]80
(?::[0-9A-Fa-f]{1,4}){7}
%[-0-9A-Za-z._~]+
\z/x
Regex_CompressedHexLinkLocal =

Compressed IPv6 link local address format fe80::b%em1

/\A
[Ff][Ee]80:
(?:
  ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?) ::
  ((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)
  |
  :((?:[0-9A-Fa-f]{1,4}(?::[0-9A-Fa-f]{1,4})*)?)
)?
:[0-9A-Fa-f]{1,4}%[-0-9A-Za-z._~]+
\z/x
Regex =

A composite IPv6 address Regexp.

/
(?:#{Regex_8Hex}) |
(?:#{Regex_CompressedHex}) |
(?:#{Regex_6Hex4Dec}) |
(?:#{Regex_CompressedHex4Dec}) |
(?:#{Regex_8HexLinkLocal}) |
(?:#{Regex_CompressedHexLinkLocal})
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv6

:nodoc:



3029
3030
3031
3032
3033
3034
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3029

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

#addressObject (readonly)

The raw IPv6 address as a String.



3039
3040
3041
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3039

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object

Creates a new IPv6 address from arg which may be:

IPv6

returns arg.

String

arg must match one of the IPv6::Regex* constants



2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
# File 'lib/rubygems/vendor/resolv/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

Instance Method Details

#==(other) ⇒ Object

:nodoc:



3059
3060
3061
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3059

def ==(other) # :nodoc:
  return @address == other.address
end

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


3063
3064
3065
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3063

def eql?(other) # :nodoc:
  return self == other
end

#hashObject

:nodoc:



3067
3068
3069
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3067

def hash # :nodoc:
  return @address.hash
end

#inspectObject

:nodoc:



3045
3046
3047
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3045

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

#to_nameObject

Turns this IPv6 address into a Gem::Resolv::DNS::Name. – ip6.arpa should be searched too. [RFC3152]



3054
3055
3056
3057
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3054

def to_name
  return DNS::Name.new(
    @address.unpack("H32")[0].split(//).reverse + ['ip6', 'arpa'])
end

#to_sObject

:nodoc:



3041
3042
3043
# File 'lib/rubygems/vendor/resolv/lib/resolv.rb', line 3041

def to_s # :nodoc:
  sprintf("%x:%x:%x:%x:%x:%x:%x:%x", *@address.unpack("nnnnnnnn")).sub(/(^|:)0(:0)+(:|$)/, '::')
end