You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dns_private.h 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /* Copyright (c) 2014, Vsevolod Stakhov
  2. * All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. *
  12. * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
  13. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
  16. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  17. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  18. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  19. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  20. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  21. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. */
  23. #ifndef DNS_PRIVATE_H_
  24. #define DNS_PRIVATE_H_
  25. #include "config.h"
  26. #include "uthash.h"
  27. #include "utlist.h"
  28. #include "rdns.h"
  29. #include "upstream.h"
  30. #include "ref.h"
  31. static const int dns_port = 53;
  32. static const int default_io_cnt = 8;
  33. #define UDP_PACKET_SIZE 4096
  34. #define DNS_COMPRESSION_BITS 0xC0
  35. #define DNS_D_MAXLABEL 63 /* + 1 '\0' */
  36. #define DNS_D_MAXNAME 255 /* + 1 '\0' */
  37. #define RESOLV_CONF "/etc/resolv.conf"
  38. /**
  39. * Represents DNS server
  40. */
  41. struct rdns_server {
  42. char *name;
  43. unsigned int port;
  44. unsigned int io_cnt;
  45. struct rdns_io_channel **io_channels;
  46. void *ups_elt;
  47. upstream_entry_t up;
  48. };
  49. enum rdns_request_state {
  50. RDNS_REQUEST_NEW = 0,
  51. RDNS_REQUEST_REGISTERED = 1,
  52. RDNS_REQUEST_WAIT_SEND,
  53. RDNS_REQUEST_WAIT_REPLY,
  54. RDNS_REQUEST_REPLIED,
  55. RDNS_REQUEST_FAKE,
  56. };
  57. struct rdns_request {
  58. struct rdns_resolver *resolver;
  59. struct rdns_async_context *async;
  60. struct rdns_io_channel *io;
  61. struct rdns_reply *reply;
  62. enum rdns_request_type type;
  63. double timeout;
  64. unsigned int retransmits;
  65. int id;
  66. struct rdns_request_name *requested_names;
  67. unsigned int qcount;
  68. enum rdns_request_state state;
  69. uint8_t *packet;
  70. off_t pos;
  71. unsigned int packet_len;
  72. dns_callback_type func;
  73. void *arg;
  74. void *async_event;
  75. #if defined(TWEETNACL) || defined(USE_RSPAMD_CRYPTOBOX)
  76. void *curve_plugin_data;
  77. #endif
  78. UT_hash_handle hh;
  79. ref_entry_t ref;
  80. };
  81. /**
  82. * IO channel for a specific DNS server
  83. */
  84. struct rdns_io_channel {
  85. struct rdns_server *srv;
  86. struct rdns_resolver *resolver;
  87. struct sockaddr *saddr;
  88. socklen_t slen;
  89. int sock; /**< persistent socket */
  90. bool active;
  91. bool connected;
  92. void *async_io; /** async opaque ptr */
  93. struct rdns_request *requests; /**< requests in flight */
  94. uint64_t uses;
  95. ref_entry_t ref;
  96. };
  97. struct rdns_fake_reply_idx {
  98. enum rdns_request_type type;
  99. unsigned len;
  100. char request[0];
  101. };
  102. struct rdns_fake_reply {
  103. enum dns_rcode rcode;
  104. struct rdns_reply_entry *result;
  105. UT_hash_handle hh;
  106. struct rdns_fake_reply_idx key;
  107. };
  108. struct rdns_resolver {
  109. struct rdns_server *servers;
  110. struct rdns_io_channel *io_channels; /**< hash of io chains indexed by socket */
  111. struct rdns_async_context *async; /** async callbacks */
  112. void *periodic; /** periodic event for resolver */
  113. struct rdns_upstream_context *ups;
  114. struct rdns_plugin *curve_plugin;
  115. struct rdns_fake_reply *fake_elts;
  116. #ifdef __GNUC__
  117. __attribute__((format(printf, 4, 0)))
  118. #endif
  119. rdns_log_function logger;
  120. void *log_data;
  121. enum rdns_log_level log_level;
  122. uint64_t max_ioc_uses;
  123. void *refresh_ioc_periodic;
  124. bool async_binded;
  125. bool initialized;
  126. bool enable_dnssec;
  127. int flags;
  128. ref_entry_t ref;
  129. };
  130. struct dns_header;
  131. struct dns_query;
  132. /* Internal DNS structs */
  133. struct dns_header {
  134. unsigned int qid :16;
  135. #if BYTE_ORDER == BIG_ENDIAN
  136. unsigned int qr:1;
  137. unsigned int opcode:4;
  138. unsigned int aa:1;
  139. unsigned int tc:1;
  140. unsigned int rd:1;
  141. unsigned int ra:1;
  142. unsigned int cd : 1;
  143. unsigned int ad : 1;
  144. unsigned int z : 1;
  145. unsigned int rcode:4;
  146. #else
  147. unsigned int rd :1;
  148. unsigned int tc :1;
  149. unsigned int aa :1;
  150. unsigned int opcode :4;
  151. unsigned int qr :1;
  152. unsigned int rcode :4;
  153. unsigned int z : 1;
  154. unsigned int ad : 1;
  155. unsigned int cd : 1;
  156. unsigned int ra :1;
  157. #endif
  158. unsigned int qdcount :16;
  159. unsigned int ancount :16;
  160. unsigned int nscount :16;
  161. unsigned int arcount :16;
  162. };
  163. enum dns_section {
  164. DNS_S_QD = 0x01,
  165. #define DNS_S_QUESTION DNS_S_QD
  166. DNS_S_AN = 0x02,
  167. #define DNS_S_ANSWER DNS_S_AN
  168. DNS_S_NS = 0x04,
  169. #define DNS_S_AUTHORITY DNS_S_NS
  170. DNS_S_AR = 0x08,
  171. #define DNS_S_ADDITIONAL DNS_S_AR
  172. DNS_S_ALL = 0x0f
  173. };
  174. /* enum dns_section */
  175. enum dns_opcode {
  176. DNS_OP_QUERY = 0,
  177. DNS_OP_IQUERY = 1,
  178. DNS_OP_STATUS = 2,
  179. DNS_OP_NOTIFY = 4,
  180. DNS_OP_UPDATE = 5,
  181. };
  182. /* dns_opcode */
  183. enum dns_class {
  184. DNS_C_IN = 1,
  185. DNS_C_ANY = 255
  186. };
  187. /* enum dns_class */
  188. struct dns_query {
  189. char *qname;
  190. unsigned int qtype :16;
  191. unsigned int qclass :16;
  192. };
  193. enum dns_type {
  194. DNS_T_A = RDNS_REQUEST_A,
  195. DNS_T_NS = RDNS_REQUEST_NS,
  196. DNS_T_CNAME = 5,
  197. DNS_T_SOA = RDNS_REQUEST_SOA,
  198. DNS_T_PTR = RDNS_REQUEST_PTR,
  199. DNS_T_MX = RDNS_REQUEST_MX,
  200. DNS_T_TXT = RDNS_REQUEST_TXT,
  201. DNS_T_AAAA = RDNS_REQUEST_AAAA,
  202. DNS_T_SRV = RDNS_REQUEST_SRV,
  203. DNS_T_OPT = 41,
  204. DNS_T_SSHFP = 44,
  205. DNS_T_TLSA = RDNS_REQUEST_TLSA,
  206. DNS_T_SPF = RDNS_REQUEST_SPF,
  207. DNS_T_ALL = RDNS_REQUEST_ANY
  208. };
  209. /* enum dns_type */
  210. static const char dns_rcodes[][32] = {
  211. [RDNS_RC_NOERROR] = "no error",
  212. [RDNS_RC_FORMERR] = "query format error",
  213. [RDNS_RC_SERVFAIL] = "server fail",
  214. [RDNS_RC_NXDOMAIN] = "no records with this name",
  215. [RDNS_RC_NOTIMP] = "not implemented",
  216. [RDNS_RC_REFUSED] = "query refused",
  217. [RDNS_RC_YXDOMAIN] = "YXDOMAIN",
  218. [RDNS_RC_YXRRSET] = "YXRRSET",
  219. [RDNS_RC_NXRRSET] = "NXRRSET",
  220. [RDNS_RC_NOTAUTH] = "not authorized",
  221. [RDNS_RC_NOTZONE] = "no such zone",
  222. [RDNS_RC_TIMEOUT] = "query timed out",
  223. [RDNS_RC_NETERR] = "network error",
  224. [RDNS_RC_NOREC] = "requested record is not found"
  225. };
  226. static const char dns_types[][16] = {
  227. [RDNS_REQUEST_A] = "A request",
  228. [RDNS_REQUEST_NS] = "NS request",
  229. [RDNS_REQUEST_PTR] = "PTR request",
  230. [RDNS_REQUEST_MX] = "MX request",
  231. [RDNS_REQUEST_TXT] = "TXT request",
  232. [RDNS_REQUEST_SRV] = "SRV request",
  233. [RDNS_REQUEST_SPF] = "SPF request",
  234. [RDNS_REQUEST_AAAA] = "AAAA request",
  235. [RDNS_REQUEST_TLSA] = "TLSA request",
  236. [RDNS_REQUEST_ANY] = "ANY request"
  237. };
  238. #endif /* DNS_PRIVATE_H_ */