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