Convert qemu-socket to use QAPI exclusively, update MAINTAINERS.

-----BEGIN PGP SIGNATURE-----
 Version: GnuPG v2.0.22 (GNU/Linux)
 
 iQIcBAABAgAGBQJWny3jAAoJEEy22O7T6HE4pV4QANB6PRPt+cud0yzsnOIspel3
 tfYy23LU561tKJeuKa95FQaW2KUdDTJRiMuk65YOFamgbrvYDetqzxJfHgo5RqFp
 75fYC8zu5PLRaD9bxz4U9tn/hnLARx8HkwRkLGOVLczgzatmPAo5nKDVxaF1i/gF
 cGDQLBn0KlYD+8xwlVNkfWABXkodrUlou3bVT8jKo2Lmxy4XQsbLSMFmwUGEPk6p
 ljX4BZltg6IUemsM2Gqm+40sedGBmHVNORWzD7dys5H8O/vN8LlDEyAKahL6tl8P
 7HCz8dV1vKH7TFPPxBdT/aPrky+JE7OZFbIXdeTIrnGQMBJ1QsxfycYnrsiLiLYR
 5h77RJ4Il3ogoRb8NkPb7YwPjJ/JV8LgCn4Basp6mn+pUL8Hv8i9MIuBzSe94wt9
 PKZDjn68Oln8Z2CuP4TqPwyqFkVGLKr9Gp0/nVhjmvO5KfAGGqkR8bDyTpuwh0VE
 fOBODDvo/Z7uA+azilFx6Fnoe2pd4N/nHLM0qj2keYfmILf76WA17wGab4wYF1Ok
 APsjuZisrjfSxTZyKsrEO2HV3qSjGj35SR/wVUuDqWTWtIZp6N8/buuPZ3ZT5QvQ
 aM23v7PLk1F/2S640y3E3jMgk72UBax9jpUvaldqc04HcmJZnvVg6cKsYChMDu5L
 Vl0yYHJ5T2+Zn6HmvcOg
 =CccP
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/kraxel/tags/pull-socket-20160120-1' into staging

Convert qemu-socket to use QAPI exclusively, update MAINTAINERS.

# gpg: Signature made Wed 20 Jan 2016 06:49:07 GMT using RSA key ID D3E87138
# gpg: Good signature from "Gerd Hoffmann (work) <kraxel@redhat.com>"
# gpg:                 aka "Gerd Hoffmann <gerd@kraxel.org>"
# gpg:                 aka "Gerd Hoffmann (private) <kraxel@gmail.com>"

* remotes/kraxel/tags/pull-socket-20160120-1:
  vnc: distiguish between ipv4/ipv6 omitted vs set to off
  sockets: remove use of QemuOpts from socket_dgram
  sockets: remove use of QemuOpts from socket_connect
  sockets: remove use of QemuOpts from socket_listen
  sockets: remove use of QemuOpts from header file
  add MAINTAINERS entry for qemu socket code

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2016-01-21 12:09:41 +00:00
commit a953853eba
4 changed files with 178 additions and 204 deletions

View File

@ -1260,6 +1260,14 @@ F: io/
F: include/io/ F: include/io/
F: tests/test-io-* F: tests/test-io-*
Sockets
M: Daniel P. Berrange <berrange@redhat.com>
M: Gerd Hoffmann <kraxel@redhat.com>
M: Paolo Bonzini <pbonzini@redhat.com>
S: Maintained
F: include/qemu/sockets.h
F: util/qemu-sockets.c
Usermode Emulation Usermode Emulation
------------------ ------------------
Overall Overall

View File

@ -26,12 +26,9 @@ int inet_aton(const char *cp, struct in_addr *ia);
#endif /* !_WIN32 */ #endif /* !_WIN32 */
#include "qemu/option.h"
#include "qapi/error.h" #include "qapi/error.h"
#include "qapi-types.h" #include "qapi-types.h"
extern QemuOptsList socket_optslist;
/* misc helpers */ /* misc helpers */
int qemu_socket(int domain, int type, int protocol); int qemu_socket(int domain, int type, int protocol);
int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen); int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen);
@ -54,23 +51,16 @@ int socket_set_fast_reuse(int fd);
typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque); typedef void NonBlockingConnectHandler(int fd, Error *err, void *opaque);
InetSocketAddress *inet_parse(const char *str, Error **errp); InetSocketAddress *inet_parse(const char *str, Error **errp);
int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp);
int inet_listen(const char *str, char *ostr, int olen, int inet_listen(const char *str, char *ostr, int olen,
int socktype, int port_offset, Error **errp); int socktype, int port_offset, Error **errp);
int inet_connect_opts(QemuOpts *opts, Error **errp,
NonBlockingConnectHandler *callback, void *opaque);
int inet_connect(const char *str, Error **errp); int inet_connect(const char *str, Error **errp);
int inet_nonblocking_connect(const char *str, int inet_nonblocking_connect(const char *str,
NonBlockingConnectHandler *callback, NonBlockingConnectHandler *callback,
void *opaque, Error **errp); void *opaque, Error **errp);
int inet_dgram_opts(QemuOpts *opts, Error **errp);
NetworkAddressFamily inet_netfamily(int family); NetworkAddressFamily inet_netfamily(int family);
int unix_listen_opts(QemuOpts *opts, Error **errp);
int unix_listen(const char *path, char *ostr, int olen, Error **errp); int unix_listen(const char *path, char *ostr, int olen, Error **errp);
int unix_connect_opts(QemuOpts *opts, Error **errp,
NonBlockingConnectHandler *callback, void *opaque);
int unix_connect(const char *path, Error **errp); int unix_connect(const char *path, Error **errp);
int unix_nonblocking_connect(const char *str, int unix_nonblocking_connect(const char *str,
NonBlockingConnectHandler *callback, NonBlockingConnectHandler *callback,

View File

@ -3502,8 +3502,10 @@ void vnc_display_open(const char *id, Error **errp)
const char *websocket = qemu_opt_get(opts, "websocket"); const char *websocket = qemu_opt_get(opts, "websocket");
int to = qemu_opt_get_number(opts, "to", 0); int to = qemu_opt_get_number(opts, "to", 0);
bool has_ipv4 = qemu_opt_get_bool(opts, "ipv4", false); bool has_ipv4 = qemu_opt_get(opts, "ipv4");
bool has_ipv6 = qemu_opt_get_bool(opts, "ipv6", false); bool has_ipv6 = qemu_opt_get(opts, "ipv6");
bool ipv4 = qemu_opt_get_bool(opts, "ipv4", false);
bool ipv6 = qemu_opt_get_bool(opts, "ipv6", false);
saddr = g_new0(SocketAddress, 1); saddr = g_new0(SocketAddress, 1);
if (websocket) { if (websocket) {
@ -3551,8 +3553,10 @@ void vnc_display_open(const char *id, Error **errp)
saddr->u.inet->has_to = true; saddr->u.inet->has_to = true;
saddr->u.inet->to = to + 5900; saddr->u.inet->to = to + 5900;
} }
saddr->u.inet->ipv4 = saddr->u.inet->has_ipv4 = has_ipv4; saddr->u.inet->ipv4 = ipv4;
saddr->u.inet->ipv6 = saddr->u.inet->has_ipv6 = has_ipv6; saddr->u.inet->has_ipv4 = has_ipv4;
saddr->u.inet->ipv6 = ipv6;
saddr->u.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;
@ -3564,8 +3568,10 @@ void vnc_display_open(const char *id, Error **errp)
wsaddr->u.inet->has_to = true; wsaddr->u.inet->has_to = true;
wsaddr->u.inet->to = to; wsaddr->u.inet->to = to;
} }
wsaddr->u.inet->ipv4 = wsaddr->u.inet->has_ipv4 = has_ipv4; wsaddr->u.inet->ipv4 = ipv4;
wsaddr->u.inet->ipv6 = wsaddr->u.inet->has_ipv6 = has_ipv6; wsaddr->u.inet->has_ipv4 = has_ipv4;
wsaddr->u.inet->ipv6 = ipv6;
wsaddr->u.inet->has_ipv6 = has_ipv6;
} }
} }
} else { } else {

View File

@ -36,39 +36,6 @@
# define AI_V4MAPPED 0 # define AI_V4MAPPED 0
#endif #endif
/* used temporarily until all users are converted to QemuOpts */
QemuOptsList socket_optslist = {
.name = "socket",
.head = QTAILQ_HEAD_INITIALIZER(socket_optslist.head),
.desc = {
{
.name = "path",
.type = QEMU_OPT_STRING,
},{
.name = "host",
.type = QEMU_OPT_STRING,
},{
.name = "port",
.type = QEMU_OPT_STRING,
},{
.name = "localaddr",
.type = QEMU_OPT_STRING,
},{
.name = "localport",
.type = QEMU_OPT_STRING,
},{
.name = "to",
.type = QEMU_OPT_NUMBER,
},{
.name = "ipv4",
.type = QEMU_OPT_BOOL,
},{
.name = "ipv6",
.type = QEMU_OPT_BOOL,
},
{ /* end if list */ }
},
};
static int inet_getport(struct addrinfo *e) static int inet_getport(struct addrinfo *e)
{ {
@ -114,36 +81,78 @@ NetworkAddressFamily inet_netfamily(int family)
return NETWORK_ADDRESS_FAMILY_UNKNOWN; return NETWORK_ADDRESS_FAMILY_UNKNOWN;
} }
int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp) /*
* Matrix we're trying to apply
*
* ipv4 ipv6 family
* - - PF_UNSPEC
* - f PF_INET
* - t PF_INET6
* f - PF_INET6
* f f <error>
* f t PF_INET6
* t - PF_INET
* t f PF_INET
* t t PF_INET6
*
* NB, this matrix is only about getting the neccessary results
* from getaddrinfo(). Some of the cases require further work
* after reading results from getaddrinfo in order to fully
* apply the logic the end user wants. eg with the last case
* ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
* guarantee the ipv6=t part of the request - we need more
* checks to provide ipv4=t part of the guarantee. This is
* outside scope of this method and not currently handled by
* callers at all.
*/
static int inet_ai_family_from_address(InetSocketAddress *addr,
Error **errp)
{
if (addr->has_ipv6 && addr->has_ipv4 &&
!addr->ipv6 && !addr->ipv4) {
error_setg(errp, "Cannot disable IPv4 and IPv6 at same time");
return PF_UNSPEC;
}
if ((addr->has_ipv6 && addr->ipv6) || (addr->has_ipv4 && !addr->ipv4)) {
return PF_INET6;
}
if ((addr->has_ipv4 && addr->ipv4) || (addr->has_ipv6 && !addr->ipv6)) {
return PF_INET;
}
return PF_UNSPEC;
}
static int inet_listen_saddr(InetSocketAddress *saddr,
int port_offset,
bool update_addr,
Error **errp)
{ {
struct addrinfo ai,*res,*e; struct addrinfo ai,*res,*e;
const char *addr;
char port[33]; char port[33];
char uaddr[INET6_ADDRSTRLEN+1]; char uaddr[INET6_ADDRSTRLEN+1];
char uport[33]; char uport[33];
int slisten, rc, to, port_min, port_max, p; int slisten, rc, port_min, port_max, p;
Error *err = NULL;
memset(&ai,0, sizeof(ai)); memset(&ai,0, sizeof(ai));
ai.ai_flags = AI_PASSIVE; ai.ai_flags = AI_PASSIVE;
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;
if ((qemu_opt_get(opts, "host") == NULL)) { if (err) {
error_propagate(errp, err);
return -1;
}
if (saddr->host == NULL) {
error_setg(errp, "host not specified"); error_setg(errp, "host not specified");
return -1; return -1;
} }
if (qemu_opt_get(opts, "port") != NULL) { if (saddr->port != NULL) {
pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); pstrcpy(port, sizeof(port), saddr->port);
} else { } else {
port[0] = '\0'; port[0] = '\0';
} }
addr = qemu_opt_get(opts, "host");
to = qemu_opt_get_number(opts, "to", 0);
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 */
if (port_offset) { if (port_offset) {
@ -163,11 +172,11 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
} }
snprintf(port, sizeof(port), "%d", (int)baseport + port_offset); snprintf(port, sizeof(port), "%d", (int)baseport + port_offset);
} }
rc = getaddrinfo(strlen(addr) ? addr : NULL, rc = getaddrinfo(strlen(saddr->host) ? saddr->host : NULL,
strlen(port) ? port : NULL, &ai, &res); strlen(port) ? port : NULL, &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, port, gai_strerror(rc));
return -1; return -1;
} }
@ -195,7 +204,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
#endif #endif
port_min = inet_getport(e); port_min = inet_getport(e);
port_max = to ? to + port_offset : port_min; port_max = saddr->has_to ? saddr->to + port_offset : port_min;
for (p = port_min; p <= port_max; p++) { for (p = port_min; p <= port_max; p++) {
inet_setport(e, p); inet_setport(e, p);
if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) { if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
@ -219,13 +228,15 @@ listen:
freeaddrinfo(res); freeaddrinfo(res);
return -1; return -1;
} }
qemu_opt_set(opts, "host", uaddr, &error_abort); if (update_addr) {
qemu_opt_set_number(opts, "port", inet_getport(e) - port_offset, g_free(saddr->host);
&error_abort); saddr->host = g_strdup(uaddr);
qemu_opt_set_bool(opts, "ipv6", e->ai_family == PF_INET6, g_free(saddr->port);
&error_abort); saddr->port = g_strdup_printf("%d",
qemu_opt_set_bool(opts, "ipv4", e->ai_family != PF_INET6, inet_getport(e) - port_offset);
&error_abort); saddr->has_ipv6 = saddr->ipv6 = e->ai_family == PF_INET6;
saddr->has_ipv4 = saddr->ipv4 = e->ai_family != PF_INET6;
}
freeaddrinfo(res); freeaddrinfo(res);
return slisten; return slisten;
} }
@ -340,38 +351,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;
@ -380,8 +387,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
@ -392,8 +398,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.
*/ */
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;
@ -401,7 +407,7 @@ 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;
} }
@ -440,21 +446,29 @@ int inet_connect_opts(QemuOpts *opts, Error **errp,
return sock; return sock;
} }
int inet_dgram_opts(QemuOpts *opts, Error **errp) static int inet_dgram_saddr(InetSocketAddress *sraddr,
InetSocketAddress *sladdr,
Error **errp)
{ {
struct addrinfo ai, *peer = NULL, *local = NULL; struct addrinfo ai, *peer = NULL, *local = NULL;
const char *addr; const char *addr;
const char *port; const char *port;
int sock = -1, rc; int sock = -1, rc;
Error *err = NULL;
/* lookup peer addr */ /* lookup peer addr */
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(sraddr, &err);
ai.ai_socktype = SOCK_DGRAM; ai.ai_socktype = SOCK_DGRAM;
addr = qemu_opt_get(opts, "host"); if (err) {
port = qemu_opt_get(opts, "port"); error_propagate(errp, err);
return -1;
}
addr = sraddr->host;
port = sraddr->port;
if (addr == NULL || strlen(addr) == 0) { if (addr == NULL || strlen(addr) == 0) {
addr = "localhost"; addr = "localhost";
} }
@ -463,11 +477,6 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
return -1; return -1;
} }
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;
if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) { if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
gai_strerror(rc)); gai_strerror(rc));
@ -480,13 +489,19 @@ int inet_dgram_opts(QemuOpts *opts, Error **errp)
ai.ai_family = peer->ai_family; ai.ai_family = peer->ai_family;
ai.ai_socktype = SOCK_DGRAM; ai.ai_socktype = SOCK_DGRAM;
addr = qemu_opt_get(opts, "localaddr"); if (sladdr) {
port = qemu_opt_get(opts, "localport"); addr = sladdr->host;
if (addr == NULL || strlen(addr) == 0) { port = sladdr->port;
if (addr == NULL || strlen(addr) == 0) {
addr = NULL;
}
if (!port || strlen(port) == 0) {
port = "0";
}
} else {
addr = NULL; addr = NULL;
}
if (!port || strlen(port) == 0)
port = "0"; port = "0";
}
if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) { if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
error_setg(errp, "address resolution failed for %s:%s: %s", addr, port, error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
@ -595,54 +610,31 @@ fail:
return NULL; return NULL;
} }
static void inet_addr_to_opts(QemuOpts *opts, const InetSocketAddress *addr)
{
bool ipv4 = addr->has_ipv4 && addr->ipv4;
bool ipv6 = addr->has_ipv6 && addr->ipv6;
if (ipv4 || ipv6) {
qemu_opt_set_bool(opts, "ipv4", ipv4, &error_abort);
qemu_opt_set_bool(opts, "ipv6", ipv6, &error_abort);
} else if (addr->has_ipv4 || addr->has_ipv6) {
qemu_opt_set_bool(opts, "ipv4", !addr->has_ipv4, &error_abort);
qemu_opt_set_bool(opts, "ipv6", !addr->has_ipv6, &error_abort);
}
if (addr->has_to) {
qemu_opt_set_number(opts, "to", addr->to, &error_abort);
}
qemu_opt_set(opts, "host", addr->host, &error_abort);
qemu_opt_set(opts, "port", addr->port, &error_abort);
}
int inet_listen(const char *str, char *ostr, int olen, int inet_listen(const char *str, char *ostr, int olen,
int socktype, int port_offset, Error **errp) int socktype, int port_offset, Error **errp)
{ {
QemuOpts *opts;
char *optstr; char *optstr;
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_listen_saddr(addr, port_offset, true, errp);
inet_addr_to_opts(opts, addr);
qapi_free_InetSocketAddress(addr);
sock = inet_listen_opts(opts, port_offset, errp);
if (sock != -1 && ostr) { if (sock != -1 && ostr) {
optstr = strchr(str, ','); optstr = strchr(str, ',');
if (qemu_opt_get_bool(opts, "ipv6", 0)) { if (addr->ipv6) {
snprintf(ostr, olen, "[%s]:%s%s", snprintf(ostr, olen, "[%s]:%s%s",
qemu_opt_get(opts, "host"), addr->host,
qemu_opt_get(opts, "port"), addr->port,
optstr ? optstr : ""); optstr ? optstr : "");
} else { } else {
snprintf(ostr, olen, "%s:%s%s", snprintf(ostr, olen, "%s:%s%s",
qemu_opt_get(opts, "host"), addr->host,
qemu_opt_get(opts, "port"), addr->port,
optstr ? optstr : ""); optstr ? optstr : "");
} }
} }
qemu_opts_del(opts); qapi_free_InetSocketAddress(addr);
} }
return sock; return sock;
} }
@ -657,17 +649,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;
} }
@ -689,7 +677,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;
@ -697,21 +684,19 @@ 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;
} }
#ifndef _WIN32 #ifndef _WIN32
int unix_listen_opts(QemuOpts *opts, Error **errp) static int unix_listen_saddr(UnixSocketAddress *saddr,
bool update_addr,
Error **errp)
{ {
struct sockaddr_un un; struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
int sock, fd; int sock, fd;
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0); sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
@ -722,8 +707,8 @@ int unix_listen_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;
if (path && strlen(path)) { if (saddr->path && strlen(saddr->path)) {
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path); snprintf(un.sun_path, sizeof(un.sun_path), "%s", saddr->path);
} else { } else {
const char *tmpdir = getenv("TMPDIR"); const char *tmpdir = getenv("TMPDIR");
tmpdir = tmpdir ? tmpdir : "/tmp"; tmpdir = tmpdir ? tmpdir : "/tmp";
@ -748,7 +733,10 @@ int unix_listen_opts(QemuOpts *opts, Error **errp)
goto err; goto err;
} }
close(fd); close(fd);
qemu_opt_set(opts, "path", un.sun_path, &error_abort); if (update_addr) {
g_free(saddr->path);
saddr->path = g_strdup(un.sun_path);
}
} }
if (unlink(un.sun_path) < 0 && errno != ENOENT) { if (unlink(un.sun_path) < 0 && errno != ENOENT) {
@ -772,15 +760,14 @@ err:
return -1; return -1;
} }
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;
} }
@ -799,7 +786,7 @@ 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 {
@ -832,15 +819,17 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,
#else #else
int unix_listen_opts(QemuOpts *opts, Error **errp) static int unix_listen_saddr(UnixSocketAddress *saddr,
bool update_addr,
Error **errp)
{ {
error_setg(errp, "unix sockets are not available on windows"); error_setg(errp, "unix sockets are not available on windows");
errno = ENOTSUP; errno = ENOTSUP;
return -1; return -1;
} }
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;
@ -851,11 +840,11 @@ int unix_connect_opts(QemuOpts *opts, Error **errp,
/* compatibility wrapper */ /* compatibility wrapper */
int unix_listen(const char *str, char *ostr, int olen, Error **errp) int unix_listen(const char *str, char *ostr, int olen, Error **errp)
{ {
QemuOpts *opts;
char *path, *optstr; char *path, *optstr;
int sock, len; int sock, len;
UnixSocketAddress *saddr;
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort); saddr = g_new0(UnixSocketAddress, 1);
optstr = strchr(str, ','); optstr = strchr(str, ',');
if (optstr) { if (optstr) {
@ -863,30 +852,29 @@ int unix_listen(const char *str, char *ostr, int olen, Error **errp)
if (len) { if (len) {
path = g_malloc(len+1); path = g_malloc(len+1);
snprintf(path, len+1, "%.*s", len, str); snprintf(path, len+1, "%.*s", len, str);
qemu_opt_set(opts, "path", path, &error_abort); saddr->path = path;
g_free(path);
} }
} else { } else {
qemu_opt_set(opts, "path", str, &error_abort); saddr->path = g_strdup(str);
} }
sock = unix_listen_opts(opts, errp); sock = unix_listen_saddr(saddr, true, errp);
if (sock != -1 && ostr) if (sock != -1 && ostr)
snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : ""); snprintf(ostr, olen, "%s%s", saddr->path, optstr ? optstr : "");
qemu_opts_del(opts); qapi_free_UnixSocketAddress(saddr);
return sock; return sock;
} }
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;
} }
@ -895,15 +883,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;
} }
@ -947,19 +935,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:
@ -973,25 +957,20 @@ int socket_connect(SocketAddress *addr, Error **errp,
default: default:
abort(); abort();
} }
qemu_opts_del(opts);
return fd; return fd;
} }
int socket_listen(SocketAddress *addr, Error **errp) int socket_listen(SocketAddress *addr, Error **errp)
{ {
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_listen_saddr(addr->u.inet, 0, false, errp);
fd = inet_listen_opts(opts, 0, errp);
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_listen_saddr(addr->u.q_unix, false, errp);
fd = unix_listen_opts(opts, errp);
break; break;
case SOCKET_ADDRESS_KIND_FD: case SOCKET_ADDRESS_KIND_FD:
@ -1001,31 +980,22 @@ int socket_listen(SocketAddress *addr, Error **errp)
default: default:
abort(); abort();
} }
qemu_opts_del(opts);
return fd; return fd;
} }
int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp) int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
{ {
QemuOpts *opts;
int fd; int fd;
opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
switch (remote->type) { switch (remote->type) {
case SOCKET_ADDRESS_KIND_INET: case SOCKET_ADDRESS_KIND_INET:
inet_addr_to_opts(opts, remote->u.inet); fd = inet_dgram_saddr(remote->u.inet, local ? local->u.inet : NULL, errp);
if (local) {
qemu_opt_set(opts, "localaddr", local->u.inet->host, &error_abort);
qemu_opt_set(opts, "localport", local->u.inet->port, &error_abort);
}
fd = inet_dgram_opts(opts, errp);
break; break;
default: default:
error_setg(errp, "socket type unsupported for datagram"); error_setg(errp, "socket type unsupported for datagram");
fd = -1; fd = -1;
} }
qemu_opts_del(opts);
return fd; return fd;
} }