tag.h revision 1.1.1.1
1/* $NetBSD: tag.h,v 1.1.1.1 2013/04/11 16:43:34 christos Exp $ */ 2/* 3 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 4 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28#ifndef _EVENT2_TAG_H_ 29#define _EVENT2_TAG_H_ 30 31/** @file event2/tag.h 32 33 Helper functions for reading and writing tagged data onto buffers. 34 35 */ 36 37#ifdef __cplusplus 38extern "C" { 39#endif 40 41#include <event2/event-config.h> 42#ifdef _EVENT_HAVE_SYS_TYPES_H 43#include <sys/types.h> 44#endif 45#ifdef _EVENT_HAVE_SYS_TIME_H 46#include <sys/time.h> 47#endif 48 49/* For int types. */ 50#include <event2/util.h> 51 52struct evbuffer; 53 54/* 55 * Marshaling tagged data - We assume that all tags are inserted in their 56 * numeric order - so that unknown tags will always be higher than the 57 * known ones - and we can just ignore the end of an event buffer. 58 */ 59 60void evtag_init(void); 61 62/** 63 Unmarshals the header and returns the length of the payload 64 65 @param evbuf the buffer from which to unmarshal data 66 @param ptag a pointer in which the tag id is being stored 67 @returns -1 on failure or the number of bytes in the remaining payload. 68*/ 69int evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag); 70 71void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data, 72 ev_uint32_t len); 73void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag, 74 struct evbuffer *data); 75 76/** 77 Encode an integer and store it in an evbuffer. 78 79 We encode integers by nybbles; the first nibble contains the number 80 of significant nibbles - 1; this allows us to encode up to 64-bit 81 integers. This function is byte-order independent. 82 83 @param evbuf evbuffer to store the encoded number 84 @param number a 32-bit integer 85 */ 86void evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number); 87void evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number); 88 89void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, 90 ev_uint32_t integer); 91void evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag, 92 ev_uint64_t integer); 93 94void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, 95 const char *string); 96 97void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, 98 struct timeval *tv); 99 100int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, 101 struct evbuffer *dst); 102int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag); 103int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength); 104int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength); 105int evtag_consume(struct evbuffer *evbuf); 106 107int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag, 108 ev_uint32_t *pinteger); 109int evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag, 110 ev_uint64_t *pinteger); 111 112int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, 113 void *data, size_t len); 114 115int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag, 116 char **pstring); 117 118int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag, 119 struct timeval *ptv); 120 121#ifdef __cplusplus 122} 123#endif 124 125#endif /* _EVENT2_TAG_H_ */ 126