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