From 9b589ffb1237ec54f8341d8a80ba77f31d428842 Mon Sep 17 00:00:00 2001 From: Daniel P. Berrange Date: Fri, 22 Dec 2017 11:45:24 +0000 Subject: sockets: pull code for testing IP availability out of specific test The test-io-channel-socket.c file has some useful helper functions for checking if a specific IP protocol is available. Other tests need to perform similar kinds of checks to avoid running tests that will fail due to missing IP protocols. Reviewed-by: Eric Blake Reviewed-by: Marc-André Lureau Signed-off-by: Daniel P. Berrange --- tests/socket-helpers.c | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 tests/socket-helpers.c (limited to 'tests/socket-helpers.c') diff --git a/tests/socket-helpers.c b/tests/socket-helpers.c new file mode 100644 index 0000000000..6f4bf07aa9 --- /dev/null +++ b/tests/socket-helpers.c @@ -0,0 +1,100 @@ +/* + * 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 . + * + */ + +#include "qemu/osdep.h" +#include "qemu-common.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 + +int socket_can_bind(const char *hostname) +{ + int fd = -1; + struct addrinfo ai, *res = NULL; + int rc; + int ret = -1; + + memset(&ai, 0, sizeof(ai)); + ai.ai_flags = AI_CANONNAME | AI_ADDRCONFIG; + ai.ai_family = AF_UNSPEC; + ai.ai_socktype = SOCK_STREAM; + + /* lookup */ + rc = getaddrinfo(hostname, NULL, &ai, &res); + if (rc != 0) { + if (rc == EAI_ADDRFAMILY || + rc == EAI_FAMILY) { + errno = EADDRNOTAVAIL; + } else { + errno = EINVAL; + } + goto cleanup; + } + + fd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (fd < 0) { + goto cleanup; + } + + if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) { + goto cleanup; + } + + ret = 0; + + cleanup: + if (fd != -1) { + close(fd); + } + if (res) { + freeaddrinfo(res); + } + return ret; +} + + +int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6) +{ + *has_ipv4 = *has_ipv6 = false; + + if (socket_can_bind("127.0.0.1") < 0) { + if (errno != EADDRNOTAVAIL) { + return -1; + } + } else { + *has_ipv4 = true; + } + + if (socket_can_bind("::1") < 0) { + if (errno != EADDRNOTAVAIL) { + return -1; + } + } else { + *has_ipv6 = true; + } + + return 0; +} -- cgit v1.2.3-55-g7522 From abd983c0e0beb4b15326e51a837e1b5177df0e08 Mon Sep 17 00:00:00 2001 From: Daniel P. Berrange Date: Fri, 22 Dec 2017 11:48:49 +0000 Subject: sockets: strengthen test suite IP protocol availability checks Instead of just checking whether it is possible to bind() on a socket, also check that we can successfully connect() to the socket we bound to. This more closely replicates the level of functionality that tests will actually use. Reviewed-by: Eric Blake Reviewed-by: Marc-André Lureau Signed-off-by: Daniel P. Berrange --- tests/socket-helpers.c | 67 +++++++++++++++++++++++++++++++++++++++++++------- tests/socket-helpers.h | 4 +-- 2 files changed, 60 insertions(+), 11 deletions(-) (limited to 'tests/socket-helpers.c') diff --git a/tests/socket-helpers.c b/tests/socket-helpers.c index 6f4bf07aa9..8112763f5b 100644 --- a/tests/socket-helpers.c +++ b/tests/socket-helpers.c @@ -30,10 +30,15 @@ # define EAI_ADDRFAMILY 0 #endif -int socket_can_bind(const char *hostname) +int socket_can_bind_connect(const char *hostname) { - int fd = -1; + int lfd = -1, cfd = -1, afd = -1; struct addrinfo ai, *res = NULL; + struct sockaddr_storage ss; + socklen_t sslen = sizeof(ss); + int soerr; + socklen_t soerrlen = sizeof(soerr); + bool check_soerr = false; int rc; int ret = -1; @@ -54,20 +59,64 @@ int socket_can_bind(const char *hostname) goto cleanup; } - fd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (fd < 0) { + lfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (lfd < 0) { goto cleanup; } - if (bind(fd, res->ai_addr, res->ai_addrlen) < 0) { + cfd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); + if (cfd < 0) { goto cleanup; } + 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; + } + + qemu_set_nonblock(cfd); + 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) { + if (qemu_getsockopt(cfd, SOL_SOCKET, SO_ERROR, &soerr, &soerrlen) < 0) { + goto cleanup; + } + if (soerr) { + errno = soerr; + goto cleanup; + } + } + ret = 0; cleanup: - if (fd != -1) { - close(fd); + if (afd != -1) { + close(afd); + } + if (cfd != -1) { + close(cfd); + } + if (lfd != -1) { + close(lfd); } if (res) { freeaddrinfo(res); @@ -80,7 +129,7 @@ int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6) { *has_ipv4 = *has_ipv6 = false; - if (socket_can_bind("127.0.0.1") < 0) { + if (socket_can_bind_connect("127.0.0.1") < 0) { if (errno != EADDRNOTAVAIL) { return -1; } @@ -88,7 +137,7 @@ int socket_check_protocol_support(bool *has_ipv4, bool *has_ipv6) *has_ipv4 = true; } - if (socket_can_bind("::1") < 0) { + if (socket_can_bind_connect("::1") < 0) { if (errno != EADDRNOTAVAIL) { return -1; } diff --git a/tests/socket-helpers.h b/tests/socket-helpers.h index efa96eddc2..1c07d6d656 100644 --- a/tests/socket-helpers.h +++ b/tests/socket-helpers.h @@ -21,13 +21,13 @@ /* * @hostname: a DNS name or numeric IP address * - * Check whether it is possible to bind to ports + * 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 */ -int socket_can_bind(const char *hostname); +int socket_can_bind_connect(const char *hostname); /* * @has_ipv4: set to true on return if IPv4 is available -- cgit v1.2.3-55-g7522