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