xref: /illumos-gate/usr/src/lib/libresolv2/common/resolv/res_data.c (revision 8a2b682e57a046b828f37bcde1776f131ef4629f)
1 /*
2  * Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved.
3  */
4 
5 
6 /*
7  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
8  * Copyright (c) 1995-1999 by Internet Software Consortium.
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
20  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22 
23 #include "port_before.h"
24 
25 #include <sys/types.h>
26 #include <sys/param.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
33 
34 #include <ctype.h>
35 #include <netdb.h>
36 #include <resolv.h>
37 #include <res_update.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "port_after.h"
44 
45 #ifndef	ORIGINAL_ISC_CODE
46 #pragma weak	__fp_nquery	=	fp_nquery
47 #pragma weak	__fp_query	=	fp_query
48 #pragma weak	__p_query	=	p_query
49 #pragma weak	__hostalias	=	hostalias
50 #pragma weak	__res_randomid	=	res_randomid
51 #endif
52 
53 const char *_res_opcodes[] = {
54 	"QUERY",
55 	"IQUERY",
56 	"CQUERYM",
57 	"CQUERYU",	/*%< experimental */
58 	"NOTIFY",	/*%< experimental */
59 	"UPDATE",
60 	"6",
61 	"7",
62 	"8",
63 	"9",
64 	"10",
65 	"11",
66 	"12",
67 	"13",
68 	"ZONEINIT",
69 	"ZONEREF",
70 };
71 
72 #ifdef BIND_UPDATE
73 const char *_res_sectioncodes[] = {
74 	"ZONE",
75 	"PREREQUISITES",
76 	"UPDATE",
77 	"ADDITIONAL",
78 };
79 #endif
80 
81 #undef _res
82 #ifndef __BIND_NOSTATIC
83 struct __res_state _res
84 # if defined(__BIND_RES_TEXT)
85 	= { RES_TIMEOUT, }	/*%< Motorola, et al. */
86 # endif
87         ;
88 
89 #ifdef	ORIGINAL_ISC_CODE
90 #if defined(DO_PTHREADS) || defined(__linux)
91 #define _res (*__res_state())
92 #endif
93 #endif
94 
95 /* Proto. */
96 
97 int  res_ourserver_p(const res_state, const struct sockaddr_in *);
98 
99 int
100 res_init(void) {
101 	extern int __res_vinit(res_state, int);
102 
103 	/*
104 	 * These three fields used to be statically initialized.  This made
105 	 * it hard to use this code in a shared library.  It is necessary,
106 	 * now that we're doing dynamic initialization here, that we preserve
107 	 * the old semantics: if an application modifies one of these three
108 	 * fields of _res before res_init() is called, res_init() will not
109 	 * alter them.  Of course, if an application is setting them to
110 	 * _zero_ before calling res_init(), hoping to override what used
111 	 * to be the static default, we can't detect it and unexpected results
112 	 * will follow.  Zero for any of these fields would make no sense,
113 	 * so one can safely assume that the applications were already getting
114 	 * unexpected results.
115 	 *
116 	 * _res.options is tricky since some apps were known to diddle the bits
117 	 * before res_init() was first called. We can't replicate that semantic
118 	 * with dynamic initialization (they may have turned bits off that are
119 	 * set in RES_DEFAULT).  Our solution is to declare such applications
120 	 * "broken".  They could fool us by setting RES_INIT but none do (yet).
121 	 */
122 	if (!_res.retrans)
123 		_res.retrans = RES_TIMEOUT;
124 	if (!_res.retry)
125 		_res.retry = 4;
126 	if (!(_res.options & RES_INIT))
127 		_res.options = RES_DEFAULT;
128 
129 	/*
130 	 * This one used to initialize implicitly to zero, so unless the app
131 	 * has set it to something in particular, we can randomize it now.
132 	 */
133 	if (!_res.id)
134 		_res.id = res_nrandomid(&_res);
135 
136 	return (__res_vinit(&_res, 1));
137 }
138 
139 void
140 p_query(const u_char *msg) {
141 	fp_query(msg, stdout);
142 }
143 
144 void
145 fp_query(const u_char *msg, FILE *file) {
146 	fp_nquery(msg, PACKETSZ, file);
147 }
148 
149 void
150 fp_nquery(const u_char *msg, int len, FILE *file) {
151 	if ((_res.options & RES_INIT) == 0U && res_init() == -1)
152 		return;
153 
154 	res_pquery(&_res, msg, len, file);
155 }
156 
157 int
158 res_mkquery(int op,			/*!< opcode of query  */
159 	    const char *dname,		/*!< domain name  */
160 	    int class, int type,	/*!< class and type of query  */
161 	    const u_char *data,		/*!< resource record data  */
162 	    int datalen,		/*!< length of data  */
163 	    const u_char *newrr_in,	/*!< new rr for modify or append  */
164 	    u_char *buf,		/*!< buffer to put query  */
165 	    int buflen)			/*!< size of buffer  */
166 {
167 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
168 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
169 		return (-1);
170 	}
171 	return (res_nmkquery(&_res, op, dname, class, type,
172 			     data, datalen,
173 			     newrr_in, buf, buflen));
174 }
175 
176 int
177 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
178 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
179 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
180 		return (-1);
181 	}
182 
183 	return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
184 }
185 
186 int
187 res_query(const char *name,	/*!< domain name  */
188 	  int class, int type,	/*!< class and type of query  */
189 	  u_char *answer,	/*!< buffer to put answer  */
190 	  int anslen)		/*!< size of answer buffer  */
191 {
192 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
193 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
194 		return (-1);
195 	}
196 	return (res_nquery(&_res, name, class, type, answer, anslen));
197 }
198 
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 
209 int
210 res_isourserver(const struct sockaddr_in *inp) {
211 	return (res_ourserver_p(&_res, inp));
212 }
213 
214 int
215 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
216 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
217 		/* errno should have been set by res_init() in this case. */
218 		return (-1);
219 	}
220 
221 	return (res_nsend(&_res, buf, buflen, ans, anssiz));
222 }
223 
224 int
225 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
226 	       u_char *ans, int anssiz)
227 {
228 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
229 		/* errno should have been set by res_init() in this case. */
230 		return (-1);
231 	}
232 
233 	return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
234 }
235 
236 void
237 res_close(void) {
238 	res_nclose(&_res);
239 }
240 
241 int
242 res_update(ns_updrec *rrecp_in) {
243 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
244 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
245 		return (-1);
246 	}
247 
248 	return (res_nupdate(&_res, rrecp_in, NULL));
249 }
250 
251 int
252 res_search(const char *name,	/*!< domain 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  */
256 {
257 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
258 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
259 		return (-1);
260 	}
261 
262 	return (res_nsearch(&_res, name, class, type, answer, anslen));
263 }
264 
265 int
266 res_querydomain(const char *name,
267 		const char *domain,
268 		int class, int type,	/*!< class and type of query  */
269 		u_char *answer,		/*!< buffer to put answer  */
270 		int anslen)		/*!< size of answer  */
271 {
272 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
273 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
274 		return (-1);
275 	}
276 
277 	return (res_nquerydomain(&_res, name, domain,
278 				 class, type,
279 				 answer, anslen));
280 }
281 
282 u_int
283 res_randomid(void) {
284 	if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
285 		RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
286 		return (-1);
287 	}
288 
289 	return (res_nrandomid(&_res));
290 }
291 
292 const char *
293 hostalias(const char *name) {
294 	static char abuf[MAXDNAME];
295 
296 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
297 }
298 
299 #ifdef ultrix
300 int
301 local_hostname_length(const char *hostname) {
302 	int len_host, len_domain;
303 
304 	if (!*_res.defdname)
305 		res_init();
306 	len_host = strlen(hostname);
307 	len_domain = strlen(_res.defdname);
308 	if (len_host > len_domain &&
309 	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
310 	    hostname[len_host - len_domain - 1] == '.')
311 		return (len_host - len_domain - 1);
312 	return (0);
313 }
314 #endif /*ultrix*/
315 
316 #endif
317 
318 /*! \file */
319