1 /* $NetBSD: res_data.c,v 1.14 2009/10/24 05:35:37 christos Exp $ */
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 #include <sys/cdefs.h>
21 #if defined(LIBC_SCCS) && !defined(lint)
23 static const char rcsid
[] = "Id: res_data.c,v 1.7 2008/12/11 09:59:00 marka Exp";
25 __RCSID("$NetBSD: res_data.c,v 1.14 2009/10/24 05:35:37 christos Exp $");
27 #endif /* LIBC_SCCS and not lint */
29 #include "port_before.h"
31 #include "namespace.h"
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
39 #include <arpa/nameser.h>
44 #include <res_update.h>
50 #include "port_after.h"
52 #include "res_private.h"
55 __weak_alias(res_init
,_res_init
)
56 __weak_alias(res_mkquery
,_res_mkquery
)
57 __weak_alias(res_query
,_res_query
)
58 __weak_alias(res_search
,_res_search
)
59 __weak_alias(res_send
,__res_send
)
60 __weak_alias(res_close
,__res_close
)
61 /* XXX: these leaked in the old bind8 libc */
62 __weak_alias(res_querydomain
,__res_querydomain
)
63 __weak_alias(res_send_setqhook
,__res_send_setqhook
)
64 __weak_alias(res_send_setrhook
,__res_send_setrhook
)
66 __weak_alias(p_query
,__p_query
)
67 __weak_alias(fp_query
,__fp_query
)
68 __weak_alias(fp_nquery
,__fp_nquery
)
69 __weak_alias(res_isourserver
,__res_isourserver
)
70 __weak_alias(res_opt
,_res_opt
)
71 __weak_alias(hostalias
,__hostalias
)
75 const char *_res_opcodes
[] = {
79 "CQUERYU", /*%< experimental */
80 "NOTIFY", /*%< experimental */
95 const char *_res_sectioncodes
[] = {
103 #ifndef __BIND_NOSTATIC
104 extern struct __res_state _nres
;
108 int res_ourserver_p(const res_state
, const struct sockaddr
*);
115 * Compatibility with program that were accessing _res directly
116 * to set options. We keep another struct res that is the same
117 * size as the original res structure, and then copy fields to
118 * it so that we achieve the same initialization
120 extern void *__res_get_old_state(void);
121 extern void __res_put_old_state(void *);
122 res_state ores
= __res_get_old_state();
124 if (ores
->options
!= 0)
125 _nres
.options
= ores
->options
;
126 if (ores
->retrans
!= 0)
127 _nres
.retrans
= ores
->retrans
;
128 if (ores
->retry
!= 0)
129 _nres
.retry
= ores
->retry
;
133 * These three fields used to be statically initialized. This made
134 * it hard to use this code in a shared library. It is necessary,
135 * now that we're doing dynamic initialization here, that we preserve
136 * the old semantics: if an application modifies one of these three
137 * fields of _res before res_init() is called, res_init() will not
138 * alter them. Of course, if an application is setting them to
139 * _zero_ before calling res_init(), hoping to override what used
140 * to be the static default, we can't detect it and unexpected results
141 * will follow. Zero for any of these fields would make no sense,
142 * so one can safely assume that the applications were already getting
143 * unexpected results.
145 * _nres.options is tricky since some apps were known to diddle the bits
146 * before res_init() was first called. We can't replicate that semantic
147 * with dynamic initialization (they may have turned bits off that are
148 * set in RES_DEFAULT). Our solution is to declare such applications
149 * "broken". They could fool us by setting RES_INIT but none do (yet).
152 _nres
.retrans
= RES_TIMEOUT
;
155 if (!(_nres
.options
& RES_INIT
))
156 _nres
.options
= RES_DEFAULT
;
159 * This one used to initialize implicitly to zero, so unless the app
160 * has set it to something in particular, we can randomize it now.
163 _nres
.id
= res_nrandomid(&_nres
);
165 rv
= __res_vinit(&_nres
, 1);
167 __res_put_old_state(&_nres
);
173 p_query(const u_char
*msg
) {
174 fp_query(msg
, stdout
);
178 fp_query(const u_char
*msg
, FILE *file
) {
179 fp_nquery(msg
, PACKETSZ
, file
);
183 fp_nquery(const u_char
*msg
, int len
, FILE *file
) {
184 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1)
187 res_pquery(&_nres
, msg
, len
, file
);
191 res_mkquery(int op
, /*!< opcode of query */
192 const char *dname
, /*!< domain name */
193 int class, int type
, /*!< class and type of query */
194 const u_char
*data
, /*!< resource record data */
195 int datalen
, /*!< length of data */
196 const u_char
*newrr_in
, /*!< new rr for modify or append */
197 u_char
*buf
, /*!< buffer to put query */
198 int buflen
) /*!< size of buffer */
200 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
201 RES_SET_H_ERRNO(&_nres
, NETDB_INTERNAL
);
204 return (res_nmkquery(&_nres
, op
, dname
, class, type
,
206 newrr_in
, buf
, buflen
));
211 res_mkupdate(ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
212 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
213 RES_SET_H_ERRNO(&_nres
, NETDB_INTERNAL
);
217 return (res_nmkupdate(&_nres
, rrecp_in
, buf
, buflen
));
222 res_query(const char *name
, /*!< domain name */
223 int class, int type
, /*!< class and type of query */
224 u_char
*answer
, /*!< buffer to put answer */
225 int anslen
) /*!< size of answer buffer */
227 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
228 RES_SET_H_ERRNO(&_nres
, NETDB_INTERNAL
);
231 return (res_nquery(&_nres
, name
, class, type
, answer
, anslen
));
235 res_send_setqhook(res_send_qhook hook
) {
240 res_send_setrhook(res_send_rhook hook
) {
245 res_isourserver(const struct sockaddr_in
*inp
) {
246 return (res_ourserver_p(&_nres
, (const struct sockaddr
*)(const void *)inp
));
250 res_send(const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
) {
251 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
252 /* errno should have been set by res_init() in this case. */
256 return (res_nsend(&_nres
, buf
, buflen
, ans
, anssiz
));
261 res_sendsigned(const u_char
*buf
, int buflen
, ns_tsig_key
*key
,
262 u_char
*ans
, int anssiz
)
264 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
265 /* errno should have been set by res_init() in this case. */
269 return (res_nsendsigned(&_nres
, buf
, buflen
, key
, ans
, anssiz
));
280 res_update(ns_updrec
*rrecp_in
) {
281 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
282 RES_SET_H_ERRNO(&_nres
, NETDB_INTERNAL
);
286 return (res_nupdate(&_nres
, rrecp_in
, NULL
));
291 res_search(const char *name
, /*!< domain name */
292 int class, int type
, /*!< class and type of query */
293 u_char
*answer
, /*!< buffer to put answer */
294 int anslen
) /*!< size of answer */
296 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
297 RES_SET_H_ERRNO(&_nres
, NETDB_INTERNAL
);
301 return (res_nsearch(&_nres
, name
, class, type
, answer
, anslen
));
305 res_querydomain(const char *name
,
307 int class, int type
, /*!< class and type of query */
308 u_char
*answer
, /*!< buffer to put answer */
309 int anslen
) /*!< size of answer */
311 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
312 RES_SET_H_ERRNO(&_nres
, NETDB_INTERNAL
);
316 return (res_nquerydomain(&_nres
, name
, domain
,
322 res_opt(int a
, u_char
*b
, int c
, int d
)
324 return res_nopt(&_nres
, a
, b
, c
, d
);
329 if ((_nres
.options
& RES_INIT
) == 0U && res_init() == -1) {
330 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
334 return (res_nrandomid(&_nres
));
338 hostalias(const char *name
) {
339 static char abuf
[MAXDNAME
];
341 return (res_hostalias(&_nres
, name
, abuf
, sizeof abuf
));
346 local_hostname_length(const char *hostname
) {
347 int len_host
, len_domain
;
349 if (!*_nres
.defdname
)
351 len_host
= strlen(hostname
);
352 len_domain
= strlen(_nres
.defdname
);
353 if (len_host
> len_domain
&&
354 !strcasecmp(hostname
+ len_host
- len_domain
, _nres
.defdname
) &&
355 hostname
[len_host
- len_domain
- 1] == '.')
356 return (len_host
- len_domain
- 1);