Class: Resolv::IPv4

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

Overview

A Resolv::DNS IPv4 address.

Constant Summary collapse

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

#initialize(address) ⇒ IPv4

:nodoc:


2331
2332
2333
2334
2335
2336
2337
2338
2339
# File 'lib/resolv.rb', line 2331

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

#addressObject (readonly)

The raw IPv4 address as a String.


2347
2348
2349
# File 'lib/resolv.rb', line 2347

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object


2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
# File 'lib/resolv.rb', line 2313

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:


2365
2366
2367
# File 'lib/resolv.rb', line 2365

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

#eql?(other) ⇒ Boolean

:nodoc:


2369
2370
2371
# File 'lib/resolv.rb', line 2369

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

#hashObject

:nodoc:


2373
2374
2375
# File 'lib/resolv.rb', line 2373

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

#inspectObject

:nodoc:


2353
2354
2355
# File 'lib/resolv.rb', line 2353

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

#to_nameObject

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


2360
2361
2362
2363
# File 'lib/resolv.rb', line 2360

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

#to_sObject

:nodoc:


2349
2350
2351
# File 'lib/resolv.rb', line 2349

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