Many calls in the DNS API require a DNS context. A DNS context contains the information that the API needs in order to process DNS calls, such as the locations of upstream DNS servers, DNSSEC trust anchors, and so on. The internal structure of the DNS context is opaque, and might be different on each OS. When a context is passed to any function, it must be an allocated context; the context must not be NULL.
.LP
A typical application using this API doesn't need to know anything about contexts. Basically, the application creates a default context, uses it in the functions that require a context, and then deallocates it when done. Context manipulation is available for more DNS-aware programs, but is unlikely to be of interest to applications that just want the results of lookups for A, AAAA, SRV, and PTR records.
.LP
It is expected that contexts in implementations of the API will not necessarily be thread-safe, but they will not be thread-hostile. A context should not be used by multiple threads: create a new context for use on a different thread. It is just fine for an application to have many contexts, and some DNS-heavy applications will certainly want to have many even if the application uses a single thread.
.LP
When the context is used in the API for the first time and set_from_os is 1, the API starts replacing some of the values with values from the OS, such as those that would be found in res_query(3), /etc/resolv.conf, and so on, then proceeds with the new function. Some advanced users will not want the API to change the values to the OS's defaults; if set_from_os is 0, the API will not do any updates to the initial values based on changes in the OS. For example, this might be useful if the API is acting as a stub resolver that is using a specific upstream recursive resolver chosen by the application, not the one that might come back from DHCP.
.HP3
.Icontext
.RP
In calls to the getdns_create_context functions this parameter is used to return a newly allocated and initialized context (if there are no errors). In the getdns_destroy_context function this is the context whose associated memory will be released.
If set_from_os is 0 then the caller must provide forwarding name servers if running in stub mode. If set_from_os is 1 then the system files are used to initialize the context. /etc/resolv.conf is used to populate forwarders when running as a stub resolver (only "nameserver" lines are recognized). If set_from_os is 1 /etc/hosts entries are preferred before resorting to a DNS query. Errors in the system files will not prevent the context form being constructed.
In the extended use case this argument is passed unchanged to each of the memory management functions each time they are called.
.HP3
.Imalloc
.RP
The function that will be used for creating response dicts (and the members within the response dicts). By default the system malloc is used.
.HP3
.Irealloc
.RP
The function that will be used for creating response dicts (and the members within the response dicts). By default the system realloc is used.
.HP3
.Ifree
.RP
The function that will be used for releasing storage for response dicts (and the members within the response dicts). By default the system free is used.
.HP
.SH"RETURN VALUES"
Upon successful completion each of these functions return
.BGETDNS_RETURN_GOOD
, otherwise the following error values are returned:
.LP
.BGETDNS_RETURN_GENERIC_ERROR
memory allocation failed or some other untoward thing happened while initializing the context
.LP
.BGETDNS_RETURN_BAD_CONTEXT
if the context pointer is invalid (getdns_context_destroy)