08c3a6
commit f282cdbe7f436c75864e5640a409a10485e9abb2
08c3a6
Author: Florian Weimer <fweimer@redhat.com>
08c3a6
Date:   Fri Jun 24 18:16:41 2022 +0200
08c3a6
08c3a6
    resolv: Implement no-aaaa stub resolver option
08c3a6
    
08c3a6
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
08c3a6
08c3a6
diff --git a/resolv/Makefile b/resolv/Makefile
08c3a6
index 59e599535c7aa6eb..e8269dcb5bcf216b 100644
08c3a6
--- a/resolv/Makefile
08c3a6
+++ b/resolv/Makefile
08c3a6
@@ -51,6 +51,7 @@ routines := \
08c3a6
   nss_dns_functions \
08c3a6
   res-close \
08c3a6
   res-name-checking \
08c3a6
+  res-noaaaa \
08c3a6
   res-state \
08c3a6
   res_context_hostalias \
08c3a6
   res_enable_icmp \
08c3a6
@@ -93,6 +94,7 @@ tests += \
08c3a6
   tst-resolv-binary \
08c3a6
   tst-resolv-edns \
08c3a6
   tst-resolv-network \
08c3a6
+  tst-resolv-noaaaa \
08c3a6
   tst-resolv-nondecimal \
08c3a6
   tst-resolv-res_init-multi \
08c3a6
   tst-resolv-search \
08c3a6
@@ -256,6 +258,7 @@ $(objpfx)tst-resolv-res_init-multi: $(objpfx)libresolv.so \
08c3a6
   $(shared-thread-library)
08c3a6
 $(objpfx)tst-resolv-res_init-thread: $(objpfx)libresolv.so \
08c3a6
   $(shared-thread-library)
08c3a6
+$(objpfx)tst-resolv-noaaaa: $(objpfx)libresolv.so $(shared-thread-library)
08c3a6
 $(objpfx)tst-resolv-nondecimal: $(objpfx)libresolv.so $(shared-thread-library)
08c3a6
 $(objpfx)tst-resolv-qtypes: $(objpfx)libresolv.so $(shared-thread-library)
08c3a6
 $(objpfx)tst-resolv-rotate: $(objpfx)libresolv.so $(shared-thread-library)
08c3a6
diff --git a/resolv/nss_dns/dns-host.c b/resolv/nss_dns/dns-host.c
08c3a6
index 7248ade18db5ba47..6e83fca1c5b1f98c 100644
08c3a6
--- a/resolv/nss_dns/dns-host.c
08c3a6
+++ b/resolv/nss_dns/dns-host.c
08c3a6
@@ -125,6 +125,14 @@ static enum nss_status gaih_getanswer (const querybuf *answer1, int anslen1,
08c3a6
 				       char *buffer, size_t buflen,
08c3a6
 				       int *errnop, int *h_errnop,
08c3a6
 				       int32_t *ttlp);
08c3a6
+static enum nss_status gaih_getanswer_noaaaa (const querybuf *answer1,
08c3a6
+					      int anslen1,
08c3a6
+					      const char *qname,
08c3a6
+					      struct gaih_addrtuple **pat,
08c3a6
+					      char *buffer, size_t buflen,
08c3a6
+					      int *errnop, int *h_errnop,
08c3a6
+					      int32_t *ttlp);
08c3a6
+
08c3a6
 
08c3a6
 static enum nss_status gethostbyname3_context (struct resolv_context *ctx,
08c3a6
 					       const char *name, int af,
08c3a6
@@ -370,17 +378,31 @@ _nss_dns_gethostbyname4_r (const char *name, struct gaih_addrtuple **pat,
08c3a6
   int resplen2 = 0;
08c3a6
   int ans2p_malloced = 0;
08c3a6
 
08c3a6
+
08c3a6
   int olderr = errno;
08c3a6
-  int n = __res_context_search (ctx, name, C_IN, T_QUERY_A_AND_AAAA,
08c3a6
+  int n;
08c3a6
+
08c3a6
+  if ((ctx->resp->options & RES_NOAAAA) == 0)
08c3a6
+    {
08c3a6
+      n = __res_context_search (ctx, name, C_IN, T_QUERY_A_AND_AAAA,
08c3a6
 				host_buffer.buf->buf, 2048, &host_buffer.ptr,
08c3a6
 				&ans2p, &nans2p, &resplen2, &ans2p_malloced);
08c3a6
-  if (n >= 0)
08c3a6
-    {
08c3a6
-      status = gaih_getanswer (host_buffer.buf, n, (const querybuf *) ans2p,
08c3a6
-			       resplen2, name, pat, buffer, buflen,
08c3a6
-			       errnop, herrnop, ttlp);
08c3a6
+      if (n >= 0)
08c3a6
+	status = gaih_getanswer (host_buffer.buf, n, (const querybuf *) ans2p,
08c3a6
+				 resplen2, name, pat, buffer, buflen,
08c3a6
+				 errnop, herrnop, ttlp);
08c3a6
     }
08c3a6
   else
08c3a6
+    {
08c3a6
+      n = __res_context_search (ctx, name, C_IN, T_A,
08c3a6
+				host_buffer.buf->buf, 2048, NULL,
08c3a6
+				NULL, NULL, NULL, NULL);
08c3a6
+      if (n >= 0)
08c3a6
+	status = gaih_getanswer_noaaaa (host_buffer.buf, n,
08c3a6
+					name, pat, buffer, buflen,
08c3a6
+					errnop, herrnop, ttlp);
08c3a6
+    }
08c3a6
+  if (n < 0)
08c3a6
     {
08c3a6
       switch (errno)
08c3a6
 	{
08c3a6
@@ -1388,3 +1410,21 @@ gaih_getanswer (const querybuf *answer1, int anslen1, const querybuf *answer2,
08c3a6
 
08c3a6
   return status;
08c3a6
 }
08c3a6
+
08c3a6
+/* Variant of gaih_getanswer without a second (AAAA) response.  */
08c3a6
+static enum nss_status
08c3a6
+gaih_getanswer_noaaaa (const querybuf *answer1, int anslen1, const char *qname,
08c3a6
+		       struct gaih_addrtuple **pat,
08c3a6
+		       char *buffer, size_t buflen,
08c3a6
+		       int *errnop, int *h_errnop, int32_t *ttlp)
08c3a6
+{
08c3a6
+  int first = 1;
08c3a6
+
08c3a6
+  enum nss_status status = NSS_STATUS_NOTFOUND;
08c3a6
+  if (anslen1 > 0)
08c3a6
+    status = gaih_getanswer_slice (answer1, anslen1, qname,
08c3a6
+				   &pat, &buffer, &buflen,
08c3a6
+				   errnop, h_errnop, ttlp,
08c3a6
+				   &first);
08c3a6
+  return status;
08c3a6
+}
08c3a6
diff --git a/resolv/res-noaaaa.c b/resolv/res-noaaaa.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..4ba197664a86aed7
08c3a6
--- /dev/null
08c3a6
+++ b/resolv/res-noaaaa.c
08c3a6
@@ -0,0 +1,143 @@
08c3a6
+/* Implement suppression of AAAA queries.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <resolv.h>
08c3a6
+#include <string.h>
08c3a6
+#include <resolv-internal.h>
08c3a6
+#include <resolv_context.h>
08c3a6
+#include <arpa/nameser.h>
08c3a6
+
08c3a6
+/* Returns true if the question type at P matches EXPECTED, and the
08c3a6
+   class is IN.  */
08c3a6
+static bool
08c3a6
+qtype_matches (const unsigned char *p, int expected)
08c3a6
+{
08c3a6
+  /* This assumes that T_A/C_IN constants are less than 256, which
08c3a6
+     they are.  */
08c3a6
+  return p[0] == 0 && p[1] == expected && p[2] == 0 && p[3] == C_IN;
08c3a6
+}
08c3a6
+
08c3a6
+/* Handle RES_NOAAAA translation of AAAA queries.  To produce a Name
08c3a6
+   Error (NXDOMAIN) repsonse for domain names that do not exist, it is
08c3a6
+   still necessary to send a query.  Using question type A is a
08c3a6
+   conservative choice.  In the returned answer, it is necessary to
08c3a6
+   switch back the question type to AAAA.  */
08c3a6
+bool
08c3a6
+__res_handle_no_aaaa (struct resolv_context *ctx,
08c3a6
+                      const unsigned char *buf, int buflen,
08c3a6
+                      unsigned char *ans, int anssiz, int *result)
08c3a6
+{
08c3a6
+  /* AAAA mode is not active, or the query looks invalid (will not be
08c3a6
+     able to be parsed).  */
08c3a6
+  if ((ctx->resp->options & RES_NOAAAA) == 0
08c3a6
+      || buflen <= sizeof (HEADER))
08c3a6
+    return false;
08c3a6
+
08c3a6
+  /* The replacement A query is produced here.  */
08c3a6
+  struct
08c3a6
+  {
08c3a6
+    HEADER header;
08c3a6
+    unsigned char question[NS_MAXCDNAME + 4];
08c3a6
+  } replacement;
08c3a6
+  memcpy (&replacement.header, buf, sizeof (replacement.header));
08c3a6
+
08c3a6
+  if (replacement.header.qr
08c3a6
+      || replacement.header.opcode != 0
08c3a6
+      || replacement.header.rcode != 0
08c3a6
+      || ntohs (replacement.header.qdcount) != 1
08c3a6
+      || ntohs (replacement.header.ancount) != 0
08c3a6
+      || ntohs (replacement.header.nscount) != 0)
08c3a6
+    /* Not a well-formed question.  Let the core resolver code produce
08c3a6
+       the proper error.  */
08c3a6
+    return false;
08c3a6
+
08c3a6
+  /* Disable EDNS0.  */
08c3a6
+  replacement.header.arcount = htons (0);
08c3a6
+
08c3a6
+  /* Extract the QNAME.  */
08c3a6
+  int ret = __ns_name_unpack (buf, buf + buflen, buf + sizeof (HEADER),
08c3a6
+                              replacement.question, NS_MAXCDNAME);
08c3a6
+  if (ret < 0)
08c3a6
+    /* Format error.  */
08c3a6
+    return false;
08c3a6
+
08c3a6
+  /* Compute the end of the question name.  */
08c3a6
+  const unsigned char *after_question = buf + sizeof (HEADER) + ret;
08c3a6
+
08c3a6
+  /* Check that we are dealing with an AAAA query.  */
08c3a6
+  if (buf + buflen - after_question < 4
08c3a6
+      || !qtype_matches (after_question, T_AAAA))
08c3a6
+    return false;
08c3a6
+
08c3a6
+  /* Find the place to store the type/class data in the replacement
08c3a6
+     query.  */
08c3a6
+  after_question = replacement.question;
08c3a6
+  /* This cannot fail because __ns_name_unpack above produced a valid
08c3a6
+     domain name.  */
08c3a6
+  (void) __ns_name_skip (&after_question, &replacement.question[NS_MAXCDNAME]);
08c3a6
+  unsigned char *start_of_query = (unsigned char *) &replacement;
08c3a6
+  const unsigned char *end_of_query = after_question + 4;
08c3a6
+
08c3a6
+  /* Produce an A/IN query.  */
08c3a6
+  {
08c3a6
+    unsigned char *p = (unsigned char *) after_question;
08c3a6
+    p[0] = 0;
08c3a6
+    p[1] = T_A;
08c3a6
+    p[2] = 0;
08c3a6
+    p[3] = C_IN;
08c3a6
+  }
08c3a6
+
08c3a6
+  /* Clear the output buffer, to avoid reading undefined data when
08c3a6
+     rewriting the result from A to AAAA.  */
08c3a6
+  memset (ans, 0, anssiz);
08c3a6
+
08c3a6
+  /* Always perform the message translation, independent of the error
08c3a6
+     code.  */
08c3a6
+  ret = __res_context_send (ctx,
08c3a6
+                            start_of_query, end_of_query - start_of_query,
08c3a6
+                            NULL, 0, ans, anssiz,
08c3a6
+                            NULL, NULL, NULL, NULL, NULL);
08c3a6
+
08c3a6
+  /* Patch in the AAAA question type if there is room and the A query
08c3a6
+     type was received.  */
08c3a6
+  after_question = ans + sizeof (HEADER);
08c3a6
+  if (__ns_name_skip (&after_question, ans + anssiz) == 0
08c3a6
+      && ans + anssiz - after_question >= 4
08c3a6
+      && qtype_matches (after_question, T_A))
08c3a6
+    {
08c3a6
+      ((unsigned char *) after_question)[1] = T_AAAA;
08c3a6
+
08c3a6
+      /* Create an aligned copy of the header.  Hide all data except
08c3a6
+         the question from the response.  Put back the header.  There is
08c3a6
+         no need to change the response code.  The zero answer count turns
08c3a6
+         a positive response with data into a no-data response.  */
08c3a6
+      memcpy (&replacement.header, ans, sizeof (replacement.header));
08c3a6
+      replacement.header.ancount = htons (0);
08c3a6
+      replacement.header.nscount = htons (0);
08c3a6
+      replacement.header.arcount = htons (0);
08c3a6
+      memcpy (ans, &replacement.header, sizeof (replacement.header));
08c3a6
+
08c3a6
+      /* Truncate the reply.  */
08c3a6
+      if (ret <= 0)
08c3a6
+        *result = ret;
08c3a6
+      else
08c3a6
+        *result = after_question - ans + 4;
08c3a6
+    }
08c3a6
+
08c3a6
+  return true;
08c3a6
+}
08c3a6
diff --git a/resolv/res_debug.c b/resolv/res_debug.c
08c3a6
index 030df0aa90c9f34f..b0fe69b1aa5186a0 100644
08c3a6
--- a/resolv/res_debug.c
08c3a6
+++ b/resolv/res_debug.c
08c3a6
@@ -613,6 +613,7 @@ p_option(u_long option) {
08c3a6
 	case RES_NOTLDQUERY:	return "no-tld-query";
08c3a6
 	case RES_NORELOAD:	return "no-reload";
08c3a6
 	case RES_TRUSTAD:	return "trust-ad";
08c3a6
+	case RES_NOAAAA:	return "no-aaaa";
08c3a6
 				/* XXX nonreentrant */
08c3a6
 	default:		sprintf(nbuf, "?0x%lx?", (u_long)option);
08c3a6
 				return (nbuf);
08c3a6
diff --git a/resolv/res_init.c b/resolv/res_init.c
08c3a6
index 6b2936eda9618ac9..8bde915903565f60 100644
08c3a6
--- a/resolv/res_init.c
08c3a6
+++ b/resolv/res_init.c
08c3a6
@@ -695,6 +695,7 @@ res_setoptions (struct resolv_conf_parser *parser, const char *options)
08c3a6
             { STRnLEN ("no-reload"), 0, RES_NORELOAD },
08c3a6
             { STRnLEN ("use-vc"), 0, RES_USEVC },
08c3a6
             { STRnLEN ("trust-ad"), 0, RES_TRUSTAD },
08c3a6
+            { STRnLEN ("no-aaaa"), 0, RES_NOAAAA },
08c3a6
           };
08c3a6
 #define noptions (sizeof (options) / sizeof (options[0]))
08c3a6
           for (int i = 0; i < noptions; ++i)
08c3a6
diff --git a/resolv/res_query.c b/resolv/res_query.c
08c3a6
index 75b0e5f2f7b51eb2..2f3c28cfc8c0d832 100644
08c3a6
--- a/resolv/res_query.c
08c3a6
+++ b/resolv/res_query.c
08c3a6
@@ -204,10 +204,26 @@ __res_context_query (struct resolv_context *ctx, const char *name,
08c3a6
 			free (buf);
08c3a6
 		return (n);
08c3a6
 	}
08c3a6
-	assert (answerp == NULL || (void *) *answerp == (void *) answer);
08c3a6
-	n = __res_context_send (ctx, query1, nquery1, query2, nquery2, answer,
08c3a6
-				anslen, answerp, answerp2, nanswerp2, resplen2,
08c3a6
-				answerp2_malloced);
08c3a6
+
08c3a6
+	/* Suppress AAAA lookups if required.  __res_handle_no_aaaa
08c3a6
+	   checks RES_NOAAAA first, so avoids parsing the
08c3a6
+	   just-generated query packet in most cases.  nss_dns avoids
08c3a6
+	   using T_QUERY_A_AND_AAAA in RES_NOAAAA mode, so there is no
08c3a6
+	   need to handle it here.  */
08c3a6
+	if (type == T_AAAA && __res_handle_no_aaaa (ctx, query1, nquery1,
08c3a6
+						    answer, anslen, &n))
08c3a6
+	  /* There must be no second query for AAAA queries.  The code
08c3a6
+	     below is still needed to translate NODATA responses.  */
08c3a6
+	  assert (query2 == NULL);
08c3a6
+	else
08c3a6
+	  {
08c3a6
+	    assert (answerp == NULL || (void *) *answerp == (void *) answer);
08c3a6
+	    n = __res_context_send (ctx, query1, nquery1, query2, nquery2,
08c3a6
+				    answer, anslen,
08c3a6
+				    answerp, answerp2, nanswerp2, resplen2,
08c3a6
+				    answerp2_malloced);
08c3a6
+	  }
08c3a6
+
08c3a6
 	if (use_malloc)
08c3a6
 		free (buf);
08c3a6
 	if (n < 0) {
08c3a6
diff --git a/resolv/res_send.c b/resolv/res_send.c
08c3a6
index 9f86f5fe47194887..8ac6a307b40fa2ca 100644
08c3a6
--- a/resolv/res_send.c
08c3a6
+++ b/resolv/res_send.c
08c3a6
@@ -438,8 +438,13 @@ context_send_common (struct resolv_context *ctx,
08c3a6
       RES_SET_H_ERRNO (&_res, NETDB_INTERNAL);
08c3a6
       return -1;
08c3a6
     }
08c3a6
-  int result = __res_context_send (ctx, buf, buflen, NULL, 0, ans, anssiz,
08c3a6
-				   NULL, NULL, NULL, NULL, NULL);
08c3a6
+
08c3a6
+  int result;
08c3a6
+  if (__res_handle_no_aaaa (ctx, buf, buflen, ans, anssiz, &result))
08c3a6
+    return result;
08c3a6
+
08c3a6
+  result = __res_context_send (ctx, buf, buflen, NULL, 0, ans, anssiz,
08c3a6
+			       NULL, NULL, NULL, NULL, NULL);
08c3a6
   __resolv_context_put (ctx);
08c3a6
   return result;
08c3a6
 }
08c3a6
diff --git a/resolv/resolv-internal.h b/resolv/resolv-internal.h
08c3a6
index 216e47ed42076b72..8ab02fc9e648d30f 100644
08c3a6
--- a/resolv/resolv-internal.h
08c3a6
+++ b/resolv/resolv-internal.h
08c3a6
@@ -78,6 +78,14 @@ int __res_context_send (struct resolv_context *, const unsigned char *, int,
08c3a6
                         int *, int *, int *);
08c3a6
 libc_hidden_proto (__res_context_send)
08c3a6
 
08c3a6
+/* Return true if the query has been handled in RES_NOAAAA mode.  For
08c3a6
+   that, RES_NOAAAA must be active, and the question type must be AAAA.
08c3a6
+   The caller is expected to return *RESULT as the return value.  */
08c3a6
+bool __res_handle_no_aaaa (struct resolv_context *ctx,
08c3a6
+                           const unsigned char *buf, int buflen,
08c3a6
+                           unsigned char *ans, int anssiz, int *result)
08c3a6
+  attribute_hidden;
08c3a6
+
08c3a6
 /* Internal function similar to res_hostalias.  */
08c3a6
 const char *__res_context_hostalias (struct resolv_context *,
08c3a6
                                      const char *, char *, size_t);
08c3a6
diff --git a/resolv/resolv.h b/resolv/resolv.h
08c3a6
index e7c8d44645912ddf..3a79ffea57a6916f 100644
08c3a6
--- a/resolv/resolv.h
08c3a6
+++ b/resolv/resolv.h
08c3a6
@@ -132,6 +132,7 @@ struct res_sym {
08c3a6
 					   as a TLD.  */
08c3a6
 #define RES_NORELOAD    0x02000000 /* No automatic configuration reload.  */
08c3a6
 #define RES_TRUSTAD     0x04000000 /* Request AD bit, keep it in responses.  */
08c3a6
+#define RES_NOAAAA      0x08000000 /* Suppress AAAA queries.  */
08c3a6
 
08c3a6
 #define RES_DEFAULT	(RES_RECURSE|RES_DEFNAMES|RES_DNSRCH)
08c3a6
 
08c3a6
diff --git a/resolv/tst-resolv-noaaaa.c b/resolv/tst-resolv-noaaaa.c
08c3a6
new file mode 100644
08c3a6
index 0000000000000000..56b25f88a58ad286
08c3a6
--- /dev/null
08c3a6
+++ b/resolv/tst-resolv-noaaaa.c
08c3a6
@@ -0,0 +1,533 @@
08c3a6
+/* Test the RES_NOAAAA resolver option.
08c3a6
+   Copyright (C) 2022 Free Software Foundation, Inc.
08c3a6
+   This file is part of the GNU C Library.
08c3a6
+
08c3a6
+   The GNU C Library is free software; you can redistribute it and/or
08c3a6
+   modify it under the terms of the GNU Lesser General Public
08c3a6
+   License as published by the Free Software Foundation; either
08c3a6
+   version 2.1 of the License, or (at your option) any later version.
08c3a6
+
08c3a6
+   The GNU C Library is distributed in the hope that it will be useful,
08c3a6
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
08c3a6
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
08c3a6
+   Lesser General Public License for more details.
08c3a6
+
08c3a6
+   You should have received a copy of the GNU Lesser General Public
08c3a6
+   License along with the GNU C Library; if not, see
08c3a6
+   <https://www.gnu.org/licenses/>.  */
08c3a6
+
08c3a6
+#include <errno.h>
08c3a6
+#include <netdb.h>
08c3a6
+#include <resolv.h>
08c3a6
+#include <stdlib.h>
08c3a6
+#include <support/check.h>
08c3a6
+#include <support/check_nss.h>
08c3a6
+#include <support/resolv_test.h>
08c3a6
+#include <support/support.h>
08c3a6
+
08c3a6
+/* Used to keep track of the number of queries.  */
08c3a6
+static volatile unsigned int queries;
08c3a6
+
08c3a6
+static void
08c3a6
+response (const struct resolv_response_context *ctx,
08c3a6
+          struct resolv_response_builder *b,
08c3a6
+          const char *qname, uint16_t qclass, uint16_t qtype)
08c3a6
+{
08c3a6
+  /* Each test should only send one query.  */
08c3a6
+  ++queries;
08c3a6
+  TEST_COMPARE (queries, 1);
08c3a6
+
08c3a6
+  /* AAAA queries are supposed to be disabled.  */
08c3a6
+  TEST_VERIFY (qtype != T_AAAA);
08c3a6
+  TEST_COMPARE (qclass, C_IN);
08c3a6
+
08c3a6
+  /* The only other query type besides A is PTR.  */
08c3a6
+  if (qtype != T_A)
08c3a6
+    TEST_COMPARE (qtype, T_PTR);
08c3a6
+
08c3a6
+  int an, ns, ar;
08c3a6
+  char *tail;
08c3a6
+  if (sscanf (qname, "an%d.ns%d.ar%d.%ms", &an, &ns, &ar, &tail) != 4)
08c3a6
+    FAIL_EXIT1 ("invalid QNAME: %s\n", qname);
08c3a6
+  TEST_COMPARE_STRING (tail, "example");
08c3a6
+  free (tail);
08c3a6
+
08c3a6
+  if (an < 0 || ns < 0 || ar < 0)
08c3a6
+    {
08c3a6
+      struct resolv_response_flags flags = { .rcode = NXDOMAIN, };
08c3a6
+      resolv_response_init (b, flags);
08c3a6
+      resolv_response_add_question (b, qname, qclass, qtype);
08c3a6
+      return;
08c3a6
+    }
08c3a6
+
08c3a6
+  struct resolv_response_flags flags = {};
08c3a6
+  resolv_response_init (b, flags);
08c3a6
+  resolv_response_add_question (b, qname, qclass, qtype);
08c3a6
+
08c3a6
+  resolv_response_section (b, ns_s_an);
08c3a6
+  for (int i = 0; i < an; ++i)
08c3a6
+    {
08c3a6
+      resolv_response_open_record (b, qname, qclass, qtype, 60);
08c3a6
+      switch (qtype)
08c3a6
+        {
08c3a6
+        case T_A:
08c3a6
+          char ipv4[4] = {192, 0, 2, i + 1};
08c3a6
+          resolv_response_add_data (b, &ipv4, sizeof (ipv4));
08c3a6
+          break;
08c3a6
+
08c3a6
+        case T_PTR:
08c3a6
+          char *name = xasprintf ("ptr-%d", i);
08c3a6
+          resolv_response_add_name (b, name);
08c3a6
+          free (name);
08c3a6
+          break;
08c3a6
+        }
08c3a6
+      resolv_response_close_record (b);
08c3a6
+    }
08c3a6
+
08c3a6
+  resolv_response_section (b, ns_s_ns);
08c3a6
+  for (int i = 0; i < ns; ++i)
08c3a6
+    {
08c3a6
+      resolv_response_open_record (b, qname, qclass, T_NS, 60);
08c3a6
+      char *name = xasprintf ("ns%d.example.net", i);
08c3a6
+      resolv_response_add_name (b, name);
08c3a6
+      free (name);
08c3a6
+      resolv_response_close_record (b);
08c3a6
+    }
08c3a6
+
08c3a6
+  resolv_response_section (b, ns_s_ar);
08c3a6
+  int addr = 1;
08c3a6
+  for (int i = 0; i < ns; ++i)
08c3a6
+    {
08c3a6
+      char *name = xasprintf ("ns%d.example.net", i);
08c3a6
+      for (int j = 0; j < ar; ++j)
08c3a6
+        {
08c3a6
+          resolv_response_open_record (b, name, qclass, T_A, 60);
08c3a6
+          char ipv4[4] = {192, 0, 2, addr};
08c3a6
+          resolv_response_add_data (b, &ipv4, sizeof (ipv4));
08c3a6
+          resolv_response_close_record (b);
08c3a6
+
08c3a6
+          resolv_response_open_record (b, name, qclass, T_AAAA, 60);
08c3a6
+          char ipv6[16]
08c3a6
+            = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, addr};
08c3a6
+          resolv_response_add_data (b, &ipv6, sizeof (ipv6));
08c3a6
+          resolv_response_close_record (b);
08c3a6
+
08c3a6
+          ++addr;
08c3a6
+        }
08c3a6
+      free (name);
08c3a6
+    }
08c3a6
+}
08c3a6
+
08c3a6
+/* Number of modes.  Lowest bit encodes *n* function vs implicit _res
08c3a6
+   argument.  The mode numbers themselves are arbitrary.  */
08c3a6
+enum { mode_count = 8 };
08c3a6
+
08c3a6
+/* res_send-like modes do not perform error translation.  */
08c3a6
+enum { first_send_mode = 6 };
08c3a6
+
08c3a6
+static int
08c3a6
+libresolv_query (unsigned int mode, const char *qname, uint16_t qtype,
08c3a6
+                 unsigned char *buf, size_t buflen)
08c3a6
+{
08c3a6
+  int saved_errno = errno;
08c3a6
+
08c3a6
+  TEST_VERIFY_EXIT (mode < mode_count);
08c3a6
+
08c3a6
+  switch (mode)
08c3a6
+    {
08c3a6
+    case 0:
08c3a6
+      return res_query (qname, C_IN, qtype, buf, buflen);
08c3a6
+    case 1:
08c3a6
+      return res_nquery (&_res, qname, C_IN, qtype, buf, buflen);
08c3a6
+    case 2:
08c3a6
+      return res_search (qname, C_IN, qtype, buf, buflen);
08c3a6
+    case 3:
08c3a6
+      return res_nsearch (&_res, qname, C_IN, qtype, buf, buflen);
08c3a6
+    case 4:
08c3a6
+      return res_querydomain (qname, "", C_IN, qtype, buf, buflen);
08c3a6
+    case 5:
08c3a6
+      return res_nquerydomain (&_res, qname, "", C_IN, qtype, buf, buflen);
08c3a6
+    case 6:
08c3a6
+      {
08c3a6
+        unsigned char querybuf[512];
08c3a6
+        int ret = res_mkquery (QUERY, qname, C_IN, qtype,
08c3a6
+                               NULL, 0, NULL, querybuf, sizeof (querybuf));
08c3a6
+        TEST_VERIFY_EXIT (ret > 0);
08c3a6
+        errno = saved_errno;
08c3a6
+        return res_send (querybuf, ret, buf, buflen);
08c3a6
+      }
08c3a6
+    case 7:
08c3a6
+      {
08c3a6
+        unsigned char querybuf[512];
08c3a6
+        int ret = res_nmkquery (&_res, QUERY, qname, C_IN, qtype,
08c3a6
+                                NULL, 0, NULL, querybuf, sizeof (querybuf));
08c3a6
+        TEST_VERIFY_EXIT (ret > 0);
08c3a6
+        errno = saved_errno;
08c3a6
+        return res_nsend (&_res, querybuf, ret, buf, buflen);
08c3a6
+      }
08c3a6
+    }
08c3a6
+  __builtin_unreachable ();
08c3a6
+}
08c3a6
+
08c3a6
+static int
08c3a6
+do_test (void)
08c3a6
+{
08c3a6
+  struct resolv_test *obj = resolv_test_start
08c3a6
+    ((struct resolv_redirect_config)
08c3a6
+     {
08c3a6
+       .response_callback = response
08c3a6
+     });
08c3a6
+
08c3a6
+  _res.options |= RES_NOAAAA;
08c3a6
+
08c3a6
+  check_hostent ("an1.ns2.ar1.example",
08c3a6
+                 gethostbyname ("an1.ns2.ar1.example"),
08c3a6
+                 "name: an1.ns2.ar1.example\n"
08c3a6
+                 "address: 192.0.2.1\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an0.ns2.ar1.example",
08c3a6
+                 gethostbyname ("an0.ns2.ar1.example"),
08c3a6
+                 "error: NO_ADDRESS\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an-1.ns2.ar1.example",
08c3a6
+                 gethostbyname ("an-1.ns2.ar1.example"),
08c3a6
+                 "error: HOST_NOT_FOUND\n");
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  check_hostent ("an1.ns2.ar1.example AF_INET",
08c3a6
+                 gethostbyname2 ("an1.ns2.ar1.example", AF_INET),
08c3a6
+                 "name: an1.ns2.ar1.example\n"
08c3a6
+                 "address: 192.0.2.1\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an0.ns2.ar1.example AF_INET",
08c3a6
+                 gethostbyname2 ("an0.ns2.ar1.example", AF_INET),
08c3a6
+                 "error: NO_ADDRESS\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an-1.ns2.ar1.example AF_INET",
08c3a6
+                 gethostbyname2 ("an-1.ns2.ar1.example", AF_INET),
08c3a6
+                 "error: HOST_NOT_FOUND\n");
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  check_hostent ("an1.ns2.ar1.example AF_INET6",
08c3a6
+                 gethostbyname2 ("an1.ns2.ar1.example", AF_INET6),
08c3a6
+                 "error: NO_ADDRESS\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an0.ns2.ar1.example AF_INET6",
08c3a6
+                 gethostbyname2 ("an0.ns2.ar1.example", AF_INET6),
08c3a6
+                 "error: NO_ADDRESS\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an-1.ns2.ar1.example AF_INET6",
08c3a6
+                 gethostbyname2 ("an-1.ns2.ar1.example", AF_INET6),
08c3a6
+                 "error: HOST_NOT_FOUND\n");
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  /* Multiple addresses.  */
08c3a6
+  check_hostent ("an2.ns0.ar0.example",
08c3a6
+                 gethostbyname ("an2.ns0.ar0.example"),
08c3a6
+                 "name: an2.ns0.ar0.example\n"
08c3a6
+                 "address: 192.0.2.1\n"
08c3a6
+                 "address: 192.0.2.2\n");
08c3a6
+  queries = 0;
08c3a6
+  check_hostent ("an2.ns0.ar0.example AF_INET6",
08c3a6
+                 gethostbyname2 ("an2.ns0.ar0.example", AF_INET6),
08c3a6
+                 "error: NO_ADDRESS\n");
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  /* getaddrinfo checks with one address.  */
08c3a6
+  struct addrinfo *ai;
08c3a6
+  int ret;
08c3a6
+  ret = getaddrinfo ("an1.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an1.ns2.ar1.example (AF_INET)", ai, ret,
08c3a6
+                  "address: STREAM/TCP 192.0.2.1 80\n");
08c3a6
+  freeaddrinfo (ai);
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an1.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET6,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an1.ns2.ar1.example (AF_INET6)", ai, ret,
08c3a6
+                  "error: No address associated with hostname\n");
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an1.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_UNSPEC,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an1.ns2.ar1.example (AF_UNSPEC)", ai, ret,
08c3a6
+                  "address: STREAM/TCP 192.0.2.1 80\n");
08c3a6
+  freeaddrinfo (ai);
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  /* getaddrinfo checks with three addresses.  */
08c3a6
+  ret = getaddrinfo ("an3.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an3.ns2.ar1.example (AF_INET)", ai, ret,
08c3a6
+                  "address: STREAM/TCP 192.0.2.1 80\n"
08c3a6
+                  "address: STREAM/TCP 192.0.2.2 80\n"
08c3a6
+                  "address: STREAM/TCP 192.0.2.3 80\n");
08c3a6
+  freeaddrinfo (ai);
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an3.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET6,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an3.ns2.ar1.example (AF_INET6)", ai, ret,
08c3a6
+                  "error: No address associated with hostname\n");
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an3.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_UNSPEC,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an3.ns2.ar1.example (AF_UNSPEC)", ai, ret,
08c3a6
+                  "address: STREAM/TCP 192.0.2.1 80\n"
08c3a6
+                  "address: STREAM/TCP 192.0.2.2 80\n"
08c3a6
+                  "address: STREAM/TCP 192.0.2.3 80\n");
08c3a6
+  freeaddrinfo (ai);
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  /* getaddrinfo checks with no address.  */
08c3a6
+  ret = getaddrinfo ("an0.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an0.ns2.ar1.example (AF_INET)", ai, ret,
08c3a6
+                  "error: No address associated with hostname\n");
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an0.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET6,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an0.ns2.ar1.example (AF_INET6)", ai, ret,
08c3a6
+                  "error: No address associated with hostname\n");
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an0.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_UNSPEC,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an-1.ns2.ar1.example (AF_UNSPEC)", ai, ret,
08c3a6
+                  "error: No address associated with hostname\n");
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  /* getaddrinfo checks with NXDOMAIN.  */
08c3a6
+  ret = getaddrinfo ("an-1.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an-1.ns2.ar1.example (AF_INET)", ai, ret,
08c3a6
+                  "error: Name or service not known\n");
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an-1.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_INET6,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an-1.ns2.ar1.example (AF_INET6)", ai, ret,
08c3a6
+                  "error: Name or service not known\n");
08c3a6
+  queries = 0;
08c3a6
+  ret = getaddrinfo ("an-1.ns2.ar1.example", "80",
08c3a6
+                     &(struct addrinfo)
08c3a6
+                     {
08c3a6
+                       .ai_family = AF_UNSPEC,
08c3a6
+                       .ai_socktype = SOCK_STREAM,
08c3a6
+                     }, &ai;;
08c3a6
+  check_addrinfo ("an-1.ns2.ar1.example (AF_UNSPEC)", ai, ret,
08c3a6
+                  "error: Name or service not known\n");
08c3a6
+  queries = 0;
08c3a6
+
08c3a6
+  for (unsigned int mode = 0; mode < mode_count; ++mode)
08c3a6
+    {
08c3a6
+      unsigned char *buf;
08c3a6
+      int ret;
08c3a6
+
08c3a6
+      /* Response for A.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      ret = libresolv_query (mode, "an1.ns2.ar1.example", T_A, buf, 512);
08c3a6
+      TEST_VERIFY_EXIT (ret > 0);
08c3a6
+      check_dns_packet ("an1.ns2.ar1.example A", buf, ret,
08c3a6
+                        "name: an1.ns2.ar1.example\n"
08c3a6
+                        "address: 192.0.2.1\n");
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NODATA response for A.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an0.ns2.ar1.example", T_A, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, NO_ADDRESS);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, 0);
08c3a6
+          check_dns_packet ("an1.ns2.ar1.example A", buf, ret,
08c3a6
+                            "name: an0.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NXDOMAIN response for A.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an-1.ns2.ar1.example", T_A, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, HOST_NOT_FOUND);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, NXDOMAIN);
08c3a6
+          check_dns_packet ("an1.ns2.ar1.example A", buf, ret,
08c3a6
+                            "name: an-1.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* Response for PTR.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      ret = libresolv_query (mode, "an1.ns2.ar1.example", T_PTR, buf, 512);
08c3a6
+      TEST_VERIFY_EXIT (ret > 0);
08c3a6
+      check_dns_packet ("an1.ns2.ar1.example PTR", buf, ret,
08c3a6
+                        "name: an1.ns2.ar1.example\n"
08c3a6
+                        "data: an1.ns2.ar1.example PTR ptr-0\n");
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NODATA response for PTR.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an0.ns2.ar1.example", T_PTR, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, NO_ADDRESS);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, 0);
08c3a6
+          check_dns_packet ("an1.ns2.ar1.example PTR", buf, ret,
08c3a6
+                            "name: an0.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NXDOMAIN response for PTR.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an-1.ns2.ar1.example", T_PTR, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, HOST_NOT_FOUND);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, NXDOMAIN);
08c3a6
+          check_dns_packet ("an1.ns2.ar1.example PTR", buf, ret,
08c3a6
+                            "name: an-1.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NODATA response for AAAA.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an1.ns2.ar1.example", T_AAAA, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, NO_ADDRESS);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, 0);
08c3a6
+          check_dns_packet ("an1.ns2.ar1.example A", buf, ret,
08c3a6
+                            "name: an1.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NODATA response for AAAA (original is already NODATA).  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an0.ns2.ar1.example", T_AAAA, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, NO_ADDRESS);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, 0);
08c3a6
+          check_dns_packet ("an0.ns2.ar1.example A", buf, ret,
08c3a6
+                            "name: an0.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+
08c3a6
+      /* NXDOMAIN response.  */
08c3a6
+      buf = malloc (512);
08c3a6
+      errno = 0;
08c3a6
+      ret = libresolv_query (mode, "an-1.ns2.ar1.example", T_AAAA, buf, 512);
08c3a6
+      if (mode < first_send_mode)
08c3a6
+        {
08c3a6
+          TEST_COMPARE (ret, -1);
08c3a6
+          TEST_COMPARE (errno, 0);
08c3a6
+          TEST_COMPARE (h_errno, HOST_NOT_FOUND);
08c3a6
+        }
08c3a6
+      else
08c3a6
+        {
08c3a6
+          TEST_VERIFY_EXIT (ret > 0);
08c3a6
+          TEST_COMPARE (((HEADER *)buf)->rcode, NXDOMAIN);
08c3a6
+          check_dns_packet ("an-1.ns2.ar1.example A", buf, ret,
08c3a6
+                            "name: an-1.ns2.ar1.example\n");
08c3a6
+        }
08c3a6
+      free (buf);
08c3a6
+      queries = 0;
08c3a6
+    }
08c3a6
+
08c3a6
+  resolv_test_end (obj);
08c3a6
+
08c3a6
+  return 0;
08c3a6
+}
08c3a6
+
08c3a6
+#include <support/test-driver.c>
08c3a6
diff --git a/resolv/tst-resolv-res_init-skeleton.c b/resolv/tst-resolv-res_init-skeleton.c
08c3a6
index c87596762fcb23b1..28ed9c2eb150532d 100644
08c3a6
--- a/resolv/tst-resolv-res_init-skeleton.c
08c3a6
+++ b/resolv/tst-resolv-res_init-skeleton.c
08c3a6
@@ -128,6 +128,7 @@ print_resp (FILE *fp, res_state resp)
08c3a6
         print_option_flag (fp, &options, RES_NOTLDQUERY, "no-tld-query");
08c3a6
         print_option_flag (fp, &options, RES_NORELOAD, "no-reload");
08c3a6
         print_option_flag (fp, &options, RES_TRUSTAD, "trust-ad");
08c3a6
+        print_option_flag (fp, &options, RES_NOAAAA, "no-aaaa");
08c3a6
         fputc ('\n', fp);
08c3a6
         if (options != 0)
08c3a6
           fprintf (fp, "; error: unresolved option bits: 0x%x\n", options);
08c3a6
@@ -721,6 +722,15 @@ struct test_case test_cases[] =
08c3a6
      "nameserver 192.0.2.1\n"
08c3a6
      "; nameserver[0]: [192.0.2.1]:53\n"
08c3a6
     },
08c3a6
+    {.name = "no-aaaa flag",
08c3a6
+     .conf = "options no-aaaa\n"
08c3a6
+     "nameserver 192.0.2.1\n",
08c3a6
+     .expected = "options no-aaaa\n"
08c3a6
+     "search example.com\n"
08c3a6
+     "; search[0]: example.com\n"
08c3a6
+     "nameserver 192.0.2.1\n"
08c3a6
+     "; nameserver[0]: [192.0.2.1]:53\n"
08c3a6
+    },
08c3a6
     { NULL }
08c3a6
   };
08c3a6