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