blob: 3ecb6c108b48e785ac5448ff546de0476edd8dc8 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: resolv.h,v 1.31 2005/12/26 19:01:47 perry Exp $ */
2
3/*
4 * Copyright (c) 1983, 1987, 1989
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/*
33 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
34 * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
35 *
36 * Permission to use, copy, modify, and distribute this software for any
37 * purpose with or without fee is hereby granted, provided that the above
38 * copyright notice and this permission notice appear in all copies.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
41 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
43 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
46 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 */
48
49/*
50 * @(#)resolv.h 8.1 (Berkeley) 6/2/93
51 * Id: resolv.h,v 1.7.2.11.4.2 2004/06/25 00:41:05 marka Exp
52 */
53
Bernie Innocenti189eb502018-10-01 23:10:18 +090054#ifndef NETD_RESOLV_PRIVATE_H
55#define NETD_RESOLV_PRIVATE_H
Bernie Innocenti55864192018-08-30 04:05:20 +090056
chenbruce16adee42019-02-20 19:45:50 +080057#include <android-base/logging.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090058#include <net/if.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090059#include <resolv.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090060#include <time.h>
chenbruce16adee42019-02-20 19:45:50 +080061#include <string>
Bernie Innocenti189eb502018-10-01 23:10:18 +090062
63#include "netd_resolv/params.h"
nuccachene172a4e2018-10-23 17:10:58 +080064#include "netd_resolv/resolv.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090065#include "netd_resolv/stats.h"
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090066#include "resolv_static.h"
Frank Li08ce7b82019-06-27 10:11:52 -070067#include "stats.pb.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090068
69// Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
70// RFC 1034 and RFC 1035 is 255 octets.
71#ifdef MAXHOSTNAMELEN
72#undef MAXHOSTNAMELEN
73#endif
74#define MAXHOSTNAMELEN 256
75
76/*
Bernie Innocenti55864192018-08-30 04:05:20 +090077 * Global defines and variables for resolver stub.
78 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090079#define MAXDFLSRCH 3 /* # default domain levels to try */
80#define LOCALDOMAINPARTS 2 /* min levels in name that is "local" */
Bernie Innocenti55864192018-08-30 04:05:20 +090081
waynema2371eab2019-01-18 14:02:31 +080082#define RES_TIMEOUT 5000 /* min. milliseconds between retries */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090083#define MAXRESOLVSORT 10 /* number of net to sort on */
84#define RES_MAXNDOTS 15 /* should reflect bit field size */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090085#define RES_DFLRETRY 2 /* Default #/tries. */
86#define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
Bernie Innocenti55864192018-08-30 04:05:20 +090087
Bernie Innocenti189eb502018-10-01 23:10:18 +090088struct res_state_ext;
Bernie Innocenti55864192018-08-30 04:05:20 +090089
90struct __res_state {
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090091 unsigned netid; /* NetId: cache key and socket mark */
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090092 u_long options; /* option flags - see below. */
93 int nscount; /* number of name srvers */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090094 struct sockaddr_in nsaddr_list[MAXNS]; /* address of name server */
95#define nsaddr nsaddr_list[0] /* for backward compatibility */
96 u_short id; /* current message id */
97 char* dnsrch[MAXDNSRCH + 1]; /* components of domain to search */
98 char defdname[256]; /* default domain (deprecated) */
Bernie Innocenti2fd418e2018-08-30 12:04:03 +090099 unsigned ndots : 4; /* threshold for initial abs. query */
100 unsigned nsort : 4; /* number of elements in sort_list[] */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900101 char unused[3];
102 struct {
103 struct in_addr addr;
104 uint32_t mask;
105 } sort_list[MAXRESOLVSORT];
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900106 unsigned _mark; /* If non-0 SET_MARK to _mark on all request sockets */
107 int _vcsock; /* PRIVATE: for res_send VC i/o */
108 u_int _flags; /* PRIVATE: see below */
109 u_int _pad; /* make _u 64 bit aligned */
110 union {
111 /* On an 32-bit arch this means 512b total. */
112 char pad[72 - 4 * sizeof(int) - 2 * sizeof(void*)];
113 struct {
114 uint16_t nscount;
115 uint16_t nstimes[MAXNS]; /* ms. */
116 int nssocks[MAXNS];
Bernie Innocenti189eb502018-10-01 23:10:18 +0900117 struct res_state_ext* ext; /* extention for IPv6 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900118 } _ext;
119 } _u;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 struct res_static rstatic[1];
Mike Yua46fae72018-11-01 20:07:00 +0800121 bool use_local_nameserver; /* DNS-over-TLS bypass */
Frank Li08ce7b82019-06-27 10:11:52 -0700122 android::net::NetworkDnsEventReported* event;
Bernie Innocenti55864192018-08-30 04:05:20 +0900123};
124
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900125typedef struct __res_state* res_state;
Bernie Innocenti55864192018-08-30 04:05:20 +0900126
127/* Retrieve a local copy of the stats for the given netid. The buffer must have space for
128 * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
129 */
Bernie Innocenti34de3ba2019-02-19 18:08:36 +0900130int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]);
Bernie Innocenti55864192018-08-30 04:05:20 +0900131
132/* Add a sample to the shared struct for the given netid and server, provided that the
133 * revision_id of the stored servers has not changed.
134 */
Bernie Innocentia8cfe092018-09-13 16:00:42 +0900135void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
Bernie Innocenti189eb502018-10-01 23:10:18 +0900136 const res_sample* sample, int max_samples);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900137
138// Calculate the round-trip-time from start time t0 and end time t1.
139int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
140
141// Create a sample for calculating server reachability statistics.
Bernie Innocenti189eb502018-10-01 23:10:18 +0900142void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
Bernie Innocentiee1b85b2018-09-25 14:23:19 +0900143
Bernie Innocenti55864192018-08-30 04:05:20 +0900144/* End of stats related definitions */
145
Bernie Innocenti49009f42019-01-08 18:36:28 +0900146// Flags for res_state->_flags
147#define RES_F_VC 0x00000001 // socket is TCP
148#define RES_F_EDNS0ERR 0x00000004 // EDNS0 caused errors
Bernie Innocenti55864192018-08-30 04:05:20 +0900149
150/*
151 * Resolver options (keep these in synch with res_debug.c, please)
152 */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900153#define RES_INIT 0x00000001 /* address initialized */
154#define RES_DEBUG 0x00000002 /* print debug messages */
155#define RES_AAONLY 0x00000004 /* authoritative answers only (!IMPL)*/
156#define RES_USEVC 0x00000008 /* use virtual circuit */
157#define RES_PRIMARY 0x00000010 /* query primary server only (!IMPL) */
158#define RES_IGNTC 0x00000020 /* ignore trucation errors */
159#define RES_RECURSE 0x00000040 /* recursion desired */
160#define RES_DEFNAMES 0x00000080 /* use default domain name */
161#define RES_STAYOPEN 0x00000100 /* Keep TCP socket open */
162#define RES_DNSRCH 0x00000200 /* search up local domain tree */
163#define RES_INSECURE1 0x00000400 /* type 1 security disabled */
164#define RES_INSECURE2 0x00000800 /* type 2 security disabled */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900165#define RES_USE_INET6 0x00002000 /* use/map IPv6 in gethostbyname() */
166#define RES_ROTATE 0x00004000 /* rotate ns list after each query */
167#define RES_NOCHECKNAME 0x00008000 /* do not check names for sanity. */
168#define RES_KEEPTSIG 0x00010000 /* do not strip TSIG records */
169#define RES_BLAST 0x00020000 /* blast all recursive servers */
170#define RES_NOTLDQUERY 0x00100000 /* don't unqualified name as a tld */
171#define RES_USE_DNSSEC 0x00200000 /* use DNSSEC using OK bit in OPT */
172/* #define RES_DEBUG2 0x00400000 */ /* nslookup internal */
Bernie Innocenti55864192018-08-30 04:05:20 +0900173/* KAME extensions: use higher bit to avoid conflict with ISC use */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900174#define RES_USE_DNAME 0x10000000 /* use DNAME */
175#define RES_USE_EDNS0 0x40000000 /* use EDNS0 if configured */
176#define RES_NO_NIBBLE2 0x80000000 /* disable alternate nibble lookup */
Bernie Innocenti55864192018-08-30 04:05:20 +0900177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178#define RES_DEFAULT (RES_RECURSE | RES_DEFNAMES | RES_DNSRCH | RES_NO_NIBBLE2)
Bernie Innocenti55864192018-08-30 04:05:20 +0900179
Bernie Innocenti55864192018-08-30 04:05:20 +0900180
Hungming Chen7f0d3292018-12-27 18:33:19 +0800181/*
182 * Error code extending h_errno codes defined in bionic/libc/include/netdb.h.
183 *
184 * This error code, including legacy h_errno, is returned from res_nquery(), res_nsearch(),
185 * res_nquerydomain(), res_queryN(), res_searchN() and res_querydomainN() for DNS metrics.
186 *
187 * TODO: Consider mapping legacy and extended h_errno into a unified resolver error code mapping.
188 */
189#define NETD_RESOLV_H_ERRNO_EXT_TIMEOUT RCODE_TIMEOUT
190
Mike Yu69615f62018-11-06 15:42:36 +0800191extern const char* const _res_opcodes[];
192
Bernie Innocenti55864192018-08-30 04:05:20 +0900193/* Things involving an internal (static) resolver context. */
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900194struct __res_state* res_get_state(void);
Bernie Innocenti55864192018-08-30 04:05:20 +0900195
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900196int res_hnok(const char*);
197int res_ownok(const char*);
198int res_mailok(const char*);
199int res_dnok(const char*);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900200int dn_skipname(const u_char*, const u_char*);
201void putlong(uint32_t, u_char*);
202void putshort(uint16_t, u_char*);
Bernie Innocentid017e972019-03-03 19:39:53 +0900203
204// Thread-unsafe functions returning pointers to static buffers :-(
205// TODO: switch all res_debug to std::string
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900206const char* p_class(int);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900207const char* p_type(int);
208const char* p_rcode(int);
Bernie Innocentid017e972019-03-03 19:39:53 +0900209const char* p_section(int, int);
210
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900211int res_nameinquery(const char*, int, int, const u_char*, const u_char*);
212int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*);
Bernie Innocenti55864192018-08-30 04:05:20 +0900213/* Things involving a resolver context. */
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900214int res_ninit(res_state);
chenbrucee8911f92019-03-06 13:52:43 +0800215void res_pquery(const u_char*, int);
chenbruce16adee42019-02-20 19:45:50 +0800216
Hungming Chen7f0d3292018-12-27 18:33:19 +0800217int res_nquery(res_state, const char*, int, int, u_char*, int, int*);
218int res_nsearch(res_state, const char*, int, int, u_char*, int, int*);
219int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int, int*);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900220int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*,
221 int);
Luke Huang952d0942018-12-26 16:53:03 +0800222int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223void res_nclose(res_state);
Bernie Innocentia8cfe092018-09-13 16:00:42 +0900224int res_nopt(res_state, int, u_char*, int, int);
Bernie Innocenti4acba1a2018-09-26 11:52:04 +0900225int res_vinit(res_state, int);
Bernie Innocentia8cfe092018-09-13 16:00:42 +0900226void res_ndestroy(res_state);
nuccachene172a4e2018-10-23 17:10:58 +0800227void res_setservers(res_state, const sockaddr_union*, int);
228int res_getservers(res_state, sockaddr_union*, int);
Bernie Innocenti55864192018-08-30 04:05:20 +0900229
230struct android_net_context; /* forward */
Frank Li08ce7b82019-06-27 10:11:52 -0700231void res_setnetcontext(res_state, const struct android_net_context*,
232 android::net::NetworkDnsEventReported* event);
Bernie Innocenti55864192018-08-30 04:05:20 +0900233
Bernie Innocentic165ce82018-10-16 23:35:28 +0900234int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
235 addrinfo** result);
236
Mike Yu69615f62018-11-06 15:42:36 +0800237// Helper function for converting h_errno to the error codes visible to netd
Hungming Chen7f0d3292018-12-27 18:33:19 +0800238int herrnoToAiErrno(int herrno);
Mike Yu69615f62018-11-06 15:42:36 +0800239
chenbruce16adee42019-02-20 19:45:50 +0800240// switch resolver log severity
241android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr);
242
Frank Li08ce7b82019-06-27 10:11:52 -0700243template <typename Dest>
244Dest saturate_cast(int64_t x) {
245 using DestLimits = std::numeric_limits<Dest>;
246 if (x > DestLimits::max()) return DestLimits::max();
247 if (x < DestLimits::min()) return DestLimits::min();
248 return static_cast<Dest>(x);
249}
250
251android::net::NsType getQueryType(const uint8_t* msg, size_t msgLen);
252
253android::net::IpVersion ipFamilyToIPVersion(int ipFamily);
254
Bernie Innocenti189eb502018-10-01 23:10:18 +0900255#endif // NETD_RESOLV_PRIVATE_H