Class: Resolv::IPv4

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

Overview

A Resolv::DNS IPv4 address.

Constant Summary collapse

Regex =

Regular expression IPv4 addresses must match.

/\A(\d+)\.(\d+)\.(\d+)\.(\d+)\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv4

:nodoc:



2089
2090
2091
2092
2093
2094
# File 'lib/resolv.rb', line 2089

def initialize(address) # :nodoc:
  unless address.kind_of?(String) && address.length == 4
    raise ArgumentError.new('IPv4 address must be 4 bytes')
  end
  @address = address
end

Instance Attribute Details

#addressObject (readonly)

The raw IPv4 address as a String.



2102
2103
2104
# File 'lib/resolv.rb', line 2102

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object



2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
# File 'lib/resolv.rb', line 2071

def self.create(arg)
  case arg
  when IPv4
    return arg
  when Regex
    if (0..255) === (a = $1.to_i) &&
       (0..255) === (b = $2.to_i) &&
       (0..255) === (c = $3.to_i) &&
       (0..255) === (d = $4.to_i)
      return self.new([a, b, c, d].pack("CCCC"))
    else
      raise ArgumentError.new("IPv4 address with invalid value: " + arg)
    end
  else
    raise ArgumentError.new("cannot interpret as IPv4 address: #{arg.inspect}")
  end
end

Instance Method Details

#==(other) ⇒ Object

:nodoc:



2120
2121
2122
# File 'lib/resolv.rb', line 2120

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


2124
2125
2126
# File 'lib/resolv.rb', line 2124

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

#hashObject

:nodoc:



2128
2129
2130
# File 'lib/resolv.rb', line 2128

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

#inspectObject

:nodoc:



2108
2109
2110
# File 'lib/resolv.rb', line 2108

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

#to_nameObject

Turns this IPv4 address into a Resolv::DNS::Name.



2115
2116
2117
2118
# File 'lib/resolv.rb', line 2115

def to_name
  return DNS::Name.create(
    '%d.%d.%d.%d.in-addr.arpa.' % @address.unpack('CCCC').reverse)
end

#to_sObject

:nodoc:



2104
2105
2106
# File 'lib/resolv.rb', line 2104

def to_s # :nodoc:
  return sprintf("%d.%d.%d.%d", *@address.unpack("CCCC"))
end