# module Puppet::Util::NetworkDevice::IPCalc

### Constants

IP
IPv4
IPv6_full
IPv6_partial
Octet

  This is a rip-off of authstore

### Public Instance Methods

bits(family) click to toggle source

    # File lib/puppet/util/network_device/ipcalc.rb, line 22
    def bits(family)
      family == Socket::AF_INET6 ? 128 : 32
    end

fullmask(family) click to toggle source

    # File lib/puppet/util/network_device/ipcalc.rb, line 26
    def fullmask(family)
      (1 << bits(family)) - 1
    end

linklocal?(ip) click to toggle source

    # File lib/puppet/util/network_device/ipcalc.rb, line 65
    def linklocal?(ip)
    end

mask(family, length) click to toggle source

    # File lib/puppet/util/network_device/ipcalc.rb, line 30
    def mask(family, length)
      (1 << (bits(family) - length)) - 1
    end

netmask(family, length) click to toggle source

returns ip address netmask from prefix length

    # File lib/puppet/util/network_device/ipcalc.rb, line 35
    def netmask(family, length)
      IPAddr.new(fullmask(family) & ~mask(family, length) , family)
    end

parse(value) click to toggle source

    # File lib/puppet/util/network_device/ipcalc.rb, line 12
    def parse(value)
      case value
      when /^(#{IP})\/(\d+)$/  # 12.34.56.78/24, a001:b002::efff/120, c444:1000:2000::9:192.168.0.1/112
        [$2.to_i,IPAddr.new($1)]
      when /^(#{IP})$/           # 10.20.30.40,
        value = IPAddr.new(value)
        [bits(value.family),value]
      end
    end

prefix_length(netmask) click to toggle source

returns ip address prefix length from netmask

    # File lib/puppet/util/network_device/ipcalc.rb, line 45
    def prefix_length(netmask)
      mask_addr = netmask.to_i
      return 0 if mask_addr == 0
      length=32
      if (netmask.ipv6?)
        length=128
      end

      mask = mask_addr < 2**length ? length : 128

      mask.times do
          if ((mask_addr & 1) == 1)
              break
          end
          mask_addr = mask_addr >> 1
          mask = mask - 1
      end
      mask
    end

wildmask(family, length) click to toggle source

returns an IOS wildmask

    # File lib/puppet/util/network_device/ipcalc.rb, line 40
    def wildmask(family, length)
      IPAddr.new(mask(family, length) , family)
    end