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