resolv_private.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* $NetBSD: resolv.h,v 1.31 2005/12/26 19:01:47 perry Exp $ */
  2. /*
  3. * Copyright (c) 1983, 1987, 1989
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /*
  31. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  32. * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
  33. *
  34. * Permission to use, copy, modify, and distribute this software for any
  35. * purpose with or without fee is hereby granted, provided that the above
  36. * copyright notice and this permission notice appear in all copies.
  37. *
  38. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  39. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  40. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  41. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  42. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  43. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  44. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  45. */
  46. /*
  47. * @(#)resolv.h 8.1 (Berkeley) 6/2/93
  48. * Id: resolv.h,v 1.7.2.11.4.2 2004/06/25 00:41:05 marka Exp
  49. */
  50. #ifndef NETD_RESOLV_PRIVATE_H
  51. #define NETD_RESOLV_PRIVATE_H
  52. #include <android-base/logging.h>
  53. #include <net/if.h>
  54. #include <resolv.h>
  55. #include <time.h>
  56. #include <string>
  57. #include "netd_resolv/params.h"
  58. #include "netd_resolv/resolv.h"
  59. #include "netd_resolv/stats.h"
  60. #include "resolv_static.h"
  61. #include "stats.pb.h"
  62. // Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
  63. // RFC 1034 and RFC 1035 is 255 octets.
  64. #ifdef MAXHOSTNAMELEN
  65. #undef MAXHOSTNAMELEN
  66. #endif
  67. #define MAXHOSTNAMELEN 256
  68. /*
  69. * Global defines and variables for resolver stub.
  70. */
  71. #define MAXDFLSRCH 3 /* # default domain levels to try */
  72. #define LOCALDOMAINPARTS 2 /* min levels in name that is "local" */
  73. #define RES_TIMEOUT 5000 /* min. milliseconds between retries */
  74. #define MAXRESOLVSORT 10 /* number of net to sort on */
  75. #define RES_MAXNDOTS 15 /* should reflect bit field size */
  76. #define RES_DFLRETRY 2 /* Default #/tries. */
  77. #define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
  78. struct res_state_ext;
  79. struct __res_state {
  80. unsigned netid; /* NetId: cache key and socket mark */
  81. u_long options; /* option flags - see below. */
  82. int nscount; /* number of name srvers */
  83. struct sockaddr_in nsaddr_list[MAXNS]; /* address of name server */
  84. #define nsaddr nsaddr_list[0] /* for backward compatibility */
  85. u_short id; /* current message id */
  86. char* dnsrch[MAXDNSRCH + 1]; /* components of domain to search */
  87. char defdname[256]; /* default domain (deprecated) */
  88. unsigned ndots : 4; /* threshold for initial abs. query */
  89. unsigned nsort : 4; /* number of elements in sort_list[] */
  90. char unused[3];
  91. struct {
  92. struct in_addr addr;
  93. uint32_t mask;
  94. } sort_list[MAXRESOLVSORT];
  95. unsigned _mark; /* If non-0 SET_MARK to _mark on all request sockets */
  96. int _vcsock; /* PRIVATE: for res_send VC i/o */
  97. u_int _flags; /* PRIVATE: see below */
  98. u_int _pad; /* make _u 64 bit aligned */
  99. union {
  100. /* On an 32-bit arch this means 512b total. */
  101. char pad[72 - 4 * sizeof(int) - 2 * sizeof(void*)];
  102. struct {
  103. uint16_t nscount;
  104. uint16_t nstimes[MAXNS]; /* ms. */
  105. int nssocks[MAXNS];
  106. struct res_state_ext* ext; /* extention for IPv6 */
  107. } _ext;
  108. } _u;
  109. struct res_static rstatic[1];
  110. bool use_local_nameserver; /* DNS-over-TLS bypass */
  111. android::net::NetworkDnsEventReported* event;
  112. };
  113. typedef struct __res_state* res_state;
  114. /* Retrieve a local copy of the stats for the given netid. The buffer must have space for
  115. * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
  116. */
  117. int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]);
  118. /* Add a sample to the shared struct for the given netid and server, provided that the
  119. * revision_id of the stored servers has not changed.
  120. */
  121. void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
  122. const res_sample* sample, int max_samples);
  123. // Calculate the round-trip-time from start time t0 and end time t1.
  124. int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
  125. // Create a sample for calculating server reachability statistics.
  126. void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
  127. /* End of stats related definitions */
  128. // Flags for res_state->_flags
  129. #define RES_F_VC 0x00000001 // socket is TCP
  130. #define RES_F_EDNS0ERR 0x00000004 // EDNS0 caused errors
  131. /*
  132. * Resolver options (keep these in synch with res_debug.c, please)
  133. */
  134. #define RES_INIT 0x00000001 /* address initialized */
  135. #define RES_DEBUG 0x00000002 /* print debug messages */
  136. #define RES_AAONLY 0x00000004 /* authoritative answers only (!IMPL)*/
  137. #define RES_USEVC 0x00000008 /* use virtual circuit */
  138. #define RES_PRIMARY 0x00000010 /* query primary server only (!IMPL) */
  139. #define RES_IGNTC 0x00000020 /* ignore trucation errors */
  140. #define RES_RECURSE 0x00000040 /* recursion desired */
  141. #define RES_DEFNAMES 0x00000080 /* use default domain name */
  142. #define RES_STAYOPEN 0x00000100 /* Keep TCP socket open */
  143. #define RES_DNSRCH 0x00000200 /* search up local domain tree */
  144. #define RES_INSECURE1 0x00000400 /* type 1 security disabled */
  145. #define RES_INSECURE2 0x00000800 /* type 2 security disabled */
  146. #define RES_USE_INET6 0x00002000 /* use/map IPv6 in gethostbyname() */
  147. #define RES_ROTATE 0x00004000 /* rotate ns list after each query */
  148. #define RES_NOCHECKNAME 0x00008000 /* do not check names for sanity. */
  149. #define RES_KEEPTSIG 0x00010000 /* do not strip TSIG records */
  150. #define RES_BLAST 0x00020000 /* blast all recursive servers */
  151. #define RES_NOTLDQUERY 0x00100000 /* don't unqualified name as a tld */
  152. #define RES_USE_DNSSEC 0x00200000 /* use DNSSEC using OK bit in OPT */
  153. /* #define RES_DEBUG2 0x00400000 */ /* nslookup internal */
  154. /* KAME extensions: use higher bit to avoid conflict with ISC use */
  155. #define RES_USE_DNAME 0x10000000 /* use DNAME */
  156. #define RES_USE_EDNS0 0x40000000 /* use EDNS0 if configured */
  157. #define RES_NO_NIBBLE2 0x80000000 /* disable alternate nibble lookup */
  158. #define RES_DEFAULT (RES_RECURSE | RES_DEFNAMES | RES_DNSRCH | RES_NO_NIBBLE2)
  159. /*
  160. * Error code extending h_errno codes defined in bionic/libc/include/netdb.h.
  161. *
  162. * This error code, including legacy h_errno, is returned from res_nquery(), res_nsearch(),
  163. * res_nquerydomain(), res_queryN(), res_searchN() and res_querydomainN() for DNS metrics.
  164. *
  165. * TODO: Consider mapping legacy and extended h_errno into a unified resolver error code mapping.
  166. */
  167. #define NETD_RESOLV_H_ERRNO_EXT_TIMEOUT RCODE_TIMEOUT
  168. extern const char* const _res_opcodes[];
  169. /* Things involving an internal (static) resolver context. */
  170. struct __res_state* res_get_state(void);
  171. int res_hnok(const char*);
  172. int res_ownok(const char*);
  173. int res_mailok(const char*);
  174. int res_dnok(const char*);
  175. int dn_skipname(const u_char*, const u_char*);
  176. void putlong(uint32_t, u_char*);
  177. void putshort(uint16_t, u_char*);
  178. // Thread-unsafe functions returning pointers to static buffers :-(
  179. // TODO: switch all res_debug to std::string
  180. const char* p_class(int);
  181. const char* p_type(int);
  182. const char* p_rcode(int);
  183. const char* p_section(int, int);
  184. int res_nameinquery(const char*, int, int, const u_char*, const u_char*);
  185. int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*);
  186. /* Things involving a resolver context. */
  187. int res_ninit(res_state);
  188. void res_pquery(const u_char*, int);
  189. int res_nquery(res_state, const char*, int, int, u_char*, int, int*);
  190. int res_nsearch(res_state, const char*, int, int, u_char*, int, int*);
  191. int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int, int*);
  192. int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*,
  193. int);
  194. int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t);
  195. void res_nclose(res_state);
  196. int res_nopt(res_state, int, u_char*, int, int);
  197. int res_vinit(res_state, int);
  198. void res_ndestroy(res_state);
  199. void res_setservers(res_state, const sockaddr_union*, int);
  200. int res_getservers(res_state, sockaddr_union*, int);
  201. struct android_net_context; /* forward */
  202. void res_setnetcontext(res_state, const struct android_net_context*,
  203. android::net::NetworkDnsEventReported* event);
  204. int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
  205. addrinfo** result);
  206. // Helper function for converting h_errno to the error codes visible to netd
  207. int herrnoToAiErrno(int herrno);
  208. // switch resolver log severity
  209. android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr);
  210. template <typename Dest>
  211. Dest saturate_cast(int64_t x) {
  212. using DestLimits = std::numeric_limits<Dest>;
  213. if (x > DestLimits::max()) return DestLimits::max();
  214. if (x < DestLimits::min()) return DestLimits::min();
  215. return static_cast<Dest>(x);
  216. }
  217. android::net::NsType getQueryType(const uint8_t* msg, size_t msgLen);
  218. android::net::IpVersion ipFamilyToIPVersion(int ipFamily);
  219. #endif // NETD_RESOLV_PRIVATE_H