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