• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
54 #ifndef NETD_RESOLV_PRIVATE_H
55 #define NETD_RESOLV_PRIVATE_H
56 
57 #include <android-base/logging.h>
58 #include <net/if.h>
59 #include <resolv.h>
60 #include <time.h>
61 #include <string>
62 
63 #include "netd_resolv/params.h"
64 #include "netd_resolv/resolv.h"
65 #include "netd_resolv/stats.h"
66 #include "resolv_static.h"
67 
68 // Linux defines MAXHOSTNAMELEN as 64, while the domain name limit in
69 // RFC 1034 and RFC 1035 is 255 octets.
70 #ifdef MAXHOSTNAMELEN
71 #undef MAXHOSTNAMELEN
72 #endif
73 #define MAXHOSTNAMELEN 256
74 
75 /*
76  * Global defines and variables for resolver stub.
77  */
78 #define MAXDFLSRCH 3       /* # default domain levels to try */
79 #define LOCALDOMAINPARTS 2 /* min levels in name that is "local" */
80 
81 #define RES_TIMEOUT 5000 /* min. milliseconds between retries */
82 #define MAXRESOLVSORT 10  /* number of net to sort on */
83 #define RES_MAXNDOTS 15   /* should reflect bit field size */
84 #define RES_DFLRETRY 2    /* Default #/tries. */
85 #define RES_MAXTIME 65535 /* Infinity, in milliseconds. */
86 
87 struct res_state_ext;
88 
89 struct __res_state {
90     unsigned netid;                        /* NetId: cache key and socket mark */
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     char* dnsrch[MAXDNSRCH + 1];           /* components of domain to search */
97     char defdname[256];                    /* default domain (deprecated) */
98     unsigned ndots : 4;                    /* threshold for initial abs. query */
99     unsigned nsort : 4;                    /* number of elements in sort_list[] */
100     char unused[3];
101     struct {
102         struct in_addr addr;
103         uint32_t mask;
104     } sort_list[MAXRESOLVSORT];
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];
116             struct res_state_ext* ext; /* extention for IPv6 */
117         } _ext;
118     } _u;
119     struct res_static rstatic[1];
120     bool use_local_nameserver; /* DNS-over-TLS bypass */
121 };
122 
123 typedef struct __res_state* res_state;
124 
125 /* Retrieve a local copy of the stats for the given netid. The buffer must have space for
126  * MAXNS __resolver_stats. Returns the revision id of the resolvers used.
127  */
128 int resolv_cache_get_resolver_stats(unsigned netid, res_params* params, res_stats stats[MAXNS]);
129 
130 /* Add a sample to the shared struct for the given netid and server, provided that the
131  * revision_id of the stored servers has not changed.
132  */
133 void _resolv_cache_add_resolver_stats_sample(unsigned netid, int revision_id, int ns,
134                                              const res_sample* sample, int max_samples);
135 
136 // Calculate the round-trip-time from start time t0 and end time t1.
137 int _res_stats_calculate_rtt(const timespec* t1, const timespec* t0);
138 
139 // Create a sample for calculating server reachability statistics.
140 void _res_stats_set_sample(res_sample* sample, time_t now, int rcode, int rtt);
141 
142 /* End of stats related definitions */
143 
144 // Flags for res_state->_flags
145 #define RES_F_VC 0x00000001        // socket is TCP
146 #define RES_F_EDNS0ERR 0x00000004  // EDNS0 caused errors
147 
148 /*
149  * Resolver options (keep these in synch with res_debug.c, please)
150  */
151 #define RES_INIT 0x00000001           /* address initialized */
152 #define RES_DEBUG 0x00000002          /* print debug messages */
153 #define RES_AAONLY 0x00000004         /* authoritative answers only (!IMPL)*/
154 #define RES_USEVC 0x00000008          /* use virtual circuit */
155 #define RES_PRIMARY 0x00000010        /* query primary server only (!IMPL) */
156 #define RES_IGNTC 0x00000020          /* ignore trucation errors */
157 #define RES_RECURSE 0x00000040        /* recursion desired */
158 #define RES_DEFNAMES 0x00000080       /* use default domain name */
159 #define RES_STAYOPEN 0x00000100       /* Keep TCP socket open */
160 #define RES_DNSRCH 0x00000200         /* search up local domain tree */
161 #define RES_INSECURE1 0x00000400      /* type 1 security disabled */
162 #define RES_INSECURE2 0x00000800      /* type 2 security disabled */
163 #define RES_USE_INET6 0x00002000      /* use/map IPv6 in gethostbyname() */
164 #define RES_ROTATE 0x00004000         /* rotate ns list after each query */
165 #define RES_NOCHECKNAME 0x00008000    /* do not check names for sanity. */
166 #define RES_KEEPTSIG 0x00010000       /* do not strip TSIG records */
167 #define RES_BLAST 0x00020000          /* blast all recursive servers */
168 #define RES_NOTLDQUERY 0x00100000     /* don't unqualified name as a tld */
169 #define RES_USE_DNSSEC 0x00200000     /* use DNSSEC using OK bit in OPT */
170 /* #define RES_DEBUG2	0x00400000 */ /* nslookup internal */
171 /* KAME extensions: use higher bit to avoid conflict with ISC use */
172 #define RES_USE_DNAME 0x10000000  /* use DNAME */
173 #define RES_USE_EDNS0 0x40000000  /* use EDNS0 if configured */
174 #define RES_NO_NIBBLE2 0x80000000 /* disable alternate nibble lookup */
175 
176 #define RES_DEFAULT (RES_RECURSE | RES_DEFNAMES | RES_DNSRCH | RES_NO_NIBBLE2)
177 
178 
179 /*
180  * Error code extending h_errno codes defined in bionic/libc/include/netdb.h.
181  *
182  * This error code, including legacy h_errno, is returned from res_nquery(), res_nsearch(),
183  * res_nquerydomain(), res_queryN(), res_searchN() and res_querydomainN() for DNS metrics.
184  *
185  * TODO: Consider mapping legacy and extended h_errno into a unified resolver error code mapping.
186  */
187 #define NETD_RESOLV_H_ERRNO_EXT_TIMEOUT RCODE_TIMEOUT
188 
189 extern const char* const _res_opcodes[];
190 
191 /* Things involving an internal (static) resolver context. */
192 struct __res_state* res_get_state(void);
193 
194 int res_hnok(const char*);
195 int res_ownok(const char*);
196 int res_mailok(const char*);
197 int res_dnok(const char*);
198 int dn_skipname(const u_char*, const u_char*);
199 void putlong(uint32_t, u_char*);
200 void putshort(uint16_t, u_char*);
201 
202 // Thread-unsafe functions returning pointers to static buffers :-(
203 // TODO: switch all res_debug to std::string
204 const char* p_class(int);
205 const char* p_type(int);
206 const char* p_rcode(int);
207 const char* p_section(int, int);
208 
209 int res_nameinquery(const char*, int, int, const u_char*, const u_char*);
210 int res_queriesmatch(const u_char*, const u_char*, const u_char*, const u_char*);
211 /* Things involving a resolver context. */
212 int res_ninit(res_state);
213 void res_pquery(const u_char*, int);
214 
215 int res_nquery(res_state, const char*, int, int, u_char*, int, int*);
216 int res_nsearch(res_state, const char*, int, int, u_char*, int, int*);
217 int res_nquerydomain(res_state, const char*, const char*, int, int, u_char*, int, int*);
218 int res_nmkquery(res_state, int, const char*, int, int, const u_char*, int, const u_char*, u_char*,
219                  int);
220 int res_nsend(res_state, const u_char*, int, u_char*, int, int*, uint32_t);
221 void res_nclose(res_state);
222 int res_nopt(res_state, int, u_char*, int, int);
223 int res_vinit(res_state, int);
224 void res_ndestroy(res_state);
225 void res_setservers(res_state, const sockaddr_union*, int);
226 int res_getservers(res_state, sockaddr_union*, int);
227 
228 struct android_net_context; /* forward */
229 void res_setnetcontext(res_state, const struct android_net_context*);
230 
231 int getaddrinfo_numeric(const char* hostname, const char* servname, addrinfo hints,
232                         addrinfo** result);
233 
234 // Helper function for converting h_errno to the error codes visible to netd
235 int herrnoToAiErrno(int herrno);
236 
237 // switch resolver log severity
238 android::base::LogSeverity logSeverityStrToEnum(const std::string& logSeverityStr);
239 
240 #endif  // NETD_RESOLV_PRIVATE_H
241