diff mbox

[05/16] kvm tools: Get domain name and nameserver from host

Message ID 1310893024-21615-6-git-send-email-asias.hejun@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Asias He July 17, 2011, 8:56 a.m. 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 <asias.hejun@gmail.com>
---
 tools/kvm/include/kvm/uip.h |    6 ++++++
 tools/kvm/net/uip/dhcp.c    |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 0 deletions(-)

Comments

Sasha Levin July 17, 2011, 9:36 a.m. UTC | #1
On Sun, 2011-07-17 at 16:56 +0800, Asias He wrote:
> 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 <asias.hejun@gmail.com>
> ---

Seeing this after this patch:

cc1: warnings being treated as errors
net/uip/dhcp.c: In function 'uip_dhcp_get_dns':
net/uip/dhcp.c:49:9: error: ignoring return value of 'fscanf', declared
with attribute warn_unused_result
make: *** [net/uip/dhcp.o] Error 1

>  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 <arpa/inet.h>
> +
>  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;
> +}
Asias He July 18, 2011, 4:48 a.m. UTC | #2
On 07/17/2011 05:36 PM, Sasha Levin wrote:
> On Sun, 2011-07-17 at 16:56 +0800, Asias He wrote:
>> 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 <asias.hejun@gmail.com>
>> ---
> 
> Seeing this after this patch:
> 
> cc1: warnings being treated as errors
> net/uip/dhcp.c: In function 'uip_dhcp_get_dns':
> net/uip/dhcp.c:49:9: error: ignoring return value of 'fscanf', declared
> with attribute warn_unused_result
> make: *** [net/uip/dhcp.o] Error 1

Yup. We need to check the return value of 'fscanf'.

My gcc (4.6.1) does not catch this by default, I guess we need to enable
this explicitly in our Makefile.

>>  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 <arpa/inet.h>
>> +
>>  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;
>> +}
> 
>
Pekka Enberg July 18, 2011, 8:27 a.m. UTC | #3
On Mon, 2011-07-18 at 12:48 +0800, Asias He wrote:
> My gcc (4.6.1) does not catch this by default, I guess we need to enable
> this explicitly in our Makefile.

Yes, please.

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

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 <arpa/inet.h>
+
 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;
+}