|
1 /* $NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $ */ |
|
2 |
|
3 /* |
|
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") |
|
5 * Copyright (c) 1995-1999 by Internet Software Consortium. |
|
6 * |
|
7 * Permission to use, copy, modify, and distribute this software for any |
|
8 * purpose with or without fee is hereby granted, provided that the above |
|
9 * copyright notice and this permission notice appear in all copies. |
|
10 * |
|
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES |
|
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
|
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR |
|
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
18 */ |
|
19 |
|
20 /* |
|
21 * This version of this file is derived from Android 2.3 "Gingerbread", |
|
22 * which contains uncredited changes by Android/Google developers. It has |
|
23 * been modified in 2011 for use in the Android build of Mozilla Firefox by |
|
24 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>, |
|
25 * and Steve Workman <sjhworkman@gmail.com>). |
|
26 * These changes are offered under the same license as the original NetBSD |
|
27 * file, whose copyright and license are unchanged above. |
|
28 */ |
|
29 |
|
30 #define ANDROID_CHANGES 1 |
|
31 #define MOZILLA_NECKO_EXCLUDE_CODE 1 |
|
32 |
|
33 #include <sys/cdefs.h> |
|
34 #if defined(LIBC_SCCS) && !defined(lint) |
|
35 #ifdef notdef |
|
36 static const char rcsid[] = "Id: res_data.c,v 1.1.206.2 2004/03/16 12:34:18 marka Exp"; |
|
37 #else |
|
38 __RCSID("$NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $"); |
|
39 #endif |
|
40 #endif /* LIBC_SCCS and not lint */ |
|
41 |
|
42 |
|
43 |
|
44 #include <sys/types.h> |
|
45 #include <sys/param.h> |
|
46 #include <sys/socket.h> |
|
47 #include <sys/time.h> |
|
48 |
|
49 #include <netinet/in.h> |
|
50 #include <arpa/inet.h> |
|
51 #include "arpa_nameser.h" |
|
52 |
|
53 #include <ctype.h> |
|
54 #include <netdb.h> |
|
55 #include "resolv_private.h" |
|
56 #include <stdio.h> |
|
57 #include <stdlib.h> |
|
58 #include <string.h> |
|
59 #include <unistd.h> |
|
60 |
|
61 |
|
62 const char * const _res_opcodes[] = { |
|
63 "QUERY", |
|
64 "IQUERY", |
|
65 "CQUERYM", |
|
66 "CQUERYU", /* experimental */ |
|
67 "NOTIFY", /* experimental */ |
|
68 "UPDATE", |
|
69 "6", |
|
70 "7", |
|
71 "8", |
|
72 "9", |
|
73 "10", |
|
74 "11", |
|
75 "12", |
|
76 "13", |
|
77 "ZONEINIT", |
|
78 "ZONEREF", |
|
79 }; |
|
80 |
|
81 #ifdef BIND_UPDATE |
|
82 const char * const _res_sectioncodes[] = { |
|
83 "ZONE", |
|
84 "PREREQUISITES", |
|
85 "UPDATE", |
|
86 "ADDITIONAL", |
|
87 }; |
|
88 #endif |
|
89 |
|
90 #ifndef MOZILLA_NECKO_EXCLUDE_CODE |
|
91 #ifndef __BIND_NOSTATIC |
|
92 extern struct __res_state _nres; |
|
93 |
|
94 /* Proto. */ |
|
95 |
|
96 int res_ourserver_p(const res_state, const struct sockaddr *); |
|
97 |
|
98 #ifdef ANDROID_CHANGES |
|
99 int res_need_init() { |
|
100 return ((_nres.options & RES_INIT) == 0U) || res_get_dns_changed(); |
|
101 } |
|
102 #else |
|
103 #define res_need_init() ((_nres.options & RES_INIT) == 0U) |
|
104 #endif |
|
105 |
|
106 int |
|
107 res_init(void) { |
|
108 int rv; |
|
109 extern int __res_vinit(res_state, int); |
|
110 #ifdef COMPAT__RES |
|
111 /* |
|
112 * Compatibility with program that were accessing _res directly |
|
113 * to set options. We keep another struct res that is the same |
|
114 * size as the original res structure, and then copy fields to |
|
115 * it so that we achieve the same initialization |
|
116 */ |
|
117 extern void *__res_get_old_state(void); |
|
118 extern void __res_put_old_state(void *); |
|
119 res_state ores = __res_get_old_state(); |
|
120 |
|
121 if (ores->options != 0) |
|
122 _nres.options = ores->options; |
|
123 if (ores->retrans != 0) |
|
124 _nres.retrans = ores->retrans; |
|
125 if (ores->retry != 0) |
|
126 _nres.retry = ores->retry; |
|
127 #endif |
|
128 |
|
129 /* |
|
130 * These three fields used to be statically initialized. This made |
|
131 * it hard to use this code in a shared library. It is necessary, |
|
132 * now that we're doing dynamic initialization here, that we preserve |
|
133 * the old semantics: if an application modifies one of these three |
|
134 * fields of _res before res_init() is called, res_init() will not |
|
135 * alter them. Of course, if an application is setting them to |
|
136 * _zero_ before calling res_init(), hoping to override what used |
|
137 * to be the static default, we can't detect it and unexpected results |
|
138 * will follow. Zero for any of these fields would make no sense, |
|
139 * so one can safely assume that the applications were already getting |
|
140 * unexpected results. |
|
141 * |
|
142 * _nres.options is tricky since some apps were known to diddle the bits |
|
143 * before res_init() was first called. We can't replicate that semantic |
|
144 * with dynamic initialization (they may have turned bits off that are |
|
145 * set in RES_DEFAULT). Our solution is to declare such applications |
|
146 * "broken". They could fool us by setting RES_INIT but none do (yet). |
|
147 */ |
|
148 if (!_nres.retrans) |
|
149 _nres.retrans = RES_TIMEOUT; |
|
150 if (!_nres.retry) |
|
151 _nres.retry = 4; |
|
152 if (!(_nres.options & RES_INIT)) |
|
153 _nres.options = RES_DEFAULT; |
|
154 |
|
155 /* |
|
156 * This one used to initialize implicitly to zero, so unless the app |
|
157 * has set it to something in particular, we can randomize it now. |
|
158 */ |
|
159 if (!_nres.id) |
|
160 _nres.id = res_randomid(); |
|
161 |
|
162 rv = __res_vinit(&_nres, 1); |
|
163 #ifdef COMPAT__RES |
|
164 __res_put_old_state(&_nres); |
|
165 #endif |
|
166 return rv; |
|
167 } |
|
168 |
|
169 void |
|
170 p_query(const u_char *msg) { |
|
171 fp_query(msg, stdout); |
|
172 } |
|
173 |
|
174 void |
|
175 fp_query(const u_char *msg, FILE *file) { |
|
176 fp_nquery(msg, PACKETSZ, file); |
|
177 } |
|
178 |
|
179 void |
|
180 fp_nquery(const u_char *msg, int len, FILE *file) { |
|
181 if (res_need_init() && res_init() == -1) |
|
182 return; |
|
183 |
|
184 res_pquery(&_nres, msg, len, file); |
|
185 } |
|
186 |
|
187 int |
|
188 res_mkquery(int op, /* opcode of query */ |
|
189 const char *dname, /* domain name */ |
|
190 int class, int type, /* class and type of query */ |
|
191 const u_char *data, /* resource record data */ |
|
192 int datalen, /* length of data */ |
|
193 const u_char *newrr_in, /* new rr for modify or append */ |
|
194 u_char *buf, /* buffer to put query */ |
|
195 int buflen) /* size of buffer */ |
|
196 { |
|
197 if (res_need_init() && res_init() == -1) { |
|
198 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
|
199 return (-1); |
|
200 } |
|
201 return (res_nmkquery(&_nres, op, dname, class, type, |
|
202 data, datalen, |
|
203 newrr_in, buf, buflen)); |
|
204 } |
|
205 |
|
206 #ifdef _LIBRESOLV |
|
207 int |
|
208 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) { |
|
209 if (res_need_init() && res_init() == -1) { |
|
210 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
|
211 return (-1); |
|
212 } |
|
213 |
|
214 return (res_nmkupdate(&_nres, rrecp_in, buf, buflen)); |
|
215 } |
|
216 #endif |
|
217 |
|
218 int |
|
219 res_query(const char *name, /* domain name */ |
|
220 int class, int type, /* class and type of query */ |
|
221 u_char *answer, /* buffer to put answer */ |
|
222 int anslen) /* size of answer buffer */ |
|
223 { |
|
224 if (res_need_init() && res_init() == -1) { |
|
225 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
|
226 return (-1); |
|
227 } |
|
228 return (res_nquery(&_nres, name, class, type, answer, anslen)); |
|
229 } |
|
230 |
|
231 void |
|
232 res_send_setqhook(res_send_qhook hook) { |
|
233 _nres.qhook = hook; |
|
234 } |
|
235 |
|
236 void |
|
237 res_send_setrhook(res_send_rhook hook) { |
|
238 _nres.rhook = hook; |
|
239 } |
|
240 |
|
241 int |
|
242 res_isourserver(const struct sockaddr_in *inp) { |
|
243 return (res_ourserver_p(&_nres, (const struct sockaddr *)(const void *)inp)); |
|
244 } |
|
245 |
|
246 int |
|
247 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) { |
|
248 if (res_need_init() && res_init() == -1) { |
|
249 /* errno should have been set by res_init() in this case. */ |
|
250 return (-1); |
|
251 } |
|
252 |
|
253 return (res_nsend(&_nres, buf, buflen, ans, anssiz)); |
|
254 } |
|
255 |
|
256 #ifdef _LIBRESOLV |
|
257 int |
|
258 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key, |
|
259 u_char *ans, int anssiz) |
|
260 { |
|
261 if (res_need_init() && res_init() == -1) { |
|
262 /* errno should have been set by res_init() in this case. */ |
|
263 return (-1); |
|
264 } |
|
265 |
|
266 return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz)); |
|
267 } |
|
268 #endif |
|
269 |
|
270 void |
|
271 res_close(void) { |
|
272 res_nclose(&_nres); |
|
273 } |
|
274 |
|
275 #ifdef _LIBRESOLV |
|
276 int |
|
277 res_update(ns_updrec *rrecp_in) { |
|
278 if (res_need_init() && res_init() == -1) { |
|
279 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
|
280 return (-1); |
|
281 } |
|
282 |
|
283 return (res_nupdate(&_nres, rrecp_in, NULL)); |
|
284 } |
|
285 #endif |
|
286 |
|
287 int |
|
288 res_search(const char *name, /* domain name */ |
|
289 int class, int type, /* class and type of query */ |
|
290 u_char *answer, /* buffer to put answer */ |
|
291 int anslen) /* size of answer */ |
|
292 { |
|
293 if (res_need_init() && res_init() == -1) { |
|
294 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
|
295 return (-1); |
|
296 } |
|
297 |
|
298 return (res_nsearch(&_nres, name, class, type, answer, anslen)); |
|
299 } |
|
300 |
|
301 int |
|
302 res_querydomain(const char *name, |
|
303 const char *domain, |
|
304 int class, int type, /* class and type of query */ |
|
305 u_char *answer, /* buffer to put answer */ |
|
306 int anslen) /* size of answer */ |
|
307 { |
|
308 if (res_need_init() && res_init() == -1) { |
|
309 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
|
310 return (-1); |
|
311 } |
|
312 |
|
313 return (res_nquerydomain(&_nres, name, domain, |
|
314 class, type, |
|
315 answer, anslen)); |
|
316 } |
|
317 |
|
318 int |
|
319 res_opt(int a, u_char *b, int c, int d) |
|
320 { |
|
321 return res_nopt(&_nres, a, b, c, d); |
|
322 } |
|
323 #endif |
|
324 #endif |
|
325 |
|
326 const char * |
|
327 hostalias(const char *name) { |
|
328 return NULL; |
|
329 } |
|
330 |
|
331 #ifndef MOZILLA_NECKO_EXCLUDE_CODE |
|
332 #ifdef ultrix |
|
333 int |
|
334 local_hostname_length(const char *hostname) { |
|
335 int len_host, len_domain; |
|
336 |
|
337 if (!*_nres.defdname) |
|
338 res_init(); |
|
339 len_host = strlen(hostname); |
|
340 len_domain = strlen(_nres.defdname); |
|
341 if (len_host > len_domain && |
|
342 !strcasecmp(hostname + len_host - len_domain, _nres.defdname) && |
|
343 hostname[len_host - len_domain - 1] == '.') |
|
344 return (len_host - len_domain - 1); |
|
345 return (0); |
|
346 } |
|
347 #endif /*ultrix*/ |
|
348 #endif |