From patchwork Sat Mar 5 09:50:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Markus Armbruster X-Patchwork-Id: 8509711 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5F86D9F314 for ; Sat, 5 Mar 2016 09:52:51 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4CB712022A for ; Sat, 5 Mar 2016 09:52:50 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 0AF6F201B9 for ; Sat, 5 Mar 2016 09:52:49 +0000 (UTC) Received: from localhost ([::1]:45457 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ac8tE-0004SJ-GR for patchwork-qemu-devel@patchwork.kernel.org; Sat, 05 Mar 2016 04:52:48 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39983) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ac8rL-00018X-0E for qemu-devel@nongnu.org; Sat, 05 Mar 2016 04:50:52 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ac8rI-0001by-LA for qemu-devel@nongnu.org; Sat, 05 Mar 2016 04:50:50 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41053) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ac8rI-0001bY-BI for qemu-devel@nongnu.org; Sat, 05 Mar 2016 04:50:48 -0500 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (Postfix) with ESMTPS id E902819CF3B for ; Sat, 5 Mar 2016 09:50:47 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-116-19.ams2.redhat.com [10.36.116.19]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u259oihd017557 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Sat, 5 Mar 2016 04:50:45 -0500 Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 02F403011FAB; Sat, 5 Mar 2016 10:50:41 +0100 (CET) From: Markus Armbruster To: qemu-devel@nongnu.org Date: Sat, 5 Mar 2016 10:50:37 +0100 Message-Id: <1457171441-24602-9-git-send-email-armbru@redhat.com> In-Reply-To: <1457171441-24602-1-git-send-email-armbru@redhat.com> References: <1457171441-24602-1-git-send-email-armbru@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PULL v2 08/12] util: Shorten references into SocketAddress X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Blake An upcoming patch will alter how simple unions, like SocketAddress, are laid out, which will impact all lines of the form 'addr->u.XXX' (expanding it to the longer 'addr->u.XXX.data'). For better legibility in that patch, and less need for line wrapping, it's better to use a temporary variable to reduce the effect of a layout change to just the variable initializations, rather than every reference within a SocketAddress. Also, take advantage of some C99 initialization where it makes sense (simplifying g_new0() to g_new()). Signed-off-by: Eric Blake Message-Id: <1457021813-10704-7-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster --- block/nbd.c | 14 ++++++------ qemu-char.c | 49 ++++++++++++++++++++++++------------------ qemu-nbd.c | 9 ++++---- tests/test-io-channel-socket.c | 34 ++++++++++++++++++----------- ui/vnc.c | 39 +++++++++++++++++---------------- util/qemu-sockets.c | 11 +++++----- 6 files changed, 88 insertions(+), 68 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index db57b49..9f333c9 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -204,18 +204,20 @@ static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export, saddr = g_new0(SocketAddress, 1); if (qdict_haskey(options, "path")) { + UnixSocketAddress *q_unix; saddr->type = SOCKET_ADDRESS_KIND_UNIX; - saddr->u.q_unix = g_new0(UnixSocketAddress, 1); - saddr->u.q_unix->path = g_strdup(qdict_get_str(options, "path")); + q_unix = saddr->u.q_unix = g_new0(UnixSocketAddress, 1); + q_unix->path = g_strdup(qdict_get_str(options, "path")); qdict_del(options, "path"); } else { + InetSocketAddress *inet; saddr->type = SOCKET_ADDRESS_KIND_INET; - saddr->u.inet = g_new0(InetSocketAddress, 1); - saddr->u.inet->host = g_strdup(qdict_get_str(options, "host")); + inet = saddr->u.inet = g_new0(InetSocketAddress, 1); + inet->host = g_strdup(qdict_get_str(options, "host")); if (!qdict_get_try_str(options, "port")) { - saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); + inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); } else { - saddr->u.inet->port = g_strdup(qdict_get_str(options, "port")); + inet->port = g_strdup(qdict_get_str(options, "port")); } qdict_del(options, "host"); qdict_del(options, "port"); diff --git a/qemu-char.c b/qemu-char.c index 5ea1d34..af31102 100644 --- a/qemu-char.c +++ b/qemu-char.c @@ -3659,20 +3659,23 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend, addr = g_new0(SocketAddress, 1); if (path) { + UnixSocketAddress *q_unix; addr->type = SOCKET_ADDRESS_KIND_UNIX; - addr->u.q_unix = g_new0(UnixSocketAddress, 1); - addr->u.q_unix->path = g_strdup(path); + q_unix = addr->u.q_unix = g_new0(UnixSocketAddress, 1); + q_unix->path = g_strdup(path); } else { addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(host); - addr->u.inet->port = g_strdup(port); - addr->u.inet->has_to = qemu_opt_get(opts, "to"); - addr->u.inet->to = qemu_opt_get_number(opts, "to", 0); - addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); - addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); - addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); - addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); + addr->u.inet = g_new(InetSocketAddress, 1); + *addr->u.inet = (InetSocketAddress) { + .host = g_strdup(host), + .port = g_strdup(port), + .has_to = qemu_opt_get(opts, "to"), + .to = qemu_opt_get_number(opts, "to", 0), + .has_ipv4 = qemu_opt_get(opts, "ipv4"), + .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), + .has_ipv6 = qemu_opt_get(opts, "ipv6"), + .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), + }; } sock->addr = addr; } @@ -3711,22 +3714,26 @@ static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend, addr = g_new0(SocketAddress, 1); addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(host); - addr->u.inet->port = g_strdup(port); - addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); - addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); - addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); - addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); + addr->u.inet = g_new(InetSocketAddress, 1); + *addr->u.inet = (InetSocketAddress) { + .host = g_strdup(host), + .port = g_strdup(port), + .has_ipv4 = qemu_opt_get(opts, "ipv4"), + .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0), + .has_ipv6 = qemu_opt_get(opts, "ipv6"), + .ipv6 = qemu_opt_get_bool(opts, "ipv6", 0), + }; udp->remote = addr; if (has_local) { udp->has_local = true; addr = g_new0(SocketAddress, 1); addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(localaddr); - addr->u.inet->port = g_strdup(localport); + addr->u.inet = g_new(InetSocketAddress, 1); + *addr->u.inet = (InetSocketAddress) { + .host = g_strdup(localaddr), + .port = g_strdup(localport), + }; udp->local = addr; } } diff --git a/qemu-nbd.c b/qemu-nbd.c index 5fe94d0..a5c1d95 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -380,13 +380,14 @@ static SocketAddress *nbd_build_socket_address(const char *sockpath, saddr->u.q_unix = g_new0(UnixSocketAddress, 1); saddr->u.q_unix->path = g_strdup(sockpath); } else { + InetSocketAddress *inet; saddr->type = SOCKET_ADDRESS_KIND_INET; - saddr->u.inet = g_new0(InetSocketAddress, 1); - saddr->u.inet->host = g_strdup(bindto); + inet = saddr->u.inet = g_new0(InetSocketAddress, 1); + inet->host = g_strdup(bindto); if (port) { - saddr->u.inet->port = g_strdup(port); + inet->port = g_strdup(port); } else { - saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); + inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); } } diff --git a/tests/test-io-channel-socket.c b/tests/test-io-channel-socket.c index 0697363..8a34056 100644 --- a/tests/test-io-channel-socket.c +++ b/tests/test-io-channel-socket.c @@ -1,7 +1,7 @@ /* * QEMU I/O channel sockets test * - * Copyright (c) 2015 Red Hat, Inc. + * Copyright (c) 2015-2016 Red Hat, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -283,14 +283,18 @@ static void test_io_channel_ipv4(bool async) SocketAddress *connect_addr = g_new0(SocketAddress, 1); listen_addr->type = SOCKET_ADDRESS_KIND_INET; - listen_addr->u.inet = g_new0(InetSocketAddress, 1); - listen_addr->u.inet->host = g_strdup("127.0.0.1"); - listen_addr->u.inet->port = NULL; /* Auto-select */ + listen_addr->u.inet = g_new(InetSocketAddress, 1); + *listen_addr->u.inet = (InetSocketAddress) { + .host = g_strdup("127.0.0.1"), + .port = NULL, /* Auto-select */ + }; connect_addr->type = SOCKET_ADDRESS_KIND_INET; - connect_addr->u.inet = g_new0(InetSocketAddress, 1); - connect_addr->u.inet->host = g_strdup("127.0.0.1"); - connect_addr->u.inet->port = NULL; /* Filled in later */ + connect_addr->u.inet = g_new(InetSocketAddress, 1); + *connect_addr->u.inet = (InetSocketAddress) { + .host = g_strdup("127.0.0.1"), + .port = NULL, /* Filled in later */ + }; test_io_channel(async, listen_addr, connect_addr, false); @@ -317,14 +321,18 @@ static void test_io_channel_ipv6(bool async) SocketAddress *connect_addr = g_new0(SocketAddress, 1); listen_addr->type = SOCKET_ADDRESS_KIND_INET; - listen_addr->u.inet = g_new0(InetSocketAddress, 1); - listen_addr->u.inet->host = g_strdup("::1"); - listen_addr->u.inet->port = NULL; /* Auto-select */ + listen_addr->u.inet = g_new(InetSocketAddress, 1); + *listen_addr->u.inet = (InetSocketAddress) { + .host = g_strdup("::1"), + .port = NULL, /* Auto-select */ + }; connect_addr->type = SOCKET_ADDRESS_KIND_INET; - connect_addr->u.inet = g_new0(InetSocketAddress, 1); - connect_addr->u.inet->host = g_strdup("::1"); - connect_addr->u.inet->port = NULL; /* Filled in later */ + connect_addr->u.inet = g_new(InetSocketAddress, 1); + *connect_addr->u.inet = (InetSocketAddress) { + .host = g_strdup("::1"), + .port = NULL, /* Filled in later */ + }; test_io_channel(async, listen_addr, connect_addr, false); diff --git a/ui/vnc.c b/ui/vnc.c index ce4c669..6cd6314 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -3530,12 +3530,13 @@ void vnc_display_open(const char *id, Error **errp) } } else { unsigned long long baseport; + InetSocketAddress *inet; saddr->type = SOCKET_ADDRESS_KIND_INET; - saddr->u.inet = g_new0(InetSocketAddress, 1); + inet = saddr->u.inet = g_new0(InetSocketAddress, 1); if (vnc[0] == '[' && vnc[hlen - 1] == ']') { - saddr->u.inet->host = g_strndup(vnc + 1, hlen - 2); + inet->host = g_strndup(vnc + 1, hlen - 2); } else { - saddr->u.inet->host = g_strndup(vnc, hlen); + inet->host = g_strndup(vnc, hlen); } if (parse_uint_full(h + 1, &baseport, 10) < 0) { error_setg(errp, "can't convert to a number: %s", h + 1); @@ -3546,32 +3547,32 @@ void vnc_display_open(const char *id, Error **errp) error_setg(errp, "port %s out of range", h + 1); goto fail; } - saddr->u.inet->port = g_strdup_printf( + inet->port = g_strdup_printf( "%d", (int)baseport + 5900); if (to) { - saddr->u.inet->has_to = true; - saddr->u.inet->to = to + 5900; + inet->has_to = true; + inet->to = to + 5900; } - saddr->u.inet->ipv4 = ipv4; - saddr->u.inet->has_ipv4 = has_ipv4; - saddr->u.inet->ipv6 = ipv6; - saddr->u.inet->has_ipv6 = has_ipv6; + inet->ipv4 = ipv4; + inet->has_ipv4 = has_ipv4; + inet->ipv6 = ipv6; + inet->has_ipv6 = has_ipv6; if (vs->ws_enabled) { wsaddr->type = SOCKET_ADDRESS_KIND_INET; - wsaddr->u.inet = g_new0(InetSocketAddress, 1); - wsaddr->u.inet->host = g_strdup(saddr->u.inet->host); - wsaddr->u.inet->port = g_strdup(websocket); + inet = wsaddr->u.inet = g_new0(InetSocketAddress, 1); + inet->host = g_strdup(saddr->u.inet->host); + inet->port = g_strdup(websocket); if (to) { - wsaddr->u.inet->has_to = true; - wsaddr->u.inet->to = to; + inet->has_to = true; + inet->to = to; } - wsaddr->u.inet->ipv4 = ipv4; - wsaddr->u.inet->has_ipv4 = has_ipv4; - wsaddr->u.inet->ipv6 = ipv6; - wsaddr->u.inet->has_ipv6 = has_ipv6; + inet->ipv4 = ipv4; + inet->has_ipv4 = has_ipv4; + inet->ipv6 = ipv6; + inet->has_ipv6 = has_ipv6; } } } else { diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c index 557da20..ad7c00c 100644 --- a/util/qemu-sockets.c +++ b/util/qemu-sockets.c @@ -1003,6 +1003,7 @@ socket_sockaddr_to_address_inet(struct sockaddr_storage *sa, char host[NI_MAXHOST]; char serv[NI_MAXSERV]; SocketAddress *addr; + InetSocketAddress *inet; int ret; ret = getnameinfo((struct sockaddr *)sa, salen, @@ -1017,13 +1018,13 @@ socket_sockaddr_to_address_inet(struct sockaddr_storage *sa, addr = g_new0(SocketAddress, 1); addr->type = SOCKET_ADDRESS_KIND_INET; - addr->u.inet = g_new0(InetSocketAddress, 1); - addr->u.inet->host = g_strdup(host); - addr->u.inet->port = g_strdup(serv); + inet = addr->u.inet = g_new0(InetSocketAddress, 1); + inet->host = g_strdup(host); + inet->port = g_strdup(serv); if (sa->ss_family == AF_INET) { - addr->u.inet->has_ipv4 = addr->u.inet->ipv4 = true; + inet->has_ipv4 = inet->ipv4 = true; } else { - addr->u.inet->has_ipv6 = addr->u.inet->ipv6 = true; + inet->has_ipv6 = inet->ipv6 = true; } return addr;