blob: 0e0cbadc832257291602012d7d4afa73e24a2508 [file] [log] [blame]
Bernie Innocenti318ed2d2018-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 Innocentiac18b122018-10-01 23:10:18 +090054#ifndef NETD_RESOLV_PRIVATE_H
55#define NETD_RESOLV_PRIVATE_H
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090056
chenbruceacb832c2019-02-20 19:45:50 +080057#include <android-base/logging.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090058#include <net/if.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090059#include <resolv.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090060#include <time.h>
chenbruceacb832c2019-02-20 19:45:50 +080061#include <string>
Luke Huang2dac4382019-06-24 13:28:44 +080062#include <vector>
Bernie Innocentiac18b122018-10-01 23:10:18 +090063
64#include "netd_resolv/params.h"
nuccachenb980f2f2018-10-23 17:10:58 +080065#include "netd_resolv/resolv.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090066#include "netd_resolv/stats.h"
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090067#include "resolv_static.h"
lifr94981782019-05-17 21:15:19 +080068#include "stats.pb.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090069
70// Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
71// RFC 1034 and RFC 1035 is 255 octets.
72#ifdef MAXHOSTNAMELEN
73#undef MAXHOSTNAMELEN
74#endif
75#define MAXHOSTNAMELEN 256
76
77/*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090078 * Global defines and variables for resolver stub.
79 */
waynemaa74195e2019-01-18 14:02:31 +080080#define RES_TIMEOUT 5000 /* min. milliseconds between retries */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090081#define MAXRESOLVSORT 10 /* number of net to sort on */
82#define RES_MAXNDOTS 15 /* should reflect bit field size */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090083#define RES_DFLRETRY 2 /* Default #/tries. */
84#define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090085
Bernie Innocentiac18b122018-10-01 23:10:18 +090086struct res_state_ext;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090087
88struct __res_state {
Luke Huang2dac4382019-06-24 13:28:44 +080089 unsigned netid; // NetId: cache key and socket mark
90 uid_t uid; // uid of the app that sent the DNS lookup
91 u_long options; // option flags - see below.
92 int nscount; // number of name srvers
93 struct sockaddr_in nsaddr_list[MAXNS]; // address of name server
94#define nsaddr nsaddr_list[0] // for backward compatibility
95 u_short id; // current message id
96 std::vector<std::string> search_domains; // domains to search
97 unsigned ndots : 4; // threshold for initial abs. query
98 unsigned nsort : 4; // number of elements in sort_list[]
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090099 char unused[3];
100 struct {
101 struct in_addr addr;
102 uint32_t mask;
103 } sort_list[MAXRESOLVSORT];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900104 unsigned _mark; /* If non-0 SET_MARK to _mark on all request sockets */
105 int _vcsock; /* PRIVATE: for res_send VC i/o */
106 u_int _flags; /* PRIVATE: see below */
107 u_int _pad; /* make _u 64 bit aligned */
108 union {
109 /* On an 32-bit arch this means 512b total. */
110 char pad[72 - 4 * sizeof(int) - 2 * sizeof(void*)];
111 struct {
112 uint16_t nscount;
113 uint16_t nstimes[MAXNS]; /* ms. */
114 int nssocks[MAXNS];
Bernie Innocentiac18b122018-10-01 23:10:18 +0900115 struct res_state_ext* ext; /* extention for IPv6 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900116 } _ext;
117 } _u;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900118 struct res_static rstatic[1];
lifr94981782019-05-17 21:15:19 +0800119 android::net::NetworkDnsEventReported* event;
chenbruced8cbb9b2019-06-20 18:25:28 +0800120 uint32_t netcontext_flags;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900121};
122
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900123typedef struct __res_state* res_state;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900124
Bernie Innocenti91980d22019-06-12 21:54:28 +0900125// Holds either a sockaddr_in or a sockaddr_in6.
126typedef union sockaddr_union {
127 struct sockaddr sa;
128 struct sockaddr_in sin;
129 struct sockaddr_in6 sin6;
130} sockaddr_union;
131
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900132/* Retrieve a local copy of the stats for the given netid. The buffer must have space for
133 * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
134 */
Bernie Innocenti758005f2019-02-19 18:08:36 +0900135int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900136
137/* Add a sample to the shared struct for the given netid and server, provided that the
138 * revision_id of the stored servers has not changed.
139 */
Bernie Innocentia74088b2018-09-13 16:00:42 +0900140void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
Bernie Innocentiac18b122018-10-01 23:10:18 +0900141 const res_sample* sample, int max_samples);
Bernie Innocenti8fca66a2018-09-25 14:23:19 +0900142
143// Calculate the round-trip-time from start time t0 and end time t1.
144int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
145
146// Create a sample for calculating server reachability statistics.
Bernie Innocentiac18b122018-10-01 23:10:18 +0900147void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
Bernie Innocenti8fca66a2018-09-25 14:23:19 +0900148
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900149/* End of stats related definitions */
150
Bernie Innocenti9c2c06f2019-01-08 18:36:28 +0900151// Flags for res_state->_flags
152#define RES_F_VC 0x00000001 // socket is TCP
153#define RES_F_EDNS0ERR 0x00000004 // EDNS0 caused errors
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900154
155/*
Hungming Chen947aab02018-12-27 18:33:19 +0800156 * Error code extending h_errno codes defined in bionic/libc/include/netdb.h.
157 *
158 * This error code, including legacy h_errno, is returned from res_nquery(), res_nsearch(),
159 * res_nquerydomain(), res_queryN(), res_searchN() and res_querydomainN() for DNS metrics.
160 *
161 * TODO: Consider mapping legacy and extended h_errno into a unified resolver error code mapping.
162 */
163#define NETD_RESOLV_H_ERRNO_EXT_TIMEOUT RCODE_TIMEOUT
164
Mike Yubfb1b342018-11-06 15:42:36 +0800165extern const char* const _res_opcodes[];
166
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900167/* Things involving an internal (static) resolver context. */
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900168struct __res_state* res_get_state(void);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900169
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900170int res_hnok(const char*);
171int res_ownok(const char*);
172int res_mailok(const char*);
173int res_dnok(const char*);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900174int dn_skipname(const u_char*, const u_char*);
175void putlong(uint32_t, u_char*);
176void putshort(uint16_t, u_char*);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900177
178// Thread-unsafe functions returning pointers to static buffers :-(
179// TODO: switch all res_debug to std::string
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900180const char* p_class(int);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900181const char* p_type(int);
182const char* p_rcode(int);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900183const char* p_section(int, int);
184
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900185int res_nameinquery(const char*, int, int, const u_char*, const u_char*);
186int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900187/* Things involving a resolver context. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900188int res_ninit(res_state);
chenbrucedd210722019-03-06 13:52:43 +0800189void res_pquery(const u_char*, int);
chenbruceacb832c2019-02-20 19:45:50 +0800190
Hungming Chen947aab02018-12-27 18:33:19 +0800191int res_nquery(res_state, const char*, int, int, u_char*, int, int*);
192int res_nsearch(res_state, const char*, int, int, u_char*, int, int*);
193int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int, int*);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900194int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*,
195 int);
Luke Huangba7bef92018-12-26 16:53:03 +0800196int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900197void res_nclose(res_state);
Bernie Innocentia74088b2018-09-13 16:00:42 +0900198int res_nopt(res_state, int, u_char*, int, int);
Bernie Innocentia74088b2018-09-13 16:00:42 +0900199void res_ndestroy(res_state);
nuccachenb980f2f2018-10-23 17:10:58 +0800200void res_setservers(res_state, const sockaddr_union*, int);
201int res_getservers(res_state, sockaddr_union*, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900202
203struct android_net_context; /* forward */
lifr94981782019-05-17 21:15:19 +0800204void res_setnetcontext(res_state, const struct android_net_context*,
205 android::net::NetworkDnsEventReported* event);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900206
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900207int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
208 addrinfo** result);
209
Mike Yubfb1b342018-11-06 15:42:36 +0800210// Helper function for converting h_errno to the error codes visible to netd
Hungming Chen947aab02018-12-27 18:33:19 +0800211int herrnoToAiErrno(int herrno);
Mike Yubfb1b342018-11-06 15:42:36 +0800212
chenbruceacb832c2019-02-20 19:45:50 +0800213// switch resolver log severity
214android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr);
215
lifr94981782019-05-17 21:15:19 +0800216template <typename Dest>
217Dest saturate_cast(int64_t x) {
218 using DestLimits = std::numeric_limits<Dest>;
219 if (x > DestLimits::max()) return DestLimits::max();
220 if (x < DestLimits::min()) return DestLimits::min();
221 return static_cast<Dest>(x);
222}
223
224android::net::NsType getQueryType(const uint8_t* msg, size_t msgLen);
225
226android::net::IpVersion ipFamilyToIPVersion(int ipFamily);
227
Bernie Innocentiac18b122018-10-01 23:10:18 +0900228#endif // NETD_RESOLV_PRIVATE_H