blob: 8c35fcd49da6cdfda905a1b0d5e4eea47b633ce3 [file] [log] [blame]
Bernie Innocenti55864192018-08-30 04:05:20 +09001/* $NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $ */
2
3/*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20#include <sys/cdefs.h>
21#if defined(LIBC_SCCS) && !defined(lint)
22#ifdef notdef
23static const char rcsid[] = "Id: res_data.c,v 1.1.206.2 2004/03/16 12:34:18 marka Exp";
24#else
25__RCSID("$NetBSD: res_data.c,v 1.8 2004/06/09 18:07:03 christos Exp $");
26#endif
27#endif /* LIBC_SCCS and not lint */
28
Bernie Innocenti55864192018-08-30 04:05:20 +090029#include <sys/param.h>
30#include <sys/socket.h>
31#include <sys/time.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090032#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090033
Bernie Innocenti55864192018-08-30 04:05:20 +090034#include <arpa/inet.h>
35#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090036#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090037
38#include <ctype.h>
39#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090040#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <unistd.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090044#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090045
46__LIBC_HIDDEN__
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090047const char* const _res_opcodes[] = {
48 "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
49 "NOTIFY", /* experimental */
50 "UPDATE", "6", "7", "8", "9", "10",
51 "11", "12", "13", "ZONEINIT", "ZONEREF",
Bernie Innocenti55864192018-08-30 04:05:20 +090052};
53
54#ifdef BIND_UPDATE
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090055const char* const _res_sectioncodes[] = {
56 "ZONE",
57 "PREREQUISITES",
58 "UPDATE",
59 "ADDITIONAL",
Bernie Innocenti55864192018-08-30 04:05:20 +090060};
61#endif
62
63#ifndef __BIND_NOSTATIC
64extern struct __res_state _nres;
65
66/* Proto. */
67
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090068int res_ourserver_p(const res_state, const struct sockaddr*);
Bernie Innocenti55864192018-08-30 04:05:20 +090069
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090070#define res_need_init() ((_nres.options & RES_INIT) == 0U)
Bernie Innocenti55864192018-08-30 04:05:20 +090071
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090072int res_init(void) {
73 int rv;
74 extern int __res_vinit(res_state, int);
Bernie Innocenti55864192018-08-30 04:05:20 +090075#ifdef COMPAT__RES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090076 /*
77 * Compatibility with program that were accessing _res directly
78 * to set options. We keep another struct res that is the same
79 * size as the original res structure, and then copy fields to
80 * it so that we achieve the same initialization
81 */
82 extern void* __res_get_old_state(void);
83 extern void __res_put_old_state(void*);
84 res_state ores = __res_get_old_state();
Bernie Innocenti55864192018-08-30 04:05:20 +090085
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090086 if (ores->options != 0) _nres.options = ores->options;
87 if (ores->retrans != 0) _nres.retrans = ores->retrans;
88 if (ores->retry != 0) _nres.retry = ores->retry;
Bernie Innocenti55864192018-08-30 04:05:20 +090089#endif
90
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090091 /*
92 * These three fields used to be statically initialized. This made
93 * it hard to use this code in a shared library. It is necessary,
94 * now that we're doing dynamic initialization here, that we preserve
95 * the old semantics: if an application modifies one of these three
96 * fields of _res before res_init() is called, res_init() will not
97 * alter them. Of course, if an application is setting them to
98 * _zero_ before calling res_init(), hoping to override what used
99 * to be the static default, we can't detect it and unexpected results
100 * will follow. Zero for any of these fields would make no sense,
101 * so one can safely assume that the applications were already getting
102 * unexpected results.
103 *
104 * _nres.options is tricky since some apps were known to diddle the bits
105 * before res_init() was first called. We can't replicate that semantic
106 * with dynamic initialization (they may have turned bits off that are
107 * set in RES_DEFAULT). Our solution is to declare such applications
108 * "broken". They could fool us by setting RES_INIT but none do (yet).
109 */
110 if (!_nres.retrans) _nres.retrans = RES_TIMEOUT;
111 if (!_nres.retry) _nres.retry = 4;
112 if (!(_nres.options & RES_INIT)) _nres.options = RES_DEFAULT;
Bernie Innocenti55864192018-08-30 04:05:20 +0900113
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900114 /*
115 * This one used to initialize implicitly to zero, so unless the app
116 * has set it to something in particular, we can randomize it now.
117 */
118 if (!_nres.id) _nres.id = res_randomid();
Bernie Innocenti55864192018-08-30 04:05:20 +0900119
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 rv = __res_vinit(&_nres, 1);
Bernie Innocenti55864192018-08-30 04:05:20 +0900121#ifdef COMPAT__RES
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900122 __res_put_old_state(&_nres);
Bernie Innocenti55864192018-08-30 04:05:20 +0900123#endif
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900124 return rv;
Bernie Innocenti55864192018-08-30 04:05:20 +0900125}
126
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127void p_query(const u_char* msg) {
128 fp_query(msg, stdout);
Bernie Innocenti55864192018-08-30 04:05:20 +0900129}
130
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900131void fp_query(const u_char* msg, FILE* file) {
132 fp_nquery(msg, PACKETSZ, file);
Bernie Innocenti55864192018-08-30 04:05:20 +0900133}
134
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135void fp_nquery(const u_char* msg, int len, FILE* file) {
136 if (res_need_init() && res_init() == -1) return;
Bernie Innocenti55864192018-08-30 04:05:20 +0900137
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900138 res_pquery(&_nres, msg, len, file);
Bernie Innocenti55864192018-08-30 04:05:20 +0900139}
140
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900141int res_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 */
Bernie Innocenti55864192018-08-30 04:05:20 +0900149{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900150 if (res_need_init() && res_init() == -1) {
151 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
152 return (-1);
153 }
154 return (res_nmkquery(&_nres, op, dname, class, type, data, datalen, newrr_in, buf, buflen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900155}
156
157#ifdef _LIBRESOLV
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900158int res_mkupdate(ns_updrec* rrecp_in, u_char* buf, int buflen) {
159 if (res_need_init() && res_init() == -1) {
160 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
161 return (-1);
162 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900163
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900164 return (res_nmkupdate(&_nres, rrecp_in, buf, buflen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900165}
166#endif
167
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900168int res_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 */
Bernie Innocenti55864192018-08-30 04:05:20 +0900172{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173 if (res_need_init() && res_init() == -1) {
174 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
175 return (-1);
176 }
177 return (res_nquery(&_nres, name, class, type, answer, anslen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900178}
179
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900180void res_send_setqhook(res_send_qhook hook) {
181 _nres.qhook = hook;
Bernie Innocenti55864192018-08-30 04:05:20 +0900182}
183
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900184void res_send_setrhook(res_send_rhook hook) {
185 _nres.rhook = hook;
Bernie Innocenti55864192018-08-30 04:05:20 +0900186}
187
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900188int res_isourserver(const struct sockaddr_in* inp) {
189 return (res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp));
Bernie Innocenti55864192018-08-30 04:05:20 +0900190}
191
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900192int res_send(const u_char* buf, int buflen, u_char* ans, int anssiz) {
193 if (res_need_init() && res_init() == -1) {
194 /* errno should have been set by res_init() in this case. */
195 return (-1);
196 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900197
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900198 return (res_nsend(&_nres, buf, buflen, ans, anssiz));
Bernie Innocenti55864192018-08-30 04:05:20 +0900199}
200
201#ifdef _LIBRESOLV
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900202int res_sendsigned(const u_char* buf, int buflen, ns_tsig_key* key, u_char* ans, int anssiz) {
203 if (res_need_init() && res_init() == -1) {
204 /* errno should have been set by res_init() in this case. */
205 return (-1);
206 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900207
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900208 return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz));
Bernie Innocenti55864192018-08-30 04:05:20 +0900209}
210#endif
211
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900212void res_close(void) {
213 res_nclose(&_nres);
Bernie Innocenti55864192018-08-30 04:05:20 +0900214}
215
216#ifdef _LIBRESOLV
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900217int res_update(ns_updrec* rrecp_in) {
218 if (res_need_init() && res_init() == -1) {
219 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
220 return (-1);
221 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900222
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900223 return (res_nupdate(&_nres, rrecp_in, NULL));
Bernie Innocenti55864192018-08-30 04:05:20 +0900224}
225#endif
226
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900227int res_search(const char* name, /* domain name */
228 int class, int type, /* class and type of query */
229 u_char* answer, /* buffer to put answer */
230 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900231{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900232 if (res_need_init() && res_init() == -1) {
233 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
234 return (-1);
235 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900236
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900237 return (res_nsearch(&_nres, name, class, type, answer, anslen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900238}
239
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900240int res_querydomain(const char* name, const char* domain, int class,
241 int type, /* class and type of query */
242 u_char* answer, /* buffer to put answer */
243 int anslen) /* size of answer */
Bernie Innocenti55864192018-08-30 04:05:20 +0900244{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900245 if (res_need_init() && res_init() == -1) {
246 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
247 return (-1);
248 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900249
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900250 return (res_nquerydomain(&_nres, name, domain, class, type, answer, anslen));
Bernie Innocenti55864192018-08-30 04:05:20 +0900251}
252
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900253int res_opt(int a, u_char* b, int c, int d) {
254 return res_nopt(&_nres, a, b, c, d);
Bernie Innocenti55864192018-08-30 04:05:20 +0900255}
256
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900257const char* hostalias(const char* name) {
258 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900259}
260
261#ifdef ultrix
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900262int local_hostname_length(const char* hostname) {
263 int len_host, len_domain;
Bernie Innocenti55864192018-08-30 04:05:20 +0900264
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900265 if (!*_nres.defdname) res_init();
266 len_host = strlen(hostname);
267 len_domain = strlen(_nres.defdname);
268 if (len_host > len_domain && !strcasecmp(hostname + len_host - len_domain, _nres.defdname) &&
269 hostname[len_host - len_domain - 1] == '.')
270 return (len_host - len_domain - 1);
271 return (0);
Bernie Innocenti55864192018-08-30 04:05:20 +0900272}
273#endif /*ultrix*/
274
275#endif