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 #include "monitor/monitor.h"
21 #include "qapi/error.h"
22 #include "qemu/sockets.h"
23 #include "qemu/main-loop.h"
24 #include "qapi/qmp-input-visitor.h"
25 #include "qapi/qmp-output-visitor.h"
26 #include "qapi-visit.h"
29 # define AI_ADDRCONFIG 0
32 # define AI_V4MAPPED 0
36 static int inet_getport(struct addrinfo
*e
)
38 struct sockaddr_in
*i4
;
39 struct sockaddr_in6
*i6
;
41 switch (e
->ai_family
) {
43 i6
= (void*)e
->ai_addr
;
44 return ntohs(i6
->sin6_port
);
46 i4
= (void*)e
->ai_addr
;
47 return ntohs(i4
->sin_port
);
53 static void inet_setport(struct addrinfo
*e
, int port
)
55 struct sockaddr_in
*i4
;
56 struct sockaddr_in6
*i6
;
58 switch (e
->ai_family
) {
60 i6
= (void*)e
->ai_addr
;
61 i6
->sin6_port
= htons(port
);
64 i4
= (void*)e
->ai_addr
;
65 i4
->sin_port
= htons(port
);
70 NetworkAddressFamily
inet_netfamily(int family
)
73 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
74 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
75 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
77 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
81 * Matrix we're trying to apply
94 * NB, this matrix is only about getting the neccessary results
95 * from getaddrinfo(). Some of the cases require further work
96 * after reading results from getaddrinfo in order to fully
97 * apply the logic the end user wants. eg with the last case
98 * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
99 * guarantee the ipv6=t part of the request - we need more
100 * checks to provide ipv4=t part of the guarantee. This is
101 * outside scope of this method and not currently handled by
104 static int inet_ai_family_from_address(InetSocketAddress
*addr
,
107 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
108 !addr
->ipv6
&& !addr
->ipv4
) {
109 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
112 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
115 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
121 static int inet_listen_saddr(InetSocketAddress
*saddr
,
126 struct addrinfo ai
,*res
,*e
;
128 char uaddr
[INET6_ADDRSTRLEN
+1];
130 int slisten
, rc
, port_min
, port_max
, p
;
133 memset(&ai
,0, sizeof(ai
));
134 ai
.ai_flags
= AI_PASSIVE
;
135 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
136 ai
.ai_socktype
= SOCK_STREAM
;
139 error_propagate(errp
, err
);
143 if (saddr
->host
== NULL
) {
144 error_setg(errp
, "host not specified");
147 if (saddr
->port
!= NULL
) {
148 pstrcpy(port
, sizeof(port
), saddr
->port
);
155 unsigned long long baseport
;
156 if (strlen(port
) == 0) {
157 error_setg(errp
, "port not specified");
160 if (parse_uint_full(port
, &baseport
, 10) < 0) {
161 error_setg(errp
, "can't convert to a number: %s", port
);
164 if (baseport
> 65535 ||
165 baseport
+ port_offset
> 65535) {
166 error_setg(errp
, "port %s out of range", port
);
169 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
171 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
172 strlen(port
) ? port
: NULL
, &ai
, &res
);
174 error_setg(errp
, "address resolution failed for %s:%s: %s",
175 saddr
->host
, port
, gai_strerror(rc
));
179 /* create socket + bind */
180 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
181 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
182 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
183 NI_NUMERICHOST
| NI_NUMERICSERV
);
184 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
187 error_setg_errno(errp
, errno
, "Failed to create socket");
192 socket_set_fast_reuse(slisten
);
194 if (e
->ai_family
== PF_INET6
) {
195 /* listen on both ipv4 and ipv6 */
197 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
202 port_min
= inet_getport(e
);
203 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
204 for (p
= port_min
; p
<= port_max
; p
++) {
206 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
211 error_setg_errno(errp
, errno
, "Failed to bind socket");
215 closesocket(slisten
);
221 if (listen(slisten
,1) != 0) {
222 error_setg_errno(errp
, errno
, "Failed to listen on socket");
223 closesocket(slisten
);
229 saddr
->host
= g_strdup(uaddr
);
231 saddr
->port
= g_strdup_printf("%d",
232 inet_getport(e
) - port_offset
);
233 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
234 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
241 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
242 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
244 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
245 ((rc) == -EINPROGRESS)
248 /* Struct to store connect state for non blocking connect */
249 typedef struct ConnectState
{
251 struct addrinfo
*addr_list
;
252 struct addrinfo
*current_addr
;
253 NonBlockingConnectHandler
*callback
;
257 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
258 ConnectState
*connect_state
, Error
**errp
);
260 static void wait_for_connect(void *opaque
)
262 ConnectState
*s
= opaque
;
264 socklen_t valsize
= sizeof(val
);
268 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
271 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
272 } while (rc
== -1 && errno
== EINTR
);
274 /* update rc to contain error */
282 error_setg_errno(&err
, errno
, "Error connecting to socket");
287 /* try to connect to the next address on the list */
288 if (s
->current_addr
) {
289 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
290 s
->current_addr
= s
->current_addr
->ai_next
;
291 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
295 error_setg_errno(&err
, errno
, "Unable to start socket connect");
297 /* connect in progress */
303 freeaddrinfo(s
->addr_list
);
307 s
->callback(s
->fd
, err
, s
->opaque
);
314 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
315 ConnectState
*connect_state
, Error
**errp
)
319 *in_progress
= false;
321 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
323 error_setg_errno(errp
, errno
, "Failed to create socket");
326 socket_set_fast_reuse(sock
);
327 if (connect_state
!= NULL
) {
328 qemu_set_nonblock(sock
);
330 /* connect to peer */
333 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
336 } while (rc
== -EINTR
);
338 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
339 connect_state
->fd
= sock
;
340 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
343 error_setg_errno(errp
, errno
, "Failed to connect socket");
350 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
353 struct addrinfo ai
, *res
;
357 memset(&ai
, 0, sizeof(ai
));
359 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
360 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
361 ai
.ai_socktype
= SOCK_STREAM
;
364 error_propagate(errp
, err
);
368 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
369 error_setg(errp
, "host and/or port not specified");
374 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
376 error_setg(errp
, "address resolution failed for %s:%s: %s",
377 saddr
->host
, saddr
->port
, gai_strerror(rc
));
384 * Create a socket and connect it to an address.
386 * @saddr: Inet socket address specification
387 * @errp: set on error
388 * @callback: callback function for non-blocking connect
389 * @opaque: opaque for callback function
391 * Returns: -1 on error, file descriptor on success.
393 * If @callback is non-null, the connect is non-blocking. If this
394 * function succeeds, callback will be called when the connection
395 * completes, with the file descriptor on success, or -1 on error.
397 static int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
,
398 NonBlockingConnectHandler
*callback
, void *opaque
)
400 Error
*local_err
= NULL
;
401 struct addrinfo
*res
, *e
;
404 ConnectState
*connect_state
= NULL
;
406 res
= inet_parse_connect_saddr(saddr
, errp
);
411 if (callback
!= NULL
) {
412 connect_state
= g_malloc0(sizeof(*connect_state
));
413 connect_state
->addr_list
= res
;
414 connect_state
->callback
= callback
;
415 connect_state
->opaque
= opaque
;
418 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
419 error_free(local_err
);
421 if (connect_state
!= NULL
) {
422 connect_state
->current_addr
= e
;
424 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
431 error_propagate(errp
, local_err
);
432 } else if (in_progress
) {
433 /* wait_for_connect() will do the rest */
437 callback(sock
, NULL
, opaque
);
440 g_free(connect_state
);
445 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
446 InetSocketAddress
*sladdr
,
449 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
455 /* lookup peer addr */
456 memset(&ai
,0, sizeof(ai
));
457 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
458 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
459 ai
.ai_socktype
= SOCK_DGRAM
;
462 error_propagate(errp
, err
);
468 if (addr
== NULL
|| strlen(addr
) == 0) {
471 if (port
== NULL
|| strlen(port
) == 0) {
472 error_setg(errp
, "remote port not specified");
476 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &peer
))) {
477 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
482 /* lookup local addr */
483 memset(&ai
,0, sizeof(ai
));
484 ai
.ai_flags
= AI_PASSIVE
;
485 ai
.ai_family
= peer
->ai_family
;
486 ai
.ai_socktype
= SOCK_DGRAM
;
491 if (addr
== NULL
|| strlen(addr
) == 0) {
494 if (!port
|| strlen(port
) == 0) {
502 if (0 != (rc
= getaddrinfo(addr
, port
, &ai
, &local
))) {
503 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
509 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
511 error_setg_errno(errp
, errno
, "Failed to create socket");
514 socket_set_fast_reuse(sock
);
517 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
518 error_setg_errno(errp
, errno
, "Failed to bind socket");
522 /* connect to peer */
523 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
524 error_setg_errno(errp
, errno
, "Failed to connect socket");
542 /* compatibility wrapper */
543 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
545 InetSocketAddress
*addr
;
546 const char *optstr
, *h
;
552 addr
= g_new0(InetSocketAddress
, 1);
558 if (1 != sscanf(str
, ":%32[^,]%n", port
, &pos
)) {
559 error_setg(errp
, "error parsing port in address '%s'", str
);
562 } else if (str
[0] == '[') {
564 if (2 != sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
)) {
565 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
568 addr
->ipv6
= addr
->has_ipv6
= true;
570 /* hostname or IPv4 addr */
571 if (2 != sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
)) {
572 error_setg(errp
, "error parsing address '%s'", str
);
575 if (host
[strspn(host
, "0123456789.")] == '\0') {
576 addr
->ipv4
= addr
->has_ipv4
= true;
580 addr
->host
= g_strdup(host
);
581 addr
->port
= g_strdup(port
);
585 h
= strstr(optstr
, ",to=");
588 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
589 (h
[pos
] != '\0' && h
[pos
] != ',')) {
590 error_setg(errp
, "error parsing to= argument");
596 if (strstr(optstr
, ",ipv4")) {
597 addr
->ipv4
= addr
->has_ipv4
= true;
599 if (strstr(optstr
, ",ipv6")) {
600 addr
->ipv6
= addr
->has_ipv6
= true;
605 qapi_free_InetSocketAddress(addr
);
609 int inet_listen(const char *str
, char *ostr
, int olen
,
610 int socktype
, int port_offset
, Error
**errp
)
614 InetSocketAddress
*addr
;
616 addr
= inet_parse(str
, errp
);
618 sock
= inet_listen_saddr(addr
, port_offset
, true, errp
);
619 if (sock
!= -1 && ostr
) {
620 optstr
= strchr(str
, ',');
622 snprintf(ostr
, olen
, "[%s]:%s%s",
625 optstr
? optstr
: "");
627 snprintf(ostr
, olen
, "%s:%s%s",
630 optstr
? optstr
: "");
633 qapi_free_InetSocketAddress(addr
);
639 * Create a blocking socket and connect it to an address.
641 * @str: address string
642 * @errp: set in case of an error
644 * Returns -1 in case of error, file descriptor on success
646 int inet_connect(const char *str
, Error
**errp
)
649 InetSocketAddress
*addr
;
651 addr
= inet_parse(str
, errp
);
653 sock
= inet_connect_saddr(addr
, errp
, NULL
, NULL
);
654 qapi_free_InetSocketAddress(addr
);
660 * Create a non-blocking socket and connect it to an address.
661 * Calls the callback function with fd in case of success or -1 in case of
664 * @str: address string
665 * @callback: callback function that is called when connect completes,
667 * @opaque: opaque for callback function
668 * @errp: set in case of an error
670 * Returns: -1 on immediate error, file descriptor on success.
672 int inet_nonblocking_connect(const char *str
,
673 NonBlockingConnectHandler
*callback
,
674 void *opaque
, Error
**errp
)
677 InetSocketAddress
*addr
;
679 g_assert(callback
!= NULL
);
681 addr
= inet_parse(str
, errp
);
683 sock
= inet_connect_saddr(addr
, errp
, callback
, opaque
);
684 qapi_free_InetSocketAddress(addr
);
691 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
695 struct sockaddr_un un
;
698 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
700 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
704 memset(&un
, 0, sizeof(un
));
705 un
.sun_family
= AF_UNIX
;
706 if (saddr
->path
&& strlen(saddr
->path
)) {
707 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
709 const char *tmpdir
= getenv("TMPDIR");
710 tmpdir
= tmpdir
? tmpdir
: "/tmp";
711 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
712 tmpdir
) >= sizeof(un
.sun_path
)) {
713 error_setg_errno(errp
, errno
,
714 "TMPDIR environment variable (%s) too large", tmpdir
);
719 * This dummy fd usage silences the mktemp() unsecure warning.
720 * Using mkstemp() doesn't make things more secure here
721 * though. bind() complains about existing files, so we have
722 * to unlink first and thus re-open the race window. The
723 * worst case possible is bind() failing, i.e. a DoS attack.
725 fd
= mkstemp(un
.sun_path
);
727 error_setg_errno(errp
, errno
,
728 "Failed to make a temporary socket name in %s", tmpdir
);
734 saddr
->path
= g_strdup(un
.sun_path
);
738 if (unlink(un
.sun_path
) < 0 && errno
!= ENOENT
) {
739 error_setg_errno(errp
, errno
,
740 "Failed to unlink socket %s", un
.sun_path
);
743 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
744 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
747 if (listen(sock
, 1) < 0) {
748 error_setg_errno(errp
, errno
, "Failed to listen on socket");
759 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
760 NonBlockingConnectHandler
*callback
, void *opaque
)
762 struct sockaddr_un un
;
763 ConnectState
*connect_state
= NULL
;
766 if (saddr
->path
== NULL
) {
767 error_setg(errp
, "unix connect: no path specified");
771 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
773 error_setg_errno(errp
, errno
, "Failed to create socket");
776 if (callback
!= NULL
) {
777 connect_state
= g_malloc0(sizeof(*connect_state
));
778 connect_state
->callback
= callback
;
779 connect_state
->opaque
= opaque
;
780 qemu_set_nonblock(sock
);
783 memset(&un
, 0, sizeof(un
));
784 un
.sun_family
= AF_UNIX
;
785 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
787 /* connect to peer */
790 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
793 } while (rc
== -EINTR
);
795 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
796 connect_state
->fd
= sock
;
797 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
799 } else if (rc
>= 0) {
800 /* non blocking socket immediate success, call callback */
801 if (callback
!= NULL
) {
802 callback(sock
, NULL
, opaque
);
807 error_setg_errno(errp
, -rc
, "Failed to connect socket");
812 g_free(connect_state
);
818 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
822 error_setg(errp
, "unix sockets are not available on windows");
827 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
828 NonBlockingConnectHandler
*callback
, void *opaque
)
830 error_setg(errp
, "unix sockets are not available on windows");
836 /* compatibility wrapper */
837 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
841 UnixSocketAddress
*saddr
;
843 saddr
= g_new0(UnixSocketAddress
, 1);
845 optstr
= strchr(str
, ',');
849 path
= g_malloc(len
+1);
850 snprintf(path
, len
+1, "%.*s", len
, str
);
854 saddr
->path
= g_strdup(str
);
857 sock
= unix_listen_saddr(saddr
, true, errp
);
859 if (sock
!= -1 && ostr
)
860 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
861 qapi_free_UnixSocketAddress(saddr
);
865 int unix_connect(const char *path
, Error
**errp
)
867 UnixSocketAddress
*saddr
;
870 saddr
= g_new0(UnixSocketAddress
, 1);
871 saddr
->path
= g_strdup(path
);
872 sock
= unix_connect_saddr(saddr
, errp
, NULL
, NULL
);
873 qapi_free_UnixSocketAddress(saddr
);
878 int unix_nonblocking_connect(const char *path
,
879 NonBlockingConnectHandler
*callback
,
880 void *opaque
, Error
**errp
)
882 UnixSocketAddress
*saddr
;
885 g_assert(callback
!= NULL
);
887 saddr
= g_new0(UnixSocketAddress
, 1);
888 saddr
->path
= g_strdup(path
);
889 sock
= unix_connect_saddr(saddr
, errp
, callback
, opaque
);
890 qapi_free_UnixSocketAddress(saddr
);
894 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
898 addr
= g_new0(SocketAddress
, 1);
899 if (strstart(str
, "unix:", NULL
)) {
900 if (str
[5] == '\0') {
901 error_setg(errp
, "invalid Unix socket address");
904 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
905 addr
->u
.q_unix
.data
= g_new(UnixSocketAddress
, 1);
906 addr
->u
.q_unix
.data
->path
= g_strdup(str
+ 5);
908 } else if (strstart(str
, "fd:", NULL
)) {
909 if (str
[3] == '\0') {
910 error_setg(errp
, "invalid file descriptor address");
913 addr
->type
= SOCKET_ADDRESS_KIND_FD
;
914 addr
->u
.fd
.data
= g_new(String
, 1);
915 addr
->u
.fd
.data
->str
= g_strdup(str
+ 3);
918 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
919 addr
->u
.inet
.data
= inet_parse(str
, errp
);
920 if (addr
->u
.inet
.data
== NULL
) {
927 qapi_free_SocketAddress(addr
);
931 int socket_connect(SocketAddress
*addr
, Error
**errp
,
932 NonBlockingConnectHandler
*callback
, void *opaque
)
936 switch (addr
->type
) {
937 case SOCKET_ADDRESS_KIND_INET
:
938 fd
= inet_connect_saddr(addr
->u
.inet
.data
, errp
, callback
, opaque
);
941 case SOCKET_ADDRESS_KIND_UNIX
:
942 fd
= unix_connect_saddr(addr
->u
.q_unix
.data
, errp
, callback
, opaque
);
945 case SOCKET_ADDRESS_KIND_FD
:
946 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
947 if (fd
>= 0 && callback
) {
948 qemu_set_nonblock(fd
);
949 callback(fd
, NULL
, opaque
);
959 int socket_listen(SocketAddress
*addr
, Error
**errp
)
963 switch (addr
->type
) {
964 case SOCKET_ADDRESS_KIND_INET
:
965 fd
= inet_listen_saddr(addr
->u
.inet
.data
, 0, false, errp
);
968 case SOCKET_ADDRESS_KIND_UNIX
:
969 fd
= unix_listen_saddr(addr
->u
.q_unix
.data
, false, errp
);
972 case SOCKET_ADDRESS_KIND_FD
:
973 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
982 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
986 switch (remote
->type
) {
987 case SOCKET_ADDRESS_KIND_INET
:
988 fd
= inet_dgram_saddr(remote
->u
.inet
.data
,
989 local
? local
->u
.inet
.data
: NULL
, errp
);
993 error_setg(errp
, "socket type unsupported for datagram");
1000 static SocketAddress
*
1001 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
1005 char host
[NI_MAXHOST
];
1006 char serv
[NI_MAXSERV
];
1007 SocketAddress
*addr
;
1008 InetSocketAddress
*inet
;
1011 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
1014 NI_NUMERICHOST
| NI_NUMERICSERV
);
1016 error_setg(errp
, "Cannot format numeric socket address: %s",
1021 addr
= g_new0(SocketAddress
, 1);
1022 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
1023 inet
= addr
->u
.inet
.data
= g_new0(InetSocketAddress
, 1);
1024 inet
->host
= g_strdup(host
);
1025 inet
->port
= g_strdup(serv
);
1026 if (sa
->ss_family
== AF_INET
) {
1027 inet
->has_ipv4
= inet
->ipv4
= true;
1029 inet
->has_ipv6
= inet
->ipv6
= true;
1037 static SocketAddress
*
1038 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1042 SocketAddress
*addr
;
1043 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1045 addr
= g_new0(SocketAddress
, 1);
1046 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1047 addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1048 if (su
->sun_path
[0]) {
1049 addr
->u
.q_unix
.data
->path
= g_strndup(su
->sun_path
,
1050 sizeof(su
->sun_path
));
1058 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1062 switch (sa
->ss_family
) {
1065 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1069 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1073 error_setg(errp
, "socket family %d unsupported",
1081 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1083 struct sockaddr_storage ss
;
1084 socklen_t sslen
= sizeof(ss
);
1086 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1087 error_setg_errno(errp
, errno
, "%s",
1088 "Unable to query local socket address");
1092 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1096 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1098 struct sockaddr_storage ss
;
1099 socklen_t sslen
= sizeof(ss
);
1101 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1102 error_setg_errno(errp
, errno
, "%s",
1103 "Unable to query remote socket address");
1107 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1111 void qapi_copy_SocketAddress(SocketAddress
**p_dest
,
1114 QmpOutputVisitor
*qov
;
1115 QmpInputVisitor
*qiv
;
1121 qov
= qmp_output_visitor_new();
1122 ov
= qmp_output_get_visitor(qov
);
1123 visit_type_SocketAddress(ov
, NULL
, &src
, &error_abort
);
1124 obj
= qmp_output_get_qobject(qov
);
1125 qmp_output_visitor_cleanup(qov
);
1130 qiv
= qmp_input_visitor_new(obj
);
1131 iv
= qmp_input_get_visitor(qiv
);
1132 visit_type_SocketAddress(iv
, NULL
, p_dest
, &error_abort
);
1133 qmp_input_visitor_cleanup(qiv
);
1134 qobject_decref(obj
);