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