From 43236eb0e13a64c84bb5e2a902fbf1411b487d36 Mon Sep 17 00:00:00 2001 From: Michael Graff Date: Thu, 13 Jan 2000 01:08:05 +0000 Subject: [PATCH] snapshot --- lib/lwres/include/lwres/lwbuffer.h | 2 + lib/lwres/include/lwres/lwpacket.h | 7 +++ lib/lwres/include/lwres/lwres.h | 67 ++++++++++++-------- lib/lwres/lwres.c | 98 +++++++++++++++++++++++------- 4 files changed, 126 insertions(+), 48 deletions(-) diff --git a/lib/lwres/include/lwres/lwbuffer.h b/lib/lwres/include/lwres/lwbuffer.h index 2285bcb917..0a5d895575 100644 --- a/lib/lwres/include/lwres/lwbuffer.h +++ b/lib/lwres/include/lwres/lwbuffer.h @@ -130,6 +130,8 @@ ISC_LANG_BEGINDECLS * Get the length of the available region of buffer "b" */ #define LWRES_BUFFER_AVAILABLECOUNT(b) ((b)->length - (b)->used) +#define LWRES_BUFFER_AVAILABLECHECK(b, bytes) \ + (LWRES_BUFFER_AVAILABLECOUNT(b) >= (bytes)) /* * Note that the buffer structure is public. This is principally so buffer diff --git a/lib/lwres/include/lwres/lwpacket.h b/lib/lwres/include/lwres/lwpacket.h index a0f60cc3f5..37a42a393b 100644 --- a/lib/lwres/include/lwres/lwpacket.h +++ b/lib/lwres/include/lwres/lwpacket.h @@ -23,6 +23,8 @@ #include #include +#include + typedef struct lwres_lwpacket lwres_lwpacket_t; struct lwres_lwpacket { @@ -111,6 +113,11 @@ struct lwres_lwpacket { ISC_LANG_BEGINDECLS +int +lwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt, + isc_uint32_t buflen, isc_uint16_t flags, + isc_uint32_t serial, isc_uint32_t opcode, + isc_uint32_t result, isc_uint32_t recvlength); ISC_LANG_ENDDECLS diff --git a/lib/lwres/include/lwres/lwres.h b/lib/lwres/include/lwres/lwres.h index 71992f8d1b..a50be68dc9 100644 --- a/lib/lwres/include/lwres/lwres.h +++ b/lib/lwres/include/lwres/lwres.h @@ -28,18 +28,25 @@ */ typedef struct lwres_context lwres_context_t; +/* + * NO-OP + */ #define LWRES_OPCODE_NOOP 0x00000000U + typedef struct { /* public */ isc_uint32_t result; + isc_uint16_t datalength; + unsigned char *data; + /* private */ isc_uint32_t buflen; void *buffer; } lwres_noop_t; /* - * These are the structure versions of the data passed around via the - * various functions. For the wire format of these, see doc/design/lwres + * GET ADDRESSES BY NAME */ +#define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U typedef struct { isc_uint32_t family; @@ -47,7 +54,6 @@ typedef struct { unsigned char *address; } lwres_addr_t; -#define LWRES_OPCODE_GETADDRSBYNAME 0x00010001U typedef struct { /* public */ isc_uint32_t result; @@ -62,6 +68,9 @@ typedef struct { /* variable length data follows */ } lwres_getaddrsbyname_t; +/* + * GET NAME BY ADDRESS + */ #define LWRES_OPCODE_GETNAMEBYADDR 0x00010002U typedef struct { /* public */ @@ -81,10 +90,10 @@ typedef struct { ISC_LANG_BEGINDECLS typedef void *(*lwres_malloc_t)(void *arg, size_t length); -typedef void (*lwres_free_t)(void *arg, size_t length, void *mem); +typedef void (*lwres_free_t)(void *arg, void *mem, size_t length); int -lwres_getaddrsbyname(lwres_context_t *context, +lwres_getaddrsbyname(lwres_context_t *ctx, char *name, isc_uint32_t addrtypes, lwres_getaddrsbyname_t **structp); /* @@ -92,7 +101,7 @@ lwres_getaddrsbyname(lwres_context_t *context, * * Requires: * - * context != NULL, and be a context returned via lwres_contextcreate(). + * ctx != NULL, and be a context returned via lwres_contextcreate(). * * structp != NULL && *structp == NULL. * @@ -112,14 +121,14 @@ lwres_getaddrsbyname(lwres_context_t *context, */ void -lwres_freegetaddrsbyname(lwres_context_t *context, +lwres_freegetaddrsbyname(lwres_context_t *ctx, lwres_getaddrsbyname_t **structp); /* * Frees any dynamically allocated memory for this structure. * * Requires: * - * context != NULL, and be a context returned via lwres_contextcreate(). + * ctx != NULL, and be a context returned via lwres_contextcreate(). * * structp != NULL && *structp != NULL. * @@ -132,7 +141,15 @@ lwres_freegetaddrsbyname(lwres_context_t *context, */ int -lwres_getnamebyaddr(lwres_context_t *context, isc_uint32_t addrtype, +lwres_getaddrsbyname_fromstruct(lwres_context_t *ctx, + isc_uint8_t *buf, size_t len, + lwres_getaddrsbyname_t *gabn); +/* + * XXXMLG document + */ + +int +lwres_getnamebyaddr(lwres_context_t *ctx, isc_uint32_t addrtype, isc_uint16_t addrlen, unsigned char *addr, lwres_getnamebyaddr_t **structp); /* @@ -140,7 +157,7 @@ lwres_getnamebyaddr(lwres_context_t *context, isc_uint32_t addrtype, * * Requires: * - * context != NULL, and be a context returned via lwres_contextcreate(). + * ctx != NULL, and be a context returned via lwres_contextcreate(). * * structp != NULL && *structp == NULL. * @@ -159,14 +176,14 @@ lwres_getnamebyaddr(lwres_context_t *context, isc_uint32_t addrtype, */ void -lwres_freegetnamebyaddr(lwres_context_t *context, +lwres_freegetnamebyaddr(lwres_context_t *ctx, lwres_getnamebyaddr_t **structp); /* * Frees any dynamically allocated memory for this structure. * * Requires: * - * context != NULL, and be a context returned via lwres_contextcreate(). + * ctx != NULL, and be a context returned via lwres_contextcreate(). * * structp != NULL && *structp != NULL. * @@ -175,39 +192,39 @@ lwres_freegetnamebyaddr(lwres_context_t *context, * *structp == NULL. * * All memory allocated by this structure will be returned to the - * system via the context's free function. + * system via the ctx's free function. */ int -lwres_noop(lwres_context_t *context, isc_uint16_t datalength, void *data, - lwres_noop_t **structp); +lwres_noop_render(lwres_context_t *ctx, isc_uint16_t datalength, + void *data, lwres_buffer_t *b); /* - * Transmit a noop to the lw resolver. + * Allocate space and render into wire format a noop request packet. * * Requires: * - * context != NULL, and be a context returned via lwres_contextcreate(). + * ctx != NULL, and be a context returned via lwres_contextcreate(). * - * structp != NULL && *structp != NULL. + * b != NULL, and points to a lwres_buffer_t. The contents of the + * buffer structure will be initialized to contain the wire-format + * noop request packet. * * Returns: * * Returns 0 on success, non-zero on failure. * - * On successful return, *structp will be non-NULL, and will point to - * an allocated structure returning the data requested. This structure - * must be freed using lwres_freenoop() when it is no longer - * needed. + * On successful return, *b will contain data about the wire-format + * packet. It can be transmitted in any way, including lwres_sendblock(). */ void -lwres_freenoop(lwres_context_t *context, lwres_noop_t **structp); +lwres_freenoop(lwres_context_t *ctx, lwres_noop_t **structp); /* * Frees any dynamically allocated memory for this structure. * * Requires: * - * context != NULL, and be a context returned via lwres_contextcreate(). + * ctx != NULL, and be a context returned via lwres_contextcreate(). * * structp != NULL && *structp != NULL. * @@ -224,7 +241,7 @@ lwres_contextcreate(lwres_context_t **contextp, void *arg, lwres_malloc_t malloc_function, lwres_free_t free_function); /* - * Allocated a lwres context. This is used in all lwres calls. + * Allocate a lwres context. This is used in all lwres calls. * * Memory management can be replaced here by passing in two functions. * If one is non-NULL, they must both be non-NULL. "arg" is passed to diff --git a/lib/lwres/lwres.c b/lib/lwres/lwres.c index b64501bb84..261d50650c 100644 --- a/lib/lwres/lwres.c +++ b/lib/lwres/lwres.c @@ -29,7 +29,14 @@ #include static void *lwres_malloc(void *, size_t); -static void lwres_free(void *, size_t, void *); +static void lwres_free(void *, void *, size_t); + +/* + * Helper functions, assuming the context is always called "ctx" in + * the scope these functions are called from. + */ +#define CTXMALLOC(len) ctx->malloc(ctx->arg, (len)) +#define CTXFREE(addr, len) ctx->free(ctx->arg, (addr), (len)) #define LWRES_DEFAULT_TIMEOUT 30 /* 30 seconds for a reply */ @@ -39,6 +46,7 @@ static void lwres_free(void *, size_t, void *); */ struct lwres_context { unsigned int timeout; /* time to wait for reply */ + isc_uint32_t serial; /* serial number state */ /* * Function pointers for allocating memory. @@ -53,7 +61,7 @@ lwres_contextcreate(lwres_context_t **contextp, void *arg, lwres_malloc_t malloc_function, lwres_free_t free_function) { - lwres_context_t *context; + lwres_context_t *ctx; /* * If we were not given anything special to use, use our own @@ -64,8 +72,8 @@ lwres_contextcreate(lwres_context_t **contextp, void *arg, free_function = lwres_free; } - context = malloc_function(arg, sizeof(lwres_context_t)); - if (context == NULL) { + ctx = malloc_function(arg, sizeof(lwres_context_t)); + if (ctx == NULL) { errno = ENOMEM; return (-1); } @@ -73,26 +81,26 @@ lwres_contextcreate(lwres_context_t **contextp, void *arg, /* * Set up the context. */ - context->malloc = malloc_function; - context->free = free_function; - context->arg = arg; + ctx->malloc = malloc_function; + ctx->free = free_function; + ctx->arg = arg; - context->timeout = LWRES_DEFAULT_TIMEOUT; + ctx->timeout = LWRES_DEFAULT_TIMEOUT; + ctx->serial = (isc_uint32_t)ctx; /* XXXMLG */ - *contextp = context; + *contextp = ctx; return (0); } void lwres_freecontext(lwres_context_t **contextp) { - lwres_context_t *context; + lwres_context_t *ctx; - context = *contextp; + ctx = *contextp; *contextp = NULL; - /* This is always allocated via malloc() for now... */ - context->free(context->arg, sizeof(lwres_context_t), context); + CTXFREE(sizeof(lwres_context_t), ctx); } int @@ -114,25 +122,69 @@ lwres_freegetaddrsbyname(lwres_context_t *contextp, } int -lwres_noop(lwres_context_t *context, isc_uint16_t datalength, void *data, - lwres_noop_t **structp) -{ -} - -void -lwres_freenoop(lwres_context_t *context, lwres_noop_t **structp) +lwres_getaddrsbyname_fromstruct(lwres_context_t *contextp, + isc_uint8_t *buf, size_t len, + lwres_getaddrsbyname_t *gabn) { } int -lwres_getnamebyaddr(lwres_context_t *contextp, isc_uint32_t addrtype, +lwres_noop_render(lwres_context_t *ctx, isc_uint16_t datalength, void *data, + lwres_buffer_t *b) +{ + lwres_lwpacket_t pkt; + unsigned char *buf; + size_t buflen; + int ret; + + buflen = sizeof(lwres_lwpacket_t) + sizeof(isc_uint16_t) + datalength; + buf = CTXMALLOC(buflen); + if (buf == NULL) { + errno = ENOMEM; + return (-1); + } + lwres_buffer_init(b, buf, buflen); + + ret = lwres_lwpacket_renderheader(b, &pkt, buflen, 0, ctx->serial++, + LWRES_OPCODE_NOOP, 0, buflen); + if (ret != 0) { + lwres_buffer_invalidate(b); + CTXFREE(buf, buflen); + return (ret); + } + + if (!LWRES_BUFFER_AVAILABLECHECK(b, + datalength + sizeof(isc_uint16_t))) { + CTXFREE(buf, buflen); + lwres_buffer_invalidate(b); + errno = EOVERFLOW; /* this is a programmer botch! */ + return (-1); + } + + /* + * Put the length and the data. We know this will fit because we + * just checked for it. + */ + lwres_buffer_putuint16(b, datalength); + lwres_buffer_putmem(b, data, datalength); + + return (0); +} + +void +lwres_freenoop(lwres_context_t *ctx, lwres_noop_t **structp) +{ +} + +int +lwres_getnamebyaddr(lwres_context_t *ctx, isc_uint32_t addrtype, isc_uint16_t addrlen, unsigned char *addr, lwres_getnamebyaddr_t **structp) { } void -lwres_freegetnamebyaddr(lwres_context_t *contextp, +lwres_freegetnamebyaddr(lwres_context_t *ctx, lwres_getnamebyaddr_t **structp) { } @@ -154,7 +206,7 @@ lwres_malloc(void *arg, size_t len) } static void -lwres_free(void *arg, size_t len, void *mem) +lwres_free(void *arg, void *mem, size_t len) { (void)arg;