1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*! \file */
13 
14 /**
15  *    These functions rely on a struct lwres_lwpacket which is defined in
16  *    \link lwpacket.h lwres/lwpacket.h.\endlink
17  *
18  *    The following opcodes are currently defined:
19  *
20  * \li   #LWRES_OPCODE_NOOP
21  *           Success is always returned and the packet contents are
22  *           echoed. The \link lwres_noop.c lwres_noop_*()\endlink functions should be used for this
23  *           type.
24  *
25  * \li   #LWRES_OPCODE_GETADDRSBYNAME
26  *           returns all known addresses for a given name. The
27  *           \link lwres_gabn.c lwres_gabn_*()\endlink functions should be used for this type.
28  *
29  * \li   #LWRES_OPCODE_GETNAMEBYADDR
30  *           return the hostname for the given address. The
31  *           \link lwres_gnba.c lwres_gnba_*() \endlink functions should be used for this type.
32  *
33  *    lwres_lwpacket_renderheader() transfers the contents of lightweight
34  *    resolver packet structure #lwres_lwpacket_t *pkt in network byte
35  *    order to the lightweight resolver buffer, *b.
36  *
37  *    lwres_lwpacket_parseheader() performs the converse operation. It
38  *    transfers data in network byte order from buffer *b to resolver
39  *    packet *pkt. The contents of the buffer b should correspond to a
40  *    #lwres_lwpacket_t.
41  *
42  * \section lwpacket_return Return Values
43  *
44  *    Successful calls to lwres_lwpacket_renderheader() and
45  *    lwres_lwpacket_parseheader() return #LWRES_R_SUCCESS. If there is
46  *    insufficient space to copy data between the buffer *b and
47  *    lightweight resolver packet *pkt both functions return
48  *    #LWRES_R_UNEXPECTEDEND.
49  */
50 
51 #include <config.h>
52 
53 #include <assert.h>
54 #include <inttypes.h>
55 #include <stdlib.h>
56 #include <string.h>
57 
58 #include <lwres/lwbuffer.h>
59 #include <lwres/lwpacket.h>
60 #include <lwres/result.h>
61 
62 #include "assert_p.h"
63 
64 /*% Length of Packet */
65 #define LWPACKET_LENGTH \
66 	(sizeof(uint16_t) * 4 + sizeof(uint32_t) * 5)
67 
68 /*% transfers the contents of lightweight resolver packet structure lwres_lwpacket_t *pkt in network byte order to the lightweight resolver buffer, *b. */
69 
70 lwres_result_t
lwres_lwpacket_renderheader(lwres_buffer_t * b,lwres_lwpacket_t * pkt)71 lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
72 	REQUIRE(b != NULL);
73 	REQUIRE(pkt != NULL);
74 
75 	if (!SPACE_OK(b, LWPACKET_LENGTH))
76 		return (LWRES_R_UNEXPECTEDEND);
77 
78 	lwres_buffer_putuint32(b, pkt->length);
79 	lwres_buffer_putuint16(b, pkt->version);
80 	lwres_buffer_putuint16(b, pkt->pktflags);
81 	lwres_buffer_putuint32(b, pkt->serial);
82 	lwres_buffer_putuint32(b, pkt->opcode);
83 	lwres_buffer_putuint32(b, pkt->result);
84 	lwres_buffer_putuint32(b, pkt->recvlength);
85 	lwres_buffer_putuint16(b, pkt->authtype);
86 	lwres_buffer_putuint16(b, pkt->authlength);
87 
88 	return (LWRES_R_SUCCESS);
89 }
90 
91 /*% 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. */
92 
93 lwres_result_t
lwres_lwpacket_parseheader(lwres_buffer_t * b,lwres_lwpacket_t * pkt)94 lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt) {
95 	uint32_t space;
96 
97 	REQUIRE(b != NULL);
98 	REQUIRE(pkt != NULL);
99 
100 	space = LWRES_BUFFER_REMAINING(b);
101 	if (space < LWPACKET_LENGTH)
102 		return (LWRES_R_UNEXPECTEDEND);
103 
104 	pkt->length = lwres_buffer_getuint32(b);
105 	/*
106 	 * XXXBEW/MLG Checking that the buffer is long enough probably
107 	 * shouldn't be done here, since this function is supposed to just
108 	 * parse the header.
109 	 */
110 	if (pkt->length > space)
111 		return (LWRES_R_UNEXPECTEDEND);
112 	pkt->version = lwres_buffer_getuint16(b);
113 	pkt->pktflags = lwres_buffer_getuint16(b);
114 	pkt->serial = lwres_buffer_getuint32(b);
115 	pkt->opcode = lwres_buffer_getuint32(b);
116 	pkt->result = lwres_buffer_getuint32(b);
117 	pkt->recvlength = lwres_buffer_getuint32(b);
118 	pkt->authtype = lwres_buffer_getuint16(b);
119 	pkt->authlength = lwres_buffer_getuint16(b);
120 
121 	return (LWRES_R_SUCCESS);
122 }
123