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