Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 54 | #ifndef NETD_RESOLV_PRIVATE_H |
| 55 | #define NETD_RESOLV_PRIVATE_H |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 56 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 57 | #include <android-base/logging.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 58 | #include <net/if.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 59 | #include <resolv.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 60 | #include <time.h> |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 61 | #include <string> |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 62 | #include <vector> |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 63 | |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 64 | #include "DnsResolver.h" |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 65 | #include "netd_resolv/params.h" |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 66 | #include "netd_resolv/resolv.h" |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 67 | #include "netd_resolv/stats.h" |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 68 | #include "resolv_static.h" |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 69 | #include "stats.pb.h" |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 70 | |
| 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 Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 79 | * Global defines and variables for resolver stub. |
| 80 | */ |
waynema | a74195e | 2019-01-18 14:02:31 +0800 | [diff] [blame] | 81 | #define RES_TIMEOUT 5000 /* min. milliseconds between retries */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 82 | #define MAXRESOLVSORT 10 /* number of net to sort on */ |
| 83 | #define RES_MAXNDOTS 15 /* should reflect bit field size */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 84 | #define RES_DFLRETRY 2 /* Default #/tries. */ |
| 85 | #define RES_MAXTIME 65535 /* Infinity, in milliseconds. */ |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 86 | |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 87 | struct res_state_ext; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 88 | |
| 89 | struct __res_state { |
Luke Huang | 2dac438 | 2019-06-24 13:28:44 +0800 | [diff] [blame] | 90 | 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 Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 100 | char unused[3]; |
| 101 | struct { |
| 102 | struct in_addr addr; |
| 103 | uint32_t mask; |
| 104 | } sort_list[MAXRESOLVSORT]; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 105 | 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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 116 | struct res_state_ext* ext; /* extention for IPv6 */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 117 | } _ext; |
| 118 | } _u; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | struct res_static rstatic[1]; |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 120 | android::net::NetworkDnsEventReported* event; |
chenbruce | d8cbb9b | 2019-06-20 18:25:28 +0800 | [diff] [blame] | 121 | uint32_t netcontext_flags; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 122 | }; |
| 123 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 124 | typedef struct __res_state* res_state; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 125 | |
Bernie Innocenti | 91980d2 | 2019-06-12 21:54:28 +0900 | [diff] [blame] | 126 | // Holds either a sockaddr_in or a sockaddr_in6. |
| 127 | typedef union sockaddr_union { |
| 128 | struct sockaddr sa; |
| 129 | struct sockaddr_in sin; |
| 130 | struct sockaddr_in6 sin6; |
| 131 | } sockaddr_union; |
| 132 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 133 | /* 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 Innocenti | 758005f | 2019-02-19 18:08:36 +0900 | [diff] [blame] | 136 | int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 137 | |
| 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 Innocenti | a74088b | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 141 | void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns, |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 142 | const res_sample* sample, int max_samples); |
Bernie Innocenti | 8fca66a | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 143 | |
| 144 | // Calculate the round-trip-time from start time t0 and end time t1. |
| 145 | int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0); |
| 146 | |
| 147 | // Create a sample for calculating server reachability statistics. |
Bernie Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 148 | void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt); |
Bernie Innocenti | 8fca66a | 2018-09-25 14:23:19 +0900 | [diff] [blame] | 149 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 150 | /* End of stats related definitions */ |
| 151 | |
Bernie Innocenti | 9c2c06f | 2019-01-08 18:36:28 +0900 | [diff] [blame] | 152 | // Flags for res_state->_flags |
| 153 | #define RES_F_VC 0x00000001 // socket is TCP |
| 154 | #define RES_F_EDNS0ERR 0x00000004 // EDNS0 caused errors |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 155 | |
| 156 | /* |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 157 | * 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 Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 166 | extern const char* const _res_opcodes[]; |
| 167 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 168 | /* Things involving an internal (static) resolver context. */ |
Bernie Innocenti | 2e8540b | 2018-09-26 11:52:04 +0900 | [diff] [blame] | 169 | struct __res_state* res_get_state(void); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 170 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 171 | int res_hnok(const char*); |
| 172 | int res_ownok(const char*); |
| 173 | int res_mailok(const char*); |
| 174 | int res_dnok(const char*); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 175 | int dn_skipname(const u_char*, const u_char*); |
| 176 | void putlong(uint32_t, u_char*); |
| 177 | void putshort(uint16_t, u_char*); |
Bernie Innocenti | 3952ccc | 2019-03-03 19:39:53 +0900 | [diff] [blame] | 178 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 179 | int res_nameinquery(const char*, int, int, const u_char*, const u_char*); |
| 180 | int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 181 | /* Things involving a resolver context. */ |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 182 | int res_ninit(res_state); |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 183 | |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 184 | int res_nquery(res_state, const char*, int, int, u_char*, int, int*); |
| 185 | int res_nsearch(res_state, const char*, int, int, u_char*, int, int*); |
| 186 | int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int, int*); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 187 | int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*, |
| 188 | int); |
Luke Huang | ba7bef9 | 2018-12-26 16:53:03 +0800 | [diff] [blame] | 189 | int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 190 | void res_nclose(res_state); |
Bernie Innocenti | a74088b | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 191 | int res_nopt(res_state, int, u_char*, int, int); |
Bernie Innocenti | a74088b | 2018-09-13 16:00:42 +0900 | [diff] [blame] | 192 | void res_ndestroy(res_state); |
nuccachen | b980f2f | 2018-10-23 17:10:58 +0800 | [diff] [blame] | 193 | void res_setservers(res_state, const sockaddr_union*, int); |
| 194 | int res_getservers(res_state, sockaddr_union*, int); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 195 | |
| 196 | struct android_net_context; /* forward */ |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 197 | void res_setnetcontext(res_state, const struct android_net_context*, |
| 198 | android::net::NetworkDnsEventReported* event); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 199 | |
Bernie Innocenti | e2bc46f | 2018-10-16 23:35:28 +0900 | [diff] [blame] | 200 | int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints, |
| 201 | addrinfo** result); |
| 202 | |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 203 | // Helper function for converting h_errno to the error codes visible to netd |
Hungming Chen | 947aab0 | 2018-12-27 18:33:19 +0800 | [diff] [blame] | 204 | int herrnoToAiErrno(int herrno); |
Mike Yu | bfb1b34 | 2018-11-06 15:42:36 +0800 | [diff] [blame] | 205 | |
chenbruce | acb832c | 2019-02-20 19:45:50 +0800 | [diff] [blame] | 206 | // switch resolver log severity |
| 207 | android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr); |
| 208 | |
lifr | 9498178 | 2019-05-17 21:15:19 +0800 | [diff] [blame] | 209 | template <typename Dest> |
| 210 | Dest 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 | |
| 217 | android::net::NsType getQueryType(const uint8_t* msg, size_t msgLen); |
| 218 | |
| 219 | android::net::IpVersion ipFamilyToIPVersion(int ipFamily); |
| 220 | |
Sehee Park | d975bf3 | 2019-08-07 13:21:16 +0900 | [diff] [blame] | 221 | inline 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 Innocenti | ac18b12 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 233 | #endif // NETD_RESOLV_PRIVATE_H |