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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 "uthash.h"
  26. #include "utlist.h"
  27. #include "rdns.h"
  28. #include "upstream.h"
  29. #include "ref.h"
  30. static const int dns_port = 53;
  31. static const int default_io_cnt = 8;
  32. #define UDP_PACKET_SIZE 4096
  33. #define DNS_COMPRESSION_BITS 0xC0
  34. #define DNS_D_MAXLABEL 63 /* + 1 '\0' */
  35. #define DNS_D_MAXNAME 255 /* + 1 '\0' */
  36. #define RESOLV_CONF "/etc/resolv.conf"
  37. /**
  38. * Represents DNS server
  39. */
  40. struct rdns_server {
  41. char *name;
  42. unsigned int port;
  43. unsigned int io_cnt;
  44. struct rdns_io_channel **io_channels;
  45. void *ups_elt;
  46. upstream_entry_t up;
  47. };
  48. struct rdns_request {
  49. struct rdns_resolver *resolver;
  50. struct rdns_async_context *async;
  51. struct rdns_io_channel *io;
  52. struct rdns_reply *reply;
  53. enum rdns_request_type type;
  54. double timeout;
  55. unsigned int retransmits;
  56. int id;
  57. struct rdns_request_name *requested_names;
  58. unsigned int qcount;
  59. enum {
  60. RDNS_REQUEST_NEW = 0,
  61. RDNS_REQUEST_REGISTERED = 1,
  62. RDNS_REQUEST_WAIT_SEND,
  63. RDNS_REQUEST_WAIT_REPLY,
  64. RDNS_REQUEST_REPLIED
  65. } state;
  66. uint8_t *packet;
  67. off_t pos;
  68. unsigned int packet_len;
  69. dns_callback_type func;
  70. void *arg;
  71. void *async_event;
  72. #ifdef TWEETNACL
  73. void *curve_plugin_data;
  74. #endif
  75. UT_hash_handle hh;
  76. ref_entry_t ref;
  77. };
  78. /**
  79. * IO channel for a specific DNS server
  80. */
  81. struct rdns_io_channel {
  82. struct rdns_server *srv;
  83. struct rdns_resolver *resolver;
  84. int sock; /**< persistent socket */
  85. bool active;
  86. void *async_io; /** async opaque ptr */
  87. struct rdns_request *requests; /**< requests in flight */
  88. uint64_t uses;
  89. ref_entry_t ref;
  90. };
  91. struct rdns_resolver {
  92. struct rdns_server *servers;
  93. struct rdns_io_channel *io_channels; /**< hash of io chains indexed by socket */
  94. struct rdns_async_context *async; /** async callbacks */
  95. void *periodic; /** periodic event for resolver */
  96. struct rdns_upstream_context *ups;
  97. struct rdns_plugin *curve_plugin;
  98. rdns_log_function logger;
  99. void *log_data;
  100. enum rdns_log_level log_level;
  101. uint64_t max_ioc_uses;
  102. void *refresh_ioc_periodic;
  103. bool async_binded;
  104. bool initialized;
  105. bool enable_dnssec;
  106. ref_entry_t ref;
  107. };
  108. struct dns_header;
  109. struct dns_query;
  110. /* Internal DNS structs */
  111. struct dns_header {
  112. unsigned int qid :16;
  113. #if BYTE_ORDER == BIG_ENDIAN
  114. unsigned int qr:1;
  115. unsigned int opcode:4;
  116. unsigned int aa:1;
  117. unsigned int tc:1;
  118. unsigned int rd:1;
  119. unsigned int ra:1;
  120. unsigned int cd : 1;
  121. unsigned int ad : 1;
  122. unsigned int z : 1;
  123. unsigned int rcode:4;
  124. #else
  125. unsigned int rd :1;
  126. unsigned int tc :1;
  127. unsigned int aa :1;
  128. unsigned int opcode :4;
  129. unsigned int qr :1;
  130. unsigned int rcode :4;
  131. unsigned int z : 1;
  132. unsigned int ad : 1;
  133. unsigned int cd : 1;
  134. unsigned int ra :1;
  135. #endif
  136. unsigned int qdcount :16;
  137. unsigned int ancount :16;
  138. unsigned int nscount :16;
  139. unsigned int arcount :16;
  140. };
  141. enum dns_section {
  142. DNS_S_QD = 0x01,
  143. #define DNS_S_QUESTION DNS_S_QD
  144. DNS_S_AN = 0x02,
  145. #define DNS_S_ANSWER DNS_S_AN
  146. DNS_S_NS = 0x04,
  147. #define DNS_S_AUTHORITY DNS_S_NS
  148. DNS_S_AR = 0x08,
  149. #define DNS_S_ADDITIONAL DNS_S_AR
  150. DNS_S_ALL = 0x0f
  151. };
  152. /* enum dns_section */
  153. enum dns_opcode {
  154. DNS_OP_QUERY = 0,
  155. DNS_OP_IQUERY = 1,
  156. DNS_OP_STATUS = 2,
  157. DNS_OP_NOTIFY = 4,
  158. DNS_OP_UPDATE = 5,
  159. };
  160. /* dns_opcode */
  161. enum dns_class {
  162. DNS_C_IN = 1,
  163. DNS_C_ANY = 255
  164. };
  165. /* enum dns_class */
  166. struct dns_query {
  167. char *qname;
  168. unsigned int qtype :16;
  169. unsigned int qclass :16;
  170. };
  171. enum dns_type {
  172. DNS_T_A = RDNS_REQUEST_A,
  173. DNS_T_NS = RDNS_REQUEST_NS,
  174. DNS_T_CNAME = 5,
  175. DNS_T_SOA = RDNS_REQUEST_SOA,
  176. DNS_T_PTR = RDNS_REQUEST_PTR,
  177. DNS_T_MX = RDNS_REQUEST_MX,
  178. DNS_T_TXT = RDNS_REQUEST_TXT,
  179. DNS_T_AAAA = RDNS_REQUEST_AAAA,
  180. DNS_T_SRV = RDNS_REQUEST_SRV,
  181. DNS_T_OPT = 41,
  182. DNS_T_SSHFP = 44,
  183. DNS_T_TLSA = RDNS_REQUEST_TLSA,
  184. DNS_T_SPF = RDNS_REQUEST_SPF,
  185. DNS_T_ALL = RDNS_REQUEST_ANY
  186. };
  187. /* enum dns_type */
  188. static const char dns_rcodes[][32] = {
  189. [RDNS_RC_NOERROR] = "no error",
  190. [RDNS_RC_FORMERR] = "query format error",
  191. [RDNS_RC_SERVFAIL] = "server fail",
  192. [RDNS_RC_NXDOMAIN] = "no records with this name",
  193. [RDNS_RC_NOTIMP] = "not implemented",
  194. [RDNS_RC_REFUSED] = "query refused",
  195. [RDNS_RC_YXDOMAIN] = "YXDOMAIN",
  196. [RDNS_RC_YXRRSET] = "YXRRSET",
  197. [RDNS_RC_NXRRSET] = "NXRRSET",
  198. [RDNS_RC_NOTAUTH] = "not authorized",
  199. [RDNS_RC_NOTZONE] = "no such zone",
  200. [RDNS_RC_TIMEOUT] = "query timed out",
  201. [RDNS_RC_NETERR] = "network error",
  202. [RDNS_RC_NOREC] = "requested record is not found"
  203. };
  204. static const char dns_types[][16] = {
  205. [RDNS_REQUEST_A] = "A request",
  206. [RDNS_REQUEST_NS] = "NS request",
  207. [RDNS_REQUEST_PTR] = "PTR request",
  208. [RDNS_REQUEST_MX] = "MX request",
  209. [RDNS_REQUEST_TXT] = "TXT request",
  210. [RDNS_REQUEST_SRV] = "SRV request",
  211. [RDNS_REQUEST_SPF] = "SPF request",
  212. [RDNS_REQUEST_AAAA] = "AAAA request",
  213. [RDNS_REQUEST_TLSA] = "TLSA request",
  214. [RDNS_REQUEST_ANY] = "ANY request"
  215. };
  216. #endif /* DNS_PRIVATE_H_ */