3 /***************************************************************************
5 * Project ___| | | | _ \| |
7 * | (__| |_| | _ <| |___
8 * \___|\___/|_| \_\_____|
10 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://curl.haxx.se/docs/copyright.html.
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.
20 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21 * KIND, either express or implied.
24 ***************************************************************************/
31 #define in_addr_t unsigned long
35 * Setup comfortable CURLRES_* defines to use in the host*.c sources.
39 #include <ares_version.h>
43 #define CURLRES_ASYNCH
47 #ifdef USE_THREADING_GETHOSTBYNAME
48 #define CURLRES_ASYNCH
49 #define CURLRES_THREADED
52 #ifdef USE_THREADING_GETADDRINFO
53 #define CURLRES_ASYNCH
54 #define CURLRES_THREADED
63 #if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
64 #if !defined(HAVE_GETHOSTBYNAME_R) || defined(CURLRES_ASYNCH)
65 /* If built for ipv4 and missing gethostbyname_r(), or if using async name
66 resolve, we need the Curl_addrinfo_copy() function (which itself needs the
67 Curl_he2ai() function)) */
68 #define CURLRES_ADDRINFO_COPY
70 #endif /* IPv4/ares-only */
72 #ifndef CURLRES_ASYNCH
80 /* Allocate enough memory to hold the full name information structs and
81 * everything. OSF1 is known to require at least 8872 bytes. The buffer
82 * required for storing all possible aliases and IP numbers is according to
83 * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
85 #define CURL_HOSTENT_SIZE 9000
87 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
88 many seconds for a name resolve */
91 #define CURL_ASYNC_SUCCESS ARES_SUCCESS
92 #if ARES_VERSION >= 0x010500
93 /* c-ares 1.5.0 or later, the callback proto is modified */
94 #define HAVE_CARES_CALLBACK_TIMEOUTS 1
97 #define CURL_ASYNC_SUCCESS CURLE_OK
98 #define ares_cancel(x) do {} while(0)
99 #define ares_destroy(x) do {} while(0)
103 * Curl_addrinfo MUST be used for all name resolved info.
106 typedef struct addrinfo Curl_addrinfo;
108 /* OK, so some ipv4-only include tree probably have the addrinfo struct, but
109 to work even on those that don't, we provide our own look-alike! */
110 struct Curl_addrinfo {
115 socklen_t ai_addrlen; /* Follow rfc3493 struct addrinfo */
117 struct sockaddr *ai_addr;
118 struct Curl_addrinfo *ai_next;
120 typedef struct Curl_addrinfo Curl_addrinfo;
125 struct SessionHandle;
129 * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
130 * Global DNS cache is general badness. Do not use. This will be removed in
131 * a future version. Use the share interface instead!
133 * Returns a struct curl_hash pointer on success, NULL on failure.
135 struct curl_hash *Curl_global_host_cache_init(void);
136 void Curl_global_host_cache_dtor(void);
138 struct Curl_dns_entry {
141 long inuse; /* use-counter, make very sure you decrease this
142 when you're done using the address you received */
146 * Curl_resolv() returns an entry with the info for the specified host
149 * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
150 * use, or we'll leak memory!
153 #define CURLRESOLV_ERROR -1
154 #define CURLRESOLV_RESOLVED 0
155 #define CURLRESOLV_PENDING 1
156 int Curl_resolv(struct connectdata *conn, const char *hostname,
157 int port, struct Curl_dns_entry **dnsentry);
160 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
161 * been set and returns TRUE if they are OK.
163 bool Curl_ipvalid(struct SessionHandle *data);
166 * Curl_getaddrinfo() is the generic low-level name resolve API within this
167 * source file. There are several versions of this function - for different
168 * name resolve layers (selected at build-time). They all take this same set
171 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
172 const char *hostname,
176 CURLcode Curl_is_resolved(struct connectdata *conn,
177 struct Curl_dns_entry **dns);
178 CURLcode Curl_wait_for_resolv(struct connectdata *conn,
179 struct Curl_dns_entry **dnsentry);
181 /* Curl_resolv_getsock() is a generic function that exists in multiple
182 versions depending on what name resolve technology we've built to use. The
183 function is called from the multi_getsock() function. 'sock' is a pointer
184 to an array to hold the file descriptors, with 'numsock' being the size of
185 that array (in number of entries). This function is supposed to return
186 bitmask indicating what file descriptors (referring to array indexes in the
187 'sock' array) to wait for, read/write. */
188 int Curl_resolv_getsock(struct connectdata *conn, curl_socket_t *sock,
191 /* unlock a previously resolved dns entry */
192 void Curl_resolv_unlock(struct SessionHandle *data,
193 struct Curl_dns_entry *dns);
195 /* for debugging purposes only: */
196 void Curl_scan_cache_used(void *user, void *ptr);
199 void Curl_freeaddrinfo(Curl_addrinfo *freeaddr);
201 /* make a new dns cache and return the handle */
202 struct curl_hash *Curl_mk_dnscache(void);
204 /* prune old entries from the DNS cache */
205 void Curl_hostcache_prune(struct SessionHandle *data);
207 /* Return # of adresses in a Curl_addrinfo struct */
208 int Curl_num_addresses (const Curl_addrinfo *addr);
211 void curl_dofreeaddrinfo(struct addrinfo *freethis,
212 int line, const char *source);
213 int curl_dogetaddrinfo(const char *hostname, const char *service,
214 struct addrinfo *hints,
215 struct addrinfo **result,
216 int line, const char *source);
217 #ifdef HAVE_GETNAMEINFO
218 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
219 GETNAMEINFO_TYPE_ARG2 salen,
220 char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
221 char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
222 GETNAMEINFO_TYPE_ARG7 flags,
223 int line, const char *source);
227 /* This is the callback function that is used when we build with asynch
229 CURLcode Curl_addrinfo4_callback(void *arg,
231 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
234 struct hostent *hostent);
235 /* This is the callback function that is used when we build with asynch
237 CURLcode Curl_addrinfo6_callback(void *arg,
239 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
242 struct addrinfo *ai);
245 /* [ipv4/ares only] Creates a Curl_addrinfo struct from a numerical-only IP
247 Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port);
249 /* [ipv4/ares only] Curl_he2ai() converts a struct hostent to a Curl_addrinfo chain
251 Curl_addrinfo *Curl_he2ai(const struct hostent *, int port);
253 /* Clone a Curl_addrinfo struct, works protocol independently */
254 Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port);
257 * Curl_printable_address() returns a printable version of the 1st address
258 * given in the 'ip' argument. The result will be stored in the buf that is
261 const char *Curl_printable_address(const Curl_addrinfo *ip,
262 char *buf, size_t bufsize);
265 * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
267 * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
269 struct Curl_dns_entry *
270 Curl_cache_addr(struct SessionHandle *data, Curl_addrinfo *addr,
271 const char *hostname, int port);
274 * Curl_destroy_thread_data() cleans up async resolver data.
275 * Complementary of ares_destroy.
277 struct Curl_async; /* forward-declaration */
278 void Curl_destroy_thread_data(struct Curl_async *async);
281 #define CURL_INADDR_NONE (in_addr_t) ~0
283 #define CURL_INADDR_NONE INADDR_NONE