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 "qapi/qapi-visit-sockets.h"
28 #include "qemu/sockets.h"
29 #include "qemu/main-loop.h"
30 #include "qapi/qobject-input-visitor.h"
31 #include "qapi/qobject-output-visitor.h"
32 #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 !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 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
,
213 struct addrinfo ai
,*res
,*e
;
215 char uaddr
[INET6_ADDRSTRLEN
+1];
217 int rc
, port_min
, port_max
, p
;
220 bool socket_created
= false;
223 if (saddr
->keep_alive
) {
224 error_setg(errp
, "keep-alive option is not supported for passive "
229 memset(&ai
,0, sizeof(ai
));
230 ai
.ai_flags
= AI_PASSIVE
;
231 if (saddr
->has_numeric
&& saddr
->numeric
) {
232 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
234 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
235 ai
.ai_socktype
= SOCK_STREAM
;
238 error_propagate(errp
, err
);
242 if (saddr
->host
== NULL
) {
243 error_setg(errp
, "host not specified");
246 if (saddr
->port
!= NULL
) {
247 pstrcpy(port
, sizeof(port
), saddr
->port
);
254 unsigned long long baseport
;
255 if (strlen(port
) == 0) {
256 error_setg(errp
, "port not specified");
259 if (parse_uint_full(port
, &baseport
, 10) < 0) {
260 error_setg(errp
, "can't convert to a number: %s", port
);
263 if (baseport
> 65535 ||
264 baseport
+ port_offset
> 65535) {
265 error_setg(errp
, "port %s out of range", port
);
268 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
270 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
271 strlen(port
) ? port
: NULL
, &ai
, &res
);
273 error_setg(errp
, "address resolution failed for %s:%s: %s",
274 saddr
->host
, port
, gai_strerror(rc
));
278 /* create socket + bind/listen */
279 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
280 #ifdef HAVE_IPPROTO_MPTCP
281 if (saddr
->has_mptcp
&& saddr
->mptcp
) {
282 e
->ai_protocol
= IPPROTO_MPTCP
;
285 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
286 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
287 NI_NUMERICHOST
| NI_NUMERICSERV
);
289 port_min
= inet_getport(e
);
290 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
291 for (p
= port_min
; p
<= port_max
; p
++) {
294 slisten
= create_fast_reuse_socket(e
);
296 /* First time we expect we might fail to create the socket
297 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
298 * Later iterations should always succeed if first iteration
299 * worked though, so treat that as fatal.
304 error_setg_errno(errp
, errno
,
305 "Failed to recreate failed listening socket");
309 socket_created
= true;
311 rc
= try_bind(slisten
, saddr
, e
);
313 if (errno
!= EADDRINUSE
) {
314 error_setg_errno(errp
, errno
, "Failed to bind socket");
318 if (!listen(slisten
, num
)) {
321 if (errno
!= EADDRINUSE
) {
322 error_setg_errno(errp
, errno
, "Failed to listen on socket");
326 /* Someone else managed to bind to the same port and beat us
327 * to listen on it! Socket semantics does not allow us to
328 * recover from this situation, so we need to recreate the
329 * socket to allow bind attempts for subsequent ports:
331 closesocket(slisten
);
335 error_setg_errno(errp
, errno
,
337 "Failed to find an available port" :
338 "Failed to create a socket");
342 closesocket(slisten
);
354 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
355 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
357 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
358 ((rc) == -EINPROGRESS)
361 static int inet_connect_addr(const InetSocketAddress
*saddr
,
362 struct addrinfo
*addr
, Error
**errp
)
366 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
368 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
372 socket_set_fast_reuse(sock
);
374 /* connect to peer */
377 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
380 } while (rc
== -EINTR
);
383 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
384 saddr
->host
, saddr
->port
);
392 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
395 struct addrinfo ai
, *res
;
398 static int useV4Mapped
= 1;
400 memset(&ai
, 0, sizeof(ai
));
402 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
403 if (qatomic_read(&useV4Mapped
)) {
404 ai
.ai_flags
|= AI_V4MAPPED
;
406 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
407 ai
.ai_socktype
= SOCK_STREAM
;
410 error_propagate(errp
, err
);
414 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
415 error_setg(errp
, "host and/or port not specified");
420 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
422 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
423 * then don't implement it in their getaddrinfo(). Detect
424 * this and retry without the flag since that's preferable
427 if (rc
== EAI_BADFLAGS
&&
428 (ai
.ai_flags
& AI_V4MAPPED
)) {
429 qatomic_set(&useV4Mapped
, 0);
430 ai
.ai_flags
&= ~AI_V4MAPPED
;
431 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
434 error_setg(errp
, "address resolution failed for %s:%s: %s",
435 saddr
->host
, saddr
->port
, gai_strerror(rc
));
442 * Create a socket and connect it to an address.
444 * @saddr: Inet socket address specification
445 * @errp: set on error
447 * Returns: -1 on error, file descriptor on success.
449 int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
)
451 Error
*local_err
= NULL
;
452 struct addrinfo
*res
, *e
;
455 res
= inet_parse_connect_saddr(saddr
, errp
);
460 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
461 error_free(local_err
);
464 #ifdef HAVE_IPPROTO_MPTCP
465 if (saddr
->has_mptcp
&& saddr
->mptcp
) {
466 e
->ai_protocol
= IPPROTO_MPTCP
;
470 sock
= inet_connect_addr(saddr
, e
, &local_err
);
479 error_propagate(errp
, local_err
);
483 if (saddr
->keep_alive
) {
485 int ret
= setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
489 error_setg_errno(errp
, errno
, "Unable to set KEEPALIVE");
498 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
499 InetSocketAddress
*sladdr
,
502 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
508 /* lookup peer addr */
509 memset(&ai
,0, sizeof(ai
));
510 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
511 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
512 ai
.ai_socktype
= SOCK_DGRAM
;
515 error_propagate(errp
, err
);
521 if (addr
== NULL
|| strlen(addr
) == 0) {
524 if (port
== NULL
|| strlen(port
) == 0) {
525 error_setg(errp
, "remote port not specified");
529 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
530 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
535 /* lookup local addr */
536 memset(&ai
,0, sizeof(ai
));
537 ai
.ai_flags
= AI_PASSIVE
;
538 ai
.ai_family
= peer
->ai_family
;
539 ai
.ai_socktype
= SOCK_DGRAM
;
544 if (addr
== NULL
|| strlen(addr
) == 0) {
547 if (!port
|| strlen(port
) == 0) {
555 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
556 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
562 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
564 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
568 socket_set_fast_reuse(sock
);
571 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
572 error_setg_errno(errp
, errno
, "Failed to bind socket");
576 /* connect to peer */
577 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
578 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
601 /* compatibility wrapper */
602 static int inet_parse_flag(const char *flagname
, const char *optstr
, bool *val
,
608 end
= strstr(optstr
, ",");
610 if (end
[1] == ',') { /* Reject 'ipv6=on,,foo' */
611 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
616 len
= strlen(optstr
);
618 if (len
== 0 || (len
== 3 && strncmp(optstr
, "=on", len
) == 0)) {
620 } else if (len
== 4 && strncmp(optstr
, "=off", len
) == 0) {
623 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
629 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
631 const char *optstr
, *h
;
638 memset(addr
, 0, sizeof(*addr
));
644 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
645 error_setg(errp
, "error parsing port in address '%s'", str
);
648 } else if (str
[0] == '[') {
650 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
651 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
655 /* hostname or IPv4 addr */
656 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
657 error_setg(errp
, "error parsing address '%s'", str
);
662 addr
->host
= g_strdup(host
);
663 addr
->port
= g_strdup(port
);
667 h
= strstr(optstr
, ",to=");
670 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
671 (h
[pos
] != '\0' && h
[pos
] != ',')) {
672 error_setg(errp
, "error parsing to= argument");
678 begin
= strstr(optstr
, ",ipv4");
680 if (inet_parse_flag("ipv4", begin
+ 5, &addr
->ipv4
, errp
) < 0) {
683 addr
->has_ipv4
= true;
685 begin
= strstr(optstr
, ",ipv6");
687 if (inet_parse_flag("ipv6", begin
+ 5, &addr
->ipv6
, errp
) < 0) {
690 addr
->has_ipv6
= true;
692 begin
= strstr(optstr
, ",keep-alive");
694 if (inet_parse_flag("keep-alive", begin
+ strlen(",keep-alive"),
695 &addr
->keep_alive
, errp
) < 0)
699 addr
->has_keep_alive
= true;
701 #ifdef HAVE_IPPROTO_MPTCP
702 begin
= strstr(optstr
, ",mptcp");
704 if (inet_parse_flag("mptcp", begin
+ strlen(",mptcp"),
705 &addr
->mptcp
, errp
) < 0)
709 addr
->has_mptcp
= true;
717 * Create a blocking socket and connect it to an address.
719 * @str: address string
720 * @errp: set in case of an error
722 * Returns -1 in case of error, file descriptor on success
724 int inet_connect(const char *str
, Error
**errp
)
727 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
729 if (!inet_parse(addr
, str
, errp
)) {
730 sock
= inet_connect_saddr(addr
, errp
);
732 qapi_free_InetSocketAddress(addr
);
736 #ifdef CONFIG_AF_VSOCK
737 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
738 struct sockaddr_vm
*svm
,
741 unsigned long long val
;
743 memset(svm
, 0, sizeof(*svm
));
744 svm
->svm_family
= AF_VSOCK
;
746 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
748 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
753 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
755 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
763 static int vsock_connect_addr(const VsockSocketAddress
*vaddr
,
764 const struct sockaddr_vm
*svm
, Error
**errp
)
768 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
770 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
775 /* connect to peer */
778 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
781 } while (rc
== -EINTR
);
784 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
785 vaddr
->cid
, vaddr
->port
);
793 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
795 struct sockaddr_vm svm
;
797 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
801 return vsock_connect_addr(vaddr
, &svm
, errp
);
804 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
808 struct sockaddr_vm svm
;
811 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
815 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
817 error_setg_errno(errp
, errno
, "Failed to create socket");
821 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
822 error_setg_errno(errp
, errno
, "Failed to bind socket");
823 closesocket(slisten
);
827 if (listen(slisten
, num
) != 0) {
828 error_setg_errno(errp
, errno
, "Failed to listen on socket");
829 closesocket(slisten
);
835 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
842 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
843 error_setg(errp
, "error parsing address '%s'", str
);
846 if (str
[n
] != '\0') {
847 error_setg(errp
, "trailing characters in address '%s'", str
);
851 addr
->cid
= g_strdup(cid
);
852 addr
->port
= g_strdup(port
);
856 static void vsock_unsupported(Error
**errp
)
858 error_setg(errp
, "socket family AF_VSOCK unsupported");
861 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
863 vsock_unsupported(errp
);
867 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
871 vsock_unsupported(errp
);
875 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
878 vsock_unsupported(errp
);
881 #endif /* CONFIG_AF_VSOCK */
885 static bool saddr_is_abstract(UnixSocketAddress
*saddr
)
888 return saddr
->abstract
;
894 static bool saddr_is_tight(UnixSocketAddress
*saddr
)
897 return !saddr
->has_tight
|| saddr
->tight
;
903 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
907 bool abstract
= saddr_is_abstract(saddr
);
908 struct sockaddr_un un
;
910 char *pathbuf
= NULL
;
915 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
917 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
921 if (saddr
->path
[0] || abstract
) {
924 const char *tmpdir
= getenv("TMPDIR");
925 tmpdir
= tmpdir
? tmpdir
: "/tmp";
926 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
929 pathlen
= strlen(path
);
930 if (pathlen
> sizeof(un
.sun_path
) ||
931 (abstract
&& pathlen
> (sizeof(un
.sun_path
) - 1))) {
932 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
933 error_append_hint(errp
, "Path must be less than %zu bytes\n",
934 abstract
? sizeof(un
.sun_path
) - 1 :
935 sizeof(un
.sun_path
));
939 if (pathbuf
!= NULL
) {
941 * This dummy fd usage silences the mktemp() unsecure warning.
942 * Using mkstemp() doesn't make things more secure here
943 * though. bind() complains about existing files, so we have
944 * to unlink first and thus re-open the race window. The
945 * worst case possible is bind() failing, i.e. a DoS attack.
947 fd
= mkstemp(pathbuf
);
949 error_setg_errno(errp
, errno
,
950 "Failed to make a temporary socket %s", pathbuf
);
956 if (!abstract
&& unlink(path
) < 0 && errno
!= ENOENT
) {
957 error_setg_errno(errp
, errno
,
958 "Failed to unlink socket %s", path
);
962 memset(&un
, 0, sizeof(un
));
963 un
.sun_family
= AF_UNIX
;
964 addrlen
= sizeof(un
);
967 un
.sun_path
[0] = '\0';
968 memcpy(&un
.sun_path
[1], path
, pathlen
);
969 if (saddr_is_tight(saddr
)) {
970 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + pathlen
;
973 memcpy(un
.sun_path
, path
, pathlen
);
976 if (bind(sock
, (struct sockaddr
*) &un
, addrlen
) < 0) {
977 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
980 if (listen(sock
, num
) < 0) {
981 error_setg_errno(errp
, errno
, "Failed to listen on socket");
994 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
996 bool abstract
= saddr_is_abstract(saddr
);
997 struct sockaddr_un un
;
1002 if (saddr
->path
== NULL
) {
1003 error_setg(errp
, "unix connect: no path specified");
1007 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
1009 error_setg_errno(errp
, errno
, "Failed to create socket");
1013 pathlen
= strlen(saddr
->path
);
1014 if (pathlen
> sizeof(un
.sun_path
) ||
1015 (abstract
&& pathlen
> (sizeof(un
.sun_path
) - 1))) {
1016 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
1017 error_append_hint(errp
, "Path must be less than %zu bytes\n",
1018 abstract
? sizeof(un
.sun_path
) - 1 :
1019 sizeof(un
.sun_path
));
1023 memset(&un
, 0, sizeof(un
));
1024 un
.sun_family
= AF_UNIX
;
1025 addrlen
= sizeof(un
);
1028 un
.sun_path
[0] = '\0';
1029 memcpy(&un
.sun_path
[1], saddr
->path
, pathlen
);
1030 if (saddr_is_tight(saddr
)) {
1031 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + pathlen
;
1034 memcpy(un
.sun_path
, saddr
->path
, pathlen
);
1036 /* connect to peer */
1039 if (connect(sock
, (struct sockaddr
*) &un
, addrlen
) < 0) {
1042 } while (rc
== -EINTR
);
1045 error_setg_errno(errp
, -rc
, "Failed to connect to '%s'",
1059 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
1063 error_setg(errp
, "unix sockets are not available on windows");
1068 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
1070 error_setg(errp
, "unix sockets are not available on windows");
1076 /* compatibility wrapper */
1077 int unix_listen(const char *str
, Error
**errp
)
1079 UnixSocketAddress
*saddr
;
1082 saddr
= g_new0(UnixSocketAddress
, 1);
1083 saddr
->path
= g_strdup(str
);
1084 sock
= unix_listen_saddr(saddr
, 1, errp
);
1085 qapi_free_UnixSocketAddress(saddr
);
1089 int unix_connect(const char *path
, Error
**errp
)
1091 UnixSocketAddress
*saddr
;
1094 saddr
= g_new0(UnixSocketAddress
, 1);
1095 saddr
->path
= g_strdup(path
);
1096 sock
= unix_connect_saddr(saddr
, errp
);
1097 qapi_free_UnixSocketAddress(saddr
);
1102 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1104 SocketAddress
*addr
;
1106 addr
= g_new0(SocketAddress
, 1);
1107 if (strstart(str
, "unix:", NULL
)) {
1108 if (str
[5] == '\0') {
1109 error_setg(errp
, "invalid Unix socket address");
1112 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1113 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1115 } else if (strstart(str
, "fd:", NULL
)) {
1116 if (str
[3] == '\0') {
1117 error_setg(errp
, "invalid file descriptor address");
1120 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1121 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1123 } else if (strstart(str
, "vsock:", NULL
)) {
1124 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1125 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1129 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1130 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1137 qapi_free_SocketAddress(addr
);
1141 static int socket_get_fd(const char *fdstr
, Error
**errp
)
1143 Monitor
*cur_mon
= monitor_cur();
1146 fd
= monitor_get_fd(cur_mon
, fdstr
, errp
);
1151 if (qemu_strtoi(fdstr
, NULL
, 10, &fd
) < 0) {
1152 error_setg_errno(errp
, errno
,
1153 "Unable to parse FD number %s",
1158 if (!fd_is_socket(fd
)) {
1159 error_setg(errp
, "File descriptor '%s' is not a socket", fdstr
);
1166 int socket_address_parse_named_fd(SocketAddress
*addr
, Error
**errp
)
1170 if (addr
->type
!= SOCKET_ADDRESS_TYPE_FD
) {
1174 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1179 g_free(addr
->u
.fd
.str
);
1180 addr
->u
.fd
.str
= g_strdup_printf("%d", fd
);
1185 int socket_connect(SocketAddress
*addr
, Error
**errp
)
1189 switch (addr
->type
) {
1190 case SOCKET_ADDRESS_TYPE_INET
:
1191 fd
= inet_connect_saddr(&addr
->u
.inet
, errp
);
1194 case SOCKET_ADDRESS_TYPE_UNIX
:
1195 fd
= unix_connect_saddr(&addr
->u
.q_unix
, errp
);
1198 case SOCKET_ADDRESS_TYPE_FD
:
1199 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1202 case SOCKET_ADDRESS_TYPE_VSOCK
:
1203 fd
= vsock_connect_saddr(&addr
->u
.vsock
, errp
);
1212 int socket_listen(SocketAddress
*addr
, int num
, Error
**errp
)
1216 trace_socket_listen(num
);
1217 switch (addr
->type
) {
1218 case SOCKET_ADDRESS_TYPE_INET
:
1219 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, num
, errp
);
1222 case SOCKET_ADDRESS_TYPE_UNIX
:
1223 fd
= unix_listen_saddr(&addr
->u
.q_unix
, num
, errp
);
1226 case SOCKET_ADDRESS_TYPE_FD
:
1227 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1233 * If the socket is not yet in the listen state, then transition it to
1234 * the listen state now.
1236 * If it's already listening then this updates the backlog value as
1239 * If this socket cannot listen because it's already in another state
1240 * (e.g. unbound or connected) then we'll catch the error here.
1242 if (listen(fd
, num
) != 0) {
1243 error_setg_errno(errp
, errno
, "Failed to listen on fd socket");
1249 case SOCKET_ADDRESS_TYPE_VSOCK
:
1250 fd
= vsock_listen_saddr(&addr
->u
.vsock
, num
, errp
);
1259 void socket_listen_cleanup(int fd
, Error
**errp
)
1261 SocketAddress
*addr
;
1263 addr
= socket_local_address(fd
, errp
);
1268 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1269 && addr
->u
.q_unix
.path
) {
1270 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1271 error_setg_errno(errp
, errno
,
1272 "Failed to unlink socket %s",
1273 addr
->u
.q_unix
.path
);
1277 qapi_free_SocketAddress(addr
);
1280 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1285 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1286 * (although other address families can do SOCK_DGRAM, too)
1288 switch (remote
->type
) {
1289 case SOCKET_ADDRESS_TYPE_INET
:
1290 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1291 local
? &local
->u
.inet
: NULL
, errp
);
1295 error_setg(errp
, "socket type unsupported for datagram");
1302 static SocketAddress
*
1303 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1307 char host
[NI_MAXHOST
];
1308 char serv
[NI_MAXSERV
];
1309 SocketAddress
*addr
;
1310 InetSocketAddress
*inet
;
1313 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1316 NI_NUMERICHOST
| NI_NUMERICSERV
);
1318 error_setg(errp
, "Cannot format numeric socket address: %s",
1323 addr
= g_new0(SocketAddress
, 1);
1324 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1325 inet
= &addr
->u
.inet
;
1326 inet
->host
= g_strdup(host
);
1327 inet
->port
= g_strdup(serv
);
1328 if (sa
->ss_family
== AF_INET
) {
1329 inet
->has_ipv4
= inet
->ipv4
= true;
1331 inet
->has_ipv6
= inet
->ipv6
= true;
1339 static SocketAddress
*
1340 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1344 SocketAddress
*addr
;
1345 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1347 addr
= g_new0(SocketAddress
, 1);
1348 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1349 salen
-= offsetof(struct sockaddr_un
, sun_path
);
1351 if (salen
> 0 && !su
->sun_path
[0]) {
1352 /* Linux abstract socket */
1353 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
+ 1, salen
- 1);
1354 addr
->u
.q_unix
.has_abstract
= true;
1355 addr
->u
.q_unix
.abstract
= true;
1356 addr
->u
.q_unix
.has_tight
= true;
1357 addr
->u
.q_unix
.tight
= salen
< sizeof(su
->sun_path
);
1362 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, salen
);
1367 #ifdef CONFIG_AF_VSOCK
1368 static SocketAddress
*
1369 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1373 SocketAddress
*addr
;
1374 VsockSocketAddress
*vaddr
;
1375 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1377 addr
= g_new0(SocketAddress
, 1);
1378 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1379 vaddr
= &addr
->u
.vsock
;
1380 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1381 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1385 #endif /* CONFIG_AF_VSOCK */
1388 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1392 switch (sa
->ss_family
) {
1395 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1399 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1402 #ifdef CONFIG_AF_VSOCK
1404 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1408 error_setg(errp
, "socket family %d unsupported",
1416 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1418 struct sockaddr_storage ss
;
1419 socklen_t sslen
= sizeof(ss
);
1421 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1422 error_setg_errno(errp
, errno
, "%s",
1423 "Unable to query local socket address");
1427 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1431 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1433 struct sockaddr_storage ss
;
1434 socklen_t sslen
= sizeof(ss
);
1436 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1437 error_setg_errno(errp
, errno
, "%s",
1438 "Unable to query remote socket address");
1442 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1446 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1448 SocketAddress
*addr
;
1454 addr
= g_new(SocketAddress
, 1);
1456 switch (addr_legacy
->type
) {
1457 case SOCKET_ADDRESS_TYPE_INET
:
1458 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1459 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1460 addr_legacy
->u
.inet
.data
);
1462 case SOCKET_ADDRESS_TYPE_UNIX
:
1463 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1464 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1465 addr_legacy
->u
.q_unix
.data
);
1467 case SOCKET_ADDRESS_TYPE_VSOCK
:
1468 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1469 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1470 addr_legacy
->u
.vsock
.data
);
1472 case SOCKET_ADDRESS_TYPE_FD
:
1473 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1474 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);