2 * Copyright (c) 1995-1999 by Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid
[] = "$BINDId: res_data.c,v 8.17 1999/10/13 17:11:31 vixie Exp $";
20 #endif /* LIBC_SCCS and not lint */
22 #include <sys/types.h>
23 #include <sys/param.h>
24 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
35 #include <res_update.h>
44 const char *_res_opcodes
[] = {
48 "CQUERYU", /* experimental */
49 "NOTIFY", /* experimental */
64 const char *_res_sectioncodes
[] = {
72 #ifndef __BIND_NOSTATIC
74 extern struct __res_state _res
;
76 /* The definition has been moved to res_libc.c. */
77 struct __res_state _res
78 # if defined(__BIND_RES_TEXT)
79 = { RES_TIMEOUT
, } /* Motorola, et al. */
86 int res_ourserver_p(const res_state
, const struct sockaddr_in
*);
87 void res_pquery(const res_state
, const u_char
*, int, FILE *);
91 /* Moved to res_libc.c since res_init() should go into libc.so but the
92 rest of this file not. */
95 extern int __res_vinit(res_state
, int);
98 * These three fields used to be statically initialized. This made
99 * it hard to use this code in a shared library. It is necessary,
100 * now that we're doing dynamic initialization here, that we preserve
101 * the old semantics: if an application modifies one of these three
102 * fields of _res before res_init() is called, res_init() will not
103 * alter them. Of course, if an application is setting them to
104 * _zero_ before calling res_init(), hoping to override what used
105 * to be the static default, we can't detect it and unexpected results
106 * will follow. Zero for any of these fields would make no sense,
107 * so one can safely assume that the applications were already getting
108 * unexpected results.
110 * _res.options is tricky since some apps were known to diddle the bits
111 * before res_init() was first called. We can't replicate that semantic
112 * with dynamic initialization (they may have turned bits off that are
113 * set in RES_DEFAULT). Our solution is to declare such applications
114 * "broken". They could fool us by setting RES_INIT but none do (yet).
117 _res
.retrans
= RES_TIMEOUT
;
120 if (!(_res
.options
& RES_INIT
))
121 _res
.options
= RES_DEFAULT
;
124 * This one used to initialize implicitly to zero, so unless the app
125 * has set it to something in particular, we can randomize it now.
128 _res
.id
= res_randomid();
130 return (__res_vinit(&_res
, 1));
135 p_query(const u_char
*msg
) {
136 fp_query(msg
, stdout
);
140 fp_query(const u_char
*msg
, FILE *file
) {
141 fp_nquery(msg
, PACKETSZ
, file
);
145 fp_nquery(const u_char
*msg
, int len
, FILE *file
) {
146 if ((_res
.options
& RES_INIT
) == 0 && __res_ninit(&_res
) == -1)
149 res_pquery(&_res
, msg
, len
, file
);
153 res_mkquery(int op
, /* opcode of query */
154 const char *dname
, /* domain name */
155 int class, int type
, /* class and type of query */
156 const u_char
*data
, /* resource record data */
157 int datalen
, /* length of data */
158 const u_char
*newrr_in
, /* new rr for modify or append */
159 u_char
*buf
, /* buffer to put query */
160 int buflen
) /* size of buffer */
162 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
163 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
166 return (res_nmkquery(&_res
, op
, dname
, class, type
,
168 newrr_in
, buf
, buflen
));
173 res_mkupdate(ns_updrec
*rrecp_in
, u_char
*buf
, int buflen
) {
174 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
175 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
179 return (res_nmkupdate(&_res
, rrecp_in
, buf
, buflen
));
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 */
189 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
190 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
193 return (res_nquery(&_res
, name
, class, type
, answer
, anslen
));
197 res_send_setqhook(res_send_qhook hook
) {
202 res_send_setrhook(res_send_rhook hook
) {
207 res_isourserver(const struct sockaddr_in
*inp
) {
208 return (res_ourserver_p(&_res
, (const struct sockaddr_in6
*) inp
));
212 res_send(const u_char
*buf
, int buflen
, u_char
*ans
, int anssiz
) {
213 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
214 /* errno should have been set by res_init() in this case. */
218 return (res_nsend(&_res
, buf
, buflen
, ans
, anssiz
));
223 res_sendsigned(const u_char
*buf
, int buflen
, ns_tsig_key
*key
,
224 u_char
*ans
, int anssiz
)
226 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
227 /* errno should have been set by res_init() in this case. */
231 return (res_nsendsigned(&_res
, buf
, buflen
, key
, ans
, anssiz
));
239 * Some stupid programs out there call res_close() before res_init().
240 * Since _res._vcsock isn't explicitly initialized, these means that
241 * we could do a close(0), which might lead to some security problems.
242 * Therefore we check if res_init() was called before by looking at
243 * the RES_INIT bit in _res.options. If it hasn't been set we bail out
245 if ((_res
.options
& RES_INIT
) == 0)
253 res_update(ns_updrec
*rrecp_in
) {
254 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
255 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
259 return (res_nupdate(&_res
, rrecp_in
, NULL
));
264 res_search(const char *name
, /* domain name */
265 int class, int type
, /* class and type of query */
266 u_char
*answer
, /* buffer to put answer */
267 int anslen
) /* size of answer */
269 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
270 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
274 return (res_nsearch(&_res
, name
, class, type
, answer
, anslen
));
278 res_querydomain(const char *name
,
280 int class, int type
, /* class and type of query */
281 u_char
*answer
, /* buffer to put answer */
282 int anslen
) /* size of answer */
284 if ((_res
.options
& RES_INIT
) == 0 && res_init() == -1) {
285 RES_SET_H_ERRNO(&_res
, NETDB_INTERNAL
);
289 return (res_nquerydomain(&_res
, name
, domain
,
295 hostalias(const char *name
) {
296 static char abuf
[MAXDNAME
];
298 return (res_hostalias(&_res
, name
, abuf
, sizeof abuf
));
303 local_hostname_length(const char *hostname
) {
304 int len_host
, len_domain
;
308 len_host
= strlen(hostname
);
309 len_domain
= strlen(_res
.defdname
);
310 if (len_host
> len_domain
&&
311 !strcasecmp(hostname
+ len_host
- len_domain
, _res
.defdname
) &&
312 hostname
[len_host
- len_domain
- 1] == '.')
313 return (len_host
- len_domain
- 1);
321 #include <shlib-compat.h>
323 #if SHLIB_COMPAT(libresolv, GLIBC_2_0, GLIBC_2_2)
326 # undef res_querydomain
328 weak_alias (__res_mkquery
, res_mkquery
);
329 weak_alias (__res_query
, res_query
);
330 weak_alias (__res_querydomain
, res_querydomain
);
331 weak_alias (__res_search
, res_search
);