blob: 4f8df6930fe394ee99ac1d51c3957ea375efafc4 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/* $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#include <sys/cdefs.h>
21#if defined(LIBC_SCCS) && !defined(lint)
22#ifdef notdef
23static const char rcsid[] = "Id: res_data.c,v 1.1.206.2 2004/03/16 12:34:18 marka Exp";
24#else
25__RCSID("$NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $");
26#endif
27#endif /* LIBC_SCCS and not lint */
28
29
30
31#include <sys/types.h>
32#include <sys/param.h>
33#include <sys/socket.h>
34#include <sys/time.h>
35
36#include <netinet/in.h>
37#include <arpa/inet.h>
Calin Juravle569fb982014-03-04 15:01:29 +000038#include <arpa/nameser.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039
40#include <ctype.h>
41#include <netdb.h>
42#include "resolv_private.h"
Luke Huang75830fb2020-12-23 11:49:11 +080043#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080044#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48
49
Jim Huang7cc56662010-10-15 02:02:57 +080050__LIBC_HIDDEN__
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080051const char * const _res_opcodes[] = {
52 "QUERY",
53 "IQUERY",
54 "CQUERYM",
55 "CQUERYU", /* experimental */
56 "NOTIFY", /* experimental */
57 "UPDATE",
58 "6",
59 "7",
60 "8",
61 "9",
62 "10",
63 "11",
64 "12",
65 "13",
66 "ZONEINIT",
67 "ZONEREF",
68};
69
70#ifdef BIND_UPDATE
71const char * const _res_sectioncodes[] = {
72 "ZONE",
73 "PREREQUISITES",
74 "UPDATE",
75 "ADDITIONAL",
76};
77#endif
78
79#ifndef __BIND_NOSTATIC
80extern struct __res_state _nres;
81
82/* Proto. */
83
84int res_ourserver_p(const res_state, const struct sockaddr *);
85
Luke Huang75830fb2020-12-23 11:49:11 +080086static pthread_once_t once_control = PTHREAD_ONCE_INIT;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087
Luke Huang75830fb2020-12-23 11:49:11 +080088static void init_once(void) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089 extern int __res_vinit(res_state, int);
90#ifdef COMPAT__RES
91 /*
92 * Compatibility with program that were accessing _res directly
93 * to set options. We keep another struct res that is the same
94 * size as the original res structure, and then copy fields to
95 * it so that we achieve the same initialization
96 */
97 extern void *__res_get_old_state(void);
98 extern void __res_put_old_state(void *);
99 res_state ores = __res_get_old_state();
100
101 if (ores->options != 0)
102 _nres.options = ores->options;
103 if (ores->retrans != 0)
104 _nres.retrans = ores->retrans;
105 if (ores->retry != 0)
106 _nres.retry = ores->retry;
107#endif
108
109 /*
110 * These three fields used to be statically initialized. This made
111 * it hard to use this code in a shared library. It is necessary,
112 * now that we're doing dynamic initialization here, that we preserve
113 * the old semantics: if an application modifies one of these three
114 * fields of _res before res_init() is called, res_init() will not
115 * alter them. Of course, if an application is setting them to
116 * _zero_ before calling res_init(), hoping to override what used
117 * to be the static default, we can't detect it and unexpected results
118 * will follow. Zero for any of these fields would make no sense,
119 * so one can safely assume that the applications were already getting
120 * unexpected results.
121 *
122 * _nres.options is tricky since some apps were known to diddle the bits
123 * before res_init() was first called. We can't replicate that semantic
124 * with dynamic initialization (they may have turned bits off that are
125 * set in RES_DEFAULT). Our solution is to declare such applications
126 * "broken". They could fool us by setting RES_INIT but none do (yet).
127 */
128 if (!_nres.retrans)
129 _nres.retrans = RES_TIMEOUT;
130 if (!_nres.retry)
131 _nres.retry = 4;
132 if (!(_nres.options & RES_INIT))
133 _nres.options = RES_DEFAULT;
134
135 /*
136 * This one used to initialize implicitly to zero, so unless the app
137 * has set it to something in particular, we can randomize it now.
138 */
139 if (!_nres.id)
140 _nres.id = res_randomid();
141
Luke Huang75830fb2020-12-23 11:49:11 +0800142 __res_vinit(&_nres, 1);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143#ifdef COMPAT__RES
144 __res_put_old_state(&_nres);
145#endif
Luke Huang75830fb2020-12-23 11:49:11 +0800146}
147
148int
149res_init(void) {
150 pthread_once(&once_control, init_once);
151 return 0;
152}
153
154static res_state get_static_res_state() {
155 pthread_once(&once_control, init_once);
156 return &_nres;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157}
158
159void
160p_query(const u_char *msg) {
161 fp_query(msg, stdout);
162}
163
164void
165fp_query(const u_char *msg, FILE *file) {
166 fp_nquery(msg, PACKETSZ, file);
167}
168
169void
170fp_nquery(const u_char *msg, int len, FILE *file) {
Luke Huang75830fb2020-12-23 11:49:11 +0800171 res_pquery(get_static_res_state(), msg, len, file);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172}
173
174int
175res_mkquery(int op, /* opcode of query */
176 const char *dname, /* domain name */
177 int class, int type, /* class and type of query */
178 const u_char *data, /* resource record data */
179 int datalen, /* length of data */
180 const u_char *newrr_in, /* new rr for modify or append */
181 u_char *buf, /* buffer to put query */
182 int buflen) /* size of buffer */
183{
Luke Huang75830fb2020-12-23 11:49:11 +0800184 return (res_nmkquery(get_static_res_state(), op, dname, class, type,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185 data, datalen,
186 newrr_in, buf, buflen));
187}
188
189#ifdef _LIBRESOLV
190int
191res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
Luke Huang75830fb2020-12-23 11:49:11 +0800192 return (res_nmkupdate(get_static_res_state(), rrecp_in, buf, buflen));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800193}
194#endif
195
196int
197res_query(const char *name, /* domain name */
198 int class, int type, /* class and type of query */
199 u_char *answer, /* buffer to put answer */
200 int anslen) /* size of answer buffer */
201{
Luke Huang75830fb2020-12-23 11:49:11 +0800202 return (res_nquery(get_static_res_state(), name, class, type, answer, anslen));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800203}
204
205void
206res_send_setqhook(res_send_qhook hook) {
207 _nres.qhook = hook;
208}
209
210void
211res_send_setrhook(res_send_rhook hook) {
212 _nres.rhook = hook;
213}
214
215int
216res_isourserver(const struct sockaddr_in *inp) {
217 return (res_ourserver_p(&_nres, (const struct sockaddr *)(const void *)inp));
218}
219
220int
221res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
Luke Huang75830fb2020-12-23 11:49:11 +0800222 return (res_nsend(get_static_res_state(), buf, buflen, ans, anssiz));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223}
224
225#ifdef _LIBRESOLV
226int
227res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
228 u_char *ans, int anssiz)
229{
Luke Huang75830fb2020-12-23 11:49:11 +0800230 return (res_nsendsigned(get_static_res_state(), buf, buflen, key, ans, anssiz));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231}
232#endif
233
234void
235res_close(void) {
236 res_nclose(&_nres);
237}
238
239#ifdef _LIBRESOLV
240int
241res_update(ns_updrec *rrecp_in) {
Luke Huang75830fb2020-12-23 11:49:11 +0800242 return (res_nupdate(get_static_res_state(), rrecp_in, NULL));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243}
244#endif
245
246int
247res_search(const char *name, /* domain name */
248 int class, int type, /* class and type of query */
249 u_char *answer, /* buffer to put answer */
250 int anslen) /* size of answer */
251{
Luke Huang75830fb2020-12-23 11:49:11 +0800252 return (res_nsearch(get_static_res_state(), name, class, type, answer, anslen));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800253}
254
255int
256res_querydomain(const char *name,
257 const char *domain,
258 int class, int type, /* class and type of query */
259 u_char *answer, /* buffer to put answer */
260 int anslen) /* size of answer */
261{
Luke Huang75830fb2020-12-23 11:49:11 +0800262 return (res_nquerydomain(get_static_res_state(), name, domain,
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800263 class, type,
264 answer, anslen));
265}
266
267int
268res_opt(int a, u_char *b, int c, int d)
269{
270 return res_nopt(&_nres, a, b, c, d);
271}
272
273const char *
274hostalias(const char *name) {
275 return NULL;
276}
277
278#ifdef ultrix
279int
280local_hostname_length(const char *hostname) {
281 int len_host, len_domain;
282
283 if (!*_nres.defdname)
284 res_init();
285 len_host = strlen(hostname);
286 len_domain = strlen(_nres.defdname);
287 if (len_host > len_domain &&
288 !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
289 hostname[len_host - len_domain - 1] == '.')
290 return (len_host - len_domain - 1);
291 return (0);
292}
293#endif /*ultrix*/
294
295#endif