kvm tools: Get domain name and nameserver from host

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>
[ penberg@kernel.org: check fscanf() for errors ]
Signed-off-by: Pekka Enberg <penberg@kernel.org>
This commit is contained in:
Asias He
2015-06-01 16:39:46 +01:00
committed by Will Deacon
parent 8f5f2f958f
commit bdcc9e40df
2 changed files with 41 additions and 0 deletions
+6
View File
@@ -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 */
+35
View File
@@ -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,36 @@ 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)) {
if (fscanf(fp, "%s %s\n", key, val) != 2)
continue;
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;
}