2
0

hostip.h 8.1 KB

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