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_handler2(s
->fd
, NULL
, 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_handler2(sock
, NULL
, NULL
, wait_for_connect
,
323 error_setg_errno(errp
, errno
, "Failed to connect socket");
330 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
332 struct addrinfo ai
, *res
;
337 memset(&ai
, 0, sizeof(ai
));
339 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
340 ai
.ai_family
= PF_UNSPEC
;
341 ai
.ai_socktype
= SOCK_STREAM
;
343 addr
= qemu_opt_get(opts
, "host");
344 port
= qemu_opt_get(opts
, "port");
345 if (addr
== NULL
|| port
== NULL
) {
346 error_setg(errp
, "host and/or port not specified");
350 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
351 ai
.ai_family
= PF_INET
;
353 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
354 ai
.ai_family
= PF_INET6
;
358 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
360 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
368 * Create a socket and connect it to an address.
370 * @opts: QEMU options, recognized parameters strings "host" and "port",
371 * bools "ipv4" and "ipv6".
372 * @errp: set on error
373 * @callback: callback function for non-blocking connect
374 * @opaque: opaque for callback function
376 * Returns: -1 on error, file descriptor on success.
378 * If @callback is non-null, the connect is non-blocking. If this
379 * function succeeds, callback will be called when the connection
380 * completes, with the file descriptor on success, or -1 on error.
382 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
,
383 NonBlockingConnectHandler
*callback
, void *opaque
)
385 Error
*local_err
= NULL
;
386 struct addrinfo
*res
, *e
;
389 ConnectState
*connect_state
= NULL
;
391 res
= inet_parse_connect_opts(opts
, errp
);
396 if (callback
!= NULL
) {
397 connect_state
= g_malloc0(sizeof(*connect_state
));
398 connect_state
->addr_list
= res
;
399 connect_state
->callback
= callback
;
400 connect_state
->opaque
= opaque
;
403 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
404 error_free(local_err
);
406 if (connect_state
!= NULL
) {
407 connect_state
->current_addr
= e
;
409 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
416 error_propagate(errp
, local_err
);
417 } else if (in_progress
) {
418 /* wait_for_connect() will do the rest */
422 callback(sock
, NULL
, opaque
);
425 g_free(connect_state
);
430 int inet_dgram_opts(QemuOpts
*opts
, Error
**errp
)
432 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
437 /* lookup peer addr */
438 memset(&ai
,0, sizeof(ai
));
439 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
440 ai
.ai_family
= PF_UNSPEC
;
441 ai
.ai_socktype
= SOCK_DGRAM
;
443 addr
= qemu_opt_get(opts
, "host");
444 port
= qemu_opt_get(opts
, "port");
445 if (addr
== NULL
|| strlen(addr
) == 0) {
448 if (port
== NULL
|| strlen(port
) == 0) {
449 error_setg(errp
, "remote port not specified");
453 if (qemu_opt_get_bool(opts
, "ipv4", 0))
454 ai
.ai_family
= PF_INET
;
455 if (qemu_opt_get_bool(opts
, "ipv6", 0))
456 ai
.ai_family
= PF_INET6
;
458 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
459 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
464 /* lookup local addr */
465 memset(&ai
,0, sizeof(ai
));
466 ai
.ai_flags
= AI_PASSIVE
;
467 ai
.ai_family
= peer
->ai_family
;
468 ai
.ai_socktype
= SOCK_DGRAM
;
470 addr
= qemu_opt_get(opts
, "localaddr");
471 port
= qemu_opt_get(opts
, "localport");
472 if (addr
== NULL
|| strlen(addr
) == 0) {
475 if (!port
|| strlen(port
) == 0)
478 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
479 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
485 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
487 error_setg_errno(errp
, errno
, "Failed to create socket");
490 socket_set_fast_reuse(sock
);
493 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
494 error_setg_errno(errp
, errno
, "Failed to bind socket");
498 /* connect to peer */
499 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
500 error_setg_errno(errp
, errno
, "Failed to connect socket");
518 /* compatibility wrapper */
519 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
521 InetSocketAddress
*addr
;
522 const char *optstr
, *h
;
528 addr
= g_new0(InetSocketAddress
, 1);
534 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
535 error_setg(errp
, "error parsing port in address '%s'", str
);
538 } else if (str
[0] == '[') {
540 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
541 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
544 addr
->ipv6
= addr
->has_ipv6
= true;
546 /* hostname or IPv4 addr */
547 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
548 error_setg(errp
, "error parsing address '%s'", str
);
551 if (host
[strspn(host
, "0123456789.")] == '\0') {
552 addr
->ipv4
= addr
->has_ipv4
= true;
556 addr
->host
= g_strdup(host
);
557 addr
->port
= g_strdup(port
);
561 h
= strstr(optstr
, ",to=");
564 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
565 (h
[pos
] != '\0' && h
[pos
] != ',')) {
566 error_setg(errp
, "error parsing to= argument");
572 if (strstr(optstr
, ",ipv4")) {
573 addr
->ipv4
= addr
->has_ipv4
= true;
575 if (strstr(optstr
, ",ipv6")) {
576 addr
->ipv6
= addr
->has_ipv6
= true;
581 qapi_free_InetSocketAddress(addr
);
585 static void inet_addr_to_opts(QemuOpts
*opts
, const InetSocketAddress
*addr
)
587 bool ipv4
= addr
->ipv4
|| !addr
->has_ipv4
;
588 bool ipv6
= addr
->ipv6
|| !addr
->has_ipv6
;
590 if (!ipv4
|| !ipv6
) {
591 qemu_opt_set_bool(opts
, "ipv4", ipv4
, &error_abort
);
592 qemu_opt_set_bool(opts
, "ipv6", ipv6
, &error_abort
);
595 qemu_opt_set_number(opts
, "to", addr
->to
, &error_abort
);
597 qemu_opt_set(opts
, "host", addr
->host
, &error_abort
);
598 qemu_opt_set(opts
, "port", addr
->port
, &error_abort
);
601 int inet_listen(const char *str
, char *ostr
, int olen
,
602 int socktype
, int port_offset
, Error
**errp
)
607 InetSocketAddress
*addr
;
609 addr
= inet_parse(str
, errp
);
611 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
612 inet_addr_to_opts(opts
, addr
);
613 qapi_free_InetSocketAddress(addr
);
614 sock
= inet_listen_opts(opts
, port_offset
, errp
);
615 if (sock
!= -1 && ostr
) {
616 optstr
= strchr(str
, ',');
617 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
618 snprintf(ostr
, olen
, "[%s]:%s%s",
619 qemu_opt_get(opts
, "host"),
620 qemu_opt_get(opts
, "port"),
621 optstr
? optstr
: "");
623 snprintf(ostr
, olen
, "%s:%s%s",
624 qemu_opt_get(opts
, "host"),
625 qemu_opt_get(opts
, "port"),
626 optstr
? optstr
: "");
635 * Create a blocking socket and connect it to an address.
637 * @str: address string
638 * @errp: set in case of an error
640 * Returns -1 in case of error, file descriptor on success
642 int inet_connect(const char *str
, Error
**errp
)
646 InetSocketAddress
*addr
;
648 addr
= inet_parse(str
, errp
);
650 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
651 inet_addr_to_opts(opts
, addr
);
652 qapi_free_InetSocketAddress(addr
);
653 sock
= inet_connect_opts(opts
, errp
, NULL
, NULL
);
660 * Create a non-blocking socket and connect it to an address.
661 * Calls the callback function with fd in case of success or -1 in case of
664 * @str: address string
665 * @callback: callback function that is called when connect completes,
667 * @opaque: opaque for callback function
668 * @errp: set in case of an error
670 * Returns: -1 on immediate error, file descriptor on success.
672 int inet_nonblocking_connect(const char *str
,
673 NonBlockingConnectHandler
*callback
,
674 void *opaque
, Error
**errp
)
678 InetSocketAddress
*addr
;
680 g_assert(callback
!= NULL
);
682 addr
= inet_parse(str
, errp
);
684 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
685 inet_addr_to_opts(opts
, addr
);
686 qapi_free_InetSocketAddress(addr
);
687 sock
= inet_connect_opts(opts
, errp
, callback
, opaque
);
695 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
697 struct sockaddr_un un
;
698 const char *path
= qemu_opt_get(opts
, "path");
701 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
703 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
707 memset(&un
, 0, sizeof(un
));
708 un
.sun_family
= AF_UNIX
;
709 if (path
&& strlen(path
)) {
710 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
712 const char *tmpdir
= getenv("TMPDIR");
713 tmpdir
= tmpdir
? tmpdir
: "/tmp";
714 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
715 tmpdir
) >= sizeof(un
.sun_path
)) {
716 error_setg_errno(errp
, errno
,
717 "TMPDIR environment variable (%s) too large", tmpdir
);
722 * This dummy fd usage silences the mktemp() unsecure warning.
723 * Using mkstemp() doesn't make things more secure here
724 * though. bind() complains about existing files, so we have
725 * to unlink first and thus re-open the race window. The
726 * worst case possible is bind() failing, i.e. a DoS attack.
728 fd
= mkstemp(un
.sun_path
);
730 error_setg_errno(errp
, errno
,
731 "Failed to make a temporary socket name in %s", tmpdir
);
735 qemu_opt_set(opts
, "path", un
.sun_path
, &error_abort
);
738 if ((access(un
.sun_path
, F_OK
) == 0) &&
739 unlink(un
.sun_path
) < 0) {
740 error_setg_errno(errp
, errno
,
741 "Failed to unlink socket %s", un
.sun_path
);
744 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
745 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
748 if (listen(sock
, 1) < 0) {
749 error_setg_errno(errp
, errno
, "Failed to listen on socket");
760 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
761 NonBlockingConnectHandler
*callback
, void *opaque
)
763 struct sockaddr_un un
;
764 const char *path
= qemu_opt_get(opts
, "path");
765 ConnectState
*connect_state
= NULL
;
769 error_setg(errp
, "unix connect: no path specified");
773 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
775 error_setg_errno(errp
, errno
, "Failed to create socket");
778 if (callback
!= NULL
) {
779 connect_state
= g_malloc0(sizeof(*connect_state
));
780 connect_state
->callback
= callback
;
781 connect_state
->opaque
= opaque
;
782 qemu_set_nonblock(sock
);
785 memset(&un
, 0, sizeof(un
));
786 un
.sun_family
= AF_UNIX
;
787 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
789 /* connect to peer */
792 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
793 rc
= -socket_error();
795 } while (rc
== -EINTR
);
797 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
798 connect_state
->fd
= sock
;
799 qemu_set_fd_handler2(sock
, NULL
, NULL
, wait_for_connect
,
802 } else if (rc
>= 0) {
803 /* non blocking socket immediate success, call callback */
804 if (callback
!= NULL
) {
805 callback(sock
, NULL
, opaque
);
810 error_setg_errno(errp
, -rc
, "Failed to connect socket");
815 g_free(connect_state
);
821 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
823 error_setg(errp
, "unix sockets are not available on windows");
828 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
829 NonBlockingConnectHandler
*callback
, void *opaque
)
831 error_setg(errp
, "unix sockets are not available on windows");
837 /* compatibility wrapper */
838 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
844 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
846 optstr
= strchr(str
, ',');
850 path
= g_malloc(len
+1);
851 snprintf(path
, len
+1, "%.*s", len
, str
);
852 qemu_opt_set(opts
, "path", path
, &error_abort
);
856 qemu_opt_set(opts
, "path", str
, &error_abort
);
859 sock
= unix_listen_opts(opts
, errp
);
861 if (sock
!= -1 && ostr
)
862 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
867 int unix_connect(const char *path
, Error
**errp
)
872 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
873 qemu_opt_set(opts
, "path", path
, &error_abort
);
874 sock
= unix_connect_opts(opts
, errp
, NULL
, NULL
);
880 int unix_nonblocking_connect(const char *path
,
881 NonBlockingConnectHandler
*callback
,
882 void *opaque
, Error
**errp
)
887 g_assert(callback
!= NULL
);
889 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
890 qemu_opt_set(opts
, "path", path
, &error_abort
);
891 sock
= unix_connect_opts(opts
, errp
, callback
, opaque
);
896 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
900 addr
= g_new0(SocketAddress
, 1);
901 if (strstart(str
, "unix:", NULL
)) {
902 if (str
[5] == '\0') {
903 error_setg(errp
, "invalid Unix socket address");
906 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
907 addr
->q_unix
= g_new(UnixSocketAddress
, 1);
908 addr
->q_unix
->path
= g_strdup(str
+ 5);
910 } else if (strstart(str
, "fd:", NULL
)) {
911 if (str
[3] == '\0') {
912 error_setg(errp
, "invalid file descriptor address");
915 addr
->kind
= SOCKET_ADDRESS_KIND_FD
;
916 addr
->fd
= g_new(String
, 1);
917 addr
->fd
->str
= g_strdup(str
+ 3);
920 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
921 addr
->inet
= inet_parse(str
, errp
);
922 if (addr
->inet
== NULL
) {
929 qapi_free_SocketAddress(addr
);
933 int socket_connect(SocketAddress
*addr
, Error
**errp
,
934 NonBlockingConnectHandler
*callback
, void *opaque
)
939 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
940 switch (addr
->kind
) {
941 case SOCKET_ADDRESS_KIND_INET
:
942 inet_addr_to_opts(opts
, addr
->inet
);
943 fd
= inet_connect_opts(opts
, errp
, callback
, opaque
);
946 case SOCKET_ADDRESS_KIND_UNIX
:
947 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
948 fd
= unix_connect_opts(opts
, errp
, callback
, opaque
);
951 case SOCKET_ADDRESS_KIND_FD
:
952 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
953 if (fd
>= 0 && callback
) {
954 qemu_set_nonblock(fd
);
955 callback(fd
, NULL
, opaque
);
966 int socket_listen(SocketAddress
*addr
, Error
**errp
)
971 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
972 switch (addr
->kind
) {
973 case SOCKET_ADDRESS_KIND_INET
:
974 inet_addr_to_opts(opts
, addr
->inet
);
975 fd
= inet_listen_opts(opts
, 0, errp
);
978 case SOCKET_ADDRESS_KIND_UNIX
:
979 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
980 fd
= unix_listen_opts(opts
, errp
);
983 case SOCKET_ADDRESS_KIND_FD
:
984 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
994 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
999 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
1000 switch (remote
->kind
) {
1001 case SOCKET_ADDRESS_KIND_INET
:
1002 inet_addr_to_opts(opts
, remote
->inet
);
1004 qemu_opt_set(opts
, "localaddr", local
->inet
->host
, &error_abort
);
1005 qemu_opt_set(opts
, "localport", local
->inet
->port
, &error_abort
);
1007 fd
= inet_dgram_opts(opts
, errp
);
1011 error_setg(errp
, "socket type unsupported for datagram");
1014 qemu_opts_del(opts
);