113498266Sopenharmony_ci#ifndef HEADER_CURL_HOSTIP_H
213498266Sopenharmony_ci#define HEADER_CURL_HOSTIP_H
313498266Sopenharmony_ci/***************************************************************************
413498266Sopenharmony_ci *                                  _   _ ____  _
513498266Sopenharmony_ci *  Project                     ___| | | |  _ \| |
613498266Sopenharmony_ci *                             / __| | | | |_) | |
713498266Sopenharmony_ci *                            | (__| |_| |  _ <| |___
813498266Sopenharmony_ci *                             \___|\___/|_| \_\_____|
913498266Sopenharmony_ci *
1013498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
1113498266Sopenharmony_ci *
1213498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which
1313498266Sopenharmony_ci * you should have received as part of this distribution. The terms
1413498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html.
1513498266Sopenharmony_ci *
1613498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell
1713498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is
1813498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file.
1913498266Sopenharmony_ci *
2013498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
2113498266Sopenharmony_ci * KIND, either express or implied.
2213498266Sopenharmony_ci *
2313498266Sopenharmony_ci * SPDX-License-Identifier: curl
2413498266Sopenharmony_ci *
2513498266Sopenharmony_ci ***************************************************************************/
2613498266Sopenharmony_ci
2713498266Sopenharmony_ci#include "curl_setup.h"
2813498266Sopenharmony_ci#include "hash.h"
2913498266Sopenharmony_ci#include "curl_addrinfo.h"
3013498266Sopenharmony_ci#include "timeval.h" /* for timediff_t */
3113498266Sopenharmony_ci#include "asyn.h"
3213498266Sopenharmony_ci
3313498266Sopenharmony_ci#include <setjmp.h>
3413498266Sopenharmony_ci
3513498266Sopenharmony_ci/* Allocate enough memory to hold the full name information structs and
3613498266Sopenharmony_ci * everything. OSF1 is known to require at least 8872 bytes. The buffer
3713498266Sopenharmony_ci * required for storing all possible aliases and IP numbers is according to
3813498266Sopenharmony_ci * Stevens' Unix Network Programming 2nd edition, p. 304: 8192 bytes!
3913498266Sopenharmony_ci */
4013498266Sopenharmony_ci#define CURL_HOSTENT_SIZE 9000
4113498266Sopenharmony_ci
4213498266Sopenharmony_ci#define CURL_TIMEOUT_RESOLVE 300 /* when using asynch methods, we allow this
4313498266Sopenharmony_ci                                    many seconds for a name resolve */
4413498266Sopenharmony_ci
4513498266Sopenharmony_ci#define CURL_ASYNC_SUCCESS CURLE_OK
4613498266Sopenharmony_ci
4713498266Sopenharmony_cistruct addrinfo;
4813498266Sopenharmony_cistruct hostent;
4913498266Sopenharmony_cistruct Curl_easy;
5013498266Sopenharmony_cistruct connectdata;
5113498266Sopenharmony_ci
5213498266Sopenharmony_ci/*
5313498266Sopenharmony_ci * Curl_global_host_cache_init() initializes and sets up a global DNS cache.
5413498266Sopenharmony_ci * Global DNS cache is general badness. Do not use. This will be removed in
5513498266Sopenharmony_ci * a future version. Use the share interface instead!
5613498266Sopenharmony_ci *
5713498266Sopenharmony_ci * Returns a struct Curl_hash pointer on success, NULL on failure.
5813498266Sopenharmony_ci */
5913498266Sopenharmony_cistruct Curl_hash *Curl_global_host_cache_init(void);
6013498266Sopenharmony_ci
6113498266Sopenharmony_cistruct Curl_dns_entry {
6213498266Sopenharmony_ci  struct Curl_addrinfo *addr;
6313498266Sopenharmony_ci  /* timestamp == 0 -- permanent CURLOPT_RESOLVE entry (doesn't time out) */
6413498266Sopenharmony_ci  time_t timestamp;
6513498266Sopenharmony_ci  /* use-counter, use Curl_resolv_unlock to release reference */
6613498266Sopenharmony_ci  long inuse;
6713498266Sopenharmony_ci  /* hostname port number that resolved to addr. */
6813498266Sopenharmony_ci  int hostport;
6913498266Sopenharmony_ci  /* hostname that resolved to addr. may be NULL (unix domain sockets). */
7013498266Sopenharmony_ci  char hostname[1];
7113498266Sopenharmony_ci};
7213498266Sopenharmony_ci
7313498266Sopenharmony_cibool Curl_host_is_ipnum(const char *hostname);
7413498266Sopenharmony_ci
7513498266Sopenharmony_ci/*
7613498266Sopenharmony_ci * Curl_resolv() returns an entry with the info for the specified host
7713498266Sopenharmony_ci * and port.
7813498266Sopenharmony_ci *
7913498266Sopenharmony_ci * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
8013498266Sopenharmony_ci * use, or we'll leak memory!
8113498266Sopenharmony_ci */
8213498266Sopenharmony_ci/* return codes */
8313498266Sopenharmony_cienum resolve_t {
8413498266Sopenharmony_ci  CURLRESOLV_TIMEDOUT = -2,
8513498266Sopenharmony_ci  CURLRESOLV_ERROR    = -1,
8613498266Sopenharmony_ci  CURLRESOLV_RESOLVED =  0,
8713498266Sopenharmony_ci  CURLRESOLV_PENDING  =  1
8813498266Sopenharmony_ci};
8913498266Sopenharmony_cienum resolve_t Curl_resolv(struct Curl_easy *data,
9013498266Sopenharmony_ci                           const char *hostname,
9113498266Sopenharmony_ci                           int port,
9213498266Sopenharmony_ci                           bool allowDOH,
9313498266Sopenharmony_ci                           struct Curl_dns_entry **dnsentry);
9413498266Sopenharmony_cienum resolve_t Curl_resolv_timeout(struct Curl_easy *data,
9513498266Sopenharmony_ci                                   const char *hostname, int port,
9613498266Sopenharmony_ci                                   struct Curl_dns_entry **dnsentry,
9713498266Sopenharmony_ci                                   timediff_t timeoutms);
9813498266Sopenharmony_ci
9913498266Sopenharmony_ci#ifdef ENABLE_IPV6
10013498266Sopenharmony_ci/*
10113498266Sopenharmony_ci * Curl_ipv6works() returns TRUE if IPv6 seems to work.
10213498266Sopenharmony_ci */
10313498266Sopenharmony_cibool Curl_ipv6works(struct Curl_easy *data);
10413498266Sopenharmony_ci#else
10513498266Sopenharmony_ci#define Curl_ipv6works(x) FALSE
10613498266Sopenharmony_ci#endif
10713498266Sopenharmony_ci
10813498266Sopenharmony_ci/*
10913498266Sopenharmony_ci * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
11013498266Sopenharmony_ci * been set and returns TRUE if they are OK.
11113498266Sopenharmony_ci */
11213498266Sopenharmony_cibool Curl_ipvalid(struct Curl_easy *data, struct connectdata *conn);
11313498266Sopenharmony_ci
11413498266Sopenharmony_ci
11513498266Sopenharmony_ci/*
11613498266Sopenharmony_ci * Curl_getaddrinfo() is the generic low-level name resolve API within this
11713498266Sopenharmony_ci * source file. There are several versions of this function - for different
11813498266Sopenharmony_ci * name resolve layers (selected at build-time). They all take this same set
11913498266Sopenharmony_ci * of arguments
12013498266Sopenharmony_ci */
12113498266Sopenharmony_cistruct Curl_addrinfo *Curl_getaddrinfo(struct Curl_easy *data,
12213498266Sopenharmony_ci                                       const char *hostname,
12313498266Sopenharmony_ci                                       int port,
12413498266Sopenharmony_ci                                       int *waitp);
12513498266Sopenharmony_ci
12613498266Sopenharmony_ci
12713498266Sopenharmony_ci/* unlock a previously resolved dns entry */
12813498266Sopenharmony_civoid Curl_resolv_unlock(struct Curl_easy *data,
12913498266Sopenharmony_ci                        struct Curl_dns_entry *dns);
13013498266Sopenharmony_ci
13113498266Sopenharmony_ci/* init a new dns cache */
13213498266Sopenharmony_civoid Curl_init_dnscache(struct Curl_hash *hash, int hashsize);
13313498266Sopenharmony_ci
13413498266Sopenharmony_ci/* prune old entries from the DNS cache */
13513498266Sopenharmony_civoid Curl_hostcache_prune(struct Curl_easy *data);
13613498266Sopenharmony_ci
13713498266Sopenharmony_ci/* IPv4 threadsafe resolve function used for synch and asynch builds */
13813498266Sopenharmony_cistruct Curl_addrinfo *Curl_ipv4_resolve_r(const char *hostname, int port);
13913498266Sopenharmony_ci
14013498266Sopenharmony_ciCURLcode Curl_once_resolved(struct Curl_easy *data, bool *protocol_connect);
14113498266Sopenharmony_ci
14213498266Sopenharmony_ci/*
14313498266Sopenharmony_ci * Curl_addrinfo_callback() is used when we build with any asynch specialty.
14413498266Sopenharmony_ci * Handles end of async request processing. Inserts ai into hostcache when
14513498266Sopenharmony_ci * status is CURL_ASYNC_SUCCESS. Twiddles fields in conn to indicate async
14613498266Sopenharmony_ci * request completed whether successful or failed.
14713498266Sopenharmony_ci */
14813498266Sopenharmony_ciCURLcode Curl_addrinfo_callback(struct Curl_easy *data,
14913498266Sopenharmony_ci                                int status,
15013498266Sopenharmony_ci                                struct Curl_addrinfo *ai);
15113498266Sopenharmony_ci
15213498266Sopenharmony_ci/*
15313498266Sopenharmony_ci * Curl_printable_address() returns a printable version of the 1st address
15413498266Sopenharmony_ci * given in the 'ip' argument. The result will be stored in the buf that is
15513498266Sopenharmony_ci * bufsize bytes big.
15613498266Sopenharmony_ci */
15713498266Sopenharmony_civoid Curl_printable_address(const struct Curl_addrinfo *ip,
15813498266Sopenharmony_ci                            char *buf, size_t bufsize);
15913498266Sopenharmony_ci
16013498266Sopenharmony_ci/*
16113498266Sopenharmony_ci * Curl_fetch_addr() fetches a 'Curl_dns_entry' already in the DNS cache.
16213498266Sopenharmony_ci *
16313498266Sopenharmony_ci * Returns the Curl_dns_entry entry pointer or NULL if not in the cache.
16413498266Sopenharmony_ci *
16513498266Sopenharmony_ci * The returned data *MUST* be "unlocked" with Curl_resolv_unlock() after
16613498266Sopenharmony_ci * use, or we'll leak memory!
16713498266Sopenharmony_ci */
16813498266Sopenharmony_cistruct Curl_dns_entry *
16913498266Sopenharmony_ciCurl_fetch_addr(struct Curl_easy *data,
17013498266Sopenharmony_ci                const char *hostname,
17113498266Sopenharmony_ci                int port);
17213498266Sopenharmony_ci
17313498266Sopenharmony_ci/*
17413498266Sopenharmony_ci * Curl_cache_addr() stores a 'Curl_addrinfo' struct in the DNS cache.
17513498266Sopenharmony_ci *
17613498266Sopenharmony_ci * Returns the Curl_dns_entry entry pointer or NULL if the storage failed.
17713498266Sopenharmony_ci */
17813498266Sopenharmony_cistruct Curl_dns_entry *
17913498266Sopenharmony_ciCurl_cache_addr(struct Curl_easy *data, struct Curl_addrinfo *addr,
18013498266Sopenharmony_ci                const char *hostname, size_t hostlen, int port);
18113498266Sopenharmony_ci
18213498266Sopenharmony_ci#ifndef INADDR_NONE
18313498266Sopenharmony_ci#define CURL_INADDR_NONE (in_addr_t) ~0
18413498266Sopenharmony_ci#else
18513498266Sopenharmony_ci#define CURL_INADDR_NONE INADDR_NONE
18613498266Sopenharmony_ci#endif
18713498266Sopenharmony_ci
18813498266Sopenharmony_ci/*
18913498266Sopenharmony_ci * Function provided by the resolver backend to set DNS servers to use.
19013498266Sopenharmony_ci */
19113498266Sopenharmony_ciCURLcode Curl_set_dns_servers(struct Curl_easy *data, char *servers);
19213498266Sopenharmony_ci
19313498266Sopenharmony_ci/*
19413498266Sopenharmony_ci * Function provided by the resolver backend to set
19513498266Sopenharmony_ci * outgoing interface to use for DNS requests
19613498266Sopenharmony_ci */
19713498266Sopenharmony_ciCURLcode Curl_set_dns_interface(struct Curl_easy *data,
19813498266Sopenharmony_ci                                const char *interf);
19913498266Sopenharmony_ci
20013498266Sopenharmony_ci/*
20113498266Sopenharmony_ci * Function provided by the resolver backend to set
20213498266Sopenharmony_ci * local IPv4 address to use as source address for DNS requests
20313498266Sopenharmony_ci */
20413498266Sopenharmony_ciCURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
20513498266Sopenharmony_ci                                const char *local_ip4);
20613498266Sopenharmony_ci
20713498266Sopenharmony_ci/*
20813498266Sopenharmony_ci * Function provided by the resolver backend to set
20913498266Sopenharmony_ci * local IPv6 address to use as source address for DNS requests
21013498266Sopenharmony_ci */
21113498266Sopenharmony_ciCURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
21213498266Sopenharmony_ci                                const char *local_ip6);
21313498266Sopenharmony_ci
21413498266Sopenharmony_ci/*
21513498266Sopenharmony_ci * Clean off entries from the cache
21613498266Sopenharmony_ci */
21713498266Sopenharmony_civoid Curl_hostcache_clean(struct Curl_easy *data, struct Curl_hash *hash);
21813498266Sopenharmony_ci
21913498266Sopenharmony_ci/*
22013498266Sopenharmony_ci * Populate the cache with specified entries from CURLOPT_RESOLVE.
22113498266Sopenharmony_ci */
22213498266Sopenharmony_ciCURLcode Curl_loadhostpairs(struct Curl_easy *data);
22313498266Sopenharmony_ciCURLcode Curl_resolv_check(struct Curl_easy *data,
22413498266Sopenharmony_ci                           struct Curl_dns_entry **dns);
22513498266Sopenharmony_ciint Curl_resolv_getsock(struct Curl_easy *data,
22613498266Sopenharmony_ci                        curl_socket_t *socks);
22713498266Sopenharmony_ci
22813498266Sopenharmony_ciCURLcode Curl_resolver_error(struct Curl_easy *data);
22913498266Sopenharmony_ci#endif /* HEADER_CURL_HOSTIP_H */
230