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
,
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;
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_socktype
= SOCK_STREAM
;
235 ai
.ai_family
= inet_ai_family_from_address(saddr
, errp
);
240 if (saddr
->host
== NULL
) {
241 error_setg(errp
, "host not specified");
244 if (saddr
->port
!= NULL
) {
245 pstrcpy(port
, sizeof(port
), saddr
->port
);
252 unsigned long long baseport
;
253 if (strlen(port
) == 0) {
254 error_setg(errp
, "port not specified");
257 if (parse_uint_full(port
, &baseport
, 10) < 0) {
258 error_setg(errp
, "can't convert to a number: %s", port
);
261 if (baseport
> 65535 ||
262 baseport
+ port_offset
> 65535) {
263 error_setg(errp
, "port %s out of range", port
);
266 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
268 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
269 strlen(port
) ? port
: NULL
, &ai
, &res
);
271 error_setg(errp
, "address resolution failed for %s:%s: %s",
272 saddr
->host
, port
, gai_strerror(rc
));
276 /* create socket + bind/listen */
277 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
278 #ifdef HAVE_IPPROTO_MPTCP
279 if (saddr
->has_mptcp
&& saddr
->mptcp
) {
280 e
->ai_protocol
= IPPROTO_MPTCP
;
283 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
284 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
285 NI_NUMERICHOST
| NI_NUMERICSERV
);
287 port_min
= inet_getport(e
);
288 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
289 for (p
= port_min
; p
<= port_max
; p
++) {
292 slisten
= create_fast_reuse_socket(e
);
294 /* First time we expect we might fail to create the socket
295 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
296 * Later iterations should always succeed if first iteration
297 * worked though, so treat that as fatal.
302 error_setg_errno(errp
, errno
,
303 "Failed to recreate failed listening socket");
307 socket_created
= true;
309 rc
= try_bind(slisten
, saddr
, e
);
311 if (errno
!= EADDRINUSE
) {
312 error_setg_errno(errp
, errno
, "Failed to bind socket");
316 if (!listen(slisten
, num
)) {
319 if (errno
!= EADDRINUSE
) {
320 error_setg_errno(errp
, errno
, "Failed to listen on socket");
324 /* Someone else managed to bind to the same port and beat us
325 * to listen on it! Socket semantics does not allow us to
326 * recover from this situation, so we need to recreate the
327 * socket to allow bind attempts for subsequent ports:
333 error_setg_errno(errp
, errno
,
335 "Failed to find an available port" :
336 "Failed to create a socket");
352 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
353 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
355 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
356 ((rc) == -EINPROGRESS)
359 static int inet_connect_addr(const InetSocketAddress
*saddr
,
360 struct addrinfo
*addr
, Error
**errp
)
364 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
366 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
370 socket_set_fast_reuse(sock
);
372 /* connect to peer */
375 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
378 } while (rc
== -EINTR
);
381 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
382 saddr
->host
, saddr
->port
);
390 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
394 struct addrinfo ai
, *res
;
396 static int useV4Mapped
= 1;
398 memset(&ai
, 0, sizeof(ai
));
400 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
401 if (qatomic_read(&useV4Mapped
)) {
402 ai
.ai_flags
|= AI_V4MAPPED
;
404 ai
.ai_socktype
= SOCK_STREAM
;
405 ai
.ai_family
= inet_ai_family_from_address(saddr
, errp
);
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
);
460 #ifdef HAVE_IPPROTO_MPTCP
461 if (saddr
->has_mptcp
&& saddr
->mptcp
) {
462 e
->ai_protocol
= IPPROTO_MPTCP
;
466 sock
= inet_connect_addr(saddr
, e
, &local_err
);
475 error_propagate(errp
, local_err
);
479 if (saddr
->keep_alive
) {
481 int ret
= setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
485 error_setg_errno(errp
, errno
, "Unable to set KEEPALIVE");
494 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
495 InetSocketAddress
*sladdr
,
499 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
504 /* lookup peer addr */
505 memset(&ai
,0, sizeof(ai
));
506 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
507 ai
.ai_socktype
= SOCK_DGRAM
;
508 ai
.ai_family
= inet_ai_family_from_address(sraddr
, errp
);
515 if (addr
== NULL
|| strlen(addr
) == 0) {
518 if (port
== NULL
|| strlen(port
) == 0) {
519 error_setg(errp
, "remote port not specified");
523 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
524 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
529 /* lookup local addr */
530 memset(&ai
,0, sizeof(ai
));
531 ai
.ai_flags
= AI_PASSIVE
;
532 ai
.ai_family
= peer
->ai_family
;
533 ai
.ai_socktype
= SOCK_DGRAM
;
538 if (addr
== NULL
|| strlen(addr
) == 0) {
541 if (!port
|| strlen(port
) == 0) {
549 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
550 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
556 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
558 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
562 socket_set_fast_reuse(sock
);
565 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
566 error_setg_errno(errp
, errno
, "Failed to bind socket");
570 /* connect to peer */
571 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
572 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
595 /* compatibility wrapper */
596 static int inet_parse_flag(const char *flagname
, const char *optstr
, bool *val
,
602 end
= strstr(optstr
, ",");
604 if (end
[1] == ',') { /* Reject 'ipv6=on,,foo' */
605 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
610 len
= strlen(optstr
);
612 if (len
== 0 || (len
== 3 && strncmp(optstr
, "=on", len
) == 0)) {
614 } else if (len
== 4 && strncmp(optstr
, "=off", len
) == 0) {
617 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
623 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
625 const char *optstr
, *h
;
632 memset(addr
, 0, sizeof(*addr
));
638 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
639 error_setg(errp
, "error parsing port in address '%s'", str
);
642 } else if (str
[0] == '[') {
644 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
645 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
649 /* hostname or IPv4 addr */
650 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
651 error_setg(errp
, "error parsing address '%s'", str
);
656 addr
->host
= g_strdup(host
);
657 addr
->port
= g_strdup(port
);
661 h
= strstr(optstr
, ",to=");
664 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
665 (h
[pos
] != '\0' && h
[pos
] != ',')) {
666 error_setg(errp
, "error parsing to= argument");
672 begin
= strstr(optstr
, ",ipv4");
674 if (inet_parse_flag("ipv4", begin
+ 5, &addr
->ipv4
, errp
) < 0) {
677 addr
->has_ipv4
= true;
679 begin
= strstr(optstr
, ",ipv6");
681 if (inet_parse_flag("ipv6", begin
+ 5, &addr
->ipv6
, errp
) < 0) {
684 addr
->has_ipv6
= true;
686 begin
= strstr(optstr
, ",keep-alive");
688 if (inet_parse_flag("keep-alive", begin
+ strlen(",keep-alive"),
689 &addr
->keep_alive
, errp
) < 0)
693 addr
->has_keep_alive
= true;
695 #ifdef HAVE_IPPROTO_MPTCP
696 begin
= strstr(optstr
, ",mptcp");
698 if (inet_parse_flag("mptcp", begin
+ strlen(",mptcp"),
699 &addr
->mptcp
, errp
) < 0)
703 addr
->has_mptcp
= true;
711 * Create a blocking socket and connect it to an address.
713 * @str: address string
714 * @errp: set in case of an error
716 * Returns -1 in case of error, file descriptor on success
718 int inet_connect(const char *str
, Error
**errp
)
721 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
723 if (!inet_parse(addr
, str
, errp
)) {
724 sock
= inet_connect_saddr(addr
, errp
);
726 qapi_free_InetSocketAddress(addr
);
730 #ifdef CONFIG_AF_VSOCK
731 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
732 struct sockaddr_vm
*svm
,
735 unsigned long long val
;
737 memset(svm
, 0, sizeof(*svm
));
738 svm
->svm_family
= AF_VSOCK
;
740 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
742 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
747 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
749 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
757 static int vsock_connect_addr(const VsockSocketAddress
*vaddr
,
758 const struct sockaddr_vm
*svm
, Error
**errp
)
762 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
764 error_setg_errno(errp
, errno
, "Failed to create socket family %d",
769 /* connect to peer */
772 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
775 } while (rc
== -EINTR
);
778 error_setg_errno(errp
, errno
, "Failed to connect to '%s:%s'",
779 vaddr
->cid
, vaddr
->port
);
787 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
789 struct sockaddr_vm svm
;
791 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
795 return vsock_connect_addr(vaddr
, &svm
, errp
);
798 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
802 struct sockaddr_vm svm
;
805 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
809 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
811 error_setg_errno(errp
, errno
, "Failed to create socket");
815 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
816 error_setg_errno(errp
, errno
, "Failed to bind socket");
821 if (listen(slisten
, num
) != 0) {
822 error_setg_errno(errp
, errno
, "Failed to listen on socket");
829 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
836 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
837 error_setg(errp
, "error parsing address '%s'", str
);
840 if (str
[n
] != '\0') {
841 error_setg(errp
, "trailing characters in address '%s'", str
);
845 addr
->cid
= g_strdup(cid
);
846 addr
->port
= g_strdup(port
);
850 static void vsock_unsupported(Error
**errp
)
852 error_setg(errp
, "socket family AF_VSOCK unsupported");
855 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
857 vsock_unsupported(errp
);
861 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
865 vsock_unsupported(errp
);
869 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
872 vsock_unsupported(errp
);
875 #endif /* CONFIG_AF_VSOCK */
877 static bool saddr_is_abstract(UnixSocketAddress
*saddr
)
880 return saddr
->abstract
;
886 static bool saddr_is_tight(UnixSocketAddress
*saddr
)
889 return !saddr
->has_tight
|| saddr
->tight
;
895 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
899 bool abstract
= saddr_is_abstract(saddr
);
900 struct sockaddr_un un
;
902 char *pathbuf
= NULL
;
907 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
909 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
913 if (saddr
->path
[0] || abstract
) {
916 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX",
920 pathlen
= strlen(path
);
921 if (pathlen
> sizeof(un
.sun_path
) ||
922 (abstract
&& pathlen
> (sizeof(un
.sun_path
) - 1))) {
923 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
924 error_append_hint(errp
, "Path must be less than %zu bytes\n",
925 abstract
? sizeof(un
.sun_path
) - 1 :
926 sizeof(un
.sun_path
));
930 if (pathbuf
!= NULL
) {
932 * This dummy fd usage silences the mktemp() unsecure warning.
933 * Using mkstemp() doesn't make things more secure here
934 * though. bind() complains about existing files, so we have
935 * to unlink first and thus re-open the race window. The
936 * worst case possible is bind() failing, i.e. a DoS attack.
938 fd
= mkstemp(pathbuf
);
940 error_setg_errno(errp
, errno
,
941 "Failed to make a temporary socket %s", pathbuf
);
947 if (!abstract
&& unlink(path
) < 0 && errno
!= ENOENT
) {
948 error_setg_errno(errp
, errno
,
949 "Failed to unlink socket %s", path
);
953 memset(&un
, 0, sizeof(un
));
954 un
.sun_family
= AF_UNIX
;
955 addrlen
= sizeof(un
);
958 un
.sun_path
[0] = '\0';
959 memcpy(&un
.sun_path
[1], path
, pathlen
);
960 if (saddr_is_tight(saddr
)) {
961 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + pathlen
;
964 memcpy(un
.sun_path
, path
, pathlen
);
967 if (bind(sock
, (struct sockaddr
*) &un
, addrlen
) < 0) {
968 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
971 if (listen(sock
, num
) < 0) {
972 error_setg_errno(errp
, errno
, "Failed to listen on socket");
985 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
987 bool abstract
= saddr_is_abstract(saddr
);
988 struct sockaddr_un un
;
993 if (saddr
->path
== NULL
) {
994 error_setg(errp
, "unix connect: no path specified");
998 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
1000 error_setg_errno(errp
, errno
, "Failed to create socket");
1004 pathlen
= strlen(saddr
->path
);
1005 if (pathlen
> sizeof(un
.sun_path
) ||
1006 (abstract
&& pathlen
> (sizeof(un
.sun_path
) - 1))) {
1007 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
1008 error_append_hint(errp
, "Path must be less than %zu bytes\n",
1009 abstract
? sizeof(un
.sun_path
) - 1 :
1010 sizeof(un
.sun_path
));
1014 memset(&un
, 0, sizeof(un
));
1015 un
.sun_family
= AF_UNIX
;
1016 addrlen
= sizeof(un
);
1019 un
.sun_path
[0] = '\0';
1020 memcpy(&un
.sun_path
[1], saddr
->path
, pathlen
);
1021 if (saddr_is_tight(saddr
)) {
1022 addrlen
= offsetof(struct sockaddr_un
, sun_path
) + 1 + pathlen
;
1025 memcpy(un
.sun_path
, saddr
->path
, pathlen
);
1027 /* connect to peer */
1030 if (connect(sock
, (struct sockaddr
*) &un
, addrlen
) < 0) {
1033 } while (rc
== -EINTR
);
1036 error_setg_errno(errp
, -rc
, "Failed to connect to '%s'",
1048 /* compatibility wrapper */
1049 int unix_listen(const char *str
, Error
**errp
)
1051 UnixSocketAddress
*saddr
;
1054 saddr
= g_new0(UnixSocketAddress
, 1);
1055 saddr
->path
= g_strdup(str
);
1056 sock
= unix_listen_saddr(saddr
, 1, errp
);
1057 qapi_free_UnixSocketAddress(saddr
);
1061 int unix_connect(const char *path
, Error
**errp
)
1063 UnixSocketAddress
*saddr
;
1066 saddr
= g_new0(UnixSocketAddress
, 1);
1067 saddr
->path
= g_strdup(path
);
1068 sock
= unix_connect_saddr(saddr
, errp
);
1069 qapi_free_UnixSocketAddress(saddr
);
1073 char *socket_uri(SocketAddress
*addr
)
1075 switch (addr
->type
) {
1076 case SOCKET_ADDRESS_TYPE_INET
:
1077 return g_strdup_printf("tcp:%s:%s",
1080 case SOCKET_ADDRESS_TYPE_UNIX
:
1081 return g_strdup_printf("unix:%s",
1082 addr
->u
.q_unix
.path
);
1083 case SOCKET_ADDRESS_TYPE_FD
:
1084 return g_strdup_printf("fd:%s", addr
->u
.fd
.str
);
1085 case SOCKET_ADDRESS_TYPE_VSOCK
:
1086 return g_strdup_printf("vsock:%s:%s",
1088 addr
->u
.vsock
.port
);
1090 return g_strdup("unknown address type");
1094 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1096 SocketAddress
*addr
;
1098 addr
= g_new0(SocketAddress
, 1);
1099 if (strstart(str
, "unix:", NULL
)) {
1100 if (str
[5] == '\0') {
1101 error_setg(errp
, "invalid Unix socket address");
1104 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1105 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1107 } else if (strstart(str
, "fd:", NULL
)) {
1108 if (str
[3] == '\0') {
1109 error_setg(errp
, "invalid file descriptor address");
1112 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1113 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1115 } else if (strstart(str
, "vsock:", NULL
)) {
1116 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1117 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1120 } else if (strstart(str
, "tcp:", NULL
)) {
1121 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1122 if (inet_parse(&addr
->u
.inet
, str
+ strlen("tcp:"), errp
)) {
1126 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1127 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1134 qapi_free_SocketAddress(addr
);
1138 static int socket_get_fd(const char *fdstr
, Error
**errp
)
1140 Monitor
*cur_mon
= monitor_cur();
1143 fd
= monitor_get_fd(cur_mon
, fdstr
, errp
);
1148 if (qemu_strtoi(fdstr
, NULL
, 10, &fd
) < 0) {
1149 error_setg_errno(errp
, errno
,
1150 "Unable to parse FD number %s",
1155 if (!fd_is_socket(fd
)) {
1156 error_setg(errp
, "File descriptor '%s' is not a socket", fdstr
);
1163 int socket_address_parse_named_fd(SocketAddress
*addr
, Error
**errp
)
1167 if (addr
->type
!= SOCKET_ADDRESS_TYPE_FD
) {
1171 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1176 g_free(addr
->u
.fd
.str
);
1177 addr
->u
.fd
.str
= g_strdup_printf("%d", fd
);
1182 int socket_connect(SocketAddress
*addr
, Error
**errp
)
1186 switch (addr
->type
) {
1187 case SOCKET_ADDRESS_TYPE_INET
:
1188 fd
= inet_connect_saddr(&addr
->u
.inet
, errp
);
1191 case SOCKET_ADDRESS_TYPE_UNIX
:
1192 fd
= unix_connect_saddr(&addr
->u
.q_unix
, errp
);
1195 case SOCKET_ADDRESS_TYPE_FD
:
1196 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1199 case SOCKET_ADDRESS_TYPE_VSOCK
:
1200 fd
= vsock_connect_saddr(&addr
->u
.vsock
, errp
);
1209 int socket_listen(SocketAddress
*addr
, int num
, Error
**errp
)
1213 trace_socket_listen(num
);
1214 switch (addr
->type
) {
1215 case SOCKET_ADDRESS_TYPE_INET
:
1216 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, num
, errp
);
1219 case SOCKET_ADDRESS_TYPE_UNIX
:
1220 fd
= unix_listen_saddr(&addr
->u
.q_unix
, num
, errp
);
1223 case SOCKET_ADDRESS_TYPE_FD
:
1224 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1230 * If the socket is not yet in the listen state, then transition it to
1231 * the listen state now.
1233 * If it's already listening then this updates the backlog value as
1236 * If this socket cannot listen because it's already in another state
1237 * (e.g. unbound or connected) then we'll catch the error here.
1239 if (listen(fd
, num
) != 0) {
1240 error_setg_errno(errp
, errno
, "Failed to listen on fd socket");
1246 case SOCKET_ADDRESS_TYPE_VSOCK
:
1247 fd
= vsock_listen_saddr(&addr
->u
.vsock
, num
, errp
);
1256 void socket_listen_cleanup(int fd
, Error
**errp
)
1258 SocketAddress
*addr
;
1260 addr
= socket_local_address(fd
, errp
);
1265 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1266 && addr
->u
.q_unix
.path
) {
1267 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1268 error_setg_errno(errp
, errno
,
1269 "Failed to unlink socket %s",
1270 addr
->u
.q_unix
.path
);
1274 qapi_free_SocketAddress(addr
);
1277 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1282 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1283 * (although other address families can do SOCK_DGRAM, too)
1285 switch (remote
->type
) {
1286 case SOCKET_ADDRESS_TYPE_INET
:
1287 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1288 local
? &local
->u
.inet
: NULL
, errp
);
1292 error_setg(errp
, "socket type unsupported for datagram");
1299 static SocketAddress
*
1300 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1304 char host
[NI_MAXHOST
];
1305 char serv
[NI_MAXSERV
];
1306 SocketAddress
*addr
;
1307 InetSocketAddress
*inet
;
1310 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1313 NI_NUMERICHOST
| NI_NUMERICSERV
);
1315 error_setg(errp
, "Cannot format numeric socket address: %s",
1320 addr
= g_new0(SocketAddress
, 1);
1321 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1322 inet
= &addr
->u
.inet
;
1323 inet
->host
= g_strdup(host
);
1324 inet
->port
= g_strdup(serv
);
1325 if (sa
->ss_family
== AF_INET
) {
1326 inet
->has_ipv4
= inet
->ipv4
= true;
1328 inet
->has_ipv6
= inet
->ipv6
= true;
1335 static SocketAddress
*
1336 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1340 SocketAddress
*addr
;
1341 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1343 addr
= g_new0(SocketAddress
, 1);
1344 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1345 salen
-= offsetof(struct sockaddr_un
, sun_path
);
1347 if (salen
> 0 && !su
->sun_path
[0]) {
1348 /* Linux abstract socket */
1349 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
+ 1, salen
- 1);
1350 addr
->u
.q_unix
.has_abstract
= true;
1351 addr
->u
.q_unix
.abstract
= true;
1352 addr
->u
.q_unix
.has_tight
= true;
1353 addr
->u
.q_unix
.tight
= salen
< sizeof(su
->sun_path
);
1358 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, salen
);
1362 #ifdef CONFIG_AF_VSOCK
1363 static SocketAddress
*
1364 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1368 SocketAddress
*addr
;
1369 VsockSocketAddress
*vaddr
;
1370 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1372 addr
= g_new0(SocketAddress
, 1);
1373 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1374 vaddr
= &addr
->u
.vsock
;
1375 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1376 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1380 #endif /* CONFIG_AF_VSOCK */
1383 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1387 switch (sa
->ss_family
) {
1390 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1393 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1395 #ifdef CONFIG_AF_VSOCK
1397 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1401 error_setg(errp
, "socket family %d unsupported",
1409 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1411 struct sockaddr_storage ss
;
1412 socklen_t sslen
= sizeof(ss
);
1414 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1415 error_setg_errno(errp
, errno
, "%s",
1416 "Unable to query local socket address");
1420 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1424 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1426 struct sockaddr_storage ss
;
1427 socklen_t sslen
= sizeof(ss
);
1429 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1430 error_setg_errno(errp
, errno
, "%s",
1431 "Unable to query remote socket address");
1435 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1439 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1441 SocketAddress
*addr
;
1447 addr
= g_new(SocketAddress
, 1);
1449 switch (addr_legacy
->type
) {
1450 case SOCKET_ADDRESS_TYPE_INET
:
1451 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1452 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1453 addr_legacy
->u
.inet
.data
);
1455 case SOCKET_ADDRESS_TYPE_UNIX
:
1456 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1457 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1458 addr_legacy
->u
.q_unix
.data
);
1460 case SOCKET_ADDRESS_TYPE_VSOCK
:
1461 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1462 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1463 addr_legacy
->u
.vsock
.data
);
1465 case SOCKET_ADDRESS_TYPE_FD
:
1466 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1467 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);