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