• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "httpsrr.h"
33 
34 #include <setjmp.h>
35 
36 #ifdef USE_HTTPSRR
37 # include <stdint.h>
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 enum alpnid {
58   ALPN_none = 0,
59   ALPN_h1 = CURLALTSVC_H1,
60   ALPN_h2 = CURLALTSVC_H2,
61   ALPN_h3 = CURLALTSVC_H3
62 };
63 
64 /*
65  * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
66  * Global DNS cache is general badness. Do not use. This will be removed in
67  * a future version. Use the share interface instead!
68  *
69  * Returns a struct Curl_hash pointer on success, NULL on failure.
70  */
71 struct Curl_hash *Curl_global_host_cache_init(void);
72 
73 struct Curl_dns_entry {
74   struct Curl_addrinfo *addr;
75 #ifdef USE_HTTPSRR
76   struct Curl_https_rrinfo *hinfo;
77 #endif
78   /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (does not time out) */
79   time_t timestamp;
80   /* reference counter, entry is freed on reaching 0 */
81   size_t refcount;
82   /* hostname port number that resolved to addr. */
83   int hostport;
84   /* hostname that resolved to addr. may be NULL (Unix domain sockets). */
85   char hostname[1];
86 };
87 
88 bool Curl_host_is_ipnum(const char *hostname);
89 
90 /*
91  * Curl_resolv() returns an entry with the info for the specified host
92  * and port.
93  *
94  * The returned data *MUST* be "released" with Curl_resolv_unlink() after
95  * use, or we will leak memory!
96  */
97 /* return codes */
98 enum resolve_t {
99   CURLRESOLV_TIMEDOUT = -2,
100   CURLRESOLV_ERROR    = -1,
101   CURLRESOLV_RESOLVED =  0,
102   CURLRESOLV_PENDING  =  1
103 };
104 enum resolve_t Curl_resolv(struct Curl_easy *data,
105                            const char *hostname,
106                            int port,
107                            bool allowDOH,
108                            struct Curl_dns_entry **dnsentry);
109 enum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
110                                    const char *hostname, int port,
111                                    struct Curl_dns_entry **dnsentry,
112                                    timediff_t timeoutms);
113 
114 #ifdef USE_IPV6
115 /*
116  * Curl_ipv6works() returns TRUE if IPv6 seems to work.
117  */
118 bool Curl_ipv6works(struct Curl_easy *data);
119 #else
120 #define Curl_ipv6works(x) FALSE
121 #endif
122 
123 /*
124  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
125  * been set and returns TRUE if they are OK.
126  */
127 bool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn);
128 
129 
130 /*
131  * Curl_getaddrinfo() is the generic low-level name resolve API within this
132  * source file. There are several versions of this function - for different
133  * name resolve layers (selected at build-time). They all take this same set
134  * of arguments
135  */
136 struct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
137                                        const char *hostname,
138                                        int port,
139                                        int *waitp);
140 
141 
142 /* unlink a dns entry, potentially shared with a cache */
143 void Curl_resolv_unlink(struct Curl_easy *data,
144                         struct Curl_dns_entry **pdns);
145 
146 /* init a new dns cache */
147 void Curl_init_dnscache(struct Curl_hash *hash, size_t hashsize);
148 
149 /* prune old entries from the DNS cache */
150 void Curl_hostcache_prune(struct Curl_easy *data);
151 
152 /* IPv4 threadsafe resolve function used for synch and asynch builds */
153 struct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port);
154 
155 CURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect);
156 
157 /*
158  * Curl_addrinfo_callback() is used when we build with any asynch specialty.
159  * Handles end of async request processing. Inserts ai into hostcache when
160  * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
161  * request completed whether successful or failed.
162  */
163 CURLcode Curl_addrinfo_callback(struct Curl_easy *data,
164                                 int status,
165                                 struct Curl_addrinfo *ai);
166 
167 /*
168  * Curl_printable_address() returns a printable version of the 1st address
169  * given in the 'ip' argument. The result will be stored in the buf that is
170  * bufsize bytes big.
171  */
172 void Curl_printable_address(const struct Curl_addrinfo *ip,
173                             char *buf, size_t bufsize);
174 
175 /*
176  * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
177  *
178  * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
179  *
180  * The returned data *MUST* be "released" with Curl_resolv_unlink() after
181  * use, or we will leak memory!
182  */
183 struct Curl_dns_entry *
184 Curl_fetch_addr(struct Curl_easy *data,
185                 const char *hostname,
186                 int port);
187 
188 /*
189  * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
190  * @param permanent   iff TRUE, entry will never become stale
191  * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
192  */
193 struct Curl_dns_entry *
194 Curl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr,
195                 const char *hostname, size_t hostlen, int port,
196                 bool permanent);
197 
198 #ifndef INADDR_NONE
199 #define CURL_INADDR_NONE (in_addr_t) ~0
200 #else
201 #define CURL_INADDR_NONE INADDR_NONE
202 #endif
203 
204 /*
205  * Function provided by the resolver backend to set DNS servers to use.
206  */
207 CURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers);
208 
209 /*
210  * Function provided by the resolver backend to set
211  * outgoing interface to use for DNS requests
212  */
213 CURLcode Curl_set_dns_interface(struct Curl_easy *data,
214                                 const char *interf);
215 
216 /*
217  * Function provided by the resolver backend to set
218  * local IPv4 address to use as source address for DNS requests
219  */
220 CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
221                                 const char *local_ip4);
222 
223 /*
224  * Function provided by the resolver backend to set
225  * local IPv6 address to use as source address for DNS requests
226  */
227 CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
228                                 const char *local_ip6);
229 
230 /*
231  * Clean off entries from the cache
232  */
233 void Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash);
234 
235 /*
236  * Populate the cache with specified entries from CURLOPT_RESOLVE.
237  */
238 CURLcode Curl_loadhostpairs(struct Curl_easy *data);
239 CURLcode Curl_resolv_check(struct Curl_easy *data,
240                            struct Curl_dns_entry **dns);
241 int Curl_resolv_getsock(struct Curl_easy *data,
242                         curl_socket_t *socks);
243 
244 CURLcode Curl_resolver_error(struct Curl_easy *data);
245 #endif /* HEADER_CURL_HOSTIP_H */
246