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 # define AI_V4MAPPED 0
36 /* used temporarily until all users are converted to QemuOpts */
37 QemuOptsList socket_optslist
= {
39 .head
= QTAILQ_HEAD_INITIALIZER(socket_optslist
.head
),
43 .type
= QEMU_OPT_STRING
,
46 .type
= QEMU_OPT_STRING
,
49 .type
= QEMU_OPT_STRING
,
52 .type
= QEMU_OPT_STRING
,
55 .type
= QEMU_OPT_STRING
,
58 .type
= QEMU_OPT_NUMBER
,
61 .type
= QEMU_OPT_BOOL
,
64 .type
= QEMU_OPT_BOOL
,
70 static int inet_getport(struct addrinfo
*e
)
72 struct sockaddr_in
*i4
;
73 struct sockaddr_in6
*i6
;
75 switch (e
->ai_family
) {
77 i6
= (void*)e
->ai_addr
;
78 return ntohs(i6
->sin6_port
);
80 i4
= (void*)e
->ai_addr
;
81 return ntohs(i4
->sin_port
);
87 static void inet_setport(struct addrinfo
*e
, int port
)
89 struct sockaddr_in
*i4
;
90 struct sockaddr_in6
*i6
;
92 switch (e
->ai_family
) {
94 i6
= (void*)e
->ai_addr
;
95 i6
->sin6_port
= htons(port
);
98 i4
= (void*)e
->ai_addr
;
99 i4
->sin_port
= htons(port
);
104 NetworkAddressFamily
inet_netfamily(int family
)
107 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
108 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
109 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
111 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
114 int inet_listen_opts(QemuOpts
*opts
, int port_offset
, Error
**errp
)
116 struct addrinfo ai
,*res
,*e
;
119 char uaddr
[INET6_ADDRSTRLEN
+1];
121 int slisten
, rc
, to
, port_min
, port_max
, p
;
123 memset(&ai
,0, sizeof(ai
));
124 ai
.ai_flags
= AI_PASSIVE
;
125 ai
.ai_family
= PF_UNSPEC
;
126 ai
.ai_socktype
= SOCK_STREAM
;
128 if ((qemu_opt_get(opts
, "host") == NULL
) ||
129 (qemu_opt_get(opts
, "port") == NULL
)) {
130 error_setg(errp
, "host and/or port not specified");
133 pstrcpy(port
, sizeof(port
), qemu_opt_get(opts
, "port"));
134 addr
= qemu_opt_get(opts
, "host");
136 to
= qemu_opt_get_number(opts
, "to", 0);
137 if (qemu_opt_get_bool(opts
, "ipv4", 0))
138 ai
.ai_family
= PF_INET
;
139 if (qemu_opt_get_bool(opts
, "ipv6", 0))
140 ai
.ai_family
= PF_INET6
;
144 unsigned long long baseport
;
145 if (parse_uint_full(port
, &baseport
, 10) < 0) {
146 error_setg(errp
, "can't convert to a number: %s", port
);
149 if (baseport
> 65535 ||
150 baseport
+ port_offset
> 65535) {
151 error_setg(errp
, "port %s out of range", port
);
154 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
156 rc
= getaddrinfo(strlen(addr
) ? addr
: NULL
, port
, &ai
, &res
);
158 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
163 /* create socket + bind */
164 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
165 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
166 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
167 NI_NUMERICHOST
| NI_NUMERICSERV
);
168 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
171 error_setg_errno(errp
, errno
, "Failed to create socket");
176 socket_set_fast_reuse(slisten
);
178 if (e
->ai_family
== PF_INET6
) {
179 /* listen on both ipv4 and ipv6 */
181 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
186 port_min
= inet_getport(e
);
187 port_max
= to
? to
+ port_offset
: port_min
;
188 for (p
= port_min
; p
<= port_max
; p
++) {
190 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
195 error_setg_errno(errp
, errno
, "Failed to bind socket");
199 closesocket(slisten
);
205 if (listen(slisten
,1) != 0) {
206 error_setg_errno(errp
, errno
, "Failed to listen on socket");
207 closesocket(slisten
);
211 qemu_opt_set(opts
, "host", uaddr
, &error_abort
);
212 qemu_opt_set_number(opts
, "port", inet_getport(e
) - port_offset
,
214 qemu_opt_set_bool(opts
, "ipv6", e
->ai_family
== PF_INET6
,
216 qemu_opt_set_bool(opts
, "ipv4", e
->ai_family
!= PF_INET6
,
223 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
224 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
226 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
227 ((rc) == -EINPROGRESS)
230 /* Struct to store connect state for non blocking connect */
231 typedef struct ConnectState
{
233 struct addrinfo
*addr_list
;
234 struct addrinfo
*current_addr
;
235 NonBlockingConnectHandler
*callback
;
239 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
240 ConnectState
*connect_state
, Error
**errp
);
242 static void wait_for_connect(void *opaque
)
244 ConnectState
*s
= opaque
;
246 socklen_t valsize
= sizeof(val
);
250 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
253 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
254 } while (rc
== -1 && socket_error() == EINTR
);
256 /* update rc to contain error */
264 error_setg_errno(&err
, errno
, "Error connecting to socket");
269 /* try to connect to the next address on the list */
270 if (s
->current_addr
) {
271 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
272 s
->current_addr
= s
->current_addr
->ai_next
;
273 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
277 error_setg_errno(&err
, errno
, "Unable to start socket connect");
279 /* connect in progress */
285 freeaddrinfo(s
->addr_list
);
289 s
->callback(s
->fd
, err
, s
->opaque
);
296 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
297 ConnectState
*connect_state
, Error
**errp
)
301 *in_progress
= false;
303 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
305 error_setg_errno(errp
, errno
, "Failed to create socket");
308 socket_set_fast_reuse(sock
);
309 if (connect_state
!= NULL
) {
310 qemu_set_nonblock(sock
);
312 /* connect to peer */
315 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
316 rc
= -socket_error();
318 } while (rc
== -EINTR
);
320 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
321 connect_state
->fd
= sock
;
322 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
325 error_setg_errno(errp
, errno
, "Failed to connect socket");
332 static struct addrinfo
*inet_parse_connect_opts(QemuOpts
*opts
, Error
**errp
)
334 struct addrinfo ai
, *res
;
339 memset(&ai
, 0, sizeof(ai
));
341 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
342 ai
.ai_family
= PF_UNSPEC
;
343 ai
.ai_socktype
= SOCK_STREAM
;
345 addr
= qemu_opt_get(opts
, "host");
346 port
= qemu_opt_get(opts
, "port");
347 if (addr
== NULL
|| port
== NULL
) {
348 error_setg(errp
, "host and/or port not specified");
352 if (qemu_opt_get_bool(opts
, "ipv4", 0)) {
353 ai
.ai_family
= PF_INET
;
355 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
356 ai
.ai_family
= PF_INET6
;
360 rc
= getaddrinfo(addr
, port
, &ai
, &res
);
362 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
370 * Create a socket and connect it to an address.
372 * @opts: QEMU options, recognized parameters strings "host" and "port",
373 * bools "ipv4" and "ipv6".
374 * @errp: set on error
375 * @callback: callback function for non-blocking connect
376 * @opaque: opaque for callback function
378 * Returns: -1 on error, file descriptor on success.
380 * If @callback is non-null, the connect is non-blocking. If this
381 * function succeeds, callback will be called when the connection
382 * completes, with the file descriptor on success, or -1 on error.
384 int inet_connect_opts(QemuOpts
*opts
, Error
**errp
,
385 NonBlockingConnectHandler
*callback
, void *opaque
)
387 Error
*local_err
= NULL
;
388 struct addrinfo
*res
, *e
;
391 ConnectState
*connect_state
= NULL
;
393 res
= inet_parse_connect_opts(opts
, errp
);
398 if (callback
!= NULL
) {
399 connect_state
= g_malloc0(sizeof(*connect_state
));
400 connect_state
->addr_list
= res
;
401 connect_state
->callback
= callback
;
402 connect_state
->opaque
= opaque
;
405 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
406 error_free(local_err
);
408 if (connect_state
!= NULL
) {
409 connect_state
->current_addr
= e
;
411 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
418 error_propagate(errp
, local_err
);
419 } else if (in_progress
) {
420 /* wait_for_connect() will do the rest */
424 callback(sock
, NULL
, opaque
);
427 g_free(connect_state
);
432 int inet_dgram_opts(QemuOpts
*opts
, Error
**errp
)
434 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
439 /* lookup peer addr */
440 memset(&ai
,0, sizeof(ai
));
441 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
442 ai
.ai_family
= PF_UNSPEC
;
443 ai
.ai_socktype
= SOCK_DGRAM
;
445 addr
= qemu_opt_get(opts
, "host");
446 port
= qemu_opt_get(opts
, "port");
447 if (addr
== NULL
|| strlen(addr
) == 0) {
450 if (port
== NULL
|| strlen(port
) == 0) {
451 error_setg(errp
, "remote port not specified");
455 if (qemu_opt_get_bool(opts
, "ipv4", 0))
456 ai
.ai_family
= PF_INET
;
457 if (qemu_opt_get_bool(opts
, "ipv6", 0))
458 ai
.ai_family
= PF_INET6
;
460 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
461 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
466 /* lookup local addr */
467 memset(&ai
,0, sizeof(ai
));
468 ai
.ai_flags
= AI_PASSIVE
;
469 ai
.ai_family
= peer
->ai_family
;
470 ai
.ai_socktype
= SOCK_DGRAM
;
472 addr
= qemu_opt_get(opts
, "localaddr");
473 port
= qemu_opt_get(opts
, "localport");
474 if (addr
== NULL
|| strlen(addr
) == 0) {
477 if (!port
|| strlen(port
) == 0)
480 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
481 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
487 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
489 error_setg_errno(errp
, errno
, "Failed to create socket");
492 socket_set_fast_reuse(sock
);
495 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
496 error_setg_errno(errp
, errno
, "Failed to bind socket");
500 /* connect to peer */
501 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
502 error_setg_errno(errp
, errno
, "Failed to connect socket");
520 /* compatibility wrapper */
521 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
523 InetSocketAddress
*addr
;
524 const char *optstr
, *h
;
530 addr
= g_new0(InetSocketAddress
, 1);
536 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
537 error_setg(errp
, "error parsing port in address '%s'", str
);
540 } else if (str
[0] == '[') {
542 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
543 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
546 addr
->ipv6
= addr
->has_ipv6
= true;
548 /* hostname or IPv4 addr */
549 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
550 error_setg(errp
, "error parsing address '%s'", str
);
553 if (host
[strspn(host
, "0123456789.")] == '\0') {
554 addr
->ipv4
= addr
->has_ipv4
= true;
558 addr
->host
= g_strdup(host
);
559 addr
->port
= g_strdup(port
);
563 h
= strstr(optstr
, ",to=");
566 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
567 (h
[pos
] != '\0' && h
[pos
] != ',')) {
568 error_setg(errp
, "error parsing to= argument");
574 if (strstr(optstr
, ",ipv4")) {
575 addr
->ipv4
= addr
->has_ipv4
= true;
577 if (strstr(optstr
, ",ipv6")) {
578 addr
->ipv6
= addr
->has_ipv6
= true;
583 qapi_free_InetSocketAddress(addr
);
587 static void inet_addr_to_opts(QemuOpts
*opts
, const InetSocketAddress
*addr
)
589 bool ipv4
= addr
->ipv4
|| !addr
->has_ipv4
;
590 bool ipv6
= addr
->ipv6
|| !addr
->has_ipv6
;
592 if (!ipv4
|| !ipv6
) {
593 qemu_opt_set_bool(opts
, "ipv4", ipv4
, &error_abort
);
594 qemu_opt_set_bool(opts
, "ipv6", ipv6
, &error_abort
);
597 qemu_opt_set_number(opts
, "to", addr
->to
, &error_abort
);
599 qemu_opt_set(opts
, "host", addr
->host
, &error_abort
);
600 qemu_opt_set(opts
, "port", addr
->port
, &error_abort
);
603 int inet_listen(const char *str
, char *ostr
, int olen
,
604 int socktype
, int port_offset
, Error
**errp
)
609 InetSocketAddress
*addr
;
611 addr
= inet_parse(str
, errp
);
613 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
614 inet_addr_to_opts(opts
, addr
);
615 qapi_free_InetSocketAddress(addr
);
616 sock
= inet_listen_opts(opts
, port_offset
, errp
);
617 if (sock
!= -1 && ostr
) {
618 optstr
= strchr(str
, ',');
619 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
620 snprintf(ostr
, olen
, "[%s]:%s%s",
621 qemu_opt_get(opts
, "host"),
622 qemu_opt_get(opts
, "port"),
623 optstr
? optstr
: "");
625 snprintf(ostr
, olen
, "%s:%s%s",
626 qemu_opt_get(opts
, "host"),
627 qemu_opt_get(opts
, "port"),
628 optstr
? optstr
: "");
637 * Create a blocking socket and connect it to an address.
639 * @str: address string
640 * @errp: set in case of an error
642 * Returns -1 in case of error, file descriptor on success
644 int inet_connect(const char *str
, Error
**errp
)
648 InetSocketAddress
*addr
;
650 addr
= inet_parse(str
, errp
);
652 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
653 inet_addr_to_opts(opts
, addr
);
654 qapi_free_InetSocketAddress(addr
);
655 sock
= inet_connect_opts(opts
, errp
, NULL
, NULL
);
662 * Create a non-blocking socket and connect it to an address.
663 * Calls the callback function with fd in case of success or -1 in case of
666 * @str: address string
667 * @callback: callback function that is called when connect completes,
669 * @opaque: opaque for callback function
670 * @errp: set in case of an error
672 * Returns: -1 on immediate error, file descriptor on success.
674 int inet_nonblocking_connect(const char *str
,
675 NonBlockingConnectHandler
*callback
,
676 void *opaque
, Error
**errp
)
680 InetSocketAddress
*addr
;
682 g_assert(callback
!= NULL
);
684 addr
= inet_parse(str
, errp
);
686 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
687 inet_addr_to_opts(opts
, addr
);
688 qapi_free_InetSocketAddress(addr
);
689 sock
= inet_connect_opts(opts
, errp
, callback
, opaque
);
697 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
699 struct sockaddr_un un
;
700 const char *path
= qemu_opt_get(opts
, "path");
703 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
705 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
709 memset(&un
, 0, sizeof(un
));
710 un
.sun_family
= AF_UNIX
;
711 if (path
&& strlen(path
)) {
712 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
714 const char *tmpdir
= getenv("TMPDIR");
715 tmpdir
= tmpdir
? tmpdir
: "/tmp";
716 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
717 tmpdir
) >= sizeof(un
.sun_path
)) {
718 error_setg_errno(errp
, errno
,
719 "TMPDIR environment variable (%s) too large", tmpdir
);
724 * This dummy fd usage silences the mktemp() unsecure warning.
725 * Using mkstemp() doesn't make things more secure here
726 * though. bind() complains about existing files, so we have
727 * to unlink first and thus re-open the race window. The
728 * worst case possible is bind() failing, i.e. a DoS attack.
730 fd
= mkstemp(un
.sun_path
);
732 error_setg_errno(errp
, errno
,
733 "Failed to make a temporary socket name in %s", tmpdir
);
737 qemu_opt_set(opts
, "path", un
.sun_path
, &error_abort
);
740 if ((access(un
.sun_path
, F_OK
) == 0) &&
741 unlink(un
.sun_path
) < 0) {
742 error_setg_errno(errp
, errno
,
743 "Failed to unlink socket %s", un
.sun_path
);
746 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
747 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
750 if (listen(sock
, 1) < 0) {
751 error_setg_errno(errp
, errno
, "Failed to listen on socket");
762 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
763 NonBlockingConnectHandler
*callback
, void *opaque
)
765 struct sockaddr_un un
;
766 const char *path
= qemu_opt_get(opts
, "path");
767 ConnectState
*connect_state
= NULL
;
771 error_setg(errp
, "unix connect: no path specified");
775 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
777 error_setg_errno(errp
, errno
, "Failed to create socket");
780 if (callback
!= NULL
) {
781 connect_state
= g_malloc0(sizeof(*connect_state
));
782 connect_state
->callback
= callback
;
783 connect_state
->opaque
= opaque
;
784 qemu_set_nonblock(sock
);
787 memset(&un
, 0, sizeof(un
));
788 un
.sun_family
= AF_UNIX
;
789 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
791 /* connect to peer */
794 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
795 rc
= -socket_error();
797 } while (rc
== -EINTR
);
799 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
800 connect_state
->fd
= sock
;
801 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
803 } else if (rc
>= 0) {
804 /* non blocking socket immediate success, call callback */
805 if (callback
!= NULL
) {
806 callback(sock
, NULL
, opaque
);
811 error_setg_errno(errp
, -rc
, "Failed to connect socket");
816 g_free(connect_state
);
822 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
824 error_setg(errp
, "unix sockets are not available on windows");
829 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
830 NonBlockingConnectHandler
*callback
, void *opaque
)
832 error_setg(errp
, "unix sockets are not available on windows");
838 /* compatibility wrapper */
839 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
845 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
847 optstr
= strchr(str
, ',');
851 path
= g_malloc(len
+1);
852 snprintf(path
, len
+1, "%.*s", len
, str
);
853 qemu_opt_set(opts
, "path", path
, &error_abort
);
857 qemu_opt_set(opts
, "path", str
, &error_abort
);
860 sock
= unix_listen_opts(opts
, errp
);
862 if (sock
!= -1 && ostr
)
863 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
868 int unix_connect(const char *path
, Error
**errp
)
873 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
874 qemu_opt_set(opts
, "path", path
, &error_abort
);
875 sock
= unix_connect_opts(opts
, errp
, NULL
, NULL
);
881 int unix_nonblocking_connect(const char *path
,
882 NonBlockingConnectHandler
*callback
,
883 void *opaque
, Error
**errp
)
888 g_assert(callback
!= NULL
);
890 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
891 qemu_opt_set(opts
, "path", path
, &error_abort
);
892 sock
= unix_connect_opts(opts
, errp
, callback
, opaque
);
897 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
901 addr
= g_new0(SocketAddress
, 1);
902 if (strstart(str
, "unix:", NULL
)) {
903 if (str
[5] == '\0') {
904 error_setg(errp
, "invalid Unix socket address");
907 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
908 addr
->q_unix
= g_new(UnixSocketAddress
, 1);
909 addr
->q_unix
->path
= g_strdup(str
+ 5);
911 } else if (strstart(str
, "fd:", NULL
)) {
912 if (str
[3] == '\0') {
913 error_setg(errp
, "invalid file descriptor address");
916 addr
->kind
= SOCKET_ADDRESS_KIND_FD
;
917 addr
->fd
= g_new(String
, 1);
918 addr
->fd
->str
= g_strdup(str
+ 3);
921 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
922 addr
->inet
= inet_parse(str
, errp
);
923 if (addr
->inet
== NULL
) {
930 qapi_free_SocketAddress(addr
);
934 int socket_connect(SocketAddress
*addr
, Error
**errp
,
935 NonBlockingConnectHandler
*callback
, void *opaque
)
940 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
941 switch (addr
->kind
) {
942 case SOCKET_ADDRESS_KIND_INET
:
943 inet_addr_to_opts(opts
, addr
->inet
);
944 fd
= inet_connect_opts(opts
, errp
, callback
, opaque
);
947 case SOCKET_ADDRESS_KIND_UNIX
:
948 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
949 fd
= unix_connect_opts(opts
, errp
, callback
, opaque
);
952 case SOCKET_ADDRESS_KIND_FD
:
953 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
954 if (fd
>= 0 && callback
) {
955 qemu_set_nonblock(fd
);
956 callback(fd
, NULL
, opaque
);
967 int socket_listen(SocketAddress
*addr
, Error
**errp
)
972 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
973 switch (addr
->kind
) {
974 case SOCKET_ADDRESS_KIND_INET
:
975 inet_addr_to_opts(opts
, addr
->inet
);
976 fd
= inet_listen_opts(opts
, 0, errp
);
979 case SOCKET_ADDRESS_KIND_UNIX
:
980 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
981 fd
= unix_listen_opts(opts
, errp
);
984 case SOCKET_ADDRESS_KIND_FD
:
985 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
995 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1000 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
1001 switch (remote
->kind
) {
1002 case SOCKET_ADDRESS_KIND_INET
:
1003 inet_addr_to_opts(opts
, remote
->inet
);
1005 qemu_opt_set(opts
, "localaddr", local
->inet
->host
, &error_abort
);
1006 qemu_opt_set(opts
, "localport", local
->inet
->port
, &error_abort
);
1008 fd
= inet_dgram_opts(opts
, errp
);
1012 error_setg(errp
, "socket type unsupported for datagram");
1015 qemu_opts_del(opts
);