util: Shorten references into SocketAddress

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 <eblake@redhat.com>
Message-Id: <1457021813-10704-7-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
master
Eric Blake 2016-03-03 09:16:48 -07:00 committed by Markus Armbruster
parent f194a1ae53
commit 0399293e5b
6 changed files with 88 additions and 68 deletions

View File

@ -204,18 +204,20 @@ static SocketAddress *nbd_config(BDRVNBDState *s, QDict *options, char **export,
saddr = g_new0(SocketAddress, 1); saddr = g_new0(SocketAddress, 1);
if (qdict_haskey(options, "path")) { if (qdict_haskey(options, "path")) {
UnixSocketAddress *q_unix;
saddr->type = SOCKET_ADDRESS_KIND_UNIX; saddr->type = SOCKET_ADDRESS_KIND_UNIX;
saddr->u.q_unix = g_new0(UnixSocketAddress, 1); q_unix = saddr->u.q_unix = g_new0(UnixSocketAddress, 1);
saddr->u.q_unix->path = g_strdup(qdict_get_str(options, "path")); q_unix->path = g_strdup(qdict_get_str(options, "path"));
qdict_del(options, "path"); qdict_del(options, "path");
} else { } else {
InetSocketAddress *inet;
saddr->type = SOCKET_ADDRESS_KIND_INET; saddr->type = SOCKET_ADDRESS_KIND_INET;
saddr->u.inet = g_new0(InetSocketAddress, 1); inet = saddr->u.inet = g_new0(InetSocketAddress, 1);
saddr->u.inet->host = g_strdup(qdict_get_str(options, "host")); inet->host = g_strdup(qdict_get_str(options, "host"));
if (!qdict_get_try_str(options, "port")) { 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 { } 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, "host");
qdict_del(options, "port"); qdict_del(options, "port");

View File

@ -3659,20 +3659,23 @@ static void qemu_chr_parse_socket(QemuOpts *opts, ChardevBackend *backend,
addr = g_new0(SocketAddress, 1); addr = g_new0(SocketAddress, 1);
if (path) { if (path) {
UnixSocketAddress *q_unix;
addr->type = SOCKET_ADDRESS_KIND_UNIX; addr->type = SOCKET_ADDRESS_KIND_UNIX;
addr->u.q_unix = g_new0(UnixSocketAddress, 1); q_unix = addr->u.q_unix = g_new0(UnixSocketAddress, 1);
addr->u.q_unix->path = g_strdup(path); q_unix->path = g_strdup(path);
} else { } else {
addr->type = SOCKET_ADDRESS_KIND_INET; addr->type = SOCKET_ADDRESS_KIND_INET;
addr->u.inet = g_new0(InetSocketAddress, 1); addr->u.inet = g_new(InetSocketAddress, 1);
addr->u.inet->host = g_strdup(host); *addr->u.inet = (InetSocketAddress) {
addr->u.inet->port = g_strdup(port); .host = g_strdup(host),
addr->u.inet->has_to = qemu_opt_get(opts, "to"); .port = g_strdup(port),
addr->u.inet->to = qemu_opt_get_number(opts, "to", 0); .has_to = qemu_opt_get(opts, "to"),
addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); .to = qemu_opt_get_number(opts, "to", 0),
addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); .has_ipv4 = qemu_opt_get(opts, "ipv4"),
addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); .has_ipv6 = qemu_opt_get(opts, "ipv6"),
.ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
};
} }
sock->addr = addr; sock->addr = addr;
} }
@ -3711,22 +3714,26 @@ static void qemu_chr_parse_udp(QemuOpts *opts, ChardevBackend *backend,
addr = g_new0(SocketAddress, 1); addr = g_new0(SocketAddress, 1);
addr->type = SOCKET_ADDRESS_KIND_INET; addr->type = SOCKET_ADDRESS_KIND_INET;
addr->u.inet = g_new0(InetSocketAddress, 1); addr->u.inet = g_new(InetSocketAddress, 1);
addr->u.inet->host = g_strdup(host); *addr->u.inet = (InetSocketAddress) {
addr->u.inet->port = g_strdup(port); .host = g_strdup(host),
addr->u.inet->has_ipv4 = qemu_opt_get(opts, "ipv4"); .port = g_strdup(port),
addr->u.inet->ipv4 = qemu_opt_get_bool(opts, "ipv4", 0); .has_ipv4 = qemu_opt_get(opts, "ipv4"),
addr->u.inet->has_ipv6 = qemu_opt_get(opts, "ipv6"); .ipv4 = qemu_opt_get_bool(opts, "ipv4", 0),
addr->u.inet->ipv6 = qemu_opt_get_bool(opts, "ipv6", 0); .has_ipv6 = qemu_opt_get(opts, "ipv6"),
.ipv6 = qemu_opt_get_bool(opts, "ipv6", 0),
};
udp->remote = addr; udp->remote = addr;
if (has_local) { if (has_local) {
udp->has_local = true; udp->has_local = true;
addr = g_new0(SocketAddress, 1); addr = g_new0(SocketAddress, 1);
addr->type = SOCKET_ADDRESS_KIND_INET; addr->type = SOCKET_ADDRESS_KIND_INET;
addr->u.inet = g_new0(InetSocketAddress, 1); addr->u.inet = g_new(InetSocketAddress, 1);
addr->u.inet->host = g_strdup(localaddr); *addr->u.inet = (InetSocketAddress) {
addr->u.inet->port = g_strdup(localport); .host = g_strdup(localaddr),
.port = g_strdup(localport),
};
udp->local = addr; udp->local = addr;
} }
} }

View File

@ -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 = g_new0(UnixSocketAddress, 1);
saddr->u.q_unix->path = g_strdup(sockpath); saddr->u.q_unix->path = g_strdup(sockpath);
} else { } else {
InetSocketAddress *inet;
saddr->type = SOCKET_ADDRESS_KIND_INET; saddr->type = SOCKET_ADDRESS_KIND_INET;
saddr->u.inet = g_new0(InetSocketAddress, 1); inet = saddr->u.inet = g_new0(InetSocketAddress, 1);
saddr->u.inet->host = g_strdup(bindto); inet->host = g_strdup(bindto);
if (port) { if (port) {
saddr->u.inet->port = g_strdup(port); inet->port = g_strdup(port);
} else { } else {
saddr->u.inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT); inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* QEMU I/O channel sockets test * 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 * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * 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); SocketAddress *connect_addr = g_new0(SocketAddress, 1);
listen_addr->type = SOCKET_ADDRESS_KIND_INET; listen_addr->type = SOCKET_ADDRESS_KIND_INET;
listen_addr->u.inet = g_new0(InetSocketAddress, 1); listen_addr->u.inet = g_new(InetSocketAddress, 1);
listen_addr->u.inet->host = g_strdup("127.0.0.1"); *listen_addr->u.inet = (InetSocketAddress) {
listen_addr->u.inet->port = NULL; /* Auto-select */ .host = g_strdup("127.0.0.1"),
.port = NULL, /* Auto-select */
};
connect_addr->type = SOCKET_ADDRESS_KIND_INET; connect_addr->type = SOCKET_ADDRESS_KIND_INET;
connect_addr->u.inet = g_new0(InetSocketAddress, 1); connect_addr->u.inet = g_new(InetSocketAddress, 1);
connect_addr->u.inet->host = g_strdup("127.0.0.1"); *connect_addr->u.inet = (InetSocketAddress) {
connect_addr->u.inet->port = NULL; /* Filled in later */ .host = g_strdup("127.0.0.1"),
.port = NULL, /* Filled in later */
};
test_io_channel(async, listen_addr, connect_addr, false); 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); SocketAddress *connect_addr = g_new0(SocketAddress, 1);
listen_addr->type = SOCKET_ADDRESS_KIND_INET; listen_addr->type = SOCKET_ADDRESS_KIND_INET;
listen_addr->u.inet = g_new0(InetSocketAddress, 1); listen_addr->u.inet = g_new(InetSocketAddress, 1);
listen_addr->u.inet->host = g_strdup("::1"); *listen_addr->u.inet = (InetSocketAddress) {
listen_addr->u.inet->port = NULL; /* Auto-select */ .host = g_strdup("::1"),
.port = NULL, /* Auto-select */
};
connect_addr->type = SOCKET_ADDRESS_KIND_INET; connect_addr->type = SOCKET_ADDRESS_KIND_INET;
connect_addr->u.inet = g_new0(InetSocketAddress, 1); connect_addr->u.inet = g_new(InetSocketAddress, 1);
connect_addr->u.inet->host = g_strdup("::1"); *connect_addr->u.inet = (InetSocketAddress) {
connect_addr->u.inet->port = NULL; /* Filled in later */ .host = g_strdup("::1"),
.port = NULL, /* Filled in later */
};
test_io_channel(async, listen_addr, connect_addr, false); test_io_channel(async, listen_addr, connect_addr, false);

View File

@ -3530,12 +3530,13 @@ void vnc_display_open(const char *id, Error **errp)
} }
} else { } else {
unsigned long long baseport; unsigned long long baseport;
InetSocketAddress *inet;
saddr->type = SOCKET_ADDRESS_KIND_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] == ']') { 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 { } else {
saddr->u.inet->host = g_strndup(vnc, hlen); inet->host = g_strndup(vnc, hlen);
} }
if (parse_uint_full(h + 1, &baseport, 10) < 0) { if (parse_uint_full(h + 1, &baseport, 10) < 0) {
error_setg(errp, "can't convert to a number: %s", h + 1); 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); error_setg(errp, "port %s out of range", h + 1);
goto fail; goto fail;
} }
saddr->u.inet->port = g_strdup_printf( inet->port = g_strdup_printf(
"%d", (int)baseport + 5900); "%d", (int)baseport + 5900);
if (to) { if (to) {
saddr->u.inet->has_to = true; inet->has_to = true;
saddr->u.inet->to = to + 5900; inet->to = to + 5900;
} }
saddr->u.inet->ipv4 = ipv4; inet->ipv4 = ipv4;
saddr->u.inet->has_ipv4 = has_ipv4; inet->has_ipv4 = has_ipv4;
saddr->u.inet->ipv6 = ipv6; inet->ipv6 = ipv6;
saddr->u.inet->has_ipv6 = has_ipv6; inet->has_ipv6 = has_ipv6;
if (vs->ws_enabled) { if (vs->ws_enabled) {
wsaddr->type = SOCKET_ADDRESS_KIND_INET; wsaddr->type = SOCKET_ADDRESS_KIND_INET;
wsaddr->u.inet = g_new0(InetSocketAddress, 1); inet = wsaddr->u.inet = g_new0(InetSocketAddress, 1);
wsaddr->u.inet->host = g_strdup(saddr->u.inet->host); inet->host = g_strdup(saddr->u.inet->host);
wsaddr->u.inet->port = g_strdup(websocket); inet->port = g_strdup(websocket);
if (to) { if (to) {
wsaddr->u.inet->has_to = true; inet->has_to = true;
wsaddr->u.inet->to = to; inet->to = to;
} }
wsaddr->u.inet->ipv4 = ipv4; inet->ipv4 = ipv4;
wsaddr->u.inet->has_ipv4 = has_ipv4; inet->has_ipv4 = has_ipv4;
wsaddr->u.inet->ipv6 = ipv6; inet->ipv6 = ipv6;
wsaddr->u.inet->has_ipv6 = has_ipv6; inet->has_ipv6 = has_ipv6;
} }
} }
} else { } else {

View File

@ -1003,6 +1003,7 @@ socket_sockaddr_to_address_inet(struct sockaddr_storage *sa,
char host[NI_MAXHOST]; char host[NI_MAXHOST];
char serv[NI_MAXSERV]; char serv[NI_MAXSERV];
SocketAddress *addr; SocketAddress *addr;
InetSocketAddress *inet;
int ret; int ret;
ret = getnameinfo((struct sockaddr *)sa, salen, 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 = g_new0(SocketAddress, 1);
addr->type = SOCKET_ADDRESS_KIND_INET; addr->type = SOCKET_ADDRESS_KIND_INET;
addr->u.inet = g_new0(InetSocketAddress, 1); inet = addr->u.inet = g_new0(InetSocketAddress, 1);
addr->u.inet->host = g_strdup(host); inet->host = g_strdup(host);
addr->u.inet->port = g_strdup(serv); inet->port = g_strdup(serv);
if (sa->ss_family == AF_INET) { if (sa->ss_family == AF_INET) {
addr->u.inet->has_ipv4 = addr->u.inet->ipv4 = true; inet->has_ipv4 = inet->ipv4 = true;
} else { } else {
addr->u.inet->has_ipv6 = addr->u.inet->ipv6 = true; inet->has_ipv6 = inet->ipv6 = true;
} }
return addr; return addr;