1 /* 2 * Copyright (c) 2002-2007 Niels Provos <[email protected]> 3 * Copyright (c) 2007-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 _WIN32 32 #include <winsock2.h> 33 #include <windows.h> 34 #include <io.h> 35 #endif 36 37 #ifdef EVENT__HAVE_VASPRINTF 38 /* If we have vasprintf, we need to define _GNU_SOURCE before we include 39 * stdio.h. This comes from evconfig-private.h. 40 */ 41 #endif 42 43 #include <sys/types.h> 44 45 #ifdef EVENT__HAVE_SYS_TIME_H 46 #include <sys/time.h> 47 #endif 48 49 #ifdef EVENT__HAVE_SYS_SOCKET_H 50 #include <sys/socket.h> 51 #endif 52 53 #ifdef EVENT__HAVE_SYS_UIO_H 54 #include <sys/uio.h> 55 #endif 56 57 #ifdef EVENT__HAVE_SYS_IOCTL_H 58 #include <sys/ioctl.h> 59 #endif 60 61 #ifdef EVENT__HAVE_SYS_MMAN_H 62 #include <sys/mman.h> 63 #endif 64 65 #ifdef EVENT__HAVE_SYS_SENDFILE_H 66 #include <sys/sendfile.h> 67 #endif 68 #ifdef EVENT__HAVE_SYS_STAT_H 69 #include <sys/stat.h> 70 #endif 71 72 73 #include <errno.h> 74 #include <stdio.h> 75 #include <stdlib.h> 76 #include <string.h> 77 #ifdef EVENT__HAVE_STDARG_H 78 #include <stdarg.h> 79 #endif 80 #ifdef EVENT__HAVE_UNISTD_H 81 #include <unistd.h> 82 #endif 83 #include <limits.h> 84 85 #include "event2/event.h" 86 #include "event2/buffer.h" 87 #include "event2/buffer_compat.h" 88 #include "event2/bufferevent.h" 89 #include "event2/bufferevent_compat.h" 90 #include "event2/bufferevent_struct.h" 91 #include "event2/thread.h" 92 #include "log-internal.h" 93 #include "mm-internal.h" 94 #include "util-internal.h" 95 #include "evthread-internal.h" 96 #include "evbuffer-internal.h" 97 #include "bufferevent-internal.h" 98 #include "event-internal.h" 99 100 /* some systems do not have MAP_FAILED */ 101 #ifndef MAP_FAILED 102 #define MAP_FAILED ((void *)-1) 103 #endif 104 105 /* send file support */ 106 #if defined(EVENT__HAVE_SYS_SENDFILE_H) && defined(EVENT__HAVE_SENDFILE) && defined(__linux__) 107 #define USE_SENDFILE 1 108 #define SENDFILE_IS_LINUX 1 109 #elif defined(EVENT__HAVE_SENDFILE) && defined(__FreeBSD__) 110 #define USE_SENDFILE 1 111 #define SENDFILE_IS_FREEBSD 1 112 #elif defined(EVENT__HAVE_SENDFILE) && defined(__APPLE__) 113 #define USE_SENDFILE 1 114 #define SENDFILE_IS_MACOSX 1 115 #elif defined(EVENT__HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__) 116 #define USE_SENDFILE 1 117 #define SENDFILE_IS_SOLARIS 1 118 #endif 119 120 /* Mask of user-selectable callback flags. */ 121 #define EVBUFFER_CB_USER_FLAGS 0xffff 122 /* Mask of all internal-use-only flags. */ 123 #define EVBUFFER_CB_INTERNAL_FLAGS 0xffff0000 124 125 /* Flag set if the callback is using the cb_obsolete function pointer */ 126 #define EVBUFFER_CB_OBSOLETE 0x00040000 127 128 /* evbuffer_chain support */ 129 #define CHAIN_SPACE_PTR(ch) ((ch)->buffer + (ch)->misalign + (ch)->off) 130 #define CHAIN_SPACE_LEN(ch) ((ch)->flags & EVBUFFER_IMMUTABLE ? \ 131 0 : (ch)->buffer_len - ((ch)->misalign + (ch)->off)) 132 133 #define CHAIN_PINNED(ch) (((ch)->flags & EVBUFFER_MEM_PINNED_ANY) != 0) 134 #define CHAIN_PINNED_R(ch) (((ch)->flags & EVBUFFER_MEM_PINNED_R) != 0) 135 136 /* evbuffer_ptr support */ 137 #define PTR_NOT_FOUND(ptr) do { \ 138 (ptr)->pos = -1; \ 139 (ptr)->internal_.chain = NULL; \ 140 (ptr)->internal_.pos_in_chain = 0; \ 141 } while (0) 142 143 static void evbuffer_chain_align(struct evbuffer_chain *chain); 144 static int evbuffer_chain_should_realign(struct evbuffer_chain *chain, 145 size_t datalen); 146 static void evbuffer_deferred_callback(struct event_callback *cb, void *arg); 147 static int evbuffer_ptr_memcmp(const struct evbuffer *buf, 148 const struct evbuffer_ptr *pos, const char *mem, size_t len); 149 static struct evbuffer_chain *evbuffer_expand_singlechain(struct evbuffer *buf, 150 size_t datlen); 151 static int evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos, 152 size_t howfar); 153 static int evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg); 154 static inline void evbuffer_chain_incref(struct evbuffer_chain *chain); 155 156 static struct evbuffer_chain * 157 evbuffer_chain_new(size_t size) 158 { 159 struct evbuffer_chain *chain; 160 size_t to_alloc; 161 162 if (size > EVBUFFER_CHAIN_MAX - EVBUFFER_CHAIN_SIZE) 163 return (NULL); 164 165 size += EVBUFFER_CHAIN_SIZE; 166 167 /* get the next largest memory that can hold the buffer */ 168 if (size < EVBUFFER_CHAIN_MAX / 2) { 169 to_alloc = MIN_BUFFER_SIZE; 170 while (to_alloc < size) { 171 to_alloc <<= 1; 172 } 173 } else { 174 to_alloc = size; 175 } 176 177 /* we get everything in one chunk */ 178 if ((chain = mm_malloc(to_alloc)) == NULL) 179 return (NULL); 180 181 memset(chain, 0, EVBUFFER_CHAIN_SIZE); 182 183 chain->buffer_len = to_alloc - EVBUFFER_CHAIN_SIZE; 184 185 /* this way we can manipulate the buffer to different addresses, 186 * which is required for mmap for example. 187 */ 188 chain->buffer = EVBUFFER_CHAIN_EXTRA(unsigned char, chain); 189 190 chain->refcnt = 1; 191 192 return (chain); 193 } 194 195 static inline void 196 evbuffer_chain_free(struct evbuffer_chain *chain) 197 { 198 EVUTIL_ASSERT(chain->refcnt > 0); 199 if (--chain->refcnt > 0) { 200 /* chain is still referenced by other chains */ 201 return; 202 } 203 204 if (CHAIN_PINNED(chain)) { 205 /* will get freed once no longer dangling */ 206 chain->refcnt++; 207 chain->flags |= EVBUFFER_DANGLING; 208 return; 209 } 210 211 /* safe to release chain, it's either a referencing 212 * chain or all references to it have been freed */ 213 if (chain->flags & EVBUFFER_REFERENCE) { 214 struct evbuffer_chain_reference *info = 215 EVBUFFER_CHAIN_EXTRA( 216 struct evbuffer_chain_reference, 217 chain); 218 if (info->cleanupfn) 219 (*info->cleanupfn)(chain->buffer, 220 chain->buffer_len, 221 info->extra); 222 } 223 if (chain->flags & EVBUFFER_FILESEGMENT) { 224 struct evbuffer_chain_file_segment *info = 225 EVBUFFER_CHAIN_EXTRA( 226 struct evbuffer_chain_file_segment, 227 chain); 228 if (info->segment) { 229 #ifdef _WIN32 230 if (info->segment->is_mapping) 231 UnmapViewOfFile(chain->buffer); 232 #endif 233 evbuffer_file_segment_free(info->segment); 234 } 235 } 236 if (chain->flags & EVBUFFER_MULTICAST) { 237 struct evbuffer_multicast_parent *info = 238 EVBUFFER_CHAIN_EXTRA( 239 struct evbuffer_multicast_parent, 240 chain); 241 /* referencing chain is being freed, decrease 242 * refcounts of source chain and associated 243 * evbuffer (which get freed once both reach 244 * zero) */ 245 EVUTIL_ASSERT(info->source != NULL); 246 EVUTIL_ASSERT(info->parent != NULL); 247 EVBUFFER_LOCK(info->source); 248 evbuffer_chain_free(info->parent); 249 evbuffer_decref_and_unlock_(info->source); 250 } 251 252 mm_free(chain); 253 } 254 255 static void 256 evbuffer_free_all_chains(struct evbuffer_chain *chain) 257 { 258 struct evbuffer_chain *next; 259 for (; chain; chain = next) { 260 next = chain->next; 261 evbuffer_chain_free(chain); 262 } 263 } 264 265 #ifndef NDEBUG 266 static int 267 evbuffer_chains_all_empty(struct evbuffer_chain *chain) 268 { 269 for (; chain; chain = chain->next) { 270 if (chain->off) 271 return 0; 272 } 273 return 1; 274 } 275 #else 276 /* The definition is needed for EVUTIL_ASSERT, which uses sizeof to avoid 277 "unused variable" warnings. */ 278 static inline int evbuffer_chains_all_empty(struct evbuffer_chain *chain) { 279 return 1; 280 } 281 #endif 282 283 /* Free all trailing chains in 'buf' that are neither pinned nor empty, prior 284 * to replacing them all with a new chain. Return a pointer to the place 285 * where the new chain will go. 286 * 287 * Internal; requires lock. The caller must fix up buf->last and buf->first 288 * as needed; they might have been freed. 289 */ 290 static struct evbuffer_chain ** 291 evbuffer_free_trailing_empty_chains(struct evbuffer *buf) 292 { 293 struct evbuffer_chain **ch = buf->last_with_datap; 294 /* Find the first victim chain. It might be *last_with_datap */ 295 while ((*ch) && ((*ch)->off != 0 || CHAIN_PINNED(*ch))) 296 ch = &(*ch)->next; 297 if (*ch) { 298 EVUTIL_ASSERT(evbuffer_chains_all_empty(*ch)); 299 evbuffer_free_all_chains(*ch); 300 *ch = NULL; 301 } 302 return ch; 303 } 304 305 /* Add a single chain 'chain' to the end of 'buf', freeing trailing empty 306 * chains as necessary. Requires lock. Does not schedule callbacks. 307 */ 308 static void 309 evbuffer_chain_insert(struct evbuffer *buf, 310 struct evbuffer_chain *chain) 311 { 312 ASSERT_EVBUFFER_LOCKED(buf); 313 if (*buf->last_with_datap == NULL) { 314 /* There are no chains data on the buffer at all. */ 315 EVUTIL_ASSERT(buf->last_with_datap == &buf->first); 316 EVUTIL_ASSERT(buf->first == NULL); 317 buf->first = buf->last = chain; 318 } else { 319 struct evbuffer_chain **chp; 320 chp = evbuffer_free_trailing_empty_chains(buf); 321 *chp = chain; 322 if (chain->off) 323 buf->last_with_datap = chp; 324 buf->last = chain; 325 } 326 buf->total_len += chain->off; 327 } 328 329 static inline struct evbuffer_chain * 330 evbuffer_chain_insert_new(struct evbuffer *buf, size_t datlen) 331 { 332 struct evbuffer_chain *chain; 333 if ((chain = evbuffer_chain_new(datlen)) == NULL) 334 return NULL; 335 evbuffer_chain_insert(buf, chain); 336 return chain; 337 } 338 339 void 340 evbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag) 341 { 342 EVUTIL_ASSERT((chain->flags & flag) == 0); 343 chain->flags |= flag; 344 } 345 346 void 347 evbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag) 348 { 349 EVUTIL_ASSERT((chain->flags & flag) != 0); 350 chain->flags &= ~flag; 351 if (chain->flags & EVBUFFER_DANGLING) 352 evbuffer_chain_free(chain); 353 } 354 355 static inline void 356 evbuffer_chain_incref(struct evbuffer_chain *chain) 357 { 358 ++chain->refcnt; 359 } 360 361 struct evbuffer * 362 evbuffer_new(void) 363 { 364 struct evbuffer *buffer; 365 366 buffer = mm_calloc(1, sizeof(struct evbuffer)); 367 if (buffer == NULL) 368 return (NULL); 369 370 LIST_INIT(&buffer->callbacks); 371 buffer->refcnt = 1; 372 buffer->last_with_datap = &buffer->first; 373 374 return (buffer); 375 } 376 377 int 378 evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags) 379 { 380 EVBUFFER_LOCK(buf); 381 buf->flags |= (ev_uint32_t)flags; 382 EVBUFFER_UNLOCK(buf); 383 return 0; 384 } 385 386 int 387 evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags) 388 { 389 EVBUFFER_LOCK(buf); 390 buf->flags &= ~(ev_uint32_t)flags; 391 EVBUFFER_UNLOCK(buf); 392 return 0; 393 } 394 395 void 396 evbuffer_incref_(struct evbuffer *buf) 397 { 398 EVBUFFER_LOCK(buf); 399 ++buf->refcnt; 400 EVBUFFER_UNLOCK(buf); 401 } 402 403 void 404 evbuffer_incref_and_lock_(struct evbuffer *buf) 405 { 406 EVBUFFER_LOCK(buf); 407 ++buf->refcnt; 408 } 409 410 int 411 evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base) 412 { 413 EVBUFFER_LOCK(buffer); 414 buffer->cb_queue = base; 415 buffer->deferred_cbs = 1; 416 event_deferred_cb_init_(&buffer->deferred, 417 event_base_get_npriorities(base) / 2, 418 evbuffer_deferred_callback, buffer); 419 EVBUFFER_UNLOCK(buffer); 420 return 0; 421 } 422 423 int 424 evbuffer_enable_locking(struct evbuffer *buf, void *lock) 425 { 426 #ifdef EVENT__DISABLE_THREAD_SUPPORT 427 return -1; 428 #else 429 if (buf->lock) 430 return -1; 431 432 if (!lock) { 433 EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE); 434 if (!lock) 435 return -1; 436 buf->lock = lock; 437 buf->own_lock = 1; 438 } else { 439 buf->lock = lock; 440 buf->own_lock = 0; 441 } 442 443 return 0; 444 #endif 445 } 446 447 void 448 evbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev) 449 { 450 EVBUFFER_LOCK(buf); 451 buf->parent = bev; 452 EVBUFFER_UNLOCK(buf); 453 } 454 455 static void 456 evbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred) 457 { 458 struct evbuffer_cb_entry *cbent, *next; 459 struct evbuffer_cb_info info; 460 size_t new_size; 461 ev_uint32_t mask, masked_val; 462 int clear = 1; 463 464 if (running_deferred) { 465 mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED; 466 masked_val = EVBUFFER_CB_ENABLED; 467 } else if (buffer->deferred_cbs) { 468 mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED; 469 masked_val = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED; 470 /* Don't zero-out n_add/n_del, since the deferred callbacks 471 will want to see them. */ 472 clear = 0; 473 } else { 474 mask = EVBUFFER_CB_ENABLED; 475 masked_val = EVBUFFER_CB_ENABLED; 476 } 477 478 ASSERT_EVBUFFER_LOCKED(buffer); 479 480 if (LIST_EMPTY(&buffer->callbacks)) { 481 buffer->n_add_for_cb = buffer->n_del_for_cb = 0; 482 return; 483 } 484 if (buffer->n_add_for_cb == 0 && buffer->n_del_for_cb == 0) 485 return; 486 487 new_size = buffer->total_len; 488 info.orig_size = new_size + buffer->n_del_for_cb - buffer->n_add_for_cb; 489 info.n_added = buffer->n_add_for_cb; 490 info.n_deleted = buffer->n_del_for_cb; 491 if (clear) { 492 buffer->n_add_for_cb = 0; 493 buffer->n_del_for_cb = 0; 494 } 495 for (cbent = LIST_FIRST(&buffer->callbacks); 496 cbent != LIST_END(&buffer->callbacks); 497 cbent = next) { 498 /* Get the 'next' pointer now in case this callback decides 499 * to remove itself or something. */ 500 next = LIST_NEXT(cbent, next); 501 502 if ((cbent->flags & mask) != masked_val) 503 continue; 504 505 if ((cbent->flags & EVBUFFER_CB_OBSOLETE)) 506 cbent->cb.cb_obsolete(buffer, 507 info.orig_size, new_size, cbent->cbarg); 508 else 509 cbent->cb.cb_func(buffer, &info, cbent->cbarg); 510 } 511 } 512 513 void 514 evbuffer_invoke_callbacks_(struct evbuffer *buffer) 515 { 516 if (LIST_EMPTY(&buffer->callbacks)) { 517 buffer->n_add_for_cb = buffer->n_del_for_cb = 0; 518 return; 519 } 520 521 if (buffer->deferred_cbs) { 522 if (event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred)) { 523 evbuffer_incref_and_lock_(buffer); 524 if (buffer->parent) 525 bufferevent_incref_(buffer->parent); 526 EVBUFFER_UNLOCK(buffer); 527 } 528 } 529 530 evbuffer_run_callbacks(buffer, 0); 531 } 532 533 static void 534 evbuffer_deferred_callback(struct event_callback *cb, void *arg) 535 { 536 struct bufferevent *parent = NULL; 537 struct evbuffer *buffer = arg; 538 539 /* XXXX It would be better to run these callbacks without holding the 540 * lock */ 541 EVBUFFER_LOCK(buffer); 542 parent = buffer->parent; 543 evbuffer_run_callbacks(buffer, 1); 544 evbuffer_decref_and_unlock_(buffer); 545 if (parent) 546 bufferevent_decref_(parent); 547 } 548 549 static void 550 evbuffer_remove_all_callbacks(struct evbuffer *buffer) 551 { 552 struct evbuffer_cb_entry *cbent; 553 554 while ((cbent = LIST_FIRST(&buffer->callbacks))) { 555 LIST_REMOVE(cbent, next); 556 mm_free(cbent); 557 } 558 } 559 560 void 561 evbuffer_decref_and_unlock_(struct evbuffer *buffer) 562 { 563 struct evbuffer_chain *chain, *next; 564 ASSERT_EVBUFFER_LOCKED(buffer); 565 566 EVUTIL_ASSERT(buffer->refcnt > 0); 567 568 if (--buffer->refcnt > 0) { 569 EVBUFFER_UNLOCK(buffer); 570 return; 571 } 572 573 for (chain = buffer->first; chain != NULL; chain = next) { 574 next = chain->next; 575 evbuffer_chain_free(chain); 576 } 577 evbuffer_remove_all_callbacks(buffer); 578 if (buffer->deferred_cbs) 579 event_deferred_cb_cancel_(buffer->cb_queue, &buffer->deferred); 580 581 EVBUFFER_UNLOCK(buffer); 582 if (buffer->own_lock) 583 EVTHREAD_FREE_LOCK(buffer->lock, EVTHREAD_LOCKTYPE_RECURSIVE); 584 mm_free(buffer); 585 } 586 587 void 588 evbuffer_free(struct evbuffer *buffer) 589 { 590 EVBUFFER_LOCK(buffer); 591 evbuffer_decref_and_unlock_(buffer); 592 } 593 594 void 595 evbuffer_lock(struct evbuffer *buf) 596 { 597 EVBUFFER_LOCK(buf); 598 } 599 600 void 601 evbuffer_unlock(struct evbuffer *buf) 602 { 603 EVBUFFER_UNLOCK(buf); 604 } 605 606 size_t 607 evbuffer_get_length(const struct evbuffer *buffer) 608 { 609 size_t result; 610 611 EVBUFFER_LOCK(buffer); 612 613 result = (buffer->total_len); 614 615 EVBUFFER_UNLOCK(buffer); 616 617 return result; 618 } 619 620 size_t 621 evbuffer_get_contiguous_space(const struct evbuffer *buf) 622 { 623 struct evbuffer_chain *chain; 624 size_t result; 625 626 EVBUFFER_LOCK(buf); 627 chain = buf->first; 628 result = (chain != NULL ? chain->off : 0); 629 EVBUFFER_UNLOCK(buf); 630 631 return result; 632 } 633 634 size_t 635 evbuffer_add_iovec(struct evbuffer * buf, struct evbuffer_iovec * vec, int n_vec) { 636 int n; 637 size_t res; 638 size_t to_alloc; 639 640 EVBUFFER_LOCK(buf); 641 642 res = to_alloc = 0; 643 644 for (n = 0; n < n_vec; n++) { 645 to_alloc += vec[n].iov_len; 646 } 647 648 if (evbuffer_expand_fast_(buf, to_alloc, 2) < 0) { 649 goto done; 650 } 651 652 for (n = 0; n < n_vec; n++) { 653 /* XXX each 'add' call here does a bunch of setup that's 654 * obviated by evbuffer_expand_fast_, and some cleanup that we 655 * would like to do only once. Instead we should just extract 656 * the part of the code that's needed. */ 657 658 if (evbuffer_add(buf, vec[n].iov_base, vec[n].iov_len) < 0) { 659 goto done; 660 } 661 662 res += vec[n].iov_len; 663 } 664 665 done: 666 EVBUFFER_UNLOCK(buf); 667 return res; 668 } 669 670 int 671 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size, 672 struct evbuffer_iovec *vec, int n_vecs) 673 { 674 struct evbuffer_chain *chain, **chainp; 675 int n = -1; 676 677 EVBUFFER_LOCK(buf); 678 if (buf->freeze_end) 679 goto done; 680 if (n_vecs < 1) 681 goto done; 682 if (n_vecs == 1) { 683 if ((chain = evbuffer_expand_singlechain(buf, size)) == NULL) 684 goto done; 685 686 vec[0].iov_base = (void *)CHAIN_SPACE_PTR(chain); 687 vec[0].iov_len = (size_t)CHAIN_SPACE_LEN(chain); 688 EVUTIL_ASSERT(size<0 || (size_t)vec[0].iov_len >= (size_t)size); 689 n = 1; 690 } else { 691 if (evbuffer_expand_fast_(buf, size, n_vecs)<0) 692 goto done; 693 n = evbuffer_read_setup_vecs_(buf, size, vec, n_vecs, 694 &chainp, 0); 695 } 696 697 done: 698 EVBUFFER_UNLOCK(buf); 699 return n; 700 701 } 702 703 static int 704 advance_last_with_data(struct evbuffer *buf) 705 { 706 int n = 0; 707 struct evbuffer_chain **chainp = buf->last_with_datap; 708 709 ASSERT_EVBUFFER_LOCKED(buf); 710 711 if (!*chainp) 712 return 0; 713 714 while ((*chainp)->next) { 715 chainp = &(*chainp)->next; 716 if ((*chainp)->off) 717 buf->last_with_datap = chainp; 718 ++n; 719 } 720 return n; 721 } 722 723 int 724 evbuffer_commit_space(struct evbuffer *buf, 725 struct evbuffer_iovec *vec, int n_vecs) 726 { 727 struct evbuffer_chain *chain, **firstchainp, **chainp; 728 int result = -1; 729 size_t added = 0; 730 int i; 731 732 EVBUFFER_LOCK(buf); 733 734 if (buf->freeze_end) 735 goto done; 736 if (n_vecs == 0) { 737 result = 0; 738 goto done; 739 } else if (n_vecs == 1 && 740 (buf->last && vec[0].iov_base == (void *)CHAIN_SPACE_PTR(buf->last))) { 741 /* The user only got or used one chain; it might not 742 * be the first one with space in it. */ 743 if ((size_t)vec[0].iov_len > (size_t)CHAIN_SPACE_LEN(buf->last)) 744 goto done; 745 buf->last->off += vec[0].iov_len; 746 added = vec[0].iov_len; 747 if (added) 748 advance_last_with_data(buf); 749 goto okay; 750 } 751 752 /* Advance 'firstchain' to the first chain with space in it. */ 753 firstchainp = buf->last_with_datap; 754 if (!*firstchainp) 755 goto done; 756 if (CHAIN_SPACE_LEN(*firstchainp) == 0) { 757 firstchainp = &(*firstchainp)->next; 758 } 759 760 chain = *firstchainp; 761 /* pass 1: make sure that the pointers and lengths of vecs[] are in 762 * bounds before we try to commit anything. */ 763 for (i=0; i<n_vecs; ++i) { 764 if (!chain) 765 goto done; 766 if (vec[i].iov_base != (void *)CHAIN_SPACE_PTR(chain) || 767 (size_t)vec[i].iov_len > CHAIN_SPACE_LEN(chain)) 768 goto done; 769 chain = chain->next; 770 } 771 /* pass 2: actually adjust all the chains. */ 772 chainp = firstchainp; 773 for (i=0; i<n_vecs; ++i) { 774 (*chainp)->off += vec[i].iov_len; 775 added += vec[i].iov_len; 776 if (vec[i].iov_len) { 777 buf->last_with_datap = chainp; 778 } 779 chainp = &(*chainp)->next; 780 } 781 782 okay: 783 buf->total_len += added; 784 buf->n_add_for_cb += added; 785 result = 0; 786 evbuffer_invoke_callbacks_(buf); 787 788 done: 789 EVBUFFER_UNLOCK(buf); 790 return result; 791 } 792 793 static inline int 794 HAS_PINNED_R(struct evbuffer *buf) 795 { 796 return (buf->last && CHAIN_PINNED_R(buf->last)); 797 } 798 799 static inline void 800 ZERO_CHAIN(struct evbuffer *dst) 801 { 802 ASSERT_EVBUFFER_LOCKED(dst); 803 dst->first = NULL; 804 dst->last = NULL; 805 dst->last_with_datap = &(dst)->first; 806 dst->total_len = 0; 807 } 808 809 /* Prepares the contents of src to be moved to another buffer by removing 810 * read-pinned chains. The first pinned chain is saved in first, and the 811 * last in last. If src has no read-pinned chains, first and last are set 812 * to NULL. */ 813 static int 814 PRESERVE_PINNED(struct evbuffer *src, struct evbuffer_chain **first, 815 struct evbuffer_chain **last) 816 { 817 struct evbuffer_chain *chain, **pinned; 818 819 ASSERT_EVBUFFER_LOCKED(src); 820 821 if (!HAS_PINNED_R(src)) { 822 *first = *last = NULL; 823 return 0; 824 } 825 826 pinned = src->last_with_datap; 827 if (!CHAIN_PINNED_R(*pinned)) 828 pinned = &(*pinned)->next; 829 EVUTIL_ASSERT(CHAIN_PINNED_R(*pinned)); 830 chain = *first = *pinned; 831 *last = src->last; 832 833 /* If there's data in the first pinned chain, we need to allocate 834 * a new chain and copy the data over. */ 835 if (chain->off) { 836 struct evbuffer_chain *tmp; 837 838 EVUTIL_ASSERT(pinned == src->last_with_datap); 839 tmp = evbuffer_chain_new(chain->off); 840 if (!tmp) 841 return -1; 842 memcpy(tmp->buffer, chain->buffer + chain->misalign, 843 chain->off); 844 tmp->off = chain->off; 845 *src->last_with_datap = tmp; 846 src->last = tmp; 847 chain->misalign += chain->off; 848 chain->off = 0; 849 } else { 850 src->last = *src->last_with_datap; 851 *pinned = NULL; 852 } 853 854 return 0; 855 } 856 857 static inline void 858 RESTORE_PINNED(struct evbuffer *src, struct evbuffer_chain *pinned, 859 struct evbuffer_chain *last) 860 { 861 ASSERT_EVBUFFER_LOCKED(src); 862 863 if (!pinned) { 864 ZERO_CHAIN(src); 865 return; 866 } 867 868 src->first = pinned; 869 src->last = last; 870 src->last_with_datap = &src->first; 871 src->total_len = 0; 872 } 873 874 static inline void 875 COPY_CHAIN(struct evbuffer *dst, struct evbuffer *src) 876 { 877 ASSERT_EVBUFFER_LOCKED(dst); 878 ASSERT_EVBUFFER_LOCKED(src); 879 dst->first = src->first; 880 if (src->last_with_datap == &src->first) 881 dst->last_with_datap = &dst->first; 882 else 883 dst->last_with_datap = src->last_with_datap; 884 dst->last = src->last; 885 dst->total_len = src->total_len; 886 } 887 888 static void 889 APPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src) 890 { 891 struct evbuffer_chain **chp; 892 893 ASSERT_EVBUFFER_LOCKED(dst); 894 ASSERT_EVBUFFER_LOCKED(src); 895 896 chp = evbuffer_free_trailing_empty_chains(dst); 897 *chp = src->first; 898 899 if (src->last_with_datap == &src->first) 900 dst->last_with_datap = chp; 901 else 902 dst->last_with_datap = src->last_with_datap; 903 dst->last = src->last; 904 dst->total_len += src->total_len; 905 } 906 907 static inline void 908 APPEND_CHAIN_MULTICAST(struct evbuffer *dst, struct evbuffer *src) 909 { 910 struct evbuffer_chain *tmp; 911 struct evbuffer_chain *chain = src->first; 912 struct evbuffer_multicast_parent *extra; 913 914 ASSERT_EVBUFFER_LOCKED(dst); 915 ASSERT_EVBUFFER_LOCKED(src); 916 917 for (; chain; chain = chain->next) { 918 if (!chain->off || chain->flags & EVBUFFER_DANGLING) { 919 /* skip empty chains */ 920 continue; 921 } 922 923 tmp = evbuffer_chain_new(sizeof(struct evbuffer_multicast_parent)); 924 if (!tmp) { 925 event_warn("%s: out of memory", __func__); 926 return; 927 } 928 extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_multicast_parent, tmp); 929 /* reference evbuffer containing source chain so it 930 * doesn't get released while the chain is still 931 * being referenced to */ 932 evbuffer_incref_(src); 933 extra->source = src; 934 /* reference source chain which now becomes immutable */ 935 evbuffer_chain_incref(chain); 936 extra->parent = chain; 937 chain->flags |= EVBUFFER_IMMUTABLE; 938 tmp->buffer_len = chain->buffer_len; 939 tmp->misalign = chain->misalign; 940 tmp->off = chain->off; 941 tmp->flags |= EVBUFFER_MULTICAST|EVBUFFER_IMMUTABLE; 942 tmp->buffer = chain->buffer; 943 evbuffer_chain_insert(dst, tmp); 944 } 945 } 946 947 static void 948 PREPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src) 949 { 950 ASSERT_EVBUFFER_LOCKED(dst); 951 ASSERT_EVBUFFER_LOCKED(src); 952 src->last->next = dst->first; 953 dst->first = src->first; 954 dst->total_len += src->total_len; 955 if (*dst->last_with_datap == NULL) { 956 if (src->last_with_datap == &(src)->first) 957 dst->last_with_datap = &dst->first; 958 else 959 dst->last_with_datap = src->last_with_datap; 960 } else if (dst->last_with_datap == &dst->first) { 961 dst->last_with_datap = &src->last->next; 962 } 963 } 964 965 int 966 evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) 967 { 968 struct evbuffer_chain *pinned, *last; 969 size_t in_total_len, out_total_len; 970 int result = 0; 971 972 EVBUFFER_LOCK2(inbuf, outbuf); 973 in_total_len = inbuf->total_len; 974 out_total_len = outbuf->total_len; 975 976 if (in_total_len == 0 || outbuf == inbuf) 977 goto done; 978 979 if (outbuf->freeze_end || inbuf->freeze_start) { 980 result = -1; 981 goto done; 982 } 983 984 if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) { 985 result = -1; 986 goto done; 987 } 988 989 if (out_total_len == 0) { 990 /* There might be an empty chain at the start of outbuf; free 991 * it. */ 992 evbuffer_free_all_chains(outbuf->first); 993 COPY_CHAIN(outbuf, inbuf); 994 } else { 995 APPEND_CHAIN(outbuf, inbuf); 996 } 997 998 RESTORE_PINNED(inbuf, pinned, last); 999 1000 inbuf->n_del_for_cb += in_total_len; 1001 outbuf->n_add_for_cb += in_total_len; 1002 1003 evbuffer_invoke_callbacks_(inbuf); 1004 evbuffer_invoke_callbacks_(outbuf); 1005 1006 done: 1007 EVBUFFER_UNLOCK2(inbuf, outbuf); 1008 return result; 1009 } 1010 1011 int 1012 evbuffer_add_buffer_reference(struct evbuffer *outbuf, struct evbuffer *inbuf) 1013 { 1014 size_t in_total_len, out_total_len; 1015 struct evbuffer_chain *chain; 1016 int result = 0; 1017 1018 EVBUFFER_LOCK2(inbuf, outbuf); 1019 in_total_len = inbuf->total_len; 1020 out_total_len = outbuf->total_len; 1021 chain = inbuf->first; 1022 1023 if (in_total_len == 0) 1024 goto done; 1025 1026 if (outbuf->freeze_end || outbuf == inbuf) { 1027 result = -1; 1028 goto done; 1029 } 1030 1031 for (; chain; chain = chain->next) { 1032 if ((chain->flags & (EVBUFFER_FILESEGMENT|EVBUFFER_SENDFILE|EVBUFFER_MULTICAST)) != 0) { 1033 /* chain type can not be referenced */ 1034 result = -1; 1035 goto done; 1036 } 1037 } 1038 1039 if (out_total_len == 0) { 1040 /* There might be an empty chain at the start of outbuf; free 1041 * it. */ 1042 evbuffer_free_all_chains(outbuf->first); 1043 } 1044 APPEND_CHAIN_MULTICAST(outbuf, inbuf); 1045 1046 outbuf->n_add_for_cb += in_total_len; 1047 evbuffer_invoke_callbacks_(outbuf); 1048 1049 done: 1050 EVBUFFER_UNLOCK2(inbuf, outbuf); 1051 return result; 1052 } 1053 1054 int 1055 evbuffer_prepend_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf) 1056 { 1057 struct evbuffer_chain *pinned, *last; 1058 size_t in_total_len, out_total_len; 1059 int result = 0; 1060 1061 EVBUFFER_LOCK2(inbuf, outbuf); 1062 1063 in_total_len = inbuf->total_len; 1064 out_total_len = outbuf->total_len; 1065 1066 if (!in_total_len || inbuf == outbuf) 1067 goto done; 1068 1069 if (outbuf->freeze_start || inbuf->freeze_start) { 1070 result = -1; 1071 goto done; 1072 } 1073 1074 if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) { 1075 result = -1; 1076 goto done; 1077 } 1078 1079 if (out_total_len == 0) { 1080 /* There might be an empty chain at the start of outbuf; free 1081 * it. */ 1082 evbuffer_free_all_chains(outbuf->first); 1083 COPY_CHAIN(outbuf, inbuf); 1084 } else { 1085 PREPEND_CHAIN(outbuf, inbuf); 1086 } 1087 1088 RESTORE_PINNED(inbuf, pinned, last); 1089 1090 inbuf->n_del_for_cb += in_total_len; 1091 outbuf->n_add_for_cb += in_total_len; 1092 1093 evbuffer_invoke_callbacks_(inbuf); 1094 evbuffer_invoke_callbacks_(outbuf); 1095 done: 1096 EVBUFFER_UNLOCK2(inbuf, outbuf); 1097 return result; 1098 } 1099 1100 int 1101 evbuffer_drain(struct evbuffer *buf, size_t len) 1102 { 1103 struct evbuffer_chain *chain, *next; 1104 size_t remaining, old_len; 1105 int result = 0; 1106 1107 EVBUFFER_LOCK(buf); 1108 old_len = buf->total_len; 1109 1110 if (old_len == 0) 1111 goto done; 1112 1113 if (buf->freeze_start) { 1114 result = -1; 1115 goto done; 1116 } 1117 1118 if (len >= old_len && !HAS_PINNED_R(buf)) { 1119 len = old_len; 1120 for (chain = buf->first; chain != NULL; chain = next) { 1121 next = chain->next; 1122 evbuffer_chain_free(chain); 1123 } 1124 1125 ZERO_CHAIN(buf); 1126 } else { 1127 if (len >= old_len) 1128 len = old_len; 1129 1130 buf->total_len -= len; 1131 remaining = len; 1132 for (chain = buf->first; 1133 remaining >= chain->off; 1134 chain = next) { 1135 next = chain->next; 1136 remaining -= chain->off; 1137 1138 if (chain == *buf->last_with_datap) { 1139 buf->last_with_datap = &buf->first; 1140 } 1141 if (&chain->next == buf->last_with_datap) 1142 buf->last_with_datap = &buf->first; 1143 1144 if (CHAIN_PINNED_R(chain)) { 1145 EVUTIL_ASSERT(remaining == 0); 1146 chain->misalign += chain->off; 1147 chain->off = 0; 1148 break; 1149 } else 1150 evbuffer_chain_free(chain); 1151 } 1152 1153 buf->first = chain; 1154 EVUTIL_ASSERT(remaining <= chain->off); 1155 chain->misalign += remaining; 1156 chain->off -= remaining; 1157 } 1158 1159 buf->n_del_for_cb += len; 1160 /* Tell someone about changes in this buffer */ 1161 evbuffer_invoke_callbacks_(buf); 1162 1163 done: 1164 EVBUFFER_UNLOCK(buf); 1165 return result; 1166 } 1167 1168 /* Reads data from an event buffer and drains the bytes read */ 1169 int 1170 evbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen) 1171 { 1172 ev_ssize_t n; 1173 EVBUFFER_LOCK(buf); 1174 n = evbuffer_copyout_from(buf, NULL, data_out, datlen); 1175 if (n > 0) { 1176 if (evbuffer_drain(buf, n)<0) 1177 n = -1; 1178 } 1179 EVBUFFER_UNLOCK(buf); 1180 return (int)n; 1181 } 1182 1183 ev_ssize_t 1184 evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen) 1185 { 1186 return evbuffer_copyout_from(buf, NULL, data_out, datlen); 1187 } 1188 1189 ev_ssize_t 1190 evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, 1191 void *data_out, size_t datlen) 1192 { 1193 /*XXX fails badly on sendfile case. */ 1194 struct evbuffer_chain *chain; 1195 char *data = data_out; 1196 size_t nread; 1197 ev_ssize_t result = 0; 1198 size_t pos_in_chain; 1199 1200 EVBUFFER_LOCK(buf); 1201 1202 if (pos) { 1203 if (datlen > (size_t)(EV_SSIZE_MAX - pos->pos)) { 1204 result = -1; 1205 goto done; 1206 } 1207 chain = pos->internal_.chain; 1208 pos_in_chain = pos->internal_.pos_in_chain; 1209 if (datlen + pos->pos > buf->total_len) 1210 datlen = buf->total_len - pos->pos; 1211 } else { 1212 chain = buf->first; 1213 pos_in_chain = 0; 1214 if (datlen > buf->total_len) 1215 datlen = buf->total_len; 1216 } 1217 1218 1219 if (datlen == 0) 1220 goto done; 1221 1222 if (buf->freeze_start) { 1223 result = -1; 1224 goto done; 1225 } 1226 1227 nread = datlen; 1228 1229 while (datlen && datlen >= chain->off - pos_in_chain) { 1230 size_t copylen = chain->off - pos_in_chain; 1231 memcpy(data, 1232 chain->buffer + chain->misalign + pos_in_chain, 1233 copylen); 1234 data += copylen; 1235 datlen -= copylen; 1236 1237 chain = chain->next; 1238 pos_in_chain = 0; 1239 EVUTIL_ASSERT(chain || datlen==0); 1240 } 1241 1242 if (datlen) { 1243 EVUTIL_ASSERT(chain); 1244 EVUTIL_ASSERT(datlen+pos_in_chain <= chain->off); 1245 1246 memcpy(data, chain->buffer + chain->misalign + pos_in_chain, 1247 datlen); 1248 } 1249 1250 result = nread; 1251 done: 1252 EVBUFFER_UNLOCK(buf); 1253 return result; 1254 } 1255 1256 /* reads data from the src buffer to the dst buffer, avoids memcpy as 1257 * possible. */ 1258 /* XXXX should return ev_ssize_t */ 1259 int 1260 evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst, 1261 size_t datlen) 1262 { 1263 /*XXX We should have an option to force this to be zero-copy.*/ 1264 1265 /*XXX can fail badly on sendfile case. */ 1266 struct evbuffer_chain *chain, *previous; 1267 size_t nread = 0; 1268 int result; 1269 1270 EVBUFFER_LOCK2(src, dst); 1271 1272 chain = previous = src->first; 1273 1274 if (datlen == 0 || dst == src) { 1275 result = 0; 1276 goto done; 1277 } 1278 1279 if (dst->freeze_end || src->freeze_start) { 1280 result = -1; 1281 goto done; 1282 } 1283 1284 /* short-cut if there is no more data buffered */ 1285 if (datlen >= src->total_len) { 1286 datlen = src->total_len; 1287 evbuffer_add_buffer(dst, src); 1288 result = (int)datlen; /*XXXX should return ev_ssize_t*/ 1289 goto done; 1290 } 1291 1292 /* removes chains if possible */ 1293 while (chain->off <= datlen) { 1294 /* We can't remove the last with data from src unless we 1295 * remove all chains, in which case we would have done the if 1296 * block above */ 1297 EVUTIL_ASSERT(chain != *src->last_with_datap); 1298 nread += chain->off; 1299 datlen -= chain->off; 1300 previous = chain; 1301 if (src->last_with_datap == &chain->next) 1302 src->last_with_datap = &src->first; 1303 chain = chain->next; 1304 } 1305 1306 if (chain != src->first) { 1307 /* we can remove the chain */ 1308 struct evbuffer_chain **chp; 1309 chp = evbuffer_free_trailing_empty_chains(dst); 1310 1311 if (dst->first == NULL) { 1312 dst->first = src->first; 1313 } else { 1314 *chp = src->first; 1315 } 1316 dst->last = previous; 1317 previous->next = NULL; 1318 src->first = chain; 1319 advance_last_with_data(dst); 1320 1321 dst->total_len += nread; 1322 dst->n_add_for_cb += nread; 1323 } 1324 1325 /* we know that there is more data in the src buffer than 1326 * we want to read, so we manually drain the chain */ 1327 evbuffer_add(dst, chain->buffer + chain->misalign, datlen); 1328 chain->misalign += datlen; 1329 chain->off -= datlen; 1330 nread += datlen; 1331 1332 /* You might think we would want to increment dst->n_add_for_cb 1333 * here too. But evbuffer_add above already took care of that. 1334 */ 1335 src->total_len -= nread; 1336 src->n_del_for_cb += nread; 1337 1338 if (nread) { 1339 evbuffer_invoke_callbacks_(dst); 1340 evbuffer_invoke_callbacks_(src); 1341 } 1342 result = (int)nread;/*XXXX should change return type */ 1343 1344 done: 1345 EVBUFFER_UNLOCK2(src, dst); 1346 return result; 1347 } 1348 1349 unsigned char * 1350 evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size) 1351 { 1352 struct evbuffer_chain *chain, *next, *tmp, *last_with_data; 1353 unsigned char *buffer, *result = NULL; 1354 ev_ssize_t remaining; 1355 int removed_last_with_data = 0; 1356 int removed_last_with_datap = 0; 1357 1358 EVBUFFER_LOCK(buf); 1359 1360 chain = buf->first; 1361 1362 if (size < 0) 1363 size = buf->total_len; 1364 /* if size > buf->total_len, we cannot guarantee to the user that she 1365 * is going to have a long enough buffer afterwards; so we return 1366 * NULL */ 1367 if (size == 0 || (size_t)size > buf->total_len) 1368 goto done; 1369 1370 /* No need to pull up anything; the first size bytes are 1371 * already here. */ 1372 if (chain->off >= (size_t)size) { 1373 result = chain->buffer + chain->misalign; 1374 goto done; 1375 } 1376 1377 /* Make sure that none of the chains we need to copy from is pinned. */ 1378 remaining = size - chain->off; 1379 EVUTIL_ASSERT(remaining >= 0); 1380 for (tmp=chain->next; tmp; tmp=tmp->next) { 1381 if (CHAIN_PINNED(tmp)) 1382 goto done; 1383 if (tmp->off >= (size_t)remaining) 1384 break; 1385 remaining -= tmp->off; 1386 } 1387 1388 if (CHAIN_PINNED(chain)) { 1389 size_t old_off = chain->off; 1390 if (CHAIN_SPACE_LEN(chain) < size - chain->off) { 1391 /* not enough room at end of chunk. */ 1392 goto done; 1393 } 1394 buffer = CHAIN_SPACE_PTR(chain); 1395 tmp = chain; 1396 tmp->off = size; 1397 size -= old_off; 1398 chain = chain->next; 1399 } else if (chain->buffer_len - chain->misalign >= (size_t)size) { 1400 /* already have enough space in the first chain */ 1401 size_t old_off = chain->off; 1402 buffer = chain->buffer + chain->misalign + chain->off; 1403 tmp = chain; 1404 tmp->off = size; 1405 size -= old_off; 1406 chain = chain->next; 1407 } else { 1408 if ((tmp = evbuffer_chain_new(size)) == NULL) { 1409 event_warn("%s: out of memory", __func__); 1410 goto done; 1411 } 1412 buffer = tmp->buffer; 1413 tmp->off = size; 1414 buf->first = tmp; 1415 } 1416 1417 /* TODO(niels): deal with buffers that point to NULL like sendfile */ 1418 1419 /* Copy and free every chunk that will be entirely pulled into tmp */ 1420 last_with_data = *buf->last_with_datap; 1421 for (; chain != NULL && (size_t)size >= chain->off; chain = next) { 1422 next = chain->next; 1423 1424 memcpy(buffer, chain->buffer + chain->misalign, chain->off); 1425 size -= chain->off; 1426 buffer += chain->off; 1427 if (chain == last_with_data) 1428 removed_last_with_data = 1; 1429 if (&chain->next == buf->last_with_datap) 1430 removed_last_with_datap = 1; 1431 1432 evbuffer_chain_free(chain); 1433 } 1434 1435 if (chain != NULL) { 1436 memcpy(buffer, chain->buffer + chain->misalign, size); 1437 chain->misalign += size; 1438 chain->off -= size; 1439 } else { 1440 buf->last = tmp; 1441 } 1442 1443 tmp->next = chain; 1444 1445 if (removed_last_with_data) { 1446 buf->last_with_datap = &buf->first; 1447 } else if (removed_last_with_datap) { 1448 if (buf->first->next && buf->first->next->off) 1449 buf->last_with_datap = &buf->first->next; 1450 else 1451 buf->last_with_datap = &buf->first; 1452 } 1453 1454 result = (tmp->buffer + tmp->misalign); 1455 1456 done: 1457 EVBUFFER_UNLOCK(buf); 1458 return result; 1459 } 1460 1461 /* 1462 * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'. 1463 * The returned buffer needs to be freed by the called. 1464 */ 1465 char * 1466 evbuffer_readline(struct evbuffer *buffer) 1467 { 1468 return evbuffer_readln(buffer, NULL, EVBUFFER_EOL_ANY); 1469 } 1470 1471 static inline ev_ssize_t 1472 evbuffer_strchr(struct evbuffer_ptr *it, const char chr) 1473 { 1474 struct evbuffer_chain *chain = it->internal_.chain; 1475 size_t i = it->internal_.pos_in_chain; 1476 while (chain != NULL) { 1477 char *buffer = (char *)chain->buffer + chain->misalign; 1478 char *cp = memchr(buffer+i, chr, chain->off-i); 1479 if (cp) { 1480 it->internal_.chain = chain; 1481 it->internal_.pos_in_chain = cp - buffer; 1482 it->pos += (cp - buffer - i); 1483 return it->pos; 1484 } 1485 it->pos += chain->off - i; 1486 i = 0; 1487 chain = chain->next; 1488 } 1489 1490 return (-1); 1491 } 1492 1493 static inline char * 1494 find_eol_char(char *s, size_t len) 1495 { 1496 #define CHUNK_SZ 128 1497 /* Lots of benchmarking found this approach to be faster in practice 1498 * than doing two memchrs over the whole buffer, doin a memchr on each 1499 * char of the buffer, or trying to emulate memchr by hand. */ 1500 char *s_end, *cr, *lf; 1501 s_end = s+len; 1502 while (s < s_end) { 1503 size_t chunk = (s + CHUNK_SZ < s_end) ? CHUNK_SZ : (s_end - s); 1504 cr = memchr(s, '\r', chunk); 1505 lf = memchr(s, '\n', chunk); 1506 if (cr) { 1507 if (lf && lf < cr) 1508 return lf; 1509 return cr; 1510 } else if (lf) { 1511 return lf; 1512 } 1513 s += CHUNK_SZ; 1514 } 1515 1516 return NULL; 1517 #undef CHUNK_SZ 1518 } 1519 1520 static ev_ssize_t 1521 evbuffer_find_eol_char(struct evbuffer_ptr *it) 1522 { 1523 struct evbuffer_chain *chain = it->internal_.chain; 1524 size_t i = it->internal_.pos_in_chain; 1525 while (chain != NULL) { 1526 char *buffer = (char *)chain->buffer + chain->misalign; 1527 char *cp = find_eol_char(buffer+i, chain->off-i); 1528 if (cp) { 1529 it->internal_.chain = chain; 1530 it->internal_.pos_in_chain = cp - buffer; 1531 it->pos += (cp - buffer) - i; 1532 return it->pos; 1533 } 1534 it->pos += chain->off - i; 1535 i = 0; 1536 chain = chain->next; 1537 } 1538 1539 return (-1); 1540 } 1541 1542 static inline size_t 1543 evbuffer_strspn( 1544 struct evbuffer_ptr *ptr, const char *chrset) 1545 { 1546 size_t count = 0; 1547 struct evbuffer_chain *chain = ptr->internal_.chain; 1548 size_t i = ptr->internal_.pos_in_chain; 1549 1550 if (!chain) 1551 return 0; 1552 1553 while (1) { 1554 char *buffer = (char *)chain->buffer + chain->misalign; 1555 for (; i < chain->off; ++i) { 1556 const char *p = chrset; 1557 while (*p) { 1558 if (buffer[i] == *p++) 1559 goto next; 1560 } 1561 ptr->internal_.chain = chain; 1562 ptr->internal_.pos_in_chain = i; 1563 ptr->pos += count; 1564 return count; 1565 next: 1566 ++count; 1567 } 1568 i = 0; 1569 1570 if (! chain->next) { 1571 ptr->internal_.chain = chain; 1572 ptr->internal_.pos_in_chain = i; 1573 ptr->pos += count; 1574 return count; 1575 } 1576 1577 chain = chain->next; 1578 } 1579 } 1580 1581 1582 static inline int 1583 evbuffer_getchr(struct evbuffer_ptr *it) 1584 { 1585 struct evbuffer_chain *chain = it->internal_.chain; 1586 size_t off = it->internal_.pos_in_chain; 1587 1588 if (chain == NULL) 1589 return -1; 1590 1591 return (unsigned char)chain->buffer[chain->misalign + off]; 1592 } 1593 1594 struct evbuffer_ptr 1595 evbuffer_search_eol(struct evbuffer *buffer, 1596 struct evbuffer_ptr *start, size_t *eol_len_out, 1597 enum evbuffer_eol_style eol_style) 1598 { 1599 struct evbuffer_ptr it, it2; 1600 size_t extra_drain = 0; 1601 int ok = 0; 1602 1603 /* Avoid locking in trivial edge cases */ 1604 if (start && start->internal_.chain == NULL) { 1605 PTR_NOT_FOUND(&it); 1606 if (eol_len_out) 1607 *eol_len_out = extra_drain; 1608 return it; 1609 } 1610 1611 EVBUFFER_LOCK(buffer); 1612 1613 if (start) { 1614 memcpy(&it, start, sizeof(it)); 1615 } else { 1616 it.pos = 0; 1617 it.internal_.chain = buffer->first; 1618 it.internal_.pos_in_chain = 0; 1619 } 1620 1621 /* the eol_style determines our first stop character and how many 1622 * characters we are going to drain afterwards. */ 1623 switch (eol_style) { 1624 case EVBUFFER_EOL_ANY: 1625 if (evbuffer_find_eol_char(&it) < 0) 1626 goto done; 1627 memcpy(&it2, &it, sizeof(it)); 1628 extra_drain = evbuffer_strspn(&it2, "\r\n"); 1629 break; 1630 case EVBUFFER_EOL_CRLF_STRICT: { 1631 it = evbuffer_search(buffer, "\r\n", 2, &it); 1632 if (it.pos < 0) 1633 goto done; 1634 extra_drain = 2; 1635 break; 1636 } 1637 case EVBUFFER_EOL_CRLF: { 1638 ev_ssize_t start_pos = it.pos; 1639 /* Look for a LF ... */ 1640 if (evbuffer_strchr(&it, '\n') < 0) 1641 goto done; 1642 extra_drain = 1; 1643 /* ... optionally preceeded by a CR. */ 1644 if (it.pos == start_pos) 1645 break; /* If the first character is \n, don't back up */ 1646 /* This potentially does an extra linear walk over the first 1647 * few chains. Probably, that's not too expensive unless you 1648 * have a really pathological setup. */ 1649 memcpy(&it2, &it, sizeof(it)); 1650 if (evbuffer_ptr_subtract(buffer, &it2, 1)<0) 1651 break; 1652 if (evbuffer_getchr(&it2) == '\r') { 1653 memcpy(&it, &it2, sizeof(it)); 1654 extra_drain = 2; 1655 } 1656 break; 1657 } 1658 case EVBUFFER_EOL_LF: 1659 if (evbuffer_strchr(&it, '\n') < 0) 1660 goto done; 1661 extra_drain = 1; 1662 break; 1663 case EVBUFFER_EOL_NUL: 1664 if (evbuffer_strchr(&it, '\0') < 0) 1665 goto done; 1666 extra_drain = 1; 1667 break; 1668 default: 1669 goto done; 1670 } 1671 1672 ok = 1; 1673 done: 1674 EVBUFFER_UNLOCK(buffer); 1675 1676 if (!ok) 1677 PTR_NOT_FOUND(&it); 1678 if (eol_len_out) 1679 *eol_len_out = extra_drain; 1680 1681 return it; 1682 } 1683 1684 char * 1685 evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out, 1686 enum evbuffer_eol_style eol_style) 1687 { 1688 struct evbuffer_ptr it; 1689 char *line; 1690 size_t n_to_copy=0, extra_drain=0; 1691 char *result = NULL; 1692 1693 EVBUFFER_LOCK(buffer); 1694 1695 if (buffer->freeze_start) { 1696 goto done; 1697 } 1698 1699 it = evbuffer_search_eol(buffer, NULL, &extra_drain, eol_style); 1700 if (it.pos < 0) 1701 goto done; 1702 n_to_copy = it.pos; 1703 1704 if ((line = mm_malloc(n_to_copy+1)) == NULL) { 1705 event_warn("%s: out of memory", __func__); 1706 goto done; 1707 } 1708 1709 evbuffer_remove(buffer, line, n_to_copy); 1710 line[n_to_copy] = '\0'; 1711 1712 evbuffer_drain(buffer, extra_drain); 1713 result = line; 1714 done: 1715 EVBUFFER_UNLOCK(buffer); 1716 1717 if (n_read_out) 1718 *n_read_out = result ? n_to_copy : 0; 1719 1720 return result; 1721 } 1722 1723 #define EVBUFFER_CHAIN_MAX_AUTO_SIZE 4096 1724 1725 /* Adds data to an event buffer */ 1726 1727 int 1728 evbuffer_add(struct evbuffer *buf, const void *data_in, size_t datlen) 1729 { 1730 struct evbuffer_chain *chain, *tmp; 1731 const unsigned char *data = data_in; 1732 size_t remain, to_alloc; 1733 int result = -1; 1734 1735 EVBUFFER_LOCK(buf); 1736 1737 if (buf->freeze_end) { 1738 goto done; 1739 } 1740 /* Prevent buf->total_len overflow */ 1741 if (datlen > EV_SIZE_MAX - buf->total_len) { 1742 goto done; 1743 } 1744 1745 if (*buf->last_with_datap == NULL) { 1746 chain = buf->last; 1747 } else { 1748 chain = *buf->last_with_datap; 1749 } 1750 1751 /* If there are no chains allocated for this buffer, allocate one 1752 * big enough to hold all the data. */ 1753 if (chain == NULL) { 1754 chain = evbuffer_chain_new(datlen); 1755 if (!chain) 1756 goto done; 1757 evbuffer_chain_insert(buf, chain); 1758 } 1759 1760 if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) { 1761 /* Always true for mutable buffers */ 1762 EVUTIL_ASSERT(chain->misalign >= 0 && 1763 (ev_uint64_t)chain->misalign <= EVBUFFER_CHAIN_MAX); 1764 remain = chain->buffer_len - (size_t)chain->misalign - chain->off; 1765 if (remain >= datlen) { 1766 /* there's enough space to hold all the data in the 1767 * current last chain */ 1768 memcpy(chain->buffer + chain->misalign + chain->off, 1769 data, datlen); 1770 chain->off += datlen; 1771 buf->total_len += datlen; 1772 buf->n_add_for_cb += datlen; 1773 goto out; 1774 } else if (!CHAIN_PINNED(chain) && 1775 evbuffer_chain_should_realign(chain, datlen)) { 1776 /* we can fit the data into the misalignment */ 1777 evbuffer_chain_align(chain); 1778 1779 memcpy(chain->buffer + chain->off, data, datlen); 1780 chain->off += datlen; 1781 buf->total_len += datlen; 1782 buf->n_add_for_cb += datlen; 1783 goto out; 1784 } 1785 } else { 1786 /* we cannot write any data to the last chain */ 1787 remain = 0; 1788 } 1789 1790 /* we need to add another chain */ 1791 to_alloc = chain->buffer_len; 1792 if (to_alloc <= EVBUFFER_CHAIN_MAX_AUTO_SIZE/2) 1793 to_alloc <<= 1; 1794 if (datlen > to_alloc) 1795 to_alloc = datlen; 1796 tmp = evbuffer_chain_new(to_alloc); 1797 if (tmp == NULL) 1798 goto done; 1799 1800 if (remain) { 1801 memcpy(chain->buffer + chain->misalign + chain->off, 1802 data, remain); 1803 chain->off += remain; 1804 buf->total_len += remain; 1805 buf->n_add_for_cb += remain; 1806 } 1807 1808 data += remain; 1809 datlen -= remain; 1810 1811 memcpy(tmp->buffer, data, datlen); 1812 tmp->off = datlen; 1813 evbuffer_chain_insert(buf, tmp); 1814 buf->n_add_for_cb += datlen; 1815 1816 out: 1817 evbuffer_invoke_callbacks_(buf); 1818 result = 0; 1819 done: 1820 EVBUFFER_UNLOCK(buf); 1821 return result; 1822 } 1823 1824 int 1825 evbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen) 1826 { 1827 struct evbuffer_chain *chain, *tmp; 1828 int result = -1; 1829 1830 EVBUFFER_LOCK(buf); 1831 1832 if (datlen == 0) { 1833 result = 0; 1834 goto done; 1835 } 1836 if (buf->freeze_start) { 1837 goto done; 1838 } 1839 if (datlen > EV_SIZE_MAX - buf->total_len) { 1840 goto done; 1841 } 1842 1843 chain = buf->first; 1844 1845 if (chain == NULL) { 1846 chain = evbuffer_chain_new(datlen); 1847 if (!chain) 1848 goto done; 1849 evbuffer_chain_insert(buf, chain); 1850 } 1851 1852 /* we cannot touch immutable buffers */ 1853 if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) { 1854 /* Always true for mutable buffers */ 1855 EVUTIL_ASSERT(chain->misalign >= 0 && 1856 (ev_uint64_t)chain->misalign <= EVBUFFER_CHAIN_MAX); 1857 1858 /* If this chain is empty, we can treat it as 1859 * 'empty at the beginning' rather than 'empty at the end' */ 1860 if (chain->off == 0) 1861 chain->misalign = chain->buffer_len; 1862 1863 if ((size_t)chain->misalign >= datlen) { 1864 /* we have enough space to fit everything */ 1865 memcpy(chain->buffer + chain->misalign - datlen, 1866 data, datlen); 1867 chain->off += datlen; 1868 chain->misalign -= datlen; 1869 buf->total_len += datlen; 1870 buf->n_add_for_cb += datlen; 1871 goto out; 1872 } else if (chain->misalign) { 1873 /* we can only fit some of the data. */ 1874 memcpy(chain->buffer, 1875 (char*)data + datlen - chain->misalign, 1876 (size_t)chain->misalign); 1877 chain->off += (size_t)chain->misalign; 1878 buf->total_len += (size_t)chain->misalign; 1879 buf->n_add_for_cb += (size_t)chain->misalign; 1880 datlen -= (size_t)chain->misalign; 1881 chain->misalign = 0; 1882 } 1883 } 1884 1885 /* we need to add another chain */ 1886 if ((tmp = evbuffer_chain_new(datlen)) == NULL) 1887 goto done; 1888 buf->first = tmp; 1889 if (buf->last_with_datap == &buf->first && chain->off) 1890 buf->last_with_datap = &tmp->next; 1891 1892 tmp->next = chain; 1893 1894 tmp->off = datlen; 1895 EVUTIL_ASSERT(datlen <= tmp->buffer_len); 1896 tmp->misalign = tmp->buffer_len - datlen; 1897 1898 memcpy(tmp->buffer + tmp->misalign, data, datlen); 1899 buf->total_len += datlen; 1900 buf->n_add_for_cb += datlen; 1901 1902 out: 1903 evbuffer_invoke_callbacks_(buf); 1904 result = 0; 1905 done: 1906 EVBUFFER_UNLOCK(buf); 1907 return result; 1908 } 1909 1910 /** Helper: realigns the memory in chain->buffer so that misalign is 0. */ 1911 static void 1912 evbuffer_chain_align(struct evbuffer_chain *chain) 1913 { 1914 EVUTIL_ASSERT(!(chain->flags & EVBUFFER_IMMUTABLE)); 1915 EVUTIL_ASSERT(!(chain->flags & EVBUFFER_MEM_PINNED_ANY)); 1916 memmove(chain->buffer, chain->buffer + chain->misalign, chain->off); 1917 chain->misalign = 0; 1918 } 1919 1920 #define MAX_TO_COPY_IN_EXPAND 4096 1921 #define MAX_TO_REALIGN_IN_EXPAND 2048 1922 1923 /** Helper: return true iff we should realign chain to fit datalen bytes of 1924 data in it. */ 1925 static int 1926 evbuffer_chain_should_realign(struct evbuffer_chain *chain, 1927 size_t datlen) 1928 { 1929 return chain->buffer_len - chain->off >= datlen && 1930 (chain->off < chain->buffer_len / 2) && 1931 (chain->off <= MAX_TO_REALIGN_IN_EXPAND); 1932 } 1933 1934 /* Expands the available space in the event buffer to at least datlen, all in 1935 * a single chunk. Return that chunk. */ 1936 static struct evbuffer_chain * 1937 evbuffer_expand_singlechain(struct evbuffer *buf, size_t datlen) 1938 { 1939 struct evbuffer_chain *chain, **chainp; 1940 struct evbuffer_chain *result = NULL; 1941 ASSERT_EVBUFFER_LOCKED(buf); 1942 1943 chainp = buf->last_with_datap; 1944 1945 /* XXX If *chainp is no longer writeable, but has enough space in its 1946 * misalign, this might be a bad idea: we could still use *chainp, not 1947 * (*chainp)->next. */ 1948 if (*chainp && CHAIN_SPACE_LEN(*chainp) == 0) 1949 chainp = &(*chainp)->next; 1950 1951 /* 'chain' now points to the first chain with writable space (if any) 1952 * We will either use it, realign it, replace it, or resize it. */ 1953 chain = *chainp; 1954 1955 if (chain == NULL || 1956 (chain->flags & (EVBUFFER_IMMUTABLE|EVBUFFER_MEM_PINNED_ANY))) { 1957 /* We can't use the last_with_data chain at all. Just add a 1958 * new one that's big enough. */ 1959 goto insert_new; 1960 } 1961 1962 /* If we can fit all the data, then we don't have to do anything */ 1963 if (CHAIN_SPACE_LEN(chain) >= datlen) { 1964 result = chain; 1965 goto ok; 1966 } 1967 1968 /* If the chain is completely empty, just replace it by adding a new 1969 * empty chain. */ 1970 if (chain->off == 0) { 1971 goto insert_new; 1972 } 1973 1974 /* If the misalignment plus the remaining space fulfills our data 1975 * needs, we could just force an alignment to happen. Afterwards, we 1976 * have enough space. But only do this if we're saving a lot of space 1977 * and not moving too much data. Otherwise the space savings are 1978 * probably offset by the time lost in copying. 1979 */ 1980 if (evbuffer_chain_should_realign(chain, datlen)) { 1981 evbuffer_chain_align(chain); 1982 result = chain; 1983 goto ok; 1984 } 1985 1986 /* At this point, we can either resize the last chunk with space in 1987 * it, use the next chunk after it, or If we add a new chunk, we waste 1988 * CHAIN_SPACE_LEN(chain) bytes in the former last chunk. If we 1989 * resize, we have to copy chain->off bytes. 1990 */ 1991 1992 /* Would expanding this chunk be affordable and worthwhile? */ 1993 if (CHAIN_SPACE_LEN(chain) < chain->buffer_len / 8 || 1994 chain->off > MAX_TO_COPY_IN_EXPAND || 1995 datlen >= (EVBUFFER_CHAIN_MAX - chain->off)) { 1996 /* It's not worth resizing this chain. Can the next one be 1997 * used? */ 1998 if (chain->next && CHAIN_SPACE_LEN(chain->next) >= datlen) { 1999 /* Yes, we can just use the next chain (which should 2000 * be empty. */ 2001 result = chain->next; 2002 goto ok; 2003 } else { 2004 /* No; append a new chain (which will free all 2005 * terminal empty chains.) */ 2006 goto insert_new; 2007 } 2008 } else { 2009 /* Okay, we're going to try to resize this chain: Not doing so 2010 * would waste at least 1/8 of its current allocation, and we 2011 * can do so without having to copy more than 2012 * MAX_TO_COPY_IN_EXPAND bytes. */ 2013 /* figure out how much space we need */ 2014 size_t length = chain->off + datlen; 2015 struct evbuffer_chain *tmp = evbuffer_chain_new(length); 2016 if (tmp == NULL) 2017 goto err; 2018 2019 /* copy the data over that we had so far */ 2020 tmp->off = chain->off; 2021 memcpy(tmp->buffer, chain->buffer + chain->misalign, 2022 chain->off); 2023 /* fix up the list */ 2024 EVUTIL_ASSERT(*chainp == chain); 2025 result = *chainp = tmp; 2026 2027 if (buf->last == chain) 2028 buf->last = tmp; 2029 2030 tmp->next = chain->next; 2031 evbuffer_chain_free(chain); 2032 goto ok; 2033 } 2034 2035 insert_new: 2036 result = evbuffer_chain_insert_new(buf, datlen); 2037 if (!result) 2038 goto err; 2039 ok: 2040 EVUTIL_ASSERT(result); 2041 EVUTIL_ASSERT(CHAIN_SPACE_LEN(result) >= datlen); 2042 err: 2043 return result; 2044 } 2045 2046 /* Make sure that datlen bytes are available for writing in the last n 2047 * chains. Never copies or moves data. */ 2048 int 2049 evbuffer_expand_fast_(struct evbuffer *buf, size_t datlen, int n) 2050 { 2051 struct evbuffer_chain *chain = buf->last, *tmp, *next; 2052 size_t avail; 2053 int used; 2054 2055 ASSERT_EVBUFFER_LOCKED(buf); 2056 EVUTIL_ASSERT(n >= 2); 2057 2058 if (chain == NULL || (chain->flags & EVBUFFER_IMMUTABLE)) { 2059 /* There is no last chunk, or we can't touch the last chunk. 2060 * Just add a new chunk. */ 2061 chain = evbuffer_chain_new(datlen); 2062 if (chain == NULL) 2063 return (-1); 2064 2065 evbuffer_chain_insert(buf, chain); 2066 return (0); 2067 } 2068 2069 used = 0; /* number of chains we're using space in. */ 2070 avail = 0; /* how much space they have. */ 2071 /* How many bytes can we stick at the end of buffer as it is? Iterate 2072 * over the chains at the end of the buffer, tring to see how much 2073 * space we have in the first n. */ 2074 for (chain = *buf->last_with_datap; chain; chain = chain->next) { 2075 if (chain->off) { 2076 size_t space = (size_t) CHAIN_SPACE_LEN(chain); 2077 EVUTIL_ASSERT(chain == *buf->last_with_datap); 2078 if (space) { 2079 avail += space; 2080 ++used; 2081 } 2082 } else { 2083 /* No data in chain; realign it. */ 2084 chain->misalign = 0; 2085 avail += chain->buffer_len; 2086 ++used; 2087 } 2088 if (avail >= datlen) { 2089 /* There is already enough space. Just return */ 2090 return (0); 2091 } 2092 if (used == n) 2093 break; 2094 } 2095 2096 /* There wasn't enough space in the first n chains with space in 2097 * them. Either add a new chain with enough space, or replace all 2098 * empty chains with one that has enough space, depending on n. */ 2099 if (used < n) { 2100 /* The loop ran off the end of the chains before it hit n 2101 * chains; we can add another. */ 2102 EVUTIL_ASSERT(chain == NULL); 2103 2104 tmp = evbuffer_chain_new(datlen - avail); 2105 if (tmp == NULL) 2106 return (-1); 2107 2108 buf->last->next = tmp; 2109 buf->last = tmp; 2110 /* (we would only set last_with_data if we added the first 2111 * chain. But if the buffer had no chains, we would have 2112 * just allocated a new chain earlier) */ 2113 return (0); 2114 } else { 2115 /* Nuke _all_ the empty chains. */ 2116 int rmv_all = 0; /* True iff we removed last_with_data. */ 2117 chain = *buf->last_with_datap; 2118 if (!chain->off) { 2119 EVUTIL_ASSERT(chain == buf->first); 2120 rmv_all = 1; 2121 avail = 0; 2122 } else { 2123 /* can't overflow, since only mutable chains have 2124 * huge misaligns. */ 2125 avail = (size_t) CHAIN_SPACE_LEN(chain); 2126 chain = chain->next; 2127 } 2128 2129 2130 for (; chain; chain = next) { 2131 next = chain->next; 2132 EVUTIL_ASSERT(chain->off == 0); 2133 evbuffer_chain_free(chain); 2134 } 2135 EVUTIL_ASSERT(datlen >= avail); 2136 tmp = evbuffer_chain_new(datlen - avail); 2137 if (tmp == NULL) { 2138 if (rmv_all) { 2139 ZERO_CHAIN(buf); 2140 } else { 2141 buf->last = *buf->last_with_datap; 2142 (*buf->last_with_datap)->next = NULL; 2143 } 2144 return (-1); 2145 } 2146 2147 if (rmv_all) { 2148 buf->first = buf->last = tmp; 2149 buf->last_with_datap = &buf->first; 2150 } else { 2151 (*buf->last_with_datap)->next = tmp; 2152 buf->last = tmp; 2153 } 2154 return (0); 2155 } 2156 } 2157 2158 int 2159 evbuffer_expand(struct evbuffer *buf, size_t datlen) 2160 { 2161 struct evbuffer_chain *chain; 2162 2163 EVBUFFER_LOCK(buf); 2164 chain = evbuffer_expand_singlechain(buf, datlen); 2165 EVBUFFER_UNLOCK(buf); 2166 return chain ? 0 : -1; 2167 } 2168 2169 /* 2170 * Reads data from a file descriptor into a buffer. 2171 */ 2172 2173 #if defined(EVENT__HAVE_SYS_UIO_H) || defined(_WIN32) 2174 #define USE_IOVEC_IMPL 2175 #endif 2176 2177 #ifdef USE_IOVEC_IMPL 2178 2179 #ifdef EVENT__HAVE_SYS_UIO_H 2180 /* number of iovec we use for writev, fragmentation is going to determine 2181 * how much we end up writing */ 2182 2183 #define DEFAULT_WRITE_IOVEC 128 2184 2185 #if defined(UIO_MAXIOV) && UIO_MAXIOV < DEFAULT_WRITE_IOVEC 2186 #define NUM_WRITE_IOVEC UIO_MAXIOV 2187 #elif defined(IOV_MAX) && IOV_MAX < DEFAULT_WRITE_IOVEC 2188 #define NUM_WRITE_IOVEC IOV_MAX 2189 #else 2190 #define NUM_WRITE_IOVEC DEFAULT_WRITE_IOVEC 2191 #endif 2192 2193 #define IOV_TYPE struct iovec 2194 #define IOV_PTR_FIELD iov_base 2195 #define IOV_LEN_FIELD iov_len 2196 #define IOV_LEN_TYPE size_t 2197 #else 2198 #define NUM_WRITE_IOVEC 16 2199 #define IOV_TYPE WSABUF 2200 #define IOV_PTR_FIELD buf 2201 #define IOV_LEN_FIELD len 2202 #define IOV_LEN_TYPE unsigned long 2203 #endif 2204 #endif 2205 #define NUM_READ_IOVEC 4 2206 2207 #define EVBUFFER_MAX_READ 4096 2208 2209 /** Helper function to figure out which space to use for reading data into 2210 an evbuffer. Internal use only. 2211 2212 @param buf The buffer to read into 2213 @param howmuch How much we want to read. 2214 @param vecs An array of two or more iovecs or WSABUFs. 2215 @param n_vecs_avail The length of vecs 2216 @param chainp A pointer to a variable to hold the first chain we're 2217 reading into. 2218 @param exact Boolean: if true, we do not provide more than 'howmuch' 2219 space in the vectors, even if more space is available. 2220 @return The number of buffers we're using. 2221 */ 2222 int 2223 evbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch, 2224 struct evbuffer_iovec *vecs, int n_vecs_avail, 2225 struct evbuffer_chain ***chainp, int exact) 2226 { 2227 struct evbuffer_chain *chain; 2228 struct evbuffer_chain **firstchainp; 2229 size_t so_far; 2230 int i; 2231 ASSERT_EVBUFFER_LOCKED(buf); 2232 2233 if (howmuch < 0) 2234 return -1; 2235 2236 so_far = 0; 2237 /* Let firstchain be the first chain with any space on it */ 2238 firstchainp = buf->last_with_datap; 2239 EVUTIL_ASSERT(*firstchainp); 2240 if (CHAIN_SPACE_LEN(*firstchainp) == 0) { 2241 firstchainp = &(*firstchainp)->next; 2242 } 2243 2244 chain = *firstchainp; 2245 EVUTIL_ASSERT(chain); 2246 for (i = 0; i < n_vecs_avail && so_far < (size_t)howmuch; ++i) { 2247 size_t avail = (size_t) CHAIN_SPACE_LEN(chain); 2248 if (avail > (howmuch - so_far) && exact) 2249 avail = howmuch - so_far; 2250 vecs[i].iov_base = (void *)CHAIN_SPACE_PTR(chain); 2251 vecs[i].iov_len = avail; 2252 so_far += avail; 2253 chain = chain->next; 2254 } 2255 2256 *chainp = firstchainp; 2257 return i; 2258 } 2259 2260 static int 2261 get_n_bytes_readable_on_socket(evutil_socket_t fd) 2262 { 2263 #if defined(FIONREAD) && defined(_WIN32) 2264 unsigned long lng = EVBUFFER_MAX_READ; 2265 if (ioctlsocket(fd, FIONREAD, &lng) < 0) 2266 return -1; 2267 /* Can overflow, but mostly harmlessly. XXXX */ 2268 return (int)lng; 2269 #elif defined(FIONREAD) 2270 int n = EVBUFFER_MAX_READ; 2271 if (ioctl(fd, FIONREAD, &n) < 0) 2272 return -1; 2273 return n; 2274 #else 2275 return EVBUFFER_MAX_READ; 2276 #endif 2277 } 2278 2279 /* TODO(niels): should this function return ev_ssize_t and take ev_ssize_t 2280 * as howmuch? */ 2281 int 2282 evbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch) 2283 { 2284 struct evbuffer_chain **chainp; 2285 int n; 2286 int result; 2287 2288 #ifdef USE_IOVEC_IMPL 2289 int nvecs, i, remaining; 2290 #else 2291 struct evbuffer_chain *chain; 2292 unsigned char *p; 2293 #endif 2294 2295 EVBUFFER_LOCK(buf); 2296 2297 if (buf->freeze_end) { 2298 result = -1; 2299 goto done; 2300 } 2301 2302 n = get_n_bytes_readable_on_socket(fd); 2303 if (n <= 0 || n > EVBUFFER_MAX_READ) 2304 n = EVBUFFER_MAX_READ; 2305 if (howmuch < 0 || howmuch > n) 2306 howmuch = n; 2307 2308 #ifdef USE_IOVEC_IMPL 2309 /* Since we can use iovecs, we're willing to use the last 2310 * NUM_READ_IOVEC chains. */ 2311 if (evbuffer_expand_fast_(buf, howmuch, NUM_READ_IOVEC) == -1) { 2312 result = -1; 2313 goto done; 2314 } else { 2315 IOV_TYPE vecs[NUM_READ_IOVEC]; 2316 #ifdef EVBUFFER_IOVEC_IS_NATIVE_ 2317 nvecs = evbuffer_read_setup_vecs_(buf, howmuch, vecs, 2318 NUM_READ_IOVEC, &chainp, 1); 2319 #else 2320 /* We aren't using the native struct iovec. Therefore, 2321 we are on win32. */ 2322 struct evbuffer_iovec ev_vecs[NUM_READ_IOVEC]; 2323 nvecs = evbuffer_read_setup_vecs_(buf, howmuch, ev_vecs, 2, 2324 &chainp, 1); 2325 2326 for (i=0; i < nvecs; ++i) 2327 WSABUF_FROM_EVBUFFER_IOV(&vecs[i], &ev_vecs[i]); 2328 #endif 2329 2330 #ifdef _WIN32 2331 { 2332 DWORD bytesRead; 2333 DWORD flags=0; 2334 if (WSARecv(fd, vecs, nvecs, &bytesRead, &flags, NULL, NULL)) { 2335 /* The read failed. It might be a close, 2336 * or it might be an error. */ 2337 if (WSAGetLastError() == WSAECONNABORTED) 2338 n = 0; 2339 else 2340 n = -1; 2341 } else 2342 n = bytesRead; 2343 } 2344 #else 2345 n = readv(fd, vecs, nvecs); 2346 #endif 2347 } 2348 2349 #else /*!USE_IOVEC_IMPL*/ 2350 /* If we don't have FIONREAD, we might waste some space here */ 2351 /* XXX we _will_ waste some space here if there is any space left 2352 * over on buf->last. */ 2353 if ((chain = evbuffer_expand_singlechain(buf, howmuch)) == NULL) { 2354 result = -1; 2355 goto done; 2356 } 2357 2358 /* We can append new data at this point */ 2359 p = chain->buffer + chain->misalign + chain->off; 2360 2361 #ifndef _WIN32 2362 n = read(fd, p, howmuch); 2363 #else 2364 n = recv(fd, p, howmuch, 0); 2365 #endif 2366 #endif /* USE_IOVEC_IMPL */ 2367 2368 if (n == -1) { 2369 result = -1; 2370 goto done; 2371 } 2372 if (n == 0) { 2373 result = 0; 2374 goto done; 2375 } 2376 2377 #ifdef USE_IOVEC_IMPL 2378 remaining = n; 2379 for (i=0; i < nvecs; ++i) { 2380 /* can't overflow, since only mutable chains have 2381 * huge misaligns. */ 2382 size_t space = (size_t) CHAIN_SPACE_LEN(*chainp); 2383 /* XXXX This is a kludge that can waste space in perverse 2384 * situations. */ 2385 if (space > EVBUFFER_CHAIN_MAX) 2386 space = EVBUFFER_CHAIN_MAX; 2387 if ((ev_ssize_t)space < remaining) { 2388 (*chainp)->off += space; 2389 remaining -= (int)space; 2390 } else { 2391 (*chainp)->off += remaining; 2392 buf->last_with_datap = chainp; 2393 break; 2394 } 2395 chainp = &(*chainp)->next; 2396 } 2397 #else 2398 chain->off += n; 2399 advance_last_with_data(buf); 2400 #endif 2401 buf->total_len += n; 2402 buf->n_add_for_cb += n; 2403 2404 /* Tell someone about changes in this buffer */ 2405 evbuffer_invoke_callbacks_(buf); 2406 result = n; 2407 done: 2408 EVBUFFER_UNLOCK(buf); 2409 return result; 2410 } 2411 2412 #ifdef USE_IOVEC_IMPL 2413 static inline int 2414 evbuffer_write_iovec(struct evbuffer *buffer, evutil_socket_t fd, 2415 ev_ssize_t howmuch) 2416 { 2417 IOV_TYPE iov[NUM_WRITE_IOVEC]; 2418 struct evbuffer_chain *chain = buffer->first; 2419 int n, i = 0; 2420 2421 if (howmuch < 0) 2422 return -1; 2423 2424 ASSERT_EVBUFFER_LOCKED(buffer); 2425 /* XXX make this top out at some maximal data length? if the 2426 * buffer has (say) 1MB in it, split over 128 chains, there's 2427 * no way it all gets written in one go. */ 2428 while (chain != NULL && i < NUM_WRITE_IOVEC && howmuch) { 2429 #ifdef USE_SENDFILE 2430 /* we cannot write the file info via writev */ 2431 if (chain->flags & EVBUFFER_SENDFILE) 2432 break; 2433 #endif 2434 iov[i].IOV_PTR_FIELD = (void *) (chain->buffer + chain->misalign); 2435 if ((size_t)howmuch >= chain->off) { 2436 /* XXXcould be problematic when windows supports mmap*/ 2437 iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)chain->off; 2438 howmuch -= chain->off; 2439 } else { 2440 /* XXXcould be problematic when windows supports mmap*/ 2441 iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)howmuch; 2442 break; 2443 } 2444 chain = chain->next; 2445 } 2446 if (! i) 2447 return 0; 2448 2449 #ifdef _WIN32 2450 { 2451 DWORD bytesSent; 2452 if (WSASend(fd, iov, i, &bytesSent, 0, NULL, NULL)) 2453 n = -1; 2454 else 2455 n = bytesSent; 2456 } 2457 #else 2458 n = writev(fd, iov, i); 2459 #endif 2460 return (n); 2461 } 2462 #endif 2463 2464 #ifdef USE_SENDFILE 2465 static inline int 2466 evbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t dest_fd, 2467 ev_ssize_t howmuch) 2468 { 2469 struct evbuffer_chain *chain = buffer->first; 2470 struct evbuffer_chain_file_segment *info = 2471 EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment, 2472 chain); 2473 const int source_fd = info->segment->fd; 2474 #if defined(SENDFILE_IS_MACOSX) || defined(SENDFILE_IS_FREEBSD) 2475 int res; 2476 ev_off_t len = chain->off; 2477 #elif defined(SENDFILE_IS_LINUX) || defined(SENDFILE_IS_SOLARIS) 2478 ev_ssize_t res; 2479 off_t offset = chain->misalign; 2480 #endif 2481 2482 ASSERT_EVBUFFER_LOCKED(buffer); 2483 2484 #if defined(SENDFILE_IS_MACOSX) 2485 res = sendfile(source_fd, dest_fd, chain->misalign, &len, NULL, 0); 2486 if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno)) 2487 return (-1); 2488 2489 return (len); 2490 #elif defined(SENDFILE_IS_FREEBSD) 2491 res = sendfile(source_fd, dest_fd, chain->misalign, chain->off, NULL, &len, 0); 2492 if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno)) 2493 return (-1); 2494 2495 return (len); 2496 #elif defined(SENDFILE_IS_LINUX) 2497 /* TODO(niels): implement splice */ 2498 res = sendfile(dest_fd, source_fd, &offset, chain->off); 2499 if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) { 2500 /* if this is EAGAIN or EINTR return 0; otherwise, -1 */ 2501 return (0); 2502 } 2503 return (res); 2504 #elif defined(SENDFILE_IS_SOLARIS) 2505 { 2506 const off_t offset_orig = offset; 2507 res = sendfile(dest_fd, source_fd, &offset, chain->off); 2508 if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) { 2509 if (offset - offset_orig) 2510 return offset - offset_orig; 2511 /* if this is EAGAIN or EINTR and no bytes were 2512 * written, return 0 */ 2513 return (0); 2514 } 2515 return (res); 2516 } 2517 #endif 2518 } 2519 #endif 2520 2521 int 2522 evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd, 2523 ev_ssize_t howmuch) 2524 { 2525 int n = -1; 2526 2527 EVBUFFER_LOCK(buffer); 2528 2529 if (buffer->freeze_start) { 2530 goto done; 2531 } 2532 2533 if (howmuch < 0 || (size_t)howmuch > buffer->total_len) 2534 howmuch = buffer->total_len; 2535 2536 if (howmuch > 0) { 2537 #ifdef USE_SENDFILE 2538 struct evbuffer_chain *chain = buffer->first; 2539 if (chain != NULL && (chain->flags & EVBUFFER_SENDFILE)) 2540 n = evbuffer_write_sendfile(buffer, fd, howmuch); 2541 else { 2542 #endif 2543 #ifdef USE_IOVEC_IMPL 2544 n = evbuffer_write_iovec(buffer, fd, howmuch); 2545 #elif defined(_WIN32) 2546 /* XXX(nickm) Don't disable this code until we know if 2547 * the WSARecv code above works. */ 2548 void *p = evbuffer_pullup(buffer, howmuch); 2549 EVUTIL_ASSERT(p || !howmuch); 2550 n = send(fd, p, howmuch, 0); 2551 #else 2552 void *p = evbuffer_pullup(buffer, howmuch); 2553 EVUTIL_ASSERT(p || !howmuch); 2554 n = write(fd, p, howmuch); 2555 #endif 2556 #ifdef USE_SENDFILE 2557 } 2558 #endif 2559 } 2560 2561 if (n > 0) 2562 evbuffer_drain(buffer, n); 2563 2564 done: 2565 EVBUFFER_UNLOCK(buffer); 2566 return (n); 2567 } 2568 2569 int 2570 evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd) 2571 { 2572 return evbuffer_write_atmost(buffer, fd, -1); 2573 } 2574 2575 unsigned char * 2576 evbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len) 2577 { 2578 unsigned char *search; 2579 struct evbuffer_ptr ptr; 2580 2581 EVBUFFER_LOCK(buffer); 2582 2583 ptr = evbuffer_search(buffer, (const char *)what, len, NULL); 2584 if (ptr.pos < 0) { 2585 search = NULL; 2586 } else { 2587 search = evbuffer_pullup(buffer, ptr.pos + len); 2588 if (search) 2589 search += ptr.pos; 2590 } 2591 EVBUFFER_UNLOCK(buffer); 2592 return search; 2593 } 2594 2595 /* Subract <b>howfar</b> from the position of <b>pos</b> within 2596 * <b>buf</b>. Returns 0 on success, -1 on failure. 2597 * 2598 * This isn't exposed yet, because of potential inefficiency issues. 2599 * Maybe it should be. */ 2600 static int 2601 evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos, 2602 size_t howfar) 2603 { 2604 if (pos->pos < 0) 2605 return -1; 2606 if (howfar > (size_t)pos->pos) 2607 return -1; 2608 if (pos->internal_.chain && howfar <= pos->internal_.pos_in_chain) { 2609 pos->internal_.pos_in_chain -= howfar; 2610 pos->pos -= howfar; 2611 return 0; 2612 } else { 2613 const size_t newpos = pos->pos - howfar; 2614 /* Here's the inefficient part: it walks over the 2615 * chains until we hit newpos. */ 2616 return evbuffer_ptr_set(buf, pos, newpos, EVBUFFER_PTR_SET); 2617 } 2618 } 2619 2620 int 2621 evbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos, 2622 size_t position, enum evbuffer_ptr_how how) 2623 { 2624 size_t left = position; 2625 struct evbuffer_chain *chain = NULL; 2626 int result = 0; 2627 2628 EVBUFFER_LOCK(buf); 2629 2630 switch (how) { 2631 case EVBUFFER_PTR_SET: 2632 chain = buf->first; 2633 pos->pos = position; 2634 position = 0; 2635 break; 2636 case EVBUFFER_PTR_ADD: 2637 /* this avoids iterating over all previous chains if 2638 we just want to advance the position */ 2639 if (pos->pos < 0 || EV_SIZE_MAX - position < (size_t)pos->pos) { 2640 EVBUFFER_UNLOCK(buf); 2641 return -1; 2642 } 2643 chain = pos->internal_.chain; 2644 pos->pos += position; 2645 position = pos->internal_.pos_in_chain; 2646 break; 2647 } 2648 2649 EVUTIL_ASSERT(EV_SIZE_MAX - left >= position); 2650 while (chain && position + left >= chain->off) { 2651 left -= chain->off - position; 2652 chain = chain->next; 2653 position = 0; 2654 } 2655 if (chain) { 2656 pos->internal_.chain = chain; 2657 pos->internal_.pos_in_chain = position + left; 2658 } else if (left == 0) { 2659 /* The first byte in the (nonexistent) chain after the last chain */ 2660 pos->internal_.chain = NULL; 2661 pos->internal_.pos_in_chain = 0; 2662 } else { 2663 PTR_NOT_FOUND(pos); 2664 result = -1; 2665 } 2666 2667 EVBUFFER_UNLOCK(buf); 2668 2669 return result; 2670 } 2671 2672 /** 2673 Compare the bytes in buf at position pos to the len bytes in mem. Return 2674 less than 0, 0, or greater than 0 as memcmp. 2675 */ 2676 static int 2677 evbuffer_ptr_memcmp(const struct evbuffer *buf, const struct evbuffer_ptr *pos, 2678 const char *mem, size_t len) 2679 { 2680 struct evbuffer_chain *chain; 2681 size_t position; 2682 int r; 2683 2684 ASSERT_EVBUFFER_LOCKED(buf); 2685 2686 if (pos->pos < 0 || 2687 EV_SIZE_MAX - len < (size_t)pos->pos || 2688 pos->pos + len > buf->total_len) 2689 return -1; 2690 2691 chain = pos->internal_.chain; 2692 position = pos->internal_.pos_in_chain; 2693 while (len && chain) { 2694 size_t n_comparable; 2695 if (len + position > chain->off) 2696 n_comparable = chain->off - position; 2697 else 2698 n_comparable = len; 2699 r = memcmp(chain->buffer + chain->misalign + position, mem, 2700 n_comparable); 2701 if (r) 2702 return r; 2703 mem += n_comparable; 2704 len -= n_comparable; 2705 position = 0; 2706 chain = chain->next; 2707 } 2708 2709 return 0; 2710 } 2711 2712 struct evbuffer_ptr 2713 evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start) 2714 { 2715 return evbuffer_search_range(buffer, what, len, start, NULL); 2716 } 2717 2718 struct evbuffer_ptr 2719 evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end) 2720 { 2721 struct evbuffer_ptr pos; 2722 struct evbuffer_chain *chain, *last_chain = NULL; 2723 const unsigned char *p; 2724 char first; 2725 2726 EVBUFFER_LOCK(buffer); 2727 2728 if (start) { 2729 memcpy(&pos, start, sizeof(pos)); 2730 chain = pos.internal_.chain; 2731 } else { 2732 pos.pos = 0; 2733 chain = pos.internal_.chain = buffer->first; 2734 pos.internal_.pos_in_chain = 0; 2735 } 2736 2737 if (end) 2738 last_chain = end->internal_.chain; 2739 2740 if (!len || len > EV_SSIZE_MAX) 2741 goto done; 2742 2743 first = what[0]; 2744 2745 while (chain) { 2746 const unsigned char *start_at = 2747 chain->buffer + chain->misalign + 2748 pos.internal_.pos_in_chain; 2749 p = memchr(start_at, first, 2750 chain->off - pos.internal_.pos_in_chain); 2751 if (p) { 2752 pos.pos += p - start_at; 2753 pos.internal_.pos_in_chain += p - start_at; 2754 if (!evbuffer_ptr_memcmp(buffer, &pos, what, len)) { 2755 if (end && pos.pos + (ev_ssize_t)len > end->pos) 2756 goto not_found; 2757 else 2758 goto done; 2759 } 2760 ++pos.pos; 2761 ++pos.internal_.pos_in_chain; 2762 if (pos.internal_.pos_in_chain == chain->off) { 2763 chain = pos.internal_.chain = chain->next; 2764 pos.internal_.pos_in_chain = 0; 2765 } 2766 } else { 2767 if (chain == last_chain) 2768 goto not_found; 2769 pos.pos += chain->off - pos.internal_.pos_in_chain; 2770 chain = pos.internal_.chain = chain->next; 2771 pos.internal_.pos_in_chain = 0; 2772 } 2773 } 2774 2775 not_found: 2776 PTR_NOT_FOUND(&pos); 2777 done: 2778 EVBUFFER_UNLOCK(buffer); 2779 return pos; 2780 } 2781 2782 int 2783 evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len, 2784 struct evbuffer_ptr *start_at, 2785 struct evbuffer_iovec *vec, int n_vec) 2786 { 2787 struct evbuffer_chain *chain; 2788 int idx = 0; 2789 ev_ssize_t len_so_far = 0; 2790 2791 /* Avoid locking in trivial edge cases */ 2792 if (start_at && start_at->internal_.chain == NULL) 2793 return 0; 2794 2795 EVBUFFER_LOCK(buffer); 2796 2797 if (start_at) { 2798 chain = start_at->internal_.chain; 2799 len_so_far = chain->off 2800 - start_at->internal_.pos_in_chain; 2801 idx = 1; 2802 if (n_vec > 0) { 2803 vec[0].iov_base = (void *)(chain->buffer + chain->misalign 2804 + start_at->internal_.pos_in_chain); 2805 vec[0].iov_len = len_so_far; 2806 } 2807 chain = chain->next; 2808 } else { 2809 chain = buffer->first; 2810 } 2811 2812 if (n_vec == 0 && len < 0) { 2813 /* If no vectors are provided and they asked for "everything", 2814 * pretend they asked for the actual available amount. */ 2815 len = buffer->total_len; 2816 if (start_at) { 2817 len -= start_at->pos; 2818 } 2819 } 2820 2821 while (chain) { 2822 if (len >= 0 && len_so_far >= len) 2823 break; 2824 if (idx<n_vec) { 2825 vec[idx].iov_base = (void *)(chain->buffer + chain->misalign); 2826 vec[idx].iov_len = chain->off; 2827 } else if (len<0) { 2828 break; 2829 } 2830 ++idx; 2831 len_so_far += chain->off; 2832 chain = chain->next; 2833 } 2834 2835 EVBUFFER_UNLOCK(buffer); 2836 2837 return idx; 2838 } 2839 2840 2841 int 2842 evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap) 2843 { 2844 char *buffer; 2845 size_t space; 2846 int sz, result = -1; 2847 va_list aq; 2848 struct evbuffer_chain *chain; 2849 2850 2851 EVBUFFER_LOCK(buf); 2852 2853 if (buf->freeze_end) { 2854 goto done; 2855 } 2856 2857 /* make sure that at least some space is available */ 2858 if ((chain = evbuffer_expand_singlechain(buf, 64)) == NULL) 2859 goto done; 2860 2861 for (;;) { 2862 #if 0 2863 size_t used = chain->misalign + chain->off; 2864 buffer = (char *)chain->buffer + chain->misalign + chain->off; 2865 EVUTIL_ASSERT(chain->buffer_len >= used); 2866 space = chain->buffer_len - used; 2867 #endif 2868 buffer = (char*) CHAIN_SPACE_PTR(chain); 2869 space = (size_t) CHAIN_SPACE_LEN(chain); 2870 2871 #ifndef va_copy 2872 #define va_copy(dst, src) memcpy(&(dst), &(src), sizeof(va_list)) 2873 #endif 2874 va_copy(aq, ap); 2875 2876 sz = evutil_vsnprintf(buffer, space, fmt, aq); 2877 2878 va_end(aq); 2879 2880 if (sz < 0) 2881 goto done; 2882 if (INT_MAX >= EVBUFFER_CHAIN_MAX && 2883 (size_t)sz >= EVBUFFER_CHAIN_MAX) 2884 goto done; 2885 if ((size_t)sz < space) { 2886 chain->off += sz; 2887 buf->total_len += sz; 2888 buf->n_add_for_cb += sz; 2889 2890 advance_last_with_data(buf); 2891 evbuffer_invoke_callbacks_(buf); 2892 result = sz; 2893 goto done; 2894 } 2895 if ((chain = evbuffer_expand_singlechain(buf, sz + 1)) == NULL) 2896 goto done; 2897 } 2898 /* NOTREACHED */ 2899 2900 done: 2901 EVBUFFER_UNLOCK(buf); 2902 return result; 2903 } 2904 2905 int 2906 evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...) 2907 { 2908 int res = -1; 2909 va_list ap; 2910 2911 va_start(ap, fmt); 2912 res = evbuffer_add_vprintf(buf, fmt, ap); 2913 va_end(ap); 2914 2915 return (res); 2916 } 2917 2918 int 2919 evbuffer_add_reference(struct evbuffer *outbuf, 2920 const void *data, size_t datlen, 2921 evbuffer_ref_cleanup_cb cleanupfn, void *extra) 2922 { 2923 struct evbuffer_chain *chain; 2924 struct evbuffer_chain_reference *info; 2925 int result = -1; 2926 2927 chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_reference)); 2928 if (!chain) 2929 return (-1); 2930 chain->flags |= EVBUFFER_REFERENCE | EVBUFFER_IMMUTABLE; 2931 chain->buffer = (unsigned char *)data; 2932 chain->buffer_len = datlen; 2933 chain->off = datlen; 2934 2935 info = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_reference, chain); 2936 info->cleanupfn = cleanupfn; 2937 info->extra = extra; 2938 2939 EVBUFFER_LOCK(outbuf); 2940 if (outbuf->freeze_end) { 2941 /* don't call chain_free; we do not want to actually invoke 2942 * the cleanup function */ 2943 mm_free(chain); 2944 goto done; 2945 } 2946 evbuffer_chain_insert(outbuf, chain); 2947 outbuf->n_add_for_cb += datlen; 2948 2949 evbuffer_invoke_callbacks_(outbuf); 2950 2951 result = 0; 2952 done: 2953 EVBUFFER_UNLOCK(outbuf); 2954 2955 return result; 2956 } 2957 2958 /* TODO(niels): we may want to add to automagically convert to mmap, in 2959 * case evbuffer_remove() or evbuffer_pullup() are being used. 2960 */ 2961 struct evbuffer_file_segment * 2962 evbuffer_file_segment_new( 2963 int fd, ev_off_t offset, ev_off_t length, unsigned flags) 2964 { 2965 struct evbuffer_file_segment *seg = 2966 mm_calloc(sizeof(struct evbuffer_file_segment), 1); 2967 if (!seg) 2968 return NULL; 2969 seg->refcnt = 1; 2970 seg->fd = fd; 2971 seg->flags = flags; 2972 seg->file_offset = offset; 2973 seg->cleanup_cb = NULL; 2974 seg->cleanup_cb_arg = NULL; 2975 #ifdef _WIN32 2976 #ifndef lseek 2977 #define lseek _lseeki64 2978 #endif 2979 #ifndef fstat 2980 #define fstat _fstat 2981 #endif 2982 #ifndef stat 2983 #define stat _stat 2984 #endif 2985 #endif 2986 if (length == -1) { 2987 struct stat st; 2988 if (fstat(fd, &st) < 0) 2989 goto err; 2990 length = st.st_size; 2991 } 2992 seg->length = length; 2993 2994 if (offset < 0 || length < 0 || 2995 ((ev_uint64_t)length > EVBUFFER_CHAIN_MAX) || 2996 (ev_uint64_t)offset > (ev_uint64_t)(EVBUFFER_CHAIN_MAX - length)) 2997 goto err; 2998 2999 #if defined(USE_SENDFILE) 3000 if (!(flags & EVBUF_FS_DISABLE_SENDFILE)) { 3001 seg->can_sendfile = 1; 3002 goto done; 3003 } 3004 #endif 3005 3006 if (evbuffer_file_segment_materialize(seg)<0) 3007 goto err; 3008 3009 #if defined(USE_SENDFILE) 3010 done: 3011 #endif 3012 if (!(flags & EVBUF_FS_DISABLE_LOCKING)) { 3013 EVTHREAD_ALLOC_LOCK(seg->lock, 0); 3014 } 3015 return seg; 3016 err: 3017 mm_free(seg); 3018 return NULL; 3019 } 3020 3021 #ifdef EVENT__HAVE_MMAP 3022 static long 3023 get_page_size(void) 3024 { 3025 #ifdef SC_PAGE_SIZE 3026 return sysconf(SC_PAGE_SIZE); 3027 #elif defined(_SC_PAGE_SIZE) 3028 return sysconf(_SC_PAGE_SIZE); 3029 #else 3030 return 1; 3031 #endif 3032 } 3033 #endif 3034 3035 /* DOCDOC */ 3036 /* Requires lock */ 3037 static int 3038 evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg) 3039 { 3040 const unsigned flags = seg->flags; 3041 const int fd = seg->fd; 3042 const ev_off_t length = seg->length; 3043 const ev_off_t offset = seg->file_offset; 3044 3045 if (seg->contents) 3046 return 0; /* already materialized */ 3047 3048 #if defined(EVENT__HAVE_MMAP) 3049 if (!(flags & EVBUF_FS_DISABLE_MMAP)) { 3050 off_t offset_rounded = 0, offset_leftover = 0; 3051 void *mapped; 3052 if (offset) { 3053 /* mmap implementations don't generally like us 3054 * to have an offset that isn't a round */ 3055 long page_size = get_page_size(); 3056 if (page_size == -1) 3057 goto err; 3058 offset_leftover = offset % page_size; 3059 offset_rounded = offset - offset_leftover; 3060 } 3061 mapped = mmap(NULL, length + offset_leftover, 3062 PROT_READ, 3063 #ifdef MAP_NOCACHE 3064 MAP_NOCACHE | /* ??? */ 3065 #endif 3066 #ifdef MAP_FILE 3067 MAP_FILE | 3068 #endif 3069 MAP_PRIVATE, 3070 fd, offset_rounded); 3071 if (mapped == MAP_FAILED) { 3072 event_warn("%s: mmap(%d, %d, %zu) failed", 3073 __func__, fd, 0, (size_t)(offset + length)); 3074 } else { 3075 seg->mapping = mapped; 3076 seg->contents = (char*)mapped+offset_leftover; 3077 seg->mmap_offset = 0; 3078 seg->is_mapping = 1; 3079 goto done; 3080 } 3081 } 3082 #endif 3083 #ifdef _WIN32 3084 if (!(flags & EVBUF_FS_DISABLE_MMAP)) { 3085 intptr_t h = _get_osfhandle(fd); 3086 HANDLE m; 3087 ev_uint64_t total_size = length+offset; 3088 if ((HANDLE)h == INVALID_HANDLE_VALUE) 3089 goto err; 3090 m = CreateFileMapping((HANDLE)h, NULL, PAGE_READONLY, 3091 (total_size >> 32), total_size & 0xfffffffful, 3092 NULL); 3093 if (m != INVALID_HANDLE_VALUE) { /* Does h leak? */ 3094 seg->mapping_handle = m; 3095 seg->mmap_offset = offset; 3096 seg->is_mapping = 1; 3097 goto done; 3098 } 3099 } 3100 #endif 3101 { 3102 ev_off_t start_pos = lseek(fd, 0, SEEK_CUR), pos; 3103 ev_off_t read_so_far = 0; 3104 char *mem; 3105 int e; 3106 ev_ssize_t n = 0; 3107 if (!(mem = mm_malloc(length))) 3108 goto err; 3109 if (start_pos < 0) { 3110 mm_free(mem); 3111 goto err; 3112 } 3113 if (lseek(fd, offset, SEEK_SET) < 0) { 3114 mm_free(mem); 3115 goto err; 3116 } 3117 while (read_so_far < length) { 3118 n = read(fd, mem+read_so_far, length-read_so_far); 3119 if (n <= 0) 3120 break; 3121 read_so_far += n; 3122 } 3123 3124 e = errno; 3125 pos = lseek(fd, start_pos, SEEK_SET); 3126 if (n < 0 || (n == 0 && length > read_so_far)) { 3127 mm_free(mem); 3128 errno = e; 3129 goto err; 3130 } else if (pos < 0) { 3131 mm_free(mem); 3132 goto err; 3133 } 3134 3135 seg->contents = mem; 3136 } 3137 3138 done: 3139 return 0; 3140 err: 3141 return -1; 3142 } 3143 3144 void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg, 3145 evbuffer_file_segment_cleanup_cb cb, void* arg) 3146 { 3147 EVUTIL_ASSERT(seg->refcnt > 0); 3148 seg->cleanup_cb = cb; 3149 seg->cleanup_cb_arg = arg; 3150 } 3151 3152 void 3153 evbuffer_file_segment_free(struct evbuffer_file_segment *seg) 3154 { 3155 int refcnt; 3156 EVLOCK_LOCK(seg->lock, 0); 3157 refcnt = --seg->refcnt; 3158 EVLOCK_UNLOCK(seg->lock, 0); 3159 if (refcnt > 0) 3160 return; 3161 EVUTIL_ASSERT(refcnt == 0); 3162 3163 if (seg->is_mapping) { 3164 #ifdef _WIN32 3165 CloseHandle(seg->mapping_handle); 3166 #elif defined (EVENT__HAVE_MMAP) 3167 off_t offset_leftover; 3168 offset_leftover = seg->file_offset % get_page_size(); 3169 if (munmap(seg->mapping, seg->length + offset_leftover) == -1) 3170 event_warn("%s: munmap failed", __func__); 3171 #endif 3172 } else if (seg->contents) { 3173 mm_free(seg->contents); 3174 } 3175 3176 if ((seg->flags & EVBUF_FS_CLOSE_ON_FREE) && seg->fd >= 0) { 3177 close(seg->fd); 3178 } 3179 3180 if (seg->cleanup_cb) { 3181 (*seg->cleanup_cb)((struct evbuffer_file_segment const*)seg, 3182 seg->flags, seg->cleanup_cb_arg); 3183 seg->cleanup_cb = NULL; 3184 seg->cleanup_cb_arg = NULL; 3185 } 3186 3187 EVTHREAD_FREE_LOCK(seg->lock, 0); 3188 mm_free(seg); 3189 } 3190 3191 int 3192 evbuffer_add_file_segment(struct evbuffer *buf, 3193 struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length) 3194 { 3195 struct evbuffer_chain *chain; 3196 struct evbuffer_chain_file_segment *extra; 3197 int can_use_sendfile = 0; 3198 3199 EVBUFFER_LOCK(buf); 3200 EVLOCK_LOCK(seg->lock, 0); 3201 if (buf->flags & EVBUFFER_FLAG_DRAINS_TO_FD) { 3202 can_use_sendfile = 1; 3203 } else { 3204 if (!seg->contents) { 3205 if (evbuffer_file_segment_materialize(seg)<0) { 3206 EVLOCK_UNLOCK(seg->lock, 0); 3207 EVBUFFER_UNLOCK(buf); 3208 return -1; 3209 } 3210 } 3211 } 3212 EVLOCK_UNLOCK(seg->lock, 0); 3213 3214 if (buf->freeze_end) 3215 goto err; 3216 3217 if (length < 0) { 3218 if (offset > seg->length) 3219 goto err; 3220 length = seg->length - offset; 3221 } 3222 3223 /* Can we actually add this? */ 3224 if (offset+length > seg->length) 3225 goto err; 3226 3227 chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_file_segment)); 3228 if (!chain) 3229 goto err; 3230 extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment, chain); 3231 3232 chain->flags |= EVBUFFER_IMMUTABLE|EVBUFFER_FILESEGMENT; 3233 if (can_use_sendfile && seg->can_sendfile) { 3234 chain->flags |= EVBUFFER_SENDFILE; 3235 chain->misalign = seg->file_offset + offset; 3236 chain->off = length; 3237 chain->buffer_len = chain->misalign + length; 3238 } else if (seg->is_mapping) { 3239 #ifdef _WIN32 3240 ev_uint64_t total_offset = seg->mmap_offset+offset; 3241 ev_uint64_t offset_rounded=0, offset_remaining=0; 3242 LPVOID data; 3243 if (total_offset) { 3244 SYSTEM_INFO si; 3245 memset(&si, 0, sizeof(si)); /* cargo cult */ 3246 GetSystemInfo(&si); 3247 offset_remaining = total_offset % si.dwAllocationGranularity; 3248 offset_rounded = total_offset - offset_remaining; 3249 } 3250 data = MapViewOfFile( 3251 seg->mapping_handle, 3252 FILE_MAP_READ, 3253 offset_rounded >> 32, 3254 offset_rounded & 0xfffffffful, 3255 length + offset_remaining); 3256 if (data == NULL) { 3257 mm_free(chain); 3258 goto err; 3259 } 3260 chain->buffer = (unsigned char*) data; 3261 chain->buffer_len = length+offset_remaining; 3262 chain->misalign = offset_remaining; 3263 chain->off = length; 3264 #else 3265 chain->buffer = (unsigned char*)(seg->contents + offset); 3266 chain->buffer_len = length; 3267 chain->off = length; 3268 #endif 3269 } else { 3270 chain->buffer = (unsigned char*)(seg->contents + offset); 3271 chain->buffer_len = length; 3272 chain->off = length; 3273 } 3274 3275 EVLOCK_LOCK(seg->lock, 0); 3276 ++seg->refcnt; 3277 EVLOCK_UNLOCK(seg->lock, 0); 3278 extra->segment = seg; 3279 buf->n_add_for_cb += length; 3280 evbuffer_chain_insert(buf, chain); 3281 3282 evbuffer_invoke_callbacks_(buf); 3283 3284 EVBUFFER_UNLOCK(buf); 3285 3286 return 0; 3287 err: 3288 EVBUFFER_UNLOCK(buf); 3289 evbuffer_file_segment_free(seg); /* Lowers the refcount */ 3290 return -1; 3291 } 3292 3293 int 3294 evbuffer_add_file(struct evbuffer *buf, int fd, ev_off_t offset, ev_off_t length) 3295 { 3296 struct evbuffer_file_segment *seg; 3297 unsigned flags = EVBUF_FS_CLOSE_ON_FREE; 3298 int r; 3299 3300 seg = evbuffer_file_segment_new(fd, offset, length, flags); 3301 if (!seg) 3302 return -1; 3303 r = evbuffer_add_file_segment(buf, seg, 0, length); 3304 if (r == 0) 3305 evbuffer_file_segment_free(seg); 3306 return r; 3307 } 3308 3309 int 3310 evbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg) 3311 { 3312 EVBUFFER_LOCK(buffer); 3313 3314 if (!LIST_EMPTY(&buffer->callbacks)) 3315 evbuffer_remove_all_callbacks(buffer); 3316 3317 if (cb) { 3318 struct evbuffer_cb_entry *ent = 3319 evbuffer_add_cb(buffer, NULL, cbarg); 3320 if (!ent) { 3321 EVBUFFER_UNLOCK(buffer); 3322 return -1; 3323 } 3324 ent->cb.cb_obsolete = cb; 3325 ent->flags |= EVBUFFER_CB_OBSOLETE; 3326 } 3327 EVBUFFER_UNLOCK(buffer); 3328 return 0; 3329 } 3330 3331 struct evbuffer_cb_entry * 3332 evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg) 3333 { 3334 struct evbuffer_cb_entry *e; 3335 if (! (e = mm_calloc(1, sizeof(struct evbuffer_cb_entry)))) 3336 return NULL; 3337 EVBUFFER_LOCK(buffer); 3338 e->cb.cb_func = cb; 3339 e->cbarg = cbarg; 3340 e->flags = EVBUFFER_CB_ENABLED; 3341 LIST_INSERT_HEAD(&buffer->callbacks, e, next); 3342 EVBUFFER_UNLOCK(buffer); 3343 return e; 3344 } 3345 3346 int 3347 evbuffer_remove_cb_entry(struct evbuffer *buffer, 3348 struct evbuffer_cb_entry *ent) 3349 { 3350 EVBUFFER_LOCK(buffer); 3351 LIST_REMOVE(ent, next); 3352 EVBUFFER_UNLOCK(buffer); 3353 mm_free(ent); 3354 return 0; 3355 } 3356 3357 int 3358 evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg) 3359 { 3360 struct evbuffer_cb_entry *cbent; 3361 int result = -1; 3362 EVBUFFER_LOCK(buffer); 3363 LIST_FOREACH(cbent, &buffer->callbacks, next) { 3364 if (cb == cbent->cb.cb_func && cbarg == cbent->cbarg) { 3365 result = evbuffer_remove_cb_entry(buffer, cbent); 3366 goto done; 3367 } 3368 } 3369 done: 3370 EVBUFFER_UNLOCK(buffer); 3371 return result; 3372 } 3373 3374 int 3375 evbuffer_cb_set_flags(struct evbuffer *buffer, 3376 struct evbuffer_cb_entry *cb, ev_uint32_t flags) 3377 { 3378 /* the user isn't allowed to mess with these. */ 3379 flags &= ~EVBUFFER_CB_INTERNAL_FLAGS; 3380 EVBUFFER_LOCK(buffer); 3381 cb->flags |= flags; 3382 EVBUFFER_UNLOCK(buffer); 3383 return 0; 3384 } 3385 3386 int 3387 evbuffer_cb_clear_flags(struct evbuffer *buffer, 3388 struct evbuffer_cb_entry *cb, ev_uint32_t flags) 3389 { 3390 /* the user isn't allowed to mess with these. */ 3391 flags &= ~EVBUFFER_CB_INTERNAL_FLAGS; 3392 EVBUFFER_LOCK(buffer); 3393 cb->flags &= ~flags; 3394 EVBUFFER_UNLOCK(buffer); 3395 return 0; 3396 } 3397 3398 int 3399 evbuffer_freeze(struct evbuffer *buffer, int start) 3400 { 3401 EVBUFFER_LOCK(buffer); 3402 if (start) 3403 buffer->freeze_start = 1; 3404 else 3405 buffer->freeze_end = 1; 3406 EVBUFFER_UNLOCK(buffer); 3407 return 0; 3408 } 3409 3410 int 3411 evbuffer_unfreeze(struct evbuffer *buffer, int start) 3412 { 3413 EVBUFFER_LOCK(buffer); 3414 if (start) 3415 buffer->freeze_start = 0; 3416 else 3417 buffer->freeze_end = 0; 3418 EVBUFFER_UNLOCK(buffer); 3419 return 0; 3420 } 3421 3422 #if 0 3423 void 3424 evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb) 3425 { 3426 if (!(cb->flags & EVBUFFER_CB_SUSPENDED)) { 3427 cb->size_before_suspend = evbuffer_get_length(buffer); 3428 cb->flags |= EVBUFFER_CB_SUSPENDED; 3429 } 3430 } 3431 3432 void 3433 evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb) 3434 { 3435 if ((cb->flags & EVBUFFER_CB_SUSPENDED)) { 3436 unsigned call = (cb->flags & EVBUFFER_CB_CALL_ON_UNSUSPEND); 3437 size_t sz = cb->size_before_suspend; 3438 cb->flags &= ~(EVBUFFER_CB_SUSPENDED| 3439 EVBUFFER_CB_CALL_ON_UNSUSPEND); 3440 cb->size_before_suspend = 0; 3441 if (call && (cb->flags & EVBUFFER_CB_ENABLED)) { 3442 cb->cb(buffer, sz, evbuffer_get_length(buffer), cb->cbarg); 3443 } 3444 } 3445 } 3446 #endif 3447 3448 int 3449 evbuffer_get_callbacks_(struct evbuffer *buffer, struct event_callback **cbs, 3450 int max_cbs) 3451 { 3452 int r = 0; 3453 EVBUFFER_LOCK(buffer); 3454 if (buffer->deferred_cbs) { 3455 if (max_cbs < 1) { 3456 r = -1; 3457 goto done; 3458 } 3459 cbs[0] = &buffer->deferred; 3460 r = 1; 3461 } 3462 done: 3463 EVBUFFER_UNLOCK(buffer); 3464 return r; 3465 } 3466