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]
This commit is contained in:
Markus Armbruster 2017-04-26 09:36:37 +02:00
parent 4db5c619a2
commit 0785bd7a7c
5 changed files with 22 additions and 27 deletions

View File

@ -198,16 +198,16 @@ static void nbd_parse_filename(const char *filename, QDict *options,
qdict_put_str(options, "server.type", "unix"); qdict_put_str(options, "server.type", "unix");
qdict_put_str(options, "server.path", unixpath); qdict_put_str(options, "server.path", unixpath);
} else { } else {
InetSocketAddress *addr = NULL; InetSocketAddress *addr = g_new(InetSocketAddress, 1);
addr = inet_parse(host_spec, errp); if (inet_parse(addr, host_spec, errp)) {
if (!addr) { goto out_inet;
goto out;
} }
qdict_put_str(options, "server.type", "inet"); qdict_put_str(options, "server.type", "inet");
qdict_put_str(options, "server.host", addr->host); qdict_put_str(options, "server.host", addr->host);
qdict_put_str(options, "server.port", addr->port); qdict_put_str(options, "server.port", addr->port);
out_inet:
qapi_free_InetSocketAddress(addr); qapi_free_InetSocketAddress(addr);
} }

View File

@ -34,7 +34,7 @@ typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque);
int inet_ai_family_from_address(InetSocketAddress *addr, int inet_ai_family_from_address(InetSocketAddress *addr,
Error **errp); Error **errp);
InetSocketAddress *inet_parse(const char *str, Error **errp); int inet_parse(InetSocketAddress *addr, const char *str, Error **errp);
int inet_connect(const char *str, Error **errp); int inet_connect(const char *str, Error **errp);
int inet_connect_saddr(InetSocketAddress *saddr, int inet_connect_saddr(InetSocketAddress *saddr,
NonBlockingConnectHandler *callback, void *opaque, NonBlockingConnectHandler *callback, void *opaque,

View File

@ -2506,8 +2506,8 @@ static void *qemu_rdma_data_init(const char *host_port, Error **errp)
rdma->current_index = -1; rdma->current_index = -1;
rdma->current_chunk = -1; rdma->current_chunk = -1;
addr = inet_parse(host_port, NULL); addr = g_new(InetSocketAddress, 1);
if (addr != NULL) { if (!inet_parse(addr, host_port, NULL)) {
rdma->port = atoi(addr->port); rdma->port = atoi(addr->port);
rdma->host = g_strdup(addr->host); rdma->host = g_strdup(addr->host);
} else { } else {

View File

@ -27,10 +27,11 @@
static SocketAddress *tcp_build_address(const char *host_port, Error **errp) static SocketAddress *tcp_build_address(const char *host_port, Error **errp)
{ {
InetSocketAddress *iaddr = inet_parse(host_port, errp); InetSocketAddress *iaddr = g_new(InetSocketAddress, 1);
SocketAddress *saddr; SocketAddress *saddr;
if (!iaddr) { if (inet_parse(iaddr, host_port, errp)) {
qapi_free_InetSocketAddress(iaddr);
return NULL; return NULL;
} }

View File

@ -578,16 +578,15 @@ err:
} }
/* compatibility wrapper */ /* 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; const char *optstr, *h;
char host[65]; char host[65];
char port[33]; char port[33];
int to; int to;
int pos; int pos;
addr = g_new0(InetSocketAddress, 1); memset(addr, 0, sizeof(*addr));
/* parse address */ /* parse address */
if (str[0] == ':') { if (str[0] == ':') {
@ -595,20 +594,20 @@ InetSocketAddress *inet_parse(const char *str, Error **errp)
host[0] = '\0'; host[0] = '\0';
if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) { if (sscanf(str, ":%32[^,]%n", port, &pos) != 1) {
error_setg(errp, "error parsing port in address '%s'", str); error_setg(errp, "error parsing port in address '%s'", str);
goto fail; return -1;
} }
} else if (str[0] == '[') { } else if (str[0] == '[') {
/* IPv6 addr */ /* IPv6 addr */
if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) { if (sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos) != 2) {
error_setg(errp, "error parsing IPv6 address '%s'", str); error_setg(errp, "error parsing IPv6 address '%s'", str);
goto fail; return -1;
} }
addr->ipv6 = addr->has_ipv6 = true; addr->ipv6 = addr->has_ipv6 = true;
} else { } else {
/* hostname or IPv4 addr */ /* hostname or IPv4 addr */
if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) { if (sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos) != 2) {
error_setg(errp, "error parsing address '%s'", str); error_setg(errp, "error parsing address '%s'", str);
goto fail; return -1;
} }
if (host[strspn(host, "0123456789.")] == '\0') { if (host[strspn(host, "0123456789.")] == '\0') {
addr->ipv4 = addr->has_ipv4 = true; 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 || if (sscanf(h, "%d%n", &to, &pos) != 1 ||
(h[pos] != '\0' && h[pos] != ',')) { (h[pos] != '\0' && h[pos] != ',')) {
error_setg(errp, "error parsing to= argument"); error_setg(errp, "error parsing to= argument");
goto fail; return -1;
} }
addr->has_to = true; addr->has_to = true;
addr->to = to; addr->to = to;
@ -637,11 +636,7 @@ InetSocketAddress *inet_parse(const char *str, Error **errp)
if (strstr(optstr, ",ipv6")) { if (strstr(optstr, ",ipv6")) {
addr->ipv6 = addr->has_ipv6 = true; addr->ipv6 = addr->has_ipv6 = true;
} }
return addr; return 0;
fail:
qapi_free_InetSocketAddress(addr);
return NULL;
} }
@ -656,13 +651,12 @@ fail:
int inet_connect(const char *str, Error **errp) int inet_connect(const char *str, Error **errp)
{ {
int sock = -1; int sock = -1;
InetSocketAddress *addr; InetSocketAddress *addr = g_new(InetSocketAddress, 1);
addr = inet_parse(str, errp); if (!inet_parse(addr, str, errp)) {
if (addr != NULL) {
sock = inet_connect_saddr(addr, NULL, NULL, errp); sock = inet_connect_saddr(addr, NULL, NULL, errp);
qapi_free_InetSocketAddress(addr);
} }
qapi_free_InetSocketAddress(addr);
return sock; return sock;
} }
@ -1066,8 +1060,8 @@ SocketAddress *socket_parse(const char *str, Error **errp)
} }
} else { } else {
addr->type = SOCKET_ADDRESS_KIND_INET; addr->type = SOCKET_ADDRESS_KIND_INET;
addr->u.inet.data = inet_parse(str, errp); addr->u.inet.data = g_new(InetSocketAddress, 1);
if (addr->u.inet.data == NULL) { if (inet_parse(addr->u.inet.data, str, errp)) {
goto fail; goto fail;
} }
} }