1275970Scy/*
2275970Scy * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3275970Scy * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4275970Scy *
5275970Scy * Redistribution and use in source and binary forms, with or without
6275970Scy * modification, are permitted provided that the following conditions
7275970Scy * are met:
8275970Scy * 1. Redistributions of source code must retain the above copyright
9275970Scy *    notice, this list of conditions and the following disclaimer.
10275970Scy * 2. Redistributions in binary form must reproduce the above copyright
11275970Scy *    notice, this list of conditions and the following disclaimer in the
12275970Scy *    documentation and/or other materials provided with the distribution.
13275970Scy * 3. The name of the author may not be used to endorse or promote products
14275970Scy *    derived from this software without specific prior written permission.
15275970Scy *
16275970Scy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17275970Scy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18275970Scy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19275970Scy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20275970Scy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22275970Scy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23275970Scy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24275970Scy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25275970Scy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26275970Scy */
27275970Scy#ifndef EVENT2_TAG_H_INCLUDED_
28275970Scy#define EVENT2_TAG_H_INCLUDED_
29275970Scy
30275970Scy/** @file event2/tag.h
31275970Scy
32275970Scy  Helper functions for reading and writing tagged data onto buffers.
33275970Scy
34275970Scy */
35275970Scy
36275970Scy#include <event2/visibility.h>
37275970Scy
38275970Scy#ifdef __cplusplus
39275970Scyextern "C" {
40275970Scy#endif
41275970Scy
42275970Scy#include <event2/event-config.h>
43275970Scy#ifdef EVENT__HAVE_SYS_TYPES_H
44275970Scy#include <sys/types.h>
45275970Scy#endif
46275970Scy#ifdef EVENT__HAVE_SYS_TIME_H
47275970Scy#include <sys/time.h>
48275970Scy#endif
49275970Scy
50275970Scy/* For int types. */
51275970Scy#include <event2/util.h>
52275970Scy
53275970Scystruct evbuffer;
54275970Scy
55275970Scy/*
56275970Scy * Marshaling tagged data - We assume that all tags are inserted in their
57275970Scy * numeric order - so that unknown tags will always be higher than the
58275970Scy * known ones - and we can just ignore the end of an event buffer.
59275970Scy */
60275970Scy
61275970ScyEVENT2_EXPORT_SYMBOL
62275970Scyvoid evtag_init(void);
63275970Scy
64275970Scy/**
65275970Scy   Unmarshals the header and returns the length of the payload
66275970Scy
67275970Scy   @param evbuf the buffer from which to unmarshal data
68275970Scy   @param ptag a pointer in which the tag id is being stored
69275970Scy   @returns -1 on failure or the number of bytes in the remaining payload.
70275970Scy*/
71275970ScyEVENT2_EXPORT_SYMBOL
72275970Scyint evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag);
73275970Scy
74275970ScyEVENT2_EXPORT_SYMBOL
75275970Scyvoid evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
76275970Scy    ev_uint32_t len);
77275970ScyEVENT2_EXPORT_SYMBOL
78275970Scyvoid evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
79275970Scy    struct evbuffer *data);
80275970Scy
81275970Scy/**
82275970Scy  Encode an integer and store it in an evbuffer.
83275970Scy
84275970Scy  We encode integers by nybbles; the first nibble contains the number
85275970Scy  of significant nibbles - 1;  this allows us to encode up to 64-bit
86275970Scy  integers.  This function is byte-order independent.
87275970Scy
88275970Scy  @param evbuf evbuffer to store the encoded number
89275970Scy  @param number a 32-bit integer
90275970Scy */
91275970ScyEVENT2_EXPORT_SYMBOL
92275970Scyvoid evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number);
93275970ScyEVENT2_EXPORT_SYMBOL
94275970Scyvoid evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number);
95275970Scy
96275970ScyEVENT2_EXPORT_SYMBOL
97275970Scyvoid evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
98275970Scy    ev_uint32_t integer);
99275970ScyEVENT2_EXPORT_SYMBOL
100275970Scyvoid evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
101275970Scy    ev_uint64_t integer);
102275970Scy
103275970ScyEVENT2_EXPORT_SYMBOL
104275970Scyvoid evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
105275970Scy    const char *string);
106275970Scy
107275970ScyEVENT2_EXPORT_SYMBOL
108275970Scyvoid evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
109275970Scy    struct timeval *tv);
110275970Scy
111275970ScyEVENT2_EXPORT_SYMBOL
112275970Scyint evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
113275970Scy    struct evbuffer *dst);
114275970ScyEVENT2_EXPORT_SYMBOL
115275970Scyint evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
116275970ScyEVENT2_EXPORT_SYMBOL
117275970Scyint evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
118275970ScyEVENT2_EXPORT_SYMBOL
119275970Scyint evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
120275970ScyEVENT2_EXPORT_SYMBOL
121275970Scyint evtag_consume(struct evbuffer *evbuf);
122275970Scy
123275970ScyEVENT2_EXPORT_SYMBOL
124275970Scyint evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
125275970Scy    ev_uint32_t *pinteger);
126275970ScyEVENT2_EXPORT_SYMBOL
127275970Scyint evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
128275970Scy    ev_uint64_t *pinteger);
129275970Scy
130275970ScyEVENT2_EXPORT_SYMBOL
131275970Scyint evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
132275970Scy    void *data, size_t len);
133275970Scy
134275970ScyEVENT2_EXPORT_SYMBOL
135275970Scyint evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
136275970Scy    char **pstring);
137275970Scy
138275970ScyEVENT2_EXPORT_SYMBOL
139275970Scyint evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
140275970Scy    struct timeval *ptv);
141275970Scy
142275970Scy#ifdef __cplusplus
143275970Scy}
144275970Scy#endif
145275970Scy
146275970Scy#endif /* EVENT2_TAG_H_INCLUDED_ */
147