]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/Network.pm
net: add get_reachable_networks
[pve-common.git] / src / PVE / Network.pm
index 38019f7e3aedff5db8a52821947ad639f74db291..e44648bb56e48bae40386039200f4e66f9949e7f 100644 (file)
@@ -9,13 +9,15 @@ use PVE::Tools qw(run_command lock_file);
 
 use File::Basename;
 use IO::Socket::IP;
+use JSON;
 use Net::IP;
+use NetAddr::IP qw(:lower);
 use POSIX qw(ECONNREFUSED);
 use Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
 
 # host network related utility functions
 
-our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib\d+)/;
+our $PHYSICAL_NIC_RE = qr/(?:eth\d+|en[^:.]+|ib[^:.]+)/;
 
 our $ipv4_reverse_mask = [
     '0.0.0.0',
@@ -592,9 +594,44 @@ 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 if !defined($overlap);
+
+    return $overlap == $Net::IP::IP_B_IN_A_OVERLAP || $overlap == $Net::IP::IP_IDENTICAL;
 }
 
+# get all currently configured addresses that have a global scope, i.e., are reachable from the
+# outside of the host and thus are neither loopback nor link-local ones
+# returns an array ref of: { addr => "IP", cidr => "IP/PREFIXLEN", family => "inet|inet6" }
+sub get_reachable_networks {
+    my $raw = '';
+    run_command([qw(ip -j addr show up scope global)], outfunc => sub { $raw .= shift });
+    my $addrs = decode_json($raw);
+
+    # sort by family (IPv4/IPv6) and then by addr, just for some stability
+    my $addrs_sorter = sub {
+       my ($a, $b) = @_;
+       my $family = $a->{family} cmp $b->{family};
+       return $family if $family != 0;
+       return $a->{local} cmp $b->{local};
+    };
+
+    my $res = [];
+    for my $addr ($addrs->@*) {
+       next if !exists $addr->{addr_info};
+       next if grep { $_ eq 'LOOPBACK' } $addr->{flags}->@*;
+       for my $info (sort $addrs_sorter grep { $_ && $_->{local}} $addr->{addr_info}->@*) {
+           push $res->@*, {
+               addr => $info->{local},
+               cidr => "$info->{local}/$info->{prefixlen}",
+               family => $info->{family},
+           };
+       }
+    }
+
+    return $res;
+}
 
 sub get_local_ip_from_cidr {
     my ($cidr) = @_;
@@ -627,21 +664,15 @@ sub get_ip_from_hostname {
        return undef;
     }
 
-    my ($ip, $family);
     for my $ai (@res) {
-       $family = $ai->{family};
-       my $tmpip = addr_to_ip($ai->{addr});
-       if ($tmpip !~ m/^127\.|^::1$/) {
-           $ip = $tmpip;
-           last;
+       my $ip = addr_to_ip($ai->{addr});
+       if ($ip !~ m/^127\.|^::1$/) {
+           return wantarray ? ($ip, $ai->{family}) : $ip;
        }
     }
-    if (!defined($ip) ) {
-       die "hostname lookup '$hostname' failed - got local IP address '$ip'\n" if !$noerr;
-       return undef;
-    }
-
-    return wantarray ? ($ip, $family) : $ip;
+    # NOTE: we only get here if no WAN/LAN IP was found, so this is now the error path!
+    die "address lookup for '$hostname' did not find any IP address\n" if !$noerr;
+    return undef;
 }
 
 sub lock_network {
@@ -651,4 +682,33 @@ sub lock_network {
     return $res;
 }
 
+# the canonical form of the given IP, i.e. dotted quad for IPv4 and RFC 5952 for IPv6
+sub canonical_ip {
+    my ($ip) = @_;
+
+    my $ip_obj = NetAddr::IP->new($ip) or die "invalid IP string '$ip'\n";
+
+    return $ip_obj->canon();
+}
+
+# List of unique, canonical IPs in the provided list.
+# Keeps the original order, filtering later duplicates.
+sub unique_ips {
+    my ($ips) = @_;
+
+    my $res = [];
+    my $seen = {};
+
+    for my $ip (@{$ips}) {
+       $ip = canonical_ip($ip);
+
+       next if $seen->{$ip};
+
+       $seen->{$ip} = 1;
+       push @{$res}, $ip;
+    }
+
+    return $res;
+}
+
 1;