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.



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

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

Instance Attribute Details

#fromObject

Start of range *Required



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

def from
  @from
end

#toObject

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



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

def to
  @to
end

Instance Method Details

#<=>(other) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/nexpose/site.rb', line 441

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



456
457
458
# File 'lib/nexpose/site.rb', line 456

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


460
461
462
463
# File 'lib/nexpose/site.rb', line 460

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

#hashObject



480
481
482
# File 'lib/nexpose/site.rb', line 480

def hash
  to_xml.hash
end

#include?(single_ip) ⇒ Boolean

Returns:

  • (Boolean)


465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/nexpose/site.rb', line 465

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.



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

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

#to_xmlObject



490
491
492
# File 'lib/nexpose/site.rb', line 490

def to_xml
  to_xml_elem.to_s
end

#to_xml_elemObject



484
485
486
487
488
# File 'lib/nexpose/site.rb', line 484

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