From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Knut Omang <knut.omang@oracle.com>,
"Daniel P . Berrange" <berrange@redhat.com>
Subject: [Qemu-devel] [PULL v1 3/9] sockets: factor out a new try_bind() function
Date: Tue, 5 Sep 2017 10:22:24 +0100 [thread overview]
Message-ID: <20170905092230.8243-4-berrange@redhat.com> (raw)
In-Reply-To: <20170905092230.8243-1-berrange@redhat.com>
From: Knut Omang <knut.omang@oracle.com>
A refactoring step to prepare for the problem
exposed by the test-listen test in the previous commit.
Simplify and reorganize the IPv6 specific extra
measures and move it out of the for loop to increase
code readability. No semantic changes.
Signed-off-by: Knut Omang <knut.omang@oracle.com>
Reviewed-by: Daniel P. Berrange <berrange@redhat.com>
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
util/qemu-sockets.c | 69 ++++++++++++++++++++++++++++++-----------------------
1 file changed, 39 insertions(+), 30 deletions(-)
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 1358c81bcc..b4a2f243f4 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -149,6 +149,44 @@ int inet_ai_family_from_address(InetSocketAddress *addr,
return PF_UNSPEC;
}
+static int try_bind(int socket, InetSocketAddress *saddr, struct addrinfo *e)
+{
+#ifndef IPV6_V6ONLY
+ return bind(socket, e->ai_addr, e->ai_addrlen);
+#else
+ /*
+ * Deals with first & last cases in matrix in comment
+ * for inet_ai_family_from_address().
+ */
+ int v6only =
+ ((!saddr->has_ipv4 && !saddr->has_ipv6) ||
+ (saddr->has_ipv4 && saddr->ipv4 &&
+ saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1;
+ int stat;
+
+ rebind:
+ if (e->ai_family == PF_INET6) {
+ qemu_setsockopt(socket, IPPROTO_IPV6, IPV6_V6ONLY, &v6only,
+ sizeof(v6only));
+ }
+
+ stat = bind(socket, e->ai_addr, e->ai_addrlen);
+ if (!stat) {
+ return 0;
+ }
+
+ /* If we got EADDRINUSE from an IPv6 bind & v6only is unset,
+ * it could be that the IPv4 port is already claimed, so retry
+ * with v6only set
+ */
+ if (e->ai_family == PF_INET6 && errno == EADDRINUSE && !v6only) {
+ v6only = 1;
+ goto rebind;
+ }
+ return stat;
+#endif
+}
+
static int inet_listen_saddr(InetSocketAddress *saddr,
int port_offset,
bool update_addr,
@@ -228,39 +266,10 @@ static int inet_listen_saddr(InetSocketAddress *saddr,
port_min = inet_getport(e);
port_max = saddr->has_to ? saddr->to + port_offset : port_min;
for (p = port_min; p <= port_max; p++) {
-#ifdef IPV6_V6ONLY
- /*
- * Deals with first & last cases in matrix in comment
- * for inet_ai_family_from_address().
- */
- int v6only =
- ((!saddr->has_ipv4 && !saddr->has_ipv6) ||
- (saddr->has_ipv4 && saddr->ipv4 &&
- saddr->has_ipv6 && saddr->ipv6)) ? 0 : 1;
-#endif
inet_setport(e, p);
-#ifdef IPV6_V6ONLY
- rebind:
- if (e->ai_family == PF_INET6) {
- qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &v6only,
- sizeof(v6only));
- }
-#endif
- if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
+ if (try_bind(slisten, saddr, e) >= 0) {
goto listen;
}
-
-#ifdef IPV6_V6ONLY
- /* If we got EADDRINUSE from an IPv6 bind & V6ONLY is unset,
- * it could be that the IPv4 port is already claimed, so retry
- * with V6ONLY set
- */
- if (e->ai_family == PF_INET6 && errno == EADDRINUSE && !v6only) {
- v6only = 1;
- goto rebind;
- }
-#endif
-
if (p == port_max) {
if (!e->ai_next) {
error_setg_errno(errp, errno, "Failed to bind socket");
--
2.13.5
next prev parent reply other threads:[~2017-09-05 9:22 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-09-05 9:22 [Qemu-devel] [PULL v1 0/9] Merge QEMU I/O 2017/09/05 Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 1/9] io: fix temp directory used by test-io-channel-tls test Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 2/9] tests: Add test-listen - a stress test for QEMU socket listen Daniel P. Berrange
2017-09-05 9:22 ` Daniel P. Berrange [this message]
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 4/9] sockets: factor out create_fast_reuse_socket Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 5/9] sockets: Handle race condition between binds to the same port Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 6/9] util: remove the obsolete non-blocking connect Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 7/9] io: fix typo in docs comment for qio_channel_read Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 8/9] io: add new qio_channel_{readv, writev, read, write}_all functions Daniel P. Berrange
2017-09-05 9:22 ` [Qemu-devel] [PULL v1 9/9] io: fix check for handshake completion in TLS test Daniel P. Berrange
2017-09-05 9:37 ` [Qemu-devel] [PULL v1 0/9] Merge QEMU I/O 2017/09/05 Peter Maydell
2017-09-05 9:42 ` Peter Maydell
2017-09-05 9:45 ` Daniel P. Berrange
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=20170905092230.8243-4-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=knut.omang@oracle.com \
--cc=peter.maydell@linaro.org \
--cc=qemu-devel@nongnu.org \
/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;
as well as URLs for NNTP newsgroup(s).