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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. int sock; /**< persistent socket */
  88. bool active;
  89. void *async_io; /** async opaque ptr */
  90. struct rdns_request *requests; /**< requests in flight */
  91. uint64_t uses;
  92. ref_entry_t ref;
  93. };
  94. struct rdns_fake_reply_idx {
  95. enum rdns_request_type type;
  96. unsigned len;
  97. char request[0];
  98. };
  99. struct rdns_fake_reply {
  100. enum dns_rcode rcode;
  101. struct rdns_reply_entry *result;
  102. UT_hash_handle hh;
  103. struct rdns_fake_reply_idx key;
  104. };
  105. struct rdns_resolver {
  106. struct rdns_server *servers;
  107. struct rdns_io_channel *io_channels; /**< hash of io chains indexed by socket */
  108. struct rdns_async_context *async; /** async callbacks */
  109. void *periodic; /** periodic event for resolver */
  110. struct rdns_upstream_context *ups;
  111. struct rdns_plugin *curve_plugin;
  112. struct rdns_fake_reply *fake_elts;
  113. rdns_log_function logger;
  114. void *log_data;
  115. enum rdns_log_level log_level;
  116. uint64_t max_ioc_uses;
  117. void *refresh_ioc_periodic;
  118. bool async_binded;
  119. bool initialized;
  120. bool enable_dnssec;
  121. ref_entry_t ref;
  122. };
  123. struct dns_header;
  124. struct dns_query;
  125. /* Internal DNS structs */
  126. struct dns_header {
  127. unsigned int qid :16;
  128. #if BYTE_ORDER == BIG_ENDIAN
  129. unsigned int qr:1;
  130. unsigned int opcode:4;
  131. unsigned int aa:1;
  132. unsigned int tc:1;
  133. unsigned int rd:1;
  134. unsigned int ra:1;
  135. unsigned int cd : 1;
  136. unsigned int ad : 1;
  137. unsigned int z : 1;
  138. unsigned int rcode:4;
  139. #else
  140. unsigned int rd :1;
  141. unsigned int tc :1;
  142. unsigned int aa :1;
  143. unsigned int opcode :4;
  144. unsigned int qr :1;
  145. unsigned int rcode :4;
  146. unsigned int z : 1;
  147. unsigned int ad : 1;
  148. unsigned int cd : 1;
  149. unsigned int ra :1;
  150. #endif
  151. unsigned int qdcount :16;
  152. unsigned int ancount :16;
  153. unsigned int nscount :16;
  154. unsigned int arcount :16;
  155. };
  156. enum dns_section {
  157. DNS_S_QD = 0x01,
  158. #define DNS_S_QUESTION DNS_S_QD
  159. DNS_S_AN = 0x02,
  160. #define DNS_S_ANSWER DNS_S_AN
  161. DNS_S_NS = 0x04,
  162. #define DNS_S_AUTHORITY DNS_S_NS
  163. DNS_S_AR = 0x08,
  164. #define DNS_S_ADDITIONAL DNS_S_AR
  165. DNS_S_ALL = 0x0f
  166. };
  167. /* enum dns_section */
  168. enum dns_opcode {
  169. DNS_OP_QUERY = 0,
  170. DNS_OP_IQUERY = 1,
  171. DNS_OP_STATUS = 2,
  172. DNS_OP_NOTIFY = 4,
  173. DNS_OP_UPDATE = 5,
  174. };
  175. /* dns_opcode */
  176. enum dns_class {
  177. DNS_C_IN = 1,
  178. DNS_C_ANY = 255
  179. };
  180. /* enum dns_class */
  181. struct dns_query {
  182. char *qname;
  183. unsigned int qtype :16;
  184. unsigned int qclass :16;
  185. };
  186. enum dns_type {
  187. DNS_T_A = RDNS_REQUEST_A,
  188. DNS_T_NS = RDNS_REQUEST_NS,
  189. DNS_T_CNAME = 5,
  190. DNS_T_SOA = RDNS_REQUEST_SOA,
  191. DNS_T_PTR = RDNS_REQUEST_PTR,
  192. DNS_T_MX = RDNS_REQUEST_MX,
  193. DNS_T_TXT = RDNS_REQUEST_TXT,
  194. DNS_T_AAAA = RDNS_REQUEST_AAAA,
  195. DNS_T_SRV = RDNS_REQUEST_SRV,
  196. DNS_T_OPT = 41,
  197. DNS_T_SSHFP = 44,
  198. DNS_T_TLSA = RDNS_REQUEST_TLSA,
  199. DNS_T_SPF = RDNS_REQUEST_SPF,
  200. DNS_T_ALL = RDNS_REQUEST_ANY
  201. };
  202. /* enum dns_type */
  203. static const char dns_rcodes[][32] = {
  204. [RDNS_RC_NOERROR] = "no error",
  205. [RDNS_RC_FORMERR] = "query format error",
  206. [RDNS_RC_SERVFAIL] = "server fail",
  207. [RDNS_RC_NXDOMAIN] = "no records with this name",
  208. [RDNS_RC_NOTIMP] = "not implemented",
  209. [RDNS_RC_REFUSED] = "query refused",
  210. [RDNS_RC_YXDOMAIN] = "YXDOMAIN",
  211. [RDNS_RC_YXRRSET] = "YXRRSET",
  212. [RDNS_RC_NXRRSET] = "NXRRSET",
  213. [RDNS_RC_NOTAUTH] = "not authorized",
  214. [RDNS_RC_NOTZONE] = "no such zone",
  215. [RDNS_RC_TIMEOUT] = "query timed out",
  216. [RDNS_RC_NETERR] = "network error",
  217. [RDNS_RC_NOREC] = "requested record is not found"
  218. };
  219. static const char dns_types[][16] = {
  220. [RDNS_REQUEST_A] = "A request",
  221. [RDNS_REQUEST_NS] = "NS request",
  222. [RDNS_REQUEST_PTR] = "PTR request",
  223. [RDNS_REQUEST_MX] = "MX request",
  224. [RDNS_REQUEST_TXT] = "TXT request",
  225. [RDNS_REQUEST_SRV] = "SRV request",
  226. [RDNS_REQUEST_SPF] = "SPF request",
  227. [RDNS_REQUEST_AAAA] = "AAAA request",
  228. [RDNS_REQUEST_TLSA] = "TLSA request",
  229. [RDNS_REQUEST_ANY] = "ANY request"
  230. };
  231. #endif /* DNS_PRIVATE_H_ */