xref: /third_party/curl/lib/doh.h (revision 13498266)
113498266Sopenharmony_ci#ifndef HEADER_CURL_DOH_H
213498266Sopenharmony_ci#define HEADER_CURL_DOH_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "urldata.h"
2813498266Sopenharmony_ci#include "curl_addrinfo.h"
2913498266Sopenharmony_ci
3013498266Sopenharmony_ci#ifndef CURL_DISABLE_DOH
3113498266Sopenharmony_ci
3213498266Sopenharmony_citypedef enum {
3313498266Sopenharmony_ci  DOH_OK,
3413498266Sopenharmony_ci  DOH_DNS_BAD_LABEL,    /* 1 */
3513498266Sopenharmony_ci  DOH_DNS_OUT_OF_RANGE, /* 2 */
3613498266Sopenharmony_ci  DOH_DNS_LABEL_LOOP,   /* 3 */
3713498266Sopenharmony_ci  DOH_TOO_SMALL_BUFFER, /* 4 */
3813498266Sopenharmony_ci  DOH_OUT_OF_MEM,       /* 5 */
3913498266Sopenharmony_ci  DOH_DNS_RDATA_LEN,    /* 6 */
4013498266Sopenharmony_ci  DOH_DNS_MALFORMAT,    /* 7 */
4113498266Sopenharmony_ci  DOH_DNS_BAD_RCODE,    /* 8 - no such name */
4213498266Sopenharmony_ci  DOH_DNS_UNEXPECTED_TYPE,  /* 9 */
4313498266Sopenharmony_ci  DOH_DNS_UNEXPECTED_CLASS, /* 10 */
4413498266Sopenharmony_ci  DOH_NO_CONTENT,           /* 11 */
4513498266Sopenharmony_ci  DOH_DNS_BAD_ID,           /* 12 */
4613498266Sopenharmony_ci  DOH_DNS_NAME_TOO_LONG     /* 13 */
4713498266Sopenharmony_ci} DOHcode;
4813498266Sopenharmony_ci
4913498266Sopenharmony_citypedef enum {
5013498266Sopenharmony_ci  DNS_TYPE_A = 1,
5113498266Sopenharmony_ci  DNS_TYPE_NS = 2,
5213498266Sopenharmony_ci  DNS_TYPE_CNAME = 5,
5313498266Sopenharmony_ci  DNS_TYPE_AAAA = 28,
5413498266Sopenharmony_ci  DNS_TYPE_DNAME = 39           /* RFC6672 */
5513498266Sopenharmony_ci} DNStype;
5613498266Sopenharmony_ci
5713498266Sopenharmony_ci/* one of these for each DoH request */
5813498266Sopenharmony_cistruct dnsprobe {
5913498266Sopenharmony_ci  CURL *easy;
6013498266Sopenharmony_ci  DNStype dnstype;
6113498266Sopenharmony_ci  unsigned char dohbuffer[512];
6213498266Sopenharmony_ci  size_t dohlen;
6313498266Sopenharmony_ci  struct dynbuf serverdoh;
6413498266Sopenharmony_ci};
6513498266Sopenharmony_ci
6613498266Sopenharmony_cistruct dohdata {
6713498266Sopenharmony_ci  struct curl_slist *headers;
6813498266Sopenharmony_ci  struct dnsprobe probe[DOH_PROBE_SLOTS];
6913498266Sopenharmony_ci  unsigned int pending; /* still outstanding requests */
7013498266Sopenharmony_ci  int port;
7113498266Sopenharmony_ci  const char *host;
7213498266Sopenharmony_ci};
7313498266Sopenharmony_ci
7413498266Sopenharmony_ci/*
7513498266Sopenharmony_ci * Curl_doh() resolve a name using DoH (DNS-over-HTTPS). It resolves a name
7613498266Sopenharmony_ci * and returns a 'Curl_addrinfo *' with the address information.
7713498266Sopenharmony_ci */
7813498266Sopenharmony_ci
7913498266Sopenharmony_cistruct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
8013498266Sopenharmony_ci                               const char *hostname,
8113498266Sopenharmony_ci                               int port,
8213498266Sopenharmony_ci                               int *waitp);
8313498266Sopenharmony_ci
8413498266Sopenharmony_ciCURLcode Curl_doh_is_resolved(struct Curl_easy *data,
8513498266Sopenharmony_ci                              struct Curl_dns_entry **dns);
8613498266Sopenharmony_ci
8713498266Sopenharmony_ciint Curl_doh_getsock(struct connectdata *conn, curl_socket_t *socks);
8813498266Sopenharmony_ci
8913498266Sopenharmony_ci#define DOH_MAX_ADDR 24
9013498266Sopenharmony_ci#define DOH_MAX_CNAME 4
9113498266Sopenharmony_ci
9213498266Sopenharmony_cistruct dohaddr {
9313498266Sopenharmony_ci  int type;
9413498266Sopenharmony_ci  union {
9513498266Sopenharmony_ci    unsigned char v4[4]; /* network byte order */
9613498266Sopenharmony_ci    unsigned char v6[16];
9713498266Sopenharmony_ci  } ip;
9813498266Sopenharmony_ci};
9913498266Sopenharmony_ci
10013498266Sopenharmony_cistruct dohentry {
10113498266Sopenharmony_ci  struct dynbuf cname[DOH_MAX_CNAME];
10213498266Sopenharmony_ci  struct dohaddr addr[DOH_MAX_ADDR];
10313498266Sopenharmony_ci  int numaddr;
10413498266Sopenharmony_ci  unsigned int ttl;
10513498266Sopenharmony_ci  int numcname;
10613498266Sopenharmony_ci};
10713498266Sopenharmony_ci
10813498266Sopenharmony_ci
10913498266Sopenharmony_ci#ifdef DEBUGBUILD
11013498266Sopenharmony_ciDOHcode doh_encode(const char *host,
11113498266Sopenharmony_ci                   DNStype dnstype,
11213498266Sopenharmony_ci                   unsigned char *dnsp, /* buffer */
11313498266Sopenharmony_ci                   size_t len,  /* buffer size */
11413498266Sopenharmony_ci                   size_t *olen); /* output length */
11513498266Sopenharmony_ciDOHcode doh_decode(const unsigned char *doh,
11613498266Sopenharmony_ci                   size_t dohlen,
11713498266Sopenharmony_ci                   DNStype dnstype,
11813498266Sopenharmony_ci                   struct dohentry *d);
11913498266Sopenharmony_civoid de_init(struct dohentry *d);
12013498266Sopenharmony_civoid de_cleanup(struct dohentry *d);
12113498266Sopenharmony_ci#endif
12213498266Sopenharmony_ci
12313498266Sopenharmony_ci#else /* if DoH is disabled */
12413498266Sopenharmony_ci#define Curl_doh(a,b,c,d) NULL
12513498266Sopenharmony_ci#define Curl_doh_is_resolved(x,y) CURLE_COULDNT_RESOLVE_HOST
12613498266Sopenharmony_ci#endif
12713498266Sopenharmony_ci
12813498266Sopenharmony_ci#endif /* HEADER_CURL_DOH_H */
129