Set a callback that will be invoked to generate transaction IDs. More...

Detailed Description

Welcome, gentle reader.

Async DNS lookups are really a whole lot harder than they should be, mostly stemming from the fact that the libc resolver has never been very good at them. Before you use this library you should see if libc can do the job for you with the modern async call getaddrinfo_a (see http://www.imperialviolet.org/page25.html#e498). Otherwise, please continue.

The library keeps track of the state of nameservers and will avoid them when they go down. Otherwise it will round robin between them.

When the lookup is complete the callback function is called. The first argument will be one of the DNS_ERR_* defines in evdns.h. Hopefully it will be DNS_ERR_NONE, in which case type will be DNS_IPv4_A, count will be the number of IP addresses, ttl is the time which the data can be cached for (in seconds), addresses will point to an array of uint32_t's and arg will be whatever you passed to evdns_resolve.

Searching:

In order for this library to be a good replacement for glibc's resolver it supports searching. This involves setting a list of default domains, in which names will be queried for. The number of dots in the query name determines the order in which this list is used.

Searching appears to be a single lookup from the point of view of the API, although many DNS queries may be generated from a single call to evdns_resolve. Searching can also drastically slow down the resolution of names.

To disable searching:

Never set it up. If you never call evdns_resolv_conf_parse or evdns_search_add then no searching will occur.

If you do call evdns_resolv_conf_parse then don't pass DNS_OPTION_SEARCH (or DNS_OPTIONS_ALL, which implies it).

When calling evdns_resolve, pass the DNS_QUERY_NO_SEARCH flag.

The order of searches depends on the number of dots in the name. If the number is greater than the ndots setting then the names is first tried globally. Otherwise each search domain is appended in turn.

The ndots setting can either be set from a resolv.conf, or by calling evdns_search_ndots_set.

For example, with ndots set to 1 (the default) and a search domain list of ["myhome.net"]: Query: www Order: www.myhome.net, www.

Query: www.abc Order: www.abc., www.abc.myhome.net

Internals:

Requests are kept in two queues. The first is the inflight queue. In this queue requests have an allocated transaction id and nameserver. They will soon be transmitted if they haven't already been.

The second is the waiting queue. The size of the inflight ring is limited and all other requests wait in waiting queue for space. This bounds the number of concurrent requests so that we don't flood the nameserver. Several algorithms require a full walk of the inflight queue and so bounding its size keeps thing going nicely under huge (many thousands of requests) loads.

If a nameserver loses too many requests it is considered down and we try not to use it. After a while we send a probe to that nameserver (a lookup for google.com) and, if it replies, we consider it working again. If the nameserver fails a probe we wait longer to try again with the next probe.

The callback function receives a DNS request. It should then optionally add a number of answers to the reply using the evdns_server_request_add_*_reply functions, before calling either evdns_server_request_respond to send the reply back, or evdns_server_request_drop to decline to answer the request.

This returns the number of configured nameservers (not necessarily the number of running nameservers). This is useful for double-checking whether our calls to the various nameserver configuration functions have been successful.

Make a non-blocking getaddrinfo request using the dns_base in 'dns_base'.

If we can answer the request immediately (with an error or not!), then we invoke cb immediately and return NULL. Otherwise we return an evdns_getaddrinfo_request and invoke cb later.

When the callback is invoked, we pass as its first argument the error code that getaddrinfo would return (or 0 for no error). As its second argument, we pass the evutil_addrinfo structures we found (or NULL on error). We pass 'arg' as the third argument.