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