blob: 80aa908044df8c10beddd633bd7d83907c2e3e68 [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
Bernie Innocenti55864192018-08-30 04:05:20 +090020#include <sys/param.h>
21#include <sys/socket.h>
22#include <sys/time.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090023#include <sys/types.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090024
Bernie Innocenti55864192018-08-30 04:05:20 +090025#include <arpa/inet.h>
26#include <arpa/nameser.h>
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090027#include <netinet/in.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090028
29#include <ctype.h>
30#include <netdb.h>
Bernie Innocenti55864192018-08-30 04:05:20 +090031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <unistd.h>
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090035#include "res_private.h" // res_ourserver_p()
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090036#include "resolv_private.h"
Bernie Innocenti55864192018-08-30 04:05:20 +090037
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090038extern const char* const _res_opcodes[] = {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090039 "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */
40 "NOTIFY", /* experimental */
41 "UPDATE", "6", "7", "8", "9", "10",
42 "11", "12", "13", "ZONEINIT", "ZONEREF",
Bernie Innocenti55864192018-08-30 04:05:20 +090043};
44
Bernie Innocenti55864192018-08-30 04:05:20 +090045extern struct __res_state _nres;
46
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090047#define res_need_init() ((_nres.options & RES_INIT) == 0U)
Bernie Innocenti55864192018-08-30 04:05:20 +090048
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090049int res_init(void) {
50 int rv;
51 extern int __res_vinit(res_state, int);
Bernie Innocenti55864192018-08-30 04:05:20 +090052
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090053 /*
54 * These three fields used to be statically initialized. This made
55 * it hard to use this code in a shared library. It is necessary,
56 * now that we're doing dynamic initialization here, that we preserve
57 * the old semantics: if an application modifies one of these three
58 * fields of _res before res_init() is called, res_init() will not
59 * alter them. Of course, if an application is setting them to
60 * _zero_ before calling res_init(), hoping to override what used
61 * to be the static default, we can't detect it and unexpected results
62 * will follow. Zero for any of these fields would make no sense,
63 * so one can safely assume that the applications were already getting
64 * unexpected results.
65 *
66 * _nres.options is tricky since some apps were known to diddle the bits
67 * before res_init() was first called. We can't replicate that semantic
68 * with dynamic initialization (they may have turned bits off that are
69 * set in RES_DEFAULT). Our solution is to declare such applications
70 * "broken". They could fool us by setting RES_INIT but none do (yet).
71 */
72 if (!_nres.retrans) _nres.retrans = RES_TIMEOUT;
73 if (!_nres.retry) _nres.retry = 4;
74 if (!(_nres.options & RES_INIT)) _nres.options = RES_DEFAULT;
Bernie Innocenti55864192018-08-30 04:05:20 +090075
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090076 /*
77 * This one used to initialize implicitly to zero, so unless the app
78 * has set it to something in particular, we can randomize it now.
79 */
80 if (!_nres.id) _nres.id = res_randomid();
Bernie Innocenti55864192018-08-30 04:05:20 +090081
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090082 rv = __res_vinit(&_nres, 1);
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090083 return rv;
Bernie Innocenti55864192018-08-30 04:05:20 +090084}
85
Bernie Innocentia8cfe092018-09-13 16:00:42 +090086static void fp_nquery(const u_char* msg, int len, FILE* file) {
87 if (res_need_init() && res_init() == -1) return;
88 res_pquery(&_nres, msg, len, file);
Bernie Innocenti55864192018-08-30 04:05:20 +090089}
90
Bernie Innocentia8cfe092018-09-13 16:00:42 +090091static void fp_query(const u_char* msg, FILE* file) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +090092 fp_nquery(msg, PACKETSZ, file);
Bernie Innocenti55864192018-08-30 04:05:20 +090093}
94
Bernie Innocentia8cfe092018-09-13 16:00:42 +090095void p_query(const u_char* msg) {
96 fp_query(msg, stdout);
Bernie Innocenti55864192018-08-30 04:05:20 +090097}
98
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +090099int res_mkquery(int op, // opcode of query
100 const char* dname, // domain name
101 int cl, int type, // class and type of query
102 const u_char* data, // resource record data
103 int datalen, // length of data
104 const u_char* newrr_in, // new rr for modify or append
105 u_char* buf, // buffer to put query
106 int buflen) // size of buffer
Bernie Innocenti55864192018-08-30 04:05:20 +0900107{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900108 if (res_need_init() && res_init() == -1) {
109 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900110 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900111 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900112 return res_nmkquery(&_nres, op, dname, cl, type, data, datalen, newrr_in, buf, buflen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900113}
114
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900115int res_query(const char* name, // domain name
116 int cl, int type, // class and type of query
117 u_char* answer, // buffer to put answer
118 int anslen) // size of answer buffer
Bernie Innocenti55864192018-08-30 04:05:20 +0900119{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900120 if (res_need_init() && res_init() == -1) {
121 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900122 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900123 }
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900124 return res_nquery(&_nres, name, cl, type, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900125}
126
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900127void res_send_setqhook(res_send_qhook hook) {
128 _nres.qhook = hook;
Bernie Innocenti55864192018-08-30 04:05:20 +0900129}
130
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900131void res_send_setrhook(res_send_rhook hook) {
132 _nres.rhook = hook;
Bernie Innocenti55864192018-08-30 04:05:20 +0900133}
134
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900135int res_isourserver(const struct sockaddr_in* inp) {
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900136 return res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp);
Bernie Innocenti55864192018-08-30 04:05:20 +0900137}
138
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900139int res_send(const u_char* buf, int buflen, u_char* ans, int anssiz) {
140 if (res_need_init() && res_init() == -1) {
141 /* errno should have been set by res_init() in this case. */
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900142 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900143 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900144
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900145 return res_nsend(&_nres, buf, buflen, ans, anssiz);
Bernie Innocenti55864192018-08-30 04:05:20 +0900146}
147
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900148void res_close(void) {
149 res_nclose(&_nres);
Bernie Innocenti55864192018-08-30 04:05:20 +0900150}
151
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900152int res_search(const char* name, // domain name
153 int cl, int type, // class and type of query
154 u_char* answer, // buffer to put answer
155 int anslen) // size of answer
Bernie Innocenti55864192018-08-30 04:05:20 +0900156{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900157 if (res_need_init() && res_init() == -1) {
158 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900159 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900160 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900161
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900162 return res_nsearch(&_nres, name, cl, type, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900163}
164
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900165int res_querydomain(const char* name, const char* domain,
166 int cl, int type, // class and type of query
167 u_char* answer, // buffer to put answer
168 int anslen) // size of answer
Bernie Innocenti55864192018-08-30 04:05:20 +0900169{
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900170 if (res_need_init() && res_init() == -1) {
171 RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL);
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900172 return -1;
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900173 }
Bernie Innocenti55864192018-08-30 04:05:20 +0900174
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900175 return res_nquerydomain(&_nres, name, domain, cl, type, answer, anslen);
Bernie Innocenti55864192018-08-30 04:05:20 +0900176}
177
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900178int res_opt(int a, u_char* b, int c, int d) {
179 return res_nopt(&_nres, a, b, c, d);
Bernie Innocenti55864192018-08-30 04:05:20 +0900180}
181
Bernie Innocenti1f4a9fd2018-09-07 21:10:25 +0900182const char* hostalias(const char* /*name*/) {
Bernie Innocentif12d5bb2018-08-31 14:09:46 +0900183 return NULL;
Bernie Innocenti55864192018-08-30 04:05:20 +0900184}