1 /*
2 * Copyright (c) 2003-2009 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2009-2012 Niels Provos and Nick Mathewson
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 #include "event2/event-config.h"
29 #include "evconfig-private.h"
30
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
37
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
44
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
52
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>
64
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"
71
72 /*
73 Here's our wire format:
74
75 Stream = TaggedData*
76
77 TaggedData = Tag Length Data
78 where the integer value of 'Length' is the length of 'data'.
79
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]
86
87 Length = Integer
88
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.
94 */
95
96 EVENT2_EXPORT_SYMBOL
97 int evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf);
98 EVENT2_EXPORT_SYMBOL
99 int evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf);
100 EVENT2_EXPORT_SYMBOL
101 int evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag);
102 EVENT2_EXPORT_SYMBOL
103 int evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf);
104
105 void
evtag_init(void)106 evtag_init(void)
107 {
108 }
109
110 /*
111 * We encode integers by nibbles; the first nibble contains the number
112 * of significant nibbles - 1; this allows us to encode up to 64-bit
113 * integers. This function is byte-order independent.
114 *
115 * @param number a 32-bit unsigned integer to encode
116 * @param data a pointer to where the data should be written. Must
117 * have at least 5 bytes free.
118 * @return the number of bytes written into data.
119 */
120
121 #define ENCODE_INT_INTERNAL(data, number) do { \
122 int off = 1, nibbles = 0; \
123 \
124 memset(data, 0, sizeof(number)+1); \
125 while (number) { \
126 if (off & 0x1) \
127 data[off/2] = (data[off/2] & 0xf0) | (number & 0x0f); \
128 else \
129 data[off/2] = (data[off/2] & 0x0f) | \
130 ((number & 0x0f) << 4); \
131 number >>= 4; \
132 off++; \
133 } \
134 \
135 if (off > 2) \
136 nibbles = off - 2; \
137 \
138 /* Off - 1 is the number of encoded nibbles */ \
139 data[0] = (data[0] & 0x0f) | ((nibbles & 0x0f) << 4); \
140 \
141 return ((off + 1) / 2); \
142 } while (0)
143
144 static inline int
encode_int_internal(ev_uint8_t * data,ev_uint32_t number)145 encode_int_internal(ev_uint8_t *data, ev_uint32_t number)
146 {
147 ENCODE_INT_INTERNAL(data, number);
148 }
149
150 static inline int
encode_int64_internal(ev_uint8_t * data,ev_uint64_t number)151 encode_int64_internal(ev_uint8_t *data, ev_uint64_t number)
152 {
153 ENCODE_INT_INTERNAL(data, number);
154 }
155
156 void
evtag_encode_int(struct evbuffer * evbuf,ev_uint32_t number)157 evtag_encode_int(struct evbuffer *evbuf, ev_uint32_t number)
158 {
159 ev_uint8_t data[5];
160 int len = encode_int_internal(data, number);
161 evbuffer_add(evbuf, data, len);
162 }
163
164 void
evtag_encode_int64(struct evbuffer * evbuf,ev_uint64_t number)165 evtag_encode_int64(struct evbuffer *evbuf, ev_uint64_t number)
166 {
167 ev_uint8_t data[9];
168 int len = encode_int64_internal(data, number);
169 evbuffer_add(evbuf, data, len);
170 }
171
172 /*
173 * Support variable length encoding of tags; we use the high bit in each
174 * octet as a continuation signal.
175 */
176
177 int
evtag_encode_tag(struct evbuffer * evbuf,ev_uint32_t tag)178 evtag_encode_tag(struct evbuffer *evbuf, ev_uint32_t tag)
179 {
180 int bytes = 0;
181 ev_uint8_t data[5];
182
183 memset(data, 0, sizeof(data));
184 do {
185 ev_uint8_t lower = tag & 0x7f;
186 tag >>= 7;
187
188 if (tag)
189 lower |= 0x80;
190
191 data[bytes++] = lower;
192 } while (tag);
193
194 if (evbuf != NULL)
195 evbuffer_add(evbuf, data, bytes);
196
197 return (bytes);
198 }
199
200 static int
decode_tag_internal(ev_uint32_t * ptag,struct evbuffer * evbuf,int dodrain)201 decode_tag_internal(ev_uint32_t *ptag, struct evbuffer *evbuf, int dodrain)
202 {
203 ev_uint32_t number = 0;
204 size_t len = evbuffer_get_length(evbuf);
205 ev_uint8_t *data;
206 size_t count = 0;
207 int shift = 0, done = 0;
208
209 /*
210 * the encoding of a number is at most one byte more than its
211 * storage size. however, it may also be much smaller.
212 */
213 data = evbuffer_pullup(
214 evbuf, len < sizeof(number) + 1 ? len : sizeof(number) + 1);
215 if (!data)
216 return (-1);
217
218 while (count++ < len) {
219 ev_uint8_t lower = *data++;
220 if (shift >= 28) {
221 /* Make sure it fits into 32 bits */
222 if (shift > 28)
223 return (-1);
224 if ((lower & 0x7f) > 15)
225 return (-1);
226 }
227 number |= (lower & (unsigned)0x7f) << shift;
228 shift += 7;
229
230 if (!(lower & 0x80)) {
231 done = 1;
232 break;
233 }
234 }
235
236 if (!done)
237 return (-1);
238
239 if (dodrain)
240 evbuffer_drain(evbuf, count);
241
242 if (ptag != NULL)
243 *ptag = number;
244
245 return count > INT_MAX ? INT_MAX : (int)(count);
246 }
247
248 int
evtag_decode_tag(ev_uint32_t * ptag,struct evbuffer * evbuf)249 evtag_decode_tag(ev_uint32_t *ptag, struct evbuffer *evbuf)
250 {
251 return (decode_tag_internal(ptag, evbuf, 1 /* dodrain */));
252 }
253
254 /*
255 * Marshal a data type, the general format is as follows:
256 *
257 * tag number: one byte; length: var bytes; payload: var bytes
258 */
259
260 void
evtag_marshal(struct evbuffer * evbuf,ev_uint32_t tag,const void * data,ev_uint32_t len)261 evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag,
262 const void *data, ev_uint32_t len)
263 {
264 evtag_encode_tag(evbuf, tag);
265 evtag_encode_int(evbuf, len);
266 evbuffer_add(evbuf, (void *)data, len);
267 }
268
269 void
evtag_marshal_buffer(struct evbuffer * evbuf,ev_uint32_t tag,struct evbuffer * data)270 evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag,
271 struct evbuffer *data)
272 {
273 evtag_encode_tag(evbuf, tag);
274 /* XXX support more than UINT32_MAX data */
275 evtag_encode_int(evbuf, (ev_uint32_t)evbuffer_get_length(data));
276 evbuffer_add_buffer(evbuf, data);
277 }
278
279 /* Marshaling for integers */
280 void
evtag_marshal_int(struct evbuffer * evbuf,ev_uint32_t tag,ev_uint32_t integer)281 evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, ev_uint32_t integer)
282 {
283 ev_uint8_t data[5];
284 int len = encode_int_internal(data, integer);
285
286 evtag_encode_tag(evbuf, tag);
287 evtag_encode_int(evbuf, len);
288 evbuffer_add(evbuf, data, len);
289 }
290
291 void
evtag_marshal_int64(struct evbuffer * evbuf,ev_uint32_t tag,ev_uint64_t integer)292 evtag_marshal_int64(struct evbuffer *evbuf, ev_uint32_t tag,
293 ev_uint64_t integer)
294 {
295 ev_uint8_t data[9];
296 int len = encode_int64_internal(data, integer);
297
298 evtag_encode_tag(evbuf, tag);
299 evtag_encode_int(evbuf, len);
300 evbuffer_add(evbuf, data, len);
301 }
302
303 void
evtag_marshal_string(struct evbuffer * buf,ev_uint32_t tag,const char * string)304 evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, const char *string)
305 {
306 /* TODO support strings longer than UINT32_MAX ? */
307 evtag_marshal(buf, tag, string, (ev_uint32_t)strlen(string));
308 }
309
310 void
evtag_marshal_timeval(struct evbuffer * evbuf,ev_uint32_t tag,struct timeval * tv)311 evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, struct timeval *tv)
312 {
313 ev_uint8_t data[10];
314 int len = encode_int_internal(data, tv->tv_sec);
315 len += encode_int_internal(data + len, tv->tv_usec);
316 evtag_marshal(evbuf, tag, data, len);
317 }
318
319 #define DECODE_INT_INTERNAL(number, maxnibbles, pnumber, evbuf, offset) \
320 do { \
321 ev_uint8_t *data; \
322 ev_ssize_t len = evbuffer_get_length(evbuf) - offset; \
323 int nibbles = 0; \
324 \
325 if (len <= 0) \
326 return (-1); \
327 \
328 /* XXX(niels): faster? */ \
329 data = evbuffer_pullup(evbuf, offset + 1) + offset; \
330 if (!data) \
331 return (-1); \
332 \
333 nibbles = ((data[0] & 0xf0) >> 4) + 1; \
334 if (nibbles > maxnibbles || (nibbles >> 1) + 1 > len) \
335 return (-1); \
336 len = (nibbles >> 1) + 1; \
337 \
338 data = evbuffer_pullup(evbuf, offset + len) + offset; \
339 if (!data) \
340 return (-1); \
341 \
342 while (nibbles > 0) { \
343 number <<= 4; \
344 if (nibbles & 0x1) \
345 number |= data[nibbles >> 1] & 0x0f; \
346 else \
347 number |= (data[nibbles >> 1] & 0xf0) >> 4; \
348 nibbles--; \
349 } \
350 \
351 *pnumber = number; \
352 \
353 return (int)(len); \
354 } while (0)
355
356 /* Internal: decode an integer from an evbuffer, without draining it.
357 * Only integers up to 32-bits are supported.
358 *
359 * @param evbuf the buffer to read from
360 * @param offset an index into the buffer at which we should start reading.
361 * @param pnumber a pointer to receive the integer.
362 * @return The length of the number as encoded, or -1 on error.
363 */
364
365 static int
decode_int_internal(ev_uint32_t * pnumber,struct evbuffer * evbuf,int offset)366 decode_int_internal(ev_uint32_t *pnumber, struct evbuffer *evbuf, int offset)
367 {
368 ev_uint32_t number = 0;
369 DECODE_INT_INTERNAL(number, 8, pnumber, evbuf, offset);
370 }
371
372 static int
decode_int64_internal(ev_uint64_t * pnumber,struct evbuffer * evbuf,int offset)373 decode_int64_internal(ev_uint64_t *pnumber, struct evbuffer *evbuf, int offset)
374 {
375 ev_uint64_t number = 0;
376 DECODE_INT_INTERNAL(number, 16, pnumber, evbuf, offset);
377 }
378
379 int
evtag_decode_int(ev_uint32_t * pnumber,struct evbuffer * evbuf)380 evtag_decode_int(ev_uint32_t *pnumber, struct evbuffer *evbuf)
381 {
382 int res = decode_int_internal(pnumber, evbuf, 0);
383 if (res != -1)
384 evbuffer_drain(evbuf, res);
385
386 return (res == -1 ? -1 : 0);
387 }
388
389 int
evtag_decode_int64(ev_uint64_t * pnumber,struct evbuffer * evbuf)390 evtag_decode_int64(ev_uint64_t *pnumber, struct evbuffer *evbuf)
391 {
392 int res = decode_int64_internal(pnumber, evbuf, 0);
393 if (res != -1)
394 evbuffer_drain(evbuf, res);
395
396 return (res == -1 ? -1 : 0);
397 }
398
399 int
evtag_peek(struct evbuffer * evbuf,ev_uint32_t * ptag)400 evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag)
401 {
402 return (decode_tag_internal(ptag, evbuf, 0 /* dodrain */));
403 }
404
405 int
evtag_peek_length(struct evbuffer * evbuf,ev_uint32_t * plength)406 evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength)
407 {
408 int res, len;
409
410 len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
411 if (len == -1)
412 return (-1);
413
414 res = decode_int_internal(plength, evbuf, len);
415 if (res == -1)
416 return (-1);
417
418 *plength += res + len;
419
420 return (0);
421 }
422
423 int
evtag_payload_length(struct evbuffer * evbuf,ev_uint32_t * plength)424 evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength)
425 {
426 int res, len;
427
428 len = decode_tag_internal(NULL, evbuf, 0 /* dodrain */);
429 if (len == -1)
430 return (-1);
431
432 res = decode_int_internal(plength, evbuf, len);
433 if (res == -1)
434 return (-1);
435
436 return (0);
437 }
438
439 /* just unmarshals the header and returns the length of the remaining data */
440
441 int
evtag_unmarshal_header(struct evbuffer * evbuf,ev_uint32_t * ptag)442 evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag)
443 {
444 ev_uint32_t len;
445
446 if (decode_tag_internal(ptag, evbuf, 1 /* dodrain */) == -1)
447 return (-1);
448 if (evtag_decode_int(&len, evbuf) == -1)
449 return (-1);
450
451 if (evbuffer_get_length(evbuf) < len)
452 return (-1);
453
454 return (len);
455 }
456
457 int
evtag_consume(struct evbuffer * evbuf)458 evtag_consume(struct evbuffer *evbuf)
459 {
460 int len;
461 if ((len = evtag_unmarshal_header(evbuf, NULL)) == -1)
462 return (-1);
463 evbuffer_drain(evbuf, len);
464
465 return (0);
466 }
467
468 /* Reads the data type from an event buffer */
469
470 int
evtag_unmarshal(struct evbuffer * src,ev_uint32_t * ptag,struct evbuffer * dst)471 evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, struct evbuffer *dst)
472 {
473 int len;
474
475 if ((len = evtag_unmarshal_header(src, ptag)) == -1)
476 return (-1);
477
478 if (evbuffer_add(dst, evbuffer_pullup(src, len), len) == -1)
479 return (-1);
480
481 evbuffer_drain(src, len);
482
483 return (len);
484 }
485
486 /* Marshaling for integers */
487
488 int
evtag_unmarshal_int(struct evbuffer * evbuf,ev_uint32_t need_tag,ev_uint32_t * pinteger)489 evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
490 ev_uint32_t *pinteger)
491 {
492 ev_uint32_t tag;
493 ev_uint32_t len;
494 int result;
495
496 if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
497 return (-1);
498 if (need_tag != tag)
499 return (-1);
500 if (evtag_decode_int(&len, evbuf) == -1)
501 return (-1);
502
503 if (evbuffer_get_length(evbuf) < len)
504 return (-1);
505
506 result = decode_int_internal(pinteger, evbuf, 0);
507 evbuffer_drain(evbuf, len);
508 if (result < 0 || (size_t)result > len) /* XXX Should this be != rather than > ?*/
509 return (-1);
510 else
511 return result;
512 }
513
514 int
evtag_unmarshal_int64(struct evbuffer * evbuf,ev_uint32_t need_tag,ev_uint64_t * pinteger)515 evtag_unmarshal_int64(struct evbuffer *evbuf, ev_uint32_t need_tag,
516 ev_uint64_t *pinteger)
517 {
518 ev_uint32_t tag;
519 ev_uint32_t len;
520 int result;
521
522 if (decode_tag_internal(&tag, evbuf, 1 /* dodrain */) == -1)
523 return (-1);
524 if (need_tag != tag)
525 return (-1);
526 if (evtag_decode_int(&len, evbuf) == -1)
527 return (-1);
528
529 if (evbuffer_get_length(evbuf) < len)
530 return (-1);
531
532 result = decode_int64_internal(pinteger, evbuf, 0);
533 evbuffer_drain(evbuf, len);
534 if (result < 0 || (size_t)result > len) /* XXX Should this be != rather than > ?*/
535 return (-1);
536 else
537 return result;
538 }
539
540 /* Unmarshal a fixed length tag */
541
542 int
evtag_unmarshal_fixed(struct evbuffer * src,ev_uint32_t need_tag,void * data,size_t len)543 evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, void *data,
544 size_t len)
545 {
546 ev_uint32_t tag;
547 int tag_len;
548
549 /* Now unmarshal a tag and check that it matches the tag we want */
550 if ((tag_len = evtag_unmarshal_header(src, &tag)) < 0 ||
551 tag != need_tag)
552 return (-1);
553
554 if ((size_t)tag_len != len)
555 return (-1);
556
557 evbuffer_remove(src, data, len);
558 return (0);
559 }
560
561 int
evtag_unmarshal_string(struct evbuffer * evbuf,ev_uint32_t need_tag,char ** pstring)562 evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
563 char **pstring)
564 {
565 ev_uint32_t tag;
566 int tag_len;
567
568 if ((tag_len = evtag_unmarshal_header(evbuf, &tag)) == -1 ||
569 tag != need_tag)
570 return (-1);
571
572 *pstring = mm_malloc(tag_len + 1);
573 if (*pstring == NULL) {
574 event_warn("%s: malloc", __func__);
575 return -1;
576 }
577 evbuffer_remove(evbuf, *pstring, tag_len);
578 (*pstring)[tag_len] = '\0';
579
580 return (0);
581 }
582
583 int
evtag_unmarshal_timeval(struct evbuffer * evbuf,ev_uint32_t need_tag,struct timeval * ptv)584 evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
585 struct timeval *ptv)
586 {
587 ev_uint32_t tag;
588 ev_uint32_t integer;
589 int len, offset, offset2;
590 int result = -1;
591
592 if ((len = evtag_unmarshal_header(evbuf, &tag)) == -1)
593 return (-1);
594 if (tag != need_tag)
595 goto done;
596 if ((offset = decode_int_internal(&integer, evbuf, 0)) == -1)
597 goto done;
598 ptv->tv_sec = integer;
599 if ((offset2 = decode_int_internal(&integer, evbuf, offset)) == -1)
600 goto done;
601 ptv->tv_usec = integer;
602 if (offset + offset2 > len) /* XXX Should this be != instead of > ? */
603 goto done;
604
605 result = 0;
606 done:
607 evbuffer_drain(evbuf, len);
608 return result;
609 }
610