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