446cf2
commit 873e239a4c3d8ec235c27439c1bdc5bbf8aa1818
446cf2
Author: Florian Weimer <fweimer@redhat.com>
446cf2
Date:   Wed Oct 14 10:54:39 2020 +0200
446cf2
446cf2
    support: Provide a way to reorder responses within the DNS test server
446cf2
446cf2
diff --git a/support/Makefile b/support/Makefile
446cf2
index 3c940aa6a7bdfc99..37d5dcc92a5c6dee 100644
446cf2
--- a/support/Makefile
446cf2
+++ b/support/Makefile
446cf2
@@ -35,6 +35,8 @@ libsupport-routines = \
446cf2
   ignore_stderr \
446cf2
   next_to_fault \
446cf2
   oom_error \
446cf2
+  resolv_response_context_duplicate \
446cf2
+  resolv_response_context_free \
446cf2
   resolv_test \
446cf2
   set_fortify_handler \
446cf2
   support-xfstat \
446cf2
diff --git a/support/resolv_response_context_duplicate.c b/support/resolv_response_context_duplicate.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..f9c5c3462ad053ec
446cf2
--- /dev/null
446cf2
+++ b/support/resolv_response_context_duplicate.c
446cf2
@@ -0,0 +1,37 @@
446cf2
+/* Duplicate a response context used in DNS resolver tests.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <string.h>
446cf2
+#include <support/resolv_test.h>
446cf2
+#include <support/support.h>
446cf2
+
446cf2
+struct resolv_response_context *
446cf2
+resolv_response_context_duplicate (const struct resolv_response_context *ctx)
446cf2
+{
446cf2
+  struct resolv_response_context *result = xmalloc (sizeof (*result));
446cf2
+  memcpy (result, ctx, sizeof (*result));
446cf2
+  if (result->client_address != NULL)
446cf2
+    {
446cf2
+      result->client_address = xmalloc (result->client_address_length);
446cf2
+      memcpy (result->client_address, ctx->client_address,
446cf2
+              result->client_address_length);
446cf2
+    }
446cf2
+  result->query_buffer = xmalloc (result->query_length);
446cf2
+  memcpy (result->query_buffer, ctx->query_buffer, result->query_length);
446cf2
+  return result;
446cf2
+}
446cf2
diff --git a/support/resolv_response_context_free.c b/support/resolv_response_context_free.c
446cf2
new file mode 100644
446cf2
index 0000000000000000..b88c05ffd4acfdd4
446cf2
--- /dev/null
446cf2
+++ b/support/resolv_response_context_free.c
446cf2
@@ -0,0 +1,28 @@
446cf2
+/* Free a response context used in DNS resolver tests.
446cf2
+   Copyright (C) 2020 Free Software Foundation, Inc.
446cf2
+   This file is part of the GNU C Library.
446cf2
+
446cf2
+   The GNU C Library is free software; you can redistribute it and/or
446cf2
+   modify it under the terms of the GNU Lesser General Public
446cf2
+   License as published by the Free Software Foundation; either
446cf2
+   version 2.1 of the License, or (at your option) any later version.
446cf2
+
446cf2
+   The GNU C Library is distributed in the hope that it will be useful,
446cf2
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
446cf2
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
446cf2
+   Lesser General Public License for more details.
446cf2
+
446cf2
+   You should have received a copy of the GNU Lesser General Public
446cf2
+   License along with the GNU C Library; if not, see
446cf2
+   <https://www.gnu.org/licenses/>.  */
446cf2
+
446cf2
+#include <stdlib.h>
446cf2
+#include <support/resolv_test.h>
446cf2
+
446cf2
+void
446cf2
+resolv_response_context_free (struct resolv_response_context *ctx)
446cf2
+{
446cf2
+  free (ctx->query_buffer);
446cf2
+  free (ctx->client_address);
446cf2
+  free (ctx);
446cf2
+}
446cf2
diff --git a/support/resolv_test.c b/support/resolv_test.c
446cf2
index 28af227cb5ed901c..8cca4e6cf723de28 100644
446cf2
--- a/support/resolv_test.c
446cf2
+++ b/support/resolv_test.c
446cf2
@@ -434,9 +434,9 @@ resolv_response_buffer (const struct resolv_response_builder *b)
446cf2
   return result;
446cf2
 }
446cf2
 
446cf2
-static struct resolv_response_builder *
446cf2
-response_builder_allocate
446cf2
-  (const unsigned char *query_buffer, size_t query_length)
446cf2
+struct resolv_response_builder *
446cf2
+resolv_response_builder_allocate (const unsigned char *query_buffer,
446cf2
+                                  size_t query_length)
446cf2
 {
446cf2
   struct resolv_response_builder *b = xmalloc (sizeof (*b));
446cf2
   memset (b, 0, offsetof (struct resolv_response_builder, buffer));
446cf2
@@ -445,8 +445,8 @@ response_builder_allocate
446cf2
   return b;
446cf2
 }
446cf2
 
446cf2
-static void
446cf2
-response_builder_free (struct resolv_response_builder *b)
446cf2
+void
446cf2
+resolv_response_builder_free (struct resolv_response_builder *b)
446cf2
 {
446cf2
   tdestroy (b->compression_offsets, free);
446cf2
   free (b);
446cf2
@@ -661,13 +661,17 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
446cf2
 
446cf2
   struct resolv_response_context ctx =
446cf2
     {
446cf2
+      .test = obj,
446cf2
+      .client_address = &peer,
446cf2
+      .client_address_length = peerlen,
446cf2
       .query_buffer = query,
446cf2
       .query_length = length,
446cf2
       .server_index = server_index,
446cf2
       .tcp = false,
446cf2
       .edns = qinfo.edns,
446cf2
     };
446cf2
-  struct resolv_response_builder *b = response_builder_allocate (query, length);
446cf2
+  struct resolv_response_builder *b
446cf2
+    = resolv_response_builder_allocate (query, length);
446cf2
   obj->config.response_callback
446cf2
     (&ctx, b, qinfo.qname, qinfo.qclass, qinfo.qtype);
446cf2
 
446cf2
@@ -684,7 +688,7 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
446cf2
           if (b->offset >= 12)
446cf2
             printf ("info: UDP server %d: sending response:"
446cf2
                     " %zu bytes, RCODE %d (for %s/%u/%u)\n",
446cf2
-                    server_index, b->offset, b->buffer[3] & 0x0f,
446cf2
+                    ctx.server_index, b->offset, b->buffer[3] & 0x0f,
446cf2
                     qinfo.qname, qinfo.qclass, qinfo.qtype);
446cf2
           else
446cf2
             printf ("info: UDP server %d: sending response: %zu bytes"
446cf2
@@ -694,23 +698,31 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
446cf2
           if (b->truncate_bytes > 0)
446cf2
             printf ("info:    truncated by %u bytes\n", b->truncate_bytes);
446cf2
         }
446cf2
-      size_t to_send = b->offset;
446cf2
-      if (to_send < b->truncate_bytes)
446cf2
-        to_send = 0;
446cf2
-      else
446cf2
-        to_send -= b->truncate_bytes;
446cf2
-
446cf2
-      /* Ignore most errors here because the other end may have closed
446cf2
-         the socket. */
446cf2
-      if (sendto (obj->servers[server_index].socket_udp,
446cf2
-                  b->buffer, to_send, 0,
446cf2
-                  (struct sockaddr *) &peer, peerlen) < 0)
446cf2
-        TEST_VERIFY_EXIT (errno != EBADF);
446cf2
+      resolv_response_send_udp (&ctx, b);
446cf2
     }
446cf2
-  response_builder_free (b);
446cf2
+  resolv_response_builder_free (b);
446cf2
   return true;
446cf2
 }
446cf2
 
446cf2
+void
446cf2
+resolv_response_send_udp (const struct resolv_response_context *ctx,
446cf2
+                          struct resolv_response_builder *b)
446cf2
+{
446cf2
+  TEST_VERIFY_EXIT (!ctx->tcp);
446cf2
+  size_t to_send = b->offset;
446cf2
+  if (to_send < b->truncate_bytes)
446cf2
+    to_send = 0;
446cf2
+  else
446cf2
+    to_send -= b->truncate_bytes;
446cf2
+
446cf2
+  /* Ignore most errors here because the other end may have closed
446cf2
+     the socket.  */
446cf2
+  if (sendto (ctx->test->servers[ctx->server_index].socket_udp,
446cf2
+              b->buffer, to_send, 0,
446cf2
+              ctx->client_address, ctx->client_address_length) < 0)
446cf2
+    TEST_VERIFY_EXIT (errno != EBADF);
446cf2
+}
446cf2
+
446cf2
 /* UDP thread_callback function.  Variant for one thread per
446cf2
    server.  */
446cf2
 static void
446cf2
@@ -897,14 +909,15 @@ server_thread_tcp_client (void *arg)
446cf2
 
446cf2
       struct resolv_response_context ctx =
446cf2
         {
446cf2
+          .test = closure->obj,
446cf2
           .query_buffer = query_buffer,
446cf2
           .query_length = query_length,
446cf2
           .server_index = closure->server_index,
446cf2
           .tcp = true,
446cf2
           .edns = qinfo.edns,
446cf2
         };
446cf2
-      struct resolv_response_builder *b = response_builder_allocate
446cf2
-        (query_buffer, query_length);
446cf2
+      struct resolv_response_builder *b
446cf2
+        = resolv_response_builder_allocate (query_buffer, query_length);
446cf2
       closure->obj->config.response_callback
446cf2
         (&ctx, b, qinfo.qname, qinfo.qclass, qinfo.qtype);
446cf2
 
446cf2
@@ -936,7 +949,7 @@ server_thread_tcp_client (void *arg)
446cf2
           writev_fully (closure->client_socket, buffers, 2);
446cf2
         }
446cf2
       bool close_flag = b->close;
446cf2
-      response_builder_free (b);
446cf2
+      resolv_response_builder_free (b);
446cf2
       free (query_buffer);
446cf2
       if (close_flag)
446cf2
         break;
446cf2
diff --git a/support/resolv_test.h b/support/resolv_test.h
446cf2
index be736aead40cd0cc..ff5571dace92c936 100644
446cf2
--- a/support/resolv_test.h
446cf2
+++ b/support/resolv_test.h
446cf2
@@ -35,25 +35,36 @@ struct resolv_edns_info
446cf2
   uint16_t payload_size;
446cf2
 };
446cf2
 
446cf2
+/* This opaque struct collects information about the resolver testing
446cf2
+   currently in progress.  */
446cf2
+struct resolv_test;
446cf2
+
446cf2
 /* This struct provides context information when the response callback
446cf2
    specified in struct resolv_redirect_config is invoked. */
446cf2
 struct resolv_response_context
446cf2
 {
446cf2
-  const unsigned char *query_buffer;
446cf2
+  struct resolv_test *test;
446cf2
+  void *client_address;
446cf2
+  size_t client_address_length;
446cf2
+  unsigned char *query_buffer;
446cf2
   size_t query_length;
446cf2
   int server_index;
446cf2
   bool tcp;
446cf2
   struct resolv_edns_info edns;
446cf2
 };
446cf2
 
446cf2
+/* Produces a deep copy of the context.  */
446cf2
+struct resolv_response_context *
446cf2
+  resolv_response_context_duplicate (const struct resolv_response_context *);
446cf2
+
446cf2
+/* Frees the copy.  For the context passed to the response function,
446cf2
+   this happens implicitly.  */
446cf2
+void resolv_response_context_free (struct resolv_response_context *);
446cf2
+
446cf2
 /* This opaque struct is used to construct responses from within the
446cf2
    response callback function.  */
446cf2
 struct resolv_response_builder;
446cf2
 
446cf2
-/* This opaque struct collects information about the resolver testing
446cf2
-   currently in progress.  */
446cf2
-struct resolv_test;
446cf2
-
446cf2
 enum
446cf2
   {
446cf2
     /* Maximum number of test servers supported by the framework.  */
446cf2
@@ -188,6 +199,22 @@ void resolv_response_close (struct resolv_response_builder *);
446cf2
 /* The size of the response packet built so far.  */
446cf2
 size_t resolv_response_length (const struct resolv_response_builder *);
446cf2
 
446cf2
+/* Allocates a response builder tied to a specific query packet,
446cf2
+   starting at QUERY_BUFFER, containing QUERY_LENGTH bytes.  */
446cf2
+struct resolv_response_builder *
446cf2
+  resolv_response_builder_allocate (const unsigned char *query_buffer,
446cf2
+                                    size_t query_length);
446cf2
+
446cf2
+/* Deallocates a response buffer.  */
446cf2
+void resolv_response_builder_free (struct resolv_response_builder *);
446cf2
+
446cf2
+/* Sends a UDP response using a specific context.  This can be used to
446cf2
+   reorder or duplicate responses, along with
446cf2
+   resolv_response_context_duplicate and
446cf2
+   response_builder_allocate.  */
446cf2
+void resolv_response_send_udp (const struct resolv_response_context *,
446cf2
+                               struct resolv_response_builder *);
446cf2
+
446cf2
 __END_DECLS
446cf2
 
446cf2
 #endif /* SUPPORT_RESOLV_TEST_H */