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 | |
| 20 | #include <sys/cdefs.h> |
| 21 | #if defined(LIBC_SCCS) && !defined(lint) |
| 22 | #ifdef notdef |
| 23 | static 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 29 | #include <sys/param.h> |
| 30 | #include <sys/socket.h> |
| 31 | #include <sys/time.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 32 | #include <sys/types.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 33 | |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 34 | #include <arpa/inet.h> |
| 35 | #include <arpa/nameser.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 36 | #include <netinet/in.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 37 | |
| 38 | #include <ctype.h> |
| 39 | #include <netdb.h> |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | #include <unistd.h> |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 44 | #include "resolv_private.h" |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 45 | |
| 46 | __LIBC_HIDDEN__ |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 47 | const 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #ifdef BIND_UPDATE |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 55 | const char* const _res_sectioncodes[] = { |
| 56 | "ZONE", |
| 57 | "PREREQUISITES", |
| 58 | "UPDATE", |
| 59 | "ADDITIONAL", |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 60 | }; |
| 61 | #endif |
| 62 | |
| 63 | #ifndef __BIND_NOSTATIC |
| 64 | extern struct __res_state _nres; |
| 65 | |
| 66 | /* Proto. */ |
| 67 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 68 | int res_ourserver_p(const res_state, const struct sockaddr*); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 69 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 70 | #define res_need_init() ((_nres.options & RES_INIT) == 0U) |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 71 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 72 | int res_init(void) { |
| 73 | int rv; |
| 74 | extern int __res_vinit(res_state, int); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 75 | #ifdef COMPAT__RES |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 76 | /* |
| 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 85 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 86 | 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 89 | #endif |
| 90 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 91 | /* |
| 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 113 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 114 | /* |
| 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 119 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 120 | rv = __res_vinit(&_nres, 1); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 121 | #ifdef COMPAT__RES |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 122 | __res_put_old_state(&_nres); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 123 | #endif |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 124 | return rv; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 125 | } |
| 126 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 127 | void p_query(const u_char* msg) { |
| 128 | fp_query(msg, stdout); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 129 | } |
| 130 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 131 | void fp_query(const u_char* msg, FILE* file) { |
| 132 | fp_nquery(msg, PACKETSZ, file); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 133 | } |
| 134 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 135 | void fp_nquery(const u_char* msg, int len, FILE* file) { |
| 136 | if (res_need_init() && res_init() == -1) return; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 137 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 138 | res_pquery(&_nres, msg, len, file); |
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_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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 149 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 150 | 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | #ifdef _LIBRESOLV |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 158 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 163 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 164 | return (res_nmkupdate(&_nres, rrecp_in, buf, buflen)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 165 | } |
| 166 | #endif |
| 167 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 168 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 172 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 173 | 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 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 | void res_send_setqhook(res_send_qhook hook) { |
| 181 | _nres.qhook = hook; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 182 | } |
| 183 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 184 | void res_send_setrhook(res_send_rhook hook) { |
| 185 | _nres.rhook = hook; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 186 | } |
| 187 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 188 | int res_isourserver(const struct sockaddr_in* inp) { |
| 189 | return (res_ourserver_p(&_nres, (const struct sockaddr*) (const void*) inp)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 190 | } |
| 191 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 192 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 197 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 198 | return (res_nsend(&_nres, buf, buflen, ans, anssiz)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | #ifdef _LIBRESOLV |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 202 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 207 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 208 | return (res_nsendsigned(&_nres, buf, buflen, key, ans, anssiz)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 209 | } |
| 210 | #endif |
| 211 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 212 | void res_close(void) { |
| 213 | res_nclose(&_nres); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | #ifdef _LIBRESOLV |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 217 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 222 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 223 | return (res_nupdate(&_nres, rrecp_in, NULL)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 224 | } |
| 225 | #endif |
| 226 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 227 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 231 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 232 | if (res_need_init() && res_init() == -1) { |
| 233 | RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
| 234 | return (-1); |
| 235 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 236 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 237 | return (res_nsearch(&_nres, name, class, type, answer, anslen)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 238 | } |
| 239 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 240 | int 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 244 | { |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 245 | if (res_need_init() && res_init() == -1) { |
| 246 | RES_SET_H_ERRNO(&_nres, NETDB_INTERNAL); |
| 247 | return (-1); |
| 248 | } |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 249 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 250 | return (res_nquerydomain(&_nres, name, domain, class, type, answer, anslen)); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 251 | } |
| 252 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 253 | int res_opt(int a, u_char* b, int c, int d) { |
| 254 | return res_nopt(&_nres, a, b, c, d); |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 255 | } |
| 256 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 257 | const char* hostalias(const char* name) { |
| 258 | return NULL; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | #ifdef ultrix |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 262 | int local_hostname_length(const char* hostname) { |
| 263 | int len_host, len_domain; |
Bernie Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 264 | |
Bernie Innocenti | f12d5bb | 2018-08-31 14:09:46 +0900 | [diff] [blame^] | 265 | 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 Innocenti | 5586419 | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 272 | } |
| 273 | #endif /*ultrix*/ |
| 274 | |
| 275 | #endif |