doh.h (5371B)
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 27 #include "urldata.h" 28 #include "curl_addrinfo.h" 29 #ifdef USE_HTTPSRR 30 # include <stdint.h> 31 # include "httpsrr.h" 32 #endif 33 34 #ifndef CURL_DISABLE_DOH 35 36 typedef enum { 37 DOH_OK, 38 DOH_DNS_BAD_LABEL, /* 1 */ 39 DOH_DNS_OUT_OF_RANGE, /* 2 */ 40 DOH_DNS_LABEL_LOOP, /* 3 */ 41 DOH_TOO_SMALL_BUFFER, /* 4 */ 42 DOH_OUT_OF_MEM, /* 5 */ 43 DOH_DNS_RDATA_LEN, /* 6 */ 44 DOH_DNS_MALFORMAT, /* 7 */ 45 DOH_DNS_BAD_RCODE, /* 8 - no such name */ 46 DOH_DNS_UNEXPECTED_TYPE, /* 9 */ 47 DOH_DNS_UNEXPECTED_CLASS, /* 10 */ 48 DOH_NO_CONTENT, /* 11 */ 49 DOH_DNS_BAD_ID, /* 12 */ 50 DOH_DNS_NAME_TOO_LONG /* 13 */ 51 } DOHcode; 52 53 typedef enum { 54 DNS_TYPE_A = 1, 55 DNS_TYPE_NS = 2, 56 DNS_TYPE_CNAME = 5, 57 DNS_TYPE_AAAA = 28, 58 DNS_TYPE_DNAME = 39, /* RFC6672 */ 59 DNS_TYPE_HTTPS = 65 60 } DNStype; 61 62 enum doh_slot_num { 63 /* Explicit values for first two symbols so as to match hard-coded 64 * constants in existing code 65 */ 66 DOH_SLOT_IPV4 = 0, /* make 'V4' stand out for readability */ 67 DOH_SLOT_IPV6 = 1, /* 'V6' likewise */ 68 69 /* Space here for (possibly build-specific) additional slot definitions */ 70 #ifdef USE_HTTPSRR 71 DOH_SLOT_HTTPS_RR = 2, /* for HTTPS RR */ 72 #endif 73 74 /* for example */ 75 /* #ifdef WANT_DOH_FOOBAR_TXT */ 76 /* DOH_PROBE_SLOT_FOOBAR_TXT, */ 77 /* #endif */ 78 79 /* AFTER all slot definitions, establish how many we have */ 80 DOH_SLOT_COUNT 81 }; 82 83 #define CURL_EZM_DOH_PROBE "ezm:doh-p" 84 85 /* the largest one we can make, based on RFCs 1034, 1035 */ 86 #define DOH_MAX_DNSREQ_SIZE (256 + 16) 87 88 /* each DoH probe request has this 89 * as easy meta for CURL_EZM_DOH_PROBE */ 90 struct doh_request { 91 unsigned char req_body[DOH_MAX_DNSREQ_SIZE]; 92 struct curl_slist *req_hds; 93 struct dynbuf resp_body; 94 size_t req_body_len; 95 DNStype dnstype; 96 }; 97 98 struct doh_response { 99 unsigned int probe_mid; 100 struct dynbuf body; 101 DNStype dnstype; 102 CURLcode result; 103 }; 104 105 /* each transfer firing off DoH requests has this 106 * as easy meta for CURL_EZM_DOH_MASTER */ 107 struct doh_probes { 108 struct doh_response probe_resp[DOH_SLOT_COUNT]; 109 unsigned int pending; /* still outstanding probes */ 110 int port; 111 const char *host; 112 }; 113 114 /* 115 * Curl_doh() resolve a name using DoH (DNS-over-HTTPS). It resolves a name 116 * and returns a 'Curl_addrinfo *' with the address information. 117 */ 118 119 struct Curl_addrinfo *Curl_doh(struct Curl_easy *data, 120 const char *hostname, 121 int port, 122 int ip_version, 123 int *waitp); 124 125 CURLcode Curl_doh_is_resolved(struct Curl_easy *data, 126 struct Curl_dns_entry **dns); 127 128 #define DOH_MAX_ADDR 24 129 #define DOH_MAX_CNAME 4 130 #define DOH_MAX_HTTPS 4 131 132 struct dohaddr { 133 int type; 134 union { 135 unsigned char v4[4]; /* network byte order */ 136 unsigned char v6[16]; 137 } ip; 138 }; 139 140 #ifdef USE_HTTPSRR 141 142 /* 143 * These may need escaping when found within an ALPN string 144 * value. 145 */ 146 #define COMMA_CHAR ',' 147 #define BACKSLASH_CHAR '\\' 148 149 struct dohhttps_rr { 150 uint16_t len; /* raw encoded length */ 151 unsigned char *val; /* raw encoded octets */ 152 }; 153 #endif 154 155 struct dohentry { 156 struct dynbuf cname[DOH_MAX_CNAME]; 157 struct dohaddr addr[DOH_MAX_ADDR]; 158 int numaddr; 159 unsigned int ttl; 160 int numcname; 161 #ifdef USE_HTTPSRR 162 struct dohhttps_rr https_rrs[DOH_MAX_HTTPS]; 163 int numhttps_rrs; 164 #endif 165 }; 166 167 void Curl_doh_close(struct Curl_easy *data); 168 void Curl_doh_cleanup(struct Curl_easy *data); 169 170 #ifdef UNITTESTS 171 UNITTEST DOHcode doh_req_encode(const char *host, 172 DNStype dnstype, 173 unsigned char *dnsp, /* buffer */ 174 size_t len, /* buffer size */ 175 size_t *olen); /* output length */ 176 UNITTEST DOHcode doh_resp_decode(const unsigned char *doh, 177 size_t dohlen, 178 DNStype dnstype, 179 struct dohentry *d); 180 181 UNITTEST void de_init(struct dohentry *d); 182 UNITTEST void de_cleanup(struct dohentry *d); 183 #endif 184 185 #else /* if DoH is disabled */ 186 #define Curl_doh(a,b,c,d,e) NULL 187 #define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST 188 #endif 189 190 #endif /* HEADER_CURL_DOH_H */