tag.h revision 290001
1/*
2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 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#ifndef EVENT2_TAG_H_INCLUDED_
28#define EVENT2_TAG_H_INCLUDED_
29
30/** @file event2/tag.h
31
32  Helper functions for reading and writing tagged data onto buffers.
33
34 */
35
36#include <event2/visibility.h>
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
61EVENT2_EXPORT_SYMBOL
62void evtag_init(void);
63
64/**
65   Unmarshals the header and returns the length of the payload
66
67   @param evbuf the buffer from which to unmarshal data
68   @param ptag a pointer in which the tag id is being stored
69   @returns -1 on failure or the number of bytes in the remaining payload.
70*/
71EVENT2_EXPORT_SYMBOL
72int evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag);
73
74EVENT2_EXPORT_SYMBOL
75void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
76    ev_uint32_t len);
77EVENT2_EXPORT_SYMBOL
78void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
79    struct evbuffer *data);
80
81/**
82  Encode an integer and store it in an evbuffer.
83
84  We encode integers by nybbles; the first nibble contains the number
85  of significant nibbles - 1;  this allows us to encode up to 64-bit
86  integers.  This function is byte-order independent.
87
88  @param evbuf evbuffer to store the encoded number
89  @param number a 32-bit integer
90 */
91EVENT2_EXPORT_SYMBOL
92void evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number);
93EVENT2_EXPORT_SYMBOL
94void evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number);
95
96EVENT2_EXPORT_SYMBOL
97void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
98    ev_uint32_t integer);
99EVENT2_EXPORT_SYMBOL
100void evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
101    ev_uint64_t integer);
102
103EVENT2_EXPORT_SYMBOL
104void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
105    const char *string);
106
107EVENT2_EXPORT_SYMBOL
108void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
109    struct timeval *tv);
110
111EVENT2_EXPORT_SYMBOL
112int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
113    struct evbuffer *dst);
114EVENT2_EXPORT_SYMBOL
115int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
116EVENT2_EXPORT_SYMBOL
117int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
118EVENT2_EXPORT_SYMBOL
119int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
120EVENT2_EXPORT_SYMBOL
121int evtag_consume(struct evbuffer *evbuf);
122
123EVENT2_EXPORT_SYMBOL
124int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
125    ev_uint32_t *pinteger);
126EVENT2_EXPORT_SYMBOL
127int evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
128    ev_uint64_t *pinteger);
129
130EVENT2_EXPORT_SYMBOL
131int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
132    void *data, size_t len);
133
134EVENT2_EXPORT_SYMBOL
135int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
136    char **pstring);
137
138EVENT2_EXPORT_SYMBOL
139int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
140    struct timeval *ptv);
141
142#ifdef __cplusplus
143}
144#endif
145
146#endif /* EVENT2_TAG_H_INCLUDED_ */
147