Files
metasploit-gs/lib/rex/socket/range_walker.rb
T
HD Moore 1fa95d6050 This patch adds support for IPv6 scope_ids to AddressRange parameters. Ranges consisting of a single address are not expanded
git-svn-id: file:///home/svn/framework3/trunk@5640 4d416f70-5f16-0410-b530-b9f4589650da
2008-08-23 07:05:38 +00:00

87 lines
1.5 KiB
Ruby

require 'rex/socket'
module Rex
module Socket
###
#
# This class provides an interface to enumerating an IP range
#
###
class RangeWalker
#
# Initializes a walker instance using the supplied range
#
def initialize(ranges)
self.ranges = []
ranges.split(',').each do |range|
a,b = range.split('-')
b ||= a
a,scope = a.split("%")
b,scope = b.split("%") if not scope
a = Rex::Socket.addr_atoi(a)
b = Rex::Socket.addr_atoi(b)
if (b < a)
t = a
a = b
b = t
end
self.ranges << [a,b,scope]
end
reset
end
#
# Resets the subnet walker back to its original state.
#
def reset
self.curr_range = 0
self.curr_ip = self.ranges[0][0]
self.curr_scope = self.ranges[0][2]
self.num_ips = 0
self.ranges.each {|r| self.num_ips += r[1]-r[0] + 1 }
end
#
# Returns the next IP address.
#
def next_ip
if (self.curr_ip > self.ranges[self.curr_range][1])
if (self.curr_range == self.ranges.length - 1)
return nil
end
self.curr_range += 1
self.curr_ip = self.ranges[self.curr_range][0]
self.curr_scope = self.ranges[self.curr_range][1]
end
addr = Rex::Socket.addr_itoa(self.curr_ip)
self.curr_ip += 1
addr += "%#{self.curr_scope}" if self.curr_scope
return addr
end
#
# The total number of IPs within the range
#
attr_reader :num_ips
protected
attr_writer :num_ips # :nodoc:
attr_accessor :addr_start, :addr_stop, :curr_ip, :curr_range, :ranges, :curr_scope # :nodoc:
end
end
end