diff --git a/bin/tests/lwres_test.c b/bin/tests/lwres_test.c index d7418e0b83..db64193df4 100644 --- a/bin/tests/lwres_test.c +++ b/bin/tests/lwres_test.c @@ -17,19 +17,104 @@ #include +#include #include #include #include #include -#include - #include #include #include #include +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); } diff --git a/lib/lwres/Makefile.in b/lib/lwres/Makefile.in index a9ccbe988e..58591ed9d1 100644 --- a/lib/lwres/Makefile.in +++ b/lib/lwres/Makefile.in @@ -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@ diff --git a/lib/lwres/context.c b/lib/lwres/context.c index 1160aaccaa..ce578e8a7d 100644 --- a/lib/lwres/context.c +++ b/lib/lwres/context.c @@ -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; diff --git a/lib/lwres/include/lwres/context.h b/lib/lwres/include/lwres/context.h index 499574bd4c..9dfc38f0fd 100644 --- a/lib/lwres/include/lwres/context.h +++ b/lib/lwres/include/lwres/context.h @@ -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. * diff --git a/lib/lwres/include/lwres/lwres.h b/lib/lwres/include/lwres/lwres.h index 030314af5f..899a2cf9b7 100644 --- a/lib/lwres/include/lwres/lwres.h +++ b/lib/lwres/include/lwres/lwres.h @@ -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; /* diff --git a/lib/lwres/lwpacket.c b/lib/lwres/lwpacket.c index 74a60f99a5..e0f8326fad 100644 --- a/lib/lwres/lwpacket.c +++ b/lib/lwres/lwpacket.c @@ -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); } diff --git a/lib/lwres/lwres_noop.c b/lib/lwres/lwres_noop.c index e0fddbc7fc..444cfc0110 100644 --- a/lib/lwres/lwres_noop.c +++ b/lib/lwres/lwres_noop.c @@ -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)); }