lwres_noop.c revision 658db10162f779c8a5ed4e40c77111a7e18492be
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley/*
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * Copyright (C) 2000 Internet Software Consortium.
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley *
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * Permission to use, copy, modify, and distribute this software for any
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * purpose with or without fee is hereby granted, provided that the above
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * copyright notice and this permission notice appear in all copies.
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley *
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
c46bcd7c6908745e19b0b1fe18a23bbaaee553edBob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley * SOFTWARE.
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley */
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley#include <config.h>
f41f183f628a148860a6d1f0070208cddd45b0c6Bob Halley
8ddad82fa3757f6acc28686251d3c37b1a060478Bob Halley#include <assert.h>
8ddad82fa3757f6acc28686251d3c37b1a060478Bob Halley#include <errno.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <stdlib.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <string.h>
8ddad82fa3757f6acc28686251d3c37b1a060478Bob Halley
94167262742225f3c56fb21b3f07cdfc162b7b35Brian Wellington#include <lwres/lwbuffer.h>
94167262742225f3c56fb21b3f07cdfc162b7b35Brian Wellington#include <lwres/lwpacket.h>
6d5dcd0dc9bdbd679282b1ffc47987d24c3a1346Bob Halley#include <lwres/lwres.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include "context_p.h"
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include "assert_p.h"
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halleyint
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Grafflwres_nooprequest_render(lwres_context_t *ctx, lwres_nooprequest_t *req,
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley lwres_lwpacket_t *pkt, lwres_buffer_t *b)
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley{
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff unsigned char *buf;
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff size_t buflen;
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley int ret;
53f06462e89cd2b5b8639a1d8c48094558974437William King size_t payload_length;
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley
20eb22375f52b35105fa36263f83f116b580eba8Bob Halley REQUIRE(ctx != NULL);
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley REQUIRE(req != NULL);
1292abfde97472be1625898f74b4d6f33ceafc8cBob Halley REQUIRE(b != NULL);
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley payload_length = sizeof(isc_uint16_t) + req->datalength;
c9f9dd2dd2344e45cf7b95d064338d97f08f1fb8Bob Halley
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley buflen = sizeof(lwres_lwpacket_t) + payload_length;
9a2eda6f8e435ecf0d84502c84b1e75e2f5e9220Bob Halley buf = CTXMALLOC(buflen);
3d7e707dd6059735e48d54af7dfc96927e22ca9aBob Halley if (buf == NULL) {
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence errno = ENOMEM;
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence return (-1);
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence }
3ff55a3111fe09f517218905248974b8319b2c59Mark Andrews lwres_buffer_init(b, buf, buflen);
e535faecc70425e7c9fc3faf4064792804c125a0Bob Halley
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews pkt->length = buflen;
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff pkt->version = LWRES_LWPACKETVERSION_0;
706f4a2b36a4687647194df6c6cd4dc414a5238aMichael Graff pkt->flags = 0;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff pkt->opcode = LWRES_OPCODE_NOOP;
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews pkt->result = 0;
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews pkt->authtype = 0;
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley pkt->authlength = 0;
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley
20eb22375f52b35105fa36263f83f116b580eba8Bob Halley ret = lwres_lwpacket_renderheader(b, pkt);
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley if (ret != 0) {
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley lwres_buffer_invalidate(b);
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley CTXFREE(buf, buflen);
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley return (ret);
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley }
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley INSIST(SPACE_OK(b, payload_length));
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley /*
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley * Put the length and the data. We know this will fit because we
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley * just checked for it.
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley */
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley lwres_buffer_putuint16(b, req->datalength);
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley lwres_buffer_putmem(b, req->data, req->datalength);
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halley
7243506104b74e1a77cbd7040ca8379ff63c816fBob Halley return (0);
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews}
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews
56b40c0185bcbd4a04422acc1211b5e2d2ba8a3eBob Halleyint
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halleylwres_noopresponse_render(lwres_context_t *ctx, lwres_noopresponse_t *req,
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley lwres_lwpacket_t *pkt, lwres_buffer_t *b)
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley{
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley unsigned char *buf;
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley size_t buflen;
7732d70d2f0d4d69bd9acadc393dc099efc7b95bBob Halley int ret;
20eb22375f52b35105fa36263f83f116b580eba8Bob Halley size_t payload_length;
20eb22375f52b35105fa36263f83f116b580eba8Bob Halley
20eb22375f52b35105fa36263f83f116b580eba8Bob Halley REQUIRE(ctx != NULL);
20eb22375f52b35105fa36263f83f116b580eba8Bob Halley REQUIRE(req != NULL);
1292abfde97472be1625898f74b4d6f33ceafc8cBob Halley REQUIRE(b != NULL);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff payload_length = sizeof(isc_uint16_t) + req->datalength;
1292abfde97472be1625898f74b4d6f33ceafc8cBob Halley
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff buflen = sizeof(lwres_lwpacket_t) + payload_length;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley buf = CTXMALLOC(buflen);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff if (buf == NULL) {
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff errno = ENOMEM;
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff return (-1);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley }
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff lwres_buffer_init(b, buf, buflen);
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff pkt->length = buflen;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley pkt->version = LWRES_LWPACKETVERSION_0;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff pkt->flags |= LWRES_LWPACKETFLAG_RESPONSE;
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley pkt->opcode = LWRES_OPCODE_NOOP;
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff pkt->authtype = 0;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley pkt->authlength = 0;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley ret = lwres_lwpacket_renderheader(b, pkt);
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff if (ret != 0) {
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley lwres_buffer_invalidate(b);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff CTXFREE(buf, buflen);
c9f9dd2dd2344e45cf7b95d064338d97f08f1fb8Bob Halley return (ret);
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff }
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff INSIST(SPACE_OK(b, payload_length));
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff /*
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley * Put the length and the data. We know this will fit because we
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff * just checked for it.
9a2eda6f8e435ecf0d84502c84b1e75e2f5e9220Bob Halley */
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff lwres_buffer_putuint16(b, req->datalength);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley lwres_buffer_putmem(b, req->data, req->datalength);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0);
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley return (0);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff}
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graffint
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halleylwres_nooprequest_parse(lwres_context_t *ctx, lwres_buffer_t *b,
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff lwres_lwpacket_t *pkt, lwres_nooprequest_t **structp)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews{
7cde03651858b494a50ad13d4587c865e9e27868Michael Graff int ret;
7cde03651858b494a50ad13d4587c865e9e27868Michael Graff lwres_nooprequest_t *req;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence REQUIRE(ctx != NULL);
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff REQUIRE(b != NULL);
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley REQUIRE(pkt != NULL);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff REQUIRE(structp != NULL && *structp == NULL);
9e4292a2b46bc30568bd1eb204761f7134609405David Lawrence
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff if ((pkt->flags & LWRES_LWPACKETFLAG_RESPONSE) != 0)
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley return (-1);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
3ff55a3111fe09f517218905248974b8319b2c59Mark Andrews req = CTXMALLOC(sizeof(lwres_nooprequest_t));
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff if (req == NULL)
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley return (-1);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
e535faecc70425e7c9fc3faf4064792804c125a0Bob Halley if (!SPACE_REMAINING(b, sizeof(isc_uint16_t))) {
60804eec9b2e36ead801e6ff7ad46586774ad828Michael Graff ret = -1;
64e41159a919b0711321fe688ca5da4f4d1b7d80Bob Halley goto out;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff }
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews req->datalength = lwres_buffer_getuint16(b);
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews if (!SPACE_REMAINING(b, req->datalength)) {
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews ret = -1;
60650dd537ca5e2eda953914bf5715d5e8f8b872Mark Andrews goto out;
7cde03651858b494a50ad13d4587c865e9e27868Michael Graff }
7cde03651858b494a50ad13d4587c865e9e27868Michael Graff req->data = b->base + b->current;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff /* success! */
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff *structp = req;
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff return (0);
adde4612541b23d82b00741563ef84cb5192df8cMichael Graff
706f4a2b36a4687647194df6c6cd4dc414a5238aMichael Graff /* Error return */
3d7e707dd6059735e48d54af7dfc96927e22ca9aBob Halley out:
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley CTXFREE(req, sizeof(lwres_nooprequest_t));
44aae046c38e796e581110b7ecdf4478167d684dBob Halley return (ret);
}
int
lwres_noopresponse_parse(lwres_context_t *ctx, lwres_buffer_t *b,
lwres_lwpacket_t *pkt, lwres_noopresponse_t **structp)
{
int ret;
lwres_noopresponse_t *req;
REQUIRE(ctx != NULL);
REQUIRE(b != NULL);
REQUIRE(pkt != NULL);
REQUIRE(structp != NULL && *structp == NULL);
if ((pkt->flags & LWRES_LWPACKETFLAG_RESPONSE) == 0)
return (-1);
req = CTXMALLOC(sizeof(lwres_noopresponse_t));
if (req == NULL)
return (-1);
if (!SPACE_REMAINING(b, sizeof(isc_uint16_t))) {
ret = -1;
goto out;
}
req->datalength = lwres_buffer_getuint16(b);
if (!SPACE_REMAINING(b, req->datalength)) {
ret = -1;
goto out;
}
req->data = b->base + b->current;
/* success! */
*structp = req;
return (0);
/* Error return */
out:
CTXFREE(req, sizeof(lwres_noopresponse_t));
return (ret);
}
void
lwres_noopresponse_free(lwres_context_t *ctx, lwres_noopresponse_t **structp)
{
lwres_noopresponse_t *noop;
REQUIRE(ctx != NULL);
REQUIRE(structp != NULL && *structp != NULL);
noop = *structp;
*structp = NULL;
if (noop->buffer != NULL)
CTXFREE(noop->buffer, noop->buflen);
CTXFREE(noop, sizeof(lwres_noopresponse_t));
}
void
lwres_nooprequest_free(lwres_context_t *ctx, lwres_nooprequest_t **structp)
{
lwres_nooprequest_t *noop;
REQUIRE(ctx != NULL);
REQUIRE(structp != NULL && *structp != NULL);
noop = *structp;
*structp = NULL;
if (noop->buffer != NULL)
CTXFREE(noop->buffer, noop->buflen);
CTXFREE(noop, sizeof(lwres_nooprequest_t));
}