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