hostip.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. #ifndef __HOSTIP_H
  2. #define __HOSTIP_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) 1998 - 2008, Daniel Stenberg, <[email protected]>, 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. #include "setup.h"
  26. #include "hash.h"
  27. #ifdef NETWARE
  28. #undef in_addr_t
  29. #define in_addr_t unsigned long
  30. #endif
  31. /*
  32. * Setup comfortable CURLRES_* defines to use in the host*.c sources.
  33. */
  34. #ifdef USE_ARES
  35. #include <ares_version.h>
  36. #endif
  37. #ifdef USE_ARES
  38. #define CURLRES_ASYNCH
  39. #define CURLRES_ARES
  40. #endif
  41. #ifdef USE_THREADING_GETHOSTBYNAME
  42. #define CURLRES_ASYNCH
  43. #define CURLRES_THREADED
  44. #endif
  45. #ifdef USE_THREADING_GETADDRINFO
  46. #define CURLRES_ASYNCH
  47. #define CURLRES_THREADED
  48. #endif
  49. #ifdef ENABLE_IPV6
  50. #define CURLRES_IPV6
  51. #else
  52. #define CURLRES_IPV4
  53. #endif
  54. #if defined(CURLRES_IPV4) || defined(CURLRES_ARES)
  55. #if !defined(HAVE_GETHOSTBYNAME_R) || defined(CURLRES_ASYNCH)
  56. /* If built for ipv4 and missing gethostbyname_r(), or if using async name
  57. resolve, we need the Curl_addrinfo_copy() function (which itself needs the
  58. Curl_he2ai() function)) */
  59. #define CURLRES_ADDRINFO_COPY
  60. #endif
  61. #endif /* IPv4/ares-only */
  62. #ifndef CURLRES_ASYNCH
  63. #define CURLRES_SYNCH
  64. #endif
  65. #ifndef USE_LIBIDN
  66. #define CURLRES_IDN
  67. #endif
  68. /* Allocate enough memory to hold the full name information structs and
  69. * everything. OSF1 is known to require at least 8872 bytes. The buffer
  70. * required for storing all possible aliases and IP numbers is according to
  71. * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
  72. */
  73. #define CURL_HOSTENT_SIZE 9000
  74. #define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
  75. many seconds for a name resolve */
  76. #ifdef CURLRES_ARES
  77. #define CURL_ASYNC_SUCCESS ARES_SUCCESS
  78. #if ARES_VERSION >= 0x010500
  79. /* c-ares 1.5.0 or later, the callback proto is modified */
  80. #define HAVE_CARES_CALLBACK_TIMEOUTS 1
  81. #endif
  82. #else
  83. #define CURL_ASYNC_SUCCESS CURLE_OK
  84. #define ares_cancel(x) do {} while(0)
  85. #define ares_destroy(x) do {} while(0)
  86. #endif
  87. /*
  88. * Curl_addrinfo MUST be used for all name resolved info.
  89. */
  90. #ifdef CURLRES_IPV6
  91. typedef struct addrinfo Curl_addrinfo;
  92. #ifdef CURLRES_ARES
  93. Curl_addrinfo *Curl_ip2addr6(struct in6_addr *in,
  94. const char *hostname, int port);
  95. #endif
  96. #else
  97. /* OK, so some ipv4-only include tree probably have the addrinfo struct, but
  98. to work even on those that don't, we provide our own look-alike! */
  99. struct Curl_addrinfo {
  100. int ai_flags;
  101. int ai_family;
  102. int ai_socktype;
  103. int ai_protocol;
  104. socklen_t ai_addrlen; /* Follow rfc3493 struct addrinfo */
  105. char *ai_canonname;
  106. struct sockaddr *ai_addr;
  107. struct Curl_addrinfo *ai_next;
  108. };
  109. typedef struct Curl_addrinfo Curl_addrinfo;
  110. #endif
  111. struct addrinfo;
  112. struct hostent;
  113. struct SessionHandle;
  114. struct connectdata;
  115. /*
  116. * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
  117. * Global DNS cache is general badness. Do not use. This will be removed in
  118. * a future version. Use the share interface instead!
  119. *
  120. * Returns a struct curl_hash pointer on success, NULL on failure.
  121. */
  122. struct curl_hash *Curl_global_host_cache_init(void);
  123. void Curl_global_host_cache_dtor(void);
  124. struct Curl_dns_entry {
  125. Curl_addrinfo *addr;
  126. time_t timestamp;
  127. long inuse; /* use-counter, make very sure you decrease this
  128. when you're done using the address you received */
  129. };
  130. /*
  131. * Curl_resolv() returns an entry with the info for the specified host
  132. * and port.
  133. *
  134. * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
  135. * use, or we'll leak memory!
  136. */
  137. /* return codes */
  138. #define CURLRESOLV_ERROR -1
  139. #define CURLRESOLV_RESOLVED 0
  140. #define CURLRESOLV_PENDING 1
  141. int Curl_resolv(struct connectdata *conn, const char *hostname,
  142. int port, struct Curl_dns_entry **dnsentry);
  143. /*
  144. * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
  145. * been set and returns TRUE if they are OK.
  146. */
  147. bool Curl_ipvalid(struct SessionHandle *data);
  148. /*
  149. * Curl_getaddrinfo() is the generic low-level name resolve API within this
  150. * source file. There are several versions of this function - for different
  151. * name resolve layers (selected at build-time). They all take this same set
  152. * of arguments
  153. */
  154. Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
  155. const char *hostname,
  156. int port,
  157. int *waitp);
  158. CURLcode Curl_is_resolved(struct connectdata *conn,
  159. struct Curl_dns_entry **dns);
  160. CURLcode Curl_wait_for_resolv(struct connectdata *conn,
  161. struct Curl_dns_entry **dnsentry);
  162. /* Curl_resolv_getsock() is a generic function that exists in multiple
  163. versions depending on what name resolve technology we've built to use. The
  164. function is called from the multi_getsock() function. 'sock' is a pointer
  165. to an array to hold the file descriptors, with 'numsock' being the size of
  166. that array (in number of entries). This function is supposed to return
  167. bitmask indicating what file descriptors (referring to array indexes in the
  168. 'sock' array) to wait for, read/write. */
  169. int Curl_resolv_getsock(struct connectdata *conn, curl_socket_t *sock,
  170. int numsocks);
  171. /* unlock a previously resolved dns entry */
  172. void Curl_resolv_unlock(struct SessionHandle *data,
  173. struct Curl_dns_entry *dns);
  174. /* for debugging purposes only: */
  175. void Curl_scan_cache_used(void *user, void *ptr);
  176. /* free name info */
  177. void Curl_freeaddrinfo(Curl_addrinfo *freeaddr);
  178. /* make a new dns cache and return the handle */
  179. struct curl_hash *Curl_mk_dnscache(void);
  180. /* prune old entries from the DNS cache */
  181. void Curl_hostcache_prune(struct SessionHandle *data);
  182. /* Return # of adresses in a Curl_addrinfo struct */
  183. int Curl_num_addresses (const Curl_addrinfo *addr);
  184. #ifdef CURLDEBUG
  185. void curl_dofreeaddrinfo(struct addrinfo *freethis,
  186. int line, const char *source);
  187. int curl_dogetaddrinfo(const char *hostname, const char *service,
  188. struct addrinfo *hints,
  189. struct addrinfo **result,
  190. int line, const char *source);
  191. #ifdef HAVE_GETNAMEINFO
  192. int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
  193. GETNAMEINFO_TYPE_ARG2 salen,
  194. char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
  195. char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
  196. GETNAMEINFO_TYPE_ARG7 flags,
  197. int line, const char *source);
  198. #endif
  199. #endif
  200. /* This is the callback function that is used when we build with asynch
  201. resolve, ipv4 */
  202. CURLcode Curl_addrinfo4_callback(void *arg,
  203. int status,
  204. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  205. int timeouts,
  206. #endif
  207. struct hostent *hostent);
  208. /* This is the callback function that is used when we build with asynch
  209. resolve, ipv6 */
  210. CURLcode Curl_addrinfo6_callback(void *arg,
  211. int status,
  212. #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
  213. int timeouts,
  214. #endif
  215. struct addrinfo *ai);
  216. /* [ipv4/ares only] Creates a Curl_addrinfo struct from a numerical-only IP
  217. address */
  218. Curl_addrinfo *Curl_ip2addr(in_addr_t num, const char *hostname, int port);
  219. /* [ipv4/ares only] Curl_he2ai() converts a struct hostent to a Curl_addrinfo chain
  220. and returns it */
  221. Curl_addrinfo *Curl_he2ai(const struct hostent *, int port);
  222. /* Clone a Curl_addrinfo struct, works protocol independently */
  223. Curl_addrinfo *Curl_addrinfo_copy(const void *orig, int port);
  224. /*
  225. * Curl_printable_address() returns a printable version of the 1st address
  226. * given in the 'ip' argument. The result will be stored in the buf that is
  227. * bufsize bytes big.
  228. */
  229. const char *Curl_printable_address(const Curl_addrinfo *ip,
  230. char *buf, size_t bufsize);
  231. /*
  232. * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
  233. *
  234. * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
  235. */
  236. struct Curl_dns_entry *
  237. Curl_cache_addr(struct SessionHandle *data, Curl_addrinfo *addr,
  238. const char *hostname, int port);
  239. /*
  240. * Curl_destroy_thread_data() cleans up async resolver data.
  241. * Complementary of ares_destroy.
  242. */
  243. struct Curl_async; /* forward-declaration */
  244. void Curl_destroy_thread_data(struct Curl_async *async);
  245. #ifndef INADDR_NONE
  246. #define CURL_INADDR_NONE (in_addr_t) ~0
  247. #else
  248. #define CURL_INADDR_NONE INADDR_NONE
  249. #endif
  250. #endif