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