dns: treat names rejected by res_mkquery as nonexistent rather than error
[musl] / src / network / lookup_name.c
index 5a096ac..bec6ba2 100644 (file)
@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <pthread.h>
 #include <errno.h>
+#include <resolv.h>
 #include "lookup.h"
 #include "stdio_impl.h"
 #include "syscall.h"
@@ -49,7 +50,7 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati
 {
        char line[512];
        size_t l = strlen(name);
-       int cnt = 0, badfam = 0;
+       int cnt = 0, badfam = 0, have_canon = 0;
        unsigned char _buf[1032];
        FILE _f, *f = __fopen_rb_ca("/etc/hosts", &_f, _buf, sizeof _buf);
        if (!f) switch (errno) {
@@ -79,14 +80,19 @@ static int name_from_hosts(struct address buf[static MAXADDRS], char canon[stati
                        continue;
                default:
                        badfam = EAI_NONAME;
-                       continue;
+                       break;
                }
 
+               if (have_canon) continue;
+
                /* Extract first name as canonical name */
                for (; *p && isspace(*p); p++);
                for (z=p; *z && !isspace(*z); z++);
                *z = 0;
-               if (is_valid_hostname(p)) memcpy(canon, p, z-p+1);
+               if (is_valid_hostname(p)) {
+                       have_canon = 1;
+                       memcpy(canon, p, z-p+1);
+               }
        }
        __fclose_ca(f);
        return cnt ? cnt : badfam;
@@ -98,9 +104,6 @@ struct dpc_ctx {
        int cnt;
 };
 
-int __dn_expand(const unsigned char *, const unsigned char *, const unsigned char *, char *, int);
-int __res_mkquery(int, const char *, int, int, const unsigned char *, int, const unsigned char*, unsigned char *, int);
-
 #define RR_A 1
 #define RR_CNAME 5
 #define RR_AAAA 28
@@ -150,7 +153,11 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static
                        qlens[nq] = __res_mkquery(0, name, 1, afrr[i].rr,
                                0, 0, 0, qbuf[nq], sizeof *qbuf);
                        if (qlens[nq] == -1)
-                               return EAI_NONAME;
+                               return 0;
+                       qbuf[nq][3] = 0; /* don't need AD flag */
+                       /* Ensure query IDs are distinct. */
+                       if (nq && qbuf[nq][0] == qbuf[0][0])
+                               qbuf[nq][0]++;
                        nq++;
                }
        }
@@ -158,14 +165,17 @@ static int name_from_dns(struct address buf[static MAXADDRS], char canon[static
        if (__res_msend_rc(nq, qp, qlens, ap, alens, sizeof *abuf, conf) < 0)
                return EAI_SYSTEM;
 
+       for (i=0; i<nq; i++) {
+               if (alens[i] < 4 || (abuf[i][3] & 15) == 2) return EAI_AGAIN;
+               if ((abuf[i][3] & 15) == 3) return 0;
+               if ((abuf[i][3] & 15) != 0) return EAI_FAIL;
+       }
+
        for (i=0; i<nq; i++)
                __dns_parse(abuf[i], alens[i], dns_parse_callback, &ctx);
 
        if (ctx.cnt) return ctx.cnt;
-       if (alens[0] < 4 || (abuf[0][3] & 15) == 2) return EAI_AGAIN;
-       if ((abuf[0][3] & 15) == 0) return EAI_NONAME;
-       if ((abuf[0][3] & 15) == 3) return 0;
-       return EAI_FAIL;
+       return EAI_NONAME;
 }
 
 static int name_from_dns_search(struct address buf[static MAXADDRS], char canon[static 256], const char *name, int family)