Woops, partly revert my previous commit and do it slightly differently instead.
[platform/upstream/curl.git] / lib / hostip.h
1 #ifndef __HOSTIP_H
2 #define __HOSTIP_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2008, 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 http://curl.haxx.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  * $Id$
24  ***************************************************************************/
25
26 #include "setup.h"
27 #include "hash.h"
28
29 #ifdef NETWARE
30 #undef in_addr_t
31 #define in_addr_t unsigned long
32 #endif
33
34 /*
35  * Setup comfortable CURLRES_* defines to use in the host*.c sources.
36  */
37
38 #ifdef USE_ARES
39 #include <ares_version.h>
40 #endif
41
42 #ifdef USE_ARES
43 #define CURLRES_ASYNCH
44 #define CURLRES_ARES
45 #endif
46
47 #ifdef USE_THREADING_GETHOSTBYNAME
48 #define CURLRES_ASYNCH
49 #define CURLRES_THREADED
50 #endif
51
52 #ifdef USE_THREADING_GETADDRINFO
53 #define CURLRES_ASYNCH
54 #define CURLRES_THREADED
55 #endif
56
57 #ifdef ENABLE_IPV6
58 #define CURLRES_IPV6
59 #else
60 #define CURLRES_IPV4
61 #endif
62
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
69 #endif
70 #endif /* IPv4/ares-only */
71
72 #ifndef CURLRES_ASYNCH
73 #define CURLRES_SYNCH
74 #endif
75
76 #ifndef USE_LIBIDN
77 #define CURLRES_IDN
78 #endif
79
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!
84  */
85 #define CURL_HOSTENT_SIZE 9000
86
87 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
88                                     many seconds for a name resolve */
89
90 #ifdef CURLRES_ARES
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
95 #endif
96 #else
97 #define CURL_ASYNC_SUCCESS CURLE_OK
98 #define ares_cancel(x) do {} while(0)
99 #define ares_destroy(x) do {} while(0)
100 #endif
101
102 /*
103  * Curl_addrinfo MUST be used for all name resolved info.
104  */
105 #ifdef CURLRES_IPV6
106 typedef struct addrinfo Curl_addrinfo;
107 #else
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 {
111   int                   ai_flags;
112   int                   ai_family;
113   int                   ai_socktype;
114   int                   ai_protocol;
115   socklen_t             ai_addrlen;   /* Follow rfc3493 struct addrinfo */
116   char                 *ai_canonname;
117   struct sockaddr      *ai_addr;
118   struct Curl_addrinfo *ai_next;
119 };
120 typedef struct Curl_addrinfo Curl_addrinfo;
121 #endif
122
123 struct addrinfo;
124 struct hostent;
125 struct SessionHandle;
126 struct connectdata;
127
128 /*
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!
132  *
133  * Returns a struct curl_hash pointer on success, NULL on failure.
134  */
135 struct curl_hash *Curl_global_host_cache_init(void);
136 void Curl_global_host_cache_dtor(void);
137
138 struct Curl_dns_entry {
139   Curl_addrinfo *addr;
140   time_t timestamp;
141   long inuse;      /* use-counter, make very sure you decrease this
142                       when you're done using the address you received */
143 };
144
145 /*
146  * Curl_resolv() returns an entry with the info for the specified host
147  * and port.
148  *
149  * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
150  * use, or we'll leak memory!
151  */
152 /* return codes */
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);
158
159 /*
160  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
161  * been set and returns TRUE if they are OK.
162  */
163 bool Curl_ipvalid(struct SessionHandle *data);
164
165 /*
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
169  * of arguments
170  */
171 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
172                                 const char *hostname,
173                                 int port,
174                                 int *waitp);
175
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);
180
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,
189                         int numsocks);
190
191 /* unlock a previously resolved dns entry */
192 void Curl_resolv_unlock(struct SessionHandle *data,
193                         struct Curl_dns_entry *dns);
194
195 /* for debugging purposes only: */
196 void Curl_scan_cache_used(void *user, void *ptr);
197
198 /* free name info */
199 void Curl_freeaddrinfo(Curl_addrinfo *freeaddr);
200
201 /* make a new dns cache and return the handle */
202 struct curl_hash *Curl_mk_dnscache(void);
203
204 /* prune old entries from the DNS cache */
205 void Curl_hostcache_prune(struct SessionHandle *data);
206
207 /* Return # of adresses in a Curl_addrinfo struct */
208 int Curl_num_addresses (const Curl_addrinfo *addr);
209
210 #ifdef CURLDEBUG
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);
224 #endif
225 #endif
226
227 /* This is the callback function that is used when we build with asynch
228    resolve, ipv4 */
229 CURLcode Curl_addrinfo4_callback(void *arg,
230                                  int status,
231 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
232                                  int timeouts,
233 #endif
234                                  struct hostent *hostent);
235 /* This is the callback function that is used when we build with asynch
236    resolve, ipv6 */
237 CURLcode Curl_addrinfo6_callback(void *arg,
238                                  int status,
239 #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
240                                  int timeouts,
241 #endif
242                                  struct addrinfo *ai);
243
244
245 /* [ipv4/ares only] Creates a Curl_addrinfo struct from a numerical-only IP
246    address */
247 Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port);
248
249 /* [ipv4/ares only] Curl_he2ai() converts a struct hostent to a Curl_addrinfo chain
250    and returns it */
251 Curl_addrinfo *Curl_he2ai(const struct hostent *, int port);
252
253 /* Clone a Curl_addrinfo struct, works protocol independently */
254 Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port);
255
256 /*
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
259  * bufsize bytes big.
260  */
261 const char *Curl_printable_address(const Curl_addrinfo *ip,
262                                    char *buf, size_t bufsize);
263
264 /*
265  * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
266  *
267  * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
268  */
269 struct Curl_dns_entry *
270 Curl_cache_addr(struct SessionHandle *data, Curl_addrinfo *addr,
271                 const char *hostname, int port);
272
273 /*
274  * Curl_destroy_thread_data() cleans up async resolver data.
275  * Complementary of ares_destroy.
276  */
277 struct Curl_async; /* forward-declaration */
278 void Curl_destroy_thread_data(struct Curl_async *async);
279
280 #ifndef INADDR_NONE
281 #define CURL_INADDR_NONE (in_addr_t) ~0
282 #else
283 #define CURL_INADDR_NONE INADDR_NONE
284 #endif
285
286
287
288
289 #endif