Class: Scriptroute::IPaddress

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

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ IPaddress

Returns a new instance of IPaddress.

Parameters:

  • n (String, IPaddress, Integer)

    a hostname, an IP address in string form, another IPaddress or an IP address in numeric form



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/scriptroute/packets.rb', line 221

def initialize(n)
  raise "Seems bad to initialize an IPaddress with nil." unless n
  if(n.is_a?(String)) then
    shf = 32
    if n =~ /^[\d\.]+$/ then
      @addr = n.split('.').map { |i| shf -= 8; i.to_i << shf }.sum
    else
      addr = Scriptroute::dns_lookup(n)
      if addr then
        @addr = addr.split('.').map { |i| shf -= 8; i.to_i << shf }.sum
      else
        $stderr.puts "unable to lookup #{n}"
        @addr = 0
      end
    end
  elsif(n.is_a?(IPaddress)) then
    @addr = n.to_i
  else
    @addr = n
  end
end

Instance Method Details

#<=>(other) ⇒ Object

Compares numerically, for sorting.



264
265
266
# File 'lib/scriptroute/packets.rb', line 264

def <=>(other)
  @addr <=> other.to_i
end

#==(other) ⇒ Object

Compares numerically, for equality test.



268
269
270
# File 'lib/scriptroute/packets.rb', line 268

def ==(other)
  @addr == other.to_i
end

#eql?(other) ⇒ Boolean

in case you would like to store a Hash with IPaddresses as keys

Returns:

  • (Boolean)


260
261
262
# File 'lib/scriptroute/packets.rb', line 260

def eql?(other)
  @addr == other.to_i
end

#hashObject

in case you would like to store a Hash with IPaddresses as keys



256
257
258
# File 'lib/scriptroute/packets.rb', line 256

def hash
  @addr
end

#inspectString

just invokes to_s for now.

Returns:

  • (String)


248
249
250
# File 'lib/scriptroute/packets.rb', line 248

def inspect
  to_s
end

#nameifyObject



272
273
274
# File 'lib/scriptroute/packets.rb', line 272

def nameify
  Scriptroute.nameify(self.to_s)
end

#to_iInteger

Returns:

  • (Integer)


252
253
254
# File 'lib/scriptroute/packets.rb', line 252

def to_i
  @addr
end

#to_sString

Returns dotted quad notation of the IP address.

Returns:

  • (String)

    dotted quad notation of the IP address.



243
244
245
# File 'lib/scriptroute/packets.rb', line 243

def to_s
  [ @addr >> 24, @addr >> 16, @addr >> 8, @addr ].map { |i| i & 0xff }.join(".")
end