]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Network.pm
network: is_ip_in_cidr: correctly handle the CIDR being a singleton range
[pve-common.git] / src / PVE / Network.pm
index 12536c7609e9cc58182e97e8672ccd00e12ade72..366d24236d52f2d3a7be06832942b7baaa23acb2 100644 (file)
@@ -592,21 +592,24 @@ sub is_ip_in_cidr {
     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;
+    my $overlap = $cidr_obj->overlaps($ip_obj);
+
+    return $overlap == $Net::IP::IP_B_IN_A_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL;
 }
 
 
 sub get_local_ip_from_cidr {
     my ($cidr) = @_;
 
-    my $IPs = [];
+    my $IPs = {};
+    my $i = 1;
     run_command(['/sbin/ip', 'address', 'show', 'to', $cidr, 'up'], outfunc => sub {
        if ($_[0] =~ m!^\s*inet(?:6)?\s+($PVE::Tools::IPRE)(?:/\d+|\s+peer\s+)!) {
-           push @$IPs, $1;
+           $IPs->{$1} = $i++ if !exists($IPs->{$1});
        }
     });
 
-    return $IPs;
+    return [ sort { $IPs->{$a} <=> $IPs->{$b} } keys %{$IPs} ];
 }
 
 sub addr_to_ip {