aboutsummaryrefslogtreecommitdiff
path: root/util/qemu-sockets.c
diff options
context:
space:
mode:
authorMarkus Armbruster <armbru@redhat.com>2017-04-26 09:36:37 +0200
committerMarkus Armbruster <armbru@redhat.com>2017-05-09 09:14:40 +0200
commit0785bd7a7c58ffaf533f484cb0661671a64ecffd (patch)
treec2edcdd19a9e8075bc1536175bcecf40feac2090 /util/qemu-sockets.c
parent4db5c619a2f03c2ee25105b4f6359075c825aecc (diff)
sockets: Prepare inet_parse() for flattened SocketAddress
I'm going to flatten SocketAddress: rename SocketAddress to SocketAddressLegacy, SocketAddressFlat to SocketAddress, eliminate SocketAddressLegacy except in external interfaces. inet_parse() returns a newly allocated InetSocketAddress. Lift the allocation from inet_parse() into its caller socket_parse() to prepare for flattening SocketAddress. Signed-off-by: Markus Armbruster <armbru@redhat.com> Message-Id: <1493192202-3184-3-git-send-email-armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> [Straightforward rebase]
Diffstat (limited to 'util/qemu-sockets.c')
-rw-r--r--util/qemu-sockets.c30
1 files changed, 12 insertions, 18 deletions
diff --git a/util/qemu-sockets.c b/util/qemu-sockets.c
index 1abe095493..62c93ba9a1 100644
--- a/util/qemu-sockets.c
+++ b/util/qemu-sockets.c
@@ -578,16 +578,15 @@ err:
}
/* compatibility wrapper */
-InetSocketAddress *inet_parse(const char *str, Error **errp)
+int inet_parse(InetSocketAddress *addr, const char *str, Error **errp)
{
- InetSocketAddress *addr;
const char *optstr, *h;
char host[65];
char port[33];
int to;
int pos;
- addr = g_new0(InetSocketAddress, 1);
+ memset(addr, 0, sizeof(*addr));
/* parse address */
if (str[0] == ':') {
@@ -595,20 +594,20 @@ InetSocketAddress *inet_parse(const char *str, Error **errp)
host[0] = '\0';
if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) {
error_setg(errp, "error parsing port in address '%s'", str);
- goto fail;
+ return -1;
}
} else if (str[0] == '[') {
/* IPv6 addr */
if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) {
error_setg(errp, "error parsing IPv6 address '%s'", str);
- goto fail;
+ return -1;
}
addr->ipv6 = addr->has_ipv6 = true;
} else {
/* hostname or IPv4 addr */
if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) {
error_setg(errp, "error parsing address '%s'", str);
- goto fail;
+ return -1;
}
if (host[strspn(host, "0123456789.")] == '\0') {
addr->ipv4 = addr->has_ipv4 = true;
@@ -626,7 +625,7 @@ InetSocketAddress *inet_parse(const char *str, Error **errp)
if (sscanf(h, "%d%n", &to, &pos) != 1 ||
(h[pos] != '\0' && h[pos] != ',')) {
error_setg(errp, "error parsing to= argument");
- goto fail;
+ return -1;
}
addr->has_to = true;
addr->to = to;
@@ -637,11 +636,7 @@ InetSocketAddress *inet_parse(const char *str, Error **errp)
if (strstr(optstr, ",ipv6")) {
addr->ipv6 = addr->has_ipv6 = true;
}
- return addr;
-
-fail:
- qapi_free_InetSocketAddress(addr);
- return NULL;
+ return 0;
}
@@ -656,13 +651,12 @@ fail:
int inet_connect(const char *str, Error **errp)
{
int sock = -1;
- InetSocketAddress *addr;
+ InetSocketAddress *addr = g_new(InetSocketAddress, 1);
- addr = inet_parse(str, errp);
- if (addr != NULL) {
+ if (!inet_parse(addr, str, errp)) {
sock = inet_connect_saddr(addr, NULL, NULL, errp);
- qapi_free_InetSocketAddress(addr);
}
+ qapi_free_InetSocketAddress(addr);
return sock;
}
@@ -1066,8 +1060,8 @@ SocketAddress *socket_parse(const char *str, Error **errp)
}
} else {
addr->type = SOCKET_ADDRESS_KIND_INET;
- addr->u.inet.data = inet_parse(str, errp);
- if (addr->u.inet.data == NULL) {
+ addr->u.inet.data = g_new(InetSocketAddress, 1);
+ if (inet_parse(addr->u.inet.data, str, errp)) {
goto fail;
}
}