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