hostip.h 7.9 KB

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