class IPRange

Attributes

first[R]
last[R]
vrange[R]

Public Class Methods

new(args) click to toggle source

create an IP-range

# File lib/ip_range.rb, line 50
def initialize(args) 
  @log = @@log
  if args && args.length >= 2
    
    #first IP
    @first = ip_to_number args[0]
    #last IP
    @last = ip_to_number args[1]
    if @last < @first
      @log.error('ERROR! Last IP is smaller than first. Aborting!') 
      exit false
    end
    @vrange = args[0,2] 
  end
end

Public Instance Methods

in_range?(cip) click to toggle source

check IP in range ?

# File lib/ip_range.rb, line 41
def in_range?(cip)
  (@first..@last) === ip_to_number(cip)
end
irange() click to toggle source
# File lib/ip_range.rb, line 45
def irange()
  [@first, @last]
end

Private Instance Methods

ip_to_number(ip) click to toggle source

return ip as Integer

# File lib/ip_range.rb, line 32
def ip_to_number(ip)
  nibbles = ip.split('.')
  nibbles = nibbles.collect {|n| n.to_i}
  ( (nibbles[0] * 256 + nibbles[1] ) * 256 + nibbles[2] ) * 256 + nibbles[3]
end