doh.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. int Curl_doh_getsock(struct connectdata *conn, curl_socket_t *socks);
  82. #define DOH_MAX_ADDR 24
  83. #define DOH_MAX_CNAME 4
  84. #define DOH_MAX_HTTPS 4
  85. struct dohaddr {
  86. int type;
  87. union {
  88. unsigned char v4[4]; /* network byte order */
  89. unsigned char v6[16];
  90. } ip;
  91. };
  92. #ifdef USE_HTTPSRR
  93. /*
  94. * These are the code points for DNS wire format SvcParams as
  95. * per draft-ietf-dnsop-svcb-https
  96. * Not all are supported now, and even those that are may need
  97. * more work in future to fully support the spec.
  98. */
  99. #define HTTPS_RR_CODE_ALPN 0x01
  100. #define HTTPS_RR_CODE_NO_DEF_ALPN 0x02
  101. #define HTTPS_RR_CODE_PORT 0x03
  102. #define HTTPS_RR_CODE_IPV4 0x04
  103. #define HTTPS_RR_CODE_ECH 0x05
  104. #define HTTPS_RR_CODE_IPV6 0x06
  105. /*
  106. * These may need escaping when found within an alpn string
  107. * value.
  108. */
  109. #define COMMA_CHAR ','
  110. #define BACKSLASH_CHAR '\\'
  111. struct dohhttps_rr {
  112. uint16_t len; /* raw encoded length */
  113. unsigned char *val; /* raw encoded octets */
  114. };
  115. #endif
  116. struct dohentry {
  117. struct dynbuf cname[DOH_MAX_CNAME];
  118. struct dohaddr addr[DOH_MAX_ADDR];
  119. int numaddr;
  120. unsigned int ttl;
  121. int numcname;
  122. #ifdef USE_HTTPSRR
  123. struct dohhttps_rr https_rrs[DOH_MAX_HTTPS];
  124. int numhttps_rrs;
  125. #endif
  126. };
  127. #ifdef DEBUGBUILD
  128. DOHcode doh_encode(const char *host,
  129. DNStype dnstype,
  130. unsigned char *dnsp, /* buffer */
  131. size_t len, /* buffer size */
  132. size_t *olen); /* output length */
  133. DOHcode doh_decode(const unsigned char *doh,
  134. size_t dohlen,
  135. DNStype dnstype,
  136. struct dohentry *d);
  137. void de_init(struct dohentry *d);
  138. void de_cleanup(struct dohentry *d);
  139. #endif
  140. extern struct curl_trc_feat Curl_doh_trc;
  141. #else /* if DoH is disabled */
  142. #define Curl_doh(a,b,c,d) NULL
  143. #define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
  144. #endif
  145. #endif /* HEADER_CURL_DOH_H */