tag.h revision 1.3
1/* $NetBSD: tag.h,v 1.3 2015/07/10 14:20:34 christos Exp $ */ 2 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_INCLUDED_ 30#define EVENT2_TAG_H_INCLUDED_ 31 32/** @file event2/tag.h 33 34 Helper functions for reading and writing tagged data onto buffers. 35 36 */ 37 38#include <event2/visibility.h> 39 40#ifdef __cplusplus 41extern "C" { 42#endif 43 44#include <event2/event-config.h> 45#ifdef EVENT__HAVE_SYS_TYPES_H 46#include <sys/types.h> 47#endif 48#ifdef EVENT__HAVE_SYS_TIME_H 49#include <sys/time.h> 50#endif 51 52/* For int types. */ 53#include <event2/util.h> 54 55struct evbuffer; 56 57/* 58 * Marshaling tagged data - We assume that all tags are inserted in their 59 * numeric order - so that unknown tags will always be higher than the 60 * known ones - and we can just ignore the end of an event buffer. 61 */ 62 63EVENT2_EXPORT_SYMBOL 64void evtag_init(void); 65 66/** 67 Unmarshals the header and returns the length of the payload 68 69 @param evbuf the buffer from which to unmarshal data 70 @param ptag a pointer in which the tag id is being stored 71 @returns -1 on failure or the number of bytes in the remaining payload. 72*/ 73EVENT2_EXPORT_SYMBOL 74int evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag); 75 76EVENT2_EXPORT_SYMBOL 77void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data, 78 ev_uint32_t len); 79EVENT2_EXPORT_SYMBOL 80void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag, 81 struct evbuffer *data); 82 83/** 84 Encode an integer and store it in an evbuffer. 85 86 We encode integers by nybbles; the first nibble contains the number 87 of significant nibbles - 1; this allows us to encode up to 64-bit 88 integers. This function is byte-order independent. 89 90 @param evbuf evbuffer to store the encoded number 91 @param number a 32-bit integer 92 */ 93EVENT2_EXPORT_SYMBOL 94void evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number); 95EVENT2_EXPORT_SYMBOL 96void evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number); 97 98EVENT2_EXPORT_SYMBOL 99void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, 100 ev_uint32_t integer); 101EVENT2_EXPORT_SYMBOL 102void evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag, 103 ev_uint64_t integer); 104 105EVENT2_EXPORT_SYMBOL 106void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, 107 const char *string); 108 109EVENT2_EXPORT_SYMBOL 110void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, 111 struct timeval *tv); 112 113EVENT2_EXPORT_SYMBOL 114int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, 115 struct evbuffer *dst); 116EVENT2_EXPORT_SYMBOL 117int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag); 118EVENT2_EXPORT_SYMBOL 119int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength); 120EVENT2_EXPORT_SYMBOL 121int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength); 122EVENT2_EXPORT_SYMBOL 123int evtag_consume(struct evbuffer *evbuf); 124 125EVENT2_EXPORT_SYMBOL 126int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag, 127 ev_uint32_t *pinteger); 128EVENT2_EXPORT_SYMBOL 129int evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag, 130 ev_uint64_t *pinteger); 131 132EVENT2_EXPORT_SYMBOL 133int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, 134 void *data, size_t len); 135 136EVENT2_EXPORT_SYMBOL 137int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag, 138 char **pstring); 139 140EVENT2_EXPORT_SYMBOL 141int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag, 142 struct timeval *ptv); 143 144#ifdef __cplusplus 145} 146#endif 147 148#endif /* EVENT2_TAG_H_INCLUDED_ */ 149