2 * SPDX-License-Identifier: ISC
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium.
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.
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.
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "$Id: res_data.c,v 1.7 2008/12/11 09:59:00 marka Exp $";
22 #endif /* LIBC_SCCS and not lint */
23 #include "port_before.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"
44 const char *_res_opcodes[] = {
48 "CQUERYU", /*%< experimental */
49 "NOTIFY", /*%< experimental */
64 const char *_res_sectioncodes[] = {
72 #ifndef __BIND_NOSTATIC
76 int res_ourserver_p(const res_state, const struct sockaddr_in *);
80 extern int __res_vinit(res_state, int);
81 res_state statp = &_res;
84 * These three fields used to be statically initialized. This made
85 * it hard to use this code in a shared library. It is necessary,
86 * now that we're doing dynamic initialization here, that we preserve
87 * the old semantics: if an application modifies one of these three
88 * fields of _res before res_init() is called, res_init() will not
89 * alter them. Of course, if an application is setting them to
90 * _zero_ before calling res_init(), hoping to override what used
91 * to be the static default, we can't detect it and unexpected results
92 * will follow. Zero for any of these fields would make no sense,
93 * so one can safely assume that the applications were already getting
96 * _res.options is tricky since some apps were known to diddle the bits
97 * before res_init() was first called. We can't replicate that semantic
98 * with dynamic initialization (they may have turned bits off that are
99 * set in RES_DEFAULT). Our solution is to declare such applications
100 * "broken". They could fool us by setting RES_INIT but none do (yet).
103 statp->retrans = RES_TIMEOUT;
105 statp->retry = RES_DFLRETRY;
106 if (!(statp->options & RES_INIT))
107 statp->options = RES_DEFAULT;
109 return (__res_vinit(statp, 1));
113 p_query(const u_char *msg) {
114 fp_query(msg, stdout);
118 fp_query(const u_char *msg, FILE *file) {
119 fp_nquery(msg, PACKETSZ, file);
123 fp_nquery(const u_char *msg, int len, FILE *file) {
124 res_state statp = &_res;
125 if ((statp->options & RES_INIT) == 0U && res_init() == -1)
128 res_pquery(statp, msg, len, file);
132 res_mkquery(int op, /*!< opcode of query */
133 const char *dname, /*!< domain name */
134 int class, int type, /*!< class and type of query */
135 const u_char *data, /*!< resource record data */
136 int datalen, /*!< length of data */
137 const u_char *newrr_in, /*!< new rr for modify or append */
138 u_char *buf, /*!< buffer to put query */
139 int buflen) /*!< size of buffer */
141 res_state statp = &_res;
142 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
143 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
146 return (res_nmkquery(statp, op, dname, class, type,
148 newrr_in, buf, buflen));
152 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
153 res_state statp = &_res;
154 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
155 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
159 return (res_nmkupdate(statp, rrecp_in, buf, buflen));
163 res_query(const char *name, /*!< domain name */
164 int class, int type, /*!< class and type of query */
165 u_char *answer, /*!< buffer to put answer */
166 int anslen) /*!< size of answer buffer */
168 res_state statp = &_res;
169 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
170 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
173 return (res_nquery(statp, name, class, type, answer, anslen));
178 res_send_setqhook(res_send_qhook hook) {
183 res_send_setrhook(res_send_rhook hook) {
189 res_isourserver(const struct sockaddr_in *inp) {
190 return (res_ourserver_p(&_res, inp));
194 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
195 res_state statp = &_res;
196 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
197 /* errno should have been set by res_init() in this case. */
201 return (res_nsend(statp, buf, buflen, ans, anssiz));
206 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
207 u_char *ans, int anssiz)
209 res_state statp = &_res;
210 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
211 /* errno should have been set by res_init() in this case. */
215 return (res_nsendsigned(statp, buf, buflen, key, ans, anssiz));
225 res_update(ns_updrec *rrecp_in) {
226 res_state statp = &_res;
227 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
228 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
232 return (res_nupdate(statp, rrecp_in, NULL));
236 res_search(const char *name, /*!< domain name */
237 int class, int type, /*!< class and type of query */
238 u_char *answer, /*!< buffer to put answer */
239 int anslen) /*!< size of answer */
241 res_state statp = &_res;
242 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
243 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
247 return (res_nsearch(statp, 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 res_state statp = &_res;
258 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
259 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
263 return (res_nquerydomain(statp, name, domain,
270 res_state statp = &_res;
271 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
272 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
276 return (res_nrandomid(statp));
280 res_opt(int n0, u_char *buf, int buflen, int anslen)
282 return (res_nopt(&_res, n0, buf, buflen, anslen));
286 hostalias(const char *name) {
287 static char abuf[MAXDNAME];
289 return (res_hostalias(&_res, name, abuf, sizeof abuf));
294 local_hostname_length(const char *hostname) {
295 int len_host, len_domain;
299 if (!*statp->defdname)
301 len_host = strlen(hostname);
302 len_domain = strlen(statp->defdname);
303 if (len_host > len_domain &&
304 !strcasecmp(hostname + len_host - len_domain, statp->defdname) &&
305 hostname[len_host - len_domain - 1] == '.')
306 return (len_host - len_domain - 1);
312 * Weak aliases for applications that use certain private entry points,
313 * and fail to include <resolv.h>.
316 __weak_reference(__res_init, res_init);
318 __weak_reference(__p_query, p_query);
320 __weak_reference(__res_mkquery, res_mkquery);
322 __weak_reference(__res_query, res_query);
324 __weak_reference(__res_send, res_send);
326 __weak_reference(__res_close, _res_close);
328 __weak_reference(__res_search, res_search);
329 #undef res_querydomain
330 __weak_reference(__res_querydomain, res_querydomain);