2
0

hostip.h 7.5 KB

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