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/error.h"
26 #include "qemu/sockets.h"
27 #include "qemu/main-loop.h"
28 #include "qapi/qobject-input-visitor.h"
29 #include "qapi/qobject-output-visitor.h"
30 #include "qapi-visit.h"
31 #include "qemu/cutils.h"
34 # define AI_ADDRCONFIG 0
38 # define AI_V4MAPPED 0
42 static int inet_getport(struct addrinfo
*e
)
44 struct sockaddr_in
*i4
;
45 struct sockaddr_in6
*i6
;
47 switch (e
->ai_family
) {
49 i6
= (void*)e
->ai_addr
;
50 return ntohs(i6
->sin6_port
);
52 i4
= (void*)e
->ai_addr
;
53 return ntohs(i4
->sin_port
);
59 static void inet_setport(struct addrinfo
*e
, int port
)
61 struct sockaddr_in
*i4
;
62 struct sockaddr_in6
*i6
;
64 switch (e
->ai_family
) {
66 i6
= (void*)e
->ai_addr
;
67 i6
->sin6_port
= htons(port
);
70 i4
= (void*)e
->ai_addr
;
71 i4
->sin_port
= htons(port
);
76 NetworkAddressFamily
inet_netfamily(int family
)
79 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
80 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
81 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
82 #ifdef CONFIG_AF_VSOCK
83 case PF_VSOCK
: return NETWORK_ADDRESS_FAMILY_VSOCK
;
84 #endif /* CONFIG_AF_VSOCK */
86 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
90 * Matrix we're trying to apply
103 * NB, this matrix is only about getting the necessary results
104 * from getaddrinfo(). Some of the cases require further work
105 * after reading results from getaddrinfo in order to fully
106 * apply the logic the end user wants. eg with the last case
107 * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
108 * guarantee the ipv6=t part of the request - we need more
109 * checks to provide ipv4=t part of the guarantee. This is
110 * outside scope of this method and not currently handled by
113 static int inet_ai_family_from_address(InetSocketAddress
*addr
,
116 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
117 !addr
->ipv6
&& !addr
->ipv4
) {
118 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
121 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
124 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
130 static int inet_listen_saddr(InetSocketAddress
*saddr
,
135 struct addrinfo ai
,*res
,*e
;
137 char uaddr
[INET6_ADDRSTRLEN
+1];
139 int slisten
, rc
, port_min
, port_max
, p
;
142 memset(&ai
,0, sizeof(ai
));
143 ai
.ai_flags
= AI_PASSIVE
;
144 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
145 ai
.ai_socktype
= SOCK_STREAM
;
148 error_propagate(errp
, err
);
152 if (saddr
->host
== NULL
) {
153 error_setg(errp
, "host not specified");
156 if (saddr
->port
!= NULL
) {
157 pstrcpy(port
, sizeof(port
), saddr
->port
);
164 unsigned long long baseport
;
165 if (strlen(port
) == 0) {
166 error_setg(errp
, "port not specified");
169 if (parse_uint_full(port
, &baseport
, 10) < 0) {
170 error_setg(errp
, "can't convert to a number: %s", port
);
173 if (baseport
> 65535 ||
174 baseport
+ port_offset
> 65535) {
175 error_setg(errp
, "port %s out of range", port
);
178 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
180 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
181 strlen(port
) ? port
: NULL
, &ai
, &res
);
183 error_setg(errp
, "address resolution failed for %s:%s: %s",
184 saddr
->host
, port
, gai_strerror(rc
));
188 /* create socket + bind */
189 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
190 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
191 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
192 NI_NUMERICHOST
| NI_NUMERICSERV
);
193 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
196 error_setg_errno(errp
, errno
, "Failed to create socket");
201 socket_set_fast_reuse(slisten
);
203 if (e
->ai_family
== PF_INET6
) {
204 /* listen on both ipv4 and ipv6 */
206 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
211 port_min
= inet_getport(e
);
212 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
213 for (p
= port_min
; p
<= port_max
; p
++) {
215 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
220 error_setg_errno(errp
, errno
, "Failed to bind socket");
224 closesocket(slisten
);
230 if (listen(slisten
,1) != 0) {
231 error_setg_errno(errp
, errno
, "Failed to listen on socket");
232 closesocket(slisten
);
238 saddr
->host
= g_strdup(uaddr
);
240 saddr
->port
= g_strdup_printf("%d",
241 inet_getport(e
) - port_offset
);
242 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
243 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
250 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
251 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
253 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
254 ((rc) == -EINPROGRESS)
257 /* Struct to store connect state for non blocking connect */
258 typedef struct ConnectState
{
260 struct addrinfo
*addr_list
;
261 struct addrinfo
*current_addr
;
262 NonBlockingConnectHandler
*callback
;
266 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
267 ConnectState
*connect_state
, Error
**errp
);
269 static void wait_for_connect(void *opaque
)
271 ConnectState
*s
= opaque
;
273 socklen_t valsize
= sizeof(val
);
277 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
280 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
281 } while (rc
== -1 && errno
== EINTR
);
283 /* update rc to contain error */
291 error_setg_errno(&err
, errno
, "Error connecting to socket");
296 /* try to connect to the next address on the list */
297 if (s
->current_addr
) {
298 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
299 s
->current_addr
= s
->current_addr
->ai_next
;
300 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
304 error_setg_errno(&err
, errno
, "Unable to start socket connect");
306 /* connect in progress */
312 freeaddrinfo(s
->addr_list
);
316 s
->callback(s
->fd
, err
, s
->opaque
);
323 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
324 ConnectState
*connect_state
, Error
**errp
)
328 *in_progress
= false;
330 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
332 error_setg_errno(errp
, errno
, "Failed to create socket");
335 socket_set_fast_reuse(sock
);
336 if (connect_state
!= NULL
) {
337 qemu_set_nonblock(sock
);
339 /* connect to peer */
342 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
345 } while (rc
== -EINTR
);
347 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
348 connect_state
->fd
= sock
;
349 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
352 error_setg_errno(errp
, errno
, "Failed to connect socket");
359 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
362 struct addrinfo ai
, *res
;
365 static int useV4Mapped
= 1;
367 memset(&ai
, 0, sizeof(ai
));
369 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
370 if (atomic_read(&useV4Mapped
)) {
371 ai
.ai_flags
|= AI_V4MAPPED
;
373 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
374 ai
.ai_socktype
= SOCK_STREAM
;
377 error_propagate(errp
, err
);
381 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
382 error_setg(errp
, "host and/or port not specified");
387 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
389 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
390 * then don't implement it in their getaddrinfo(). Detect
391 * this and retry without the flag since that's preferrable
394 if (rc
== EAI_BADFLAGS
&&
395 (ai
.ai_flags
& AI_V4MAPPED
)) {
396 atomic_set(&useV4Mapped
, 0);
397 ai
.ai_flags
&= ~AI_V4MAPPED
;
398 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
401 error_setg(errp
, "address resolution failed for %s:%s: %s",
402 saddr
->host
, saddr
->port
, gai_strerror(rc
));
409 * Create a socket and connect it to an address.
411 * @saddr: Inet socket address specification
412 * @errp: set on error
413 * @callback: callback function for non-blocking connect
414 * @opaque: opaque for callback function
416 * Returns: -1 on error, file descriptor on success.
418 * If @callback is non-null, the connect is non-blocking. If this
419 * function succeeds, callback will be called when the connection
420 * completes, with the file descriptor on success, or -1 on error.
422 int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
,
423 NonBlockingConnectHandler
*callback
, void *opaque
)
425 Error
*local_err
= NULL
;
426 struct addrinfo
*res
, *e
;
429 ConnectState
*connect_state
= NULL
;
431 res
= inet_parse_connect_saddr(saddr
, errp
);
436 if (callback
!= NULL
) {
437 connect_state
= g_malloc0(sizeof(*connect_state
));
438 connect_state
->addr_list
= res
;
439 connect_state
->callback
= callback
;
440 connect_state
->opaque
= opaque
;
443 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
444 error_free(local_err
);
446 if (connect_state
!= NULL
) {
447 connect_state
->current_addr
= e
;
449 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
456 error_propagate(errp
, local_err
);
457 } else if (in_progress
) {
458 /* wait_for_connect() will do the rest */
462 callback(sock
, NULL
, opaque
);
465 g_free(connect_state
);
470 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
471 InetSocketAddress
*sladdr
,
474 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
480 /* lookup peer addr */
481 memset(&ai
,0, sizeof(ai
));
482 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
483 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
484 ai
.ai_socktype
= SOCK_DGRAM
;
487 error_propagate(errp
, err
);
493 if (addr
== NULL
|| strlen(addr
) == 0) {
496 if (port
== NULL
|| strlen(port
) == 0) {
497 error_setg(errp
, "remote port not specified");
501 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
502 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
507 /* lookup local addr */
508 memset(&ai
,0, sizeof(ai
));
509 ai
.ai_flags
= AI_PASSIVE
;
510 ai
.ai_family
= peer
->ai_family
;
511 ai
.ai_socktype
= SOCK_DGRAM
;
516 if (addr
== NULL
|| strlen(addr
) == 0) {
519 if (!port
|| strlen(port
) == 0) {
527 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
528 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
534 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
536 error_setg_errno(errp
, errno
, "Failed to create socket");
539 socket_set_fast_reuse(sock
);
542 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
543 error_setg_errno(errp
, errno
, "Failed to bind socket");
547 /* connect to peer */
548 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
549 error_setg_errno(errp
, errno
, "Failed to connect socket");
571 /* compatibility wrapper */
572 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
574 InetSocketAddress
*addr
;
575 const char *optstr
, *h
;
581 addr
= g_new0(InetSocketAddress
, 1);
587 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
588 error_setg(errp
, "error parsing port in address '%s'", str
);
591 } else if (str
[0] == '[') {
593 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
594 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
597 addr
->ipv6
= addr
->has_ipv6
= true;
599 /* hostname or IPv4 addr */
600 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
601 error_setg(errp
, "error parsing address '%s'", str
);
604 if (host
[strspn(host
, "0123456789.")] == '\0') {
605 addr
->ipv4
= addr
->has_ipv4
= true;
609 addr
->host
= g_strdup(host
);
610 addr
->port
= g_strdup(port
);
614 h
= strstr(optstr
, ",to=");
617 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
618 (h
[pos
] != '\0' && h
[pos
] != ',')) {
619 error_setg(errp
, "error parsing to= argument");
625 if (strstr(optstr
, ",ipv4")) {
626 addr
->ipv4
= addr
->has_ipv4
= true;
628 if (strstr(optstr
, ",ipv6")) {
629 addr
->ipv6
= addr
->has_ipv6
= true;
634 qapi_free_InetSocketAddress(addr
);
640 * Create a blocking socket and connect it to an address.
642 * @str: address string
643 * @errp: set in case of an error
645 * Returns -1 in case of error, file descriptor on success
647 int inet_connect(const char *str
, Error
**errp
)
650 InetSocketAddress
*addr
;
652 addr
= inet_parse(str
, errp
);
654 sock
= inet_connect_saddr(addr
, errp
, NULL
, NULL
);
655 qapi_free_InetSocketAddress(addr
);
660 #ifdef CONFIG_AF_VSOCK
661 static bool vsock_parse_vaddr_to_sockaddr(const VsockSocketAddress
*vaddr
,
662 struct sockaddr_vm
*svm
,
665 unsigned long long val
;
667 memset(svm
, 0, sizeof(*svm
));
668 svm
->svm_family
= AF_VSOCK
;
670 if (parse_uint_full(vaddr
->cid
, &val
, 10) < 0 ||
672 error_setg(errp
, "Failed to parse cid '%s'", vaddr
->cid
);
677 if (parse_uint_full(vaddr
->port
, &val
, 10) < 0 ||
679 error_setg(errp
, "Failed to parse port '%s'", vaddr
->port
);
687 static int vsock_connect_addr(const struct sockaddr_vm
*svm
, bool *in_progress
,
688 ConnectState
*connect_state
, Error
**errp
)
692 *in_progress
= false;
694 sock
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
696 error_setg_errno(errp
, errno
, "Failed to create socket");
699 if (connect_state
!= NULL
) {
700 qemu_set_nonblock(sock
);
702 /* connect to peer */
705 if (connect(sock
, (const struct sockaddr
*)svm
, sizeof(*svm
)) < 0) {
708 } while (rc
== -EINTR
);
710 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
711 connect_state
->fd
= sock
;
712 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
715 error_setg_errno(errp
, errno
, "Failed to connect socket");
722 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
,
723 NonBlockingConnectHandler
*callback
,
726 struct sockaddr_vm svm
;
729 ConnectState
*connect_state
= NULL
;
731 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
735 if (callback
!= NULL
) {
736 connect_state
= g_malloc0(sizeof(*connect_state
));
737 connect_state
->callback
= callback
;
738 connect_state
->opaque
= opaque
;
741 sock
= vsock_connect_addr(&svm
, &in_progress
, connect_state
, errp
);
744 } else if (in_progress
) {
745 /* wait_for_connect() will do the rest */
749 callback(sock
, NULL
, opaque
);
752 g_free(connect_state
);
756 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
759 struct sockaddr_vm svm
;
762 if (!vsock_parse_vaddr_to_sockaddr(vaddr
, &svm
, errp
)) {
766 slisten
= qemu_socket(AF_VSOCK
, SOCK_STREAM
, 0);
768 error_setg_errno(errp
, errno
, "Failed to create socket");
772 if (bind(slisten
, (const struct sockaddr
*)&svm
, sizeof(svm
)) != 0) {
773 error_setg_errno(errp
, errno
, "Failed to bind socket");
774 closesocket(slisten
);
778 if (listen(slisten
, 1) != 0) {
779 error_setg_errno(errp
, errno
, "Failed to listen on socket");
780 closesocket(slisten
);
786 static VsockSocketAddress
*vsock_parse(const char *str
, Error
**errp
)
788 VsockSocketAddress
*addr
= NULL
;
793 if (sscanf(str
, "%32[^:]:%32[^,]%n", cid
, port
, &n
) != 2) {
794 error_setg(errp
, "error parsing address '%s'", str
);
797 if (str
[n
] != '\0') {
798 error_setg(errp
, "trailing characters in address '%s'", str
);
802 addr
= g_new0(VsockSocketAddress
, 1);
803 addr
->cid
= g_strdup(cid
);
804 addr
->port
= g_strdup(port
);
808 static void vsock_unsupported(Error
**errp
)
810 error_setg(errp
, "socket family AF_VSOCK unsupported");
813 static int vsock_connect_saddr(VsockSocketAddress
*vaddr
, Error
**errp
,
814 NonBlockingConnectHandler
*callback
,
817 vsock_unsupported(errp
);
821 static int vsock_listen_saddr(VsockSocketAddress
*vaddr
,
824 vsock_unsupported(errp
);
828 static VsockSocketAddress
*vsock_parse(const char *str
, Error
**errp
)
830 vsock_unsupported(errp
);
833 #endif /* CONFIG_AF_VSOCK */
837 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
841 struct sockaddr_un un
;
844 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
846 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
850 memset(&un
, 0, sizeof(un
));
851 un
.sun_family
= AF_UNIX
;
852 if (saddr
->path
&& strlen(saddr
->path
)) {
853 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
855 const char *tmpdir
= getenv("TMPDIR");
856 tmpdir
= tmpdir
? tmpdir
: "/tmp";
857 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
858 tmpdir
) >= sizeof(un
.sun_path
)) {
859 error_setg_errno(errp
, errno
,
860 "TMPDIR environment variable (%s) too large", tmpdir
);
865 * This dummy fd usage silences the mktemp() unsecure warning.
866 * Using mkstemp() doesn't make things more secure here
867 * though. bind() complains about existing files, so we have
868 * to unlink first and thus re-open the race window. The
869 * worst case possible is bind() failing, i.e. a DoS attack.
871 fd
= mkstemp(un
.sun_path
);
873 error_setg_errno(errp
, errno
,
874 "Failed to make a temporary socket name in %s", tmpdir
);
880 saddr
->path
= g_strdup(un
.sun_path
);
884 if (unlink(un
.sun_path
) < 0 && errno
!= ENOENT
) {
885 error_setg_errno(errp
, errno
,
886 "Failed to unlink socket %s", un
.sun_path
);
889 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
890 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
893 if (listen(sock
, 1) < 0) {
894 error_setg_errno(errp
, errno
, "Failed to listen on socket");
905 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
906 NonBlockingConnectHandler
*callback
, void *opaque
)
908 struct sockaddr_un un
;
909 ConnectState
*connect_state
= NULL
;
912 if (saddr
->path
== NULL
) {
913 error_setg(errp
, "unix connect: no path specified");
917 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
919 error_setg_errno(errp
, errno
, "Failed to create socket");
922 if (callback
!= NULL
) {
923 connect_state
= g_malloc0(sizeof(*connect_state
));
924 connect_state
->callback
= callback
;
925 connect_state
->opaque
= opaque
;
926 qemu_set_nonblock(sock
);
929 memset(&un
, 0, sizeof(un
));
930 un
.sun_family
= AF_UNIX
;
931 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
933 /* connect to peer */
936 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
939 } while (rc
== -EINTR
);
941 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
942 connect_state
->fd
= sock
;
943 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
945 } else if (rc
>= 0) {
946 /* non blocking socket immediate success, call callback */
947 if (callback
!= NULL
) {
948 callback(sock
, NULL
, opaque
);
953 error_setg_errno(errp
, -rc
, "Failed to connect socket");
958 g_free(connect_state
);
964 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
968 error_setg(errp
, "unix sockets are not available on windows");
973 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
974 NonBlockingConnectHandler
*callback
, void *opaque
)
976 error_setg(errp
, "unix sockets are not available on windows");
982 /* compatibility wrapper */
983 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
987 UnixSocketAddress
*saddr
;
989 saddr
= g_new0(UnixSocketAddress
, 1);
991 optstr
= strchr(str
, ',');
995 path
= g_malloc(len
+1);
996 snprintf(path
, len
+1, "%.*s", len
, str
);
1000 saddr
->path
= g_strdup(str
);
1003 sock
= unix_listen_saddr(saddr
, true, errp
);
1005 if (sock
!= -1 && ostr
) {
1006 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
1009 qapi_free_UnixSocketAddress(saddr
);
1013 int unix_connect(const char *path
, Error
**errp
)
1015 UnixSocketAddress
*saddr
;
1018 saddr
= g_new0(UnixSocketAddress
, 1);
1019 saddr
->path
= g_strdup(path
);
1020 sock
= unix_connect_saddr(saddr
, errp
, NULL
, NULL
);
1021 qapi_free_UnixSocketAddress(saddr
);
1026 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
1028 SocketAddress
*addr
;
1030 addr
= g_new0(SocketAddress
, 1);
1031 if (strstart(str
, "unix:", NULL
)) {
1032 if (str
[5] == '\0') {
1033 error_setg(errp
, "invalid Unix socket address");
1036 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1037 addr
->u
.q_unix
.data
= g_new(UnixSocketAddress
, 1);
1038 addr
->u
.q_unix
.data
->path
= g_strdup(str
+ 5);
1040 } else if (strstart(str
, "fd:", NULL
)) {
1041 if (str
[3] == '\0') {
1042 error_setg(errp
, "invalid file descriptor address");
1045 addr
->type
= SOCKET_ADDRESS_KIND_FD
;
1046 addr
->u
.fd
.data
= g_new(String
, 1);
1047 addr
->u
.fd
.data
->str
= g_strdup(str
+ 3);
1049 } else if (strstart(str
, "vsock:", NULL
)) {
1050 addr
->type
= SOCKET_ADDRESS_KIND_VSOCK
;
1051 addr
->u
.vsock
.data
= vsock_parse(str
+ strlen("vsock:"), errp
);
1052 if (addr
->u
.vsock
.data
== NULL
) {
1056 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1057 addr
->u
.inet
.data
= inet_parse(str
, errp
);
1058 if (addr
->u
.inet
.data
== NULL
) {
1065 qapi_free_SocketAddress(addr
);
1069 int socket_connect(SocketAddress
*addr
, Error
**errp
,
1070 NonBlockingConnectHandler
*callback
, void *opaque
)
1074 switch (addr
->type
) {
1075 case SOCKET_ADDRESS_KIND_INET
:
1076 fd
= inet_connect_saddr(addr
->u
.inet
.data
, errp
, callback
, opaque
);
1079 case SOCKET_ADDRESS_KIND_UNIX
:
1080 fd
= unix_connect_saddr(addr
->u
.q_unix
.data
, errp
, callback
, opaque
);
1083 case SOCKET_ADDRESS_KIND_FD
:
1084 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
1085 if (fd
>= 0 && callback
) {
1086 qemu_set_nonblock(fd
);
1087 callback(fd
, NULL
, opaque
);
1091 case SOCKET_ADDRESS_KIND_VSOCK
:
1092 fd
= vsock_connect_saddr(addr
->u
.vsock
.data
, errp
, callback
, opaque
);
1101 int socket_listen(SocketAddress
*addr
, Error
**errp
)
1105 switch (addr
->type
) {
1106 case SOCKET_ADDRESS_KIND_INET
:
1107 fd
= inet_listen_saddr(addr
->u
.inet
.data
, 0, false, errp
);
1110 case SOCKET_ADDRESS_KIND_UNIX
:
1111 fd
= unix_listen_saddr(addr
->u
.q_unix
.data
, false, errp
);
1114 case SOCKET_ADDRESS_KIND_FD
:
1115 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
1118 case SOCKET_ADDRESS_KIND_VSOCK
:
1119 fd
= vsock_listen_saddr(addr
->u
.vsock
.data
, errp
);
1128 void socket_listen_cleanup(int fd
, Error
**errp
)
1130 SocketAddress
*addr
;
1132 addr
= socket_local_address(fd
, errp
);
1134 if (addr
->type
== SOCKET_ADDRESS_KIND_UNIX
1135 && addr
->u
.q_unix
.data
->path
) {
1136 if (unlink(addr
->u
.q_unix
.data
->path
) < 0 && errno
!= ENOENT
) {
1137 error_setg_errno(errp
, errno
,
1138 "Failed to unlink socket %s",
1139 addr
->u
.q_unix
.data
->path
);
1143 qapi_free_SocketAddress(addr
);
1146 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
1150 switch (remote
->type
) {
1151 case SOCKET_ADDRESS_KIND_INET
:
1152 fd
= inet_dgram_saddr(remote
->u
.inet
.data
,
1153 local
? local
->u
.inet
.data
: NULL
, errp
);
1157 error_setg(errp
, "socket type unsupported for datagram");
1164 static SocketAddress
*
1165 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1169 char host
[NI_MAXHOST
];
1170 char serv
[NI_MAXSERV
];
1171 SocketAddress
*addr
;
1172 InetSocketAddress
*inet
;
1175 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1178 NI_NUMERICHOST
| NI_NUMERICSERV
);
1180 error_setg(errp
, "Cannot format numeric socket address: %s",
1185 addr
= g_new0(SocketAddress
, 1);
1186 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1187 inet
= addr
->u
.inet
.data
= g_new0(InetSocketAddress
, 1);
1188 inet
->host
= g_strdup(host
);
1189 inet
->port
= g_strdup(serv
);
1190 if (sa
->ss_family
== AF_INET
) {
1191 inet
->has_ipv4
= inet
->ipv4
= true;
1193 inet
->has_ipv6
= inet
->ipv6
= true;
1201 static SocketAddress
*
1202 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1206 SocketAddress
*addr
;
1207 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1209 addr
= g_new0(SocketAddress
, 1);
1210 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1211 addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1212 if (su
->sun_path
[0]) {
1213 addr
->u
.q_unix
.data
->path
= g_strndup(su
->sun_path
,
1214 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_KIND_VSOCK
;
1233 addr
->u
.vsock
.data
= vaddr
= g_new0(VsockSocketAddress
, 1);
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
);
1299 char *socket_address_to_string(struct SocketAddress
*addr
, Error
**errp
)
1302 InetSocketAddress
*inet
;
1303 char host_port
[INET6_ADDRSTRLEN
+ 5 + 4];
1305 switch (addr
->type
) {
1306 case SOCKET_ADDRESS_KIND_INET
:
1307 inet
= addr
->u
.inet
.data
;
1308 if (strchr(inet
->host
, ':') == NULL
) {
1309 snprintf(host_port
, sizeof(host_port
), "%s:%s", inet
->host
,
1311 buf
= g_strdup(host_port
);
1313 snprintf(host_port
, sizeof(host_port
), "[%s]:%s", inet
->host
,
1315 buf
= g_strdup(host_port
);
1319 case SOCKET_ADDRESS_KIND_UNIX
:
1320 buf
= g_strdup(addr
->u
.q_unix
.data
->path
);
1323 case SOCKET_ADDRESS_KIND_FD
:
1324 buf
= g_strdup(addr
->u
.fd
.data
->str
);
1327 case SOCKET_ADDRESS_KIND_VSOCK
:
1328 buf
= g_strdup_printf("%s:%s",
1329 addr
->u
.vsock
.data
->cid
,
1330 addr
->u
.vsock
.data
->port
);
1334 error_setg(errp
, "socket family %d unsupported",