From patchwork Tue Mar 15 14:53:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= X-Patchwork-Id: 8588941 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id E95369F54C for ; Tue, 15 Mar 2016 14:53:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 4A601201F5 for ; Tue, 15 Mar 2016 14:53:35 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 8AC6F201EF for ; Tue, 15 Mar 2016 14:53:34 +0000 (UTC) Received: from localhost ([::1]:48780 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afqLm-000099-0F for patchwork-qemu-devel@patchwork.kernel.org; Tue, 15 Mar 2016 10:53:34 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:55255) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afqLd-00008j-Vg for qemu-devel@nongnu.org; Tue, 15 Mar 2016 10:53:26 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1afqLZ-0004JU-07 for qemu-devel@nongnu.org; Tue, 15 Mar 2016 10:53:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55335) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1afqLY-0004JQ-Qz for qemu-devel@nongnu.org; Tue, 15 Mar 2016 10:53:20 -0400 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 546D0C0467F0; Tue, 15 Mar 2016 14:53:20 +0000 (UTC) Received: from t530wlan.home.berrange.com.com ([10.42.17.224]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u2FErIx5024780; Tue, 15 Mar 2016 10:53:19 -0400 From: "Daniel P. Berrange" To: qemu-devel@nongnu.org Date: Tue, 15 Mar 2016 14:53:09 +0000 Message-Id: <1458053589-7075-1-git-send-email-berrange@redhat.com> In-Reply-To: <1458053165-5187-1-git-send-email-berrange@redhat.com> References: <1458053165-5187-1-git-send-email-berrange@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Peter Maydell Subject: [Qemu-devel] [PULL v1] io: stronger check for support for IPv4/6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Instead of just checking for bind(), also check whether getaddrinfo can resolve IPv6 addresses. This catches failure when travis runs QEMU builds inside minimal docker containers Signed-off-by: Daniel P. Berrange --- tests/test-io-channel-socket.c | 70 +++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/tests/test-io-channel-socket.c b/tests/test-io-channel-socket.c index ae665f5..87018ac 100644 --- a/tests/test-io-channel-socket.c +++ b/tests/test-io-channel-socket.c @@ -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) +#ifndef AI_ADDRCONFIG +# define AI_ADDRCONFIG 0 +#endif +#ifndef AI_V4MAPPED +# define AI_V4MAPPED 0 +#endif +#ifndef EAI_ADDRFAMILY +# define EAI_ADDRFAMILY 0 +#endif + +static int check_bind(const char *hostname, bool *has_proto) { - int fd; + 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; + } - fd = socket(sa->sa_family, SOCK_STREAM, 0); + fd = qemu_socket(res->ai_family, res->ai_socktype, res->ai_protocol); if (fd < 0) { - return -1; + goto cleanup; } - if (bind(fd, sa, salen) < 0) { - close(fd); + 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; }