hostip.h 7.9 KB

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