lwres_gnba.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 2000-2002, 2004, 2005, 2007, 2013, 2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id: lwres_gnba.c,v 1.28 2007/09/24 17:18:25 each Exp $ */
/*! \file lwres_gnba.c
These are low-level routines for creating and parsing lightweight
resolver address-to-name lookup request and response messages.
There are four main functions for the getnamebyaddr opcode. One
render function converts a getnamebyaddr request structure --
lwres_gnbarequest_t -- to the lightweight resolver's canonical
format. It is complemented by a parse function that converts a
packet in this canonical format to a getnamebyaddr request
structure. Another render function converts the getnamebyaddr
response structure -- lwres_gnbaresponse_t to the canonical format.
This is complemented by a parse function which converts a packet in
canonical format to a getnamebyaddr response structure.
below.
\code
#define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U
typedef struct {
lwres_uint32_t flags;
lwres_addr_t addr;
} lwres_gnbarequest_t;
typedef struct {
lwres_uint32_t flags;
lwres_uint16_t naliases;
char *realname;
char **aliases;
lwres_uint16_t realnamelen;
lwres_uint16_t *aliaslen;
void *base;
size_t baselen;
} lwres_gnbaresponse_t;
\endcode
lwres_gnbarequest_render() uses resolver context ctx to convert
getnamebyaddr request structure req to canonical format. The packet
header structure pkt is initialised and transferred to buffer b.
The contents of *req are then appended to the buffer in canonical
format. lwres_gnbaresponse_render() performs the same task, except
it converts a getnamebyaddr response structure lwres_gnbaresponse_t
to the lightweight resolver's canonical format.
lwres_gnbarequest_parse() uses context ctx to convert the contents
of packet pkt to a lwres_gnbarequest_t structure. Buffer b provides
space to be used for storing this structure. When the function
succeeds, the resulting lwres_gnbarequest_t is made available
through *structp. lwres_gnbaresponse_parse() offers the same
semantics as lwres_gnbarequest_parse() except it yields a
lwres_gnbaresponse_t structure.
lwres_gnbaresponse_free() and lwres_gnbarequest_free() release the
memory in resolver context ctx that was allocated to the
lwres_gnbaresponse_t or lwres_gnbarequest_t structures referenced
via structp. Any memory associated with ancillary buffers and
strings for those structures is also discarded.
\section lwres_gbna_return Return Values
The getnamebyaddr opcode functions lwres_gnbarequest_render(),
lwres_gnbaresponse_render() lwres_gnbarequest_parse() and
lwres_gnbaresponse_parse() all return #LWRES_R_SUCCESS on success.
They return #LWRES_R_NOMEMORY if memory allocation fails.
#LWRES_R_UNEXPECTEDEND is returned if the available space in the
buffer b is too small to accommodate the packet header or the
lwres_gnbarequest_t and lwres_gnbaresponse_t structures.
lwres_gnbarequest_parse() and lwres_gnbaresponse_parse() will
return #LWRES_R_UNEXPECTEDEND if the buffer is not empty after
decoding the received packet. These functions will return
#LWRES_R_FAILURE if pktflags in the packet header structure
#lwres_lwpacket_t indicate that the packet is not a response to an
earlier query.
\section lwres_gbna_see See Also
\link lwpacket.c lwres_packet\endlink
*/
#include <config.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <lwres/lwbuffer.h>
#include <lwres/lwpacket.h>
#include "context_p.h"
#include "assert_p.h"
/*% Uses resolver context ctx to convert getnamebyaddr request structure req to canonical format. */
{
unsigned char *buf;
int ret;
return (LWRES_R_NOMEMORY);
pkt->authlength = 0;
if (ret != LWRES_R_SUCCESS) {
return (ret);
}
/*
* Put the length and the data. We know this will fit because we
* just checked for it.
*/
INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
return (LWRES_R_SUCCESS);
}
/*% Converts a getnamebyaddr response structure lwres_gnbaresponse_t to the lightweight resolver's canonical format. */
{
unsigned char *buf;
int ret;
int x;
/*
* Calculate packet size.
*/
return (LWRES_R_NOMEMORY);
pkt->authlength = 0;
if (ret != LWRES_R_SUCCESS) {
return (ret);
}
/* encode naliases */
/* encode the real name */
lwres_buffer_putuint8(b, 0);
/* encode the aliases */
datalen);
lwres_buffer_putuint8(b, 0);
}
INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
return (LWRES_R_SUCCESS);
}
/*% Uses context ctx to convert the contents of packet pkt to a lwres_gnbarequest_t structure. */
{
int ret;
return (LWRES_R_FAILURE);
if (!SPACE_REMAINING(b, 4))
return (LWRES_R_UNEXPECTEDEND);
return (LWRES_R_NOMEMORY);
if (ret != LWRES_R_SUCCESS)
goto out;
if (LWRES_BUFFER_REMAINING(b) != 0) {
goto out;
}
return (LWRES_R_SUCCESS);
out:
return (ret);
}
/*% Offers the same semantics as lwres_gnbarequest_parse() except it yields a lwres_gnbaresponse_t structure. */
{
int ret;
unsigned int x;
return (LWRES_R_FAILURE);
/*
* Pull off flags & naliases
*/
return (LWRES_R_UNEXPECTEDEND);
flags = lwres_buffer_getuint32(b);
return (LWRES_R_NOMEMORY);
if (naliases > 0) {
goto out;
}
goto out;
}
}
/*
* Now, pull off the real name.
*/
if (ret != LWRES_R_SUCCESS)
goto out;
/*
* Parse off the aliases.
*/
if (ret != LWRES_R_SUCCESS)
goto out;
}
if (LWRES_BUFFER_REMAINING(b) != 0) {
goto out;
}
return (LWRES_R_SUCCESS);
out:
sizeof(lwres_uint16_t) * naliases);
}
return (ret);
}
/*% Release the memory in resolver context ctx that was allocated to the lwres_gnbarequest_t. */
void
{
}
/*% Release the memory in resolver context ctx that was allocated to the lwres_gnbaresponse_t. */
void
{
}
}