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 #include "port_before.h"
22 #include <sys/param.h>
23 #include <sys/socket.h>
26 #include <netinet/in.h>
27 #include <arpa/inet.h>
28 #include <arpa/nameser.h>
33 #include <res_update.h>
39 #include "port_after.h"
41 const char *_res_opcodes[] = {
45 "CQUERYU", /*%< experimental */
46 "NOTIFY", /*%< experimental */
61 const char *_res_sectioncodes[] = {
69 #ifndef __BIND_NOSTATIC
73 int res_ourserver_p(const res_state, const struct sockaddr_in *);
77 extern int __res_vinit(res_state, int);
78 res_state statp = &_res;
81 * These three fields used to be statically initialized. This made
82 * it hard to use this code in a shared library. It is necessary,
83 * now that we're doing dynamic initialization here, that we preserve
84 * the old semantics: if an application modifies one of these three
85 * fields of _res before res_init() is called, res_init() will not
86 * alter them. Of course, if an application is setting them to
87 * _zero_ before calling res_init(), hoping to override what used
88 * to be the static default, we can't detect it and unexpected results
89 * will follow. Zero for any of these fields would make no sense,
90 * so one can safely assume that the applications were already getting
93 * _res.options is tricky since some apps were known to diddle the bits
94 * before res_init() was first called. We can't replicate that semantic
95 * with dynamic initialization (they may have turned bits off that are
96 * set in RES_DEFAULT). Our solution is to declare such applications
97 * "broken". They could fool us by setting RES_INIT but none do (yet).
100 statp->retrans = RES_TIMEOUT;
102 statp->retry = RES_DFLRETRY;
103 if (!(statp->options & RES_INIT))
104 statp->options = RES_DEFAULT;
106 return (__res_vinit(statp, 1));
110 p_query(const u_char *msg) {
111 fp_query(msg, stdout);
115 fp_query(const u_char *msg, FILE *file) {
116 fp_nquery(msg, PACKETSZ, file);
120 fp_nquery(const u_char *msg, int len, FILE *file) {
121 res_state statp = &_res;
122 if ((statp->options & RES_INIT) == 0U && res_init() == -1)
125 res_pquery(statp, msg, len, file);
129 res_mkquery(int op, /*!< opcode of query */
130 const char *dname, /*!< domain name */
131 int class, int type, /*!< class and type of query */
132 const u_char *data, /*!< resource record data */
133 int datalen, /*!< length of data */
134 const u_char *newrr_in, /*!< new rr for modify or append */
135 u_char *buf, /*!< buffer to put query */
136 int buflen) /*!< size of buffer */
138 res_state statp = &_res;
139 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
140 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
143 return (res_nmkquery(statp, op, dname, class, type,
145 newrr_in, buf, buflen));
149 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
150 res_state statp = &_res;
151 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
152 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
156 return (res_nmkupdate(statp, rrecp_in, buf, buflen));
160 res_query(const char *name, /*!< domain name */
161 int class, int type, /*!< class and type of query */
162 u_char *answer, /*!< buffer to put answer */
163 int anslen) /*!< size of answer buffer */
165 res_state statp = &_res;
166 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
167 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
170 return (res_nquery(statp, name, class, type, answer, anslen));
175 res_send_setqhook(res_send_qhook hook) {
180 res_send_setrhook(res_send_rhook hook) {
186 res_isourserver(const struct sockaddr_in *inp) {
187 return (res_ourserver_p(&_res, inp));
191 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
192 res_state statp = &_res;
193 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
194 /* errno should have been set by res_init() in this case. */
198 return (res_nsend(statp, buf, buflen, ans, anssiz));
203 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
204 u_char *ans, int anssiz)
206 res_state statp = &_res;
207 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
208 /* errno should have been set by res_init() in this case. */
212 return (res_nsendsigned(statp, buf, buflen, key, ans, anssiz));
222 res_update(ns_updrec *rrecp_in) {
223 res_state statp = &_res;
224 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
225 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
229 return (res_nupdate(statp, rrecp_in, NULL));
233 res_search(const char *name, /*!< domain name */
234 int class, int type, /*!< class and type of query */
235 u_char *answer, /*!< buffer to put answer */
236 int anslen) /*!< size of answer */
238 res_state statp = &_res;
239 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
240 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
244 return (res_nsearch(statp, name, class, type, answer, anslen));
248 res_querydomain(const char *name,
250 int class, int type, /*!< class and type of query */
251 u_char *answer, /*!< buffer to put answer */
252 int anslen) /*!< size of answer */
254 res_state statp = &_res;
255 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
256 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
260 return (res_nquerydomain(statp, name, domain,
267 res_state statp = &_res;
268 if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
269 RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
273 return (res_nrandomid(statp));
277 res_opt(int n0, u_char *buf, int buflen, int anslen)
279 return (res_nopt(&_res, n0, buf, buflen, anslen));
283 hostalias(const char *name) {
284 static char abuf[MAXDNAME];
286 return (res_hostalias(&_res, name, abuf, sizeof abuf));
291 local_hostname_length(const char *hostname) {
292 int len_host, len_domain;
296 if (!*statp->defdname)
298 len_host = strlen(hostname);
299 len_domain = strlen(statp->defdname);
300 if (len_host > len_domain &&
301 !strcasecmp(hostname + len_host - len_domain, statp->defdname) &&
302 hostname[len_host - len_domain - 1] == '.')
303 return (len_host - len_domain - 1);
309 * Weak aliases for applications that use certain private entry points,
310 * and fail to include <resolv.h>.
313 __weak_reference(__res_init, res_init);
315 __weak_reference(__p_query, p_query);
317 __weak_reference(__res_mkquery, res_mkquery);
319 __weak_reference(__res_query, res_query);
321 __weak_reference(__res_send, res_send);
323 __weak_reference(__res_close, _res_close);
325 __weak_reference(__res_search, res_search);
326 #undef res_querydomain
327 __weak_reference(__res_querydomain, res_querydomain);