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/clone-visitor.h"
26 #include "qapi/error.h"
27 #include "qemu/sockets.h"
28 #include "qemu/main-loop.h"
29 #include "qapi/clone-visitor.h"
30 #include "qapi/qobject-input-visitor.h"
31 #include "qapi/qobject-output-visitor.h"
32 #include "qapi-visit.h"
33 #include "qemu/cutils.h"
36 # define AI_ADDRCONFIG 0
40 # define AI_V4MAPPED 0
43 #ifndef AI_NUMERICSERV
44 # define AI_NUMERICSERV 0
48 static int inet_getport(struct addrinfo
*e
)
50 struct sockaddr_in
*i4
;
51 struct sockaddr_in6
*i6
;
53 switch (e
->ai_family
) {
55 i6
= (void*)e
->ai_addr
;
56 return ntohs(i6
->sin6_port
);
58 i4
= (void*)e
->ai_addr
;
59 return ntohs(i4
->sin_port
);
65 static void inet_setport(struct addrinfo
*e
, int port
)
67 struct sockaddr_in
*i4
;
68 struct sockaddr_in6
*i6
;
70 switch (e
->ai_family
) {
72 i6
= (void*)e
->ai_addr
;
73 i6
->sin6_port
= htons(port
);
76 i4
= (void*)e
->ai_addr
;
77 i4
->sin_port
= htons(port
);
82 NetworkAddressFamily
inet_netfamily(int family
)
85 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
86 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
87 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
88 #ifdef CONFIG_AF_VSOCK
89 case PF_VSOCK
: return NETWORK_ADDRESS_FAMILY_VSOCK
;
90 #endif /* CONFIG_AF_VSOCK */
92 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
96 * Matrix we're trying to apply
107 * t t PF_INET6/PF_UNSPEC
109 * NB, this matrix is only about getting the necessary results
110 * from getaddrinfo(). Some of the cases require further work
111 * after reading results from getaddrinfo in order to fully
112 * apply the logic the end user wants.
114 * In the first and last cases, we must set IPV6_V6ONLY=0
115 * when binding, to allow a single listener to potentially
116 * accept both IPv4+6 addresses.
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
)) {
128 * Some backends can only do a single listener. In that case
129 * we want empty hostname to resolve to "::" and then use the
130 * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This
131 * doesn't work for addresses other than "", so they're just
132 * inevitably broken until multiple listeners can be used,
133 * and thus we honour getaddrinfo automatic protocol detection
134 * Once all backends do multi-listener, remove the PF_INET6
137 if (!addr
->host
|| g_str_equal(addr
->host
, "")) {
143 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
146 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
152 static int inet_listen_saddr(InetSocketAddress
*saddr
,
157 struct addrinfo ai
,*res
,*e
;
159 char uaddr
[INET6_ADDRSTRLEN
+1];
161 int slisten
, rc
, port_min
, port_max
, p
;
164 memset(&ai
,0, sizeof(ai
));
165 ai
.ai_flags
= AI_PASSIVE
;
166 if (saddr
->has_numeric
&& saddr
->numeric
) {
167 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
169 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
170 ai
.ai_socktype
= SOCK_STREAM
;
173 error_propagate(errp
, err
);
177 if (saddr
->host
== NULL
) {
178 error_setg(errp
, "host not specified");
181 if (saddr
->port
!= NULL
) {
182 pstrcpy(port
, sizeof(port
), saddr
->port
);
189 unsigned long long baseport
;
190 if (strlen(port
) == 0) {
191 error_setg(errp
, "port not specified");
194 if (parse_uint_full(port
, &baseport
, 10) < 0) {
195 error_setg(errp
, "can't convert to a number: %s", port
);
198 if (baseport
> 65535 ||
199 baseport
+ port_offset
> 65535) {
200 error_setg(errp
, "port %s out of range", port
);
203 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
205 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
206 strlen(port
) ? port
: NULL
, &ai
, &res
);
208 error_setg(errp
, "address resolution failed for %s:%s: %s",
209 saddr
->host
, port
, gai_strerror(rc
));
213 /* create socket + bind */
214 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
215 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
216 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
217 NI_NUMERICHOST
| NI_NUMERICSERV
);
218 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
221 error_setg_errno(errp
, errno
, "Failed to create socket");
226 socket_set_fast_reuse(slisten
);
228 port_min
= inet_getport(e
);
229 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
230 for (p
= port_min
; p
<= port_max
; p
++) {
233 * Deals with first & last cases in matrix in comment
234 * for inet_ai_family_from_address().
237 ((!saddr
->has_ipv4
&& !saddr
->has_ipv6
) ||
238 (saddr
->has_ipv4
&& saddr
->ipv4
&&
239 saddr
->has_ipv6
&& saddr
->ipv6
)) ? 0 : 1;
244 if (e
->ai_family
== PF_INET6
) {
245 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
,
249 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
254 /* If we got EADDRINUSE from an IPv6 bind & V6ONLY is unset,
255 * it could be that the IPv4 port is already claimed, so retry
258 if (e
->ai_family
== PF_INET6
&& errno
== EADDRINUSE
&& !v6only
) {
266 error_setg_errno(errp
, errno
, "Failed to bind socket");
270 closesocket(slisten
);
276 if (listen(slisten
,1) != 0) {
277 error_setg_errno(errp
, errno
, "Failed to listen on socket");
278 closesocket(slisten
);
284 saddr
->host
= g_strdup(uaddr
);
286 saddr
->port
= g_strdup_printf("%d",
287 inet_getport(e
) - port_offset
);
288 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
289 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
296 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
297 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
299 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
300 ((rc) == -EINPROGRESS)
303 /* Struct to store connect state for non blocking connect */
304 typedef struct ConnectState
{
306 struct addrinfo
*addr_list
;
307 struct addrinfo
*current_addr
;
308 NonBlockingConnectHandler
*callback
;
312 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
313 ConnectState
*connect_state
, Error
**errp
);
315 static void wait_for_connect(void *opaque
)
317 ConnectState
*s
= opaque
;
319 socklen_t valsize
= sizeof(val
);
323 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
326 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
327 } while (rc
== -1 && errno
== EINTR
);
329 /* update rc to contain error */
337 error_setg_errno(&err
, errno
, "Error connecting to socket");
342 /* try to connect to the next address on the list */
343 if (s
->current_addr
) {
344 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
345 s
->current_addr
= s
->current_addr
->ai_next
;
346 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
350 error_setg_errno(&err
, errno
, "Unable to start socket connect");
352 /* connect in progress */
358 freeaddrinfo(s
->addr_list
);
362 s
->callback(s
->fd
, err
, s
->opaque
);
369 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
370 ConnectState
*connect_state
, Error
**errp
)
374 *in_progress
= false;
376 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
378 error_setg_errno(errp
, errno
, "Failed to create socket");
381 socket_set_fast_reuse(sock
);
382 if (connect_state
!= NULL
) {
383 qemu_set_nonblock(sock
);
385 /* connect to peer */
388 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
391 } while (rc
== -EINTR
);
393 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
394 connect_state
->fd
= sock
;
395 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
398 error_setg_errno(errp
, errno
, "Failed to connect socket");
405 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
408 struct addrinfo ai
, *res
;
411 static int useV4Mapped
= 1;
413 memset(&ai
, 0, sizeof(ai
));
415 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
416 if (atomic_read(&useV4Mapped
)) {
417 ai
.ai_flags
|= AI_V4MAPPED
;
419 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
420 ai
.ai_socktype
= SOCK_STREAM
;
423 error_propagate(errp
, err
);
427 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
428 error_setg(errp
, "host and/or port not specified");
433 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
435 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
436 * then don't implement it in their getaddrinfo(). Detect
437 * this and retry without the flag since that's preferrable
440 if (rc
== EAI_BADFLAGS
&&
441 (ai
.ai_flags
& AI_V4MAPPED
)) {
442 atomic_set(&useV4Mapped
, 0);
443 ai
.ai_flags
&= ~AI_V4MAPPED
;
444 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
447 error_setg(errp
, "address resolution failed for %s:%s: %s",
448 saddr
->host
, saddr
->port
, gai_strerror(rc
));
455 * Create a socket and connect it to an address.
457 * @saddr: Inet socket address specification
458 * @errp: set on error
459 * @callback: callback function for non-blocking connect
460 * @opaque: opaque for callback function
462 * Returns: -1 on error, file descriptor on success.
464 * If @callback is non-null, the connect is non-blocking. If this
465 * function succeeds, callback will be called when the connection
466 * completes, with the file descriptor on success, or -1 on error.
468 int inet_connect_saddr(InetSocketAddress
*saddr
,
469 NonBlockingConnectHandler
*callback
, void *opaque
,
472 Error
*local_err
= NULL
;
473 struct addrinfo
*res
, *e
;
476 ConnectState
*connect_state
= NULL
;
478 res
= inet_parse_connect_saddr(saddr
, errp
);
483 if (callback
!= NULL
) {
484 connect_state
= g_malloc0(sizeof(*connect_state
));
485 connect_state
->addr_list
= res
;
486 connect_state
->callback
= callback
;
487 connect_state
->opaque
= opaque
;
490 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
491 error_free(local_err
);
493 if (connect_state
!= NULL
) {
494 connect_state
->current_addr
= e
;
496 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
503 error_propagate(errp
, local_err
);
504 } else if (in_progress
) {
505 /* wait_for_connect() will do the rest */
509 callback(sock
, NULL
, opaque
);
512 g_free(connect_state
);
517 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
518 InetSocketAddress
*sladdr
,
521 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
527 /* lookup peer addr */
528 memset(&ai
,0, sizeof(ai
));
529 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
530 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
531 ai
.ai_socktype
= SOCK_DGRAM
;
534 error_propagate(errp
, err
);
540 if (addr
== NULL
|| strlen(addr
) == 0) {
543 if (port
== NULL
|| strlen(port
) == 0) {
544 error_setg(errp
, "remote port not specified");
548 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
549 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
554 /* lookup local addr */
555 memset(&ai
,0, sizeof(ai
));
556 ai
.ai_flags
= AI_PASSIVE
;
557 ai
.ai_family
= peer
->ai_family
;
558 ai
.ai_socktype
= SOCK_DGRAM
;
563 if (addr
== NULL
|| strlen(addr
) == 0) {
566 if (!port
|| strlen(port
) == 0) {
574 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
575 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
581 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
583 error_setg_errno(errp
, errno
, "Failed to create socket");
586 socket_set_fast_reuse(sock
);
589 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
590 error_setg_errno(errp
, errno
, "Failed to bind socket");
594 /* connect to peer */
595 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
596 error_setg_errno(errp
, errno
, "Failed to connect socket");
618 /* compatibility wrapper */
619 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
621 const char *optstr
, *h
;
627 memset(addr
, 0, sizeof(*addr
));
633 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
634 error_setg(errp
, "error parsing port in address '%s'", str
);
637 } else if (str
[0] == '[') {
639 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
640 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
644 /* hostname or IPv4 addr */
645 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
646 error_setg(errp
, "error parsing address '%s'", str
);
651 addr
->host
= g_strdup(host
);
652 addr
->port
= g_strdup(port
);
656 h
= strstr(optstr
, ",to=");
659 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
660 (h
[pos
] != '\0' && h
[pos
] != ',')) {
661 error_setg(errp
, "error parsing to= argument");
667 if (strstr(optstr
, ",ipv4")) {
668 addr
->ipv4
= addr
->has_ipv4
= true;
670 if (strstr(optstr
, ",ipv6")) {
671 addr
->ipv6
= addr
->has_ipv6
= true;
678 * Create a blocking socket and connect it to an address.
680 * @str: address string
681 * @errp: set in case of an error
683 * Returns -1 in case of error, file descriptor on success
685 int inet_connect(const char *str
, Error
**errp
)
688 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
690 if (!inet_parse(addr
, str
, errp
)) {
691 sock
= inet_connect_saddr(addr
, NULL
, NULL
, errp
);
693 qapi_free_InetSocketAddress(addr
);
697 #ifdef CONFIG_AF_VSOCK
698 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
699 struct sockaddr_vm
*svm
,
702 unsigned long long val
;
704 memset(svm
, 0, sizeof(*svm
));
705 svm
->svm_family
= AF_VSOCK
;
707 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
709 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
714 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
716 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
724 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, bool *in_progress
,
725 ConnectState
*connect_state
, Error
**errp
)
729 *in_progress
= false;
731 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
733 error_setg_errno(errp
, errno
, "Failed to create socket");
736 if (connect_state
!= NULL
) {
737 qemu_set_nonblock(sock
);
739 /* connect to peer */
742 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
745 } while (rc
== -EINTR
);
747 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
748 connect_state
->fd
= sock
;
749 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
752 error_setg_errno(errp
, errno
, "Failed to connect socket");
759 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
,
760 NonBlockingConnectHandler
*callback
,
764 struct sockaddr_vm svm
;
767 ConnectState
*connect_state
= NULL
;
769 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
773 if (callback
!= NULL
) {
774 connect_state
= g_malloc0(sizeof(*connect_state
));
775 connect_state
->callback
= callback
;
776 connect_state
->opaque
= opaque
;
779 sock
= vsock_connect_addr(&svm
, &in_progress
, connect_state
, errp
);
782 } else if (in_progress
) {
783 /* wait_for_connect() will do the rest */
787 callback(sock
, NULL
, opaque
);
790 g_free(connect_state
);
794 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
797 struct sockaddr_vm svm
;
800 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
804 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
806 error_setg_errno(errp
, errno
, "Failed to create socket");
810 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
811 error_setg_errno(errp
, errno
, "Failed to bind socket");
812 closesocket(slisten
);
816 if (listen(slisten
, 1) != 0) {
817 error_setg_errno(errp
, errno
, "Failed to listen on socket");
818 closesocket(slisten
);
824 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
831 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
832 error_setg(errp
, "error parsing address '%s'", str
);
835 if (str
[n
] != '\0') {
836 error_setg(errp
, "trailing characters in address '%s'", str
);
840 addr
->cid
= g_strdup(cid
);
841 addr
->port
= g_strdup(port
);
845 static void vsock_unsupported(Error
**errp
)
847 error_setg(errp
, "socket family AF_VSOCK unsupported");
850 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
,
851 NonBlockingConnectHandler
*callback
,
852 void *opaque
, Error
**errp
)
854 vsock_unsupported(errp
);
858 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
861 vsock_unsupported(errp
);
865 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
868 vsock_unsupported(errp
);
871 #endif /* CONFIG_AF_VSOCK */
875 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
879 struct sockaddr_un un
;
881 char *pathbuf
= NULL
;
884 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
886 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
890 if (saddr
->path
&& saddr
->path
[0]) {
893 const char *tmpdir
= getenv("TMPDIR");
894 tmpdir
= tmpdir
? tmpdir
: "/tmp";
895 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
898 if (strlen(path
) > sizeof(un
.sun_path
)) {
899 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
900 error_append_hint(errp
, "Path must be less than %zu bytes\n",
901 sizeof(un
.sun_path
));
905 if (pathbuf
!= NULL
) {
907 * This dummy fd usage silences the mktemp() unsecure warning.
908 * Using mkstemp() doesn't make things more secure here
909 * though. bind() complains about existing files, so we have
910 * to unlink first and thus re-open the race window. The
911 * worst case possible is bind() failing, i.e. a DoS attack.
913 fd
= mkstemp(pathbuf
);
915 error_setg_errno(errp
, errno
,
916 "Failed to make a temporary socket %s", pathbuf
);
922 if (unlink(path
) < 0 && errno
!= ENOENT
) {
923 error_setg_errno(errp
, errno
,
924 "Failed to unlink socket %s", path
);
928 memset(&un
, 0, sizeof(un
));
929 un
.sun_family
= AF_UNIX
;
930 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
));
932 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
933 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
936 if (listen(sock
, 1) < 0) {
937 error_setg_errno(errp
, errno
, "Failed to listen on socket");
941 if (update_addr
&& pathbuf
) {
943 saddr
->path
= pathbuf
;
955 static int unix_connect_saddr(UnixSocketAddress
*saddr
,
956 NonBlockingConnectHandler
*callback
, void *opaque
,
959 struct sockaddr_un un
;
960 ConnectState
*connect_state
= NULL
;
963 if (saddr
->path
== NULL
) {
964 error_setg(errp
, "unix connect: no path specified");
968 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
970 error_setg_errno(errp
, errno
, "Failed to create socket");
973 if (callback
!= NULL
) {
974 connect_state
= g_malloc0(sizeof(*connect_state
));
975 connect_state
->callback
= callback
;
976 connect_state
->opaque
= opaque
;
977 qemu_set_nonblock(sock
);
980 if (strlen(saddr
->path
) > sizeof(un
.sun_path
)) {
981 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
982 error_append_hint(errp
, "Path must be less than %zu bytes\n",
983 sizeof(un
.sun_path
));
987 memset(&un
, 0, sizeof(un
));
988 un
.sun_family
= AF_UNIX
;
989 strncpy(un
.sun_path
, saddr
->path
, sizeof(un
.sun_path
));
991 /* connect to peer */
994 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
997 } while (rc
== -EINTR
);
999 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
1000 connect_state
->fd
= sock
;
1001 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
1003 } else if (rc
>= 0) {
1004 /* non blocking socket immediate success, call callback */
1005 if (callback
!= NULL
) {
1006 callback(sock
, NULL
, opaque
);
1011 error_setg_errno(errp
, -rc
, "Failed to connect socket %s",
1016 g_free(connect_state
);
1021 g_free(connect_state
);
1027 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
1031 error_setg(errp
, "unix sockets are not available on windows");
1036 static int unix_connect_saddr(UnixSocketAddress
*saddr
,
1037 NonBlockingConnectHandler
*callback
, void *opaque
,
1040 error_setg(errp
, "unix sockets are not available on windows");
1046 /* compatibility wrapper */
1047 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
1049 char *path
, *optstr
;
1051 UnixSocketAddress
*saddr
;
1053 saddr
= g_new0(UnixSocketAddress
, 1);
1055 optstr
= strchr(str
, ',');
1059 path
= g_malloc(len
+1);
1060 snprintf(path
, len
+1, "%.*s", len
, str
);
1064 saddr
->path
= g_strdup(str
);
1067 sock
= unix_listen_saddr(saddr
, true, errp
);
1069 if (sock
!= -1 && ostr
) {
1070 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
1073 qapi_free_UnixSocketAddress(saddr
);
1077 int unix_connect(const char *path
, Error
**errp
)
1079 UnixSocketAddress
*saddr
;
1082 saddr
= g_new0(UnixSocketAddress
, 1);
1083 saddr
->path
= g_strdup(path
);
1084 sock
= unix_connect_saddr(saddr
, NULL
, NULL
, errp
);
1085 qapi_free_UnixSocketAddress(saddr
);
1090 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1092 SocketAddress
*addr
;
1094 addr
= g_new0(SocketAddress
, 1);
1095 if (strstart(str
, "unix:", NULL
)) {
1096 if (str
[5] == '\0') {
1097 error_setg(errp
, "invalid Unix socket address");
1100 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1101 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1103 } else if (strstart(str
, "fd:", NULL
)) {
1104 if (str
[3] == '\0') {
1105 error_setg(errp
, "invalid file descriptor address");
1108 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1109 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1111 } else if (strstart(str
, "vsock:", NULL
)) {
1112 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1113 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1117 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1118 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1125 qapi_free_SocketAddress(addr
);
1129 int socket_connect(SocketAddress
*addr
, NonBlockingConnectHandler
*callback
,
1130 void *opaque
, Error
**errp
)
1134 switch (addr
->type
) {
1135 case SOCKET_ADDRESS_TYPE_INET
:
1136 fd
= inet_connect_saddr(&addr
->u
.inet
, callback
, opaque
, errp
);
1139 case SOCKET_ADDRESS_TYPE_UNIX
:
1140 fd
= unix_connect_saddr(&addr
->u
.q_unix
, callback
, opaque
, errp
);
1143 case SOCKET_ADDRESS_TYPE_FD
:
1144 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.str
, errp
);
1145 if (fd
>= 0 && callback
) {
1146 qemu_set_nonblock(fd
);
1147 callback(fd
, NULL
, opaque
);
1151 case SOCKET_ADDRESS_TYPE_VSOCK
:
1152 fd
= vsock_connect_saddr(&addr
->u
.vsock
, callback
, opaque
, errp
);
1161 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1165 switch (addr
->type
) {
1166 case SOCKET_ADDRESS_TYPE_INET
:
1167 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, false, errp
);
1170 case SOCKET_ADDRESS_TYPE_UNIX
:
1171 fd
= unix_listen_saddr(&addr
->u
.q_unix
, false, errp
);
1174 case SOCKET_ADDRESS_TYPE_FD
:
1175 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.str
, errp
);
1178 case SOCKET_ADDRESS_TYPE_VSOCK
:
1179 fd
= vsock_listen_saddr(&addr
->u
.vsock
, errp
);
1188 void socket_listen_cleanup(int fd
, Error
**errp
)
1190 SocketAddress
*addr
;
1192 addr
= socket_local_address(fd
, errp
);
1194 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1195 && addr
->u
.q_unix
.path
) {
1196 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1197 error_setg_errno(errp
, errno
,
1198 "Failed to unlink socket %s",
1199 addr
->u
.q_unix
.path
);
1203 qapi_free_SocketAddress(addr
);
1206 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1211 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1212 * (although other address families can do SOCK_DGRAM, too)
1214 switch (remote
->type
) {
1215 case SOCKET_ADDRESS_TYPE_INET
:
1216 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1217 local
? &local
->u
.inet
: NULL
, errp
);
1221 error_setg(errp
, "socket type unsupported for datagram");
1228 static SocketAddress
*
1229 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1233 char host
[NI_MAXHOST
];
1234 char serv
[NI_MAXSERV
];
1235 SocketAddress
*addr
;
1236 InetSocketAddress
*inet
;
1239 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1242 NI_NUMERICHOST
| NI_NUMERICSERV
);
1244 error_setg(errp
, "Cannot format numeric socket address: %s",
1249 addr
= g_new0(SocketAddress
, 1);
1250 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1251 inet
= &addr
->u
.inet
;
1252 inet
->host
= g_strdup(host
);
1253 inet
->port
= g_strdup(serv
);
1254 if (sa
->ss_family
== AF_INET
) {
1255 inet
->has_ipv4
= inet
->ipv4
= true;
1257 inet
->has_ipv6
= inet
->ipv6
= true;
1265 static SocketAddress
*
1266 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1270 SocketAddress
*addr
;
1271 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1273 addr
= g_new0(SocketAddress
, 1);
1274 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1275 if (su
->sun_path
[0]) {
1276 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, sizeof(su
->sun_path
));
1283 #ifdef CONFIG_AF_VSOCK
1284 static SocketAddress
*
1285 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1289 SocketAddress
*addr
;
1290 VsockSocketAddress
*vaddr
;
1291 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1293 addr
= g_new0(SocketAddress
, 1);
1294 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1295 vaddr
= &addr
->u
.vsock
;
1296 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1297 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1301 #endif /* CONFIG_AF_VSOCK */
1304 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1308 switch (sa
->ss_family
) {
1311 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1315 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1318 #ifdef CONFIG_AF_VSOCK
1320 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1324 error_setg(errp
, "socket family %d unsupported",
1332 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1334 struct sockaddr_storage ss
;
1335 socklen_t sslen
= sizeof(ss
);
1337 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1338 error_setg_errno(errp
, errno
, "%s",
1339 "Unable to query local socket address");
1343 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1347 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1349 struct sockaddr_storage ss
;
1350 socklen_t sslen
= sizeof(ss
);
1352 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1353 error_setg_errno(errp
, errno
, "%s",
1354 "Unable to query remote socket address");
1358 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1362 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1364 SocketAddress
*addr
;
1370 addr
= g_new(SocketAddress
, 1);
1372 switch (addr_legacy
->type
) {
1373 case SOCKET_ADDRESS_LEGACY_KIND_INET
:
1374 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1375 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1376 addr_legacy
->u
.inet
.data
);
1378 case SOCKET_ADDRESS_LEGACY_KIND_UNIX
:
1379 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1380 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1381 addr_legacy
->u
.q_unix
.data
);
1383 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK
:
1384 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1385 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1386 addr_legacy
->u
.vsock
.data
);
1388 case SOCKET_ADDRESS_LEGACY_KIND_FD
:
1389 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1390 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);