xref: /freebsd/lib/libc/resolv/res_data.c (revision 7afb8adff33d47f10a11368ff54bb2eec5b30165)
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 int
81 res_init(void) {
82 	extern int __res_vinit(res_state, int);
83 
84 	/*
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
95 	 * unexpected results.
96 	 *
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).
102 	 */
103 	if (!_res.retrans)
104 		_res.retrans = RES_TIMEOUT;
105 	if (!_res.retry)
106 		_res.retry = RES_DFLRETRY;
107 	if (!(_res.options & RES_INIT))
108 		_res.options = RES_DEFAULT;
109 
110 	return (__res_vinit(&_res, 1));
111 }
112 
113 void
114 p_query(const u_char *msg) {
115 	fp_query(msg, stdout);
116 }
117 
118 void
119 fp_query(const u_char *msg, FILE *file) {
120 	fp_nquery(msg, PACKETSZ, file);
121 }
122 
123 void
124 fp_nquery(const u_char *msg, int len, FILE *file) {
125 	if ((_res.options & RES_INIT) == 0U && res_init() == -1)
126 		return;
127 
128 	res_pquery(&_res, msg, len, file);
129 }
130 
131 int
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  */
140 {
141 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
142 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
143 		return (-1);
144 	}
145 	return (res_nmkquery(&_res, op, dname, class, type,
146 			     data, datalen,
147 			     newrr_in, buf, buflen));
148 }
149 
150 int
151 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
152 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
153 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
154 		return (-1);
155 	}
156 
157 	return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
158 }
159 
160 int
161 res_query(const char *name,	/*!< domain name  */
162 	  int class, int type,	/*!< class and type of query  */
163 	  u_char *answer,	/*!< buffer to put answer  */
164 	  int anslen)		/*!< size of answer buffer  */
165 {
166 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
167 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
168 		return (-1);
169 	}
170 	return (res_nquery(&_res, name, class, type, answer, anslen));
171 }
172 
173 #ifndef _LIBC
174 void
175 res_send_setqhook(res_send_qhook hook) {
176 	_res.qhook = hook;
177 }
178 
179 void
180 res_send_setrhook(res_send_rhook hook) {
181 	_res.rhook = hook;
182 }
183 #endif
184 
185 int
186 res_isourserver(const struct sockaddr_in *inp) {
187 	return (res_ourserver_p(&_res, inp));
188 }
189 
190 int
191 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
192 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
193 		/* errno should have been set by res_init() in this case. */
194 		return (-1);
195 	}
196 
197 	return (res_nsend(&_res, buf, buflen, ans, anssiz));
198 }
199 
200 #ifndef _LIBC
201 int
202 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
203 	       u_char *ans, int anssiz)
204 {
205 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
206 		/* errno should have been set by res_init() in this case. */
207 		return (-1);
208 	}
209 
210 	return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
211 }
212 #endif
213 
214 void
215 res_close(void) {
216 	res_nclose(&_res);
217 }
218 
219 int
220 res_update(ns_updrec *rrecp_in) {
221 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
222 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
223 		return (-1);
224 	}
225 
226 	return (res_nupdate(&_res, rrecp_in, NULL));
227 }
228 
229 int
230 res_search(const char *name,	/*!< domain name  */
231 	   int class, int type,	/*!< class and type of query  */
232 	   u_char *answer,	/*!< buffer to put answer  */
233 	   int anslen)		/*!< size of answer  */
234 {
235 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
236 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
237 		return (-1);
238 	}
239 
240 	return (res_nsearch(&_res, name, class, type, answer, anslen));
241 }
242 
243 int
244 res_querydomain(const char *name,
245 		const char *domain,
246 		int class, int type,	/*!< class and type of query  */
247 		u_char *answer,		/*!< buffer to put answer  */
248 		int anslen)		/*!< size of answer  */
249 {
250 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
251 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
252 		return (-1);
253 	}
254 
255 	return (res_nquerydomain(&_res, name, domain,
256 				 class, type,
257 				 answer, anslen));
258 }
259 
260 u_int
261 res_randomid(void) {
262 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
263 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
264 		return (-1);
265 	}
266 
267 	return (res_nrandomid(&_res));
268 }
269 
270 int
271 res_opt(int n0, u_char *buf, int buflen, int anslen)
272 {
273 	return (res_nopt(&_res, n0, buf, buflen, anslen));
274 }
275 
276 const char *
277 hostalias(const char *name) {
278 	static char abuf[MAXDNAME];
279 
280 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
281 }
282 
283 #ifdef ultrix
284 int
285 local_hostname_length(const char *hostname) {
286 	int len_host, len_domain;
287 
288 	if (!*_res.defdname)
289 		res_init();
290 	len_host = strlen(hostname);
291 	len_domain = strlen(_res.defdname);
292 	if (len_host > len_domain &&
293 	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
294 	    hostname[len_host - len_domain - 1] == '.')
295 		return (len_host - len_domain - 1);
296 	return (0);
297 }
298 #endif /*ultrix*/
299 
300 /*
301  * Weak aliases for applications that use certain private entry points,
302  * and fail to include <resolv.h>.
303  */
304 #undef res_init
305 __weak_reference(__res_init, res_init);
306 #undef p_query
307 __weak_reference(__p_query, p_query);
308 #undef res_mkquery
309 __weak_reference(__res_mkquery, res_mkquery);
310 #undef res_query
311 __weak_reference(__res_query, res_query);
312 #undef res_send
313 __weak_reference(__res_send, res_send);
314 #undef res_close
315 __weak_reference(__res_close, _res_close);
316 #undef res_search
317 __weak_reference(__res_search, res_search);
318 #undef res_querydomain
319 __weak_reference(__res_querydomain, res_querydomain);
320 
321 #endif
322 
323 /*! \file */
324