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.



612
613
614
615
# File 'lib/nexpose/site.rb', line 612

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

Instance Attribute Details

#fromObject

Start of range *Required



608
609
610
# File 'lib/nexpose/site.rb', line 608

def from
  @from
end

#toObject

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



610
611
612
# File 'lib/nexpose/site.rb', line 610

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/nexpose/site.rb', line 631

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



646
647
648
# File 'lib/nexpose/site.rb', line 646

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

#as_xmlObject Also known as: to_xml_elem



674
675
676
677
678
# File 'lib/nexpose/site.rb', line 674

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


650
651
652
653
# File 'lib/nexpose/site.rb', line 650

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

#hashObject



670
671
672
# File 'lib/nexpose/site.rb', line 670

def hash
  to_xml.hash
end

#include?(single_ip) ⇒ Boolean

Returns:

  • (Boolean)


655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/nexpose/site.rb', line 655

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.



622
623
624
625
626
627
# File 'lib/nexpose/site.rb', line 622

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

#to_xmlObject



681
682
683
# File 'lib/nexpose/site.rb', line 681

def to_xml
  as_xml.to_s
end