Class: Resolv::IPv4

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

Overview

A Resolv::DNS IPv4 address.

Constant Summary collapse

Regex256 =
/0
|1(?:[0-9][0-9]?)?
|2(?:[0-4][0-9]?|5[0-5]?|[6-9])?
|[3-9][0-9]?/x
Regex =

Regular expression IPv4 addresses must match.

/\A(#{Regex256})\.(#{Regex256})\.(#{Regex256})\.(#{Regex256})\z/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(address) ⇒ IPv4

:nodoc:



3148
3149
3150
3151
3152
3153
3154
3155
3156
# File 'lib/resolv.rb', line 3148

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

#address ⇒ Object (readonly)

The raw IPv4 address as a String.



3164
3165
3166
# File 'lib/resolv.rb', line 3164

def address
  @address
end

Class Method Details

.create(arg) ⇒ Object

Creates a new IPv4 address from arg which may be:

IPv4

returns arg.

String

arg must match the IPv4::Regex constant



3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
# File 'lib/resolv.rb', line 3130

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:



3182
3183
3184
# File 'lib/resolv.rb', line 3182

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

#eql?(other) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


3186
3187
3188
# File 'lib/resolv.rb', line 3186

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

#hash ⇒ Object

:nodoc:



3190
3191
3192
# File 'lib/resolv.rb', line 3190

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

#inspect ⇒ Object

:nodoc:



3170
3171
3172
# File 'lib/resolv.rb', line 3170

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

#to_name ⇒ Object

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



3177
3178
3179
3180
# File 'lib/resolv.rb', line 3177

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

#to_s ⇒ Object

:nodoc:



3166
3167
3168
# File 'lib/resolv.rb', line 3166

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