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 "qemu/sockets.h"
28 #include "qemu/main-loop.h"
29 #include "qapi/clone-visitor.h"
30 #include "qapi/qobject-input-visitor.h"
31 #include "qapi/qobject-output-visitor.h"
32 #include "qapi-visit.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
;
96 * Matrix we're trying to apply
109 * NB, this matrix is only about getting the necessary results
110 * from getaddrinfo(). Some of the cases require further work
111 * after reading results from getaddrinfo in order to fully
112 * apply the logic the end user wants. eg with the last case
113 * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
114 * guarantee the ipv6=t part of the request - we need more
115 * checks to provide ipv4=t part of the guarantee. This is
116 * outside scope of this method and not currently handled by
119 int inet_ai_family_from_address(InetSocketAddress
*addr
,
122 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
123 !addr
->ipv6
&& !addr
->ipv4
) {
124 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
127 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
130 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
136 static int inet_listen_saddr(InetSocketAddress
*saddr
,
141 struct addrinfo ai
,*res
,*e
;
143 char uaddr
[INET6_ADDRSTRLEN
+1];
145 int slisten
, rc
, port_min
, port_max
, p
;
148 memset(&ai
,0, sizeof(ai
));
149 ai
.ai_flags
= AI_PASSIVE
;
150 if (saddr
->has_numeric
&& saddr
->numeric
) {
151 ai
.ai_flags
|= AI_NUMERICHOST
| AI_NUMERICSERV
;
153 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
154 ai
.ai_socktype
= SOCK_STREAM
;
157 error_propagate(errp
, err
);
161 if (saddr
->host
== NULL
) {
162 error_setg(errp
, "host not specified");
165 if (saddr
->port
!= NULL
) {
166 pstrcpy(port
, sizeof(port
), saddr
->port
);
173 unsigned long long baseport
;
174 if (strlen(port
) == 0) {
175 error_setg(errp
, "port not specified");
178 if (parse_uint_full(port
, &baseport
, 10) < 0) {
179 error_setg(errp
, "can't convert to a number: %s", port
);
182 if (baseport
> 65535 ||
183 baseport
+ port_offset
> 65535) {
184 error_setg(errp
, "port %s out of range", port
);
187 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
189 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
190 strlen(port
) ? port
: NULL
, &ai
, &res
);
192 error_setg(errp
, "address resolution failed for %s:%s: %s",
193 saddr
->host
, port
, gai_strerror(rc
));
197 /* create socket + bind */
198 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
199 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
200 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
201 NI_NUMERICHOST
| NI_NUMERICSERV
);
202 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
205 error_setg_errno(errp
, errno
, "Failed to create socket");
210 socket_set_fast_reuse(slisten
);
212 if (e
->ai_family
== PF_INET6
) {
213 /* listen on both ipv4 and ipv6 */
215 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
220 port_min
= inet_getport(e
);
221 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
222 for (p
= port_min
; p
<= port_max
; p
++) {
224 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
229 error_setg_errno(errp
, errno
, "Failed to bind socket");
233 closesocket(slisten
);
239 if (listen(slisten
,1) != 0) {
240 error_setg_errno(errp
, errno
, "Failed to listen on socket");
241 closesocket(slisten
);
247 saddr
->host
= g_strdup(uaddr
);
249 saddr
->port
= g_strdup_printf("%d",
250 inet_getport(e
) - port_offset
);
251 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
252 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
259 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
260 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
262 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
263 ((rc) == -EINPROGRESS)
266 /* Struct to store connect state for non blocking connect */
267 typedef struct ConnectState
{
269 struct addrinfo
*addr_list
;
270 struct addrinfo
*current_addr
;
271 NonBlockingConnectHandler
*callback
;
275 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
276 ConnectState
*connect_state
, Error
**errp
);
278 static void wait_for_connect(void *opaque
)
280 ConnectState
*s
= opaque
;
282 socklen_t valsize
= sizeof(val
);
286 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
289 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
290 } while (rc
== -1 && errno
== EINTR
);
292 /* update rc to contain error */
300 error_setg_errno(&err
, errno
, "Error connecting to socket");
305 /* try to connect to the next address on the list */
306 if (s
->current_addr
) {
307 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
308 s
->current_addr
= s
->current_addr
->ai_next
;
309 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
313 error_setg_errno(&err
, errno
, "Unable to start socket connect");
315 /* connect in progress */
321 freeaddrinfo(s
->addr_list
);
325 s
->callback(s
->fd
, err
, s
->opaque
);
332 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
333 ConnectState
*connect_state
, Error
**errp
)
337 *in_progress
= false;
339 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
341 error_setg_errno(errp
, errno
, "Failed to create socket");
344 socket_set_fast_reuse(sock
);
345 if (connect_state
!= NULL
) {
346 qemu_set_nonblock(sock
);
348 /* connect to peer */
351 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
354 } while (rc
== -EINTR
);
356 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
357 connect_state
->fd
= sock
;
358 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
361 error_setg_errno(errp
, errno
, "Failed to connect socket");
368 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
371 struct addrinfo ai
, *res
;
374 static int useV4Mapped
= 1;
376 memset(&ai
, 0, sizeof(ai
));
378 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
379 if (atomic_read(&useV4Mapped
)) {
380 ai
.ai_flags
|= AI_V4MAPPED
;
382 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
383 ai
.ai_socktype
= SOCK_STREAM
;
386 error_propagate(errp
, err
);
390 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
391 error_setg(errp
, "host and/or port not specified");
396 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
398 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
399 * then don't implement it in their getaddrinfo(). Detect
400 * this and retry without the flag since that's preferrable
403 if (rc
== EAI_BADFLAGS
&&
404 (ai
.ai_flags
& AI_V4MAPPED
)) {
405 atomic_set(&useV4Mapped
, 0);
406 ai
.ai_flags
&= ~AI_V4MAPPED
;
407 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
410 error_setg(errp
, "address resolution failed for %s:%s: %s",
411 saddr
->host
, saddr
->port
, gai_strerror(rc
));
418 * Create a socket and connect it to an address.
420 * @saddr: Inet socket address specification
421 * @errp: set on error
422 * @callback: callback function for non-blocking connect
423 * @opaque: opaque for callback function
425 * Returns: -1 on error, file descriptor on success.
427 * If @callback is non-null, the connect is non-blocking. If this
428 * function succeeds, callback will be called when the connection
429 * completes, with the file descriptor on success, or -1 on error.
431 int inet_connect_saddr(InetSocketAddress
*saddr
,
432 NonBlockingConnectHandler
*callback
, void *opaque
,
435 Error
*local_err
= NULL
;
436 struct addrinfo
*res
, *e
;
439 ConnectState
*connect_state
= NULL
;
441 res
= inet_parse_connect_saddr(saddr
, errp
);
446 if (callback
!= NULL
) {
447 connect_state
= g_malloc0(sizeof(*connect_state
));
448 connect_state
->addr_list
= res
;
449 connect_state
->callback
= callback
;
450 connect_state
->opaque
= opaque
;
453 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
454 error_free(local_err
);
456 if (connect_state
!= NULL
) {
457 connect_state
->current_addr
= e
;
459 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
466 error_propagate(errp
, local_err
);
467 } else if (in_progress
) {
468 /* wait_for_connect() will do the rest */
472 callback(sock
, NULL
, opaque
);
475 g_free(connect_state
);
480 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
481 InetSocketAddress
*sladdr
,
484 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
490 /* lookup peer addr */
491 memset(&ai
,0, sizeof(ai
));
492 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
493 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
494 ai
.ai_socktype
= SOCK_DGRAM
;
497 error_propagate(errp
, err
);
503 if (addr
== NULL
|| strlen(addr
) == 0) {
506 if (port
== NULL
|| strlen(port
) == 0) {
507 error_setg(errp
, "remote port not specified");
511 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
512 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
517 /* lookup local addr */
518 memset(&ai
,0, sizeof(ai
));
519 ai
.ai_flags
= AI_PASSIVE
;
520 ai
.ai_family
= peer
->ai_family
;
521 ai
.ai_socktype
= SOCK_DGRAM
;
526 if (addr
== NULL
|| strlen(addr
) == 0) {
529 if (!port
|| strlen(port
) == 0) {
537 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
538 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
544 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
546 error_setg_errno(errp
, errno
, "Failed to create socket");
549 socket_set_fast_reuse(sock
);
552 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
553 error_setg_errno(errp
, errno
, "Failed to bind socket");
557 /* connect to peer */
558 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
559 error_setg_errno(errp
, errno
, "Failed to connect socket");
581 /* compatibility wrapper */
582 int inet_parse(InetSocketAddress
*addr
, const char *str
, Error
**errp
)
584 const char *optstr
, *h
;
590 memset(addr
, 0, sizeof(*addr
));
596 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
597 error_setg(errp
, "error parsing port in address '%s'", str
);
600 } else if (str
[0] == '[') {
602 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
603 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
606 addr
->ipv6
= addr
->has_ipv6
= true;
608 /* hostname or IPv4 addr */
609 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
610 error_setg(errp
, "error parsing address '%s'", str
);
613 if (host
[strspn(host
, "0123456789.")] == '\0') {
614 addr
->ipv4
= addr
->has_ipv4
= true;
618 addr
->host
= g_strdup(host
);
619 addr
->port
= g_strdup(port
);
623 h
= strstr(optstr
, ",to=");
626 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
627 (h
[pos
] != '\0' && h
[pos
] != ',')) {
628 error_setg(errp
, "error parsing to= argument");
634 if (strstr(optstr
, ",ipv4")) {
635 addr
->ipv4
= addr
->has_ipv4
= true;
637 if (strstr(optstr
, ",ipv6")) {
638 addr
->ipv6
= addr
->has_ipv6
= true;
645 * Create a blocking socket and connect it to an address.
647 * @str: address string
648 * @errp: set in case of an error
650 * Returns -1 in case of error, file descriptor on success
652 int inet_connect(const char *str
, Error
**errp
)
655 InetSocketAddress
*addr
= g_new(InetSocketAddress
, 1);
657 if (!inet_parse(addr
, str
, errp
)) {
658 sock
= inet_connect_saddr(addr
, NULL
, NULL
, errp
);
660 qapi_free_InetSocketAddress(addr
);
664 #ifdef CONFIG_AF_VSOCK
665 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
666 struct sockaddr_vm
*svm
,
669 unsigned long long val
;
671 memset(svm
, 0, sizeof(*svm
));
672 svm
->svm_family
= AF_VSOCK
;
674 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
676 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
681 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
683 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
691 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, bool *in_progress
,
692 ConnectState
*connect_state
, Error
**errp
)
696 *in_progress
= false;
698 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
700 error_setg_errno(errp
, errno
, "Failed to create socket");
703 if (connect_state
!= NULL
) {
704 qemu_set_nonblock(sock
);
706 /* connect to peer */
709 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
712 } while (rc
== -EINTR
);
714 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
715 connect_state
->fd
= sock
;
716 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
719 error_setg_errno(errp
, errno
, "Failed to connect socket");
726 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
,
727 NonBlockingConnectHandler
*callback
,
731 struct sockaddr_vm svm
;
734 ConnectState
*connect_state
= NULL
;
736 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
740 if (callback
!= NULL
) {
741 connect_state
= g_malloc0(sizeof(*connect_state
));
742 connect_state
->callback
= callback
;
743 connect_state
->opaque
= opaque
;
746 sock
= vsock_connect_addr(&svm
, &in_progress
, connect_state
, errp
);
749 } else if (in_progress
) {
750 /* wait_for_connect() will do the rest */
754 callback(sock
, NULL
, opaque
);
757 g_free(connect_state
);
761 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
764 struct sockaddr_vm svm
;
767 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
771 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
773 error_setg_errno(errp
, errno
, "Failed to create socket");
777 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
778 error_setg_errno(errp
, errno
, "Failed to bind socket");
779 closesocket(slisten
);
783 if (listen(slisten
, 1) != 0) {
784 error_setg_errno(errp
, errno
, "Failed to listen on socket");
785 closesocket(slisten
);
791 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
798 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
799 error_setg(errp
, "error parsing address '%s'", str
);
802 if (str
[n
] != '\0') {
803 error_setg(errp
, "trailing characters in address '%s'", str
);
807 addr
->cid
= g_strdup(cid
);
808 addr
->port
= g_strdup(port
);
812 static void vsock_unsupported(Error
**errp
)
814 error_setg(errp
, "socket family AF_VSOCK unsupported");
817 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
,
818 NonBlockingConnectHandler
*callback
,
819 void *opaque
, Error
**errp
)
821 vsock_unsupported(errp
);
825 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
828 vsock_unsupported(errp
);
832 static int vsock_parse(VsockSocketAddress
*addr
, const char *str
,
835 vsock_unsupported(errp
);
838 #endif /* CONFIG_AF_VSOCK */
842 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
846 struct sockaddr_un un
;
848 char *pathbuf
= NULL
;
851 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
853 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
857 if (saddr
->path
&& saddr
->path
[0]) {
860 const char *tmpdir
= getenv("TMPDIR");
861 tmpdir
= tmpdir
? tmpdir
: "/tmp";
862 path
= pathbuf
= g_strdup_printf("%s/qemu-socket-XXXXXX", tmpdir
);
865 if (strlen(path
) > sizeof(un
.sun_path
)) {
866 error_setg(errp
, "UNIX socket path '%s' is too long", path
);
867 error_append_hint(errp
, "Path must be less than %zu bytes\n",
868 sizeof(un
.sun_path
));
872 if (pathbuf
!= NULL
) {
874 * This dummy fd usage silences the mktemp() unsecure warning.
875 * Using mkstemp() doesn't make things more secure here
876 * though. bind() complains about existing files, so we have
877 * to unlink first and thus re-open the race window. The
878 * worst case possible is bind() failing, i.e. a DoS attack.
880 fd
= mkstemp(pathbuf
);
882 error_setg_errno(errp
, errno
,
883 "Failed to make a temporary socket %s", pathbuf
);
889 if (unlink(path
) < 0 && errno
!= ENOENT
) {
890 error_setg_errno(errp
, errno
,
891 "Failed to unlink socket %s", path
);
895 memset(&un
, 0, sizeof(un
));
896 un
.sun_family
= AF_UNIX
;
897 strncpy(un
.sun_path
, path
, sizeof(un
.sun_path
));
899 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
900 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", path
);
903 if (listen(sock
, 1) < 0) {
904 error_setg_errno(errp
, errno
, "Failed to listen on socket");
908 if (update_addr
&& pathbuf
) {
910 saddr
->path
= pathbuf
;
922 static int unix_connect_saddr(UnixSocketAddress
*saddr
,
923 NonBlockingConnectHandler
*callback
, void *opaque
,
926 struct sockaddr_un un
;
927 ConnectState
*connect_state
= NULL
;
930 if (saddr
->path
== NULL
) {
931 error_setg(errp
, "unix connect: no path specified");
935 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
937 error_setg_errno(errp
, errno
, "Failed to create socket");
940 if (callback
!= NULL
) {
941 connect_state
= g_malloc0(sizeof(*connect_state
));
942 connect_state
->callback
= callback
;
943 connect_state
->opaque
= opaque
;
944 qemu_set_nonblock(sock
);
947 if (strlen(saddr
->path
) > sizeof(un
.sun_path
)) {
948 error_setg(errp
, "UNIX socket path '%s' is too long", saddr
->path
);
949 error_append_hint(errp
, "Path must be less than %zu bytes\n",
950 sizeof(un
.sun_path
));
954 memset(&un
, 0, sizeof(un
));
955 un
.sun_family
= AF_UNIX
;
956 strncpy(un
.sun_path
, saddr
->path
, sizeof(un
.sun_path
));
958 /* connect to peer */
961 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
964 } while (rc
== -EINTR
);
966 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
967 connect_state
->fd
= sock
;
968 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
970 } else if (rc
>= 0) {
971 /* non blocking socket immediate success, call callback */
972 if (callback
!= NULL
) {
973 callback(sock
, NULL
, opaque
);
978 error_setg_errno(errp
, -rc
, "Failed to connect socket %s",
983 g_free(connect_state
);
988 g_free(connect_state
);
994 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
998 error_setg(errp
, "unix sockets are not available on windows");
1003 static int unix_connect_saddr(UnixSocketAddress
*saddr
,
1004 NonBlockingConnectHandler
*callback
, void *opaque
,
1007 error_setg(errp
, "unix sockets are not available on windows");
1013 /* compatibility wrapper */
1014 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
1016 char *path
, *optstr
;
1018 UnixSocketAddress
*saddr
;
1020 saddr
= g_new0(UnixSocketAddress
, 1);
1022 optstr
= strchr(str
, ',');
1026 path
= g_malloc(len
+1);
1027 snprintf(path
, len
+1, "%.*s", len
, str
);
1031 saddr
->path
= g_strdup(str
);
1034 sock
= unix_listen_saddr(saddr
, true, errp
);
1036 if (sock
!= -1 && ostr
) {
1037 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
1040 qapi_free_UnixSocketAddress(saddr
);
1044 int unix_connect(const char *path
, Error
**errp
)
1046 UnixSocketAddress
*saddr
;
1049 saddr
= g_new0(UnixSocketAddress
, 1);
1050 saddr
->path
= g_strdup(path
);
1051 sock
= unix_connect_saddr(saddr
, NULL
, NULL
, errp
);
1052 qapi_free_UnixSocketAddress(saddr
);
1057 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1059 SocketAddress
*addr
;
1061 addr
= g_new0(SocketAddress
, 1);
1062 if (strstart(str
, "unix:", NULL
)) {
1063 if (str
[5] == '\0') {
1064 error_setg(errp
, "invalid Unix socket address");
1067 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1068 addr
->u
.q_unix
.path
= g_strdup(str
+ 5);
1070 } else if (strstart(str
, "fd:", NULL
)) {
1071 if (str
[3] == '\0') {
1072 error_setg(errp
, "invalid file descriptor address");
1075 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1076 addr
->u
.fd
.str
= g_strdup(str
+ 3);
1078 } else if (strstart(str
, "vsock:", NULL
)) {
1079 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1080 if (vsock_parse(&addr
->u
.vsock
, str
+ strlen("vsock:"), errp
)) {
1084 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1085 if (inet_parse(&addr
->u
.inet
, str
, errp
)) {
1092 qapi_free_SocketAddress(addr
);
1096 int socket_connect(SocketAddress
*addr
, NonBlockingConnectHandler
*callback
,
1097 void *opaque
, Error
**errp
)
1101 switch (addr
->type
) {
1102 case SOCKET_ADDRESS_TYPE_INET
:
1103 fd
= inet_connect_saddr(&addr
->u
.inet
, callback
, opaque
, errp
);
1106 case SOCKET_ADDRESS_TYPE_UNIX
:
1107 fd
= unix_connect_saddr(&addr
->u
.q_unix
, callback
, opaque
, errp
);
1110 case SOCKET_ADDRESS_TYPE_FD
:
1111 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.str
, errp
);
1112 if (fd
>= 0 && callback
) {
1113 qemu_set_nonblock(fd
);
1114 callback(fd
, NULL
, opaque
);
1118 case SOCKET_ADDRESS_TYPE_VSOCK
:
1119 fd
= vsock_connect_saddr(&addr
->u
.vsock
, callback
, opaque
, errp
);
1128 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1132 switch (addr
->type
) {
1133 case SOCKET_ADDRESS_TYPE_INET
:
1134 fd
= inet_listen_saddr(&addr
->u
.inet
, 0, false, errp
);
1137 case SOCKET_ADDRESS_TYPE_UNIX
:
1138 fd
= unix_listen_saddr(&addr
->u
.q_unix
, false, errp
);
1141 case SOCKET_ADDRESS_TYPE_FD
:
1142 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.str
, errp
);
1145 case SOCKET_ADDRESS_TYPE_VSOCK
:
1146 fd
= vsock_listen_saddr(&addr
->u
.vsock
, errp
);
1155 void socket_listen_cleanup(int fd
, Error
**errp
)
1157 SocketAddress
*addr
;
1159 addr
= socket_local_address(fd
, errp
);
1161 if (addr
->type
== SOCKET_ADDRESS_TYPE_UNIX
1162 && addr
->u
.q_unix
.path
) {
1163 if (unlink(addr
->u
.q_unix
.path
) < 0 && errno
!= ENOENT
) {
1164 error_setg_errno(errp
, errno
,
1165 "Failed to unlink socket %s",
1166 addr
->u
.q_unix
.path
);
1170 qapi_free_SocketAddress(addr
);
1173 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1178 * TODO SOCKET_ADDRESS_TYPE_FD when fd is AF_INET or AF_INET6
1179 * (although other address families can do SOCK_DGRAM, too)
1181 switch (remote
->type
) {
1182 case SOCKET_ADDRESS_TYPE_INET
:
1183 fd
= inet_dgram_saddr(&remote
->u
.inet
,
1184 local
? &local
->u
.inet
: NULL
, errp
);
1188 error_setg(errp
, "socket type unsupported for datagram");
1195 static SocketAddress
*
1196 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1200 char host
[NI_MAXHOST
];
1201 char serv
[NI_MAXSERV
];
1202 SocketAddress
*addr
;
1203 InetSocketAddress
*inet
;
1206 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1209 NI_NUMERICHOST
| NI_NUMERICSERV
);
1211 error_setg(errp
, "Cannot format numeric socket address: %s",
1216 addr
= g_new0(SocketAddress
, 1);
1217 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1218 inet
= &addr
->u
.inet
;
1219 inet
->host
= g_strdup(host
);
1220 inet
->port
= g_strdup(serv
);
1221 if (sa
->ss_family
== AF_INET
) {
1222 inet
->has_ipv4
= inet
->ipv4
= true;
1224 inet
->has_ipv6
= inet
->ipv6
= true;
1232 static SocketAddress
*
1233 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1237 SocketAddress
*addr
;
1238 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1240 addr
= g_new0(SocketAddress
, 1);
1241 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1242 if (su
->sun_path
[0]) {
1243 addr
->u
.q_unix
.path
= g_strndup(su
->sun_path
, sizeof(su
->sun_path
));
1250 #ifdef CONFIG_AF_VSOCK
1251 static SocketAddress
*
1252 socket_sockaddr_to_address_vsock(struct sockaddr_storage
*sa
,
1256 SocketAddress
*addr
;
1257 VsockSocketAddress
*vaddr
;
1258 struct sockaddr_vm
*svm
= (struct sockaddr_vm
*)sa
;
1260 addr
= g_new0(SocketAddress
, 1);
1261 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1262 vaddr
= &addr
->u
.vsock
;
1263 vaddr
->cid
= g_strdup_printf("%u", svm
->svm_cid
);
1264 vaddr
->port
= g_strdup_printf("%u", svm
->svm_port
);
1268 #endif /* CONFIG_AF_VSOCK */
1271 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1275 switch (sa
->ss_family
) {
1278 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1282 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1285 #ifdef CONFIG_AF_VSOCK
1287 return socket_sockaddr_to_address_vsock(sa
, salen
, errp
);
1291 error_setg(errp
, "socket family %d unsupported",
1299 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1301 struct sockaddr_storage ss
;
1302 socklen_t sslen
= sizeof(ss
);
1304 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1305 error_setg_errno(errp
, errno
, "%s",
1306 "Unable to query local socket address");
1310 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1314 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1316 struct sockaddr_storage ss
;
1317 socklen_t sslen
= sizeof(ss
);
1319 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1320 error_setg_errno(errp
, errno
, "%s",
1321 "Unable to query remote socket address");
1325 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1329 SocketAddress
*socket_address_flatten(SocketAddressLegacy
*addr_legacy
)
1331 SocketAddress
*addr
;
1337 addr
= g_new(SocketAddress
, 1);
1339 switch (addr_legacy
->type
) {
1340 case SOCKET_ADDRESS_LEGACY_KIND_INET
:
1341 addr
->type
= SOCKET_ADDRESS_TYPE_INET
;
1342 QAPI_CLONE_MEMBERS(InetSocketAddress
, &addr
->u
.inet
,
1343 addr_legacy
->u
.inet
.data
);
1345 case SOCKET_ADDRESS_LEGACY_KIND_UNIX
:
1346 addr
->type
= SOCKET_ADDRESS_TYPE_UNIX
;
1347 QAPI_CLONE_MEMBERS(UnixSocketAddress
, &addr
->u
.q_unix
,
1348 addr_legacy
->u
.q_unix
.data
);
1350 case SOCKET_ADDRESS_LEGACY_KIND_VSOCK
:
1351 addr
->type
= SOCKET_ADDRESS_TYPE_VSOCK
;
1352 QAPI_CLONE_MEMBERS(VsockSocketAddress
, &addr
->u
.vsock
,
1353 addr_legacy
->u
.vsock
.data
);
1355 case SOCKET_ADDRESS_LEGACY_KIND_FD
:
1356 addr
->type
= SOCKET_ADDRESS_TYPE_FD
;
1357 QAPI_CLONE_MEMBERS(String
, &addr
->u
.fd
, addr_legacy
->u
.fd
.data
);