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