blob: f2668b50749ced12e3009d65b77ab49da74e2699 [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
Sehee Parkd975bf32019-08-07 13:21:16 +090064#include "DnsResolver.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090065#include "netd_resolv/params.h"
nuccachenb980f2f2018-10-23 17:10:58 +080066#include "netd_resolv/resolv.h"
Bernie Innocentiac18b122018-10-01 23:10:18 +090067#include "netd_resolv/stats.h"
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090068#include "resolv_static.h"
lifr94981782019-05-17 21:15:19 +080069#include "stats.pb.h"
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090070
71// Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
72// RFC 1034 and RFC 1035 is 255 octets.
73#ifdef MAXHOSTNAMELEN
74#undef MAXHOSTNAMELEN
75#endif
76#define MAXHOSTNAMELEN 256
77
78/*
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090079 * Global defines and variables for resolver stub.
80 */
waynemaa74195e2019-01-18 14:02:31 +080081#define RES_TIMEOUT 5000 /* min. milliseconds between retries */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090082#define MAXRESOLVSORT 10 /* number of net to sort on */
83#define RES_MAXNDOTS 15 /* should reflect bit field size */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090084#define RES_DFLRETRY 2 /* Default #/tries. */
85#define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090086
Bernie Innocentiac18b122018-10-01 23:10:18 +090087struct res_state_ext;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090088
89struct __res_state {
Luke Huang2dac4382019-06-24 13:28:44 +080090 unsigned netid; // NetId: cache key and socket mark
91 uid_t uid; // uid of the app that sent the DNS lookup
92 u_long options; // option flags - see below.
93 int nscount; // number of name srvers
94 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 std::vector<std::string> search_domains; // domains to search
98 unsigned ndots : 4; // threshold for initial abs. query
99 unsigned nsort : 4; // number of elements in sort_list[]
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900100 char unused[3];
101 struct {
102 struct in_addr addr;
103 uint32_t mask;
104 } sort_list[MAXRESOLVSORT];
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900105 unsigned _mark; /* If non-0 SET_MARK to _mark on all request sockets */
106 int _vcsock; /* PRIVATE: for res_send VC i/o */
107 u_int _flags; /* PRIVATE: see below */
108 u_int _pad; /* make _u 64 bit aligned */
109 union {
110 /* On an 32-bit arch this means 512b total. */
111 char pad[72 - 4 * sizeof(int) - 2 * sizeof(void*)];
112 struct {
113 uint16_t nscount;
114 uint16_t nstimes[MAXNS]; /* ms. */
115 int nssocks[MAXNS];
Bernie Innocentiac18b122018-10-01 23:10:18 +0900116 struct res_state_ext* ext; /* extention for IPv6 */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900117 } _ext;
118 } _u;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900119 struct res_static rstatic[1];
lifr94981782019-05-17 21:15:19 +0800120 android::net::NetworkDnsEventReported* event;
chenbruced8cbb9b2019-06-20 18:25:28 +0800121 uint32_t netcontext_flags;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900122};
123
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900124typedef struct __res_state* res_state;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900125
Bernie Innocenti91980d22019-06-12 21:54:28 +0900126// Holds either a sockaddr_in or a sockaddr_in6.
127typedef union sockaddr_union {
128 struct sockaddr sa;
129 struct sockaddr_in sin;
130 struct sockaddr_in6 sin6;
131} sockaddr_union;
132
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900133/* Retrieve a local copy of the stats for the given netid. The buffer must have space for
134 * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
135 */
Bernie Innocenti758005f2019-02-19 18:08:36 +0900136int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900137
138/* Add a sample to the shared struct for the given netid and server, provided that the
139 * revision_id of the stored servers has not changed.
140 */
Bernie Innocentia74088b2018-09-13 16:00:42 +0900141void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
Bernie Innocentiac18b122018-10-01 23:10:18 +0900142 const res_sample* sample, int max_samples);
Bernie Innocenti8fca66a2018-09-25 14:23:19 +0900143
144// Calculate the round-trip-time from start time t0 and end time t1.
145int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
146
147// Create a sample for calculating server reachability statistics.
Bernie Innocentiac18b122018-10-01 23:10:18 +0900148void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
Bernie Innocenti8fca66a2018-09-25 14:23:19 +0900149
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900150/* End of stats related definitions */
151
Bernie Innocenti9c2c06f2019-01-08 18:36:28 +0900152// Flags for res_state->_flags
153#define RES_F_VC 0x00000001 // socket is TCP
154#define RES_F_EDNS0ERR 0x00000004 // EDNS0 caused errors
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900155
156/*
Hungming Chen947aab02018-12-27 18:33:19 +0800157 * Error code extending h_errno codes defined in bionic/libc/include/netdb.h.
158 *
159 * This error code, including legacy h_errno, is returned from res_nquery(), res_nsearch(),
160 * res_nquerydomain(), res_queryN(), res_searchN() and res_querydomainN() for DNS metrics.
161 *
162 * TODO: Consider mapping legacy and extended h_errno into a unified resolver error code mapping.
163 */
164#define NETD_RESOLV_H_ERRNO_EXT_TIMEOUT RCODE_TIMEOUT
165
Mike Yubfb1b342018-11-06 15:42:36 +0800166extern const char* const _res_opcodes[];
167
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900168/* Things involving an internal (static) resolver context. */
Bernie Innocenti2e8540b2018-09-26 11:52:04 +0900169struct __res_state* res_get_state(void);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900170
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900171int res_hnok(const char*);
172int res_ownok(const char*);
173int res_mailok(const char*);
174int res_dnok(const char*);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900175int dn_skipname(const u_char*, const u_char*);
176void putlong(uint32_t, u_char*);
177void putshort(uint16_t, u_char*);
Bernie Innocenti3952ccc2019-03-03 19:39:53 +0900178
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900179int res_nameinquery(const char*, int, int, const u_char*, const u_char*);
180int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900181/* Things involving a resolver context. */
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900182int res_ninit(res_state);
chenbruceacb832c2019-02-20 19:45:50 +0800183
Hungming Chen947aab02018-12-27 18:33:19 +0800184int res_nquery(res_state, const char*, int, int, u_char*, int, int*);
185int res_nsearch(res_state, const char*, int, int, u_char*, int, int*);
186int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int, int*);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900187int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*,
188 int);
Luke Huangba7bef92018-12-26 16:53:03 +0800189int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900190void res_nclose(res_state);
Bernie Innocentia74088b2018-09-13 16:00:42 +0900191int res_nopt(res_state, int, u_char*, int, int);
Bernie Innocentia74088b2018-09-13 16:00:42 +0900192void res_ndestroy(res_state);
nuccachenb980f2f2018-10-23 17:10:58 +0800193void res_setservers(res_state, const sockaddr_union*, int);
194int res_getservers(res_state, sockaddr_union*, int);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900195
196struct android_net_context; /* forward */
lifr94981782019-05-17 21:15:19 +0800197void res_setnetcontext(res_state, const struct android_net_context*,
198 android::net::NetworkDnsEventReported* event);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900199
Bernie Innocentie2bc46f2018-10-16 23:35:28 +0900200int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
201 addrinfo** result);
202
Mike Yubfb1b342018-11-06 15:42:36 +0800203// Helper function for converting h_errno to the error codes visible to netd
Hungming Chen947aab02018-12-27 18:33:19 +0800204int herrnoToAiErrno(int herrno);
Mike Yubfb1b342018-11-06 15:42:36 +0800205
chenbruceacb832c2019-02-20 19:45:50 +0800206// switch resolver log severity
207android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr);
208
lifr94981782019-05-17 21:15:19 +0800209template <typename Dest>
210Dest saturate_cast(int64_t x) {
211 using DestLimits = std::numeric_limits<Dest>;
212 if (x > DestLimits::max()) return DestLimits::max();
213 if (x < DestLimits::min()) return DestLimits::min();
214 return static_cast<Dest>(x);
215}
216
217android::net::NsType getQueryType(const uint8_t* msg, size_t msgLen);
218
219android::net::IpVersion ipFamilyToIPVersion(int ipFamily);
220
Sehee Parkd975bf32019-08-07 13:21:16 +0900221inline void resolv_tag_socket(int sock, uid_t uid) {
222 if (android::net::gResNetdCallbacks.tagSocket != nullptr) {
223 if (int err = android::net::gResNetdCallbacks.tagSocket(sock, TAG_SYSTEM_DNS, uid)) {
224 LOG(WARNING) << "Failed to tag socket: " << strerror(-err);
225 }
226 }
227
228 if (fchown(sock, uid, -1) == -1) {
229 LOG(WARNING) << "Failed to chown socket: " << strerror(errno);
230 }
231}
232
Bernie Innocentiac18b122018-10-01 23:10:18 +0900233#endif // NETD_RESOLV_PRIVATE_H