Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | #ifndef HEADER_CURL_HOSTIP_H |
| 2 | #define HEADER_CURL_HOSTIP_H |
| 3 | /*************************************************************************** |
| 4 | * _ _ ____ _ |
| 5 | * Project ___| | | | _ \| | |
| 6 | * / __| | | | |_) | | |
| 7 | * | (__| |_| | _ <| |___ |
| 8 | * \___|\___/|_| \_\_____| |
| 9 | * |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 10 | * Copyright (C) 1998 - 2018, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 14 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | ***************************************************************************/ |
| 24 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 25 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 26 | #include "hash.h" |
| 27 | #include "curl_addrinfo.h" |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 28 | #include "asyn.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 29 | |
| 30 | #ifdef HAVE_SETJMP_H |
| 31 | #include <setjmp.h> |
| 32 | #endif |
| 33 | |
| 34 | #ifdef NETWARE |
| 35 | #undef in_addr_t |
| 36 | #define in_addr_t unsigned long |
| 37 | #endif |
| 38 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 49 | #define CURL_ASYNC_SUCCESS CURLE_OK |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 50 | |
| 51 | struct addrinfo; |
| 52 | struct hostent; |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 53 | struct Curl_easy; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 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 | void Curl_global_host_cache_dtor(void); |
| 65 | |
| 66 | struct Curl_dns_entry { |
| 67 | Curl_addrinfo *addr; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 68 | /* timestamp == 0 -- CURLOPT_RESOLVE entry, doesn't timeout */ |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 69 | time_t timestamp; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 70 | /* use-counter, use Curl_resolv_unlock to release reference */ |
| 71 | long inuse; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /* |
| 75 | * Curl_resolv() returns an entry with the info for the specified host |
| 76 | * and port. |
| 77 | * |
| 78 | * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after |
| 79 | * use, or we'll leak memory! |
| 80 | */ |
| 81 | /* return codes */ |
| 82 | #define CURLRESOLV_TIMEDOUT -2 |
| 83 | #define CURLRESOLV_ERROR -1 |
| 84 | #define CURLRESOLV_RESOLVED 0 |
| 85 | #define CURLRESOLV_PENDING 1 |
| 86 | int Curl_resolv(struct connectdata *conn, const char *hostname, |
| 87 | int port, struct Curl_dns_entry **dnsentry); |
| 88 | int Curl_resolv_timeout(struct connectdata *conn, const char *hostname, |
| 89 | int port, struct Curl_dns_entry **dnsentry, |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 90 | time_t timeoutms); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 91 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 92 | #ifdef CURLRES_IPV6 |
| 93 | /* |
| 94 | * Curl_ipv6works() returns TRUE if IPv6 seems to work. |
| 95 | */ |
| 96 | bool Curl_ipv6works(void); |
| 97 | #else |
| 98 | #define Curl_ipv6works() FALSE |
| 99 | #endif |
| 100 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 101 | /* |
| 102 | * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've |
| 103 | * been set and returns TRUE if they are OK. |
| 104 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 105 | bool Curl_ipvalid(struct connectdata *conn); |
| 106 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Curl_getaddrinfo() is the generic low-level name resolve API within this |
| 110 | * source file. There are several versions of this function - for different |
| 111 | * name resolve layers (selected at build-time). They all take this same set |
| 112 | * of arguments |
| 113 | */ |
| 114 | Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, |
| 115 | const char *hostname, |
| 116 | int port, |
| 117 | int *waitp); |
| 118 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 119 | |
| 120 | /* unlock a previously resolved dns entry */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 121 | void Curl_resolv_unlock(struct Curl_easy *data, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 122 | struct Curl_dns_entry *dns); |
| 123 | |
| 124 | /* for debugging purposes only: */ |
| 125 | void Curl_scan_cache_used(void *user, void *ptr); |
| 126 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 127 | /* init a new dns cache and return success */ |
| 128 | int Curl_mk_dnscache(struct curl_hash *hash); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 129 | |
| 130 | /* prune old entries from the DNS cache */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 131 | void Curl_hostcache_prune(struct Curl_easy *data); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 132 | |
Elliott Hughes | 82be86d | 2017-09-20 17:00:17 -0700 | [diff] [blame] | 133 | /* Return # of addresses in a Curl_addrinfo struct */ |
| 134 | int Curl_num_addresses(const Curl_addrinfo *addr); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 135 | |
| 136 | #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) |
| 137 | int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa, |
| 138 | GETNAMEINFO_TYPE_ARG2 salen, |
| 139 | char *host, GETNAMEINFO_TYPE_ARG46 hostlen, |
| 140 | char *serv, GETNAMEINFO_TYPE_ARG46 servlen, |
| 141 | GETNAMEINFO_TYPE_ARG7 flags, |
| 142 | int line, const char *source); |
| 143 | #endif |
| 144 | |
| 145 | /* IPv4 threadsafe resolve function used for synch and asynch builds */ |
Elliott Hughes | cee0338 | 2017-06-23 12:17:18 -0700 | [diff] [blame] | 146 | Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 147 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 148 | CURLcode Curl_async_resolved(struct connectdata *conn, |
| 149 | bool *protocol_connect); |
| 150 | |
| 151 | #ifndef CURLRES_ASYNCH |
| 152 | #define Curl_async_resolved(x,y) CURLE_OK |
| 153 | #endif |
| 154 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 155 | /* |
| 156 | * Curl_addrinfo_callback() is used when we build with any asynch specialty. |
| 157 | * Handles end of async request processing. Inserts ai into hostcache when |
| 158 | * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 159 | * request completed whether successful or failed. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 160 | */ |
| 161 | CURLcode Curl_addrinfo_callback(struct connectdata *conn, |
| 162 | int status, |
| 163 | Curl_addrinfo *ai); |
| 164 | |
| 165 | /* |
| 166 | * Curl_printable_address() returns a printable version of the 1st address |
| 167 | * given in the 'ip' argument. The result will be stored in the buf that is |
| 168 | * bufsize bytes big. |
| 169 | */ |
| 170 | const char *Curl_printable_address(const Curl_addrinfo *ip, |
| 171 | char *buf, size_t bufsize); |
| 172 | |
| 173 | /* |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 174 | * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache. |
| 175 | * |
| 176 | * Returns the Curl_dns_entry entry pointer or NULL if not in the cache. |
| 177 | * |
| 178 | * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after |
| 179 | * use, or we'll leak memory! |
| 180 | */ |
| 181 | struct Curl_dns_entry * |
| 182 | Curl_fetch_addr(struct connectdata *conn, |
| 183 | const char *hostname, |
| 184 | int port); |
Elliott Hughes | 1ef06ba | 2018-05-30 15:43:58 -0700 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Curl_shuffle_addr() shuffles the order of addresses in a 'Curl_addrinfo' |
| 188 | * struct by re-linking its linked list. |
| 189 | * |
| 190 | * The addr argument should be the address of a pointer to the head node of a |
| 191 | * `Curl_addrinfo` list and it will be modified to point to the new head after |
| 192 | * shuffling. |
| 193 | */ |
| 194 | CURLcode Curl_shuffle_addr(struct Curl_easy *data, Curl_addrinfo **addr); |
| 195 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 196 | /* |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 197 | * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache. |
| 198 | * |
| 199 | * Returns the Curl_dns_entry entry pointer or NULL if the storage failed. |
| 200 | */ |
| 201 | struct Curl_dns_entry * |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 202 | Curl_cache_addr(struct Curl_easy *data, Curl_addrinfo *addr, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 203 | const char *hostname, int port); |
| 204 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 205 | #ifndef INADDR_NONE |
| 206 | #define CURL_INADDR_NONE (in_addr_t) ~0 |
| 207 | #else |
| 208 | #define CURL_INADDR_NONE INADDR_NONE |
| 209 | #endif |
| 210 | |
| 211 | #ifdef HAVE_SIGSETJMP |
| 212 | /* Forward-declaration of variable defined in hostip.c. Beware this |
| 213 | * is a global and unique instance. This is used to store the return |
| 214 | * address that we can jump back to from inside a signal handler. |
| 215 | * This is not thread-safe stuff. |
| 216 | */ |
| 217 | extern sigjmp_buf curl_jmpenv; |
| 218 | #endif |
| 219 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 220 | /* |
| 221 | * Function provided by the resolver backend to set DNS servers to use. |
| 222 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 223 | CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 224 | |
| 225 | /* |
| 226 | * Function provided by the resolver backend to set |
| 227 | * outgoing interface to use for DNS requests |
| 228 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 229 | CURLcode Curl_set_dns_interface(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 230 | const char *interf); |
| 231 | |
| 232 | /* |
| 233 | * Function provided by the resolver backend to set |
| 234 | * local IPv4 address to use as source address for DNS requests |
| 235 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 236 | CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 237 | const char *local_ip4); |
| 238 | |
| 239 | /* |
| 240 | * Function provided by the resolver backend to set |
| 241 | * local IPv6 address to use as source address for DNS requests |
| 242 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 243 | CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data, |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 244 | const char *local_ip6); |
| 245 | |
| 246 | /* |
| 247 | * Clean off entries from the cache |
| 248 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 249 | void Curl_hostcache_clean(struct Curl_easy *data, struct curl_hash *hash); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * Destroy the hostcache of this handle. |
| 253 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 254 | void Curl_hostcache_destroy(struct Curl_easy *data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 255 | |
| 256 | /* |
| 257 | * Populate the cache with specified entries from CURLOPT_RESOLVE. |
| 258 | */ |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 259 | CURLcode Curl_loadhostpairs(struct Curl_easy *data); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 260 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 261 | #endif /* HEADER_CURL_HOSTIP_H */ |