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"
37 # define AI_ADDRCONFIG 0
41 # define AI_V4MAPPED 0
44 #ifndef AI_NUMERICSERV
45 # define AI_NUMERICSERV 0
49 static int inet_getport(struct addrinfo
*e
)
51 struct sockaddr_in
*i4
;
52 struct sockaddr_in6
*i6
;
54 switch (e
->ai_family
) {
56 i6
= (void*)e
->ai_addr
;
57 return ntohs(i6
->sin6_port
);
59 i4
= (void*)e
->ai_addr
;
60 return ntohs(i4
->sin_port
);
66 static void inet_setport(struct addrinfo
*e
, int port
)
68 struct sockaddr_in
*i4
;
69 struct sockaddr_in6
*i6
;
71 switch (e
->ai_family
) {
73 i6
= (void*)e
->ai_addr
;
74 i6
->sin6_port
= htons(port
);
77 i4
= (void*)e
->ai_addr
;
78 i4
->sin_port
= htons(port
);
83 NetworkAddressFamily
inet_netfamily(int family
)
86 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
87 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
88 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
89 #ifdef CONFIG_AF_VSOCK
90 case PF_VSOCK
: return NETWORK_ADDRESS_FAMILY_VSOCK
;
91 #endif /* CONFIG_AF_VSOCK */
93 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
96 bool fd_is_socket(int fd
)
99 socklen_t optlen
= sizeof(optval
);
100 return !qemu_getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &optval
, &optlen
);
105 * Matrix we're trying to apply
116 * t t PF_INET6/PF_UNSPEC
118 * NB, this matrix is only about getting the necessary results
119 * from getaddrinfo(). Some of the cases require further work
120 * after reading results from getaddrinfo in order to fully
121 * apply the logic the end user wants.
123 * In the first and last cases, we must set IPV6_V6ONLY=0
124 * when binding, to allow a single listener to potentially
125 * accept both IPv4+6 addresses.
127 int inet_ai_family_from_address(InetSocketAddress
*addr
,
130 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
131 !addr
->ipv6
&& !addr
->ipv4
) {
132 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
135 if ((addr
->has_ipv6
&& addr
->ipv6
) && (addr
->has_ipv4
&& addr
->ipv4
)) {
137 * Some backends can only do a single listener. In that case
138 * we want empty hostname to resolve to "::" and then use the
139 * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This
140 * doesn't work for addresses other than "", so they're just
141 * inevitably broken until multiple listeners can be used,
142 * and thus we honour getaddrinfo automatic protocol detection
143 * Once all backends do multi-listener, remove the PF_INET6
146 if (!addr
->host
|| g_str_equal(addr
->host
, "")) {
152 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
155 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
161 static int create_fast_reuse_socket(struct addrinfo
*e
)
163 int slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
167 socket_set_fast_reuse(slisten
);
171 static int try_bind(int socket
, InetSocketAddress
*saddr
, struct addrinfo
*e
)
174 return bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
177 * Deals with first & last cases in matrix in comment
178 * for inet_ai_family_from_address().
181 ((!saddr
->has_ipv4
&& !saddr
->has_ipv6
) ||
182 (saddr
->has_ipv4
&& saddr
->ipv4
&&
183 saddr
->has_ipv6
&& saddr
->ipv6
)) ? 0 : 1;
187 if (e
->ai_family
== PF_INET6
) {
188 qemu_setsockopt(socket
, IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
,
192 stat
= bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
197 /* If we got EADDRINUSE from an IPv6 bind & v6only is unset,
198 * it could be that the IPv4 port is already claimed, so retry
201 if (e
->ai_family
== PF_INET6
&& errno
== EADDRINUSE
&& !v6only
) {
209 static int inet_listen_saddr(InetSocketAddress
*saddr
,
214 struct addrinfo ai
,*res
,*e
;
216 char uaddr
[INET6_ADDRSTRLEN
+1];
218 int rc
, port_min
, port_max
, p
;
221 bool socket_created
= false;
224 if (saddr
->keep_alive
) {
225 error_setg(errp
, "keep-alive option is not supported for passive "
230 memset(&ai
,0, sizeof(ai
));
231 ai
.ai_flags
= AI_PASSIVE
;
232 if (saddr
->has_numeric
&& saddr
->numeric
) {
233 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
235 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
236 ai
.ai_socktype
= SOCK_STREAM
;
239 error_propagate(errp
, err
);
243 if (saddr
->host
== NULL
) {
244 error_setg(errp
, "host not specified");
247 if (saddr
->port
!= NULL
) {
248 pstrcpy(port
, sizeof(port
), saddr
->port
);
255 unsigned long long baseport
;
256 if (strlen(port
) == 0) {
257 error_setg(errp
, "port not specified");
260 if (parse_uint_full(port
, &baseport
, 10) < 0) {
261 error_setg(errp
, "can't convert to a number: %s", port
);
264 if (baseport
> 65535 ||
265 baseport
+ port_offset
> 65535) {
266 error_setg(errp
, "port %s out of range", port
);
269 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
271 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
272 strlen(port
) ? port
: NULL
, &ai
, &res
);
274 error_setg(errp
, "address resolution failed for %s:%s: %s",
275 saddr
->host
, port
, gai_strerror(rc
));
279 /* create socket + bind/listen */
280 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
281 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
282 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
283 NI_NUMERICHOST
| NI_NUMERICSERV
);
285 port_min
= inet_getport(e
);
286 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
287 for (p
= port_min
; p
<= port_max
; p
++) {
290 slisten
= create_fast_reuse_socket(e
);
292 /* First time we expect we might fail to create the socket
293 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
294 * Later iterations should always succeed if first iteration
295 * worked though, so treat that as fatal.
300 error_setg_errno(errp
, errno
,
301 "Failed to recreate failed listening socket");
305 socket_created
= true;
307 rc
= try_bind(slisten
, saddr
, e
);
309 if (errno
!= EADDRINUSE
) {
310 error_setg_errno(errp
, errno
, "Failed to bind socket");
314 if (!listen(slisten
, num
)) {
317 if (errno
!= EADDRINUSE
) {
318 error_setg_errno(errp
, errno
, "Failed to listen on socket");
322 /* Someone else managed to bind to the same port and beat us
323 * to listen on it! Socket semantics does not allow us to
324 * recover from this situation, so we need to recreate the
325 * socket to allow bind attempts for subsequent ports:
327 closesocket(slisten
);
331 error_setg_errno(errp
, errno
,
333 "Failed to find an available port" :
334 "Failed to create a socket");
338 closesocket(slisten
);
350 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
351 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
353 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
354 ((rc) == -EINPROGRESS)
357 static int inet_connect_addr(const InetSocketAddress
*saddr
,
358 struct addrinfo
*addr
, Error
**errp
)
362 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
364 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
368 socket_set_fast_reuse(sock
);
370 /* connect to peer */
373 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
376 } while (rc
== -EINTR
);
379 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
380 saddr
->host
, saddr
->port
);
388 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
391 struct addrinfo ai
, *res
;
394 static int useV4Mapped
= 1;
396 memset(&ai
, 0, sizeof(ai
));
398 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
399 if (qatomic_read(&useV4Mapped
)) {
400 ai
.ai_flags
|= AI_V4MAPPED
;
402 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
403 ai
.ai_socktype
= SOCK_STREAM
;
406 error_propagate(errp
, err
);
410 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
411 error_setg(errp
, "host and/or port not specified");
416 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
418 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
419 * then don't implement it in their getaddrinfo(). Detect
420 * this and retry without the flag since that's preferable
423 if (rc
== EAI_BADFLAGS
&&
424 (ai
.ai_flags
& AI_V4MAPPED
)) {
425 qatomic_set(&useV4Mapped
, 0);
426 ai
.ai_flags
&= ~AI_V4MAPPED
;
427 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
430 error_setg(errp
, "address resolution failed for %s:%s: %s",
431 saddr
->host
, saddr
->port
, gai_strerror(rc
));
438 * Create a socket and connect it to an address.
440 * @saddr: Inet socket address specification
441 * @errp: set on error
443 * Returns: -1 on error, file descriptor on success.
445 int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
)
447 Error
*local_err
= NULL
;
448 struct addrinfo
*res
, *e
;
451 res
= inet_parse_connect_saddr(saddr
, errp
);
456 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
457 error_free(local_err
);
459 sock
= inet_connect_addr(saddr
, e
, &local_err
);
468 error_propagate(errp
, local_err
);
472 if (saddr
->keep_alive
) {
474 int ret
= qemu_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
478 error_setg_errno(errp
, errno
, "Unable to set KEEPALIVE");
487 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
488 InetSocketAddress
*sladdr
,
491 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
497 /* lookup peer addr */
498 memset(&ai
,0, sizeof(ai
));
499 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
500 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
501 ai
.ai_socktype
= SOCK_DGRAM
;
504 error_propagate(errp
, err
);
510 if (addr
== NULL
|| strlen(addr
) == 0) {
513 if (port
== NULL
|| strlen(port
) == 0) {
514 error_setg(errp
, "remote port not specified");
518 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
519 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
524 /* lookup local addr */
525 memset(&ai
,0, sizeof(ai
));
526 ai
.ai_flags
= AI_PASSIVE
;
527 ai
.ai_family
= peer
->ai_family
;
528 ai
.ai_socktype
= SOCK_DGRAM
;
533 if (addr
== NULL
|| strlen(addr
) == 0) {
536 if (!port
|| strlen(port
) == 0) {
544 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
545 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
551 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
553 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
557 socket_set_fast_reuse(sock
);
560 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
561 error_setg_errno(errp
, errno
, "Failed to bind socket");
565 /* connect to peer */
566 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
567 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
590 /* compatibility wrapper */
591 static int inet_parse_flag(const char *flagname
, const char *optstr
, bool *val
,
597 end
= strstr(optstr
, ",");
599 if (end
[1] == ',') { /* Reject 'ipv6=on,,foo' */
600 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
605 len
= strlen(optstr
);
607 if (len
== 0 || (len
== 3 && strncmp(optstr
, "=on", len
) == 0)) {
609 } else if (len
== 4 && strncmp(optstr
, "=off", len
) == 0) {
612 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
618 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
620 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 begin
= strstr(optstr
, ",ipv4");
669 if (inet_parse_flag("ipv4", begin
+ 5, &addr
->ipv4
, errp
) < 0) {
672 addr
->has_ipv4
= true;
674 begin
= strstr(optstr
, ",ipv6");
676 if (inet_parse_flag("ipv6", begin
+ 5, &addr
->ipv6
, errp
) < 0) {
679 addr
->has_ipv6
= true;
681 begin
= strstr(optstr
, ",keep-alive");
683 if (inet_parse_flag("keep-alive", begin
+ strlen(",keep-alive"),
684 &addr
->keep_alive
, errp
) < 0)
688 addr
->has_keep_alive
= true;
695 * Create a blocking socket and connect it to an address.
697 * @str: address string
698 * @errp: set in case of an error
700 * Returns -1 in case of error, file descriptor on success
702 int inet_connect(const char *str
, Error
**errp
)
705 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
707 if (!inet_parse(addr
, str
, errp
)) {
708 sock
= inet_connect_saddr(addr
, errp
);
710 qapi_free_InetSocketAddress(addr
);
714 #ifdef CONFIG_AF_VSOCK
715 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
716 struct sockaddr_vm
*svm
,
719 unsigned long long val
;
721 memset(svm
, 0, sizeof(*svm
));
722 svm
->svm_family
= AF_VSOCK
;
724 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
726 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
731 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
733 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
741 static int vsock_connect_addr(const VsockSocketAddress
*vaddr
,
742 const struct sockaddr_vm
*svm
, Error
**errp
)
746 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
748 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
753 /* connect to peer */
756 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
759 } while (rc
== -EINTR
);
762 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
763 vaddr
->cid
, vaddr
->port
);
771 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
773 struct sockaddr_vm svm
;
775 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
779 return vsock_connect_addr(vaddr
, &svm
, errp
);
782 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
786 struct sockaddr_vm svm
;
789 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
793 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
795 error_setg_errno(errp
, errno
, "Failed to create socket");
799 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
800 error_setg_errno(errp
, errno
, "Failed to bind socket");
801 closesocket(slisten
);
805 if (listen(slisten
, num
) != 0) {
806 error_setg_errno(errp
, errno
, "Failed to listen on socket");
807 closesocket(slisten
);
813 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
820 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
821 error_setg(errp
, "error parsing address '%s'", str
);
824 if (str
[n
] != '\0') {
825 error_setg(errp
, "trailing characters in address '%s'", str
);
829 addr
->cid
= g_strdup(cid
);
830 addr
->port
= g_strdup(port
);
834 static void vsock_unsupported(Error
**errp
)
836 error_setg(errp
, "socket family AF_VSOCK unsupported");
839 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
841 vsock_unsupported(errp
);
845 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
849 vsock_unsupported(errp
);
853 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
856 vsock_unsupported(errp
);
859 #endif /* CONFIG_AF_VSOCK */
863 static bool saddr_is_abstract(UnixSocketAddress
*saddr
)
866 return saddr
->abstract
;
872 static bool saddr_is_tight(UnixSocketAddress
*saddr
)
875 return !saddr
->has_tight
|| saddr
->tight
;
881 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
885 bool abstract
= saddr_is_abstract(saddr
);
886 struct sockaddr_un un
;
888 char *pathbuf
= NULL
;
893 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
895 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
899 if (saddr
->path
[0] || abstract
) {
902 const char *tmpdir
= getenv("TMPDIR");
903 tmpdir
= tmpdir
? tmpdir
: "/tmp";
904 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
907 pathlen
= strlen(path
);
908 if (pathlen
> sizeof(un
.sun_path
) ||
909 (abstract
&& pathlen
> (sizeof(un
.sun_path
) - 1))) {
910 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
911 error_append_hint(errp
, "Path must be less than %zu bytes\n",
912 abstract
? sizeof(un
.sun_path
) - 1 :
913 sizeof(un
.sun_path
));
917 if (pathbuf
!= NULL
) {
919 * This dummy fd usage silences the mktemp() unsecure warning.
920 * Using mkstemp() doesn't make things more secure here
921 * though. bind() complains about existing files, so we have
922 * to unlink first and thus re-open the race window. The
923 * worst case possible is bind() failing, i.e. a DoS attack.
925 fd
= mkstemp(pathbuf
);
927 error_setg_errno(errp
, errno
,
928 "Failed to make a temporary socket %s", pathbuf
);
934 if (!abstract
&& unlink(path
) < 0 && errno
!= ENOENT
) {
935 error_setg_errno(errp
, errno
,
936 "Failed to unlink socket %s", path
);
940 memset(&un
, 0, sizeof(un
));
941 un
.sun_family
= AF_UNIX
;
942 addrlen
= sizeof(un
);
945 un
.sun_path
[0] = '\0';
946 memcpy(&un
.sun_path
[1], path
, pathlen
);
947 if (saddr_is_tight(saddr
)) {
948 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + pathlen
;
951 memcpy(un
.sun_path
, path
, pathlen
);
954 if (bind(sock
, (struct sockaddr
*) &un
, addrlen
) < 0) {
955 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
958 if (listen(sock
, num
) < 0) {
959 error_setg_errno(errp
, errno
, "Failed to listen on socket");
972 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
974 bool abstract
= saddr_is_abstract(saddr
);
975 struct sockaddr_un un
;
980 if (saddr
->path
== NULL
) {
981 error_setg(errp
, "unix connect: no path specified");
985 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
987 error_setg_errno(errp
, errno
, "Failed to create socket");
991 pathlen
= strlen(saddr
->path
);
992 if (pathlen
> sizeof(un
.sun_path
) ||
993 (abstract
&& pathlen
> (sizeof(un
.sun_path
) - 1))) {
994 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
995 error_append_hint(errp
, "Path must be less than %zu bytes\n",
996 abstract
? sizeof(un
.sun_path
) - 1 :
997 sizeof(un
.sun_path
));
1001 memset(&un
, 0, sizeof(un
));
1002 un
.sun_family
= AF_UNIX
;
1003 addrlen
= sizeof(un
);
1006 un
.sun_path
[0] = '\0';
1007 memcpy(&un
.sun_path
[1], saddr
->path
, pathlen
);
1008 if (saddr_is_tight(saddr
)) {
1009 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + pathlen
;
1012 memcpy(un
.sun_path
, saddr
->path
, pathlen
);
1014 /* connect to peer */
1017 if (connect(sock
, (struct sockaddr
*) &un
, addrlen
) < 0) {
1020 } while (rc
== -EINTR
);
1023 error_setg_errno(errp
, -rc
, "Failed to connect to '%s'",
1037 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
1041 error_setg(errp
, "unix sockets are not available on windows");
1046 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
1048 error_setg(errp
, "unix sockets are not available on windows");
1054 /* compatibility wrapper */
1055 int unix_listen(const char *str
, Error
**errp
)
1057 UnixSocketAddress
*saddr
;
1060 saddr
= g_new0(UnixSocketAddress
, 1);
1061 saddr
->path
= g_strdup(str
);
1062 sock
= unix_listen_saddr(saddr
, 1, errp
);
1063 qapi_free_UnixSocketAddress(saddr
);
1067 int unix_connect(const char *path
, Error
**errp
)
1069 UnixSocketAddress
*saddr
;
1072 saddr
= g_new0(UnixSocketAddress
, 1);
1073 saddr
->path
= g_strdup(path
);
1074 sock
= unix_connect_saddr(saddr
, errp
);
1075 qapi_free_UnixSocketAddress(saddr
);
1080 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1082 SocketAddress
*addr
;
1084 addr
= g_new0(SocketAddress
, 1);
1085 if (strstart(str
, "unix:", NULL
)) {
1086 if (str
[5] == '\0') {
1087 error_setg(errp
, "invalid Unix socket address");
1090 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1091 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1093 } else if (strstart(str
, "fd:", NULL
)) {
1094 if (str
[3] == '\0') {
1095 error_setg(errp
, "invalid file descriptor address");
1098 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1099 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1101 } else if (strstart(str
, "vsock:", NULL
)) {
1102 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1103 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1107 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1108 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1115 qapi_free_SocketAddress(addr
);
1119 static int socket_get_fd(const char *fdstr
, int num
, Error
**errp
)
1121 Monitor
*cur_mon
= monitor_cur();
1124 error_setg_errno(errp
, EINVAL
, "socket_get_fd: too many connections");
1128 fd
= monitor_get_fd(cur_mon
, fdstr
, errp
);
1133 if (qemu_strtoi(fdstr
, NULL
, 10, &fd
) < 0) {
1134 error_setg_errno(errp
, errno
,
1135 "Unable to parse FD number %s",
1140 if (!fd_is_socket(fd
)) {
1141 error_setg(errp
, "File descriptor '%s' is not a socket", fdstr
);
1148 int socket_connect(SocketAddress
*addr
, Error
**errp
)
1152 switch (addr
->type
) {
1153 case SOCKET_ADDRESS_TYPE_INET
:
1154 fd
= inet_connect_saddr(&addr
->u
.inet
, errp
);
1157 case SOCKET_ADDRESS_TYPE_UNIX
:
1158 fd
= unix_connect_saddr(&addr
->u
.q_unix
, errp
);
1161 case SOCKET_ADDRESS_TYPE_FD
:
1162 fd
= socket_get_fd(addr
->u
.fd
.str
, 1, errp
);
1165 case SOCKET_ADDRESS_TYPE_VSOCK
:
1166 fd
= vsock_connect_saddr(&addr
->u
.vsock
, errp
);
1175 int socket_listen(SocketAddress
*addr
, int num
, Error
**errp
)
1179 trace_socket_listen(num
);
1180 switch (addr
->type
) {
1181 case SOCKET_ADDRESS_TYPE_INET
:
1182 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, num
, errp
);
1185 case SOCKET_ADDRESS_TYPE_UNIX
:
1186 fd
= unix_listen_saddr(&addr
->u
.q_unix
, num
, errp
);
1189 case SOCKET_ADDRESS_TYPE_FD
:
1190 fd
= socket_get_fd(addr
->u
.fd
.str
, num
, errp
);
1193 case SOCKET_ADDRESS_TYPE_VSOCK
:
1194 fd
= vsock_listen_saddr(&addr
->u
.vsock
, num
, errp
);
1203 void socket_listen_cleanup(int fd
, Error
**errp
)
1205 SocketAddress
*addr
;
1207 addr
= socket_local_address(fd
, errp
);
1212 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1213 && addr
->u
.q_unix
.path
) {
1214 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1215 error_setg_errno(errp
, errno
,
1216 "Failed to unlink socket %s",
1217 addr
->u
.q_unix
.path
);
1221 qapi_free_SocketAddress(addr
);
1224 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1229 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1230 * (although other address families can do SOCK_DGRAM, too)
1232 switch (remote
->type
) {
1233 case SOCKET_ADDRESS_TYPE_INET
:
1234 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1235 local
? &local
->u
.inet
: NULL
, errp
);
1239 error_setg(errp
, "socket type unsupported for datagram");
1246 static SocketAddress
*
1247 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1251 char host
[NI_MAXHOST
];
1252 char serv
[NI_MAXSERV
];
1253 SocketAddress
*addr
;
1254 InetSocketAddress
*inet
;
1257 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1260 NI_NUMERICHOST
| NI_NUMERICSERV
);
1262 error_setg(errp
, "Cannot format numeric socket address: %s",
1267 addr
= g_new0(SocketAddress
, 1);
1268 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1269 inet
= &addr
->u
.inet
;
1270 inet
->host
= g_strdup(host
);
1271 inet
->port
= g_strdup(serv
);
1272 if (sa
->ss_family
== AF_INET
) {
1273 inet
->has_ipv4
= inet
->ipv4
= true;
1275 inet
->has_ipv6
= inet
->ipv6
= true;
1283 static SocketAddress
*
1284 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1288 SocketAddress
*addr
;
1289 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1291 addr
= g_new0(SocketAddress
, 1);
1292 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1294 if (!su
->sun_path
[0]) {
1295 /* Linux abstract socket */
1296 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
+ 1,
1297 sizeof(su
->sun_path
) - 1);
1298 addr
->u
.q_unix
.has_abstract
= true;
1299 addr
->u
.q_unix
.abstract
= true;
1300 addr
->u
.q_unix
.has_tight
= true;
1301 addr
->u
.q_unix
.tight
= salen
< sizeof(*su
);
1306 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, sizeof(su
->sun_path
));
1311 #ifdef CONFIG_AF_VSOCK
1312 static SocketAddress
*
1313 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1317 SocketAddress
*addr
;
1318 VsockSocketAddress
*vaddr
;
1319 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1321 addr
= g_new0(SocketAddress
, 1);
1322 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1323 vaddr
= &addr
->u
.vsock
;
1324 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1325 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1329 #endif /* CONFIG_AF_VSOCK */
1332 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1336 switch (sa
->ss_family
) {
1339 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1343 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1346 #ifdef CONFIG_AF_VSOCK
1348 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1352 error_setg(errp
, "socket family %d unsupported",
1360 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1362 struct sockaddr_storage ss
;
1363 socklen_t sslen
= sizeof(ss
);
1365 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1366 error_setg_errno(errp
, errno
, "%s",
1367 "Unable to query local socket address");
1371 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1375 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1377 struct sockaddr_storage ss
;
1378 socklen_t sslen
= sizeof(ss
);
1380 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1381 error_setg_errno(errp
, errno
, "%s",
1382 "Unable to query remote socket address");
1386 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1390 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1392 SocketAddress
*addr
;
1398 addr
= g_new(SocketAddress
, 1);
1400 switch (addr_legacy
->type
) {
1401 case SOCKET_ADDRESS_LEGACY_KIND_INET
:
1402 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1403 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1404 addr_legacy
->u
.inet
.data
);
1406 case SOCKET_ADDRESS_LEGACY_KIND_UNIX
:
1407 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1408 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1409 addr_legacy
->u
.q_unix
.data
);
1411 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK
:
1412 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1413 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1414 addr_legacy
->u
.vsock
.data
);
1416 case SOCKET_ADDRESS_LEGACY_KIND_FD
:
1417 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1418 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);