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(ip) ⇒ IPRange #initialize(start_ip, end_ip) ⇒ IPRange #initialize(cidr_range) ⇒ IPRange

Returns an IP address range of one or more addresses.

Overloads:

  • #initialize(ip) ⇒ IPRange

    Examples:

    Nexpose::IPRange.new('192.168.1.0')
    

    Parameters:

    • from (#to_s)

      the IP single IP address.

  • #initialize(start_ip, end_ip) ⇒ IPRange

    Examples:

    Nexpose::IPRange.new('192.168.1.0', '192.168.1.255')
    

    Parameters:

    • from (#to_s)

      the IP to start the range with.

    • to (#to_s)

      the IP to end the range with.

  • #initialize(cidr_range) ⇒ IPRange
    Note:

    The range will not be stripped of reserved IP addresses (such as x.x.x.0 and x.x.x.255).

    Examples:

    Nexpose::IPRange.new('192.168.1.0/24')
    

    Parameters:

    • from (#to_s)

      the CIDR notation IP address range.



649
650
651
652
653
654
655
656
657
658
659
660
# File 'lib/nexpose/site.rb', line 649

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

  return unless @to.nil?

  range = IPAddr.new(@from.to_s).to_range
  unless range.one?
    @from = range.first.to_s
    @to   = range.last.to_s
  end
end

Instance Attribute Details

#fromObject

Start of range *Required



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

def from
  @from
end

#toObject

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



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

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



676
677
678
679
680
681
682
683
684
685
686
687
688
689
# File 'lib/nexpose/site.rb', line 676

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



691
692
693
# File 'lib/nexpose/site.rb', line 691

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

#as_xmlObject Also known as: to_xml_elem



719
720
721
722
723
# File 'lib/nexpose/site.rb', line 719

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


695
696
697
698
# File 'lib/nexpose/site.rb', line 695

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

#hashObject



715
716
717
# File 'lib/nexpose/site.rb', line 715

def hash
  to_xml.hash
end

#include?(single_ip) ⇒ Boolean

Returns:

  • (Boolean)


700
701
702
703
704
705
706
707
708
709
710
711
712
713
# File 'lib/nexpose/site.rb', line 700

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.



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

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

#to_sObject



730
731
732
733
# File 'lib/nexpose/site.rb', line 730

def to_s
  return from.to_s if to.nil?
  "#{from.to_s} - #{to.to_s}"
end

#to_xmlObject



726
727
728
# File 'lib/nexpose/site.rb', line 726

def to_xml
  as_xml.to_s
end