lwpacket.c revision e4f074a2c2340ea80099beebecc3b89aa234fa8f
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein/*
c92c50783e4e93699f2a42643b8f200b9b719c87Automatic Updater * Copyright (C) 2000 Internet Software Consortium.
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Permission to use, copy, modify, and distribute this software for any
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater * purpose with or without fee is hereby granted, provided that the above
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * copyright notice and this permission notice appear in all copies.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * SOFTWARE.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <config.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <assert.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <errno.h>
e21a2904f02a03fa06b6db04d348f65fe9c67b2bMark Andrews#include <stdlib.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <string.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <lwres/lwbuffer.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <lwres/lwpacket.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include "assert_p.h"
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#define LWPACKET_LENGTH (sizeof(isc_uint16_t) * 4 + sizeof(isc_uint32_t) * 5)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinint
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinlwres_lwpacket_renderheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein{
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein REQUIRE(b != NULL);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein REQUIRE(pkt != NULL);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein if (!SPACE_OK(b, LWPACKET_LENGTH))
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (-1);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lwres_buffer_putuint32(b, pkt->length);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lwres_buffer_putuint16(b, pkt->version);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User lwres_buffer_putuint16(b, pkt->flags);
c92c50783e4e93699f2a42643b8f200b9b719c87Automatic Updater lwres_buffer_putuint32(b, pkt->serial);
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews lwres_buffer_putuint32(b, pkt->opcode);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lwres_buffer_putuint32(b, pkt->result);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lwres_buffer_putuint32(b, pkt->recvlength);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lwres_buffer_putuint16(b, pkt->authtype);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein lwres_buffer_putuint16(b, pkt->authlength);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (0);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox Userint
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox Userlwres_lwpacket_parseheader(lwres_buffer_t *b, lwres_lwpacket_t *pkt)
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User{
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User isc_uint32_t space;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User REQUIRE(b != NULL);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User REQUIRE(pkt != NULL);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User if (!SPACE_REMAINING(b, LWPACKET_LENGTH))
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User return (-1);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User space = LWRES_BUFFER_REMAINING(b);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein pkt->length = lwres_buffer_getuint32(b);
71c66a876ecca77923638d3f94cc0783152b2f03Mark Andrews if (pkt->length > space)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (-1);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User pkt->version = lwres_buffer_getuint16(b);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User pkt->flags = lwres_buffer_getuint16(b);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User pkt->serial = lwres_buffer_getuint32(b);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User pkt->opcode = lwres_buffer_getuint32(b);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User pkt->result = lwres_buffer_getuint32(b);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein pkt->recvlength = lwres_buffer_getuint32(b);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein pkt->authtype = lwres_buffer_getuint16(b);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein pkt->authlength = lwres_buffer_getuint16(b);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return (0);
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User}
ea94d370123a5892f6c47a97f21d1b28d44bb168Tinderbox User