• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* MIT License
2  *
3  * Copyright (c) 1998 Massachusetts Institute of Technology
4  * Copyright (c) 2010 Daniel Stenberg
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice (including the next
14  * paragraph) shall be included in all copies or substantial portions of the
15  * Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * SPDX-License-Identifier: MIT
26  */
27 #ifndef __ARES_PRIVATE_H
28 #define __ARES_PRIVATE_H
29 
30 /* ============================================================================
31  * NOTE: All c-ares source files should include ares_private.h as the first
32  *       header.
33  * ============================================================================
34  */
35 
36 #include "ares_setup.h"
37 #include "ares.h"
38 
39 #ifdef HAVE_NETINET_IN_H
40 #  include <netinet/in.h>
41 #endif
42 
43 #include "ares_mem.h"
44 #include "ares_ipv6.h"
45 #include "util/ares_math.h"
46 #include "util/ares_time.h"
47 #include "util/ares_rand.h"
48 #include "ares_array.h"
49 #include "ares_llist.h"
50 #include "dsa/ares_slist.h"
51 #include "ares_htable_strvp.h"
52 #include "ares_htable_szvp.h"
53 #include "ares_htable_asvp.h"
54 #include "ares_htable_dict.h"
55 #include "ares_htable_vpvp.h"
56 #include "ares_htable_vpstr.h"
57 #include "record/ares_dns_multistring.h"
58 #include "ares_buf.h"
59 #include "record/ares_dns_private.h"
60 #include "util/ares_iface_ips.h"
61 #include "util/ares_threads.h"
62 #include "ares_socket.h"
63 #include "ares_conn.h"
64 #include "ares_str.h"
65 #include "str/ares_strsplit.h"
66 #include "util/ares_uri.h"
67 
68 #ifndef HAVE_GETENV
69 #  include "ares_getenv.h"
70 #  define getenv(ptr) ares_getenv(ptr)
71 #endif
72 
73 #define DEFAULT_TIMEOUT 2000 /* milliseconds */
74 #define DEFAULT_TRIES   3
75 #ifndef INADDR_NONE
76 #  define INADDR_NONE 0xffffffff
77 #endif
78 
79 /* By using a double cast, we can get rid of the bogus warning of
80  * warning: cast from 'const struct sockaddr *' to 'const struct sockaddr_in6 *'
81  * increases required alignment from 1 to 4 [-Wcast-align]
82  */
83 #define CARES_INADDR_CAST(type, var) ((type)((const void *)var))
84 
85 #if defined(USE_WINSOCK)
86 
87 #  define WIN_NS_9X     "System\\CurrentControlSet\\Services\\VxD\\MSTCP"
88 #  define WIN_NS_NT_KEY "System\\CurrentControlSet\\Services\\Tcpip\\Parameters"
89 #  define WIN_DNSCLIENT "Software\\Policies\\Microsoft\\System\\DNSClient"
90 #  define WIN_NT_DNSCLIENT \
91     "Software\\Policies\\Microsoft\\Windows NT\\DNSClient"
92 #  define NAMESERVER           "NameServer"
93 #  define DHCPNAMESERVER       "DhcpNameServer"
94 #  define DATABASEPATH         "DatabasePath"
95 #  define WIN_PATH_HOSTS       "\\hosts"
96 #  define SEARCHLIST_KEY       "SearchList"
97 #  define PRIMARYDNSSUFFIX_KEY "PrimaryDNSSuffix"
98 #  define INTERFACES_KEY       "Interfaces"
99 #  define DOMAIN_KEY           "Domain"
100 #  define DHCPDOMAIN_KEY       "DhcpDomain"
101 #  define PATH_RESOLV_CONF     ""
102 #elif defined(WATT32)
103 
104 #  define PATH_RESOLV_CONF "/dev/ENV/etc/resolv.conf"
105 W32_FUNC const char *_w32_GetHostsFile(void);
106 
107 #elif defined(NETWARE)
108 
109 #  define PATH_RESOLV_CONF "sys:/etc/resolv.cfg"
110 #  define PATH_HOSTS       "sys:/etc/hosts"
111 
112 #elif defined(__riscos__)
113 
114 #  define PATH_RESOLV_CONF ""
115 #  define PATH_HOSTS       "InetDBase:Hosts"
116 
117 #elif defined(__HAIKU__)
118 
119 #  define PATH_RESOLV_CONF "/system/settings/network/resolv.conf"
120 #  define PATH_HOSTS       "/system/settings/network/hosts"
121 
122 #else
123 
124 #  define PATH_RESOLV_CONF "/etc/resolv.conf"
125 #  ifdef ETC_INET
126 #    define PATH_HOSTS "/etc/inet/hosts"
127 #  else
128 #    define PATH_HOSTS "/etc/hosts"
129 #  endif
130 
131 #endif
132 
133 /********* EDNS defines section ******/
134 #define EDNSPACKETSZ                                          \
135   1232 /* Reasonable UDP payload size, as agreed by operators \
136           https://www.dnsflagday.net/2020/#faq */
137 #define MAXENDSSZ   4096 /* Maximum (local) limit for edns packet size */
138 #define EDNSFIXEDSZ 11   /* Size of EDNS header */
139 
140 /********* EDNS defines section ******/
141 
142 /* Default values for server failover behavior. We retry failed servers with
143  * a 10% probability and a minimum delay of 5 seconds between retries.
144  */
145 #define DEFAULT_SERVER_RETRY_CHANCE 10
146 #define DEFAULT_SERVER_RETRY_DELAY  5000
147 
148 struct ares_query;
149 typedef struct ares_query ares_query_t;
150 
151 /* State to represent a DNS query */
152 struct ares_query {
153   /* Query ID from qbuf, for faster lookup, and current timeout */
154   unsigned short       qid; /* host byte order */
155   ares_timeval_t       ts;  /*!< Timestamp query was sent */
156   ares_timeval_t       timeout;
157   ares_channel_t      *channel;
158 
159   /*
160    * Node object for each list entry the query belongs to in order to
161    * make removal operations O(1).
162    */
163   ares_slist_node_t   *node_queries_by_timeout;
164   ares_llist_node_t   *node_queries_to_conn;
165   ares_llist_node_t   *node_all_queries;
166 
167   /* connection handle query is associated with */
168   ares_conn_t         *conn;
169 
170   /* Query */
171   ares_dns_record_t   *query;
172 
173   ares_callback_dnsrec callback;
174   void                *arg;
175 
176   /* Query status */
177   size_t        try_count; /* Number of times we tried this query already. */
178   size_t        cookie_try_count; /* Attempt count for cookie resends */
179   ares_bool_t   using_tcp;
180   ares_status_t error_status;
181   size_t        timeouts;   /* number of timeouts we saw for this request */
182   ares_bool_t   no_retries; /* do not perform any additional retries, this is
183                              * set when a query is to be canceled */
184 };
185 
186 struct apattern {
187   struct ares_addr addr;
188   unsigned char    mask;
189 };
190 
191 struct ares_qcache;
192 typedef struct ares_qcache ares_qcache_t;
193 
194 struct ares_hosts_file;
195 typedef struct ares_hosts_file ares_hosts_file_t;
196 
197 struct ares_channeldata {
198   /* Configuration data */
199   unsigned int         flags;
200   size_t               timeout; /* in milliseconds */
201   size_t               tries;
202   size_t               ndots;
203   size_t               maxtimeout;                 /* in milliseconds */
204   ares_bool_t          rotate;
205   unsigned short       udp_port;                   /* stored in network order */
206   unsigned short       tcp_port;                   /* stored in network order */
207   int                  socket_send_buffer_size;    /* setsockopt takes int */
208   int                  socket_receive_buffer_size; /* setsockopt takes int */
209   char               **domains;
210   size_t               ndomains;
211   struct apattern     *sortlist;
212   size_t               nsort;
213   char                *lookups;
214   size_t               ednspsz;
215   unsigned int         qcache_max_ttl;
216   ares_evsys_t         evsys;
217   unsigned int         optmask;
218 
219   /* For binding to local devices and/or IP addresses.  Leave
220    * them null/zero for no binding.
221    */
222   char                 local_dev_name[32];
223   unsigned int         local_ip4;
224   unsigned char        local_ip6[16];
225 
226   /* Thread safety lock */
227   ares_thread_mutex_t *lock;
228 
229   /* Conditional to wake waiters when queue is empty */
230   ares_thread_cond_t  *cond_empty;
231 
232   /* Server addresses and communications state. Sorted by least consecutive
233    * failures, followed by the configuration order if failures are equal. */
234   ares_slist_t        *servers;
235 
236   /* random state to use when generating new ids and generating retry penalties
237    */
238   ares_rand_state     *rand_state;
239 
240   /* All active queries in a single list */
241   ares_llist_t        *all_queries;
242   /* Queries bucketed by qid, for quickly dispatching DNS responses: */
243   ares_htable_szvp_t  *queries_by_qid;
244 
245   /* Queries bucketed by timeout, for quickly handling timeouts: */
246   ares_slist_t        *queries_by_timeout;
247 
248   /* Map linked list node member for connection to file descriptor.  We use
249    * the node instead of the connection object itself so we can quickly look
250    * up a connection and remove it if necessary (as otherwise we'd have to
251    * scan all connections) */
252   ares_htable_asvp_t  *connnode_by_socket;
253 
254   ares_sock_state_cb   sock_state_cb;
255   void                *sock_state_cb_data;
256 
257   ares_sock_create_callback           sock_create_cb;
258   void                               *sock_create_cb_data;
259 
260   ares_sock_config_callback           sock_config_cb;
261   void                               *sock_config_cb_data;
262 
263   struct ares_socket_functions_ex     sock_funcs;
264   void                               *sock_func_cb_data;
265   const struct ares_socket_functions *legacy_sock_funcs;
266   void                               *legacy_sock_funcs_cb_data;
267 
268   ares_pending_write_cb               notify_pending_write_cb;
269   void                               *notify_pending_write_cb_data;
270   ares_bool_t                         notify_pending_write;
271 
272   /* Path for resolv.conf file, configurable via ares_options */
273   char                               *resolvconf_path;
274 
275   /* Path for hosts file, configurable via ares_options */
276   char                               *hosts_path;
277 
278   /* Maximum UDP queries per connection allowed */
279   size_t                              udp_max_queries;
280 
281   /* Cache of local hosts file */
282   ares_hosts_file_t                  *hf;
283 
284   /* Query Cache */
285   ares_qcache_t                      *qcache;
286 
287   /* Fields controlling server failover behavior.
288    * The retry chance is the probability (1/N) by which we will retry a failed
289    * server instead of the best server when selecting a server to send queries
290    * to.
291    * The retry delay is the minimum time in milliseconds to wait between doing
292    * such retries (applied per-server).
293    */
294   unsigned short                      server_retry_chance;
295   size_t                              server_retry_delay;
296 
297   /* Callback triggered when a server has a successful or failed response */
298   ares_server_state_callback          server_state_cb;
299   void                               *server_state_cb_data;
300 
301   /* TRUE if a reinit is pending.  Reinit spawns a thread to read the system
302    * configuration and then apply the configuration since configuration
303    * reading may block.  The thread handle is provided for waiting on thread
304    * exit. */
305   ares_bool_t                         reinit_pending;
306   ares_thread_t                      *reinit_thread;
307 
308   /* Whether the system is up or not.  This is mainly to prevent deadlocks
309    * and access violations during the cleanup process.  Some things like
310    * system config changes might get triggered and we need a flag to make
311    * sure we don't take action. */
312   ares_bool_t                         sys_up;
313 };
314 
315 #if OHOS_DNS_PROXY_BY_NETSYS
316 struct ares_family_query_info {
317   int retCode;
318   char *serverAddr;
319   unsigned char isNoAnswer;
320   unsigned char cname;
321 };
322 
323 struct ares_process_info {
324   long long queryTime;
325   char *hostname;
326   int retCode;
327   int firstQueryEndDuration;
328   int firstQueryEnd2AppDuration;
329   unsigned short firstReturnType; /* a or aaaa */
330   unsigned char isFromCache;
331   unsigned char sourceFrom;
332   struct ares_family_query_info ipv4QueryInfo;
333   struct ares_family_query_info ipv6QueryInfo;
334 };
335 #endif
336 
337 /* Does the domain end in ".onion" or ".onion."? Case-insensitive. */
338 ares_bool_t   ares_is_onion_domain(const char *name);
339 
340 /* Returns one of the normal ares status codes like ARES_SUCCESS */
341 ares_status_t ares_send_query(ares_server_t *requested_server /* Optional */,
342                               ares_query_t *query, const ares_timeval_t *now);
343 ares_status_t ares_requeue_query(ares_query_t *query, const ares_timeval_t *now,
344                                  ares_status_t            status,
345                                  ares_bool_t              inc_try_count,
346                                  const ares_dns_record_t *dnsrec,
347                                  ares_array_t           **requeue);
348 
349 /*! Count the number of labels (dots+1) in a domain */
350 size_t        ares_name_label_cnt(const char *name);
351 
352 /*! Retrieve a list of names to use for searching.  The first successful
353  *  query in the list wins.  This function also uses the HOSTSALIASES file
354  *  as well as uses channel configuration to determine the search order.
355  *
356  *  \param[in]  channel   initialized ares channel
357  *  \param[in]  name      initial name being searched
358  *  \param[out] names     array of names to attempt, use ares_strsplit_free()
359  *                        when no longer needed.
360  *  \param[out] names_len number of names in array
361  *  \return ARES_SUCCESS on success, otherwise one of the other error codes.
362  */
363 ares_status_t ares_search_name_list(const ares_channel_t *channel,
364                                     const char *name, char ***names,
365                                     size_t *names_len);
366 
367 /*! Function to create callback arg for converting from ares_callback_dnsrec
368  *  to ares_calback */
369 void         *ares_dnsrec_convert_arg(ares_callback callback, void *arg);
370 
371 /*! Callback function used to convert from the ares_callback_dnsrec prototype to
372  *  the ares_callback prototype, by writing the result and passing that to
373  *  the inner callback.
374  */
375 void ares_dnsrec_convert_cb(void *arg, ares_status_t status, size_t timeouts,
376                             const ares_dns_record_t *dnsrec);
377 
378 void ares_free_query(ares_query_t *query);
379 
380 unsigned short ares_generate_new_id(ares_rand_state *state);
381 ares_status_t  ares_expand_name_validated(const unsigned char *encoded,
382                                           const unsigned char *abuf, size_t alen,
383                                           char **s, size_t *enclen,
384                                           ares_bool_t is_hostname);
385 ares_status_t  ares_expand_string_ex(const unsigned char *encoded,
386                                      const unsigned char *abuf, size_t alen,
387                                      unsigned char **s, size_t *enclen);
388 ares_status_t  ares_init_servers_state(ares_channel_t *channel);
389 ares_status_t  ares_init_by_options(ares_channel_t            *channel,
390                                     const struct ares_options *options,
391                                     int                        optmask);
392 ares_status_t  ares_init_by_sysconfig(ares_channel_t *channel);
393 void           ares_set_socket_functions_def(ares_channel_t *channel);
394 
395 typedef struct {
396   ares_llist_t    *sconfig;
397   struct apattern *sortlist;
398   size_t           nsortlist;
399   char           **domains;
400   size_t           ndomains;
401   char            *lookups;
402   size_t           ndots;
403   size_t           tries;
404   ares_bool_t      rotate;
405   size_t           timeout_ms;
406   ares_bool_t      usevc;
407 } ares_sysconfig_t;
408 
409 ares_status_t ares_sysconfig_set_options(ares_sysconfig_t *sysconfig,
410                                          const char       *str);
411 
412 ares_status_t ares_init_by_environment(ares_sysconfig_t *sysconfig);
413 
414 
415 typedef ares_status_t (*ares_sysconfig_line_cb_t)(const ares_channel_t *channel,
416                                                   ares_sysconfig_t     *sysconfig,
417                                                   ares_buf_t           *line);
418 
419 ares_status_t ares_sysconfig_parse_resolv_line(const ares_channel_t *channel,
420                                                ares_sysconfig_t     *sysconfig,
421                                                ares_buf_t           *line);
422 
423 ares_status_t ares_sysconfig_process_buf(const ares_channel_t    *channel,
424                                          ares_sysconfig_t        *sysconfig,
425                                          ares_buf_t              *buf,
426                                          ares_sysconfig_line_cb_t cb);
427 
428 ares_status_t ares_init_sysconfig_files(const ares_channel_t *channel,
429                                         ares_sysconfig_t     *sysconfig,
430                                         ares_bool_t process_resolvconf);
431 #ifdef __APPLE__
432 ares_status_t ares_init_sysconfig_macos(const ares_channel_t *channel,
433                                         ares_sysconfig_t     *sysconfig);
434 #endif
435 #ifdef USE_WINSOCK
436 ares_status_t ares_init_sysconfig_windows(const ares_channel_t *channel,
437                                           ares_sysconfig_t     *sysconfig);
438 #endif
439 
440 ares_status_t ares_parse_sortlist(struct apattern **sortlist, size_t *nsort,
441                                   const char *str);
442 
443 /* Returns ARES_SUCCESS if alias found, alias is set.  Returns ARES_ENOTFOUND
444  * if not alias found.  Returns other errors on critical failure like
445  * ARES_ENOMEM */
446 ares_status_t ares_lookup_hostaliases(const ares_channel_t *channel,
447                                       const char *name, char **alias);
448 
449 ares_status_t ares_cat_domain(const char *name, const char *domain, char **s);
450 ares_status_t ares_sortaddrinfo(ares_channel_t            *channel,
451                                 struct ares_addrinfo_node *ai_node);
452 
453 void          ares_freeaddrinfo_nodes(struct ares_addrinfo_node *ai_node);
454 ares_bool_t   ares_is_localhost(const char *name);
455 
456 struct ares_addrinfo_node    *
457   ares_append_addrinfo_node(struct ares_addrinfo_node **ai_node);
458 void ares_addrinfo_cat_nodes(struct ares_addrinfo_node **head,
459                              struct ares_addrinfo_node  *tail);
460 
461 void ares_freeaddrinfo_cnames(struct ares_addrinfo_cname *ai_cname);
462 
463 struct ares_addrinfo_cname             *
464   ares_append_addrinfo_cname(struct ares_addrinfo_cname **ai_cname);
465 
466 ares_status_t ares_append_ai_node(int aftype, unsigned short port,
467                                   unsigned int ttl, const void *adata,
468                                   struct ares_addrinfo_node **nodes);
469 
470 void          ares_addrinfo_cat_cnames(struct ares_addrinfo_cname **head,
471                                        struct ares_addrinfo_cname  *tail);
472 
473 ares_status_t ares_parse_into_addrinfo(const ares_dns_record_t *dnsrec,
474                                        ares_bool_t    cname_only_is_enodata,
475                                        unsigned short port,
476                                        struct ares_addrinfo *ai);
477 ares_status_t ares_parse_ptr_reply_dnsrec(const ares_dns_record_t *dnsrec,
478                                           const void *addr, int addrlen,
479                                           int family, struct hostent **host);
480 
481 /* host address must be valid or NULL as will create or append */
482 ares_status_t ares_addrinfo2hostent(const struct ares_addrinfo *ai, int family,
483                                     struct hostent **host);
484 
485 ares_status_t ares_addrinfo2addrttl(const struct ares_addrinfo *ai, int family,
486                                     size_t                req_naddrttls,
487                                     struct ares_addrttl  *addrttls,
488                                     struct ares_addr6ttl *addr6ttls,
489                                     size_t               *naddrttls);
490 ares_status_t ares_addrinfo_localhost(const char *name, unsigned short port,
491                                       const struct ares_addrinfo_hints *hints,
492                                       struct ares_addrinfo             *ai);
493 
494 ares_status_t ares_servers_update(ares_channel_t *channel,
495                                   ares_llist_t   *server_list,
496                                   ares_bool_t     user_specified);
497 ares_status_t
498   ares_sconfig_append(const ares_channel_t *channel, ares_llist_t **sconfig,
499                       const struct ares_addr *addr, unsigned short udp_port,
500                       unsigned short tcp_port, const char *ll_iface);
501 ares_status_t ares_sconfig_append_fromstr(const ares_channel_t *channel,
502                                           ares_llist_t        **sconfig,
503                                           const char           *str,
504                                           ares_bool_t           ignore_invalid);
505 ares_status_t ares_in_addr_to_sconfig_llist(const struct in_addr *servers,
506                                             size_t                nservers,
507                                             ares_llist_t        **llist);
508 ares_status_t ares_get_server_addr(const ares_server_t *server,
509                                    ares_buf_t          *buf);
510 
511 struct ares_hosts_entry;
512 typedef struct ares_hosts_entry ares_hosts_entry_t;
513 
514 void                            ares_hosts_file_destroy(ares_hosts_file_t *hf);
515 ares_status_t ares_hosts_search_ipaddr(ares_channel_t *channel,
516                                        ares_bool_t use_env, const char *ipaddr,
517                                        const ares_hosts_entry_t **entry);
518 ares_status_t ares_hosts_search_host(ares_channel_t *channel,
519                                      ares_bool_t use_env, const char *host,
520                                      const ares_hosts_entry_t **entry);
521 ares_status_t ares_hosts_entry_to_hostent(const ares_hosts_entry_t *entry,
522                                           int family, struct hostent **hostent);
523 ares_status_t ares_hosts_entry_to_addrinfo(const ares_hosts_entry_t *entry,
524                                            const char *name, int family,
525                                            unsigned short        port,
526                                            ares_bool_t           want_cnames,
527                                            struct ares_addrinfo *ai);
528 
529 /* Same as ares_query_dnsrec() except does not take a channel lock.  Use this
530  * if a channel lock is already held */
531 ares_status_t ares_query_nolock(ares_channel_t *channel, const char *name,
532                                 ares_dns_class_t     dnsclass,
533                                 ares_dns_rec_type_t  type,
534                                 ares_callback_dnsrec callback, void *arg,
535                                 unsigned short *qid);
536 
537 /*! Flags controlling behavior for ares_send_nolock() */
538 typedef enum {
539   ARES_SEND_FLAG_NOCACHE = 1 << 0, /*!< Do not query the cache */
540   ARES_SEND_FLAG_NORETRY = 1 << 1  /*!< Do not retry this query on error */
541 } ares_send_flags_t;
542 
543 /* Similar to ares_send_dnsrec() except does not take a channel lock, allows
544  * specifying a particular server to use, and also flags controlling behavior.
545  */
546 ares_status_t ares_send_nolock(ares_channel_t *channel, ares_server_t *server,
547                                ares_send_flags_t        flags,
548                                const ares_dns_record_t *dnsrec,
549                                ares_callback_dnsrec callback, void *arg,
550                                unsigned short *qid);
551 
552 /* Same as ares_gethostbyaddr() except does not take a channel lock.  Use this
553  * if a channel lock is already held */
554 void ares_gethostbyaddr_nolock(ares_channel_t *channel, const void *addr,
555                                int addrlen, int family,
556                                ares_host_callback callback, void *arg);
557 
558 /*! Parse a compressed DNS name as defined in RFC1035 starting at the current
559  *  offset within the buffer.
560  *
561  *  It is assumed that either a const buffer is being used, or before
562  *  the message processing was started that ares_buf_reclaim() was called.
563  *
564  *  \param[in]  buf        Initialized buffer object
565  *  \param[out] name       Pointer passed by reference to be filled in with
566  *                         allocated string of the parsed name that must be
567  *                         ares_free()'d by the caller.
568  *  \param[in] is_hostname if ARES_TRUE, will validate the character set for
569  *                         a valid hostname or will return error.
570  *  \return ARES_SUCCESS on success
571  */
572 ares_status_t ares_dns_name_parse(ares_buf_t *buf, char **name,
573                                   ares_bool_t is_hostname);
574 
575 /*! Write the DNS name to the buffer in the DNS domain-name syntax as a
576  *  series of labels.  The maximum domain name length is 255 characters with
577  *  each label being a maximum of 63 characters.  If the validate_hostname
578  *  flag is set, it will strictly validate the character set.
579  *
580  *  \param[in,out]  buf   Initialized buffer object to write name to
581  *  \param[in,out]  list  Pointer passed by reference to maintain a list of
582  *                        domain name to indexes used for name compression.
583  *                        Pass NULL (not by reference) if name compression isn't
584  *                        desired.  Otherwise the list will be automatically
585  *                        created upon first entry.
586  *  \param[in]      validate_hostname Validate the hostname character set.
587  *  \param[in]      name              Name to write out, it may have escape
588  *                                    sequences.
589  *  \return ARES_SUCCESS on success, most likely ARES_EBADNAME if the name is
590  *          bad.
591  */
592 ares_status_t ares_dns_name_write(ares_buf_t *buf, ares_llist_t **list,
593                                   ares_bool_t validate_hostname,
594                                   const char *name);
595 
596 /*! Check if the queue is empty, if so, wake any waiters.  This is only
597  *  effective if built with threading support.
598  *
599  *  Must be holding a channel lock when calling this function.
600  *
601  *  \param[in]  channel Initialized ares channel object
602  */
603 void          ares_queue_notify_empty(ares_channel_t *channel);
604 
605 #define ARES_CONFIG_CHECK(x)                                              \
606   (x && x->lookups && ares_slist_len(x->servers) > 0 && x->timeout > 0 && \
607    x->tries > 0)
608 
609 ares_bool_t   ares_subnet_match(const struct ares_addr *addr,
610                                 const struct ares_addr *subnet,
611                                 unsigned char           netmask);
612 ares_bool_t   ares_addr_is_linklocal(const struct ares_addr *addr);
613 
614 void          ares_qcache_destroy(ares_qcache_t *cache);
615 ares_status_t ares_qcache_create(ares_rand_state *rand_state,
616                                  unsigned int     max_ttl,
617                                  ares_qcache_t  **cache_out);
618 void          ares_qcache_flush(ares_qcache_t *cache);
619 ares_status_t ares_qcache_insert(ares_channel_t       *channel,
620                                  const ares_timeval_t *now,
621                                  const ares_query_t   *query,
622                                  ares_dns_record_t    *dnsrec);
623 ares_status_t ares_qcache_fetch(ares_channel_t           *channel,
624                                 const ares_timeval_t     *now,
625                                 const ares_dns_record_t  *dnsrec,
626                                 const ares_dns_record_t **dnsrec_resp);
627 
628 void   ares_metrics_record(const ares_query_t *query, ares_server_t *server,
629                            ares_status_t status, const ares_dns_record_t *dnsrec);
630 size_t ares_metrics_server_timeout(const ares_server_t  *server,
631                                    const ares_timeval_t *now);
632 
633 ares_status_t ares_cookie_apply(ares_dns_record_t *dnsrec, ares_conn_t *conn,
634                                 const ares_timeval_t *now);
635 ares_status_t ares_cookie_validate(ares_query_t            *query,
636                                    const ares_dns_record_t *dnsresp,
637                                    ares_conn_t             *conn,
638                                    const ares_timeval_t    *now,
639                                    ares_array_t           **requeue);
640 
641 ares_status_t ares_channel_threading_init(ares_channel_t *channel);
642 void          ares_channel_threading_destroy(ares_channel_t *channel);
643 void          ares_channel_lock(const ares_channel_t *channel);
644 void          ares_channel_unlock(const ares_channel_t *channel);
645 
646 struct ares_event_thread;
647 typedef struct ares_event_thread ares_event_thread_t;
648 
649 void          ares_event_thread_destroy(ares_channel_t *channel);
650 ares_status_t ares_event_thread_init(ares_channel_t *channel);
651 
652 
653 #ifdef _WIN32
654 #  define HOSTENT_ADDRTYPE_TYPE short
655 #  define HOSTENT_LENGTH_TYPE   short
656 #else
657 #  define HOSTENT_ADDRTYPE_TYPE int
658 #  define HOSTENT_LENGTH_TYPE   int
659 #endif
660 
661 #endif /* __ARES_PRIVATE_H */
662