Blame support/resolv_test.h

Packit 6c4009
/* DNS test framework and libresolv redirection.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#ifndef SUPPORT_RESOLV_TEST_H
Packit 6c4009
#define SUPPORT_RESOLV_TEST_H
Packit 6c4009
Packit 6c4009
#include <arpa/nameser.h>
Packit 6c4009
#include <stdbool.h>
Packit 6c4009
#include <sys/cdefs.h>
Packit 6c4009
Packit 6c4009
__BEGIN_DECLS
Packit 6c4009
Packit 6c4009
/* Information about EDNS properties of a DNS query.  */
Packit 6c4009
struct resolv_edns_info
Packit 6c4009
{
Packit 6c4009
  bool active;
Packit 6c4009
  uint8_t extended_rcode;
Packit 6c4009
  uint8_t version;
Packit 6c4009
  uint16_t flags;
Packit 6c4009
  uint16_t payload_size;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* This struct provides context information when the response callback
Packit 6c4009
   specified in struct resolv_redirect_config is invoked. */
Packit 6c4009
struct resolv_response_context
Packit 6c4009
{
Packit Bot 0c2104
  const unsigned char *query_buffer;
Packit 6c4009
  size_t query_length;
Packit 6c4009
  int server_index;
Packit 6c4009
  bool tcp;
Packit 6c4009
  struct resolv_edns_info edns;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* This opaque struct is used to construct responses from within the
Packit 6c4009
   response callback function.  */
Packit 6c4009
struct resolv_response_builder;
Packit 6c4009
Packit Bot 0c2104
/* This opaque struct collects information about the resolver testing
Packit Bot 0c2104
   currently in progress.  */
Packit Bot 0c2104
struct resolv_test;
Packit Bot 0c2104
Packit 6c4009
enum
Packit 6c4009
  {
Packit 6c4009
    /* Maximum number of test servers supported by the framework.  */
Packit 6c4009
    resolv_max_test_servers = 3,
Packit 6c4009
  };
Packit 6c4009
Packit 6c4009
/* Configuration settings specific to individual test servers.  */
Packit 6c4009
struct resolv_redirect_server_config
Packit 6c4009
{
Packit 6c4009
  bool disable_tcp;             /* If true, no TCP server is listening.  */
Packit 6c4009
  bool disable_udp;             /* If true, no UDP server is listening.  */
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Instructions for setting up the libresolv redirection.  */
Packit 6c4009
struct resolv_redirect_config
Packit 6c4009
{
Packit 6c4009
  /* The response_callback function is called for every incoming DNS
Packit 6c4009
     packet, over UDP or TCP.  It must be specified, the other
Packit 6c4009
     configuration settings are optional.  */
Packit 6c4009
  void (*response_callback) (const struct resolv_response_context *,
Packit 6c4009
                             struct resolv_response_builder *,
Packit 6c4009
                             const char *qname,
Packit 6c4009
                             uint16_t qclass, uint16_t qtype);
Packit 6c4009
Packit 6c4009
  /* Per-server configuration.  */
Packit 6c4009
  struct resolv_redirect_server_config servers[resolv_max_test_servers];
Packit 6c4009
Packit 6c4009
  /* Search path entries.  The first entry serves as the default
Packit 6c4009
     domain name as well.  */
Packit 6c4009
  const char *search[7];
Packit 6c4009
Packit 6c4009
  /* Number of servers to activate in resolv.  0 means the default,
Packit 6c4009
     resolv_max_test_servers.  */
Packit 6c4009
  int nscount;
Packit 6c4009
Packit 6c4009
  /* If true, use a single thread to process all UDP queries.  This
Packit 6c4009
     may results in more predictable ordering of queries and
Packit 6c4009
     responses.  */
Packit 6c4009
  bool single_thread_udp;
Packit 6c4009
Packit 6c4009
  /* Do not rewrite the _res variable or change NSS defaults.  Use
Packit 6c4009
     server_address_overrides below to tell the testing framework on
Packit 6c4009
     which addresses to create the servers.  */
Packit 6c4009
  bool disable_redirect;
Packit 6c4009
Packit 6c4009
  /* Use these addresses for creating the DNS servers.  The array must
Packit 6c4009
     have ns_count (or resolv_max_test_servers) sockaddr * elements if
Packit 6c4009
     not NULL.  */
Packit 6c4009
  const struct sockaddr *const *server_address_overrides;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Configure NSS to use, nss_dns only for aplicable databases, and try
Packit 6c4009
   to put the process into a network namespace for better isolation.
Packit 6c4009
   This may have to be called before resolv_test_start, before the
Packit 6c4009
   process creates any threads.  Otherwise, initialization is
Packit 6c4009
   performed by resolv_test_start implicitly.  */
Packit 6c4009
void resolv_test_init (void);
Packit 6c4009
Packit 6c4009
/* Initiate resolver testing.  This updates the _res variable as
Packit 6c4009
   needed.  As a side effect, NSS is reconfigured to use nss_dns only
Packit 6c4009
   for aplicable databases, and the process may enter a network
Packit 6c4009
   namespace for better isolation.  */
Packit 6c4009
struct resolv_test *resolv_test_start (struct resolv_redirect_config);
Packit 6c4009
Packit 6c4009
/* Call this function at the end of resolver testing, to free
Packit 6c4009
   resources and report pending errors (if any).  */
Packit 6c4009
void resolv_test_end (struct resolv_test *);
Packit 6c4009
Packit 6c4009
/* The remaining facilities in this file are used for constructing
Packit 6c4009
   response packets from the response_callback function.  */
Packit 6c4009
Packit 6c4009
/* Special settings for constructing responses from the callback.  */
Packit 6c4009
struct resolv_response_flags
Packit 6c4009
{
Packit 6c4009
  /* 4-bit response code to incorporate into the response. */
Packit 6c4009
  unsigned char rcode;
Packit 6c4009
Packit 6c4009
  /* If true, the TC (truncation) flag will be set.  */
Packit 6c4009
  bool tc;
Packit 6c4009
Packit 6c4009
  /* Initial section count values.  Can be used to artificially
Packit 6c4009
     increase the counts, for malformed packet testing.*/
Packit 6c4009
  unsigned short qdcount;
Packit 6c4009
  unsigned short ancount;
Packit 6c4009
  unsigned short nscount;
Packit 6c4009
  unsigned short adcount;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Begin a new response with the requested flags.  Must be called
Packit 6c4009
   first.  */
Packit 6c4009
void resolv_response_init (struct resolv_response_builder *,
Packit 6c4009
                           struct resolv_response_flags);
Packit 6c4009
Packit 6c4009
/* Switches to the section in the response packet.  Only forward
Packit 6c4009
   movement is supported.  */
Packit 6c4009
void resolv_response_section (struct resolv_response_builder *, ns_sect);
Packit 6c4009
Packit 6c4009
/* Add a question record to the question section.  */
Packit 6c4009
void resolv_response_add_question (struct resolv_response_builder *,
Packit 6c4009
                                   const char *name, uint16_t class,
Packit 6c4009
                                   uint16_t type);
Packit 6c4009
/* Starts a new resource record with the specified owner name, class,
Packit 6c4009
   type, and TTL.  Data is supplied with resolv_response_add_data or
Packit 6c4009
   resolv_response_add_name.  */
Packit 6c4009
void resolv_response_open_record (struct resolv_response_builder *,
Packit 6c4009
                                  const char *name, uint16_t class,
Packit 6c4009
                                  uint16_t type, uint32_t ttl);
Packit 6c4009
Packit 6c4009
/* Add unstructed bytes to the RDATA part of a resource record.  */
Packit 6c4009
void resolv_response_add_data (struct resolv_response_builder *,
Packit 6c4009
                               const void *, size_t);
Packit 6c4009
Packit 6c4009
/* Add a compressed domain name to the RDATA part of a resource
Packit 6c4009
   record.  */
Packit 6c4009
void resolv_response_add_name (struct resolv_response_builder *,
Packit 6c4009
                               const char *name);
Packit 6c4009
Packit 6c4009
/* Mark the end of the constructed record.  Must be called last.  */
Packit 6c4009
void resolv_response_close_record (struct resolv_response_builder *);
Packit 6c4009
Packit 6c4009
/* Drop this query packet (that is, do not send a response, not even
Packit 6c4009
   an empty packet).  */
Packit 6c4009
void resolv_response_drop (struct resolv_response_builder *);
Packit 6c4009
Packit 6c4009
/* In TCP mode, close the connection after this packet (if a response
Packit 6c4009
   is sent).  */
Packit 6c4009
void resolv_response_close (struct resolv_response_builder *);
Packit 6c4009
Packit 6c4009
/* The size of the response packet built so far.  */
Packit 6c4009
size_t resolv_response_length (const struct resolv_response_builder *);
Packit 6c4009
Packit 6c4009
__END_DECLS
Packit 6c4009
Packit 6c4009
#endif /* SUPPORT_RESOLV_TEST_H */