Update.
[glibc.git] / resolv / res_data.c
blob7488ba777204a6eeb943fe7012544d6ef74d6e16
1 /*
2 * Copyright (c) 1995-1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15 * SOFTWARE.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$BINDId: res_data.c,v 8.17 1999/10/13 17:11:31 vixie Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
31 #include <ctype.h>
32 #include <netdb.h>
33 #include <resolv.h>
34 #ifdef BIND_UPDATE
35 #include <res_update.h>
36 #endif
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
42 const char *_res_opcodes[] = {
43 "QUERY",
44 "IQUERY",
45 "CQUERYM",
46 "CQUERYU", /* experimental */
47 "NOTIFY", /* experimental */
48 "UPDATE",
49 "6",
50 "7",
51 "8",
52 "9",
53 "10",
54 "11",
55 "12",
56 "13",
57 "ZONEINIT",
58 "ZONEREF",
61 #ifdef BIND_UPDATE
62 const char *_res_sectioncodes[] = {
63 "ZONE",
64 "PREREQUISITES",
65 "UPDATE",
66 "ADDITIONAL",
68 #endif
70 #ifndef __BIND_NOSTATIC
71 #ifdef _LIBC
72 /* The definition has been moved to res_libc.c. */
73 #else
74 #undef _res
75 struct __res_state _res
76 # if defined(__BIND_RES_TEXT)
77 = { RES_TIMEOUT, } /* Motorola, et al. */
78 # endif
80 #endif
82 /* Proto. */
83 #ifndef _LIBC
84 int res_ourserver_p(const res_state, const struct sockaddr_in *);
85 void res_pquery(const res_state, const u_char *, int, FILE *);
86 #endif
88 #ifndef _LIBC
89 /* Moved to res_libc.c since res_init() should go into libc.so but the
90 rest of this file not. */
91 int
92 res_init(void) {
93 extern int __res_vinit(res_state, int);
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.
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).
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;
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.
125 if (!_res.id)
126 _res.id = res_randomid();
128 return (__res_vinit(&_res, 1));
130 #endif
132 void
133 p_query(const u_char *msg) {
134 fp_query(msg, stdout);
137 void
138 fp_query(const u_char *msg, FILE *file) {
139 fp_nquery(msg, PACKETSZ, file);
142 void
143 fp_nquery(const u_char *msg, int len, FILE *file) {
144 if ((_res.options & RES_INIT) == 0 && __res_ninit(&_res) == -1)
145 return;
147 res_pquery(&_res, msg, len, file);
151 res_mkquery(int op, /* opcode of query */
152 const char *dname, /* domain name */
153 int class, int type, /* class and type of query */
154 const u_char *data, /* resource record data */
155 int datalen, /* length of data */
156 const u_char *newrr_in, /* new rr for modify or append */
157 u_char *buf, /* buffer to put query */
158 int buflen) /* size of buffer */
160 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
161 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
162 return (-1);
164 return (res_nmkquery(&_res, op, dname, class, type,
165 data, datalen,
166 newrr_in, buf, buflen));
169 #ifdef BIND_UPDATE
171 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
172 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
173 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
174 return (-1);
177 return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
179 #endif
182 res_query(const char *name, /* domain name */
183 int class, int type, /* class and type of query */
184 u_char *answer, /* buffer to put answer */
185 int anslen) /* size of answer buffer */
187 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
188 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
189 return (-1);
191 return (res_nquery(&_res, name, class, type, answer, anslen));
194 void
195 res_send_setqhook(res_send_qhook hook) {
196 _res.qhook = hook;
199 void
200 res_send_setrhook(res_send_rhook hook) {
201 _res.rhook = hook;
205 res_isourserver(const struct sockaddr_in *inp) {
206 return (res_ourserver_p(&_res, (const struct sockaddr_in6 *) inp));
210 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
211 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
212 /* errno should have been set by res_init() in this case. */
213 return (-1);
216 return (res_nsend(&_res, buf, buflen, ans, anssiz));
219 #ifndef _LIBC
221 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
222 u_char *ans, int anssiz)
224 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
225 /* errno should have been set by res_init() in this case. */
226 return (-1);
229 return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
231 #endif
233 void
234 res_close(void) {
235 #ifdef _LIBC
237 * Some stupid programs out there call res_close() before res_init().
238 * Since _res._vcsock isn't explicitly initialized, these means that
239 * we could do a close(0), which might lead to some security problems.
240 * Therefore we check if res_init() was called before by looking at
241 * the RES_INIT bit in _res.options. If it hasn't been set we bail out
242 * early. */
243 if ((_res.options & RES_INIT) == 0)
244 return;
245 #endif
246 res_nclose(&_res);
249 #ifdef BIND_UPDATE
251 res_update(ns_updrec *rrecp_in) {
252 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
253 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
254 return (-1);
257 return (res_nupdate(&_res, rrecp_in, NULL));
259 #endif
262 res_search(const char *name, /* domain name */
263 int class, int type, /* class and type of query */
264 u_char *answer, /* buffer to put answer */
265 int anslen) /* size of answer */
267 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
268 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
269 return (-1);
272 return (res_nsearch(&_res, name, class, type, answer, anslen));
276 res_querydomain(const char *name,
277 const char *domain,
278 int class, int type, /* class and type of query */
279 u_char *answer, /* buffer to put answer */
280 int anslen) /* size of answer */
282 if ((_res.options & RES_INIT) == 0 && res_init() == -1) {
283 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
284 return (-1);
287 return (res_nquerydomain(&_res, name, domain,
288 class, type,
289 answer, anslen));
292 const char *
293 hostalias(const char *name) {
294 static char abuf[MAXDNAME];
296 return (res_hostalias(&_res, name, abuf, sizeof abuf));
299 #ifdef ultrix
301 local_hostname_length(const char *hostname) {
302 int len_host, len_domain;
304 if (!*_res.defdname)
305 res_init();
306 len_host = strlen(hostname);
307 len_domain = strlen(_res.defdname);
308 if (len_host > len_domain &&
309 !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
310 hostname[len_host - len_domain - 1] == '.')
311 return (len_host - len_domain - 1);
312 return (0);
314 #endif /*ultrix*/
316 #endif
319 #include <shlib-compat.h>
321 #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2)
322 # undef res_mkquery
323 # undef res_query
324 # undef res_querydomain
325 # undef res_search
326 weak_alias (__res_mkquery, res_mkquery);
327 weak_alias (__res_query, res_query);
328 weak_alias (__res_querydomain, res_querydomain);
329 weak_alias (__res_search, res_search);
330 #endif