1 #ifndef HEADER_CURL_HOSTIP_H
2 #define HEADER_CURL_HOSTIP_H
3 /***************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 1998 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
23 * SPDX-License-Identifier: curl
25 ***************************************************************************/
27 #include "curl_setup.h"
29 #include "curl_addrinfo.h"
30 #include "timeval.h" /* for timediff_t */
39 #define in_addr_t unsigned long
42 /* Allocate enough memory to hold the full name information structs and
43 * everything. OSF1 is known to require at least 8872 bytes. The buffer
44 * required for storing all possible aliases and IP numbers is according to
45 * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
47 #define CURL_HOSTENT_SIZE 9000
49 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
50 many seconds for a name resolve */
52 #define CURL_ASYNC_SUCCESS CURLE_OK
60 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
61 * Global DNS cache is general badness. Do not use. This will be removed in
62 * a future version. Use the share interface instead!
64 * Returns a struct Curl_hash pointer on success, NULL on failure.
66 struct Curl_hash *Curl_global_host_cache_init(void);
68 struct Curl_dns_entry {
69 struct Curl_addrinfo *addr;
70 /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (doesn't time out) */
72 /* use-counter, use Curl_resolv_unlock to release reference */
76 bool Curl_host_is_ipnum(const char *hostname);
79 * Curl_resolv() returns an entry with the info for the specified host
82 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
83 * use, or we'll leak memory!
87 CURLRESOLV_TIMEDOUT = -2,
88 CURLRESOLV_ERROR = -1,
89 CURLRESOLV_RESOLVED = 0,
90 CURLRESOLV_PENDING = 1
92 enum resolve_t Curl_resolv(struct Curl_easy *data,
96 struct Curl_dns_entry **dnsentry);
97 enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
98 const char *hostname, int port,
99 struct Curl_dns_entry **dnsentry,
100 timediff_t timeoutms);
104 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
106 bool Curl_ipv6works(struct Curl_easy *data);
108 #define Curl_ipv6works(x) FALSE
112 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
113 * been set and returns TRUE if they are OK.
115 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn);
119 * Curl_getaddrinfo() is the generic low-level name resolve API within this
120 * source file. There are several versions of this function - for different
121 * name resolve layers (selected at build-time). They all take this same set
124 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
125 const char *hostname,
130 /* unlock a previously resolved dns entry */
131 void Curl_resolv_unlock(struct Curl_easy *data,
132 struct Curl_dns_entry *dns);
134 /* init a new dns cache */
135 void Curl_init_dnscache(struct Curl_hash *hash, int hashsize);
137 /* prune old entries from the DNS cache */
138 void Curl_hostcache_prune(struct Curl_easy *data);
140 /* Return # of addresses in a Curl_addrinfo struct */
141 int Curl_num_addresses(const struct Curl_addrinfo *addr);
143 /* IPv4 threadsafe resolve function used for synch and asynch builds */
144 struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port);
146 CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect);
149 * Curl_addrinfo_callback() is used when we build with any asynch specialty.
150 * Handles end of async request processing. Inserts ai into hostcache when
151 * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
152 * request completed whether successful or failed.
154 CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
156 struct Curl_addrinfo *ai);
159 * Curl_printable_address() returns a printable version of the 1st address
160 * given in the 'ip' argument. The result will be stored in the buf that is
163 void Curl_printable_address(const struct Curl_addrinfo *ip,
164 char *buf, size_t bufsize);
167 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
169 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
171 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
172 * use, or we'll leak memory!
174 struct Curl_dns_entry *
175 Curl_fetch_addr(struct Curl_easy *data,
176 const char *hostname,
180 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
182 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
184 struct Curl_dns_entry *
185 Curl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr,
186 const char *hostname, int port);
189 #define CURL_INADDR_NONE (in_addr_t) ~0
191 #define CURL_INADDR_NONE INADDR_NONE
194 #ifdef HAVE_SIGSETJMP
195 /* Forward-declaration of variable defined in hostip.c. Beware this
196 * is a global and unique instance. This is used to store the return
197 * address that we can jump back to from inside a signal handler.
198 * This is not thread-safe stuff.
200 extern sigjmp_buf curl_jmpenv;
204 * Function provided by the resolver backend to set DNS servers to use.
206 CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers);
209 * Function provided by the resolver backend to set
210 * outgoing interface to use for DNS requests
212 CURLcode Curl_set_dns_interface(struct Curl_easy *data,
216 * Function provided by the resolver backend to set
217 * local IPv4 address to use as source address for DNS requests
219 CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
220 const char *local_ip4);
223 * Function provided by the resolver backend to set
224 * local IPv6 address to use as source address for DNS requests
226 CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
227 const char *local_ip6);
230 * Clean off entries from the cache
232 void Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash);
235 * Populate the cache with specified entries from CURLOPT_RESOLVE.
237 CURLcode Curl_loadhostpairs(struct Curl_easy *data);
238 CURLcode Curl_resolv_check(struct Curl_easy *data,
239 struct Curl_dns_entry **dns);
240 int Curl_resolv_getsock(struct Curl_easy *data,
241 curl_socket_t *socks);
243 CURLcode Curl_resolver_error(struct Curl_easy *data);
244 #endif /* HEADER_CURL_HOSTIP_H */