xref: /titanic_44/usr/src/lib/libresolv2/common/resolv/res_data.c (revision 749f21d359d8fbd020c974a1a5227316221bfc9c)
1 /*
2  * Copyright 2003 by Sun Microsystems, Inc. All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1995-1999 by Internet Software Consortium.
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 #if defined(LIBC_SCCS) && !defined(lint)
26 static const char rcsid[] = "$Id: res_data.c,v 8.18 2000/12/23 08:14:58 vixie Exp $";
27 #endif /* LIBC_SCCS and not lint */
28 
29 #include "port_before.h"
30 
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/time.h>
35 
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <arpa/nameser.h>
39 
40 #include <ctype.h>
41 #include <netdb.h>
42 #include <resolv.h>
43 #include <res_update.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include "port_after.h"
50 #undef _res
51 
52 #ifdef	ORIGINAL_ISC_CODE
53 #else
54 #pragma weak	__fp_nquery	=	fp_nquery
55 #pragma weak	__fp_query	=	fp_query
56 #pragma weak	__p_query	=	p_query
57 #pragma weak	__hostalias	=	hostalias
58 #endif
59 
60 const char *_res_opcodes[] = {
61 	"QUERY",
62 	"IQUERY",
63 	"CQUERYM",
64 	"CQUERYU",	/* experimental */
65 	"NOTIFY",	/* experimental */
66 	"UPDATE",
67 	"6",
68 	"7",
69 	"8",
70 	"9",
71 	"10",
72 	"11",
73 	"12",
74 	"13",
75 	"ZONEINIT",
76 	"ZONEREF",
77 };
78 
79 #ifdef BIND_UPDATE
80 const char *_res_sectioncodes[] = {
81 	"ZONE",
82 	"PREREQUISITES",
83 	"UPDATE",
84 	"ADDITIONAL",
85 };
86 #endif
87 
88 #ifndef __BIND_NOSTATIC
89 struct __res_state _res
90 # if defined(__BIND_RES_TEXT)
91 	= { RES_TIMEOUT, }	/* Motorola, et al. */
92 # endif
93         ;
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_randomid();
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) == 0 && 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) == 0 && 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) == 0 && 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) == 0 && 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) == 0 && 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) == 0 && 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) == 0 && 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) == 0 && 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) == 0 && 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 const char *
283 hostalias(const char *name) {
284 	static char abuf[MAXDNAME];
285 
286 	return (res_hostalias(&_res, name, abuf, sizeof abuf));
287 }
288 
289 #ifdef ultrix
290 int
291 local_hostname_length(const char *hostname) {
292 	int len_host, len_domain;
293 
294 	if (!*_res.defdname)
295 		res_init();
296 	len_host = strlen(hostname);
297 	len_domain = strlen(_res.defdname);
298 	if (len_host > len_domain &&
299 	    !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
300 	    hostname[len_host - len_domain - 1] == '.')
301 		return (len_host - len_domain - 1);
302 	return (0);
303 }
304 #endif /*ultrix*/
305 
306 #endif
307