resolv_test.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* DNS test framework and libresolv redirection.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #ifndef SUPPORT_RESOLV_TEST_H
  16. #define SUPPORT_RESOLV_TEST_H
  17. #include <arpa/nameser.h>
  18. #include <stdbool.h>
  19. #include <sys/cdefs.h>
  20. __BEGIN_DECLS
  21. /* Information about EDNS properties of a DNS query. */
  22. struct resolv_edns_info
  23. {
  24. bool active;
  25. uint8_t extended_rcode;
  26. uint8_t version;
  27. uint16_t flags;
  28. uint16_t payload_size;
  29. };
  30. /* This struct provides context information when the response callback
  31. specified in struct resolv_redirect_config is invoked. */
  32. struct resolv_response_context
  33. {
  34. const unsigned char *query_buffer;
  35. size_t query_length;
  36. int server_index;
  37. bool tcp;
  38. struct resolv_edns_info edns;
  39. };
  40. /* This opaque struct is used to construct responses from within the
  41. response callback function. */
  42. struct resolv_response_builder;
  43. /* This opaque struct collects information about the resolver testing
  44. currently in progress. */
  45. struct resolv_test;
  46. enum
  47. {
  48. /* Maximum number of test servers supported by the framework. */
  49. resolv_max_test_servers = 3,
  50. };
  51. /* Configuration settings specific to individual test servers. */
  52. struct resolv_redirect_server_config
  53. {
  54. bool disable_tcp; /* If true, no TCP server is listening. */
  55. bool disable_udp; /* If true, no UDP server is listening. */
  56. };
  57. /* Instructions for setting up the libresolv redirection. */
  58. struct resolv_redirect_config
  59. {
  60. /* The response_callback function is called for every incoming DNS
  61. packet, over UDP or TCP. It must be specified, the other
  62. configuration settings are optional. */
  63. void (*response_callback) (const struct resolv_response_context *,
  64. struct resolv_response_builder *,
  65. const char *qname,
  66. uint16_t qclass, uint16_t qtype);
  67. /* Per-server configuration. */
  68. struct resolv_redirect_server_config servers[resolv_max_test_servers];
  69. /* Search path entries. The first entry serves as the default
  70. domain name as well. */
  71. const char *search[7];
  72. /* Number of servers to activate in resolv. 0 means the default,
  73. resolv_max_test_servers. */
  74. int nscount;
  75. /* If true, use a single thread to process all UDP queries. This
  76. may results in more predictable ordering of queries and
  77. responses. */
  78. bool single_thread_udp;
  79. /* Do not rewrite the _res variable or change NSS defaults. Use
  80. server_address_overrides below to tell the testing framework on
  81. which addresses to create the servers. */
  82. bool disable_redirect;
  83. /* Use these addresses for creating the DNS servers. The array must
  84. have ns_count (or resolv_max_test_servers) sockaddr * elements if
  85. not NULL. */
  86. const struct sockaddr *const *server_address_overrides;
  87. };
  88. /* Configure NSS to use, nss_dns only for aplicable databases, and try
  89. to put the process into a network namespace for better isolation.
  90. This may have to be called before resolv_test_start, before the
  91. process creates any threads. Otherwise, initialization is
  92. performed by resolv_test_start implicitly. */
  93. void resolv_test_init (void);
  94. /* Initiate resolver testing. This updates the _res variable as
  95. needed. As a side effect, NSS is reconfigured to use nss_dns only
  96. for aplicable databases, and the process may enter a network
  97. namespace for better isolation. */
  98. struct resolv_test *resolv_test_start (struct resolv_redirect_config);
  99. /* Call this function at the end of resolver testing, to free
  100. resources and report pending errors (if any). */
  101. void resolv_test_end (struct resolv_test *);
  102. /* The remaining facilities in this file are used for constructing
  103. response packets from the response_callback function. */
  104. /* Special settings for constructing responses from the callback. */
  105. struct resolv_response_flags
  106. {
  107. /* 4-bit response code to incorporate into the response. */
  108. unsigned char rcode;
  109. /* If true, the TC (truncation) flag will be set. */
  110. bool tc;
  111. /* Initial section count values. Can be used to artificially
  112. increase the counts, for malformed packet testing.*/
  113. unsigned short qdcount;
  114. unsigned short ancount;
  115. unsigned short nscount;
  116. unsigned short adcount;
  117. };
  118. /* Begin a new response with the requested flags. Must be called
  119. first. */
  120. void resolv_response_init (struct resolv_response_builder *,
  121. struct resolv_response_flags);
  122. /* Switches to the section in the response packet. Only forward
  123. movement is supported. */
  124. void resolv_response_section (struct resolv_response_builder *, ns_sect);
  125. /* Add a question record to the question section. */
  126. void resolv_response_add_question (struct resolv_response_builder *,
  127. const char *name, uint16_t class,
  128. uint16_t type);
  129. /* Starts a new resource record with the specified owner name, class,
  130. type, and TTL. Data is supplied with resolv_response_add_data or
  131. resolv_response_add_name. */
  132. void resolv_response_open_record (struct resolv_response_builder *,
  133. const char *name, uint16_t class,
  134. uint16_t type, uint32_t ttl);
  135. /* Add unstructed bytes to the RDATA part of a resource record. */
  136. void resolv_response_add_data (struct resolv_response_builder *,
  137. const void *, size_t);
  138. /* Add a compressed domain name to the RDATA part of a resource
  139. record. */
  140. void resolv_response_add_name (struct resolv_response_builder *,
  141. const char *name);
  142. /* Mark the end of the constructed record. Must be called last. */
  143. void resolv_response_close_record (struct resolv_response_builder *);
  144. /* Drop this query packet (that is, do not send a response, not even
  145. an empty packet). */
  146. void resolv_response_drop (struct resolv_response_builder *);
  147. /* In TCP mode, close the connection after this packet (if a response
  148. is sent). */
  149. void resolv_response_close (struct resolv_response_builder *);
  150. /* The size of the response packet built so far. */
  151. size_t resolv_response_length (const struct resolv_response_builder *);
  152. __END_DECLS
  153. #endif /* SUPPORT_RESOLV_TEST_H */