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.
25 #include "monitor/monitor.h"
26 #include "qemu/sockets.h"
27 #include "qemu/main-loop.h"
30 # define AI_ADDRCONFIG 0
33 /* used temporarily until all users are converted to QemuOpts */
34 QemuOptsList socket_optslist
= {
36 .head
= QTAILQ_HEAD_INITIALIZER(socket_optslist
.head
),
40 .type
= QEMU_OPT_STRING
,
43 .type
= QEMU_OPT_STRING
,
46 .type
= QEMU_OPT_STRING
,
49 .type
= QEMU_OPT_STRING
,
52 .type
= QEMU_OPT_STRING
,
55 .type
= QEMU_OPT_NUMBER
,
58 .type
= QEMU_OPT_BOOL
,
61 .type
= QEMU_OPT_BOOL
,
67 static int inet_getport(struct addrinfo
*e
)
69 struct sockaddr_in
*i4
;
70 struct sockaddr_in6
*i6
;
72 switch (e
->ai_family
) {
74 i6
= (void*)e
->ai_addr
;
75 return ntohs(i6
->sin6_port
);
77 i4
= (void*)e
->ai_addr
;
78 return ntohs(i4
->sin_port
);
84 static void inet_setport(struct addrinfo
*e
, int port
)
86 struct sockaddr_in
*i4
;
87 struct sockaddr_in6
*i6
;
89 switch (e
->ai_family
) {
91 i6
= (void*)e
->ai_addr
;
92 i6
->sin6_port
= htons(port
);
95 i4
= (void*)e
->ai_addr
;
96 i4
->sin_port
= htons(port
);
101 NetworkAddressFamily
inet_netfamily(int family
)
104 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
105 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
106 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
108 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
111 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
113 struct addrinfo ai
,*res
,*e
;
116 char uaddr
[INET6_ADDRSTRLEN
+1];
118 int slisten
, rc
, to
, port_min
, port_max
, p
;
120 memset(&ai
,0, sizeof(ai
));
121 ai
.ai_flags
= AI_PASSIVE
| AI_ADDRCONFIG
;
122 ai
.ai_family
= PF_UNSPEC
;
123 ai
.ai_socktype
= SOCK_STREAM
;
125 if ((qemu_opt_get(opts
, "host") == NULL
) ||
126 (qemu_opt_get(opts
, "port") == NULL
)) {
127 error_setg(errp
, "host and/or port not specified");
130 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
131 addr
= qemu_opt_get(opts
, "host");
133 to
= qemu_opt_get_number(opts
, "to", 0);
134 if (qemu_opt_get_bool(opts
, "ipv4", 0))
135 ai
.ai_family
= PF_INET
;
136 if (qemu_opt_get_bool(opts
, "ipv6", 0))
137 ai
.ai_family
= PF_INET6
;
141 unsigned long long baseport
;
142 if (parse_uint_full(port
, &baseport
, 10) < 0) {
143 error_setg(errp
, "can't convert to a number: %s", port
);
146 if (baseport
> 65535 ||
147 baseport
+ port_offset
> 65535) {
148 error_setg(errp
, "port %s out of range", port
);
151 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
153 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
155 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
160 /* create socket + bind */
161 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
162 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
163 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
164 NI_NUMERICHOST
| NI_NUMERICSERV
);
165 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
168 error_setg_errno(errp
, errno
, "Failed to create socket");
173 socket_set_fast_reuse(slisten
);
175 if (e
->ai_family
== PF_INET6
) {
176 /* listen on both ipv4 and ipv6 */
178 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
183 port_min
= inet_getport(e
);
184 port_max
= to
? to
+ port_offset
: port_min
;
185 for (p
= port_min
; p
<= port_max
; p
++) {
187 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
192 error_setg_errno(errp
, errno
, "Failed to bind socket");
196 closesocket(slisten
);
202 if (listen(slisten
,1) != 0) {
203 error_setg_errno(errp
, errno
, "Failed to listen on socket");
204 closesocket(slisten
);
208 qemu_opt_set(opts
, "host", uaddr
, &error_abort
);
209 qemu_opt_set_number(opts
, "port", inet_getport(e
) - port_offset
,
211 qemu_opt_set_bool(opts
, "ipv6", e
->ai_family
== PF_INET6
,
213 qemu_opt_set_bool(opts
, "ipv4", e
->ai_family
!= PF_INET6
,
220 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
221 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
223 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
224 ((rc) == -EINPROGRESS)
227 /* Struct to store connect state for non blocking connect */
228 typedef struct ConnectState
{
230 struct addrinfo
*addr_list
;
231 struct addrinfo
*current_addr
;
232 NonBlockingConnectHandler
*callback
;
236 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
237 ConnectState
*connect_state
, Error
**errp
);
239 static void wait_for_connect(void *opaque
)
241 ConnectState
*s
= opaque
;
243 socklen_t valsize
= sizeof(val
);
247 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
250 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
251 } while (rc
== -1 && socket_error() == EINTR
);
253 /* update rc to contain error */
261 error_setg_errno(&err
, errno
, "Error connecting to socket");
266 /* try to connect to the next address on the list */
267 if (s
->current_addr
) {
268 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
269 s
->current_addr
= s
->current_addr
->ai_next
;
270 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
274 error_setg_errno(&err
, errno
, "Unable to start socket connect");
276 /* connect in progress */
282 freeaddrinfo(s
->addr_list
);
286 s
->callback(s
->fd
, err
, s
->opaque
);
293 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
294 ConnectState
*connect_state
, Error
**errp
)
298 *in_progress
= false;
300 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
302 error_setg_errno(errp
, errno
, "Failed to create socket");
305 socket_set_fast_reuse(sock
);
306 if (connect_state
!= NULL
) {
307 qemu_set_nonblock(sock
);
309 /* connect to peer */
312 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
313 rc
= -socket_error();
315 } while (rc
== -EINTR
);
317 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
318 connect_state
->fd
= sock
;
319 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
322 error_setg_errno(errp
, errno
, "Failed to connect socket");
329 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
331 struct addrinfo ai
, *res
;
336 memset(&ai
, 0, sizeof(ai
));
338 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
339 ai
.ai_family
= PF_UNSPEC
;
340 ai
.ai_socktype
= SOCK_STREAM
;
342 addr
= qemu_opt_get(opts
, "host");
343 port
= qemu_opt_get(opts
, "port");
344 if (addr
== NULL
|| port
== NULL
) {
345 error_setg(errp
, "host and/or port not specified");
349 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
350 ai
.ai_family
= PF_INET
;
352 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
353 ai
.ai_family
= PF_INET6
;
357 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
359 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
367 * Create a socket and connect it to an address.
369 * @opts: QEMU options, recognized parameters strings "host" and "port",
370 * bools "ipv4" and "ipv6".
371 * @errp: set on error
372 * @callback: callback function for non-blocking connect
373 * @opaque: opaque for callback function
375 * Returns: -1 on error, file descriptor on success.
377 * If @callback is non-null, the connect is non-blocking. If this
378 * function succeeds, callback will be called when the connection
379 * completes, with the file descriptor on success, or -1 on error.
381 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
,
382 NonBlockingConnectHandler
*callback
, void *opaque
)
384 Error
*local_err
= NULL
;
385 struct addrinfo
*res
, *e
;
388 ConnectState
*connect_state
= NULL
;
390 res
= inet_parse_connect_opts(opts
, errp
);
395 if (callback
!= NULL
) {
396 connect_state
= g_malloc0(sizeof(*connect_state
));
397 connect_state
->addr_list
= res
;
398 connect_state
->callback
= callback
;
399 connect_state
->opaque
= opaque
;
402 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
403 error_free(local_err
);
405 if (connect_state
!= NULL
) {
406 connect_state
->current_addr
= e
;
408 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
415 error_propagate(errp
, local_err
);
416 } else if (in_progress
) {
417 /* wait_for_connect() will do the rest */
421 callback(sock
, NULL
, opaque
);
424 g_free(connect_state
);
429 int inet_dgram_opts(QemuOpts
*opts
, Error
**errp
)
431 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
436 /* lookup peer addr */
437 memset(&ai
,0, sizeof(ai
));
438 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
439 ai
.ai_family
= PF_UNSPEC
;
440 ai
.ai_socktype
= SOCK_DGRAM
;
442 addr
= qemu_opt_get(opts
, "host");
443 port
= qemu_opt_get(opts
, "port");
444 if (addr
== NULL
|| strlen(addr
) == 0) {
447 if (port
== NULL
|| strlen(port
) == 0) {
448 error_setg(errp
, "remote port not specified");
452 if (qemu_opt_get_bool(opts
, "ipv4", 0))
453 ai
.ai_family
= PF_INET
;
454 if (qemu_opt_get_bool(opts
, "ipv6", 0))
455 ai
.ai_family
= PF_INET6
;
457 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
458 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
463 /* lookup local addr */
464 memset(&ai
,0, sizeof(ai
));
465 ai
.ai_flags
= AI_PASSIVE
;
466 ai
.ai_family
= peer
->ai_family
;
467 ai
.ai_socktype
= SOCK_DGRAM
;
469 addr
= qemu_opt_get(opts
, "localaddr");
470 port
= qemu_opt_get(opts
, "localport");
471 if (addr
== NULL
|| strlen(addr
) == 0) {
474 if (!port
|| strlen(port
) == 0)
477 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
478 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
484 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
486 error_setg_errno(errp
, errno
, "Failed to create socket");
489 socket_set_fast_reuse(sock
);
492 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
493 error_setg_errno(errp
, errno
, "Failed to bind socket");
497 /* connect to peer */
498 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
499 error_setg_errno(errp
, errno
, "Failed to connect socket");
517 /* compatibility wrapper */
518 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
520 InetSocketAddress
*addr
;
521 const char *optstr
, *h
;
527 addr
= g_new0(InetSocketAddress
, 1);
533 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
534 error_setg(errp
, "error parsing port in address '%s'", str
);
537 } else if (str
[0] == '[') {
539 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
540 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
543 addr
->ipv6
= addr
->has_ipv6
= true;
545 /* hostname or IPv4 addr */
546 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
547 error_setg(errp
, "error parsing address '%s'", str
);
550 if (host
[strspn(host
, "0123456789.")] == '\0') {
551 addr
->ipv4
= addr
->has_ipv4
= true;
555 addr
->host
= g_strdup(host
);
556 addr
->port
= g_strdup(port
);
560 h
= strstr(optstr
, ",to=");
563 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
564 (h
[pos
] != '\0' && h
[pos
] != ',')) {
565 error_setg(errp
, "error parsing to= argument");
571 if (strstr(optstr
, ",ipv4")) {
572 addr
->ipv4
= addr
->has_ipv4
= true;
574 if (strstr(optstr
, ",ipv6")) {
575 addr
->ipv6
= addr
->has_ipv6
= true;
580 qapi_free_InetSocketAddress(addr
);
584 static void inet_addr_to_opts(QemuOpts
*opts
, const InetSocketAddress
*addr
)
586 bool ipv4
= addr
->ipv4
|| !addr
->has_ipv4
;
587 bool ipv6
= addr
->ipv6
|| !addr
->has_ipv6
;
589 if (!ipv4
|| !ipv6
) {
590 qemu_opt_set_bool(opts
, "ipv4", ipv4
, &error_abort
);
591 qemu_opt_set_bool(opts
, "ipv6", ipv6
, &error_abort
);
594 qemu_opt_set_number(opts
, "to", addr
->to
, &error_abort
);
596 qemu_opt_set(opts
, "host", addr
->host
, &error_abort
);
597 qemu_opt_set(opts
, "port", addr
->port
, &error_abort
);
600 int inet_listen(const char *str
, char *ostr
, int olen
,
601 int socktype
, int port_offset
, Error
**errp
)
606 InetSocketAddress
*addr
;
608 addr
= inet_parse(str
, errp
);
610 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
611 inet_addr_to_opts(opts
, addr
);
612 qapi_free_InetSocketAddress(addr
);
613 sock
= inet_listen_opts(opts
, port_offset
, errp
);
614 if (sock
!= -1 && ostr
) {
615 optstr
= strchr(str
, ',');
616 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
617 snprintf(ostr
, olen
, "[%s]:%s%s",
618 qemu_opt_get(opts
, "host"),
619 qemu_opt_get(opts
, "port"),
620 optstr
? optstr
: "");
622 snprintf(ostr
, olen
, "%s:%s%s",
623 qemu_opt_get(opts
, "host"),
624 qemu_opt_get(opts
, "port"),
625 optstr
? optstr
: "");
634 * Create a blocking socket and connect it to an address.
636 * @str: address string
637 * @errp: set in case of an error
639 * Returns -1 in case of error, file descriptor on success
641 int inet_connect(const char *str
, Error
**errp
)
645 InetSocketAddress
*addr
;
647 addr
= inet_parse(str
, errp
);
649 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
650 inet_addr_to_opts(opts
, addr
);
651 qapi_free_InetSocketAddress(addr
);
652 sock
= inet_connect_opts(opts
, errp
, NULL
, NULL
);
659 * Create a non-blocking socket and connect it to an address.
660 * Calls the callback function with fd in case of success or -1 in case of
663 * @str: address string
664 * @callback: callback function that is called when connect completes,
666 * @opaque: opaque for callback function
667 * @errp: set in case of an error
669 * Returns: -1 on immediate error, file descriptor on success.
671 int inet_nonblocking_connect(const char *str
,
672 NonBlockingConnectHandler
*callback
,
673 void *opaque
, Error
**errp
)
677 InetSocketAddress
*addr
;
679 g_assert(callback
!= NULL
);
681 addr
= inet_parse(str
, errp
);
683 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
684 inet_addr_to_opts(opts
, addr
);
685 qapi_free_InetSocketAddress(addr
);
686 sock
= inet_connect_opts(opts
, errp
, callback
, opaque
);
694 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
696 struct sockaddr_un un
;
697 const char *path
= qemu_opt_get(opts
, "path");
700 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
702 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
706 memset(&un
, 0, sizeof(un
));
707 un
.sun_family
= AF_UNIX
;
708 if (path
&& strlen(path
)) {
709 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
711 const char *tmpdir
= getenv("TMPDIR");
712 tmpdir
= tmpdir
? tmpdir
: "/tmp";
713 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
714 tmpdir
) >= sizeof(un
.sun_path
)) {
715 error_setg_errno(errp
, errno
,
716 "TMPDIR environment variable (%s) too large", tmpdir
);
721 * This dummy fd usage silences the mktemp() unsecure warning.
722 * Using mkstemp() doesn't make things more secure here
723 * though. bind() complains about existing files, so we have
724 * to unlink first and thus re-open the race window. The
725 * worst case possible is bind() failing, i.e. a DoS attack.
727 fd
= mkstemp(un
.sun_path
);
729 error_setg_errno(errp
, errno
,
730 "Failed to make a temporary socket name in %s", tmpdir
);
734 qemu_opt_set(opts
, "path", un
.sun_path
, &error_abort
);
737 if ((access(un
.sun_path
, F_OK
) == 0) &&
738 unlink(un
.sun_path
) < 0) {
739 error_setg_errno(errp
, errno
,
740 "Failed to unlink socket %s", un
.sun_path
);
743 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
744 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
747 if (listen(sock
, 1) < 0) {
748 error_setg_errno(errp
, errno
, "Failed to listen on socket");
759 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
760 NonBlockingConnectHandler
*callback
, void *opaque
)
762 struct sockaddr_un un
;
763 const char *path
= qemu_opt_get(opts
, "path");
764 ConnectState
*connect_state
= NULL
;
768 error_setg(errp
, "unix connect: no path specified");
772 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
774 error_setg_errno(errp
, errno
, "Failed to create socket");
777 if (callback
!= NULL
) {
778 connect_state
= g_malloc0(sizeof(*connect_state
));
779 connect_state
->callback
= callback
;
780 connect_state
->opaque
= opaque
;
781 qemu_set_nonblock(sock
);
784 memset(&un
, 0, sizeof(un
));
785 un
.sun_family
= AF_UNIX
;
786 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
788 /* connect to peer */
791 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
792 rc
= -socket_error();
794 } while (rc
== -EINTR
);
796 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
797 connect_state
->fd
= sock
;
798 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
800 } else if (rc
>= 0) {
801 /* non blocking socket immediate success, call callback */
802 if (callback
!= NULL
) {
803 callback(sock
, NULL
, opaque
);
808 error_setg_errno(errp
, -rc
, "Failed to connect socket");
813 g_free(connect_state
);
819 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
821 error_setg(errp
, "unix sockets are not available on windows");
826 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
827 NonBlockingConnectHandler
*callback
, void *opaque
)
829 error_setg(errp
, "unix sockets are not available on windows");
835 /* compatibility wrapper */
836 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
842 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
844 optstr
= strchr(str
, ',');
848 path
= g_malloc(len
+1);
849 snprintf(path
, len
+1, "%.*s", len
, str
);
850 qemu_opt_set(opts
, "path", path
, &error_abort
);
854 qemu_opt_set(opts
, "path", str
, &error_abort
);
857 sock
= unix_listen_opts(opts
, errp
);
859 if (sock
!= -1 && ostr
)
860 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
865 int unix_connect(const char *path
, Error
**errp
)
870 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
871 qemu_opt_set(opts
, "path", path
, &error_abort
);
872 sock
= unix_connect_opts(opts
, errp
, NULL
, NULL
);
878 int unix_nonblocking_connect(const char *path
,
879 NonBlockingConnectHandler
*callback
,
880 void *opaque
, Error
**errp
)
885 g_assert(callback
!= NULL
);
887 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
888 qemu_opt_set(opts
, "path", path
, &error_abort
);
889 sock
= unix_connect_opts(opts
, errp
, callback
, opaque
);
894 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
898 addr
= g_new0(SocketAddress
, 1);
899 if (strstart(str
, "unix:", NULL
)) {
900 if (str
[5] == '\0') {
901 error_setg(errp
, "invalid Unix socket address");
904 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
905 addr
->q_unix
= g_new(UnixSocketAddress
, 1);
906 addr
->q_unix
->path
= g_strdup(str
+ 5);
908 } else if (strstart(str
, "fd:", NULL
)) {
909 if (str
[3] == '\0') {
910 error_setg(errp
, "invalid file descriptor address");
913 addr
->kind
= SOCKET_ADDRESS_KIND_FD
;
914 addr
->fd
= g_new(String
, 1);
915 addr
->fd
->str
= g_strdup(str
+ 3);
918 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
919 addr
->inet
= inet_parse(str
, errp
);
920 if (addr
->inet
== NULL
) {
927 qapi_free_SocketAddress(addr
);
931 int socket_connect(SocketAddress
*addr
, Error
**errp
,
932 NonBlockingConnectHandler
*callback
, void *opaque
)
937 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
938 switch (addr
->kind
) {
939 case SOCKET_ADDRESS_KIND_INET
:
940 inet_addr_to_opts(opts
, addr
->inet
);
941 fd
= inet_connect_opts(opts
, errp
, callback
, opaque
);
944 case SOCKET_ADDRESS_KIND_UNIX
:
945 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
946 fd
= unix_connect_opts(opts
, errp
, callback
, opaque
);
949 case SOCKET_ADDRESS_KIND_FD
:
950 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
951 if (fd
>= 0 && callback
) {
952 qemu_set_nonblock(fd
);
953 callback(fd
, NULL
, opaque
);
964 int socket_listen(SocketAddress
*addr
, Error
**errp
)
969 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
970 switch (addr
->kind
) {
971 case SOCKET_ADDRESS_KIND_INET
:
972 inet_addr_to_opts(opts
, addr
->inet
);
973 fd
= inet_listen_opts(opts
, 0, errp
);
976 case SOCKET_ADDRESS_KIND_UNIX
:
977 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
978 fd
= unix_listen_opts(opts
, errp
);
981 case SOCKET_ADDRESS_KIND_FD
:
982 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
992 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
997 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
998 switch (remote
->kind
) {
999 case SOCKET_ADDRESS_KIND_INET
:
1000 inet_addr_to_opts(opts
, remote
->inet
);
1002 qemu_opt_set(opts
, "localaddr", local
->inet
->host
, &error_abort
);
1003 qemu_opt_set(opts
, "localport", local
->inet
->port
, &error_abort
);
1005 fd
= inet_dgram_opts(opts
, errp
);
1009 error_setg(errp
, "socket type unsupported for datagram");
1012 qemu_opts_del(opts
);