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.



436
437
438
439
# File 'lib/nexpose/site.rb', line 436

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

Instance Attribute Details

#fromObject

Start of range *Required



432
433
434
# File 'lib/nexpose/site.rb', line 432

def from
  @from
end

#toObject

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



434
435
436
# File 'lib/nexpose/site.rb', line 434

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
# File 'lib/nexpose/site.rb', line 455

def <=>(other)
  return 1 unless other.respond_to? :from
  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



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  return false unless other.respond_to? :from
  @from == other.from && @to == other.to
end

#hashObject



494
495
496
# File 'lib/nexpose/site.rb', line 494

def hash
  to_xml.hash
end

#include?(single_ip) ⇒ Boolean

Returns:

  • (Boolean)


479
480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/nexpose/site.rb', line 479

def include?(single_ip)
  return false unless single_ip.respond_to? :from
  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

#sizeFixnum

Size of the IP range. The total number of IP addresses represented by this range.

Returns:

  • (Fixnum)

    size of the range.



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

def size
  return 1 if @to.nil?
  from = IPAddr.new(@from)
  to = IPAddr.new(@to)
  (from..to).to_a.size
end

#to_xmlObject



504
505
506
# File 'lib/nexpose/site.rb', line 504

def to_xml
  to_xml_elem.to_s
end

#to_xml_elemObject



498
499
500
501
502
# File 'lib/nexpose/site.rb', line 498

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