hostip.h 8.8 KB

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