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 "qemu/osdep.h"
20 #ifdef CONFIG_AF_VSOCK
21 #include <linux/vm_sockets.h>
22 #endif /* CONFIG_AF_VSOCK */
24 #include "monitor/monitor.h"
25 #include "qapi/error.h"
26 #include "qemu/sockets.h"
27 #include "qemu/main-loop.h"
28 #include "qapi/clone-visitor.h"
29 #include "qapi/qobject-input-visitor.h"
30 #include "qapi/qobject-output-visitor.h"
31 #include "qapi-visit.h"
32 #include "qemu/cutils.h"
35 # define AI_ADDRCONFIG 0
39 # define AI_V4MAPPED 0
42 #ifndef AI_NUMERICSERV
43 # define AI_NUMERICSERV 0
47 static int inet_getport(struct addrinfo
*e
)
49 struct sockaddr_in
*i4
;
50 struct sockaddr_in6
*i6
;
52 switch (e
->ai_family
) {
54 i6
= (void*)e
->ai_addr
;
55 return ntohs(i6
->sin6_port
);
57 i4
= (void*)e
->ai_addr
;
58 return ntohs(i4
->sin_port
);
64 static void inet_setport(struct addrinfo
*e
, int port
)
66 struct sockaddr_in
*i4
;
67 struct sockaddr_in6
*i6
;
69 switch (e
->ai_family
) {
71 i6
= (void*)e
->ai_addr
;
72 i6
->sin6_port
= htons(port
);
75 i4
= (void*)e
->ai_addr
;
76 i4
->sin_port
= htons(port
);
81 NetworkAddressFamily
inet_netfamily(int family
)
84 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
85 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
86 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
87 #ifdef CONFIG_AF_VSOCK
88 case PF_VSOCK
: return NETWORK_ADDRESS_FAMILY_VSOCK
;
89 #endif /* CONFIG_AF_VSOCK */
91 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
95 * Matrix we're trying to apply
108 * NB, this matrix is only about getting the necessary results
109 * from getaddrinfo(). Some of the cases require further work
110 * after reading results from getaddrinfo in order to fully
111 * apply the logic the end user wants. eg with the last case
112 * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
113 * guarantee the ipv6=t part of the request - we need more
114 * checks to provide ipv4=t part of the guarantee. This is
115 * outside scope of this method and not currently handled by
118 int inet_ai_family_from_address(InetSocketAddress
*addr
,
121 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
122 !addr
->ipv6
&& !addr
->ipv4
) {
123 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
126 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
129 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
135 static int inet_listen_saddr(InetSocketAddress
*saddr
,
140 struct addrinfo ai
,*res
,*e
;
142 char uaddr
[INET6_ADDRSTRLEN
+1];
144 int slisten
, rc
, port_min
, port_max
, p
;
147 memset(&ai
,0, sizeof(ai
));
148 ai
.ai_flags
= AI_PASSIVE
;
149 if (saddr
->has_numeric
&& saddr
->numeric
) {
150 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
152 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
153 ai
.ai_socktype
= SOCK_STREAM
;
156 error_propagate(errp
, err
);
160 if (saddr
->host
== NULL
) {
161 error_setg(errp
, "host not specified");
164 if (saddr
->port
!= NULL
) {
165 pstrcpy(port
, sizeof(port
), saddr
->port
);
172 unsigned long long baseport
;
173 if (strlen(port
) == 0) {
174 error_setg(errp
, "port not specified");
177 if (parse_uint_full(port
, &baseport
, 10) < 0) {
178 error_setg(errp
, "can't convert to a number: %s", port
);
181 if (baseport
> 65535 ||
182 baseport
+ port_offset
> 65535) {
183 error_setg(errp
, "port %s out of range", port
);
186 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
188 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
189 strlen(port
) ? port
: NULL
, &ai
, &res
);
191 error_setg(errp
, "address resolution failed for %s:%s: %s",
192 saddr
->host
, port
, gai_strerror(rc
));
196 /* create socket + bind */
197 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
198 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
199 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
200 NI_NUMERICHOST
| NI_NUMERICSERV
);
201 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
204 error_setg_errno(errp
, errno
, "Failed to create socket");
209 socket_set_fast_reuse(slisten
);
211 if (e
->ai_family
== PF_INET6
) {
212 /* listen on both ipv4 and ipv6 */
214 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
219 port_min
= inet_getport(e
);
220 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
221 for (p
= port_min
; p
<= port_max
; p
++) {
223 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
228 error_setg_errno(errp
, errno
, "Failed to bind socket");
232 closesocket(slisten
);
238 if (listen(slisten
,1) != 0) {
239 error_setg_errno(errp
, errno
, "Failed to listen on socket");
240 closesocket(slisten
);
246 saddr
->host
= g_strdup(uaddr
);
248 saddr
->port
= g_strdup_printf("%d",
249 inet_getport(e
) - port_offset
);
250 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
251 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
258 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
259 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
261 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
262 ((rc) == -EINPROGRESS)
265 /* Struct to store connect state for non blocking connect */
266 typedef struct ConnectState
{
268 struct addrinfo
*addr_list
;
269 struct addrinfo
*current_addr
;
270 NonBlockingConnectHandler
*callback
;
274 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
275 ConnectState
*connect_state
, Error
**errp
);
277 static void wait_for_connect(void *opaque
)
279 ConnectState
*s
= opaque
;
281 socklen_t valsize
= sizeof(val
);
285 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
288 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
289 } while (rc
== -1 && errno
== EINTR
);
291 /* update rc to contain error */
299 error_setg_errno(&err
, errno
, "Error connecting to socket");
304 /* try to connect to the next address on the list */
305 if (s
->current_addr
) {
306 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
307 s
->current_addr
= s
->current_addr
->ai_next
;
308 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
312 error_setg_errno(&err
, errno
, "Unable to start socket connect");
314 /* connect in progress */
320 freeaddrinfo(s
->addr_list
);
324 s
->callback(s
->fd
, err
, s
->opaque
);
331 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
332 ConnectState
*connect_state
, Error
**errp
)
336 *in_progress
= false;
338 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
340 error_setg_errno(errp
, errno
, "Failed to create socket");
343 socket_set_fast_reuse(sock
);
344 if (connect_state
!= NULL
) {
345 qemu_set_nonblock(sock
);
347 /* connect to peer */
350 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
353 } while (rc
== -EINTR
);
355 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
356 connect_state
->fd
= sock
;
357 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
360 error_setg_errno(errp
, errno
, "Failed to connect socket");
367 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
370 struct addrinfo ai
, *res
;
373 static int useV4Mapped
= 1;
375 memset(&ai
, 0, sizeof(ai
));
377 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
378 if (atomic_read(&useV4Mapped
)) {
379 ai
.ai_flags
|= AI_V4MAPPED
;
381 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
382 ai
.ai_socktype
= SOCK_STREAM
;
385 error_propagate(errp
, err
);
389 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
390 error_setg(errp
, "host and/or port not specified");
395 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
397 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
398 * then don't implement it in their getaddrinfo(). Detect
399 * this and retry without the flag since that's preferrable
402 if (rc
== EAI_BADFLAGS
&&
403 (ai
.ai_flags
& AI_V4MAPPED
)) {
404 atomic_set(&useV4Mapped
, 0);
405 ai
.ai_flags
&= ~AI_V4MAPPED
;
406 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
409 error_setg(errp
, "address resolution failed for %s:%s: %s",
410 saddr
->host
, saddr
->port
, gai_strerror(rc
));
417 * Create a socket and connect it to an address.
419 * @saddr: Inet socket address specification
420 * @errp: set on error
421 * @callback: callback function for non-blocking connect
422 * @opaque: opaque for callback function
424 * Returns: -1 on error, file descriptor on success.
426 * If @callback is non-null, the connect is non-blocking. If this
427 * function succeeds, callback will be called when the connection
428 * completes, with the file descriptor on success, or -1 on error.
430 int inet_connect_saddr(InetSocketAddress
*saddr
,
431 NonBlockingConnectHandler
*callback
, void *opaque
,
434 Error
*local_err
= NULL
;
435 struct addrinfo
*res
, *e
;
438 ConnectState
*connect_state
= NULL
;
440 res
= inet_parse_connect_saddr(saddr
, errp
);
445 if (callback
!= NULL
) {
446 connect_state
= g_malloc0(sizeof(*connect_state
));
447 connect_state
->addr_list
= res
;
448 connect_state
->callback
= callback
;
449 connect_state
->opaque
= opaque
;
452 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
453 error_free(local_err
);
455 if (connect_state
!= NULL
) {
456 connect_state
->current_addr
= e
;
458 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
465 error_propagate(errp
, local_err
);
466 } else if (in_progress
) {
467 /* wait_for_connect() will do the rest */
471 callback(sock
, NULL
, opaque
);
474 g_free(connect_state
);
479 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
480 InetSocketAddress
*sladdr
,
483 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
489 /* lookup peer addr */
490 memset(&ai
,0, sizeof(ai
));
491 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
492 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
493 ai
.ai_socktype
= SOCK_DGRAM
;
496 error_propagate(errp
, err
);
502 if (addr
== NULL
|| strlen(addr
) == 0) {
505 if (port
== NULL
|| strlen(port
) == 0) {
506 error_setg(errp
, "remote port not specified");
510 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
511 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
516 /* lookup local addr */
517 memset(&ai
,0, sizeof(ai
));
518 ai
.ai_flags
= AI_PASSIVE
;
519 ai
.ai_family
= peer
->ai_family
;
520 ai
.ai_socktype
= SOCK_DGRAM
;
525 if (addr
== NULL
|| strlen(addr
) == 0) {
528 if (!port
|| strlen(port
) == 0) {
536 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
537 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
543 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
545 error_setg_errno(errp
, errno
, "Failed to create socket");
548 socket_set_fast_reuse(sock
);
551 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
552 error_setg_errno(errp
, errno
, "Failed to bind socket");
556 /* connect to peer */
557 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
558 error_setg_errno(errp
, errno
, "Failed to connect socket");
580 /* compatibility wrapper */
581 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
583 InetSocketAddress
*addr
;
584 const char *optstr
, *h
;
590 addr
= g_new0(InetSocketAddress
, 1);
596 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
597 error_setg(errp
, "error parsing port in address '%s'", str
);
600 } else if (str
[0] == '[') {
602 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
603 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
606 addr
->ipv6
= addr
->has_ipv6
= true;
608 /* hostname or IPv4 addr */
609 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
610 error_setg(errp
, "error parsing address '%s'", str
);
613 if (host
[strspn(host
, "0123456789.")] == '\0') {
614 addr
->ipv4
= addr
->has_ipv4
= true;
618 addr
->host
= g_strdup(host
);
619 addr
->port
= g_strdup(port
);
623 h
= strstr(optstr
, ",to=");
626 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
627 (h
[pos
] != '\0' && h
[pos
] != ',')) {
628 error_setg(errp
, "error parsing to= argument");
634 if (strstr(optstr
, ",ipv4")) {
635 addr
->ipv4
= addr
->has_ipv4
= true;
637 if (strstr(optstr
, ",ipv6")) {
638 addr
->ipv6
= addr
->has_ipv6
= true;
643 qapi_free_InetSocketAddress(addr
);
649 * Create a blocking socket and connect it to an address.
651 * @str: address string
652 * @errp: set in case of an error
654 * Returns -1 in case of error, file descriptor on success
656 int inet_connect(const char *str
, Error
**errp
)
659 InetSocketAddress
*addr
;
661 addr
= inet_parse(str
, errp
);
663 sock
= inet_connect_saddr(addr
, NULL
, NULL
, errp
);
664 qapi_free_InetSocketAddress(addr
);
669 #ifdef CONFIG_AF_VSOCK
670 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
671 struct sockaddr_vm
*svm
,
674 unsigned long long val
;
676 memset(svm
, 0, sizeof(*svm
));
677 svm
->svm_family
= AF_VSOCK
;
679 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
681 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
686 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
688 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
696 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, bool *in_progress
,
697 ConnectState
*connect_state
, Error
**errp
)
701 *in_progress
= false;
703 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
705 error_setg_errno(errp
, errno
, "Failed to create socket");
708 if (connect_state
!= NULL
) {
709 qemu_set_nonblock(sock
);
711 /* connect to peer */
714 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
717 } while (rc
== -EINTR
);
719 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
720 connect_state
->fd
= sock
;
721 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
724 error_setg_errno(errp
, errno
, "Failed to connect socket");
731 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
,
732 NonBlockingConnectHandler
*callback
,
736 struct sockaddr_vm svm
;
739 ConnectState
*connect_state
= NULL
;
741 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
745 if (callback
!= NULL
) {
746 connect_state
= g_malloc0(sizeof(*connect_state
));
747 connect_state
->callback
= callback
;
748 connect_state
->opaque
= opaque
;
751 sock
= vsock_connect_addr(&svm
, &in_progress
, connect_state
, errp
);
754 } else if (in_progress
) {
755 /* wait_for_connect() will do the rest */
759 callback(sock
, NULL
, opaque
);
762 g_free(connect_state
);
766 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
769 struct sockaddr_vm svm
;
772 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
776 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
778 error_setg_errno(errp
, errno
, "Failed to create socket");
782 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
783 error_setg_errno(errp
, errno
, "Failed to bind socket");
784 closesocket(slisten
);
788 if (listen(slisten
, 1) != 0) {
789 error_setg_errno(errp
, errno
, "Failed to listen on socket");
790 closesocket(slisten
);
796 static VsockSocketAddress
*vsock_parse(const char *str
, Error
**errp
)
798 VsockSocketAddress
*addr
= NULL
;
803 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
804 error_setg(errp
, "error parsing address '%s'", str
);
807 if (str
[n
] != '\0') {
808 error_setg(errp
, "trailing characters in address '%s'", str
);
812 addr
= g_new0(VsockSocketAddress
, 1);
813 addr
->cid
= g_strdup(cid
);
814 addr
->port
= g_strdup(port
);
818 static void vsock_unsupported(Error
**errp
)
820 error_setg(errp
, "socket family AF_VSOCK unsupported");
823 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
,
824 NonBlockingConnectHandler
*callback
,
825 void *opaque
, Error
**errp
)
827 vsock_unsupported(errp
);
831 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
834 vsock_unsupported(errp
);
838 static VsockSocketAddress
*vsock_parse(const char *str
, Error
**errp
)
840 vsock_unsupported(errp
);
843 #endif /* CONFIG_AF_VSOCK */
847 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
851 struct sockaddr_un un
;
854 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
856 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
860 memset(&un
, 0, sizeof(un
));
861 un
.sun_family
= AF_UNIX
;
862 if (saddr
->path
&& strlen(saddr
->path
)) {
863 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
865 const char *tmpdir
= getenv("TMPDIR");
866 tmpdir
= tmpdir
? tmpdir
: "/tmp";
867 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
868 tmpdir
) >= sizeof(un
.sun_path
)) {
869 error_setg_errno(errp
, errno
,
870 "TMPDIR environment variable (%s) too large", tmpdir
);
875 * This dummy fd usage silences the mktemp() unsecure warning.
876 * Using mkstemp() doesn't make things more secure here
877 * though. bind() complains about existing files, so we have
878 * to unlink first and thus re-open the race window. The
879 * worst case possible is bind() failing, i.e. a DoS attack.
881 fd
= mkstemp(un
.sun_path
);
883 error_setg_errno(errp
, errno
,
884 "Failed to make a temporary socket name in %s", tmpdir
);
890 saddr
->path
= g_strdup(un
.sun_path
);
894 if (unlink(un
.sun_path
) < 0 && errno
!= ENOENT
) {
895 error_setg_errno(errp
, errno
,
896 "Failed to unlink socket %s", un
.sun_path
);
899 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
900 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
903 if (listen(sock
, 1) < 0) {
904 error_setg_errno(errp
, errno
, "Failed to listen on socket");
915 static int unix_connect_saddr(UnixSocketAddress
*saddr
,
916 NonBlockingConnectHandler
*callback
, void *opaque
,
919 struct sockaddr_un un
;
920 ConnectState
*connect_state
= NULL
;
923 if (saddr
->path
== NULL
) {
924 error_setg(errp
, "unix connect: no path specified");
928 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
930 error_setg_errno(errp
, errno
, "Failed to create socket");
933 if (callback
!= NULL
) {
934 connect_state
= g_malloc0(sizeof(*connect_state
));
935 connect_state
->callback
= callback
;
936 connect_state
->opaque
= opaque
;
937 qemu_set_nonblock(sock
);
940 memset(&un
, 0, sizeof(un
));
941 un
.sun_family
= AF_UNIX
;
942 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
944 /* connect to peer */
947 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
950 } while (rc
== -EINTR
);
952 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
953 connect_state
->fd
= sock
;
954 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
956 } else if (rc
>= 0) {
957 /* non blocking socket immediate success, call callback */
958 if (callback
!= NULL
) {
959 callback(sock
, NULL
, opaque
);
964 error_setg_errno(errp
, -rc
, "Failed to connect socket");
969 g_free(connect_state
);
975 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
979 error_setg(errp
, "unix sockets are not available on windows");
984 static int unix_connect_saddr(UnixSocketAddress
*saddr
,
985 NonBlockingConnectHandler
*callback
, void *opaque
,
988 error_setg(errp
, "unix sockets are not available on windows");
994 /* compatibility wrapper */
995 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
999 UnixSocketAddress
*saddr
;
1001 saddr
= g_new0(UnixSocketAddress
, 1);
1003 optstr
= strchr(str
, ',');
1007 path
= g_malloc(len
+1);
1008 snprintf(path
, len
+1, "%.*s", len
, str
);
1012 saddr
->path
= g_strdup(str
);
1015 sock
= unix_listen_saddr(saddr
, true, errp
);
1017 if (sock
!= -1 && ostr
) {
1018 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
1021 qapi_free_UnixSocketAddress(saddr
);
1025 int unix_connect(const char *path
, Error
**errp
)
1027 UnixSocketAddress
*saddr
;
1030 saddr
= g_new0(UnixSocketAddress
, 1);
1031 saddr
->path
= g_strdup(path
);
1032 sock
= unix_connect_saddr(saddr
, NULL
, NULL
, errp
);
1033 qapi_free_UnixSocketAddress(saddr
);
1038 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1040 SocketAddress
*addr
;
1042 addr
= g_new0(SocketAddress
, 1);
1043 if (strstart(str
, "unix:", NULL
)) {
1044 if (str
[5] == '\0') {
1045 error_setg(errp
, "invalid Unix socket address");
1048 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1049 addr
->u
.q_unix
.data
= g_new(UnixSocketAddress
, 1);
1050 addr
->u
.q_unix
.data
->path
= g_strdup(str
+ 5);
1052 } else if (strstart(str
, "fd:", NULL
)) {
1053 if (str
[3] == '\0') {
1054 error_setg(errp
, "invalid file descriptor address");
1057 addr
->type
= SOCKET_ADDRESS_KIND_FD
;
1058 addr
->u
.fd
.data
= g_new(String
, 1);
1059 addr
->u
.fd
.data
->str
= g_strdup(str
+ 3);
1061 } else if (strstart(str
, "vsock:", NULL
)) {
1062 addr
->type
= SOCKET_ADDRESS_KIND_VSOCK
;
1063 addr
->u
.vsock
.data
= vsock_parse(str
+ strlen("vsock:"), errp
);
1064 if (addr
->u
.vsock
.data
== NULL
) {
1068 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1069 addr
->u
.inet
.data
= inet_parse(str
, errp
);
1070 if (addr
->u
.inet
.data
== NULL
) {
1077 qapi_free_SocketAddress(addr
);
1081 int socket_connect(SocketAddress
*addr
, NonBlockingConnectHandler
*callback
,
1082 void *opaque
, Error
**errp
)
1086 switch (addr
->type
) {
1087 case SOCKET_ADDRESS_KIND_INET
:
1088 fd
= inet_connect_saddr(addr
->u
.inet
.data
, callback
, opaque
, errp
);
1091 case SOCKET_ADDRESS_KIND_UNIX
:
1092 fd
= unix_connect_saddr(addr
->u
.q_unix
.data
, callback
, opaque
, errp
);
1095 case SOCKET_ADDRESS_KIND_FD
:
1096 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
1097 if (fd
>= 0 && callback
) {
1098 qemu_set_nonblock(fd
);
1099 callback(fd
, NULL
, opaque
);
1103 case SOCKET_ADDRESS_KIND_VSOCK
:
1104 fd
= vsock_connect_saddr(addr
->u
.vsock
.data
, callback
, opaque
, errp
);
1113 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1117 switch (addr
->type
) {
1118 case SOCKET_ADDRESS_KIND_INET
:
1119 fd
= inet_listen_saddr(addr
->u
.inet
.data
, 0, false, errp
);
1122 case SOCKET_ADDRESS_KIND_UNIX
:
1123 fd
= unix_listen_saddr(addr
->u
.q_unix
.data
, false, errp
);
1126 case SOCKET_ADDRESS_KIND_FD
:
1127 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
1130 case SOCKET_ADDRESS_KIND_VSOCK
:
1131 fd
= vsock_listen_saddr(addr
->u
.vsock
.data
, errp
);
1140 void socket_listen_cleanup(int fd
, Error
**errp
)
1142 SocketAddress
*addr
;
1144 addr
= socket_local_address(fd
, errp
);
1146 if (addr
->type
== SOCKET_ADDRESS_KIND_UNIX
1147 && addr
->u
.q_unix
.data
->path
) {
1148 if (unlink(addr
->u
.q_unix
.data
->path
) < 0 && errno
!= ENOENT
) {
1149 error_setg_errno(errp
, errno
,
1150 "Failed to unlink socket %s",
1151 addr
->u
.q_unix
.data
->path
);
1155 qapi_free_SocketAddress(addr
);
1158 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1163 * TODO SOCKET_ADDRESS_KIND_FD when fd is AF_INET or AF_INET6
1164 * (although other address families can do SOCK_DGRAM, too)
1166 switch (remote
->type
) {
1167 case SOCKET_ADDRESS_KIND_INET
:
1168 fd
= inet_dgram_saddr(remote
->u
.inet
.data
,
1169 local
? local
->u
.inet
.data
: NULL
, errp
);
1173 error_setg(errp
, "socket type unsupported for datagram");
1180 static SocketAddress
*
1181 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1185 char host
[NI_MAXHOST
];
1186 char serv
[NI_MAXSERV
];
1187 SocketAddress
*addr
;
1188 InetSocketAddress
*inet
;
1191 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1194 NI_NUMERICHOST
| NI_NUMERICSERV
);
1196 error_setg(errp
, "Cannot format numeric socket address: %s",
1201 addr
= g_new0(SocketAddress
, 1);
1202 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1203 inet
= addr
->u
.inet
.data
= g_new0(InetSocketAddress
, 1);
1204 inet
->host
= g_strdup(host
);
1205 inet
->port
= g_strdup(serv
);
1206 if (sa
->ss_family
== AF_INET
) {
1207 inet
->has_ipv4
= inet
->ipv4
= true;
1209 inet
->has_ipv6
= inet
->ipv6
= true;
1217 static SocketAddress
*
1218 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1222 SocketAddress
*addr
;
1223 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1225 addr
= g_new0(SocketAddress
, 1);
1226 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1227 addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1228 if (su
->sun_path
[0]) {
1229 addr
->u
.q_unix
.data
->path
= g_strndup(su
->sun_path
,
1230 sizeof(su
->sun_path
));
1237 #ifdef CONFIG_AF_VSOCK
1238 static SocketAddress
*
1239 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1243 SocketAddress
*addr
;
1244 VsockSocketAddress
*vaddr
;
1245 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1247 addr
= g_new0(SocketAddress
, 1);
1248 addr
->type
= SOCKET_ADDRESS_KIND_VSOCK
;
1249 addr
->u
.vsock
.data
= vaddr
= g_new0(VsockSocketAddress
, 1);
1250 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1251 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1255 #endif /* CONFIG_AF_VSOCK */
1258 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1262 switch (sa
->ss_family
) {
1265 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1269 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1272 #ifdef CONFIG_AF_VSOCK
1274 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1278 error_setg(errp
, "socket family %d unsupported",
1286 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1288 struct sockaddr_storage ss
;
1289 socklen_t sslen
= sizeof(ss
);
1291 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1292 error_setg_errno(errp
, errno
, "%s",
1293 "Unable to query local socket address");
1297 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1301 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1303 struct sockaddr_storage ss
;
1304 socklen_t sslen
= sizeof(ss
);
1306 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1307 error_setg_errno(errp
, errno
, "%s",
1308 "Unable to query remote socket address");
1312 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1315 char *socket_address_to_string(struct SocketAddress
*addr
, Error
**errp
)
1318 InetSocketAddress
*inet
;
1320 switch (addr
->type
) {
1321 case SOCKET_ADDRESS_KIND_INET
:
1322 inet
= addr
->u
.inet
.data
;
1323 if (strchr(inet
->host
, ':') == NULL
) {
1324 buf
= g_strdup_printf("%s:%s", inet
->host
, inet
->port
);
1326 buf
= g_strdup_printf("[%s]:%s", inet
->host
, inet
->port
);
1330 case SOCKET_ADDRESS_KIND_UNIX
:
1331 buf
= g_strdup(addr
->u
.q_unix
.data
->path
);
1334 case SOCKET_ADDRESS_KIND_FD
:
1335 buf
= g_strdup(addr
->u
.fd
.data
->str
);
1338 case SOCKET_ADDRESS_KIND_VSOCK
:
1339 buf
= g_strdup_printf("%s:%s",
1340 addr
->u
.vsock
.data
->cid
,
1341 addr
->u
.vsock
.data
->port
);
1350 SocketAddress
*socket_address_crumple(SocketAddressFlat
*addr_flat
)
1352 SocketAddress
*addr
= g_new(SocketAddress
, 1);
1354 switch (addr_flat
->type
) {
1355 case SOCKET_ADDRESS_FLAT_TYPE_INET
:
1356 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1357 addr
->u
.inet
.data
= QAPI_CLONE(InetSocketAddress
,
1358 &addr_flat
->u
.inet
);
1360 case SOCKET_ADDRESS_FLAT_TYPE_UNIX
:
1361 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1362 addr
->u
.q_unix
.data
= QAPI_CLONE(UnixSocketAddress
,
1363 &addr_flat
->u
.q_unix
);
1365 case SOCKET_ADDRESS_FLAT_TYPE_VSOCK
:
1366 addr
->type
= SOCKET_ADDRESS_KIND_VSOCK
;
1367 addr
->u
.vsock
.data
= QAPI_CLONE(VsockSocketAddress
,
1368 &addr_flat
->u
.vsock
);
1370 case SOCKET_ADDRESS_FLAT_TYPE_FD
:
1371 addr
->type
= SOCKET_ADDRESS_KIND_FD
;
1372 addr
->u
.fd
.data
= QAPI_CLONE(String
, &addr_flat
->u
.fd
);