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