xref: /freebsd/lib/libc/resolv/res_data.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*
2  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (c) 1995-1999 by Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17 
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: res_data.c,v 1.7 2008/12/11 09:59:00 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21 #include <sys/cdefs.h>
22 __FBSDID("$FreeBSD$");
23 
24 #include "port_before.h"
25 
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
30 
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
34 
35 #include <ctype.h>
36 #include <netdb.h>
37 #include <resolv.h>
38 #include <res_update.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "port_after.h"
45 
46 const char *_res_opcodes[] = {
47 	"QUERY",
48 	"IQUERY",
49 	"CQUERYM",
50 	"CQUERYU",	/*%< experimental */
51 	"NOTIFY",	/*%< experimental */
52 	"UPDATE",
53 	"6",
54 	"7",
55 	"8",
56 	"9",
57 	"10",
58 	"11",
59 	"12",
60 	"13",
61 	"ZONEINIT",
62 	"ZONEREF",
63 };
64 
65 #ifdef BIND_UPDATE
66 const char *_res_sectioncodes[] = {
67 	"ZONE",
68 	"PREREQUISITES",
69 	"UPDATE",
70 	"ADDITIONAL",
71 };
72 #endif
73 
74 #ifndef __BIND_NOSTATIC
75 
76 /* Proto. */
77 
78 int  res_ourserver_p(const res_state, const struct sockaddr_in *);
79 
80 __noinline int
81 res_init(void) {
82 	extern int __res_vinit(res_state, int);
83 	res_state statp = &_res;
84 
85 	/*
86 	 * These three fields used to be statically initialized.  This made
87 	 * it hard to use this code in a shared library.  It is necessary,
88 	 * now that we're doing dynamic initialization here, that we preserve
89 	 * the old semantics: if an application modifies one of these three
90 	 * fields of _res before res_init() is called, res_init() will not
91 	 * alter them.  Of course, if an application is setting them to
92 	 * _zero_ before calling res_init(), hoping to override what used
93 	 * to be the static default, we can't detect it and unexpected results
94 	 * will follow.  Zero for any of these fields would make no sense,
95 	 * so one can safely assume that the applications were already getting
96 	 * unexpected results.
97 	 *
98 	 * _res.options is tricky since some apps were known to diddle the bits
99 	 * before res_init() was first called. We can't replicate that semantic
100 	 * with dynamic initialization (they may have turned bits off that are
101 	 * set in RES_DEFAULT).  Our solution is to declare such applications
102 	 * "broken".  They could fool us by setting RES_INIT but none do (yet).
103 	 */
104 	if (!statp->retrans)
105 		statp->retrans = RES_TIMEOUT;
106 	if (!statp->retry)
107 		statp->retry = RES_DFLRETRY;
108 	if (!(statp->options & RES_INIT))
109 		statp->options = RES_DEFAULT;
110 
111 	return (__res_vinit(statp, 1));
112 }
113 
114 void
115 p_query(const u_char *msg) {
116 	fp_query(msg, stdout);
117 }
118 
119 void
120 fp_query(const u_char *msg, FILE *file) {
121 	fp_nquery(msg, PACKETSZ, file);
122 }
123 
124 void
125 fp_nquery(const u_char *msg, int len, FILE *file) {
126 	res_state statp = &_res;
127 	if ((statp->options & RES_INIT) == 0U && res_init() == -1)
128 		return;
129 
130 	res_pquery(statp, msg, len, file);
131 }
132 
133 int
134 res_mkquery(int op,			/*!< opcode of query  */
135 	    const char *dname,		/*!< domain name  */
136 	    int class, int type,	/*!< class and type of query  */
137 	    const u_char *data,		/*!< resource record data  */
138 	    int datalen,		/*!< length of data  */
139 	    const u_char *newrr_in,	/*!< new rr for modify or append  */
140 	    u_char *buf,		/*!< buffer to put query  */
141 	    int buflen)			/*!< size of buffer  */
142 {
143 	res_state statp = &_res;
144 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
145 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
146 		return (-1);
147 	}
148 	return (res_nmkquery(statp, op, dname, class, type,
149 			     data, datalen,
150 			     newrr_in, buf, buflen));
151 }
152 
153 int
154 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
155 	res_state statp = &_res;
156 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
157 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
158 		return (-1);
159 	}
160 
161 	return (res_nmkupdate(statp, rrecp_in, buf, buflen));
162 }
163 
164 int
165 res_query(const char *name,	/*!< domain name  */
166 	  int class, int type,	/*!< class and type of query  */
167 	  u_char *answer,	/*!< buffer to put answer  */
168 	  int anslen)		/*!< size of answer buffer  */
169 {
170 	res_state statp = &_res;
171 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
172 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
173 		return (-1);
174 	}
175 	return (res_nquery(statp, name, class, type, answer, anslen));
176 }
177 
178 #ifndef _LIBC
179 void
180 res_send_setqhook(res_send_qhook hook) {
181 	_res.qhook = hook;
182 }
183 
184 void
185 res_send_setrhook(res_send_rhook hook) {
186 	_res.rhook = hook;
187 }
188 #endif
189 
190 int
191 res_isourserver(const struct sockaddr_in *inp) {
192 	return (res_ourserver_p(&_res, inp));
193 }
194 
195 int
196 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
197 	res_state statp = &_res;
198 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
199 		/* errno should have been set by res_init() in this case. */
200 		return (-1);
201 	}
202 
203 	return (res_nsend(statp, buf, buflen, ans, anssiz));
204 }
205 
206 #ifndef _LIBC
207 int
208 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
209 	       u_char *ans, int anssiz)
210 {
211 	res_state statp = &_res;
212 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
213 		/* errno should have been set by res_init() in this case. */
214 		return (-1);
215 	}
216 
217 	return (res_nsendsigned(statp, buf, buflen, key, ans, anssiz));
218 }
219 #endif
220 
221 void
222 res_close(void) {
223 	res_nclose(&_res);
224 }
225 
226 int
227 res_update(ns_updrec *rrecp_in) {
228 	res_state statp = &_res;
229 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
230 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
231 		return (-1);
232 	}
233 
234 	return (res_nupdate(statp, rrecp_in, NULL));
235 }
236 
237 int
238 res_search(const char *name,	/*!< domain name  */
239 	   int class, int type,	/*!< class and type of query  */
240 	   u_char *answer,	/*!< buffer to put answer  */
241 	   int anslen)		/*!< size of answer  */
242 {
243 	res_state statp = &_res;
244 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
245 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
246 		return (-1);
247 	}
248 
249 	return (res_nsearch(statp, name, class, type, answer, anslen));
250 }
251 
252 int
253 res_querydomain(const char *name,
254 		const char *domain,
255 		int class, int type,	/*!< class and type of query  */
256 		u_char *answer,		/*!< buffer to put answer  */
257 		int anslen)		/*!< size of answer  */
258 {
259 	res_state statp = &_res;
260 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
261 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
262 		return (-1);
263 	}
264 
265 	return (res_nquerydomain(statp, name, domain,
266 				 class, type,
267 				 answer, anslen));
268 }
269 
270 u_int
271 res_randomid(void) {
272 	res_state statp = &_res;
273 	if ((statp->options & RES_INIT) == 0U && res_init() == -1) {
274 		RES_SET_H_ERRNO(statp, NETDB_INTERNAL);
275 		return (-1);
276 	}
277 
278 	return (res_nrandomid(statp));
279 }
280 
281 int
282 res_opt(int n0, u_char *buf, int buflen, int anslen)
283 {
284 	return (res_nopt(&_res, n0, buf, buflen, anslen));
285 }
286 
287 const char *
288 hostalias(const char *name) {
289 	static char abuf[MAXDNAME];
290 
291 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
292 }
293 
294 #ifdef ultrix
295 int
296 local_hostname_length(const char *hostname) {
297 	int len_host, len_domain;
298 	res_state statp;
299 
300 	statp = &_res;
301 	if (!*statp->defdname)
302 		res_init();
303 	len_host = strlen(hostname);
304 	len_domain = strlen(statp->defdname);
305 	if (len_host > len_domain &&
306 	    !strcasecmp(hostname + len_host - len_domain, statp->defdname) &&
307 	    hostname[len_host - len_domain - 1] == '.')
308 		return (len_host - len_domain - 1);
309 	return (0);
310 }
311 #endif /*ultrix*/
312 
313 /*
314  * Weak aliases for applications that use certain private entry points,
315  * and fail to include <resolv.h>.
316  */
317 #undef res_init
318 __weak_reference(__res_init, res_init);
319 #undef p_query
320 __weak_reference(__p_query, p_query);
321 #undef res_mkquery
322 __weak_reference(__res_mkquery, res_mkquery);
323 #undef res_query
324 __weak_reference(__res_query, res_query);
325 #undef res_send
326 __weak_reference(__res_send, res_send);
327 #undef res_close
328 __weak_reference(__res_close, _res_close);
329 #undef res_search
330 __weak_reference(__res_search, res_search);
331 #undef res_querydomain
332 __weak_reference(__res_querydomain, res_querydomain);
333 
334 #endif
335 
336 /*! \file */
337