Module: Puppet::Util::NetworkDevice::IPCalc

Included in:
Cisco::Device, Cisco::Interface, Cisco::Interface
Defined in:
lib/vendor/puppet/util/network_device/ipcalc.rb

Constant Summary collapse

Octet =

This is a rip-off of authstore

'(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])'
IPv4 =
"#{Octet}\.#{Octet}\.#{Octet}\.#{Octet}"
IPv6_full =
"_:_:_:_:_:_:_:_|_:_:_:_:_:_::_?|_:_:_:_:_::((_:)?_)?|_:_:_:_::((_:){0,2}_)?|_:_:_::((_:){0,3}_)?|_:_::((_:){0,4}_)?|_::((_:){0,5}_)?|::((_:){0,6}_)?"
IPv6_partial =
"_:_:_:_:_:_:|_:_:_:_::(_:)?|_:_::(_:){0,2}|_::(_:){0,3}"
IP =
"#{IPv4}|#{IPv6_full}".gsub(/_/,'([0-9a-fA-F]{1,4})').gsub(/\(/,'(?:')

Instance Method Summary collapse

Instance Method Details

#bits(family) ⇒ Object



22
23
24
# File 'lib/vendor/puppet/util/network_device/ipcalc.rb', line 22

def bits(family)
  family == Socket::AF_INET6 ? 128 : 32
end

#fullmask(family) ⇒ Object



26
27
28
# File 'lib/vendor/puppet/util/network_device/ipcalc.rb', line 26

def fullmask(family)
  (1 << bits(family)) - 1
end

#linklocal?(ip) ⇒ Boolean

Returns:

  • (Boolean)


65
66
# File 'lib/vendor/puppet/util/network_device/ipcalc.rb', line 65

def linklocal?(ip)
end

#mask(family, length) ⇒ Object



30
31
32
# File 'lib/vendor/puppet/util/network_device/ipcalc.rb', line 30

def mask(family, length)
  (1 << (bits(family) - length)) - 1
end

#netmask(family, length) ⇒ Object

returns ip address netmask from prefix length



35
36
37
# File 'lib/vendor/puppet/util/network_device/ipcalc.rb', line 35

def netmask(family, length)
  IPAddr.new(fullmask(family) & ~mask(family, length) , family)
end

#parse(value) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/vendor/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) ⇒ Object

returns ip address prefix length from netmask



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vendor/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) ⇒ Object

returns an IOS wildmask



40
41
42
# File 'lib/vendor/puppet/util/network_device/ipcalc.rb', line 40

def wildmask(family, length)
  IPAddr.new(mask(family, length) , family)
end