doh.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 dnsprobe {
  58. CURL *easy;
  59. DNStype dnstype;
  60. unsigned char dohbuffer[512];
  61. size_t dohlen;
  62. struct dynbuf serverdoh;
  63. };
  64. struct dohdata {
  65. struct curl_slist *headers;
  66. struct dnsprobe probe[DOH_PROBE_SLOTS];
  67. unsigned int pending; /* still outstanding requests */
  68. int port;
  69. const char *host;
  70. };
  71. /*
  72. * Curl_doh() resolve a name using DoH (DNS-over-HTTPS). It resolves a name
  73. * and returns a 'Curl_addrinfo *' with the address information.
  74. */
  75. struct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
  76. const char *hostname,
  77. int port,
  78. int *waitp);
  79. CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
  80. struct Curl_dns_entry **dns);
  81. #define DOH_MAX_ADDR 24
  82. #define DOH_MAX_CNAME 4
  83. #define DOH_MAX_HTTPS 4
  84. struct dohaddr {
  85. int type;
  86. union {
  87. unsigned char v4[4]; /* network byte order */
  88. unsigned char v6[16];
  89. } ip;
  90. };
  91. #ifdef USE_HTTPSRR
  92. /*
  93. * These are the code points for DNS wire format SvcParams as
  94. * per draft-ietf-dnsop-svcb-https
  95. * Not all are supported now, and even those that are may need
  96. * more work in future to fully support the spec.
  97. */
  98. #define HTTPS_RR_CODE_ALPN 0x01
  99. #define HTTPS_RR_CODE_NO_DEF_ALPN 0x02
  100. #define HTTPS_RR_CODE_PORT 0x03
  101. #define HTTPS_RR_CODE_IPV4 0x04
  102. #define HTTPS_RR_CODE_ECH 0x05
  103. #define HTTPS_RR_CODE_IPV6 0x06
  104. /*
  105. * These may need escaping when found within an alpn string
  106. * value.
  107. */
  108. #define COMMA_CHAR ','
  109. #define BACKSLASH_CHAR '\\'
  110. struct dohhttps_rr {
  111. uint16_t len; /* raw encoded length */
  112. unsigned char *val; /* raw encoded octets */
  113. };
  114. #endif
  115. struct dohentry {
  116. struct dynbuf cname[DOH_MAX_CNAME];
  117. struct dohaddr addr[DOH_MAX_ADDR];
  118. int numaddr;
  119. unsigned int ttl;
  120. int numcname;
  121. #ifdef USE_HTTPSRR
  122. struct dohhttps_rr https_rrs[DOH_MAX_HTTPS];
  123. int numhttps_rrs;
  124. #endif
  125. };
  126. void Curl_doh_close(struct Curl_easy *data);
  127. void Curl_doh_cleanup(struct Curl_easy *data);
  128. #ifdef UNITTESTS
  129. UNITTEST DOHcode doh_encode(const char *host,
  130. DNStype dnstype,
  131. unsigned char *dnsp, /* buffer */
  132. size_t len, /* buffer size */
  133. size_t *olen); /* output length */
  134. UNITTEST DOHcode doh_decode(const unsigned char *doh,
  135. size_t dohlen,
  136. DNStype dnstype,
  137. struct dohentry *d);
  138. UNITTEST void de_init(struct dohentry *d);
  139. UNITTEST void de_cleanup(struct dohentry *d);
  140. #endif
  141. extern struct curl_trc_feat Curl_doh_trc;
  142. #else /* if DoH is disabled */
  143. #define Curl_doh(a,b,c,d) NULL
  144. #define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
  145. #endif
  146. #endif /* HEADER_CURL_DOH_H */