From patchwork Sun Jul 17 08:56:53 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Asias He X-Patchwork-Id: 982932 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.4) with ESMTP id p6H8xQuL023048 for ; Sun, 17 Jul 2011 08:59:26 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753822Ab1GQI7T (ORCPT ); Sun, 17 Jul 2011 04:59:19 -0400 Received: from mail-iy0-f174.google.com ([209.85.210.174]:39735 "EHLO mail-iy0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753731Ab1GQI7R (ORCPT ); Sun, 17 Jul 2011 04:59:17 -0400 Received: by mail-iy0-f174.google.com with SMTP id 12so2303241iyb.19 for ; Sun, 17 Jul 2011 01:59:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=9392UMV7tb5NSVWy0YVQi2tE0g3INSN0bkU3kXzXLSQ=; b=V0amreIpLsA598kvyLSPyo3BOrC8jmuOwJBc/xPt7H4w7fLIENKVFcRWBXxtxLcLAH CdFJS58dMia7jHYBLV3AQFqwY9k0Y7KNT94o88vODCDIYZmbLtZviVYj/ftEldpr1CEg +aDwEpwyluycF9ka5++LWhFnDxobLxThxraIs= Received: by 10.42.149.201 with SMTP id x9mr5896938icv.424.1310893156651; Sun, 17 Jul 2011 01:59:16 -0700 (PDT) Received: from localhost.localdomain ([219.224.169.130]) by mx.google.com with ESMTPS id us2sm3593680icb.19.2011.07.17.01.59.13 (version=TLSv1/SSLv3 cipher=OTHER); Sun, 17 Jul 2011 01:59:16 -0700 (PDT) From: Asias He To: Pekka Enberg Cc: Cyrill Gorcunov , Ingo Molnar , Sasha Levin , Prasad Joshi , kvm@vger.kernel.org, Asias He Subject: [PATCH 05/16] kvm tools: Get domain name and nameserver from host Date: Sun, 17 Jul 2011 16:56:53 +0800 Message-Id: <1310893024-21615-6-git-send-email-asias.hejun@gmail.com> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: <1310893024-21615-1-git-send-email-asias.hejun@gmail.com> References: <1310893024-21615-1-git-send-email-asias.hejun@gmail.com> Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Sun, 17 Jul 2011 08:59:26 +0000 (UTC) This patch get domain name and nameserver information from host config file /etc/resolv.conf. Guest can obtain DNS information through DHCP. Signed-off-by: Asias He --- tools/kvm/include/kvm/uip.h | 6 ++++++ tools/kvm/net/uip/dhcp.c | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 0 deletions(-) diff --git a/tools/kvm/include/kvm/uip.h b/tools/kvm/include/kvm/uip.h index 6534c7f..e645d3f 100644 --- a/tools/kvm/include/kvm/uip.h +++ b/tools/kvm/include/kvm/uip.h @@ -42,6 +42,8 @@ #define UIP_DHCP_OFFER 2 #define UIP_DHCP_REQUEST 3 #define UIP_DHCP_ACK 5 +#define UIP_DHCP_MAX_DNS_SERVER_NR 3 +#define UIP_DHCP_MAX_DOMAIN_NAME_LEN 256 #define UIP_DHCP_TAG_MSG_TYPE 53 #define UIP_DHCP_TAG_MSG_TYPE_LEN 1 /* @@ -178,6 +180,8 @@ struct uip_info { int buf_free_nr; int buf_used_nr; u32 host_ip; + u32 dns_ip[UIP_DHCP_MAX_DNS_SERVER_NR]; + char *domain_name; u32 buf_nr; }; @@ -327,4 +331,6 @@ struct uip_buf *uip_buf_clone(struct uip_tx_arg *arg); int uip_udp_make_pkg(struct uip_info *info, struct uip_udp_socket *sk, struct uip_buf *buf, u8 *payload, int payload_len); bool uip_udp_is_dhcp(struct uip_udp *udp); + +int uip_dhcp_get_dns(struct uip_info *info); #endif /* KVM__UIP_H */ diff --git a/tools/kvm/net/uip/dhcp.c b/tools/kvm/net/uip/dhcp.c index 0a6293a..9321cdc 100644 --- a/tools/kvm/net/uip/dhcp.c +++ b/tools/kvm/net/uip/dhcp.c @@ -1,5 +1,7 @@ #include "kvm/uip.h" +#include + static inline bool uip_dhcp_is_discovery(struct uip_dhcp *dhcp) { return (dhcp->option[2] == UIP_DHCP_DISCOVER && @@ -29,3 +31,35 @@ bool uip_udp_is_dhcp(struct uip_udp *udp) return true; } + +int uip_dhcp_get_dns(struct uip_info *info) +{ + char key[256], val[256]; + struct in_addr addr; + int ret = -1; + int n = 0; + FILE *fp; + u32 ip; + + fp = fopen("/etc/resolv.conf", "r"); + if (!fp) + goto out; + + while (!feof(fp)) { + fscanf(fp, "%s %s\n", key, val); + if (strncmp("domain", key, 6) == 0) + info->domain_name = strndup(val, UIP_DHCP_MAX_DOMAIN_NAME_LEN); + else if (strncmp("nameserver", key, 10) == 0) { + if (!inet_aton(val, &addr)) + continue; + ip = ntohl(addr.s_addr); + if (n < UIP_DHCP_MAX_DNS_SERVER_NR) + info->dns_ip[n++] = ip; + ret = 0; + } + } + +out: + fclose(fp); + return ret; +}