Class: PuppetX::Firewall::IPCidr

Inherits:
IPAddr
  • Object
show all
Defined in:
lib/puppet_x/puppetlabs/firewall/ipcidr.rb

Overview

IPCidr object wrapper for IPAddr

Instance Method Summary collapse

Constructor Details

#initialize(ipaddr, family = Socket::AF_UNSPEC) ⇒ IPCidr

Returns a new instance of IPCidr.



9
10
11
12
13
14
15
# File 'lib/puppet_x/puppetlabs/firewall/ipcidr.rb', line 9

def initialize(ipaddr, family = Socket::AF_UNSPEC)
  super(ipaddr, family)
rescue ArgumentError => e
  raise ArgumentError, "Invalid address from IPAddr.new: #{ipaddr}" if e.message.include?('invalid address')

  raise e
end

Instance Method Details

#cidrObject



35
36
37
# File 'lib/puppet_x/puppetlabs/firewall/ipcidr.rb', line 35

def cidr
  "#{self}/#{prefixlen}"
end

#netmaskObject



17
18
19
# File 'lib/puppet_x/puppetlabs/firewall/ipcidr.rb', line 17

def netmask
  _to_string(@mask_addr)
end

#prefixlenObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/puppet_x/puppetlabs/firewall/ipcidr.rb', line 21

def prefixlen
  m = case @family
      when Socket::AF_INET
        IN4MASK
      when Socket::AF_INET6
        IN6MASK
      else
        raise 'unsupported address family'
      end
  return Regexp.last_match(1).length if %r{\A(1*)(0*)\z} =~ (@mask_addr & m).to_s(2)

  raise 'bad addr_mask format'
end