From c33751f1f5288b1e2c726b986793e6d351d17739 Mon Sep 17 00:00:00 2001 From: Olivier Blin Date: Thu, 3 Apr 2008 03:00:22 +0000 Subject: add netmask_to_vlsm function (for shorewall masq computing) --- lib/network/network.pm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/network/network.pm b/lib/network/network.pm index 4ec23a7..827e567 100644 --- a/lib/network/network.pm +++ b/lib/network/network.pm @@ -353,6 +353,18 @@ sub netmask { } } +sub netmask_to_vlsm { + my ($netmask) = @_; + #- based on Network::IPv4Addr::ipv4_msk2cidr + my @bytes = split /\./, $netmask; + my $prefix = 0; + foreach (@bytes) { + my $bits = unpack("B*", pack("C", $_)); + $prefix += $bits =~ tr/1/1/; + } + return $prefix; +} + sub masked_ip { my ($ip) = @_; my @ip = is_ip($ip) or return ''; -- cgit v1.2.1