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