sockets: remove use of QemuOpts from socket_connect

The socket_connect method accepts a QAPI SocketAddress object
which it then turns into QemuOpts before calling the
inet_connect_opts/unix_connect_opts helper methods. By
converting the latter to use QAPI SocketAddress directly,
the QemuOpts conversion step can be eliminated

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-id: 1452518225-11751-4-git-send-email-berrange@redhat.com
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
This commit is contained in:
Daniel P. Berrange 2016-01-11 13:17:03 +00:00 committed by Gerd Hoffmann
parent 1856835d6d
commit 2942e420e0

View File

@ -384,38 +384,34 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
return sock; return sock;
} }
static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp) static struct addrinfo *inet_parse_connect_saddr(InetSocketAddress *saddr,
Error **errp)
{ {
struct addrinfo ai, *res; struct addrinfo ai, *res;
int rc; int rc;
const char *addr; Error *err = NULL;
const char *port;
memset(&ai, 0, sizeof(ai)); memset(&ai, 0, sizeof(ai));
ai.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ADDRCONFIG; ai.ai_flags = AI_CANONNAME | AI_V4MAPPED | AI_ADDRCONFIG;
ai.ai_family = PF_UNSPEC; ai.ai_family = inet_ai_family_from_address(saddr, &err);
ai.ai_socktype = SOCK_STREAM; ai.ai_socktype = SOCK_STREAM;
addr = qemu_opt_get(opts, "host"); if (err) {
port = qemu_opt_get(opts, "port"); error_propagate(errp, err);
if (addr == NULL || port == NULL) { return NULL;
}
if (saddr->host == NULL || saddr->port == NULL) {
error_setg(errp, "host and/or port not specified"); error_setg(errp, "host and/or port not specified");
return NULL; return NULL;
} }
if (qemu_opt_get_bool(opts, "ipv4", 0)) {
ai.ai_family = PF_INET;
}
if (qemu_opt_get_bool(opts, "ipv6", 0)) {
ai.ai_family = PF_INET6;
}
/* lookup */ /* lookup */
rc = getaddrinfo(addr, port, &ai, &res); rc = getaddrinfo(saddr->host, saddr->port, &ai, &res);
if (rc != 0) { if (rc != 0) {
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, error_setg(errp, "address resolution failed for %s:%s: %s",
gai_strerror(rc)); saddr->host, saddr->port, gai_strerror(rc));
return NULL; return NULL;
} }
return res; return res;
@ -424,8 +420,7 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
/** /**
* Create a socket and connect it to an address. * Create a socket and connect it to an address.
* *
* @opts: QEMU options, recognized parameters strings "host" and "port", * @saddr: Inet socket address specification
* bools "ipv4" and "ipv6".
* @errp: set on error * @errp: set on error
* @callback: callback function for non-blocking connect * @callback: callback function for non-blocking connect
* @opaque: opaque for callback function * @opaque: opaque for callback function
@ -436,8 +431,8 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
* function succeeds, callback will be called when the connection * function succeeds, callback will be called when the connection
* completes, with the file descriptor on success, or -1 on error. * completes, with the file descriptor on success, or -1 on error.
*/ */
static int inet_connect_opts(QemuOpts *opts, Error **errp, static int inet_connect_saddr(InetSocketAddress *saddr, Error **errp,
NonBlockingConnectHandler *callback, void *opaque) NonBlockingConnectHandler *callback, void *opaque)
{ {
Error *local_err = NULL; Error *local_err = NULL;
struct addrinfo *res, *e; struct addrinfo *res, *e;
@ -445,7 +440,7 @@ static int inet_connect_opts(QemuOpts *opts, Error **errp,
bool in_progress; bool in_progress;
ConnectState *connect_state = NULL; ConnectState *connect_state = NULL;
res = inet_parse_connect_opts(opts, errp); res = inet_parse_connect_saddr(saddr, errp);
if (!res) { if (!res) {
return -1; return -1;
} }
@ -697,17 +692,13 @@ int inet_listen(const char *str, char *ostr, int olen,
**/ **/
int inet_connect(const char *str, Error **errp) int inet_connect(const char *str, Error **errp)
{ {
QemuOpts *opts;
int sock = -1; int sock = -1;
InetSocketAddress *addr; InetSocketAddress *addr;
addr = inet_parse(str, errp); addr = inet_parse(str, errp);
if (addr != NULL) { if (addr != NULL) {
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); sock = inet_connect_saddr(addr, errp, NULL, NULL);
inet_addr_to_opts(opts, addr);
qapi_free_InetSocketAddress(addr); qapi_free_InetSocketAddress(addr);
sock = inet_connect_opts(opts, errp, NULL, NULL);
qemu_opts_del(opts);
} }
return sock; return sock;
} }
@ -729,7 +720,6 @@ int inet_nonblocking_connect(const char *str,
NonBlockingConnectHandler *callback, NonBlockingConnectHandler *callback,
void *opaque, Error **errp) void *opaque, Error **errp)
{ {
QemuOpts *opts;
int sock = -1; int sock = -1;
InetSocketAddress *addr; InetSocketAddress *addr;
@ -737,11 +727,8 @@ int inet_nonblocking_connect(const char *str,
addr = inet_parse(str, errp); addr = inet_parse(str, errp);
if (addr != NULL) { if (addr != NULL) {
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); sock = inet_connect_saddr(addr, errp, callback, opaque);
inet_addr_to_opts(opts, addr);
qapi_free_InetSocketAddress(addr); qapi_free_InetSocketAddress(addr);
sock = inet_connect_opts(opts, errp, callback, opaque);
qemu_opts_del(opts);
} }
return sock; return sock;
} }
@ -816,15 +803,14 @@ err:
return -1; return -1;
} }
static int unix_connect_opts(QemuOpts *opts, Error **errp, static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp,
NonBlockingConnectHandler *callback, void *opaque) NonBlockingConnectHandler *callback, void *opaque)
{ {
struct sockaddr_un un; struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
ConnectState *connect_state = NULL; ConnectState *connect_state = NULL;
int sock, rc; int sock, rc;
if (path == NULL) { if (saddr->path == NULL) {
error_setg(errp, "unix connect: no path specified"); error_setg(errp, "unix connect: no path specified");
return -1; return -1;
} }
@ -843,7 +829,7 @@ static int unix_connect_opts(QemuOpts *opts, Error **errp,
memset(&un, 0, sizeof(un)); memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX; un.sun_family = AF_UNIX;
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); snprintf(un.sun_path, sizeof(un.sun_path), "%s", saddr->path);
/* connect to peer */ /* connect to peer */
do { do {
@ -885,8 +871,8 @@ static int unix_listen_saddr(UnixSocketAddress *saddr,
return -1; return -1;
} }
static int unix_connect_opts(QemuOpts *opts, Error **errp, static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp,
NonBlockingConnectHandler *callback, void *opaque) NonBlockingConnectHandler *callback, void *opaque)
{ {
error_setg(errp, "unix sockets are not available on windows"); error_setg(errp, "unix sockets are not available on windows");
errno = ENOTSUP; errno = ENOTSUP;
@ -925,13 +911,13 @@ int unix_listen(const char *str, char *ostr, int olen, Error **errp)
int unix_connect(const char *path, Error **errp) int unix_connect(const char *path, Error **errp)
{ {
QemuOpts *opts; UnixSocketAddress *saddr;
int sock; int sock;
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); saddr = g_new0(UnixSocketAddress, 1);
qemu_opt_set(opts, "path", path, &error_abort); saddr->path = g_strdup(path);
sock = unix_connect_opts(opts, errp, NULL, NULL); sock = unix_connect_saddr(saddr, errp, NULL, NULL);
qemu_opts_del(opts); qapi_free_UnixSocketAddress(saddr);
return sock; return sock;
} }
@ -940,15 +926,15 @@ int unix_nonblocking_connect(const char *path,
NonBlockingConnectHandler *callback, NonBlockingConnectHandler *callback,
void *opaque, Error **errp) void *opaque, Error **errp)
{ {
QemuOpts *opts; UnixSocketAddress *saddr;
int sock = -1; int sock = -1;
g_assert(callback != NULL); g_assert(callback != NULL);
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); saddr = g_new0(UnixSocketAddress, 1);
qemu_opt_set(opts, "path", path, &error_abort); saddr->path = g_strdup(path);
sock = unix_connect_opts(opts, errp, callback, opaque); sock = unix_connect_saddr(saddr, errp, callback, opaque);
qemu_opts_del(opts); qapi_free_UnixSocketAddress(saddr);
return sock; return sock;
} }
@ -992,19 +978,15 @@ fail:
int socket_connect(SocketAddress *addr, Error **errp, int socket_connect(SocketAddress *addr, Error **errp,
NonBlockingConnectHandler *callback, void *opaque) NonBlockingConnectHandler *callback, void *opaque)
{ {
QemuOpts *opts;
int fd; int fd;
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
switch (addr->type) { switch (addr->type) {
case SOCKET_ADDRESS_KIND_INET: case SOCKET_ADDRESS_KIND_INET:
inet_addr_to_opts(opts, addr->u.inet); fd = inet_connect_saddr(addr->u.inet, errp, callback, opaque);
fd = inet_connect_opts(opts, errp, callback, opaque);
break; break;
case SOCKET_ADDRESS_KIND_UNIX: case SOCKET_ADDRESS_KIND_UNIX:
qemu_opt_set(opts, "path", addr->u.q_unix->path, &error_abort); fd = unix_connect_saddr(addr->u.q_unix, errp, callback, opaque);
fd = unix_connect_opts(opts, errp, callback, opaque);
break; break;
case SOCKET_ADDRESS_KIND_FD: case SOCKET_ADDRESS_KIND_FD:
@ -1018,7 +1000,6 @@ int socket_connect(SocketAddress *addr, Error **errp,
default: default:
abort(); abort();
} }
qemu_opts_del(opts);
return fd; return fd;
} }