s390x/kvm: make flic play well with old kernels
[qemu.git] / util / qemu-sockets.c
blob627e60931a2453d8752b86e948a79568e79143fa
1 /*
2 * inet and unix socket functions for qemu
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
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.
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.
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.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <unistd.h>
25 #include "monitor/monitor.h"
26 #include "qemu/sockets.h"
27 #include "qemu/main-loop.h"
29 #ifndef AI_ADDRCONFIG
30 # define AI_ADDRCONFIG 0
31 #endif
33 static const int on=1, off=0;
35 /* used temporarily until all users are converted to QemuOpts */
36 QemuOptsList socket_optslist = {
37 .name = "socket",
38 .head = QTAILQ_HEAD_INITIALIZER(socket_optslist.head),
39 .desc = {
41 .name = "path",
42 .type = QEMU_OPT_STRING,
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,
59 { /* end if list */ }
63 static int inet_getport(struct addrinfo *e)
65 struct sockaddr_in *i4;
66 struct sockaddr_in6 *i6;
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;
80 static void inet_setport(struct addrinfo *e, int port)
82 struct sockaddr_in *i4;
83 struct sockaddr_in6 *i6;
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;
97 const char *inet_strfamily(int family)
99 switch (family) {
100 case PF_INET6: return "ipv6";
101 case PF_INET: return "ipv4";
102 case PF_UNIX: return "unix";
104 return "unknown";
107 int inet_listen_opts(QemuOpts *opts, int port_offset, Error **errp)
109 struct addrinfo ai,*res,*e;
110 const char *addr;
111 char port[33];
112 char uaddr[INET6_ADDRSTRLEN+1];
113 char uport[33];
114 int slisten, rc, to, port_min, port_max, p;
116 memset(&ai,0, sizeof(ai));
117 ai.ai_flags = AI_PASSIVE | AI_ADDRCONFIG;
118 ai.ai_family = PF_UNSPEC;
119 ai.ai_socktype = SOCK_STREAM;
121 if ((qemu_opt_get(opts, "host") == NULL) ||
122 (qemu_opt_get(opts, "port") == NULL)) {
123 error_setg(errp, "host and/or port not specified");
124 return -1;
126 pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port"));
127 addr = qemu_opt_get(opts, "host");
129 to = qemu_opt_get_number(opts, "to", 0);
130 if (qemu_opt_get_bool(opts, "ipv4", 0))
131 ai.ai_family = PF_INET;
132 if (qemu_opt_get_bool(opts, "ipv6", 0))
133 ai.ai_family = PF_INET6;
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) {
140 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
141 gai_strerror(rc));
142 return -1;
145 /* create socket + bind */
146 for (e = res; e != NULL; e = e->ai_next) {
147 getnameinfo((struct sockaddr*)e->ai_addr,e->ai_addrlen,
148 uaddr,INET6_ADDRSTRLEN,uport,32,
149 NI_NUMERICHOST | NI_NUMERICSERV);
150 slisten = qemu_socket(e->ai_family, e->ai_socktype, e->ai_protocol);
151 if (slisten < 0) {
152 if (!e->ai_next) {
153 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
155 continue;
158 socket_set_fast_reuse(slisten);
159 #ifdef IPV6_V6ONLY
160 if (e->ai_family == PF_INET6) {
161 /* listen on both ipv4 and ipv6 */
162 qemu_setsockopt(slisten, IPPROTO_IPV6, IPV6_V6ONLY, &off,
163 sizeof(off));
165 #endif
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);
171 if (bind(slisten, e->ai_addr, e->ai_addrlen) == 0) {
172 goto listen;
174 if (p == port_max) {
175 if (!e->ai_next) {
176 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
180 closesocket(slisten);
182 freeaddrinfo(res);
183 return -1;
185 listen:
186 if (listen(slisten,1) != 0) {
187 error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
188 closesocket(slisten);
189 freeaddrinfo(res);
190 return -1;
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");
197 freeaddrinfo(res);
198 return slisten;
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
209 /* Struct to store connect state for non blocking connect */
210 typedef struct ConnectState {
211 int fd;
212 struct addrinfo *addr_list;
213 struct addrinfo *current_addr;
214 NonBlockingConnectHandler *callback;
215 void *opaque;
216 } ConnectState;
218 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
219 ConnectState *connect_state, Error **errp);
221 static void wait_for_connect(void *opaque)
223 ConnectState *s = opaque;
224 int val = 0, rc = 0;
225 socklen_t valsize = sizeof(val);
226 bool in_progress;
228 qemu_set_fd_handler2(s->fd, NULL, NULL, NULL, NULL);
230 do {
231 rc = qemu_getsockopt(s->fd, SOL_SOCKET, SO_ERROR, &val, &valsize);
232 } while (rc == -1 && socket_error() == EINTR);
234 /* update rc to contain error */
235 if (!rc && val) {
236 rc = -1;
239 /* connect error */
240 if (rc < 0) {
241 closesocket(s->fd);
242 s->fd = rc;
245 /* try to connect to the next address on the list */
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;
249 s->fd = inet_connect_addr(s->current_addr, &in_progress, s, NULL);
250 /* connect in progress */
251 if (in_progress) {
252 return;
256 freeaddrinfo(s->addr_list);
259 if (s->callback) {
260 s->callback(s->fd, s->opaque);
262 g_free(s);
265 static int inet_connect_addr(struct addrinfo *addr, bool *in_progress,
266 ConnectState *connect_state, Error **errp)
268 int sock, rc;
270 *in_progress = false;
272 sock = qemu_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
273 if (sock < 0) {
274 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
275 return -1;
277 socket_set_fast_reuse(sock);
278 if (connect_state != NULL) {
279 qemu_set_nonblock(sock);
281 /* connect to peer */
282 do {
283 rc = 0;
284 if (connect(sock, addr->ai_addr, addr->ai_addrlen) < 0) {
285 rc = -socket_error();
287 } while (rc == -EINTR);
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;
294 } else if (rc < 0) {
295 error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
296 closesocket(sock);
297 return -1;
299 return sock;
302 static struct addrinfo *inet_parse_connect_opts(QemuOpts *opts, Error **errp)
304 struct addrinfo ai, *res;
305 int rc;
306 const char *addr;
307 const char *port;
309 memset(&ai, 0, sizeof(ai));
311 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
312 ai.ai_family = PF_UNSPEC;
313 ai.ai_socktype = SOCK_STREAM;
315 addr = qemu_opt_get(opts, "host");
316 port = qemu_opt_get(opts, "port");
317 if (addr == NULL || port == NULL) {
318 error_setg(errp, "host and/or port not specified");
319 return NULL;
322 if (qemu_opt_get_bool(opts, "ipv4", 0)) {
323 ai.ai_family = PF_INET;
325 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
326 ai.ai_family = PF_INET6;
329 /* lookup */
330 rc = getaddrinfo(addr, port, &ai, &res);
331 if (rc != 0) {
332 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
333 gai_strerror(rc));
334 return NULL;
336 return res;
340 * Create a socket and connect it to an address.
342 * @opts: QEMU options, recognized parameters strings "host" and "port",
343 * bools "ipv4" and "ipv6".
344 * @errp: set on error
345 * @callback: callback function for non-blocking connect
346 * @opaque: opaque for callback function
348 * Returns: -1 on error, file descriptor on success.
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.
354 int inet_connect_opts(QemuOpts *opts, Error **errp,
355 NonBlockingConnectHandler *callback, void *opaque)
357 Error *local_err = NULL;
358 struct addrinfo *res, *e;
359 int sock = -1;
360 bool in_progress;
361 ConnectState *connect_state = NULL;
363 res = inet_parse_connect_opts(opts, errp);
364 if (!res) {
365 return -1;
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;
375 for (e = res; e != NULL; e = e->ai_next) {
376 error_free(local_err);
377 local_err = NULL;
378 if (connect_state != NULL) {
379 connect_state->current_addr = e;
381 sock = inet_connect_addr(e, &in_progress, connect_state, &local_err);
382 if (sock >= 0) {
383 break;
387 if (sock < 0) {
388 error_propagate(errp, local_err);
389 } else if (in_progress) {
390 /* wait_for_connect() will do the rest */
391 return sock;
392 } else {
393 if (callback) {
394 callback(sock, opaque);
397 g_free(connect_state);
398 freeaddrinfo(res);
399 return sock;
402 int inet_dgram_opts(QemuOpts *opts, Error **errp)
404 struct addrinfo ai, *peer = NULL, *local = NULL;
405 const char *addr;
406 const char *port;
407 int sock = -1, rc;
409 /* lookup peer addr */
410 memset(&ai,0, sizeof(ai));
411 ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
412 ai.ai_family = PF_UNSPEC;
413 ai.ai_socktype = SOCK_DGRAM;
415 addr = qemu_opt_get(opts, "host");
416 port = qemu_opt_get(opts, "port");
417 if (addr == NULL || strlen(addr) == 0) {
418 addr = "localhost";
420 if (port == NULL || strlen(port) == 0) {
421 error_setg(errp, "remote port not specified");
422 return -1;
425 if (qemu_opt_get_bool(opts, "ipv4", 0))
426 ai.ai_family = PF_INET;
427 if (qemu_opt_get_bool(opts, "ipv6", 0))
428 ai.ai_family = PF_INET6;
430 if (0 != (rc = getaddrinfo(addr, port, &ai, &peer))) {
431 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
432 gai_strerror(rc));
433 return -1;
436 /* lookup local addr */
437 memset(&ai,0, sizeof(ai));
438 ai.ai_flags = AI_PASSIVE;
439 ai.ai_family = peer->ai_family;
440 ai.ai_socktype = SOCK_DGRAM;
442 addr = qemu_opt_get(opts, "localaddr");
443 port = qemu_opt_get(opts, "localport");
444 if (addr == NULL || strlen(addr) == 0) {
445 addr = NULL;
447 if (!port || strlen(port) == 0)
448 port = "0";
450 if (0 != (rc = getaddrinfo(addr, port, &ai, &local))) {
451 error_setg(errp, "address resolution failed for %s:%s: %s", addr, port,
452 gai_strerror(rc));
453 goto err;
456 /* create socket */
457 sock = qemu_socket(peer->ai_family, peer->ai_socktype, peer->ai_protocol);
458 if (sock < 0) {
459 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
460 goto err;
462 socket_set_fast_reuse(sock);
464 /* bind socket */
465 if (bind(sock, local->ai_addr, local->ai_addrlen) < 0) {
466 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
467 goto err;
470 /* connect to peer */
471 if (connect(sock,peer->ai_addr,peer->ai_addrlen) < 0) {
472 error_set_errno(errp, errno, QERR_SOCKET_CONNECT_FAILED);
473 goto err;
476 freeaddrinfo(local);
477 freeaddrinfo(peer);
478 return sock;
480 err:
481 if (-1 != sock)
482 closesocket(sock);
483 if (local)
484 freeaddrinfo(local);
485 if (peer)
486 freeaddrinfo(peer);
487 return -1;
490 /* compatibility wrapper */
491 InetSocketAddress *inet_parse(const char *str, Error **errp)
493 InetSocketAddress *addr;
494 const char *optstr, *h;
495 char host[64];
496 char port[33];
497 int to;
498 int pos;
500 addr = g_new0(InetSocketAddress, 1);
502 /* parse address */
503 if (str[0] == ':') {
504 /* no host given */
505 host[0] = '\0';
506 if (1 != sscanf(str, ":%32[^,]%n", port, &pos)) {
507 error_setg(errp, "error parsing port in address '%s'", str);
508 goto fail;
510 } else if (str[0] == '[') {
511 /* IPv6 addr */
512 if (2 != sscanf(str, "[%64[^]]]:%32[^,]%n", host, port, &pos)) {
513 error_setg(errp, "error parsing IPv6 address '%s'", str);
514 goto fail;
516 addr->ipv6 = addr->has_ipv6 = true;
517 } else {
518 /* hostname or IPv4 addr */
519 if (2 != sscanf(str, "%64[^:]:%32[^,]%n", host, port, &pos)) {
520 error_setg(errp, "error parsing address '%s'", str);
521 goto fail;
523 if (host[strspn(host, "0123456789.")] == '\0') {
524 addr->ipv4 = addr->has_ipv4 = true;
528 addr->host = g_strdup(host);
529 addr->port = g_strdup(port);
531 /* parse options */
532 optstr = str + pos;
533 h = strstr(optstr, ",to=");
534 if (h) {
535 h += 4;
536 if (sscanf(h, "%d%n", &to, &pos) != 1 ||
537 (h[pos] != '\0' && h[pos] != ',')) {
538 error_setg(errp, "error parsing to= argument");
539 goto fail;
541 addr->has_to = true;
542 addr->to = to;
544 if (strstr(optstr, ",ipv4")) {
545 addr->ipv4 = addr->has_ipv4 = true;
547 if (strstr(optstr, ",ipv6")) {
548 addr->ipv6 = addr->has_ipv6 = true;
550 return addr;
552 fail:
553 qapi_free_InetSocketAddress(addr);
554 return NULL;
557 static void inet_addr_to_opts(QemuOpts *opts, const InetSocketAddress *addr)
559 bool ipv4 = addr->ipv4 || !addr->has_ipv4;
560 bool ipv6 = addr->ipv6 || !addr->has_ipv6;
562 if (!ipv4 || !ipv6) {
563 qemu_opt_set_bool(opts, "ipv4", ipv4);
564 qemu_opt_set_bool(opts, "ipv6", ipv6);
566 if (addr->has_to) {
567 char to[20];
568 snprintf(to, sizeof(to), "%d", addr->to);
569 qemu_opt_set(opts, "to", to);
571 qemu_opt_set(opts, "host", addr->host);
572 qemu_opt_set(opts, "port", addr->port);
575 int inet_listen(const char *str, char *ostr, int olen,
576 int socktype, int port_offset, Error **errp)
578 QemuOpts *opts;
579 char *optstr;
580 int sock = -1;
581 InetSocketAddress *addr;
583 addr = inet_parse(str, errp);
584 if (addr != NULL) {
585 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
586 inet_addr_to_opts(opts, addr);
587 qapi_free_InetSocketAddress(addr);
588 sock = inet_listen_opts(opts, port_offset, errp);
589 if (sock != -1 && ostr) {
590 optstr = strchr(str, ',');
591 if (qemu_opt_get_bool(opts, "ipv6", 0)) {
592 snprintf(ostr, olen, "[%s]:%s%s",
593 qemu_opt_get(opts, "host"),
594 qemu_opt_get(opts, "port"),
595 optstr ? optstr : "");
596 } else {
597 snprintf(ostr, olen, "%s:%s%s",
598 qemu_opt_get(opts, "host"),
599 qemu_opt_get(opts, "port"),
600 optstr ? optstr : "");
603 qemu_opts_del(opts);
605 return sock;
609 * Create a blocking socket and connect it to an address.
611 * @str: address string
612 * @errp: set in case of an error
614 * Returns -1 in case of error, file descriptor on success
616 int inet_connect(const char *str, Error **errp)
618 QemuOpts *opts;
619 int sock = -1;
620 InetSocketAddress *addr;
622 addr = inet_parse(str, errp);
623 if (addr != NULL) {
624 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
625 inet_addr_to_opts(opts, addr);
626 qapi_free_InetSocketAddress(addr);
627 sock = inet_connect_opts(opts, errp, NULL, NULL);
628 qemu_opts_del(opts);
630 return sock;
634 * Create a non-blocking socket and connect it to an address.
635 * Calls the callback function with fd in case of success or -1 in case of
636 * error.
638 * @str: address string
639 * @callback: callback function that is called when connect completes,
640 * cannot be NULL.
641 * @opaque: opaque for callback function
642 * @errp: set in case of an error
644 * Returns: -1 on immediate error, file descriptor on success.
646 int inet_nonblocking_connect(const char *str,
647 NonBlockingConnectHandler *callback,
648 void *opaque, Error **errp)
650 QemuOpts *opts;
651 int sock = -1;
652 InetSocketAddress *addr;
654 g_assert(callback != NULL);
656 addr = inet_parse(str, errp);
657 if (addr != NULL) {
658 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
659 inet_addr_to_opts(opts, addr);
660 qapi_free_InetSocketAddress(addr);
661 sock = inet_connect_opts(opts, errp, callback, opaque);
662 qemu_opts_del(opts);
664 return sock;
667 #ifndef _WIN32
669 int unix_listen_opts(QemuOpts *opts, Error **errp)
671 struct sockaddr_un un;
672 const char *path = qemu_opt_get(opts, "path");
673 int sock, fd;
675 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
676 if (sock < 0) {
677 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
678 return -1;
681 memset(&un, 0, sizeof(un));
682 un.sun_family = AF_UNIX;
683 if (path && strlen(path)) {
684 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
685 } else {
686 char *tmpdir = getenv("TMPDIR");
687 snprintf(un.sun_path, sizeof(un.sun_path), "%s/qemu-socket-XXXXXX",
688 tmpdir ? tmpdir : "/tmp");
690 * This dummy fd usage silences the mktemp() unsecure warning.
691 * Using mkstemp() doesn't make things more secure here
692 * though. bind() complains about existing files, so we have
693 * to unlink first and thus re-open the race window. The
694 * worst case possible is bind() failing, i.e. a DoS attack.
696 fd = mkstemp(un.sun_path); close(fd);
697 qemu_opt_set(opts, "path", un.sun_path);
700 unlink(un.sun_path);
701 if (bind(sock, (struct sockaddr*) &un, sizeof(un)) < 0) {
702 error_set_errno(errp, errno, QERR_SOCKET_BIND_FAILED);
703 goto err;
705 if (listen(sock, 1) < 0) {
706 error_set_errno(errp, errno, QERR_SOCKET_LISTEN_FAILED);
707 goto err;
710 return sock;
712 err:
713 closesocket(sock);
714 return -1;
717 int unix_connect_opts(QemuOpts *opts, Error **errp,
718 NonBlockingConnectHandler *callback, void *opaque)
720 struct sockaddr_un un;
721 const char *path = qemu_opt_get(opts, "path");
722 ConnectState *connect_state = NULL;
723 int sock, rc;
725 if (NULL == path) {
726 error_setg(errp, "unix connect: no path specified");
727 return -1;
730 sock = qemu_socket(PF_UNIX, SOCK_STREAM, 0);
731 if (sock < 0) {
732 error_set_errno(errp, errno, QERR_SOCKET_CREATE_FAILED);
733 return -1;
735 if (callback != NULL) {
736 connect_state = g_malloc0(sizeof(*connect_state));
737 connect_state->callback = callback;
738 connect_state->opaque = opaque;
739 qemu_set_nonblock(sock);
742 memset(&un, 0, sizeof(un));
743 un.sun_family = AF_UNIX;
744 snprintf(un.sun_path, sizeof(un.sun_path), "%s", path);
746 /* connect to peer */
747 do {
748 rc = 0;
749 if (connect(sock, (struct sockaddr *) &un, sizeof(un)) < 0) {
750 rc = -socket_error();
752 } while (rc == -EINTR);
754 if (connect_state != NULL && QEMU_SOCKET_RC_INPROGRESS(rc)) {
755 connect_state->fd = sock;
756 qemu_set_fd_handler2(sock, NULL, NULL, wait_for_connect,
757 connect_state);
758 return sock;
759 } else if (rc >= 0) {
760 /* non blocking socket immediate success, call callback */
761 if (callback != NULL) {
762 callback(sock, opaque);
766 if (rc < 0) {
767 error_set_errno(errp, -rc, QERR_SOCKET_CONNECT_FAILED);
768 close(sock);
769 sock = -1;
772 g_free(connect_state);
773 return sock;
776 #else
778 int unix_listen_opts(QemuOpts *opts, Error **errp)
780 error_setg(errp, "unix sockets are not available on windows");
781 errno = ENOTSUP;
782 return -1;
785 int unix_connect_opts(QemuOpts *opts, Error **errp,
786 NonBlockingConnectHandler *callback, void *opaque)
788 error_setg(errp, "unix sockets are not available on windows");
789 errno = ENOTSUP;
790 return -1;
792 #endif
794 /* compatibility wrapper */
795 int unix_listen(const char *str, char *ostr, int olen, Error **errp)
797 QemuOpts *opts;
798 char *path, *optstr;
799 int sock, len;
801 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
803 optstr = strchr(str, ',');
804 if (optstr) {
805 len = optstr - str;
806 if (len) {
807 path = g_malloc(len+1);
808 snprintf(path, len+1, "%.*s", len, str);
809 qemu_opt_set(opts, "path", path);
810 g_free(path);
812 } else {
813 qemu_opt_set(opts, "path", str);
816 sock = unix_listen_opts(opts, errp);
818 if (sock != -1 && ostr)
819 snprintf(ostr, olen, "%s%s", qemu_opt_get(opts, "path"), optstr ? optstr : "");
820 qemu_opts_del(opts);
821 return sock;
824 int unix_connect(const char *path, Error **errp)
826 QemuOpts *opts;
827 int sock;
829 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
830 qemu_opt_set(opts, "path", path);
831 sock = unix_connect_opts(opts, errp, NULL, NULL);
832 qemu_opts_del(opts);
833 return sock;
837 int unix_nonblocking_connect(const char *path,
838 NonBlockingConnectHandler *callback,
839 void *opaque, Error **errp)
841 QemuOpts *opts;
842 int sock = -1;
844 g_assert(callback != NULL);
846 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
847 qemu_opt_set(opts, "path", path);
848 sock = unix_connect_opts(opts, errp, callback, opaque);
849 qemu_opts_del(opts);
850 return sock;
853 SocketAddress *socket_parse(const char *str, Error **errp)
855 SocketAddress *addr;
857 addr = g_new0(SocketAddress, 1);
858 if (strstart(str, "unix:", NULL)) {
859 if (str[5] == '\0') {
860 error_setg(errp, "invalid Unix socket address");
861 goto fail;
862 } else {
863 addr->kind = SOCKET_ADDRESS_KIND_UNIX;
864 addr->q_unix = g_new(UnixSocketAddress, 1);
865 addr->q_unix->path = g_strdup(str + 5);
867 } else if (strstart(str, "fd:", NULL)) {
868 if (str[3] == '\0') {
869 error_setg(errp, "invalid file descriptor address");
870 goto fail;
871 } else {
872 addr->kind = SOCKET_ADDRESS_KIND_FD;
873 addr->fd = g_new(String, 1);
874 addr->fd->str = g_strdup(str + 3);
876 } else {
877 addr->kind = SOCKET_ADDRESS_KIND_INET;
878 addr->inet = inet_parse(str, errp);
879 if (addr->inet == NULL) {
880 goto fail;
883 return addr;
885 fail:
886 qapi_free_SocketAddress(addr);
887 return NULL;
890 int socket_connect(SocketAddress *addr, Error **errp,
891 NonBlockingConnectHandler *callback, void *opaque)
893 QemuOpts *opts;
894 int fd;
896 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
897 switch (addr->kind) {
898 case SOCKET_ADDRESS_KIND_INET:
899 inet_addr_to_opts(opts, addr->inet);
900 fd = inet_connect_opts(opts, errp, callback, opaque);
901 break;
903 case SOCKET_ADDRESS_KIND_UNIX:
904 qemu_opt_set(opts, "path", addr->q_unix->path);
905 fd = unix_connect_opts(opts, errp, callback, opaque);
906 break;
908 case SOCKET_ADDRESS_KIND_FD:
909 fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
910 if (fd >= 0 && callback) {
911 qemu_set_nonblock(fd);
912 callback(fd, opaque);
914 break;
916 default:
917 abort();
919 qemu_opts_del(opts);
920 return fd;
923 int socket_listen(SocketAddress *addr, Error **errp)
925 QemuOpts *opts;
926 int fd;
928 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
929 switch (addr->kind) {
930 case SOCKET_ADDRESS_KIND_INET:
931 inet_addr_to_opts(opts, addr->inet);
932 fd = inet_listen_opts(opts, 0, errp);
933 break;
935 case SOCKET_ADDRESS_KIND_UNIX:
936 qemu_opt_set(opts, "path", addr->q_unix->path);
937 fd = unix_listen_opts(opts, errp);
938 break;
940 case SOCKET_ADDRESS_KIND_FD:
941 fd = monitor_get_fd(cur_mon, addr->fd->str, errp);
942 break;
944 default:
945 abort();
947 qemu_opts_del(opts);
948 return fd;
951 int socket_dgram(SocketAddress *remote, SocketAddress *local, Error **errp)
953 QemuOpts *opts;
954 int fd;
956 opts = qemu_opts_create(&socket_optslist, NULL, 0, &error_abort);
957 switch (remote->kind) {
958 case SOCKET_ADDRESS_KIND_INET:
959 qemu_opt_set(opts, "host", remote->inet->host);
960 qemu_opt_set(opts, "port", remote->inet->port);
961 if (local) {
962 qemu_opt_set(opts, "localaddr", local->inet->host);
963 qemu_opt_set(opts, "localport", local->inet->port);
965 fd = inet_dgram_opts(opts, errp);
966 break;
968 default:
969 error_setg(errp, "socket type unsupported for datagram");
970 fd = -1;
972 qemu_opts_del(opts);
973 return fd;