1290001Sglebius/*
2290001Sglebius * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3290001Sglebius * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4290001Sglebius *
5290001Sglebius * Redistribution and use in source and binary forms, with or without
6290001Sglebius * modification, are permitted provided that the following conditions
7290001Sglebius * are met:
8290001Sglebius * 1. Redistributions of source code must retain the above copyright
9290001Sglebius *    notice, this list of conditions and the following disclaimer.
10290001Sglebius * 2. Redistributions in binary form must reproduce the above copyright
11290001Sglebius *    notice, this list of conditions and the following disclaimer in the
12290001Sglebius *    documentation and/or other materials provided with the distribution.
13290001Sglebius * 3. The name of the author may not be used to endorse or promote products
14290001Sglebius *    derived from this software without specific prior written permission.
15290001Sglebius *
16290001Sglebius * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17290001Sglebius * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18290001Sglebius * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19290001Sglebius * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20290001Sglebius * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21290001Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22290001Sglebius * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23290001Sglebius * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24290001Sglebius * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25290001Sglebius * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26290001Sglebius */
27290001Sglebius
28290001Sglebius#include "event2/event-config.h"
29290001Sglebius#include "evconfig-private.h"
30290001Sglebius
31290001Sglebius#ifdef _WIN32
32290001Sglebius#include <winsock2.h>
33290001Sglebius#include <windows.h>
34290001Sglebius#include <io.h>
35290001Sglebius#endif
36290001Sglebius
37290001Sglebius#ifdef EVENT__HAVE_VASPRINTF
38290001Sglebius/* If we have vasprintf, we need to define _GNU_SOURCE before we include
39290001Sglebius * stdio.h.  This comes from evconfig-private.h.
40290001Sglebius */
41290001Sglebius#endif
42290001Sglebius
43290001Sglebius#include <sys/types.h>
44290001Sglebius
45290001Sglebius#ifdef EVENT__HAVE_SYS_TIME_H
46290001Sglebius#include <sys/time.h>
47290001Sglebius#endif
48290001Sglebius
49290001Sglebius#ifdef EVENT__HAVE_SYS_SOCKET_H
50290001Sglebius#include <sys/socket.h>
51290001Sglebius#endif
52290001Sglebius
53290001Sglebius#ifdef EVENT__HAVE_SYS_UIO_H
54290001Sglebius#include <sys/uio.h>
55290001Sglebius#endif
56290001Sglebius
57290001Sglebius#ifdef EVENT__HAVE_SYS_IOCTL_H
58290001Sglebius#include <sys/ioctl.h>
59290001Sglebius#endif
60290001Sglebius
61290001Sglebius#ifdef EVENT__HAVE_SYS_MMAN_H
62290001Sglebius#include <sys/mman.h>
63290001Sglebius#endif
64290001Sglebius
65290001Sglebius#ifdef EVENT__HAVE_SYS_SENDFILE_H
66290001Sglebius#include <sys/sendfile.h>
67290001Sglebius#endif
68290001Sglebius#ifdef EVENT__HAVE_SYS_STAT_H
69290001Sglebius#include <sys/stat.h>
70290001Sglebius#endif
71290001Sglebius
72290001Sglebius
73290001Sglebius#include <errno.h>
74290001Sglebius#include <stdio.h>
75290001Sglebius#include <stdlib.h>
76290001Sglebius#include <string.h>
77290001Sglebius#ifdef EVENT__HAVE_STDARG_H
78290001Sglebius#include <stdarg.h>
79290001Sglebius#endif
80290001Sglebius#ifdef EVENT__HAVE_UNISTD_H
81290001Sglebius#include <unistd.h>
82290001Sglebius#endif
83290001Sglebius#include <limits.h>
84290001Sglebius
85290001Sglebius#include "event2/event.h"
86290001Sglebius#include "event2/buffer.h"
87290001Sglebius#include "event2/buffer_compat.h"
88290001Sglebius#include "event2/bufferevent.h"
89290001Sglebius#include "event2/bufferevent_compat.h"
90290001Sglebius#include "event2/bufferevent_struct.h"
91290001Sglebius#include "event2/thread.h"
92290001Sglebius#include "log-internal.h"
93290001Sglebius#include "mm-internal.h"
94290001Sglebius#include "util-internal.h"
95290001Sglebius#include "evthread-internal.h"
96290001Sglebius#include "evbuffer-internal.h"
97290001Sglebius#include "bufferevent-internal.h"
98290001Sglebius
99290001Sglebius/* some systems do not have MAP_FAILED */
100290001Sglebius#ifndef MAP_FAILED
101290001Sglebius#define MAP_FAILED	((void *)-1)
102290001Sglebius#endif
103290001Sglebius
104290001Sglebius/* send file support */
105290001Sglebius#if defined(EVENT__HAVE_SYS_SENDFILE_H) && defined(EVENT__HAVE_SENDFILE) && defined(__linux__)
106290001Sglebius#define USE_SENDFILE		1
107290001Sglebius#define SENDFILE_IS_LINUX	1
108290001Sglebius#elif defined(EVENT__HAVE_SENDFILE) && defined(__FreeBSD__)
109290001Sglebius#define USE_SENDFILE		1
110290001Sglebius#define SENDFILE_IS_FREEBSD	1
111290001Sglebius#elif defined(EVENT__HAVE_SENDFILE) && defined(__APPLE__)
112290001Sglebius#define USE_SENDFILE		1
113290001Sglebius#define SENDFILE_IS_MACOSX	1
114290001Sglebius#elif defined(EVENT__HAVE_SENDFILE) && defined(__sun__) && defined(__svr4__)
115290001Sglebius#define USE_SENDFILE		1
116290001Sglebius#define SENDFILE_IS_SOLARIS	1
117290001Sglebius#endif
118290001Sglebius
119290001Sglebius/* Mask of user-selectable callback flags. */
120290001Sglebius#define EVBUFFER_CB_USER_FLAGS	    0xffff
121290001Sglebius/* Mask of all internal-use-only flags. */
122290001Sglebius#define EVBUFFER_CB_INTERNAL_FLAGS  0xffff0000
123290001Sglebius
124290001Sglebius/* Flag set if the callback is using the cb_obsolete function pointer  */
125290001Sglebius#define EVBUFFER_CB_OBSOLETE	       0x00040000
126290001Sglebius
127290001Sglebius/* evbuffer_chain support */
128290001Sglebius#define CHAIN_SPACE_PTR(ch) ((ch)->buffer + (ch)->misalign + (ch)->off)
129290001Sglebius#define CHAIN_SPACE_LEN(ch) ((ch)->flags & EVBUFFER_IMMUTABLE ? \
130290001Sglebius	    0 : (ch)->buffer_len - ((ch)->misalign + (ch)->off))
131290001Sglebius
132290001Sglebius#define CHAIN_PINNED(ch)  (((ch)->flags & EVBUFFER_MEM_PINNED_ANY) != 0)
133290001Sglebius#define CHAIN_PINNED_R(ch)  (((ch)->flags & EVBUFFER_MEM_PINNED_R) != 0)
134290001Sglebius
135290001Sglebius/* evbuffer_ptr support */
136290001Sglebius#define PTR_NOT_FOUND(ptr) do {			\
137290001Sglebius	(ptr)->pos = -1;					\
138290001Sglebius	(ptr)->internal_.chain = NULL;		\
139290001Sglebius	(ptr)->internal_.pos_in_chain = 0;	\
140290001Sglebius} while (0)
141290001Sglebius
142290001Sglebiusstatic void evbuffer_chain_align(struct evbuffer_chain *chain);
143290001Sglebiusstatic int evbuffer_chain_should_realign(struct evbuffer_chain *chain,
144290001Sglebius    size_t datalen);
145290001Sglebiusstatic void evbuffer_deferred_callback(struct event_callback *cb, void *arg);
146290001Sglebiusstatic int evbuffer_ptr_memcmp(const struct evbuffer *buf,
147290001Sglebius    const struct evbuffer_ptr *pos, const char *mem, size_t len);
148290001Sglebiusstatic struct evbuffer_chain *evbuffer_expand_singlechain(struct evbuffer *buf,
149290001Sglebius    size_t datlen);
150290001Sglebiusstatic int evbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
151290001Sglebius    size_t howfar);
152290001Sglebiusstatic int evbuffer_file_segment_materialize(struct evbuffer_file_segment *seg);
153290001Sglebiusstatic inline void evbuffer_chain_incref(struct evbuffer_chain *chain);
154290001Sglebius
155290001Sglebiusstatic struct evbuffer_chain *
156290001Sglebiusevbuffer_chain_new(size_t size)
157290001Sglebius{
158290001Sglebius	struct evbuffer_chain *chain;
159290001Sglebius	size_t to_alloc;
160290001Sglebius
161290001Sglebius	if (size > EVBUFFER_CHAIN_MAX - EVBUFFER_CHAIN_SIZE)
162290001Sglebius		return (NULL);
163290001Sglebius
164290001Sglebius	size += EVBUFFER_CHAIN_SIZE;
165290001Sglebius
166290001Sglebius	/* get the next largest memory that can hold the buffer */
167290001Sglebius	if (size < EVBUFFER_CHAIN_MAX / 2) {
168290001Sglebius		to_alloc = MIN_BUFFER_SIZE;
169290001Sglebius		while (to_alloc < size) {
170290001Sglebius			to_alloc <<= 1;
171290001Sglebius		}
172290001Sglebius	} else {
173290001Sglebius		to_alloc = size;
174290001Sglebius	}
175290001Sglebius
176290001Sglebius	/* we get everything in one chunk */
177290001Sglebius	if ((chain = mm_malloc(to_alloc)) == NULL)
178290001Sglebius		return (NULL);
179290001Sglebius
180290001Sglebius	memset(chain, 0, EVBUFFER_CHAIN_SIZE);
181290001Sglebius
182290001Sglebius	chain->buffer_len = to_alloc - EVBUFFER_CHAIN_SIZE;
183290001Sglebius
184290001Sglebius	/* this way we can manipulate the buffer to different addresses,
185290001Sglebius	 * which is required for mmap for example.
186290001Sglebius	 */
187290001Sglebius	chain->buffer = EVBUFFER_CHAIN_EXTRA(u_char, chain);
188290001Sglebius
189290001Sglebius	chain->refcnt = 1;
190290001Sglebius
191290001Sglebius	return (chain);
192290001Sglebius}
193290001Sglebius
194290001Sglebiusstatic inline void
195290001Sglebiusevbuffer_chain_free(struct evbuffer_chain *chain)
196290001Sglebius{
197290001Sglebius	EVUTIL_ASSERT(chain->refcnt > 0);
198290001Sglebius	if (--chain->refcnt > 0) {
199290001Sglebius		/* chain is still referenced by other chains */
200290001Sglebius		return;
201290001Sglebius	}
202290001Sglebius
203290001Sglebius	if (CHAIN_PINNED(chain)) {
204290001Sglebius		/* will get freed once no longer dangling */
205290001Sglebius		chain->refcnt++;
206290001Sglebius		chain->flags |= EVBUFFER_DANGLING;
207290001Sglebius		return;
208290001Sglebius	}
209290001Sglebius
210290001Sglebius	/* safe to release chain, it's either a referencing
211290001Sglebius	 * chain or all references to it have been freed */
212290001Sglebius	if (chain->flags & EVBUFFER_REFERENCE) {
213290001Sglebius		struct evbuffer_chain_reference *info =
214290001Sglebius		    EVBUFFER_CHAIN_EXTRA(
215290001Sglebius			    struct evbuffer_chain_reference,
216290001Sglebius			    chain);
217290001Sglebius		if (info->cleanupfn)
218290001Sglebius			(*info->cleanupfn)(chain->buffer,
219290001Sglebius			    chain->buffer_len,
220290001Sglebius			    info->extra);
221290001Sglebius	}
222290001Sglebius	if (chain->flags & EVBUFFER_FILESEGMENT) {
223290001Sglebius		struct evbuffer_chain_file_segment *info =
224290001Sglebius		    EVBUFFER_CHAIN_EXTRA(
225290001Sglebius			    struct evbuffer_chain_file_segment,
226290001Sglebius			    chain);
227290001Sglebius		if (info->segment) {
228290001Sglebius#ifdef _WIN32
229290001Sglebius			if (info->segment->is_mapping)
230290001Sglebius				UnmapViewOfFile(chain->buffer);
231290001Sglebius#endif
232290001Sglebius			evbuffer_file_segment_free(info->segment);
233290001Sglebius		}
234290001Sglebius	}
235290001Sglebius	if (chain->flags & EVBUFFER_MULTICAST) {
236290001Sglebius		struct evbuffer_multicast_parent *info =
237290001Sglebius		    EVBUFFER_CHAIN_EXTRA(
238290001Sglebius			    struct evbuffer_multicast_parent,
239290001Sglebius			    chain);
240290001Sglebius		/* referencing chain is being freed, decrease
241290001Sglebius		 * refcounts of source chain and associated
242290001Sglebius		 * evbuffer (which get freed once both reach
243290001Sglebius		 * zero) */
244290001Sglebius		EVUTIL_ASSERT(info->source != NULL);
245290001Sglebius		EVUTIL_ASSERT(info->parent != NULL);
246290001Sglebius		EVBUFFER_LOCK(info->source);
247290001Sglebius		evbuffer_chain_free(info->parent);
248290001Sglebius		evbuffer_decref_and_unlock_(info->source);
249290001Sglebius	}
250290001Sglebius
251290001Sglebius	mm_free(chain);
252290001Sglebius}
253290001Sglebius
254290001Sglebiusstatic void
255290001Sglebiusevbuffer_free_all_chains(struct evbuffer_chain *chain)
256290001Sglebius{
257290001Sglebius	struct evbuffer_chain *next;
258290001Sglebius	for (; chain; chain = next) {
259290001Sglebius		next = chain->next;
260290001Sglebius		evbuffer_chain_free(chain);
261290001Sglebius	}
262290001Sglebius}
263290001Sglebius
264290001Sglebius#ifndef NDEBUG
265290001Sglebiusstatic int
266290001Sglebiusevbuffer_chains_all_empty(struct evbuffer_chain *chain)
267290001Sglebius{
268290001Sglebius	for (; chain; chain = chain->next) {
269290001Sglebius		if (chain->off)
270290001Sglebius			return 0;
271290001Sglebius	}
272290001Sglebius	return 1;
273290001Sglebius}
274290001Sglebius#else
275290001Sglebius/* The definition is needed for EVUTIL_ASSERT, which uses sizeof to avoid
276290001Sglebius"unused variable" warnings. */
277290001Sglebiusstatic inline int evbuffer_chains_all_empty(struct evbuffer_chain *chain) {
278290001Sglebius	return 1;
279290001Sglebius}
280290001Sglebius#endif
281290001Sglebius
282290001Sglebius/* Free all trailing chains in 'buf' that are neither pinned nor empty, prior
283290001Sglebius * to replacing them all with a new chain.  Return a pointer to the place
284290001Sglebius * where the new chain will go.
285290001Sglebius *
286290001Sglebius * Internal; requires lock.  The caller must fix up buf->last and buf->first
287290001Sglebius * as needed; they might have been freed.
288290001Sglebius */
289290001Sglebiusstatic struct evbuffer_chain **
290290001Sglebiusevbuffer_free_trailing_empty_chains(struct evbuffer *buf)
291290001Sglebius{
292290001Sglebius	struct evbuffer_chain **ch = buf->last_with_datap;
293290001Sglebius	/* Find the first victim chain.  It might be *last_with_datap */
294290001Sglebius	while ((*ch) && ((*ch)->off != 0 || CHAIN_PINNED(*ch)))
295290001Sglebius		ch = &(*ch)->next;
296290001Sglebius	if (*ch) {
297290001Sglebius		EVUTIL_ASSERT(evbuffer_chains_all_empty(*ch));
298290001Sglebius		evbuffer_free_all_chains(*ch);
299290001Sglebius		*ch = NULL;
300290001Sglebius	}
301290001Sglebius	return ch;
302290001Sglebius}
303290001Sglebius
304290001Sglebius/* Add a single chain 'chain' to the end of 'buf', freeing trailing empty
305290001Sglebius * chains as necessary.  Requires lock.  Does not schedule callbacks.
306290001Sglebius */
307290001Sglebiusstatic void
308290001Sglebiusevbuffer_chain_insert(struct evbuffer *buf,
309290001Sglebius    struct evbuffer_chain *chain)
310290001Sglebius{
311290001Sglebius	ASSERT_EVBUFFER_LOCKED(buf);
312290001Sglebius	if (*buf->last_with_datap == NULL) {
313290001Sglebius		/* There are no chains data on the buffer at all. */
314290001Sglebius		EVUTIL_ASSERT(buf->last_with_datap == &buf->first);
315290001Sglebius		EVUTIL_ASSERT(buf->first == NULL);
316290001Sglebius		buf->first = buf->last = chain;
317290001Sglebius	} else {
318290001Sglebius		struct evbuffer_chain **chp;
319290001Sglebius		chp = evbuffer_free_trailing_empty_chains(buf);
320290001Sglebius		*chp = chain;
321290001Sglebius		if (chain->off)
322290001Sglebius			buf->last_with_datap = chp;
323290001Sglebius		buf->last = chain;
324290001Sglebius	}
325290001Sglebius	buf->total_len += chain->off;
326290001Sglebius}
327290001Sglebius
328290001Sglebiusstatic inline struct evbuffer_chain *
329290001Sglebiusevbuffer_chain_insert_new(struct evbuffer *buf, size_t datlen)
330290001Sglebius{
331290001Sglebius	struct evbuffer_chain *chain;
332290001Sglebius	if ((chain = evbuffer_chain_new(datlen)) == NULL)
333290001Sglebius		return NULL;
334290001Sglebius	evbuffer_chain_insert(buf, chain);
335290001Sglebius	return chain;
336290001Sglebius}
337290001Sglebius
338290001Sglebiusvoid
339290001Sglebiusevbuffer_chain_pin_(struct evbuffer_chain *chain, unsigned flag)
340290001Sglebius{
341290001Sglebius	EVUTIL_ASSERT((chain->flags & flag) == 0);
342290001Sglebius	chain->flags |= flag;
343290001Sglebius}
344290001Sglebius
345290001Sglebiusvoid
346290001Sglebiusevbuffer_chain_unpin_(struct evbuffer_chain *chain, unsigned flag)
347290001Sglebius{
348290001Sglebius	EVUTIL_ASSERT((chain->flags & flag) != 0);
349290001Sglebius	chain->flags &= ~flag;
350290001Sglebius	if (chain->flags & EVBUFFER_DANGLING)
351290001Sglebius		evbuffer_chain_free(chain);
352290001Sglebius}
353290001Sglebius
354290001Sglebiusstatic inline void
355290001Sglebiusevbuffer_chain_incref(struct evbuffer_chain *chain)
356290001Sglebius{
357290001Sglebius    ++chain->refcnt;
358290001Sglebius}
359290001Sglebius
360290001Sglebiusstruct evbuffer *
361290001Sglebiusevbuffer_new(void)
362290001Sglebius{
363290001Sglebius	struct evbuffer *buffer;
364290001Sglebius
365290001Sglebius	buffer = mm_calloc(1, sizeof(struct evbuffer));
366290001Sglebius	if (buffer == NULL)
367290001Sglebius		return (NULL);
368290001Sglebius
369290001Sglebius	LIST_INIT(&buffer->callbacks);
370290001Sglebius	buffer->refcnt = 1;
371290001Sglebius	buffer->last_with_datap = &buffer->first;
372290001Sglebius
373290001Sglebius	return (buffer);
374290001Sglebius}
375290001Sglebius
376290001Sglebiusint
377290001Sglebiusevbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags)
378290001Sglebius{
379290001Sglebius	EVBUFFER_LOCK(buf);
380290001Sglebius	buf->flags |= (ev_uint32_t)flags;
381290001Sglebius	EVBUFFER_UNLOCK(buf);
382290001Sglebius	return 0;
383290001Sglebius}
384290001Sglebius
385290001Sglebiusint
386290001Sglebiusevbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags)
387290001Sglebius{
388290001Sglebius	EVBUFFER_LOCK(buf);
389290001Sglebius	buf->flags &= ~(ev_uint32_t)flags;
390290001Sglebius	EVBUFFER_UNLOCK(buf);
391290001Sglebius	return 0;
392290001Sglebius}
393290001Sglebius
394290001Sglebiusvoid
395290001Sglebiusevbuffer_incref_(struct evbuffer *buf)
396290001Sglebius{
397290001Sglebius	EVBUFFER_LOCK(buf);
398290001Sglebius	++buf->refcnt;
399290001Sglebius	EVBUFFER_UNLOCK(buf);
400290001Sglebius}
401290001Sglebius
402290001Sglebiusvoid
403290001Sglebiusevbuffer_incref_and_lock_(struct evbuffer *buf)
404290001Sglebius{
405290001Sglebius	EVBUFFER_LOCK(buf);
406290001Sglebius	++buf->refcnt;
407290001Sglebius}
408290001Sglebius
409290001Sglebiusint
410290001Sglebiusevbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base)
411290001Sglebius{
412290001Sglebius	EVBUFFER_LOCK(buffer);
413290001Sglebius	buffer->cb_queue = base;
414290001Sglebius	buffer->deferred_cbs = 1;
415290001Sglebius	event_deferred_cb_init_(&buffer->deferred,
416290001Sglebius	    event_base_get_npriorities(base) / 2,
417290001Sglebius	    evbuffer_deferred_callback, buffer);
418290001Sglebius	EVBUFFER_UNLOCK(buffer);
419290001Sglebius	return 0;
420290001Sglebius}
421290001Sglebius
422290001Sglebiusint
423290001Sglebiusevbuffer_enable_locking(struct evbuffer *buf, void *lock)
424290001Sglebius{
425290001Sglebius#ifdef EVENT__DISABLE_THREAD_SUPPORT
426290001Sglebius	return -1;
427290001Sglebius#else
428290001Sglebius	if (buf->lock)
429290001Sglebius		return -1;
430290001Sglebius
431290001Sglebius	if (!lock) {
432290001Sglebius		EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
433290001Sglebius		if (!lock)
434290001Sglebius			return -1;
435290001Sglebius		buf->lock = lock;
436290001Sglebius		buf->own_lock = 1;
437290001Sglebius	} else {
438290001Sglebius		buf->lock = lock;
439290001Sglebius		buf->own_lock = 0;
440290001Sglebius	}
441290001Sglebius
442290001Sglebius	return 0;
443290001Sglebius#endif
444290001Sglebius}
445290001Sglebius
446290001Sglebiusvoid
447290001Sglebiusevbuffer_set_parent_(struct evbuffer *buf, struct bufferevent *bev)
448290001Sglebius{
449290001Sglebius	EVBUFFER_LOCK(buf);
450290001Sglebius	buf->parent = bev;
451290001Sglebius	EVBUFFER_UNLOCK(buf);
452290001Sglebius}
453290001Sglebius
454290001Sglebiusstatic void
455290001Sglebiusevbuffer_run_callbacks(struct evbuffer *buffer, int running_deferred)
456290001Sglebius{
457290001Sglebius	struct evbuffer_cb_entry *cbent, *next;
458290001Sglebius	struct evbuffer_cb_info info;
459290001Sglebius	size_t new_size;
460290001Sglebius	ev_uint32_t mask, masked_val;
461290001Sglebius	int clear = 1;
462290001Sglebius
463290001Sglebius	if (running_deferred) {
464290001Sglebius		mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
465290001Sglebius		masked_val = EVBUFFER_CB_ENABLED;
466290001Sglebius	} else if (buffer->deferred_cbs) {
467290001Sglebius		mask = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
468290001Sglebius		masked_val = EVBUFFER_CB_NODEFER|EVBUFFER_CB_ENABLED;
469290001Sglebius		/* Don't zero-out n_add/n_del, since the deferred callbacks
470290001Sglebius		   will want to see them. */
471290001Sglebius		clear = 0;
472290001Sglebius	} else {
473290001Sglebius		mask = EVBUFFER_CB_ENABLED;
474290001Sglebius		masked_val = EVBUFFER_CB_ENABLED;
475290001Sglebius	}
476290001Sglebius
477290001Sglebius	ASSERT_EVBUFFER_LOCKED(buffer);
478290001Sglebius
479290001Sglebius	if (LIST_EMPTY(&buffer->callbacks)) {
480290001Sglebius		buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
481290001Sglebius		return;
482290001Sglebius	}
483290001Sglebius	if (buffer->n_add_for_cb == 0 && buffer->n_del_for_cb == 0)
484290001Sglebius		return;
485290001Sglebius
486290001Sglebius	new_size = buffer->total_len;
487290001Sglebius	info.orig_size = new_size + buffer->n_del_for_cb - buffer->n_add_for_cb;
488290001Sglebius	info.n_added = buffer->n_add_for_cb;
489290001Sglebius	info.n_deleted = buffer->n_del_for_cb;
490290001Sglebius	if (clear) {
491290001Sglebius		buffer->n_add_for_cb = 0;
492290001Sglebius		buffer->n_del_for_cb = 0;
493290001Sglebius	}
494290001Sglebius	for (cbent = LIST_FIRST(&buffer->callbacks);
495290001Sglebius	     cbent != LIST_END(&buffer->callbacks);
496290001Sglebius	     cbent = next) {
497290001Sglebius		/* Get the 'next' pointer now in case this callback decides
498290001Sglebius		 * to remove itself or something. */
499290001Sglebius		next = LIST_NEXT(cbent, next);
500290001Sglebius
501290001Sglebius		if ((cbent->flags & mask) != masked_val)
502290001Sglebius			continue;
503290001Sglebius
504290001Sglebius		if ((cbent->flags & EVBUFFER_CB_OBSOLETE))
505290001Sglebius			cbent->cb.cb_obsolete(buffer,
506290001Sglebius			    info.orig_size, new_size, cbent->cbarg);
507290001Sglebius		else
508290001Sglebius			cbent->cb.cb_func(buffer, &info, cbent->cbarg);
509290001Sglebius	}
510290001Sglebius}
511290001Sglebius
512290001Sglebiusvoid
513290001Sglebiusevbuffer_invoke_callbacks_(struct evbuffer *buffer)
514290001Sglebius{
515290001Sglebius	if (LIST_EMPTY(&buffer->callbacks)) {
516290001Sglebius		buffer->n_add_for_cb = buffer->n_del_for_cb = 0;
517290001Sglebius		return;
518290001Sglebius	}
519290001Sglebius
520290001Sglebius	if (buffer->deferred_cbs) {
521290001Sglebius		if (event_deferred_cb_schedule_(buffer->cb_queue, &buffer->deferred)) {
522290001Sglebius			evbuffer_incref_and_lock_(buffer);
523290001Sglebius			if (buffer->parent)
524290001Sglebius				bufferevent_incref_(buffer->parent);
525290001Sglebius		}
526290001Sglebius		EVBUFFER_UNLOCK(buffer);
527290001Sglebius	}
528290001Sglebius
529290001Sglebius	evbuffer_run_callbacks(buffer, 0);
530290001Sglebius}
531290001Sglebius
532290001Sglebiusstatic void
533290001Sglebiusevbuffer_deferred_callback(struct event_callback *cb, void *arg)
534290001Sglebius{
535290001Sglebius	struct bufferevent *parent = NULL;
536290001Sglebius	struct evbuffer *buffer = arg;
537290001Sglebius
538290001Sglebius	/* XXXX It would be better to run these callbacks without holding the
539290001Sglebius	 * lock */
540290001Sglebius	EVBUFFER_LOCK(buffer);
541290001Sglebius	parent = buffer->parent;
542290001Sglebius	evbuffer_run_callbacks(buffer, 1);
543290001Sglebius	evbuffer_decref_and_unlock_(buffer);
544290001Sglebius	if (parent)
545290001Sglebius		bufferevent_decref_(parent);
546290001Sglebius}
547290001Sglebius
548290001Sglebiusstatic void
549290001Sglebiusevbuffer_remove_all_callbacks(struct evbuffer *buffer)
550290001Sglebius{
551290001Sglebius	struct evbuffer_cb_entry *cbent;
552290001Sglebius
553290001Sglebius	while ((cbent = LIST_FIRST(&buffer->callbacks))) {
554290001Sglebius		LIST_REMOVE(cbent, next);
555290001Sglebius		mm_free(cbent);
556290001Sglebius	}
557290001Sglebius}
558290001Sglebius
559290001Sglebiusvoid
560290001Sglebiusevbuffer_decref_and_unlock_(struct evbuffer *buffer)
561290001Sglebius{
562290001Sglebius	struct evbuffer_chain *chain, *next;
563290001Sglebius	ASSERT_EVBUFFER_LOCKED(buffer);
564290001Sglebius
565290001Sglebius	EVUTIL_ASSERT(buffer->refcnt > 0);
566290001Sglebius
567290001Sglebius	if (--buffer->refcnt > 0) {
568290001Sglebius		EVBUFFER_UNLOCK(buffer);
569290001Sglebius		return;
570290001Sglebius	}
571290001Sglebius
572290001Sglebius	for (chain = buffer->first; chain != NULL; chain = next) {
573290001Sglebius		next = chain->next;
574290001Sglebius		evbuffer_chain_free(chain);
575290001Sglebius	}
576290001Sglebius	evbuffer_remove_all_callbacks(buffer);
577290001Sglebius	if (buffer->deferred_cbs)
578290001Sglebius		event_deferred_cb_cancel_(buffer->cb_queue, &buffer->deferred);
579290001Sglebius
580290001Sglebius	EVBUFFER_UNLOCK(buffer);
581290001Sglebius	if (buffer->own_lock)
582290001Sglebius		EVTHREAD_FREE_LOCK(buffer->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
583290001Sglebius	mm_free(buffer);
584290001Sglebius}
585290001Sglebius
586290001Sglebiusvoid
587290001Sglebiusevbuffer_free(struct evbuffer *buffer)
588290001Sglebius{
589290001Sglebius	EVBUFFER_LOCK(buffer);
590290001Sglebius	evbuffer_decref_and_unlock_(buffer);
591290001Sglebius}
592290001Sglebius
593290001Sglebiusvoid
594290001Sglebiusevbuffer_lock(struct evbuffer *buf)
595290001Sglebius{
596290001Sglebius	EVBUFFER_LOCK(buf);
597290001Sglebius}
598290001Sglebius
599290001Sglebiusvoid
600290001Sglebiusevbuffer_unlock(struct evbuffer *buf)
601290001Sglebius{
602290001Sglebius	EVBUFFER_UNLOCK(buf);
603290001Sglebius}
604290001Sglebius
605290001Sglebiussize_t
606290001Sglebiusevbuffer_get_length(const struct evbuffer *buffer)
607290001Sglebius{
608290001Sglebius	size_t result;
609290001Sglebius
610290001Sglebius	EVBUFFER_LOCK(buffer);
611290001Sglebius
612290001Sglebius	result = (buffer->total_len);
613290001Sglebius
614290001Sglebius	EVBUFFER_UNLOCK(buffer);
615290001Sglebius
616290001Sglebius	return result;
617290001Sglebius}
618290001Sglebius
619290001Sglebiussize_t
620290001Sglebiusevbuffer_get_contiguous_space(const struct evbuffer *buf)
621290001Sglebius{
622290001Sglebius	struct evbuffer_chain *chain;
623290001Sglebius	size_t result;
624290001Sglebius
625290001Sglebius	EVBUFFER_LOCK(buf);
626290001Sglebius	chain = buf->first;
627290001Sglebius	result = (chain != NULL ? chain->off : 0);
628290001Sglebius	EVBUFFER_UNLOCK(buf);
629290001Sglebius
630290001Sglebius	return result;
631290001Sglebius}
632290001Sglebius
633290001Sglebiussize_t
634290001Sglebiusevbuffer_add_iovec(struct evbuffer * buf, struct evbuffer_iovec * vec, int n_vec) {
635290001Sglebius	int n;
636290001Sglebius	size_t res;
637290001Sglebius	size_t to_alloc;
638290001Sglebius
639290001Sglebius	EVBUFFER_LOCK(buf);
640290001Sglebius
641290001Sglebius	res = to_alloc = 0;
642290001Sglebius
643290001Sglebius	for (n = 0; n < n_vec; n++) {
644290001Sglebius		to_alloc += vec[n].iov_len;
645290001Sglebius	}
646290001Sglebius
647290001Sglebius	if (evbuffer_expand_fast_(buf, to_alloc, 2) < 0) {
648290001Sglebius		goto done;
649290001Sglebius	}
650290001Sglebius
651290001Sglebius	for (n = 0; n < n_vec; n++) {
652290001Sglebius		/* XXX each 'add' call here does a bunch of setup that's
653290001Sglebius		 * obviated by evbuffer_expand_fast_, and some cleanup that we
654290001Sglebius		 * would like to do only once.  Instead we should just extract
655290001Sglebius		 * the part of the code that's needed. */
656290001Sglebius
657290001Sglebius		if (evbuffer_add(buf, vec[n].iov_base, vec[n].iov_len) < 0) {
658290001Sglebius			goto done;
659290001Sglebius		}
660290001Sglebius
661290001Sglebius		res += vec[n].iov_len;
662290001Sglebius	}
663290001Sglebius
664290001Sglebiusdone:
665290001Sglebius    EVBUFFER_UNLOCK(buf);
666290001Sglebius    return res;
667290001Sglebius}
668290001Sglebius
669290001Sglebiusint
670290001Sglebiusevbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
671290001Sglebius    struct evbuffer_iovec *vec, int n_vecs)
672290001Sglebius{
673290001Sglebius	struct evbuffer_chain *chain, **chainp;
674290001Sglebius	int n = -1;
675290001Sglebius
676290001Sglebius	EVBUFFER_LOCK(buf);
677290001Sglebius	if (buf->freeze_end)
678290001Sglebius		goto done;
679290001Sglebius	if (n_vecs < 1)
680290001Sglebius		goto done;
681290001Sglebius	if (n_vecs == 1) {
682290001Sglebius		if ((chain = evbuffer_expand_singlechain(buf, size)) == NULL)
683290001Sglebius			goto done;
684290001Sglebius
685290001Sglebius		vec[0].iov_base = CHAIN_SPACE_PTR(chain);
686290001Sglebius		vec[0].iov_len = (size_t) CHAIN_SPACE_LEN(chain);
687290001Sglebius		EVUTIL_ASSERT(size<0 || (size_t)vec[0].iov_len >= (size_t)size);
688290001Sglebius		n = 1;
689290001Sglebius	} else {
690290001Sglebius		if (evbuffer_expand_fast_(buf, size, n_vecs)<0)
691290001Sglebius			goto done;
692290001Sglebius		n = evbuffer_read_setup_vecs_(buf, size, vec, n_vecs,
693290001Sglebius				&chainp, 0);
694290001Sglebius	}
695290001Sglebius
696290001Sglebiusdone:
697290001Sglebius	EVBUFFER_UNLOCK(buf);
698290001Sglebius	return n;
699290001Sglebius
700290001Sglebius}
701290001Sglebius
702290001Sglebiusstatic int
703290001Sglebiusadvance_last_with_data(struct evbuffer *buf)
704290001Sglebius{
705290001Sglebius	int n = 0;
706290001Sglebius	ASSERT_EVBUFFER_LOCKED(buf);
707290001Sglebius
708290001Sglebius	if (!*buf->last_with_datap)
709290001Sglebius		return 0;
710290001Sglebius
711290001Sglebius	while ((*buf->last_with_datap)->next && (*buf->last_with_datap)->next->off) {
712290001Sglebius		buf->last_with_datap = &(*buf->last_with_datap)->next;
713290001Sglebius		++n;
714290001Sglebius	}
715290001Sglebius	return n;
716290001Sglebius}
717290001Sglebius
718290001Sglebiusint
719290001Sglebiusevbuffer_commit_space(struct evbuffer *buf,
720290001Sglebius    struct evbuffer_iovec *vec, int n_vecs)
721290001Sglebius{
722290001Sglebius	struct evbuffer_chain *chain, **firstchainp, **chainp;
723290001Sglebius	int result = -1;
724290001Sglebius	size_t added = 0;
725290001Sglebius	int i;
726290001Sglebius
727290001Sglebius	EVBUFFER_LOCK(buf);
728290001Sglebius
729290001Sglebius	if (buf->freeze_end)
730290001Sglebius		goto done;
731290001Sglebius	if (n_vecs == 0) {
732290001Sglebius		result = 0;
733290001Sglebius		goto done;
734290001Sglebius	} else if (n_vecs == 1 &&
735290001Sglebius	    (buf->last && vec[0].iov_base == (void*)CHAIN_SPACE_PTR(buf->last))) {
736290001Sglebius		/* The user only got or used one chain; it might not
737290001Sglebius		 * be the first one with space in it. */
738290001Sglebius		if ((size_t)vec[0].iov_len > (size_t)CHAIN_SPACE_LEN(buf->last))
739290001Sglebius			goto done;
740290001Sglebius		buf->last->off += vec[0].iov_len;
741290001Sglebius		added = vec[0].iov_len;
742290001Sglebius		if (added)
743290001Sglebius			advance_last_with_data(buf);
744290001Sglebius		goto okay;
745290001Sglebius	}
746290001Sglebius
747290001Sglebius	/* Advance 'firstchain' to the first chain with space in it. */
748290001Sglebius	firstchainp = buf->last_with_datap;
749290001Sglebius	if (!*firstchainp)
750290001Sglebius		goto done;
751290001Sglebius	if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
752290001Sglebius		firstchainp = &(*firstchainp)->next;
753290001Sglebius	}
754290001Sglebius
755290001Sglebius	chain = *firstchainp;
756290001Sglebius	/* pass 1: make sure that the pointers and lengths of vecs[] are in
757290001Sglebius	 * bounds before we try to commit anything. */
758290001Sglebius	for (i=0; i<n_vecs; ++i) {
759290001Sglebius		if (!chain)
760290001Sglebius			goto done;
761290001Sglebius		if (vec[i].iov_base != (void*)CHAIN_SPACE_PTR(chain) ||
762290001Sglebius		    (size_t)vec[i].iov_len > CHAIN_SPACE_LEN(chain))
763290001Sglebius			goto done;
764290001Sglebius		chain = chain->next;
765290001Sglebius	}
766290001Sglebius	/* pass 2: actually adjust all the chains. */
767290001Sglebius	chainp = firstchainp;
768290001Sglebius	for (i=0; i<n_vecs; ++i) {
769290001Sglebius		(*chainp)->off += vec[i].iov_len;
770290001Sglebius		added += vec[i].iov_len;
771290001Sglebius		if (vec[i].iov_len) {
772290001Sglebius			buf->last_with_datap = chainp;
773290001Sglebius		}
774290001Sglebius		chainp = &(*chainp)->next;
775290001Sglebius	}
776290001Sglebius
777290001Sglebiusokay:
778290001Sglebius	buf->total_len += added;
779290001Sglebius	buf->n_add_for_cb += added;
780290001Sglebius	result = 0;
781290001Sglebius	evbuffer_invoke_callbacks_(buf);
782290001Sglebius
783290001Sglebiusdone:
784290001Sglebius	EVBUFFER_UNLOCK(buf);
785290001Sglebius	return result;
786290001Sglebius}
787290001Sglebius
788290001Sglebiusstatic inline int
789290001SglebiusHAS_PINNED_R(struct evbuffer *buf)
790290001Sglebius{
791290001Sglebius	return (buf->last && CHAIN_PINNED_R(buf->last));
792290001Sglebius}
793290001Sglebius
794290001Sglebiusstatic inline void
795290001SglebiusZERO_CHAIN(struct evbuffer *dst)
796290001Sglebius{
797290001Sglebius	ASSERT_EVBUFFER_LOCKED(dst);
798290001Sglebius	dst->first = NULL;
799290001Sglebius	dst->last = NULL;
800290001Sglebius	dst->last_with_datap = &(dst)->first;
801290001Sglebius	dst->total_len = 0;
802290001Sglebius}
803290001Sglebius
804290001Sglebius/* Prepares the contents of src to be moved to another buffer by removing
805290001Sglebius * read-pinned chains. The first pinned chain is saved in first, and the
806290001Sglebius * last in last. If src has no read-pinned chains, first and last are set
807290001Sglebius * to NULL. */
808290001Sglebiusstatic int
809290001SglebiusPRESERVE_PINNED(struct evbuffer *src, struct evbuffer_chain **first,
810290001Sglebius		struct evbuffer_chain **last)
811290001Sglebius{
812290001Sglebius	struct evbuffer_chain *chain, **pinned;
813290001Sglebius
814290001Sglebius	ASSERT_EVBUFFER_LOCKED(src);
815290001Sglebius
816290001Sglebius	if (!HAS_PINNED_R(src)) {
817290001Sglebius		*first = *last = NULL;
818290001Sglebius		return 0;
819290001Sglebius	}
820290001Sglebius
821290001Sglebius	pinned = src->last_with_datap;
822290001Sglebius	if (!CHAIN_PINNED_R(*pinned))
823290001Sglebius		pinned = &(*pinned)->next;
824290001Sglebius	EVUTIL_ASSERT(CHAIN_PINNED_R(*pinned));
825290001Sglebius	chain = *first = *pinned;
826290001Sglebius	*last = src->last;
827290001Sglebius
828290001Sglebius	/* If there's data in the first pinned chain, we need to allocate
829290001Sglebius	 * a new chain and copy the data over. */
830290001Sglebius	if (chain->off) {
831290001Sglebius		struct evbuffer_chain *tmp;
832290001Sglebius
833290001Sglebius		EVUTIL_ASSERT(pinned == src->last_with_datap);
834290001Sglebius		tmp = evbuffer_chain_new(chain->off);
835290001Sglebius		if (!tmp)
836290001Sglebius			return -1;
837290001Sglebius		memcpy(tmp->buffer, chain->buffer + chain->misalign,
838290001Sglebius			chain->off);
839290001Sglebius		tmp->off = chain->off;
840290001Sglebius		*src->last_with_datap = tmp;
841290001Sglebius		src->last = tmp;
842290001Sglebius		chain->misalign += chain->off;
843290001Sglebius		chain->off = 0;
844290001Sglebius	} else {
845290001Sglebius		src->last = *src->last_with_datap;
846290001Sglebius		*pinned = NULL;
847290001Sglebius	}
848290001Sglebius
849290001Sglebius	return 0;
850290001Sglebius}
851290001Sglebius
852290001Sglebiusstatic inline void
853290001SglebiusRESTORE_PINNED(struct evbuffer *src, struct evbuffer_chain *pinned,
854290001Sglebius		struct evbuffer_chain *last)
855290001Sglebius{
856290001Sglebius	ASSERT_EVBUFFER_LOCKED(src);
857290001Sglebius
858290001Sglebius	if (!pinned) {
859290001Sglebius		ZERO_CHAIN(src);
860290001Sglebius		return;
861290001Sglebius	}
862290001Sglebius
863290001Sglebius	src->first = pinned;
864290001Sglebius	src->last = last;
865290001Sglebius	src->last_with_datap = &src->first;
866290001Sglebius	src->total_len = 0;
867290001Sglebius}
868290001Sglebius
869290001Sglebiusstatic inline void
870290001SglebiusCOPY_CHAIN(struct evbuffer *dst, struct evbuffer *src)
871290001Sglebius{
872290001Sglebius	ASSERT_EVBUFFER_LOCKED(dst);
873290001Sglebius	ASSERT_EVBUFFER_LOCKED(src);
874290001Sglebius	dst->first = src->first;
875290001Sglebius	if (src->last_with_datap == &src->first)
876290001Sglebius		dst->last_with_datap = &dst->first;
877290001Sglebius	else
878290001Sglebius		dst->last_with_datap = src->last_with_datap;
879290001Sglebius	dst->last = src->last;
880290001Sglebius	dst->total_len = src->total_len;
881290001Sglebius}
882290001Sglebius
883290001Sglebiusstatic void
884290001SglebiusAPPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
885290001Sglebius{
886290001Sglebius	ASSERT_EVBUFFER_LOCKED(dst);
887290001Sglebius	ASSERT_EVBUFFER_LOCKED(src);
888290001Sglebius	dst->last->next = src->first;
889290001Sglebius	if (src->last_with_datap == &src->first)
890290001Sglebius		dst->last_with_datap = &dst->last->next;
891290001Sglebius	else
892290001Sglebius		dst->last_with_datap = src->last_with_datap;
893290001Sglebius	dst->last = src->last;
894290001Sglebius	dst->total_len += src->total_len;
895290001Sglebius}
896290001Sglebius
897290001Sglebiusstatic inline void
898290001SglebiusAPPEND_CHAIN_MULTICAST(struct evbuffer *dst, struct evbuffer *src)
899290001Sglebius{
900290001Sglebius	struct evbuffer_chain *tmp;
901290001Sglebius	struct evbuffer_chain *chain = src->first;
902290001Sglebius	struct evbuffer_multicast_parent *extra;
903290001Sglebius
904290001Sglebius	ASSERT_EVBUFFER_LOCKED(dst);
905290001Sglebius	ASSERT_EVBUFFER_LOCKED(src);
906290001Sglebius
907290001Sglebius	for (; chain; chain = chain->next) {
908290001Sglebius		if (!chain->off || chain->flags & EVBUFFER_DANGLING) {
909290001Sglebius			/* skip empty chains */
910290001Sglebius			continue;
911290001Sglebius		}
912290001Sglebius
913290001Sglebius		tmp = evbuffer_chain_new(sizeof(struct evbuffer_multicast_parent));
914290001Sglebius		if (!tmp) {
915290001Sglebius			event_warn("%s: out of memory", __func__);
916290001Sglebius			return;
917290001Sglebius		}
918290001Sglebius		extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_multicast_parent, tmp);
919290001Sglebius		/* reference evbuffer containing source chain so it
920290001Sglebius		 * doesn't get released while the chain is still
921290001Sglebius		 * being referenced to */
922290001Sglebius		evbuffer_incref_(src);
923290001Sglebius		extra->source = src;
924290001Sglebius		/* reference source chain which now becomes immutable */
925290001Sglebius		evbuffer_chain_incref(chain);
926290001Sglebius		extra->parent = chain;
927290001Sglebius		chain->flags |= EVBUFFER_IMMUTABLE;
928290001Sglebius		tmp->buffer_len = chain->buffer_len;
929290001Sglebius		tmp->misalign = chain->misalign;
930290001Sglebius		tmp->off = chain->off;
931290001Sglebius		tmp->flags |= EVBUFFER_MULTICAST|EVBUFFER_IMMUTABLE;
932290001Sglebius		tmp->buffer = chain->buffer;
933290001Sglebius		evbuffer_chain_insert(dst, tmp);
934290001Sglebius	}
935290001Sglebius}
936290001Sglebius
937290001Sglebiusstatic void
938290001SglebiusPREPEND_CHAIN(struct evbuffer *dst, struct evbuffer *src)
939290001Sglebius{
940290001Sglebius	ASSERT_EVBUFFER_LOCKED(dst);
941290001Sglebius	ASSERT_EVBUFFER_LOCKED(src);
942290001Sglebius	src->last->next = dst->first;
943290001Sglebius	dst->first = src->first;
944290001Sglebius	dst->total_len += src->total_len;
945290001Sglebius	if (*dst->last_with_datap == NULL) {
946290001Sglebius		if (src->last_with_datap == &(src)->first)
947290001Sglebius			dst->last_with_datap = &dst->first;
948290001Sglebius		else
949290001Sglebius			dst->last_with_datap = src->last_with_datap;
950290001Sglebius	} else if (dst->last_with_datap == &dst->first) {
951290001Sglebius		dst->last_with_datap = &src->last->next;
952290001Sglebius	}
953290001Sglebius}
954290001Sglebius
955290001Sglebiusint
956290001Sglebiusevbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
957290001Sglebius{
958290001Sglebius	struct evbuffer_chain *pinned, *last;
959290001Sglebius	size_t in_total_len, out_total_len;
960290001Sglebius	int result = 0;
961290001Sglebius
962290001Sglebius	EVBUFFER_LOCK2(inbuf, outbuf);
963290001Sglebius	in_total_len = inbuf->total_len;
964290001Sglebius	out_total_len = outbuf->total_len;
965290001Sglebius
966290001Sglebius	if (in_total_len == 0 || outbuf == inbuf)
967290001Sglebius		goto done;
968290001Sglebius
969290001Sglebius	if (outbuf->freeze_end || inbuf->freeze_start) {
970290001Sglebius		result = -1;
971290001Sglebius		goto done;
972290001Sglebius	}
973290001Sglebius
974290001Sglebius	if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) {
975290001Sglebius		result = -1;
976290001Sglebius		goto done;
977290001Sglebius	}
978290001Sglebius
979290001Sglebius	if (out_total_len == 0) {
980290001Sglebius		/* There might be an empty chain at the start of outbuf; free
981290001Sglebius		 * it. */
982290001Sglebius		evbuffer_free_all_chains(outbuf->first);
983290001Sglebius		COPY_CHAIN(outbuf, inbuf);
984290001Sglebius	} else {
985290001Sglebius		APPEND_CHAIN(outbuf, inbuf);
986290001Sglebius	}
987290001Sglebius
988290001Sglebius	RESTORE_PINNED(inbuf, pinned, last);
989290001Sglebius
990290001Sglebius	inbuf->n_del_for_cb += in_total_len;
991290001Sglebius	outbuf->n_add_for_cb += in_total_len;
992290001Sglebius
993290001Sglebius	evbuffer_invoke_callbacks_(inbuf);
994290001Sglebius	evbuffer_invoke_callbacks_(outbuf);
995290001Sglebius
996290001Sglebiusdone:
997290001Sglebius	EVBUFFER_UNLOCK2(inbuf, outbuf);
998290001Sglebius	return result;
999290001Sglebius}
1000290001Sglebius
1001290001Sglebiusint
1002290001Sglebiusevbuffer_add_buffer_reference(struct evbuffer *outbuf, struct evbuffer *inbuf)
1003290001Sglebius{
1004290001Sglebius	size_t in_total_len, out_total_len;
1005290001Sglebius	struct evbuffer_chain *chain;
1006290001Sglebius	int result = 0;
1007290001Sglebius
1008290001Sglebius	EVBUFFER_LOCK2(inbuf, outbuf);
1009290001Sglebius	in_total_len = inbuf->total_len;
1010290001Sglebius	out_total_len = outbuf->total_len;
1011290001Sglebius	chain = inbuf->first;
1012290001Sglebius
1013290001Sglebius	if (in_total_len == 0)
1014290001Sglebius		goto done;
1015290001Sglebius
1016290001Sglebius	if (outbuf->freeze_end || outbuf == inbuf) {
1017290001Sglebius		result = -1;
1018290001Sglebius		goto done;
1019290001Sglebius	}
1020290001Sglebius
1021290001Sglebius	for (; chain; chain = chain->next) {
1022290001Sglebius		if ((chain->flags & (EVBUFFER_FILESEGMENT|EVBUFFER_SENDFILE|EVBUFFER_MULTICAST)) != 0) {
1023290001Sglebius			/* chain type can not be referenced */
1024290001Sglebius			result = -1;
1025290001Sglebius			goto done;
1026290001Sglebius		}
1027290001Sglebius	}
1028290001Sglebius
1029290001Sglebius	if (out_total_len == 0) {
1030290001Sglebius		/* There might be an empty chain at the start of outbuf; free
1031290001Sglebius		 * it. */
1032290001Sglebius		evbuffer_free_all_chains(outbuf->first);
1033290001Sglebius	}
1034290001Sglebius	APPEND_CHAIN_MULTICAST(outbuf, inbuf);
1035290001Sglebius
1036290001Sglebius	outbuf->n_add_for_cb += in_total_len;
1037290001Sglebius	evbuffer_invoke_callbacks_(outbuf);
1038290001Sglebius
1039290001Sglebiusdone:
1040290001Sglebius	EVBUFFER_UNLOCK2(inbuf, outbuf);
1041290001Sglebius	return result;
1042290001Sglebius}
1043290001Sglebius
1044290001Sglebiusint
1045290001Sglebiusevbuffer_prepend_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf)
1046290001Sglebius{
1047290001Sglebius	struct evbuffer_chain *pinned, *last;
1048290001Sglebius	size_t in_total_len, out_total_len;
1049290001Sglebius	int result = 0;
1050290001Sglebius
1051290001Sglebius	EVBUFFER_LOCK2(inbuf, outbuf);
1052290001Sglebius
1053290001Sglebius	in_total_len = inbuf->total_len;
1054290001Sglebius	out_total_len = outbuf->total_len;
1055290001Sglebius
1056290001Sglebius	if (!in_total_len || inbuf == outbuf)
1057290001Sglebius		goto done;
1058290001Sglebius
1059290001Sglebius	if (outbuf->freeze_start || inbuf->freeze_start) {
1060290001Sglebius		result = -1;
1061290001Sglebius		goto done;
1062290001Sglebius	}
1063290001Sglebius
1064290001Sglebius	if (PRESERVE_PINNED(inbuf, &pinned, &last) < 0) {
1065290001Sglebius		result = -1;
1066290001Sglebius		goto done;
1067290001Sglebius	}
1068290001Sglebius
1069290001Sglebius	if (out_total_len == 0) {
1070290001Sglebius		/* There might be an empty chain at the start of outbuf; free
1071290001Sglebius		 * it. */
1072290001Sglebius		evbuffer_free_all_chains(outbuf->first);
1073290001Sglebius		COPY_CHAIN(outbuf, inbuf);
1074290001Sglebius	} else {
1075290001Sglebius		PREPEND_CHAIN(outbuf, inbuf);
1076290001Sglebius	}
1077290001Sglebius
1078290001Sglebius	RESTORE_PINNED(inbuf, pinned, last);
1079290001Sglebius
1080290001Sglebius	inbuf->n_del_for_cb += in_total_len;
1081290001Sglebius	outbuf->n_add_for_cb += in_total_len;
1082290001Sglebius
1083290001Sglebius	evbuffer_invoke_callbacks_(inbuf);
1084290001Sglebius	evbuffer_invoke_callbacks_(outbuf);
1085290001Sglebiusdone:
1086290001Sglebius	EVBUFFER_UNLOCK2(inbuf, outbuf);
1087290001Sglebius	return result;
1088290001Sglebius}
1089290001Sglebius
1090290001Sglebiusint
1091290001Sglebiusevbuffer_drain(struct evbuffer *buf, size_t len)
1092290001Sglebius{
1093290001Sglebius	struct evbuffer_chain *chain, *next;
1094290001Sglebius	size_t remaining, old_len;
1095290001Sglebius	int result = 0;
1096290001Sglebius
1097290001Sglebius	EVBUFFER_LOCK(buf);
1098290001Sglebius	old_len = buf->total_len;
1099290001Sglebius
1100290001Sglebius	if (old_len == 0)
1101290001Sglebius		goto done;
1102290001Sglebius
1103290001Sglebius	if (buf->freeze_start) {
1104290001Sglebius		result = -1;
1105290001Sglebius		goto done;
1106290001Sglebius	}
1107290001Sglebius
1108290001Sglebius	if (len >= old_len && !HAS_PINNED_R(buf)) {
1109290001Sglebius		len = old_len;
1110290001Sglebius		for (chain = buf->first; chain != NULL; chain = next) {
1111290001Sglebius			next = chain->next;
1112290001Sglebius			evbuffer_chain_free(chain);
1113290001Sglebius		}
1114290001Sglebius
1115290001Sglebius		ZERO_CHAIN(buf);
1116290001Sglebius	} else {
1117290001Sglebius		if (len >= old_len)
1118290001Sglebius			len = old_len;
1119290001Sglebius
1120290001Sglebius		buf->total_len -= len;
1121290001Sglebius		remaining = len;
1122290001Sglebius		for (chain = buf->first;
1123290001Sglebius		     remaining >= chain->off;
1124290001Sglebius		     chain = next) {
1125290001Sglebius			next = chain->next;
1126290001Sglebius			remaining -= chain->off;
1127290001Sglebius
1128290001Sglebius			if (chain == *buf->last_with_datap) {
1129290001Sglebius				buf->last_with_datap = &buf->first;
1130290001Sglebius			}
1131290001Sglebius			if (&chain->next == buf->last_with_datap)
1132290001Sglebius				buf->last_with_datap = &buf->first;
1133290001Sglebius
1134290001Sglebius			if (CHAIN_PINNED_R(chain)) {
1135290001Sglebius				EVUTIL_ASSERT(remaining == 0);
1136290001Sglebius				chain->misalign += chain->off;
1137290001Sglebius				chain->off = 0;
1138290001Sglebius				break;
1139290001Sglebius			} else
1140290001Sglebius				evbuffer_chain_free(chain);
1141290001Sglebius		}
1142290001Sglebius
1143290001Sglebius		buf->first = chain;
1144290001Sglebius		EVUTIL_ASSERT(chain && remaining <= chain->off);
1145290001Sglebius		chain->misalign += remaining;
1146290001Sglebius		chain->off -= remaining;
1147290001Sglebius	}
1148290001Sglebius
1149290001Sglebius	buf->n_del_for_cb += len;
1150290001Sglebius	/* Tell someone about changes in this buffer */
1151290001Sglebius	evbuffer_invoke_callbacks_(buf);
1152290001Sglebius
1153290001Sglebiusdone:
1154290001Sglebius	EVBUFFER_UNLOCK(buf);
1155290001Sglebius	return result;
1156290001Sglebius}
1157290001Sglebius
1158290001Sglebius/* Reads data from an event buffer and drains the bytes read */
1159290001Sglebiusint
1160290001Sglebiusevbuffer_remove(struct evbuffer *buf, void *data_out, size_t datlen)
1161290001Sglebius{
1162290001Sglebius	ev_ssize_t n;
1163290001Sglebius	EVBUFFER_LOCK(buf);
1164290001Sglebius	n = evbuffer_copyout_from(buf, NULL, data_out, datlen);
1165290001Sglebius	if (n > 0) {
1166290001Sglebius		if (evbuffer_drain(buf, n)<0)
1167290001Sglebius			n = -1;
1168290001Sglebius	}
1169290001Sglebius	EVBUFFER_UNLOCK(buf);
1170290001Sglebius	return (int)n;
1171290001Sglebius}
1172290001Sglebius
1173290001Sglebiusev_ssize_t
1174290001Sglebiusevbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen)
1175290001Sglebius{
1176290001Sglebius	return evbuffer_copyout_from(buf, NULL, data_out, datlen);
1177290001Sglebius}
1178290001Sglebius
1179290001Sglebiusev_ssize_t
1180290001Sglebiusevbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos,
1181290001Sglebius    void *data_out, size_t datlen)
1182290001Sglebius{
1183290001Sglebius	/*XXX fails badly on sendfile case. */
1184290001Sglebius	struct evbuffer_chain *chain;
1185290001Sglebius	char *data = data_out;
1186290001Sglebius	size_t nread;
1187290001Sglebius	ev_ssize_t result = 0;
1188290001Sglebius	size_t pos_in_chain;
1189290001Sglebius
1190290001Sglebius	EVBUFFER_LOCK(buf);
1191290001Sglebius
1192290001Sglebius	if (pos) {
1193290001Sglebius		if (datlen > (size_t)(EV_SSIZE_MAX - pos->pos)) {
1194290001Sglebius			result = -1;
1195290001Sglebius			goto done;
1196290001Sglebius		}
1197290001Sglebius		chain = pos->internal_.chain;
1198290001Sglebius		pos_in_chain = pos->internal_.pos_in_chain;
1199290001Sglebius		if (datlen + pos->pos > buf->total_len)
1200290001Sglebius			datlen = buf->total_len - pos->pos;
1201290001Sglebius	} else {
1202290001Sglebius		chain = buf->first;
1203290001Sglebius		pos_in_chain = 0;
1204290001Sglebius		if (datlen > buf->total_len)
1205290001Sglebius			datlen = buf->total_len;
1206290001Sglebius	}
1207290001Sglebius
1208290001Sglebius
1209290001Sglebius	if (datlen == 0)
1210290001Sglebius		goto done;
1211290001Sglebius
1212290001Sglebius	if (buf->freeze_start) {
1213290001Sglebius		result = -1;
1214290001Sglebius		goto done;
1215290001Sglebius	}
1216290001Sglebius
1217290001Sglebius	nread = datlen;
1218290001Sglebius
1219290001Sglebius	while (datlen && datlen >= chain->off - pos_in_chain) {
1220290001Sglebius		size_t copylen = chain->off - pos_in_chain;
1221290001Sglebius		memcpy(data,
1222290001Sglebius		    chain->buffer + chain->misalign + pos_in_chain,
1223290001Sglebius		    copylen);
1224290001Sglebius		data += copylen;
1225290001Sglebius		datlen -= copylen;
1226290001Sglebius
1227290001Sglebius		chain = chain->next;
1228290001Sglebius		pos_in_chain = 0;
1229290001Sglebius		EVUTIL_ASSERT(chain || datlen==0);
1230290001Sglebius	}
1231290001Sglebius
1232290001Sglebius	if (datlen) {
1233290001Sglebius		EVUTIL_ASSERT(chain);
1234290001Sglebius		EVUTIL_ASSERT(datlen+pos_in_chain <= chain->off);
1235290001Sglebius
1236290001Sglebius		memcpy(data, chain->buffer + chain->misalign + pos_in_chain,
1237290001Sglebius		    datlen);
1238290001Sglebius	}
1239290001Sglebius
1240290001Sglebius	result = nread;
1241290001Sglebiusdone:
1242290001Sglebius	EVBUFFER_UNLOCK(buf);
1243290001Sglebius	return result;
1244290001Sglebius}
1245290001Sglebius
1246290001Sglebius/* reads data from the src buffer to the dst buffer, avoids memcpy as
1247290001Sglebius * possible. */
1248290001Sglebius/*  XXXX should return ev_ssize_t */
1249290001Sglebiusint
1250290001Sglebiusevbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
1251290001Sglebius    size_t datlen)
1252290001Sglebius{
1253290001Sglebius	/*XXX We should have an option to force this to be zero-copy.*/
1254290001Sglebius
1255290001Sglebius	/*XXX can fail badly on sendfile case. */
1256290001Sglebius	struct evbuffer_chain *chain, *previous;
1257290001Sglebius	size_t nread = 0;
1258290001Sglebius	int result;
1259290001Sglebius
1260290001Sglebius	EVBUFFER_LOCK2(src, dst);
1261290001Sglebius
1262290001Sglebius	chain = previous = src->first;
1263290001Sglebius
1264290001Sglebius	if (datlen == 0 || dst == src) {
1265290001Sglebius		result = 0;
1266290001Sglebius		goto done;
1267290001Sglebius	}
1268290001Sglebius
1269290001Sglebius	if (dst->freeze_end || src->freeze_start) {
1270290001Sglebius		result = -1;
1271290001Sglebius		goto done;
1272290001Sglebius	}
1273290001Sglebius
1274290001Sglebius	/* short-cut if there is no more data buffered */
1275290001Sglebius	if (datlen >= src->total_len) {
1276290001Sglebius		datlen = src->total_len;
1277290001Sglebius		evbuffer_add_buffer(dst, src);
1278290001Sglebius		result = (int)datlen; /*XXXX should return ev_ssize_t*/
1279290001Sglebius		goto done;
1280290001Sglebius	}
1281290001Sglebius
1282290001Sglebius	/* removes chains if possible */
1283290001Sglebius	while (chain->off <= datlen) {
1284290001Sglebius		/* We can't remove the last with data from src unless we
1285290001Sglebius		 * remove all chains, in which case we would have done the if
1286290001Sglebius		 * block above */
1287290001Sglebius		EVUTIL_ASSERT(chain != *src->last_with_datap);
1288290001Sglebius		nread += chain->off;
1289290001Sglebius		datlen -= chain->off;
1290290001Sglebius		previous = chain;
1291290001Sglebius		if (src->last_with_datap == &chain->next)
1292290001Sglebius			src->last_with_datap = &src->first;
1293290001Sglebius		chain = chain->next;
1294290001Sglebius	}
1295290001Sglebius
1296290001Sglebius	if (nread) {
1297290001Sglebius		/* we can remove the chain */
1298290001Sglebius		struct evbuffer_chain **chp;
1299290001Sglebius		chp = evbuffer_free_trailing_empty_chains(dst);
1300290001Sglebius
1301290001Sglebius		if (dst->first == NULL) {
1302290001Sglebius			dst->first = src->first;
1303290001Sglebius		} else {
1304290001Sglebius			*chp = src->first;
1305290001Sglebius		}
1306290001Sglebius		dst->last = previous;
1307290001Sglebius		previous->next = NULL;
1308290001Sglebius		src->first = chain;
1309290001Sglebius		advance_last_with_data(dst);
1310290001Sglebius
1311290001Sglebius		dst->total_len += nread;
1312290001Sglebius		dst->n_add_for_cb += nread;
1313290001Sglebius	}
1314290001Sglebius
1315290001Sglebius	/* we know that there is more data in the src buffer than
1316290001Sglebius	 * we want to read, so we manually drain the chain */
1317290001Sglebius	evbuffer_add(dst, chain->buffer + chain->misalign, datlen);
1318290001Sglebius	chain->misalign += datlen;
1319290001Sglebius	chain->off -= datlen;
1320290001Sglebius	nread += datlen;
1321290001Sglebius
1322290001Sglebius	/* You might think we would want to increment dst->n_add_for_cb
1323290001Sglebius	 * here too.  But evbuffer_add above already took care of that.
1324290001Sglebius	 */
1325290001Sglebius	src->total_len -= nread;
1326290001Sglebius	src->n_del_for_cb += nread;
1327290001Sglebius
1328290001Sglebius	if (nread) {
1329290001Sglebius		evbuffer_invoke_callbacks_(dst);
1330290001Sglebius		evbuffer_invoke_callbacks_(src);
1331290001Sglebius	}
1332290001Sglebius	result = (int)nread;/*XXXX should change return type */
1333290001Sglebius
1334290001Sglebiusdone:
1335290001Sglebius	EVBUFFER_UNLOCK2(src, dst);
1336290001Sglebius	return result;
1337290001Sglebius}
1338290001Sglebius
1339290001Sglebiusunsigned char *
1340290001Sglebiusevbuffer_pullup(struct evbuffer *buf, ev_ssize_t size)
1341290001Sglebius{
1342290001Sglebius	struct evbuffer_chain *chain, *next, *tmp, *last_with_data;
1343290001Sglebius	unsigned char *buffer, *result = NULL;
1344290001Sglebius	ev_ssize_t remaining;
1345290001Sglebius	int removed_last_with_data = 0;
1346290001Sglebius	int removed_last_with_datap = 0;
1347290001Sglebius
1348290001Sglebius	EVBUFFER_LOCK(buf);
1349290001Sglebius
1350290001Sglebius	chain = buf->first;
1351290001Sglebius
1352290001Sglebius	if (size < 0)
1353290001Sglebius		size = buf->total_len;
1354290001Sglebius	/* if size > buf->total_len, we cannot guarantee to the user that she
1355290001Sglebius	 * is going to have a long enough buffer afterwards; so we return
1356290001Sglebius	 * NULL */
1357290001Sglebius	if (size == 0 || (size_t)size > buf->total_len)
1358290001Sglebius		goto done;
1359290001Sglebius
1360290001Sglebius	/* No need to pull up anything; the first size bytes are
1361290001Sglebius	 * already here. */
1362290001Sglebius	if (chain->off >= (size_t)size) {
1363290001Sglebius		result = chain->buffer + chain->misalign;
1364290001Sglebius		goto done;
1365290001Sglebius	}
1366290001Sglebius
1367290001Sglebius	/* Make sure that none of the chains we need to copy from is pinned. */
1368290001Sglebius	remaining = size - chain->off;
1369290001Sglebius	EVUTIL_ASSERT(remaining >= 0);
1370290001Sglebius	for (tmp=chain->next; tmp; tmp=tmp->next) {
1371290001Sglebius		if (CHAIN_PINNED(tmp))
1372290001Sglebius			goto done;
1373290001Sglebius		if (tmp->off >= (size_t)remaining)
1374290001Sglebius			break;
1375290001Sglebius		remaining -= tmp->off;
1376290001Sglebius	}
1377290001Sglebius
1378290001Sglebius	if (CHAIN_PINNED(chain)) {
1379290001Sglebius		size_t old_off = chain->off;
1380290001Sglebius		if (CHAIN_SPACE_LEN(chain) < size - chain->off) {
1381290001Sglebius			/* not enough room at end of chunk. */
1382290001Sglebius			goto done;
1383290001Sglebius		}
1384290001Sglebius		buffer = CHAIN_SPACE_PTR(chain);
1385290001Sglebius		tmp = chain;
1386290001Sglebius		tmp->off = size;
1387290001Sglebius		size -= old_off;
1388290001Sglebius		chain = chain->next;
1389290001Sglebius	} else if (chain->buffer_len - chain->misalign >= (size_t)size) {
1390290001Sglebius		/* already have enough space in the first chain */
1391290001Sglebius		size_t old_off = chain->off;
1392290001Sglebius		buffer = chain->buffer + chain->misalign + chain->off;
1393290001Sglebius		tmp = chain;
1394290001Sglebius		tmp->off = size;
1395290001Sglebius		size -= old_off;
1396290001Sglebius		chain = chain->next;
1397290001Sglebius	} else {
1398290001Sglebius		if ((tmp = evbuffer_chain_new(size)) == NULL) {
1399290001Sglebius			event_warn("%s: out of memory", __func__);
1400290001Sglebius			goto done;
1401290001Sglebius		}
1402290001Sglebius		buffer = tmp->buffer;
1403290001Sglebius		tmp->off = size;
1404290001Sglebius		buf->first = tmp;
1405290001Sglebius	}
1406290001Sglebius
1407290001Sglebius	/* TODO(niels): deal with buffers that point to NULL like sendfile */
1408290001Sglebius
1409290001Sglebius	/* Copy and free every chunk that will be entirely pulled into tmp */
1410290001Sglebius	last_with_data = *buf->last_with_datap;
1411290001Sglebius	for (; chain != NULL && (size_t)size >= chain->off; chain = next) {
1412290001Sglebius		next = chain->next;
1413290001Sglebius
1414290001Sglebius		memcpy(buffer, chain->buffer + chain->misalign, chain->off);
1415290001Sglebius		size -= chain->off;
1416290001Sglebius		buffer += chain->off;
1417290001Sglebius		if (chain == last_with_data)
1418290001Sglebius			removed_last_with_data = 1;
1419290001Sglebius		if (&chain->next == buf->last_with_datap)
1420290001Sglebius			removed_last_with_datap = 1;
1421290001Sglebius
1422290001Sglebius		evbuffer_chain_free(chain);
1423290001Sglebius	}
1424290001Sglebius
1425290001Sglebius	if (chain != NULL) {
1426290001Sglebius		memcpy(buffer, chain->buffer + chain->misalign, size);
1427290001Sglebius		chain->misalign += size;
1428290001Sglebius		chain->off -= size;
1429290001Sglebius	} else {
1430290001Sglebius		buf->last = tmp;
1431290001Sglebius	}
1432290001Sglebius
1433290001Sglebius	tmp->next = chain;
1434290001Sglebius
1435290001Sglebius	if (removed_last_with_data) {
1436290001Sglebius		buf->last_with_datap = &buf->first;
1437290001Sglebius	} else if (removed_last_with_datap) {
1438290001Sglebius		if (buf->first->next && buf->first->next->off)
1439290001Sglebius			buf->last_with_datap = &buf->first->next;
1440290001Sglebius		else
1441290001Sglebius			buf->last_with_datap = &buf->first;
1442290001Sglebius	}
1443290001Sglebius
1444290001Sglebius	result = (tmp->buffer + tmp->misalign);
1445290001Sglebius
1446290001Sglebiusdone:
1447290001Sglebius	EVBUFFER_UNLOCK(buf);
1448290001Sglebius	return result;
1449290001Sglebius}
1450290001Sglebius
1451290001Sglebius/*
1452290001Sglebius * Reads a line terminated by either '\r\n', '\n\r' or '\r' or '\n'.
1453290001Sglebius * The returned buffer needs to be freed by the called.
1454290001Sglebius */
1455290001Sglebiuschar *
1456290001Sglebiusevbuffer_readline(struct evbuffer *buffer)
1457290001Sglebius{
1458290001Sglebius	return evbuffer_readln(buffer, NULL, EVBUFFER_EOL_ANY);
1459290001Sglebius}
1460290001Sglebius
1461290001Sglebiusstatic inline ev_ssize_t
1462290001Sglebiusevbuffer_strchr(struct evbuffer_ptr *it, const char chr)
1463290001Sglebius{
1464290001Sglebius	struct evbuffer_chain *chain = it->internal_.chain;
1465290001Sglebius	size_t i = it->internal_.pos_in_chain;
1466290001Sglebius	while (chain != NULL) {
1467290001Sglebius		char *buffer = (char *)chain->buffer + chain->misalign;
1468290001Sglebius		char *cp = memchr(buffer+i, chr, chain->off-i);
1469290001Sglebius		if (cp) {
1470290001Sglebius			it->internal_.chain = chain;
1471290001Sglebius			it->internal_.pos_in_chain = cp - buffer;
1472290001Sglebius			it->pos += (cp - buffer - i);
1473290001Sglebius			return it->pos;
1474290001Sglebius		}
1475290001Sglebius		it->pos += chain->off - i;
1476290001Sglebius		i = 0;
1477290001Sglebius		chain = chain->next;
1478290001Sglebius	}
1479290001Sglebius
1480290001Sglebius	return (-1);
1481290001Sglebius}
1482290001Sglebius
1483290001Sglebiusstatic inline char *
1484290001Sglebiusfind_eol_char(char *s, size_t len)
1485290001Sglebius{
1486290001Sglebius#define CHUNK_SZ 128
1487290001Sglebius	/* Lots of benchmarking found this approach to be faster in practice
1488290001Sglebius	 * than doing two memchrs over the whole buffer, doin a memchr on each
1489290001Sglebius	 * char of the buffer, or trying to emulate memchr by hand. */
1490290001Sglebius	char *s_end, *cr, *lf;
1491290001Sglebius	s_end = s+len;
1492290001Sglebius	while (s < s_end) {
1493290001Sglebius		size_t chunk = (s + CHUNK_SZ < s_end) ? CHUNK_SZ : (s_end - s);
1494290001Sglebius		cr = memchr(s, '\r', chunk);
1495290001Sglebius		lf = memchr(s, '\n', chunk);
1496290001Sglebius		if (cr) {
1497290001Sglebius			if (lf && lf < cr)
1498290001Sglebius				return lf;
1499290001Sglebius			return cr;
1500290001Sglebius		} else if (lf) {
1501290001Sglebius			return lf;
1502290001Sglebius		}
1503290001Sglebius		s += CHUNK_SZ;
1504290001Sglebius	}
1505290001Sglebius
1506290001Sglebius	return NULL;
1507290001Sglebius#undef CHUNK_SZ
1508290001Sglebius}
1509290001Sglebius
1510290001Sglebiusstatic ev_ssize_t
1511290001Sglebiusevbuffer_find_eol_char(struct evbuffer_ptr *it)
1512290001Sglebius{
1513290001Sglebius	struct evbuffer_chain *chain = it->internal_.chain;
1514290001Sglebius	size_t i = it->internal_.pos_in_chain;
1515290001Sglebius	while (chain != NULL) {
1516290001Sglebius		char *buffer = (char *)chain->buffer + chain->misalign;
1517290001Sglebius		char *cp = find_eol_char(buffer+i, chain->off-i);
1518290001Sglebius		if (cp) {
1519290001Sglebius			it->internal_.chain = chain;
1520290001Sglebius			it->internal_.pos_in_chain = cp - buffer;
1521290001Sglebius			it->pos += (cp - buffer) - i;
1522290001Sglebius			return it->pos;
1523290001Sglebius		}
1524290001Sglebius		it->pos += chain->off - i;
1525290001Sglebius		i = 0;
1526290001Sglebius		chain = chain->next;
1527290001Sglebius	}
1528290001Sglebius
1529290001Sglebius	return (-1);
1530290001Sglebius}
1531290001Sglebius
1532290001Sglebiusstatic inline int
1533290001Sglebiusevbuffer_strspn(
1534290001Sglebius	struct evbuffer_ptr *ptr, const char *chrset)
1535290001Sglebius{
1536290001Sglebius	int count = 0;
1537290001Sglebius	struct evbuffer_chain *chain = ptr->internal_.chain;
1538290001Sglebius	size_t i = ptr->internal_.pos_in_chain;
1539290001Sglebius
1540290001Sglebius	if (!chain)
1541290001Sglebius		return 0;
1542290001Sglebius
1543290001Sglebius	while (1) {
1544290001Sglebius		char *buffer = (char *)chain->buffer + chain->misalign;
1545290001Sglebius		for (; i < chain->off; ++i) {
1546290001Sglebius			const char *p = chrset;
1547290001Sglebius			while (*p) {
1548290001Sglebius				if (buffer[i] == *p++)
1549290001Sglebius					goto next;
1550290001Sglebius			}
1551290001Sglebius			ptr->internal_.chain = chain;
1552290001Sglebius			ptr->internal_.pos_in_chain = i;
1553290001Sglebius			ptr->pos += count;
1554290001Sglebius			return count;
1555290001Sglebius		next:
1556290001Sglebius			++count;
1557290001Sglebius		}
1558290001Sglebius		i = 0;
1559290001Sglebius
1560290001Sglebius		if (! chain->next) {
1561290001Sglebius			ptr->internal_.chain = chain;
1562290001Sglebius			ptr->internal_.pos_in_chain = i;
1563290001Sglebius			ptr->pos += count;
1564290001Sglebius			return count;
1565290001Sglebius		}
1566290001Sglebius
1567290001Sglebius		chain = chain->next;
1568290001Sglebius	}
1569290001Sglebius}
1570290001Sglebius
1571290001Sglebius
1572290001Sglebiusstatic inline int
1573290001Sglebiusevbuffer_getchr(struct evbuffer_ptr *it)
1574290001Sglebius{
1575290001Sglebius	struct evbuffer_chain *chain = it->internal_.chain;
1576290001Sglebius	size_t off = it->internal_.pos_in_chain;
1577290001Sglebius
1578290001Sglebius	if (chain == NULL)
1579290001Sglebius		return -1;
1580290001Sglebius
1581290001Sglebius	return (unsigned char)chain->buffer[chain->misalign + off];
1582290001Sglebius}
1583290001Sglebius
1584290001Sglebiusstruct evbuffer_ptr
1585290001Sglebiusevbuffer_search_eol(struct evbuffer *buffer,
1586290001Sglebius    struct evbuffer_ptr *start, size_t *eol_len_out,
1587290001Sglebius    enum evbuffer_eol_style eol_style)
1588290001Sglebius{
1589290001Sglebius	struct evbuffer_ptr it, it2;
1590290001Sglebius	size_t extra_drain = 0;
1591290001Sglebius	int ok = 0;
1592290001Sglebius
1593290001Sglebius	/* Avoid locking in trivial edge cases */
1594290001Sglebius	if (start && start->internal_.chain == NULL) {
1595290001Sglebius		PTR_NOT_FOUND(&it);
1596290001Sglebius		if (eol_len_out)
1597290001Sglebius			*eol_len_out = extra_drain;
1598290001Sglebius		return it;
1599290001Sglebius	}
1600290001Sglebius
1601290001Sglebius	EVBUFFER_LOCK(buffer);
1602290001Sglebius
1603290001Sglebius	if (start) {
1604290001Sglebius		memcpy(&it, start, sizeof(it));
1605290001Sglebius	} else {
1606290001Sglebius		it.pos = 0;
1607290001Sglebius		it.internal_.chain = buffer->first;
1608290001Sglebius		it.internal_.pos_in_chain = 0;
1609290001Sglebius	}
1610290001Sglebius
1611290001Sglebius	/* the eol_style determines our first stop character and how many
1612290001Sglebius	 * characters we are going to drain afterwards. */
1613290001Sglebius	switch (eol_style) {
1614290001Sglebius	case EVBUFFER_EOL_ANY:
1615290001Sglebius		if (evbuffer_find_eol_char(&it) < 0)
1616290001Sglebius			goto done;
1617290001Sglebius		memcpy(&it2, &it, sizeof(it));
1618290001Sglebius		extra_drain = evbuffer_strspn(&it2, "\r\n");
1619290001Sglebius		break;
1620290001Sglebius	case EVBUFFER_EOL_CRLF_STRICT: {
1621290001Sglebius		it = evbuffer_search(buffer, "\r\n", 2, &it);
1622290001Sglebius		if (it.pos < 0)
1623290001Sglebius			goto done;
1624290001Sglebius		extra_drain = 2;
1625290001Sglebius		break;
1626290001Sglebius	}
1627290001Sglebius	case EVBUFFER_EOL_CRLF: {
1628290001Sglebius		ev_ssize_t start_pos = it.pos;
1629290001Sglebius		/* Look for a LF ... */
1630290001Sglebius		if (evbuffer_strchr(&it, '\n') < 0)
1631290001Sglebius			goto done;
1632290001Sglebius		extra_drain = 1;
1633290001Sglebius		/* ... optionally preceeded by a CR. */
1634290001Sglebius		if (it.pos == start_pos)
1635290001Sglebius			break; /* If the first character is \n, don't back up */
1636290001Sglebius		/* This potentially does an extra linear walk over the first
1637290001Sglebius		 * few chains.  Probably, that's not too expensive unless you
1638290001Sglebius		 * have a really pathological setup. */
1639290001Sglebius		memcpy(&it2, &it, sizeof(it));
1640290001Sglebius		if (evbuffer_ptr_subtract(buffer, &it2, 1)<0)
1641290001Sglebius			break;
1642290001Sglebius		if (evbuffer_getchr(&it2) == '\r') {
1643290001Sglebius			memcpy(&it, &it2, sizeof(it));
1644290001Sglebius			extra_drain = 2;
1645290001Sglebius		}
1646290001Sglebius		break;
1647290001Sglebius	}
1648290001Sglebius	case EVBUFFER_EOL_LF:
1649290001Sglebius		if (evbuffer_strchr(&it, '\n') < 0)
1650290001Sglebius			goto done;
1651290001Sglebius		extra_drain = 1;
1652290001Sglebius		break;
1653290001Sglebius	case EVBUFFER_EOL_NUL:
1654290001Sglebius		if (evbuffer_strchr(&it, '\0') < 0)
1655290001Sglebius			goto done;
1656290001Sglebius		extra_drain = 1;
1657290001Sglebius		break;
1658290001Sglebius	default:
1659290001Sglebius		goto done;
1660290001Sglebius	}
1661290001Sglebius
1662290001Sglebius	ok = 1;
1663290001Sglebiusdone:
1664290001Sglebius	EVBUFFER_UNLOCK(buffer);
1665290001Sglebius
1666290001Sglebius	if (!ok)
1667290001Sglebius		PTR_NOT_FOUND(&it);
1668290001Sglebius	if (eol_len_out)
1669290001Sglebius		*eol_len_out = extra_drain;
1670290001Sglebius
1671290001Sglebius	return it;
1672290001Sglebius}
1673290001Sglebius
1674290001Sglebiuschar *
1675290001Sglebiusevbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
1676290001Sglebius		enum evbuffer_eol_style eol_style)
1677290001Sglebius{
1678290001Sglebius	struct evbuffer_ptr it;
1679290001Sglebius	char *line;
1680290001Sglebius	size_t n_to_copy=0, extra_drain=0;
1681290001Sglebius	char *result = NULL;
1682290001Sglebius
1683290001Sglebius	EVBUFFER_LOCK(buffer);
1684290001Sglebius
1685290001Sglebius	if (buffer->freeze_start) {
1686290001Sglebius		goto done;
1687290001Sglebius	}
1688290001Sglebius
1689290001Sglebius	it = evbuffer_search_eol(buffer, NULL, &extra_drain, eol_style);
1690290001Sglebius	if (it.pos < 0)
1691290001Sglebius		goto done;
1692290001Sglebius	n_to_copy = it.pos;
1693290001Sglebius
1694290001Sglebius	if ((line = mm_malloc(n_to_copy+1)) == NULL) {
1695290001Sglebius		event_warn("%s: out of memory", __func__);
1696290001Sglebius		goto done;
1697290001Sglebius	}
1698290001Sglebius
1699290001Sglebius	evbuffer_remove(buffer, line, n_to_copy);
1700290001Sglebius	line[n_to_copy] = '\0';
1701290001Sglebius
1702290001Sglebius	evbuffer_drain(buffer, extra_drain);
1703290001Sglebius	result = line;
1704290001Sglebiusdone:
1705290001Sglebius	EVBUFFER_UNLOCK(buffer);
1706290001Sglebius
1707290001Sglebius	if (n_read_out)
1708290001Sglebius		*n_read_out = result ? n_to_copy : 0;
1709290001Sglebius
1710290001Sglebius	return result;
1711290001Sglebius}
1712290001Sglebius
1713290001Sglebius#define EVBUFFER_CHAIN_MAX_AUTO_SIZE 4096
1714290001Sglebius
1715290001Sglebius/* Adds data to an event buffer */
1716290001Sglebius
1717290001Sglebiusint
1718290001Sglebiusevbuffer_add(struct evbuffer *buf, const void *data_in, size_t datlen)
1719290001Sglebius{
1720290001Sglebius	struct evbuffer_chain *chain, *tmp;
1721290001Sglebius	const unsigned char *data = data_in;
1722290001Sglebius	size_t remain, to_alloc;
1723290001Sglebius	int result = -1;
1724290001Sglebius
1725290001Sglebius	EVBUFFER_LOCK(buf);
1726290001Sglebius
1727290001Sglebius	if (buf->freeze_end) {
1728290001Sglebius		goto done;
1729290001Sglebius	}
1730290001Sglebius	/* Prevent buf->total_len overflow */
1731290001Sglebius	if (datlen > EV_SIZE_MAX - buf->total_len) {
1732290001Sglebius		goto done;
1733290001Sglebius	}
1734290001Sglebius
1735290001Sglebius	chain = buf->last;
1736290001Sglebius
1737290001Sglebius	/* If there are no chains allocated for this buffer, allocate one
1738290001Sglebius	 * big enough to hold all the data. */
1739290001Sglebius	if (chain == NULL) {
1740290001Sglebius		chain = evbuffer_chain_new(datlen);
1741290001Sglebius		if (!chain)
1742290001Sglebius			goto done;
1743290001Sglebius		evbuffer_chain_insert(buf, chain);
1744290001Sglebius	}
1745290001Sglebius
1746290001Sglebius	if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
1747290001Sglebius		/* Always true for mutable buffers */
1748290001Sglebius		EVUTIL_ASSERT(chain->misalign >= 0 &&
1749290001Sglebius		    (ev_uint64_t)chain->misalign <= EVBUFFER_CHAIN_MAX);
1750290001Sglebius		remain = chain->buffer_len - (size_t)chain->misalign - chain->off;
1751290001Sglebius		if (remain >= datlen) {
1752290001Sglebius			/* there's enough space to hold all the data in the
1753290001Sglebius			 * current last chain */
1754290001Sglebius			memcpy(chain->buffer + chain->misalign + chain->off,
1755290001Sglebius			    data, datlen);
1756290001Sglebius			chain->off += datlen;
1757290001Sglebius			buf->total_len += datlen;
1758290001Sglebius			buf->n_add_for_cb += datlen;
1759290001Sglebius			goto out;
1760290001Sglebius		} else if (!CHAIN_PINNED(chain) &&
1761290001Sglebius		    evbuffer_chain_should_realign(chain, datlen)) {
1762290001Sglebius			/* we can fit the data into the misalignment */
1763290001Sglebius			evbuffer_chain_align(chain);
1764290001Sglebius
1765290001Sglebius			memcpy(chain->buffer + chain->off, data, datlen);
1766290001Sglebius			chain->off += datlen;
1767290001Sglebius			buf->total_len += datlen;
1768290001Sglebius			buf->n_add_for_cb += datlen;
1769290001Sglebius			goto out;
1770290001Sglebius		}
1771290001Sglebius	} else {
1772290001Sglebius		/* we cannot write any data to the last chain */
1773290001Sglebius		remain = 0;
1774290001Sglebius	}
1775290001Sglebius
1776290001Sglebius	/* we need to add another chain */
1777290001Sglebius	to_alloc = chain->buffer_len;
1778290001Sglebius	if (to_alloc <= EVBUFFER_CHAIN_MAX_AUTO_SIZE/2)
1779290001Sglebius		to_alloc <<= 1;
1780290001Sglebius	if (datlen > to_alloc)
1781290001Sglebius		to_alloc = datlen;
1782290001Sglebius	tmp = evbuffer_chain_new(to_alloc);
1783290001Sglebius	if (tmp == NULL)
1784290001Sglebius		goto done;
1785290001Sglebius
1786290001Sglebius	if (remain) {
1787290001Sglebius		memcpy(chain->buffer + chain->misalign + chain->off,
1788290001Sglebius		    data, remain);
1789290001Sglebius		chain->off += remain;
1790290001Sglebius		buf->total_len += remain;
1791290001Sglebius		buf->n_add_for_cb += remain;
1792290001Sglebius	}
1793290001Sglebius
1794290001Sglebius	data += remain;
1795290001Sglebius	datlen -= remain;
1796290001Sglebius
1797290001Sglebius	memcpy(tmp->buffer, data, datlen);
1798290001Sglebius	tmp->off = datlen;
1799290001Sglebius	evbuffer_chain_insert(buf, tmp);
1800290001Sglebius	buf->n_add_for_cb += datlen;
1801290001Sglebius
1802290001Sglebiusout:
1803290001Sglebius	evbuffer_invoke_callbacks_(buf);
1804290001Sglebius	result = 0;
1805290001Sglebiusdone:
1806290001Sglebius	EVBUFFER_UNLOCK(buf);
1807290001Sglebius	return result;
1808290001Sglebius}
1809290001Sglebius
1810290001Sglebiusint
1811290001Sglebiusevbuffer_prepend(struct evbuffer *buf, const void *data, size_t datlen)
1812290001Sglebius{
1813290001Sglebius	struct evbuffer_chain *chain, *tmp;
1814290001Sglebius	int result = -1;
1815290001Sglebius
1816290001Sglebius	EVBUFFER_LOCK(buf);
1817290001Sglebius
1818290001Sglebius	if (buf->freeze_start) {
1819290001Sglebius		goto done;
1820290001Sglebius	}
1821290001Sglebius	if (datlen > EV_SIZE_MAX - buf->total_len) {
1822290001Sglebius		goto done;
1823290001Sglebius	}
1824290001Sglebius
1825290001Sglebius	chain = buf->first;
1826290001Sglebius
1827290001Sglebius	if (chain == NULL) {
1828290001Sglebius		chain = evbuffer_chain_new(datlen);
1829290001Sglebius		if (!chain)
1830290001Sglebius			goto done;
1831290001Sglebius		evbuffer_chain_insert(buf, chain);
1832290001Sglebius	}
1833290001Sglebius
1834290001Sglebius	/* we cannot touch immutable buffers */
1835290001Sglebius	if ((chain->flags & EVBUFFER_IMMUTABLE) == 0) {
1836290001Sglebius		/* Always true for mutable buffers */
1837290001Sglebius		EVUTIL_ASSERT(chain->misalign >= 0 &&
1838290001Sglebius		    (ev_uint64_t)chain->misalign <= EVBUFFER_CHAIN_MAX);
1839290001Sglebius
1840290001Sglebius		/* If this chain is empty, we can treat it as
1841290001Sglebius		 * 'empty at the beginning' rather than 'empty at the end' */
1842290001Sglebius		if (chain->off == 0)
1843290001Sglebius			chain->misalign = chain->buffer_len;
1844290001Sglebius
1845290001Sglebius		if ((size_t)chain->misalign >= datlen) {
1846290001Sglebius			/* we have enough space to fit everything */
1847290001Sglebius			memcpy(chain->buffer + chain->misalign - datlen,
1848290001Sglebius			    data, datlen);
1849290001Sglebius			chain->off += datlen;
1850290001Sglebius			chain->misalign -= datlen;
1851290001Sglebius			buf->total_len += datlen;
1852290001Sglebius			buf->n_add_for_cb += datlen;
1853290001Sglebius			goto out;
1854290001Sglebius		} else if (chain->misalign) {
1855290001Sglebius			/* we can only fit some of the data. */
1856290001Sglebius			memcpy(chain->buffer,
1857290001Sglebius			    (char*)data + datlen - chain->misalign,
1858290001Sglebius			    (size_t)chain->misalign);
1859290001Sglebius			chain->off += (size_t)chain->misalign;
1860290001Sglebius			buf->total_len += (size_t)chain->misalign;
1861290001Sglebius			buf->n_add_for_cb += (size_t)chain->misalign;
1862290001Sglebius			datlen -= (size_t)chain->misalign;
1863290001Sglebius			chain->misalign = 0;
1864290001Sglebius		}
1865290001Sglebius	}
1866290001Sglebius
1867290001Sglebius	/* we need to add another chain */
1868290001Sglebius	if ((tmp = evbuffer_chain_new(datlen)) == NULL)
1869290001Sglebius		goto done;
1870290001Sglebius	buf->first = tmp;
1871290001Sglebius	if (buf->last_with_datap == &buf->first)
1872290001Sglebius		buf->last_with_datap = &tmp->next;
1873290001Sglebius
1874290001Sglebius	tmp->next = chain;
1875290001Sglebius
1876290001Sglebius	tmp->off = datlen;
1877290001Sglebius	EVUTIL_ASSERT(datlen <= tmp->buffer_len);
1878290001Sglebius	tmp->misalign = tmp->buffer_len - datlen;
1879290001Sglebius
1880290001Sglebius	memcpy(tmp->buffer + tmp->misalign, data, datlen);
1881290001Sglebius	buf->total_len += datlen;
1882290001Sglebius	buf->n_add_for_cb += (size_t)chain->misalign;
1883290001Sglebius
1884290001Sglebiusout:
1885290001Sglebius	evbuffer_invoke_callbacks_(buf);
1886290001Sglebius	result = 0;
1887290001Sglebiusdone:
1888290001Sglebius	EVBUFFER_UNLOCK(buf);
1889290001Sglebius	return result;
1890290001Sglebius}
1891290001Sglebius
1892290001Sglebius/** Helper: realigns the memory in chain->buffer so that misalign is 0. */
1893290001Sglebiusstatic void
1894290001Sglebiusevbuffer_chain_align(struct evbuffer_chain *chain)
1895290001Sglebius{
1896290001Sglebius	EVUTIL_ASSERT(!(chain->flags & EVBUFFER_IMMUTABLE));
1897290001Sglebius	EVUTIL_ASSERT(!(chain->flags & EVBUFFER_MEM_PINNED_ANY));
1898290001Sglebius	memmove(chain->buffer, chain->buffer + chain->misalign, chain->off);
1899290001Sglebius	chain->misalign = 0;
1900290001Sglebius}
1901290001Sglebius
1902290001Sglebius#define MAX_TO_COPY_IN_EXPAND 4096
1903290001Sglebius#define MAX_TO_REALIGN_IN_EXPAND 2048
1904290001Sglebius
1905290001Sglebius/** Helper: return true iff we should realign chain to fit datalen bytes of
1906290001Sglebius    data in it. */
1907290001Sglebiusstatic int
1908290001Sglebiusevbuffer_chain_should_realign(struct evbuffer_chain *chain,
1909290001Sglebius    size_t datlen)
1910290001Sglebius{
1911290001Sglebius	return chain->buffer_len - chain->off >= datlen &&
1912290001Sglebius	    (chain->off < chain->buffer_len / 2) &&
1913290001Sglebius	    (chain->off <= MAX_TO_REALIGN_IN_EXPAND);
1914290001Sglebius}
1915290001Sglebius
1916290001Sglebius/* Expands the available space in the event buffer to at least datlen, all in
1917290001Sglebius * a single chunk.  Return that chunk. */
1918290001Sglebiusstatic struct evbuffer_chain *
1919290001Sglebiusevbuffer_expand_singlechain(struct evbuffer *buf, size_t datlen)
1920290001Sglebius{
1921290001Sglebius	struct evbuffer_chain *chain, **chainp;
1922290001Sglebius	struct evbuffer_chain *result = NULL;
1923290001Sglebius	ASSERT_EVBUFFER_LOCKED(buf);
1924290001Sglebius
1925290001Sglebius	chainp = buf->last_with_datap;
1926290001Sglebius
1927290001Sglebius	/* XXX If *chainp is no longer writeable, but has enough space in its
1928290001Sglebius	 * misalign, this might be a bad idea: we could still use *chainp, not
1929290001Sglebius	 * (*chainp)->next. */
1930290001Sglebius	if (*chainp && CHAIN_SPACE_LEN(*chainp) == 0)
1931290001Sglebius		chainp = &(*chainp)->next;
1932290001Sglebius
1933290001Sglebius	/* 'chain' now points to the first chain with writable space (if any)
1934290001Sglebius	 * We will either use it, realign it, replace it, or resize it. */
1935290001Sglebius	chain = *chainp;
1936290001Sglebius
1937290001Sglebius	if (chain == NULL ||
1938290001Sglebius	    (chain->flags & (EVBUFFER_IMMUTABLE|EVBUFFER_MEM_PINNED_ANY))) {
1939290001Sglebius		/* We can't use the last_with_data chain at all.  Just add a
1940290001Sglebius		 * new one that's big enough. */
1941290001Sglebius		goto insert_new;
1942290001Sglebius	}
1943290001Sglebius
1944290001Sglebius	/* If we can fit all the data, then we don't have to do anything */
1945290001Sglebius	if (CHAIN_SPACE_LEN(chain) >= datlen) {
1946290001Sglebius		result = chain;
1947290001Sglebius		goto ok;
1948290001Sglebius	}
1949290001Sglebius
1950290001Sglebius	/* If the chain is completely empty, just replace it by adding a new
1951290001Sglebius	 * empty chain. */
1952290001Sglebius	if (chain->off == 0) {
1953290001Sglebius		goto insert_new;
1954290001Sglebius	}
1955290001Sglebius
1956290001Sglebius	/* If the misalignment plus the remaining space fulfills our data
1957290001Sglebius	 * needs, we could just force an alignment to happen.  Afterwards, we
1958290001Sglebius	 * have enough space.  But only do this if we're saving a lot of space
1959290001Sglebius	 * and not moving too much data.  Otherwise the space savings are
1960290001Sglebius	 * probably offset by the time lost in copying.
1961290001Sglebius	 */
1962290001Sglebius	if (evbuffer_chain_should_realign(chain, datlen)) {
1963290001Sglebius		evbuffer_chain_align(chain);
1964290001Sglebius		result = chain;
1965290001Sglebius		goto ok;
1966290001Sglebius	}
1967290001Sglebius
1968290001Sglebius	/* At this point, we can either resize the last chunk with space in
1969290001Sglebius	 * it, use the next chunk after it, or   If we add a new chunk, we waste
1970290001Sglebius	 * CHAIN_SPACE_LEN(chain) bytes in the former last chunk.  If we
1971290001Sglebius	 * resize, we have to copy chain->off bytes.
1972290001Sglebius	 */
1973290001Sglebius
1974290001Sglebius	/* Would expanding this chunk be affordable and worthwhile? */
1975290001Sglebius	if (CHAIN_SPACE_LEN(chain) < chain->buffer_len / 8 ||
1976290001Sglebius	    chain->off > MAX_TO_COPY_IN_EXPAND ||
1977290001Sglebius	    (datlen < EVBUFFER_CHAIN_MAX &&
1978290001Sglebius		EVBUFFER_CHAIN_MAX - datlen >= chain->off)) {
1979290001Sglebius		/* It's not worth resizing this chain. Can the next one be
1980290001Sglebius		 * used? */
1981290001Sglebius		if (chain->next && CHAIN_SPACE_LEN(chain->next) >= datlen) {
1982290001Sglebius			/* Yes, we can just use the next chain (which should
1983290001Sglebius			 * be empty. */
1984290001Sglebius			result = chain->next;
1985290001Sglebius			goto ok;
1986290001Sglebius		} else {
1987290001Sglebius			/* No; append a new chain (which will free all
1988290001Sglebius			 * terminal empty chains.) */
1989290001Sglebius			goto insert_new;
1990290001Sglebius		}
1991290001Sglebius	} else {
1992290001Sglebius		/* Okay, we're going to try to resize this chain: Not doing so
1993290001Sglebius		 * would waste at least 1/8 of its current allocation, and we
1994290001Sglebius		 * can do so without having to copy more than
1995290001Sglebius		 * MAX_TO_COPY_IN_EXPAND bytes. */
1996290001Sglebius		/* figure out how much space we need */
1997290001Sglebius		size_t length = chain->off + datlen;
1998290001Sglebius		struct evbuffer_chain *tmp = evbuffer_chain_new(length);
1999290001Sglebius		if (tmp == NULL)
2000290001Sglebius			goto err;
2001290001Sglebius
2002290001Sglebius		/* copy the data over that we had so far */
2003290001Sglebius		tmp->off = chain->off;
2004290001Sglebius		memcpy(tmp->buffer, chain->buffer + chain->misalign,
2005290001Sglebius		    chain->off);
2006290001Sglebius		/* fix up the list */
2007290001Sglebius		EVUTIL_ASSERT(*chainp == chain);
2008290001Sglebius		result = *chainp = tmp;
2009290001Sglebius
2010290001Sglebius		if (buf->last == chain)
2011290001Sglebius			buf->last = tmp;
2012290001Sglebius
2013290001Sglebius		tmp->next = chain->next;
2014290001Sglebius		evbuffer_chain_free(chain);
2015290001Sglebius		goto ok;
2016290001Sglebius	}
2017290001Sglebius
2018290001Sglebiusinsert_new:
2019290001Sglebius	result = evbuffer_chain_insert_new(buf, datlen);
2020290001Sglebius	if (!result)
2021290001Sglebius		goto err;
2022290001Sglebiusok:
2023290001Sglebius	EVUTIL_ASSERT(result);
2024290001Sglebius	EVUTIL_ASSERT(CHAIN_SPACE_LEN(result) >= datlen);
2025290001Sglebiuserr:
2026290001Sglebius	return result;
2027290001Sglebius}
2028290001Sglebius
2029290001Sglebius/* Make sure that datlen bytes are available for writing in the last n
2030290001Sglebius * chains.  Never copies or moves data. */
2031290001Sglebiusint
2032290001Sglebiusevbuffer_expand_fast_(struct evbuffer *buf, size_t datlen, int n)
2033290001Sglebius{
2034290001Sglebius	struct evbuffer_chain *chain = buf->last, *tmp, *next;
2035290001Sglebius	size_t avail;
2036290001Sglebius	int used;
2037290001Sglebius
2038290001Sglebius	ASSERT_EVBUFFER_LOCKED(buf);
2039290001Sglebius	EVUTIL_ASSERT(n >= 2);
2040290001Sglebius
2041290001Sglebius	if (chain == NULL || (chain->flags & EVBUFFER_IMMUTABLE)) {
2042290001Sglebius		/* There is no last chunk, or we can't touch the last chunk.
2043290001Sglebius		 * Just add a new chunk. */
2044290001Sglebius		chain = evbuffer_chain_new(datlen);
2045290001Sglebius		if (chain == NULL)
2046290001Sglebius			return (-1);
2047290001Sglebius
2048290001Sglebius		evbuffer_chain_insert(buf, chain);
2049290001Sglebius		return (0);
2050290001Sglebius	}
2051290001Sglebius
2052290001Sglebius	used = 0; /* number of chains we're using space in. */
2053290001Sglebius	avail = 0; /* how much space they have. */
2054290001Sglebius	/* How many bytes can we stick at the end of buffer as it is?  Iterate
2055290001Sglebius	 * over the chains at the end of the buffer, tring to see how much
2056290001Sglebius	 * space we have in the first n. */
2057290001Sglebius	for (chain = *buf->last_with_datap; chain; chain = chain->next) {
2058290001Sglebius		if (chain->off) {
2059290001Sglebius			size_t space = (size_t) CHAIN_SPACE_LEN(chain);
2060290001Sglebius			EVUTIL_ASSERT(chain == *buf->last_with_datap);
2061290001Sglebius			if (space) {
2062290001Sglebius				avail += space;
2063290001Sglebius				++used;
2064290001Sglebius			}
2065290001Sglebius		} else {
2066290001Sglebius			/* No data in chain; realign it. */
2067290001Sglebius			chain->misalign = 0;
2068290001Sglebius			avail += chain->buffer_len;
2069290001Sglebius			++used;
2070290001Sglebius		}
2071290001Sglebius		if (avail >= datlen) {
2072290001Sglebius			/* There is already enough space.  Just return */
2073290001Sglebius			return (0);
2074290001Sglebius		}
2075290001Sglebius		if (used == n)
2076290001Sglebius			break;
2077290001Sglebius	}
2078290001Sglebius
2079290001Sglebius	/* There wasn't enough space in the first n chains with space in
2080290001Sglebius	 * them. Either add a new chain with enough space, or replace all
2081290001Sglebius	 * empty chains with one that has enough space, depending on n. */
2082290001Sglebius	if (used < n) {
2083290001Sglebius		/* The loop ran off the end of the chains before it hit n
2084290001Sglebius		 * chains; we can add another. */
2085290001Sglebius		EVUTIL_ASSERT(chain == NULL);
2086290001Sglebius
2087290001Sglebius		tmp = evbuffer_chain_new(datlen - avail);
2088290001Sglebius		if (tmp == NULL)
2089290001Sglebius			return (-1);
2090290001Sglebius
2091290001Sglebius		buf->last->next = tmp;
2092290001Sglebius		buf->last = tmp;
2093290001Sglebius		/* (we would only set last_with_data if we added the first
2094290001Sglebius		 * chain. But if the buffer had no chains, we would have
2095290001Sglebius		 * just allocated a new chain earlier) */
2096290001Sglebius		return (0);
2097290001Sglebius	} else {
2098290001Sglebius		/* Nuke _all_ the empty chains. */
2099290001Sglebius		int rmv_all = 0; /* True iff we removed last_with_data. */
2100290001Sglebius		chain = *buf->last_with_datap;
2101290001Sglebius		if (!chain->off) {
2102290001Sglebius			EVUTIL_ASSERT(chain == buf->first);
2103290001Sglebius			rmv_all = 1;
2104290001Sglebius			avail = 0;
2105290001Sglebius		} else {
2106290001Sglebius			/* can't overflow, since only mutable chains have
2107290001Sglebius			 * huge misaligns. */
2108290001Sglebius			avail = (size_t) CHAIN_SPACE_LEN(chain);
2109290001Sglebius			chain = chain->next;
2110290001Sglebius		}
2111290001Sglebius
2112290001Sglebius
2113290001Sglebius		for (; chain; chain = next) {
2114290001Sglebius			next = chain->next;
2115290001Sglebius			EVUTIL_ASSERT(chain->off == 0);
2116290001Sglebius			evbuffer_chain_free(chain);
2117290001Sglebius		}
2118290001Sglebius		EVUTIL_ASSERT(datlen >= avail);
2119290001Sglebius		tmp = evbuffer_chain_new(datlen - avail);
2120290001Sglebius		if (tmp == NULL) {
2121290001Sglebius			if (rmv_all) {
2122290001Sglebius				ZERO_CHAIN(buf);
2123290001Sglebius			} else {
2124290001Sglebius				buf->last = *buf->last_with_datap;
2125290001Sglebius				(*buf->last_with_datap)->next = NULL;
2126290001Sglebius			}
2127290001Sglebius			return (-1);
2128290001Sglebius		}
2129290001Sglebius
2130290001Sglebius		if (rmv_all) {
2131290001Sglebius			buf->first = buf->last = tmp;
2132290001Sglebius			buf->last_with_datap = &buf->first;
2133290001Sglebius		} else {
2134290001Sglebius			(*buf->last_with_datap)->next = tmp;
2135290001Sglebius			buf->last = tmp;
2136290001Sglebius		}
2137290001Sglebius		return (0);
2138290001Sglebius	}
2139290001Sglebius}
2140290001Sglebius
2141290001Sglebiusint
2142290001Sglebiusevbuffer_expand(struct evbuffer *buf, size_t datlen)
2143290001Sglebius{
2144290001Sglebius	struct evbuffer_chain *chain;
2145290001Sglebius
2146290001Sglebius	EVBUFFER_LOCK(buf);
2147290001Sglebius	chain = evbuffer_expand_singlechain(buf, datlen);
2148290001Sglebius	EVBUFFER_UNLOCK(buf);
2149290001Sglebius	return chain ? 0 : -1;
2150290001Sglebius}
2151290001Sglebius
2152290001Sglebius/*
2153290001Sglebius * Reads data from a file descriptor into a buffer.
2154290001Sglebius */
2155290001Sglebius
2156290001Sglebius#if defined(EVENT__HAVE_SYS_UIO_H) || defined(_WIN32)
2157290001Sglebius#define USE_IOVEC_IMPL
2158290001Sglebius#endif
2159290001Sglebius
2160290001Sglebius#ifdef USE_IOVEC_IMPL
2161290001Sglebius
2162290001Sglebius#ifdef EVENT__HAVE_SYS_UIO_H
2163290001Sglebius/* number of iovec we use for writev, fragmentation is going to determine
2164290001Sglebius * how much we end up writing */
2165290001Sglebius
2166290001Sglebius#define DEFAULT_WRITE_IOVEC 128
2167290001Sglebius
2168290001Sglebius#if defined(UIO_MAXIOV) && UIO_MAXIOV < DEFAULT_WRITE_IOVEC
2169290001Sglebius#define NUM_WRITE_IOVEC UIO_MAXIOV
2170290001Sglebius#elif defined(IOV_MAX) && IOV_MAX < DEFAULT_WRITE_IOVEC
2171290001Sglebius#define NUM_WRITE_IOVEC IOV_MAX
2172290001Sglebius#else
2173290001Sglebius#define NUM_WRITE_IOVEC DEFAULT_WRITE_IOVEC
2174290001Sglebius#endif
2175290001Sglebius
2176290001Sglebius#define IOV_TYPE struct iovec
2177290001Sglebius#define IOV_PTR_FIELD iov_base
2178290001Sglebius#define IOV_LEN_FIELD iov_len
2179290001Sglebius#define IOV_LEN_TYPE size_t
2180290001Sglebius#else
2181290001Sglebius#define NUM_WRITE_IOVEC 16
2182290001Sglebius#define IOV_TYPE WSABUF
2183290001Sglebius#define IOV_PTR_FIELD buf
2184290001Sglebius#define IOV_LEN_FIELD len
2185290001Sglebius#define IOV_LEN_TYPE unsigned long
2186290001Sglebius#endif
2187290001Sglebius#endif
2188290001Sglebius#define NUM_READ_IOVEC 4
2189290001Sglebius
2190290001Sglebius#define EVBUFFER_MAX_READ	4096
2191290001Sglebius
2192290001Sglebius/** Helper function to figure out which space to use for reading data into
2193290001Sglebius    an evbuffer.  Internal use only.
2194290001Sglebius
2195290001Sglebius    @param buf The buffer to read into
2196290001Sglebius    @param howmuch How much we want to read.
2197290001Sglebius    @param vecs An array of two or more iovecs or WSABUFs.
2198290001Sglebius    @param n_vecs_avail The length of vecs
2199290001Sglebius    @param chainp A pointer to a variable to hold the first chain we're
2200290001Sglebius      reading into.
2201290001Sglebius    @param exact Boolean: if true, we do not provide more than 'howmuch'
2202290001Sglebius      space in the vectors, even if more space is available.
2203290001Sglebius    @return The number of buffers we're using.
2204290001Sglebius */
2205290001Sglebiusint
2206290001Sglebiusevbuffer_read_setup_vecs_(struct evbuffer *buf, ev_ssize_t howmuch,
2207290001Sglebius    struct evbuffer_iovec *vecs, int n_vecs_avail,
2208290001Sglebius    struct evbuffer_chain ***chainp, int exact)
2209290001Sglebius{
2210290001Sglebius	struct evbuffer_chain *chain;
2211290001Sglebius	struct evbuffer_chain **firstchainp;
2212290001Sglebius	size_t so_far;
2213290001Sglebius	int i;
2214290001Sglebius	ASSERT_EVBUFFER_LOCKED(buf);
2215290001Sglebius
2216290001Sglebius	if (howmuch < 0)
2217290001Sglebius		return -1;
2218290001Sglebius
2219290001Sglebius	so_far = 0;
2220290001Sglebius	/* Let firstchain be the first chain with any space on it */
2221290001Sglebius	firstchainp = buf->last_with_datap;
2222290001Sglebius	if (CHAIN_SPACE_LEN(*firstchainp) == 0) {
2223290001Sglebius		firstchainp = &(*firstchainp)->next;
2224290001Sglebius	}
2225290001Sglebius
2226290001Sglebius	chain = *firstchainp;
2227290001Sglebius	for (i = 0; i < n_vecs_avail && so_far < (size_t)howmuch; ++i) {
2228290001Sglebius		size_t avail = (size_t) CHAIN_SPACE_LEN(chain);
2229290001Sglebius		if (avail > (howmuch - so_far) && exact)
2230290001Sglebius			avail = howmuch - so_far;
2231290001Sglebius		vecs[i].iov_base = CHAIN_SPACE_PTR(chain);
2232290001Sglebius		vecs[i].iov_len = avail;
2233290001Sglebius		so_far += avail;
2234290001Sglebius		chain = chain->next;
2235290001Sglebius	}
2236290001Sglebius
2237290001Sglebius	*chainp = firstchainp;
2238290001Sglebius	return i;
2239290001Sglebius}
2240290001Sglebius
2241290001Sglebiusstatic int
2242290001Sglebiusget_n_bytes_readable_on_socket(evutil_socket_t fd)
2243290001Sglebius{
2244290001Sglebius#if defined(FIONREAD) && defined(_WIN32)
2245290001Sglebius	unsigned long lng = EVBUFFER_MAX_READ;
2246290001Sglebius	if (ioctlsocket(fd, FIONREAD, &lng) < 0)
2247290001Sglebius		return -1;
2248290001Sglebius	/* Can overflow, but mostly harmlessly. XXXX */
2249290001Sglebius	return (int)lng;
2250290001Sglebius#elif defined(FIONREAD)
2251290001Sglebius	int n = EVBUFFER_MAX_READ;
2252290001Sglebius	if (ioctl(fd, FIONREAD, &n) < 0)
2253290001Sglebius		return -1;
2254290001Sglebius	return n;
2255290001Sglebius#else
2256290001Sglebius	return EVBUFFER_MAX_READ;
2257290001Sglebius#endif
2258290001Sglebius}
2259290001Sglebius
2260290001Sglebius/* TODO(niels): should this function return ev_ssize_t and take ev_ssize_t
2261290001Sglebius * as howmuch? */
2262290001Sglebiusint
2263290001Sglebiusevbuffer_read(struct evbuffer *buf, evutil_socket_t fd, int howmuch)
2264290001Sglebius{
2265290001Sglebius	struct evbuffer_chain **chainp;
2266290001Sglebius	int n;
2267290001Sglebius	int result;
2268290001Sglebius
2269290001Sglebius#ifdef USE_IOVEC_IMPL
2270290001Sglebius	int nvecs, i, remaining;
2271290001Sglebius#else
2272290001Sglebius	struct evbuffer_chain *chain;
2273290001Sglebius	unsigned char *p;
2274290001Sglebius#endif
2275290001Sglebius
2276290001Sglebius	EVBUFFER_LOCK(buf);
2277290001Sglebius
2278290001Sglebius	if (buf->freeze_end) {
2279290001Sglebius		result = -1;
2280290001Sglebius		goto done;
2281290001Sglebius	}
2282290001Sglebius
2283290001Sglebius	n = get_n_bytes_readable_on_socket(fd);
2284290001Sglebius	if (n <= 0 || n > EVBUFFER_MAX_READ)
2285290001Sglebius		n = EVBUFFER_MAX_READ;
2286290001Sglebius	if (howmuch < 0 || howmuch > n)
2287290001Sglebius		howmuch = n;
2288290001Sglebius
2289290001Sglebius#ifdef USE_IOVEC_IMPL
2290290001Sglebius	/* Since we can use iovecs, we're willing to use the last
2291290001Sglebius	 * NUM_READ_IOVEC chains. */
2292290001Sglebius	if (evbuffer_expand_fast_(buf, howmuch, NUM_READ_IOVEC) == -1) {
2293290001Sglebius		result = -1;
2294290001Sglebius		goto done;
2295290001Sglebius	} else {
2296290001Sglebius		IOV_TYPE vecs[NUM_READ_IOVEC];
2297290001Sglebius#ifdef EVBUFFER_IOVEC_IS_NATIVE_
2298290001Sglebius		nvecs = evbuffer_read_setup_vecs_(buf, howmuch, vecs,
2299290001Sglebius		    NUM_READ_IOVEC, &chainp, 1);
2300290001Sglebius#else
2301290001Sglebius		/* We aren't using the native struct iovec.  Therefore,
2302290001Sglebius		   we are on win32. */
2303290001Sglebius		struct evbuffer_iovec ev_vecs[NUM_READ_IOVEC];
2304290001Sglebius		nvecs = evbuffer_read_setup_vecs_(buf, howmuch, ev_vecs, 2,
2305290001Sglebius		    &chainp, 1);
2306290001Sglebius
2307290001Sglebius		for (i=0; i < nvecs; ++i)
2308290001Sglebius			WSABUF_FROM_EVBUFFER_IOV(&vecs[i], &ev_vecs[i]);
2309290001Sglebius#endif
2310290001Sglebius
2311290001Sglebius#ifdef _WIN32
2312290001Sglebius		{
2313290001Sglebius			DWORD bytesRead;
2314290001Sglebius			DWORD flags=0;
2315290001Sglebius			if (WSARecv(fd, vecs, nvecs, &bytesRead, &flags, NULL, NULL)) {
2316290001Sglebius				/* The read failed. It might be a close,
2317290001Sglebius				 * or it might be an error. */
2318290001Sglebius				if (WSAGetLastError() == WSAECONNABORTED)
2319290001Sglebius					n = 0;
2320290001Sglebius				else
2321290001Sglebius					n = -1;
2322290001Sglebius			} else
2323290001Sglebius				n = bytesRead;
2324290001Sglebius		}
2325290001Sglebius#else
2326290001Sglebius		n = readv(fd, vecs, nvecs);
2327290001Sglebius#endif
2328290001Sglebius	}
2329290001Sglebius
2330290001Sglebius#else /*!USE_IOVEC_IMPL*/
2331290001Sglebius	/* If we don't have FIONREAD, we might waste some space here */
2332290001Sglebius	/* XXX we _will_ waste some space here if there is any space left
2333290001Sglebius	 * over on buf->last. */
2334290001Sglebius	if ((chain = evbuffer_expand_singlechain(buf, howmuch)) == NULL) {
2335290001Sglebius		result = -1;
2336290001Sglebius		goto done;
2337290001Sglebius	}
2338290001Sglebius
2339290001Sglebius	/* We can append new data at this point */
2340290001Sglebius	p = chain->buffer + chain->misalign + chain->off;
2341290001Sglebius
2342290001Sglebius#ifndef _WIN32
2343290001Sglebius	n = read(fd, p, howmuch);
2344290001Sglebius#else
2345290001Sglebius	n = recv(fd, p, howmuch, 0);
2346290001Sglebius#endif
2347290001Sglebius#endif /* USE_IOVEC_IMPL */
2348290001Sglebius
2349290001Sglebius	if (n == -1) {
2350290001Sglebius		result = -1;
2351290001Sglebius		goto done;
2352290001Sglebius	}
2353290001Sglebius	if (n == 0) {
2354290001Sglebius		result = 0;
2355290001Sglebius		goto done;
2356290001Sglebius	}
2357290001Sglebius
2358290001Sglebius#ifdef USE_IOVEC_IMPL
2359290001Sglebius	remaining = n;
2360290001Sglebius	for (i=0; i < nvecs; ++i) {
2361290001Sglebius		/* can't overflow, since only mutable chains have
2362290001Sglebius		 * huge misaligns. */
2363290001Sglebius		size_t space = (size_t) CHAIN_SPACE_LEN(*chainp);
2364290001Sglebius		/* XXXX This is a kludge that can waste space in perverse
2365290001Sglebius		 * situations. */
2366290001Sglebius		if (space > EVBUFFER_CHAIN_MAX)
2367290001Sglebius			space = EVBUFFER_CHAIN_MAX;
2368290001Sglebius		if ((ev_ssize_t)space < remaining) {
2369290001Sglebius			(*chainp)->off += space;
2370290001Sglebius			remaining -= (int)space;
2371290001Sglebius		} else {
2372290001Sglebius			(*chainp)->off += remaining;
2373290001Sglebius			buf->last_with_datap = chainp;
2374290001Sglebius			break;
2375290001Sglebius		}
2376290001Sglebius		chainp = &(*chainp)->next;
2377290001Sglebius	}
2378290001Sglebius#else
2379290001Sglebius	chain->off += n;
2380290001Sglebius	advance_last_with_data(buf);
2381290001Sglebius#endif
2382290001Sglebius	buf->total_len += n;
2383290001Sglebius	buf->n_add_for_cb += n;
2384290001Sglebius
2385290001Sglebius	/* Tell someone about changes in this buffer */
2386290001Sglebius	evbuffer_invoke_callbacks_(buf);
2387290001Sglebius	result = n;
2388290001Sglebiusdone:
2389290001Sglebius	EVBUFFER_UNLOCK(buf);
2390290001Sglebius	return result;
2391290001Sglebius}
2392290001Sglebius
2393290001Sglebius#ifdef USE_IOVEC_IMPL
2394290001Sglebiusstatic inline int
2395290001Sglebiusevbuffer_write_iovec(struct evbuffer *buffer, evutil_socket_t fd,
2396290001Sglebius    ev_ssize_t howmuch)
2397290001Sglebius{
2398290001Sglebius	IOV_TYPE iov[NUM_WRITE_IOVEC];
2399290001Sglebius	struct evbuffer_chain *chain = buffer->first;
2400290001Sglebius	int n, i = 0;
2401290001Sglebius
2402290001Sglebius	if (howmuch < 0)
2403290001Sglebius		return -1;
2404290001Sglebius
2405290001Sglebius	ASSERT_EVBUFFER_LOCKED(buffer);
2406290001Sglebius	/* XXX make this top out at some maximal data length?  if the
2407290001Sglebius	 * buffer has (say) 1MB in it, split over 128 chains, there's
2408290001Sglebius	 * no way it all gets written in one go. */
2409290001Sglebius	while (chain != NULL && i < NUM_WRITE_IOVEC && howmuch) {
2410290001Sglebius#ifdef USE_SENDFILE
2411290001Sglebius		/* we cannot write the file info via writev */
2412290001Sglebius		if (chain->flags & EVBUFFER_SENDFILE)
2413290001Sglebius			break;
2414290001Sglebius#endif
2415290001Sglebius		iov[i].IOV_PTR_FIELD = (void *) (chain->buffer + chain->misalign);
2416290001Sglebius		if ((size_t)howmuch >= chain->off) {
2417290001Sglebius			/* XXXcould be problematic when windows supports mmap*/
2418290001Sglebius			iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)chain->off;
2419290001Sglebius			howmuch -= chain->off;
2420290001Sglebius		} else {
2421290001Sglebius			/* XXXcould be problematic when windows supports mmap*/
2422290001Sglebius			iov[i++].IOV_LEN_FIELD = (IOV_LEN_TYPE)howmuch;
2423290001Sglebius			break;
2424290001Sglebius		}
2425290001Sglebius		chain = chain->next;
2426290001Sglebius	}
2427290001Sglebius	if (! i)
2428290001Sglebius		return 0;
2429290001Sglebius
2430290001Sglebius#ifdef _WIN32
2431290001Sglebius	{
2432290001Sglebius		DWORD bytesSent;
2433290001Sglebius		if (WSASend(fd, iov, i, &bytesSent, 0, NULL, NULL))
2434290001Sglebius			n = -1;
2435290001Sglebius		else
2436290001Sglebius			n = bytesSent;
2437290001Sglebius	}
2438290001Sglebius#else
2439290001Sglebius	n = writev(fd, iov, i);
2440290001Sglebius#endif
2441290001Sglebius	return (n);
2442290001Sglebius}
2443290001Sglebius#endif
2444290001Sglebius
2445290001Sglebius#ifdef USE_SENDFILE
2446290001Sglebiusstatic inline int
2447290001Sglebiusevbuffer_write_sendfile(struct evbuffer *buffer, evutil_socket_t dest_fd,
2448290001Sglebius    ev_ssize_t howmuch)
2449290001Sglebius{
2450290001Sglebius	struct evbuffer_chain *chain = buffer->first;
2451290001Sglebius	struct evbuffer_chain_file_segment *info =
2452290001Sglebius	    EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment,
2453290001Sglebius		chain);
2454290001Sglebius	const int source_fd = info->segment->fd;
2455290001Sglebius#if defined(SENDFILE_IS_MACOSX) || defined(SENDFILE_IS_FREEBSD)
2456290001Sglebius	int res;
2457290001Sglebius	ev_off_t len = chain->off;
2458290001Sglebius#elif defined(SENDFILE_IS_LINUX) || defined(SENDFILE_IS_SOLARIS)
2459290001Sglebius	ev_ssize_t res;
2460290001Sglebius	ev_off_t offset = chain->misalign;
2461290001Sglebius#endif
2462290001Sglebius
2463290001Sglebius	ASSERT_EVBUFFER_LOCKED(buffer);
2464290001Sglebius
2465290001Sglebius#if defined(SENDFILE_IS_MACOSX)
2466290001Sglebius	res = sendfile(source_fd, dest_fd, chain->misalign, &len, NULL, 0);
2467290001Sglebius	if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
2468290001Sglebius		return (-1);
2469290001Sglebius
2470290001Sglebius	return (len);
2471290001Sglebius#elif defined(SENDFILE_IS_FREEBSD)
2472290001Sglebius	res = sendfile(source_fd, dest_fd, chain->misalign, chain->off, NULL, &len, 0);
2473290001Sglebius	if (res == -1 && !EVUTIL_ERR_RW_RETRIABLE(errno))
2474290001Sglebius		return (-1);
2475290001Sglebius
2476290001Sglebius	return (len);
2477290001Sglebius#elif defined(SENDFILE_IS_LINUX)
2478290001Sglebius	/* TODO(niels): implement splice */
2479290001Sglebius	res = sendfile(dest_fd, source_fd, &offset, chain->off);
2480290001Sglebius	if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
2481290001Sglebius		/* if this is EAGAIN or EINTR return 0; otherwise, -1 */
2482290001Sglebius		return (0);
2483290001Sglebius	}
2484290001Sglebius	return (res);
2485290001Sglebius#elif defined(SENDFILE_IS_SOLARIS)
2486290001Sglebius	{
2487290001Sglebius		const off_t offset_orig = offset;
2488290001Sglebius		res = sendfile(dest_fd, source_fd, &offset, chain->off);
2489290001Sglebius		if (res == -1 && EVUTIL_ERR_RW_RETRIABLE(errno)) {
2490290001Sglebius			if (offset - offset_orig)
2491290001Sglebius				return offset - offset_orig;
2492290001Sglebius			/* if this is EAGAIN or EINTR and no bytes were
2493290001Sglebius			 * written, return 0 */
2494290001Sglebius			return (0);
2495290001Sglebius		}
2496290001Sglebius		return (res);
2497290001Sglebius	}
2498290001Sglebius#endif
2499290001Sglebius}
2500290001Sglebius#endif
2501290001Sglebius
2502290001Sglebiusint
2503290001Sglebiusevbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
2504290001Sglebius    ev_ssize_t howmuch)
2505290001Sglebius{
2506290001Sglebius	int n = -1;
2507290001Sglebius
2508290001Sglebius	EVBUFFER_LOCK(buffer);
2509290001Sglebius
2510290001Sglebius	if (buffer->freeze_start) {
2511290001Sglebius		goto done;
2512290001Sglebius	}
2513290001Sglebius
2514290001Sglebius	if (howmuch < 0 || (size_t)howmuch > buffer->total_len)
2515290001Sglebius		howmuch = buffer->total_len;
2516290001Sglebius
2517290001Sglebius	if (howmuch > 0) {
2518290001Sglebius#ifdef USE_SENDFILE
2519290001Sglebius		struct evbuffer_chain *chain = buffer->first;
2520290001Sglebius		if (chain != NULL && (chain->flags & EVBUFFER_SENDFILE))
2521290001Sglebius			n = evbuffer_write_sendfile(buffer, fd, howmuch);
2522290001Sglebius		else {
2523290001Sglebius#endif
2524290001Sglebius#ifdef USE_IOVEC_IMPL
2525290001Sglebius		n = evbuffer_write_iovec(buffer, fd, howmuch);
2526290001Sglebius#elif defined(_WIN32)
2527290001Sglebius		/* XXX(nickm) Don't disable this code until we know if
2528290001Sglebius		 * the WSARecv code above works. */
2529290001Sglebius		void *p = evbuffer_pullup(buffer, howmuch);
2530290001Sglebius		EVUTIL_ASSERT(p || !howmuch);
2531290001Sglebius		n = send(fd, p, howmuch, 0);
2532290001Sglebius#else
2533290001Sglebius		void *p = evbuffer_pullup(buffer, howmuch);
2534290001Sglebius		EVUTIL_ASSERT(p || !howmuch);
2535290001Sglebius		n = write(fd, p, howmuch);
2536290001Sglebius#endif
2537290001Sglebius#ifdef USE_SENDFILE
2538290001Sglebius		}
2539290001Sglebius#endif
2540290001Sglebius	}
2541290001Sglebius
2542290001Sglebius	if (n > 0)
2543290001Sglebius		evbuffer_drain(buffer, n);
2544290001Sglebius
2545290001Sglebiusdone:
2546290001Sglebius	EVBUFFER_UNLOCK(buffer);
2547290001Sglebius	return (n);
2548290001Sglebius}
2549290001Sglebius
2550290001Sglebiusint
2551290001Sglebiusevbuffer_write(struct evbuffer *buffer, evutil_socket_t fd)
2552290001Sglebius{
2553290001Sglebius	return evbuffer_write_atmost(buffer, fd, -1);
2554290001Sglebius}
2555290001Sglebius
2556290001Sglebiusunsigned char *
2557290001Sglebiusevbuffer_find(struct evbuffer *buffer, const unsigned char *what, size_t len)
2558290001Sglebius{
2559290001Sglebius	unsigned char *search;
2560290001Sglebius	struct evbuffer_ptr ptr;
2561290001Sglebius
2562290001Sglebius	EVBUFFER_LOCK(buffer);
2563290001Sglebius
2564290001Sglebius	ptr = evbuffer_search(buffer, (const char *)what, len, NULL);
2565290001Sglebius	if (ptr.pos < 0) {
2566290001Sglebius		search = NULL;
2567290001Sglebius	} else {
2568290001Sglebius		search = evbuffer_pullup(buffer, ptr.pos + len);
2569290001Sglebius		if (search)
2570290001Sglebius			search += ptr.pos;
2571290001Sglebius	}
2572290001Sglebius	EVBUFFER_UNLOCK(buffer);
2573290001Sglebius	return search;
2574290001Sglebius}
2575290001Sglebius
2576290001Sglebius/* Subract <b>howfar</b> from the position of <b>pos</b> within
2577290001Sglebius * <b>buf</b>. Returns 0 on success, -1 on failure.
2578290001Sglebius *
2579290001Sglebius * This isn't exposed yet, because of potential inefficiency issues.
2580290001Sglebius * Maybe it should be. */
2581290001Sglebiusstatic int
2582290001Sglebiusevbuffer_ptr_subtract(struct evbuffer *buf, struct evbuffer_ptr *pos,
2583290001Sglebius    size_t howfar)
2584290001Sglebius{
2585290001Sglebius	if (pos->pos < 0)
2586290001Sglebius		return -1;
2587290001Sglebius	if (howfar > (size_t)pos->pos)
2588290001Sglebius		return -1;
2589290001Sglebius	if (pos->internal_.chain && howfar <= pos->internal_.pos_in_chain) {
2590290001Sglebius		pos->internal_.pos_in_chain -= howfar;
2591290001Sglebius		pos->pos -= howfar;
2592290001Sglebius		return 0;
2593290001Sglebius	} else {
2594290001Sglebius		const size_t newpos = pos->pos - howfar;
2595290001Sglebius		/* Here's the inefficient part: it walks over the
2596290001Sglebius		 * chains until we hit newpos. */
2597290001Sglebius		return evbuffer_ptr_set(buf, pos, newpos, EVBUFFER_PTR_SET);
2598290001Sglebius	}
2599290001Sglebius}
2600290001Sglebius
2601290001Sglebiusint
2602290001Sglebiusevbuffer_ptr_set(struct evbuffer *buf, struct evbuffer_ptr *pos,
2603290001Sglebius    size_t position, enum evbuffer_ptr_how how)
2604290001Sglebius{
2605290001Sglebius	size_t left = position;
2606290001Sglebius	struct evbuffer_chain *chain = NULL;
2607290001Sglebius	int result = 0;
2608290001Sglebius
2609290001Sglebius	EVBUFFER_LOCK(buf);
2610290001Sglebius
2611290001Sglebius	switch (how) {
2612290001Sglebius	case EVBUFFER_PTR_SET:
2613290001Sglebius		chain = buf->first;
2614290001Sglebius		pos->pos = position;
2615290001Sglebius		position = 0;
2616290001Sglebius		break;
2617290001Sglebius	case EVBUFFER_PTR_ADD:
2618290001Sglebius		/* this avoids iterating over all previous chains if
2619290001Sglebius		   we just want to advance the position */
2620290001Sglebius		if (pos->pos < 0 || EV_SIZE_MAX - position < (size_t)pos->pos) {
2621290001Sglebius			EVBUFFER_UNLOCK(buf);
2622290001Sglebius			return -1;
2623290001Sglebius		}
2624290001Sglebius		chain = pos->internal_.chain;
2625290001Sglebius		pos->pos += position;
2626290001Sglebius		position = pos->internal_.pos_in_chain;
2627290001Sglebius		break;
2628290001Sglebius	}
2629290001Sglebius
2630290001Sglebius	EVUTIL_ASSERT(EV_SIZE_MAX - left >= position);
2631290001Sglebius	while (chain && position + left >= chain->off) {
2632290001Sglebius		left -= chain->off - position;
2633290001Sglebius		chain = chain->next;
2634290001Sglebius		position = 0;
2635290001Sglebius	}
2636290001Sglebius	if (chain) {
2637290001Sglebius		pos->internal_.chain = chain;
2638290001Sglebius		pos->internal_.pos_in_chain = position + left;
2639290001Sglebius	} else if (left == 0) {
2640290001Sglebius		/* The first byte in the (nonexistent) chain after the last chain */
2641290001Sglebius		pos->internal_.chain = NULL;
2642290001Sglebius		pos->internal_.pos_in_chain = 0;
2643290001Sglebius	} else {
2644290001Sglebius		PTR_NOT_FOUND(pos);
2645290001Sglebius		result = -1;
2646290001Sglebius	}
2647290001Sglebius
2648290001Sglebius	EVBUFFER_UNLOCK(buf);
2649290001Sglebius
2650290001Sglebius	return result;
2651290001Sglebius}
2652290001Sglebius
2653290001Sglebius/**
2654290001Sglebius   Compare the bytes in buf at position pos to the len bytes in mem.  Return
2655290001Sglebius   less than 0, 0, or greater than 0 as memcmp.
2656290001Sglebius */
2657290001Sglebiusstatic int
2658290001Sglebiusevbuffer_ptr_memcmp(const struct evbuffer *buf, const struct evbuffer_ptr *pos,
2659290001Sglebius    const char *mem, size_t len)
2660290001Sglebius{
2661290001Sglebius	struct evbuffer_chain *chain;
2662290001Sglebius	size_t position;
2663290001Sglebius	int r;
2664290001Sglebius
2665290001Sglebius	ASSERT_EVBUFFER_LOCKED(buf);
2666290001Sglebius
2667290001Sglebius	if (pos->pos < 0 ||
2668290001Sglebius	    EV_SIZE_MAX - len < (size_t)pos->pos ||
2669290001Sglebius	    pos->pos + len > buf->total_len)
2670290001Sglebius		return -1;
2671290001Sglebius
2672290001Sglebius	chain = pos->internal_.chain;
2673290001Sglebius	position = pos->internal_.pos_in_chain;
2674290001Sglebius	while (len && chain) {
2675290001Sglebius		size_t n_comparable;
2676290001Sglebius		if (len + position > chain->off)
2677290001Sglebius			n_comparable = chain->off - position;
2678290001Sglebius		else
2679290001Sglebius			n_comparable = len;
2680290001Sglebius		r = memcmp(chain->buffer + chain->misalign + position, mem,
2681290001Sglebius		    n_comparable);
2682290001Sglebius		if (r)
2683290001Sglebius			return r;
2684290001Sglebius		mem += n_comparable;
2685290001Sglebius		len -= n_comparable;
2686290001Sglebius		position = 0;
2687290001Sglebius		chain = chain->next;
2688290001Sglebius	}
2689290001Sglebius
2690290001Sglebius	return 0;
2691290001Sglebius}
2692290001Sglebius
2693290001Sglebiusstruct evbuffer_ptr
2694290001Sglebiusevbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start)
2695290001Sglebius{
2696290001Sglebius	return evbuffer_search_range(buffer, what, len, start, NULL);
2697290001Sglebius}
2698290001Sglebius
2699290001Sglebiusstruct evbuffer_ptr
2700290001Sglebiusevbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end)
2701290001Sglebius{
2702290001Sglebius	struct evbuffer_ptr pos;
2703290001Sglebius	struct evbuffer_chain *chain, *last_chain = NULL;
2704290001Sglebius	const unsigned char *p;
2705290001Sglebius	char first;
2706290001Sglebius
2707290001Sglebius	EVBUFFER_LOCK(buffer);
2708290001Sglebius
2709290001Sglebius	if (start) {
2710290001Sglebius		memcpy(&pos, start, sizeof(pos));
2711290001Sglebius		chain = pos.internal_.chain;
2712290001Sglebius	} else {
2713290001Sglebius		pos.pos = 0;
2714290001Sglebius		chain = pos.internal_.chain = buffer->first;
2715290001Sglebius		pos.internal_.pos_in_chain = 0;
2716290001Sglebius	}
2717290001Sglebius
2718290001Sglebius	if (end)
2719290001Sglebius		last_chain = end->internal_.chain;
2720290001Sglebius
2721290001Sglebius	if (!len || len > EV_SSIZE_MAX)
2722290001Sglebius		goto done;
2723290001Sglebius
2724290001Sglebius	first = what[0];
2725290001Sglebius
2726290001Sglebius	while (chain) {
2727290001Sglebius		const unsigned char *start_at =
2728290001Sglebius		    chain->buffer + chain->misalign +
2729290001Sglebius		    pos.internal_.pos_in_chain;
2730290001Sglebius		p = memchr(start_at, first,
2731290001Sglebius		    chain->off - pos.internal_.pos_in_chain);
2732290001Sglebius		if (p) {
2733290001Sglebius			pos.pos += p - start_at;
2734290001Sglebius			pos.internal_.pos_in_chain += p - start_at;
2735290001Sglebius			if (!evbuffer_ptr_memcmp(buffer, &pos, what, len)) {
2736290001Sglebius				if (end && pos.pos + (ev_ssize_t)len > end->pos)
2737290001Sglebius					goto not_found;
2738290001Sglebius				else
2739290001Sglebius					goto done;
2740290001Sglebius			}
2741290001Sglebius			++pos.pos;
2742290001Sglebius			++pos.internal_.pos_in_chain;
2743290001Sglebius			if (pos.internal_.pos_in_chain == chain->off) {
2744290001Sglebius				chain = pos.internal_.chain = chain->next;
2745290001Sglebius				pos.internal_.pos_in_chain = 0;
2746290001Sglebius			}
2747290001Sglebius		} else {
2748290001Sglebius			if (chain == last_chain)
2749290001Sglebius				goto not_found;
2750290001Sglebius			pos.pos += chain->off - pos.internal_.pos_in_chain;
2751290001Sglebius			chain = pos.internal_.chain = chain->next;
2752290001Sglebius			pos.internal_.pos_in_chain = 0;
2753290001Sglebius		}
2754290001Sglebius	}
2755290001Sglebius
2756290001Sglebiusnot_found:
2757290001Sglebius	PTR_NOT_FOUND(&pos);
2758290001Sglebiusdone:
2759290001Sglebius	EVBUFFER_UNLOCK(buffer);
2760290001Sglebius	return pos;
2761290001Sglebius}
2762290001Sglebius
2763290001Sglebiusint
2764290001Sglebiusevbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
2765290001Sglebius    struct evbuffer_ptr *start_at,
2766290001Sglebius    struct evbuffer_iovec *vec, int n_vec)
2767290001Sglebius{
2768290001Sglebius	struct evbuffer_chain *chain;
2769290001Sglebius	int idx = 0;
2770290001Sglebius	ev_ssize_t len_so_far = 0;
2771290001Sglebius
2772290001Sglebius	/* Avoid locking in trivial edge cases */
2773290001Sglebius	if (start_at && start_at->internal_.chain == NULL)
2774290001Sglebius		return 0;
2775290001Sglebius
2776290001Sglebius	EVBUFFER_LOCK(buffer);
2777290001Sglebius
2778290001Sglebius	if (start_at) {
2779290001Sglebius		chain = start_at->internal_.chain;
2780290001Sglebius		len_so_far = chain->off
2781290001Sglebius		    - start_at->internal_.pos_in_chain;
2782290001Sglebius		idx = 1;
2783290001Sglebius		if (n_vec > 0) {
2784290001Sglebius			vec[0].iov_base = chain->buffer + chain->misalign
2785290001Sglebius			    + start_at->internal_.pos_in_chain;
2786290001Sglebius			vec[0].iov_len = len_so_far;
2787290001Sglebius		}
2788290001Sglebius		chain = chain->next;
2789290001Sglebius	} else {
2790290001Sglebius		chain = buffer->first;
2791290001Sglebius	}
2792290001Sglebius
2793290001Sglebius	if (n_vec == 0 && len < 0) {
2794290001Sglebius		/* If no vectors are provided and they asked for "everything",
2795290001Sglebius		 * pretend they asked for the actual available amount. */
2796290001Sglebius		len = buffer->total_len;
2797290001Sglebius		if (start_at) {
2798290001Sglebius			len -= start_at->pos;
2799290001Sglebius		}
2800290001Sglebius	}
2801290001Sglebius
2802290001Sglebius	while (chain) {
2803290001Sglebius		if (len >= 0 && len_so_far >= len)
2804290001Sglebius			break;
2805290001Sglebius		if (idx<n_vec) {
2806290001Sglebius			vec[idx].iov_base = chain->buffer + chain->misalign;
2807290001Sglebius			vec[idx].iov_len = chain->off;
2808290001Sglebius		} else if (len<0) {
2809290001Sglebius			break;
2810290001Sglebius		}
2811290001Sglebius		++idx;
2812290001Sglebius		len_so_far += chain->off;
2813290001Sglebius		chain = chain->next;
2814290001Sglebius	}
2815290001Sglebius
2816290001Sglebius	EVBUFFER_UNLOCK(buffer);
2817290001Sglebius
2818290001Sglebius	return idx;
2819290001Sglebius}
2820290001Sglebius
2821290001Sglebius
2822290001Sglebiusint
2823290001Sglebiusevbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
2824290001Sglebius{
2825290001Sglebius	char *buffer;
2826290001Sglebius	size_t space;
2827290001Sglebius	int sz, result = -1;
2828290001Sglebius	va_list aq;
2829290001Sglebius	struct evbuffer_chain *chain;
2830290001Sglebius
2831290001Sglebius
2832290001Sglebius	EVBUFFER_LOCK(buf);
2833290001Sglebius
2834290001Sglebius	if (buf->freeze_end) {
2835290001Sglebius		goto done;
2836290001Sglebius	}
2837290001Sglebius
2838290001Sglebius	/* make sure that at least some space is available */
2839290001Sglebius	if ((chain = evbuffer_expand_singlechain(buf, 64)) == NULL)
2840290001Sglebius		goto done;
2841290001Sglebius
2842290001Sglebius	for (;;) {
2843290001Sglebius#if 0
2844290001Sglebius		size_t used = chain->misalign + chain->off;
2845290001Sglebius		buffer = (char *)chain->buffer + chain->misalign + chain->off;
2846290001Sglebius		EVUTIL_ASSERT(chain->buffer_len >= used);
2847290001Sglebius		space = chain->buffer_len - used;
2848290001Sglebius#endif
2849290001Sglebius		buffer = (char*) CHAIN_SPACE_PTR(chain);
2850290001Sglebius		space = (size_t) CHAIN_SPACE_LEN(chain);
2851290001Sglebius
2852290001Sglebius#ifndef va_copy
2853290001Sglebius#define	va_copy(dst, src)	memcpy(&(dst), &(src), sizeof(va_list))
2854290001Sglebius#endif
2855290001Sglebius		va_copy(aq, ap);
2856290001Sglebius
2857290001Sglebius		sz = evutil_vsnprintf(buffer, space, fmt, aq);
2858290001Sglebius
2859290001Sglebius		va_end(aq);
2860290001Sglebius
2861290001Sglebius		if (sz < 0)
2862290001Sglebius			goto done;
2863290001Sglebius		if (INT_MAX >= EVBUFFER_CHAIN_MAX &&
2864290001Sglebius		    (size_t)sz >= EVBUFFER_CHAIN_MAX)
2865290001Sglebius			goto done;
2866290001Sglebius		if ((size_t)sz < space) {
2867290001Sglebius			chain->off += sz;
2868290001Sglebius			buf->total_len += sz;
2869290001Sglebius			buf->n_add_for_cb += sz;
2870290001Sglebius
2871290001Sglebius			advance_last_with_data(buf);
2872290001Sglebius			evbuffer_invoke_callbacks_(buf);
2873290001Sglebius			result = sz;
2874290001Sglebius			goto done;
2875290001Sglebius		}
2876290001Sglebius		if ((chain = evbuffer_expand_singlechain(buf, sz + 1)) == NULL)
2877290001Sglebius			goto done;
2878290001Sglebius	}
2879290001Sglebius	/* NOTREACHED */
2880290001Sglebius
2881290001Sglebiusdone:
2882290001Sglebius	EVBUFFER_UNLOCK(buf);
2883290001Sglebius	return result;
2884290001Sglebius}
2885290001Sglebius
2886290001Sglebiusint
2887290001Sglebiusevbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
2888290001Sglebius{
2889290001Sglebius	int res = -1;
2890290001Sglebius	va_list ap;
2891290001Sglebius
2892290001Sglebius	va_start(ap, fmt);
2893290001Sglebius	res = evbuffer_add_vprintf(buf, fmt, ap);
2894290001Sglebius	va_end(ap);
2895290001Sglebius
2896290001Sglebius	return (res);
2897290001Sglebius}
2898290001Sglebius
2899290001Sglebiusint
2900290001Sglebiusevbuffer_add_reference(struct evbuffer *outbuf,
2901290001Sglebius    const void *data, size_t datlen,
2902290001Sglebius    evbuffer_ref_cleanup_cb cleanupfn, void *extra)
2903290001Sglebius{
2904290001Sglebius	struct evbuffer_chain *chain;
2905290001Sglebius	struct evbuffer_chain_reference *info;
2906290001Sglebius	int result = -1;
2907290001Sglebius
2908290001Sglebius	chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_reference));
2909290001Sglebius	if (!chain)
2910290001Sglebius		return (-1);
2911290001Sglebius	chain->flags |= EVBUFFER_REFERENCE | EVBUFFER_IMMUTABLE;
2912290001Sglebius	chain->buffer = (u_char *)data;
2913290001Sglebius	chain->buffer_len = datlen;
2914290001Sglebius	chain->off = datlen;
2915290001Sglebius
2916290001Sglebius	info = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_reference, chain);
2917290001Sglebius	info->cleanupfn = cleanupfn;
2918290001Sglebius	info->extra = extra;
2919290001Sglebius
2920290001Sglebius	EVBUFFER_LOCK(outbuf);
2921290001Sglebius	if (outbuf->freeze_end) {
2922290001Sglebius		/* don't call chain_free; we do not want to actually invoke
2923290001Sglebius		 * the cleanup function */
2924290001Sglebius		mm_free(chain);
2925290001Sglebius		goto done;
2926290001Sglebius	}
2927290001Sglebius	evbuffer_chain_insert(outbuf, chain);
2928290001Sglebius	outbuf->n_add_for_cb += datlen;
2929290001Sglebius
2930290001Sglebius	evbuffer_invoke_callbacks_(outbuf);
2931290001Sglebius
2932290001Sglebius	result = 0;
2933290001Sglebiusdone:
2934290001Sglebius	EVBUFFER_UNLOCK(outbuf);
2935290001Sglebius
2936290001Sglebius	return result;
2937290001Sglebius}
2938290001Sglebius
2939290001Sglebius/* TODO(niels): we may want to add to automagically convert to mmap, in
2940290001Sglebius * case evbuffer_remove() or evbuffer_pullup() are being used.
2941290001Sglebius */
2942290001Sglebiusstruct evbuffer_file_segment *
2943290001Sglebiusevbuffer_file_segment_new(
2944290001Sglebius	int fd, ev_off_t offset, ev_off_t length, unsigned flags)
2945290001Sglebius{
2946290001Sglebius	struct evbuffer_file_segment *seg =
2947290001Sglebius	    mm_calloc(sizeof(struct evbuffer_file_segment), 1);
2948290001Sglebius	if (!seg)
2949290001Sglebius		return NULL;
2950290001Sglebius	seg->refcnt = 1;
2951290001Sglebius	seg->fd = fd;
2952290001Sglebius	seg->flags = flags;
2953290001Sglebius	seg->file_offset = offset;
2954290001Sglebius	seg->cleanup_cb = NULL;
2955290001Sglebius	seg->cleanup_cb_arg = NULL;
2956290001Sglebius#ifdef _WIN32
2957290001Sglebius#ifndef lseek
2958290001Sglebius#define lseek _lseeki64
2959290001Sglebius#endif
2960290001Sglebius#ifndef fstat
2961290001Sglebius#define fstat _fstat
2962290001Sglebius#endif
2963290001Sglebius#ifndef stat
2964290001Sglebius#define stat _stat
2965290001Sglebius#endif
2966290001Sglebius#endif
2967290001Sglebius	if (length == -1) {
2968290001Sglebius		struct stat st;
2969290001Sglebius		if (fstat(fd, &st) < 0)
2970290001Sglebius			goto err;
2971290001Sglebius		length = st.st_size;
2972290001Sglebius	}
2973290001Sglebius	seg->length = length;
2974290001Sglebius
2975290001Sglebius	if (offset < 0 || length < 0 ||
2976290001Sglebius	    ((ev_uint64_t)length > EVBUFFER_CHAIN_MAX) ||
2977290001Sglebius	    (ev_uint64_t)offset > (ev_uint64_t)(EVBUFFER_CHAIN_MAX - length))
2978290001Sglebius		goto err;
2979290001Sglebius
2980290001Sglebius#if defined(USE_SENDFILE)
2981290001Sglebius	if (!(flags & EVBUF_FS_DISABLE_SENDFILE)) {
2982290001Sglebius		seg->can_sendfile = 1;
2983290001Sglebius		goto done;
2984290001Sglebius	}
2985290001Sglebius#endif
2986290001Sglebius
2987290001Sglebius	if (evbuffer_file_segment_materialize(seg)<0)
2988290001Sglebius		goto err;
2989290001Sglebius
2990290001Sglebius#if defined(USE_SENDFILE)
2991290001Sglebiusdone:
2992290001Sglebius#endif
2993290001Sglebius	if (!(flags & EVBUF_FS_DISABLE_LOCKING)) {
2994290001Sglebius		EVTHREAD_ALLOC_LOCK(seg->lock, 0);
2995290001Sglebius	}
2996290001Sglebius	return seg;
2997290001Sglebiuserr:
2998290001Sglebius	mm_free(seg);
2999290001Sglebius	return NULL;
3000290001Sglebius}
3001290001Sglebius
3002290001Sglebius#ifdef EVENT__HAVE_MMAP
3003290001Sglebiusstatic long
3004290001Sglebiusget_page_size(void)
3005290001Sglebius{
3006290001Sglebius#ifdef SC_PAGE_SIZE
3007290001Sglebius	return sysconf(SC_PAGE_SIZE);
3008290001Sglebius#elif defined(_SC_PAGE_SIZE)
3009290001Sglebius	return sysconf(_SC_PAGE_SIZE);
3010290001Sglebius#else
3011290001Sglebius	return 1;
3012290001Sglebius#endif
3013290001Sglebius}
3014290001Sglebius#endif
3015290001Sglebius
3016290001Sglebius/* DOCDOC */
3017290001Sglebius/* Requires lock */
3018290001Sglebiusstatic int
3019290001Sglebiusevbuffer_file_segment_materialize(struct evbuffer_file_segment *seg)
3020290001Sglebius{
3021290001Sglebius	const unsigned flags = seg->flags;
3022290001Sglebius	const int fd = seg->fd;
3023290001Sglebius	const ev_off_t length = seg->length;
3024290001Sglebius	const ev_off_t offset = seg->file_offset;
3025290001Sglebius
3026290001Sglebius	if (seg->contents)
3027290001Sglebius		return 0; /* already materialized */
3028290001Sglebius
3029290001Sglebius#if defined(EVENT__HAVE_MMAP)
3030290001Sglebius	if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
3031290001Sglebius		off_t offset_rounded = 0, offset_leftover = 0;
3032290001Sglebius		void *mapped;
3033290001Sglebius		if (offset) {
3034290001Sglebius			/* mmap implementations don't generally like us
3035290001Sglebius			 * to have an offset that isn't a round  */
3036290001Sglebius			long page_size = get_page_size();
3037290001Sglebius			if (page_size == -1)
3038290001Sglebius				goto err;
3039290001Sglebius			offset_leftover = offset % page_size;
3040290001Sglebius			offset_rounded = offset - offset_leftover;
3041290001Sglebius		}
3042290001Sglebius		mapped = mmap(NULL, length + offset_leftover,
3043290001Sglebius		    PROT_READ,
3044290001Sglebius#ifdef MAP_NOCACHE
3045290001Sglebius		    MAP_NOCACHE | /* ??? */
3046290001Sglebius#endif
3047290001Sglebius#ifdef MAP_FILE
3048290001Sglebius		    MAP_FILE |
3049290001Sglebius#endif
3050290001Sglebius		    MAP_PRIVATE,
3051290001Sglebius		    fd, offset_rounded);
3052290001Sglebius		if (mapped == MAP_FAILED) {
3053290001Sglebius			event_warn("%s: mmap(%d, %d, %zu) failed",
3054290001Sglebius			    __func__, fd, 0, (size_t)(offset + length));
3055290001Sglebius		} else {
3056290001Sglebius			seg->mapping = mapped;
3057290001Sglebius			seg->contents = (char*)mapped+offset_leftover;
3058290001Sglebius			seg->mmap_offset = 0;
3059290001Sglebius			seg->is_mapping = 1;
3060290001Sglebius			goto done;
3061290001Sglebius		}
3062290001Sglebius	}
3063290001Sglebius#endif
3064290001Sglebius#ifdef _WIN32
3065290001Sglebius	if (!(flags & EVBUF_FS_DISABLE_MMAP)) {
3066290001Sglebius		intptr_t h = _get_osfhandle(fd);
3067290001Sglebius		HANDLE m;
3068290001Sglebius		ev_uint64_t total_size = length+offset;
3069290001Sglebius		if ((HANDLE)h == INVALID_HANDLE_VALUE)
3070290001Sglebius			goto err;
3071290001Sglebius		m = CreateFileMapping((HANDLE)h, NULL, PAGE_READONLY,
3072290001Sglebius		    (total_size >> 32), total_size & 0xfffffffful,
3073290001Sglebius		    NULL);
3074290001Sglebius		if (m != INVALID_HANDLE_VALUE) { /* Does h leak? */
3075290001Sglebius			seg->mapping_handle = m;
3076290001Sglebius			seg->mmap_offset = offset;
3077290001Sglebius			seg->is_mapping = 1;
3078290001Sglebius			goto done;
3079290001Sglebius		}
3080290001Sglebius	}
3081290001Sglebius#endif
3082290001Sglebius	{
3083290001Sglebius		ev_off_t start_pos = lseek(fd, 0, SEEK_CUR), pos;
3084290001Sglebius		ev_off_t read_so_far = 0;
3085290001Sglebius		char *mem;
3086290001Sglebius		int e;
3087290001Sglebius		ev_ssize_t n = 0;
3088290001Sglebius		if (!(mem = mm_malloc(length)))
3089290001Sglebius			goto err;
3090290001Sglebius		if (start_pos < 0) {
3091290001Sglebius			mm_free(mem);
3092290001Sglebius			goto err;
3093290001Sglebius		}
3094290001Sglebius		if (lseek(fd, offset, SEEK_SET) < 0) {
3095290001Sglebius			mm_free(mem);
3096290001Sglebius			goto err;
3097290001Sglebius		}
3098290001Sglebius		while (read_so_far < length) {
3099290001Sglebius			n = read(fd, mem+read_so_far, length-read_so_far);
3100290001Sglebius			if (n <= 0)
3101290001Sglebius				break;
3102290001Sglebius			read_so_far += n;
3103290001Sglebius		}
3104290001Sglebius
3105290001Sglebius		e = errno;
3106290001Sglebius		pos = lseek(fd, start_pos, SEEK_SET);
3107290001Sglebius		if (n < 0 || (n == 0 && length > read_so_far)) {
3108290001Sglebius			mm_free(mem);
3109290001Sglebius			errno = e;
3110290001Sglebius			goto err;
3111290001Sglebius		} else if (pos < 0) {
3112290001Sglebius			mm_free(mem);
3113290001Sglebius			goto err;
3114290001Sglebius		}
3115290001Sglebius
3116290001Sglebius		seg->contents = mem;
3117290001Sglebius	}
3118290001Sglebius
3119290001Sglebiusdone:
3120290001Sglebius	return 0;
3121290001Sglebiuserr:
3122290001Sglebius	return -1;
3123290001Sglebius}
3124290001Sglebius
3125290001Sglebiusvoid evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
3126290001Sglebius	evbuffer_file_segment_cleanup_cb cb, void* arg)
3127290001Sglebius{
3128290001Sglebius	EVUTIL_ASSERT(seg->refcnt > 0);
3129290001Sglebius	seg->cleanup_cb = cb;
3130290001Sglebius	seg->cleanup_cb_arg = arg;
3131290001Sglebius}
3132290001Sglebius
3133290001Sglebiusvoid
3134290001Sglebiusevbuffer_file_segment_free(struct evbuffer_file_segment *seg)
3135290001Sglebius{
3136290001Sglebius	int refcnt;
3137290001Sglebius	EVLOCK_LOCK(seg->lock, 0);
3138290001Sglebius	refcnt = --seg->refcnt;
3139290001Sglebius	EVLOCK_UNLOCK(seg->lock, 0);
3140290001Sglebius	if (refcnt > 0)
3141290001Sglebius		return;
3142290001Sglebius	EVUTIL_ASSERT(refcnt == 0);
3143290001Sglebius
3144290001Sglebius	if (seg->is_mapping) {
3145290001Sglebius#ifdef _WIN32
3146290001Sglebius		CloseHandle(seg->mapping_handle);
3147290001Sglebius#elif defined (EVENT__HAVE_MMAP)
3148290001Sglebius		off_t offset_leftover;
3149290001Sglebius		offset_leftover = seg->file_offset % get_page_size();
3150290001Sglebius		if (munmap(seg->mapping, seg->length + offset_leftover) == -1)
3151290001Sglebius			event_warn("%s: munmap failed", __func__);
3152290001Sglebius#endif
3153290001Sglebius	} else if (seg->contents) {
3154290001Sglebius		mm_free(seg->contents);
3155290001Sglebius	}
3156290001Sglebius
3157290001Sglebius	if ((seg->flags & EVBUF_FS_CLOSE_ON_FREE) && seg->fd >= 0) {
3158290001Sglebius		close(seg->fd);
3159290001Sglebius	}
3160290001Sglebius
3161290001Sglebius	if (seg->cleanup_cb) {
3162290001Sglebius		(*seg->cleanup_cb)((struct evbuffer_file_segment const*)seg,
3163290001Sglebius		    seg->flags, seg->cleanup_cb_arg);
3164290001Sglebius		seg->cleanup_cb = NULL;
3165290001Sglebius		seg->cleanup_cb_arg = NULL;
3166290001Sglebius	}
3167290001Sglebius
3168290001Sglebius	EVTHREAD_FREE_LOCK(seg->lock, 0);
3169290001Sglebius	mm_free(seg);
3170290001Sglebius}
3171290001Sglebius
3172290001Sglebiusint
3173290001Sglebiusevbuffer_add_file_segment(struct evbuffer *buf,
3174290001Sglebius    struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length)
3175290001Sglebius{
3176290001Sglebius	struct evbuffer_chain *chain;
3177290001Sglebius	struct evbuffer_chain_file_segment *extra;
3178290001Sglebius	int can_use_sendfile = 0;
3179290001Sglebius
3180290001Sglebius	EVBUFFER_LOCK(buf);
3181290001Sglebius	EVLOCK_LOCK(seg->lock, 0);
3182290001Sglebius	if (buf->flags & EVBUFFER_FLAG_DRAINS_TO_FD) {
3183290001Sglebius		can_use_sendfile = 1;
3184290001Sglebius	} else {
3185290001Sglebius		if (!seg->contents) {
3186290001Sglebius			if (evbuffer_file_segment_materialize(seg)<0) {
3187290001Sglebius				EVLOCK_UNLOCK(seg->lock, 0);
3188290001Sglebius				EVBUFFER_UNLOCK(buf);
3189290001Sglebius				return -1;
3190290001Sglebius			}
3191290001Sglebius		}
3192290001Sglebius	}
3193290001Sglebius	++seg->refcnt;
3194290001Sglebius	EVLOCK_UNLOCK(seg->lock, 0);
3195290001Sglebius
3196290001Sglebius	if (buf->freeze_end)
3197290001Sglebius		goto err;
3198290001Sglebius
3199290001Sglebius	if (length < 0) {
3200290001Sglebius		if (offset > seg->length)
3201290001Sglebius			goto err;
3202290001Sglebius		length = seg->length - offset;
3203290001Sglebius	}
3204290001Sglebius
3205290001Sglebius	/* Can we actually add this? */
3206290001Sglebius	if (offset+length > seg->length)
3207290001Sglebius		goto err;
3208290001Sglebius
3209290001Sglebius	chain = evbuffer_chain_new(sizeof(struct evbuffer_chain_file_segment));
3210290001Sglebius	if (!chain)
3211290001Sglebius		goto err;
3212290001Sglebius	extra = EVBUFFER_CHAIN_EXTRA(struct evbuffer_chain_file_segment, chain);
3213290001Sglebius
3214290001Sglebius	chain->flags |= EVBUFFER_IMMUTABLE|EVBUFFER_FILESEGMENT;
3215290001Sglebius	if (can_use_sendfile && seg->can_sendfile) {
3216290001Sglebius		chain->flags |= EVBUFFER_SENDFILE;
3217290001Sglebius		chain->misalign = seg->file_offset + offset;
3218290001Sglebius		chain->off = length;
3219290001Sglebius		chain->buffer_len = chain->misalign + length;
3220290001Sglebius	} else if (seg->is_mapping) {
3221290001Sglebius#ifdef _WIN32
3222290001Sglebius		ev_uint64_t total_offset = seg->mmap_offset+offset;
3223290001Sglebius		ev_uint64_t offset_rounded=0, offset_remaining=0;
3224290001Sglebius		LPVOID data;
3225290001Sglebius		if (total_offset) {
3226290001Sglebius			SYSTEM_INFO si;
3227290001Sglebius			memset(&si, 0, sizeof(si)); /* cargo cult */
3228290001Sglebius			GetSystemInfo(&si);
3229290001Sglebius			offset_remaining = total_offset % si.dwAllocationGranularity;
3230290001Sglebius			offset_rounded = total_offset - offset_remaining;
3231290001Sglebius		}
3232290001Sglebius		data = MapViewOfFile(
3233290001Sglebius			seg->mapping_handle,
3234290001Sglebius			FILE_MAP_READ,
3235290001Sglebius			offset_rounded >> 32,
3236290001Sglebius			offset_rounded & 0xfffffffful,
3237290001Sglebius			length + offset_remaining);
3238290001Sglebius		if (data == NULL) {
3239290001Sglebius			mm_free(chain);
3240290001Sglebius			goto err;
3241290001Sglebius		}
3242290001Sglebius		chain->buffer = (unsigned char*) data;
3243290001Sglebius		chain->buffer_len = length+offset_remaining;
3244290001Sglebius		chain->misalign = offset_remaining;
3245290001Sglebius		chain->off = length;
3246290001Sglebius#else
3247290001Sglebius		chain->buffer = (unsigned char*)(seg->contents + offset);
3248290001Sglebius		chain->buffer_len = length;
3249290001Sglebius		chain->off = length;
3250290001Sglebius#endif
3251290001Sglebius	} else {
3252290001Sglebius		chain->buffer = (unsigned char*)(seg->contents + offset);
3253290001Sglebius		chain->buffer_len = length;
3254290001Sglebius		chain->off = length;
3255290001Sglebius	}
3256290001Sglebius
3257290001Sglebius	extra->segment = seg;
3258290001Sglebius	buf->n_add_for_cb += length;
3259290001Sglebius	evbuffer_chain_insert(buf, chain);
3260290001Sglebius
3261290001Sglebius	evbuffer_invoke_callbacks_(buf);
3262290001Sglebius
3263290001Sglebius	EVBUFFER_UNLOCK(buf);
3264290001Sglebius
3265290001Sglebius	return 0;
3266290001Sglebiuserr:
3267290001Sglebius	EVBUFFER_UNLOCK(buf);
3268290001Sglebius	evbuffer_file_segment_free(seg); /* Lowers the refcount */
3269290001Sglebius	return -1;
3270290001Sglebius}
3271290001Sglebius
3272290001Sglebiusint
3273290001Sglebiusevbuffer_add_file(struct evbuffer *buf, int fd, ev_off_t offset, ev_off_t length)
3274290001Sglebius{
3275290001Sglebius	struct evbuffer_file_segment *seg;
3276290001Sglebius	unsigned flags = EVBUF_FS_CLOSE_ON_FREE;
3277290001Sglebius	int r;
3278290001Sglebius
3279290001Sglebius	seg = evbuffer_file_segment_new(fd, offset, length, flags);
3280290001Sglebius	if (!seg)
3281290001Sglebius		return -1;
3282290001Sglebius	r = evbuffer_add_file_segment(buf, seg, 0, length);
3283290001Sglebius	if (r == 0)
3284290001Sglebius		evbuffer_file_segment_free(seg);
3285290001Sglebius	return r;
3286290001Sglebius}
3287290001Sglebius
3288290001Sglebiusvoid
3289290001Sglebiusevbuffer_setcb(struct evbuffer *buffer, evbuffer_cb cb, void *cbarg)
3290290001Sglebius{
3291290001Sglebius	EVBUFFER_LOCK(buffer);
3292290001Sglebius
3293290001Sglebius	if (!LIST_EMPTY(&buffer->callbacks))
3294290001Sglebius		evbuffer_remove_all_callbacks(buffer);
3295290001Sglebius
3296290001Sglebius	if (cb) {
3297290001Sglebius		struct evbuffer_cb_entry *ent =
3298290001Sglebius		    evbuffer_add_cb(buffer, NULL, cbarg);
3299290001Sglebius		ent->cb.cb_obsolete = cb;
3300290001Sglebius		ent->flags |= EVBUFFER_CB_OBSOLETE;
3301290001Sglebius	}
3302290001Sglebius	EVBUFFER_UNLOCK(buffer);
3303290001Sglebius}
3304290001Sglebius
3305290001Sglebiusstruct evbuffer_cb_entry *
3306290001Sglebiusevbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
3307290001Sglebius{
3308290001Sglebius	struct evbuffer_cb_entry *e;
3309290001Sglebius	if (! (e = mm_calloc(1, sizeof(struct evbuffer_cb_entry))))
3310290001Sglebius		return NULL;
3311290001Sglebius	EVBUFFER_LOCK(buffer);
3312290001Sglebius	e->cb.cb_func = cb;
3313290001Sglebius	e->cbarg = cbarg;
3314290001Sglebius	e->flags = EVBUFFER_CB_ENABLED;
3315290001Sglebius	LIST_INSERT_HEAD(&buffer->callbacks, e, next);
3316290001Sglebius	EVBUFFER_UNLOCK(buffer);
3317290001Sglebius	return e;
3318290001Sglebius}
3319290001Sglebius
3320290001Sglebiusint
3321290001Sglebiusevbuffer_remove_cb_entry(struct evbuffer *buffer,
3322290001Sglebius			 struct evbuffer_cb_entry *ent)
3323290001Sglebius{
3324290001Sglebius	EVBUFFER_LOCK(buffer);
3325290001Sglebius	LIST_REMOVE(ent, next);
3326290001Sglebius	EVBUFFER_UNLOCK(buffer);
3327290001Sglebius	mm_free(ent);
3328290001Sglebius	return 0;
3329290001Sglebius}
3330290001Sglebius
3331290001Sglebiusint
3332290001Sglebiusevbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg)
3333290001Sglebius{
3334290001Sglebius	struct evbuffer_cb_entry *cbent;
3335290001Sglebius	int result = -1;
3336290001Sglebius	EVBUFFER_LOCK(buffer);
3337290001Sglebius	LIST_FOREACH(cbent, &buffer->callbacks, next) {
3338290001Sglebius		if (cb == cbent->cb.cb_func && cbarg == cbent->cbarg) {
3339290001Sglebius			result = evbuffer_remove_cb_entry(buffer, cbent);
3340290001Sglebius			goto done;
3341290001Sglebius		}
3342290001Sglebius	}
3343290001Sglebiusdone:
3344290001Sglebius	EVBUFFER_UNLOCK(buffer);
3345290001Sglebius	return result;
3346290001Sglebius}
3347290001Sglebius
3348290001Sglebiusint
3349290001Sglebiusevbuffer_cb_set_flags(struct evbuffer *buffer,
3350290001Sglebius		      struct evbuffer_cb_entry *cb, ev_uint32_t flags)
3351290001Sglebius{
3352290001Sglebius	/* the user isn't allowed to mess with these. */
3353290001Sglebius	flags &= ~EVBUFFER_CB_INTERNAL_FLAGS;
3354290001Sglebius	EVBUFFER_LOCK(buffer);
3355290001Sglebius	cb->flags |= flags;
3356290001Sglebius	EVBUFFER_UNLOCK(buffer);
3357290001Sglebius	return 0;
3358290001Sglebius}
3359290001Sglebius
3360290001Sglebiusint
3361290001Sglebiusevbuffer_cb_clear_flags(struct evbuffer *buffer,
3362290001Sglebius		      struct evbuffer_cb_entry *cb, ev_uint32_t flags)
3363290001Sglebius{
3364290001Sglebius	/* the user isn't allowed to mess with these. */
3365290001Sglebius	flags &= ~EVBUFFER_CB_INTERNAL_FLAGS;
3366290001Sglebius	EVBUFFER_LOCK(buffer);
3367290001Sglebius	cb->flags &= ~flags;
3368290001Sglebius	EVBUFFER_UNLOCK(buffer);
3369290001Sglebius	return 0;
3370290001Sglebius}
3371290001Sglebius
3372290001Sglebiusint
3373290001Sglebiusevbuffer_freeze(struct evbuffer *buffer, int start)
3374290001Sglebius{
3375290001Sglebius	EVBUFFER_LOCK(buffer);
3376290001Sglebius	if (start)
3377290001Sglebius		buffer->freeze_start = 1;
3378290001Sglebius	else
3379290001Sglebius		buffer->freeze_end = 1;
3380290001Sglebius	EVBUFFER_UNLOCK(buffer);
3381290001Sglebius	return 0;
3382290001Sglebius}
3383290001Sglebius
3384290001Sglebiusint
3385290001Sglebiusevbuffer_unfreeze(struct evbuffer *buffer, int start)
3386290001Sglebius{
3387290001Sglebius	EVBUFFER_LOCK(buffer);
3388290001Sglebius	if (start)
3389290001Sglebius		buffer->freeze_start = 0;
3390290001Sglebius	else
3391290001Sglebius		buffer->freeze_end = 0;
3392290001Sglebius	EVBUFFER_UNLOCK(buffer);
3393290001Sglebius	return 0;
3394290001Sglebius}
3395290001Sglebius
3396290001Sglebius#if 0
3397290001Sglebiusvoid
3398290001Sglebiusevbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
3399290001Sglebius{
3400290001Sglebius	if (!(cb->flags & EVBUFFER_CB_SUSPENDED)) {
3401290001Sglebius		cb->size_before_suspend = evbuffer_get_length(buffer);
3402290001Sglebius		cb->flags |= EVBUFFER_CB_SUSPENDED;
3403290001Sglebius	}
3404290001Sglebius}
3405290001Sglebius
3406290001Sglebiusvoid
3407290001Sglebiusevbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb)
3408290001Sglebius{
3409290001Sglebius	if ((cb->flags & EVBUFFER_CB_SUSPENDED)) {
3410290001Sglebius		unsigned call = (cb->flags & EVBUFFER_CB_CALL_ON_UNSUSPEND);
3411290001Sglebius		size_t sz = cb->size_before_suspend;
3412290001Sglebius		cb->flags &= ~(EVBUFFER_CB_SUSPENDED|
3413290001Sglebius			       EVBUFFER_CB_CALL_ON_UNSUSPEND);
3414290001Sglebius		cb->size_before_suspend = 0;
3415290001Sglebius		if (call && (cb->flags & EVBUFFER_CB_ENABLED)) {
3416290001Sglebius			cb->cb(buffer, sz, evbuffer_get_length(buffer), cb->cbarg);
3417290001Sglebius		}
3418290001Sglebius	}
3419290001Sglebius}
3420290001Sglebius#endif
3421290001Sglebius
3422290001Sglebiusint
3423290001Sglebiusevbuffer_get_callbacks_(struct evbuffer *buffer, struct event_callback **cbs,
3424290001Sglebius    int max_cbs)
3425290001Sglebius{
3426290001Sglebius	int r = 0;
3427290001Sglebius	EVBUFFER_LOCK(buffer);
3428290001Sglebius	if (buffer->deferred_cbs) {
3429290001Sglebius		if (max_cbs < 1) {
3430290001Sglebius			r = -1;
3431290001Sglebius			goto done;
3432290001Sglebius		}
3433290001Sglebius		cbs[0] = &buffer->deferred;
3434290001Sglebius		r = 1;
3435290001Sglebius	}
3436290001Sglebiusdone:
3437290001Sglebius	EVBUFFER_UNLOCK(buffer);
3438290001Sglebius	return r;
3439290001Sglebius}
3440