]> git.proxmox.com Git - mirror_qemu.git/commitdiff
slirp: Add dns6 resolution
authorSamuel Thibault <samuel.thibault@ens-lyon.org>
Sun, 20 Mar 2016 14:09:09 +0000 (15:09 +0100)
committerSamuel Thibault <samuel.thibault@ens-lyon.org>
Sun, 3 Jul 2016 21:27:08 +0000 (23:27 +0200)
This makes get_dns_addr address family-agnostic, thus allowing to add the
IPv6 case.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Reviewed-by: Thomas Huth <thuth@redhat.com>
slirp/ip6.h
slirp/libslirp.h
slirp/slirp.c
slirp/socket.c

index 8ddfa242c4c21a1d6e776c1fb2e428bec77ed22e..da23de66f1d869fbaf215a55f357e33ee69b7fbb 100644 (file)
                         0x00, 0x00, 0x00, 0x00,\
                         0x00, 0x00, 0x00, 0x02 } }
 
+#define ZERO_ADDR  { .s6_addr = \
+                        { 0x00, 0x00, 0x00, 0x00,\
+                        0x00, 0x00, 0x00, 0x00,\
+                        0x00, 0x00, 0x00, 0x00,\
+                        0x00, 0x00, 0x00, 0x00 } }
+
 static inline bool in6_equal(const struct in6_addr *a, const struct in6_addr *b)
 {
     return memcmp(a, b, sizeof(*a)) == 0;
@@ -84,6 +90,9 @@ static inline bool in6_equal_mach(const struct in6_addr *a,
 #define in6_solicitednode_multicast(a)\
     (in6_equal_net(a, &(struct in6_addr)SOLICITED_NODE_PREFIX, 104))
 
+#define in6_zero(a)\
+    (in6_equal(a, &(struct in6_addr)ZERO_ADDR))
+
 /* Compute emulated host MAC address from its ipv6 address */
 static inline void in6_compute_ethaddr(struct in6_addr ip,
                                        uint8_t eth[ETH_ALEN])
index 127aa41d40aa7a8ce39209801dc02b75c941e0ed..b0cfbc5b0f124e1cf64c4a9933eae500da5e11ff 100644 (file)
@@ -7,6 +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);
 
 Slirp *slirp_init(int restricted, bool in_enabled, struct in_addr vnetwork,
                   struct in_addr vnetmask, struct in_addr vhost,
index e63c5e8fc6493cca13a330ba5592b8e5dbc34b45..197d9f2979d6b599f9bcdef5d098581cbb05a7e1 100644 (file)
@@ -46,7 +46,9 @@ static QTAILQ_HEAD(slirp_instances, Slirp) slirp_instances =
     QTAILQ_HEAD_INITIALIZER(slirp_instances);
 
 static struct in_addr dns_addr;
+static struct in6_addr dns6_addr;
 static u_int dns_addr_time;
+static u_int dns6_addr_time;
 
 #define TIMEOUT_FAST 2  /* milliseconds */
 #define TIMEOUT_SLOW 499  /* milliseconds */
@@ -100,6 +102,11 @@ int get_dns_addr(struct in_addr *pdns_addr)
     return 0;
 }
 
+int get_dns6_addr(struct in6_addr *pdns_addr6)
+{
+    return -1;
+}
+
 static void winsock_cleanup(void)
 {
     WSACleanup();
@@ -107,36 +114,37 @@ static void winsock_cleanup(void)
 
 #else
 
-static struct stat dns_addr_stat;
-
-static int get_dns_addr_cached(struct in_addr *pdns_addr)
+static int get_dns_addr_cached(void *pdns_addr, void *cached_addr,
+                               socklen_t addrlen,
+                               struct stat *cached_stat, u_int *cached_time)
 {
     struct stat old_stat;
-    if (curtime - dns_addr_time < TIMEOUT_DEFAULT) {
-        *pdns_addr = dns_addr;
+    if (curtime - *cached_time < TIMEOUT_DEFAULT) {
+        memcpy(pdns_addr, cached_addr, addrlen);
         return 0;
     }
-    old_stat = dns_addr_stat;
-    if (stat("/etc/resolv.conf", &dns_addr_stat) != 0) {
+    old_stat = *cached_stat;
+    if (stat("/etc/resolv.conf", cached_stat) != 0) {
         return -1;
     }
-    if (dns_addr_stat.st_dev == old_stat.st_dev
-        && dns_addr_stat.st_ino == old_stat.st_ino
-        && dns_addr_stat.st_size == old_stat.st_size
-        && dns_addr_stat.st_mtime == old_stat.st_mtime) {
-        *pdns_addr = dns_addr;
+    if (cached_stat->st_dev == old_stat.st_dev
+        && cached_stat->st_ino == old_stat.st_ino
+        && cached_stat->st_size == old_stat.st_size
+        && cached_stat->st_mtime == old_stat.st_mtime) {
+        memcpy(pdns_addr, cached_addr, addrlen);
         return 0;
     }
     return 1;
 }
 
-static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr)
+static int get_dns_addr_resolv_conf(int af, void *pdns_addr, void *cached_addr,
+                                    socklen_t addrlen, u_int *cached_time)
 {
     char buff[512];
     char buff2[257];
     FILE *f;
     int found = 0;
-    struct in_addr tmp_addr;
+    void *tmp_addr = alloca(addrlen);
 
     f = fopen("/etc/resolv.conf", "r");
     if (!f)
@@ -147,13 +155,14 @@ static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr)
 #endif
     while (fgets(buff, 512, f) != NULL) {
         if (sscanf(buff, "nameserver%*[ \t]%256s", buff2) == 1) {
-            if (!inet_aton(buff2, &tmp_addr))
+            if (!inet_pton(af, buff2, tmp_addr)) {
                 continue;
+            }
             /* If it's the first one, set it to dns_addr */
             if (!found) {
-                *pdns_addr = tmp_addr;
-                dns_addr = tmp_addr;
-                dns_addr_time = curtime;
+                memcpy(pdns_addr, tmp_addr, addrlen);
+                memcpy(cached_addr, tmp_addr, addrlen);
+                *cached_time = curtime;
             }
 #ifdef DEBUG
             else
@@ -166,8 +175,14 @@ static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr)
                 break;
             }
 #ifdef DEBUG
-            else
-                fprintf(stderr, "%s", inet_ntoa(tmp_addr));
+            else {
+                char s[INET6_ADDRSTRLEN];
+                char *res = inet_ntop(af, tmp_addr, s, sizeof(s));
+                if (!res) {
+                    res = "(string conversion error)";
+                }
+                fprintf(stderr, "%s", res);
+            }
 #endif
         }
     }
@@ -179,14 +194,34 @@ static int get_dns_addr_resolv_conf(struct in_addr *pdns_addr)
 
 int get_dns_addr(struct in_addr *pdns_addr)
 {
+    static struct stat dns_addr_stat;
+
     if (dns_addr.s_addr != 0) {
         int ret;
-        ret = get_dns_addr_cached(pdns_addr);
+        ret = get_dns_addr_cached(pdns_addr, &dns_addr, sizeof(dns_addr),
+                                  &dns_addr_stat, &dns_addr_time);
+        if (ret <= 0) {
+            return ret;
+        }
+    }
+    return get_dns_addr_resolv_conf(AF_INET, pdns_addr, &dns_addr,
+                                    sizeof(dns_addr), &dns_addr_time);
+}
+
+int get_dns6_addr(struct in6_addr *pdns6_addr)
+{
+    static struct stat dns6_addr_stat;
+
+    if (!in6_zero(&dns6_addr)) {
+        int ret;
+        ret = get_dns_addr_cached(pdns6_addr, &dns6_addr, sizeof(dns6_addr),
+                                  &dns6_addr_stat, &dns6_addr_time);
         if (ret <= 0) {
             return ret;
         }
     }
-    return get_dns_addr_resolv_conf(pdns_addr);
+    return get_dns_addr_resolv_conf(AF_INET6, pdns6_addr, &dns6_addr,
+                                    sizeof(dns6_addr), &dns6_addr_time);
 }
 
 #endif
index b336586c7b384eb8add1bf5edee01130c0fdd9ac..8e8de88044080b48673e28a3e8fe8ac9fc6c3be9 100644 (file)
@@ -816,9 +816,9 @@ 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_dns_addr(&addr) < 0) {*/ /* TODO */
+                if (get_dns6_addr(&sin6->sin6_addr) < 0) {
                     sin6->sin6_addr = in6addr_loopback;
-                /*}*/
+                }
             } else {
                 sin6->sin6_addr = in6addr_loopback;
             }