From patchwork Sun Mar 20 15:26:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 8626631 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id CEB61C0553 for ; Sun, 20 Mar 2016 15:28:36 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 0ADB220304 for ; Sun, 20 Mar 2016 15:28: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 17BC9202E6 for ; Sun, 20 Mar 2016 15:28:34 +0000 (UTC) Received: from localhost ([::1]:53324 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ahfHN-0006hz-GT for patchwork-qemu-devel@patchwork.kernel.org; Sun, 20 Mar 2016 11:28:33 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49659) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ahfFT-0003OP-Ks for qemu-devel@nongnu.org; Sun, 20 Mar 2016 11:26:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ahfFQ-0003bI-Cb for qemu-devel@nongnu.org; Sun, 20 Mar 2016 11:26:35 -0400 Received: from domu-toccata.ens-lyon.fr ([140.77.166.138]:44266 helo=sonata.ens-lyon.org) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ahfFQ-0003ax-16 for qemu-devel@nongnu.org; Sun, 20 Mar 2016 11:26:32 -0400 Received: from localhost (localhost [127.0.0.1]) by sonata.ens-lyon.org (Postfix) with ESMTP id 56054200E6; Sun, 20 Mar 2016 16:26:30 +0100 (CET) Received: from sonata.ens-lyon.org ([127.0.0.1]) by localhost (sonata.ens-lyon.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id QeThsjW__GrJ; Sun, 20 Mar 2016 16:26:30 +0100 (CET) Received: from var.youpi.perso.aquilenet.fr (LFbn-1-6757-94.w90-120.abo.wanadoo.fr [90.120.189.94]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by sonata.ens-lyon.org (Postfix) with ESMTPSA id 27F0F200E1; Sun, 20 Mar 2016 16:26:30 +0100 (CET) Received: from samy by var.youpi.perso.aquilenet.fr with local (Exim 4.86_2) (envelope-from ) id 1ahfFN-0006zI-Fk; Sun, 20 Mar 2016 16:26:29 +0100 From: Samuel Thibault To: qemu-devel@nongnu.org, thuth@redhat.com Date: Sun, 20 Mar 2016 16:26:26 +0100 Message-Id: <1458487588-26812-3-git-send-email-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1458487588-26812-1-git-send-email-samuel.thibault@ens-lyon.org> References: <1458487588-26812-1-git-send-email-samuel.thibault@ens-lyon.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 140.77.166.138 Cc: Samuel Thibault , jan.kiszka@siemens.com Subject: [Qemu-devel] [PATCH 2/4] slirp: Add dns6 resolution 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 This makes get_dns_addr address family-agnostic, thus allowing to add the IPv6 case. Signed-off-by: Samuel Thibault --- slirp/libslirp.h | 1 + slirp/slirp.c | 72 ++++++++++++++++++++++++++++++++++++++++---------------- slirp/socket.c | 4 ++-- 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/slirp/libslirp.h b/slirp/libslirp.h index c4b25c9..eea9be4 100644 --- a/slirp/libslirp.h +++ b/slirp/libslirp.h @@ -7,6 +7,7 @@ struct Slirp; typedef struct Slirp Slirp; int get_dns_addr(struct in_addr *pdns_addr); +int get_dns6_addr(struct in6_addr *pdns6_addr); Slirp *slirp_init(int restricted, struct in_addr vnetwork, struct in_addr vnetmask, struct in_addr vhost, diff --git a/slirp/slirp.c b/slirp/slirp.c index 36534fc..3deb4c2 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -45,7 +45,9 @@ static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances = QTAILQ_HEAD_INITIALIZER(slirp_instances); static struct in_addr dns_addr; +static struct in6_addr dns6_addr; static u_int dns_addr_time; +static u_int dns6_addr_time; #define TIMEOUT_FAST 2 /* milliseconds */ #define TIMEOUT_SLOW 499 /* milliseconds */ @@ -99,6 +101,11 @@ int get_dns_addr(struct in_addr *pdns_addr) return 0; } +int get_dns6_addr(struct in6_addr *pdns_addr6) +{ + return -1; +} + static void winsock_cleanup(void) { WSACleanup(); @@ -106,36 +113,40 @@ static void winsock_cleanup(void) #else -static struct stat dns_addr_stat; - -static int get_dns_addr_cached(struct in_addr *pdns_addr) +static int get_dns_addr_cached(void *pdns_addr, void *cached_addr, + socklen_t addrlen, + struct stat *cached_stat, u_int *cached_time) { struct stat old_stat; if ((curtime - dns_addr_time) < TIMEOUT_DEFAULT) { - *pdns_addr = dns_addr; + memcpy(pdns_addr, cached_addr, addrlen); return 0; } - old_stat = dns_addr_stat; - if (stat("/etc/resolv.conf", &dns_addr_stat) != 0) { + old_stat = *cached_stat; + if (stat("/etc/resolv.conf", cached_stat) != 0) { return -1; } - if ((dns_addr_stat.st_dev == old_stat.st_dev) - && (dns_addr_stat.st_ino == old_stat.st_ino) - && (dns_addr_stat.st_size == old_stat.st_size) - && (dns_addr_stat.st_mtime == old_stat.st_mtime)) { - *pdns_addr = dns_addr; + if ((cached_stat->st_dev == old_stat.st_dev) + && (cached_stat->st_ino == old_stat.st_ino) + && (cached_stat->st_size == old_stat.st_size) + && (cached_stat->st_mtime == old_stat.st_mtime)) { + memcpy(pdns_addr, cached_addr, addrlen); return 0; } return 1; } -static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr) +static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, + socklen_t addrlen, u_int *cached_time) { char buff[512]; char buff2[257]; FILE *f; int found = 0; - struct in_addr tmp_addr; + void *tmp_addr = alloca(addrlen); +#ifdef DEBUG + char s[INET6_ADDRSTRLEN]; +#endif f = fopen("/etc/resolv.conf", "r"); if (!f) @@ -146,13 +157,14 @@ static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr) #endif while (fgets(buff, 512, f) != NULL) { if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { - if (!inet_aton(buff2, &tmp_addr)) + if (!inet_pton(af, buff2, tmp_addr)) { continue; + } /* If it's the first one, set it to dns_addr */ if (!found) { - *pdns_addr = tmp_addr; - dns_addr = tmp_addr; - dns_addr_time = curtime; + memcpy(pdns_addr, tmp_addr, addrlen); + memcpy(cached_addr, tmp_addr, addrlen); + *cached_time = curtime; } #ifdef DEBUG else @@ -166,7 +178,7 @@ static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr) } #ifdef DEBUG else - fprintf(stderr, "%s", inet_ntoa(tmp_addr)); + fprintf(stderr, "%s", inet_ntop(af, tmp_addr, s, sizeof(s))); #endif } } @@ -176,16 +188,36 @@ static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr) return 0; } +static struct stat dns_addr_stat; + int get_dns_addr(struct in_addr *pdns_addr) { if (dns_addr.s_addr != 0) { int ret; - ret = get_dns_addr_cached(pdns_addr); + ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr), + &dns_addr_stat, &dns_addr_time); + if (ret <= 0) { + return ret; + } + } + return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr, + sizeof(dns_addr), &dns_addr_time); +} + +static struct stat dns6_addr_stat; + +int get_dns6_addr(struct in6_addr *pdns6_addr) +{ + if (!in6_zero(&dns6_addr)) { + int ret; + ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr), + &dns6_addr_stat, &dns6_addr_time); if (ret <= 0) { return ret; } } - return get_dns_addr_resolv_conf(pdns_addr); + return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr, + sizeof(dns6_addr), &dns6_addr_time); } #endif diff --git a/slirp/socket.c b/slirp/socket.c index b836c42..653257d 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -796,9 +796,9 @@ void sotranslate_out(struct socket *so, struct sockaddr_storage *addr) if (in6_equal_net(&so->so_faddr6, &slirp->vprefix_addr6, slirp->vprefix_len)) { if (in6_equal(&so->so_faddr6, &slirp->vnameserver_addr6)) { - /*if (get_dns_addr(&addr) < 0) {*/ /* TODO */ + if (get_dns6_addr(&sin6->sin6_addr) < 0) { sin6->sin6_addr = in6addr_loopback; - /*}*/ + } } else { sin6->sin6_addr = in6addr_loopback; }