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"
35 # define AI_ADDRCONFIG 0
39 # define AI_V4MAPPED 0
42 #ifndef AI_NUMERICSERV
43 # define AI_NUMERICSERV 0
47 static int inet_getport(struct addrinfo
*e
)
49 struct sockaddr_in
*i4
;
50 struct sockaddr_in6
*i6
;
52 switch (e
->ai_family
) {
54 i6
= (void*)e
->ai_addr
;
55 return ntohs(i6
->sin6_port
);
57 i4
= (void*)e
->ai_addr
;
58 return ntohs(i4
->sin_port
);
64 static void inet_setport(struct addrinfo
*e
, int port
)
66 struct sockaddr_in
*i4
;
67 struct sockaddr_in6
*i6
;
69 switch (e
->ai_family
) {
71 i6
= (void*)e
->ai_addr
;
72 i6
->sin6_port
= htons(port
);
75 i4
= (void*)e
->ai_addr
;
76 i4
->sin_port
= htons(port
);
81 NetworkAddressFamily
inet_netfamily(int family
)
84 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
85 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
86 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
87 #ifdef CONFIG_AF_VSOCK
88 case PF_VSOCK
: return NETWORK_ADDRESS_FAMILY_VSOCK
;
89 #endif /* CONFIG_AF_VSOCK */
91 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
94 bool fd_is_socket(int fd
)
97 socklen_t optlen
= sizeof(optval
);
98 return !qemu_getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &optval
, &optlen
);
103 * Matrix we're trying to apply
114 * t t PF_INET6/PF_UNSPEC
116 * NB, this matrix is only about getting the necessary results
117 * from getaddrinfo(). Some of the cases require further work
118 * after reading results from getaddrinfo in order to fully
119 * apply the logic the end user wants.
121 * In the first and last cases, we must set IPV6_V6ONLY=0
122 * when binding, to allow a single listener to potentially
123 * accept both IPv4+6 addresses.
125 int inet_ai_family_from_address(InetSocketAddress
*addr
,
128 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
129 !addr
->ipv6
&& !addr
->ipv4
) {
130 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
133 if ((addr
->has_ipv6
&& addr
->ipv6
) && (addr
->has_ipv4
&& addr
->ipv4
)) {
135 * Some backends can only do a single listener. In that case
136 * we want empty hostname to resolve to "::" and then use the
137 * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This
138 * doesn't work for addresses other than "", so they're just
139 * inevitably broken until multiple listeners can be used,
140 * and thus we honour getaddrinfo automatic protocol detection
141 * Once all backends do multi-listener, remove the PF_INET6
144 if (!addr
->host
|| g_str_equal(addr
->host
, "")) {
150 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
153 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
159 static int create_fast_reuse_socket(struct addrinfo
*e
)
161 int slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
165 socket_set_fast_reuse(slisten
);
169 static int try_bind(int socket
, InetSocketAddress
*saddr
, struct addrinfo
*e
)
172 return bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
175 * Deals with first & last cases in matrix in comment
176 * for inet_ai_family_from_address().
179 ((!saddr
->has_ipv4
&& !saddr
->has_ipv6
) ||
180 (saddr
->has_ipv4
&& saddr
->ipv4
&&
181 saddr
->has_ipv6
&& saddr
->ipv6
)) ? 0 : 1;
185 if (e
->ai_family
== PF_INET6
) {
186 qemu_setsockopt(socket
, IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
,
190 stat
= bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
195 /* If we got EADDRINUSE from an IPv6 bind & v6only is unset,
196 * it could be that the IPv4 port is already claimed, so retry
199 if (e
->ai_family
== PF_INET6
&& errno
== EADDRINUSE
&& !v6only
) {
207 static int inet_listen_saddr(InetSocketAddress
*saddr
,
211 struct addrinfo ai
,*res
,*e
;
213 char uaddr
[INET6_ADDRSTRLEN
+1];
215 int rc
, port_min
, port_max
, p
;
218 bool socket_created
= false;
221 memset(&ai
,0, sizeof(ai
));
222 ai
.ai_flags
= AI_PASSIVE
;
223 if (saddr
->has_numeric
&& saddr
->numeric
) {
224 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
226 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
227 ai
.ai_socktype
= SOCK_STREAM
;
230 error_propagate(errp
, err
);
234 if (saddr
->host
== NULL
) {
235 error_setg(errp
, "host not specified");
238 if (saddr
->port
!= NULL
) {
239 pstrcpy(port
, sizeof(port
), saddr
->port
);
246 unsigned long long baseport
;
247 if (strlen(port
) == 0) {
248 error_setg(errp
, "port not specified");
251 if (parse_uint_full(port
, &baseport
, 10) < 0) {
252 error_setg(errp
, "can't convert to a number: %s", port
);
255 if (baseport
> 65535 ||
256 baseport
+ port_offset
> 65535) {
257 error_setg(errp
, "port %s out of range", port
);
260 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
262 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
263 strlen(port
) ? port
: NULL
, &ai
, &res
);
265 error_setg(errp
, "address resolution failed for %s:%s: %s",
266 saddr
->host
, port
, gai_strerror(rc
));
270 /* create socket + bind/listen */
271 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
272 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
273 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
274 NI_NUMERICHOST
| NI_NUMERICSERV
);
276 port_min
= inet_getport(e
);
277 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
278 for (p
= port_min
; p
<= port_max
; p
++) {
281 slisten
= create_fast_reuse_socket(e
);
283 /* First time we expect we might fail to create the socket
284 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
285 * Later iterations should always succeed if first iteration
286 * worked though, so treat that as fatal.
291 error_setg_errno(errp
, errno
,
292 "Failed to recreate failed listening socket");
296 socket_created
= true;
298 rc
= try_bind(slisten
, saddr
, e
);
300 if (errno
!= EADDRINUSE
) {
301 error_setg_errno(errp
, errno
, "Failed to bind socket");
305 if (!listen(slisten
, 1)) {
308 if (errno
!= EADDRINUSE
) {
309 error_setg_errno(errp
, errno
, "Failed to listen on socket");
313 /* Someone else managed to bind to the same port and beat us
314 * to listen on it! Socket semantics does not allow us to
315 * recover from this situation, so we need to recreate the
316 * socket to allow bind attempts for subsequent ports:
318 closesocket(slisten
);
322 error_setg_errno(errp
, errno
,
324 "Failed to find an available port" :
325 "Failed to create a socket");
329 closesocket(slisten
);
341 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
342 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
344 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
345 ((rc) == -EINPROGRESS)
348 static int inet_connect_addr(struct addrinfo
*addr
, Error
**errp
);
350 static int inet_connect_addr(struct addrinfo
*addr
, Error
**errp
)
354 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
356 error_setg_errno(errp
, errno
, "Failed to create socket");
359 socket_set_fast_reuse(sock
);
361 /* connect to peer */
364 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
367 } while (rc
== -EINTR
);
370 error_setg_errno(errp
, errno
, "Failed to connect socket");
378 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
381 struct addrinfo ai
, *res
;
384 static int useV4Mapped
= 1;
386 memset(&ai
, 0, sizeof(ai
));
388 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
389 if (atomic_read(&useV4Mapped
)) {
390 ai
.ai_flags
|= AI_V4MAPPED
;
392 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
393 ai
.ai_socktype
= SOCK_STREAM
;
396 error_propagate(errp
, err
);
400 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
401 error_setg(errp
, "host and/or port not specified");
406 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
408 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
409 * then don't implement it in their getaddrinfo(). Detect
410 * this and retry without the flag since that's preferrable
413 if (rc
== EAI_BADFLAGS
&&
414 (ai
.ai_flags
& AI_V4MAPPED
)) {
415 atomic_set(&useV4Mapped
, 0);
416 ai
.ai_flags
&= ~AI_V4MAPPED
;
417 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
420 error_setg(errp
, "address resolution failed for %s:%s: %s",
421 saddr
->host
, saddr
->port
, gai_strerror(rc
));
428 * Create a socket and connect it to an address.
430 * @saddr: Inet socket address specification
431 * @errp: set on error
433 * Returns: -1 on error, file descriptor on success.
435 int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
)
437 Error
*local_err
= NULL
;
438 struct addrinfo
*res
, *e
;
441 res
= inet_parse_connect_saddr(saddr
, errp
);
446 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
447 error_free(local_err
);
449 sock
= inet_connect_addr(e
, &local_err
);
456 error_propagate(errp
, local_err
);
463 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
464 InetSocketAddress
*sladdr
,
467 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
473 /* lookup peer addr */
474 memset(&ai
,0, sizeof(ai
));
475 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
476 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
477 ai
.ai_socktype
= SOCK_DGRAM
;
480 error_propagate(errp
, err
);
486 if (addr
== NULL
|| strlen(addr
) == 0) {
489 if (port
== NULL
|| strlen(port
) == 0) {
490 error_setg(errp
, "remote port not specified");
494 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
495 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
500 /* lookup local addr */
501 memset(&ai
,0, sizeof(ai
));
502 ai
.ai_flags
= AI_PASSIVE
;
503 ai
.ai_family
= peer
->ai_family
;
504 ai
.ai_socktype
= SOCK_DGRAM
;
509 if (addr
== NULL
|| strlen(addr
) == 0) {
512 if (!port
|| strlen(port
) == 0) {
520 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
521 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
527 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
529 error_setg_errno(errp
, errno
, "Failed to create socket");
532 socket_set_fast_reuse(sock
);
535 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
536 error_setg_errno(errp
, errno
, "Failed to bind socket");
540 /* connect to peer */
541 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
542 error_setg_errno(errp
, errno
, "Failed to connect socket");
564 /* compatibility wrapper */
565 static int inet_parse_flag(const char *flagname
, const char *optstr
, bool *val
,
571 end
= strstr(optstr
, ",");
573 if (end
[1] == ',') { /* Reject 'ipv6=on,,foo' */
574 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
579 len
= strlen(optstr
);
581 if (len
== 0 || (len
== 3 && strncmp(optstr
, "=on", len
) == 0)) {
583 } else if (len
== 4 && strncmp(optstr
, "=off", len
) == 0) {
586 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
592 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
594 const char *optstr
, *h
;
601 memset(addr
, 0, sizeof(*addr
));
607 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
608 error_setg(errp
, "error parsing port in address '%s'", str
);
611 } else if (str
[0] == '[') {
613 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
614 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
618 /* hostname or IPv4 addr */
619 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
620 error_setg(errp
, "error parsing address '%s'", str
);
625 addr
->host
= g_strdup(host
);
626 addr
->port
= g_strdup(port
);
630 h
= strstr(optstr
, ",to=");
633 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
634 (h
[pos
] != '\0' && h
[pos
] != ',')) {
635 error_setg(errp
, "error parsing to= argument");
641 begin
= strstr(optstr
, ",ipv4");
643 if (inet_parse_flag("ipv4", begin
+ 5, &addr
->ipv4
, errp
) < 0) {
646 addr
->has_ipv4
= true;
648 begin
= strstr(optstr
, ",ipv6");
650 if (inet_parse_flag("ipv6", begin
+ 5, &addr
->ipv6
, errp
) < 0) {
653 addr
->has_ipv6
= true;
660 * Create a blocking socket and connect it to an address.
662 * @str: address string
663 * @errp: set in case of an error
665 * Returns -1 in case of error, file descriptor on success
667 int inet_connect(const char *str
, Error
**errp
)
670 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
672 if (!inet_parse(addr
, str
, errp
)) {
673 sock
= inet_connect_saddr(addr
, errp
);
675 qapi_free_InetSocketAddress(addr
);
679 #ifdef CONFIG_AF_VSOCK
680 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
681 struct sockaddr_vm
*svm
,
684 unsigned long long val
;
686 memset(svm
, 0, sizeof(*svm
));
687 svm
->svm_family
= AF_VSOCK
;
689 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
691 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
696 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
698 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
706 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, Error
**errp
)
710 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
712 error_setg_errno(errp
, errno
, "Failed to create socket");
716 /* connect to peer */
719 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
722 } while (rc
== -EINTR
);
725 error_setg_errno(errp
, errno
, "Failed to connect socket");
733 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
735 struct sockaddr_vm svm
;
738 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
742 sock
= vsock_connect_addr(&svm
, errp
);
747 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
750 struct sockaddr_vm svm
;
753 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
757 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
759 error_setg_errno(errp
, errno
, "Failed to create socket");
763 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
764 error_setg_errno(errp
, errno
, "Failed to bind socket");
765 closesocket(slisten
);
769 if (listen(slisten
, 1) != 0) {
770 error_setg_errno(errp
, errno
, "Failed to listen on socket");
771 closesocket(slisten
);
777 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
784 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
785 error_setg(errp
, "error parsing address '%s'", str
);
788 if (str
[n
] != '\0') {
789 error_setg(errp
, "trailing characters in address '%s'", str
);
793 addr
->cid
= g_strdup(cid
);
794 addr
->port
= g_strdup(port
);
798 static void vsock_unsupported(Error
**errp
)
800 error_setg(errp
, "socket family AF_VSOCK unsupported");
803 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
805 vsock_unsupported(errp
);
809 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
812 vsock_unsupported(errp
);
816 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
819 vsock_unsupported(errp
);
822 #endif /* CONFIG_AF_VSOCK */
826 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
829 struct sockaddr_un un
;
831 char *pathbuf
= NULL
;
834 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
836 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
840 if (saddr
->path
&& saddr
->path
[0]) {
843 const char *tmpdir
= getenv("TMPDIR");
844 tmpdir
= tmpdir
? tmpdir
: "/tmp";
845 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
848 if (strlen(path
) > sizeof(un
.sun_path
)) {
849 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
850 error_append_hint(errp
, "Path must be less than %zu bytes\n",
851 sizeof(un
.sun_path
));
855 if (pathbuf
!= NULL
) {
857 * This dummy fd usage silences the mktemp() unsecure warning.
858 * Using mkstemp() doesn't make things more secure here
859 * though. bind() complains about existing files, so we have
860 * to unlink first and thus re-open the race window. The
861 * worst case possible is bind() failing, i.e. a DoS attack.
863 fd
= mkstemp(pathbuf
);
865 error_setg_errno(errp
, errno
,
866 "Failed to make a temporary socket %s", pathbuf
);
872 if (unlink(path
) < 0 && errno
!= ENOENT
) {
873 error_setg_errno(errp
, errno
,
874 "Failed to unlink socket %s", path
);
878 memset(&un
, 0, sizeof(un
));
879 un
.sun_family
= AF_UNIX
;
880 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
));
882 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
883 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
886 if (listen(sock
, 1) < 0) {
887 error_setg_errno(errp
, errno
, "Failed to listen on socket");
900 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
902 struct sockaddr_un un
;
905 if (saddr
->path
== NULL
) {
906 error_setg(errp
, "unix connect: no path specified");
910 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
912 error_setg_errno(errp
, errno
, "Failed to create socket");
916 if (strlen(saddr
->path
) > sizeof(un
.sun_path
)) {
917 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
918 error_append_hint(errp
, "Path must be less than %zu bytes\n",
919 sizeof(un
.sun_path
));
923 memset(&un
, 0, sizeof(un
));
924 un
.sun_family
= AF_UNIX
;
925 strncpy(un
.sun_path
, saddr
->path
, sizeof(un
.sun_path
));
927 /* connect to peer */
930 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
933 } while (rc
== -EINTR
);
936 error_setg_errno(errp
, -rc
, "Failed to connect socket %s",
950 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
953 error_setg(errp
, "unix sockets are not available on windows");
958 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
960 error_setg(errp
, "unix sockets are not available on windows");
966 /* compatibility wrapper */
967 int unix_listen(const char *str
, Error
**errp
)
971 UnixSocketAddress
*saddr
;
973 saddr
= g_new0(UnixSocketAddress
, 1);
975 optstr
= strchr(str
, ',');
979 path
= g_malloc(len
+1);
980 snprintf(path
, len
+1, "%.*s", len
, str
);
984 saddr
->path
= g_strdup(str
);
987 sock
= unix_listen_saddr(saddr
, errp
);
989 qapi_free_UnixSocketAddress(saddr
);
993 int unix_connect(const char *path
, Error
**errp
)
995 UnixSocketAddress
*saddr
;
998 saddr
= g_new0(UnixSocketAddress
, 1);
999 saddr
->path
= g_strdup(path
);
1000 sock
= unix_connect_saddr(saddr
, errp
);
1001 qapi_free_UnixSocketAddress(saddr
);
1006 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1008 SocketAddress
*addr
;
1010 addr
= g_new0(SocketAddress
, 1);
1011 if (strstart(str
, "unix:", NULL
)) {
1012 if (str
[5] == '\0') {
1013 error_setg(errp
, "invalid Unix socket address");
1016 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1017 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1019 } else if (strstart(str
, "fd:", NULL
)) {
1020 if (str
[3] == '\0') {
1021 error_setg(errp
, "invalid file descriptor address");
1024 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1025 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1027 } else if (strstart(str
, "vsock:", NULL
)) {
1028 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1029 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1033 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1034 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1041 qapi_free_SocketAddress(addr
);
1045 static int socket_get_fd(const char *fdstr
, Error
**errp
)
1049 fd
= monitor_get_fd(cur_mon
, fdstr
, errp
);
1054 if (qemu_strtoi(fdstr
, NULL
, 10, &fd
) < 0) {
1055 error_setg_errno(errp
, errno
,
1056 "Unable to parse FD number %s",
1061 if (!fd_is_socket(fd
)) {
1062 error_setg(errp
, "File descriptor '%s' is not a socket", fdstr
);
1069 int socket_connect(SocketAddress
*addr
, Error
**errp
)
1073 switch (addr
->type
) {
1074 case SOCKET_ADDRESS_TYPE_INET
:
1075 fd
= inet_connect_saddr(&addr
->u
.inet
, errp
);
1078 case SOCKET_ADDRESS_TYPE_UNIX
:
1079 fd
= unix_connect_saddr(&addr
->u
.q_unix
, errp
);
1082 case SOCKET_ADDRESS_TYPE_FD
:
1083 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1086 case SOCKET_ADDRESS_TYPE_VSOCK
:
1087 fd
= vsock_connect_saddr(&addr
->u
.vsock
, errp
);
1096 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1100 switch (addr
->type
) {
1101 case SOCKET_ADDRESS_TYPE_INET
:
1102 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, errp
);
1105 case SOCKET_ADDRESS_TYPE_UNIX
:
1106 fd
= unix_listen_saddr(&addr
->u
.q_unix
, errp
);
1109 case SOCKET_ADDRESS_TYPE_FD
:
1110 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1113 case SOCKET_ADDRESS_TYPE_VSOCK
:
1114 fd
= vsock_listen_saddr(&addr
->u
.vsock
, errp
);
1123 void socket_listen_cleanup(int fd
, Error
**errp
)
1125 SocketAddress
*addr
;
1127 addr
= socket_local_address(fd
, errp
);
1132 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1133 && addr
->u
.q_unix
.path
) {
1134 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1135 error_setg_errno(errp
, errno
,
1136 "Failed to unlink socket %s",
1137 addr
->u
.q_unix
.path
);
1141 qapi_free_SocketAddress(addr
);
1144 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1149 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1150 * (although other address families can do SOCK_DGRAM, too)
1152 switch (remote
->type
) {
1153 case SOCKET_ADDRESS_TYPE_INET
:
1154 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1155 local
? &local
->u
.inet
: NULL
, errp
);
1159 error_setg(errp
, "socket type unsupported for datagram");
1166 static SocketAddress
*
1167 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1171 char host
[NI_MAXHOST
];
1172 char serv
[NI_MAXSERV
];
1173 SocketAddress
*addr
;
1174 InetSocketAddress
*inet
;
1177 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1180 NI_NUMERICHOST
| NI_NUMERICSERV
);
1182 error_setg(errp
, "Cannot format numeric socket address: %s",
1187 addr
= g_new0(SocketAddress
, 1);
1188 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1189 inet
= &addr
->u
.inet
;
1190 inet
->host
= g_strdup(host
);
1191 inet
->port
= g_strdup(serv
);
1192 if (sa
->ss_family
== AF_INET
) {
1193 inet
->has_ipv4
= inet
->ipv4
= true;
1195 inet
->has_ipv6
= inet
->ipv6
= true;
1203 static SocketAddress
*
1204 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1208 SocketAddress
*addr
;
1209 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1211 addr
= g_new0(SocketAddress
, 1);
1212 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1213 if (su
->sun_path
[0]) {
1214 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, sizeof(su
->sun_path
));
1221 #ifdef CONFIG_AF_VSOCK
1222 static SocketAddress
*
1223 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1227 SocketAddress
*addr
;
1228 VsockSocketAddress
*vaddr
;
1229 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1231 addr
= g_new0(SocketAddress
, 1);
1232 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1233 vaddr
= &addr
->u
.vsock
;
1234 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1235 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1239 #endif /* CONFIG_AF_VSOCK */
1242 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1246 switch (sa
->ss_family
) {
1249 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1253 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1256 #ifdef CONFIG_AF_VSOCK
1258 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1262 error_setg(errp
, "socket family %d unsupported",
1270 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1272 struct sockaddr_storage ss
;
1273 socklen_t sslen
= sizeof(ss
);
1275 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1276 error_setg_errno(errp
, errno
, "%s",
1277 "Unable to query local socket address");
1281 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1285 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1287 struct sockaddr_storage ss
;
1288 socklen_t sslen
= sizeof(ss
);
1290 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1291 error_setg_errno(errp
, errno
, "%s",
1292 "Unable to query remote socket address");
1296 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1300 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1302 SocketAddress
*addr
;
1308 addr
= g_new(SocketAddress
, 1);
1310 switch (addr_legacy
->type
) {
1311 case SOCKET_ADDRESS_LEGACY_KIND_INET
:
1312 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1313 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1314 addr_legacy
->u
.inet
.data
);
1316 case SOCKET_ADDRESS_LEGACY_KIND_UNIX
:
1317 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1318 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1319 addr_legacy
->u
.q_unix
.data
);
1321 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK
:
1322 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1323 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1324 addr_legacy
->u
.vsock
.data
);
1326 case SOCKET_ADDRESS_LEGACY_KIND_FD
:
1327 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1328 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);