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