]> git.proxmox.com Git - qemu.git/blame - util/qemu-sockets.c
Update version for 1.5.0-rc1
[qemu.git] / util / qemu-sockets.c
CommitLineData
305b0eb2
AL
1/*
2 * inet and unix socket functions for qemu
3 *
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
e6d91ab6
PB
14 *
15 * Contributions after 2012-01-13 are licensed under the terms of the
16 * GNU GPL, version 2 or (at your option) any later version.
305b0eb2 17 */
d247d25f
AL
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
22#include <errno.h>
23#include <unistd.h>
24
83c9089e 25#include "monitor/monitor.h"
1de7afc9 26#include "qemu/sockets.h"
47398b9c 27#include "qemu-common.h" /* for qemu_isdigit */
1de7afc9 28#include "qemu/main-loop.h"
d247d25f
AL
29
30#ifndef AI_ADDRCONFIG
31# define AI_ADDRCONFIG 0
32#endif
33
d247d25f
AL
34static const int on=1, off=0;
35
e62be888
KW
36/* used temporarily until all users are converted to QemuOpts */
37QemuOptsList socket_optslist = {
38 .name = "socket",
39 .head = QTAILQ_HEAD_INITIALIZER(socket_optslist.head),
2af2bf67
GH
40 .desc = {
41 {
42 .name = "path",
43 .type = QEMU_OPT_STRING,
f4c94c7c
GH
44 },{
45 .name = "host",
46 .type = QEMU_OPT_STRING,
47 },{
48 .name = "port",
49 .type = QEMU_OPT_STRING,
50 },{
51 .name = "to",
52 .type = QEMU_OPT_NUMBER,
53 },{
54 .name = "ipv4",
55 .type = QEMU_OPT_BOOL,
56 },{
57 .name = "ipv6",
58 .type = QEMU_OPT_BOOL,
2af2bf67
GH
59 },
60 { /* end if list */ }
61 },
62};
63
d247d25f
AL
64static int inet_getport(struct addrinfo *e)
65{
66 struct sockaddr_in *i4;
67 struct sockaddr_in6 *i6;
68
69 switch (e->ai_family) {
70 case PF_INET6:
71 i6 = (void*)e->ai_addr;
72 return ntohs(i6->sin6_port);
73 case PF_INET:
74 i4 = (void*)e->ai_addr;
75 return ntohs(i4->sin_port);
76 default:
77 return 0;
78 }
79}
80
81static void inet_setport(struct addrinfo *e, int port)
82{
83 struct sockaddr_in *i4;
84 struct sockaddr_in6 *i6;
85
86 switch (e->ai_family) {
87 case PF_INET6:
88 i6 = (void*)e->ai_addr;
89 i6->sin6_port = htons(port);
90 break;
91 case PF_INET:
92 i4 = (void*)e->ai_addr;
93 i4->sin_port = htons(port);
94 break;
95 }
96}
97
c9c4b34e 98const char *inet_strfamily(int family)
d247d25f
AL
99{
100 switch (family) {
101 case PF_INET6: return "ipv6";
102 case PF_INET: return "ipv4";
103 case PF_UNIX: return "unix";
104 }
c445321e 105 return "unknown";
d247d25f
AL
106}
107
029409e5 108int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
d247d25f
AL
109{
110 struct addrinfo ai,*res,*e;
e5bc776f 111 const char *addr;
d247d25f
AL
112 char port[33];
113 char uaddr[INET6_ADDRSTRLEN+1];
114 char uport[33];
877691f9 115 int slisten, rc, to, port_min, port_max, p;
d247d25f
AL
116
117 memset(&ai,0, sizeof(ai));
118 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
119 ai.ai_family = PF_UNSPEC;
e5bc776f 120 ai.ai_socktype = SOCK_STREAM;
d247d25f 121
e23a22e6
JO
122 if ((qemu_opt_get(opts, "host") == NULL) ||
123 (qemu_opt_get(opts, "port") == NULL)) {
a12fb8ad 124 error_setg(errp, "host and/or port not specified");
e5bc776f 125 return -1;
d247d25f 126 }
e5bc776f
GH
127 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
128 addr = qemu_opt_get(opts, "host");
d247d25f 129
e5bc776f
GH
130 to = qemu_opt_get_number(opts, "to", 0);
131 if (qemu_opt_get_bool(opts, "ipv4", 0))
d247d25f 132 ai.ai_family = PF_INET;
e5bc776f 133 if (qemu_opt_get_bool(opts, "ipv6", 0))
d247d25f
AL
134 ai.ai_family = PF_INET6;
135
136 /* lookup */
137 if (port_offset)
138 snprintf(port, sizeof(port), "%d", atoi(port) + port_offset);
139 rc = getaddrinfo(strlen(addr) ? addr : NULL, port, &ai, &res);
140 if (rc != 0) {
a12fb8ad
PB
141 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
142 gai_strerror(rc));
d247d25f
AL
143 return -1;
144 }
d247d25f
AL
145
146 /* create socket + bind */
147 for (e = res; e != NULL; e = e->ai_next) {
39b6efc8 148 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
149 uaddr,INET6_ADDRSTRLEN,uport,32,
150 NI_NUMERICHOST | NI_NUMERICSERV);
40ff6d7e 151 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
39b6efc8 152 if (slisten < 0) {
029409e5 153 if (!e->ai_next) {
a12fb8ad 154 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
029409e5 155 }
39b6efc8 156 continue;
157 }
d247d25f 158
9957fc7f 159 qemu_setsockopt(slisten, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
d247d25f
AL
160#ifdef IPV6_V6ONLY
161 if (e->ai_family == PF_INET6) {
162 /* listen on both ipv4 and ipv6 */
9957fc7f
SW
163 qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off,
164 sizeof(off));
d247d25f
AL
165 }
166#endif
167
877691f9
MA
168 port_min = inet_getport(e);
169 port_max = to ? to + port_offset : port_min;
170 for (p = port_min; p <= port_max; p++) {
171 inet_setport(e, p);
d247d25f 172 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
d247d25f
AL
173 goto listen;
174 }
877691f9 175 if (p == port_max) {
029409e5 176 if (!e->ai_next) {
a12fb8ad 177 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
029409e5 178 }
d247d25f 179 }
d247d25f
AL
180 }
181 closesocket(slisten);
182 }
d247d25f
AL
183 freeaddrinfo(res);
184 return -1;
185
186listen:
187 if (listen(slisten,1) != 0) {
a12fb8ad 188 error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
d247d25f 189 closesocket(slisten);
39b6efc8 190 freeaddrinfo(res);
d247d25f
AL
191 return -1;
192 }
e5bc776f
GH
193 snprintf(uport, sizeof(uport), "%d", inet_getport(e) - port_offset);
194 qemu_opt_set(opts, "host", uaddr);
195 qemu_opt_set(opts, "port", uport);
196 qemu_opt_set(opts, "ipv6", (e->ai_family == PF_INET6) ? "on" : "off");
197 qemu_opt_set(opts, "ipv4", (e->ai_family != PF_INET6) ? "on" : "off");
d247d25f
AL
198 freeaddrinfo(res);
199 return slisten;
200}
201
05bc1d8a
MT
202#ifdef _WIN32
203#define QEMU_SOCKET_RC_INPROGRESS(rc) \
204 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
205#else
206#define QEMU_SOCKET_RC_INPROGRESS(rc) \
207 ((rc) == -EINPROGRESS)
208#endif
209
233aa5c2
OW
210/* Struct to store connect state for non blocking connect */
211typedef struct ConnectState {
212 int fd;
213 struct addrinfo *addr_list;
214 struct addrinfo *current_addr;
215 NonBlockingConnectHandler *callback;
216 void *opaque;
217} ConnectState;
218
219static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
11663b55 220 ConnectState *connect_state, Error **errp);
233aa5c2
OW
221
222static void wait_for_connect(void *opaque)
d247d25f 223{
233aa5c2
OW
224 ConnectState *s = opaque;
225 int val = 0, rc = 0;
226 socklen_t valsize = sizeof(val);
227 bool in_progress;
228
229 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
230
231 do {
9957fc7f 232 rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
233aa5c2
OW
233 } while (rc == -1 && socket_error() == EINTR);
234
235 /* update rc to contain error */
236 if (!rc && val) {
237 rc = -1;
238 }
239
240 /* connect error */
241 if (rc < 0) {
242 closesocket(s->fd);
243 s->fd = rc;
244 }
245
246 /* try to connect to the next address on the list */
1fc05adf
PB
247 if (s->current_addr) {
248 while (s->current_addr->ai_next != NULL && s->fd < 0) {
249 s->current_addr = s->current_addr->ai_next;
11663b55 250 s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
1fc05adf
PB
251 /* connect in progress */
252 if (in_progress) {
253 return;
254 }
233aa5c2 255 }
1fc05adf
PB
256
257 freeaddrinfo(s->addr_list);
233aa5c2 258 }
05bc1d8a 259
233aa5c2
OW
260 if (s->callback) {
261 s->callback(s->fd, s->opaque);
05bc1d8a 262 }
233aa5c2 263 g_free(s);
233aa5c2
OW
264}
265
266static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
11663b55 267 ConnectState *connect_state, Error **errp)
233aa5c2
OW
268{
269 int sock, rc;
270
271 *in_progress = false;
05bc1d8a
MT
272
273 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
274 if (sock < 0) {
11663b55 275 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
05bc1d8a
MT
276 return -1;
277 }
58455eb9 278 qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
233aa5c2 279 if (connect_state != NULL) {
f9e8cacc 280 qemu_set_nonblock(sock);
05bc1d8a
MT
281 }
282 /* connect to peer */
283 do {
284 rc = 0;
285 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
286 rc = -socket_error();
287 }
288 } while (rc == -EINTR);
289
233aa5c2
OW
290 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
291 connect_state->fd = sock;
292 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
293 connect_state);
294 *in_progress = true;
05bc1d8a 295 } else if (rc < 0) {
11663b55 296 error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
05bc1d8a
MT
297 closesocket(sock);
298 return -1;
299 }
300 return sock;
301}
302
303static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
304{
305 struct addrinfo ai, *res;
306 int rc;
f4c94c7c
GH
307 const char *addr;
308 const char *port;
d247d25f 309
05bc1d8a 310 memset(&ai, 0, sizeof(ai));
233aa5c2 311
d247d25f
AL
312 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
313 ai.ai_family = PF_UNSPEC;
f4c94c7c 314 ai.ai_socktype = SOCK_STREAM;
d247d25f 315
f4c94c7c
GH
316 addr = qemu_opt_get(opts, "host");
317 port = qemu_opt_get(opts, "port");
318 if (addr == NULL || port == NULL) {
a12fb8ad 319 error_setg(errp, "host and/or port not specified");
05bc1d8a 320 return NULL;
d247d25f
AL
321 }
322
05bc1d8a 323 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
d247d25f 324 ai.ai_family = PF_INET;
05bc1d8a
MT
325 }
326 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
d247d25f 327 ai.ai_family = PF_INET6;
05bc1d8a 328 }
d247d25f
AL
329
330 /* lookup */
05bc1d8a
MT
331 rc = getaddrinfo(addr, port, &ai, &res);
332 if (rc != 0) {
a12fb8ad
PB
333 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
334 gai_strerror(rc));
05bc1d8a
MT
335 return NULL;
336 }
337 return res;
338}
339
5db5f44c
OW
340/**
341 * Create a socket and connect it to an address.
342 *
343 * @opts: QEMU options, recognized parameters strings "host" and "port",
344 * bools "ipv4" and "ipv6".
5db5f44c 345 * @errp: set on error
233aa5c2
OW
346 * @callback: callback function for non-blocking connect
347 * @opaque: opaque for callback function
5db5f44c
OW
348 *
349 * Returns: -1 on error, file descriptor on success.
233aa5c2
OW
350 *
351 * If @callback is non-null, the connect is non-blocking. If this
352 * function succeeds, callback will be called when the connection
353 * completes, with the file descriptor on success, or -1 on error.
5db5f44c 354 */
233aa5c2
OW
355int inet_connect_opts(QemuOpts *opts, Error **errp,
356 NonBlockingConnectHandler *callback, void *opaque)
05bc1d8a
MT
357{
358 struct addrinfo *res, *e;
359 int sock = -1;
233aa5c2
OW
360 bool in_progress;
361 ConnectState *connect_state = NULL;
05bc1d8a
MT
362
363 res = inet_parse_connect_opts(opts, errp);
364 if (!res) {
365 return -1;
366 }
367
233aa5c2
OW
368 if (callback != NULL) {
369 connect_state = g_malloc0(sizeof(*connect_state));
370 connect_state->addr_list = res;
371 connect_state->callback = callback;
372 connect_state->opaque = opaque;
d247d25f 373 }
d247d25f
AL
374
375 for (e = res; e != NULL; e = e->ai_next) {
baca6f18
AL
376 if (error_is_set(errp)) {
377 error_free(*errp);
378 *errp = NULL;
379 }
233aa5c2
OW
380 if (connect_state != NULL) {
381 connect_state->current_addr = e;
382 }
11663b55 383 sock = inet_connect_addr(e, &in_progress, connect_state, errp);
233aa5c2
OW
384 if (in_progress) {
385 return sock;
386 } else if (sock >= 0) {
387 /* non blocking socket immediate success, call callback */
388 if (callback != NULL) {
389 callback(sock, opaque);
390 }
05bc1d8a 391 break;
a6ba35b3 392 }
d247d25f 393 }
233aa5c2 394 g_free(connect_state);
d247d25f 395 freeaddrinfo(res);
05bc1d8a 396 return sock;
d247d25f
AL
397}
398
7fc4e63e 399int inet_dgram_opts(QemuOpts *opts, Error **errp)
7e1b35b4
GH
400{
401 struct addrinfo ai, *peer = NULL, *local = NULL;
402 const char *addr;
403 const char *port;
7e1b35b4
GH
404 int sock = -1, rc;
405
406 /* lookup peer addr */
407 memset(&ai,0, sizeof(ai));
408 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
409 ai.ai_family = PF_UNSPEC;
410 ai.ai_socktype = SOCK_DGRAM;
411
412 addr = qemu_opt_get(opts, "host");
413 port = qemu_opt_get(opts, "port");
414 if (addr == NULL || strlen(addr) == 0) {
415 addr = "localhost";
416 }
417 if (port == NULL || strlen(port) == 0) {
4f085c82 418 error_setg(errp, "remote port not specified");
7e1b35b4
GH
419 return -1;
420 }
421
422 if (qemu_opt_get_bool(opts, "ipv4", 0))
423 ai.ai_family = PF_INET;
424 if (qemu_opt_get_bool(opts, "ipv6", 0))
425 ai.ai_family = PF_INET6;
426
427 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
4f085c82
PB
428 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
429 gai_strerror(rc));
7e1b35b4
GH
430 return -1;
431 }
7e1b35b4
GH
432
433 /* lookup local addr */
434 memset(&ai,0, sizeof(ai));
435 ai.ai_flags = AI_PASSIVE;
436 ai.ai_family = peer->ai_family;
437 ai.ai_socktype = SOCK_DGRAM;
438
439 addr = qemu_opt_get(opts, "localaddr");
440 port = qemu_opt_get(opts, "localport");
441 if (addr == NULL || strlen(addr) == 0) {
442 addr = NULL;
443 }
444 if (!port || strlen(port) == 0)
445 port = "0";
446
447 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
4f085c82
PB
448 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
449 gai_strerror(rc));
39b38459 450 goto err;
7e1b35b4 451 }
7e1b35b4
GH
452
453 /* create socket */
40ff6d7e 454 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
7e1b35b4 455 if (sock < 0) {
4f085c82 456 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
7e1b35b4
GH
457 goto err;
458 }
9957fc7f 459 qemu_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
7e1b35b4
GH
460
461 /* bind socket */
7e1b35b4 462 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
4f085c82 463 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
7e1b35b4
GH
464 goto err;
465 }
466
467 /* connect to peer */
7e1b35b4 468 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
4f085c82 469 error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
7e1b35b4
GH
470 goto err;
471 }
472
473 freeaddrinfo(local);
474 freeaddrinfo(peer);
475 return sock;
476
477err:
478 if (-1 != sock)
479 closesocket(sock);
480 if (local)
481 freeaddrinfo(local);
482 if (peer)
483 freeaddrinfo(peer);
484 return -1;
485}
486
f4c94c7c 487/* compatibility wrapper */
f17c90be 488InetSocketAddress *inet_parse(const char *str, Error **errp)
f4c94c7c 489{
879e45c7 490 InetSocketAddress *addr;
f4c94c7c 491 const char *optstr, *h;
879e45c7 492 char host[64];
f4c94c7c 493 char port[33];
879e45c7 494 int to;
f4c94c7c
GH
495 int pos;
496
879e45c7
PB
497 addr = g_new0(InetSocketAddress, 1);
498
f4c94c7c
GH
499 /* parse address */
500 if (str[0] == ':') {
501 /* no host given */
879e45c7
PB
502 host[0] = '\0';
503 if (1 != sscanf(str, ":%32[^,]%n", port, &pos)) {
2f002c43 504 error_setg(errp, "error parsing port in address '%s'", str);
879e45c7 505 goto fail;
f4c94c7c
GH
506 }
507 } else if (str[0] == '[') {
508 /* IPv6 addr */
879e45c7 509 if (2 != sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos)) {
2f002c43 510 error_setg(errp, "error parsing IPv6 address '%s'", str);
879e45c7 511 goto fail;
f4c94c7c 512 }
879e45c7 513 addr->ipv6 = addr->has_ipv6 = true;
f4c94c7c
GH
514 } else if (qemu_isdigit(str[0])) {
515 /* IPv4 addr */
879e45c7 516 if (2 != sscanf(str, "%64[0-9.]:%32[^,]%n", host, port, &pos)) {
2f002c43 517 error_setg(errp, "error parsing IPv4 address '%s'", str);
879e45c7 518 goto fail;
f4c94c7c 519 }
879e45c7 520 addr->ipv4 = addr->has_ipv4 = true;
f4c94c7c
GH
521 } else {
522 /* hostname */
879e45c7 523 if (2 != sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos)) {
2f002c43 524 error_setg(errp, "error parsing address '%s'", str);
879e45c7 525 goto fail;
f4c94c7c
GH
526 }
527 }
879e45c7
PB
528
529 addr->host = g_strdup(host);
530 addr->port = g_strdup(port);
f4c94c7c
GH
531
532 /* parse options */
533 optstr = str + pos;
534 h = strstr(optstr, ",to=");
879e45c7 535 if (h) {
1ccbc285
AP
536 h += 4;
537 if (sscanf(h, "%d%n", &to, &pos) != 1 ||
538 (h[pos] != '\0' && h[pos] != ',')) {
879e45c7
PB
539 error_setg(errp, "error parsing to= argument");
540 goto fail;
541 }
542 addr->has_to = true;
543 addr->to = to;
544 }
545 if (strstr(optstr, ",ipv4")) {
546 addr->ipv4 = addr->has_ipv4 = true;
547 }
548 if (strstr(optstr, ",ipv6")) {
549 addr->ipv6 = addr->has_ipv6 = true;
550 }
551 return addr;
552
553fail:
554 qapi_free_InetSocketAddress(addr);
555 return NULL;
556}
557
f17c90be 558static void inet_addr_to_opts(QemuOpts *opts, const InetSocketAddress *addr)
879e45c7
PB
559{
560 bool ipv4 = addr->ipv4 || !addr->has_ipv4;
561 bool ipv6 = addr->ipv6 || !addr->has_ipv6;
562
563 if (!ipv4 || !ipv6) {
564 qemu_opt_set_bool(opts, "ipv4", ipv4);
565 qemu_opt_set_bool(opts, "ipv6", ipv6);
566 }
567 if (addr->has_to) {
568 char to[20];
569 snprintf(to, sizeof(to), "%d", addr->to);
570 qemu_opt_set(opts, "to", to);
571 }
572 qemu_opt_set(opts, "host", addr->host);
573 qemu_opt_set(opts, "port", addr->port);
f4c94c7c
GH
574}
575
e5bc776f 576int inet_listen(const char *str, char *ostr, int olen,
029409e5 577 int socktype, int port_offset, Error **errp)
e5bc776f
GH
578{
579 QemuOpts *opts;
580 char *optstr;
581 int sock = -1;
879e45c7 582 InetSocketAddress *addr;
e5bc776f 583
879e45c7
PB
584 addr = inet_parse(str, errp);
585 if (addr != NULL) {
e62be888 586 opts = qemu_opts_create_nofail(&socket_optslist);
879e45c7
PB
587 inet_addr_to_opts(opts, addr);
588 qapi_free_InetSocketAddress(addr);
029409e5 589 sock = inet_listen_opts(opts, port_offset, errp);
e5bc776f
GH
590 if (sock != -1 && ostr) {
591 optstr = strchr(str, ',');
592 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
593 snprintf(ostr, olen, "[%s]:%s%s",
594 qemu_opt_get(opts, "host"),
595 qemu_opt_get(opts, "port"),
596 optstr ? optstr : "");
597 } else {
598 snprintf(ostr, olen, "%s:%s%s",
599 qemu_opt_get(opts, "host"),
600 qemu_opt_get(opts, "port"),
601 optstr ? optstr : "");
602 }
603 }
879e45c7 604 qemu_opts_del(opts);
e5bc776f 605 }
e5bc776f
GH
606 return sock;
607}
608
5db5f44c
OW
609/**
610 * Create a blocking socket and connect it to an address.
611 *
612 * @str: address string
613 * @errp: set in case of an error
614 *
615 * Returns -1 in case of error, file descriptor on success
616 **/
617int inet_connect(const char *str, Error **errp)
f4c94c7c
GH
618{
619 QemuOpts *opts;
620 int sock = -1;
879e45c7 621 InetSocketAddress *addr;
f4c94c7c 622
879e45c7
PB
623 addr = inet_parse(str, errp);
624 if (addr != NULL) {
f17c90be 625 opts = qemu_opts_create_nofail(&socket_optslist);
879e45c7
PB
626 inet_addr_to_opts(opts, addr);
627 qapi_free_InetSocketAddress(addr);
233aa5c2 628 sock = inet_connect_opts(opts, errp, NULL, NULL);
879e45c7 629 qemu_opts_del(opts);
5db5f44c 630 }
5db5f44c
OW
631 return sock;
632}
633
634/**
635 * Create a non-blocking socket and connect it to an address.
233aa5c2
OW
636 * Calls the callback function with fd in case of success or -1 in case of
637 * error.
5db5f44c
OW
638 *
639 * @str: address string
233aa5c2
OW
640 * @callback: callback function that is called when connect completes,
641 * cannot be NULL.
642 * @opaque: opaque for callback function
5db5f44c
OW
643 * @errp: set in case of an error
644 *
233aa5c2 645 * Returns: -1 on immediate error, file descriptor on success.
5db5f44c 646 **/
233aa5c2
OW
647int inet_nonblocking_connect(const char *str,
648 NonBlockingConnectHandler *callback,
649 void *opaque, Error **errp)
5db5f44c
OW
650{
651 QemuOpts *opts;
652 int sock = -1;
879e45c7 653 InetSocketAddress *addr;
5db5f44c 654
233aa5c2
OW
655 g_assert(callback != NULL);
656
879e45c7
PB
657 addr = inet_parse(str, errp);
658 if (addr != NULL) {
e62be888 659 opts = qemu_opts_create_nofail(&socket_optslist);
879e45c7
PB
660 inet_addr_to_opts(opts, addr);
661 qapi_free_InetSocketAddress(addr);
233aa5c2 662 sock = inet_connect_opts(opts, errp, callback, opaque);
879e45c7 663 qemu_opts_del(opts);
a6ba35b3 664 }
f4c94c7c
GH
665 return sock;
666}
667
d247d25f
AL
668#ifndef _WIN32
669
7fc4e63e 670int unix_listen_opts(QemuOpts *opts, Error **errp)
d247d25f
AL
671{
672 struct sockaddr_un un;
62b6adfb
GH
673 const char *path = qemu_opt_get(opts, "path");
674 int sock, fd;
d247d25f 675
40ff6d7e 676 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
d247d25f 677 if (sock < 0) {
58899664 678 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
39b6efc8 679 return -1;
d247d25f
AL
680 }
681
d247d25f
AL
682 memset(&un, 0, sizeof(un));
683 un.sun_family = AF_UNIX;
684 if (path && strlen(path)) {
685 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
686 } else {
687 char *tmpdir = getenv("TMPDIR");
688 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
689 tmpdir ? tmpdir : "/tmp");
690 /*
691 * This dummy fd usage silences the mktemp() unsecure warning.
692 * Using mkstemp() doesn't make things more secure here
693 * though. bind() complains about existing files, so we have
694 * to unlink first and thus re-open the race window. The
695 * worst case possible is bind() failing, i.e. a DoS attack.
696 */
697 fd = mkstemp(un.sun_path); close(fd);
62b6adfb 698 qemu_opt_set(opts, "path", un.sun_path);
d247d25f 699 }
d247d25f
AL
700
701 unlink(un.sun_path);
702 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
58899664 703 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
d247d25f
AL
704 goto err;
705 }
706 if (listen(sock, 1) < 0) {
58899664 707 error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
d247d25f
AL
708 goto err;
709 }
710
d247d25f
AL
711 return sock;
712
713err:
d247d25f
AL
714 closesocket(sock);
715 return -1;
716}
717
1fc05adf
PB
718int unix_connect_opts(QemuOpts *opts, Error **errp,
719 NonBlockingConnectHandler *callback, void *opaque)
d247d25f
AL
720{
721 struct sockaddr_un un;
2af2bf67 722 const char *path = qemu_opt_get(opts, "path");
1fc05adf
PB
723 ConnectState *connect_state = NULL;
724 int sock, rc;
d247d25f 725
2af2bf67 726 if (NULL == path) {
312fd5f2 727 error_setg(errp, "unix connect: no path specified");
2af2bf67
GH
728 return -1;
729 }
730
40ff6d7e 731 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
d247d25f 732 if (sock < 0) {
58899664 733 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
39b6efc8 734 return -1;
d247d25f 735 }
1fc05adf
PB
736 if (callback != NULL) {
737 connect_state = g_malloc0(sizeof(*connect_state));
738 connect_state->callback = callback;
739 connect_state->opaque = opaque;
f9e8cacc 740 qemu_set_nonblock(sock);
1fc05adf 741 }
d247d25f
AL
742
743 memset(&un, 0, sizeof(un));
744 un.sun_family = AF_UNIX;
745 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
1fc05adf
PB
746
747 /* connect to peer */
748 do {
749 rc = 0;
750 if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
751 rc = -socket_error();
752 }
753 } while (rc == -EINTR);
754
755 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
756 connect_state->fd = sock;
757 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
758 connect_state);
759 return sock;
760 } else if (rc >= 0) {
761 /* non blocking socket immediate success, call callback */
762 if (callback != NULL) {
763 callback(sock, opaque);
764 }
765 }
766
767 if (rc < 0) {
58899664 768 error_set_errno(errp, -rc, QERR_SOCKET_CONNECT_FAILED);
9d947472 769 close(sock);
1fc05adf 770 sock = -1;
d247d25f
AL
771 }
772
1fc05adf 773 g_free(connect_state);
d247d25f
AL
774 return sock;
775}
776
0c814709
PB
777#else
778
779int unix_listen_opts(QemuOpts *opts, Error **errp)
780{
58899664 781 error_setg(errp, "unix sockets are not available on windows");
0c814709
PB
782 errno = ENOTSUP;
783 return -1;
784}
785
1fc05adf
PB
786int unix_connect_opts(QemuOpts *opts, Error **errp,
787 NonBlockingConnectHandler *callback, void *opaque)
0c814709 788{
58899664 789 error_setg(errp, "unix sockets are not available on windows");
0c814709
PB
790 errno = ENOTSUP;
791 return -1;
792}
793#endif
794
2af2bf67 795/* compatibility wrapper */
7fc4e63e 796int unix_listen(const char *str, char *ostr, int olen, Error **errp)
62b6adfb
GH
797{
798 QemuOpts *opts;
799 char *path, *optstr;
800 int sock, len;
801
e62be888 802 opts = qemu_opts_create_nofail(&socket_optslist);
62b6adfb
GH
803
804 optstr = strchr(str, ',');
805 if (optstr) {
806 len = optstr - str;
807 if (len) {
7267c094 808 path = g_malloc(len+1);
62b6adfb
GH
809 snprintf(path, len+1, "%.*s", len, str);
810 qemu_opt_set(opts, "path", path);
7267c094 811 g_free(path);
62b6adfb
GH
812 }
813 } else {
814 qemu_opt_set(opts, "path", str);
815 }
816
7fc4e63e 817 sock = unix_listen_opts(opts, errp);
62b6adfb
GH
818
819 if (sock != -1 && ostr)
820 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
821 qemu_opts_del(opts);
822 return sock;
823}
824
7fc4e63e 825int unix_connect(const char *path, Error **errp)
2af2bf67
GH
826{
827 QemuOpts *opts;
828 int sock;
829
e62be888 830 opts = qemu_opts_create_nofail(&socket_optslist);
2af2bf67 831 qemu_opt_set(opts, "path", path);
1fc05adf
PB
832 sock = unix_connect_opts(opts, errp, NULL, NULL);
833 qemu_opts_del(opts);
834 return sock;
835}
836
837
838int unix_nonblocking_connect(const char *path,
839 NonBlockingConnectHandler *callback,
840 void *opaque, Error **errp)
841{
842 QemuOpts *opts;
843 int sock = -1;
844
845 g_assert(callback != NULL);
846
e62be888 847 opts = qemu_opts_create_nofail(&socket_optslist);
1fc05adf
PB
848 qemu_opt_set(opts, "path", path);
849 sock = unix_connect_opts(opts, errp, callback, opaque);
2af2bf67
GH
850 qemu_opts_del(opts);
851 return sock;
852}
853
101f9cbc
PB
854SocketAddress *socket_parse(const char *str, Error **errp)
855{
856 SocketAddress *addr = NULL;
857
858 addr = g_new(SocketAddress, 1);
859 if (strstart(str, "unix:", NULL)) {
860 if (str[5] == '\0') {
312fd5f2 861 error_setg(errp, "invalid Unix socket address");
101f9cbc
PB
862 goto fail;
863 } else {
864 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
865 addr->q_unix = g_new(UnixSocketAddress, 1);
866 addr->q_unix->path = g_strdup(str + 5);
867 }
868 } else if (strstart(str, "fd:", NULL)) {
869 if (str[3] == '\0') {
312fd5f2 870 error_setg(errp, "invalid file descriptor address");
101f9cbc
PB
871 goto fail;
872 } else {
873 addr->kind = SOCKET_ADDRESS_KIND_FD;
874 addr->fd = g_new(String, 1);
875 addr->fd->str = g_strdup(str + 3);
876 }
877 } else {
878 addr->kind = SOCKET_ADDRESS_KIND_INET;
879 addr->inet = g_new(InetSocketAddress, 1);
880 addr->inet = inet_parse(str, errp);
881 if (addr->inet == NULL) {
882 goto fail;
883 }
884 }
885 return addr;
886
887fail:
888 qapi_free_SocketAddress(addr);
889 return NULL;
890}
891
892int socket_connect(SocketAddress *addr, Error **errp,
893 NonBlockingConnectHandler *callback, void *opaque)
894{
895 QemuOpts *opts;
896 int fd;
897
e62be888 898 opts = qemu_opts_create_nofail(&socket_optslist);
101f9cbc
PB
899 switch (addr->kind) {
900 case SOCKET_ADDRESS_KIND_INET:
901 inet_addr_to_opts(opts, addr->inet);
902 fd = inet_connect_opts(opts, errp, callback, opaque);
903 break;
904
905 case SOCKET_ADDRESS_KIND_UNIX:
906 qemu_opt_set(opts, "path", addr->q_unix->path);
907 fd = unix_connect_opts(opts, errp, callback, opaque);
908 break;
909
910 case SOCKET_ADDRESS_KIND_FD:
911 fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
912 if (callback) {
1a751ebf 913 qemu_set_nonblock(fd);
101f9cbc
PB
914 callback(fd, opaque);
915 }
916 break;
917
918 default:
919 abort();
920 }
921 qemu_opts_del(opts);
922 return fd;
923}
924
925int socket_listen(SocketAddress *addr, Error **errp)
926{
927 QemuOpts *opts;
928 int fd;
929
e62be888 930 opts = qemu_opts_create_nofail(&socket_optslist);
101f9cbc
PB
931 switch (addr->kind) {
932 case SOCKET_ADDRESS_KIND_INET:
933 inet_addr_to_opts(opts, addr->inet);
934 fd = inet_listen_opts(opts, 0, errp);
935 break;
936
937 case SOCKET_ADDRESS_KIND_UNIX:
938 qemu_opt_set(opts, "path", addr->q_unix->path);
939 fd = unix_listen_opts(opts, errp);
940 break;
941
942 case SOCKET_ADDRESS_KIND_FD:
943 fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
944 break;
945
946 default:
947 abort();
948 }
949 qemu_opts_del(opts);
950 return fd;
951}
952
3ecc059d
GH
953int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
954{
955 QemuOpts *opts;
956 int fd;
957
e62be888 958 opts = qemu_opts_create_nofail(&socket_optslist);
3ecc059d
GH
959 switch (remote->kind) {
960 case SOCKET_ADDRESS_KIND_INET:
961 qemu_opt_set(opts, "host", remote->inet->host);
962 qemu_opt_set(opts, "port", remote->inet->port);
963 if (local) {
964 qemu_opt_set(opts, "localaddr", local->inet->host);
965 qemu_opt_set(opts, "localport", local->inet->port);
966 }
967 fd = inet_dgram_opts(opts, errp);
968 break;
969
970 default:
971 error_setg(errp, "socket type unsupported for datagram");
972 return -1;
973 }
974 qemu_opts_del(opts);
975 return fd;
976}