1 | #include <machine/rtems-bsd-user-space.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.7 2008/12/11 09:59:00 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 <rtems/bsd/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 | return (__res_vinit(&_res, 1)); |
---|
113 | } |
---|
114 | |
---|
115 | void |
---|
116 | p_query(const u_char *msg) { |
---|
117 | fp_query(msg, stdout); |
---|
118 | } |
---|
119 | |
---|
120 | void |
---|
121 | fp_query(const u_char *msg, FILE *file) { |
---|
122 | fp_nquery(msg, PACKETSZ, file); |
---|
123 | } |
---|
124 | |
---|
125 | void |
---|
126 | fp_nquery(const u_char *msg, int len, FILE *file) { |
---|
127 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) |
---|
128 | return; |
---|
129 | |
---|
130 | res_pquery(&_res, msg, len, file); |
---|
131 | } |
---|
132 | |
---|
133 | int |
---|
134 | res_mkquery(int op, /*!< opcode of query */ |
---|
135 | const char *dname, /*!< domain name */ |
---|
136 | int class, int type, /*!< class and type of query */ |
---|
137 | const u_char *data, /*!< resource record data */ |
---|
138 | int datalen, /*!< length of data */ |
---|
139 | const u_char *newrr_in, /*!< new rr for modify or append */ |
---|
140 | u_char *buf, /*!< buffer to put query */ |
---|
141 | int buflen) /*!< size of buffer */ |
---|
142 | { |
---|
143 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
144 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
145 | return (-1); |
---|
146 | } |
---|
147 | return (res_nmkquery(&_res, op, dname, class, type, |
---|
148 | data, datalen, |
---|
149 | newrr_in, buf, buflen)); |
---|
150 | } |
---|
151 | |
---|
152 | int |
---|
153 | res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) { |
---|
154 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
155 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
156 | return (-1); |
---|
157 | } |
---|
158 | |
---|
159 | return (res_nmkupdate(&_res, rrecp_in, buf, buflen)); |
---|
160 | } |
---|
161 | |
---|
162 | int |
---|
163 | res_query(const char *name, /*!< domain name */ |
---|
164 | int class, int type, /*!< class and type of query */ |
---|
165 | u_char *answer, /*!< buffer to put answer */ |
---|
166 | int anslen) /*!< size of answer buffer */ |
---|
167 | { |
---|
168 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
169 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
170 | return (-1); |
---|
171 | } |
---|
172 | return (res_nquery(&_res, name, class, type, answer, anslen)); |
---|
173 | } |
---|
174 | |
---|
175 | #ifndef _LIBC |
---|
176 | void |
---|
177 | res_send_setqhook(res_send_qhook hook) { |
---|
178 | _res.qhook = hook; |
---|
179 | } |
---|
180 | |
---|
181 | void |
---|
182 | res_send_setrhook(res_send_rhook hook) { |
---|
183 | _res.rhook = hook; |
---|
184 | } |
---|
185 | #endif |
---|
186 | |
---|
187 | int |
---|
188 | res_isourserver(const struct sockaddr_in *inp) { |
---|
189 | return (res_ourserver_p(&_res, inp)); |
---|
190 | } |
---|
191 | |
---|
192 | int |
---|
193 | res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) { |
---|
194 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
195 | /* errno should have been set by res_init() in this case. */ |
---|
196 | return (-1); |
---|
197 | } |
---|
198 | |
---|
199 | return (res_nsend(&_res, buf, buflen, ans, anssiz)); |
---|
200 | } |
---|
201 | |
---|
202 | #ifndef _LIBC |
---|
203 | int |
---|
204 | res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key, |
---|
205 | u_char *ans, int anssiz) |
---|
206 | { |
---|
207 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
208 | /* errno should have been set by res_init() in this case. */ |
---|
209 | return (-1); |
---|
210 | } |
---|
211 | |
---|
212 | return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz)); |
---|
213 | } |
---|
214 | #endif |
---|
215 | |
---|
216 | void |
---|
217 | res_close(void) { |
---|
218 | res_nclose(&_res); |
---|
219 | } |
---|
220 | |
---|
221 | int |
---|
222 | res_update(ns_updrec *rrecp_in) { |
---|
223 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
224 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
225 | return (-1); |
---|
226 | } |
---|
227 | |
---|
228 | return (res_nupdate(&_res, rrecp_in, NULL)); |
---|
229 | } |
---|
230 | |
---|
231 | int |
---|
232 | res_search(const char *name, /*!< domain name */ |
---|
233 | int class, int type, /*!< class and type of query */ |
---|
234 | u_char *answer, /*!< buffer to put answer */ |
---|
235 | int anslen) /*!< size of answer */ |
---|
236 | { |
---|
237 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
238 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
239 | return (-1); |
---|
240 | } |
---|
241 | |
---|
242 | return (res_nsearch(&_res, name, class, type, answer, anslen)); |
---|
243 | } |
---|
244 | |
---|
245 | int |
---|
246 | res_querydomain(const char *name, |
---|
247 | const char *domain, |
---|
248 | int class, int type, /*!< class and type of query */ |
---|
249 | u_char *answer, /*!< buffer to put answer */ |
---|
250 | int anslen) /*!< size of answer */ |
---|
251 | { |
---|
252 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
253 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
254 | return (-1); |
---|
255 | } |
---|
256 | |
---|
257 | return (res_nquerydomain(&_res, name, domain, |
---|
258 | class, type, |
---|
259 | answer, anslen)); |
---|
260 | } |
---|
261 | |
---|
262 | u_int |
---|
263 | res_randomid(void) { |
---|
264 | if ((_res.options & RES_INIT) == 0U && res_init() == -1) { |
---|
265 | RES_SET_H_ERRNO(&_res, NETDB_INTERNAL); |
---|
266 | return (-1); |
---|
267 | } |
---|
268 | |
---|
269 | return (res_nrandomid(&_res)); |
---|
270 | } |
---|
271 | |
---|
272 | int |
---|
273 | res_opt(int n0, u_char *buf, int buflen, int anslen) |
---|
274 | { |
---|
275 | return (res_nopt(&_res, n0, buf, buflen, anslen)); |
---|
276 | } |
---|
277 | |
---|
278 | const char * |
---|
279 | hostalias(const char *name) { |
---|
280 | static char abuf[MAXDNAME]; |
---|
281 | |
---|
282 | return (res_hostalias(&_res, name, abuf, sizeof abuf)); |
---|
283 | } |
---|
284 | |
---|
285 | #ifdef ultrix |
---|
286 | int |
---|
287 | local_hostname_length(const char *hostname) { |
---|
288 | int len_host, len_domain; |
---|
289 | |
---|
290 | if (!*_res.defdname) |
---|
291 | res_init(); |
---|
292 | len_host = strlen(hostname); |
---|
293 | len_domain = strlen(_res.defdname); |
---|
294 | if (len_host > len_domain && |
---|
295 | !strcasecmp(hostname + len_host - len_domain, _res.defdname) && |
---|
296 | hostname[len_host - len_domain - 1] == '.') |
---|
297 | return (len_host - len_domain - 1); |
---|
298 | return (0); |
---|
299 | } |
---|
300 | #endif /*ultrix*/ |
---|
301 | |
---|
302 | /* |
---|
303 | * Weak aliases for applications that use certain private entry points, |
---|
304 | * and fail to include <resolv.h>. |
---|
305 | */ |
---|
306 | #undef res_init |
---|
307 | __weak_reference(__res_init, res_init); |
---|
308 | #undef p_query |
---|
309 | __weak_reference(__p_query, p_query); |
---|
310 | #undef res_mkquery |
---|
311 | __weak_reference(__res_mkquery, res_mkquery); |
---|
312 | #undef res_query |
---|
313 | __weak_reference(__res_query, res_query); |
---|
314 | #undef res_send |
---|
315 | __weak_reference(__res_send, res_send); |
---|
316 | #undef res_close |
---|
317 | __weak_reference(__res_close, _res_close); |
---|
318 | #undef res_search |
---|
319 | __weak_reference(__res_search, res_search); |
---|
320 | #undef res_querydomain |
---|
321 | __weak_reference(__res_querydomain, res_querydomain); |
---|
322 | |
---|
323 | #endif |
---|
324 | |
---|
325 | /*! \file */ |
---|