Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 587 lines (489 sloc) 14.103 kb
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
1 /*
b85b710 Update copyright statements to reflect the facts that:
Nick Mathewson authored
2 * Copyright (c) 2003-2009 Niels Provos <provos@citi.umich.edu>
17efc1c Update all our copyright notices to say "2010"
Nick Mathewson authored
3 * Copyright (c) 2009-2010 Niels Provos and Nick Mathewson
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
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
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
28 #include "event-config.h"
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
29
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
30 #ifdef _EVENT_HAVE_SYS_TYPES_H
4e1ec3e Make all the C files in the libraries compile under MSVC 2005 Express. ...
Nick Mathewson authored
31 #include <sys/types.h>
32 #endif
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
33 #ifdef _EVENT_HAVE_SYS_PARAM_H
4e1ec3e Make all the C files in the libraries compile under MSVC 2005 Express. ...
Nick Mathewson authored
34 #include <sys/param.h>
35 #endif
36
868f10e @provos mingw fixes from Nick
provos authored
37 #ifdef WIN32
38 #define WIN32_LEAN_AND_MEAN
4e1ec3e Make all the C files in the libraries compile under MSVC 2005 Express. ...
Nick Mathewson authored
39 #include <winsock2.h>
868f10e @provos mingw fixes from Nick
provos authored
40 #include <windows.h>
41 #undef WIN32_LEAN_AND_MEAN
42 #else
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
43 #include <sys/ioctl.h>
868f10e @provos mingw fixes from Nick
provos authored
44 #endif
45
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
46 #include <sys/queue.h>
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
47 #ifdef _EVENT_HAVE_SYS_TIME_H
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
48 #include <sys/time.h>
49 #endif
50
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
868f10e @provos mingw fixes from Nick
provos authored
55 #ifndef WIN32
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
56 #include <syslog.h>
868f10e @provos mingw fixes from Nick
provos authored
57 #endif
8889a77 Replace all use of config.h with event-config.h.
Nick Mathewson authored
58 #ifdef _EVENT_HAVE_UNISTD_H
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
59 #include <unistd.h>
4e1ec3e Make all the C files in the libraries compile under MSVC 2005 Express. ...
Nick Mathewson authored
60 #endif
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
61
0ac7307 r15193@tombo: nickm | 2008-04-16 16:00:35 -0400
Nick Mathewson authored
62 #include "event2/event.h"
63 #include "event2/tag.h"
64 #include "event2/buffer.h"
169321c Rename four internal headers to follow the -internal.h convention.
Nick Mathewson authored
65 #include "log-internal.h"
7eb250e r14939@tombo: nickm | 2007-11-25 11:59:26 -0500
Nick Mathewson authored
66 #include "mm-internal.h"
e865eb9 More msvc build tweaks.
Nick Mathewson authored
67 #include "util-internal.h"
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
68
5b3fb5b More documentation and unit tests for event_tagging.
Nick Mathewson authored
69 /*
70 Here's our wire format:
71
72 Stream = TaggedData*
73
74 TaggedData = Tag Length Data
75 where the integer value of 'Length' is the length of 'data'.
76
77 Tag = HByte* LByte
78 where HByte is a byte with the high bit set, and LByte is a byte
79 with the high bit clear. The integer value of the tag is taken
80 by concatenating the lower 7 bits from all the tags. So for example,
81 the tag 0x66 is encoded as [66], whereas the tag 0x166 is encoded as
82 [82 66]
83
84 Length = Integer
85
86 Integer = NNibbles Nibble* Padding?
87 where NNibbles is a 4-bit value encoding the number of nibbles-1,
88 and each Nibble is 4 bits worth of encoded integer, in big-endian
89 order. If the total encoded integer size is an odd number of nibbles,
90 a final padding nibble with value 0 is appended.
91 */
92
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
93 int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf);
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
94 int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf);
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
95 int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag);
96 int evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
97
98 void
35983cd r14618@catbus: nickm | 2007-08-16 17:11:47 -0400
Nick Mathewson authored
99 evtag_init(void)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
100 {
101 }
102
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
103 /*
104 * We encode integers by nibbles; the first nibble contains the number
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
105 * of significant nibbles - 1; this allows us to encode up to 64-bit
106 * integers. This function is byte-order independent.
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
107 *
108 * @param number a 32-bit unsigned integer to encode
109 * @param data a pointer to where the data should be written. Must
110 * have at least 5 bytes free.
111 * @return the number of bytes written into data.
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
112 */
113
99a1063 @provos support 64-bit integers in rpc structs
provos authored
114 #define ENCODE_INT_INTERNAL(data, number) do { \
115 int off = 1, nibbles = 0; \
116 \
117 memset(data, 0, sizeof(number)+1); \
118 while (number) { \
119 if (off & 0x1) \
120 data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f); \
121 else \
122 data[off/2] = (data[off/2] & 0x0f) | \
123 ((number & 0x0f) << 4); \
124 number >>= 4; \
125 off++; \
126 } \
127 \
128 if (off > 2) \
129 nibbles = off - 2; \
130 \
131 /* Off - 1 is the number of encoded nibbles */ \
132 data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4); \
133 \
134 return ((off + 1) / 2); \
135 } while (0)
136
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
137 static inline int
138 encode_int_internal(ev_uint8_t *data, ev_uint32_t number)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
139 {
99a1063 @provos support 64-bit integers in rpc structs
provos authored
140 ENCODE_INT_INTERNAL(data, number);
141 }
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
142
99a1063 @provos support 64-bit integers in rpc structs
provos authored
143 static inline int
144 encode_int64_internal(ev_uint8_t *data, ev_uint64_t number)
145 {
146 ENCODE_INT_INTERNAL(data, number);
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
147 }
148
149 void
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
150 evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number)
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
151 {
152 ev_uint8_t data[5];
153 int len = encode_int_internal(data, number);
154 evbuffer_add(evbuf, data, len);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
155 }
156
99a1063 @provos support 64-bit integers in rpc structs
provos authored
157 void
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
158 evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number)
99a1063 @provos support 64-bit integers in rpc structs
provos authored
159 {
160 ev_uint8_t data[9];
161 int len = encode_int64_internal(data, number);
162 evbuffer_add(evbuf, data, len);
163 }
164
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
165 /*
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
166 * Support variable length encoding of tags; we use the high bit in each
167 * octet as a continuation signal.
168 */
169
170 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
171 evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag)
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
172 {
173 int bytes = 0;
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
174 ev_uint8_t data[5];
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
175
176 memset(data, 0, sizeof(data));
177 do {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
178 ev_uint8_t lower = tag & 0x7f;
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
179 tag >>= 7;
180
181 if (tag)
182 lower |= 0x80;
183
184 data[bytes++] = lower;
185 } while (tag);
186
187 if (evbuf != NULL)
188 evbuffer_add(evbuf, data, bytes);
189
190 return (bytes);
191 }
192
ffd606c @provos prefix {encode,decode}_tag functions with evtag to avoid collisions
provos authored
193 static int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
194 decode_tag_internal(ev_uint32_t *ptag, struct evbuffer *evbuf, int dodrain)
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
195 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
196 ev_uint32_t number = 0;
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_compat...
Nick Mathewson authored
197 int len = evbuffer_get_length(evbuf);
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
198 ev_uint8_t *data;
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
199 int count = 0, shift = 0, done = 0;
200
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
201 /*
202 * the encoding of a number is at most one byte more than its
203 * storage size. however, it may also be much smaller.
204 */
205 data = evbuffer_pullup(
206 evbuf, len < sizeof(number) + 1 ? len : sizeof(number) + 1);
207
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
208 while (count++ < len) {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
209 ev_uint8_t lower = *data++;
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
210 number |= (lower & 0x7f) << shift;
211 shift += 7;
212
213 if (!(lower & 0x80)) {
214 done = 1;
215 break;
216 }
217 }
218
219 if (!done)
220 return (-1);
221
222 if (dodrain)
223 evbuffer_drain(evbuf, count);
224
225 if (ptag != NULL)
226 *ptag = number;
227
228 return (count);
229 }
230
231 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
232 evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf)
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
233 {
234 return (decode_tag_internal(ptag, evbuf, 1 /* dodrain */));
235 }
236
237 /*
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
238 * Marshal a data type, the general format is as follows:
239 *
240 * tag number: one byte; length: var bytes; payload: var bytes
241 */
242
243 void
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
244 evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag,
245 const void *data, ev_uint32_t len)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
246 {
ffd606c @provos prefix {encode,decode}_tag functions with evtag to avoid collisions
provos authored
247 evtag_encode_tag(evbuf, tag);
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
248 evtag_encode_int(evbuf, len);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
249 evbuffer_add(evbuf, (void *)data, len);
250 }
251
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
252 void
253 evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
254 struct evbuffer *data)
255 {
256 evtag_encode_tag(evbuf, tag);
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
257 evtag_encode_int(evbuf, evbuffer_get_length(data));
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
258 evbuffer_add_buffer(evbuf, data);
259 }
260
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
261 /* Marshaling for integers */
262 void
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
263 evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
264 {
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
265 ev_uint8_t data[5];
266 int len = encode_int_internal(data, integer);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
267
ffd606c @provos prefix {encode,decode}_tag functions with evtag to avoid collisions
provos authored
268 evtag_encode_tag(evbuf, tag);
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
269 evtag_encode_int(evbuf, len);
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
270 evbuffer_add(evbuf, data, len);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
271 }
272
273 void
99a1063 @provos support 64-bit integers in rpc structs
provos authored
274 evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
275 ev_uint64_t integer)
276 {
277 ev_uint8_t data[9];
278 int len = encode_int64_internal(data, integer);
279
280 evtag_encode_tag(evbuf, tag);
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
281 evtag_encode_int(evbuf, len);
99a1063 @provos support 64-bit integers in rpc structs
provos authored
282 evbuffer_add(evbuf, data, len);
283 }
284
285 void
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
286 evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, const char *string)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
287 {
288 evtag_marshal(buf, tag, string, strlen(string));
289 }
290
291 void
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
292 evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, struct timeval *tv)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
293 {
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
294 ev_uint8_t data[10];
295 int len = encode_int_internal(data, tv->tv_sec);
296 len += encode_int_internal(data + len, tv->tv_usec);
297 evtag_marshal(evbuf, tag, data, len);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
298 }
299
99a1063 @provos support 64-bit integers in rpc structs
provos authored
300 #define DECODE_INT_INTERNAL(number, maxnibbles, pnumber, evbuf, offset) \
301 do { \
302 ev_uint8_t *data; \
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_compat...
Nick Mathewson authored
303 int len = evbuffer_get_length(evbuf) - offset; \
99a1063 @provos support 64-bit integers in rpc structs
provos authored
304 int nibbles = 0; \
305 \
306 if (len <= 0) \
307 return (-1); \
308 \
309 /* XXX(niels): faster? */ \
310 data = evbuffer_pullup(evbuf, offset + 1) + offset; \
311 \
312 nibbles = ((data[0] & 0xf0) >> 4) + 1; \
313 if (nibbles > maxnibbles || (nibbles >> 1) + 1 > len) \
314 return (-1); \
315 len = (nibbles >> 1) + 1; \
316 \
317 data = evbuffer_pullup(evbuf, offset + len) + offset; \
318 \
319 while (nibbles > 0) { \
320 number <<= 4; \
321 if (nibbles & 0x1) \
322 number |= data[nibbles >> 1] & 0x0f; \
323 else \
324 number |= (data[nibbles >> 1] & 0xf0) >> 4; \
325 nibbles--; \
326 } \
327 \
328 *pnumber = number; \
329 \
330 return (len); \
331 } while (0)
332
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
333 /* Internal: decode an integer from an evbuffer, without draining it.
334 * Only integers up to 32-bits are supported.
335 *
336 * @param evbuf the buffer to read from
337 * @param offset an index into the buffer at which we should start reading.
338 * @param pnumber a pointer to receive the integer.
339 * @return The length of the number as encoded, or -1 on error.
340 */
99a1063 @provos support 64-bit integers in rpc structs
provos authored
341
f0ff792 @provos fixes from Joerg Sonnenberger:
provos authored
342 static int
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
343 decode_int_internal(ev_uint32_t *pnumber, struct evbuffer *evbuf, int offset)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
344 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
345 ev_uint32_t number = 0;
99a1063 @provos support 64-bit integers in rpc structs
provos authored
346 DECODE_INT_INTERNAL(number, 8, pnumber, evbuf, offset);
347 }
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
348
99a1063 @provos support 64-bit integers in rpc structs
provos authored
349 static int
350 decode_int64_internal(ev_uint64_t *pnumber, struct evbuffer *evbuf, int offset)
351 {
352 ev_uint64_t number = 0;
353 DECODE_INT_INTERNAL(number, 16, pnumber, evbuf, offset);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
354 }
355
356 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
357 evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
358 {
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
359 int res = decode_int_internal(pnumber, evbuf, 0);
360 if (res != -1)
361 evbuffer_drain(evbuf, res);
362
363 return (res == -1 ? -1 : 0);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
364 }
365
366 int
e8400a4 Rename encode_int(64) to avoid polluting the global namespace.
Nick Mathewson authored
367 evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf)
368 {
369 int res = decode_int64_internal(pnumber, evbuf, 0);
370 if (res != -1)
371 evbuffer_drain(evbuf, res);
372
373 return (res == -1 ? -1 : 0);
374 }
375
376 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
377 evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
378 {
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
379 return (decode_tag_internal(ptag, evbuf, 0 /* dodrain */));
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
380 }
381
382 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
383 evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
384 {
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
385 int res, len;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
386
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
387 len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
388 if (len == -1)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
389 return (-1);
390
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
391 res = decode_int_internal(plength, evbuf, len);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
392 if (res == -1)
393 return (-1);
394
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
395 *plength += res + len;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
396
397 return (0);
398 }
399
400 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
401 evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength)
32acc28 @provos a few more bug fixes
provos authored
402 {
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
403 int res, len;
32acc28 @provos a few more bug fixes
provos authored
404
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
405 len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
406 if (len == -1)
32acc28 @provos a few more bug fixes
provos authored
407 return (-1);
408
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
409 res = decode_int_internal(plength, evbuf, len);
32acc28 @provos a few more bug fixes
provos authored
410 if (res == -1)
411 return (-1);
412
413 return (0);
414 }
415
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
416 /* just unmarshals the header and returns the length of the remaining data */
417
32acc28 @provos a few more bug fixes
provos authored
418 int
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
419 evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
420 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
421 ev_uint32_t len;
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
422
423 if (decode_tag_internal(ptag, evbuf, 1 /* dodrain */) == -1)
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
424 return (-1);
ffd606c @provos prefix {encode,decode}_tag functions with evtag to avoid collisions
provos authored
425 if (evtag_decode_int(&len, evbuf) == -1)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
426 return (-1);
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
427
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_compat...
Nick Mathewson authored
428 if (evbuffer_get_length(evbuf) < len)
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
429 return (-1);
430
431 return (len);
432 }
433
434 int
435 evtag_consume(struct evbuffer *evbuf)
436 {
437 int len;
438 if ((len = evtag_unmarshal_header(evbuf, NULL)) == -1)
439 return (-1);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
440 evbuffer_drain(evbuf, len);
441
442 return (0);
443 }
444
445 /* Reads the data type from an event buffer */
446
447 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
448 evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, struct evbuffer *dst)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
449 {
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
450 int len;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
451
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
452 if ((len = evtag_unmarshal_header(src, ptag)) == -1)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
453 return (-1);
454
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
455 if (evbuffer_add(dst, evbuffer_pullup(src, len), len) == -1)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
456 return (-1);
457
458 evbuffer_drain(src, len);
459
460 return (len);
461 }
462
463 /* Marshaling for integers */
464
465 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
466 evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
467 ev_uint32_t *pinteger)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
468 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
469 ev_uint32_t tag;
470 ev_uint32_t len;
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
471 int result;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
472
ba48719 @provos support for 32-bit tag numbers in rpc structures
provos authored
473 if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
474 return (-1);
475 if (need_tag != tag)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
476 return (-1);
99a1063 @provos support 64-bit integers in rpc structs
provos authored
477 if (evtag_decode_int(&len, evbuf) == -1)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
478 return (-1);
479
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_compat...
Nick Mathewson authored
480 if (evbuffer_get_length(evbuf) < len)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
481 return (-1);
482
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
483 result = decode_int_internal(pinteger, evbuf, 0);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
484 evbuffer_drain(evbuf, len);
e865eb9 More msvc build tweaks.
Nick Mathewson authored
485 if (result < 0 || (size_t)result > len) /* XXX Should this be != rather than > ?*/
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
486 return (-1);
487 else
488 return result;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
489 }
490
99a1063 @provos support 64-bit integers in rpc structs
provos authored
491 int
492 evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
493 ev_uint64_t *pinteger)
494 {
495 ev_uint32_t tag;
496 ev_uint32_t len;
497 int result;
498
499 if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
500 return (-1);
501 if (need_tag != tag)
502 return (-1);
503 if (evtag_decode_int(&len, evbuf) == -1)
504 return (-1);
505
a8f6d96 Actually stop using EVBUFFER_LENGTH/DATA, and move them to buffer_compat...
Nick Mathewson authored
506 if (evbuffer_get_length(evbuf) < len)
99a1063 @provos support 64-bit integers in rpc structs
provos authored
507 return (-1);
508
509 result = decode_int64_internal(pinteger, evbuf, 0);
510 evbuffer_drain(evbuf, len);
e865eb9 More msvc build tweaks.
Nick Mathewson authored
511 if (result < 0 || (size_t)result > len) /* XXX Should this be != rather than > ?*/
99a1063 @provos support 64-bit integers in rpc structs
provos authored
512 return (-1);
513 else
514 return result;
515 }
516
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
517 /* Unmarshal a fixed length tag */
518
519 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
520 evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, void *data,
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
521 size_t len)
522 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
523 ev_uint32_t tag;
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
524 int tag_len;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
525
526 /* Now unmarshal a tag and check that it matches the tag we want */
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
527 if ((tag_len = evtag_unmarshal_header(src, &tag)) == -1 ||
528 tag != need_tag)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
529 return (-1);
530
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
531 if (tag_len != len)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
532 return (-1);
9993137 Remove all trailing whitespace in all the source files.
Nick Mathewson authored
533
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
534 evbuffer_remove(src, data, len);
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
535 return (0);
536 }
537
538 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
539 evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
50f7aae @provos make it compile on mac os x
provos authored
540 char **pstring)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
541 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
542 ev_uint32_t tag;
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
543 int tag_len;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
544
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
545 if ((tag_len = evtag_unmarshal_header(evbuf, &tag)) == -1 ||
546 tag != need_tag)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
547 return (-1);
548
49868b6 r15316@tombo: nickm | 2008-04-24 20:58:36 -0400
Nick Mathewson authored
549 *pstring = mm_malloc(tag_len + 1);
cdf5800 Change an OOM err to a warn in event_tagging.c
Nick Mathewson authored
550 if (*pstring == NULL) {
551 event_warn("%s: malloc", __func__);
552 return -1;
553 }
5c70ea4 @provos improved code for evbuffer; avoids memcpy
provos authored
554 evbuffer_remove(evbuf, *pstring, tag_len);
555 (*pstring)[tag_len] = '\0';
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
556
557 return (0);
558 }
559
560 int
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
561 evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
562 struct timeval *ptv)
563 {
11230f7 r18145@catbus: nickm | 2008-02-18 15:02:20 -0500
Nick Mathewson authored
564 ev_uint32_t tag;
565 ev_uint32_t integer;
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
566 int len, offset, offset2;
567 int result = -1;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
568
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
569 if ((len = evtag_unmarshal_header(evbuf, &tag)) == -1)
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
570 return (-1);
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
571 if (tag != need_tag)
572 goto done;
573 if ((offset = decode_int_internal(&integer, evbuf, 0)) == -1)
574 goto done;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
575 ptv->tv_sec = integer;
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
576 if ((offset2 = decode_int_internal(&integer, evbuf, offset)) == -1)
577 goto done;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
578 ptv->tv_usec = integer;
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
579 if (offset + offset2 > len) /* XXX Should this be != instead of > ? */
580 goto done;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
581
8d2a616 r15242@tombo: nickm | 2008-04-18 09:24:44 -0400
Nick Mathewson authored
582 result = 0;
583 done:
584 evbuffer_drain(evbuf, len);
585 return result;
c4e6099 @provos including the tagging code that is required by event_rpcgen.py; test the
provos authored
586 }
Something went wrong with that request. Please try again.