1feee8
commit e7c03f47651bd451ebf2c3c65899491d0bf7167e
1feee8
Author: Florian Weimer <fweimer@redhat.com>
1feee8
Date:   Tue Aug 30 10:02:49 2022 +0200
1feee8
1feee8
    resolv: Add DNS packet parsing helpers geared towards wire format
1feee8
    
1feee8
    The public parser functions around the ns_rr record type produce
1feee8
    textual domain names, but usually, this is not what we need while
1feee8
    parsing DNS packets within glibc.  This commit adds two new helper
1feee8
    functions, __ns_rr_cursor_init and __ns_rr_cursor_next, for writing
1feee8
    packet parsers, and struct ns_rr_cursor, struct ns_rr_wire as
1feee8
    supporting types.
1feee8
    
1feee8
    In theory, it is possible to avoid copying the owner name
1feee8
    into the rname field in __ns_rr_cursor_next, but this would need
1feee8
    more functions that work on compressed names.
1feee8
    
1feee8
    Eventually, __res_context_send could be enhanced to preserve the
1feee8
    result of the packet parsing that is necessary for matching the
1feee8
    incoming UDP packets, so that this works does not have to be done
1feee8
    twice.
1feee8
    
1feee8
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
1feee8
    (cherry picked from commit 857c890d9b42c50c8a94b76d47d4a61ab6d2f49c)
1feee8
1feee8
diff --git a/include/arpa/nameser.h b/include/arpa/nameser.h
1feee8
index 6e4808f00d60caf9..c27e7886b7891997 100644
1feee8
--- a/include/arpa/nameser.h
1feee8
+++ b/include/arpa/nameser.h
1feee8
@@ -103,5 +103,97 @@ libc_hidden_proto (__libc_ns_samename)
1feee8
    must point one past the last byte in the packet.  */
1feee8
 int __ns_name_length_uncompressed (const unsigned char *p,
1feee8
 				   const unsigned char *eom) attribute_hidden;
1feee8
+
1feee8
+/* Iterator over the resource records in a DNS packet.  */
1feee8
+struct ns_rr_cursor
1feee8
+{
1feee8
+  /* These members are not changed after initialization.  */
1feee8
+  const unsigned char *begin;	/* First byte of packet.  */
1feee8
+  const unsigned char *end;	/* One past the last byte of the packet.  */
1feee8
+  const unsigned char *first_rr; /* First resource record (or packet end).  */
1feee8
+
1feee8
+  /* Advanced towards the end while reading the packet.  */
1feee8
+  const unsigned char *current;
1feee8
+};
1feee8
+
1feee8
+/* Returns the RCODE field from the DNS header.  */
1feee8
+static inline int
1feee8
+ns_rr_cursor_rcode (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  return c->begin[3] & 0x0f;	/* Lower 4 bits at offset 3.  */
1feee8
+}
1feee8
+
1feee8
+/* Returns the length of the answer section according to the DNS header.  */
1feee8
+static inline int
1feee8
+ns_rr_cursor_ancount (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  return c->begin[6] * 256 + c->begin[7]; /* 16 bits at offset 6.  */
1feee8
+}
1feee8
+
1feee8
+/* Returns the length of the authority (name server) section according
1feee8
+   to the DNS header.  */
1feee8
+static inline int
1feee8
+ns_rr_cursor_nscount (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  return c->begin[8] * 256 + c->begin[9]; /* 16 bits at offset 8.  */
1feee8
+}
1feee8
+
1feee8
+/* Returns the length of the additional data section according to the
1feee8
+   DNS header.  */
1feee8
+static inline int
1feee8
+ns_rr_cursor_adcount (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  return c->begin[10] * 256 + c->begin[11]; /* 16 bits at offset 10.  */
1feee8
+}
1feee8
+
1feee8
+/* Returns a pointer to the uncompressed question name in wire
1feee8
+   format.  */
1feee8
+static inline const unsigned char *
1feee8
+ns_rr_cursor_qname (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  return c->begin + 12;		/* QNAME starts right after the header.  */
1feee8
+}
1feee8
+
1feee8
+/* Returns the question type of the first and only question.  */
1feee8
+static inline const int
1feee8
+ns_rr_cursor_qtype (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  /* 16 bits 4 bytes back from the first RR header start.  */
1feee8
+  return c->first_rr[-4] * 256 + c->first_rr[-3];
1feee8
+}
1feee8
+
1feee8
+/* Returns the clss of the first and only question (usally C_IN).  */
1feee8
+static inline const int
1feee8
+ns_rr_cursor_qclass (const struct ns_rr_cursor *c)
1feee8
+{
1feee8
+  /* 16 bits 2 bytes back from the first RR header start.  */
1feee8
+  return c->first_rr[-2] * 256 + c->first_rr[-1];
1feee8
+}
1feee8
+
1feee8
+/* Initializes *C to cover the packet [BUF, BUF+LEN).  Returns false
1feee8
+   if LEN is less than sizeof (*HD), if the packet does not contain a
1feee8
+   full (uncompressed) question, or if the question count is not 1.  */
1feee8
+_Bool __ns_rr_cursor_init (struct ns_rr_cursor *c,
1feee8
+			   const unsigned char *buf, size_t len)
1feee8
+  attribute_hidden;
1feee8
+
1feee8
+/* Like ns_rr, but the record owner name is not decoded into text format.  */
1feee8
+struct ns_rr_wire
1feee8
+{
1feee8
+  unsigned char rname[NS_MAXCDNAME]; /* Owner name of the record.  */
1feee8
+  uint16_t rtype;		/* Resource record type (T_*).  */
1feee8
+  uint16_t rclass;		/* Resource record class (C_*).  */
1feee8
+  uint32_t ttl;			/* Time-to-live field.  */
1feee8
+  const unsigned char *rdata;	/* Start of resource record data.  */
1feee8
+  uint16_t rdlength;		/* Length of the data at rdata, in bytes.  */
1feee8
+};
1feee8
+
1feee8
+/* Attempts to parse the record at C into *RR.  On success, return
1feee8
+   true, and C is advanced past the record, and RR->rdata points to
1feee8
+   the record data.  On failure, errno is set to EMSGSIZE, and false
1feee8
+   is returned.  */
1feee8
+_Bool __ns_rr_cursor_next (struct ns_rr_cursor *c, struct ns_rr_wire *rr)
1feee8
+  attribute_hidden;
1feee8
+
1feee8
 # endif /* !_ISOMAC */
1feee8
 #endif
1feee8
diff --git a/resolv/Makefile b/resolv/Makefile
1feee8
index 308f18622a04965a..fded244d61068060 100644
1feee8
--- a/resolv/Makefile
1feee8
+++ b/resolv/Makefile
1feee8
@@ -47,6 +47,8 @@ routines := \
1feee8
   ns_name_skip \
1feee8
   ns_name_uncompress \
1feee8
   ns_name_unpack \
1feee8
+  ns_rr_cursor_init \
1feee8
+  ns_rr_cursor_next \
1feee8
   ns_samebinaryname \
1feee8
   ns_samename \
1feee8
   nsap_addr \
1feee8
@@ -117,6 +119,10 @@ tests-static += tst-ns_samebinaryname
1feee8
 tests-internal += tst-ns_name_length_uncompressed
1feee8
 tests-static += tst-ns_name_length_uncompressed
1feee8
 
1feee8
+# Likewise for struct ns_rr_cursor and its functions.
1feee8
+tests-internal += tst-ns_rr_cursor
1feee8
+tests-static += tst-ns_rr_cursor
1feee8
+
1feee8
 # These tests need libdl.
1feee8
 ifeq (yes,$(build-shared))
1feee8
 tests += \
1feee8
diff --git a/resolv/ns_rr_cursor_init.c b/resolv/ns_rr_cursor_init.c
1feee8
new file mode 100644
1feee8
index 0000000000000000..6ee80b30e927ecb7
1feee8
--- /dev/null
1feee8
+++ b/resolv/ns_rr_cursor_init.c
1feee8
@@ -0,0 +1,62 @@
1feee8
+/* Initialize a simple DNS packet parser.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#include <arpa/nameser.h>
1feee8
+#include <errno.h>
1feee8
+#include <stdbool.h>
1feee8
+#include <string.h>
1feee8
+
1feee8
+bool
1feee8
+__ns_rr_cursor_init (struct ns_rr_cursor *c,
1feee8
+                     const unsigned char *buf, size_t len)
1feee8
+{
1feee8
+  c->begin = buf;
1feee8
+  c->end = buf + len;
1feee8
+
1feee8
+  /* Check for header size and 16-bit question count value (it must be 1).  */
1feee8
+  if (len < 12 || buf[4] != 0 || buf[5] != 1)
1feee8
+    {
1feee8
+      __set_errno (EMSGSIZE);
1feee8
+      c->current = c->end;
1feee8
+      return false;
1feee8
+    }
1feee8
+  c->current = buf + 12;
1feee8
+
1feee8
+  int consumed = __ns_name_length_uncompressed (c->current, c->end);
1feee8
+  if (consumed < 0)
1feee8
+    {
1feee8
+      __set_errno (EMSGSIZE);
1feee8
+      c->current = c->end;
1feee8
+      c->first_rr = NULL;
1feee8
+      return false;
1feee8
+    }
1feee8
+  c->current += consumed;
1feee8
+
1feee8
+  /* Ensure there is room for question type and class.  */
1feee8
+  if (c->end - c->current < 4)
1feee8
+    {
1feee8
+      __set_errno (EMSGSIZE);
1feee8
+      c->current = c->end;
1feee8
+      c->first_rr = NULL;
1feee8
+      return false;
1feee8
+    }
1feee8
+  c->current += 4;
1feee8
+  c->first_rr = c->current;
1feee8
+
1feee8
+  return true;
1feee8
+}
1feee8
diff --git a/resolv/ns_rr_cursor_next.c b/resolv/ns_rr_cursor_next.c
1feee8
new file mode 100644
1feee8
index 0000000000000000..33652fc5da322d69
1feee8
--- /dev/null
1feee8
+++ b/resolv/ns_rr_cursor_next.c
1feee8
@@ -0,0 +1,74 @@
1feee8
+/* Simple DNS record parser without textual name decoding.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#include <arpa/nameser.h>
1feee8
+#include <errno.h>
1feee8
+#include <stdbool.h>
1feee8
+#include <string.h>
1feee8
+
1feee8
+bool
1feee8
+__ns_rr_cursor_next (struct ns_rr_cursor *c, struct ns_rr_wire *rr)
1feee8
+{
1feee8
+  rr->rdata = NULL;
1feee8
+
1feee8
+  /* Extract the record owner name.  */
1feee8
+  int consumed = __ns_name_unpack (c->begin, c->end, c->current,
1feee8
+                                   rr->rname, sizeof (rr->rname));
1feee8
+  if (consumed < 0)
1feee8
+    {
1feee8
+      memset (rr, 0, sizeof (*rr));
1feee8
+      __set_errno (EMSGSIZE);
1feee8
+      return false;
1feee8
+    }
1feee8
+  c->current += consumed;
1feee8
+
1feee8
+  /* Extract the metadata.  */
1feee8
+  struct
1feee8
+  {
1feee8
+    uint16_t rtype;
1feee8
+    uint16_t rclass;
1feee8
+    uint32_t ttl;
1feee8
+    uint16_t rdlength;
1feee8
+  } __attribute__ ((packed)) metadata;
1feee8
+  _Static_assert (sizeof (metadata) == 10, "sizeof metadata");
1feee8
+  if (c->end - c->current < sizeof (metadata))
1feee8
+    {
1feee8
+      memset (rr, 0, sizeof (*rr));
1feee8
+      __set_errno (EMSGSIZE);
1feee8
+      return false;
1feee8
+    }
1feee8
+  memcpy (&metadata, c->current, sizeof (metadata));
1feee8
+  c->current += sizeof (metadata);
1feee8
+  /* Endianess conversion.  */
1feee8
+  rr->rtype = ntohs (metadata.rtype);
1feee8
+  rr->rclass = ntohs (metadata.rclass);
1feee8
+  rr->ttl = ntohl (metadata.ttl);
1feee8
+  rr->rdlength = ntohs (metadata.rdlength);
1feee8
+
1feee8
+  /* Extract record data.  */
1feee8
+  if (c->end - c->current < rr->rdlength)
1feee8
+    {
1feee8
+      memset (rr, 0, sizeof (*rr));
1feee8
+      __set_errno (EMSGSIZE);
1feee8
+      return false;
1feee8
+    }
1feee8
+  rr->rdata = c->current;
1feee8
+  c->current += rr->rdlength;
1feee8
+
1feee8
+  return true;
1feee8
+}
1feee8
diff --git a/resolv/tst-ns_rr_cursor.c b/resolv/tst-ns_rr_cursor.c
1feee8
new file mode 100644
1feee8
index 0000000000000000..c3c09089053d0c40
1feee8
--- /dev/null
1feee8
+++ b/resolv/tst-ns_rr_cursor.c
1feee8
@@ -0,0 +1,227 @@
1feee8
+/* Tests for resource record parsing.
1feee8
+   Copyright (C) 2022 Free Software Foundation, Inc.
1feee8
+   This file is part of the GNU C Library.
1feee8
+
1feee8
+   The GNU C Library is free software; you can redistribute it and/or
1feee8
+   modify it under the terms of the GNU Lesser General Public
1feee8
+   License as published by the Free Software Foundation; either
1feee8
+   version 2.1 of the License, or (at your option) any later version.
1feee8
+
1feee8
+   The GNU C Library is distributed in the hope that it will be useful,
1feee8
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
1feee8
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1feee8
+   Lesser General Public License for more details.
1feee8
+
1feee8
+   You should have received a copy of the GNU Lesser General Public
1feee8
+   License along with the GNU C Library; if not, see
1feee8
+   <https://www.gnu.org/licenses/>.  */
1feee8
+
1feee8
+#include <arpa/nameser.h>
1feee8
+#include <string.h>
1feee8
+#include <support/check.h>
1feee8
+#include <support/next_to_fault.h>
1feee8
+
1feee8
+/* Reference packet for packet parsing.  */
1feee8
+static const unsigned char valid_packet[] =
1feee8
+  { 0x11, 0x12, 0x13, 0x14,
1feee8
+    0x00, 0x01,               /* Question count.  */
1feee8
+    0x00, 0x02,               /* Answer count.  */
1feee8
+    0x21, 0x22, 0x23, 0x24,   /* Other counts (not actually in packet).  */
1feee8
+    3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0,
1feee8
+    0x00, 0x1c,               /* Question type: AAAA.  */
1feee8
+    0x00, 0x01,               /* Question class: IN.  */
1feee8
+    0xc0, 0x0c,               /* Compression reference to QNAME.  */
1feee8
+    0x00, 0x1c,               /* Record type: AAAA.  */
1feee8
+    0x00, 0x01,               /* Record class: IN.  */
1feee8
+    0x12, 0x34, 0x56, 0x78,   /* Record TTL.  */
1feee8
+    0x00, 0x10,               /* Record data length (16 bytes).  */
1feee8
+    0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
1feee8
+    0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, /* IPv6 address.  */
1feee8
+    0xc0, 0x0c,               /* Compression reference to QNAME.  */
1feee8
+    0x00, 0x1c,               /* Record type: AAAA.  */
1feee8
+    0x00, 0x01,               /* Record class: IN.  */
1feee8
+    0x11, 0x33, 0x55, 0x77,   /* Record TTL.  */
1feee8
+    0x00, 0x10,               /* Record data length (16 bytes).  */
1feee8
+    0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
1feee8
+    0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, /* IPv6 address.  */
1feee8
+  };
1feee8
+
1feee8
+/* Special offsets in valid_packet.  */
1feee8
+enum
1feee8
+  {
1feee8
+    offset_of_first_record = 29,
1feee8
+    offset_of_second_record = 57,
1feee8
+  };
1feee8
+
1feee8
+/* Check that parsing valid_packet succeeds.  */
1feee8
+static void
1feee8
+test_valid (void)
1feee8
+{
1feee8
+  struct ns_rr_cursor c;
1feee8
+  TEST_VERIFY_EXIT (__ns_rr_cursor_init (&c, valid_packet,
1feee8
+                                         sizeof (valid_packet)));
1feee8
+  TEST_COMPARE (ns_rr_cursor_rcode (&c), 4);
1feee8
+  TEST_COMPARE (ns_rr_cursor_ancount (&c), 2);
1feee8
+  TEST_COMPARE (ns_rr_cursor_nscount (&c), 0x2122);
1feee8
+  TEST_COMPARE (ns_rr_cursor_adcount (&c), 0x2324);
1feee8
+  TEST_COMPARE_BLOB (ns_rr_cursor_qname (&c), 13, &valid_packet[12], 13);
1feee8
+  TEST_COMPARE (ns_rr_cursor_qtype (&c), T_AAAA);
1feee8
+  TEST_COMPARE (ns_rr_cursor_qclass (&c), C_IN);
1feee8
+  TEST_COMPARE (c.current - valid_packet, offset_of_first_record);
1feee8
+
1feee8
+  struct ns_rr_wire r;
1feee8
+  TEST_VERIFY_EXIT (__ns_rr_cursor_next (&c, &r);;
1feee8
+  TEST_COMPARE (r.rtype, T_AAAA);
1feee8
+  TEST_COMPARE (r.rclass, C_IN);
1feee8
+  TEST_COMPARE (r.ttl, 0x12345678);
1feee8
+  TEST_COMPARE_BLOB (r.rdata, r.rdlength,
1feee8
+                     "\x90\x91\x92\x93\x94\x95\x96\x97"
1feee8
+                     "\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f", 16);
1feee8
+  TEST_COMPARE (c.current - valid_packet, offset_of_second_record);
1feee8
+  TEST_VERIFY_EXIT (__ns_rr_cursor_next (&c, &r);;
1feee8
+  TEST_COMPARE (r.rtype, T_AAAA);
1feee8
+  TEST_COMPARE (r.rclass, C_IN);
1feee8
+  TEST_COMPARE (r.ttl, 0x11335577);
1feee8
+  TEST_COMPARE_BLOB (r.rdata, r.rdlength,
1feee8
+                     "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7"
1feee8
+                     "\xa8\xa9\xaa\xab\xac\xad\xae\xaf", 16);
1feee8
+  TEST_VERIFY (c.current == c.end);
1feee8
+}
1feee8
+
1feee8
+/* Check that trying to parse a packet with a compressed QNAME fails.  */
1feee8
+static void
1feee8
+test_compressed_qname (void)
1feee8
+{
1feee8
+  static const unsigned char packet[] =
1feee8
+    { 0x11, 0x12, 0x13, 0x14,
1feee8
+      0x00, 0x01,               /* Question count.  */
1feee8
+      0x00, 0x00,               /* Answer count.  */
1feee8
+      0x00, 0x00, 0x00, 0x00,   /* Other counts.  */
1feee8
+      3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0xc0, 0x04,
1feee8
+      0x00, 0x01,               /* Question type: A.  */
1feee8
+      0x00, 0x01,               /* Question class: IN.  */
1feee8
+    };
1feee8
+
1feee8
+  struct ns_rr_cursor c;
1feee8
+  TEST_VERIFY_EXIT (!__ns_rr_cursor_init (&c, packet, sizeof (packet)));
1feee8
+}
1feee8
+
1feee8
+/* Check that trying to parse a packet with two questions fails.  */
1feee8
+static void
1feee8
+test_two_questions (void)
1feee8
+{
1feee8
+  static const unsigned char packet[] =
1feee8
+    { 0x11, 0x12, 0x13, 0x14,
1feee8
+      0x00, 0x02,               /* Question count.  */
1feee8
+      0x00, 0x00,               /* Answer count.  */
1feee8
+      0x00, 0x00, 0x00, 0x00,   /* Other counts.  */
1feee8
+      3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0xc0, 0x04,
1feee8
+      0x00, 0x01,               /* Question type: A.  */
1feee8
+      0x00, 0x01,               /* Question class: IN.  */
1feee8
+      3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0xc0, 0x04,
1feee8
+      0x00, 0x1c,               /* Question type: AAAA.  */
1feee8
+      0x00, 0x01,               /* Question class: IN.  */
1feee8
+    };
1feee8
+
1feee8
+  struct ns_rr_cursor c;
1feee8
+  TEST_VERIFY_EXIT (!__ns_rr_cursor_init (&c, packet, sizeof (packet)));
1feee8
+}
1feee8
+
1feee8
+/* Used to check that parsing truncated packets does not over-read.  */
1feee8
+static struct support_next_to_fault ntf;
1feee8
+
1feee8
+/* Truncated packet in the second resource record.  */
1feee8
+static void
1feee8
+test_truncated_one_rr (size_t length)
1feee8
+{
1feee8
+  unsigned char *end = (unsigned char *) ntf.buffer - ntf.length;
1feee8
+  unsigned char *start = end - length;
1feee8
+
1feee8
+  /* Produce the truncated packet.  */
1feee8
+  memcpy (start, valid_packet, length);
1feee8
+
1feee8
+  struct ns_rr_cursor c;
1feee8
+  TEST_VERIFY_EXIT (__ns_rr_cursor_init (&c, start, length));
1feee8
+  TEST_COMPARE (ns_rr_cursor_rcode (&c), 4);
1feee8
+  TEST_COMPARE (ns_rr_cursor_ancount (&c), 2);
1feee8
+  TEST_COMPARE (ns_rr_cursor_nscount (&c), 0x2122);
1feee8
+  TEST_COMPARE (ns_rr_cursor_adcount (&c), 0x2324);
1feee8
+  TEST_COMPARE_BLOB (ns_rr_cursor_qname (&c), 13, &valid_packet[12], 13);
1feee8
+  TEST_COMPARE (ns_rr_cursor_qtype (&c), T_AAAA);
1feee8
+  TEST_COMPARE (ns_rr_cursor_qclass (&c), C_IN);
1feee8
+  TEST_COMPARE (c.current - start, offset_of_first_record);
1feee8
+
1feee8
+  struct ns_rr_wire r;
1feee8
+  TEST_VERIFY_EXIT (__ns_rr_cursor_next (&c, &r);;
1feee8
+  TEST_COMPARE (r.rtype, T_AAAA);
1feee8
+  TEST_COMPARE (r.rclass, C_IN);
1feee8
+  TEST_COMPARE (r.ttl, 0x12345678);
1feee8
+  TEST_COMPARE_BLOB (r.rdata, r.rdlength,
1feee8
+                     "\x90\x91\x92\x93\x94\x95\x96\x97"
1feee8
+                     "\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f", 16);
1feee8
+  TEST_COMPARE (c.current - start, offset_of_second_record);
1feee8
+  TEST_VERIFY (!__ns_rr_cursor_next (&c, &r);;
1feee8
+}
1feee8
+
1feee8
+/* Truncated packet in the first resource record.  */
1feee8
+static void
1feee8
+test_truncated_no_rr (size_t length)
1feee8
+{
1feee8
+  unsigned char *end = (unsigned char *) ntf.buffer - ntf.length;
1feee8
+  unsigned char *start = end - length;
1feee8
+
1feee8
+  /* Produce the truncated packet.  */
1feee8
+  memcpy (start, valid_packet, length);
1feee8
+
1feee8
+  struct ns_rr_cursor c;
1feee8
+  TEST_VERIFY_EXIT (__ns_rr_cursor_init (&c, start, length));
1feee8
+  TEST_COMPARE (ns_rr_cursor_rcode (&c), 4);
1feee8
+  TEST_COMPARE (ns_rr_cursor_ancount (&c), 2);
1feee8
+  TEST_COMPARE (ns_rr_cursor_nscount (&c), 0x2122);
1feee8
+  TEST_COMPARE (ns_rr_cursor_adcount (&c), 0x2324);
1feee8
+  TEST_COMPARE_BLOB (ns_rr_cursor_qname (&c), 13, &valid_packet[12], 13);
1feee8
+  TEST_COMPARE (ns_rr_cursor_qtype (&c), T_AAAA);
1feee8
+  TEST_COMPARE (ns_rr_cursor_qclass (&c), C_IN);
1feee8
+  TEST_COMPARE (c.current - start, offset_of_first_record);
1feee8
+
1feee8
+  struct ns_rr_wire r;
1feee8
+  TEST_VERIFY (!__ns_rr_cursor_next (&c, &r);;
1feee8
+}
1feee8
+
1feee8
+/* Truncated packet before first resource record.  */
1feee8
+static void
1feee8
+test_truncated_before_rr (size_t length)
1feee8
+{
1feee8
+  unsigned char *end = (unsigned char *) ntf.buffer - ntf.length;
1feee8
+  unsigned char *start = end - length;
1feee8
+
1feee8
+  /* Produce the truncated packet.  */
1feee8
+  memcpy (start, valid_packet, length);
1feee8
+
1feee8
+  struct ns_rr_cursor c;
1feee8
+  TEST_VERIFY_EXIT (!__ns_rr_cursor_init (&c, start, length));
1feee8
+}
1feee8
+
1feee8
+static int
1feee8
+do_test (void)
1feee8
+{
1feee8
+  ntf = support_next_to_fault_allocate (sizeof (valid_packet));
1feee8
+
1feee8
+  test_valid ();
1feee8
+  test_compressed_qname ();
1feee8
+  test_two_questions ();
1feee8
+
1feee8
+  for (int length = offset_of_second_record; length < sizeof (valid_packet);
1feee8
+       ++length)
1feee8
+    test_truncated_one_rr (length);
1feee8
+  for (int length = offset_of_first_record; length < offset_of_second_record;
1feee8
+       ++length)
1feee8
+    test_truncated_no_rr (length);
1feee8
+  for (int length = 0; length < offset_of_first_record; ++length)
1feee8
+    test_truncated_before_rr (length);
1feee8
+
1feee8
+  support_next_to_fault_free (&ntf;;
1feee8
+  return 0;
1feee8
+}
1feee8
+
1feee8
+#include <support/test-driver.c>