2017-12-22 14:45:24 +03:00
|
|
|
/*
|
|
|
|
* Helper functions for tests using sockets
|
|
|
|
*
|
|
|
|
* Copyright 2015-2018 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 or
|
|
|
|
* (at your option) version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/sockets.h"
|
|
|
|
#include "socket-helpers.h"
|
|
|
|
|
|
|
|
#ifndef AI_ADDRCONFIG
|
|
|
|
# define AI_ADDRCONFIG 0
|
|
|
|
#endif
|
|
|
|
#ifndef EAI_ADDRFAMILY
|
|
|
|
# define EAI_ADDRFAMILY 0
|
|
|
|
#endif
|
|
|
|
|
2019-07-09 17:12:28 +03:00
|
|
|
/*
|
|
|
|
* @hostname: a DNS name or numeric IP address
|
|
|
|
*
|
|
|
|
* Check whether it is possible to bind & connect to ports
|
|
|
|
* on the DNS name or IP address @hostname. If an IP address
|
|
|
|
* is used, it must not be a wildcard address.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error with errno set
|
|
|
|
*/
|
|
|
|
static int socket_can_bind_connect(const char *hostname, int family)
|
2017-12-22 14:45:24 +03:00
|
|
|
{
|
2017-12-22 14:48:49 +03:00
|
|
|
int lfd = -1, cfd = -1, afd = -1;
|
2017-12-22 14:45:24 +03:00
|
|
|
struct addrinfo ai, *res = NULL;
|
2017-12-22 14:48:49 +03:00
|
|
|
struct sockaddr_storage ss;
|
|
|
|
socklen_t sslen = sizeof(ss);
|
|
|
|
int soerr;
|
|
|
|
socklen_t soerrlen = sizeof(soerr);
|
|
|
|
bool check_soerr = false;
|
2017-12-22 14:45:24 +03:00
|
|
|
int rc;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
memset(&ai, 0, sizeof(ai));
|
|
|
|
ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
|
2019-07-09 17:12:28 +03:00
|
|
|
ai.ai_family = family;
|
2017-12-22 14:45:24 +03:00
|
|
|
ai.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
/* lookup */
|
|
|
|
rc = getaddrinfo(hostname, NULL, &ai, &res);
|
|
|
|
if (rc != 0) {
|
2020-09-08 14:48:13 +03:00
|
|
|
if (rc == EAI_ADDRFAMILY || rc == EAI_FAMILY || rc == EAI_NONAME) {
|
2017-12-22 14:45:24 +03:00
|
|
|
errno = EADDRNOTAVAIL;
|
|
|
|
} else {
|
|
|
|
errno = EINVAL;
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:48:49 +03:00
|
|
|
lfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
if (lfd < 0) {
|
2017-12-22 14:45:24 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:48:49 +03:00
|
|
|
cfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
|
|
|
if (cfd < 0) {
|
2017-12-22 14:45:24 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:48:49 +03:00
|
|
|
if (bind(lfd, res->ai_addr, res->ai_addrlen) < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (listen(lfd, 1) < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (getsockname(lfd, (struct sockaddr *)&ss, &sslen) < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-04-25 16:33:47 +03:00
|
|
|
qemu_socket_set_nonblock(cfd);
|
2017-12-22 14:48:49 +03:00
|
|
|
if (connect(cfd, (struct sockaddr *)&ss, sslen) < 0) {
|
|
|
|
if (errno == EINPROGRESS) {
|
|
|
|
check_soerr = true;
|
|
|
|
} else {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sslen = sizeof(ss);
|
|
|
|
afd = accept(lfd, (struct sockaddr *)&ss, &sslen);
|
|
|
|
if (afd < 0) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_soerr) {
|
2022-02-19 00:34:50 +03:00
|
|
|
if (getsockopt(cfd, SOL_SOCKET, SO_ERROR, &soerr, &soerrlen) < 0) {
|
2017-12-22 14:48:49 +03:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (soerr) {
|
|
|
|
errno = soerr;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-22 14:45:24 +03:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2017-12-22 14:48:49 +03:00
|
|
|
if (afd != -1) {
|
|
|
|
close(afd);
|
|
|
|
}
|
|
|
|
if (cfd != -1) {
|
|
|
|
close(cfd);
|
|
|
|
}
|
|
|
|
if (lfd != -1) {
|
|
|
|
close(lfd);
|
2017-12-22 14:45:24 +03:00
|
|
|
}
|
|
|
|
if (res) {
|
|
|
|
freeaddrinfo(res);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6)
|
|
|
|
{
|
|
|
|
*has_ipv4 = *has_ipv6 = false;
|
|
|
|
|
2019-07-09 17:12:28 +03:00
|
|
|
if (socket_can_bind_connect("127.0.0.1", PF_INET) < 0) {
|
2017-12-22 14:45:24 +03:00
|
|
|
if (errno != EADDRNOTAVAIL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*has_ipv4 = true;
|
|
|
|
}
|
|
|
|
|
2019-07-09 17:12:28 +03:00
|
|
|
if (socket_can_bind_connect("::1", PF_INET6) < 0) {
|
2017-12-22 14:45:24 +03:00
|
|
|
if (errno != EADDRNOTAVAIL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*has_ipv6 = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2022-08-02 10:52:00 +03:00
|
|
|
|
|
|
|
void socket_check_afunix_support(bool *has_afunix)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
|
|
closesocket(fd);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
*has_afunix = (fd != (int)INVALID_SOCKET);
|
|
|
|
#else
|
|
|
|
*has_afunix = (fd >= 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|