]> git.proxmox.com Git - pve-common.git/commitdiff
network: IP_from_cidr and is_ip_in_cidr helpers
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Fri, 16 Oct 2015 13:53:49 +0000 (15:53 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 16 Oct 2015 14:51:01 +0000 (16:51 +0200)
src/PVE/Network.pm

index 34a280f60022292469d6ed1e4c7c32793cd62981..024b9b4917afc62318d54ca75da86f27b71da208 100644 (file)
@@ -9,6 +9,8 @@ use File::Basename;
 use IO::Socket::IP;
 use POSIX qw(ECONNREFUSED);
 
+use Net::IP;
+
 # host network related utility functions
 
 our $ipv4_reverse_mask = [
@@ -467,4 +469,35 @@ sub tcp_ping {
     return $result;
 }
 
+sub IP_from_cidr {
+    my ($cidr, $version) = @_;
+
+    return if $cidr !~ m!^(\S+?)/(\S+)$!;
+    my ($ip, $prefix) = ($1, $2);
+
+    my $ipobj = Net::IP->new($ip, $version);
+    return if !$ipobj;
+
+    $version = $ipobj->version();
+
+    my $binmask = Net::IP::ip_get_mask($prefix, $version);
+    return if !$binmask;
+
+    my $masked_binip = $ipobj->binip() & $binmask;
+    my $masked_ip = Net::IP::ip_bintoip($masked_binip, $version);
+    return Net::IP->new("$masked_ip/$prefix");
+}
+
+sub is_ip_in_cidr {
+    my ($ip, $cidr, $version) = @_;
+
+    my $cidr_obj = IP_from_cidr($cidr, $version);
+    return undef if !$cidr_obj;
+
+    my $ip_obj = Net::IP->new($ip, $version);
+    return undef if !$ip_obj;
+
+    return $cidr_obj->overlaps($ip_obj) == $Net::IP::IP_B_IN_A_OVERLAP;
+}
+
 1;