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