Class: Nexpose::IPRange

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/nexpose/site.rb

Overview

Object that represents a single IP address or an inclusive range of IP addresses. If to is nil then the from field will be used to specify a single IP Address only.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to = nil) ⇒ IPRange

Returns a new instance of IPRange.



421
422
423
424
# File 'lib/nexpose/site.rb', line 421

def initialize(from, to = nil)
  @from = from
  @to = to unless from == to
end

Instance Attribute Details

#fromObject

Start of range *Required



417
418
419
# File 'lib/nexpose/site.rb', line 417

def from
  @from
end

#toObject

End of range *Optional (If nil then IPRange is a single IP Address)



419
420
421
# File 'lib/nexpose/site.rb', line 419

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/nexpose/site.rb', line 428

def <=>(other)
  from = IPAddr.new(@from)
  to = @to.nil? ? from : IPAddr.new(@to)
  cf_from = IPAddr.new(other.from)
  cf_to = IPAddr.new(other.to.nil? ? other.from : other.to)
  if cf_to < from
    1
  elsif to < cf_from
    -1
  else # Overlapping
    0
  end
end

#==(other) ⇒ Object



442
443
444
# File 'lib/nexpose/site.rb', line 442

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


446
447
448
# File 'lib/nexpose/site.rb', line 446

def eql?(other)
  @from == other.from && @to == other.to
end

#hashObject



464
465
466
# File 'lib/nexpose/site.rb', line 464

def hash
  to_xml.hash
end

#include?(single_ip) ⇒ Boolean

Returns:

  • (Boolean)


450
451
452
453
454
455
456
457
458
459
460
461
462
# File 'lib/nexpose/site.rb', line 450

def include?(single_ip)
  from = IPAddr.new(@from)
  to = @to.nil? ? from : IPAddr.new(@to)
  other = IPAddr.new(single_ip)

  if other < from
    false
  elsif to < other
    false
  else
    true
  end
end

#to_xmlObject



474
475
476
# File 'lib/nexpose/site.rb', line 474

def to_xml
  to_xml_elem.to_s
end

#to_xml_elemObject



468
469
470
471
472
# File 'lib/nexpose/site.rb', line 468

def to_xml_elem
  xml = REXML::Element.new('range')
  xml.add_attributes({ 'from' => @from, 'to' => @to })
  xml
end