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 "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "qapi/clone-visitor.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-visit-sockets.h"
29 #include "qemu/sockets.h"
30 #include "qemu/main-loop.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qobject-output-visitor.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
;
95 bool fd_is_socket(int fd
)
98 socklen_t optlen
= sizeof(optval
);
99 return !qemu_getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &optval
, &optlen
);
104 * Matrix we're trying to apply
115 * t t PF_INET6/PF_UNSPEC
117 * NB, this matrix is only about getting the necessary results
118 * from getaddrinfo(). Some of the cases require further work
119 * after reading results from getaddrinfo in order to fully
120 * apply the logic the end user wants.
122 * In the first and last cases, we must set IPV6_V6ONLY=0
123 * when binding, to allow a single listener to potentially
124 * accept both IPv4+6 addresses.
126 int inet_ai_family_from_address(InetSocketAddress
*addr
,
129 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
130 !addr
->ipv6
&& !addr
->ipv4
) {
131 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
134 if ((addr
->has_ipv6
&& addr
->ipv6
) && (addr
->has_ipv4
&& addr
->ipv4
)) {
136 * Some backends can only do a single listener. In that case
137 * we want empty hostname to resolve to "::" and then use the
138 * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This
139 * doesn't work for addresses other than "", so they're just
140 * inevitably broken until multiple listeners can be used,
141 * and thus we honour getaddrinfo automatic protocol detection
142 * Once all backends do multi-listener, remove the PF_INET6
145 if (!addr
->host
|| g_str_equal(addr
->host
, "")) {
151 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
154 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
160 static int create_fast_reuse_socket(struct addrinfo
*e
)
162 int slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
166 socket_set_fast_reuse(slisten
);
170 static int try_bind(int socket
, InetSocketAddress
*saddr
, struct addrinfo
*e
)
173 return bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
176 * Deals with first & last cases in matrix in comment
177 * for inet_ai_family_from_address().
180 ((!saddr
->has_ipv4
&& !saddr
->has_ipv6
) ||
181 (saddr
->has_ipv4
&& saddr
->ipv4
&&
182 saddr
->has_ipv6
&& saddr
->ipv6
)) ? 0 : 1;
186 if (e
->ai_family
== PF_INET6
) {
187 qemu_setsockopt(socket
, IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
,
191 stat
= bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
196 /* If we got EADDRINUSE from an IPv6 bind & v6only is unset,
197 * it could be that the IPv4 port is already claimed, so retry
200 if (e
->ai_family
== PF_INET6
&& errno
== EADDRINUSE
&& !v6only
) {
208 static int inet_listen_saddr(InetSocketAddress
*saddr
,
212 struct addrinfo ai
,*res
,*e
;
214 char uaddr
[INET6_ADDRSTRLEN
+1];
216 int rc
, port_min
, port_max
, p
;
219 bool socket_created
= false;
222 memset(&ai
,0, sizeof(ai
));
223 ai
.ai_flags
= AI_PASSIVE
;
224 if (saddr
->has_numeric
&& saddr
->numeric
) {
225 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
227 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
228 ai
.ai_socktype
= SOCK_STREAM
;
231 error_propagate(errp
, err
);
235 if (saddr
->host
== NULL
) {
236 error_setg(errp
, "host not specified");
239 if (saddr
->port
!= NULL
) {
240 pstrcpy(port
, sizeof(port
), saddr
->port
);
247 unsigned long long baseport
;
248 if (strlen(port
) == 0) {
249 error_setg(errp
, "port not specified");
252 if (parse_uint_full(port
, &baseport
, 10) < 0) {
253 error_setg(errp
, "can't convert to a number: %s", port
);
256 if (baseport
> 65535 ||
257 baseport
+ port_offset
> 65535) {
258 error_setg(errp
, "port %s out of range", port
);
261 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
263 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
264 strlen(port
) ? port
: NULL
, &ai
, &res
);
266 error_setg(errp
, "address resolution failed for %s:%s: %s",
267 saddr
->host
, port
, gai_strerror(rc
));
271 /* create socket + bind/listen */
272 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
273 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
274 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
275 NI_NUMERICHOST
| NI_NUMERICSERV
);
277 port_min
= inet_getport(e
);
278 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
279 for (p
= port_min
; p
<= port_max
; p
++) {
282 slisten
= create_fast_reuse_socket(e
);
284 /* First time we expect we might fail to create the socket
285 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
286 * Later iterations should always succeed if first iteration
287 * worked though, so treat that as fatal.
292 error_setg_errno(errp
, errno
,
293 "Failed to recreate failed listening socket");
297 socket_created
= true;
299 rc
= try_bind(slisten
, saddr
, e
);
301 if (errno
!= EADDRINUSE
) {
302 error_setg_errno(errp
, errno
, "Failed to bind socket");
306 if (!listen(slisten
, 1)) {
309 if (errno
!= EADDRINUSE
) {
310 error_setg_errno(errp
, errno
, "Failed to listen on socket");
314 /* Someone else managed to bind to the same port and beat us
315 * to listen on it! Socket semantics does not allow us to
316 * recover from this situation, so we need to recreate the
317 * socket to allow bind attempts for subsequent ports:
319 closesocket(slisten
);
323 error_setg_errno(errp
, errno
,
325 "Failed to find an available port" :
326 "Failed to create a socket");
330 closesocket(slisten
);
342 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
343 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
345 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
346 ((rc) == -EINPROGRESS)
349 static int inet_connect_addr(struct addrinfo
*addr
, Error
**errp
);
351 static int inet_connect_addr(struct addrinfo
*addr
, Error
**errp
)
355 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
357 error_setg_errno(errp
, errno
, "Failed to create socket");
360 socket_set_fast_reuse(sock
);
362 /* connect to peer */
365 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
368 } while (rc
== -EINTR
);
371 error_setg_errno(errp
, errno
, "Failed to connect socket");
379 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
382 struct addrinfo ai
, *res
;
385 static int useV4Mapped
= 1;
387 memset(&ai
, 0, sizeof(ai
));
389 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
390 if (atomic_read(&useV4Mapped
)) {
391 ai
.ai_flags
|= AI_V4MAPPED
;
393 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
394 ai
.ai_socktype
= SOCK_STREAM
;
397 error_propagate(errp
, err
);
401 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
402 error_setg(errp
, "host and/or port not specified");
407 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
409 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
410 * then don't implement it in their getaddrinfo(). Detect
411 * this and retry without the flag since that's preferrable
414 if (rc
== EAI_BADFLAGS
&&
415 (ai
.ai_flags
& AI_V4MAPPED
)) {
416 atomic_set(&useV4Mapped
, 0);
417 ai
.ai_flags
&= ~AI_V4MAPPED
;
418 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
421 error_setg(errp
, "address resolution failed for %s:%s: %s",
422 saddr
->host
, saddr
->port
, gai_strerror(rc
));
429 * Create a socket and connect it to an address.
431 * @saddr: Inet socket address specification
432 * @errp: set on error
434 * Returns: -1 on error, file descriptor on success.
436 int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
)
438 Error
*local_err
= NULL
;
439 struct addrinfo
*res
, *e
;
442 res
= inet_parse_connect_saddr(saddr
, errp
);
447 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
448 error_free(local_err
);
450 sock
= inet_connect_addr(e
, &local_err
);
457 error_propagate(errp
, local_err
);
464 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
465 InetSocketAddress
*sladdr
,
468 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
474 /* lookup peer addr */
475 memset(&ai
,0, sizeof(ai
));
476 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
477 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
478 ai
.ai_socktype
= SOCK_DGRAM
;
481 error_propagate(errp
, err
);
487 if (addr
== NULL
|| strlen(addr
) == 0) {
490 if (port
== NULL
|| strlen(port
) == 0) {
491 error_setg(errp
, "remote port not specified");
495 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
496 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
501 /* lookup local addr */
502 memset(&ai
,0, sizeof(ai
));
503 ai
.ai_flags
= AI_PASSIVE
;
504 ai
.ai_family
= peer
->ai_family
;
505 ai
.ai_socktype
= SOCK_DGRAM
;
510 if (addr
== NULL
|| strlen(addr
) == 0) {
513 if (!port
|| strlen(port
) == 0) {
521 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
522 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
528 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
530 error_setg_errno(errp
, errno
, "Failed to create socket");
533 socket_set_fast_reuse(sock
);
536 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
537 error_setg_errno(errp
, errno
, "Failed to bind socket");
541 /* connect to peer */
542 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
543 error_setg_errno(errp
, errno
, "Failed to connect socket");
565 /* compatibility wrapper */
566 static int inet_parse_flag(const char *flagname
, const char *optstr
, bool *val
,
572 end
= strstr(optstr
, ",");
574 if (end
[1] == ',') { /* Reject 'ipv6=on,,foo' */
575 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
580 len
= strlen(optstr
);
582 if (len
== 0 || (len
== 3 && strncmp(optstr
, "=on", len
) == 0)) {
584 } else if (len
== 4 && strncmp(optstr
, "=off", len
) == 0) {
587 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
593 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
595 const char *optstr
, *h
;
602 memset(addr
, 0, sizeof(*addr
));
608 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
609 error_setg(errp
, "error parsing port in address '%s'", str
);
612 } else if (str
[0] == '[') {
614 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
615 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
619 /* hostname or IPv4 addr */
620 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
621 error_setg(errp
, "error parsing address '%s'", str
);
626 addr
->host
= g_strdup(host
);
627 addr
->port
= g_strdup(port
);
631 h
= strstr(optstr
, ",to=");
634 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
635 (h
[pos
] != '\0' && h
[pos
] != ',')) {
636 error_setg(errp
, "error parsing to= argument");
642 begin
= strstr(optstr
, ",ipv4");
644 if (inet_parse_flag("ipv4", begin
+ 5, &addr
->ipv4
, errp
) < 0) {
647 addr
->has_ipv4
= true;
649 begin
= strstr(optstr
, ",ipv6");
651 if (inet_parse_flag("ipv6", begin
+ 5, &addr
->ipv6
, errp
) < 0) {
654 addr
->has_ipv6
= true;
661 * Create a blocking socket and connect it to an address.
663 * @str: address string
664 * @errp: set in case of an error
666 * Returns -1 in case of error, file descriptor on success
668 int inet_connect(const char *str
, Error
**errp
)
671 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
673 if (!inet_parse(addr
, str
, errp
)) {
674 sock
= inet_connect_saddr(addr
, errp
);
676 qapi_free_InetSocketAddress(addr
);
680 #ifdef CONFIG_AF_VSOCK
681 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
682 struct sockaddr_vm
*svm
,
685 unsigned long long val
;
687 memset(svm
, 0, sizeof(*svm
));
688 svm
->svm_family
= AF_VSOCK
;
690 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
692 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
697 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
699 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
707 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, Error
**errp
)
711 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
713 error_setg_errno(errp
, errno
, "Failed to create socket");
717 /* connect to peer */
720 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
723 } while (rc
== -EINTR
);
726 error_setg_errno(errp
, errno
, "Failed to connect socket");
734 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
736 struct sockaddr_vm svm
;
739 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
743 sock
= vsock_connect_addr(&svm
, errp
);
748 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
751 struct sockaddr_vm svm
;
754 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
758 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
760 error_setg_errno(errp
, errno
, "Failed to create socket");
764 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
765 error_setg_errno(errp
, errno
, "Failed to bind socket");
766 closesocket(slisten
);
770 if (listen(slisten
, 1) != 0) {
771 error_setg_errno(errp
, errno
, "Failed to listen on socket");
772 closesocket(slisten
);
778 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
785 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
786 error_setg(errp
, "error parsing address '%s'", str
);
789 if (str
[n
] != '\0') {
790 error_setg(errp
, "trailing characters in address '%s'", str
);
794 addr
->cid
= g_strdup(cid
);
795 addr
->port
= g_strdup(port
);
799 static void vsock_unsupported(Error
**errp
)
801 error_setg(errp
, "socket family AF_VSOCK unsupported");
804 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
806 vsock_unsupported(errp
);
810 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
813 vsock_unsupported(errp
);
817 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
820 vsock_unsupported(errp
);
823 #endif /* CONFIG_AF_VSOCK */
827 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
830 struct sockaddr_un un
;
832 char *pathbuf
= NULL
;
836 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
838 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
842 if (saddr
->path
&& saddr
->path
[0]) {
845 const char *tmpdir
= getenv("TMPDIR");
846 tmpdir
= tmpdir
? tmpdir
: "/tmp";
847 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
850 pathlen
= strlen(path
);
851 if (pathlen
> sizeof(un
.sun_path
)) {
852 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
853 error_append_hint(errp
, "Path must be less than %zu bytes\n",
854 sizeof(un
.sun_path
));
858 if (pathbuf
!= NULL
) {
860 * This dummy fd usage silences the mktemp() unsecure warning.
861 * Using mkstemp() doesn't make things more secure here
862 * though. bind() complains about existing files, so we have
863 * to unlink first and thus re-open the race window. The
864 * worst case possible is bind() failing, i.e. a DoS attack.
866 fd
= mkstemp(pathbuf
);
868 error_setg_errno(errp
, errno
,
869 "Failed to make a temporary socket %s", pathbuf
);
875 if (unlink(path
) < 0 && errno
!= ENOENT
) {
876 error_setg_errno(errp
, errno
,
877 "Failed to unlink socket %s", path
);
881 memset(&un
, 0, sizeof(un
));
882 un
.sun_family
= AF_UNIX
;
883 memcpy(un
.sun_path
, path
, pathlen
);
885 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
886 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
889 if (listen(sock
, 1) < 0) {
890 error_setg_errno(errp
, errno
, "Failed to listen on socket");
903 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
905 struct sockaddr_un un
;
909 if (saddr
->path
== NULL
) {
910 error_setg(errp
, "unix connect: no path specified");
914 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
916 error_setg_errno(errp
, errno
, "Failed to create socket");
920 pathlen
= strlen(saddr
->path
);
921 if (pathlen
> sizeof(un
.sun_path
)) {
922 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
923 error_append_hint(errp
, "Path must be less than %zu bytes\n",
924 sizeof(un
.sun_path
));
928 memset(&un
, 0, sizeof(un
));
929 un
.sun_family
= AF_UNIX
;
930 memcpy(un
.sun_path
, saddr
->path
, pathlen
);
932 /* connect to peer */
935 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
938 } while (rc
== -EINTR
);
941 error_setg_errno(errp
, -rc
, "Failed to connect socket %s",
955 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
958 error_setg(errp
, "unix sockets are not available on windows");
963 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
965 error_setg(errp
, "unix sockets are not available on windows");
971 /* compatibility wrapper */
972 int unix_listen(const char *str
, Error
**errp
)
974 UnixSocketAddress
*saddr
;
977 saddr
= g_new0(UnixSocketAddress
, 1);
978 saddr
->path
= g_strdup(str
);
979 sock
= unix_listen_saddr(saddr
, errp
);
980 qapi_free_UnixSocketAddress(saddr
);
984 int unix_connect(const char *path
, Error
**errp
)
986 UnixSocketAddress
*saddr
;
989 saddr
= g_new0(UnixSocketAddress
, 1);
990 saddr
->path
= g_strdup(path
);
991 sock
= unix_connect_saddr(saddr
, errp
);
992 qapi_free_UnixSocketAddress(saddr
);
997 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1001 addr
= g_new0(SocketAddress
, 1);
1002 if (strstart(str
, "unix:", NULL
)) {
1003 if (str
[5] == '\0') {
1004 error_setg(errp
, "invalid Unix socket address");
1007 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1008 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1010 } else if (strstart(str
, "fd:", NULL
)) {
1011 if (str
[3] == '\0') {
1012 error_setg(errp
, "invalid file descriptor address");
1015 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1016 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1018 } else if (strstart(str
, "vsock:", NULL
)) {
1019 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1020 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1024 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1025 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1032 qapi_free_SocketAddress(addr
);
1036 static int socket_get_fd(const char *fdstr
, Error
**errp
)
1040 fd
= monitor_get_fd(cur_mon
, fdstr
, errp
);
1045 if (qemu_strtoi(fdstr
, NULL
, 10, &fd
) < 0) {
1046 error_setg_errno(errp
, errno
,
1047 "Unable to parse FD number %s",
1052 if (!fd_is_socket(fd
)) {
1053 error_setg(errp
, "File descriptor '%s' is not a socket", fdstr
);
1060 int socket_connect(SocketAddress
*addr
, Error
**errp
)
1064 switch (addr
->type
) {
1065 case SOCKET_ADDRESS_TYPE_INET
:
1066 fd
= inet_connect_saddr(&addr
->u
.inet
, errp
);
1069 case SOCKET_ADDRESS_TYPE_UNIX
:
1070 fd
= unix_connect_saddr(&addr
->u
.q_unix
, errp
);
1073 case SOCKET_ADDRESS_TYPE_FD
:
1074 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1077 case SOCKET_ADDRESS_TYPE_VSOCK
:
1078 fd
= vsock_connect_saddr(&addr
->u
.vsock
, errp
);
1087 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1091 switch (addr
->type
) {
1092 case SOCKET_ADDRESS_TYPE_INET
:
1093 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, errp
);
1096 case SOCKET_ADDRESS_TYPE_UNIX
:
1097 fd
= unix_listen_saddr(&addr
->u
.q_unix
, errp
);
1100 case SOCKET_ADDRESS_TYPE_FD
:
1101 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1104 case SOCKET_ADDRESS_TYPE_VSOCK
:
1105 fd
= vsock_listen_saddr(&addr
->u
.vsock
, errp
);
1114 void socket_listen_cleanup(int fd
, Error
**errp
)
1116 SocketAddress
*addr
;
1118 addr
= socket_local_address(fd
, errp
);
1123 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1124 && addr
->u
.q_unix
.path
) {
1125 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1126 error_setg_errno(errp
, errno
,
1127 "Failed to unlink socket %s",
1128 addr
->u
.q_unix
.path
);
1132 qapi_free_SocketAddress(addr
);
1135 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1140 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1141 * (although other address families can do SOCK_DGRAM, too)
1143 switch (remote
->type
) {
1144 case SOCKET_ADDRESS_TYPE_INET
:
1145 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1146 local
? &local
->u
.inet
: NULL
, errp
);
1150 error_setg(errp
, "socket type unsupported for datagram");
1157 static SocketAddress
*
1158 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1162 char host
[NI_MAXHOST
];
1163 char serv
[NI_MAXSERV
];
1164 SocketAddress
*addr
;
1165 InetSocketAddress
*inet
;
1168 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1171 NI_NUMERICHOST
| NI_NUMERICSERV
);
1173 error_setg(errp
, "Cannot format numeric socket address: %s",
1178 addr
= g_new0(SocketAddress
, 1);
1179 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1180 inet
= &addr
->u
.inet
;
1181 inet
->host
= g_strdup(host
);
1182 inet
->port
= g_strdup(serv
);
1183 if (sa
->ss_family
== AF_INET
) {
1184 inet
->has_ipv4
= inet
->ipv4
= true;
1186 inet
->has_ipv6
= inet
->ipv6
= true;
1194 static SocketAddress
*
1195 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1199 SocketAddress
*addr
;
1200 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1202 addr
= g_new0(SocketAddress
, 1);
1203 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1204 if (su
->sun_path
[0]) {
1205 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, sizeof(su
->sun_path
));
1212 #ifdef CONFIG_AF_VSOCK
1213 static SocketAddress
*
1214 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1218 SocketAddress
*addr
;
1219 VsockSocketAddress
*vaddr
;
1220 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1222 addr
= g_new0(SocketAddress
, 1);
1223 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1224 vaddr
= &addr
->u
.vsock
;
1225 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1226 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1230 #endif /* CONFIG_AF_VSOCK */
1233 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1237 switch (sa
->ss_family
) {
1240 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1244 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1247 #ifdef CONFIG_AF_VSOCK
1249 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1253 error_setg(errp
, "socket family %d unsupported",
1261 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1263 struct sockaddr_storage ss
;
1264 socklen_t sslen
= sizeof(ss
);
1266 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1267 error_setg_errno(errp
, errno
, "%s",
1268 "Unable to query local socket address");
1272 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1276 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1278 struct sockaddr_storage ss
;
1279 socklen_t sslen
= sizeof(ss
);
1281 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1282 error_setg_errno(errp
, errno
, "%s",
1283 "Unable to query remote socket address");
1287 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1291 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1293 SocketAddress
*addr
;
1299 addr
= g_new(SocketAddress
, 1);
1301 switch (addr_legacy
->type
) {
1302 case SOCKET_ADDRESS_LEGACY_KIND_INET
:
1303 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1304 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1305 addr_legacy
->u
.inet
.data
);
1307 case SOCKET_ADDRESS_LEGACY_KIND_UNIX
:
1308 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1309 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1310 addr_legacy
->u
.q_unix
.data
);
1312 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK
:
1313 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1314 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1315 addr_legacy
->u
.vsock
.data
);
1317 case SOCKET_ADDRESS_LEGACY_KIND_FD
:
1318 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1319 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);