module Conjur::CIDR

Utility methods for CIDR network addresses

Attributes

mask_addr[R]

Public Class Methods

extended(addr) click to toggle source
# File lib/conjur/cidr.rb, line 35
def self.extended addr
  addr.prefixlen # validates
end
validate(addr) click to toggle source

Parse addr into an IPAddr if it’s not one already, then extend it with CIDR module. This will force validation and will raise ArgumentError if invalid. @return [IPAddr] the address (extended with CIDR module)

# File lib/conjur/cidr.rb, line 30
def self.validate addr
  addr = IPAddr.new addr unless addr.kind_of? IPAddr
  addr.extend self
end

Public Instance Methods

prefixlen() click to toggle source

@return [Fixnum] the length of the network mask prefix

# File lib/conjur/cidr.rb, line 53
def prefixlen
  unless @prefixlen
    return @prefixlen = 0 if (mask = mask_addr) == 0

    @prefixlen = ipv4? ? 32 : 128

    while (mask & 1) == 0
      mask >>= 1
      @prefixlen -= 1
    end

    if mask != ((1 << @prefixlen) - 1)
      fail InvalidCIDR, "#{inspect} is not a valid CIDR network address"
    end
  end
  return @prefixlen
end
to_s() click to toggle source

@return [String] the address as an “address/mask length” string @example

IPAddr.new("192.0.2.0/255.255.255.0").extend(CIDR).to_s == "192.0.2.0/24"
Calls superclass method
# File lib/conjur/cidr.rb, line 48
def to_s
  [super, prefixlen].join '/'
end