deimos.event2.dns

@file event2/dns.h

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.

Quick start guide: #include "evdns.h" void callback(int result, char type, int count, int ttl, void* addresses, void* arg); evdns_resolv_conf_parse(DNS_OPTIONS_ALL, "/etc/resolv.conf"); evdns_resolve("www.hostname.com", 0, callback, NULL);

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: 1. Never set it up. If you never call evdns_resolv_conf_parse or evdns_search_add then no searching will occur.

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

3. 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.

Public Imports

deimos.event2.event_struct
public import deimos.event2.event_struct;
deimos.event2.util
public import deimos.event2.util;

Members

Aliases

evdns_callback_type
alias evdns_callback_type = ExternC!(void function(int result, char type, int count, int ttl, void* addresses, void* arg))

The callback that contains the results from a lookup. - result is one of the DNS_ERR_* values (DNS_ERR_NONE for success) - type is either DNS_IPv4_A or DNS_PTR or DNS_IPv6_AAAA - count contains the number of addresses of form type - ttl is the number of seconds the resolution may be cached for. - addresses needs to be cast according to type. It will be an array of 4-byte sequences for ipv4, or an array of 16-byte sequences for ipv6, or a nul-terminated string for PTR.

evdns_debug_log_fn_type
alias evdns_debug_log_fn_type = ExternC!(void function(int is_warning, const(char)* msg))

A callback that is invoked when a log message is generated

evdns_getaddrinfo_cb
alias evdns_getaddrinfo_cb = ExternC!(void function(int result, evutil_addrinfo* res, void* arg))

Callback for evdns_getaddrinfo.

evdns_request_callback_fn_type
alias evdns_request_callback_fn_type = ExternC!(void function(evdns_server_request*, void*))

A callback to implement a DNS server. 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.

Functions

evdns_add_server_port_with_base
evdns_server_port* evdns_add_server_port_with_base(event_base* base, evutil_socket_t socket, int flags, evdns_request_callback_fn_type callback, void* user_data)

Create a new DNS server port.

evdns_base_clear_nameservers_and_suspend
int evdns_base_clear_nameservers_and_suspend(evdns_base* base)

Remove all configured nameservers, and suspend all pending resolves.

evdns_base_config_windows_nameservers
int evdns_base_config_windows_nameservers(evdns_base* )

Obtain nameserver information using the Windows API.

evdns_base_count_nameservers
int evdns_base_count_nameservers(evdns_base* base)

Get the number of configured nameservers.

evdns_base_free
void evdns_base_free(evdns_base* base, int fail_requests)

Shut down the asynchronous DNS resolver and terminate all active requests.

evdns_base_load_hosts
int evdns_base_load_hosts(evdns_base* base, const(char)* hosts_fname)

Load an /etc/hosts-style file from 'hosts_fname' into 'base'.

evdns_base_nameserver_add
int evdns_base_nameserver_add(evdns_base* base, c_ulong address)

Add a nameserver.

evdns_base_nameserver_ip_add
int evdns_base_nameserver_ip_add(evdns_base* base, const(char)* ip_as_string)

Add a nameserver by string address.

evdns_base_nameserver_sockaddr_add
int evdns_base_nameserver_sockaddr_add(evdns_base* base, const(sockaddr)* sa, ev_socklen_t len, uint flags)

Add a nameserver by sockaddr.

evdns_base_new
evdns_base* evdns_base_new(event_base* event_base, int initialize_nameservers)

Initialize the asynchronous DNS library.

evdns_base_resolv_conf_parse
int evdns_base_resolv_conf_parse(evdns_base* base, int flags, const(char)* filename)

Parse a resolv.conf file.

evdns_base_resolve_ipv4
evdns_request* evdns_base_resolve_ipv4(evdns_base* base, const(char)* name, int flags, evdns_callback_type callback, void* ptr)

Lookup an A record for a given name.

evdns_base_resolve_ipv6
evdns_request* evdns_base_resolve_ipv6(evdns_base* base, const(char)* name, int flags, evdns_callback_type callback, void* ptr)

Lookup an AAAA record for a given name.

evdns_base_resolve_reverse
evdns_request* evdns_base_resolve_reverse(evdns_base* base, const(in_addr)* in_, int flags, evdns_callback_type callback, void* ptr)

Lookup a PTR record for a given IP address.

evdns_base_resolve_reverse_ipv6
evdns_request* evdns_base_resolve_reverse_ipv6(evdns_base* base, in6_addr* in_, int flags, evdns_callback_type callback, void* ptr)

Lookup a PTR record for a given IPv6 address.

evdns_base_resume
int evdns_base_resume(evdns_base* base)

Resume normal operation and continue any suspended resolve requests.

evdns_base_search_add
void evdns_base_search_add(evdns_base* base, const(char)* domain_)

Add a domain to the list of search domains

evdns_base_search_clear
void evdns_base_search_clear(evdns_base* base)

Clear the list of search domains.

evdns_base_search_ndots_set
void evdns_base_search_ndots_set(evdns_base* base, int ndots)

Set the 'ndots' parameter for searches.

evdns_base_set_option
int evdns_base_set_option(evdns_base* base, const(char)* option, const(char)* val)

Set the value of a configuration option.

evdns_cancel_request
void evdns_cancel_request(evdns_base* base, evdns_request* req)

Cancels a pending DNS resolution request.

evdns_close_server_port
void evdns_close_server_port(evdns_server_port* port)

Close down a DNS server port, and free associated structures.

evdns_err_to_string
const(char)* evdns_err_to_string(int err)

Convert a DNS error code to a string.

evdns_getaddrinfo
evdns_getaddrinfo_request* evdns_getaddrinfo(evdns_base* dns_base, const(char)* nodename, const(char)* servname, const(evutil_addrinfo)* hints_in_, evdns_getaddrinfo_cb cb, void* arg)

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

evdns_getaddrinfo_cancel
void evdns_getaddrinfo_cancel(evdns_getaddrinfo_request* req)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request_add_a_reply
int evdns_server_request_add_a_reply(evdns_server_request* req, const(char)* name, int n, const(void)* addrs, int ttl)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request_add_aaaa_reply
int evdns_server_request_add_aaaa_reply(evdns_server_request* req, const(char)* name, int n, const(void)* addrs, int ttl)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request_add_cname_reply
int evdns_server_request_add_cname_reply(evdns_server_request* req, const(char)* name, const(char)* cname, int ttl)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request_add_ptr_reply
int evdns_server_request_add_ptr_reply(evdns_server_request* req, in_addr* in_, const(char)* inaddr_name, const(char)* hostname, int ttl)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request_add_reply
int evdns_server_request_add_reply(evdns_server_request* req, int section, const(char)* name, int type, int dns_class, int ttl, int datalen, int is_name, const(char)* data)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request_drop
int evdns_server_request_drop(evdns_server_request* req)

Free a DNS request without sending back a reply.

evdns_server_request_get_requesting_addr
int evdns_server_request_get_requesting_addr(evdns_server_request* _req, sockaddr* sa, int addr_len)

Get the address that made a DNS request.

evdns_server_request_respond
int evdns_server_request_respond(evdns_server_request* req, int err)

Send back a response to a DNS request, and free the request structure.

evdns_server_request_set_flags
void evdns_server_request_set_flags(evdns_server_request* req, int flags)

Sets some flags in a reply we're building. Allows setting of the AA or RD flags

evdns_set_log_fn
void evdns_set_log_fn(evdns_debug_log_fn_type fn)

Set the callback function to handle DNS log messages. If this callback is not set, evdns log messages are handled with the regular Libevent logging system.

evdns_set_random_bytes_fn
void evdns_set_random_bytes_fn(ExternC!(void function(char*, size_t)) fn)

Set a callback used to generate random bytes. By default, we use the same function as passed to evdns_set_transaction_id_fn to generate bytes two at a time. If a function is provided here, it's also used to generate transaction IDs.

evdns_set_transaction_id_fn
void evdns_set_transaction_id_fn(ExternC!(ev_uint16_t function()) fn)

Set a callback that will be invoked to generate transaction IDs. By default, we pick transaction IDs based on the current clock time, which is bad for security.

Manifest constants

DNS_ERR_CANCEL
enum DNS_ERR_CANCEL;

The request was canceled via a call to evdns_cancel_request

DNS_ERR_FORMAT
enum DNS_ERR_FORMAT;

The name server was unable to interpret the query

DNS_ERR_NODATA
enum DNS_ERR_NODATA;

There were no answers and no error condition in the DNS packet. * This can happen when you ask for an address that exists, but a record * type that doesn't.

DNS_ERR_NONE
enum DNS_ERR_NONE;

Error codes 0-5 are as described in RFC 1035.

DNS_ERR_NOTEXIST
enum DNS_ERR_NOTEXIST;

The domain name does not exist

DNS_ERR_NOTIMPL
enum DNS_ERR_NOTIMPL;

The name server does not support the requested kind of query

DNS_ERR_REFUSED
enum DNS_ERR_REFUSED;

The name server refuses to reform the specified operation for policy * reasons

DNS_ERR_SERVERFAILED
enum DNS_ERR_SERVERFAILED;

The name server was unable to process this query due to a problem with the * name server

DNS_ERR_SHUTDOWN
enum DNS_ERR_SHUTDOWN;

The request was canceled because the DNS subsystem was shut down.

DNS_ERR_TIMEOUT
enum DNS_ERR_TIMEOUT;

Communication with the server timed out

DNS_ERR_TRUNCATED
enum DNS_ERR_TRUNCATED;

The reply was truncated or ill-formatted

DNS_ERR_UNKNOWN
enum DNS_ERR_UNKNOWN;

An unknown error occurred

DNS_IPv4_A
enum DNS_IPv4_A;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_IPv6_AAAA
enum DNS_IPv6_AAAA;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_NO_SEARCH
enum DNS_NO_SEARCH;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_OPTIONS_ALL
enum DNS_OPTIONS_ALL;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_OPTION_HOSTSFILE
enum DNS_OPTION_HOSTSFILE;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_OPTION_MISC
enum DNS_OPTION_MISC;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_OPTION_NAMESERVERS
enum DNS_OPTION_NAMESERVERS;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_OPTION_SEARCH
enum DNS_OPTION_SEARCH;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_PTR
enum DNS_PTR;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
DNS_QUERY_NO_SEARCH
enum DNS_QUERY_NO_SEARCH;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_ADDITIONAL_SECTION
enum EVDNS_ADDITIONAL_SECTION;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_ANSWER_SECTION
enum EVDNS_ANSWER_SECTION;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_AUTHORITY_SECTION
enum EVDNS_AUTHORITY_SECTION;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED
enum EVDNS_BASE_CONFIG_WINDOWS_NAMESERVERS_IMPLEMENTED;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_CLASS_INET
enum EVDNS_CLASS_INET;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_FLAGS_AA
enum EVDNS_FLAGS_AA;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_FLAGS_RD
enum EVDNS_FLAGS_RD;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_QTYPE_ALL
enum EVDNS_QTYPE_ALL;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_QTYPE_AXFR
enum EVDNS_QTYPE_AXFR;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_A
enum EVDNS_TYPE_A;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_AAAA
enum EVDNS_TYPE_AAAA;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_CNAME
enum EVDNS_TYPE_CNAME;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_MX
enum EVDNS_TYPE_MX;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_NS
enum EVDNS_TYPE_NS;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_PTR
enum EVDNS_TYPE_PTR;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_SOA
enum EVDNS_TYPE_SOA;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
EVDNS_TYPE_TXT
enum EVDNS_TYPE_TXT;
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Structs

evdns_base
struct evdns_base
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_getaddrinfo_request
struct evdns_getaddrinfo_request
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_request
struct evdns_request
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_port
struct evdns_server_port
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_question
struct evdns_server_question
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
evdns_server_request
struct evdns_server_request
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
in6_addr
struct in6_addr
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
in_addr
struct in_addr
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Meta