1#ifndef HEADER_CURL_HOSTIP_H
2#define HEADER_CURL_HOSTIP_H
3/***************************************************************************
4 * _ _ ____ _
5 * Project ___| | | | _ \| |
6 * / __| | | | |_) | |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 1998 - 2022, Daniel Stenberg, <[email protected]>, 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#ifdef HAVE_SETJMP_H
34#include <setjmp.h>
35#endif
36
37#ifdef NETWARE
38#undef in_addr_t
39#define in_addr_t unsigned long
40#endif
41
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!
46 */
47#define CURL_HOSTENT_SIZE 9000
48
49#define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
50 many seconds for a name resolve */
51
52#define CURL_ASYNC_SUCCESS CURLE_OK
53
54struct addrinfo;
55struct hostent;
56struct Curl_easy;
57struct connectdata;
58
59/*
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!
63 *
64 * Returns a struct Curl_hash pointer on success, NULL on failure.
65 */
66struct Curl_hash *Curl_global_host_cache_init(void);
67
68struct Curl_dns_entry {
69 struct Curl_addrinfo *addr;
70 /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (doesn't time out) */
71 time_t timestamp;
72 /* use-counter, use Curl_resolv_unlock to release reference */
73 long inuse;
74};
75
76bool Curl_host_is_ipnum(const char *hostname);
77
78/*
79 * Curl_resolv() returns an entry with the info for the specified host
80 * and port.
81 *
82 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
83 * use, or we'll leak memory!
84 */
85/* return codes */
86enum resolve_t {
87 CURLRESOLV_TIMEDOUT = -2,
88 CURLRESOLV_ERROR = -1,
89 CURLRESOLV_RESOLVED = 0,
90 CURLRESOLV_PENDING = 1
91};
92enum resolve_t Curl_resolv(struct Curl_easy *data,
93 const char *hostname,
94 int port,
95 bool allowDOH,
96 struct Curl_dns_entry **dnsentry);
97enum 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);
101
102#ifdef ENABLE_IPV6
103/*
104 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
105 */
106bool Curl_ipv6works(struct Curl_easy *data);
107#else
108#define Curl_ipv6works(x) FALSE
109#endif
110
111/*
112 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
113 * been set and returns TRUE if they are OK.
114 */
115bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn);
116
117
118/*
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
122 * of arguments
123 */
124struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
125 const char *hostname,
126 int port,
127 int *waitp);
128
129
130/* unlock a previously resolved dns entry */
131void Curl_resolv_unlock(struct Curl_easy *data,
132 struct Curl_dns_entry *dns);
133
134/* init a new dns cache */
135void Curl_init_dnscache(struct Curl_hash *hash, int hashsize);
136
137/* prune old entries from the DNS cache */
138void Curl_hostcache_prune(struct Curl_easy *data);
139
140/* Return # of addresses in a Curl_addrinfo struct */
141int Curl_num_addresses(const struct Curl_addrinfo *addr);
142
143/* IPv4 threadsafe resolve function used for synch and asynch builds */
144struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port);
145
146CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect);
147
148/*
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.
153 */
154CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
155 int status,
156 struct Curl_addrinfo *ai);
157
158/*
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
161 * bufsize bytes big.
162 */
163void Curl_printable_address(const struct Curl_addrinfo *ip,
164 char *buf, size_t bufsize);
165
166/*
167 * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
168 *
169 * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
170 *
171 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
172 * use, or we'll leak memory!
173 */
174struct Curl_dns_entry *
175Curl_fetch_addr(struct Curl_easy *data,
176 const char *hostname,
177 int port);
178
179/*
180 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
181 *
182 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
183 */
184struct Curl_dns_entry *
185Curl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr,
186 const char *hostname, int port);
187
188#ifndef INADDR_NONE
189#define CURL_INADDR_NONE (in_addr_t) ~0
190#else
191#define CURL_INADDR_NONE INADDR_NONE
192#endif
193
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.
199 */
200extern sigjmp_buf curl_jmpenv;
201#endif
202
203/*
204 * Function provided by the resolver backend to set DNS servers to use.
205 */
206CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers);
207
208/*
209 * Function provided by the resolver backend to set
210 * outgoing interface to use for DNS requests
211 */
212CURLcode Curl_set_dns_interface(struct Curl_easy *data,
213 const char *interf);
214
215/*
216 * Function provided by the resolver backend to set
217 * local IPv4 address to use as source address for DNS requests
218 */
219CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
220 const char *local_ip4);
221
222/*
223 * Function provided by the resolver backend to set
224 * local IPv6 address to use as source address for DNS requests
225 */
226CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
227 const char *local_ip6);
228
229/*
230 * Clean off entries from the cache
231 */
232void Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash);
233
234/*
235 * Populate the cache with specified entries from CURLOPT_RESOLVE.
236 */
237CURLcode Curl_loadhostpairs(struct Curl_easy *data);
238CURLcode Curl_resolv_check(struct Curl_easy *data,
239 struct Curl_dns_entry **dns);
240int Curl_resolv_getsock(struct Curl_easy *data,
241 curl_socket_t *socks);
242
243CURLcode Curl_resolver_error(struct Curl_easy *data);
244#endif /* HEADER_CURL_HOSTIP_H */
245