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.
25 #include "monitor/monitor.h"
26 #include "qemu/sockets.h"
27 #include "qemu/main-loop.h"
28 #include "qapi/qmp-input-visitor.h"
29 #include "qapi/qmp-output-visitor.h"
30 #include "qapi-visit.h"
33 # define AI_ADDRCONFIG 0
36 # define AI_V4MAPPED 0
40 static int inet_getport(struct addrinfo
*e
)
42 struct sockaddr_in
*i4
;
43 struct sockaddr_in6
*i6
;
45 switch (e
->ai_family
) {
47 i6
= (void*)e
->ai_addr
;
48 return ntohs(i6
->sin6_port
);
50 i4
= (void*)e
->ai_addr
;
51 return ntohs(i4
->sin_port
);
57 static void inet_setport(struct addrinfo
*e
, int port
)
59 struct sockaddr_in
*i4
;
60 struct sockaddr_in6
*i6
;
62 switch (e
->ai_family
) {
64 i6
= (void*)e
->ai_addr
;
65 i6
->sin6_port
= htons(port
);
68 i4
= (void*)e
->ai_addr
;
69 i4
->sin_port
= htons(port
);
74 NetworkAddressFamily
inet_netfamily(int family
)
77 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
78 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
79 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
81 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
85 * Matrix we're trying to apply
98 * NB, this matrix is only about getting the neccessary results
99 * from getaddrinfo(). Some of the cases require further work
100 * after reading results from getaddrinfo in order to fully
101 * apply the logic the end user wants. eg with the last case
102 * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
103 * guarantee the ipv6=t part of the request - we need more
104 * checks to provide ipv4=t part of the guarantee. This is
105 * outside scope of this method and not currently handled by
108 static int inet_ai_family_from_address(InetSocketAddress
*addr
,
111 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
112 !addr
->ipv6
&& !addr
->ipv4
) {
113 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
116 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
119 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
125 static int inet_listen_saddr(InetSocketAddress
*saddr
,
130 struct addrinfo ai
,*res
,*e
;
132 char uaddr
[INET6_ADDRSTRLEN
+1];
134 int slisten
, rc
, port_min
, port_max
, p
;
137 memset(&ai
,0, sizeof(ai
));
138 ai
.ai_flags
= AI_PASSIVE
;
139 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
140 ai
.ai_socktype
= SOCK_STREAM
;
143 error_propagate(errp
, err
);
147 if (saddr
->host
== NULL
) {
148 error_setg(errp
, "host not specified");
151 if (saddr
->port
!= NULL
) {
152 pstrcpy(port
, sizeof(port
), saddr
->port
);
159 unsigned long long baseport
;
160 if (strlen(port
) == 0) {
161 error_setg(errp
, "port not specified");
164 if (parse_uint_full(port
, &baseport
, 10) < 0) {
165 error_setg(errp
, "can't convert to a number: %s", port
);
168 if (baseport
> 65535 ||
169 baseport
+ port_offset
> 65535) {
170 error_setg(errp
, "port %s out of range", port
);
173 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
175 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
176 strlen(port
) ? port
: NULL
, &ai
, &res
);
178 error_setg(errp
, "address resolution failed for %s:%s: %s",
179 saddr
->host
, port
, gai_strerror(rc
));
183 /* create socket + bind */
184 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
185 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
186 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
187 NI_NUMERICHOST
| NI_NUMERICSERV
);
188 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
191 error_setg_errno(errp
, errno
, "Failed to create socket");
196 socket_set_fast_reuse(slisten
);
198 if (e
->ai_family
== PF_INET6
) {
199 /* listen on both ipv4 and ipv6 */
201 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
206 port_min
= inet_getport(e
);
207 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
208 for (p
= port_min
; p
<= port_max
; p
++) {
210 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
215 error_setg_errno(errp
, errno
, "Failed to bind socket");
219 closesocket(slisten
);
225 if (listen(slisten
,1) != 0) {
226 error_setg_errno(errp
, errno
, "Failed to listen on socket");
227 closesocket(slisten
);
233 saddr
->host
= g_strdup(uaddr
);
235 saddr
->port
= g_strdup_printf("%d",
236 inet_getport(e
) - port_offset
);
237 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
238 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
245 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
246 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
248 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
249 ((rc) == -EINPROGRESS)
252 /* Struct to store connect state for non blocking connect */
253 typedef struct ConnectState
{
255 struct addrinfo
*addr_list
;
256 struct addrinfo
*current_addr
;
257 NonBlockingConnectHandler
*callback
;
261 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
262 ConnectState
*connect_state
, Error
**errp
);
264 static void wait_for_connect(void *opaque
)
266 ConnectState
*s
= opaque
;
268 socklen_t valsize
= sizeof(val
);
272 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
275 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
276 } while (rc
== -1 && socket_error() == EINTR
);
278 /* update rc to contain error */
286 error_setg_errno(&err
, errno
, "Error connecting to socket");
291 /* try to connect to the next address on the list */
292 if (s
->current_addr
) {
293 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
294 s
->current_addr
= s
->current_addr
->ai_next
;
295 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
299 error_setg_errno(&err
, errno
, "Unable to start socket connect");
301 /* connect in progress */
307 freeaddrinfo(s
->addr_list
);
311 s
->callback(s
->fd
, err
, s
->opaque
);
318 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
319 ConnectState
*connect_state
, Error
**errp
)
323 *in_progress
= false;
325 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
327 error_setg_errno(errp
, errno
, "Failed to create socket");
330 socket_set_fast_reuse(sock
);
331 if (connect_state
!= NULL
) {
332 qemu_set_nonblock(sock
);
334 /* connect to peer */
337 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
338 rc
= -socket_error();
340 } while (rc
== -EINTR
);
342 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
343 connect_state
->fd
= sock
;
344 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
347 error_setg_errno(errp
, errno
, "Failed to connect socket");
354 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
357 struct addrinfo ai
, *res
;
361 memset(&ai
, 0, sizeof(ai
));
363 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
364 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
365 ai
.ai_socktype
= SOCK_STREAM
;
368 error_propagate(errp
, err
);
372 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
373 error_setg(errp
, "host and/or port not specified");
378 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
380 error_setg(errp
, "address resolution failed for %s:%s: %s",
381 saddr
->host
, saddr
->port
, gai_strerror(rc
));
388 * Create a socket and connect it to an address.
390 * @saddr: Inet socket address specification
391 * @errp: set on error
392 * @callback: callback function for non-blocking connect
393 * @opaque: opaque for callback function
395 * Returns: -1 on error, file descriptor on success.
397 * If @callback is non-null, the connect is non-blocking. If this
398 * function succeeds, callback will be called when the connection
399 * completes, with the file descriptor on success, or -1 on error.
401 static int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
,
402 NonBlockingConnectHandler
*callback
, void *opaque
)
404 Error
*local_err
= NULL
;
405 struct addrinfo
*res
, *e
;
408 ConnectState
*connect_state
= NULL
;
410 res
= inet_parse_connect_saddr(saddr
, errp
);
415 if (callback
!= NULL
) {
416 connect_state
= g_malloc0(sizeof(*connect_state
));
417 connect_state
->addr_list
= res
;
418 connect_state
->callback
= callback
;
419 connect_state
->opaque
= opaque
;
422 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
423 error_free(local_err
);
425 if (connect_state
!= NULL
) {
426 connect_state
->current_addr
= e
;
428 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
435 error_propagate(errp
, local_err
);
436 } else if (in_progress
) {
437 /* wait_for_connect() will do the rest */
441 callback(sock
, NULL
, opaque
);
444 g_free(connect_state
);
449 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
450 InetSocketAddress
*sladdr
,
453 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
459 /* lookup peer addr */
460 memset(&ai
,0, sizeof(ai
));
461 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
462 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
463 ai
.ai_socktype
= SOCK_DGRAM
;
466 error_propagate(errp
, err
);
472 if (addr
== NULL
|| strlen(addr
) == 0) {
475 if (port
== NULL
|| strlen(port
) == 0) {
476 error_setg(errp
, "remote port not specified");
480 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
481 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
486 /* lookup local addr */
487 memset(&ai
,0, sizeof(ai
));
488 ai
.ai_flags
= AI_PASSIVE
;
489 ai
.ai_family
= peer
->ai_family
;
490 ai
.ai_socktype
= SOCK_DGRAM
;
495 if (addr
== NULL
|| strlen(addr
) == 0) {
498 if (!port
|| strlen(port
) == 0) {
506 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
507 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
513 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
515 error_setg_errno(errp
, errno
, "Failed to create socket");
518 socket_set_fast_reuse(sock
);
521 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
522 error_setg_errno(errp
, errno
, "Failed to bind socket");
526 /* connect to peer */
527 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
528 error_setg_errno(errp
, errno
, "Failed to connect socket");
546 /* compatibility wrapper */
547 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
549 InetSocketAddress
*addr
;
550 const char *optstr
, *h
;
556 addr
= g_new0(InetSocketAddress
, 1);
562 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
563 error_setg(errp
, "error parsing port in address '%s'", str
);
566 } else if (str
[0] == '[') {
568 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
569 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
572 addr
->ipv6
= addr
->has_ipv6
= true;
574 /* hostname or IPv4 addr */
575 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
576 error_setg(errp
, "error parsing address '%s'", str
);
579 if (host
[strspn(host
, "0123456789.")] == '\0') {
580 addr
->ipv4
= addr
->has_ipv4
= true;
584 addr
->host
= g_strdup(host
);
585 addr
->port
= g_strdup(port
);
589 h
= strstr(optstr
, ",to=");
592 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
593 (h
[pos
] != '\0' && h
[pos
] != ',')) {
594 error_setg(errp
, "error parsing to= argument");
600 if (strstr(optstr
, ",ipv4")) {
601 addr
->ipv4
= addr
->has_ipv4
= true;
603 if (strstr(optstr
, ",ipv6")) {
604 addr
->ipv6
= addr
->has_ipv6
= true;
609 qapi_free_InetSocketAddress(addr
);
613 int inet_listen(const char *str
, char *ostr
, int olen
,
614 int socktype
, int port_offset
, Error
**errp
)
618 InetSocketAddress
*addr
;
620 addr
= inet_parse(str
, errp
);
622 sock
= inet_listen_saddr(addr
, port_offset
, true, errp
);
623 if (sock
!= -1 && ostr
) {
624 optstr
= strchr(str
, ',');
626 snprintf(ostr
, olen
, "[%s]:%s%s",
629 optstr
? optstr
: "");
631 snprintf(ostr
, olen
, "%s:%s%s",
634 optstr
? optstr
: "");
637 qapi_free_InetSocketAddress(addr
);
643 * Create a blocking socket and connect it to an address.
645 * @str: address string
646 * @errp: set in case of an error
648 * Returns -1 in case of error, file descriptor on success
650 int inet_connect(const char *str
, Error
**errp
)
653 InetSocketAddress
*addr
;
655 addr
= inet_parse(str
, errp
);
657 sock
= inet_connect_saddr(addr
, errp
, NULL
, NULL
);
658 qapi_free_InetSocketAddress(addr
);
664 * Create a non-blocking socket and connect it to an address.
665 * Calls the callback function with fd in case of success or -1 in case of
668 * @str: address string
669 * @callback: callback function that is called when connect completes,
671 * @opaque: opaque for callback function
672 * @errp: set in case of an error
674 * Returns: -1 on immediate error, file descriptor on success.
676 int inet_nonblocking_connect(const char *str
,
677 NonBlockingConnectHandler
*callback
,
678 void *opaque
, Error
**errp
)
681 InetSocketAddress
*addr
;
683 g_assert(callback
!= NULL
);
685 addr
= inet_parse(str
, errp
);
687 sock
= inet_connect_saddr(addr
, errp
, callback
, opaque
);
688 qapi_free_InetSocketAddress(addr
);
695 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
699 struct sockaddr_un un
;
702 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
704 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
708 memset(&un
, 0, sizeof(un
));
709 un
.sun_family
= AF_UNIX
;
710 if (saddr
->path
&& strlen(saddr
->path
)) {
711 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
713 const char *tmpdir
= getenv("TMPDIR");
714 tmpdir
= tmpdir
? tmpdir
: "/tmp";
715 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
716 tmpdir
) >= sizeof(un
.sun_path
)) {
717 error_setg_errno(errp
, errno
,
718 "TMPDIR environment variable (%s) too large", tmpdir
);
723 * This dummy fd usage silences the mktemp() unsecure warning.
724 * Using mkstemp() doesn't make things more secure here
725 * though. bind() complains about existing files, so we have
726 * to unlink first and thus re-open the race window. The
727 * worst case possible is bind() failing, i.e. a DoS attack.
729 fd
= mkstemp(un
.sun_path
);
731 error_setg_errno(errp
, errno
,
732 "Failed to make a temporary socket name in %s", tmpdir
);
738 saddr
->path
= g_strdup(un
.sun_path
);
742 if (unlink(un
.sun_path
) < 0 && errno
!= ENOENT
) {
743 error_setg_errno(errp
, errno
,
744 "Failed to unlink socket %s", un
.sun_path
);
747 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
748 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
751 if (listen(sock
, 1) < 0) {
752 error_setg_errno(errp
, errno
, "Failed to listen on socket");
763 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
764 NonBlockingConnectHandler
*callback
, void *opaque
)
766 struct sockaddr_un un
;
767 ConnectState
*connect_state
= NULL
;
770 if (saddr
->path
== NULL
) {
771 error_setg(errp
, "unix connect: no path specified");
775 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
777 error_setg_errno(errp
, errno
, "Failed to create socket");
780 if (callback
!= NULL
) {
781 connect_state
= g_malloc0(sizeof(*connect_state
));
782 connect_state
->callback
= callback
;
783 connect_state
->opaque
= opaque
;
784 qemu_set_nonblock(sock
);
787 memset(&un
, 0, sizeof(un
));
788 un
.sun_family
= AF_UNIX
;
789 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
791 /* connect to peer */
794 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
795 rc
= -socket_error();
797 } while (rc
== -EINTR
);
799 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
800 connect_state
->fd
= sock
;
801 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
803 } else if (rc
>= 0) {
804 /* non blocking socket immediate success, call callback */
805 if (callback
!= NULL
) {
806 callback(sock
, NULL
, opaque
);
811 error_setg_errno(errp
, -rc
, "Failed to connect socket");
816 g_free(connect_state
);
822 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
826 error_setg(errp
, "unix sockets are not available on windows");
831 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
832 NonBlockingConnectHandler
*callback
, void *opaque
)
834 error_setg(errp
, "unix sockets are not available on windows");
840 /* compatibility wrapper */
841 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
845 UnixSocketAddress
*saddr
;
847 saddr
= g_new0(UnixSocketAddress
, 1);
849 optstr
= strchr(str
, ',');
853 path
= g_malloc(len
+1);
854 snprintf(path
, len
+1, "%.*s", len
, str
);
858 saddr
->path
= g_strdup(str
);
861 sock
= unix_listen_saddr(saddr
, true, errp
);
863 if (sock
!= -1 && ostr
)
864 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
865 qapi_free_UnixSocketAddress(saddr
);
869 int unix_connect(const char *path
, Error
**errp
)
871 UnixSocketAddress
*saddr
;
874 saddr
= g_new0(UnixSocketAddress
, 1);
875 saddr
->path
= g_strdup(path
);
876 sock
= unix_connect_saddr(saddr
, errp
, NULL
, NULL
);
877 qapi_free_UnixSocketAddress(saddr
);
882 int unix_nonblocking_connect(const char *path
,
883 NonBlockingConnectHandler
*callback
,
884 void *opaque
, Error
**errp
)
886 UnixSocketAddress
*saddr
;
889 g_assert(callback
!= NULL
);
891 saddr
= g_new0(UnixSocketAddress
, 1);
892 saddr
->path
= g_strdup(path
);
893 sock
= unix_connect_saddr(saddr
, errp
, callback
, opaque
);
894 qapi_free_UnixSocketAddress(saddr
);
898 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
902 addr
= g_new0(SocketAddress
, 1);
903 if (strstart(str
, "unix:", NULL
)) {
904 if (str
[5] == '\0') {
905 error_setg(errp
, "invalid Unix socket address");
908 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
909 addr
->u
.q_unix
= g_new(UnixSocketAddress
, 1);
910 addr
->u
.q_unix
->path
= g_strdup(str
+ 5);
912 } else if (strstart(str
, "fd:", NULL
)) {
913 if (str
[3] == '\0') {
914 error_setg(errp
, "invalid file descriptor address");
917 addr
->type
= SOCKET_ADDRESS_KIND_FD
;
918 addr
->u
.fd
= g_new(String
, 1);
919 addr
->u
.fd
->str
= g_strdup(str
+ 3);
922 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
923 addr
->u
.inet
= inet_parse(str
, errp
);
924 if (addr
->u
.inet
== NULL
) {
931 qapi_free_SocketAddress(addr
);
935 int socket_connect(SocketAddress
*addr
, Error
**errp
,
936 NonBlockingConnectHandler
*callback
, void *opaque
)
940 switch (addr
->type
) {
941 case SOCKET_ADDRESS_KIND_INET
:
942 fd
= inet_connect_saddr(addr
->u
.inet
, errp
, callback
, opaque
);
945 case SOCKET_ADDRESS_KIND_UNIX
:
946 fd
= unix_connect_saddr(addr
->u
.q_unix
, errp
, callback
, opaque
);
949 case SOCKET_ADDRESS_KIND_FD
:
950 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
->str
, errp
);
951 if (fd
>= 0 && callback
) {
952 qemu_set_nonblock(fd
);
953 callback(fd
, NULL
, opaque
);
963 int socket_listen(SocketAddress
*addr
, Error
**errp
)
967 switch (addr
->type
) {
968 case SOCKET_ADDRESS_KIND_INET
:
969 fd
= inet_listen_saddr(addr
->u
.inet
, 0, false, errp
);
972 case SOCKET_ADDRESS_KIND_UNIX
:
973 fd
= unix_listen_saddr(addr
->u
.q_unix
, false, errp
);
976 case SOCKET_ADDRESS_KIND_FD
:
977 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
->str
, errp
);
986 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
990 switch (remote
->type
) {
991 case SOCKET_ADDRESS_KIND_INET
:
992 fd
= inet_dgram_saddr(remote
->u
.inet
, local
? local
->u
.inet
: NULL
, errp
);
996 error_setg(errp
, "socket type unsupported for datagram");
1003 static SocketAddress
*
1004 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1008 char host
[NI_MAXHOST
];
1009 char serv
[NI_MAXSERV
];
1010 SocketAddress
*addr
;
1013 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1016 NI_NUMERICHOST
| NI_NUMERICSERV
);
1018 error_setg(errp
, "Cannot format numeric socket address: %s",
1023 addr
= g_new0(SocketAddress
, 1);
1024 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1025 addr
->u
.inet
= g_new0(InetSocketAddress
, 1);
1026 addr
->u
.inet
->host
= g_strdup(host
);
1027 addr
->u
.inet
->port
= g_strdup(serv
);
1028 if (sa
->ss_family
== AF_INET
) {
1029 addr
->u
.inet
->has_ipv4
= addr
->u
.inet
->ipv4
= true;
1031 addr
->u
.inet
->has_ipv6
= addr
->u
.inet
->ipv6
= true;
1039 static SocketAddress
*
1040 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1044 SocketAddress
*addr
;
1045 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1047 addr
= g_new0(SocketAddress
, 1);
1048 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1049 addr
->u
.q_unix
= g_new0(UnixSocketAddress
, 1);
1050 if (su
->sun_path
[0]) {
1051 addr
->u
.q_unix
->path
= g_strndup(su
->sun_path
,
1052 sizeof(su
->sun_path
));
1060 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1064 switch (sa
->ss_family
) {
1067 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1071 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1075 error_setg(errp
, "socket family %d unsupported",
1083 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1085 struct sockaddr_storage ss
;
1086 socklen_t sslen
= sizeof(ss
);
1088 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1089 error_setg_errno(errp
, socket_error(), "%s",
1090 "Unable to query local socket address");
1094 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1098 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1100 struct sockaddr_storage ss
;
1101 socklen_t sslen
= sizeof(ss
);
1103 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1104 error_setg_errno(errp
, socket_error(), "%s",
1105 "Unable to query remote socket address");
1109 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1113 void qapi_copy_SocketAddress(SocketAddress
**p_dest
,
1116 QmpOutputVisitor
*qov
;
1117 QmpInputVisitor
*qiv
;
1123 qov
= qmp_output_visitor_new();
1124 ov
= qmp_output_get_visitor(qov
);
1125 visit_type_SocketAddress(ov
, &src
, NULL
, &error_abort
);
1126 obj
= qmp_output_get_qobject(qov
);
1127 qmp_output_visitor_cleanup(qov
);
1132 qiv
= qmp_input_visitor_new(obj
);
1133 iv
= qmp_input_get_visitor(qiv
);
1134 visit_type_SocketAddress(iv
, p_dest
, NULL
, &error_abort
);
1135 qmp_input_visitor_cleanup(qiv
);
1136 qobject_decref(obj
);