Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / libbind / dist / resolv / res_data.c
blob61c87d2b37b324ecb18571117c05719c5dc76db9
1 /* $NetBSD$ */
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium.
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.
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.
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 */
24 #include "port_before.h"
26 #include <sys/types.h>
27 #include <sys/param.h>
28 #include <sys/socket.h>
29 #include <sys/time.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
35 #include <ctype.h>
36 #include <netdb.h>
37 #include <resolv.h>
38 #include <res_update.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
44 #include "port_after.h"
46 const char *_res_opcodes[] = {
47 "QUERY",
48 "IQUERY",
49 "CQUERYM",
50 "CQUERYU", /*%< experimental */
51 "NOTIFY", /*%< experimental */
52 "UPDATE",
53 "6",
54 "7",
55 "8",
56 "9",
57 "10",
58 "11",
59 "12",
60 "13",
61 "ZONEINIT",
62 "ZONEREF",
65 #ifdef BIND_UPDATE
66 const char *_res_sectioncodes[] = {
67 "ZONE",
68 "PREREQUISITES",
69 "UPDATE",
70 "ADDITIONAL",
72 #endif
74 #undef _res
75 #ifndef __BIND_NOSTATIC
76 struct __res_state _res
77 # if defined(__BIND_RES_TEXT)
78 = { RES_TIMEOUT, } /*%< Motorola, et al. */
79 # endif
82 #if defined(DO_PTHREADS) || defined(__linux)
83 #define _res (*__res_state())
84 #endif
86 /* Proto. */
88 int res_ourserver_p(const res_state, const struct sockaddr_in *);
90 int
91 res_init(void) {
92 extern int __res_vinit(res_state, int);
95 * These three fields used to be statically initialized. This made
96 * it hard to use this code in a shared library. It is necessary,
97 * now that we're doing dynamic initialization here, that we preserve
98 * the old semantics: if an application modifies one of these three
99 * fields of _res before res_init() is called, res_init() will not
100 * alter them. Of course, if an application is setting them to
101 * _zero_ before calling res_init(), hoping to override what used
102 * to be the static default, we can't detect it and unexpected results
103 * will follow. Zero for any of these fields would make no sense,
104 * so one can safely assume that the applications were already getting
105 * unexpected results.
107 * _res.options is tricky since some apps were known to diddle the bits
108 * before res_init() was first called. We can't replicate that semantic
109 * with dynamic initialization (they may have turned bits off that are
110 * set in RES_DEFAULT). Our solution is to declare such applications
111 * "broken". They could fool us by setting RES_INIT but none do (yet).
113 if (!_res.retrans)
114 _res.retrans = RES_TIMEOUT;
115 if (!_res.retry)
116 _res.retry = 4;
117 if (!(_res.options & RES_INIT))
118 _res.options = RES_DEFAULT;
121 * This one used to initialize implicitly to zero, so unless the app
122 * has set it to something in particular, we can randomize it now.
124 if (!_res.id)
125 _res.id = res_nrandomid(&_res);
127 return (__res_vinit(&_res, 1));
130 void
131 p_query(const u_char *msg) {
132 fp_query(msg, stdout);
135 void
136 fp_query(const u_char *msg, FILE *file) {
137 fp_nquery(msg, PACKETSZ, file);
140 void
141 fp_nquery(const u_char *msg, int len, FILE *file) {
142 if ((_res.options & RES_INIT) == 0U && res_init() == -1)
143 return;
145 res_pquery(&_res, msg, len, file);
149 res_mkquery(int op, /*!< opcode of query */
150 const char *dname, /*!< domain name */
151 int class, int type, /*!< class and type of query */
152 const u_char *data, /*!< resource record data */
153 int datalen, /*!< length of data */
154 const u_char *newrr_in, /*!< new rr for modify or append */
155 u_char *buf, /*!< buffer to put query */
156 int buflen) /*!< size of buffer */
158 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
159 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
160 return (-1);
162 return (res_nmkquery(&_res, op, dname, class, type,
163 data, datalen,
164 newrr_in, buf, buflen));
168 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
169 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
170 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
171 return (-1);
174 return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
178 res_query(const char *name, /*!< domain name */
179 int class, int type, /*!< class and type of query */
180 u_char *answer, /*!< buffer to put answer */
181 int anslen) /*!< size of answer buffer */
183 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
184 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
185 return (-1);
187 return (res_nquery(&_res, name, class, type, answer, anslen));
190 void
191 res_send_setqhook(res_send_qhook hook) {
192 _res.qhook = hook;
195 void
196 res_send_setrhook(res_send_rhook hook) {
197 _res.rhook = hook;
201 res_isourserver(const struct sockaddr_in *inp) {
202 return (res_ourserver_p(&_res, inp));
206 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
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);
212 return (res_nsend(&_res, buf, buflen, ans, anssiz));
216 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
217 u_char *ans, int anssiz)
219 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
220 /* errno should have been set by res_init() in this case. */
221 return (-1);
224 return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
227 void
228 res_close(void) {
229 res_nclose(&_res);
233 res_update(ns_updrec *rrecp_in) {
234 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
235 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
236 return (-1);
239 return (res_nupdate(&_res, rrecp_in, NULL));
243 res_search(const char *name, /*!< domain name */
244 int class, int type, /*!< class and type of query */
245 u_char *answer, /*!< buffer to put answer */
246 int anslen) /*!< size of answer */
248 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
249 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
250 return (-1);
253 return (res_nsearch(&_res, name, class, type, answer, anslen));
257 res_querydomain(const char *name,
258 const char *domain,
259 int class, int type, /*!< class and type of query */
260 u_char *answer, /*!< buffer to put answer */
261 int anslen) /*!< size of answer */
263 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
264 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
265 return (-1);
268 return (res_nquerydomain(&_res, name, domain,
269 class, type,
270 answer, anslen));
273 u_int
274 res_randomid(void) {
275 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
276 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
277 return (-1);
280 return (res_nrandomid(&_res));
283 const char *
284 hostalias(const char *name) {
285 static char abuf[MAXDNAME];
287 return (res_hostalias(&_res, name, abuf, sizeof abuf));
290 #ifdef ultrix
292 local_hostname_length(const char *hostname) {
293 int len_host, len_domain;
295 if (!*_res.defdname)
296 res_init();
297 len_host = strlen(hostname);
298 len_domain = strlen(_res.defdname);
299 if (len_host > len_domain &&
300 !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
301 hostname[len_host - len_domain - 1] == '.')
302 return (len_host - len_domain - 1);
303 return (0);
305 #endif /*ultrix*/
307 #endif
309 /*! \file */