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