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/* Allocate enough memory to hold the full name information structs and
36 * everything. OSF1 is known to require at least 8872 bytes. The buffer
37 * required for storing all possible aliases and IP numbers is according to
38 * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
39 */
40#define CURL_HOSTENT_SIZE 9000
41
42#define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
43 many seconds for a name resolve */
44
45#define CURL_ASYNC_SUCCESS CURLE_OK
46
47struct addrinfo;
48struct hostent;
49struct Curl_easy;
50struct connectdata;
51
52/*
53 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
54 * Global DNS cache is general badness. Do not use. This will be removed in
55 * a future version. Use the share interface instead!
56 *
57 * Returns a struct Curl_hash pointer on success, NULL on failure.
58 */
59struct Curl_hash *Curl_global_host_cache_init(void);
60
61struct Curl_dns_entry {
62 struct Curl_addrinfo *addr;
63 /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (doesn't time out) */
64 time_t timestamp;
65 /* use-counter, use Curl_resolv_unlock to release reference */
66 long inuse;
67};
68
69bool Curl_host_is_ipnum(const char *hostname);
70
71/*
72 * Curl_resolv() returns an entry with the info for the specified host
73 * and port.
74 *
75 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
76 * use, or we'll leak memory!
77 */
78/* return codes */
79enum resolve_t {
80 CURLRESOLV_TIMEDOUT = -2,
81 CURLRESOLV_ERROR = -1,
82 CURLRESOLV_RESOLVED = 0,
83 CURLRESOLV_PENDING = 1
84};
85enum resolve_t Curl_resolv(struct Curl_easy *data,
86 const char *hostname,
87 int port,
88 bool allowDOH,
89 struct Curl_dns_entry **dnsentry);
90enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
91 const char *hostname, int port,
92 struct Curl_dns_entry **dnsentry,
93 timediff_t timeoutms);
94
95#ifdef ENABLE_IPV6
96/*
97 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
98 */
99bool Curl_ipv6works(struct Curl_easy *data);
100#else
101#define Curl_ipv6works(x) FALSE
102#endif
103
104/*
105 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
106 * been set and returns TRUE if they are OK.
107 */
108bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn);
109
110
111/*
112 * Curl_getaddrinfo() is the generic low-level name resolve API within this
113 * source file. There are several versions of this function - for different
114 * name resolve layers (selected at build-time). They all take this same set
115 * of arguments
116 */
117struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
118 const char *hostname,
119 int port,
120 int *waitp);
121
122
123/* unlock a previously resolved dns entry */
124void Curl_resolv_unlock(struct Curl_easy *data,
125 struct Curl_dns_entry *dns);
126
127/* init a new dns cache */
128void Curl_init_dnscache(struct Curl_hash *hash, int hashsize);
129
130/* prune old entries from the DNS cache */
131void Curl_hostcache_prune(struct Curl_easy *data);
132
133/* IPv4 threadsafe resolve function used for synch and asynch builds */
134struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port);
135
136CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect);
137
138/*
139 * Curl_addrinfo_callback() is used when we build with any asynch specialty.
140 * Handles end of async request processing. Inserts ai into hostcache when
141 * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
142 * request completed whether successful or failed.
143 */
144CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
145 int status,
146 struct Curl_addrinfo *ai);
147
148/*
149 * Curl_printable_address() returns a printable version of the 1st address
150 * given in the 'ip' argument. The result will be stored in the buf that is
151 * bufsize bytes big.
152 */
153void Curl_printable_address(const struct Curl_addrinfo *ip,
154 char *buf, size_t bufsize);
155
156/*
157 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
158 *
159 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
160 *
161 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
162 * use, or we'll leak memory!
163 */
164struct Curl_dns_entry *
165Curl_fetch_addr(struct Curl_easy *data,
166 const char *hostname,
167 int port);
168
169/*
170 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
171 *
172 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
173 */
174struct Curl_dns_entry *
175Curl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr,
176 const char *hostname, size_t hostlen, int port);
177
178#ifndef INADDR_NONE
179#define CURL_INADDR_NONE (in_addr_t) ~0
180#else
181#define CURL_INADDR_NONE INADDR_NONE
182#endif
183
184/*
185 * Function provided by the resolver backend to set DNS servers to use.
186 */
187CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers);
188
189/*
190 * Function provided by the resolver backend to set
191 * outgoing interface to use for DNS requests
192 */
193CURLcode Curl_set_dns_interface(struct Curl_easy *data,
194 const char *interf);
195
196/*
197 * Function provided by the resolver backend to set
198 * local IPv4 address to use as source address for DNS requests
199 */
200CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
201 const char *local_ip4);
202
203/*
204 * Function provided by the resolver backend to set
205 * local IPv6 address to use as source address for DNS requests
206 */
207CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
208 const char *local_ip6);
209
210/*
211 * Clean off entries from the cache
212 */
213void Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash);
214
215/*
216 * Populate the cache with specified entries from CURLOPT_RESOLVE.
217 */
218CURLcode Curl_loadhostpairs(struct Curl_easy *data);
219CURLcode Curl_resolv_check(struct Curl_easy *data,
220 struct Curl_dns_entry **dns);
221int Curl_resolv_getsock(struct Curl_easy *data,
222 curl_socket_t *socks);
223
224CURLcode Curl_resolver_error(struct Curl_easy *data);
225#endif /* HEADER_CURL_HOSTIP_H */
226