doh.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, <daniel@haxx.se>, 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. #endif
  31. #ifndef CURL_DISABLE_DOH
  32. typedef enum {
  33. DOH_OK,
  34. DOH_DNS_BAD_LABEL, /* 1 */
  35. DOH_DNS_OUT_OF_RANGE, /* 2 */
  36. DOH_DNS_LABEL_LOOP, /* 3 */
  37. DOH_TOO_SMALL_BUFFER, /* 4 */
  38. DOH_OUT_OF_MEM, /* 5 */
  39. DOH_DNS_RDATA_LEN, /* 6 */
  40. DOH_DNS_MALFORMAT, /* 7 */
  41. DOH_DNS_BAD_RCODE, /* 8 - no such name */
  42. DOH_DNS_UNEXPECTED_TYPE, /* 9 */
  43. DOH_DNS_UNEXPECTED_CLASS, /* 10 */
  44. DOH_NO_CONTENT, /* 11 */
  45. DOH_DNS_BAD_ID, /* 12 */
  46. DOH_DNS_NAME_TOO_LONG /* 13 */
  47. } DOHcode;
  48. typedef enum {
  49. DNS_TYPE_A = 1,
  50. DNS_TYPE_NS = 2,
  51. DNS_TYPE_CNAME = 5,
  52. DNS_TYPE_AAAA = 28,
  53. DNS_TYPE_DNAME = 39, /* RFC6672 */
  54. DNS_TYPE_HTTPS = 65
  55. } DNStype;
  56. /* one of these for each DoH request */
  57. struct doh_probe {
  58. curl_off_t easy_mid; /* multi id of easy handle doing the lookup */
  59. DNStype dnstype;
  60. unsigned char req_body[512];
  61. size_t req_body_len;
  62. struct dynbuf resp_body;
  63. };
  64. enum doh_slot_num {
  65. /* Explicit values for first two symbols so as to match hard-coded
  66. * constants in existing code
  67. */
  68. DOH_SLOT_IPV4 = 0, /* make 'V4' stand out for readability */
  69. DOH_SLOT_IPV6 = 1, /* 'V6' likewise */
  70. /* Space here for (possibly build-specific) additional slot definitions */
  71. #ifdef USE_HTTPSRR
  72. DOH_SLOT_HTTPS_RR = 2, /* for HTTPS RR */
  73. #endif
  74. /* for example */
  75. /* #ifdef WANT_DOH_FOOBAR_TXT */
  76. /* DOH_PROBE_SLOT_FOOBAR_TXT, */
  77. /* #endif */
  78. /* AFTER all slot definitions, establish how many we have */
  79. DOH_SLOT_COUNT
  80. };
  81. struct doh_probes {
  82. struct curl_slist *req_hds;
  83. struct doh_probe probe[DOH_SLOT_COUNT];
  84. unsigned int pending; /* still outstanding probes */
  85. int port;
  86. const char *host;
  87. };
  88. /*
  89. * Curl_doh() resolve a name using DoH (DNS-over-HTTPS). It resolves a name
  90. * and returns a 'Curl_addrinfo *' with the address information.
  91. */
  92. struct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
  93. const char *hostname,
  94. int port,
  95. int *waitp);
  96. CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
  97. struct Curl_dns_entry **dns);
  98. #define DOH_MAX_ADDR 24
  99. #define DOH_MAX_CNAME 4
  100. #define DOH_MAX_HTTPS 4
  101. struct dohaddr {
  102. int type;
  103. union {
  104. unsigned char v4[4]; /* network byte order */
  105. unsigned char v6[16];
  106. } ip;
  107. };
  108. #ifdef USE_HTTPSRR
  109. /*
  110. * These are the code points for DNS wire format SvcParams as
  111. * per draft-ietf-dnsop-svcb-https
  112. * Not all are supported now, and even those that are may need
  113. * more work in future to fully support the spec.
  114. */
  115. #define HTTPS_RR_CODE_ALPN 0x01
  116. #define HTTPS_RR_CODE_NO_DEF_ALPN 0x02
  117. #define HTTPS_RR_CODE_PORT 0x03
  118. #define HTTPS_RR_CODE_IPV4 0x04
  119. #define HTTPS_RR_CODE_ECH 0x05
  120. #define HTTPS_RR_CODE_IPV6 0x06
  121. /*
  122. * These may need escaping when found within an ALPN string
  123. * value.
  124. */
  125. #define COMMA_CHAR ','
  126. #define BACKSLASH_CHAR '\\'
  127. struct dohhttps_rr {
  128. uint16_t len; /* raw encoded length */
  129. unsigned char *val; /* raw encoded octets */
  130. };
  131. #endif
  132. struct dohentry {
  133. struct dynbuf cname[DOH_MAX_CNAME];
  134. struct dohaddr addr[DOH_MAX_ADDR];
  135. int numaddr;
  136. unsigned int ttl;
  137. int numcname;
  138. #ifdef USE_HTTPSRR
  139. struct dohhttps_rr https_rrs[DOH_MAX_HTTPS];
  140. int numhttps_rrs;
  141. #endif
  142. };
  143. void Curl_doh_close(struct Curl_easy *data);
  144. void Curl_doh_cleanup(struct Curl_easy *data);
  145. #ifdef UNITTESTS
  146. UNITTEST DOHcode doh_req_encode(const char *host,
  147. DNStype dnstype,
  148. unsigned char *dnsp, /* buffer */
  149. size_t len, /* buffer size */
  150. size_t *olen); /* output length */
  151. UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
  152. size_t dohlen,
  153. DNStype dnstype,
  154. struct dohentry *d);
  155. UNITTEST void de_init(struct dohentry *d);
  156. UNITTEST void de_cleanup(struct dohentry *d);
  157. #endif
  158. extern struct curl_trc_feat Curl_doh_trc;
  159. #else /* if DoH is disabled */
  160. #define Curl_doh(a,b,c,d) NULL
  161. #define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
  162. #endif
  163. #endif /* HEADER_CURL_DOH_H */