From: Stoiko Ivanov <s.ivanov@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [RFC common 1/1] tools: add get_wildcard_address
Date: Thu, 29 Apr 2021 15:37:01 +0200 [thread overview]
Message-ID: <20210429133702.23584-2-s.ivanov@proxmox.com> (raw)
In-Reply-To: <20210429133702.23584-1-s.ivanov@proxmox.com>
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@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
next prev parent reply other threads:[~2021-04-29 13:37 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-29 13:37 [pve-devel] [RFC common/manger] use appropriate wildcard address for pveproxy/spiceproxy Stoiko Ivanov
2021-04-29 13:37 ` Stoiko Ivanov [this message]
2021-04-29 13:37 ` [pve-devel] [RFC manager 1/1] proxy: fix wildcard address use Stoiko Ivanov
2021-04-30 7:12 ` Wolfgang Bumiller
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210429133702.23584-2-s.ivanov@proxmox.com \
--to=s.ivanov@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox