---
name: "ares_init_options"
section: "3"
platform: "linux"
package: "c-ares"
version: "1.34.9-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/ares_init_options"
---

# ares\_init\_options(3)

Initialize a resolver channel

## NAME

ares\_init\_options, ares\_init - Initialize a resolver channel

## SYNOPSIS

```
#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

The **[ares\_init(3)](/ares_init/3.md)** function is equivalent to calling **ares\_init\_options(channelptr, NULL, 0)**.  It is recommended to use **[ares\_init\_options(3)](/ares_init_options/3.md)** instead and to set or make configurable the appropriate options for your application.

The **[ares\_init\_options(3)](/ares_init_options/3.md)** function initializes a communications channel for name service lookups.  If it returns successfully, **[ares\_init\_options(3)](/ares_init_options/3.md)** 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)](/ares_destroy/3.md)* on the handle when the channel is no longer needed.

It is recommended for an application to 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)](/ares_reinit/3.md)* 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)](/ares_getaddrinfo/3.md)** or **[ares\_gethostbyname(3)](/ares_gethostbyname/3.md)**, they may spawn 2 queries internally.  The reason for the 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.

The *optmask* parameter generally specifies which fields in the structure pointed to by *options* are set, as follows:

- **ARES\_OPT\_FLAGS**

  **int *flags*;**\
   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 if *ARES\_FLAG\_NORECURSE* is 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 the HOSTALIASES environment variable, which normally specifies a file of hostname translations.

  - **ARES\_FLAG\_NOCHECKRESP**

    Do not discard responses with the SERVFAIL, NOTIMP, or REFUSED response 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 of v1.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 with *ARES\_ENOSERVER*.

  - **ARES\_FLAG\_DNS0x20**

    Enable support for DNS 0x20 as per https://datatracker.ietf.org/doc/html/draft-vixie-dnsext-dns0x20-00 which 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.  See *ARES\_OPT\_TIMEOUTMS* which 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 below 250ms will automatically be set to 250ms (roughly the RTT half-way around the world). Note that this option is specified with the same struct field as the former *ARES\_OPT\_TIMEOUT*, it is but the option bits that tell c-ares how to interpret the number. This option was added in c-ares 1.5.2.

  As of c-ares 1.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 is 1 unless set otherwise by resolv.conf or the RES\_OPTIONS environment variable.  Valid range is 0-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 is 53, 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 is 53, the standard name service port.

- **ARES\_OPT\_SERVERS**

  **struct in\_addr \**servers*;**\
   **int *nservers*;**\
   The list of IPv4 servers to contact, instead of the servers specified in resolv.conf or the local named. In order to allow specification of either IPv4 or IPv6 name servers, the  instead.

- **ARES\_OPT\_DOMAINS**

  **char \*\**domains*;**\
   **int *ndomains*;**\
   The domains to search, instead of the domains specified in resolv.conf or the domain derived from the kernel hostname variable.

- **ARES\_OPT\_LOOKUPS**

  **char \**lookups*;**\
   The lookups to perform for host queries. *lookups* should 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**

  **void (\**sock\_state\_cb*)(void \*data, ares\_socket\_t socket\_fd, int readable, int writable);**\
   **void \**sock\_state\_cb\_data*;**\
   A callback function to be invoked when a socket changes state. *socket\_fd* will be passed the socket whose state has changed; *readable* will be set to true if the socket should listen for read events, and *writable* will be set to true if the socket should listen for write events.

  This callback may call c-ares functions on the same channel, including [**ares\_cancel**(3)](/ares_cancel/3.md) and the server configuration functions. Notifications are delivered once the library has finished the work that produced them rather than part-way through it, so a reentrant call cannot close a socket the library is still using. They arrive in the order the state changes occurred, and always before the outermost c-ares call returns. A call made from within a callback is not the outermost one, so notifications it produces arrive after it has returned. [**ares\_destroy**(3)](/ares_destroy/3.md) is no exception: the closure of every socket the channel still holds is reported before it returns, and the callback is never invoked afterwards. 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* will be passed as the *data* argument.  The channel lock is held during this callback, if in a multithreaded application, care must be taken to ensure lock order is correct to be able to handle this and avoid deadlocks.

  Cannot be used with **ARES\_OPT\_EVENT\_THREAD**.

- **ARES\_OPT\_SORTLIST**

  **struct apattern \**sortlist*;**\
   **int *nsort*;**\
   A list of IP address ranges that specifies the order of preference that results from *ares\_gethostbyname* should be returned in.  Note that this can only be used with a sortlist retrieved via **[ares\_save\_options(3)](/ares_save_options/3.md)** (because **struct apattern** is opaque); to set a fresh sort list, use **[ares\_set\_sortlist(3)](/ares_set_sortlist/3.md)**.

- **ARES\_OPT\_SOCK\_SNDBUF**

  **int *socket\_send\_buffer\_size*;**\
   The send buffer size to set for the socket.

- **ARES\_OPT\_SOCK\_RCVBUF**

  **int *socket\_receive\_buffer\_size*;**\
   The receive buffer size to set for the socket.

- **ARES\_OPT\_EDNSPSZ**

  **int *ednspsz*;**\
   The message size to be advertised in EDNS; only takes effect if the **ARES\_FLAG\_EDNS** flag is set.  Defaults to 1232, the recommended size.

- **ARES\_OPT\_RESOLVCONF**

  **char \**resolvconf\_path*;**\
   The path to use for reading the resolv.conf file. The *resolvconf\_path* should be set to a path string, and will be honoured on \*nix like systems. The default is **/etc/resolv.conf**\

- **ARES\_OPT\_HOSTS\_FILE**

  **char \**hosts\_path*;**\
   The path to use for reading the hosts file. The *hosts\_path* should be set to a path string, and will be honoured on \*nix like systems. The default is **/etc/hosts**\

- **ARES\_OPT\_UDP\_MAX\_QUERIES**

  **int *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 of 0 or less will be considered unlimited, and is the default.\

- **ARES\_OPT\_QUERY\_CACHE**

  **unsigned int *qcache\_max\_ttl*;**\
   As of c-ares 1.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 of 0.  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 the *qcache\_max\_ttl* with 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 as 300 (5 minutes) or 3600 (1 hour).  The query cache is automatically flushed if a server configuration change is made.  As of c-ares 1.35.0, and c-ares 1.34.9 on 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**

  **ares\_evsys\_t *evsys*;**\
   Enable the built-in event thread (Recommended). Introduced in c-ares 1.26.0. Set the *evsys* parameter to **ARES\_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)](/ares_set_socket_functions/3.md)* or *[ares\_set\_socket\_configure\_callback(3)](/ares_set_socket_configure_callback/3.md)* functions.

  When enabled, the integrator is no longer responsible for notifying c-ares of any events on the file descriptors, so *[ares\_process(3)](/ares_process/3.md)* nor *[ares\_process\_fd(3)](/ares_process_fd/3.md)* 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)](/ares_threadsafety/3.md)* 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**

  **struct ares\_server\_failover\_options *server\_failover\_opts*;**\
   Configure 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.  However, in order to detect when such a server has recovered, c-ares will occasionally retry failed servers by probing with a copy of the query, without affecting the latency of the actual requested query.  The *ares\_server\_failover\_options* structure contains options to control this behavior. The *retry\_chance* field gives the probability (1/N) of retrying a failed server on any given query.  Setting to a value of 0 disables retries. The *retry\_delay* field gives the minimum delay in milliseconds that c-ares will wait before retrying a specific failed server. If this option is not specificed 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)](/ares_init_options/3.md)** and **[ares\_init(3)](/ares_init/3.md)** 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)](/ares_init_options/3.md)** and **[ares\_init(3)](/ares_init/3.md)** reads 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)](/resolv.conf/5.md) manual page.

## SEE ALSO

[**ares\_reinit**(3)](/ares_reinit/3.md), [**ares\_destroy**(3)](/ares_destroy/3.md), [**ares\_dup**(3)](/ares_dup/3.md), [**ares\_library\_init**(3)](/ares_library_init/3.md), [**ares\_save\_options**(3)](/ares_save_options/3.md), [**ares\_set\_servers**(3)](/ares_set_servers/3.md), [**ares\_set\_sortlist**(3)](/ares_set_sortlist/3.md), [**ares\_threadsafety**(3)](/ares_threadsafety/3.md)
