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