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