[pve-devel] [RFC common 1/1] tools: add get_wildcard_address

Stoiko Ivanov s.ivanov at proxmox.com
Thu Apr 29 15:37:01 CEST 2021


the get_wildcard_address sub returns the appropriate wildcard address:
* '::' if the hosts has any v6 address (including link-local scoped)
  configured (i.e. default behavior without any modification to sysctl
  or kernel commandline)
* '0.0.0.0' if ipv6 is disabled via sysctl or kernel commandline

This should ensure the best behaviour for listening sockets in
dual-stacked environments:
* binding to '::' allows to connect via both v6 and v4 addresses (the
  latter are treated as v4-mapped-v6 addresses (::ffff:192.0.2.1))
* the current unconditional bind to '::' fails if AF_INET6 is not
  supported via kernel commandline setting [0].
* the previous approach of finding the family based on the node-name
  getaddrinfo result leads to the service only being available on v4
  despite having a working v6 address configured, in case it's
  /etc/hosts entry points only to the v4 address.

[0] on linux passing ipv6.disable=1 on the kernel commandline removes
support for AF_INET6 sockets (setting the net.ipv6.conf.*.disable_ipv6
sysctls keeps support)

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PVE/Tools.pm | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/PVE/Tools.pm b/src/PVE/Tools.pm
index 16ae3d2..fb520ce 100644
--- a/src/PVE/Tools.pm
+++ b/src/PVE/Tools.pm
@@ -4,8 +4,8 @@ use strict;
 use warnings;
 use POSIX qw(EINTR EEXIST EOPNOTSUPP);
 use IO::Socket::IP;
-use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED AI_CANONNAME SOCK_DGRAM
-	      IPPROTO_TCP);
+use Socket qw(AF_INET AF_INET6 AI_ALL AI_V4MAPPED AI_CANONNAME AI_PASSIVE
+	    AI_ADDRCONFIG NI_NUMERICHOST NI_NUMERICSERV SOCK_DGRAM IPPROTO_TCP);
 use IO::Select;
 use File::Basename;
 use File::Path qw(make_path);
@@ -1405,6 +1405,24 @@ sub get_host_address_family {
     return $res[0]->{family};
 }
 
+# returns "::" if the host supports AF_INET6 else "0.0.0.0"
+sub get_wildcard_address {
+    my %hints = (
+	flags => AI_PASSIVE|AI_ADDRCONFIG,
+    );
+    my ($err, @res) = Socket::getaddrinfo('', '0', \%hints);
+    die "failed to get address info for wildcard address: $err\n" if $err;
+
+    my $sockaddr = $res[0]->{addr};
+    if (my @v6support = grep {$_->{family} eq AF_INET6} @res) {;
+	$sockaddr = $v6support[0]->{addr};
+    }
+    my $host;
+    ($err, $host) = Socket::getnameinfo($sockaddr, NI_NUMERICHOST|NI_NUMERICSERV);
+    die "failed to get name info for wildcard address: $err\n" if $err;
+    return $host;
+}
+
 # get the fully qualified domain name of a host
 # same logic as hostname(1): The FQDN is the name getaddrinfo(3) returns,
 # given a nodename as a parameter
-- 
2.20.1






More information about the pve-devel mailing list