aboutsummaryrefslogtreecommitdiff
path: root/qemu-sockets.c
diff options
context:
space:
mode:
Diffstat (limited to 'qemu-sockets.c')
-rw-r--r--qemu-sockets.c424
1 files changed, 282 insertions, 142 deletions
diff --git a/qemu-sockets.c b/qemu-sockets.c
index 2b1ed2f0e9..cfed9c5a5b 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <unistd.h>
+#include "monitor.h"
#include "qemu_socket.h"
#include "qemu-common.h" /* for qemu_isdigit */
#include "main-loop.h"
@@ -120,8 +121,7 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
if ((qemu_opt_get(opts, "host") == NULL) ||
(qemu_opt_get(opts, "port") == NULL)) {
- fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__);
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ error_setg(errp, "host and/or port not specified");
return -1;
}
pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
@@ -138,9 +138,8 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
if (rc != 0) {
- fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
- gai_strerror(rc));
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
+ gai_strerror(rc));
return -1;
}
@@ -151,10 +150,8 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
NI_NUMERICHOST | NI_NUMERICSERV);
slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
if (slisten < 0) {
- fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
- inet_strfamily(e->ai_family), strerror(errno));
if (!e->ai_next) {
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
}
continue;
}
@@ -176,24 +173,19 @@ int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
goto listen;
}
if (p == port_max) {
- fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__,
- inet_strfamily(e->ai_family), uaddr, inet_getport(e),
- strerror(errno));
if (!e->ai_next) {
- error_set(errp, QERR_SOCKET_BIND_FAILED);
+ error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
}
}
}
closesocket(slisten);
}
- fprintf(stderr, "%s: FAILED\n", __FUNCTION__);
freeaddrinfo(res);
return -1;
listen:
if (listen(slisten,1) != 0) {
- error_set(errp, QERR_SOCKET_LISTEN_FAILED);
- perror("listen");
+ error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
closesocket(slisten);
freeaddrinfo(res);
return -1;
@@ -225,7 +217,7 @@ typedef struct ConnectState {
} ConnectState;
static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
- ConnectState *connect_state);
+ ConnectState *connect_state, Error **errp);
static void wait_for_connect(void *opaque)
{
@@ -252,16 +244,19 @@ static void wait_for_connect(void *opaque)
}
/* try to connect to the next address on the list */
- while (s->current_addr->ai_next != NULL && s->fd < 0) {
- s->current_addr = s->current_addr->ai_next;
- s->fd = inet_connect_addr(s->current_addr, &in_progress, s);
- /* connect in progress */
- if (in_progress) {
- return;
+ if (s->current_addr) {
+ while (s->current_addr->ai_next != NULL && s->fd < 0) {
+ s->current_addr = s->current_addr->ai_next;
+ s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
+ /* connect in progress */
+ if (in_progress) {
+ return;
+ }
}
+
+ freeaddrinfo(s->addr_list);
}
- freeaddrinfo(s->addr_list);
if (s->callback) {
s->callback(s->fd, s->opaque);
}
@@ -269,7 +264,7 @@ static void wait_for_connect(void *opaque)
}
static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
- ConnectState *connect_state)
+ ConnectState *connect_state, Error **errp)
{
int sock, rc;
@@ -277,8 +272,7 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (sock < 0) {
- fprintf(stderr, "%s: socket(%s): %s\n", __func__,
- inet_strfamily(addr->ai_family), strerror(errno));
+ error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
return -1;
}
qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
@@ -299,6 +293,7 @@ static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
connect_state);
*in_progress = true;
} else if (rc < 0) {
+ error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
closesocket(sock);
return -1;
}
@@ -321,9 +316,7 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
addr = qemu_opt_get(opts, "host");
port = qemu_opt_get(opts, "port");
if (addr == NULL || port == NULL) {
- fprintf(stderr,
- "inet_parse_connect_opts: host and/or port not specified\n");
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ error_setg(errp, "host and/or port not specified");
return NULL;
}
@@ -337,9 +330,8 @@ static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
/* lookup */
rc = getaddrinfo(addr, port, &ai, &res);
if (rc != 0) {
- fprintf(stderr, "getaddrinfo(%s,%s): %s\n", addr, port,
- gai_strerror(rc));
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
+ gai_strerror(rc));
return NULL;
}
return res;
@@ -384,7 +376,7 @@ int inet_connect_opts(QemuOpts *opts, Error **errp,
if (connect_state != NULL) {
connect_state->current_addr = e;
}
- sock = inet_connect_addr(e, &in_progress, connect_state);
+ sock = inet_connect_addr(e, &in_progress, connect_state, errp);
if (in_progress) {
return sock;
} else if (sock >= 0) {
@@ -395,21 +387,16 @@ int inet_connect_opts(QemuOpts *opts, Error **errp,
break;
}
}
- if (sock < 0) {
- error_set(errp, QERR_SOCKET_CONNECT_FAILED);
- }
g_free(connect_state);
freeaddrinfo(res);
return sock;
}
-int inet_dgram_opts(QemuOpts *opts)
+int inet_dgram_opts(QemuOpts *opts, Error **errp)
{
struct addrinfo ai, *peer = NULL, *local = NULL;
const char *addr;
const char *port;
- char uaddr[INET6_ADDRSTRLEN+1];
- char uport[33];
int sock = -1, rc;
/* lookup peer addr */
@@ -424,7 +411,7 @@ int inet_dgram_opts(QemuOpts *opts)
addr = "localhost";
}
if (port == NULL || strlen(port) == 0) {
- fprintf(stderr, "inet_dgram: port not specified\n");
+ error_setg(errp, "remote port not specified");
return -1;
}
@@ -434,8 +421,8 @@ int inet_dgram_opts(QemuOpts *opts)
ai.ai_family = PF_INET6;
if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
- fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
- gai_strerror(rc));
+ error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
+ gai_strerror(rc));
return -1;
}
@@ -454,44 +441,28 @@ int inet_dgram_opts(QemuOpts *opts)
port = "0";
if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
- fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
- gai_strerror(rc));
+ error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
+ gai_strerror(rc));
goto err;
}
/* create socket */
sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
if (sock < 0) {
- fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__,
- inet_strfamily(peer->ai_family), strerror(errno));
+ error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
goto err;
}
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
/* bind socket */
- if (getnameinfo((struct sockaddr*)local->ai_addr,local->ai_addrlen,
- uaddr,INET6_ADDRSTRLEN,uport,32,
- NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
- fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
- goto err;
- }
if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
- fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__,
- inet_strfamily(local->ai_family), uaddr, inet_getport(local));
+ error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
goto err;
}
/* connect to peer */
- if (getnameinfo((struct sockaddr*)peer->ai_addr, peer->ai_addrlen,
- uaddr, INET6_ADDRSTRLEN, uport, 32,
- NI_NUMERICHOST | NI_NUMERICSERV) != 0) {
- fprintf(stderr, "%s: getnameinfo: oops\n", __FUNCTION__);
- goto err;
- }
if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
- fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
- inet_strfamily(peer->ai_family),
- peer->ai_canonname, uaddr, uport, strerror(errno));
+ error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
goto err;
}
@@ -510,59 +481,91 @@ err:
}
/* compatibility wrapper */
-static int inet_parse(QemuOpts *opts, const char *str)
+static InetSocketAddress *inet_parse(const char *str, Error **errp)
{
+ InetSocketAddress *addr;
const char *optstr, *h;
- char addr[64];
+ char host[64];
char port[33];
+ int to;
int pos;
+ addr = g_new0(InetSocketAddress, 1);
+
/* parse address */
if (str[0] == ':') {
/* no host given */
- addr[0] = '\0';
- if (1 != sscanf(str,":%32[^,]%n",port,&pos)) {
- fprintf(stderr, "%s: portonly parse error (%s)\n",
- __FUNCTION__, str);
- return -1;
+ host[0] = '\0';
+ if (1 != sscanf(str, ":%32[^,]%n", port, &pos)) {
+ error_setg(errp, "error parsing port in address '%s'", str);
+ goto fail;
}
} else if (str[0] == '[') {
/* IPv6 addr */
- if (2 != sscanf(str,"[%64[^]]]:%32[^,]%n",addr,port,&pos)) {
- fprintf(stderr, "%s: ipv6 parse error (%s)\n",
- __FUNCTION__, str);
- return -1;
+ if (2 != sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos)) {
+ error_setg(errp, "error parsing IPv6 address '%s'", str);
+ goto fail;
}
- qemu_opt_set(opts, "ipv6", "on");
+ addr->ipv6 = addr->has_ipv6 = true;
} else if (qemu_isdigit(str[0])) {
/* IPv4 addr */
- if (2 != sscanf(str,"%64[0-9.]:%32[^,]%n",addr,port,&pos)) {
- fprintf(stderr, "%s: ipv4 parse error (%s)\n",
- __FUNCTION__, str);
- return -1;
+ if (2 != sscanf(str, "%64[0-9.]:%32[^,]%n", host, port, &pos)) {
+ error_setg(errp, "error parsing IPv4 address '%s'", str);
+ goto fail;
}
- qemu_opt_set(opts, "ipv4", "on");
+ addr->ipv4 = addr->has_ipv4 = true;
} else {
/* hostname */
- if (2 != sscanf(str,"%64[^:]:%32[^,]%n",addr,port,&pos)) {
- fprintf(stderr, "%s: hostname parse error (%s)\n",
- __FUNCTION__, str);
- return -1;
+ if (2 != sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos)) {
+ error_setg(errp, "error parsing address '%s'", str);
+ goto fail;
}
}
- qemu_opt_set(opts, "host", addr);
- qemu_opt_set(opts, "port", port);
+
+ addr->host = g_strdup(host);
+ addr->port = g_strdup(port);
/* parse options */
optstr = str + pos;
h = strstr(optstr, ",to=");
- if (h)
- qemu_opt_set(opts, "to", h+4);
- if (strstr(optstr, ",ipv4"))
- qemu_opt_set(opts, "ipv4", "on");
- if (strstr(optstr, ",ipv6"))
- qemu_opt_set(opts, "ipv6", "on");
- return 0;
+ if (h) {
+ if (1 != sscanf(str, "%d%n", &to, &pos) ||
+ (str[pos] != '\0' && str[pos] != ',')) {
+ error_setg(errp, "error parsing to= argument");
+ goto fail;
+ }
+ addr->has_to = true;
+ addr->to = to;
+ }
+ if (strstr(optstr, ",ipv4")) {
+ addr->ipv4 = addr->has_ipv4 = true;
+ }
+ if (strstr(optstr, ",ipv6")) {
+ addr->ipv6 = addr->has_ipv6 = true;
+ }
+ return addr;
+
+fail:
+ qapi_free_InetSocketAddress(addr);
+ return NULL;
+}
+
+static void inet_addr_to_opts(QemuOpts *opts, InetSocketAddress *addr)
+{
+ bool ipv4 = addr->ipv4 || !addr->has_ipv4;
+ bool ipv6 = addr->ipv6 || !addr->has_ipv6;
+
+ if (!ipv4 || !ipv6) {
+ qemu_opt_set_bool(opts, "ipv4", ipv4);
+ qemu_opt_set_bool(opts, "ipv6", ipv6);
+ }
+ if (addr->has_to) {
+ char to[20];
+ snprintf(to, sizeof(to), "%d", addr->to);
+ qemu_opt_set(opts, "to", to);
+ }
+ qemu_opt_set(opts, "host", addr->host);
+ qemu_opt_set(opts, "port", addr->port);
}
int inet_listen(const char *str, char *ostr, int olen,
@@ -571,9 +574,13 @@ int inet_listen(const char *str, char *ostr, int olen,
QemuOpts *opts;
char *optstr;
int sock = -1;
+ InetSocketAddress *addr;
- opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
- if (inet_parse(opts, str) == 0) {
+ addr = inet_parse(str, errp);
+ if (addr != NULL) {
+ opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+ inet_addr_to_opts(opts, addr);
+ qapi_free_InetSocketAddress(addr);
sock = inet_listen_opts(opts, port_offset, errp);
if (sock != -1 && ostr) {
optstr = strchr(str, ',');
@@ -589,10 +596,8 @@ int inet_listen(const char *str, char *ostr, int olen,
optstr ? optstr : "");
}
}
- } else {
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ qemu_opts_del(opts);
}
- qemu_opts_del(opts);
return sock;
}
@@ -608,14 +613,16 @@ int inet_connect(const char *str, Error **errp)
{
QemuOpts *opts;
int sock = -1;
+ InetSocketAddress *addr;
- opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
- if (inet_parse(opts, str) == 0) {
+ addr = inet_parse(str, errp);
+ if (addr != NULL) {
+ opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+ inet_addr_to_opts(opts, addr);
+ qapi_free_InetSocketAddress(addr);
sock = inet_connect_opts(opts, errp, NULL, NULL);
- } else {
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ qemu_opts_del(opts);
}
- qemu_opts_del(opts);
return sock;
}
@@ -638,22 +645,24 @@ int inet_nonblocking_connect(const char *str,
{
QemuOpts *opts;
int sock = -1;
+ InetSocketAddress *addr;
g_assert(callback != NULL);
- opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
- if (inet_parse(opts, str) == 0) {
+ addr = inet_parse(str, errp);
+ if (addr != NULL) {
+ opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+ inet_addr_to_opts(opts, addr);
+ qapi_free_InetSocketAddress(addr);
sock = inet_connect_opts(opts, errp, callback, opaque);
- } else {
- error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ qemu_opts_del(opts);
}
- qemu_opts_del(opts);
return sock;
}
#ifndef _WIN32
-int unix_listen_opts(QemuOpts *opts)
+int unix_listen_opts(QemuOpts *opts, Error **errp)
{
struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
@@ -661,7 +670,7 @@ int unix_listen_opts(QemuOpts *opts)
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
- perror("socket(unix)");
+ error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
return -1;
}
@@ -686,11 +695,11 @@ int unix_listen_opts(QemuOpts *opts)
unlink(un.sun_path);
if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
- fprintf(stderr, "bind(unix:%s): %s\n", un.sun_path, strerror(errno));
+ error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
goto err;
}
if (listen(sock, 1) < 0) {
- fprintf(stderr, "listen(unix:%s): %s\n", un.sun_path, strerror(errno));
+ error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
goto err;
}
@@ -701,37 +710,85 @@ err:
return -1;
}
-int unix_connect_opts(QemuOpts *opts)
+int unix_connect_opts(QemuOpts *opts, Error **errp,
+ NonBlockingConnectHandler *callback, void *opaque)
{
struct sockaddr_un un;
const char *path = qemu_opt_get(opts, "path");
- int sock;
+ ConnectState *connect_state = NULL;
+ int sock, rc;
if (NULL == path) {
- fprintf(stderr, "unix connect: no path specified\n");
+ error_setg(errp, "unix connect: no path specified\n");
return -1;
}
sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
if (sock < 0) {
- perror("socket(unix)");
+ error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
return -1;
}
+ if (callback != NULL) {
+ connect_state = g_malloc0(sizeof(*connect_state));
+ connect_state->callback = callback;
+ connect_state->opaque = opaque;
+ socket_set_nonblock(sock);
+ }
memset(&un, 0, sizeof(un));
un.sun_family = AF_UNIX;
snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
- if (connect(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
- fprintf(stderr, "connect(unix:%s): %s\n", path, strerror(errno));
+
+ /* connect to peer */
+ do {
+ rc = 0;
+ if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
+ rc = -socket_error();
+ }
+ } while (rc == -EINTR);
+
+ if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
+ connect_state->fd = sock;
+ qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
+ connect_state);
+ return sock;
+ } else if (rc >= 0) {
+ /* non blocking socket immediate success, call callback */
+ if (callback != NULL) {
+ callback(sock, opaque);
+ }
+ }
+
+ if (rc < 0) {
+ error_set_errno(errp, -rc, QERR_SOCKET_CONNECT_FAILED);
close(sock);
- return -1;
+ sock = -1;
}
+ g_free(connect_state);
return sock;
}
+#else
+
+int unix_listen_opts(QemuOpts *opts, Error **errp)
+{
+ error_setg(errp, "unix sockets are not available on windows");
+ errno = ENOTSUP;
+ return -1;
+}
+
+int unix_connect_opts(QemuOpts *opts, Error **errp,
+ NonBlockingConnectHandler *callback, void *opaque)
+{
+ error_setg(errp, "unix sockets are not available on windows");
+ errno = ENOTSUP;
+ return -1;
+}
+#endif
+
/* compatibility wrapper */
-int unix_listen(const char *str, char *ostr, int olen)
+int unix_listen(const char *str, char *ostr, int olen, Error **errp)
{
QemuOpts *opts;
char *path, *optstr;
@@ -752,7 +809,7 @@ int unix_listen(const char *str, char *ostr, int olen)
qemu_opt_set(opts, "path", str);
}
- sock = unix_listen_opts(opts);
+ sock = unix_listen_opts(opts, errp);
if (sock != -1 && ostr)
snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
@@ -760,49 +817,132 @@ int unix_listen(const char *str, char *ostr, int olen)
return sock;
}
-int unix_connect(const char *path)
+int unix_connect(const char *path, Error **errp)
{
QemuOpts *opts;
int sock;
opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
qemu_opt_set(opts, "path", path);
- sock = unix_connect_opts(opts);
+ sock = unix_connect_opts(opts, errp, NULL, NULL);
qemu_opts_del(opts);
return sock;
}
-#else
-int unix_listen_opts(QemuOpts *opts)
+int unix_nonblocking_connect(const char *path,
+ NonBlockingConnectHandler *callback,
+ void *opaque, Error **errp)
{
- fprintf(stderr, "unix sockets are not available on windows\n");
- errno = ENOTSUP;
- return -1;
+ QemuOpts *opts;
+ int sock = -1;
+
+ g_assert(callback != NULL);
+
+ opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+ qemu_opt_set(opts, "path", path);
+ sock = unix_connect_opts(opts, errp, callback, opaque);
+ qemu_opts_del(opts);
+ return sock;
}
-int unix_connect_opts(QemuOpts *opts)
+SocketAddress *socket_parse(const char *str, Error **errp)
{
- fprintf(stderr, "unix sockets are not available on windows\n");
- errno = ENOTSUP;
- return -1;
+ SocketAddress *addr = NULL;
+
+ addr = g_new(SocketAddress, 1);
+ if (strstart(str, "unix:", NULL)) {
+ if (str[5] == '\0') {
+ error_setg(errp, "invalid Unix socket address\n");
+ goto fail;
+ } else {
+ addr->kind = SOCKET_ADDRESS_KIND_UNIX;
+ addr->q_unix = g_new(UnixSocketAddress, 1);
+ addr->q_unix->path = g_strdup(str + 5);
+ }
+ } else if (strstart(str, "fd:", NULL)) {
+ if (str[3] == '\0') {
+ error_setg(errp, "invalid file descriptor address\n");
+ goto fail;
+ } else {
+ addr->kind = SOCKET_ADDRESS_KIND_FD;
+ addr->fd = g_new(String, 1);
+ addr->fd->str = g_strdup(str + 3);
+ }
+ } else {
+ addr->kind = SOCKET_ADDRESS_KIND_INET;
+ addr->inet = g_new(InetSocketAddress, 1);
+ addr->inet = inet_parse(str, errp);
+ if (addr->inet == NULL) {
+ goto fail;
+ }
+ }
+ return addr;
+
+fail:
+ qapi_free_SocketAddress(addr);
+ return NULL;
}
-int unix_listen(const char *path, char *ostr, int olen)
+int socket_connect(SocketAddress *addr, Error **errp,
+ NonBlockingConnectHandler *callback, void *opaque)
{
- fprintf(stderr, "unix sockets are not available on windows\n");
- errno = ENOTSUP;
- return -1;
+ QemuOpts *opts;
+ int fd;
+
+ opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+ switch (addr->kind) {
+ case SOCKET_ADDRESS_KIND_INET:
+ inet_addr_to_opts(opts, addr->inet);
+ fd = inet_connect_opts(opts, errp, callback, opaque);
+ break;
+
+ case SOCKET_ADDRESS_KIND_UNIX:
+ qemu_opt_set(opts, "path", addr->q_unix->path);
+ fd = unix_connect_opts(opts, errp, callback, opaque);
+ break;
+
+ case SOCKET_ADDRESS_KIND_FD:
+ fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
+ if (callback) {
+ callback(fd, opaque);
+ }
+ break;
+
+ default:
+ abort();
+ }
+ qemu_opts_del(opts);
+ return fd;
}
-int unix_connect(const char *path)
+int socket_listen(SocketAddress *addr, Error **errp)
{
- fprintf(stderr, "unix sockets are not available on windows\n");
- errno = ENOTSUP;
- return -1;
-}
+ QemuOpts *opts;
+ int fd;
-#endif
+ opts = qemu_opts_create(&dummy_opts, NULL, 0, NULL);
+ switch (addr->kind) {
+ case SOCKET_ADDRESS_KIND_INET:
+ inet_addr_to_opts(opts, addr->inet);
+ fd = inet_listen_opts(opts, 0, errp);
+ break;
+
+ case SOCKET_ADDRESS_KIND_UNIX:
+ qemu_opt_set(opts, "path", addr->q_unix->path);
+ fd = unix_listen_opts(opts, errp);
+ break;
+
+ case SOCKET_ADDRESS_KIND_FD:
+ fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
+ break;
+
+ default:
+ abort();
+ }
+ qemu_opts_del(opts);
+ return fd;
+}
#ifdef _WIN32
static void socket_cleanup(void)