1 #ifndef HEADER_CURL_HOSTIP_H 2 #define HEADER_CURL_HOSTIP_H 3 /*************************************************************************** 4 * _ _ ____ _ 5 * Project ___| | | | _ \| | 6 * / __| | | | |_) | | 7 * | (__| |_| | _ <| |___ 8 * \___|\___/|_| \_\_____| 9 * 10 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 11 * 12 * This software is licensed as described in the file COPYING, which 13 * you should have received as part of this distribution. The terms 14 * are also available at https://curl.se/docs/copyright.html. 15 * 16 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 17 * copies of the Software, and permit persons to whom the Software is 18 * furnished to do so, under the terms of the COPYING file. 19 * 20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 21 * KIND, either express or implied. 22 * 23 * SPDX-License-Identifier: curl 24 * 25 ***************************************************************************/ 26 27 #include "curl_setup.h" 28 #include "hash.h" 29 #include "curl_addrinfo.h" 30 #include "timeval.h" /* for timediff_t */ 31 #include "asyn.h" 32 33 #include <setjmp.h> 34 35 #ifdef USE_HTTPSRR 36 # include <stdint.h> 37 #endif 38 39 /* Allocate enough memory to hold the full name information structs and 40 * everything. OSF1 is known to require at least 8872 bytes. The buffer 41 * required for storing all possible aliases and IP numbers is according to 42 * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes! 43 */ 44 #define CURL_HOSTENT_SIZE 9000 45 46 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this 47 many seconds for a name resolve */ 48 49 #define CURL_ASYNC_SUCCESS CURLE_OK 50 51 struct addrinfo; 52 struct hostent; 53 struct Curl_easy; 54 struct connectdata; 55 56 /* 57 * Curl_global_host_cache_init() initializes and sets up a global DNS cache. 58 * Global DNS cache is general badness. Do not use. This will be removed in 59 * a future version. Use the share interface instead! 60 * 61 * Returns a struct Curl_hash pointer on success, NULL on failure. 62 */ 63 struct Curl_hash *Curl_global_host_cache_init(void); 64 65 #ifdef USE_HTTPSRR 66 67 #define CURL_MAXLEN_host_name 253 68 69 struct Curl_https_rrinfo { 70 size_t len; /* raw encoded length */ 71 unsigned char *val; /* raw encoded octets */ 72 /* 73 * fields from HTTPS RR, with the mandatory fields 74 * first (priority, target), then the others in the 75 * order of the keytag numbers defined at 76 * https://datatracker.ietf.org/doc/html/rfc9460#section-14.3.2 77 */ 78 uint16_t priority; 79 char *target; 80 char *alpns; /* keytag = 1 */ 81 bool no_def_alpn; /* keytag = 2 */ 82 /* 83 * we do not support ports (keytag = 3) as we do not support 84 * port-switching yet 85 */ 86 unsigned char *ipv4hints; /* keytag = 4 */ 87 size_t ipv4hints_len; 88 unsigned char *echconfiglist; /* keytag = 5 */ 89 size_t echconfiglist_len; 90 unsigned char *ipv6hints; /* keytag = 6 */ 91 size_t ipv6hints_len; 92 }; 93 #endif 94 95 struct Curl_dns_entry { 96 struct Curl_addrinfo *addr; 97 #ifdef USE_HTTPSRR 98 struct Curl_https_rrinfo *hinfo; 99 #endif 100 /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (does not time out) */ 101 time_t timestamp; 102 /* reference counter, entry is freed on reaching 0 */ 103 size_t refcount; 104 /* hostname port number that resolved to addr. */ 105 int hostport; 106 /* hostname that resolved to addr. may be NULL (Unix domain sockets). */ 107 char hostname[1]; 108 }; 109 110 bool Curl_host_is_ipnum(const char *hostname); 111 112 /* 113 * Curl_resolv() returns an entry with the info for the specified host 114 * and port. 115 * 116 * The returned data *MUST* be "released" with Curl_resolv_unlink() after 117 * use, or we will leak memory! 118 */ 119 /* return codes */ 120 enum resolve_t { 121 CURLRESOLV_TIMEDOUT = -2, 122 CURLRESOLV_ERROR = -1, 123 CURLRESOLV_RESOLVED = 0, 124 CURLRESOLV_PENDING = 1 125 }; 126 enum resolve_t Curl_resolv(struct Curl_easy *data, 127 const char *hostname, 128 int port, 129 bool allowDOH, 130 struct Curl_dns_entry **dnsentry); 131 enum resolve_t Curl_resolv_timeout(struct Curl_easy *data, 132 const char *hostname, int port, 133 struct Curl_dns_entry **dnsentry, 134 timediff_t timeoutms); 135 136 #ifdef USE_IPV6 137 /* 138 * Curl_ipv6works() returns TRUE if IPv6 seems to work. 139 */ 140 bool Curl_ipv6works(struct Curl_easy *data); 141 #else 142 #define Curl_ipv6works(x) FALSE 143 #endif 144 145 /* 146 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've 147 * been set and returns TRUE if they are OK. 148 */ 149 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn); 150 151 152 /* 153 * Curl_getaddrinfo() is the generic low-level name resolve API within this 154 * source file. There are several versions of this function - for different 155 * name resolve layers (selected at build-time). They all take this same set 156 * of arguments 157 */ 158 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data, 159 const char *hostname, 160 int port, 161 int *waitp); 162 163 164 /* unlink a dns entry, potentially shared with a cache */ 165 void Curl_resolv_unlink(struct Curl_easy *data, 166 struct Curl_dns_entry **pdns); 167 168 /* init a new dns cache */ 169 void Curl_init_dnscache(struct Curl_hash *hash, size_t hashsize); 170 171 /* prune old entries from the DNS cache */ 172 void Curl_hostcache_prune(struct Curl_easy *data); 173 174 /* IPv4 threadsafe resolve function used for synch and asynch builds */ 175 struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port); 176 177 CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect); 178 179 /* 180 * Curl_addrinfo_callback() is used when we build with any asynch specialty. 181 * Handles end of async request processing. Inserts ai into hostcache when 182 * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async 183 * request completed whether successful or failed. 184 */ 185 CURLcode Curl_addrinfo_callback(struct Curl_easy *data, 186 int status, 187 struct Curl_addrinfo *ai); 188 189 /* 190 * Curl_printable_address() returns a printable version of the 1st address 191 * given in the 'ip' argument. The result will be stored in the buf that is 192 * bufsize bytes big. 193 */ 194 void Curl_printable_address(const struct Curl_addrinfo *ip, 195 char *buf, size_t bufsize); 196 197 /* 198 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache. 199 * 200 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache. 201 * 202 * The returned data *MUST* be "released" with Curl_resolv_unlink() after 203 * use, or we will leak memory! 204 */ 205 struct Curl_dns_entry * 206 Curl_fetch_addr(struct Curl_easy *data, 207 const char *hostname, 208 int port); 209 210 /* 211 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache. 212 * @param permanent iff TRUE, entry will never become stale 213 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed. 214 */ 215 struct Curl_dns_entry * 216 Curl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr, 217 const char *hostname, size_t hostlen, int port, 218 bool permanent); 219 220 #ifndef INADDR_NONE 221 #define CURL_INADDR_NONE (in_addr_t) ~0 222 #else 223 #define CURL_INADDR_NONE INADDR_NONE 224 #endif 225 226 /* 227 * Function provided by the resolver backend to set DNS servers to use. 228 */ 229 CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers); 230 231 /* 232 * Function provided by the resolver backend to set 233 * outgoing interface to use for DNS requests 234 */ 235 CURLcode Curl_set_dns_interface(struct Curl_easy *data, 236 const char *interf); 237 238 /* 239 * Function provided by the resolver backend to set 240 * local IPv4 address to use as source address for DNS requests 241 */ 242 CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data, 243 const char *local_ip4); 244 245 /* 246 * Function provided by the resolver backend to set 247 * local IPv6 address to use as source address for DNS requests 248 */ 249 CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data, 250 const char *local_ip6); 251 252 /* 253 * Clean off entries from the cache 254 */ 255 void Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash); 256 257 /* 258 * Populate the cache with specified entries from CURLOPT_RESOLVE. 259 */ 260 CURLcode Curl_loadhostpairs(struct Curl_easy *data); 261 CURLcode Curl_resolv_check(struct Curl_easy *data, 262 struct Curl_dns_entry **dns); 263 int Curl_resolv_getsock(struct Curl_easy *data, 264 curl_socket_t *socks); 265 266 CURLcode Curl_resolver_error(struct Curl_easy *data); 267 #endif /* HEADER_CURL_HOSTIP_H */ 268