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