2005-08-22 01:34:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2003, 2004 Niels Provos <provos@citi.umich.edu>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
2007-03-01 06:25:18 +00:00
|
|
|
#include "config.h"
|
2005-08-22 01:34:34 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-22 01:21:10 +00:00
|
|
|
#ifdef WIN32
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#undef WIN32_LEAN_AND_MEAN
|
|
|
|
#else
|
2005-08-22 01:34:34 +00:00
|
|
|
#include <sys/ioctl.h>
|
2006-11-22 01:21:10 +00:00
|
|
|
#endif
|
|
|
|
|
2005-08-22 01:34:34 +00:00
|
|
|
#include <sys/tree.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2006-11-22 01:21:10 +00:00
|
|
|
#ifndef WIN32
|
2005-08-22 01:34:34 +00:00
|
|
|
#include <syslog.h>
|
2006-11-22 01:21:10 +00:00
|
|
|
#endif
|
2005-08-22 01:34:34 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "event.h"
|
2006-07-18 06:35:48 +00:00
|
|
|
#include "log.h"
|
2005-08-22 01:34:34 +00:00
|
|
|
|
2007-06-30 19:08:46 +00:00
|
|
|
int decode_int(uint32_t *pnumber, struct evbuffer *evbuf);
|
2005-08-22 01:34:34 +00:00
|
|
|
|
2006-11-17 06:06:17 +00:00
|
|
|
static struct evbuffer *_buf; /* not thread safe */
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
evtag_init()
|
|
|
|
{
|
2006-11-17 06:06:17 +00:00
|
|
|
if (_buf != NULL)
|
|
|
|
return;
|
|
|
|
|
2005-08-22 01:34:34 +00:00
|
|
|
if ((_buf = evbuffer_new()) == NULL)
|
2006-07-18 06:35:48 +00:00
|
|
|
event_err(1, "%s: malloc", __func__);
|
2005-08-22 01:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We encode integer's by nibbles; the first nibble contains the number
|
|
|
|
* of significant nibbles - 1; this allows us to encode up to 64-bit
|
|
|
|
* integers. This function is byte-order independent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2007-06-30 19:08:46 +00:00
|
|
|
encode_int(struct evbuffer *evbuf, uint32_t number)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
int off = 1, nibbles = 0;
|
2007-06-30 19:08:46 +00:00
|
|
|
uint8_t data[5];
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
memset(data, 0, sizeof(data));
|
|
|
|
while (number) {
|
|
|
|
if (off & 0x1)
|
|
|
|
data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f);
|
|
|
|
else
|
|
|
|
data[off/2] = (data[off/2] & 0x0f) |
|
|
|
|
((number & 0x0f) << 4);
|
|
|
|
number >>= 4;
|
|
|
|
off++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (off > 2)
|
|
|
|
nibbles = off - 2;
|
|
|
|
|
|
|
|
/* Off - 1 is the number of encoded nibbles */
|
|
|
|
data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4);
|
|
|
|
|
|
|
|
evbuffer_add(evbuf, data, (off + 1) / 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Marshal a data type, the general format is as follows:
|
|
|
|
*
|
|
|
|
* tag number: one byte; length: var bytes; payload: var bytes
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_marshal(struct evbuffer *evbuf, uint8_t tag,
|
|
|
|
const void *data, uint32_t len)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
evbuffer_add(evbuf, &tag, sizeof(tag));
|
|
|
|
encode_int(evbuf, len);
|
|
|
|
evbuffer_add(evbuf, (void *)data, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Marshaling for integers */
|
|
|
|
void
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_marshal_int(struct evbuffer *evbuf, uint8_t tag, uint32_t integer)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
|
|
|
|
encode_int(_buf, integer);
|
|
|
|
|
|
|
|
evbuffer_add(evbuf, &tag, sizeof(tag));
|
|
|
|
encode_int(evbuf, EVBUFFER_LENGTH(_buf));
|
|
|
|
evbuffer_add_buffer(evbuf, _buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_marshal_string(struct evbuffer *buf, uint8_t tag, const char *string)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
evtag_marshal(buf, tag, string, strlen(string));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_marshal_timeval(struct evbuffer *evbuf, uint8_t tag, struct timeval *tv)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
|
|
|
|
|
|
|
|
encode_int(_buf, tv->tv_sec);
|
|
|
|
encode_int(_buf, tv->tv_usec);
|
|
|
|
|
|
|
|
evtag_marshal(evbuf, tag, EVBUFFER_DATA(_buf),
|
|
|
|
EVBUFFER_LENGTH(_buf));
|
|
|
|
}
|
|
|
|
|
2007-06-30 18:58:34 +00:00
|
|
|
static int
|
2007-06-30 19:08:46 +00:00
|
|
|
decode_int_internal(uint32_t *pnumber, struct evbuffer *evbuf, int dodrain)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint32_t number = 0;
|
|
|
|
uint8_t *data = EVBUFFER_DATA(evbuf);
|
2005-08-22 01:34:34 +00:00
|
|
|
int len = EVBUFFER_LENGTH(evbuf);
|
|
|
|
int nibbles = 0, off;
|
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
nibbles = ((data[0] & 0xf0) >> 4) + 1;
|
|
|
|
if (nibbles > 8 || (nibbles >> 1) > len - 1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
off = nibbles;
|
|
|
|
while (off > 0) {
|
|
|
|
number <<= 4;
|
|
|
|
if (off & 0x1)
|
|
|
|
number |= data[off >> 1] & 0x0f;
|
|
|
|
else
|
|
|
|
number |= (data[off >> 1] & 0xf0) >> 4;
|
|
|
|
off--;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = (nibbles >> 1) + 1;
|
|
|
|
if (dodrain)
|
|
|
|
evbuffer_drain(evbuf, len);
|
|
|
|
|
|
|
|
*pnumber = number;
|
|
|
|
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
decode_int(uint32_t *pnumber, struct evbuffer *evbuf)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
return (decode_int_internal(pnumber, evbuf, 1) == -1 ? -1 : 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_peek(struct evbuffer *evbuf, uint8_t *ptag)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
if (EVBUFFER_LENGTH(evbuf) < 2)
|
|
|
|
return (-1);
|
|
|
|
*ptag = EVBUFFER_DATA(evbuf)[0];
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
|
|
|
struct evbuffer tmp;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(evbuf) < 2)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
tmp = *evbuf;
|
|
|
|
tmp.buffer += 1;
|
|
|
|
tmp.off -= 1;
|
|
|
|
|
|
|
|
res = decode_int_internal(plength, &tmp, 0);
|
|
|
|
if (res == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
*plength += res + 1;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-08-27 06:29:52 +00:00
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength)
|
2005-08-27 06:29:52 +00:00
|
|
|
{
|
|
|
|
struct evbuffer tmp;
|
|
|
|
int res;
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(evbuf) < 2)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
tmp = *evbuf;
|
|
|
|
tmp.buffer += 1;
|
|
|
|
tmp.off -= 1;
|
|
|
|
|
|
|
|
res = decode_int_internal(plength, &tmp, 0);
|
|
|
|
if (res == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-08-22 01:34:34 +00:00
|
|
|
int
|
|
|
|
evtag_consume(struct evbuffer *evbuf)
|
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint32_t len;
|
2005-08-22 01:34:34 +00:00
|
|
|
evbuffer_drain(evbuf, 1);
|
|
|
|
if (decode_int(&len, evbuf) == -1)
|
|
|
|
return (-1);
|
|
|
|
evbuffer_drain(evbuf, len);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Reads the data type from an event buffer */
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_unmarshal(struct evbuffer *src, uint8_t *ptag, struct evbuffer *dst)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint8_t tag;
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t integer;
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
if (evbuffer_remove(src, &tag, sizeof(tag)) != sizeof(tag))
|
|
|
|
return (-1);
|
|
|
|
if (decode_int(&integer, src) == -1)
|
|
|
|
return (-1);
|
|
|
|
len = integer;
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(src) < len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (evbuffer_add(dst, EVBUFFER_DATA(src), len) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
evbuffer_drain(src, len);
|
|
|
|
|
|
|
|
*ptag = tag;
|
|
|
|
return (len);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Marshaling for integers */
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_unmarshal_int(struct evbuffer *evbuf, uint8_t need_tag,
|
|
|
|
uint32_t *pinteger)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint8_t tag;
|
|
|
|
uint32_t len;
|
|
|
|
uint32_t integer;
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
if (evbuffer_remove(evbuf, &tag, sizeof(tag)) != sizeof(tag) ||
|
|
|
|
tag != need_tag)
|
|
|
|
return (-1);
|
|
|
|
if (decode_int(&integer, evbuf) == -1)
|
|
|
|
return (-1);
|
|
|
|
len = integer;
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(evbuf) < len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
|
|
|
|
if (evbuffer_add(_buf, EVBUFFER_DATA(evbuf), len) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
evbuffer_drain(evbuf, len);
|
|
|
|
|
|
|
|
return (decode_int(pinteger, _buf));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unmarshal a fixed length tag */
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_unmarshal_fixed(struct evbuffer *src, uint8_t need_tag, void *data,
|
2005-08-22 01:34:34 +00:00
|
|
|
size_t len)
|
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint8_t tag;
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
/* Initialize this event buffer so that we can read into it */
|
|
|
|
evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
|
|
|
|
|
|
|
|
/* Now unmarshal a tag and check that it matches the tag we want */
|
|
|
|
if (evtag_unmarshal(src, &tag, _buf) == -1 || tag != need_tag)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (EVBUFFER_LENGTH(_buf) != len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
memcpy(data, EVBUFFER_DATA(_buf), len);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_unmarshal_string(struct evbuffer *evbuf, uint8_t need_tag,
|
2005-09-09 06:56:12 +00:00
|
|
|
char **pstring)
|
2005-08-22 01:34:34 +00:00
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint8_t tag;
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
|
|
|
|
|
|
|
|
if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
*pstring = calloc(EVBUFFER_LENGTH(_buf) + 1, 1);
|
|
|
|
if (*pstring == NULL)
|
2006-07-18 06:35:48 +00:00
|
|
|
event_err(1, "%s: calloc", __func__);
|
2005-08-22 01:34:34 +00:00
|
|
|
evbuffer_remove(_buf, *pstring, EVBUFFER_LENGTH(_buf));
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-06-30 19:08:46 +00:00
|
|
|
evtag_unmarshal_timeval(struct evbuffer *evbuf, uint8_t need_tag,
|
2005-08-22 01:34:34 +00:00
|
|
|
struct timeval *ptv)
|
|
|
|
{
|
2007-06-30 19:08:46 +00:00
|
|
|
uint8_t tag;
|
|
|
|
uint32_t integer;
|
2005-08-22 01:34:34 +00:00
|
|
|
|
|
|
|
evbuffer_drain(_buf, EVBUFFER_LENGTH(_buf));
|
|
|
|
if (evtag_unmarshal(evbuf, &tag, _buf) == -1 || tag != need_tag)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (decode_int(&integer, _buf) == -1)
|
|
|
|
return (-1);
|
|
|
|
ptv->tv_sec = integer;
|
|
|
|
if (decode_int(&integer, _buf) == -1)
|
|
|
|
return (-1);
|
|
|
|
ptv->tv_usec = integer;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|