mirror of
https://gitlab.isc.org/isc-projects/bind9
synced 2025-08-31 06:25:31 +00:00
checkpoint
This commit is contained in:
@@ -17,19 +17,104 @@
|
||||
|
||||
#include <config.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <isc/assertions.h>
|
||||
|
||||
#include <lwres/context.h>
|
||||
#include <lwres/lwbuffer.h>
|
||||
#include <lwres/lwres.h>
|
||||
#include <lwres/lwpacket.h>
|
||||
|
||||
static inline void
|
||||
CHECK(int val, char *msg)
|
||||
{
|
||||
if (val != 0) {
|
||||
fprintf(stderr, "%s returned %d\n", msg, val);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
hexdump(char *msg, void *base, size_t len)
|
||||
{
|
||||
unsigned char *p;
|
||||
unsigned int cnt;
|
||||
|
||||
p = base;
|
||||
cnt = 0;
|
||||
|
||||
printf("*** %s (%u bytes @ %p)\n", msg, len, base);
|
||||
|
||||
while (cnt < len) {
|
||||
if (cnt % 16 == 0)
|
||||
printf("%p: ", p);
|
||||
else if (cnt % 8 == 0)
|
||||
printf(" |");
|
||||
printf(" %02x", *p++);
|
||||
cnt++;
|
||||
|
||||
if (cnt % 16 == 0)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
if (cnt % 16 != 0)
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static char *TESTSTRING = "This is a test. This is only a test. !!!";
|
||||
|
||||
int
|
||||
main(int argc, char *argv[])
|
||||
{
|
||||
int ret;
|
||||
lwres_context_t *ctx;
|
||||
lwres_lwpacket_t pkt, pkt2;
|
||||
lwres_nooprequest_t nooprequest, *nooprequest2;
|
||||
lwres_noopresponse_t noopresponse, *noopresponse2;
|
||||
lwres_buffer_t b;
|
||||
|
||||
ctx = NULL;
|
||||
ret = lwres_context_create(&ctx, NULL, NULL, NULL);
|
||||
CHECK(ret, "lwres_context_create");
|
||||
|
||||
pkt.serial = 0x11223344;
|
||||
pkt.recvlength = 0x55667788;
|
||||
pkt.result = 0;
|
||||
|
||||
nooprequest.datalength = strlen(TESTSTRING);
|
||||
nooprequest.data = TESTSTRING;
|
||||
ret = lwres_nooprequest_render(ctx, &nooprequest, &pkt, &b);
|
||||
CHECK(ret, "lwres_nooprequest_render");
|
||||
|
||||
hexdump("rendered noop request", b.base, b.used);
|
||||
|
||||
/*
|
||||
* Now, parse it into a new structure.
|
||||
*/
|
||||
lwres_buffer_first(&b);
|
||||
ret = lwres_lwpacket_parseheader(&b, &pkt2);
|
||||
CHECK(ret, "lwres_lwpacket_parseheader");
|
||||
|
||||
hexdump("parsed pkt2", &pkt2, sizeof(pkt2));
|
||||
|
||||
nooprequest2 = NULL;
|
||||
ret = lwres_nooprequest_parse(ctx, &b, &pkt2, &nooprequest2);
|
||||
CHECK(ret, "lwres_nooprequest_parse");
|
||||
|
||||
assert(nooprequest.datalength == nooprequest2->datalength);
|
||||
assert(memcmp(nooprequest.data, nooprequest2->data,
|
||||
nooprequest.datalength) == 0);
|
||||
|
||||
lwres_nooprequest_free(ctx, &nooprequest2);
|
||||
|
||||
lwres_context_freemem(ctx, b.base, b.length);
|
||||
b.base = NULL;
|
||||
b.length = 0;
|
||||
|
||||
lwres_context_destroy(&ctx);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
@@ -26,11 +26,11 @@ CDEFINES =
|
||||
CWARNINGS =
|
||||
|
||||
# Alphabetically
|
||||
OBJS = context.@O@ lwbuffer.@O@ \
|
||||
OBJS = context.@O@ lwbuffer.@O@ lwpacket.@O@ \
|
||||
lwres_gabn.@O@ lwres_gnba.@O@ lwres_noop.@O@
|
||||
|
||||
# Alphabetically
|
||||
SRCS = context.c lwbuffer.c \
|
||||
SRCS = context.c lwbuffer.c lwpacket.c \
|
||||
lwres_gabn.c lwres_gnba.c lwres_noop.c
|
||||
|
||||
LIBS = @LIBS@
|
||||
|
@@ -72,7 +72,7 @@ lwres_context_create(lwres_context_t **contextp, void *arg,
|
||||
}
|
||||
|
||||
void
|
||||
lwres_context_free(lwres_context_t **contextp)
|
||||
lwres_context_destroy(lwres_context_t **contextp)
|
||||
{
|
||||
lwres_context_t *ctx;
|
||||
|
||||
|
@@ -56,7 +56,7 @@ lwres_context_create(lwres_context_t **contextp, void *arg,
|
||||
*/
|
||||
|
||||
void
|
||||
lwres_context_free(lwres_context_t **contextp);
|
||||
lwres_context_destroy(lwres_context_t **contextp);
|
||||
/*
|
||||
* Frees all memory associated with a lwres context.
|
||||
*
|
||||
|
@@ -95,18 +95,12 @@ typedef struct {
|
||||
/* public */
|
||||
isc_uint16_t datalength;
|
||||
unsigned char *data;
|
||||
/* if buffer == NULL, not freed by free routines */
|
||||
isc_uint32_t buflen;
|
||||
void *buffer;
|
||||
} lwres_nooprequest_t;
|
||||
|
||||
typedef struct {
|
||||
/* public */
|
||||
isc_uint16_t datalength;
|
||||
unsigned char *data;
|
||||
/* if buffer == NULL, not freed by free routines */
|
||||
isc_uint32_t buflen;
|
||||
void *buffer;
|
||||
} lwres_noopresponse_t;
|
||||
|
||||
/*
|
||||
|
@@ -27,16 +27,53 @@
|
||||
|
||||
#include "assert_p.h"
|
||||
|
||||
#define LWPACKET_LENGTH (sizeof(isc_uint16_t) * 4 + sizeof(isc_uint32_t) * 5)
|
||||
|
||||
int
|
||||
lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt)
|
||||
{
|
||||
REQUIRE(b != NULL);
|
||||
REQUIRE(pkt != NULL);
|
||||
|
||||
if (!SPACE_OK(b, LWPACKET_LENGTH))
|
||||
return (-1);
|
||||
|
||||
lwres_buffer_putuint32(b, pkt->length);
|
||||
lwres_buffer_putuint16(b, pkt->version);
|
||||
lwres_buffer_putuint16(b, pkt->flags);
|
||||
lwres_buffer_putuint32(b, pkt->serial);
|
||||
lwres_buffer_putuint32(b, pkt->opcode);
|
||||
lwres_buffer_putuint32(b, pkt->result);
|
||||
lwres_buffer_putuint32(b, pkt->recvlength);
|
||||
lwres_buffer_putuint16(b, pkt->authtype);
|
||||
lwres_buffer_putuint16(b, pkt->authlength);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int
|
||||
lwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt)
|
||||
{
|
||||
isc_uint32_t space;
|
||||
|
||||
REQUIRE(b != NULL);
|
||||
REQUIRE(pkt != NULL);
|
||||
|
||||
if (!SPACE_REMAINING(b, LWPACKET_LENGTH))
|
||||
return (-1);
|
||||
space = LWRES_BUFFER_REMAINING(b);
|
||||
|
||||
pkt->length = lwres_buffer_getuint32(b);
|
||||
if (pkt->length > space)
|
||||
return (-1);
|
||||
pkt->version = lwres_buffer_getuint16(b);
|
||||
pkt->flags = lwres_buffer_getuint16(b);
|
||||
pkt->serial = lwres_buffer_getuint32(b);
|
||||
pkt->opcode = lwres_buffer_getuint32(b);
|
||||
pkt->result = lwres_buffer_getuint32(b);
|
||||
pkt->recvlength = lwres_buffer_getuint32(b);
|
||||
pkt->authtype = lwres_buffer_getuint16(b);
|
||||
pkt->authlength = lwres_buffer_getuint16(b);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
@@ -225,8 +225,6 @@ lwres_noopresponse_free(lwres_context_t *ctx, lwres_noopresponse_t **structp)
|
||||
noop = *structp;
|
||||
*structp = NULL;
|
||||
|
||||
if (noop->buffer != NULL)
|
||||
CTXFREE(noop->buffer, noop->buflen);
|
||||
CTXFREE(noop, sizeof(lwres_noopresponse_t));
|
||||
}
|
||||
|
||||
@@ -241,7 +239,5 @@ lwres_nooprequest_free(lwres_context_t *ctx, lwres_nooprequest_t **structp)
|
||||
noop = *structp;
|
||||
*structp = NULL;
|
||||
|
||||
if (noop->buffer != NULL)
|
||||
CTXFREE(noop->buffer, noop->buflen);
|
||||
CTXFREE(noop, sizeof(lwres_nooprequest_t));
|
||||
}
|
||||
|
Reference in New Issue
Block a user