From b95e0c55398d2789cf36fe745dae5bf01d7fe56b Mon Sep 17 00:00:00 2001 From: Jonathan Boyett Date: Mon, 9 May 2011 10:52:26 -0700 Subject: [PATCH] add ipcidr util --- lib/puppet/util/ipcidr.rb | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/puppet/util/ipcidr.rb diff --git a/lib/puppet/util/ipcidr.rb b/lib/puppet/util/ipcidr.rb new file mode 100644 index 0000000..77d9cb2 --- /dev/null +++ b/lib/puppet/util/ipcidr.rb @@ -0,0 +1,45 @@ +# Puppet Firewall Module +# +# Copyright (C) 2011 Bob.sh Limited +# Copyright (C) 2008 Camptocamp Association +# Copyright (C) 2007 Dmitri Priimak +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +require 'ipaddr' + +module Puppet::Util::IPCidr < IPAddr + + def netmask + _to_string(@mask_addr) + end + + def prefixlen + m = case @family + when Socket::AF_INET + IN4MASK + when Socket::AF_INET6 + IN6MASK + else + raise "unsupported address family" + end + return $1.length if /\A(1*)(0*)\z/ =~ (@mask_addr & m).to_s(2) + raise "bad addr_mask format" + end + + def cidr + cidr = sprintf("%s/%s", self.to_s, self.prefixlen) + cidr + end +end -- 2.45.2