Base code merged to SPIN 2.4
[platform/upstream/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 - 2015, 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 "curl_setup.h"
26 #include "hash.h"
27 #include "curl_addrinfo.h"
28 #include "asyn.h"
29
30 #ifdef HAVE_SETJMP_H
31 #include <setjmp.h>
32 #endif
33
34 #ifdef NETWARE
35 #undef in_addr_t
36 #define in_addr_t unsigned long
37 #endif
38
39 /* Allocate enough memory to hold the full name information structs and
40  * everything. OSF1 is known to require at least 8872 bytes. The buffer
41  * required for storing all possible aliases and IP numbers is according to
42  * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
43  */
44 #define CURL_HOSTENT_SIZE 9000
45
46 #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
47                                     many seconds for a name resolve */
48
49 #define CURL_ASYNC_SUCCESS CURLE_OK
50
51 struct addrinfo;
52 struct hostent;
53 struct SessionHandle;
54 struct connectdata;
55
56 /*
57  * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
58  * Global DNS cache is general badness. Do not use. This will be removed in
59  * a future version. Use the share interface instead!
60  *
61  * Returns a struct curl_hash pointer on success, NULL on failure.
62  */
63 struct curl_hash *Curl_global_host_cache_init(void);
64 void Curl_global_host_cache_dtor(void);
65
66 struct Curl_dns_entry {
67   Curl_addrinfo *addr;
68   /* timestamp == 0 -- entry not in hostcache
69      timestamp != 0 -- entry is in hostcache */
70   time_t timestamp;
71   long inuse;      /* use-counter, make very sure you decrease this
72                       when you're done using the address you received */
73 };
74
75 /*
76  * Curl_resolv() returns an entry with the info for the specified host
77  * and port.
78  *
79  * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
80  * use, or we'll leak memory!
81  */
82 /* return codes */
83 #define CURLRESOLV_TIMEDOUT -2
84 #define CURLRESOLV_ERROR    -1
85 #define CURLRESOLV_RESOLVED  0
86 #define CURLRESOLV_PENDING   1
87 int Curl_resolv(struct connectdata *conn, const char *hostname,
88                 int port, struct Curl_dns_entry **dnsentry);
89 int Curl_resolv_timeout(struct connectdata *conn, const char *hostname,
90                         int port, struct Curl_dns_entry **dnsentry,
91                         long timeoutms);
92
93 #ifdef CURLRES_IPV6
94 /*
95  * Curl_ipv6works() returns TRUE if IPv6 seems to work.
96  */
97 bool Curl_ipv6works(void);
98 #else
99 #define Curl_ipv6works() FALSE
100 #endif
101
102 /*
103  * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
104  * been set and returns TRUE if they are OK.
105  */
106 bool Curl_ipvalid(struct connectdata *conn);
107
108
109 /*
110  * Curl_getaddrinfo() is the generic low-level name resolve API within this
111  * source file. There are several versions of this function - for different
112  * name resolve layers (selected at build-time). They all take this same set
113  * of arguments
114  */
115 Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
116                                 const char *hostname,
117                                 int port,
118                                 int *waitp);
119
120
121 /* unlock a previously resolved dns entry */
122 void Curl_resolv_unlock(struct SessionHandle *data,
123                         struct Curl_dns_entry *dns);
124
125 /* for debugging purposes only: */
126 void Curl_scan_cache_used(void *user, void *ptr);
127
128 /* make a new dns cache and return the handle */
129 struct curl_hash *Curl_mk_dnscache(void);
130
131 /* prune old entries from the DNS cache */
132 void Curl_hostcache_prune(struct SessionHandle *data);
133
134 /* Return # of adresses in a Curl_addrinfo struct */
135 int Curl_num_addresses (const Curl_addrinfo *addr);
136
137 #if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
138 int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
139                        GETNAMEINFO_TYPE_ARG2 salen,
140                        char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
141                        char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
142                        GETNAMEINFO_TYPE_ARG7 flags,
143                        int line, const char *source);
144 #endif
145
146 /* IPv4 threadsafe resolve function used for synch and asynch builds */
147 Curl_addrinfo *Curl_ipv4_resolve_r(const char * hostname, int port);
148
149 CURLcode Curl_async_resolved(struct connectdata *conn,
150                              bool *protocol_connect);
151
152 #ifndef CURLRES_ASYNCH
153 #define Curl_async_resolved(x,y) CURLE_OK
154 #endif
155
156 /*
157  * Curl_addrinfo_callback() is used when we build with any asynch specialty.
158  * Handles end of async request processing. Inserts ai into hostcache when
159  * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
160  * request completed whether successful or failed.
161  */
162 CURLcode Curl_addrinfo_callback(struct connectdata *conn,
163                                 int status,
164                                 Curl_addrinfo *ai);
165
166 /*
167  * Curl_printable_address() returns a printable version of the 1st address
168  * given in the 'ip' argument. The result will be stored in the buf that is
169  * bufsize bytes big.
170  */
171 const char *Curl_printable_address(const Curl_addrinfo *ip,
172                                    char *buf, size_t bufsize);
173
174 /*
175  * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
176  *
177  * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
178  */
179 struct Curl_dns_entry *
180 Curl_fetch_addr(struct connectdata *conn,
181                 const char *hostname,
182                 int port);
183 /*
184  * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
185  *
186  * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
187  */
188 struct Curl_dns_entry *
189 Curl_cache_addr(struct SessionHandle *data, Curl_addrinfo *addr,
190                 const char *hostname, int port);
191
192 #ifndef INADDR_NONE
193 #define CURL_INADDR_NONE (in_addr_t) ~0
194 #else
195 #define CURL_INADDR_NONE INADDR_NONE
196 #endif
197
198 #ifdef HAVE_SIGSETJMP
199 /* Forward-declaration of variable defined in hostip.c. Beware this
200  * is a global and unique instance. This is used to store the return
201  * address that we can jump back to from inside a signal handler.
202  * This is not thread-safe stuff.
203  */
204 extern sigjmp_buf curl_jmpenv;
205 #endif
206
207 /*
208  * Function provided by the resolver backend to set DNS servers to use.
209  */
210 CURLcode Curl_set_dns_servers(struct SessionHandle *data, char *servers);
211
212 /*
213  * Function provided by the resolver backend to set
214  * outgoing interface to use for DNS requests
215  */
216 CURLcode Curl_set_dns_interface(struct SessionHandle *data,
217                                 const char *interf);
218
219 /*
220  * Function provided by the resolver backend to set
221  * local IPv4 address to use as source address for DNS requests
222  */
223 CURLcode Curl_set_dns_local_ip4(struct SessionHandle *data,
224                                 const char *local_ip4);
225
226 /*
227  * Function provided by the resolver backend to set
228  * local IPv6 address to use as source address for DNS requests
229  */
230 CURLcode Curl_set_dns_local_ip6(struct SessionHandle *data,
231                                 const char *local_ip6);
232
233 /*
234  * Clean off entries from the cache
235  */
236 void Curl_hostcache_clean(struct SessionHandle *data, struct curl_hash *hash);
237
238 /*
239  * Destroy the hostcache of this handle.
240  */
241 void Curl_hostcache_destroy(struct SessionHandle *data);
242
243 /*
244  * Populate the cache with specified entries from CURLOPT_RESOLVE.
245  */
246 CURLcode Curl_loadhostpairs(struct SessionHandle *data);
247
248 #endif /* HEADER_CURL_HOSTIP_H */