00001 /* 00002 * Copyright (c) 1983, 1987, 1989 00003 * The Regents of the University of California. All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * 3. Neither the name of the University nor the names of its contributors 00014 * may be used to endorse or promote products derived from this software 00015 * without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 00018 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 00021 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00023 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00024 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00025 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00026 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00027 * SUCH DAMAGE. 00028 */ 00029 00030 /* 00031 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 00032 * Portions Copyright (c) 1996-1999 by Internet Software Consortium. 00033 * 00034 * Permission to use, copy, modify, and distribute this software for any 00035 * purpose with or without fee is hereby granted, provided that the above 00036 * copyright notice and this permission notice appear in all copies. 00037 * 00038 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 00039 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 00040 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 00041 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 00042 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 00043 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 00044 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00045 */ 00046 00047 /* 00048 Imported from Bind-9.5.2-P2 00049 00050 Changes: 00051 00052 Licensed to the Apache Software Foundation (ASF) under one 00053 or more contributor license agreements. See the NOTICE file 00054 distributed with this work for additional information 00055 regarding copyright ownership. The ASF licenses this file 00056 to you under the Apache License, Version 2.0 (the 00057 "License"); you may not use this file except in compliance 00058 with the License. You may obtain a copy of the License at 00059 00060 http://www.apache.org/licenses/LICENSE-2.0 00061 00062 Unless required by applicable law or agreed to in writing, software 00063 distributed under the License is distributed on an "AS IS" BASIS, 00064 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00065 See the License for the specific language governing permissions and 00066 limitations under the License. 00067 */ 00068 00069 #ifndef _ink_resolver_h_ 00070 #define _ink_resolver_h_ 00071 00072 #include "ink_platform.h" 00073 #include <ts/ink_inet.h> 00074 #include <resolv.h> 00075 #include <arpa/nameser.h> 00076 00077 #if defined(openbsd) 00078 #define NS_INT16SZ INT16SZ 00079 #define NS_INT32SZ INT32SZ 00080 #define NS_CMPRSFLGS INDIR_MASK 00081 #define NS_GET16 GETSHORT 00082 #define NS_GET32 GETLONG 00083 #define NS_PUT16 PUTSHORT 00084 #define NS_PUT32 PUTLONG 00085 #endif 00086 00087 #define INK_RES_F_VC 0x00000001 /*%< socket is TCP */ 00088 #define INK_RES_F_CONN 0x00000002 /*%< socket is connected */ 00089 #define INK_RES_F_EDNS0ERR 0x00000004 /*%< EDNS0 caused errors */ 00090 #define INK_RES_F__UNUSED 0x00000008 /*%< (unused) */ 00091 #define INK_RES_F_LASTMASK 0x000000F0 /*%< ordinal server of last res_nsend */ 00092 #define INK_RES_F_LASTSHIFT 4 /*%< bit position of LASTMASK "flag" */ 00093 #define INK_RES_GETLAST(res) (((res)._flags & INK_RES_F_LASTMASK) >> INK_RES_F_LASTSHIFT) 00094 00095 /* res_findzonecut2() options */ 00096 #define INK_RES_EXHAUSTIVE 0x00000001 /*%< always do all queries */ 00097 #define INK_RES_IPV4ONLY 0x00000002 /*%< IPv4 only */ 00098 #define INK_RES_IPV6ONLY 0x00000004 /*%< IPv6 only */ 00099 00100 /*% 00101 * * Resolver options (keep these in synch with res_debug.c, please) 00102 [amc] Most of these are never used. AFAICT it's RECURSE and DEBUG only. 00103 * */ 00104 #define INK_RES_INIT 0x00000001 /*%< address initialized */ 00105 #define INK_RES_DEBUG 0x00000002 /*%< print debug messages */ 00106 #define INK_RES_AAONLY 0x00000004 /*%< authoritative answers only (!IMPL)*/ 00107 #define INK_RES_USEVC 0x00000008 /*%< use virtual circuit */ 00108 #define INK_RES_PRIMARY 0x00000010 /*%< query primary server only (!IMPL) */ 00109 #define INK_RES_IGNTC 0x00000020 /*%< ignore trucation errors */ 00110 #define INK_RES_RECURSE 0x00000040 /*%< recursion desired */ 00111 #define INK_RES_DEFNAMES 0x00000080 /*%< use default domain name */ 00112 #define INK_RES_STAYOPEN 0x00000100 /*%< Keep TCP socket open */ 00113 #define INK_RES_DNSRCH 0x00000200 /*%< search up local domain tree */ 00114 #define INK_RES_INSECURE1 0x00000400 /*%< type 1 security disabled */ 00115 #define INK_RES_INSECURE2 0x00000800 /*%< type 2 security disabled */ 00116 #define INK_RES_NOALIASES 0x00001000 /*%< shuts off HOSTALIASES feature */ 00117 #define INK_RES_USE_INET6 0x00002000 /*%< use/map IPv6 in gethostbyname() */ 00118 #define INK_RES_ROTATE 0x00004000 /*%< rotate ns list after each query */ 00119 #define INK_RES_NOCHECKNAME 0x00008000 /*%< do not check names for sanity. */ 00120 #define INK_RES_KEEPTSIG 0x00010000 /*%< do not strip TSIG records */ 00121 #define INK_RES_BLAST 0x00020000 /*%< blast all recursive servers */ 00122 #define INK_RES_NSID 0x00040000 /*%< request name server ID */ 00123 #define INK_RES_NOTLDQUERY 0x00100000 /*%< don't unqualified name as a tld */ 00124 #define INK_RES_USE_DNSSEC 0x00200000 /*%< use DNSSEC using OK bit in OPT */ 00125 /* #define INK_RES_DEBUG2 0x00400000 */ /* nslookup internal */ 00126 /* KAME extensions: use higher bit to avoid conflict with ISC use */ 00127 #define INK_RES_USE_DNAME 0x10000000 /*%< use DNAME */ 00128 #define INK_RES_USE_EDNS0 0x40000000 /*%< use EDNS0 if configured */ 00129 00130 #define INK_RES_DEFAULT (INK_RES_RECURSE | INK_RES_DEFNAMES | \ 00131 INK_RES_DNSRCH) 00132 00133 #define INK_MAXNS 32 /*%< max # name servers we'll track */ 00134 #define INK_MAXDFLSRCH 3 /*%< # default domain levels to try */ 00135 #define INK_MAXDNSRCH 6 /*%< max # domains in search path */ 00136 #define INK_LOCALDOMAINPARTS 2 /*%< min levels in name that is "local" */ 00137 #define INK_RES_TIMEOUT 5 /*%< min. seconds between retries */ 00138 #define INK_RES_TIMEOUT 5 /*%< min. seconds between retries */ 00139 #define INK_RES_MAXNDOTS 15 /*%< should reflect bit field size */ 00140 #define INK_RES_MAXRETRANS 30 /*%< only for resolv.conf/RES_OPTIONS */ 00141 #define INK_RES_MAXRETRY 5 /*%< only for resolv.conf/RES_OPTIONS */ 00142 #define INK_RES_DFLRETRY 2 /*%< Default #/tries. */ 00143 #define INK_RES_MAXTIME 65535 /*%< Infinity, in milliseconds. */ 00144 00145 #define INK_NS_TYPE_ELT 0x40 /*%< EDNS0 extended label type */ 00146 #define INK_DNS_LABELTYPE_BITSTRING 0x41 00147 00148 /// IP family preference for DNS resolution. 00149 /// Used for configuration. 00150 enum HostResPreference { 00151 HOST_RES_PREFER_NONE = 0, ///< Invalid / init value. 00152 HOST_RES_PREFER_CLIENT, ///< Prefer family of client connection. 00153 HOST_RES_PREFER_IPV4, ///< Prefer IPv4. 00154 HOST_RES_PREFER_IPV6 ///< Prefer IPv6 00155 }; 00156 /// # of preference values. 00157 static int const N_HOST_RES_PREFERENCE = HOST_RES_PREFER_IPV6+1; 00158 /// # of entries in a preference ordering. 00159 static int const N_HOST_RES_PREFERENCE_ORDER = 3; 00160 /// Storage for preference ordering. 00161 typedef HostResPreference HostResPreferenceOrder[N_HOST_RES_PREFERENCE_ORDER]; 00162 /// Global, hard wired default value for preference ordering. 00163 extern HostResPreferenceOrder const HOST_RES_DEFAULT_PREFERENCE_ORDER; 00164 /// Global (configurable) default. 00165 extern HostResPreferenceOrder host_res_default_preference_order; 00166 /// String versions of @c FamilyPreference 00167 extern char const* const HOST_RES_PREFERENCE_STRING[N_HOST_RES_PREFERENCE]; 00168 00169 /// IP family to use in a DNS query for a host address. 00170 /// Used during DNS query operations. 00171 enum HostResStyle{ 00172 HOST_RES_NONE = 0, ///< No preference / unspecified / init value. 00173 HOST_RES_IPV4, ///< Use IPv4 if possible. 00174 HOST_RES_IPV4_ONLY, ///< Resolve on IPv4 addresses. 00175 HOST_RES_IPV6, ///< Use IPv6 if possible. 00176 HOST_RES_IPV6_ONLY ///< Resolve only IPv6 addresses. 00177 }; 00178 00179 /// Strings for host resolution styles 00180 extern char const* const HOST_RES_STYLE_STRING[]; 00181 00182 /// Caclulate the effective resolution preferences. 00183 extern HostResStyle 00184 ats_host_res_from( 00185 int family, ///< Connection family 00186 HostResPreferenceOrder ///< Preference ordering. 00187 ); 00188 /// Calculate the host resolution style to force a family match to @a addr. 00189 extern HostResStyle 00190 ats_host_res_match(sockaddr const* addr); 00191 00192 /** Parse a host resolution configuration string. 00193 */ 00194 extern void 00195 parse_host_res_preference( 00196 char const* value, ///< [in] Configuration string. 00197 HostResPreferenceOrder order /// [out] Order to update. 00198 ); 00199 00200 #ifndef NS_GET16 00201 #define NS_GET16(s, cp) do { \ 00202 const u_char *t_cp = (const u_char *)(cp); \ 00203 (s) = ((u_int16_t)t_cp[0] << 8) \ 00204 | ((u_int16_t)t_cp[1]) \ 00205 ; \ 00206 (cp) += NS_INT16SZ; \ 00207 } while (0) 00208 #endif 00209 00210 #ifndef NS_GET32 00211 #define NS_GET32(l, cp) do { \ 00212 const u_char *t_cp = (const u_char *)(cp); \ 00213 (l) = ((u_int32_t)t_cp[0] << 24) \ 00214 | ((u_int32_t)t_cp[1] << 16) \ 00215 | ((u_int32_t)t_cp[2] << 8) \ 00216 | ((u_int32_t)t_cp[3]) \ 00217 ; \ 00218 (cp) += NS_INT32SZ; \ 00219 } while (0) 00220 #endif 00221 00222 #ifndef NS_PUT16 00223 #define NS_PUT16(s, cp) do { \ 00224 u_int16_t t_s = (u_int16_t)(s); \ 00225 u_char *t_cp = (u_char *)(cp); \ 00226 *t_cp++ = t_s >> 8; \ 00227 *t_cp = t_s; \ 00228 (cp) += NS_INT16SZ; \ 00229 } while (0) 00230 #endif 00231 00232 #ifndef NS_PUT32 00233 #define NS_PUT32(l, cp) do { \ 00234 u_int32_t t_l = (u_int32_t)(l); \ 00235 u_char *t_cp = (u_char *)(cp); \ 00236 *t_cp++ = t_l >> 24; \ 00237 *t_cp++ = t_l >> 16; \ 00238 *t_cp++ = t_l >> 8; \ 00239 *t_cp = t_l; \ 00240 (cp) += NS_INT32SZ; \ 00241 } while (0) 00242 #endif 00243 00244 // Do we really need these to be C compatible? - AMC 00245 struct ts_imp_res_state { 00246 int retrans; /*%< retransmission time interval */ 00247 int retry; /*%< number of times to retransmit */ 00248 #ifdef sun 00249 u_int options; /*%< option flags - see below. */ 00250 #else 00251 u_long options; /*%< option flags - see below. */ 00252 #endif 00253 int nscount; /*%< number of name servers */ 00254 IpEndpoint nsaddr_list[INK_MAXNS]; /*%< address of name server */ 00255 u_short id; /*%< current message id */ 00256 char *dnsrch[MAXDNSRCH+1]; /*%< components of domain to search */ 00257 char defdname[256]; /*%< default domain (deprecated) */ 00258 #ifdef sun 00259 u_int pfcode; /*%< RES_PRF_ flags - see below. */ 00260 #else 00261 u_long pfcode; /*%< RES_PRF_ flags - see below. */ 00262 #endif 00263 unsigned ndots:4; /*%< threshold for initial abs. query */ 00264 unsigned nsort:4; /*%< number of elements in sort_list[] */ 00265 char unused[3]; 00266 res_send_qhook qhook; /*%< query hook */ 00267 res_send_rhook rhook; /*%< response hook */ 00268 int res_h_errno; /*%< last one set for this context */ 00269 int _vcsock; /*%< PRIVATE: for res_send VC i/o */ 00270 u_int _flags; /*%< PRIVATE: see below */ 00271 u_int _pad; /*%< make _u 64 bit aligned */ 00272 u_int16_t _nstimes[INK_MAXNS]; /*%< ms. */ 00273 }; 00274 typedef ts_imp_res_state *ink_res_state; 00275 00276 int ink_res_init( 00277 ink_res_state, 00278 IpEndpoint const* pHostList, 00279 size_t pHostListSize, 00280 const char *pDefDomain = NULL, 00281 const char *pSearchList = NULL, 00282 const char *pResolvConf = NULL 00283 ); 00284 00285 int ink_res_mkquery(ink_res_state, int, const char *, int, int, 00286 const unsigned char *, int, const unsigned char *, unsigned char *, int); 00287 00288 int ink_ns_name_ntop(const u_char *src, char *dst, size_t dstsiz); 00289 00290 /** Initialize global values for HttpProxyPort / Host Resolution. 00291 */ 00292 void ts_host_res_global_init(); 00293 00294 /** Generate a string representation of a host resolution preference ordering. 00295 @return The length of the string. 00296 */ 00297 int 00298 ts_host_res_order_to_string( 00299 HostResPreferenceOrder const& order, ///< order to print 00300 char* out, ///< Target buffer for string. 00301 int size ///< Size of buffer. 00302 ); 00303 00304 #endif /* _ink_resolver_h_ */ 00305