1290001Sglebius/*
2290001Sglebius * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3290001Sglebius * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4290001Sglebius *
5290001Sglebius * Redistribution and use in source and binary forms, with or without
6290001Sglebius * modification, are permitted provided that the following conditions
7290001Sglebius * are met:
8290001Sglebius * 1. Redistributions of source code must retain the above copyright
9290001Sglebius *    notice, this list of conditions and the following disclaimer.
10290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11290001Sglebius *    notice, this list of conditions and the following disclaimer in the
12290001Sglebius *    documentation and/or other materials provided with the distribution.
13290001Sglebius * 3. The name of the author may not be used to endorse or promote products
14290001Sglebius *    derived from this software without specific prior written permission.
15290001Sglebius *
16290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26290001Sglebius */
27290001Sglebius#ifndef EVENT2_TAG_H_INCLUDED_
28290001Sglebius#define EVENT2_TAG_H_INCLUDED_
29290001Sglebius
30290001Sglebius/** @file event2/tag.h
31290001Sglebius
32290001Sglebius  Helper functions for reading and writing tagged data onto buffers.
33290001Sglebius
34290001Sglebius */
35290001Sglebius
36290001Sglebius#include <event2/visibility.h>
37290001Sglebius
38290001Sglebius#ifdef __cplusplus
39290001Sglebiusextern "C" {
40290001Sglebius#endif
41290001Sglebius
42290001Sglebius#include <event2/event-config.h>
43290001Sglebius#ifdef EVENT__HAVE_SYS_TYPES_H
44290001Sglebius#include <sys/types.h>
45290001Sglebius#endif
46290001Sglebius#ifdef EVENT__HAVE_SYS_TIME_H
47290001Sglebius#include <sys/time.h>
48290001Sglebius#endif
49290001Sglebius
50290001Sglebius/* For int types. */
51290001Sglebius#include <event2/util.h>
52290001Sglebius
53290001Sglebiusstruct evbuffer;
54290001Sglebius
55290001Sglebius/*
56290001Sglebius * Marshaling tagged data - We assume that all tags are inserted in their
57290001Sglebius * numeric order - so that unknown tags will always be higher than the
58290001Sglebius * known ones - and we can just ignore the end of an event buffer.
59290001Sglebius */
60290001Sglebius
61290001SglebiusEVENT2_EXPORT_SYMBOL
62290001Sglebiusvoid evtag_init(void);
63290001Sglebius
64290001Sglebius/**
65290001Sglebius   Unmarshals the header and returns the length of the payload
66290001Sglebius
67290001Sglebius   @param evbuf the buffer from which to unmarshal data
68290001Sglebius   @param ptag a pointer in which the tag id is being stored
69290001Sglebius   @returns -1 on failure or the number of bytes in the remaining payload.
70290001Sglebius*/
71290001SglebiusEVENT2_EXPORT_SYMBOL
72290001Sglebiusint evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag);
73290001Sglebius
74290001SglebiusEVENT2_EXPORT_SYMBOL
75290001Sglebiusvoid evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
76290001Sglebius    ev_uint32_t len);
77290001SglebiusEVENT2_EXPORT_SYMBOL
78290001Sglebiusvoid evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
79290001Sglebius    struct evbuffer *data);
80290001Sglebius
81290001Sglebius/**
82290001Sglebius  Encode an integer and store it in an evbuffer.
83290001Sglebius
84290001Sglebius  We encode integers by nybbles; the first nibble contains the number
85290001Sglebius  of significant nibbles - 1;  this allows us to encode up to 64-bit
86290001Sglebius  integers.  This function is byte-order independent.
87290001Sglebius
88290001Sglebius  @param evbuf evbuffer to store the encoded number
89290001Sglebius  @param number a 32-bit integer
90290001Sglebius */
91290001SglebiusEVENT2_EXPORT_SYMBOL
92290001Sglebiusvoid evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number);
93290001SglebiusEVENT2_EXPORT_SYMBOL
94290001Sglebiusvoid evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number);
95290001Sglebius
96290001SglebiusEVENT2_EXPORT_SYMBOL
97290001Sglebiusvoid evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
98290001Sglebius    ev_uint32_t integer);
99290001SglebiusEVENT2_EXPORT_SYMBOL
100290001Sglebiusvoid evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
101290001Sglebius    ev_uint64_t integer);
102290001Sglebius
103290001SglebiusEVENT2_EXPORT_SYMBOL
104290001Sglebiusvoid evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
105290001Sglebius    const char *string);
106290001Sglebius
107290001SglebiusEVENT2_EXPORT_SYMBOL
108290001Sglebiusvoid evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
109290001Sglebius    struct timeval *tv);
110290001Sglebius
111290001SglebiusEVENT2_EXPORT_SYMBOL
112290001Sglebiusint evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
113290001Sglebius    struct evbuffer *dst);
114290001SglebiusEVENT2_EXPORT_SYMBOL
115290001Sglebiusint evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
116290001SglebiusEVENT2_EXPORT_SYMBOL
117290001Sglebiusint evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
118290001SglebiusEVENT2_EXPORT_SYMBOL
119290001Sglebiusint evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
120290001SglebiusEVENT2_EXPORT_SYMBOL
121290001Sglebiusint evtag_consume(struct evbuffer *evbuf);
122290001Sglebius
123290001SglebiusEVENT2_EXPORT_SYMBOL
124290001Sglebiusint evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
125290001Sglebius    ev_uint32_t *pinteger);
126290001SglebiusEVENT2_EXPORT_SYMBOL
127290001Sglebiusint evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
128290001Sglebius    ev_uint64_t *pinteger);
129290001Sglebius
130290001SglebiusEVENT2_EXPORT_SYMBOL
131290001Sglebiusint evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
132290001Sglebius    void *data, size_t len);
133290001Sglebius
134290001SglebiusEVENT2_EXPORT_SYMBOL
135290001Sglebiusint evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
136290001Sglebius    char **pstring);
137290001Sglebius
138290001SglebiusEVENT2_EXPORT_SYMBOL
139290001Sglebiusint evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
140290001Sglebius    struct timeval *ptv);
141290001Sglebius
142290001Sglebius#ifdef __cplusplus
143290001Sglebius}
144290001Sglebius#endif
145290001Sglebius
146290001Sglebius#endif /* EVENT2_TAG_H_INCLUDED_ */
147