]> git.proxmox.com Git - pve-common.git/commitdiff
network: add canonical_ip function
authorFabian Ebner <f.ebner@proxmox.com>
Mon, 10 May 2021 12:18:15 +0000 (14:18 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 17 Jun 2021 13:17:37 +0000 (15:17 +0200)
Net::IP doesn't seem to have a function for it and normalizing to the full
quad-form is less then ideal if we inted to output IPv6 addresses returned by
that function at some point.

Instead, use NetAddr::IP, which is already used in pve-network.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
src/PVE/Network.pm

index 2d63a45c146d46d26de6e1f139bb1c8afed69d38..48268b1a5195b1563c737bedb67d1513dc33a6d9 100644 (file)
@@ -10,6 +10,7 @@ use PVE::Tools qw(run_command lock_file);
 use File::Basename;
 use IO::Socket::IP;
 use Net::IP;
+use NetAddr::IP qw(:lower);
 use POSIX qw(ECONNREFUSED);
 use Socket qw(NI_NUMERICHOST NI_NUMERICSERV);
 
@@ -655,4 +656,13 @@ 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();
+}
+
 1;