From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
Paolo Bonzini <pbonzini@redhat.com>
Subject: [Qemu-devel] [PULL 3/7] sockets: allow port to be NULL when listening on IP address
Date: Tue, 20 Oct 2015 16:33:58 +0100 [thread overview]
Message-ID: <1445355242-4397-4-git-send-email-berrange@redhat.com> (raw)
In-Reply-To: <1445355242-4397-1-git-send-email-berrange@redhat.com>
If the port in the SocketAddress struct is NULL, it can allow
the kernel to automatically select a free port. This is useful
in particular in unit tests to avoid a race trying to find a
free port to run a test case on.
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
---
qapi-schema.json | 6 ++++--
util/qemu-sockets.c | 18 +++++++++++++-----
2 files changed, 17 insertions(+), 7 deletions(-)
diff --git a/qapi-schema.json b/qapi-schema.json
index 702b7b5..f60be29 100644
--- a/qapi-schema.json
+++ b/qapi-schema.json
@@ -2614,7 +2614,9 @@
#
# @host: host part of the address
#
-# @port: port part of the address, or lowest port if @to is present
+# @port: port part of the address, or lowest port if @to is present.
+# Kernel selects a free port if omitted for listener addresses.
+# #optional
#
# @to: highest port to try
#
@@ -2629,7 +2631,7 @@
{ 'struct': 'InetSocketAddress',
'data': {
'host': 'str',
- 'port': 'str',
+ '*port': 'str',
'*to': 'uint16',
'*ipv4': 'bool',
'*ipv6': 'bool' } }
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 3338452..9142917 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -128,12 +128,15 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
ai.ai_family = PF_UNSPEC;
ai.ai_socktype = SOCK_STREAM;
- if ((qemu_opt_get(opts, "host") == NULL) ||
- (qemu_opt_get(opts, "port") == NULL)) {
- error_setg(errp, "host and/or port not specified");
+ if ((qemu_opt_get(opts, "host") == NULL)) {
+ error_setg(errp, "host not specified");
return -1;
}
- pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
+ if (qemu_opt_get(opts, "port") != NULL) {
+ pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
+ } else {
+ port[0] = '\0';
+ }
addr = qemu_opt_get(opts, "host");
to = qemu_opt_get_number(opts, "to", 0);
@@ -145,6 +148,10 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
/* lookup */
if (port_offset) {
unsigned long long baseport;
+ if (strlen(port) == 0) {
+ error_setg(errp, "port not specified");
+ return -1;
+ }
if (parse_uint_full(port, &baseport, 10) < 0) {
error_setg(errp, "can't convert to a number: %s", port);
return -1;
@@ -156,7 +163,8 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
}
snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);
}
- rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
+ rc = getaddrinfo(strlen(addr) ? addr : NULL,
+ strlen(port) ? port : NULL, &ai, &res);
if (rc != 0) {
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
gai_strerror(rc));
--
2.4.3
next prev parent reply other threads:[~2015-10-20 15:34 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-20 15:33 [Qemu-devel] [PULL 0/7] Misc refactoring to util codebase Daniel P. Berrange
2015-10-20 15:33 ` [Qemu-devel] [PULL 1/7] sockets: add helpers for creating SocketAddress from a socket Daniel P. Berrange
2015-10-20 15:33 ` [Qemu-devel] [PULL 2/7] sockets: move qapi_copy_SocketAddress into qemu-sockets.c Daniel P. Berrange
2015-10-20 15:33 ` Daniel P. Berrange [this message]
2015-10-20 15:33 ` [Qemu-devel] [PULL 4/7] ui: convert VNC startup code to use SocketAddress Daniel P. Berrange
2015-10-20 15:34 ` [Qemu-devel] [PULL 5/7] osdep: add qemu_fork() wrapper for safely handling signals Daniel P. Berrange
2015-10-20 15:34 ` [Qemu-devel] [PULL 6/7] coroutine: move into libqemuutil.a library Daniel P. Berrange
2015-10-20 15:34 ` [Qemu-devel] [PULL 7/7] util: pull Buffer code out of VNC module Daniel P. Berrange
2015-10-20 15:38 ` [Qemu-devel] [PULL 0/7] Misc refactoring to util codebase Peter Maydell
2015-10-20 15:38 ` Daniel P. Berrange
2015-10-21 12:27 ` Peter Maydell
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=1445355242-4397-4-git-send-email-berrange@redhat.com \
--to=berrange@redhat.com \
--cc=pbonzini@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.