Fixed issue with cmake version generation
[libevent.git] / event_tagging.c
blob6459dfa72e5b5c56b5c2c13ed50a042ddf00ae3a
1 /*
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
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.
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>
33 #endif
34 #ifdef EVENT__HAVE_SYS_PARAM_H
35 #include <sys/param.h>
36 #endif
38 #ifdef _WIN32
39 #define WIN32_LEAN_AND_MEAN
40 #include <winsock2.h>
41 #include <windows.h>
42 #undef WIN32_LEAN_AND_MEAN
43 #endif
45 #ifdef EVENT__HAVE_SYS_IOCTL_H
46 #include <sys/ioctl.h>
47 #endif
48 #include <sys/queue.h>
49 #ifdef EVENT__HAVE_SYS_TIME_H
50 #include <sys/time.h>
51 #endif
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #ifndef _WIN32
58 #include <syslog.h>
59 #endif
60 #ifdef EVENT__HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63 #include <limits.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:
75 Stream = TaggedData*
77 TaggedData = Tag Length Data
78 where the integer value of 'Length' is the length of 'data'.
80 Tag = HByte* LByte
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
85 [82 66]
87 Length = Integer
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);
101 void
102 evtag_init(void)
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); \
121 while (number) { \
122 if (off & 0x1) \
123 data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f); \
124 else \
125 data[off/2] = (data[off/2] & 0x0f) | \
126 ((number & 0x0f) << 4); \
127 number >>= 4; \
128 off++; \
131 if (off > 2) \
132 nibbles = off - 2; \
134 /* Off - 1 is the number of encoded nibbles */ \
135 data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4); \
137 return ((off + 1) / 2); \
138 } while (0)
140 static inline int
141 encode_int_internal(ev_uint8_t *data, ev_uint32_t number)
143 ENCODE_INT_INTERNAL(data, number);
146 static inline int
147 encode_int64_internal(ev_uint8_t *data, ev_uint64_t number)
149 ENCODE_INT_INTERNAL(data, number);
152 void
153 evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number)
155 ev_uint8_t data[5];
156 int len = encode_int_internal(data, number);
157 evbuffer_add(evbuf, data, len);
160 void
161 evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number)
163 ev_uint8_t data[9];
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)
176 int bytes = 0;
177 ev_uint8_t data[5];
179 memset(data, 0, sizeof(data));
180 do {
181 ev_uint8_t lower = tag & 0x7f;
182 tag >>= 7;
184 if (tag)
185 lower |= 0x80;
187 data[bytes++] = lower;
188 } while (tag);
190 if (evbuf != NULL)
191 evbuffer_add(evbuf, data, bytes);
193 return (bytes);
196 static int
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);
201 ev_uint8_t *data;
202 size_t count = 0;
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);
211 if (!data)
212 return (-1);
214 while (count++ < len) {
215 ev_uint8_t lower = *data++;
216 if (shift >= 28) {
217 /* Make sure it fits into 32 bits */
218 if (shift > 28)
219 return (-1);
220 if ((lower & 0x7f) > 15)
221 return (-1);
223 number |= (lower & (unsigned)0x7f) << shift;
224 shift += 7;
226 if (!(lower & 0x80)) {
227 done = 1;
228 break;
232 if (!done)
233 return (-1);
235 if (dodrain)
236 evbuffer_drain(evbuf, count);
238 if (ptag != NULL)
239 *ptag = number;
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
256 void
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);
265 void
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 */
276 void
277 evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer)
279 ev_uint8_t data[5];
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);
287 void
288 evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
289 ev_uint64_t integer)
291 ev_uint8_t data[9];
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);
299 void
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));
306 void
307 evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, struct timeval *tv)
309 ev_uint8_t data[10];
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) \
316 do { \
317 ev_uint8_t *data; \
318 ev_ssize_t len = evbuffer_get_length(evbuf) - offset; \
319 int nibbles = 0; \
321 if (len <= 0) \
322 return (-1); \
324 /* XXX(niels): faster? */ \
325 data = evbuffer_pullup(evbuf, offset + 1) + offset; \
326 if (!data) \
327 return (-1); \
329 nibbles = ((data[0] & 0xf0) >> 4) + 1; \
330 if (nibbles > maxnibbles || (nibbles >> 1) + 1 > len) \
331 return (-1); \
332 len = (nibbles >> 1) + 1; \
334 data = evbuffer_pullup(evbuf, offset + len) + offset; \
335 if (!data) \
336 return (-1); \
338 while (nibbles > 0) { \
339 number <<= 4; \
340 if (nibbles & 0x1) \
341 number |= data[nibbles >> 1] & 0x0f; \
342 else \
343 number |= (data[nibbles >> 1] & 0xf0) >> 4; \
344 nibbles--; \
347 *pnumber = number; \
349 return (int)(len); \
350 } while (0)
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.
361 static int
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);
368 static int
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);
379 if (res != -1)
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);
389 if (res != -1)
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)
404 int res, len;
406 len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
407 if (len == -1)
408 return (-1);
410 res = decode_int_internal(plength, evbuf, len);
411 if (res == -1)
412 return (-1);
414 *plength += res + len;
416 return (0);
420 evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength)
422 int res, len;
424 len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
425 if (len == -1)
426 return (-1);
428 res = decode_int_internal(plength, evbuf, len);
429 if (res == -1)
430 return (-1);
432 return (0);
435 /* just unmarshals the header and returns the length of the remaining data */
438 evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag)
440 ev_uint32_t len;
442 if (decode_tag_internal(ptag, evbuf, 1 /* dodrain */) == -1)
443 return (-1);
444 if (evtag_decode_int(&len, evbuf) == -1)
445 return (-1);
447 if (evbuffer_get_length(evbuf) < len)
448 return (-1);
450 return (len);
454 evtag_consume(struct evbuffer *evbuf)
456 int len;
457 if ((len = evtag_unmarshal_header(evbuf, NULL)) == -1)
458 return (-1);
459 evbuffer_drain(evbuf, len);
461 return (0);
464 /* Reads the data type from an event buffer */
467 evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, struct evbuffer *dst)
469 int len;
471 if ((len = evtag_unmarshal_header(src, ptag)) == -1)
472 return (-1);
474 if (evbuffer_add(dst, evbuffer_pullup(src, len), len) == -1)
475 return (-1);
477 evbuffer_drain(src, len);
479 return (len);
482 /* Marshaling for integers */
485 evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
486 ev_uint32_t *pinteger)
488 ev_uint32_t tag;
489 ev_uint32_t len;
490 int result;
492 if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
493 return (-1);
494 if (need_tag != tag)
495 return (-1);
496 if (evtag_decode_int(&len, evbuf) == -1)
497 return (-1);
499 if (evbuffer_get_length(evbuf) < len)
500 return (-1);
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 > ?*/
505 return (-1);
506 else
507 return result;
511 evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
512 ev_uint64_t *pinteger)
514 ev_uint32_t tag;
515 ev_uint32_t len;
516 int result;
518 if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
519 return (-1);
520 if (need_tag != tag)
521 return (-1);
522 if (evtag_decode_int(&len, evbuf) == -1)
523 return (-1);
525 if (evbuffer_get_length(evbuf) < len)
526 return (-1);
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 > ?*/
531 return (-1);
532 else
533 return result;
536 /* Unmarshal a fixed length tag */
539 evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, void *data,
540 size_t len)
542 ev_uint32_t tag;
543 int tag_len;
545 /* Now unmarshal a tag and check that it matches the tag we want */
546 if ((tag_len = evtag_unmarshal_header(src, &tag)) < 0 ||
547 tag != need_tag)
548 return (-1);
550 if ((size_t)tag_len != len)
551 return (-1);
553 evbuffer_remove(src, data, len);
554 return (0);
558 evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
559 char **pstring)
561 ev_uint32_t tag;
562 int tag_len;
564 if ((tag_len = evtag_unmarshal_header(evbuf, &tag)) == -1 ||
565 tag != need_tag)
566 return (-1);
568 *pstring = mm_malloc(tag_len + 1);
569 if (*pstring == NULL) {
570 event_warn("%s: malloc", __func__);
571 return -1;
573 evbuffer_remove(evbuf, *pstring, tag_len);
574 (*pstring)[tag_len] = '\0';
576 return (0);
580 evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
581 struct timeval *ptv)
583 ev_uint32_t tag;
584 ev_uint32_t integer;
585 int len, offset, offset2;
586 int result = -1;
588 if ((len = evtag_unmarshal_header(evbuf, &tag)) == -1)
589 return (-1);
590 if (tag != need_tag)
591 goto done;
592 if ((offset = decode_int_internal(&integer, evbuf, 0)) == -1)
593 goto done;
594 ptv->tv_sec = integer;
595 if ((offset2 = decode_int_internal(&integer, evbuf, offset)) == -1)
596 goto done;
597 ptv->tv_usec = integer;
598 if (offset + offset2 > len) /* XXX Should this be != instead of > ? */
599 goto done;
601 result = 0;
602 done:
603 evbuffer_drain(evbuf, len);
604 return result;