support: Provide a way to reorder responses within the DNS test server

This commit is contained in:
Florian Weimer 2020-10-14 10:54:39 +02:00
parent 880a12e96d
commit 873e239a4c
5 changed files with 135 additions and 28 deletions

View File

@ -35,6 +35,8 @@ libsupport-routines = \
ignore_stderr \ ignore_stderr \
next_to_fault \ next_to_fault \
oom_error \ oom_error \
resolv_response_context_duplicate \
resolv_response_context_free \
resolv_test \ resolv_test \
set_fortify_handler \ set_fortify_handler \
support-xfstat \ support-xfstat \

View File

@ -0,0 +1,37 @@
/* Duplicate a response context used in DNS resolver tests.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <string.h>
#include <support/resolv_test.h>
#include <support/support.h>
struct resolv_response_context *
resolv_response_context_duplicate (const struct resolv_response_context *ctx)
{
struct resolv_response_context *result = xmalloc (sizeof (*result));
memcpy (result, ctx, sizeof (*result));
if (result->client_address != NULL)
{
result->client_address = xmalloc (result->client_address_length);
memcpy (result->client_address, ctx->client_address,
result->client_address_length);
}
result->query_buffer = xmalloc (result->query_length);
memcpy (result->query_buffer, ctx->query_buffer, result->query_length);
return result;
}

View File

@ -0,0 +1,28 @@
/* Free a response context used in DNS resolver tests.
Copyright (C) 2020 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
#include <stdlib.h>
#include <support/resolv_test.h>
void
resolv_response_context_free (struct resolv_response_context *ctx)
{
free (ctx->query_buffer);
free (ctx->client_address);
free (ctx);
}

View File

@ -434,9 +434,9 @@ resolv_response_buffer (const struct resolv_response_builder *b)
return result; return result;
} }
static struct resolv_response_builder * struct resolv_response_builder *
response_builder_allocate resolv_response_builder_allocate (const unsigned char *query_buffer,
(const unsigned char *query_buffer, size_t query_length) size_t query_length)
{ {
struct resolv_response_builder *b = xmalloc (sizeof (*b)); struct resolv_response_builder *b = xmalloc (sizeof (*b));
memset (b, 0, offsetof (struct resolv_response_builder, buffer)); memset (b, 0, offsetof (struct resolv_response_builder, buffer));
@ -445,8 +445,8 @@ response_builder_allocate
return b; return b;
} }
static void void
response_builder_free (struct resolv_response_builder *b) resolv_response_builder_free (struct resolv_response_builder *b)
{ {
tdestroy (b->compression_offsets, free); tdestroy (b->compression_offsets, free);
free (b); free (b);
@ -661,13 +661,17 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
struct resolv_response_context ctx = struct resolv_response_context ctx =
{ {
.test = obj,
.client_address = &peer,
.client_address_length = peerlen,
.query_buffer = query, .query_buffer = query,
.query_length = length, .query_length = length,
.server_index = server_index, .server_index = server_index,
.tcp = false, .tcp = false,
.edns = qinfo.edns, .edns = qinfo.edns,
}; };
struct resolv_response_builder *b = response_builder_allocate (query, length); struct resolv_response_builder *b
= resolv_response_builder_allocate (query, length);
obj->config.response_callback obj->config.response_callback
(&ctx, b, qinfo.qname, qinfo.qclass, qinfo.qtype); (&ctx, b, qinfo.qname, qinfo.qclass, qinfo.qtype);
@ -684,7 +688,7 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
if (b->offset >= 12) if (b->offset >= 12)
printf ("info: UDP server %d: sending response:" printf ("info: UDP server %d: sending response:"
" %zu bytes, RCODE %d (for %s/%u/%u)\n", " %zu bytes, RCODE %d (for %s/%u/%u)\n",
server_index, b->offset, b->buffer[3] & 0x0f, ctx.server_index, b->offset, b->buffer[3] & 0x0f,
qinfo.qname, qinfo.qclass, qinfo.qtype); qinfo.qname, qinfo.qclass, qinfo.qtype);
else else
printf ("info: UDP server %d: sending response: %zu bytes" printf ("info: UDP server %d: sending response: %zu bytes"
@ -694,6 +698,17 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
if (b->truncate_bytes > 0) if (b->truncate_bytes > 0)
printf ("info: truncated by %u bytes\n", b->truncate_bytes); printf ("info: truncated by %u bytes\n", b->truncate_bytes);
} }
resolv_response_send_udp (&ctx, b);
}
resolv_response_builder_free (b);
return true;
}
void
resolv_response_send_udp (const struct resolv_response_context *ctx,
struct resolv_response_builder *b)
{
TEST_VERIFY_EXIT (!ctx->tcp);
size_t to_send = b->offset; size_t to_send = b->offset;
if (to_send < b->truncate_bytes) if (to_send < b->truncate_bytes)
to_send = 0; to_send = 0;
@ -702,13 +717,10 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index)
/* Ignore most errors here because the other end may have closed /* Ignore most errors here because the other end may have closed
the socket. */ the socket. */
if (sendto (obj->servers[server_index].socket_udp, if (sendto (ctx->test->servers[ctx->server_index].socket_udp,
b->buffer, to_send, 0, b->buffer, to_send, 0,
(struct sockaddr *) &peer, peerlen) < 0) ctx->client_address, ctx->client_address_length) < 0)
TEST_VERIFY_EXIT (errno != EBADF); TEST_VERIFY_EXIT (errno != EBADF);
}
response_builder_free (b);
return true;
} }
/* UDP thread_callback function. Variant for one thread per /* UDP thread_callback function. Variant for one thread per
@ -897,14 +909,15 @@ server_thread_tcp_client (void *arg)
struct resolv_response_context ctx = struct resolv_response_context ctx =
{ {
.test = closure->obj,
.query_buffer = query_buffer, .query_buffer = query_buffer,
.query_length = query_length, .query_length = query_length,
.server_index = closure->server_index, .server_index = closure->server_index,
.tcp = true, .tcp = true,
.edns = qinfo.edns, .edns = qinfo.edns,
}; };
struct resolv_response_builder *b = response_builder_allocate struct resolv_response_builder *b
(query_buffer, query_length); = resolv_response_builder_allocate (query_buffer, query_length);
closure->obj->config.response_callback closure->obj->config.response_callback
(&ctx, b, qinfo.qname, qinfo.qclass, qinfo.qtype); (&ctx, b, qinfo.qname, qinfo.qclass, qinfo.qtype);
@ -936,7 +949,7 @@ server_thread_tcp_client (void *arg)
writev_fully (closure->client_socket, buffers, 2); writev_fully (closure->client_socket, buffers, 2);
} }
bool close_flag = b->close; bool close_flag = b->close;
response_builder_free (b); resolv_response_builder_free (b);
free (query_buffer); free (query_buffer);
if (close_flag) if (close_flag)
break; break;

View File

@ -35,25 +35,36 @@ struct resolv_edns_info
uint16_t payload_size; uint16_t payload_size;
}; };
/* This opaque struct collects information about the resolver testing
currently in progress. */
struct resolv_test;
/* This struct provides context information when the response callback /* This struct provides context information when the response callback
specified in struct resolv_redirect_config is invoked. */ specified in struct resolv_redirect_config is invoked. */
struct resolv_response_context struct resolv_response_context
{ {
const unsigned char *query_buffer; struct resolv_test *test;
void *client_address;
size_t client_address_length;
unsigned char *query_buffer;
size_t query_length; size_t query_length;
int server_index; int server_index;
bool tcp; bool tcp;
struct resolv_edns_info edns; struct resolv_edns_info edns;
}; };
/* Produces a deep copy of the context. */
struct resolv_response_context *
resolv_response_context_duplicate (const struct resolv_response_context *);
/* Frees the copy. For the context passed to the response function,
this happens implicitly. */
void resolv_response_context_free (struct resolv_response_context *);
/* This opaque struct is used to construct responses from within the /* This opaque struct is used to construct responses from within the
response callback function. */ response callback function. */
struct resolv_response_builder; struct resolv_response_builder;
/* This opaque struct collects information about the resolver testing
currently in progress. */
struct resolv_test;
enum enum
{ {
/* Maximum number of test servers supported by the framework. */ /* Maximum number of test servers supported by the framework. */
@ -188,6 +199,22 @@ void resolv_response_close (struct resolv_response_builder *);
/* The size of the response packet built so far. */ /* The size of the response packet built so far. */
size_t resolv_response_length (const struct resolv_response_builder *); size_t resolv_response_length (const struct resolv_response_builder *);
/* Allocates a response builder tied to a specific query packet,
starting at QUERY_BUFFER, containing QUERY_LENGTH bytes. */
struct resolv_response_builder *
resolv_response_builder_allocate (const unsigned char *query_buffer,
size_t query_length);
/* Deallocates a response buffer. */
void resolv_response_builder_free (struct resolv_response_builder *);
/* Sends a UDP response using a specific context. This can be used to
reorder or duplicate responses, along with
resolv_response_context_duplicate and
response_builder_allocate. */
void resolv_response_send_udp (const struct resolv_response_context *,
struct resolv_response_builder *);
__END_DECLS __END_DECLS
#endif /* SUPPORT_RESOLV_TEST_H */ #endif /* SUPPORT_RESOLV_TEST_H */