2 * inet and unix socket functions for qemu
4 * (c) 2008 Gerd Hoffmann <kraxel@redhat.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * Contributions after 2012-01-13 are licensed under the terms of the
16 * GNU GPL, version 2 or (at your option) any later version.
18 #include "qemu/osdep.h"
20 #ifdef CONFIG_AF_VSOCK
21 #include <linux/vm_sockets.h>
22 #endif /* CONFIG_AF_VSOCK */
24 #include "qemu-common.h"
25 #include "monitor/monitor.h"
26 #include "qapi/clone-visitor.h"
27 #include "qapi/error.h"
28 #include "qapi/qapi-visit-sockets.h"
29 #include "qemu/sockets.h"
30 #include "qemu/main-loop.h"
31 #include "qapi/qobject-input-visitor.h"
32 #include "qapi/qobject-output-visitor.h"
33 #include "qemu/cutils.h"
36 # define AI_ADDRCONFIG 0
40 # define AI_V4MAPPED 0
43 #ifndef AI_NUMERICSERV
44 # define AI_NUMERICSERV 0
48 static int inet_getport(struct addrinfo
*e
)
50 struct sockaddr_in
*i4
;
51 struct sockaddr_in6
*i6
;
53 switch (e
->ai_family
) {
55 i6
= (void*)e
->ai_addr
;
56 return ntohs(i6
->sin6_port
);
58 i4
= (void*)e
->ai_addr
;
59 return ntohs(i4
->sin_port
);
65 static void inet_setport(struct addrinfo
*e
, int port
)
67 struct sockaddr_in
*i4
;
68 struct sockaddr_in6
*i6
;
70 switch (e
->ai_family
) {
72 i6
= (void*)e
->ai_addr
;
73 i6
->sin6_port
= htons(port
);
76 i4
= (void*)e
->ai_addr
;
77 i4
->sin_port
= htons(port
);
82 NetworkAddressFamily
inet_netfamily(int family
)
85 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
86 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
87 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
88 #ifdef CONFIG_AF_VSOCK
89 case PF_VSOCK
: return NETWORK_ADDRESS_FAMILY_VSOCK
;
90 #endif /* CONFIG_AF_VSOCK */
92 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
95 bool fd_is_socket(int fd
)
98 socklen_t optlen
= sizeof(optval
);
99 return !qemu_getsockopt(fd
, SOL_SOCKET
, SO_TYPE
, &optval
, &optlen
);
104 * Matrix we're trying to apply
115 * t t PF_INET6/PF_UNSPEC
117 * NB, this matrix is only about getting the necessary results
118 * from getaddrinfo(). Some of the cases require further work
119 * after reading results from getaddrinfo in order to fully
120 * apply the logic the end user wants.
122 * In the first and last cases, we must set IPV6_V6ONLY=0
123 * when binding, to allow a single listener to potentially
124 * accept both IPv4+6 addresses.
126 int inet_ai_family_from_address(InetSocketAddress
*addr
,
129 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
130 !addr
->ipv6
&& !addr
->ipv4
) {
131 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
134 if ((addr
->has_ipv6
&& addr
->ipv6
) && (addr
->has_ipv4
&& addr
->ipv4
)) {
136 * Some backends can only do a single listener. In that case
137 * we want empty hostname to resolve to "::" and then use the
138 * flag IPV6_V6ONLY==0 to get both protocols on 1 socket. This
139 * doesn't work for addresses other than "", so they're just
140 * inevitably broken until multiple listeners can be used,
141 * and thus we honour getaddrinfo automatic protocol detection
142 * Once all backends do multi-listener, remove the PF_INET6
145 if (!addr
->host
|| g_str_equal(addr
->host
, "")) {
151 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
154 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
160 static int create_fast_reuse_socket(struct addrinfo
*e
)
162 int slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
166 socket_set_fast_reuse(slisten
);
170 static int try_bind(int socket
, InetSocketAddress
*saddr
, struct addrinfo
*e
)
173 return bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
176 * Deals with first & last cases in matrix in comment
177 * for inet_ai_family_from_address().
180 ((!saddr
->has_ipv4
&& !saddr
->has_ipv6
) ||
181 (saddr
->has_ipv4
&& saddr
->ipv4
&&
182 saddr
->has_ipv6
&& saddr
->ipv6
)) ? 0 : 1;
186 if (e
->ai_family
== PF_INET6
) {
187 qemu_setsockopt(socket
, IPPROTO_IPV6
, IPV6_V6ONLY
, &v6only
,
191 stat
= bind(socket
, e
->ai_addr
, e
->ai_addrlen
);
196 /* If we got EADDRINUSE from an IPv6 bind & v6only is unset,
197 * it could be that the IPv4 port is already claimed, so retry
200 if (e
->ai_family
== PF_INET6
&& errno
== EADDRINUSE
&& !v6only
) {
208 static int inet_listen_saddr(InetSocketAddress
*saddr
,
212 struct addrinfo ai
,*res
,*e
;
214 char uaddr
[INET6_ADDRSTRLEN
+1];
216 int rc
, port_min
, port_max
, p
;
219 bool socket_created
= false;
222 if (saddr
->keep_alive
) {
223 error_setg(errp
, "keep-alive option is not supported for passive "
228 memset(&ai
,0, sizeof(ai
));
229 ai
.ai_flags
= AI_PASSIVE
;
230 if (saddr
->has_numeric
&& saddr
->numeric
) {
231 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
233 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
234 ai
.ai_socktype
= SOCK_STREAM
;
237 error_propagate(errp
, err
);
241 if (saddr
->host
== NULL
) {
242 error_setg(errp
, "host not specified");
245 if (saddr
->port
!= NULL
) {
246 pstrcpy(port
, sizeof(port
), saddr
->port
);
253 unsigned long long baseport
;
254 if (strlen(port
) == 0) {
255 error_setg(errp
, "port not specified");
258 if (parse_uint_full(port
, &baseport
, 10) < 0) {
259 error_setg(errp
, "can't convert to a number: %s", port
);
262 if (baseport
> 65535 ||
263 baseport
+ port_offset
> 65535) {
264 error_setg(errp
, "port %s out of range", port
);
267 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
269 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
270 strlen(port
) ? port
: NULL
, &ai
, &res
);
272 error_setg(errp
, "address resolution failed for %s:%s: %s",
273 saddr
->host
, port
, gai_strerror(rc
));
277 /* create socket + bind/listen */
278 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
279 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
280 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
281 NI_NUMERICHOST
| NI_NUMERICSERV
);
283 port_min
= inet_getport(e
);
284 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
285 for (p
= port_min
; p
<= port_max
; p
++) {
288 slisten
= create_fast_reuse_socket(e
);
290 /* First time we expect we might fail to create the socket
291 * eg if 'e' has AF_INET6 but ipv6 kmod is not loaded.
292 * Later iterations should always succeed if first iteration
293 * worked though, so treat that as fatal.
298 error_setg_errno(errp
, errno
,
299 "Failed to recreate failed listening socket");
303 socket_created
= true;
305 rc
= try_bind(slisten
, saddr
, e
);
307 if (errno
!= EADDRINUSE
) {
308 error_setg_errno(errp
, errno
, "Failed to bind socket");
312 if (!listen(slisten
, 1)) {
315 if (errno
!= EADDRINUSE
) {
316 error_setg_errno(errp
, errno
, "Failed to listen on socket");
320 /* Someone else managed to bind to the same port and beat us
321 * to listen on it! Socket semantics does not allow us to
322 * recover from this situation, so we need to recreate the
323 * socket to allow bind attempts for subsequent ports:
325 closesocket(slisten
);
329 error_setg_errno(errp
, errno
,
331 "Failed to find an available port" :
332 "Failed to create a socket");
336 closesocket(slisten
);
348 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
349 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
351 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
352 ((rc) == -EINPROGRESS)
355 static int inet_connect_addr(struct addrinfo
*addr
, Error
**errp
);
357 static int inet_connect_addr(struct addrinfo
*addr
, Error
**errp
)
361 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
363 error_setg_errno(errp
, errno
, "Failed to create socket");
366 socket_set_fast_reuse(sock
);
368 /* connect to peer */
371 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
374 } while (rc
== -EINTR
);
377 error_setg_errno(errp
, errno
, "Failed to connect socket");
385 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
388 struct addrinfo ai
, *res
;
391 static int useV4Mapped
= 1;
393 memset(&ai
, 0, sizeof(ai
));
395 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
396 if (atomic_read(&useV4Mapped
)) {
397 ai
.ai_flags
|= AI_V4MAPPED
;
399 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
400 ai
.ai_socktype
= SOCK_STREAM
;
403 error_propagate(errp
, err
);
407 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
408 error_setg(errp
, "host and/or port not specified");
413 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
415 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
416 * then don't implement it in their getaddrinfo(). Detect
417 * this and retry without the flag since that's preferrable
420 if (rc
== EAI_BADFLAGS
&&
421 (ai
.ai_flags
& AI_V4MAPPED
)) {
422 atomic_set(&useV4Mapped
, 0);
423 ai
.ai_flags
&= ~AI_V4MAPPED
;
424 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
427 error_setg(errp
, "address resolution failed for %s:%s: %s",
428 saddr
->host
, saddr
->port
, gai_strerror(rc
));
435 * Create a socket and connect it to an address.
437 * @saddr: Inet socket address specification
438 * @errp: set on error
440 * Returns: -1 on error, file descriptor on success.
442 int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
)
444 Error
*local_err
= NULL
;
445 struct addrinfo
*res
, *e
;
448 res
= inet_parse_connect_saddr(saddr
, errp
);
453 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
454 error_free(local_err
);
456 sock
= inet_connect_addr(e
, &local_err
);
463 error_propagate(errp
, local_err
);
468 if (saddr
->keep_alive
) {
470 int ret
= qemu_setsockopt(sock
, SOL_SOCKET
, SO_KEEPALIVE
,
474 error_setg_errno(errp
, errno
, "Unable to set KEEPALIVE");
483 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
484 InetSocketAddress
*sladdr
,
487 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
493 /* lookup peer addr */
494 memset(&ai
,0, sizeof(ai
));
495 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
496 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
497 ai
.ai_socktype
= SOCK_DGRAM
;
500 error_propagate(errp
, err
);
506 if (addr
== NULL
|| strlen(addr
) == 0) {
509 if (port
== NULL
|| strlen(port
) == 0) {
510 error_setg(errp
, "remote port not specified");
514 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
515 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
520 /* lookup local addr */
521 memset(&ai
,0, sizeof(ai
));
522 ai
.ai_flags
= AI_PASSIVE
;
523 ai
.ai_family
= peer
->ai_family
;
524 ai
.ai_socktype
= SOCK_DGRAM
;
529 if (addr
== NULL
|| strlen(addr
) == 0) {
532 if (!port
|| strlen(port
) == 0) {
540 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
541 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
547 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
549 error_setg_errno(errp
, errno
, "Failed to create socket");
552 socket_set_fast_reuse(sock
);
555 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
556 error_setg_errno(errp
, errno
, "Failed to bind socket");
560 /* connect to peer */
561 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
562 error_setg_errno(errp
, errno
, "Failed to connect socket");
584 /* compatibility wrapper */
585 static int inet_parse_flag(const char *flagname
, const char *optstr
, bool *val
,
591 end
= strstr(optstr
, ",");
593 if (end
[1] == ',') { /* Reject 'ipv6=on,,foo' */
594 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
599 len
= strlen(optstr
);
601 if (len
== 0 || (len
== 3 && strncmp(optstr
, "=on", len
) == 0)) {
603 } else if (len
== 4 && strncmp(optstr
, "=off", len
) == 0) {
606 error_setg(errp
, "error parsing '%s' flag '%s'", flagname
, optstr
);
612 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
614 const char *optstr
, *h
;
621 memset(addr
, 0, sizeof(*addr
));
627 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
628 error_setg(errp
, "error parsing port in address '%s'", str
);
631 } else if (str
[0] == '[') {
633 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
634 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
638 /* hostname or IPv4 addr */
639 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
640 error_setg(errp
, "error parsing address '%s'", str
);
645 addr
->host
= g_strdup(host
);
646 addr
->port
= g_strdup(port
);
650 h
= strstr(optstr
, ",to=");
653 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
654 (h
[pos
] != '\0' && h
[pos
] != ',')) {
655 error_setg(errp
, "error parsing to= argument");
661 begin
= strstr(optstr
, ",ipv4");
663 if (inet_parse_flag("ipv4", begin
+ 5, &addr
->ipv4
, errp
) < 0) {
666 addr
->has_ipv4
= true;
668 begin
= strstr(optstr
, ",ipv6");
670 if (inet_parse_flag("ipv6", begin
+ 5, &addr
->ipv6
, errp
) < 0) {
673 addr
->has_ipv6
= true;
675 begin
= strstr(optstr
, ",keep-alive");
677 if (inet_parse_flag("keep-alive", begin
+ strlen(",keep-alive"),
678 &addr
->keep_alive
, errp
) < 0)
682 addr
->has_keep_alive
= true;
689 * Create a blocking socket and connect it to an address.
691 * @str: address string
692 * @errp: set in case of an error
694 * Returns -1 in case of error, file descriptor on success
696 int inet_connect(const char *str
, Error
**errp
)
699 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
701 if (!inet_parse(addr
, str
, errp
)) {
702 sock
= inet_connect_saddr(addr
, errp
);
704 qapi_free_InetSocketAddress(addr
);
708 #ifdef CONFIG_AF_VSOCK
709 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
710 struct sockaddr_vm
*svm
,
713 unsigned long long val
;
715 memset(svm
, 0, sizeof(*svm
));
716 svm
->svm_family
= AF_VSOCK
;
718 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
720 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
725 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
727 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
735 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, Error
**errp
)
739 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
741 error_setg_errno(errp
, errno
, "Failed to create socket");
745 /* connect to peer */
748 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
751 } while (rc
== -EINTR
);
754 error_setg_errno(errp
, errno
, "Failed to connect socket");
762 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
764 struct sockaddr_vm svm
;
767 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
771 sock
= vsock_connect_addr(&svm
, errp
);
776 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
779 struct sockaddr_vm svm
;
782 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
786 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
788 error_setg_errno(errp
, errno
, "Failed to create socket");
792 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
793 error_setg_errno(errp
, errno
, "Failed to bind socket");
794 closesocket(slisten
);
798 if (listen(slisten
, 1) != 0) {
799 error_setg_errno(errp
, errno
, "Failed to listen on socket");
800 closesocket(slisten
);
806 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
813 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
814 error_setg(errp
, "error parsing address '%s'", str
);
817 if (str
[n
] != '\0') {
818 error_setg(errp
, "trailing characters in address '%s'", str
);
822 addr
->cid
= g_strdup(cid
);
823 addr
->port
= g_strdup(port
);
827 static void vsock_unsupported(Error
**errp
)
829 error_setg(errp
, "socket family AF_VSOCK unsupported");
832 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
)
834 vsock_unsupported(errp
);
838 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
841 vsock_unsupported(errp
);
845 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
848 vsock_unsupported(errp
);
851 #endif /* CONFIG_AF_VSOCK */
855 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
858 struct sockaddr_un un
;
860 char *pathbuf
= NULL
;
864 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
866 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
870 if (saddr
->path
&& saddr
->path
[0]) {
873 const char *tmpdir
= getenv("TMPDIR");
874 tmpdir
= tmpdir
? tmpdir
: "/tmp";
875 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
878 pathlen
= strlen(path
);
879 if (pathlen
> sizeof(un
.sun_path
)) {
880 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
881 error_append_hint(errp
, "Path must be less than %zu bytes\n",
882 sizeof(un
.sun_path
));
886 if (pathbuf
!= NULL
) {
888 * This dummy fd usage silences the mktemp() unsecure warning.
889 * Using mkstemp() doesn't make things more secure here
890 * though. bind() complains about existing files, so we have
891 * to unlink first and thus re-open the race window. The
892 * worst case possible is bind() failing, i.e. a DoS attack.
894 fd
= mkstemp(pathbuf
);
896 error_setg_errno(errp
, errno
,
897 "Failed to make a temporary socket %s", pathbuf
);
903 if (unlink(path
) < 0 && errno
!= ENOENT
) {
904 error_setg_errno(errp
, errno
,
905 "Failed to unlink socket %s", path
);
909 memset(&un
, 0, sizeof(un
));
910 un
.sun_family
= AF_UNIX
;
911 memcpy(un
.sun_path
, path
, pathlen
);
913 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
914 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
917 if (listen(sock
, 1) < 0) {
918 error_setg_errno(errp
, errno
, "Failed to listen on socket");
931 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
933 struct sockaddr_un un
;
937 if (saddr
->path
== NULL
) {
938 error_setg(errp
, "unix connect: no path specified");
942 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
944 error_setg_errno(errp
, errno
, "Failed to create socket");
948 pathlen
= strlen(saddr
->path
);
949 if (pathlen
> sizeof(un
.sun_path
)) {
950 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
951 error_append_hint(errp
, "Path must be less than %zu bytes\n",
952 sizeof(un
.sun_path
));
956 memset(&un
, 0, sizeof(un
));
957 un
.sun_family
= AF_UNIX
;
958 memcpy(un
.sun_path
, saddr
->path
, pathlen
);
960 /* connect to peer */
963 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
966 } while (rc
== -EINTR
);
969 error_setg_errno(errp
, -rc
, "Failed to connect socket %s",
983 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
986 error_setg(errp
, "unix sockets are not available on windows");
991 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
)
993 error_setg(errp
, "unix sockets are not available on windows");
999 /* compatibility wrapper */
1000 int unix_listen(const char *str
, Error
**errp
)
1002 UnixSocketAddress
*saddr
;
1005 saddr
= g_new0(UnixSocketAddress
, 1);
1006 saddr
->path
= g_strdup(str
);
1007 sock
= unix_listen_saddr(saddr
, errp
);
1008 qapi_free_UnixSocketAddress(saddr
);
1012 int unix_connect(const char *path
, Error
**errp
)
1014 UnixSocketAddress
*saddr
;
1017 saddr
= g_new0(UnixSocketAddress
, 1);
1018 saddr
->path
= g_strdup(path
);
1019 sock
= unix_connect_saddr(saddr
, errp
);
1020 qapi_free_UnixSocketAddress(saddr
);
1025 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1027 SocketAddress
*addr
;
1029 addr
= g_new0(SocketAddress
, 1);
1030 if (strstart(str
, "unix:", NULL
)) {
1031 if (str
[5] == '\0') {
1032 error_setg(errp
, "invalid Unix socket address");
1035 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1036 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1038 } else if (strstart(str
, "fd:", NULL
)) {
1039 if (str
[3] == '\0') {
1040 error_setg(errp
, "invalid file descriptor address");
1043 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1044 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1046 } else if (strstart(str
, "vsock:", NULL
)) {
1047 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1048 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1052 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1053 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1060 qapi_free_SocketAddress(addr
);
1064 static int socket_get_fd(const char *fdstr
, Error
**errp
)
1068 fd
= monitor_get_fd(cur_mon
, fdstr
, errp
);
1073 if (qemu_strtoi(fdstr
, NULL
, 10, &fd
) < 0) {
1074 error_setg_errno(errp
, errno
,
1075 "Unable to parse FD number %s",
1080 if (!fd_is_socket(fd
)) {
1081 error_setg(errp
, "File descriptor '%s' is not a socket", fdstr
);
1088 int socket_connect(SocketAddress
*addr
, Error
**errp
)
1092 switch (addr
->type
) {
1093 case SOCKET_ADDRESS_TYPE_INET
:
1094 fd
= inet_connect_saddr(&addr
->u
.inet
, errp
);
1097 case SOCKET_ADDRESS_TYPE_UNIX
:
1098 fd
= unix_connect_saddr(&addr
->u
.q_unix
, errp
);
1101 case SOCKET_ADDRESS_TYPE_FD
:
1102 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1105 case SOCKET_ADDRESS_TYPE_VSOCK
:
1106 fd
= vsock_connect_saddr(&addr
->u
.vsock
, errp
);
1115 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1119 switch (addr
->type
) {
1120 case SOCKET_ADDRESS_TYPE_INET
:
1121 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, errp
);
1124 case SOCKET_ADDRESS_TYPE_UNIX
:
1125 fd
= unix_listen_saddr(&addr
->u
.q_unix
, errp
);
1128 case SOCKET_ADDRESS_TYPE_FD
:
1129 fd
= socket_get_fd(addr
->u
.fd
.str
, errp
);
1132 case SOCKET_ADDRESS_TYPE_VSOCK
:
1133 fd
= vsock_listen_saddr(&addr
->u
.vsock
, errp
);
1142 void socket_listen_cleanup(int fd
, Error
**errp
)
1144 SocketAddress
*addr
;
1146 addr
= socket_local_address(fd
, errp
);
1151 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1152 && addr
->u
.q_unix
.path
) {
1153 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1154 error_setg_errno(errp
, errno
,
1155 "Failed to unlink socket %s",
1156 addr
->u
.q_unix
.path
);
1160 qapi_free_SocketAddress(addr
);
1163 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1168 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1169 * (although other address families can do SOCK_DGRAM, too)
1171 switch (remote
->type
) {
1172 case SOCKET_ADDRESS_TYPE_INET
:
1173 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1174 local
? &local
->u
.inet
: NULL
, errp
);
1178 error_setg(errp
, "socket type unsupported for datagram");
1185 static SocketAddress
*
1186 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1190 char host
[NI_MAXHOST
];
1191 char serv
[NI_MAXSERV
];
1192 SocketAddress
*addr
;
1193 InetSocketAddress
*inet
;
1196 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1199 NI_NUMERICHOST
| NI_NUMERICSERV
);
1201 error_setg(errp
, "Cannot format numeric socket address: %s",
1206 addr
= g_new0(SocketAddress
, 1);
1207 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1208 inet
= &addr
->u
.inet
;
1209 inet
->host
= g_strdup(host
);
1210 inet
->port
= g_strdup(serv
);
1211 if (sa
->ss_family
== AF_INET
) {
1212 inet
->has_ipv4
= inet
->ipv4
= true;
1214 inet
->has_ipv6
= inet
->ipv6
= true;
1222 static SocketAddress
*
1223 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1227 SocketAddress
*addr
;
1228 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1230 addr
= g_new0(SocketAddress
, 1);
1231 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1232 if (su
->sun_path
[0]) {
1233 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, sizeof(su
->sun_path
));
1240 #ifdef CONFIG_AF_VSOCK
1241 static SocketAddress
*
1242 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1246 SocketAddress
*addr
;
1247 VsockSocketAddress
*vaddr
;
1248 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1250 addr
= g_new0(SocketAddress
, 1);
1251 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1252 vaddr
= &addr
->u
.vsock
;
1253 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1254 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1258 #endif /* CONFIG_AF_VSOCK */
1261 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1265 switch (sa
->ss_family
) {
1268 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1272 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1275 #ifdef CONFIG_AF_VSOCK
1277 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1281 error_setg(errp
, "socket family %d unsupported",
1289 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1291 struct sockaddr_storage ss
;
1292 socklen_t sslen
= sizeof(ss
);
1294 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1295 error_setg_errno(errp
, errno
, "%s",
1296 "Unable to query local socket address");
1300 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1304 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1306 struct sockaddr_storage ss
;
1307 socklen_t sslen
= sizeof(ss
);
1309 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1310 error_setg_errno(errp
, errno
, "%s",
1311 "Unable to query remote socket address");
1315 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1319 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1321 SocketAddress
*addr
;
1327 addr
= g_new(SocketAddress
, 1);
1329 switch (addr_legacy
->type
) {
1330 case SOCKET_ADDRESS_LEGACY_KIND_INET
:
1331 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1332 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1333 addr_legacy
->u
.inet
.data
);
1335 case SOCKET_ADDRESS_LEGACY_KIND_UNIX
:
1336 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1337 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1338 addr_legacy
->u
.q_unix
.data
);
1340 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK
:
1341 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1342 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1343 addr_legacy
->u
.vsock
.data
);
1345 case SOCKET_ADDRESS_LEGACY_KIND_FD
:
1346 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1347 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);