1/*
2 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001  Internet Software Consortium.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/* $Id: lwpacket.c,v 1.18 2007/06/19 23:47:22 tbox Exp $ */
19
20/*! \file */
21
22/**
23 *    These functions rely on a struct lwres_lwpacket which is defined in
24 *    \link lwpacket.h lwres/lwpacket.h.\endlink
25 *
26 *    The following opcodes are currently defined:
27 *
28 * \li   #LWRES_OPCODE_NOOP
29 *           Success is always returned and the packet contents are
30 *           echoed. The \link lwres_noop.c lwres_noop_*()\endlink functions should be used for this
31 *           type.
32 *
33 * \li   #LWRES_OPCODE_GETADDRSBYNAME
34 *           returns all known addresses for a given name. The
35 *           \link lwres_gabn.c lwres_gabn_*()\endlink functions should be used for this type.
36 *
37 * \li   #LWRES_OPCODE_GETNAMEBYADDR
38 *           return the hostname for the given address. The
39 *           \link lwres_gnba.c lwres_gnba_*() \endlink functions should be used for this type.
40 *
41 *    lwres_lwpacket_renderheader() transfers the contents of lightweight
42 *    resolver packet structure #lwres_lwpacket_t *pkt in network byte
43 *    order to the lightweight resolver buffer, *b.
44 *
45 *    lwres_lwpacket_parseheader() performs the converse operation. It
46 *    transfers data in network byte order from buffer *b to resolver
47 *    packet *pkt. The contents of the buffer b should correspond to a
48 *    #lwres_lwpacket_t.
49 *
50 * \section lwpacket_return Return Values
51 *
52 *    Successful calls to lwres_lwpacket_renderheader() and
53 *    lwres_lwpacket_parseheader() return #LWRES_R_SUCCESS. If there is
54 *    insufficient space to copy data between the buffer *b and
55 *    lightweight resolver packet *pkt both functions return
56 *    #LWRES_R_UNEXPECTEDEND.
57 */
58
59#include <config.h>
60
61#include <assert.h>
62#include <stdlib.h>
63#include <string.h>
64
65#include <lwres/lwbuffer.h>
66#include <lwres/lwpacket.h>
67#include <lwres/result.h>
68
69#include "assert_p.h"
70
71/*% Length of Packet */
72#define LWPACKET_LENGTH \
73	(sizeof(lwres_uint16_t) * 4 + sizeof(lwres_uint32_t) * 5)
74
75/*% transfers the contents of lightweight resolver packet structure lwres_lwpacket_t *pkt in network byte order to the lightweight resolver buffer, *b. */
76
77lwres_result_t
78lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
79	REQUIRE(b != NULL);
80	REQUIRE(pkt != NULL);
81
82	if (!SPACE_OK(b, LWPACKET_LENGTH))
83		return (LWRES_R_UNEXPECTEDEND);
84
85	lwres_buffer_putuint32(b, pkt->length);
86	lwres_buffer_putuint16(b, pkt->version);
87	lwres_buffer_putuint16(b, pkt->pktflags);
88	lwres_buffer_putuint32(b, pkt->serial);
89	lwres_buffer_putuint32(b, pkt->opcode);
90	lwres_buffer_putuint32(b, pkt->result);
91	lwres_buffer_putuint32(b, pkt->recvlength);
92	lwres_buffer_putuint16(b, pkt->authtype);
93	lwres_buffer_putuint16(b, pkt->authlength);
94
95	return (LWRES_R_SUCCESS);
96}
97
98/*% transfers data in network byte order from buffer *b to resolver packet *pkt. The contents of the buffer b should correspond to a lwres_lwpacket_t. */
99
100lwres_result_t
101lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
102	lwres_uint32_t space;
103
104	REQUIRE(b != NULL);
105	REQUIRE(pkt != NULL);
106
107	space = LWRES_BUFFER_REMAINING(b);
108	if (space < LWPACKET_LENGTH)
109		return (LWRES_R_UNEXPECTEDEND);
110
111	pkt->length = lwres_buffer_getuint32(b);
112	/*
113	 * XXXBEW/MLG Checking that the buffer is long enough probably
114	 * shouldn't be done here, since this function is supposed to just
115	 * parse the header.
116	 */
117	if (pkt->length > space)
118		return (LWRES_R_UNEXPECTEDEND);
119	pkt->version = lwres_buffer_getuint16(b);
120	pkt->pktflags = lwres_buffer_getuint16(b);
121	pkt->serial = lwres_buffer_getuint32(b);
122	pkt->opcode = lwres_buffer_getuint32(b);
123	pkt->result = lwres_buffer_getuint32(b);
124	pkt->recvlength = lwres_buffer_getuint32(b);
125	pkt->authtype = lwres_buffer_getuint16(b);
126	pkt->authlength = lwres_buffer_getuint16(b);
127
128	return (LWRES_R_SUCCESS);
129}
130