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.



569
570
571
572
# File 'lib/nexpose/site.rb', line 569

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

Instance Attribute Details

#fromObject

Start of range *Required



565
566
567
# File 'lib/nexpose/site.rb', line 565

def from
  @from
end

#toObject

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



567
568
569
# File 'lib/nexpose/site.rb', line 567

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'lib/nexpose/site.rb', line 588

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



603
604
605
# File 'lib/nexpose/site.rb', line 603

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

#as_xmlObject Also known as: to_xml_elem



631
632
633
634
635
# File 'lib/nexpose/site.rb', line 631

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#hashObject



627
628
629
# File 'lib/nexpose/site.rb', line 627

def hash
  to_xml.hash
end

#include?(single_ip) ⇒ Boolean

Returns:

  • (Boolean)


612
613
614
615
616
617
618
619
620
621
622
623
624
625
# File 'lib/nexpose/site.rb', line 612

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.



579
580
581
582
583
584
# File 'lib/nexpose/site.rb', line 579

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

#to_xmlObject



638
639
640
# File 'lib/nexpose/site.rb', line 638

def to_xml
  as_xml.to_s
end