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.
17 * $Id: res_data.c,v 1.5 2007/09/14 05:32:25 marka Exp $
20 #include "port_before.h"
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
34 #include <res_update.h>
40 #include "port_after.h"
46 const char *_res_opcodes
[] = {
50 "CQUERYU", /*%< experimental */
51 "NOTIFY", /*%< experimental */
66 const char *_res_sectioncodes
[] = {
75 #ifndef __BIND_NOSTATIC
77 struct __res_state _res
78 # if defined(__BIND_RES_TEXT)
79 = { RES_TIMEOUT
, } /*%< Motorola, et al. */
84 #if defined(DO_PTHREADS) || defined(__linux)
85 #define _res (*__res_state())
90 int res_ourserver_p(const res_state
, const struct sockaddr_in
*);
94 extern int __res_vinit(res_state
, int);
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.
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).
116 _res
.retrans
= RES_TIMEOUT
;
121 _res
.retry
= RES_DFLRETRY
;
123 if (!(_res
.options
& RES_INIT
))
124 _res
.options
= RES_DEFAULT
;
127 * This one used to initialize implicitly to zero, so unless the app
128 * has set it to something in particular, we can randomize it now.
131 _res
.id
= res_randomid();
133 return (__res_vinit(&_res
, 1));
137 p_query(const u_char
*msg
) {
138 fp_query(msg
, stdout
);
142 fp_query(const u_char
*msg
, FILE *file
) {
143 fp_nquery(msg
, PACKETSZ
, file
);
147 fp_nquery(const u_char
*msg
, int len
, FILE *file
) {
148 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1)
151 res_pquery(&_res
, msg
, len
, file
);
155 res_mkquery(int op
, /*!< opcode of query */
156 const char *dname
, /*!< domain name */
157 int class, int type
, /*!< class and type of query */
158 const u_char
*data
, /*!< resource record data */
159 int datalen
, /*!< length of data */
160 const u_char
*newrr_in
, /*!< new rr for modify or append */
161 u_char
*buf
, /*!< buffer to put query */
162 int buflen
) /*!< size of buffer */
164 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
165 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
168 return (res_nmkquery(&_res
, op
, dname
, class, type
,
170 newrr_in
, buf
, buflen
));
174 res_mkupdate(ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
175 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
176 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
180 return (res_nmkupdate(&_res
, rrecp_in
, buf
, buflen
));
184 res_query(const char *name
, /*!< domain name */
185 int class, int type
, /*!< class and type of query */
186 u_char
*answer
, /*!< buffer to put answer */
187 int anslen
) /*!< size of answer buffer */
189 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
190 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
193 return (res_nquery(&_res
, name
, class, type
, answer
, anslen
));
198 res_send_setqhook(res_send_qhook hook
) {
203 res_send_setrhook(res_send_rhook hook
) {
209 res_isourserver(const struct sockaddr_in
*inp
) {
210 return (res_ourserver_p(&_res
, inp
));
214 res_send(const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
) {
215 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
216 /* errno should have been set by res_init() in this case. */
220 return (res_nsend(&_res
, buf
, buflen
, ans
, anssiz
));
225 res_sendsigned(const u_char
*buf
, int buflen
, ns_tsig_key
*key
,
226 u_char
*ans
, int anssiz
)
228 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
229 /* errno should have been set by res_init() in this case. */
233 return (res_nsendsigned(&_res
, buf
, buflen
, key
, ans
, anssiz
));
243 res_update(ns_updrec
*rrecp_in
) {
244 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
245 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
249 return (res_nupdate(&_res
, rrecp_in
, NULL
));
253 res_search(const char *name
, /*!< domain name */
254 int class, int type
, /*!< class and type of query */
255 u_char
*answer
, /*!< buffer to put answer */
256 int anslen
) /*!< size of answer */
258 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
259 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
263 return (res_nsearch(&_res
, name
, class, type
, answer
, anslen
));
267 res_querydomain(const char *name
,
269 int class, int type
, /*!< class and type of query */
270 u_char
*answer
, /*!< buffer to put answer */
271 int anslen
) /*!< size of answer */
273 if ((_res
.options
& RES_INIT
) == 0U && res_init() == -1) {
274 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
278 return (res_nquerydomain(&_res
, name
, domain
,
285 res_opt(int n0
, u_char
*buf
, int buflen
, int anslen
)
287 return (res_nopt(&_res
, n0
, buf
, buflen
, anslen
));
292 hostalias(const char *name
) {
293 static char abuf
[MAXDNAME
];
295 return (res_hostalias(&_res
, name
, abuf
, sizeof abuf
));
300 local_hostname_length(const char *hostname
) {
301 int len_host
, len_domain
;
305 len_host
= strlen(hostname
);
306 len_domain
= strlen(_res
.defdname
);
307 if (len_host
> len_domain
&&
308 !strcasecmp(hostname
+ len_host
- len_domain
, _res
.defdname
) &&
309 hostname
[len_host
- len_domain
- 1] == '.')
310 return (len_host
- len_domain
- 1);
317 * Weak aliases for applications that use certain private entry points,
318 * and fail to include <resolv.h>.
321 __weak_reference(__res_init
, res_init
);
323 __weak_reference(__p_query
, p_query
);
325 __weak_reference(__res_mkquery
, res_mkquery
);
327 __weak_reference(__res_query
, res_query
);
329 __weak_reference(__res_send
, res_send
);
331 __weak_reference(__res_close
, _res_close
);
333 __weak_reference(__res_search
, res_search
);
334 #undef res_querydomain
335 __weak_reference(__res_querydomain
, res_querydomain
);