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 * $Id: res_data.c,v 1.5 2007/09/14 05:32:25 marka Exp $
18 */
19
20 #include "port_before.h"
21
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
25 #include <sys/time.h>
26
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
30
31 #include <ctype.h>
32 #include <netdb.h>
33 #include <resolv.h>
34 #include <res_update.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39
40 #include "port_after.h"
41 #ifndef _LIBC
42 #undef _res
43 #endif
44
45
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",
63 };
64
65 #ifdef BIND_UPDATE
66 const char *_res_sectioncodes[] = {
67 "ZONE",
68 "PREREQUISITES",
69 "UPDATE",
70 "ADDITIONAL",
71 };
72 #endif
73
74 #undef _res
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 #if defined(DO_PTHREADS) || defined(__linux)
85 #define _res (*__res_state())
86 #endif
87
88 /* Proto. */
89
90 int res_ourserver_p(const res_state, const struct sockaddr_in *);
91
92 int
res_init(void)93 res_init(void) {
94 extern int __res_vinit(res_state, int);
95
96 /*
97 * These three fields used to be statically initialized. This made
98 * it hard to use this code in a shared library. It is necessary,
99 * now that we're doing dynamic initialization here, that we preserve
100 * the old semantics: if an application modifies one of these three
101 * fields of _res before res_init() is called, res_init() will not
102 * alter them. Of course, if an application is setting them to
103 * _zero_ before calling res_init(), hoping to override what used
104 * to be the static default, we can't detect it and unexpected results
105 * will follow. Zero for any of these fields would make no sense,
106 * so one can safely assume that the applications were already getting
107 * unexpected results.
108 *
109 * _res.options is tricky since some apps were known to diddle the bits
110 * before res_init() was first called. We can't replicate that semantic
111 * with dynamic initialization (they may have turned bits off that are
112 * set in RES_DEFAULT). Our solution is to declare such applications
113 * "broken". They could fool us by setting RES_INIT but none do (yet).
114 */
115 if (!_res.retrans)
116 _res.retrans = RES_TIMEOUT;
117 if (!_res.retry)
118 #ifndef _LIBC
119 _res.retry = 4;
120 #else
121 _res.retry = RES_DFLRETRY;
122 #endif
123 if (!(_res.options & RES_INIT))
124 _res.options = RES_DEFAULT;
125
126 /*
127 * This one used to initialize implicitly to zero, so unless the app
128 * has set it to something in particular, we can randomize it now.
129 */
130 if (!_res.id)
131 _res.id = res_randomid();
132
133 return (__res_vinit(&_res, 1));
134 }
135
136 void
p_query(const u_char * msg)137 p_query(const u_char *msg) {
138 fp_query(msg, stdout);
139 }
140
141 void
fp_query(const u_char * msg,FILE * file)142 fp_query(const u_char *msg, FILE *file) {
143 fp_nquery(msg, PACKETSZ, file);
144 }
145
146 void
fp_nquery(const u_char * msg,int len,FILE * file)147 fp_nquery(const u_char *msg, int len, FILE *file) {
148 if ((_res.options & RES_INIT) == 0U && res_init() == -1)
149 return;
150
151 res_pquery(&_res, msg, len, file);
152 }
153
154 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)155 res_mkquery(int op, /*!< opcode of query */
156 const char *dname, /*!< domain name */
157 int class, int type, /*!< class and type of query */
158 const u_char *data, /*!< resource record data */
159 int datalen, /*!< length of data */
160 const u_char *newrr_in, /*!< new rr for modify or append */
161 u_char *buf, /*!< buffer to put query */
162 int buflen) /*!< size of buffer */
163 {
164 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
165 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
166 return (-1);
167 }
168 return (res_nmkquery(&_res, op, dname, class, type,
169 data, datalen,
170 newrr_in, buf, buflen));
171 }
172
173 int
res_mkupdate(ns_updrec * rrecp_in,u_char * buf,int buflen)174 res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
175 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
176 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
177 return (-1);
178 }
179
180 return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
181 }
182
183 int
res_query(const char * name,int class,int type,u_char * answer,int anslen)184 res_query(const char *name, /*!< domain name */
185 int class, int type, /*!< class and type of query */
186 u_char *answer, /*!< buffer to put answer */
187 int anslen) /*!< size of answer buffer */
188 {
189 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
190 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
191 return (-1);
192 }
193 return (res_nquery(&_res, name, class, type, answer, anslen));
194 }
195
196 #ifndef _LIBC
197 void
res_send_setqhook(res_send_qhook hook)198 res_send_setqhook(res_send_qhook hook) {
199 _res.qhook = hook;
200 }
201
202 void
res_send_setrhook(res_send_rhook hook)203 res_send_setrhook(res_send_rhook hook) {
204 _res.rhook = hook;
205 }
206 #endif
207
208 int
res_isourserver(const struct sockaddr_in * inp)209 res_isourserver(const struct sockaddr_in *inp) {
210 return (res_ourserver_p(&_res, inp));
211 }
212
213 int
res_send(const u_char * buf,int buflen,u_char * ans,int anssiz)214 res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
215 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
216 /* errno should have been set by res_init() in this case. */
217 return (-1);
218 }
219
220 return (res_nsend(&_res, buf, buflen, ans, anssiz));
221 }
222
223 #ifndef _LIBC
224 int
res_sendsigned(const u_char * buf,int buflen,ns_tsig_key * key,u_char * ans,int anssiz)225 res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
226 u_char *ans, int anssiz)
227 {
228 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
229 /* errno should have been set by res_init() in this case. */
230 return (-1);
231 }
232
233 return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
234 }
235 #endif
236
237 void
res_close(void)238 res_close(void) {
239 res_nclose(&_res);
240 }
241
242 int
res_update(ns_updrec * rrecp_in)243 res_update(ns_updrec *rrecp_in) {
244 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
245 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
246 return (-1);
247 }
248
249 return (res_nupdate(&_res, rrecp_in, NULL));
250 }
251
252 int
res_search(const char * name,int class,int type,u_char * answer,int anslen)253 res_search(const char *name, /*!< domain name */
254 int class, int type, /*!< class and type of query */
255 u_char *answer, /*!< buffer to put answer */
256 int anslen) /*!< size of answer */
257 {
258 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
259 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
260 return (-1);
261 }
262
263 return (res_nsearch(&_res, name, class, type, answer, anslen));
264 }
265
266 int
res_querydomain(const char * name,const char * domain,int class,int type,u_char * answer,int anslen)267 res_querydomain(const char *name,
268 const char *domain,
269 int class, int type, /*!< class and type of query */
270 u_char *answer, /*!< buffer to put answer */
271 int anslen) /*!< size of answer */
272 {
273 if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
274 RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
275 return (-1);
276 }
277
278 return (res_nquerydomain(&_res, name, domain,
279 class, type,
280 answer, anslen));
281 }
282
283 #ifdef _LIBC
284 int
res_opt(int n0,u_char * buf,int buflen,int anslen)285 res_opt(int n0, u_char *buf, int buflen, int anslen)
286 {
287 return (res_nopt(&_res, n0, buf, buflen, anslen));
288 }
289 #endif
290
291 const char *
hostalias(const char * name)292 hostalias(const char *name) {
293 static char abuf[MAXDNAME];
294
295 return (res_hostalias(&_res, name, abuf, sizeof abuf));
296 }
297
298 #ifdef ultrix
299 int
local_hostname_length(const char * hostname)300 local_hostname_length(const char *hostname) {
301 int len_host, len_domain;
302
303 if (!*_res.defdname)
304 res_init();
305 len_host = strlen(hostname);
306 len_domain = strlen(_res.defdname);
307 if (len_host > len_domain &&
308 !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
309 hostname[len_host - len_domain - 1] == '.')
310 return (len_host - len_domain - 1);
311 return (0);
312 }
313 #endif /*ultrix*/
314
315 #ifdef _LIBC
316 /*
317 * Weak aliases for applications that use certain private entry points,
318 * and fail to include <resolv.h>.
319 */
320 #undef res_init
321 __weak_reference(__res_init, res_init);
322 #undef p_query
323 __weak_reference(__p_query, p_query);
324 #undef res_mkquery
325 __weak_reference(__res_mkquery, res_mkquery);
326 #undef res_query
327 __weak_reference(__res_query, res_query);
328 #undef res_send
329 __weak_reference(__res_send, res_send);
330 #undef res_close
331 __weak_reference(__res_close, _res_close);
332 #undef res_search
333 __weak_reference(__res_search, res_search);
334 #undef res_querydomain
335 __weak_reference(__res_querydomain, res_querydomain);
336 #endif
337
338 #endif
339 /*! \file */
340