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"
27 #include "qemu/cutils.h"
30 # define AI_ADDRCONFIG 0
34 # define AI_V4MAPPED 0
38 static int inet_getport(struct addrinfo
*e
)
40 struct sockaddr_in
*i4
;
41 struct sockaddr_in6
*i6
;
43 switch (e
->ai_family
) {
45 i6
= (void*)e
->ai_addr
;
46 return ntohs(i6
->sin6_port
);
48 i4
= (void*)e
->ai_addr
;
49 return ntohs(i4
->sin_port
);
55 static void inet_setport(struct addrinfo
*e
, int port
)
57 struct sockaddr_in
*i4
;
58 struct sockaddr_in6
*i6
;
60 switch (e
->ai_family
) {
62 i6
= (void*)e
->ai_addr
;
63 i6
->sin6_port
= htons(port
);
66 i4
= (void*)e
->ai_addr
;
67 i4
->sin_port
= htons(port
);
72 NetworkAddressFamily
inet_netfamily(int family
)
75 case PF_INET6
: return NETWORK_ADDRESS_FAMILY_IPV6
;
76 case PF_INET
: return NETWORK_ADDRESS_FAMILY_IPV4
;
77 case PF_UNIX
: return NETWORK_ADDRESS_FAMILY_UNIX
;
79 return NETWORK_ADDRESS_FAMILY_UNKNOWN
;
83 * Matrix we're trying to apply
96 * NB, this matrix is only about getting the necessary results
97 * from getaddrinfo(). Some of the cases require further work
98 * after reading results from getaddrinfo in order to fully
99 * apply the logic the end user wants. eg with the last case
100 * ipv4=t + ipv6=t + PF_INET6, getaddrinfo alone can only
101 * guarantee the ipv6=t part of the request - we need more
102 * checks to provide ipv4=t part of the guarantee. This is
103 * outside scope of this method and not currently handled by
106 static int inet_ai_family_from_address(InetSocketAddress
*addr
,
109 if (addr
->has_ipv6
&& addr
->has_ipv4
&&
110 !addr
->ipv6
&& !addr
->ipv4
) {
111 error_setg(errp
, "Cannot disable IPv4 and IPv6 at same time");
114 if ((addr
->has_ipv6
&& addr
->ipv6
) || (addr
->has_ipv4
&& !addr
->ipv4
)) {
117 if ((addr
->has_ipv4
&& addr
->ipv4
) || (addr
->has_ipv6
&& !addr
->ipv6
)) {
123 static int inet_listen_saddr(InetSocketAddress
*saddr
,
128 struct addrinfo ai
,*res
,*e
;
130 char uaddr
[INET6_ADDRSTRLEN
+1];
132 int slisten
, rc
, port_min
, port_max
, p
;
135 memset(&ai
,0, sizeof(ai
));
136 ai
.ai_flags
= AI_PASSIVE
;
137 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
138 ai
.ai_socktype
= SOCK_STREAM
;
141 error_propagate(errp
, err
);
145 if (saddr
->host
== NULL
) {
146 error_setg(errp
, "host not specified");
149 if (saddr
->port
!= NULL
) {
150 pstrcpy(port
, sizeof(port
), saddr
->port
);
157 unsigned long long baseport
;
158 if (strlen(port
) == 0) {
159 error_setg(errp
, "port not specified");
162 if (parse_uint_full(port
, &baseport
, 10) < 0) {
163 error_setg(errp
, "can't convert to a number: %s", port
);
166 if (baseport
> 65535 ||
167 baseport
+ port_offset
> 65535) {
168 error_setg(errp
, "port %s out of range", port
);
171 snprintf(port
, sizeof(port
), "%d", (int)baseport
+ port_offset
);
173 rc
= getaddrinfo(strlen(saddr
->host
) ? saddr
->host
: NULL
,
174 strlen(port
) ? port
: NULL
, &ai
, &res
);
176 error_setg(errp
, "address resolution failed for %s:%s: %s",
177 saddr
->host
, port
, gai_strerror(rc
));
181 /* create socket + bind */
182 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
183 getnameinfo((struct sockaddr
*)e
->ai_addr
,e
->ai_addrlen
,
184 uaddr
,INET6_ADDRSTRLEN
,uport
,32,
185 NI_NUMERICHOST
| NI_NUMERICSERV
);
186 slisten
= qemu_socket(e
->ai_family
, e
->ai_socktype
, e
->ai_protocol
);
189 error_setg_errno(errp
, errno
, "Failed to create socket");
194 socket_set_fast_reuse(slisten
);
196 if (e
->ai_family
== PF_INET6
) {
197 /* listen on both ipv4 and ipv6 */
199 qemu_setsockopt(slisten
, IPPROTO_IPV6
, IPV6_V6ONLY
, &off
,
204 port_min
= inet_getport(e
);
205 port_max
= saddr
->has_to
? saddr
->to
+ port_offset
: port_min
;
206 for (p
= port_min
; p
<= port_max
; p
++) {
208 if (bind(slisten
, e
->ai_addr
, e
->ai_addrlen
) == 0) {
213 error_setg_errno(errp
, errno
, "Failed to bind socket");
217 closesocket(slisten
);
223 if (listen(slisten
,1) != 0) {
224 error_setg_errno(errp
, errno
, "Failed to listen on socket");
225 closesocket(slisten
);
231 saddr
->host
= g_strdup(uaddr
);
233 saddr
->port
= g_strdup_printf("%d",
234 inet_getport(e
) - port_offset
);
235 saddr
->has_ipv6
= saddr
->ipv6
= e
->ai_family
== PF_INET6
;
236 saddr
->has_ipv4
= saddr
->ipv4
= e
->ai_family
!= PF_INET6
;
243 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
244 ((rc) == -EINPROGRESS || (rc) == -EWOULDBLOCK || (rc) == -WSAEALREADY)
246 #define QEMU_SOCKET_RC_INPROGRESS(rc) \
247 ((rc) == -EINPROGRESS)
250 /* Struct to store connect state for non blocking connect */
251 typedef struct ConnectState
{
253 struct addrinfo
*addr_list
;
254 struct addrinfo
*current_addr
;
255 NonBlockingConnectHandler
*callback
;
259 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
260 ConnectState
*connect_state
, Error
**errp
);
262 static void wait_for_connect(void *opaque
)
264 ConnectState
*s
= opaque
;
266 socklen_t valsize
= sizeof(val
);
270 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
273 rc
= qemu_getsockopt(s
->fd
, SOL_SOCKET
, SO_ERROR
, &val
, &valsize
);
274 } while (rc
== -1 && errno
== EINTR
);
276 /* update rc to contain error */
284 error_setg_errno(&err
, errno
, "Error connecting to socket");
289 /* try to connect to the next address on the list */
290 if (s
->current_addr
) {
291 while (s
->current_addr
->ai_next
!= NULL
&& s
->fd
< 0) {
292 s
->current_addr
= s
->current_addr
->ai_next
;
293 s
->fd
= inet_connect_addr(s
->current_addr
, &in_progress
, s
, NULL
);
297 error_setg_errno(&err
, errno
, "Unable to start socket connect");
299 /* connect in progress */
305 freeaddrinfo(s
->addr_list
);
309 s
->callback(s
->fd
, err
, s
->opaque
);
316 static int inet_connect_addr(struct addrinfo
*addr
, bool *in_progress
,
317 ConnectState
*connect_state
, Error
**errp
)
321 *in_progress
= false;
323 sock
= qemu_socket(addr
->ai_family
, addr
->ai_socktype
, addr
->ai_protocol
);
325 error_setg_errno(errp
, errno
, "Failed to create socket");
328 socket_set_fast_reuse(sock
);
329 if (connect_state
!= NULL
) {
330 qemu_set_nonblock(sock
);
332 /* connect to peer */
335 if (connect(sock
, addr
->ai_addr
, addr
->ai_addrlen
) < 0) {
338 } while (rc
== -EINTR
);
340 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
341 connect_state
->fd
= sock
;
342 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
345 error_setg_errno(errp
, errno
, "Failed to connect socket");
352 static struct addrinfo
*inet_parse_connect_saddr(InetSocketAddress
*saddr
,
355 struct addrinfo ai
, *res
;
358 static int useV4Mapped
= 1;
360 memset(&ai
, 0, sizeof(ai
));
362 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
363 if (atomic_read(&useV4Mapped
)) {
364 ai
.ai_flags
|= AI_V4MAPPED
;
366 ai
.ai_family
= inet_ai_family_from_address(saddr
, &err
);
367 ai
.ai_socktype
= SOCK_STREAM
;
370 error_propagate(errp
, err
);
374 if (saddr
->host
== NULL
|| saddr
->port
== NULL
) {
375 error_setg(errp
, "host and/or port not specified");
380 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
382 /* At least FreeBSD and OS-X 10.6 declare AI_V4MAPPED but
383 * then don't implement it in their getaddrinfo(). Detect
384 * this and retry without the flag since that's preferrable
387 if (rc
== EAI_BADFLAGS
&&
388 (ai
.ai_flags
& AI_V4MAPPED
)) {
389 atomic_set(&useV4Mapped
, 0);
390 ai
.ai_flags
&= ~AI_V4MAPPED
;
391 rc
= getaddrinfo(saddr
->host
, saddr
->port
, &ai
, &res
);
394 error_setg(errp
, "address resolution failed for %s:%s: %s",
395 saddr
->host
, saddr
->port
, gai_strerror(rc
));
402 * Create a socket and connect it to an address.
404 * @saddr: Inet socket address specification
405 * @errp: set on error
406 * @callback: callback function for non-blocking connect
407 * @opaque: opaque for callback function
409 * Returns: -1 on error, file descriptor on success.
411 * If @callback is non-null, the connect is non-blocking. If this
412 * function succeeds, callback will be called when the connection
413 * completes, with the file descriptor on success, or -1 on error.
415 static int inet_connect_saddr(InetSocketAddress
*saddr
, Error
**errp
,
416 NonBlockingConnectHandler
*callback
, void *opaque
)
418 Error
*local_err
= NULL
;
419 struct addrinfo
*res
, *e
;
422 ConnectState
*connect_state
= NULL
;
424 res
= inet_parse_connect_saddr(saddr
, errp
);
429 if (callback
!= NULL
) {
430 connect_state
= g_malloc0(sizeof(*connect_state
));
431 connect_state
->addr_list
= res
;
432 connect_state
->callback
= callback
;
433 connect_state
->opaque
= opaque
;
436 for (e
= res
; e
!= NULL
; e
= e
->ai_next
) {
437 error_free(local_err
);
439 if (connect_state
!= NULL
) {
440 connect_state
->current_addr
= e
;
442 sock
= inet_connect_addr(e
, &in_progress
, connect_state
, &local_err
);
449 error_propagate(errp
, local_err
);
450 } else if (in_progress
) {
451 /* wait_for_connect() will do the rest */
455 callback(sock
, NULL
, opaque
);
458 g_free(connect_state
);
463 static int inet_dgram_saddr(InetSocketAddress
*sraddr
,
464 InetSocketAddress
*sladdr
,
467 struct addrinfo ai
, *peer
= NULL
, *local
= NULL
;
473 /* lookup peer addr */
474 memset(&ai
,0, sizeof(ai
));
475 ai
.ai_flags
= AI_CANONNAME
| AI_V4MAPPED
| AI_ADDRCONFIG
;
476 ai
.ai_family
= inet_ai_family_from_address(sraddr
, &err
);
477 ai
.ai_socktype
= SOCK_DGRAM
;
480 error_propagate(errp
, err
);
486 if (addr
== NULL
|| strlen(addr
) == 0) {
489 if (port
== NULL
|| strlen(port
) == 0) {
490 error_setg(errp
, "remote port not specified");
494 if ((rc
= getaddrinfo(addr
, port
, &ai
, &peer
)) != 0) {
495 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
500 /* lookup local addr */
501 memset(&ai
,0, sizeof(ai
));
502 ai
.ai_flags
= AI_PASSIVE
;
503 ai
.ai_family
= peer
->ai_family
;
504 ai
.ai_socktype
= SOCK_DGRAM
;
509 if (addr
== NULL
|| strlen(addr
) == 0) {
512 if (!port
|| strlen(port
) == 0) {
520 if ((rc
= getaddrinfo(addr
, port
, &ai
, &local
)) != 0) {
521 error_setg(errp
, "address resolution failed for %s:%s: %s", addr
, port
,
527 sock
= qemu_socket(peer
->ai_family
, peer
->ai_socktype
, peer
->ai_protocol
);
529 error_setg_errno(errp
, errno
, "Failed to create socket");
532 socket_set_fast_reuse(sock
);
535 if (bind(sock
, local
->ai_addr
, local
->ai_addrlen
) < 0) {
536 error_setg_errno(errp
, errno
, "Failed to bind socket");
540 /* connect to peer */
541 if (connect(sock
,peer
->ai_addr
,peer
->ai_addrlen
) < 0) {
542 error_setg_errno(errp
, errno
, "Failed to connect socket");
564 /* compatibility wrapper */
565 InetSocketAddress
*inet_parse(const char *str
, Error
**errp
)
567 InetSocketAddress
*addr
;
568 const char *optstr
, *h
;
574 addr
= g_new0(InetSocketAddress
, 1);
580 if (sscanf(str
, ":%32[^,]%n", port
, &pos
) != 1) {
581 error_setg(errp
, "error parsing port in address '%s'", str
);
584 } else if (str
[0] == '[') {
586 if (sscanf(str
, "[%64[^]]]:%32[^,]%n", host
, port
, &pos
) != 2) {
587 error_setg(errp
, "error parsing IPv6 address '%s'", str
);
590 addr
->ipv6
= addr
->has_ipv6
= true;
592 /* hostname or IPv4 addr */
593 if (sscanf(str
, "%64[^:]:%32[^,]%n", host
, port
, &pos
) != 2) {
594 error_setg(errp
, "error parsing address '%s'", str
);
597 if (host
[strspn(host
, "0123456789.")] == '\0') {
598 addr
->ipv4
= addr
->has_ipv4
= true;
602 addr
->host
= g_strdup(host
);
603 addr
->port
= g_strdup(port
);
607 h
= strstr(optstr
, ",to=");
610 if (sscanf(h
, "%d%n", &to
, &pos
) != 1 ||
611 (h
[pos
] != '\0' && h
[pos
] != ',')) {
612 error_setg(errp
, "error parsing to= argument");
618 if (strstr(optstr
, ",ipv4")) {
619 addr
->ipv4
= addr
->has_ipv4
= true;
621 if (strstr(optstr
, ",ipv6")) {
622 addr
->ipv6
= addr
->has_ipv6
= true;
627 qapi_free_InetSocketAddress(addr
);
633 * Create a blocking socket and connect it to an address.
635 * @str: address string
636 * @errp: set in case of an error
638 * Returns -1 in case of error, file descriptor on success
640 int inet_connect(const char *str
, Error
**errp
)
643 InetSocketAddress
*addr
;
645 addr
= inet_parse(str
, errp
);
647 sock
= inet_connect_saddr(addr
, errp
, NULL
, NULL
);
648 qapi_free_InetSocketAddress(addr
);
655 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
659 struct sockaddr_un un
;
662 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
664 error_setg_errno(errp
, errno
, "Failed to create Unix socket");
668 memset(&un
, 0, sizeof(un
));
669 un
.sun_family
= AF_UNIX
;
670 if (saddr
->path
&& strlen(saddr
->path
)) {
671 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
673 const char *tmpdir
= getenv("TMPDIR");
674 tmpdir
= tmpdir
? tmpdir
: "/tmp";
675 if (snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s/qemu-socket-XXXXXX",
676 tmpdir
) >= sizeof(un
.sun_path
)) {
677 error_setg_errno(errp
, errno
,
678 "TMPDIR environment variable (%s) too large", tmpdir
);
683 * This dummy fd usage silences the mktemp() unsecure warning.
684 * Using mkstemp() doesn't make things more secure here
685 * though. bind() complains about existing files, so we have
686 * to unlink first and thus re-open the race window. The
687 * worst case possible is bind() failing, i.e. a DoS attack.
689 fd
= mkstemp(un
.sun_path
);
691 error_setg_errno(errp
, errno
,
692 "Failed to make a temporary socket name in %s", tmpdir
);
698 saddr
->path
= g_strdup(un
.sun_path
);
702 if (unlink(un
.sun_path
) < 0 && errno
!= ENOENT
) {
703 error_setg_errno(errp
, errno
,
704 "Failed to unlink socket %s", un
.sun_path
);
707 if (bind(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
708 error_setg_errno(errp
, errno
, "Failed to bind socket to %s", un
.sun_path
);
711 if (listen(sock
, 1) < 0) {
712 error_setg_errno(errp
, errno
, "Failed to listen on socket");
723 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
724 NonBlockingConnectHandler
*callback
, void *opaque
)
726 struct sockaddr_un un
;
727 ConnectState
*connect_state
= NULL
;
730 if (saddr
->path
== NULL
) {
731 error_setg(errp
, "unix connect: no path specified");
735 sock
= qemu_socket(PF_UNIX
, SOCK_STREAM
, 0);
737 error_setg_errno(errp
, errno
, "Failed to create socket");
740 if (callback
!= NULL
) {
741 connect_state
= g_malloc0(sizeof(*connect_state
));
742 connect_state
->callback
= callback
;
743 connect_state
->opaque
= opaque
;
744 qemu_set_nonblock(sock
);
747 memset(&un
, 0, sizeof(un
));
748 un
.sun_family
= AF_UNIX
;
749 snprintf(un
.sun_path
, sizeof(un
.sun_path
), "%s", saddr
->path
);
751 /* connect to peer */
754 if (connect(sock
, (struct sockaddr
*) &un
, sizeof(un
)) < 0) {
757 } while (rc
== -EINTR
);
759 if (connect_state
!= NULL
&& QEMU_SOCKET_RC_INPROGRESS(rc
)) {
760 connect_state
->fd
= sock
;
761 qemu_set_fd_handler(sock
, NULL
, wait_for_connect
, connect_state
);
763 } else if (rc
>= 0) {
764 /* non blocking socket immediate success, call callback */
765 if (callback
!= NULL
) {
766 callback(sock
, NULL
, opaque
);
771 error_setg_errno(errp
, -rc
, "Failed to connect socket");
776 g_free(connect_state
);
782 static int unix_listen_saddr(UnixSocketAddress
*saddr
,
786 error_setg(errp
, "unix sockets are not available on windows");
791 static int unix_connect_saddr(UnixSocketAddress
*saddr
, Error
**errp
,
792 NonBlockingConnectHandler
*callback
, void *opaque
)
794 error_setg(errp
, "unix sockets are not available on windows");
800 /* compatibility wrapper */
801 int unix_listen(const char *str
, char *ostr
, int olen
, Error
**errp
)
805 UnixSocketAddress
*saddr
;
807 saddr
= g_new0(UnixSocketAddress
, 1);
809 optstr
= strchr(str
, ',');
813 path
= g_malloc(len
+1);
814 snprintf(path
, len
+1, "%.*s", len
, str
);
818 saddr
->path
= g_strdup(str
);
821 sock
= unix_listen_saddr(saddr
, true, errp
);
823 if (sock
!= -1 && ostr
) {
824 snprintf(ostr
, olen
, "%s%s", saddr
->path
, optstr
? optstr
: "");
827 qapi_free_UnixSocketAddress(saddr
);
831 int unix_connect(const char *path
, Error
**errp
)
833 UnixSocketAddress
*saddr
;
836 saddr
= g_new0(UnixSocketAddress
, 1);
837 saddr
->path
= g_strdup(path
);
838 sock
= unix_connect_saddr(saddr
, errp
, NULL
, NULL
);
839 qapi_free_UnixSocketAddress(saddr
);
844 SocketAddress
*socket_parse(const char *str
, Error
**errp
)
848 addr
= g_new0(SocketAddress
, 1);
849 if (strstart(str
, "unix:", NULL
)) {
850 if (str
[5] == '\0') {
851 error_setg(errp
, "invalid Unix socket address");
854 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
855 addr
->u
.q_unix
.data
= g_new(UnixSocketAddress
, 1);
856 addr
->u
.q_unix
.data
->path
= g_strdup(str
+ 5);
858 } else if (strstart(str
, "fd:", NULL
)) {
859 if (str
[3] == '\0') {
860 error_setg(errp
, "invalid file descriptor address");
863 addr
->type
= SOCKET_ADDRESS_KIND_FD
;
864 addr
->u
.fd
.data
= g_new(String
, 1);
865 addr
->u
.fd
.data
->str
= g_strdup(str
+ 3);
868 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
869 addr
->u
.inet
.data
= inet_parse(str
, errp
);
870 if (addr
->u
.inet
.data
== NULL
) {
877 qapi_free_SocketAddress(addr
);
881 int socket_connect(SocketAddress
*addr
, Error
**errp
,
882 NonBlockingConnectHandler
*callback
, void *opaque
)
886 switch (addr
->type
) {
887 case SOCKET_ADDRESS_KIND_INET
:
888 fd
= inet_connect_saddr(addr
->u
.inet
.data
, errp
, callback
, opaque
);
891 case SOCKET_ADDRESS_KIND_UNIX
:
892 fd
= unix_connect_saddr(addr
->u
.q_unix
.data
, errp
, callback
, opaque
);
895 case SOCKET_ADDRESS_KIND_FD
:
896 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
897 if (fd
>= 0 && callback
) {
898 qemu_set_nonblock(fd
);
899 callback(fd
, NULL
, opaque
);
909 int socket_listen(SocketAddress
*addr
, Error
**errp
)
913 switch (addr
->type
) {
914 case SOCKET_ADDRESS_KIND_INET
:
915 fd
= inet_listen_saddr(addr
->u
.inet
.data
, 0, false, errp
);
918 case SOCKET_ADDRESS_KIND_UNIX
:
919 fd
= unix_listen_saddr(addr
->u
.q_unix
.data
, false, errp
);
922 case SOCKET_ADDRESS_KIND_FD
:
923 fd
= monitor_get_fd(cur_mon
, addr
->u
.fd
.data
->str
, errp
);
932 void socket_listen_cleanup(int fd
, Error
**errp
)
936 addr
= socket_local_address(fd
, errp
);
938 if (addr
->type
== SOCKET_ADDRESS_KIND_UNIX
939 && addr
->u
.q_unix
.data
->path
) {
940 if (unlink(addr
->u
.q_unix
.data
->path
) < 0 && errno
!= ENOENT
) {
941 error_setg_errno(errp
, errno
,
942 "Failed to unlink socket %s",
943 addr
->u
.q_unix
.data
->path
);
947 qapi_free_SocketAddress(addr
);
950 int socket_dgram(SocketAddress
*remote
, SocketAddress
*local
, Error
**errp
)
954 switch (remote
->type
) {
955 case SOCKET_ADDRESS_KIND_INET
:
956 fd
= inet_dgram_saddr(remote
->u
.inet
.data
,
957 local
? local
->u
.inet
.data
: NULL
, errp
);
961 error_setg(errp
, "socket type unsupported for datagram");
968 static SocketAddress
*
969 socket_sockaddr_to_address_inet(struct sockaddr_storage
*sa
,
973 char host
[NI_MAXHOST
];
974 char serv
[NI_MAXSERV
];
976 InetSocketAddress
*inet
;
979 ret
= getnameinfo((struct sockaddr
*)sa
, salen
,
982 NI_NUMERICHOST
| NI_NUMERICSERV
);
984 error_setg(errp
, "Cannot format numeric socket address: %s",
989 addr
= g_new0(SocketAddress
, 1);
990 addr
->type
= SOCKET_ADDRESS_KIND_INET
;
991 inet
= addr
->u
.inet
.data
= g_new0(InetSocketAddress
, 1);
992 inet
->host
= g_strdup(host
);
993 inet
->port
= g_strdup(serv
);
994 if (sa
->ss_family
== AF_INET
) {
995 inet
->has_ipv4
= inet
->ipv4
= true;
997 inet
->has_ipv6
= inet
->ipv6
= true;
1005 static SocketAddress
*
1006 socket_sockaddr_to_address_unix(struct sockaddr_storage
*sa
,
1010 SocketAddress
*addr
;
1011 struct sockaddr_un
*su
= (struct sockaddr_un
*)sa
;
1013 addr
= g_new0(SocketAddress
, 1);
1014 addr
->type
= SOCKET_ADDRESS_KIND_UNIX
;
1015 addr
->u
.q_unix
.data
= g_new0(UnixSocketAddress
, 1);
1016 if (su
->sun_path
[0]) {
1017 addr
->u
.q_unix
.data
->path
= g_strndup(su
->sun_path
,
1018 sizeof(su
->sun_path
));
1026 socket_sockaddr_to_address(struct sockaddr_storage
*sa
,
1030 switch (sa
->ss_family
) {
1033 return socket_sockaddr_to_address_inet(sa
, salen
, errp
);
1037 return socket_sockaddr_to_address_unix(sa
, salen
, errp
);
1041 error_setg(errp
, "socket family %d unsupported",
1049 SocketAddress
*socket_local_address(int fd
, Error
**errp
)
1051 struct sockaddr_storage ss
;
1052 socklen_t sslen
= sizeof(ss
);
1054 if (getsockname(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1055 error_setg_errno(errp
, errno
, "%s",
1056 "Unable to query local socket address");
1060 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1064 SocketAddress
*socket_remote_address(int fd
, Error
**errp
)
1066 struct sockaddr_storage ss
;
1067 socklen_t sslen
= sizeof(ss
);
1069 if (getpeername(fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
1070 error_setg_errno(errp
, errno
, "%s",
1071 "Unable to query remote socket address");
1075 return socket_sockaddr_to_address(&ss
, sslen
, errp
);
1078 char *socket_address_to_string(struct SocketAddress
*addr
, Error
**errp
)
1081 InetSocketAddress
*inet
;
1082 char host_port
[INET6_ADDRSTRLEN
+ 5 + 4];
1084 switch (addr
->type
) {
1085 case SOCKET_ADDRESS_KIND_INET
:
1086 inet
= addr
->u
.inet
.data
;
1087 if (strchr(inet
->host
, ':') == NULL
) {
1088 snprintf(host_port
, sizeof(host_port
), "%s:%s", inet
->host
,
1090 buf
= g_strdup(host_port
);
1092 snprintf(host_port
, sizeof(host_port
), "[%s]:%s", inet
->host
,
1094 buf
= g_strdup(host_port
);
1098 case SOCKET_ADDRESS_KIND_UNIX
:
1099 buf
= g_strdup(addr
->u
.q_unix
.data
->path
);
1102 case SOCKET_ADDRESS_KIND_FD
:
1103 buf
= g_strdup(addr
->u
.fd
.data
->str
);
1107 error_setg(errp
, "socket family %d unsupported",