2 * Helper functions for tests using sockets
4 * Copyright 2015-2018 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 or
9 * (at your option) version 3 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "qemu/sockets.h"
23 #include "socket-helpers.h"
26 # define AI_ADDRCONFIG 0
28 #ifndef EAI_ADDRFAMILY
29 # define EAI_ADDRFAMILY 0
33 * @hostname: a DNS name or numeric IP address
35 * Check whether it is possible to bind & connect to ports
36 * on the DNS name or IP address @hostname. If an IP address
37 * is used, it must not be a wildcard address.
39 * Returns 0 on success, -1 on error with errno set
41 static int socket_can_bind_connect(const char *hostname
, int family
)
43 int lfd
= -1, cfd
= -1, afd
= -1;
44 struct addrinfo ai
, *res
= NULL
;
45 struct sockaddr_storage ss
;
46 socklen_t sslen
= sizeof(ss
);
48 socklen_t soerrlen
= sizeof(soerr
);
49 bool check_soerr
= false;
53 memset(&ai
, 0, sizeof(ai
));
54 ai
.ai_flags
= AI_CANONNAME
| AI_ADDRCONFIG
;
55 ai
.ai_family
= family
;
56 ai
.ai_socktype
= SOCK_STREAM
;
59 rc
= getaddrinfo(hostname
, NULL
, &ai
, &res
);
61 if (rc
== EAI_ADDRFAMILY
|| rc
== EAI_FAMILY
|| rc
== EAI_NONAME
) {
62 errno
= EADDRNOTAVAIL
;
69 lfd
= qemu_socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
74 cfd
= qemu_socket(res
->ai_family
, res
->ai_socktype
, res
->ai_protocol
);
79 if (bind(lfd
, res
->ai_addr
, res
->ai_addrlen
) < 0) {
83 if (listen(lfd
, 1) < 0) {
87 if (getsockname(lfd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
91 qemu_socket_set_nonblock(cfd
);
92 if (connect(cfd
, (struct sockaddr
*)&ss
, sslen
) < 0) {
93 if (errno
== EINPROGRESS
) {
101 afd
= accept(lfd
, (struct sockaddr
*)&ss
, &sslen
);
107 if (getsockopt(cfd
, SOL_SOCKET
, SO_ERROR
, &soerr
, &soerrlen
) < 0) {
135 int socket_check_protocol_support(bool *has_ipv4
, bool *has_ipv6
)
137 *has_ipv4
= *has_ipv6
= false;
139 if (socket_can_bind_connect("127.0.0.1", PF_INET
) < 0) {
140 if (errno
!= EADDRNOTAVAIL
) {
147 if (socket_can_bind_connect("::1", PF_INET6
) < 0) {
148 if (errno
!= EADDRNOTAVAIL
) {
158 void socket_check_afunix_support(bool *has_afunix
)
162 fd
= socket(PF_UNIX
, SOCK_STREAM
, 0);
166 *has_afunix
= (fd
!= (int)INVALID_SOCKET
);
168 *has_afunix
= (fd
>= 0);