class NetSpace::Subnet

Class representing an IP subnet. At its core, it's a base IP and a netmask, both converted to 32-bit integers for fast comparisons.

Attributes

bits[RW]
ip[RW]
ip32[RW]
mask[RW]
mask32[RW]

Public Class Methods

new(ip, bits) click to toggle source
# File lib/netspace/subnet.rb, line 12
def initialize(ip, bits)
  @ip     = ip
  @bits   = bits
  @ip32   = NetSpace::ip2num(ip)
  @mask32 = ~(2 ** (32 - @bits) - 1)
  @mask   = NetSpace::num2ip(@mask32)
end

Public Instance Methods

in?(ip) click to toggle source

Test if indicated IP address resides within the boundaries of the subnet.

# File lib/netspace/subnet.rb, line 25
def in?(ip)
  return NetSpace::ip2num(ip) & @mask32 == @ip32
end
ips() click to toggle source

Return a list of all IPs (as strings) that are legitimately members of the subnet.

# File lib/netspace/subnet.rb, line 34
def ips
  is = []
  ip = @ip32
  while ip & @mask32 == @ip32
    is << NetSpace::num2ip(ip)
    ip += 1
  end
  return is
end