1 /* 2 * Copyright (c) 1983, 1987, 1989 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 32 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 33 * 34 * Permission to use, copy, modify, and distribute this software for any 35 * purpose with or without fee is hereby granted, provided that the above 36 * copyright notice and this permission notice appear in all copies. 37 * 38 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 39 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 40 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 41 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 42 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 43 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 44 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 45 */ 46 47 /* 48 Imported from Bind-9.5.2-P2 49 50 Changes: 51 52 Licensed to the Apache Software Foundation (ASF) under one 53 or more contributor license agreements. See the NOTICE file 54 distributed with this work for additional information 55 regarding copyright ownership. The ASF licenses this file 56 to you under the Apache License, Version 2.0 (the 57 "License"); you may not use this file except in compliance 58 with the License. You may obtain a copy of the License at 59 60 http://www.apache.org/licenses/LICENSE-2.0 61 62 Unless required by applicable law or agreed to in writing, software 63 distributed under the License is distributed on an "AS IS" BASIS, 64 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 65 See the License for the specific language governing permissions and 66 limitations under the License. 67 */ 68 69 #pragma once 70 71 #include "tscore/ink_platform.h" 72 #include "tscore/ink_inet.h" 73 74 #include <resolv.h> 75 #include <arpa/nameser.h> 76 77 #include <array> 78 79 #if defined(openbsd) 80 #define NS_INT16SZ INT16SZ 81 #define NS_INT32SZ INT32SZ 82 #define NS_CMPRSFLGS INDIR_MASK 83 #define NS_GET16 GETSHORT 84 #define NS_GET32 GETLONG 85 #define NS_PUT16 PUTSHORT 86 #define NS_PUT32 PUTLONG 87 #endif 88 89 #ifndef T_DNAME 90 #define T_DNAME ns_t_dname 91 #endif 92 #define INK_RES_F_VC 0x00000001 /*%< socket is TCP */ 93 #define INK_RES_F_CONN 0x00000002 /*%< socket is connected */ 94 #define INK_RES_F_EDNS0ERR 0x00000004 /*%< EDNS0 caused errors */ 95 #define INK_RES_F__UNUSED 0x00000008 /*%< (unused) */ 96 #define INK_RES_F_LASTMASK 0x000000F0 /*%< ordinal server of last res_nsend */ 97 #define INK_RES_F_LASTSHIFT 4 /*%< bit position of LASTMASK "flag" */ 98 #define INK_RES_GETLAST(res) (((res)._flags & INK_RES_F_LASTMASK) >> INK_RES_F_LASTSHIFT) 99 100 /* res_findzonecut2() options */ 101 #define INK_RES_EXHAUSTIVE 0x00000001 /*%< always do all queries */ 102 #define INK_RES_IPV4ONLY 0x00000002 /*%< IPv4 only */ 103 #define INK_RES_IPV6ONLY 0x00000004 /*%< IPv6 only */ 104 105 /*% 106 * * Resolver options (keep these in synch with res_debug.c, please) 107 [amc] Most of these are never used. AFAICT it's RECURSE and DEBUG only. 108 * */ 109 #define INK_RES_INIT 0x00000001 /*%< address initialized */ 110 #define INK_RES_DEBUG 0x00000002 /*%< print debug messages */ 111 #define INK_RES_AAONLY 0x00000004 /*%< authoritative answers only (!IMPL)*/ 112 #define INK_RES_USEVC 0x00000008 /*%< use virtual circuit */ 113 #define INK_RES_PRIMARY 0x00000010 /*%< query primary server only (!IMPL) */ 114 #define INK_RES_IGNTC 0x00000020 /*%< ignore truncation errors */ 115 #define INK_RES_RECURSE 0x00000040 /*%< recursion desired */ 116 #define INK_RES_DEFNAMES 0x00000080 /*%< use default domain name */ 117 #define INK_RES_STAYOPEN 0x00000100 /*%< Keep TCP socket open */ 118 #define INK_RES_DNSRCH 0x00000200 /*%< search up local domain tree */ 119 #define INK_RES_INSECURE1 0x00000400 /*%< type 1 security disabled */ 120 #define INK_RES_INSECURE2 0x00000800 /*%< type 2 security disabled */ 121 #define INK_RES_NOALIASES 0x00001000 /*%< shuts off HOSTALIASES feature */ 122 #define INK_RES_USE_INET6 0x00002000 /*%< use/map IPv6 in gethostbyname() */ 123 #define INK_RES_ROTATE 0x00004000 /*%< rotate ns list after each query */ 124 #define INK_RES_NOCHECKNAME 0x00008000 /*%< do not check names for sanity. */ 125 #define INK_RES_KEEPTSIG 0x00010000 /*%< do not strip TSIG records */ 126 #define INK_RES_BLAST 0x00020000 /*%< blast all recursive servers */ 127 #define INK_RES_NSID 0x00040000 /*%< request name server ID */ 128 #define INK_RES_NOTLDQUERY 0x00100000 /*%< don't unqualified name as a tld */ 129 #define INK_RES_USE_DNSSEC 0x00200000 /*%< use DNSSEC using OK bit in OPT */ 130 /* #define INK_RES_DEBUG2 0x00400000 */ /* nslookup internal */ 131 /* KAME extensions: use higher bit to avoid conflict with ISC use */ 132 #define INK_RES_USE_DNAME 0x10000000 /*%< use DNAME */ 133 #define INK_RES_USE_EDNS0 0x40000000 /*%< use EDNS0 if configured */ 134 135 #define INK_RES_DEFAULT (INK_RES_RECURSE | INK_RES_DEFNAMES | INK_RES_DNSRCH) 136 137 #define INK_MAXNS 32 /*%< max # name servers we'll track */ 138 #define INK_MAXDFLSRCH 3 /*%< # default domain levels to try */ 139 #define INK_MAXDNSRCH 6 /*%< max # domains in search path */ 140 #define INK_LOCALDOMAINPARTS 2 /*%< min levels in name that is "local" */ 141 #define INK_RES_TIMEOUT 5 /*%< min. seconds between retries */ 142 #define INK_RES_TIMEOUT 5 /*%< min. seconds between retries */ 143 #define INK_RES_MAXNDOTS 15 /*%< should reflect bit field size */ 144 #define INK_RES_MAXRETRANS 30 /*%< only for resolv.conf/RES_OPTIONS */ 145 #define INK_RES_MAXRETRY 5 /*%< only for resolv.conf/RES_OPTIONS */ 146 #define INK_RES_DFLRETRY 2 /*%< Default #/tries. */ 147 #define INK_RES_MAXTIME 65535 /*%< Infinity, in milliseconds. */ 148 149 #define INK_NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */ 150 #define INK_DNS_LABELTYPE_BITSTRING 0x41 151 152 /// IP family preference for DNS resolution. 153 /// Used for configuration. 154 enum HostResPreference { 155 HOST_RES_PREFER_NONE = 0, ///< Invalid / init value. 156 HOST_RES_PREFER_CLIENT, ///< Prefer family of client connection. 157 HOST_RES_PREFER_IPV4, ///< Prefer IPv4. 158 HOST_RES_PREFER_IPV6 ///< Prefer IPv6 159 }; 160 /// # of preference values. 161 static int const N_HOST_RES_PREFERENCE = HOST_RES_PREFER_IPV6 + 1; 162 /// Storage for preference ordering. 163 using HostResPreferenceOrder = std::array<HostResPreference, 3>; 164 /// Global, hard wired default value for preference ordering. 165 extern HostResPreferenceOrder const HOST_RES_DEFAULT_PREFERENCE_ORDER; 166 /// Global (configurable) default. 167 extern HostResPreferenceOrder host_res_default_preference_order; 168 /// String versions of @c FamilyPreference 169 extern const char *const HOST_RES_PREFERENCE_STRING[N_HOST_RES_PREFERENCE]; 170 171 /// IP family to use in a DNS query for a host address. 172 /// Used during DNS query operations. 173 enum HostResStyle { 174 HOST_RES_NONE = 0, ///< No preference / unspecified / init value. 175 HOST_RES_IPV4, ///< Use IPv4 if possible. 176 HOST_RES_IPV4_ONLY, ///< Resolve on IPv4 addresses. 177 HOST_RES_IPV6, ///< Use IPv6 if possible. 178 HOST_RES_IPV6_ONLY ///< Resolve only IPv6 addresses. 179 }; 180 181 /// Strings for host resolution styles 182 extern const char *const HOST_RES_STYLE_STRING[]; 183 184 /// Calculate the effective resolution preferences. 185 extern HostResStyle ats_host_res_from(int family, ///< Connection family 186 HostResPreferenceOrder const & ///< Preference ordering. 187 ); 188 189 /** Parse a host resolution configuration string. 190 */ 191 extern void parse_host_res_preference(const char *value, ///< [in] Configuration string. 192 HostResPreferenceOrder &order /// [out] Order to update. 193 ); 194 195 /// Configure the preference order to hold only what's from the client address. 196 /// @addr[in] client's address. 197 /// @order[out] Order to update 198 extern void ats_force_order_by_family(sockaddr const *addr, HostResPreferenceOrder order); 199 200 // Domain resolution priority for origin. 201 struct HostResData { 202 HostResPreferenceOrder order; 203 // keep the configuration value to satisfy the API(TSHttpTxnConfigStringSet) 204 char *conf_value{nullptr}; 205 }; 206 207 #ifndef NS_GET16 208 #define NS_GET16(s, cp) \ 209 do { \ 210 const u_char *t_cp = (const u_char *)(cp); \ 211 (s) = ((uint16_t)t_cp[0] << 8) | ((uint16_t)t_cp[1]); \ 212 (cp) += NS_INT16SZ; \ 213 } while (0) 214 #endif 215 216 #ifndef NS_GET32 217 #define NS_GET32(l, cp) \ 218 do { \ 219 const u_char *t_cp = (const u_char *)(cp); \ 220 (l) = ((uint32_t)t_cp[0] << 24) | ((uint32_t)t_cp[1] << 16) | ((uint32_t)t_cp[2] << 8) | ((uint32_t)t_cp[3]); \ 221 (cp) += NS_INT32SZ; \ 222 } while (0) 223 #endif 224 225 #ifndef NS_PUT16 226 #define NS_PUT16(s, cp) \ 227 do { \ 228 uint16_t t_s = (uint16_t)(s); \ 229 u_char *t_cp = (u_char *)(cp); \ 230 *t_cp++ = t_s >> 8; \ 231 *t_cp = t_s; \ 232 (cp) += NS_INT16SZ; \ 233 } while (0) 234 #endif 235 236 #ifndef NS_PUT32 237 #define NS_PUT32(l, cp) \ 238 do { \ 239 uint32_t t_l = (uint32_t)(l); \ 240 u_char *t_cp = (u_char *)(cp); \ 241 *t_cp++ = t_l >> 24; \ 242 *t_cp++ = t_l >> 16; \ 243 *t_cp++ = t_l >> 8; \ 244 *t_cp = t_l; \ 245 (cp) += NS_INT32SZ; \ 246 } while (0) 247 #endif 248 249 // Do we really need these to be C compatible? - AMC 250 struct ts_imp_res_state { 251 int retrans; /*%< retransmission time interval */ 252 int retry; /*%< number of times to retransmit */ 253 #ifdef sun 254 unsigned options; /*%< option flags - see below. */ 255 #else 256 u_long options; /*%< option flags - see below. */ 257 #endif 258 int nscount; /*%< number of name servers */ 259 IpEndpoint nsaddr_list[INK_MAXNS]; /*%< address of name server */ 260 u_short id; /*%< current message id */ 261 char *dnsrch[MAXDNSRCH + 1]; /*%< components of domain to search */ 262 char defdname[256]; /*%< default domain (deprecated) */ 263 #ifdef sun 264 unsigned pfcode; /*%< RES_PRF_ flags - see below. */ 265 #else 266 u_long pfcode; /*%< RES_PRF_ flags - see below. */ 267 #endif 268 unsigned ndots : 4; /*%< threshold for initial abs. query */ 269 unsigned nsort : 4; /*%< number of elements in sort_list[] */ 270 char unused[3]; 271 int res_h_errno; /*%< last one set for this context */ 272 int _vcsock; /*%< PRIVATE: for res_send VC i/o */ 273 unsigned _flags; /*%< PRIVATE: see below */ 274 unsigned _pad; /*%< make _u 64 bit aligned */ 275 uint16_t _nstimes[INK_MAXNS]; /*%< ms. */ 276 }; 277 typedef ts_imp_res_state *ink_res_state; 278 279 int ink_res_init(ink_res_state, IpEndpoint const *pHostList, size_t pHostListSize, int dnsSearch, const char *pDefDomain = nullptr, 280 const char *pSearchList = nullptr, const char *pResolvConf = nullptr); 281 282 int ink_res_mkquery(ink_res_state, int, const char *, int, int, const unsigned char *, int, const unsigned char *, unsigned char *, 283 int); 284 285 int ink_ns_name_ntop(const u_char *src, char *dst, size_t dstsiz); 286 287 /** Initialize global values for HttpProxyPort / Host Resolution. 288 */ 289 void ts_host_res_global_init(); 290 291 /** Generate a string representation of a host resolution preference ordering. 292 @return The length of the string. 293 */ 294 int ts_host_res_order_to_string(HostResPreferenceOrder const &order, ///< order to print 295 char *out, ///< Target buffer for string. 296 int size ///< Size of buffer. 297 ); 298