aboutsummaryrefslogtreecommitdiff
path: root/qemu-sockets.c
diff options
context:
space:
mode:
Diffstat (limited to 'qemu-sockets.c')
-rw-r--r--qemu-sockets.c43
1 files changed, 37 insertions, 6 deletions
diff --git a/qemu-sockets.c b/qemu-sockets.c
index 6bcb8e3e0f..ce3f06cac2 100644
--- a/qemu-sockets.c
+++ b/qemu-sockets.c
@@ -51,6 +51,9 @@ static QemuOptsList dummy_opts = {
},{
.name = "ipv6",
.type = QEMU_OPT_BOOL,
+ },{
+ .name = "block",
+ .type = QEMU_OPT_BOOL,
},
{ /* end if list */ }
},
@@ -194,7 +197,7 @@ listen:
return slisten;
}
-int inet_connect_opts(QemuOpts *opts)
+int inet_connect_opts(QemuOpts *opts, Error **errp)
{
struct addrinfo ai,*res,*e;
const char *addr;
@@ -202,6 +205,7 @@ int inet_connect_opts(QemuOpts *opts)
char uaddr[INET6_ADDRSTRLEN+1];
char uport[33];
int sock,rc;
+ bool block;
memset(&ai,0, sizeof(ai));
ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
@@ -210,8 +214,10 @@ int inet_connect_opts(QemuOpts *opts)
addr = qemu_opt_get(opts, "host");
port = qemu_opt_get(opts, "port");
+ block = qemu_opt_get_bool(opts, "block", 0);
if (addr == NULL || port == NULL) {
fprintf(stderr, "inet_connect: host and/or port not specified\n");
+ error_set(errp, QERR_SOCKET_CREATE_FAILED);
return -1;
}
@@ -224,6 +230,7 @@ int inet_connect_opts(QemuOpts *opts)
if (0 != (rc = getaddrinfo(addr, port, &ai, &res))) {
fprintf(stderr,"getaddrinfo(%s,%s): %s\n", addr, port,
gai_strerror(rc));
+ error_set(errp, QERR_SOCKET_CREATE_FAILED);
return -1;
}
@@ -241,19 +248,37 @@ int inet_connect_opts(QemuOpts *opts)
continue;
}
setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(void*)&on,sizeof(on));
-
+ if (!block) {
+ socket_set_nonblock(sock);
+ }
/* connect to peer */
- if (connect(sock,e->ai_addr,e->ai_addrlen) < 0) {
+ do {
+ rc = 0;
+ if (connect(sock, e->ai_addr, e->ai_addrlen) < 0) {
+ rc = -socket_error();
+ }
+ } while (rc == -EINTR);
+
+ #ifdef _WIN32
+ if (!block && (rc == -EINPROGRESS || rc == -EWOULDBLOCK
+ || rc == -WSAEALREADY)) {
+ #else
+ if (!block && (rc == -EINPROGRESS)) {
+ #endif
+ error_set(errp, QERR_SOCKET_CONNECT_IN_PROGRESS);
+ } else if (rc < 0) {
if (NULL == e->ai_next)
fprintf(stderr, "%s: connect(%s,%s,%s,%s): %s\n", __FUNCTION__,
inet_strfamily(e->ai_family),
e->ai_canonname, uaddr, uport, strerror(errno));
closesocket(sock);
+ sock = -1;
continue;
}
freeaddrinfo(res);
return sock;
}
+ error_set(errp, QERR_SOCKET_CONNECT_FAILED);
freeaddrinfo(res);
return -1;
}
@@ -449,14 +474,20 @@ int inet_listen(const char *str, char *ostr, int olen,
return sock;
}
-int inet_connect(const char *str, int socktype)
+int inet_connect(const char *str, bool block, Error **errp)
{
QemuOpts *opts;
int sock = -1;
opts = qemu_opts_create(&dummy_opts, NULL, 0);
- if (inet_parse(opts, str) == 0)
- sock = inet_connect_opts(opts);
+ if (inet_parse(opts, str) == 0) {
+ if (block) {
+ qemu_opt_set(opts, "block", "on");
+ }
+ sock = inet_connect_opts(opts, errp);
+ } else {
+ error_set(errp, QERR_SOCKET_CREATE_FAILED);
+ }
qemu_opts_del(opts);
return sock;
}