hostip.h 8.0 KB

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