Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / bind / resolv / res_data.c
bloba9ecf8485e6fcf14e654f0537eb81369bd6c4cfb
1 /*
2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium.
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.
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.
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: res_data.c,v 1.1.2.2 2004/03/16 12:35:33 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include "port_before.h"
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/socket.h>
27 #include <sys/time.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
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>
42 #include "port_after.h"
43 #undef _res
45 const char *_res_opcodes[] = {
46 "QUERY",
47 "IQUERY",
48 "CQUERYM",
49 "CQUERYU", /* experimental */
50 "NOTIFY", /* experimental */
51 "UPDATE",
52 "6",
53 "7",
54 "8",
55 "9",
56 "10",
57 "11",
58 "12",
59 "13",
60 "ZONEINIT",
61 "ZONEREF",
64 #ifdef BIND_UPDATE
65 const char *_res_sectioncodes[] = {
66 "ZONE",
67 "PREREQUISITES",
68 "UPDATE",
69 "ADDITIONAL",
71 #endif
73 #ifndef __BIND_NOSTATIC
74 struct __res_state _res
75 # if defined(__BIND_RES_TEXT)
76 = { RES_TIMEOUT, } /* Motorola, et al. */
77 # endif
80 /* Proto. */
82 int res_ourserver_p(const res_state, const struct sockaddr_in *);
84 int
85 res_init(void) {
86 extern int __res_vinit(res_state, int);
89 * These three fields used to be statically initialized. This made
90 * it hard to use this code in a shared library. It is necessary,
91 * now that we're doing dynamic initialization here, that we preserve
92 * the old semantics: if an application modifies one of these three
93 * fields of _res before res_init() is called, res_init() will not
94 * alter them. Of course, if an application is setting them to
95 * _zero_ before calling res_init(), hoping to override what used
96 * to be the static default, we can't detect it and unexpected results
97 * will follow. Zero for any of these fields would make no sense,
98 * so one can safely assume that the applications were already getting
99 * unexpected results.
101 * _res.options is tricky since some apps were known to diddle the bits
102 * before res_init() was first called. We can't replicate that semantic
103 * with dynamic initialization (they may have turned bits off that are
104 * set in RES_DEFAULT). Our solution is to declare such applications
105 * "broken". They could fool us by setting RES_INIT but none do (yet).
107 if (!_res.retrans)
108 _res.retrans = RES_TIMEOUT;
109 if (!_res.retry)
110 _res.retry = 4;
111 if (!(_res.options & RES_INIT))
112 _res.options = RES_DEFAULT;
115 * This one used to initialize implicitly to zero, so unless the app
116 * has set it to something in particular, we can randomize it now.
118 if (!_res.id)
119 _res.id = res_randomid();
121 return (__res_vinit(&_res, 1));
124 void
125 p_query(const u_char *msg) {
126 fp_query(msg, stdout);
129 void
130 fp_query(const u_char *msg, FILE *file) {
131 fp_nquery(msg, PACKETSZ, file);
134 void
135 fp_nquery(const u_char *msg, int len, FILE *file) {
136 if ((_res.options & RES_INIT) == 0U && res_init() == -1)
137 return;
139 res_pquery(&_res, msg, len, file);
143 res_mkquery(int op, /* opcode of query */
144 const char *dname, /* domain name */
145 int class, int type, /* class and type of query */
146 const u_char *data, /* resource record data */
147 int datalen, /* length of data */
148 const u_char *newrr_in, /* new rr for modify or append */
149 u_char *buf, /* buffer to put query */
150 int buflen) /* size of buffer */
152 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
153 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
154 return (-1);
156 return (res_nmkquery(&_res, op, dname, class, type,
157 data, datalen,
158 newrr_in, buf, buflen));
162 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
163 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
164 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
165 return (-1);
168 return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
172 res_query(const char *name, /* domain name */
173 int class, int type, /* class and type of query */
174 u_char *answer, /* buffer to put answer */
175 int anslen) /* size of answer buffer */
177 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
178 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
179 return (-1);
181 return (res_nquery(&_res, name, class, type, answer, anslen));
184 void
185 res_send_setqhook(res_send_qhook hook) {
186 _res.qhook = hook;
189 void
190 res_send_setrhook(res_send_rhook hook) {
191 _res.rhook = hook;
195 res_isourserver(const struct sockaddr_in *inp) {
196 return (res_ourserver_p(&_res, inp));
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);
206 return (res_nsend(&_res, buf, buflen, ans, anssiz));
210 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
211 u_char *ans, int anssiz)
213 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
214 /* errno should have been set by res_init() in this case. */
215 return (-1);
218 return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
221 void
222 res_close(void) {
223 res_nclose(&_res);
227 res_update(ns_updrec *rrecp_in) {
228 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
229 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
230 return (-1);
233 return (res_nupdate(&_res, rrecp_in, NULL));
237 res_search(const char *name, /* domain name */
238 int class, int type, /* class and type of query */
239 u_char *answer, /* buffer to put answer */
240 int anslen) /* size of answer */
242 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
243 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
244 return (-1);
247 return (res_nsearch(&_res, name, class, type, answer, anslen));
251 res_querydomain(const char *name,
252 const char *domain,
253 int class, int type, /* class and type of query */
254 u_char *answer, /* buffer to put answer */
255 int anslen) /* size of answer */
257 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
258 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
259 return (-1);
262 return (res_nquerydomain(&_res, name, domain,
263 class, type,
264 answer, anslen));
267 const char *
268 hostalias(const char *name) {
269 static char abuf[MAXDNAME];
271 return (res_hostalias(&_res, name, abuf, sizeof abuf));
274 #ifdef ultrix
276 local_hostname_length(const char *hostname) {
277 int len_host, len_domain;
279 if (!*_res.defdname)
280 res_init();
281 len_host = strlen(hostname);
282 len_domain = strlen(_res.defdname);
283 if (len_host > len_domain &&
284 !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
285 hostname[len_host - len_domain - 1] == '.')
286 return (len_host - len_domain - 1);
287 return (0);
289 #endif /*ultrix*/
291 #endif