lwres_context.3 revision 15a44745412679c30a6d022733925af70a38b715
Copyright (C) 2000 Internet Software Consortium.

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

$Id: lwres_context.3,v 1.2 2000/07/27 09:44:29 tale Exp $

.Dd Jun 30, 2000 .Dt LWRES_CONTEXT 3 .Os BIND9 9 .Sh NAME .Nm lwres_context_create , .Nm lwres_context_destroy , .Nm lwres_context_nextserial , .Nm lwres_context_initserial , .Nm lwres_context_freemem , .Nm lwres_context_allocmem , .Nm lwres_context_sendrecv .Nd lightweight resolver memory allocation routines .Sh SYNOPSIS .Fd #include <lwres/lwres.h> .Fd .Ft lwres_result_t .Fo lwres_context_create .Fa "lwres_context_t **contextp" .Fa "void *arg" .Fa "lwres_malloc_t malloc_function" .Fa "lwres_free_t free_function" .Fc .Ft lwres_result_t .Fo lwres_context_destroy .Fa "lwres_context_t **contextp" .Fc .Ft void .Fo lwres_context_nextserial .Fa "lwres_context_t *ctx" .Fc .Ft lwres_uint32_t .Fo lwres_context_initserial .Fa "lwres_context_t *ctx" .Fa "lwres_uint32_t serial" .Fc .Ft void .Fo lwres_context_freemem .Fa "lwres_context_t *ctx" .Fa "void *mem" .Fa "size_t len" .Fc .Ft void .Fo lwres_context_allocmem .Fa "lwres_context_t *ctx" .Fa "size_t len" .Fc .Ft void * .Fo lwres_context_sendrecv .Fa "lwres_context_t *ctx" .Fa "void *sendbase" .Fa "int sendlen" .Fa "void *recvbase" .Fa "int recvlen" .Fa "int *recvd_len" .Fc d -literal -offset indent struct lwres_context { unsigned int timeout; /* time to wait for reply */ lwres_uint32_t serial; /* serial number state */ /* * For network I/O. */ int sock; /* socket to send on */ /* * Function pointers for allocating memory. */ lwres_malloc_t malloc; lwres_free_t free; void *arg; /* * resolv.conf-like data */ lwres_conf_t confdata; }; typedef struct lwres_context lwres_context_t; .Ed .Sh DESCRIPTION .Fn lwres_context_create is used to create a .Dv lwres_context_t structure for use in lightweight resolver operations and associate general purpose memory allocation functions with that structure. .Fa *contextp is a pointer to a .Dv "struct lwres_context" . It must be non-NULL, which in turn implies that .Fa **contextp is also non-NULL. .Fa malloc_function and .Fa free_function are pointers to the functions the context should use for allocating and freeing memory respectively. Both function pointers must either be non-NULL or NULL. It is not permitted to use a custom allocator - i.e .Fa malloc_function is non-NULL - with a NULL .Fa free_function or vice versa. If both function pointers are NULL, the lightweight resolver's internal allocation routines which call .Xr malloc 3 and .Xr free 3 are used. .Fa arg is passed as the initial parameter to the memory allocation functions. The argument is passed but not used in the internal allocation and deallocation routines. Once memory for the structure has been allocated, .Xr lwres_conf_init 3 is called to initialise it. The structure is then returned via .Fa *contextp .

p A .Dv "struct lwres_context" is destroyed by .Fn lwres_context_destroy. .Fa **contextp is a pointer to a pointer to the context that is to be destroyed. It must be non-NULL, as should .Fa *contextp . If the context has an open socket, it is closed before the structure is discarded.

p The context's serial number is controlled with .Fn lwres_context_initserial and .Fn lwres_context_nextserial . Both require that .Fa ctx is not NULL. .Fn lwres_context_initserial sets the serial number for context .Fa *ctx to .Fa serial . .Fn lwres_context_nextserial returns the current serial number for the context and increments .Dv ctx->serial .

p Memory for a lightweight resolver context is allocated and freed using .Fn lwres_context_allocmem and .Fn lwres_context_freemem . These use whatever allocations were defined when the context was created with .Fn lwres_context_create . .Fn lwres_context_allocmem allocates .Fa len bytes of memory and if successful returns a pointer to the allocated storage. .Fn lwres_context_allocmem checks that .Fa len must be greater than 0. .Fn lwres_context_freemem frees .Fa len bytes of space starting at location .Fa mem . It has assertion checks to ensure that .Fa mem is not null and .Fa len is not zero.

p If the lightweight resolver's memory allocation functions are used, .Fa len bytes in the allocated buffer will be set to '0xe5' on allocation or '0xa9' when they are freed.

p .Fn lwres_context_sendrecv performs I/O for the context .Fa ctx . Data are read and written from the context's socket .Dv ctx->sock . It writes data from .Fa sendbase - typically a DNS query - and waits for a reply which is copied to the receive buffer at .Fa recvbase . .Fa sendbase is a pointer to the .Fa sendlen bytes of data that are to be sent using .Xr sendto 2. Up to .Fn recvlen bytes of data that are returned by a call to .Xr recvfrom 2 on the socket are copied to .Fa recvbase . The number of bytes that were written to this receive buffer is returned in .Fa *recvd_len . .Sh RETURN VALUES .Fn lwres_context_create returns .Er LWRES_R_NOMEMORY if memory for the .Dv "struct lwres_context" could not be allocated, otherwise .Er LWRES_R_SUCCESS is returned.

p Successful calls to the memory allocator .Fn lwres_context_allocmem return a pointer to the start of the allocated space. It returns NULL if memory could not be allocated.

p .Er LWRES_R_SUCCESS is returned when .Fn lwres_context_sendrecv completes successfully. .Er LWRES_R_IOERROR is returned if an I/O error occurs and .Er LWRES_R_TIMEOUT is returned if the .Xr recvfrom 2 call times out. .Sh SEE ALSO .Xr lwres_conf_init 3 , .Xr malloc 3 , .Xr free 3 , .Xr close 2 , .Xr memset 3 , .Xr sendto 2 , .Xr recvfrom 2 . .Sh BUGS The .Dv lwres_context_t structures and the functions which operate on them are not thread-safe.