Class: Resolv::IPv4

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

Overview

A Resolv::DNS IPv4 address.

Constant Summary

Regex256 =

Regular expression IPv4 addresses must match.

/0
|1(?:[0-9][0-9]?)?
|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
|[3-9][0-9]?/x
Regex =
/\A(#{Regex256})\.(#{Regex256})\.(#{Regex256})\.(#{Regex256})\z/

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (IPv4) initialize(address)

:nodoc:



2137
2138
2139
2140
2141
2142
2143
2144
2145
# File 'lib/resolv.rb', line 2137

def initialize(address) # :nodoc:
  unless address.kind_of?(String)
    raise ArgumentError, 'IPv4 address must be a string'
  end
  unless address.length == 4
    raise ArgumentError, "IPv4 address expects 4 bytes but #{address.length} bytes"
  end
  @address = address
end

Instance Attribute Details

- (Object) address (readonly)

The raw IPv4 address as a String.



2153
2154
2155
# File 'lib/resolv.rb', line 2153

def address
  @address
end

Class Method Details

+ (Object) create(arg)



2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
# File 'lib/resolv.rb', line 2119

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

- (Object) ==(other)

:nodoc:



2171
2172
2173
# File 'lib/resolv.rb', line 2171

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

- (Boolean) eql?(other)

:nodoc:

Returns:

  • (Boolean)


2175
2176
2177
# File 'lib/resolv.rb', line 2175

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

- (Object) hash

:nodoc:



2179
2180
2181
# File 'lib/resolv.rb', line 2179

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

- (Object) inspect

:nodoc:



2159
2160
2161
# File 'lib/resolv.rb', line 2159

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

- (Object) to_name

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



2166
2167
2168
2169
# File 'lib/resolv.rb', line 2166

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

- (Object) to_s

:nodoc:



2155
2156
2157
# File 'lib/resolv.rb', line 2155

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