cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2000, 2001, 2004, 2005, 2007, 2013, 2015, 2016 Internet Systems Consortium, Inc. ("ISC")
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington */
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington
70e5a7403f0e0a3bd292b8287c5fed5772c15270Automatic Updater/* $Id: lwres_grbn.c,v 1.10 2007/06/19 23:47:22 tbox Exp $ */
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*! \file lwres_grbn.c
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein */
cffe50abf66495ee08084f8e1081278a9b589985Brian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <config.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <assert.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <stdlib.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <string.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <lwres/lwbuffer.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <lwres/lwpacket.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <lwres/lwres.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include <lwres/result.h>
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include "context_p.h"
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington#include "assert_p.h"
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Thread-save equivalent to \link lwres_gabn.c lwres_gabn* \endlink routines. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_result_t
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_grbnrequest_render(lwres_context_t *ctx, lwres_grbnrequest_t *req,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_lwpacket_t *pkt, lwres_buffer_t *b)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington{
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington unsigned char *buf;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington size_t buflen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington int ret;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington size_t payload_length;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint16_t datalen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(ctx != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(req != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(req->name != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(pkt != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(b != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
bcb68be0a8f3c3eca58d6a6a869267e5c1841de2Francis Dupont datalen = (lwres_uint16_t) strlen(req->name);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington payload_length = 4 + 2 + 2 + 2 + req->namelen + 1;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington buflen = LWRES_LWPACKET_LENGTH + payload_length;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington buf = CTXMALLOC(buflen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (buf == NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_NOMEMORY);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews lwres_buffer_init(b, buf, (unsigned int)buflen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews pkt->length = (lwres_uint32_t)buflen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->version = LWRES_LWPACKETVERSION_0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->pktflags &= ~LWRES_LWPACKETFLAG_RESPONSE;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->opcode = LWRES_OPCODE_GETRDATABYNAME;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->result = 0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->authtype = 0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->authlength = 0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = lwres_lwpacket_renderheader(b, pkt);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != LWRES_R_SUCCESS) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_invalidate(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(buf, buflen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (ret);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington INSIST(SPACE_OK(b, payload_length));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Flags.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint32(b, req->flags);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Class.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, req->rdclass);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Type.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, req->rdtype);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Put the length and the data. We know this will fit because we
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * just checked for it.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putmem(b, (unsigned char *)req->name, datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint8(b, 0); /* trailing NUL */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_SUCCESS);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Thread-save equivalent to \link lwres_gabn.c lwres_gabn* \endlink routines. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_result_t
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_grbnresponse_render(lwres_context_t *ctx, lwres_grbnresponse_t *req,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_lwpacket_t *pkt, lwres_buffer_t *b)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington{
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington unsigned char *buf;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington size_t buflen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington int ret;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington size_t payload_length;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint16_t datalen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington int x;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(ctx != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(req != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(pkt != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(b != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* flags, class, type, ttl, nrdatas, nsigs */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington payload_length = 4 + 2 + 2 + 4 + 2 + 2;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* real name encoding */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington payload_length += 2 + req->realnamelen + 1;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* each rr */
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (x = 0; x < req->nrdatas; x++)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington payload_length += 2 + req->rdatalen[x];
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (x = 0; x < req->nsigs; x++)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington payload_length += 2 + req->siglen[x];
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington buflen = LWRES_LWPACKET_LENGTH + payload_length;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington buf = CTXMALLOC(buflen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (buf == NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_NOMEMORY);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews lwres_buffer_init(b, buf, (unsigned int)buflen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews pkt->length = (lwres_uint32_t)buflen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->version = LWRES_LWPACKETVERSION_0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->pktflags |= LWRES_LWPACKETFLAG_RESPONSE;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->opcode = LWRES_OPCODE_GETRDATABYNAME;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->authtype = 0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington pkt->authlength = 0;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = lwres_lwpacket_renderheader(b, pkt);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != LWRES_R_SUCCESS) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_invalidate(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(buf, buflen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (ret);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Check space needed here.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington INSIST(SPACE_OK(b, payload_length));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* Flags. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint32(b, req->flags);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* encode class, type, ttl, and nrdatas */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, req->rdclass);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, req->rdtype);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint32(b, req->ttl);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, req->nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, req->nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* encode the real name */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington datalen = req->realnamelen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putmem(b, (unsigned char *)req->realname, datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint8(b, 0);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* encode the rdatas */
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (x = 0; x < req->nrdatas; x++) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington datalen = req->rdatalen[x];
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putmem(b, req->rdatas[x], datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /* encode the signatures */
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (x = 0; x < req->nsigs; x++) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington datalen = req->siglen[x];
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putuint16(b, datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_buffer_putmem(b, req->sigs[x], datalen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington INSIST(LWRES_BUFFER_USEDCOUNT(b) == pkt->length);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_SUCCESS);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Thread-save equivalent to \link lwres_gabn.c lwres_gabn* \endlink routines. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_result_t
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_grbnrequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_lwpacket_t *pkt, lwres_grbnrequest_t **structp)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington{
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington int ret;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington char *name;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_grbnrequest_t *grbn;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint32_t flags;
cddd0ddf0782aa9bebfa6dc46863d54ce585a0edDanny Mayer lwres_uint16_t rdclass, rdtype;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint16_t namelen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(ctx != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(pkt != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(b != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(structp != NULL && *structp == NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if ((pkt->pktflags & LWRES_LWPACKETFLAG_RESPONSE) != 0)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_FAILURE);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (!SPACE_REMAINING(b, 4 + 2 + 2))
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_UNEXPECTEDEND);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Pull off the flags, class, and type.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington flags = lwres_buffer_getuint32(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rdclass = lwres_buffer_getuint16(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rdtype = lwres_buffer_getuint16(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Pull off the name itself
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = lwres_string_parse(b, &name, &namelen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != LWRES_R_SUCCESS)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (ret);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (LWRES_BUFFER_REMAINING(b) != 0)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_TRAILINGDATA);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn = CTXMALLOC(sizeof(lwres_grbnrequest_t));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn == NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_NOMEMORY);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->flags = flags;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdclass = rdclass;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdtype = rdtype;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->name = name;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->namelen = namelen;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington *structp = grbn;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_SUCCESS);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
83baacd8ff7bf1aa282981bd4785a71b1a4cb0dfMark Andrews/*% Thread-safe equivalent to \link lwres_gabn.c lwres_gabn* \endlink routines. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_result_t
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_grbnresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_lwpacket_t *pkt, lwres_grbnresponse_t **structp)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington{
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_result_t ret;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington unsigned int x;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint32_t flags;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint16_t rdclass, rdtype;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint32_t ttl;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_uint16_t nrdatas, nsigs;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_grbnresponse_t *grbn;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(ctx != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(pkt != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(b != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(structp != NULL && *structp == NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if ((pkt->pktflags & LWRES_LWPACKETFLAG_RESPONSE) == 0)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_FAILURE);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Pull off the flags, class, type, ttl, nrdatas, and nsigs
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (!SPACE_REMAINING(b, 4 + 2 + 2 + 4 + 2 + 2))
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_UNEXPECTEDEND);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington flags = lwres_buffer_getuint32(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rdclass = lwres_buffer_getuint16(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington rdtype = lwres_buffer_getuint16(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ttl = lwres_buffer_getuint32(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington nrdatas = lwres_buffer_getuint16(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington nsigs = lwres_buffer_getuint16(b);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Pull off the name itself
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn = CTXMALLOC(sizeof(lwres_grbnresponse_t));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn == NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_NOMEMORY);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdatas = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdatalen = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->sigs = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->siglen = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->base = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->flags = flags;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdclass = rdclass;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdtype = rdtype;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->ttl = ttl;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->nrdatas = nrdatas;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->nsigs = nsigs;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (nrdatas > 0) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdatas = CTXMALLOC(sizeof(char *) * nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->rdatas == NULL) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = LWRES_R_NOMEMORY;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->rdatalen = CTXMALLOC(sizeof(lwres_uint16_t) * nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->rdatalen == NULL) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = LWRES_R_NOMEMORY;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (nsigs > 0) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->sigs = CTXMALLOC(sizeof(char *) * nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->sigs == NULL) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = LWRES_R_NOMEMORY;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn->siglen = CTXMALLOC(sizeof(lwres_uint16_t) * nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->siglen == NULL) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = LWRES_R_NOMEMORY;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Now, pull off the real name.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = lwres_string_parse(b, &grbn->realname, &grbn->realnamelen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != LWRES_R_SUCCESS)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Parse off the rdatas.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (x = 0; x < grbn->nrdatas; x++) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = lwres_data_parse(b, &grbn->rdatas[x],
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington &grbn->rdatalen[x]);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != LWRES_R_SUCCESS)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington /*
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington * Parse off the signatures.
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington */
f1b68725503ff3e46001eee5a1751e29a43a09d1Andreas Gustafsson for (x = 0; x < grbn->nsigs; x++) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = lwres_data_parse(b, &grbn->sigs[x], &grbn->siglen[x]);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (ret != LWRES_R_SUCCESS)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (LWRES_BUFFER_REMAINING(b) != 0) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington ret = LWRES_R_TRAILINGDATA;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington goto out;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington *structp = grbn;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (LWRES_R_SUCCESS);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington out:
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn != NULL) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->rdatas != NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->rdatas, sizeof(char *) * nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->rdatalen != NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->rdatalen,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington sizeof(lwres_uint16_t) * nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->sigs != NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->sigs, sizeof(char *) * nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->siglen != NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->siglen, sizeof(lwres_uint16_t) * nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn, sizeof(lwres_grbnresponse_t));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington return (ret);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Thread-save equivalent to \link lwres_gabn.c lwres_gabn* \endlink routines. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonvoid
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_grbnrequest_free(lwres_context_t *ctx, lwres_grbnrequest_t **structp)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington{
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_grbnrequest_t *grbn;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(ctx != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(structp != NULL && *structp != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn = *structp;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington *structp = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn, sizeof(lwres_grbnrequest_t));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein/*% Thread-save equivalent to \link lwres_gabn.c lwres_gabn* \endlink routines. */
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonvoid
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellingtonlwres_grbnresponse_free(lwres_context_t *ctx, lwres_grbnresponse_t **structp)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington{
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington lwres_grbnresponse_t *grbn;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(ctx != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington REQUIRE(structp != NULL && *structp != NULL);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington grbn = *structp;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington *structp = NULL;
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->nrdatas > 0) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->rdatas, sizeof(char *) * grbn->nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->rdatalen,
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington sizeof(lwres_uint16_t) * grbn->nrdatas);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->nsigs > 0) {
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->sigs, sizeof(char *) * grbn->nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->siglen, sizeof(lwres_uint16_t) * grbn->nsigs);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington }
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington if (grbn->base != NULL)
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn->base, grbn->baselen);
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington CTXFREE(grbn, sizeof(lwres_grbnresponse_t));
668728fed845b9db9c1423946df03d5fc69f4eeeBrian Wellington}