class SSLScan::Socket::SubnetWalker

This class provides an interface to enumerating a subnet with a supplied netmask.

Attributes

netmask[R]

The netmask of the subnet.

num_ips[R]

The total number of IPs within the subnet.

subnet[R]

The subnet that is being enumerated.

Public Class Methods

new(subnet, netmask) click to toggle source

Initializes a subnet walker instance using the supplied subnet information.

# File lib/ssl_scan/socket/subnet_walker.rb, line 17
def initialize(subnet, netmask)
  self.subnet  = Socket.resolv_to_dotted(subnet)
  self.netmask = Socket.resolv_to_dotted(netmask)

  reset
end

Public Instance Methods

next_ip() click to toggle source

Returns the next IP address.

# File lib/ssl_scan/socket/subnet_walker.rb, line 36
def next_ip
  if (curr_ip_idx >= num_ips)
    return nil
  end

  if (curr_ip_idx > 0)
    self.curr_ip[3] = (curr_ip[3].to_i + 1) % 256
    self.curr_ip[2] = (curr_ip[2].to_i + 1) % 256 if (curr_ip[3] == 0)
    self.curr_ip[1] = (curr_ip[1].to_i + 1) % 256 if (curr_ip[2] == 0)
    self.curr_ip[0] = (curr_ip[0].to_i + 1) % 256 if (curr_ip[1] == 0)
  end

  self.curr_ip_idx += 1

  self.curr_ip.join('.')
end
reset() click to toggle source

Resets the subnet walker back to its original state.

# File lib/ssl_scan/socket/subnet_walker.rb, line 27
def reset
  self.curr_ip     = self.subnet.split('.')
  self.num_ips     = (1 << (32 - Socket.net2bitmask(self.netmask).to_i))
  self.curr_ip_idx = 0
end