blob: 7e5a3084918ba8fb150a6c626f32d4b562fb62ac [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>
38#include "arpa_nameser.h"
39
40#include <ctype.h>
41#include <netdb.h>
42#include "resolv_private.h"
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46#include <unistd.h>
47
48
Jim Huang7cc56662010-10-15 02:02:57 +080049__LIBC_HIDDEN__
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050const char * const _res_opcodes[] = {
51 "QUERY",
52 "IQUERY",
53 "CQUERYM",
54 "CQUERYU", /* experimental */
55 "NOTIFY", /* experimental */
56 "UPDATE",
57 "6",
58 "7",
59 "8",
60 "9",
61 "10",
62 "11",
63 "12",
64 "13",
65 "ZONEINIT",
66 "ZONEREF",
67};
68
69#ifdef BIND_UPDATE
70const char * const _res_sectioncodes[] = {
71 "ZONE",
72 "PREREQUISITES",
73 "UPDATE",
74 "ADDITIONAL",
75};
76#endif
77
78#ifndef __BIND_NOSTATIC
79extern struct __res_state _nres;
80
81/* Proto. */
82
83int res_ourserver_p(const res_state, const struct sockaddr *);
84
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080085#define res_need_init() ((_nres.options & RES_INIT) == 0U)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
87int
88res_init(void) {
89 int rv;
90 extern int __res_vinit(res_state, int);
91#ifdef COMPAT__RES
92 /*
93 * Compatibility with program that were accessing _res directly
94 * to set options. We keep another struct res that is the same
95 * size as the original res structure, and then copy fields to
96 * it so that we achieve the same initialization
97 */
98 extern void *__res_get_old_state(void);
99 extern void __res_put_old_state(void *);
100 res_state ores = __res_get_old_state();
101
102 if (ores->options != 0)
103 _nres.options = ores->options;
104 if (ores->retrans != 0)
105 _nres.retrans = ores->retrans;
106 if (ores->retry != 0)
107 _nres.retry = ores->retry;
108#endif
109
110 /*
111 * These three fields used to be statically initialized. This made
112 * it hard to use this code in a shared library. It is necessary,
113 * now that we're doing dynamic initialization here, that we preserve
114 * the old semantics: if an application modifies one of these three
115 * fields of _res before res_init() is called, res_init() will not
116 * alter them. Of course, if an application is setting them to
117 * _zero_ before calling res_init(), hoping to override what used
118 * to be the static default, we can't detect it and unexpected results
119 * will follow. Zero for any of these fields would make no sense,
120 * so one can safely assume that the applications were already getting
121 * unexpected results.
122 *
123 * _nres.options is tricky since some apps were known to diddle the bits
124 * before res_init() was first called. We can't replicate that semantic
125 * with dynamic initialization (they may have turned bits off that are
126 * set in RES_DEFAULT). Our solution is to declare such applications
127 * "broken". They could fool us by setting RES_INIT but none do (yet).
128 */
129 if (!_nres.retrans)
130 _nres.retrans = RES_TIMEOUT;
131 if (!_nres.retry)
132 _nres.retry = 4;
133 if (!(_nres.options & RES_INIT))
134 _nres.options = RES_DEFAULT;
135
136 /*
137 * This one used to initialize implicitly to zero, so unless the app
138 * has set it to something in particular, we can randomize it now.
139 */
140 if (!_nres.id)
141 _nres.id = res_randomid();
142
143 rv = __res_vinit(&_nres, 1);
144#ifdef COMPAT__RES
145 __res_put_old_state(&_nres);
146#endif
147 return rv;
148}
149
150void
151p_query(const u_char *msg) {
152 fp_query(msg, stdout);
153}
154
155void
156fp_query(const u_char *msg, FILE *file) {
157 fp_nquery(msg, PACKETSZ, file);
158}
159
160void
161fp_nquery(const u_char *msg, int len, FILE *file) {
162 if (res_need_init() && res_init() == -1)
163 return;
164
165 res_pquery(&_nres, msg, len, file);
166}
167
168int
169res_mkquery(int op, /* opcode of query */
170 const char *dname, /* domain name */
171 int class, int type, /* class and type of query */
172 const u_char *data, /* resource record data */
173 int datalen, /* length of data */
174 const u_char *newrr_in, /* new rr for modify or append */
175 u_char *buf, /* buffer to put query */
176 int buflen) /* size of buffer */
177{
178 if (res_need_init() && res_init() == -1) {
179 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
180 return (-1);
181 }
182 return (res_nmkquery(&_nres, op, dname, class, type,
183 data, datalen,
184 newrr_in, buf, buflen));
185}
186
187#ifdef _LIBRESOLV
188int
189res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
190 if (res_need_init() && res_init() == -1) {
191 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
192 return (-1);
193 }
194
195 return (res_nmkupdate(&_nres, rrecp_in, buf, buflen));
196}
197#endif
198
199int
200res_query(const char *name, /* domain name */
201 int class, int type, /* class and type of query */
202 u_char *answer, /* buffer to put answer */
203 int anslen) /* size of answer buffer */
204{
205 if (res_need_init() && res_init() == -1) {
206 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
207 return (-1);
208 }
209 return (res_nquery(&_nres, name, class, type, answer, anslen));
210}
211
212void
213res_send_setqhook(res_send_qhook hook) {
214 _nres.qhook = hook;
215}
216
217void
218res_send_setrhook(res_send_rhook hook) {
219 _nres.rhook = hook;
220}
221
222int
223res_isourserver(const struct sockaddr_in *inp) {
224 return (res_ourserver_p(&_nres, (const struct sockaddr *)(const void *)inp));
225}
226
227int
228res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
229 if (res_need_init() && res_init() == -1) {
230 /* errno should have been set by res_init() in this case. */
231 return (-1);
232 }
233
234 return (res_nsend(&_nres, buf, buflen, ans, anssiz));
235}
236
237#ifdef _LIBRESOLV
238int
239res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
240 u_char *ans, int anssiz)
241{
242 if (res_need_init() && res_init() == -1) {
243 /* errno should have been set by res_init() in this case. */
244 return (-1);
245 }
246
247 return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz));
248}
249#endif
250
251void
252res_close(void) {
253 res_nclose(&_nres);
254}
255
256#ifdef _LIBRESOLV
257int
258res_update(ns_updrec *rrecp_in) {
259 if (res_need_init() && res_init() == -1) {
260 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
261 return (-1);
262 }
263
264 return (res_nupdate(&_nres, rrecp_in, NULL));
265}
266#endif
267
268int
269res_search(const char *name, /* domain name */
270 int class, int type, /* class and type of query */
271 u_char *answer, /* buffer to put answer */
272 int anslen) /* size of answer */
273{
274 if (res_need_init() && res_init() == -1) {
275 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
276 return (-1);
277 }
278
279 return (res_nsearch(&_nres, name, class, type, answer, anslen));
280}
281
282int
283res_querydomain(const char *name,
284 const char *domain,
285 int class, int type, /* class and type of query */
286 u_char *answer, /* buffer to put answer */
287 int anslen) /* size of answer */
288{
289 if (res_need_init() && res_init() == -1) {
290 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
291 return (-1);
292 }
293
294 return (res_nquerydomain(&_nres, name, domain,
295 class, type,
296 answer, anslen));
297}
298
299int
300res_opt(int a, u_char *b, int c, int d)
301{
302 return res_nopt(&_nres, a, b, c, d);
303}
304
305const char *
306hostalias(const char *name) {
307 return NULL;
308}
309
310#ifdef ultrix
311int
312local_hostname_length(const char *hostname) {
313 int len_host, len_domain;
314
315 if (!*_nres.defdname)
316 res_init();
317 len_host = strlen(hostname);
318 len_domain = strlen(_nres.defdname);
319 if (len_host > len_domain &&
320 !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
321 hostname[len_host - len_domain - 1] == '.')
322 return (len_host - len_domain - 1);
323 return (0);
324}
325#endif /*ultrix*/
326
327#endif