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
->has_ipv4
&& addr
->ipv4
;
590 bool ipv6
= addr
->has_ipv6
&& addr
->ipv6
;
593 qemu_opt_set_bool(opts
, "ipv4", ipv4
, &error_abort
);
594 qemu_opt_set_bool(opts
, "ipv6", ipv6
, &error_abort
);
595 } else if (addr
->has_ipv4
|| addr
->has_ipv6
) {
596 qemu_opt_set_bool(opts
, "ipv4", !addr
->has_ipv4
, &error_abort
);
597 qemu_opt_set_bool(opts
, "ipv6", !addr
->has_ipv6
, &error_abort
);
600 qemu_opt_set_number(opts
, "to", addr
->to
, &error_abort
);
602 qemu_opt_set(opts
, "host", addr
->host
, &error_abort
);
603 qemu_opt_set(opts
, "port", addr
->port
, &error_abort
);
606 int inet_listen(const char *str
, char *ostr
, int olen
,
607 int socktype
, int port_offset
, Error
**errp
)
612 InetSocketAddress
*addr
;
614 addr
= inet_parse(str
, errp
);
616 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
617 inet_addr_to_opts(opts
, addr
);
618 qapi_free_InetSocketAddress(addr
);
619 sock
= inet_listen_opts(opts
, port_offset
, errp
);
620 if (sock
!= -1 && ostr
) {
621 optstr
= strchr(str
, ',');
622 if (qemu_opt_get_bool(opts
, "ipv6", 0)) {
623 snprintf(ostr
, olen
, "[%s]:%s%s",
624 qemu_opt_get(opts
, "host"),
625 qemu_opt_get(opts
, "port"),
626 optstr
? optstr
: "");
628 snprintf(ostr
, olen
, "%s:%s%s",
629 qemu_opt_get(opts
, "host"),
630 qemu_opt_get(opts
, "port"),
631 optstr
? optstr
: "");
640 * Create a blocking socket and connect it to an address.
642 * @str: address string
643 * @errp: set in case of an error
645 * Returns -1 in case of error, file descriptor on success
647 int inet_connect(const char *str
, Error
**errp
)
651 InetSocketAddress
*addr
;
653 addr
= inet_parse(str
, errp
);
655 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
656 inet_addr_to_opts(opts
, addr
);
657 qapi_free_InetSocketAddress(addr
);
658 sock
= inet_connect_opts(opts
, errp
, NULL
, NULL
);
665 * Create a non-blocking socket and connect it to an address.
666 * Calls the callback function with fd in case of success or -1 in case of
669 * @str: address string
670 * @callback: callback function that is called when connect completes,
672 * @opaque: opaque for callback function
673 * @errp: set in case of an error
675 * Returns: -1 on immediate error, file descriptor on success.
677 int inet_nonblocking_connect(const char *str
,
678 NonBlockingConnectHandler
*callback
,
679 void *opaque
, Error
**errp
)
683 InetSocketAddress
*addr
;
685 g_assert(callback
!= NULL
);
687 addr
= inet_parse(str
, errp
);
689 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
690 inet_addr_to_opts(opts
, addr
);
691 qapi_free_InetSocketAddress(addr
);
692 sock
= inet_connect_opts(opts
, errp
, callback
, opaque
);
700 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
702 struct sockaddr_un un
;
703 const char *path
= qemu_opt_get(opts
, "path");
706 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
708 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
712 memset(&un
, 0, sizeof(un
));
713 un
.sun_family
= AF_UNIX
;
714 if (path
&& strlen(path
)) {
715 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
717 const char *tmpdir
= getenv("TMPDIR");
718 tmpdir
= tmpdir
? tmpdir
: "/tmp";
719 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
720 tmpdir
) >= sizeof(un
.sun_path
)) {
721 error_setg_errno(errp
, errno
,
722 "TMPDIR environment variable (%s) too large", tmpdir
);
727 * This dummy fd usage silences the mktemp() unsecure warning.
728 * Using mkstemp() doesn't make things more secure here
729 * though. bind() complains about existing files, so we have
730 * to unlink first and thus re-open the race window. The
731 * worst case possible is bind() failing, i.e. a DoS attack.
733 fd
= mkstemp(un
.sun_path
);
735 error_setg_errno(errp
, errno
,
736 "Failed to make a temporary socket name in %s", tmpdir
);
740 qemu_opt_set(opts
, "path", un
.sun_path
, &error_abort
);
743 if ((access(un
.sun_path
, F_OK
) == 0) &&
744 unlink(un
.sun_path
) < 0) {
745 error_setg_errno(errp
, errno
,
746 "Failed to unlink socket %s", un
.sun_path
);
749 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
750 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
753 if (listen(sock
, 1) < 0) {
754 error_setg_errno(errp
, errno
, "Failed to listen on socket");
765 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
766 NonBlockingConnectHandler
*callback
, void *opaque
)
768 struct sockaddr_un un
;
769 const char *path
= qemu_opt_get(opts
, "path");
770 ConnectState
*connect_state
= NULL
;
774 error_setg(errp
, "unix connect: no path specified");
778 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
780 error_setg_errno(errp
, errno
, "Failed to create socket");
783 if (callback
!= NULL
) {
784 connect_state
= g_malloc0(sizeof(*connect_state
));
785 connect_state
->callback
= callback
;
786 connect_state
->opaque
= opaque
;
787 qemu_set_nonblock(sock
);
790 memset(&un
, 0, sizeof(un
));
791 un
.sun_family
= AF_UNIX
;
792 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", path
);
794 /* connect to peer */
797 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
798 rc
= -socket_error();
800 } while (rc
== -EINTR
);
802 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
803 connect_state
->fd
= sock
;
804 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
806 } else if (rc
>= 0) {
807 /* non blocking socket immediate success, call callback */
808 if (callback
!= NULL
) {
809 callback(sock
, NULL
, opaque
);
814 error_setg_errno(errp
, -rc
, "Failed to connect socket");
819 g_free(connect_state
);
825 int unix_listen_opts(QemuOpts
*opts
, Error
**errp
)
827 error_setg(errp
, "unix sockets are not available on windows");
832 int unix_connect_opts(QemuOpts
*opts
, Error
**errp
,
833 NonBlockingConnectHandler
*callback
, void *opaque
)
835 error_setg(errp
, "unix sockets are not available on windows");
841 /* compatibility wrapper */
842 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
848 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
850 optstr
= strchr(str
, ',');
854 path
= g_malloc(len
+1);
855 snprintf(path
, len
+1, "%.*s", len
, str
);
856 qemu_opt_set(opts
, "path", path
, &error_abort
);
860 qemu_opt_set(opts
, "path", str
, &error_abort
);
863 sock
= unix_listen_opts(opts
, errp
);
865 if (sock
!= -1 && ostr
)
866 snprintf(ostr
, olen
, "%s%s", qemu_opt_get(opts
, "path"), optstr
? optstr
: "");
871 int unix_connect(const char *path
, Error
**errp
)
876 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
877 qemu_opt_set(opts
, "path", path
, &error_abort
);
878 sock
= unix_connect_opts(opts
, errp
, NULL
, NULL
);
884 int unix_nonblocking_connect(const char *path
,
885 NonBlockingConnectHandler
*callback
,
886 void *opaque
, Error
**errp
)
891 g_assert(callback
!= NULL
);
893 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
894 qemu_opt_set(opts
, "path", path
, &error_abort
);
895 sock
= unix_connect_opts(opts
, errp
, callback
, opaque
);
900 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
904 addr
= g_new0(SocketAddress
, 1);
905 if (strstart(str
, "unix:", NULL
)) {
906 if (str
[5] == '\0') {
907 error_setg(errp
, "invalid Unix socket address");
910 addr
->kind
= SOCKET_ADDRESS_KIND_UNIX
;
911 addr
->q_unix
= g_new(UnixSocketAddress
, 1);
912 addr
->q_unix
->path
= g_strdup(str
+ 5);
914 } else if (strstart(str
, "fd:", NULL
)) {
915 if (str
[3] == '\0') {
916 error_setg(errp
, "invalid file descriptor address");
919 addr
->kind
= SOCKET_ADDRESS_KIND_FD
;
920 addr
->fd
= g_new(String
, 1);
921 addr
->fd
->str
= g_strdup(str
+ 3);
924 addr
->kind
= SOCKET_ADDRESS_KIND_INET
;
925 addr
->inet
= inet_parse(str
, errp
);
926 if (addr
->inet
== NULL
) {
933 qapi_free_SocketAddress(addr
);
937 int socket_connect(SocketAddress
*addr
, Error
**errp
,
938 NonBlockingConnectHandler
*callback
, void *opaque
)
943 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
944 switch (addr
->kind
) {
945 case SOCKET_ADDRESS_KIND_INET
:
946 inet_addr_to_opts(opts
, addr
->inet
);
947 fd
= inet_connect_opts(opts
, errp
, callback
, opaque
);
950 case SOCKET_ADDRESS_KIND_UNIX
:
951 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
952 fd
= unix_connect_opts(opts
, errp
, callback
, opaque
);
955 case SOCKET_ADDRESS_KIND_FD
:
956 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
957 if (fd
>= 0 && callback
) {
958 qemu_set_nonblock(fd
);
959 callback(fd
, NULL
, opaque
);
970 int socket_listen(SocketAddress
*addr
, Error
**errp
)
975 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
976 switch (addr
->kind
) {
977 case SOCKET_ADDRESS_KIND_INET
:
978 inet_addr_to_opts(opts
, addr
->inet
);
979 fd
= inet_listen_opts(opts
, 0, errp
);
982 case SOCKET_ADDRESS_KIND_UNIX
:
983 qemu_opt_set(opts
, "path", addr
->q_unix
->path
, &error_abort
);
984 fd
= unix_listen_opts(opts
, errp
);
987 case SOCKET_ADDRESS_KIND_FD
:
988 fd
= monitor_get_fd(cur_mon
, addr
->fd
->str
, errp
);
998 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1003 opts
= qemu_opts_create(&socket_optslist
, NULL
, 0, &error_abort
);
1004 switch (remote
->kind
) {
1005 case SOCKET_ADDRESS_KIND_INET
:
1006 inet_addr_to_opts(opts
, remote
->inet
);
1008 qemu_opt_set(opts
, "localaddr", local
->inet
->host
, &error_abort
);
1009 qemu_opt_set(opts
, "localport", local
->inet
->port
, &error_abort
);
1011 fd
= inet_dgram_opts(opts
, errp
);
1015 error_setg(errp
, "socket type unsupported for datagram");
1018 qemu_opts_del(opts
);