xref: /curl/lib/doh.h (revision d19fc8ea)
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 #endif
32 
33 #ifndef CURL_DISABLE_DOH
34 
35 typedef enum {
36   DOH_OK,
37   DOH_DNS_BAD_LABEL,    /* 1 */
38   DOH_DNS_OUT_OF_RANGE, /* 2 */
39   DOH_DNS_LABEL_LOOP,   /* 3 */
40   DOH_TOO_SMALL_BUFFER, /* 4 */
41   DOH_OUT_OF_MEM,       /* 5 */
42   DOH_DNS_RDATA_LEN,    /* 6 */
43   DOH_DNS_MALFORMAT,    /* 7 */
44   DOH_DNS_BAD_RCODE,    /* 8 - no such name */
45   DOH_DNS_UNEXPECTED_TYPE,  /* 9 */
46   DOH_DNS_UNEXPECTED_CLASS, /* 10 */
47   DOH_NO_CONTENT,           /* 11 */
48   DOH_DNS_BAD_ID,           /* 12 */
49   DOH_DNS_NAME_TOO_LONG     /* 13 */
50 } DOHcode;
51 
52 typedef enum {
53   DNS_TYPE_A = 1,
54   DNS_TYPE_NS = 2,
55   DNS_TYPE_CNAME = 5,
56   DNS_TYPE_AAAA = 28,
57   DNS_TYPE_DNAME = 39,           /* RFC6672 */
58   DNS_TYPE_HTTPS = 65
59 } DNStype;
60 
61 /* one of these for each DoH request */
62 struct dnsprobe {
63   CURL *easy;
64   DNStype dnstype;
65   unsigned char dohbuffer[512];
66   size_t dohlen;
67   struct dynbuf serverdoh;
68 };
69 
70 struct dohdata {
71   struct curl_slist *headers;
72   struct dnsprobe probe[DOH_PROBE_SLOTS];
73   unsigned int pending; /* still outstanding requests */
74   int port;
75   const char *host;
76 };
77 
78 /*
79  * Curl_doh() resolve a name using DoH (DNS-over-HTTPS). It resolves a name
80  * and returns a 'Curl_addrinfo *' with the address information.
81  */
82 
83 struct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
84                                const char *hostname,
85                                int port,
86                                int *waitp);
87 
88 CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
89                               struct Curl_dns_entry **dns);
90 
91 #define DOH_MAX_ADDR 24
92 #define DOH_MAX_CNAME 4
93 #define DOH_MAX_HTTPS 4
94 
95 struct dohaddr {
96   int type;
97   union {
98     unsigned char v4[4]; /* network byte order */
99     unsigned char v6[16];
100   } ip;
101 };
102 
103 #ifdef USE_HTTPSRR
104 
105 /*
106  * These are the code points for DNS wire format SvcParams as
107  * per draft-ietf-dnsop-svcb-https
108  * Not all are supported now, and even those that are may need
109  * more work in future to fully support the spec.
110  */
111 #define HTTPS_RR_CODE_ALPN            0x01
112 #define HTTPS_RR_CODE_NO_DEF_ALPN     0x02
113 #define HTTPS_RR_CODE_PORT            0x03
114 #define HTTPS_RR_CODE_IPV4            0x04
115 #define HTTPS_RR_CODE_ECH             0x05
116 #define HTTPS_RR_CODE_IPV6            0x06
117 
118 /*
119  * These may need escaping when found within an alpn string
120  * value.
121  */
122 #define COMMA_CHAR                    ','
123 #define BACKSLASH_CHAR                '\\'
124 
125 struct dohhttps_rr {
126   uint16_t len; /* raw encoded length */
127   unsigned char *val; /* raw encoded octets */
128 };
129 #endif
130 
131 struct dohentry {
132   struct dynbuf cname[DOH_MAX_CNAME];
133   struct dohaddr addr[DOH_MAX_ADDR];
134   int numaddr;
135   unsigned int ttl;
136   int numcname;
137 #ifdef USE_HTTPSRR
138   struct dohhttps_rr https_rrs[DOH_MAX_HTTPS];
139   int numhttps_rrs;
140 #endif
141 };
142 
143 
144 #ifdef DEBUGBUILD
145 DOHcode doh_encode(const char *host,
146                    DNStype dnstype,
147                    unsigned char *dnsp, /* buffer */
148                    size_t len,  /* buffer size */
149                    size_t *olen); /* output length */
150 DOHcode doh_decode(const unsigned char *doh,
151                    size_t dohlen,
152                    DNStype dnstype,
153                    struct dohentry *d);
154 void de_init(struct dohentry *d);
155 void de_cleanup(struct dohentry *d);
156 #endif
157 
158 extern struct curl_trc_feat Curl_doh_trc;
159 
160 #else /* if DoH is disabled */
161 #define Curl_doh(a,b,c,d) NULL
162 #define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
163 #endif
164 
165 #endif /* HEADER_CURL_DOH_H */
166