2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$Id: res_data.c,v 1.5 2007/09/14 05:32:25 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include "port_before.h"
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
36 #include <res_update.h>
42 #include "port_after.h"
48 const char *_res_opcodes
[] = {
52 "CQUERYU", /*%< experimental */
53 "NOTIFY", /*%< experimental */
68 const char *_res_sectioncodes
[] = {
77 #ifndef __BIND_NOSTATIC
79 struct __res_state _res
80 # if defined(__BIND_RES_TEXT)
81 = { RES_TIMEOUT
, } /*%< Motorola, et al. */
86 #if defined(DO_PTHREADS) || defined(__linux)
87 #define _res (*__res_state())
92 int res_ourserver_p(const res_state
, const struct sockaddr_in
*);
96 extern int __res_vinit(res_state
, int);
99 * These three fields used to be statically initialized. This made
100 * it hard to use this code in a shared library. It is necessary,
101 * now that we're doing dynamic initialization here, that we preserve
102 * the old semantics: if an application modifies one of these three
103 * fields of _res before res_init() is called, res_init() will not
104 * alter them. Of course, if an application is setting them to
105 * _zero_ before calling res_init(), hoping to override what used
106 * to be the static default, we can't detect it and unexpected results
107 * will follow. Zero for any of these fields would make no sense,
108 * so one can safely assume that the applications were already getting
109 * unexpected results.
111 * _res.options is tricky since some apps were known to diddle the bits
112 * before res_init() was first called. We can't replicate that semantic
113 * with dynamic initialization (they may have turned bits off that are
114 * set in RES_DEFAULT). Our solution is to declare such applications
115 * "broken". They could fool us by setting RES_INIT but none do (yet).
118 _res
.retrans
= RES_TIMEOUT
;
123 _res
.retry
= RES_DFLRETRY
;
125 if (!(_res
.options
& RES_INIT
))
126 _res
.options
= RES_DEFAULT
;
129 * This one used to initialize implicitly to zero, so unless the app
130 * has set it to something in particular, we can randomize it now.
133 _res
.id
= res_randomid();
135 return (__res_vinit(&_res
, 1));
139 p_query(const u_char
*msg
) {
140 fp_query(msg
, stdout
);
144 fp_query(const u_char
*msg
, FILE *file
) {
145 fp_nquery(msg
, PACKETSZ
, file
);
149 fp_nquery(const u_char
*msg
, int len
, FILE *file
) {
150 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1)
153 res_pquery(&_res
, msg
, len
, file
);
157 res_mkquery(int op
, /*!< opcode of query */
158 const char *dname
, /*!< domain name */
159 int class, int type
, /*!< class and type of query */
160 const u_char
*data
, /*!< resource record data */
161 int datalen
, /*!< length of data */
162 const u_char
*newrr_in
, /*!< new rr for modify or append */
163 u_char
*buf
, /*!< buffer to put query */
164 int buflen
) /*!< size of buffer */
166 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
167 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
170 return (res_nmkquery(&_res
, op
, dname
, class, type
,
172 newrr_in
, buf
, buflen
));
176 res_mkupdate(ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
177 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
178 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
182 return (res_nmkupdate(&_res
, rrecp_in
, buf
, buflen
));
186 res_query(const char *name
, /*!< domain name */
187 int class, int type
, /*!< class and type of query */
188 u_char
*answer
, /*!< buffer to put answer */
189 int anslen
) /*!< size of answer buffer */
191 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
192 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
195 return (res_nquery(&_res
, name
, class, type
, answer
, anslen
));
200 res_send_setqhook(res_send_qhook hook
) {
205 res_send_setrhook(res_send_rhook hook
) {
211 res_isourserver(const struct sockaddr_in
*inp
) {
212 return (res_ourserver_p(&_res
, inp
));
216 res_send(const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
) {
217 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
218 /* errno should have been set by res_init() in this case. */
222 return (res_nsend(&_res
, buf
, buflen
, ans
, anssiz
));
227 res_sendsigned(const u_char
*buf
, int buflen
, ns_tsig_key
*key
,
228 u_char
*ans
, int anssiz
)
230 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
231 /* errno should have been set by res_init() in this case. */
235 return (res_nsendsigned(&_res
, buf
, buflen
, key
, ans
, anssiz
));
245 res_update(ns_updrec
*rrecp_in
) {
246 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
247 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
251 return (res_nupdate(&_res
, rrecp_in
, NULL
));
255 res_search(const char *name
, /*!< domain name */
256 int class, int type
, /*!< class and type of query */
257 u_char
*answer
, /*!< buffer to put answer */
258 int anslen
) /*!< size of answer */
260 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
261 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
265 return (res_nsearch(&_res
, name
, class, type
, answer
, anslen
));
269 res_querydomain(const char *name
,
271 int class, int type
, /*!< class and type of query */
272 u_char
*answer
, /*!< buffer to put answer */
273 int anslen
) /*!< size of answer */
275 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
276 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
280 return (res_nquerydomain(&_res
, name
, domain
,
287 res_opt(int n0
, u_char
*buf
, int buflen
, int anslen
)
289 return (res_nopt(&_res
, n0
, buf
, buflen
, anslen
));
294 hostalias(const char *name
) {
295 static char abuf
[MAXDNAME
];
297 return (res_hostalias(&_res
, name
, abuf
, sizeof abuf
));
302 local_hostname_length(const char *hostname
) {
303 int len_host
, len_domain
;
307 len_host
= strlen(hostname
);
308 len_domain
= strlen(_res
.defdname
);
309 if (len_host
> len_domain
&&
310 !strcasecmp(hostname
+ len_host
- len_domain
, _res
.defdname
) &&
311 hostname
[len_host
- len_domain
- 1] == '.')
312 return (len_host
- len_domain
- 1);
319 * Weak aliases for applications that use certain private entry points,
320 * and fail to include <resolv.h>.
323 __weak_reference(__res_init
, res_init
);
325 __weak_reference(__p_query
, p_query
);
327 __weak_reference(__res_mkquery
, res_mkquery
);
329 __weak_reference(__res_query
, res_query
);
331 __weak_reference(__res_send
, res_send
);
333 __weak_reference(__res_close
, _res_close
);
335 __weak_reference(__res_search
, res_search
);
336 #undef res_querydomain
337 __weak_reference(__res_querydomain
, res_querydomain
);