---
name: "getdns_context_create_with_memory_functions"
section: "3"
platform: "linux"
package: "getdns"
version: "1.7.3-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/getdns_context_create_with_memory_functions"
---

# getdns\_context\_create\_with\_memory\_functions(3)

## NAME

**getdns\_context,** **getdns\_context\_create,** **getdns\_context\_create\_with\_memory\_functions,** **getdns\_context\_create\_with\_extended\_memory\_functions,** **getdns\_context\_destroy,** **getdns\_context\_get\_api\_information** -- getdns context create and destroy routines

## LIBRARY

DNS Resolver library (libgetdns, -lgetdns)

## SYNOPSIS

\#include \<getdns.h\>

getdns\_return\_t\
 **getdns\_context\_create** (getdns\_context \*\* context,

int set\_from\_os)

getdns\_return\_t\
 **getdns\_context\_create\_with\_memory\_functions** (getdns\_context \*\* context,

int set\_from\_os,\
 void \*(\*malloc) (size\_t),\
 void \*(\*realloc) (void \*, size\_t),\
 void (\*free) (void \*))

getdns\_return\_t\
 **getdns\_context\_create\_with\_extended\_memory\_functions** (getdns\_context \*\*context,

int set\_from\_os,\
 void \*userarg,\
 void \*(\*malloc) (void \*userarg, size\_t),\
 void \*(\*realloc) (void \*userarg, void \*, size\_t),\
 void (\*free) (void \*userarg, void \*))

void\
 **getdns\_context\_destroy** (getdns\_context \*context)

getdns\_dict \*\
 **getdns\_context\_get\_api\_information** (getdns\_context \*context)

## DESCRIPTION

Calls to getdns functions require a DNS context, which is a group of API settings that affect how DNS calls are made. For most applications, a default context is sufficient.

To create a new DNS context, use the function:

getdns\_return\_t **getdns\_context\_create** (getdns\_context\_t \*context, bool set\_from\_os)

The call to getdns\_context\_create immediately returns a context that can be used with other API calls; that context contains the API's default values. Most applications will want set\_from\_os set to true.

To clean up the context, including cleaning up all outstanding transactions that were called using this context, use the function:

void **getdns\_context\_destroy** (getdns\_context\_t context)

When getdns\_context\_destroy() returns, the application knows that all outstanding transactions associated with this context will have been called; callbacks that had not been called before getdns\_context\_destroy() was called will be called with a callback\_type of GETDNS\_CALLBACK\_CANCEL. getdns\_context\_destroy() returns after all of the needed cleanup is done and callbacks are made.

If you are using getdns in a multi-threaded manner, you are then of course using the underlying OpenSSL library multi-threaded and the version of that library in use might have a requirements on this issue. You may need to provide one or  two functions to allow it to function properly. For example before you call  getdns\_context\_create() you may need to use  the openssl functions CRYPTO\_set\_id\_callback and CRYPTO\_set\_locking\_callback to set up asynchronous operation (the  application calls these functions once for initialisation). Openssl 1.0.0 or later uses the CRYPTO\_THREADID\_set\_callback function.

*context* Used to return the pointer to an opaque structure.  The caller passes the address of a pointer (decl: getdns\_context \*context; passed as &context) which will be populated as a result of returning from the function.  The result is 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.

*set\_from\_os* 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.

*userarg* In the extended use case this argument is passed unchanged to each of the memory management functions each time they are called.

*malloc* The function that will be used for creating response dicts (and the members within the response dicts).  By default the system malloc is used.

*realloc* The function that will be used for creating response dicts (and the members within the response dicts).  By default the system realloc is used.

*free* 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.

## DESCRIPTION (LONG)

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.

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.

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.

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)](/res_query/3.md), /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.

## RETURN VALUES

Upon successful completion each of these functions return **GETDNS\_RETURN\_GOOD** , otherwise the following error values are returned:

**GETDNS\_RETURN\_GENERIC\_ERROR** memory allocation failed or some other untoward thing happened while initializing the context

**GETDNS\_RETURN\_BAD\_CONTEXT** if the context pointer is invalid (getdns\_context\_destroy)

The getdns\_dict returned by getdns\_context\_get\_api\_information must be destroyed by the called and includes the following name/value pairs:

- version\_string

  a bindata containing a printable string of the version of the DNS API implemented by this library

- implementation\_string

  a bindata containing a printable string set by the implementation

- resolution\_type

  an int equal to GETDNS\_RESOLUTION\_RECURSING or GETDNS\_RESOLUTION\_STUB

- all\_context

  a getdns\_dict with names for all the types of context, feed it to getdns\_pretty\_print\_dict (3) for something easily readable

## EXAMPLES

TBD

## FILES

/etc/hosts\
 /etc/resolv.conf

## SEE ALSO

[**libgetdns**(3)](/libgetdns/3.md), [**getdns\_address**(3)](/getdns_address/3.md), [**getdns\_address\_sync**(3)](/getdns_address_sync/3.md), [**getdns\_context\_set**(3)](/getdns_context_set/3.md), [**getdns\_context\_set\_context\_update\_callback**(3)](/getdns_context_set_context_update_callback/3.md), [**getdns\_general**(3)](/getdns_general/3.md), [**getdns\_general\_sync**(3)](/getdns_general_sync/3.md), [**getdns\_hostname**(3)](/getdns_hostname/3.md), [**getdns\_hostname\_sync**(3)](/getdns_hostname_sync/3.md), [**getdns\_service**(3)](/getdns_service/3.md), [**getdns\_service\_sync**(3)](/getdns_service_sync/3.md).
