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.3.18.2 2007/09/14 05:35:47 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
38 #include <res_update.h>
44 #include "port_after.h"
46 const char *_res_opcodes[] = {
50 "CQUERYU", /*%< experimental */
51 "NOTIFY", /*%< experimental */
66 const char *_res_sectioncodes[] = {
74 #ifndef __BIND_NOSTATIC
78 int res_ourserver_p(const res_state, const struct sockaddr_in *);
82 extern int __res_vinit(res_state, int);
85 * These three fields used to be statically initialized. This made
86 * it hard to use this code in a shared library. It is necessary,
87 * now that we're doing dynamic initialization here, that we preserve
88 * the old semantics: if an application modifies one of these three
89 * fields of _res before res_init() is called, res_init() will not
90 * alter them. Of course, if an application is setting them to
91 * _zero_ before calling res_init(), hoping to override what used
92 * to be the static default, we can't detect it and unexpected results
93 * will follow. Zero for any of these fields would make no sense,
94 * so one can safely assume that the applications were already getting
97 * _res.options is tricky since some apps were known to diddle the bits
98 * before res_init() was first called. We can't replicate that semantic
99 * with dynamic initialization (they may have turned bits off that are
100 * set in RES_DEFAULT). Our solution is to declare such applications
101 * "broken". They could fool us by setting RES_INIT but none do (yet).
104 _res.retrans = RES_TIMEOUT;
106 _res.retry = RES_DFLRETRY;
107 if (!(_res.options & RES_INIT))
108 _res.options = RES_DEFAULT;
111 * This one used to initialize implicitly to zero, so unless the app
112 * has set it to something in particular, we can randomize it now.
115 _res.id = res_randomid();
117 return (__res_vinit(&_res, 1));
121 p_query(const u_char *msg) {
122 fp_query(msg, stdout);
126 fp_query(const u_char *msg, FILE *file) {
127 fp_nquery(msg, PACKETSZ, file);
131 fp_nquery(const u_char *msg, int len, FILE *file) {
132 if ((_res.options & RES_INIT) == 0U && res_init() == -1)
135 res_pquery(&_res, msg, len, file);
139 res_mkquery(int op, /*!< opcode of query */
140 const char *dname, /*!< domain name */
141 int class, int type, /*!< class and type of query */
142 const u_char *data, /*!< resource record data */
143 int datalen, /*!< length of data */
144 const u_char *newrr_in, /*!< new rr for modify or append */
145 u_char *buf, /*!< buffer to put query */
146 int buflen) /*!< size of buffer */
148 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
149 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
152 return (res_nmkquery(&_res, op, dname, class, type,
154 newrr_in, buf, buflen));
158 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
159 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
160 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
164 return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
168 res_query(const char *name, /*!< domain name */
169 int class, int type, /*!< class and type of query */
170 u_char *answer, /*!< buffer to put answer */
171 int anslen) /*!< size of answer buffer */
173 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
174 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
177 return (res_nquery(&_res, name, class, type, answer, anslen));
182 res_send_setqhook(res_send_qhook hook) {
187 res_send_setrhook(res_send_rhook hook) {
193 res_isourserver(const struct sockaddr_in *inp) {
194 return (res_ourserver_p(&_res, inp));
198 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
199 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
200 /* errno should have been set by res_init() in this case. */
204 return (res_nsend(&_res, buf, buflen, ans, anssiz));
209 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
210 u_char *ans, int anssiz)
212 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
213 /* errno should have been set by res_init() in this case. */
217 return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
227 res_update(ns_updrec *rrecp_in) {
228 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
229 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
233 return (res_nupdate(&_res, rrecp_in, NULL));
237 res_search(const char *name, /*!< domain name */
238 int class, int type, /*!< class and type of query */
239 u_char *answer, /*!< buffer to put answer */
240 int anslen) /*!< size of answer */
242 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
243 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
247 return (res_nsearch(&_res, name, class, type, answer, anslen));
251 res_querydomain(const char *name,
253 int class, int type, /*!< class and type of query */
254 u_char *answer, /*!< buffer to put answer */
255 int anslen) /*!< size of answer */
257 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
258 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
262 return (res_nquerydomain(&_res, name, domain,
268 res_opt(int n0, u_char *buf, int buflen, int anslen)
270 return (res_nopt(&_res, n0, buf, buflen, anslen));
274 hostalias(const char *name) {
275 static char abuf[MAXDNAME];
277 return (res_hostalias(&_res, name, abuf, sizeof abuf));
282 local_hostname_length(const char *hostname) {
283 int len_host, len_domain;
287 len_host = strlen(hostname);
288 len_domain = strlen(_res.defdname);
289 if (len_host > len_domain &&
290 !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
291 hostname[len_host - len_domain - 1] == '.')
292 return (len_host - len_domain - 1);
298 * Weak aliases for applications that use certain private entry points,
299 * and fail to include <resolv.h>.
302 __weak_reference(__res_init, res_init);
304 __weak_reference(__p_query, p_query);
306 __weak_reference(__res_mkquery, res_mkquery);
308 __weak_reference(__res_query, res_query);
310 __weak_reference(__res_send, res_send);
312 __weak_reference(__res_close, _res_close);
314 __weak_reference(__res_search, res_search);
315 #undef res_querydomain
316 __weak_reference(__res_querydomain, res_querydomain);