ARES_INIT_OPTIONS(3)
π ARES_INIT_OPTIONS(3) β Library Functions Manual π
β¨ NAME β¨
ares_init_options, ares_init β Initialize a resolver channel, bestie. That's the whole vibe: you're setting up the channel c-ares uses for name service lookups. π
π¦ SYNOPSIS π¦
Okay so here's the exact code shape, no cap:
#include <ares.h>
struct ares_server_failover_options {
unsigned short retry_chance;
size_t retry_delay;
};
struct ares_options {
int flags;
int timeout; /* in seconds or milliseconds, depending on options */
int tries;
int ndots;
unsigned short udp_port;
unsigned short tcp_port;
int socket_send_buffer_size;
int socket_receive_buffer_size;
struct in_addr *servers;
int nservers;
char **domains;
int ndomains;
char *lookups;
ares_sock_state_cb sock_state_cb;
void *sock_state_cb_data;
struct apattern *sortlist;
int nsort;
int ednspsz;
char *resolvconf_path;
char *hosts_path;
int udp_max_queries;
int maxtimeout; /* in milliseconds */
unsigned int qcache_max_ttl; /* in seconds */
ares_evsys_t evsys;
struct ares_server_failover_options server_failover_opts;
};
int ares_init_options(ares_channel_t **channelptr,
const struct ares_options *options,
int optmask);
int ares_init(ares_channel_t **channelptr);
π DESCRIPTION π
Okay so ares_init(3) is literally the same as calling ares_init_options(channelptr, NULL, 0). But the page says it's recommended to use ares_init_options(3) instead, and to set or make configurable the appropriate options for your application. Slay. β¨
ares_init_options(3) initializes a communications channel for name service lookups. If it returns successfully, ares_init_options(3) will set the variable pointed to by channelptr to a handle used to identify the name service channel. The caller should invoke ares_destroy(3) on the handle when the channel is no longer needed. Don't forget the cleanup era, bestie. π§
Now the recommendation: an application should have at most one ares channel, and use this for all DNS queries for the life of the application. When system configuration changes, ares_reinit(3) can be called to reload the configuration if necessary. The recommended concurrent query limit is about 32k queries β but remembering that when specifying AF_UNSPEC for ares_getaddrinfo(3) or ares_gethostbyname(3), they may spawn 2 queries internally. The reason for this limit is c-ares does not allow duplicate DNS query ids (which have a maximum of 64k) to be oustanding at a given time, and it must randomly search for an available id thus 32k will limit the number of searches. This limitation should not be a concern for most implementations and c-ares may implement queuing in future releases to lift this limitation. So like, don't stress it for most stuff. π
The optmask parameter generally specifies which fields in the structure pointed to by options are set, as follows:
ARES_OPT_FLAGSβint flags;β these are the flags controlling the behavior of the resolver:ARES_FLAG_USEVCβ Always use TCP queries (the "virtual circuit") instead of UDP queries. Normally, TCP is only used if a UDP query yields a truncated result.ARES_FLAG_PRIMARYβ Only query the first server in the list of servers to query.ARES_FLAG_IGNTCβ If a truncated response to a UDP query is received, do not fall back to TCP; simply continue on with the truncated response.ARES_FLAG_NORECURSEβ Do not set the "recursion desired" bit on outgoing queries, so that the name server being contacted will not try to fetch the answer from other servers if it doesn't know the answer locally. Be aware that ares will not do the recursion for you. Recursion must be handled by the application calling ares ifARES_FLAG_NORECURSEis set.ARES_FLAG_STAYOPENβ Do not close communications sockets when the number of active queries drops to zero.ARES_FLAG_NOSEARCHβ Do not use the default search domains; only query hostnames as-is or as aliases.ARES_FLAG_NOALIASESβ Do not honor theHOSTALIASESenvironment variable, which normally specifies a file of hostname translations.ARES_FLAG_NOCHECKRESPβ Do not discard responses with theSERVFAIL,NOTIMP, orREFUSEDresponse code or responses whose questions don't match the questions in the request. Primarily useful for writing clients which might be used to test or debug name servers.ARES_FLAG_EDNSβ Include an EDNS pseudo-resource record (RFC 2671) in generated requests. As ofv1.22, this is on by default if flags are otherwise not set.ARES_FLAG_NO_DFLT_SVRβ Do not attempt to add a default local named server if there are no other servers available. Instead, fail initialization withARES_ENOSERVER.ARES_FLAG_DNS0x20β Enable support for DNS 0x20 as perhttps://datatracker.ietf.org/doc/html/draft-vixie-dnsext-dns0x20-00which adds additional entropy to the request by randomizing the case of the query name. Integrators need to ensure they treat DNS name responses as case-insensitive. In rare circumstances this may cause the inability to lookup certain domains if the upstream server or the authoritative server for the domain is non-compliant.
ARES_OPT_TIMEOUTβint timeout;β the number of seconds each name server is given to respond to a query on the first try. SeeARES_OPT_TIMEOUTMSwhich this value is converted into.ARES_OPT_TIMEOUTMSβint timeout;β the number of milliseconds each name server is given to respond to a query on the first try of any given server. The default is two seconds, however any value below250mswill automatically be set to250ms(roughly the RTT half-way around the world). Note that this option is specified with the same struct field as the formerARES_OPT_TIMEOUT, it is but the option bits that tell c-ares how to interpret the number. This option was added in c-ares1.5.2. As of c-ares1.32.0, this option is only honored on the first successful query to any given server, after that the timeout is automatically calculated based on prior query history.ARES_OPT_TRIESβint tries;β the number of tries the resolver will try contacting each name server before giving up. The default is three tries.ARES_OPT_NDOTSβint ndots;β the number of dots which must be present in a domain name for it to be queried for "as is" prior to querying for it with the default domain extensions appended. The default value is1unless set otherwise byresolv.confor theRES_OPTIONSenvironment variable. Valid range is0-15.ARES_OPT_MAXTIMEOUTMSβint maxtimeout;β the upper bound for timeout between sequential retry attempts. When retrying queries, the timeout is increased from the requested timeout parameter, this caps the value.ARES_OPT_UDP_PORTβunsigned short udp_port;β the port to use for queries over UDP, in host byte order. The default value is53, the standard name service port.ARES_OPT_TCP_PORTβunsigned short tcp_port;β the port to use for queries over TCP, in host byte order. The default value is53, the standard name service port.ARES_OPT_SERVERSβstruct in_addr *servers;andint nservers;β the list of IPv4 servers to contact, instead of the servers specified inresolv.confor the local named. And for real, the page's wording here is exactly:In order to allow specification of either IPv4 or IPv6 name servers, the 0 instead.πARES_OPT_DOMAINSβchar **domains;andint ndomains;β the domains to search, instead of the domains specified inresolv.confor the domain derived from the kernel hostname variable.ARES_OPT_LOOKUPSβchar *lookups;β the lookups to perform for host queries.lookupsshould be set to a string of the characters"b"or"f", where"b"indicates a DNS lookup and"f"indicates a lookup in the hosts file.ARES_OPT_SOCK_STATE_CBβ a callback function to be invoked when a socket changes state, plus its data pointer. The signature isvoid (*sock_state_cb)(void *data, ares_socket_t socket_fd, int readable, int writable);, and the data field isvoid *sock_state_cb_data;.socket_fdwill be passed the socket whose state has changed;readablewill be set to true if the socket should listen for read events, andwritablewill be set to true if the socket should listen for write events.
β¦okay bestie we're still on ARES_OPT_SOCK_STATE_CB, keep up π
This callback may call c-ares functions on the same channel, including ares_cancel(3) and the server configuration functions. Notifications get delivered once the library has finished the work that produced them, not part-way through it β so a reentrant call can't close a socket the library is still using. π«Ά They arrive in the order the state changes actually happened, and always before the outermost c-ares call returns. A call made from inside a callback isn't the outermost one, so the notifications it produces arrive after it has returned. ares_destroy(3) is no exception: the closure of every socket the channel still holds is reported before it returns, and the callback is never invoked afterwards. All of this applies as of c-ares version 1.35.0, and c-ares version 1.34.9 on the 1.34 branch. β¨
The value of sock_state_cb_data gets passed as the data argument. Heads up: the channel lock is held during this callback, so if you're in a multithreaded application, care must be taken to ensure lock order is correct so you can actually handle this and avoid deadlocks. π
Cannot be used with ARES_OPT_EVENT_THREAD.
ARES_OPT_SORTLISTβ fields arestruct apattern *sortlist;andint nsort;π¦ A list of IP address ranges that specifies the order of preference that results fromares_gethostbynameshould be returned in. Note that this can only be used with a sortlist retrieved viaares_save_options(3)(becausestruct apatternis opaque); to set a fresh sort list, useares_set_sortlist(3).ARES_OPT_SOCK_SNDBUFβ field isint socket_send_buffer_size;β the send buffer size to set for the socket. πARES_OPT_SOCK_RCVBUFβ field isint socket_receive_buffer_size;β the receive buffer size to set for the socket. πARES_OPT_EDNSPSZβ field isint ednspsz;β the message size to be advertised in EDNS; only takes effect if theARES_FLAG_EDNSflag is set. Defaults to1232, the recommended size. πARES_OPT_RESOLVCONFβ field ischar *resolvconf_path;β the path to use for reading theresolv.conffile. Theresolvconf_pathshould be set to a path string, and will be honoured on *nix like systems. The default is/etc/resolv.conf.ARES_OPT_HOSTS_FILEβ field ischar *hosts_path;β the path to use for reading the hosts file. Thehosts_pathshould be set to a path string, and will be honoured on *nix like systems. The default is/etc/hosts.ARES_OPT_UDP_MAX_QUERIESβ field isint udp_max_queries;β the maximum number of udp queries that can be sent on a single ephemeral port to a given DNS server before a new ephemeral port is assigned. Any value of0or less is considered unlimited, and that's the default. βARES_OPT_QUERY_CACHEβ field isunsigned int qcache_max_ttl;β okay so as of c-ares1.31.0, the query cache is enabled by default with a TTL of 1hr. To disable the query cache, specify this option with a TTL of0. The query cache is based on the returned TTL in the DNS message. Only fully successful and NXDOMAIN query results will be cached. Fill in theqcache_max_ttlwith the maximum number of seconds a query result may be cached, which will override a larger TTL in the response message. This must be a non-zero value, otherwise the cache will be disabled. Choose a reasonable value for your application such as300(5 minutes) or3600(1 hour). The query cache is automatically flushed if a server configuration change is made. As of c-ares1.35.0, and c-ares1.34.9on the 1.34 branch, a cached answer reports its remaining time to live rather than the original value received from the server, so TTLs returned on a cache hit decrease as the entry ages. Two lookups share a cached answer only if their EDNS state matches: the version, the flag bits including DNSSEC OK, and every EDNS option other than the DNS cookie and the advertised UDP payload size, which are both ignored. Options are compared in the order the application added them. An application that attaches its own options and varies them between otherwise identical lookups gets a separate cache entry for each. π§ARES_OPT_EVENT_THREADβ field isares_evsys_t evsys;β enables the built-in event thread (Recommended π₯). Introduced in c-ares1.26.0. Set theevsysparameter toARES_EVSYS_DEFAULT(0). Other values are reserved for testing and should not be used by integrators.
This option cannot be used with the ARES_OPT_SOCK_STATE_CB option, nor the ares_set_socket_functions(3) or ares_set_socket_configure_callback(3) functions.
When enabled, the integrator is no longer responsible for notifying c-ares of any events on the file descriptors, so ares_process(3) nor ares_process_fd(3) should ever be called when this option is enabled. π
As of c-ares 1.29.0, when enabled, it will also automatically re-load the system configuration when changes are detected.
Use ares_threadsafety(3) to determine if this option is available to be used.
Returns ARES_ENOTIMP if this option is passed but not available, and ARES_ESERVFAIL if there is a critical failure during initialization of the event thread.
ARES_OPT_SERVER_FAILOVERβ field isstruct ares_server_failover_options server_failover_opts;β configures server failover retry behavior. When a DNS server fails to respond to a query, c-ares will deprioritize the server. On subsequent queries, servers with fewer consecutive failures will be selected in preference. But so c-ares can detect when such a server has recovered, it will occasionally retry failed servers by probing with a copy of the query, without affecting the latency of the actual requested query. Theares_server_failover_optionsstructure contains options to control this behavior. Theretry_chancefield gives the probability (1/N) of retrying a failed server on any given query β setting it to a value of0disables retries. Theretry_delayfield gives the minimum delay in milliseconds that c-ares will wait before retrying a specific failed server. If this option is not specified then c-ares will use a probability of 10% and a minimum delay of 5 seconds. π
The optmask parameter also includes options without a corresponding field in the ares_options structure, as follows: π
ARES_OPT_ROTATEβ perform round-robin selection of the nameservers configured for the channel for each resolution. πARES_OPT_NOROTATEβ do not perform round-robin nameserver selection; always use the list of nameservers in the same order. The default is not to rotate servers, however the system configuration can specify the desire to rotate and this configuration value can negate such a system configuration.
β¨ RETURN VALUES β¨
ares_init_options(3) and ares_init(3) can return any of the following values:
ARES_SUCCESSβ initialization succeeded. πARES_EFILEβ a configuration file could not be read. πARES_ENOMEMβ the process's available memory was exhausted. πARES_ENOTINITIALIZEDβ c-ares library initialization not yet performed. πARES_ENOSERVERβ no DNS servers were available to use. π«
π NOTES π
When initializing from /etc/resolv.conf (or, alternatively when specified by the resolvconf_path path location), ares_init_options(3) and ares_init(3) read the domain and search directives to allow lookups of short names relative to the domains specified. The domain and search directives override one another. If more than one instance of either domain or search directives is specified, the last occurrence wins. For more information, please see the resolv.conf(5) manual page. π
πΈ SEE ALSO πΈ
ares_reinit(3), ares_destroy(3), ares_dup(3), ares_library_init(3), ares_save_options(3), ares_set_servers(3), ares_set_sortlist(3), ares_threadsafety(3)
and that's a wrap, bestie!! π«Άπβ¨ This page is the ARES_INIT_OPTIONS(3) man page from the c-ares manual, dated 5 March 2010 β so go forth, init your options, and vibe responsibly. π
π
π¦ where this page comes from
- alpine 3.22: c-ares-doc 1.34.9-r0 Β· MIT
- alpine 3.23: c-ares-doc 1.34.9-r0 Β· MIT
- alpine 3.24: c-ares-doc 1.34.9-r0 Β· MIT
- arch rolling: c-ares 1.34.9-1 Β· MIT
- centos-stream 10: c-ares-devel 1.34.9-1.el10 Β· MIT
- nixos 26.05: c-ares 1.34.9
- π the page's license: MIT
- Copyright (C) 2023 The c-ares project and its contributors
- Copyright 1998 by the Massachusetts Institute of Technology
- Copyright (C) 2004-2010 by Daniel Stenberg
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms