hostip.h 8.6 KB

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