Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 1 | /* $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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 20 | #include <sys/param.h> |
| 21 | #include <sys/socket.h> |
| 22 | #include <sys/time.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 23 | #include <sys/types.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 24 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 25 | #include <arpa/inet.h> |
| 26 | #include <arpa/nameser.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 27 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 28 | |
| 29 | #include <ctype.h> |
| 30 | #include <netdb.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 35 | #include "res_private.h" // res_ourserver_p() |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 36 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 37 | |
| 38 | __LIBC_HIDDEN__ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 39 | extern const char* const _res_opcodes[] = { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 40 | "QUERY", "IQUERY", "CQUERYM", "CQUERYU", /* experimental */ |
| 41 | "NOTIFY", /* experimental */ |
| 42 | "UPDATE", "6", "7", "8", "9", "10", |
| 43 | "11", "12", "13", "ZONEINIT", "ZONEREF", |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 44 | }; |
| 45 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 46 | extern struct __res_state _nres; |
| 47 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 48 | #define res_need_init() ((_nres.options & RES_INIT) == 0U) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 49 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 50 | int res_init(void) { |
| 51 | int rv; |
| 52 | extern int __res_vinit(res_state, int); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 53 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 54 | /* |
| 55 | * These three fields used to be statically initialized. This made |
| 56 | * it hard to use this code in a shared library. It is necessary, |
| 57 | * now that we're doing dynamic initialization here, that we preserve |
| 58 | * the old semantics: if an application modifies one of these three |
| 59 | * fields of _res before res_init() is called, res_init() will not |
| 60 | * alter them. Of course, if an application is setting them to |
| 61 | * _zero_ before calling res_init(), hoping to override what used |
| 62 | * to be the static default, we can't detect it and unexpected results |
| 63 | * will follow. Zero for any of these fields would make no sense, |
| 64 | * so one can safely assume that the applications were already getting |
| 65 | * unexpected results. |
| 66 | * |
| 67 | * _nres.options is tricky since some apps were known to diddle the bits |
| 68 | * before res_init() was first called. We can't replicate that semantic |
| 69 | * with dynamic initialization (they may have turned bits off that are |
| 70 | * set in RES_DEFAULT). Our solution is to declare such applications |
| 71 | * "broken". They could fool us by setting RES_INIT but none do (yet). |
| 72 | */ |
| 73 | if (!_nres.retrans) _nres.retrans = RES_TIMEOUT; |
| 74 | if (!_nres.retry) _nres.retry = 4; |
| 75 | if (!(_nres.options & RES_INIT)) _nres.options = RES_DEFAULT; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 76 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 77 | /* |
| 78 | * This one used to initialize implicitly to zero, so unless the app |
| 79 | * has set it to something in particular, we can randomize it now. |
| 80 | */ |
| 81 | if (!_nres.id) _nres.id = res_randomid(); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 82 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 83 | rv = __res_vinit(&_nres, 1); |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 84 | return rv; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 85 | } |
| 86 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 87 | void p_query(const u_char* msg) { |
| 88 | fp_query(msg, stdout); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 91 | void fp_query(const u_char* msg, FILE* file) { |
| 92 | fp_nquery(msg, PACKETSZ, file); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 95 | void fp_nquery(const u_char* msg, int len, FILE* file) { |
| 96 | if (res_need_init() && res_init() == -1) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 97 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 98 | res_pquery(&_nres, msg, len, file); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 99 | } |
| 100 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 101 | int res_mkquery(int op, // opcode of query |
| 102 | const char* dname, // domain name |
| 103 | int cl, int type, // class and type of query |
| 104 | const u_char* data, // resource record data |
| 105 | int datalen, // length of data |
| 106 | const u_char* newrr_in, // new rr for modify or append |
| 107 | u_char* buf, // buffer to put query |
| 108 | int buflen) // size of buffer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 109 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 110 | if (res_need_init() && res_init() == -1) { |
| 111 | RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 112 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 113 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 114 | return res_nmkquery(&_nres, op, dname, cl, type, data, datalen, newrr_in, buf, buflen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 115 | } |
| 116 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 117 | int res_query(const char* name, // domain name |
| 118 | int cl, int type, // class and type of query |
| 119 | u_char* answer, // buffer to put answer |
| 120 | int anslen) // size of answer buffer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 121 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 122 | if (res_need_init() && res_init() == -1) { |
| 123 | RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 124 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 125 | } |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 126 | return res_nquery(&_nres, name, cl, type, answer, anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 127 | } |
| 128 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 129 | void res_send_setqhook(res_send_qhook hook) { |
| 130 | _nres.qhook = hook; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 131 | } |
| 132 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 133 | void res_send_setrhook(res_send_rhook hook) { |
| 134 | _nres.rhook = hook; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 137 | int res_isourserver(const struct sockaddr_in* inp) { |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 138 | return res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 139 | } |
| 140 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 141 | int res_send(const u_char* buf, int buflen, u_char* ans, int anssiz) { |
| 142 | if (res_need_init() && res_init() == -1) { |
| 143 | /* errno should have been set by res_init() in this case. */ |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 144 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 145 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 146 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 147 | return res_nsend(&_nres, buf, buflen, ans, anssiz); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 148 | } |
| 149 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 150 | void res_close(void) { |
| 151 | res_nclose(&_nres); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 152 | } |
| 153 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 154 | int res_search(const char* name, // domain name |
| 155 | int cl, int type, // class and type of query |
| 156 | u_char* answer, // buffer to put answer |
| 157 | int anslen) // size of answer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 158 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 159 | if (res_need_init() && res_init() == -1) { |
| 160 | RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 161 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 162 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 163 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 164 | return res_nsearch(&_nres, name, cl, type, answer, anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 165 | } |
| 166 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 167 | int res_querydomain(const char* name, const char* domain, |
| 168 | int cl, int type, // class and type of query |
| 169 | u_char* answer, // buffer to put answer |
| 170 | int anslen) // size of answer |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 171 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 172 | if (res_need_init() && res_init() == -1) { |
| 173 | RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 174 | return -1; |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 175 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 176 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 177 | return res_nquerydomain(&_nres, name, domain, cl, type, answer, anslen); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 178 | } |
| 179 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 180 | int res_opt(int a, u_char* b, int c, int d) { |
| 181 | return res_nopt(&_nres, a, b, c, d); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 182 | } |
| 183 | |
Bernie Innocenti | 1f4a9fd | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 184 | const char* hostalias(const char* /*name*/) { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 185 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 186 | } |