From aa0f5b38aec14428b4b80e06f90ff781f8bca5f1 Mon Sep 17 00:00:00 2001 From: Rene Mayrhofer Date: Mon, 22 May 2006 05:12:18 +0000 Subject: Import initial strongswan 2.7.0 version into SVN. --- lib/liblwres/lwres_noop.c | 255 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 lib/liblwres/lwres_noop.c (limited to 'lib/liblwres/lwres_noop.c') diff --git a/lib/liblwres/lwres_noop.c b/lib/liblwres/lwres_noop.c new file mode 100644 index 000000000..a75fba351 --- /dev/null +++ b/lib/liblwres/lwres_noop.c @@ -0,0 +1,255 @@ +/* + * Copyright (C) 2000, 2001 Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM + * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL + * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING + * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, + * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION + * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* $Id: lwres_noop.c,v 1.1 2004/03/15 20:35:25 as Exp $ */ + +#include + +#include +#include +#include + +#include +#include +#include +#include + +#include "context_p.h" +#include "assert_p.h" + +lwres_result_t +lwres_nooprequest_render(lwres_context_t *ctx, lwres_nooprequest_t *req, + lwres_lwpacket_t *pkt, lwres_buffer_t *b) +{ + unsigned char *buf; + size_t buflen; + int ret; + size_t payload_length; + + REQUIRE(ctx != NULL); + REQUIRE(req != NULL); + REQUIRE(pkt != NULL); + REQUIRE(b != NULL); + + payload_length = sizeof(lwres_uint16_t) + req->datalength; + + buflen = LWRES_LWPACKET_LENGTH + payload_length; + buf = CTXMALLOC(buflen); + if (buf == NULL) + return (LWRES_R_NOMEMORY); + lwres_buffer_init(b, buf, buflen); + + pkt->length = buflen; + pkt->version = LWRES_LWPACKETVERSION_0; + pkt->pktflags &= ~LWRES_LWPACKETFLAG_RESPONSE; + pkt->opcode = LWRES_OPCODE_NOOP; + pkt->result = 0; + pkt->authtype = 0; + pkt->authlength = 0; + + ret = lwres_lwpacket_renderheader(b, pkt); + if (ret != LWRES_R_SUCCESS) { + lwres_buffer_invalidate(b); + CTXFREE(buf, buflen); + return (ret); + } + + INSIST(SPACE_OK(b, payload_length)); + + /* + * Put the length and the data. We know this will fit because we + * just checked for it. + */ + lwres_buffer_putuint16(b, req->datalength); + lwres_buffer_putmem(b, req->data, req->datalength); + + INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0); + + return (LWRES_R_SUCCESS); +} + +lwres_result_t +lwres_noopresponse_render(lwres_context_t *ctx, lwres_noopresponse_t *req, + lwres_lwpacket_t *pkt, lwres_buffer_t *b) +{ + unsigned char *buf; + size_t buflen; + int ret; + size_t payload_length; + + REQUIRE(ctx != NULL); + REQUIRE(req != NULL); + REQUIRE(pkt != NULL); + REQUIRE(b != NULL); + + payload_length = sizeof(lwres_uint16_t) + req->datalength; + + buflen = LWRES_LWPACKET_LENGTH + payload_length; + buf = CTXMALLOC(buflen); + if (buf == NULL) + return (LWRES_R_NOMEMORY); + lwres_buffer_init(b, buf, buflen); + + pkt->length = buflen; + pkt->version = LWRES_LWPACKETVERSION_0; + pkt->pktflags |= LWRES_LWPACKETFLAG_RESPONSE; + pkt->opcode = LWRES_OPCODE_NOOP; + pkt->authtype = 0; + pkt->authlength = 0; + + ret = lwres_lwpacket_renderheader(b, pkt); + if (ret != LWRES_R_SUCCESS) { + lwres_buffer_invalidate(b); + CTXFREE(buf, buflen); + return (ret); + } + + INSIST(SPACE_OK(b, payload_length)); + + /* + * Put the length and the data. We know this will fit because we + * just checked for it. + */ + lwres_buffer_putuint16(b, req->datalength); + lwres_buffer_putmem(b, req->data, req->datalength); + + INSIST(LWRES_BUFFER_AVAILABLECOUNT(b) == 0); + + return (LWRES_R_SUCCESS); +} + +lwres_result_t +lwres_nooprequest_parse(lwres_context_t *ctx, lwres_buffer_t *b, + lwres_lwpacket_t *pkt, lwres_nooprequest_t **structp) +{ + int ret; + lwres_nooprequest_t *req; + + REQUIRE(ctx != NULL); + REQUIRE(b != NULL); + REQUIRE(pkt != NULL); + REQUIRE(structp != NULL && *structp == NULL); + + if ((pkt->pktflags & LWRES_LWPACKETFLAG_RESPONSE) != 0) + return (LWRES_R_FAILURE); + + req = CTXMALLOC(sizeof(lwres_nooprequest_t)); + if (req == NULL) + return (LWRES_R_NOMEMORY); + + if (!SPACE_REMAINING(b, sizeof(lwres_uint16_t))) { + ret = LWRES_R_UNEXPECTEDEND; + goto out; + } + req->datalength = lwres_buffer_getuint16(b); + + if (!SPACE_REMAINING(b, req->datalength)) { + ret = LWRES_R_UNEXPECTEDEND; + goto out; + } + req->data = b->base + b->current; + lwres_buffer_forward(b, req->datalength); + + if (LWRES_BUFFER_REMAINING(b) != 0) { + ret = LWRES_R_TRAILINGDATA; + goto out; + } + + /* success! */ + *structp = req; + return (LWRES_R_SUCCESS); + + /* Error return */ + out: + CTXFREE(req, sizeof(lwres_nooprequest_t)); + return (ret); +} + +lwres_result_t +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->pktflags & LWRES_LWPACKETFLAG_RESPONSE) == 0) + return (LWRES_R_FAILURE); + + req = CTXMALLOC(sizeof(lwres_noopresponse_t)); + if (req == NULL) + return (LWRES_R_NOMEMORY); + + if (!SPACE_REMAINING(b, sizeof(lwres_uint16_t))) { + ret = LWRES_R_UNEXPECTEDEND; + goto out; + } + req->datalength = lwres_buffer_getuint16(b); + + if (!SPACE_REMAINING(b, req->datalength)) { + ret = LWRES_R_UNEXPECTEDEND; + goto out; + } + req->data = b->base + b->current; + + lwres_buffer_forward(b, req->datalength); + if (LWRES_BUFFER_REMAINING(b) != 0) { + ret = LWRES_R_TRAILINGDATA; + goto out; + } + + /* success! */ + *structp = req; + return (LWRES_R_SUCCESS); + + /* 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; + + 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; + + CTXFREE(noop, sizeof(lwres_nooprequest_t)); +} -- cgit v1.2.3