~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to lib/puppet/util/network_device/ipcalc.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
require 'puppet/util/network_device'
 
3
module Puppet::Util::NetworkDevice::IPCalc
 
4
 
 
5
  # This is a rip-off of authstore
 
6
  Octet = '(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])'
 
7
  IPv4 = "#{Octet}\.#{Octet}\.#{Octet}\.#{Octet}"
 
8
  IPv6_full    = "_:_:_:_:_:_:_:_|_:_:_:_:_:_::_?|_:_:_:_:_::((_:)?_)?|_:_:_:_::((_:){0,2}_)?|_:_:_::((_:){0,3}_)?|_:_::((_:){0,4}_)?|_::((_:){0,5}_)?|::((_:){0,6}_)?"
 
9
  IPv6_partial = "_:_:_:_:_:_:|_:_:_:_::(_:)?|_:_::(_:){0,2}|_::(_:){0,3}"
 
10
  IP = "#{IPv4}|#{IPv6_full}".gsub(/_/,'([0-9a-fA-F]{1,4})').gsub(/\(/,'(?:')
 
11
 
 
12
  def parse(value)
 
13
    case value
 
14
    when /^(#{IP})\/(\d+)$/  # 12.34.56.78/24, a001:b002::efff/120, c444:1000:2000::9:192.168.0.1/112
 
15
      [$2.to_i,IPAddr.new($1)]
 
16
    when /^(#{IP})$/           # 10.20.30.40,
 
17
      value = IPAddr.new(value)
 
18
      [bits(value.family),value]
 
19
    end
 
20
  end
 
21
 
 
22
  def bits(family)
 
23
    family == Socket::AF_INET6 ? 128 : 32
 
24
  end
 
25
 
 
26
  def fullmask(family)
 
27
    (1 << bits(family)) - 1
 
28
  end
 
29
 
 
30
  def mask(family, length)
 
31
    (1 << (bits(family) - length)) - 1
 
32
  end
 
33
 
 
34
  # returns ip address netmask from prefix length
 
35
  def netmask(family, length)
 
36
    IPAddr.new(fullmask(family) & ~mask(family, length) , family)
 
37
  end
 
38
 
 
39
  # returns an IOS wildmask
 
40
  def wildmask(family, length)
 
41
    IPAddr.new(mask(family, length) , family)
 
42
  end
 
43
 
 
44
  # returns ip address prefix length from netmask
 
45
  def prefix_length(netmask)
 
46
    mask_addr = netmask.to_i
 
47
    return 0 if mask_addr == 0
 
48
    length=32
 
49
    if (netmask.ipv6?)
 
50
      length=128
 
51
    end
 
52
 
 
53
    mask = mask_addr < 2**length ? length : 128
 
54
 
 
55
    mask.times do
 
56
        if ((mask_addr & 1) == 1)
 
57
            break
 
58
        end
 
59
        mask_addr = mask_addr >> 1
 
60
        mask = mask - 1
 
61
    end
 
62
    mask
 
63
  end
 
64
 
 
65
  def linklocal?(ip)
 
66
  end
 
67
 
 
68
end
 
 
b'\\ No newline at end of file'