]> review.fuel-infra Code Review - puppet-modules/puppetlabs-firewall.git/commitdiff
add ipcidr util
authorJonathan Boyett <jonathan@failingservers.com>
Mon, 9 May 2011 17:52:26 +0000 (10:52 -0700)
committerJonathan Boyett <jonathan@failingservers.com>
Mon, 9 May 2011 17:52:26 +0000 (10:52 -0700)
lib/puppet/util/ipcidr.rb [new file with mode: 0644]

diff --git a/lib/puppet/util/ipcidr.rb b/lib/puppet/util/ipcidr.rb
new file mode 100644 (file)
index 0000000..77d9cb2
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.
+
+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