Skip to content

Commit

Permalink
slirp: Support link-local DNS addresses
Browse files Browse the repository at this point in the history
They look like fe80::%eth0

Signed-off-by: Samuel Thibault <[email protected]>
Reviewed-by: Thomas Huth <[email protected]>

---
Changes since last submission:
- fix windows build
  • Loading branch information
sthibaul committed Jul 3, 2016
1 parent 1d17654 commit ef763fa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
2 changes: 1 addition & 1 deletion slirp/libslirp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 27 additions & 5 deletions slirp/slirp.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
#include "hw/hw.h"
#include "qemu/cutils.h"

#ifndef _WIN32
#include <net/if.h>
#endif

/* host loopback address */
struct in_addr loopback_addr;
/* host loopback network mask */
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand All @@ -155,13 +165,24 @@ 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;
}
/* If it's the first one, set it to dns_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
Expand Down Expand Up @@ -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;

Expand All @@ -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
Expand Down
5 changes: 4 additions & 1 deletion slirp/socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ef763fa

Please sign in to comment.