From patchwork Fri Sep 11 13:03:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 40394 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5E2B93987404; Fri, 11 Sep 2020 13:03:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5E2B93987404 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1599829432; bh=OT/Rr7UEZPAAVzj2bMgc0q54ryRM3W7QUud4nNPg8JM=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=mAWoJh9E5BDtd/jd7G2zlBs6DGZllaI7tv9b954xd1roASC/vkehDLbn6ucsDyKYS cIcdGrzE+/rnASYq5DgmjxxxUI5HA1pNcQOVqjvbIMyHHz4SmjCHCu3ga5f8tmYpFi AcDCzbymXaQiIFuUTn2cACn7xYR0wItG/uFXrnkM= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by sourceware.org (Postfix) with ESMTP id 12F9A386F01B for ; Fri, 11 Sep 2020 13:03:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 12F9A386F01B Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-462-z6PKHdIKOAO0ZtccizlVbQ-1; Fri, 11 Sep 2020 09:03:45 -0400 X-MC-Unique: z6PKHdIKOAO0ZtccizlVbQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 363DC18BE163 for ; Fri, 11 Sep 2020 13:03:44 +0000 (UTC) Received: from oldenburg2.str.redhat.com (ovpn-113-142.rdu2.redhat.com [10.10.113.142]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0DA5F821E0 for ; Fri, 11 Sep 2020 13:03:42 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH 1/3] support: Provide a way to reorder responses within the DNS test server Message-Id: <55f9a2d387df8fae7d44275e2cf7dce8eccd120e.1599829382.git.fweimer@redhat.com> Date: Fri, 11 Sep 2020 15:03:40 +0200 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0.002 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" --- support/Makefile | 2 + support/resolv_response_context_duplicate.c | 37 +++++++++++++ support/resolv_response_context_free.c | 28 ++++++++++ support/resolv_test.c | 59 +++++++++++++-------- support/resolv_test.h | 37 +++++++++++-- 5 files changed, 135 insertions(+), 28 deletions(-) create mode 100644 support/resolv_response_context_duplicate.c create mode 100644 support/resolv_response_context_free.c diff --git a/support/Makefile b/support/Makefile index 93faafddf9..4154863511 100644 --- a/support/Makefile +++ b/support/Makefile @@ -35,6 +35,8 @@ libsupport-routines = \ ignore_stderr \ next_to_fault \ oom_error \ + resolv_response_context_duplicate \ + resolv_response_context_free \ resolv_test \ set_fortify_handler \ support-xfstat \ diff --git a/support/resolv_response_context_duplicate.c b/support/resolv_response_context_duplicate.c new file mode 100644 index 0000000000..f9c5c3462a --- /dev/null +++ b/support/resolv_response_context_duplicate.c @@ -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 + . */ + +#include +#include +#include + +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; +} diff --git a/support/resolv_response_context_free.c b/support/resolv_response_context_free.c new file mode 100644 index 0000000000..b88c05ffd4 --- /dev/null +++ b/support/resolv_response_context_free.c @@ -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 + . */ + +#include +#include + +void +resolv_response_context_free (struct resolv_response_context *ctx) +{ + free (ctx->query_buffer); + free (ctx->client_address); + free (ctx); +} diff --git a/support/resolv_test.c b/support/resolv_test.c index 53b7fc41ab..e343c4b516 100644 --- a/support/resolv_test.c +++ b/support/resolv_test.c @@ -434,9 +434,9 @@ resolv_response_buffer (const struct resolv_response_builder *b) return result; } -static struct resolv_response_builder * -response_builder_allocate - (const unsigned char *query_buffer, size_t query_length) +struct resolv_response_builder * +resolv_response_builder_allocate (const unsigned char *query_buffer, + size_t query_length) { struct resolv_response_builder *b = xmalloc (sizeof (*b)); memset (b, 0, offsetof (struct resolv_response_builder, buffer)); @@ -445,8 +445,8 @@ response_builder_allocate return b; } -static void -response_builder_free (struct resolv_response_builder *b) +void +resolv_response_builder_free (struct resolv_response_builder *b) { tdestroy (b->compression_offsets, free); free (b); @@ -661,13 +661,17 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index) struct resolv_response_context ctx = { + .test = obj, + .client_address = &peer, + .client_address_length = peerlen, .query_buffer = query, .query_length = length, .server_index = server_index, .tcp = false, .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 (&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) printf ("info: UDP server %d: sending response:" " %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); else printf ("info: UDP server %d: sending response: %zu bytes" @@ -694,23 +698,31 @@ server_thread_udp_process_one (struct resolv_test *obj, int server_index) if (b->truncate_bytes > 0) printf ("info: truncated by %u bytes\n", b->truncate_bytes); } - size_t to_send = b->offset; - if (to_send < b->truncate_bytes) - to_send = 0; - else - to_send -= b->truncate_bytes; - - /* Ignore most errors here because the other end may have closed - the socket. */ - if (sendto (obj->servers[server_index].socket_udp, - b->buffer, to_send, 0, - (struct sockaddr *) &peer, peerlen) < 0) - TEST_VERIFY_EXIT (errno != EBADF); + resolv_response_send_udp (&ctx, b); } - response_builder_free (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; + if (to_send < b->truncate_bytes) + to_send = 0; + else + to_send -= b->truncate_bytes; + + /* Ignore most errors here because the other end may have closed + the socket. */ + if (sendto (ctx->test->servers[ctx->server_index].socket_udp, + b->buffer, to_send, 0, + ctx->client_address, ctx->client_address_length) < 0) + TEST_VERIFY_EXIT (errno != EBADF); +} + /* UDP thread_callback function. Variant for one thread per server. */ static void @@ -897,14 +909,15 @@ server_thread_tcp_client (void *arg) struct resolv_response_context ctx = { + .test = closure->obj, .query_buffer = query_buffer, .query_length = query_length, .server_index = closure->server_index, .tcp = true, .edns = qinfo.edns, }; - struct resolv_response_builder *b = response_builder_allocate - (query_buffer, query_length); + struct resolv_response_builder *b + = resolv_response_builder_allocate (query_buffer, query_length); closure->obj->config.response_callback (&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); } bool close_flag = b->close; - response_builder_free (b); + resolv_response_builder_free (b); free (query_buffer); if (close_flag) break; diff --git a/support/resolv_test.h b/support/resolv_test.h index 67819469a0..c46de32ff4 100644 --- a/support/resolv_test.h +++ b/support/resolv_test.h @@ -35,25 +35,36 @@ struct resolv_edns_info 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 specified in struct resolv_redirect_config is invoked. */ 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; int server_index; bool tcp; 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 response callback function. */ struct resolv_response_builder; -/* This opaque struct collects information about the resolver testing - currently in progress. */ -struct resolv_test; - enum { /* 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. */ 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 #endif /* SUPPORT_RESOLV_TEST_H */