Git init
[external/curl.git] / lib / hostip.h
1 #ifndef HEADER_CURL_HOSTIP_H
2 #define HEADER_CURL_HOSTIP_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2010, 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  ***************************************************************************/
24
25 #include "setup.h"
26 #include "hash.h"
27 #include "curl_addrinfo.h"
28
29 #ifdef HAVE_SETJMP_H
30 #include <setjmp.h>
31 #endif
32
33 #ifdef NETWARE
34 #undef in_addr_t
35 #define in_addr_t unsigned long
36 #endif
37
38 /*
39  * Comfortable CURLRES_* definitions are included from setup.h
40  */
41
42 #ifdef USE_ARES
43 #include <ares_version.h>
44 #endif
45
46 /* Allocate enough memory to hold the full name information structs and
47  * everything. OSF1 is known to require at least 8872 bytes. The buffer
48  * required for storing all possible aliases and IP numbers is according to
49  * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
50  */
51 #define CURL_HOSTENT_SIZE 9000
52
53 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
54                                     many seconds for a name resolve */
55
56 #ifdef CURLRES_ARES
57 #define CURL_ASYNC_SUCCESS ARES_SUCCESS
58 #if ARES_VERSION >= 0x010500
59 /* c-ares 1.5.0 or later, the callback proto is modified */
60 #define HAVE_CARES_CALLBACK_TIMEOUTS 1
61 #endif
62 #else
63 #define CURL_ASYNC_SUCCESS CURLE_OK
64 #define ares_cancel(x) do {} while(0)
65 #define ares_destroy(x) do {} while(0)
66 #endif
67
68 struct addrinfo;
69 struct hostent;
70 struct SessionHandle;
71 struct connectdata;
72
73 /*
74  * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
75  * Global DNS cache is general badness. Do not use. This will be removed in
76  * a future version. Use the share interface instead!
77  *
78  * Returns a struct curl_hash pointer on success, NULL on failure.
79  */
80 struct curl_hash *Curl_global_host_cache_init(void);
81 void Curl_global_host_cache_dtor(void);
82
83 struct Curl_dns_entry {
84   Curl_addrinfo *addr;
85   /* timestamp == 0 -- entry not in hostcache
86      timestamp != 0 -- entry is in hostcache */
87   time_t timestamp;
88   long inuse;      /* use-counter, make very sure you decrease this
89                       when you're done using the address you received */
90 };
91
92 /*
93  * Curl_resolv() returns an entry with the info for the specified host
94  * and port.
95  *
96  * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
97  * use, or we'll leak memory!
98  */
99 /* return codes */
100 #define CURLRESOLV_TIMEDOUT -2
101 #define CURLRESOLV_ERROR    -1
102 #define CURLRESOLV_RESOLVED  0
103 #define CURLRESOLV_PENDING   1
104 int Curl_resolv(struct connectdata *conn, const char *hostname,
105                 int port, struct Curl_dns_entry **dnsentry);
106 int Curl_resolv_timeout(struct connectdata *conn, const char *hostname,
107                         int port, struct Curl_dns_entry **dnsentry,
108                         long timeoutms);
109
110 /*
111  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
112  * been set and returns TRUE if they are OK.
113  */
114 bool Curl_ipvalid(struct connectdata *conn);
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 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
123                                 const char *hostname,
124                                 int port,
125                                 int *waitp);
126
127 CURLcode Curl_is_resolved(struct connectdata *conn,
128                           struct Curl_dns_entry **dns);
129 CURLcode Curl_wait_for_resolv(struct connectdata *conn,
130                               struct Curl_dns_entry **dnsentry);
131
132 /* Curl_resolv_getsock() is a generic function that exists in multiple
133    versions depending on what name resolve technology we've built to use. The
134    function is called from the multi_getsock() function.  'sock' is a pointer
135    to an array to hold the file descriptors, with 'numsock' being the size of
136    that array (in number of entries). This function is supposed to return
137    bitmask indicating what file descriptors (referring to array indexes in the
138    'sock' array) to wait for, read/write. */
139 int Curl_resolv_getsock(struct connectdata *conn, curl_socket_t *sock,
140                         int numsocks);
141
142 /* unlock a previously resolved dns entry */
143 void Curl_resolv_unlock(struct SessionHandle *data,
144                         struct Curl_dns_entry *dns);
145
146 /* for debugging purposes only: */
147 void Curl_scan_cache_used(void *user, void *ptr);
148
149 /* make a new dns cache and return the handle */
150 struct curl_hash *Curl_mk_dnscache(void);
151
152 /* prune old entries from the DNS cache */
153 void Curl_hostcache_prune(struct SessionHandle *data);
154
155 /* Return # of adresses in a Curl_addrinfo struct */
156 int Curl_num_addresses (const Curl_addrinfo *addr);
157
158 #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
159 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
160                        GETNAMEINFO_TYPE_ARG2 salen,
161                        char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
162                        char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
163                        GETNAMEINFO_TYPE_ARG7 flags,
164                        int line, const char *source);
165 #endif
166
167 /* IPv4 threadsafe resolve function used for synch and asynch builds */
168 Curl_addrinfo *Curl_ipv4_resolve_r(const char * hostname, int port);
169
170 /*
171  * Curl_addrinfo_callback() is used when we build with any asynch specialty.
172  * Handles end of async request processing. Inserts ai into hostcache when
173  * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
174  * request completed wether successfull or failed.
175  */
176 CURLcode Curl_addrinfo_callback(struct connectdata *conn,
177                                 int status,
178                                 Curl_addrinfo *ai);
179
180 /*
181  * Curl_printable_address() returns a printable version of the 1st address
182  * given in the 'ip' argument. The result will be stored in the buf that is
183  * bufsize bytes big.
184  */
185 const char *Curl_printable_address(const Curl_addrinfo *ip,
186                                    char *buf, size_t bufsize);
187
188 /*
189  * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
190  *
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 SessionHandle *data, Curl_addrinfo *addr,
195                 const char *hostname, int port);
196
197 /*
198  * Curl_destroy_thread_data() cleans up async resolver data.
199  * Complementary of ares_destroy.
200  */
201 struct Curl_async; /* forward-declaration */
202 void Curl_destroy_thread_data(struct Curl_async *async);
203
204 #ifndef INADDR_NONE
205 #define CURL_INADDR_NONE (in_addr_t) ~0
206 #else
207 #define CURL_INADDR_NONE INADDR_NONE
208 #endif
209
210 #ifdef HAVE_SIGSETJMP
211 /* Forward-declaration of variable defined in hostip.c. Beware this
212  * is a global and unique instance. This is used to store the return
213  * address that we can jump back to from inside a signal handler.
214  * This is not thread-safe stuff.
215  */
216 extern sigjmp_buf curl_jmpenv;
217 #endif
218
219 #endif /* HEADER_CURL_HOSTIP_H */