Merge I/O fixes

-----BEGIN PGP SIGNATURE-----
 Version: GnuPG v1
 
 iQIcBAABCAAGBQJW6B9jAAoJEL6G67QVEE/fMQwQAIhGJitLheww1QkxILwIMgGN
 f4cZy/QeUY2RBPU1VAQzOi62rBFN/l0vHgnJ7yZkVee4/hCc0gtSrX826TKuGuvB
 tZ0/aW0rZVynLOA/t0m9DRTJuyZrSDJQld0Q8oWumnL70wSAWNbp/pZ3XPhHfCiD
 YS7lI5af+fkgeAV+RLK+OQos3YfjIlaX6ZK5mXTvykJZm57rS9aShRz6jXFK1WCV
 hUBamG8wa8eGgRbbs+IpVuhgrk+JYRbE+/khiPBC46ACiu5AqnnjyNM3pBm+RTWA
 Aj968n0sBVtJnPBTyvjfYa2yoAKDaYW1j97H2UJm8oDAU6eiLW2foHin8xkfYXcO
 lED9wI9JKG/1sNk3AAI1nzMAuoImt4OJmQ0Z6eq1JvSGDO2pIaAlxgIiR1bKuAML
 A55ifMAsPkvNJfUqtOnyPTqoV9YbZIfbtAeVkaMeRxwukEE+ONx9oRMwpUX1e0xd
 jlquSzEXYzwtNBEN3K1JS89iWnH3+wxJvMRzLaBlbQJFxPKlSEmRoyFVmtTRlIml
 OvHwJWvvuAUMujzOF+6NxMdUCqWbyqr0mYHqTMEA1HGtDIR9QRd2mE6cWDyD9FuZ
 jTILLyvvMUU3yB/c9Oa5uVzg8gqQ5krciZRE7A1sIjL02OX5aQp4U013+TRRC81g
 BweR+CYe2YhQ8buY+g3y
 =vwWE
 -----END PGP SIGNATURE-----

Merge remote-tracking branch 'remotes/berrange/tags/pull-io-next-2016-03-15-1' into staging

Merge I/O fixes

# gpg: Signature made Tue 15 Mar 2016 14:42:43 GMT using RSA key ID 15104FDF
# gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>"
# gpg:                 aka "Daniel P. Berrange <berrange@redhat.com>"

* remotes/berrange/tags/pull-io-next-2016-03-15-1:
  io: stronger check for support for IPv4/6

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
This commit is contained in:
Peter Maydell 2016-03-15 15:51:06 +00:00
commit f84d587111

View File

@ -23,44 +23,72 @@
#include "io/channel-util.h"
#include "io-channel-helpers.h"
static int check_bind(struct sockaddr *sa, socklen_t salen, bool *has_proto)
{
int fd;
#ifndef AI_ADDRCONFIG
# define AI_ADDRCONFIG 0
#endif
#ifndef AI_V4MAPPED
# define AI_V4MAPPED 0
#endif
#ifndef EAI_ADDRFAMILY
# define EAI_ADDRFAMILY 0
#endif
fd = socket(sa->sa_family, SOCK_STREAM, 0);
if (fd < 0) {
return -1;
static int check_bind(const char *hostname, bool *has_proto)
{
int fd = -1;
struct addrinfo ai, *res = NULL;
int rc;
int ret = -1;
memset(&ai, 0, sizeof(ai));
ai.ai_flags = AI_CANONNAME | AI_V4MAPPED | 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) {
*has_proto = false;
goto done;
}
goto cleanup;
}
if (bind(fd, sa, salen) < 0) {
close(fd);
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) {
if (errno == EADDRNOTAVAIL) {
*has_proto = false;
return 0;
goto done;
}
return -1;
goto cleanup;
}
close(fd);
*has_proto = true;
return 0;
done:
ret = 0;
cleanup:
if (fd != -1) {
close(fd);
}
if (res) {
freeaddrinfo(res);
}
return ret;
}
static int check_protocol_support(bool *has_ipv4, bool *has_ipv6)
{
struct sockaddr_in sin = {
.sin_family = AF_INET,
.sin_addr = { .s_addr = htonl(INADDR_LOOPBACK) },
};
struct sockaddr_in6 sin6 = {
.sin6_family = AF_INET6,
.sin6_addr = IN6ADDR_LOOPBACK_INIT,
};
if (check_bind((struct sockaddr *)&sin, sizeof(sin), has_ipv4) < 0) {
if (check_bind("127.0.0.1", has_ipv4) < 0) {
return -1;
}
if (check_bind((struct sockaddr *)&sin6, sizeof(sin6), has_ipv6) < 0) {
if (check_bind("::1", has_ipv6) < 0) {
return -1;
}