doh.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef HEADER_CURL_DOH_H
  2. #define HEADER_CURL_DOH_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "urldata.h"
  27. #include "curl_addrinfo.h"
  28. #ifdef USE_HTTPSRR
  29. # include <stdint.h>
  30. # include "httpsrr.h"
  31. #endif
  32. #ifndef CURL_DISABLE_DOH
  33. typedef enum {
  34. DOH_OK,
  35. DOH_DNS_BAD_LABEL, /* 1 */
  36. DOH_DNS_OUT_OF_RANGE, /* 2 */
  37. DOH_DNS_LABEL_LOOP, /* 3 */
  38. DOH_TOO_SMALL_BUFFER, /* 4 */
  39. DOH_OUT_OF_MEM, /* 5 */
  40. DOH_DNS_RDATA_LEN, /* 6 */
  41. DOH_DNS_MALFORMAT, /* 7 */
  42. DOH_DNS_BAD_RCODE, /* 8 - no such name */
  43. DOH_DNS_UNEXPECTED_TYPE, /* 9 */
  44. DOH_DNS_UNEXPECTED_CLASS, /* 10 */
  45. DOH_NO_CONTENT, /* 11 */
  46. DOH_DNS_BAD_ID, /* 12 */
  47. DOH_DNS_NAME_TOO_LONG /* 13 */
  48. } DOHcode;
  49. typedef enum {
  50. CURL_DNS_TYPE_A = 1,
  51. CURL_DNS_TYPE_NS = 2,
  52. CURL_DNS_TYPE_CNAME = 5,
  53. CURL_DNS_TYPE_AAAA = 28,
  54. CURL_DNS_TYPE_DNAME = 39, /* RFC6672 */
  55. CURL_DNS_TYPE_HTTPS = 65
  56. } DNStype;
  57. enum doh_slot_num {
  58. /* Explicit values for first two symbols so as to match hard-coded
  59. * constants in existing code
  60. */
  61. DOH_SLOT_IPV4 = 0, /* make 'V4' stand out for readability */
  62. DOH_SLOT_IPV6 = 1, /* 'V6' likewise */
  63. /* Space here for (possibly build-specific) additional slot definitions */
  64. #ifdef USE_HTTPSRR
  65. DOH_SLOT_HTTPS_RR = 2, /* for HTTPS RR */
  66. #endif
  67. /* for example */
  68. /* #ifdef WANT_DOH_FOOBAR_TXT */
  69. /* DOH_PROBE_SLOT_FOOBAR_TXT, */
  70. /* #endif */
  71. /* AFTER all slot definitions, establish how many we have */
  72. DOH_SLOT_COUNT
  73. };
  74. #define CURL_EZM_DOH_PROBE "ezm:doh-p"
  75. /* the largest one we can make, based on RFCs 1034, 1035 */
  76. #define DOH_MAX_DNSREQ_SIZE (256 + 16)
  77. /* each DoH probe request has this
  78. * as easy meta for CURL_EZM_DOH_PROBE */
  79. struct doh_request {
  80. unsigned char req_body[DOH_MAX_DNSREQ_SIZE];
  81. struct curl_slist *req_hds;
  82. struct dynbuf resp_body;
  83. size_t req_body_len;
  84. DNStype dnstype;
  85. };
  86. struct doh_response {
  87. unsigned int probe_mid;
  88. struct dynbuf body;
  89. DNStype dnstype;
  90. CURLcode result;
  91. };
  92. /* each transfer firing off DoH requests has this
  93. * as easy meta for CURL_EZM_DOH_MASTER */
  94. struct doh_probes {
  95. struct doh_response probe_resp[DOH_SLOT_COUNT];
  96. unsigned int pending; /* still outstanding probes */
  97. int port;
  98. const char *host;
  99. };
  100. /*
  101. * Curl_doh() resolve a name using DoH (DNS-over-HTTPS). It resolves a name
  102. * and returns a 'Curl_addrinfo *' with the address information.
  103. */
  104. struct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
  105. const char *hostname,
  106. int port,
  107. int ip_version,
  108. int *waitp);
  109. CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
  110. struct Curl_dns_entry **dns);
  111. #define DOH_MAX_ADDR 24
  112. #define DOH_MAX_CNAME 4
  113. #define DOH_MAX_HTTPS 4
  114. struct dohaddr {
  115. int type;
  116. union {
  117. unsigned char v4[4]; /* network byte order */
  118. unsigned char v6[16];
  119. } ip;
  120. };
  121. #ifdef USE_HTTPSRR
  122. /*
  123. * These may need escaping when found within an ALPN string
  124. * value.
  125. */
  126. #define COMMA_CHAR ','
  127. #define BACKSLASH_CHAR '\\'
  128. struct dohhttps_rr {
  129. uint16_t len; /* raw encoded length */
  130. unsigned char *val; /* raw encoded octets */
  131. };
  132. #endif
  133. struct dohentry {
  134. struct dynbuf cname[DOH_MAX_CNAME];
  135. struct dohaddr addr[DOH_MAX_ADDR];
  136. int numaddr;
  137. unsigned int ttl;
  138. int numcname;
  139. #ifdef USE_HTTPSRR
  140. struct dohhttps_rr https_rrs[DOH_MAX_HTTPS];
  141. int numhttps_rrs;
  142. #endif
  143. };
  144. void Curl_doh_close(struct Curl_easy *data);
  145. void Curl_doh_cleanup(struct Curl_easy *data);
  146. #ifdef UNITTESTS
  147. UNITTEST DOHcode doh_req_encode(const char *host,
  148. DNStype dnstype,
  149. unsigned char *dnsp, /* buffer */
  150. size_t len, /* buffer size */
  151. size_t *olen); /* output length */
  152. UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
  153. size_t dohlen,
  154. DNStype dnstype,
  155. struct dohentry *d);
  156. UNITTEST void de_init(struct dohentry *d);
  157. UNITTEST void de_cleanup(struct dohentry *d);
  158. #endif
  159. #else /* if DoH is disabled */
  160. #define Curl_doh(a,b,c,d,e) NULL
  161. #define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
  162. #endif
  163. #endif /* HEADER_CURL_DOH_H */