[pve-devel] [PATCH v2 common 03/14] network: add canonical_ip function

Fabian Ebner f.ebner at proxmox.com
Mon May 10 14:18:15 CEST 2021


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 at proxmox.com>
---

New in v2.

Introduces a new dependency on the libnetaddr-ip-perl package!

 src/PVE/Network.pm | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/PVE/Network.pm b/src/PVE/Network.pm
index 2d63a45..48268b1 100644
--- a/src/PVE/Network.pm
+++ b/src/PVE/Network.pm
@@ -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;
-- 
2.20.1






More information about the pve-devel mailing list