2 * Copyright (c) 2003-2009 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
31 #ifdef EVENT__HAVE_SYS_TYPES_H
32 #include <sys/types.h>
34 #ifdef EVENT__HAVE_SYS_PARAM_H
35 #include <sys/param.h>
39 #define WIN32_LEAN_AND_MEAN
42 #undef WIN32_LEAN_AND_MEAN
45 #ifdef EVENT__HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
48 #include <sys/queue.h>
49 #ifdef EVENT__HAVE_SYS_TIME_H
60 #ifdef EVENT__HAVE_UNISTD_H
65 #include "event2/event.h"
66 #include "event2/tag.h"
67 #include "event2/buffer.h"
68 #include "log-internal.h"
69 #include "mm-internal.h"
70 #include "util-internal.h"
73 Here's our wire format:
77 TaggedData = Tag Length Data
78 where the integer value of 'Length' is the length of 'data'.
81 where HByte is a byte with the high bit set, and LByte is a byte
82 with the high bit clear. The integer value of the tag is taken
83 by concatenating the lower 7 bits from all the tags. So for example,
84 the tag 0x66 is encoded as [66], whereas the tag 0x166 is encoded as
89 Integer = NNibbles Nibble* Padding?
90 where NNibbles is a 4-bit value encoding the number of nibbles-1,
91 and each Nibble is 4 bits worth of encoded integer, in big-endian
92 order. If the total encoded integer size is an odd number of nibbles,
93 a final padding nibble with value 0 is appended.
96 int evtag_decode_int(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
);
97 int evtag_decode_int64(ev_uint64_t
*pnumber
, struct evbuffer
*evbuf
);
98 int evtag_encode_tag(struct evbuffer
*evbuf
, ev_uint32_t tag
);
99 int evtag_decode_tag(ev_uint32_t
*ptag
, struct evbuffer
*evbuf
);
107 * We encode integers by nibbles; the first nibble contains the number
108 * of significant nibbles - 1; this allows us to encode up to 64-bit
109 * integers. This function is byte-order independent.
111 * @param number a 32-bit unsigned integer to encode
112 * @param data a pointer to where the data should be written. Must
113 * have at least 5 bytes free.
114 * @return the number of bytes written into data.
117 #define ENCODE_INT_INTERNAL(data, number) do { \
118 int off = 1, nibbles = 0; \
120 memset(data, 0, sizeof(number)+1); \
123 data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f); \
125 data[off/2] = (data[off/2] & 0x0f) | \
126 ((number & 0x0f) << 4); \
134 /* Off - 1 is the number of encoded nibbles */ \
135 data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4); \
137 return ((off + 1) / 2); \
141 encode_int_internal(ev_uint8_t
*data
, ev_uint32_t number
)
143 ENCODE_INT_INTERNAL(data
, number
);
147 encode_int64_internal(ev_uint8_t
*data
, ev_uint64_t number
)
149 ENCODE_INT_INTERNAL(data
, number
);
153 evtag_encode_int(struct evbuffer
*evbuf
, ev_uint32_t number
)
156 int len
= encode_int_internal(data
, number
);
157 evbuffer_add(evbuf
, data
, len
);
161 evtag_encode_int64(struct evbuffer
*evbuf
, ev_uint64_t number
)
164 int len
= encode_int64_internal(data
, number
);
165 evbuffer_add(evbuf
, data
, len
);
169 * Support variable length encoding of tags; we use the high bit in each
170 * octet as a continuation signal.
174 evtag_encode_tag(struct evbuffer
*evbuf
, ev_uint32_t tag
)
179 memset(data
, 0, sizeof(data
));
181 ev_uint8_t lower
= tag
& 0x7f;
187 data
[bytes
++] = lower
;
191 evbuffer_add(evbuf
, data
, bytes
);
197 decode_tag_internal(ev_uint32_t
*ptag
, struct evbuffer
*evbuf
, int dodrain
)
199 ev_uint32_t number
= 0;
200 size_t len
= evbuffer_get_length(evbuf
);
203 int shift
= 0, done
= 0;
206 * the encoding of a number is at most one byte more than its
207 * storage size. however, it may also be much smaller.
209 data
= evbuffer_pullup(
210 evbuf
, len
< sizeof(number
) + 1 ? len
: sizeof(number
) + 1);
214 while (count
++ < len
) {
215 ev_uint8_t lower
= *data
++;
217 /* Make sure it fits into 32 bits */
220 if ((lower
& 0x7f) > 15)
223 number
|= (lower
& (unsigned)0x7f) << shift
;
226 if (!(lower
& 0x80)) {
236 evbuffer_drain(evbuf
, count
);
241 return count
> INT_MAX
? INT_MAX
: (int)(count
);
245 evtag_decode_tag(ev_uint32_t
*ptag
, struct evbuffer
*evbuf
)
247 return (decode_tag_internal(ptag
, evbuf
, 1 /* dodrain */));
251 * Marshal a data type, the general format is as follows:
253 * tag number: one byte; length: var bytes; payload: var bytes
257 evtag_marshal(struct evbuffer
*evbuf
, ev_uint32_t tag
,
258 const void *data
, ev_uint32_t len
)
260 evtag_encode_tag(evbuf
, tag
);
261 evtag_encode_int(evbuf
, len
);
262 evbuffer_add(evbuf
, (void *)data
, len
);
266 evtag_marshal_buffer(struct evbuffer
*evbuf
, ev_uint32_t tag
,
267 struct evbuffer
*data
)
269 evtag_encode_tag(evbuf
, tag
);
270 /* XXX support more than UINT32_MAX data */
271 evtag_encode_int(evbuf
, (ev_uint32_t
)evbuffer_get_length(data
));
272 evbuffer_add_buffer(evbuf
, data
);
275 /* Marshaling for integers */
277 evtag_marshal_int(struct evbuffer
*evbuf
, ev_uint32_t tag
, ev_uint32_t integer
)
280 int len
= encode_int_internal(data
, integer
);
282 evtag_encode_tag(evbuf
, tag
);
283 evtag_encode_int(evbuf
, len
);
284 evbuffer_add(evbuf
, data
, len
);
288 evtag_marshal_int64(struct evbuffer
*evbuf
, ev_uint32_t tag
,
292 int len
= encode_int64_internal(data
, integer
);
294 evtag_encode_tag(evbuf
, tag
);
295 evtag_encode_int(evbuf
, len
);
296 evbuffer_add(evbuf
, data
, len
);
300 evtag_marshal_string(struct evbuffer
*buf
, ev_uint32_t tag
, const char *string
)
302 /* TODO support strings longer than UINT32_MAX ? */
303 evtag_marshal(buf
, tag
, string
, (ev_uint32_t
)strlen(string
));
307 evtag_marshal_timeval(struct evbuffer
*evbuf
, ev_uint32_t tag
, struct timeval
*tv
)
310 int len
= encode_int_internal(data
, tv
->tv_sec
);
311 len
+= encode_int_internal(data
+ len
, tv
->tv_usec
);
312 evtag_marshal(evbuf
, tag
, data
, len
);
315 #define DECODE_INT_INTERNAL(number, maxnibbles, pnumber, evbuf, offset) \
318 ev_ssize_t len = evbuffer_get_length(evbuf) - offset; \
324 /* XXX(niels): faster? */ \
325 data = evbuffer_pullup(evbuf, offset + 1) + offset; \
329 nibbles = ((data[0] & 0xf0) >> 4) + 1; \
330 if (nibbles > maxnibbles || (nibbles >> 1) + 1 > len) \
332 len = (nibbles >> 1) + 1; \
334 data = evbuffer_pullup(evbuf, offset + len) + offset; \
338 while (nibbles > 0) { \
341 number |= data[nibbles >> 1] & 0x0f; \
343 number |= (data[nibbles >> 1] & 0xf0) >> 4; \
352 /* Internal: decode an integer from an evbuffer, without draining it.
353 * Only integers up to 32-bits are supported.
355 * @param evbuf the buffer to read from
356 * @param offset an index into the buffer at which we should start reading.
357 * @param pnumber a pointer to receive the integer.
358 * @return The length of the number as encoded, or -1 on error.
362 decode_int_internal(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
, int offset
)
364 ev_uint32_t number
= 0;
365 DECODE_INT_INTERNAL(number
, 8, pnumber
, evbuf
, offset
);
369 decode_int64_internal(ev_uint64_t
*pnumber
, struct evbuffer
*evbuf
, int offset
)
371 ev_uint64_t number
= 0;
372 DECODE_INT_INTERNAL(number
, 16, pnumber
, evbuf
, offset
);
376 evtag_decode_int(ev_uint32_t
*pnumber
, struct evbuffer
*evbuf
)
378 int res
= decode_int_internal(pnumber
, evbuf
, 0);
380 evbuffer_drain(evbuf
, res
);
382 return (res
== -1 ? -1 : 0);
386 evtag_decode_int64(ev_uint64_t
*pnumber
, struct evbuffer
*evbuf
)
388 int res
= decode_int64_internal(pnumber
, evbuf
, 0);
390 evbuffer_drain(evbuf
, res
);
392 return (res
== -1 ? -1 : 0);
396 evtag_peek(struct evbuffer
*evbuf
, ev_uint32_t
*ptag
)
398 return (decode_tag_internal(ptag
, evbuf
, 0 /* dodrain */));
402 evtag_peek_length(struct evbuffer
*evbuf
, ev_uint32_t
*plength
)
406 len
= decode_tag_internal(NULL
, evbuf
, 0 /* dodrain */);
410 res
= decode_int_internal(plength
, evbuf
, len
);
414 *plength
+= res
+ len
;
420 evtag_payload_length(struct evbuffer
*evbuf
, ev_uint32_t
*plength
)
424 len
= decode_tag_internal(NULL
, evbuf
, 0 /* dodrain */);
428 res
= decode_int_internal(plength
, evbuf
, len
);
435 /* just unmarshals the header and returns the length of the remaining data */
438 evtag_unmarshal_header(struct evbuffer
*evbuf
, ev_uint32_t
*ptag
)
442 if (decode_tag_internal(ptag
, evbuf
, 1 /* dodrain */) == -1)
444 if (evtag_decode_int(&len
, evbuf
) == -1)
447 if (evbuffer_get_length(evbuf
) < len
)
454 evtag_consume(struct evbuffer
*evbuf
)
457 if ((len
= evtag_unmarshal_header(evbuf
, NULL
)) == -1)
459 evbuffer_drain(evbuf
, len
);
464 /* Reads the data type from an event buffer */
467 evtag_unmarshal(struct evbuffer
*src
, ev_uint32_t
*ptag
, struct evbuffer
*dst
)
471 if ((len
= evtag_unmarshal_header(src
, ptag
)) == -1)
474 if (evbuffer_add(dst
, evbuffer_pullup(src
, len
), len
) == -1)
477 evbuffer_drain(src
, len
);
482 /* Marshaling for integers */
485 evtag_unmarshal_int(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
486 ev_uint32_t
*pinteger
)
492 if (decode_tag_internal(&tag
, evbuf
, 1 /* dodrain */) == -1)
496 if (evtag_decode_int(&len
, evbuf
) == -1)
499 if (evbuffer_get_length(evbuf
) < len
)
502 result
= decode_int_internal(pinteger
, evbuf
, 0);
503 evbuffer_drain(evbuf
, len
);
504 if (result
< 0 || (size_t)result
> len
) /* XXX Should this be != rather than > ?*/
511 evtag_unmarshal_int64(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
512 ev_uint64_t
*pinteger
)
518 if (decode_tag_internal(&tag
, evbuf
, 1 /* dodrain */) == -1)
522 if (evtag_decode_int(&len
, evbuf
) == -1)
525 if (evbuffer_get_length(evbuf
) < len
)
528 result
= decode_int64_internal(pinteger
, evbuf
, 0);
529 evbuffer_drain(evbuf
, len
);
530 if (result
< 0 || (size_t)result
> len
) /* XXX Should this be != rather than > ?*/
536 /* Unmarshal a fixed length tag */
539 evtag_unmarshal_fixed(struct evbuffer
*src
, ev_uint32_t need_tag
, void *data
,
545 /* Now unmarshal a tag and check that it matches the tag we want */
546 if ((tag_len
= evtag_unmarshal_header(src
, &tag
)) < 0 ||
550 if ((size_t)tag_len
!= len
)
553 evbuffer_remove(src
, data
, len
);
558 evtag_unmarshal_string(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
564 if ((tag_len
= evtag_unmarshal_header(evbuf
, &tag
)) == -1 ||
568 *pstring
= mm_malloc(tag_len
+ 1);
569 if (*pstring
== NULL
) {
570 event_warn("%s: malloc", __func__
);
573 evbuffer_remove(evbuf
, *pstring
, tag_len
);
574 (*pstring
)[tag_len
] = '\0';
580 evtag_unmarshal_timeval(struct evbuffer
*evbuf
, ev_uint32_t need_tag
,
585 int len
, offset
, offset2
;
588 if ((len
= evtag_unmarshal_header(evbuf
, &tag
)) == -1)
592 if ((offset
= decode_int_internal(&integer
, evbuf
, 0)) == -1)
594 ptv
->tv_sec
= integer
;
595 if ((offset2
= decode_int_internal(&integer
, evbuf
, offset
)) == -1)
597 ptv
->tv_usec
= integer
;
598 if (offset
+ offset2
> len
) /* XXX Should this be != instead of > ? */
603 evbuffer_drain(evbuf
, len
);