1 #ifndef HEADER_CURL_HOSTIP_H
2 #define HEADER_CURL_HOSTIP_H
3 /***************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 1998 - 2021, 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 ***************************************************************************/
25 #include "curl_setup.h"
27 #include "curl_addrinfo.h"
28 #include "timeval.h" /* for timediff_t */
37 #define in_addr_t unsigned long
40 /* Allocate enough memory to hold the full name information structs and
41 * everything. OSF1 is known to require at least 8872 bytes. The buffer
42 * required for storing all possible aliases and IP numbers is according to
43 * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
45 #define CURL_HOSTENT_SIZE 9000
47 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
48 many seconds for a name resolve */
50 #define CURL_ASYNC_SUCCESS CURLE_OK
58 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
59 * Global DNS cache is general badness. Do not use. This will be removed in
60 * a future version. Use the share interface instead!
62 * Returns a struct Curl_hash pointer on success, NULL on failure.
64 struct Curl_hash *Curl_global_host_cache_init(void);
66 struct Curl_dns_entry {
67 struct Curl_addrinfo *addr;
68 /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (doesn't time out) */
70 /* use-counter, use Curl_resolv_unlock to release reference */
74 bool Curl_host_is_ipnum(const char *hostname);
77 * Curl_resolv() returns an entry with the info for the specified host
80 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
81 * use, or we'll leak memory!
85 CURLRESOLV_TIMEDOUT = -2,
86 CURLRESOLV_ERROR = -1,
87 CURLRESOLV_RESOLVED = 0,
88 CURLRESOLV_PENDING = 1
90 enum resolve_t Curl_resolv(struct Curl_easy *data,
94 struct Curl_dns_entry **dnsentry);
95 enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
96 const char *hostname, int port,
97 struct Curl_dns_entry **dnsentry,
98 timediff_t timeoutms);
102 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
104 bool Curl_ipv6works(struct Curl_easy *data);
106 #define Curl_ipv6works(x) FALSE
110 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
111 * been set and returns TRUE if they are OK.
113 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn);
117 * Curl_getaddrinfo() is the generic low-level name resolve API within this
118 * source file. There are several versions of this function - for different
119 * name resolve layers (selected at build-time). They all take this same set
122 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
123 const char *hostname,
128 /* unlock a previously resolved dns entry */
129 void Curl_resolv_unlock(struct Curl_easy *data,
130 struct Curl_dns_entry *dns);
132 /* init a new dns cache */
133 void Curl_init_dnscache(struct Curl_hash *hash);
135 /* prune old entries from the DNS cache */
136 void Curl_hostcache_prune(struct Curl_easy *data);
138 /* Return # of addresses in a Curl_addrinfo struct */
139 int Curl_num_addresses(const struct Curl_addrinfo *addr);
141 /* IPv4 threadsafe resolve function used for synch and asynch builds */
142 struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port);
144 CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect);
147 * Curl_addrinfo_callback() is used when we build with any asynch specialty.
148 * Handles end of async request processing. Inserts ai into hostcache when
149 * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
150 * request completed whether successful or failed.
152 CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
154 struct Curl_addrinfo *ai);
157 * Curl_printable_address() returns a printable version of the 1st address
158 * given in the 'ip' argument. The result will be stored in the buf that is
161 void Curl_printable_address(const struct Curl_addrinfo *ip,
162 char *buf, size_t bufsize);
165 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
167 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
169 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
170 * use, or we'll leak memory!
172 struct Curl_dns_entry *
173 Curl_fetch_addr(struct Curl_easy *data,
174 const char *hostname,
178 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
180 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
182 struct Curl_dns_entry *
183 Curl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr,
184 const char *hostname, int port);
187 #define CURL_INADDR_NONE (in_addr_t) ~0
189 #define CURL_INADDR_NONE INADDR_NONE
192 #ifdef HAVE_SIGSETJMP
193 /* Forward-declaration of variable defined in hostip.c. Beware this
194 * is a global and unique instance. This is used to store the return
195 * address that we can jump back to from inside a signal handler.
196 * This is not thread-safe stuff.
198 extern sigjmp_buf curl_jmpenv;
202 * Function provided by the resolver backend to set DNS servers to use.
204 CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers);
207 * Function provided by the resolver backend to set
208 * outgoing interface to use for DNS requests
210 CURLcode Curl_set_dns_interface(struct Curl_easy *data,
214 * Function provided by the resolver backend to set
215 * local IPv4 address to use as source address for DNS requests
217 CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
218 const char *local_ip4);
221 * Function provided by the resolver backend to set
222 * local IPv6 address to use as source address for DNS requests
224 CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
225 const char *local_ip6);
228 * Clean off entries from the cache
230 void Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash);
233 * Populate the cache with specified entries from CURLOPT_RESOLVE.
235 CURLcode Curl_loadhostpairs(struct Curl_easy *data);
236 CURLcode Curl_resolv_check(struct Curl_easy *data,
237 struct Curl_dns_entry **dns);
238 int Curl_resolv_getsock(struct Curl_easy *data,
239 curl_socket_t *socks);
241 CURLcode Curl_resolver_error(struct Curl_easy *data);
242 #endif /* HEADER_CURL_HOSTIP_H */