From patchwork Sun Jul 3 22:04:40 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Samuel Thibault X-Patchwork-Id: 9211463 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 5880F60752 for ; Sun, 3 Jul 2016 22:05:34 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 46F06285C8 for ; Sun, 3 Jul 2016 22:05:34 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 372F028607; Sun, 3 Jul 2016 22:05:34 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 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.wl.linuxfoundation.org (Postfix) with ESMTPS id BFEDF285C8 for ; Sun, 3 Jul 2016 22:05:33 +0000 (UTC) Received: from localhost ([::1]:44284 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJpW8-0004VW-Uc for patchwork-qemu-devel@patchwork.kernel.org; Sun, 03 Jul 2016 18:05:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:36191) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJpVa-0004UQ-Ea for qemu-devel@nongnu.org; Sun, 03 Jul 2016 18:04:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bJpVU-0001zD-F4 for qemu-devel@nongnu.org; Sun, 03 Jul 2016 18:04:57 -0400 Received: from hera.aquilenet.fr ([141.255.128.1]:38970) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJpVU-0001yi-4p for qemu-devel@nongnu.org; Sun, 03 Jul 2016 18:04:52 -0400 Received: from localhost (localhost [127.0.0.1]) by hera.aquilenet.fr (Postfix) with ESMTP id 482491327; Mon, 4 Jul 2016 00:04:44 +0200 (CEST) Received: from hera.aquilenet.fr ([127.0.0.1]) by localhost (hera.aquilenet.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Raw2flZTyjVd; Mon, 4 Jul 2016 00:04:44 +0200 (CEST) Received: from var.youpi.perso.aquilenet.fr (unknown [IPv6:2a01:cb19:1af:4600:3602:86ff:fe2c:6a19]) by hera.aquilenet.fr (Postfix) with ESMTPSA id 9B49177A3; Mon, 4 Jul 2016 00:04:43 +0200 (CEST) Received: from samy by var.youpi.perso.aquilenet.fr with local (Exim 4.87) (envelope-from ) id 1bJpVP-0002DK-3Y; Mon, 04 Jul 2016 00:04:47 +0200 From: Samuel Thibault To: qemu-devel@nongnu.org, peter.maydell@linaro.org Date: Mon, 4 Jul 2016 00:04:40 +0200 Message-Id: <1467583483-8462-4-git-send-email-samuel.thibault@ens-lyon.org> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1467583483-8462-1-git-send-email-samuel.thibault@ens-lyon.org> References: <1467583483-8462-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: 141.255.128.1 Subject: [Qemu-devel] [PULL 3/6] slirp: Support link-local DNS addresses X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Samuel Thibault , jan.kiszka@siemens.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP They look like fe80::%eth0 Signed-off-by: Samuel Thibault Reviewed-by: Thomas Huth --- Changes since last submission: - fix windows build --- slirp/libslirp.h | 2 +- slirp/slirp.c | 32 +++++++++++++++++++++++++++----- slirp/socket.c | 5 ++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/slirp/libslirp.h b/slirp/libslirp.h index b0cfbc5..b6fc584 100644 --- a/slirp/libslirp.h +++ b/slirp/libslirp.h @@ -7,7 +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); +int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id); Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork, struct in_addr vnetmask, struct in_addr vhost, diff --git a/slirp/slirp.c b/slirp/slirp.c index 197d9f2..7eb183d 100644 --- a/slirp/slirp.c +++ b/slirp/slirp.c @@ -30,6 +30,10 @@ #include "hw/hw.h" #include "qemu/cutils.h" +#ifndef _WIN32 +#include +#endif + /* host loopback address */ struct in_addr loopback_addr; /* host loopback network mask */ @@ -46,9 +50,13 @@ static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances = QTAILQ_HEAD_INITIALIZER(slirp_instances); static struct in_addr dns_addr; +#ifndef _WIN32 static struct in6_addr dns6_addr; +#endif static u_int dns_addr_time; +#ifndef _WIN32 static u_int dns6_addr_time; +#endif #define TIMEOUT_FAST 2 /* milliseconds */ #define TIMEOUT_SLOW 499 /* milliseconds */ @@ -102,7 +110,7 @@ int get_dns_addr(struct in_addr *pdns_addr) return 0; } -int get_dns6_addr(struct in6_addr *pdns_addr6) +int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) { return -1; } @@ -138,13 +146,15 @@ static int get_dns_addr_cached(void *pdns_addr, void *cached_addr, } static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, - socklen_t addrlen, u_int *cached_time) + socklen_t addrlen, uint32_t *scope_id, + u_int *cached_time) { char buff[512]; char buff2[257]; FILE *f; int found = 0; void *tmp_addr = alloca(addrlen); + unsigned if_index; f = fopen("/etc/resolv.conf", "r"); if (!f) @@ -155,6 +165,14 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, #endif while (fgets(buff, 512, f) != NULL) { if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) { + char *c = strchr(buff2, '%'); + if (c) { + if_index = if_nametoindex(c + 1); + *c = '\0'; + } else { + if_index = 0; + } + if (!inet_pton(af, buff2, tmp_addr)) { continue; } @@ -162,6 +180,9 @@ static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr, if (!found) { memcpy(pdns_addr, tmp_addr, addrlen); memcpy(cached_addr, tmp_addr, addrlen); + if (scope_id) { + *scope_id = if_index; + } *cached_time = curtime; } #ifdef DEBUG @@ -205,10 +226,10 @@ int get_dns_addr(struct in_addr *pdns_addr) } } return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr, - sizeof(dns_addr), &dns_addr_time); + sizeof(dns_addr), NULL, &dns_addr_time); } -int get_dns6_addr(struct in6_addr *pdns6_addr) +int get_dns6_addr(struct in6_addr *pdns6_addr, uint32_t *scope_id) { static struct stat dns6_addr_stat; @@ -221,7 +242,8 @@ int get_dns6_addr(struct in6_addr *pdns6_addr) } } return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr, - sizeof(dns6_addr), &dns6_addr_time); + sizeof(dns6_addr), + scope_id, &dns6_addr_time); } #endif diff --git a/slirp/socket.c b/slirp/socket.c index 8e8de88..02e89ce 100644 --- a/slirp/socket.c +++ b/slirp/socket.c @@ -816,7 +816,10 @@ 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_dns6_addr(&sin6->sin6_addr) < 0) { + uint32_t scope_id; + if (get_dns6_addr(&sin6->sin6_addr, &scope_id) >= 0) { + sin6->sin6_scope_id = scope_id; + } else { sin6->sin6_addr = in6addr_loopback; } } else {