xref: /libevent-2.1.12/include/event2/buffer.h (revision a4f0387c)
15c70ea4cSNiels Provos /*
2e49e2891SNick Mathewson  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
35c70ea4cSNiels Provos  *
45c70ea4cSNiels Provos  * Redistribution and use in source and binary forms, with or without
55c70ea4cSNiels Provos  * modification, are permitted provided that the following conditions
65c70ea4cSNiels Provos  * are met:
75c70ea4cSNiels Provos  * 1. Redistributions of source code must retain the above copyright
85c70ea4cSNiels Provos  *    notice, this list of conditions and the following disclaimer.
95c70ea4cSNiels Provos  * 2. Redistributions in binary form must reproduce the above copyright
105c70ea4cSNiels Provos  *    notice, this list of conditions and the following disclaimer in the
115c70ea4cSNiels Provos  *    documentation and/or other materials provided with the distribution.
125c70ea4cSNiels Provos  * 3. The name of the author may not be used to endorse or promote products
135c70ea4cSNiels Provos  *    derived from this software without specific prior written permission.
145c70ea4cSNiels Provos  *
155c70ea4cSNiels Provos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
165c70ea4cSNiels Provos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
175c70ea4cSNiels Provos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
185c70ea4cSNiels Provos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
195c70ea4cSNiels Provos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
205c70ea4cSNiels Provos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
215c70ea4cSNiels Provos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
225c70ea4cSNiels Provos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
235c70ea4cSNiels Provos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
245c70ea4cSNiels Provos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
255c70ea4cSNiels Provos  */
263f8c7cd0SNick Mathewson #ifndef EVENT2_BUFFER_H_INCLUDED_
273f8c7cd0SNick Mathewson #define EVENT2_BUFFER_H_INCLUDED_
285c70ea4cSNiels Provos 
292888faccSNick Mathewson /** @file event2/buffer.h
305c70ea4cSNiels Provos 
315c70ea4cSNiels Provos   Functions for buffering data for network sending or receiving.
325c70ea4cSNiels Provos 
335c70ea4cSNiels Provos   An evbuffer can be used for preparing data before sending it to
345c70ea4cSNiels Provos   the network or conversely for reading data from the network.
355c70ea4cSNiels Provos   Evbuffers try to avoid memory copies as much as possible.  As a
362888faccSNick Mathewson   result, evbuffers can be used to pass data around without actually
375c70ea4cSNiels Provos   incurring the overhead of copying the data.
385c70ea4cSNiels Provos 
395c70ea4cSNiels Provos   A new evbuffer can be allocated with evbuffer_new(), and can be
402888faccSNick Mathewson   freed with evbuffer_free().  Most users will be using evbuffers via
412888faccSNick Mathewson   the bufferevent interface.  To access a bufferevent's evbuffers, use
422888faccSNick Mathewson   bufferevent_get_input() and bufferevent_get_output().
435c70ea4cSNiels Provos 
445c70ea4cSNiels Provos   There are several guidelines for using evbuffers.
455c70ea4cSNiels Provos 
465c70ea4cSNiels Provos   - if you already know how much data you are going to add as a result
475c70ea4cSNiels Provos     of calling evbuffer_add() multiple times, it makes sense to use
485c70ea4cSNiels Provos     evbuffer_expand() first to make sure that enough memory is allocated
495c70ea4cSNiels Provos     before hand.
505c70ea4cSNiels Provos 
515c70ea4cSNiels Provos   - evbuffer_add_buffer() adds the contents of one buffer to the other
52f446f149SNick Mathewson     without incurring any unnecessary memory copies.
535c70ea4cSNiels Provos 
54f446f149SNick Mathewson   - evbuffer_add() and evbuffer_add_buffer() do not mix very well:
55f446f149SNick Mathewson     if you use them, you will wind up with fragmented memory in your
56f446f149SNick Mathewson 	buffer.
575c70ea4cSNiels Provos 
582888faccSNick Mathewson   - For high-performance code, you may want to avoid copying data into and out
592888faccSNick Mathewson     of buffers.  You can skip the copy step by using
602888faccSNick Mathewson     evbuffer_reserve_space()/evbuffer_commit_space() when writing into a
612888faccSNick Mathewson     buffer, and evbuffer_peek() when reading.
625c70ea4cSNiels Provos 
632888faccSNick Mathewson   In Libevent 2.0 and later, evbuffers are represented using a linked
642888faccSNick Mathewson   list of memory chunks, with pointers to the first and last chunk in
652888faccSNick Mathewson   the chain.
662888faccSNick Mathewson 
672888faccSNick Mathewson   As the contents of an evbuffer can be stored in multiple different
682888faccSNick Mathewson   memory blocks, it cannot be accessed directly.  Instead, evbuffer_pullup()
692888faccSNick Mathewson   can be used to force a specified number of bytes to be contiguous. This
702888faccSNick Mathewson   will cause memory reallocation and memory copies if the data is split
712888faccSNick Mathewson   across multiple blocks.  It is more efficient, however, to use
722888faccSNick Mathewson   evbuffer_peek() if you don't require that the memory to be contiguous.
735c70ea4cSNiels Provos  */
745c70ea4cSNiels Provos 
754545fa9bSTrond Norbye #include <event2/visibility.h>
764545fa9bSTrond Norbye 
775c70ea4cSNiels Provos #ifdef __cplusplus
785c70ea4cSNiels Provos extern "C" {
795c70ea4cSNiels Provos #endif
805c70ea4cSNiels Provos 
81ec347b92SNick Mathewson #include <event2/event-config.h>
82beb39f57SNick Mathewson #include <stdarg.h>
8368120d9bSNick Mathewson #ifdef EVENT__HAVE_SYS_TYPES_H
8493d4f884SNick Mathewson #include <sys/types.h>
8593d4f884SNick Mathewson #endif
8668120d9bSNick Mathewson #ifdef EVENT__HAVE_SYS_UIO_H
878997f234SNick Mathewson #include <sys/uio.h>
888997f234SNick Mathewson #endif
8993d4f884SNick Mathewson #include <event2/util.h>
905c70ea4cSNiels Provos 
912888faccSNick Mathewson /**
922888faccSNick Mathewson    An evbuffer is an opaque data type for efficiently buffering data to be
932888faccSNick Mathewson    sent or received on the network.
945c70ea4cSNiels Provos 
952888faccSNick Mathewson    @see event2/event.h for more information
962888faccSNick Mathewson */
972888faccSNick Mathewson struct evbuffer
98da455e92SNick Mathewson #ifdef EVENT_IN_DOXYGEN_
992888faccSNick Mathewson {}
1002888faccSNick Mathewson #endif
1012888faccSNick Mathewson ;
1022888faccSNick Mathewson 
1032888faccSNick Mathewson /**
1042888faccSNick Mathewson     Pointer to a position within an evbuffer.
1052888faccSNick Mathewson 
1062888faccSNick Mathewson     Used when repeatedly searching through a buffer.  Calling any function
1072888faccSNick Mathewson     that modifies or re-packs the buffer contents may invalidate all
10858408eedSNick Mathewson     evbuffer_ptrs for that buffer.  Do not modify or contruct these values
10958408eedSNick Mathewson     except with evbuffer_ptr_set.
110261ba63dSNick Mathewson 
111261ba63dSNick Mathewson     An evbuffer_ptr can represent any position from the start of a buffer up
112261ba63dSNick Mathewson     to a position immediately after the end of a buffer.
1137d08a28cSNick Mathewson 
1147d08a28cSNick Mathewson     @see evbuffer_ptr_set()
115f90500a5SNick Mathewson  */
116f90500a5SNick Mathewson struct evbuffer_ptr {
117b2e8fd0eSNick Mathewson 	ev_ssize_t pos;
118f90500a5SNick Mathewson 
119261ba63dSNick Mathewson 	/* Do not alter or rely on the values of fields: they are for internal
120261ba63dSNick Mathewson 	 * use */
121f90500a5SNick Mathewson 	struct {
122f90500a5SNick Mathewson 		void *chain;
123f90500a5SNick Mathewson 		size_t pos_in_chain;
124cb9da0bfSNick Mathewson 	} internal_;
125f90500a5SNick Mathewson };
126f90500a5SNick Mathewson 
12723243b8aSNick Mathewson /** Describes a single extent of memory inside an evbuffer.  Used for
12823243b8aSNick Mathewson     direct-access functions.
12923243b8aSNick Mathewson 
13023243b8aSNick Mathewson     @see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
13123243b8aSNick Mathewson  */
13268120d9bSNick Mathewson #ifdef EVENT__HAVE_SYS_UIO_H
1338997f234SNick Mathewson #define evbuffer_iovec iovec
1348997f234SNick Mathewson /* Internal use -- defined only if we are using the native struct iovec */
135cb9da0bfSNick Mathewson #define EVBUFFER_IOVEC_IS_NATIVE_
1368997f234SNick Mathewson #else
13723243b8aSNick Mathewson struct evbuffer_iovec {
13823243b8aSNick Mathewson 	/** The start of the extent of memory. */
13923243b8aSNick Mathewson 	void *iov_base;
14023243b8aSNick Mathewson 	/** The length of the extent of memory. */
14123243b8aSNick Mathewson 	size_t iov_len;
14223243b8aSNick Mathewson };
1438997f234SNick Mathewson #endif
14423243b8aSNick Mathewson 
1455c70ea4cSNiels Provos /**
1465c70ea4cSNiels Provos   Allocate storage for a new evbuffer.
1475c70ea4cSNiels Provos 
1485c70ea4cSNiels Provos   @return a pointer to a newly allocated evbuffer struct, or NULL if an error
1495c70ea4cSNiels Provos 	occurred
1505c70ea4cSNiels Provos  */
1514545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1525c70ea4cSNiels Provos struct evbuffer *evbuffer_new(void);
1535c70ea4cSNiels Provos /**
1545c70ea4cSNiels Provos   Deallocate storage for an evbuffer.
1555c70ea4cSNiels Provos 
1565c70ea4cSNiels Provos   @param buf pointer to the evbuffer to be freed
1575c70ea4cSNiels Provos  */
1584545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1595c70ea4cSNiels Provos void evbuffer_free(struct evbuffer *buf);
1605c70ea4cSNiels Provos 
1615c70ea4cSNiels Provos /**
16260e0d59bSNick Mathewson    Enable locking on an evbuffer so that it can safely be used by multiple
16360e0d59bSNick Mathewson    threads at the same time.
16460e0d59bSNick Mathewson 
16560e0d59bSNick Mathewson    NOTE: when locking is enabled, the lock will be held when callbacks are
16660e0d59bSNick Mathewson    invoked.  This could result in deadlock if you aren't careful.  Plan
16760e0d59bSNick Mathewson    accordingly!
16860e0d59bSNick Mathewson 
16960e0d59bSNick Mathewson    @param buf An evbuffer to make lockable.
17060e0d59bSNick Mathewson    @param lock A lock object, or NULL if we should allocate our own.
17160e0d59bSNick Mathewson    @return 0 on success, -1 on failure.
17260e0d59bSNick Mathewson  */
1734545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
17460e0d59bSNick Mathewson int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
17560e0d59bSNick Mathewson 
176838d0a81SNick Mathewson /**
177838d0a81SNick Mathewson    Acquire the lock on an evbuffer.  Has no effect if locking was not enabled
178838d0a81SNick Mathewson    with evbuffer_enable_locking.
179838d0a81SNick Mathewson */
1804545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
18160e0d59bSNick Mathewson void evbuffer_lock(struct evbuffer *buf);
18260e0d59bSNick Mathewson 
183838d0a81SNick Mathewson /**
184838d0a81SNick Mathewson    Release the lock on an evbuffer.  Has no effect if locking was not enabled
185838d0a81SNick Mathewson    with evbuffer_enable_locking.
186838d0a81SNick Mathewson */
1874545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
188838d0a81SNick Mathewson void evbuffer_unlock(struct evbuffer *buf);
18960e0d59bSNick Mathewson 
1900ba0af9cSNick Mathewson 
1910ba0af9cSNick Mathewson /** If this flag is set, then we will not use evbuffer_peek(),
1920ba0af9cSNick Mathewson  * evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes
1930ba0af9cSNick Mathewson  * from this buffer: we'll only take bytes out of this buffer by
1940ba0af9cSNick Mathewson  * writing them to the network (as with evbuffer_write_atmost), by
1950ba0af9cSNick Mathewson  * removing them without observing them (as with evbuffer_drain),
1960ba0af9cSNick Mathewson  * or by copying them all out at once (as with evbuffer_add_buffer).
1970ba0af9cSNick Mathewson  *
1980ba0af9cSNick Mathewson  * Using this option allows the implementation to use sendfile-based
1990ba0af9cSNick Mathewson  * operations for evbuffer_add_file(); see that function for more
2000ba0af9cSNick Mathewson  * information.
2010ba0af9cSNick Mathewson  *
2020ba0af9cSNick Mathewson  * This flag is on by default for bufferevents that can take advantage
2030ba0af9cSNick Mathewson  * of it; you should never actually need to set it on a bufferevent's
2040ba0af9cSNick Mathewson  * output buffer.
2050ba0af9cSNick Mathewson  */
2060ba0af9cSNick Mathewson #define EVBUFFER_FLAG_DRAINS_TO_FD 1
2070ba0af9cSNick Mathewson 
2080ba0af9cSNick Mathewson /** Change the flags that are set for an evbuffer by adding more.
2090ba0af9cSNick Mathewson  *
2100ba0af9cSNick Mathewson  * @param buffer the evbuffer that the callback is watching.
2110ba0af9cSNick Mathewson  * @param cb the callback whose status we want to change.
2120ba0af9cSNick Mathewson  * @param flags One or more EVBUFFER_FLAG_* options
2130ba0af9cSNick Mathewson  * @return 0 on success, -1 on failure.
2140ba0af9cSNick Mathewson  */
2154545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
2160ba0af9cSNick Mathewson int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
2170ba0af9cSNick Mathewson /** Change the flags that are set for an evbuffer by removing some.
2180ba0af9cSNick Mathewson  *
2190ba0af9cSNick Mathewson  * @param buffer the evbuffer that the callback is watching.
2200ba0af9cSNick Mathewson  * @param cb the callback whose status we want to change.
2210ba0af9cSNick Mathewson  * @param flags One or more EVBUFFER_FLAG_* options
2220ba0af9cSNick Mathewson  * @return 0 on success, -1 on failure.
2230ba0af9cSNick Mathewson  */
2244545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
2250ba0af9cSNick Mathewson int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
2260ba0af9cSNick Mathewson 
22760e0d59bSNick Mathewson /**
2282888faccSNick Mathewson   Returns the total number of bytes stored in the evbuffer
2295c70ea4cSNiels Provos 
2305c70ea4cSNiels Provos   @param buf pointer to the evbuffer
2312888faccSNick Mathewson   @return the number of bytes stored in the evbuffer
2325c70ea4cSNiels Provos */
2334545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
23484031819SNick Mathewson size_t evbuffer_get_length(const struct evbuffer *buf);
2355c70ea4cSNiels Provos 
2365c70ea4cSNiels Provos /**
237f446f149SNick Mathewson    Returns the number of contiguous available bytes in the first buffer chain.
238becc89b7SNiels Provos 
2390b4ab122SNick Mathewson    This is useful when processing data that might be split into multiple
240f446f149SNick Mathewson    chains, or that might all be in the first chain.  Calls to
241f446f149SNick Mathewson    evbuffer_pullup() that cause reallocation and copying of data can thus be
242f446f149SNick Mathewson    avoided.
243becc89b7SNiels Provos 
244becc89b7SNiels Provos    @param buf pointer to the evbuffer
245becc89b7SNiels Provos    @return 0 if no data is available, otherwise the number of available bytes
246becc89b7SNiels Provos      in the first buffer chain.
247becc89b7SNiels Provos */
2484545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
24984031819SNick Mathewson size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
250becc89b7SNiels Provos 
251becc89b7SNiels Provos /**
2522888faccSNick Mathewson   Expands the available space in an evbuffer.
2535c70ea4cSNiels Provos 
2542888faccSNick Mathewson   Expands the available space in the evbuffer to at least datlen, so that
2550322ce0aSNick Mathewson   appending datlen additional bytes will not require any new allocations.
2565c70ea4cSNiels Provos 
2572888faccSNick Mathewson   @param buf the evbuffer to be expanded
2585c70ea4cSNiels Provos   @param datlen the new minimum length requirement
2595c70ea4cSNiels Provos   @return 0 if successful, or -1 if an error occurred
2605c70ea4cSNiels Provos */
2614545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
2625c70ea4cSNiels Provos int evbuffer_expand(struct evbuffer *buf, size_t datlen);
2635c70ea4cSNiels Provos 
264f04497e4SNiels Provos /**
2652888faccSNick Mathewson    Reserves space in the last chain or chains of an evbuffer.
266f04497e4SNiels Provos 
2672888faccSNick Mathewson    Makes space available in the last chain or chains of an evbuffer that can
268f04497e4SNiels Provos    be arbitrarily written to by a user.  The space does not become
269f446f149SNick Mathewson    available for reading until it has been committed with
270f446f149SNick Mathewson    evbuffer_commit_space().
271f04497e4SNiels Provos 
27223243b8aSNick Mathewson    The space is made available as one or more extents, represented by
27323243b8aSNick Mathewson    an initial pointer and a length.  You can force the memory to be
2742888faccSNick Mathewson    available as only one extent.  Allowing more extents, however, makes the
27523243b8aSNick Mathewson    function more efficient.
27623243b8aSNick Mathewson 
277f04497e4SNiels Provos    Multiple subsequent calls to this function will make the same space
278f04497e4SNiels Provos    available until evbuffer_commit_space() has been called.
279f04497e4SNiels Provos 
28023243b8aSNick Mathewson    It is an error to do anything that moves around the buffer's internal
28123243b8aSNick Mathewson    memory structures before committing the space.
28223243b8aSNick Mathewson 
28323243b8aSNick Mathewson    NOTE: The code currently does not ever use more than two extents.
28423243b8aSNick Mathewson    This may change in future versions.
28523243b8aSNick Mathewson 
2862888faccSNick Mathewson    @param buf the evbuffer in which to reserve space.
28723243b8aSNick Mathewson    @param size how much space to make available, at minimum.  The
28823243b8aSNick Mathewson       total length of the extents may be greater than the requested
28923243b8aSNick Mathewson       length.
29023243b8aSNick Mathewson    @param vec an array of one or more evbuffer_iovec structures to
29123243b8aSNick Mathewson       hold pointers to the reserved extents of memory.
2922888faccSNick Mathewson    @param n_vec The length of the vec array.  Must be at least 1;
2932888faccSNick Mathewson        2 is more efficient.
29423243b8aSNick Mathewson    @return the number of provided extents, or -1 on error.
2952888faccSNick Mathewson    @see evbuffer_commit_space()
296f04497e4SNiels Provos */
2974545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
29823243b8aSNick Mathewson int
2990b22ca19SNick Mathewson evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
3002888faccSNick Mathewson     struct evbuffer_iovec *vec, int n_vec);
301f04497e4SNiels Provos 
302f04497e4SNiels Provos /**
303f04497e4SNiels Provos    Commits previously reserved space.
304f04497e4SNiels Provos 
305f446f149SNick Mathewson    Commits some of the space previously reserved with
306f446f149SNick Mathewson    evbuffer_reserve_space().  It then becomes available for reading.
307f04497e4SNiels Provos 
30823243b8aSNick Mathewson    This function may return an error if the pointer in the extents do
30923243b8aSNick Mathewson    not match those returned from evbuffer_reserve_space, or if data
31023243b8aSNick Mathewson    has been added to the buffer since the space was reserved.
31123243b8aSNick Mathewson 
31223243b8aSNick Mathewson    If you want to commit less data than you got reserved space for,
3132888faccSNick Mathewson    modify the iov_len pointer of the appropriate extent to a smaller
3142888faccSNick Mathewson    value.  Note that you may have received more space than you
3152888faccSNick Mathewson    requested if it was available!
31623243b8aSNick Mathewson 
3172888faccSNick Mathewson    @param buf the evbuffer in which to reserve space.
31823243b8aSNick Mathewson    @param vec one or two extents returned by evbuffer_reserve_space.
3190b4ab122SNick Mathewson    @param n_vecs the number of extents.
320f04497e4SNiels Provos    @return 0 on success, -1 on error
3212888faccSNick Mathewson    @see evbuffer_reserve_space()
322f04497e4SNiels Provos */
3234545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
32423243b8aSNick Mathewson int evbuffer_commit_space(struct evbuffer *buf,
32523243b8aSNick Mathewson     struct evbuffer_iovec *vec, int n_vecs);
3265c70ea4cSNiels Provos 
3275c70ea4cSNiels Provos /**
3285c70ea4cSNiels Provos   Append data to the end of an evbuffer.
3295c70ea4cSNiels Provos 
3302888faccSNick Mathewson   @param buf the evbuffer to be appended to
3315c70ea4cSNiels Provos   @param data pointer to the beginning of the data buffer
3325c70ea4cSNiels Provos   @param datlen the number of bytes to be copied from the data buffer
333747331d1SNick Mathewson   @return 0 on success, -1 on failure.
3345c70ea4cSNiels Provos  */
3354545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
3365c70ea4cSNiels Provos int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
3375c70ea4cSNiels Provos 
3385c70ea4cSNiels Provos 
3395c70ea4cSNiels Provos /**
3402888faccSNick Mathewson   Read data from an evbuffer and drain the bytes read.
3415c70ea4cSNiels Provos 
3422888faccSNick Mathewson   If more bytes are requested than are available in the evbuffer, we
3432888faccSNick Mathewson   only extract as many bytes as were available.
3442888faccSNick Mathewson 
3452888faccSNick Mathewson   @param buf the evbuffer to be read from
3465c70ea4cSNiels Provos   @param data the destination buffer to store the result
3475c70ea4cSNiels Provos   @param datlen the maximum size of the destination buffer
348747331d1SNick Mathewson   @return the number of bytes read, or -1 if we can't drain the buffer.
3495c70ea4cSNiels Provos  */
3504545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
3515c70ea4cSNiels Provos int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
3525c70ea4cSNiels Provos 
3535c70ea4cSNiels Provos /**
3542888faccSNick Mathewson   Read data from an evbuffer, and leave the buffer unchanged.
355eb86c8c5SNick Mathewson 
3562888faccSNick Mathewson   If more bytes are requested than are available in the evbuffer, we
3572888faccSNick Mathewson   only extract as many bytes as were available.
3582888faccSNick Mathewson 
3592888faccSNick Mathewson   @param buf the evbuffer to be read from
3602888faccSNick Mathewson   @param data_out the destination buffer to store the result
361eb86c8c5SNick Mathewson   @param datlen the maximum size of the destination buffer
362eb86c8c5SNick Mathewson   @return the number of bytes read, or -1 if we can't drain the buffer.
363eb86c8c5SNick Mathewson  */
3644545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
365eb86c8c5SNick Mathewson ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
366eb86c8c5SNick Mathewson 
367eb86c8c5SNick Mathewson /**
36827e22255SNick Mathewson   Read data from the middle of an evbuffer, and leave the buffer unchanged.
36927e22255SNick Mathewson 
37027e22255SNick Mathewson   If more bytes are requested than are available in the evbuffer, we
37127e22255SNick Mathewson   only extract as many bytes as were available.
37227e22255SNick Mathewson 
37327e22255SNick Mathewson   @param buf the evbuffer to be read from
37427e22255SNick Mathewson   @param pos the position to start reading from
37527e22255SNick Mathewson   @param data_out the destination buffer to store the result
37627e22255SNick Mathewson   @param datlen the maximum size of the destination buffer
37727e22255SNick Mathewson   @return the number of bytes read, or -1 if we can't drain the buffer.
37827e22255SNick Mathewson  */
3794545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
38027e22255SNick Mathewson ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
38127e22255SNick Mathewson 
38227e22255SNick Mathewson /**
3832888faccSNick Mathewson   Read data from an evbuffer into another evbuffer, draining
3842888faccSNick Mathewson   the bytes from the source buffer.  This function avoids copy
3852888faccSNick Mathewson   operations to the extent possible.
3865c70ea4cSNiels Provos 
3872888faccSNick Mathewson   If more bytes are requested than are available in src, the src
3882888faccSNick Mathewson   buffer is drained completely.
3892888faccSNick Mathewson 
3902888faccSNick Mathewson   @param src the evbuffer to be read from
3912888faccSNick Mathewson   @param dst the destination evbuffer to store the result into
3925c70ea4cSNiels Provos   @param datlen the maximum numbers of bytes to transfer
3935c70ea4cSNiels Provos   @return the number of bytes read
3945c70ea4cSNiels Provos  */
3954545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
3965c70ea4cSNiels Provos int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
3975c70ea4cSNiels Provos     size_t datlen);
3985c70ea4cSNiels Provos 
3995c70ea4cSNiels Provos /** Used to tell evbuffer_readln what kind of line-ending to look for.
4005c70ea4cSNiels Provos  */
4015c70ea4cSNiels Provos enum evbuffer_eol_style {
4022888faccSNick Mathewson 	/** Any sequence of CR and LF characters is acceptable as an
4032888faccSNick Mathewson 	 * EOL.
4042888faccSNick Mathewson 	 *
4052888faccSNick Mathewson 	 * Note that this style can produce ambiguous results: the
4062888faccSNick Mathewson 	 * sequence "CRLF" will be treated as a single EOL if it is
4072888faccSNick Mathewson 	 * all in the buffer at once, but if you first read a CR from
4082888faccSNick Mathewson 	 * the network and later read an LF from the network, it will
4092888faccSNick Mathewson 	 * be treated as two EOLs.
4102888faccSNick Mathewson 	 */
4115c70ea4cSNiels Provos 	EVBUFFER_EOL_ANY,
4125c70ea4cSNiels Provos 	/** An EOL is an LF, optionally preceded by a CR.  This style is
4135c70ea4cSNiels Provos 	 * most useful for implementing text-based internet protocols. */
4145c70ea4cSNiels Provos 	EVBUFFER_EOL_CRLF,
4155c70ea4cSNiels Provos 	/** An EOL is a CR followed by an LF. */
4165c70ea4cSNiels Provos 	EVBUFFER_EOL_CRLF_STRICT,
4175c70ea4cSNiels Provos 	/** An EOL is a LF. */
418d7a8b36eSAndrea Montefusco 	EVBUFFER_EOL_LF,
419d7a8b36eSAndrea Montefusco 	/** An EOL is a NUL character (that is, a single byte with value 0) */
420d7a8b36eSAndrea Montefusco 	EVBUFFER_EOL_NUL
4215c70ea4cSNiels Provos };
4225c70ea4cSNiels Provos 
4235c70ea4cSNiels Provos /**
4242888faccSNick Mathewson  * Read a single line from an evbuffer.
4255c70ea4cSNiels Provos  *
4265c70ea4cSNiels Provos  * Reads a line terminated by an EOL as determined by the evbuffer_eol_style
4275c70ea4cSNiels Provos  * argument.  Returns a newly allocated nul-terminated string; the caller must
4285c70ea4cSNiels Provos  * free the returned value.  The EOL is not included in the returned string.
4295c70ea4cSNiels Provos  *
4305c70ea4cSNiels Provos  * @param buffer the evbuffer to read from
4315c70ea4cSNiels Provos  * @param n_read_out if non-NULL, points to a size_t that is set to the
4325c70ea4cSNiels Provos  *       number of characters in the returned string.  This is useful for
4335c70ea4cSNiels Provos  *       strings that can contain NUL characters.
4345c70ea4cSNiels Provos  * @param eol_style the style of line-ending to use.
4355c70ea4cSNiels Provos  * @return pointer to a single line, or NULL if an error occurred
4365c70ea4cSNiels Provos  */
4374545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
4385c70ea4cSNiels Provos char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
4395c70ea4cSNiels Provos     enum evbuffer_eol_style eol_style);
4405c70ea4cSNiels Provos 
4415c70ea4cSNiels Provos /**
4422888faccSNick Mathewson   Move all data from one evbuffer into another evbuffer.
4435c70ea4cSNiels Provos 
4445c70ea4cSNiels Provos   This is a destructive add.  The data from one buffer moves into
445f446f149SNick Mathewson   the other buffer.  However, no unnecessary memory copies occur.
4465c70ea4cSNiels Provos 
4475c70ea4cSNiels Provos   @param outbuf the output buffer
4485c70ea4cSNiels Provos   @param inbuf the input buffer
4495c70ea4cSNiels Provos   @return 0 if successful, or -1 if an error occurred
4502888faccSNick Mathewson 
4512888faccSNick Mathewson   @see evbuffer_remove_buffer()
4525c70ea4cSNiels Provos  */
4534545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
4545c70ea4cSNiels Provos int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
4555c70ea4cSNiels Provos 
4562888faccSNick Mathewson /**
4579d7368aeSJoachim Bauch   Copy data from one evbuffer into another evbuffer.
4589d7368aeSJoachim Bauch 
4599d7368aeSJoachim Bauch   This is a non-destructive add.  The data from one buffer is copied
4609d7368aeSJoachim Bauch   into the other buffer.  However, no unnecessary memory copies occur.
4619d7368aeSJoachim Bauch 
46226041a8eSJoachim Bauch   Note that buffers already containing buffer references can't be added
46326041a8eSJoachim Bauch   to other buffers.
46426041a8eSJoachim Bauch 
4659d7368aeSJoachim Bauch   @param outbuf the output buffer
4669d7368aeSJoachim Bauch   @param inbuf the input buffer
4679d7368aeSJoachim Bauch   @return 0 if successful, or -1 if an error occurred
4689d7368aeSJoachim Bauch  */
4694545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
47026041a8eSJoachim Bauch int evbuffer_add_buffer_reference(struct evbuffer *outbuf,
47126041a8eSJoachim Bauch     struct evbuffer *inbuf);
4729d7368aeSJoachim Bauch 
4739d7368aeSJoachim Bauch /**
4742888faccSNick Mathewson    A cleanup function for a piece of memory added to an evbuffer by
4752888faccSNick Mathewson    reference.
476dc4c7b95SNick Mathewson 
4772888faccSNick Mathewson    @see evbuffer_add_reference()
4782888faccSNick Mathewson  */
479dc4c7b95SNick Mathewson typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
480dc4c7b95SNick Mathewson     size_t datalen, void *extra);
481dc4c7b95SNick Mathewson 
482fdf69493SNiels Provos /**
483fdf69493SNiels Provos   Reference memory into an evbuffer without copying.
484fdf69493SNiels Provos 
485fdf69493SNiels Provos   The memory needs to remain valid until all the added data has been
486fdf69493SNiels Provos   read.  This function keeps just a reference to the memory without
487fdf69493SNiels Provos   actually incurring the overhead of a copy.
488fdf69493SNiels Provos 
489fdf69493SNiels Provos   @param outbuf the output buffer
490fdf69493SNiels Provos   @param data the memory to reference
491fdf69493SNiels Provos   @param datlen how memory to reference
4920b4ab122SNick Mathewson   @param cleanupfn callback to be invoked when the memory is no longer
4932888faccSNick Mathewson 	referenced by this evbuffer.
4942888faccSNick Mathewson   @param cleanupfn_arg optional argument to the cleanup callback
495fdf69493SNiels Provos   @return 0 if successful, or -1 if an error occurred
496fdf69493SNiels Provos  */
4974545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
49866b2a7ffSNiels Provos int evbuffer_add_reference(struct evbuffer *outbuf,
49966b2a7ffSNiels Provos     const void *data, size_t datlen,
5002888faccSNick Mathewson     evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
501fdf69493SNiels Provos 
502fdf69493SNiels Provos /**
5032888faccSNick Mathewson   Copy data from a file into the evbuffer for writing to a socket.
504fdf69493SNiels Provos 
505fdf69493SNiels Provos   This function avoids unnecessary data copies between userland and
5060ba0af9cSNick Mathewson   kernel.  If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD
5070ba0af9cSNick Mathewson   flag is set, it uses those functions.  Otherwise, it tries to use
5080ba0af9cSNick Mathewson   mmap (or CreateFileMapping on Windows).
509fdf69493SNiels Provos 
510fdf69493SNiels Provos   The function owns the resulting file descriptor and will close it
511fdf69493SNiels Provos   when finished transferring data.
512fdf69493SNiels Provos 
5132888faccSNick Mathewson   The results of using evbuffer_remove() or evbuffer_pullup() on
5142888faccSNick Mathewson   evbuffers whose data was added using this function are undefined.
515fdf69493SNiels Provos 
516e72afae0SNick Mathewson   For more fine-grained control, use evbuffer_add_file_segment.
517e72afae0SNick Mathewson 
518fdf69493SNiels Provos   @param outbuf the output buffer
519fdf69493SNiels Provos   @param fd the file descriptor
5207d08a28cSNick Mathewson   @param offset the offset from which to read data
521e72afae0SNick Mathewson   @param length how much data to read, or -1 to read as much as possible.
522e72afae0SNick Mathewson     (-1 requires that 'fd' support fstat.)
523fdf69493SNiels Provos   @return 0 if successful, or -1 if an error occurred
524fdf69493SNiels Provos */
525fdf69493SNiels Provos 
5264545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
5272888faccSNick Mathewson int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
528ac7e52d8SNick Mathewson     ev_off_t length);
5295c70ea4cSNiels Provos 
5305c70ea4cSNiels Provos /**
531e72afae0SNick Mathewson   An evbuffer_file_segment holds a reference to a range of a file --
532e72afae0SNick Mathewson   possibly the whole file! -- for use in writing from an evbuffer to a
533e72afae0SNick Mathewson   socket.  It could be implemented with mmap, sendfile, splice, or (if all
534e72afae0SNick Mathewson   else fails) by just pulling all the data into RAM.  A single
535e72afae0SNick Mathewson   evbuffer_file_segment can be added more than once, and to more than one
536e72afae0SNick Mathewson   evbuffer.
537e72afae0SNick Mathewson  */
538e72afae0SNick Mathewson struct evbuffer_file_segment;
539e72afae0SNick Mathewson 
540e72afae0SNick Mathewson /**
541e72afae0SNick Mathewson     Flag for creating evbuffer_file_segment: If this flag is set, then when
542e72afae0SNick Mathewson     the evbuffer_file_segment is freed and no longer in use by any
543e72afae0SNick Mathewson     evbuffer, the underlying fd is closed.
544e72afae0SNick Mathewson  */
545e72afae0SNick Mathewson #define EVBUF_FS_CLOSE_ON_FREE    0x01
546e72afae0SNick Mathewson /**
547e72afae0SNick Mathewson    Flag for creating evbuffer_file_segment: Disable memory-map based
548e72afae0SNick Mathewson    implementations.
549e72afae0SNick Mathewson  */
550e72afae0SNick Mathewson #define EVBUF_FS_DISABLE_MMAP     0x02
551e72afae0SNick Mathewson /**
552e72afae0SNick Mathewson    Flag for creating evbuffer_file_segment: Disable direct fd-to-fd
553e72afae0SNick Mathewson    implementations (including sendfile and splice).
554e72afae0SNick Mathewson 
555e72afae0SNick Mathewson    You might want to use this option if data needs to be taken from the
556e72afae0SNick Mathewson    evbuffer by any means other than writing it to the network: the sendfile
557e72afae0SNick Mathewson    backend is fast, but it only works for sending files directly to the
558e72afae0SNick Mathewson    network.
559e72afae0SNick Mathewson  */
560e72afae0SNick Mathewson #define EVBUF_FS_DISABLE_SENDFILE 0x04
561e72afae0SNick Mathewson /**
562e72afae0SNick Mathewson    Flag for creating evbuffer_file_segment: Do not allocate a lock for this
563e72afae0SNick Mathewson    segment.  If this option is set, then neither the segment nor any
564e72afae0SNick Mathewson    evbuffer it is added to may ever be accessed from more than one thread
565e72afae0SNick Mathewson    at a time.
566e72afae0SNick Mathewson  */
567e72afae0SNick Mathewson #define EVBUF_FS_DISABLE_LOCKING  0x08
568e72afae0SNick Mathewson 
569e72afae0SNick Mathewson /**
570e9f8febaSyangacer    A cleanup function for a evbuffer_file_segment added to an evbuffer
571e9f8febaSyangacer    for reference.
572e9f8febaSyangacer  */
573e9f8febaSyangacer typedef void (*evbuffer_file_segment_cleanup_cb)(
574e9f8febaSyangacer     struct evbuffer_file_segment const* seg, int flags, void* arg);
575e9f8febaSyangacer 
576e9f8febaSyangacer /**
577e72afae0SNick Mathewson    Create and return a new evbuffer_file_segment for reading data from a
578e72afae0SNick Mathewson    file and sending it out via an evbuffer.
579e72afae0SNick Mathewson 
580e72afae0SNick Mathewson    This function avoids unnecessary data copies between userland and
581e72afae0SNick Mathewson    kernel.  Where available, it uses sendfile or splice.
582e72afae0SNick Mathewson 
583e72afae0SNick Mathewson    The file descriptor must not be closed so long as any evbuffer is using
584e72afae0SNick Mathewson    this segment.
585e72afae0SNick Mathewson 
586e72afae0SNick Mathewson    The results of using evbuffer_remove() or evbuffer_pullup() or any other
587e72afae0SNick Mathewson    function that reads bytes from an evbuffer on any evbuffer containing
588e72afae0SNick Mathewson    the newly returned segment are undefined, unless you pass the
589e72afae0SNick Mathewson    EVBUF_FS_DISABLE_SENDFILE flag to this function.
590e72afae0SNick Mathewson 
591e72afae0SNick Mathewson    @param fd an open file to read from.
592e72afae0SNick Mathewson    @param offset an index within the file at which to start reading
593e72afae0SNick Mathewson    @param length how much data to read, or -1 to read as much as possible.
594e72afae0SNick Mathewson       (-1 requires that 'fd' support fstat.)
595e72afae0SNick Mathewson    @param flags any number of the EVBUF_FS_* flags
596e72afae0SNick Mathewson    @return a new evbuffer_file_segment, or NULL on failure.
597e72afae0SNick Mathewson  **/
5984545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
599e72afae0SNick Mathewson struct evbuffer_file_segment *evbuffer_file_segment_new(
600e72afae0SNick Mathewson 	int fd, ev_off_t offset, ev_off_t length, unsigned flags);
601e72afae0SNick Mathewson 
602e72afae0SNick Mathewson /**
603e72afae0SNick Mathewson    Free an evbuffer_file_segment
604e72afae0SNick Mathewson 
605e72afae0SNick Mathewson    It is safe to call this function even if the segment has been added to
606e72afae0SNick Mathewson    one or more evbuffers.  The evbuffer_file_segment will not be freed
607e72afae0SNick Mathewson    until no more references to it exist.
608e72afae0SNick Mathewson  */
6094545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
610e72afae0SNick Mathewson void evbuffer_file_segment_free(struct evbuffer_file_segment *seg);
611e72afae0SNick Mathewson 
612e72afae0SNick Mathewson /**
613e9f8febaSyangacer    Add cleanup callback and argument for the callback to an
614e9f8febaSyangacer    evbuffer_file_segment.
615e9f8febaSyangacer 
616e9f8febaSyangacer    The cleanup callback will be invoked when no more references to the
617e9f8febaSyangacer    evbuffer_file_segment exist.
618e9f8febaSyangacer  **/
6194545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
620e9f8febaSyangacer void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
621e9f8febaSyangacer 	evbuffer_file_segment_cleanup_cb cb, void* arg);
622e9f8febaSyangacer 
623e9f8febaSyangacer /**
624e72afae0SNick Mathewson    Insert some or all of an evbuffer_file_segment at the end of an evbuffer
625e72afae0SNick Mathewson 
626e72afae0SNick Mathewson    Note that the offset and length parameters of this function have a
627e72afae0SNick Mathewson    different meaning from those provided to evbuffer_file_segment_new: When
628e72afae0SNick Mathewson    you create the segment, the offset is the offset _within the file_, and
629e72afae0SNick Mathewson    the length is the length _of the segment_, whereas when you add a
630e72afae0SNick Mathewson    segment to an evbuffer, the offset is _within the segment_ and the
631e72afae0SNick Mathewson    length is the length of the _part of the segment you want to use.
632e72afae0SNick Mathewson 
633e72afae0SNick Mathewson    In other words, if you have a 10 KiB file, and you create an
634e72afae0SNick Mathewson    evbuffer_file_segment for it with offset 20 and length 1000, it will
635e72afae0SNick Mathewson    refer to bytes 20..1019 inclusive.  If you then pass this segment to
636e72afae0SNick Mathewson    evbuffer_add_file_segment and specify an offset of 20 and a length of
637e72afae0SNick Mathewson    50, you will be adding bytes 40..99 inclusive.
638e72afae0SNick Mathewson 
639e72afae0SNick Mathewson    @param buf the evbuffer to append to
640e72afae0SNick Mathewson    @param seg the segment to add
641e72afae0SNick Mathewson    @param offset the offset within the segment to start from
642e72afae0SNick Mathewson    @param length the amount of data to add, or -1 to add it all.
643e72afae0SNick Mathewson    @return 0 on success, -1 on failure.
644e72afae0SNick Mathewson  */
6454545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
646e72afae0SNick Mathewson int evbuffer_add_file_segment(struct evbuffer *buf,
647e72afae0SNick Mathewson     struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length);
648e72afae0SNick Mathewson 
649e72afae0SNick Mathewson /**
6505c70ea4cSNiels Provos   Append a formatted string to the end of an evbuffer.
6515c70ea4cSNiels Provos 
6522888faccSNick Mathewson   The string is formated as printf.
6532888faccSNick Mathewson 
6545c70ea4cSNiels Provos   @param buf the evbuffer that will be appended to
6555c70ea4cSNiels Provos   @param fmt a format string
6565c70ea4cSNiels Provos   @param ... arguments that will be passed to printf(3)
6570c843507SNick Mathewson   @return The number of bytes added if successful, or -1 if an error occurred.
6580c843507SNick Mathewson 
6592888faccSNick Mathewson   @see evutil_printf(), evbuffer_add_vprintf()
6605c70ea4cSNiels Provos  */
6614545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
6625c70ea4cSNiels Provos int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
6635c70ea4cSNiels Provos #ifdef __GNUC__
6645c70ea4cSNiels Provos   __attribute__((format(printf, 2, 3)))
6655c70ea4cSNiels Provos #endif
6665c70ea4cSNiels Provos ;
6675c70ea4cSNiels Provos 
6685c70ea4cSNiels Provos /**
6695c70ea4cSNiels Provos   Append a va_list formatted string to the end of an evbuffer.
6705c70ea4cSNiels Provos 
6715c70ea4cSNiels Provos   @param buf the evbuffer that will be appended to
6725c70ea4cSNiels Provos   @param fmt a format string
6735c70ea4cSNiels Provos   @param ap a varargs va_list argument array that will be passed to vprintf(3)
6740c843507SNick Mathewson   @return The number of bytes added if successful, or -1 if an error occurred.
6755c70ea4cSNiels Provos  */
6764545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
677117e3273SNick Mathewson int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
678117e3273SNick Mathewson #ifdef __GNUC__
679117e3273SNick Mathewson 	__attribute__((format(printf, 2, 0)))
680117e3273SNick Mathewson #endif
681117e3273SNick Mathewson ;
6825c70ea4cSNiels Provos 
6835c70ea4cSNiels Provos 
6845c70ea4cSNiels Provos /**
6855c70ea4cSNiels Provos   Remove a specified number of bytes data from the beginning of an evbuffer.
6865c70ea4cSNiels Provos 
6875c70ea4cSNiels Provos   @param buf the evbuffer to be drained
6885c70ea4cSNiels Provos   @param len the number of bytes to drain from the beginning of the buffer
689747331d1SNick Mathewson   @return 0 on success, -1 on failure.
6905c70ea4cSNiels Provos  */
6914545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
692747331d1SNick Mathewson int evbuffer_drain(struct evbuffer *buf, size_t len);
6935c70ea4cSNiels Provos 
6945c70ea4cSNiels Provos 
6955c70ea4cSNiels Provos /**
6965c70ea4cSNiels Provos   Write the contents of an evbuffer to a file descriptor.
6975c70ea4cSNiels Provos 
6985c70ea4cSNiels Provos   The evbuffer will be drained after the bytes have been successfully written.
6995c70ea4cSNiels Provos 
7005c70ea4cSNiels Provos   @param buffer the evbuffer to be written and drained
7015c70ea4cSNiels Provos   @param fd the file descriptor to be written to
7025c70ea4cSNiels Provos   @return the number of bytes written, or -1 if an error occurred
7035c70ea4cSNiels Provos   @see evbuffer_read()
7045c70ea4cSNiels Provos  */
7054545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
7065c70ea4cSNiels Provos int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
7075c70ea4cSNiels Provos 
7085c70ea4cSNiels Provos /**
70999db0e7fSNick Mathewson   Write some of the contents of an evbuffer to a file descriptor.
71099db0e7fSNick Mathewson 
71199db0e7fSNick Mathewson   The evbuffer will be drained after the bytes have been successfully written.
71299db0e7fSNick Mathewson 
71399db0e7fSNick Mathewson   @param buffer the evbuffer to be written and drained
71499db0e7fSNick Mathewson   @param fd the file descriptor to be written to
71599db0e7fSNick Mathewson   @param howmuch the largest allowable number of bytes to write, or -1
71699db0e7fSNick Mathewson 	to write as many bytes as we can.
71799db0e7fSNick Mathewson   @return the number of bytes written, or -1 if an error occurred
71899db0e7fSNick Mathewson   @see evbuffer_read()
71999db0e7fSNick Mathewson  */
7204545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
72199db0e7fSNick Mathewson int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
722b2e8fd0eSNick Mathewson 						  ev_ssize_t howmuch);
72399db0e7fSNick Mathewson 
72499db0e7fSNick Mathewson /**
7255c70ea4cSNiels Provos   Read from a file descriptor and store the result in an evbuffer.
7265c70ea4cSNiels Provos 
7272888faccSNick Mathewson   @param buffer the evbuffer to store the result
7285c70ea4cSNiels Provos   @param fd the file descriptor to read from
729*a4f0387cSAzat Khuzhin   @param howmuch the number of bytes to be read. If the given number is negative
730*a4f0387cSAzat Khuzhin   or out of maximum bytes per one read, as many bytes as we can will be read.
7315c70ea4cSNiels Provos   @return the number of bytes read, or -1 if an error occurred
7325c70ea4cSNiels Provos   @see evbuffer_write()
7335c70ea4cSNiels Provos  */
7344545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
7355c70ea4cSNiels Provos int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
7365c70ea4cSNiels Provos 
7375c70ea4cSNiels Provos /**
738f90500a5SNick Mathewson    Search for a string within an evbuffer.
7395c70ea4cSNiels Provos 
7405c70ea4cSNiels Provos    @param buffer the evbuffer to be searched
7415c70ea4cSNiels Provos    @param what the string to be searched for
7425c70ea4cSNiels Provos    @param len the length of the search string
743f90500a5SNick Mathewson    @param start NULL or a pointer to a valid struct evbuffer_ptr.
744f90500a5SNick Mathewson    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
745f90500a5SNick Mathewson      first occurrence of the string in the buffer after 'start'.  The 'pos'
746f90500a5SNick Mathewson      field of the result is -1 if the string was not found.
7475c70ea4cSNiels Provos  */
7484545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
749f90500a5SNick Mathewson struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
7505c70ea4cSNiels Provos 
7518a99083fSNick Mathewson /**
7528a99083fSNick Mathewson    Search for a string within part of an evbuffer.
7538a99083fSNick Mathewson 
7548a99083fSNick Mathewson    @param buffer the evbuffer to be searched
7558a99083fSNick Mathewson    @param what the string to be searched for
7568a99083fSNick Mathewson    @param len the length of the search string
7578a99083fSNick Mathewson    @param start NULL or a pointer to a valid struct evbuffer_ptr that
7588a99083fSNick Mathewson      indicates where we should start searching.
7598a99083fSNick Mathewson    @param end NULL or a pointer to a valid struct evbuffer_ptr that
7608a99083fSNick Mathewson      indicates where we should stop searching.
7618a99083fSNick Mathewson    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
7628a99083fSNick Mathewson      first occurrence of the string in the buffer after 'start'.  The 'pos'
7638a99083fSNick Mathewson      field of the result is -1 if the string was not found.
7648a99083fSNick Mathewson  */
7654545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
7668a99083fSNick Mathewson struct evbuffer_ptr evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end);
7678a99083fSNick Mathewson 
7682888faccSNick Mathewson /**
7692888faccSNick Mathewson    Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()
7702888faccSNick Mathewson 
7712888faccSNick Mathewson    @see evbuffer_ptr_set() */
772f90500a5SNick Mathewson enum evbuffer_ptr_how {
773f90500a5SNick Mathewson 	/** Sets the pointer to the position; can be called on with an
7740b4ab122SNick Mathewson 	    uninitialized evbuffer_ptr. */
775f90500a5SNick Mathewson 	EVBUFFER_PTR_SET,
776f90500a5SNick Mathewson 	/** Advances the pointer by adding to the current position. */
777f90500a5SNick Mathewson 	EVBUFFER_PTR_ADD
778f90500a5SNick Mathewson };
779f90500a5SNick Mathewson 
780f90500a5SNick Mathewson /**
7810b4ab122SNick Mathewson    Sets the search pointer in the buffer to position.
782f90500a5SNick Mathewson 
783261ba63dSNick Mathewson    There are two ways to use this function: you can call
784261ba63dSNick Mathewson       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
785261ba63dSNick Mathewson    to move 'pos' to a position 'N' bytes after the start of the buffer, or
7868674e4fbSjer-gentoo       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_ADD)
787261ba63dSNick Mathewson    to move 'pos' forward by 'N' bytes.
788261ba63dSNick Mathewson 
789261ba63dSNick Mathewson    If evbuffer_ptr is not initialized, this function can only be called
790f90500a5SNick Mathewson    with EVBUFFER_PTR_SET.
791f90500a5SNick Mathewson 
792261ba63dSNick Mathewson    An evbuffer_ptr can represent any position from the start of the buffer to
793261ba63dSNick Mathewson    a position immediately after the end of the buffer.
794261ba63dSNick Mathewson 
795f90500a5SNick Mathewson    @param buffer the evbuffer to be search
796f90500a5SNick Mathewson    @param ptr a pointer to a struct evbuffer_ptr
797f90500a5SNick Mathewson    @param position the position at which to start the next search
798f90500a5SNick Mathewson    @param how determines how the pointer should be manipulated.
799f90500a5SNick Mathewson    @returns 0 on success or -1 otherwise
800f90500a5SNick Mathewson */
8014545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
802f90500a5SNick Mathewson int
8032888faccSNick Mathewson evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
804f90500a5SNick Mathewson     size_t position, enum evbuffer_ptr_how how);
8055c70ea4cSNiels Provos 
806d4134772SNick Mathewson /**
807d4134772SNick Mathewson    Search for an end-of-line string within an evbuffer.
808d4134772SNick Mathewson 
809d4134772SNick Mathewson    @param buffer the evbuffer to be searched
810d4134772SNick Mathewson    @param start NULL or a pointer to a valid struct evbuffer_ptr to start
811d4134772SNick Mathewson       searching at.
812d4134772SNick Mathewson    @param eol_len_out If non-NULL, the pointed-to value will be set to
813d4134772SNick Mathewson       the length of the end-of-line string.
814d4134772SNick Mathewson    @param eol_style The kind of EOL to look for; see evbuffer_readln() for
815d4134772SNick Mathewson       more information
816d4134772SNick Mathewson    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
817d4134772SNick Mathewson      first occurrence EOL in the buffer after 'start'.  The 'pos'
818d4134772SNick Mathewson      field of the result is -1 if the string was not found.
819d4134772SNick Mathewson  */
8204545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
821d4134772SNick Mathewson struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
822d4134772SNick Mathewson     struct evbuffer_ptr *start, size_t *eol_len_out,
823d4134772SNick Mathewson     enum evbuffer_eol_style eol_style);
824d4134772SNick Mathewson 
82523243b8aSNick Mathewson /** Function to peek at data inside an evbuffer without removing it or
82623243b8aSNick Mathewson     copying it out.
82723243b8aSNick Mathewson 
82823243b8aSNick Mathewson     Pointers to the data are returned by filling the 'vec_out' array
82923243b8aSNick Mathewson     with pointers to one or more extents of data inside the buffer.
83023243b8aSNick Mathewson 
83123243b8aSNick Mathewson     The total data in the extents that you get back may be more than
83223243b8aSNick Mathewson     you requested (if there is more data last extent than you asked
83323243b8aSNick Mathewson     for), or less (if you do not provide enough evbuffer_iovecs, or if
83423243b8aSNick Mathewson     the buffer does not have as much data as you asked to see).
83523243b8aSNick Mathewson 
83623243b8aSNick Mathewson     @param buffer the evbuffer to peek into,
8377bbf6ca7SNick Mathewson     @param len the number of bytes to try to peek.  If len is negative, we
8387bbf6ca7SNick Mathewson        will try to fill as much of vec_out as we can.  If len is negative
8397bbf6ca7SNick Mathewson        and vec_out is not provided, we return the number of evbuffer_iovecs
8407bbf6ca7SNick Mathewson        that would be needed to get all the data in the buffer.
84123243b8aSNick Mathewson     @param start_at an evbuffer_ptr indicating the point at which we
84223243b8aSNick Mathewson        should start looking for data.  NULL means, "At the start of the
84323243b8aSNick Mathewson        buffer."
84423243b8aSNick Mathewson     @param vec_out an array of evbuffer_iovec
8453f0e4928SNick Mathewson     @param n_vec the length of vec_out.  If 0, we only count how many
84623243b8aSNick Mathewson        extents would be necessary to point to the requested amount of
84723243b8aSNick Mathewson        data.
84823243b8aSNick Mathewson     @return The number of extents needed.  This may be less than n_vec
84923243b8aSNick Mathewson        if we didn't need all the evbuffer_iovecs we were given, or more
85023243b8aSNick Mathewson        than n_vec if we would need more to return all the data that was
85123243b8aSNick Mathewson        requested.
85223243b8aSNick Mathewson  */
8534545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
85423243b8aSNick Mathewson int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
85523243b8aSNick Mathewson     struct evbuffer_ptr *start_at,
85623243b8aSNick Mathewson     struct evbuffer_iovec *vec_out, int n_vec);
85723243b8aSNick Mathewson 
8582888faccSNick Mathewson 
8592888faccSNick Mathewson /** Structure passed to an evbuffer_cb_func evbuffer callback
8602888faccSNick Mathewson 
8612888faccSNick Mathewson     @see evbuffer_cb_func, evbuffer_add_cb()
8622888faccSNick Mathewson  */
8632888faccSNick Mathewson struct evbuffer_cb_info {
8642888faccSNick Mathewson 	/** The number of bytes in this evbuffer when callbacks were last
8652888faccSNick Mathewson 	 * invoked. */
8662888faccSNick Mathewson 	size_t orig_size;
8672888faccSNick Mathewson 	/** The number of bytes added since callbacks were last invoked. */
8682888faccSNick Mathewson 	size_t n_added;
8692888faccSNick Mathewson 	/** The number of bytes removed since callbacks were last invoked. */
8702888faccSNick Mathewson 	size_t n_deleted;
8712888faccSNick Mathewson };
8722888faccSNick Mathewson 
873de7f7a84SNick Mathewson /** Type definition for a callback that is invoked whenever data is added or
874c735f2b4SNick Mathewson     removed from an evbuffer.
875f446f149SNick Mathewson 
876c735f2b4SNick Mathewson     An evbuffer may have one or more callbacks set at a time.  The order
8770b4ab122SNick Mathewson     in which they are executed is undefined.
878c735f2b4SNick Mathewson 
879c735f2b4SNick Mathewson     A callback function may add more callbacks, or remove itself from the
880c735f2b4SNick Mathewson     list of callbacks, or add or remove data from the buffer.  It may not
881c735f2b4SNick Mathewson     remove another callback from the list.
882c735f2b4SNick Mathewson 
883de7f7a84SNick Mathewson     If a callback adds or removes data from the buffer or from another
884de7f7a84SNick Mathewson     buffer, this can cause a recursive invocation of your callback or
885de7f7a84SNick Mathewson     other callbacks.  If you ask for an infinite loop, you might just get
886de7f7a84SNick Mathewson     one: watch out!
887c735f2b4SNick Mathewson 
888c735f2b4SNick Mathewson     @param buffer the buffer whose size has changed
88960e0d59bSNick Mathewson     @param info a structure describing how the buffer changed.
890c735f2b4SNick Mathewson     @param arg a pointer to user data
891c735f2b4SNick Mathewson */
892f1b1bad4SNick Mathewson typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
893c735f2b4SNick Mathewson 
894c735f2b4SNick Mathewson struct evbuffer_cb_entry;
895c735f2b4SNick Mathewson /** Add a new callback to an evbuffer.
896c735f2b4SNick Mathewson 
897c735f2b4SNick Mathewson   Subsequent calls to evbuffer_add_cb() add new callbacks.  To remove this
898c735f2b4SNick Mathewson   callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
899f446f149SNick Mathewson 
9005c70ea4cSNiels Provos   @param buffer the evbuffer to be monitored
901c735f2b4SNick Mathewson   @param cb the callback function to invoke when the evbuffer is modified,
902c735f2b4SNick Mathewson 	or NULL to remove all callbacks.
9035c70ea4cSNiels Provos   @param cbarg an argument to be provided to the callback function
904c735f2b4SNick Mathewson   @return a handle to the callback on success, or NULL on failure.
9055c70ea4cSNiels Provos  */
9064545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
907f1b1bad4SNick Mathewson struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
908c735f2b4SNick Mathewson 
909c735f2b4SNick Mathewson /** Remove a callback from an evbuffer, given a handle returned from
910c735f2b4SNick Mathewson     evbuffer_add_cb.
911c735f2b4SNick Mathewson 
912c735f2b4SNick Mathewson     Calling this function invalidates the handle.
913c735f2b4SNick Mathewson 
914c735f2b4SNick Mathewson     @return 0 if a callback was removed, or -1 if no matching callback was
915c735f2b4SNick Mathewson     found.
916c735f2b4SNick Mathewson  */
9174545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
918c735f2b4SNick Mathewson int evbuffer_remove_cb_entry(struct evbuffer *buffer,
919c735f2b4SNick Mathewson 			     struct evbuffer_cb_entry *ent);
920c735f2b4SNick Mathewson 
921c735f2b4SNick Mathewson /** Remove a callback from an evbuffer, given the function and argument
922c735f2b4SNick Mathewson     used to add it.
923c735f2b4SNick Mathewson 
924c735f2b4SNick Mathewson     @return 0 if a callback was removed, or -1 if no matching callback was
925c735f2b4SNick Mathewson     found.
926c735f2b4SNick Mathewson  */
9274545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
928f1b1bad4SNick Mathewson int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
9295c70ea4cSNiels Provos 
930bba69e03SNick Mathewson /** If this flag is not set, then a callback is temporarily disabled, and
9312888faccSNick Mathewson  * should not be invoked.
9322888faccSNick Mathewson  *
9332888faccSNick Mathewson  * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()
9342888faccSNick Mathewson  */
93581dd04a7SNick Mathewson #define EVBUFFER_CB_ENABLED 1
936f1b1bad4SNick Mathewson 
937bba69e03SNick Mathewson /** Change the flags that are set for a callback on a buffer by adding more.
93881dd04a7SNick Mathewson 
93981dd04a7SNick Mathewson     @param buffer the evbuffer that the callback is watching.
94081dd04a7SNick Mathewson     @param cb the callback whose status we want to change.
941bba69e03SNick Mathewson     @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
94281dd04a7SNick Mathewson     @return 0 on success, -1 on failure.
94381dd04a7SNick Mathewson  */
9444545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
94581dd04a7SNick Mathewson int evbuffer_cb_set_flags(struct evbuffer *buffer,
9468d3a10f8SNick Mathewson 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
9478d3a10f8SNick Mathewson 
948bba69e03SNick Mathewson /** Change the flags that are set for a callback on a buffer by removing some
949bba69e03SNick Mathewson 
950bba69e03SNick Mathewson     @param buffer the evbuffer that the callback is watching.
951bba69e03SNick Mathewson     @param cb the callback whose status we want to change.
952bba69e03SNick Mathewson     @param flags EVBUFFER_CB_ENABLED to disable the callback.
953bba69e03SNick Mathewson     @return 0 on success, -1 on failure.
954bba69e03SNick Mathewson  */
9554545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
956bba69e03SNick Mathewson int evbuffer_cb_clear_flags(struct evbuffer *buffer,
957bba69e03SNick Mathewson 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
958bba69e03SNick Mathewson 
95949354138SNick Mathewson #if 0
9608d3a10f8SNick Mathewson /** Postpone calling a given callback until unsuspend is called later.
9618d3a10f8SNick Mathewson 
9628d3a10f8SNick Mathewson     This is different from disabling the callback, since the callback will get
9638d3a10f8SNick Mathewson 	invoked later if the buffer size changes between now and when we unsuspend
9648d3a10f8SNick Mathewson 	it.
9658d3a10f8SNick Mathewson 
9668d3a10f8SNick Mathewson 	@param the buffer that the callback is watching.
9678d3a10f8SNick Mathewson 	@param cb the callback we want to suspend.
9688d3a10f8SNick Mathewson  */
9694545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
9708d3a10f8SNick Mathewson void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
9710b4ab122SNick Mathewson /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
9728d3a10f8SNick Mathewson 
9738d3a10f8SNick Mathewson 	If data was added to or removed from the buffer while the callback was
9748d3a10f8SNick Mathewson 	suspended, the callback will get called once now.
9758d3a10f8SNick Mathewson 
9768d3a10f8SNick Mathewson 	@param the buffer that the callback is watching.
9778d3a10f8SNick Mathewson 	@param cb the callback we want to stop suspending.
9788d3a10f8SNick Mathewson  */
9794545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
9808d3a10f8SNick Mathewson void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
98149354138SNick Mathewson #endif
98281dd04a7SNick Mathewson 
9835c70ea4cSNiels Provos /**
984773b0a5dSNick Mathewson   Makes the data at the beginning of an evbuffer contiguous.
9855c70ea4cSNiels Provos 
9865c70ea4cSNiels Provos   @param buf the evbuffer to make contiguous
9870322ce0aSNick Mathewson   @param size the number of bytes to make contiguous, or -1 to make the
9880322ce0aSNick Mathewson 	entire buffer contiguous.
989cf8d1cdbSDan Petro   @return a pointer to the contiguous memory array, or NULL if param size
990cf8d1cdbSDan Petro 	requested more data than is present in the buffer.
9915c70ea4cSNiels Provos */
9925c70ea4cSNiels Provos 
9934545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
994b2e8fd0eSNick Mathewson unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
9955c70ea4cSNiels Provos 
9965c70ea4cSNiels Provos /**
9975c70ea4cSNiels Provos   Prepends data to the beginning of the evbuffer
9985c70ea4cSNiels Provos 
9995c70ea4cSNiels Provos   @param buf the evbuffer to which to prepend data
10005c70ea4cSNiels Provos   @param data a pointer to the memory to prepend
10015c70ea4cSNiels Provos   @param size the number of bytes to prepend
10025c70ea4cSNiels Provos   @return 0 if successful, or -1 otherwise
10035c70ea4cSNiels Provos */
10045c70ea4cSNiels Provos 
10054545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
10065c70ea4cSNiels Provos int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
10075c70ea4cSNiels Provos 
10085c70ea4cSNiels Provos /**
1009f446f149SNick Mathewson   Prepends all data from the src evbuffer to the beginning of the dst
1010f446f149SNick Mathewson   evbuffer.
10115c70ea4cSNiels Provos 
10125c70ea4cSNiels Provos   @param dst the evbuffer to which to prepend data
10135c70ea4cSNiels Provos   @param src the evbuffer to prepend; it will be emptied as a result
1014747331d1SNick Mathewson   @return 0 if successful, or -1 otherwise
10155c70ea4cSNiels Provos */
10164545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1017747331d1SNick Mathewson int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
10185c70ea4cSNiels Provos 
1019747331d1SNick Mathewson /**
1020747331d1SNick Mathewson    Prevent calls that modify an evbuffer from succeeding. A buffer may
1021747331d1SNick Mathewson    frozen at the front, at the back, or at both the front and the back.
1022747331d1SNick Mathewson 
1023747331d1SNick Mathewson    If the front of a buffer is frozen, operations that drain data from
1024747331d1SNick Mathewson    the front of the buffer, or that prepend data to the buffer, will
1025747331d1SNick Mathewson    fail until it is unfrozen.   If the back a buffer is frozen, operations
1026747331d1SNick Mathewson    that append data from the buffer will fail until it is unfrozen.
1027747331d1SNick Mathewson 
1028747331d1SNick Mathewson    @param buf The buffer to freeze
1029747331d1SNick Mathewson    @param at_front If true, we freeze the front of the buffer.  If false,
1030747331d1SNick Mathewson       we freeze the back.
1031747331d1SNick Mathewson    @return 0 on success, -1 on failure.
1032747331d1SNick Mathewson */
10334545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1034747331d1SNick Mathewson int evbuffer_freeze(struct evbuffer *buf, int at_front);
1035747331d1SNick Mathewson /**
1036747331d1SNick Mathewson    Re-enable calls that modify an evbuffer.
1037747331d1SNick Mathewson 
1038747331d1SNick Mathewson    @param buf The buffer to un-freeze
1039747331d1SNick Mathewson    @param at_front If true, we unfreeze the front of the buffer.  If false,
1040747331d1SNick Mathewson       we unfreeze the back.
1041747331d1SNick Mathewson    @return 0 on success, -1 on failure.
1042747331d1SNick Mathewson  */
10434545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1044747331d1SNick Mathewson int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
1045747331d1SNick Mathewson 
1046b29b875dSNick Mathewson struct event_base;
1047b29b875dSNick Mathewson /**
1048b29b875dSNick Mathewson    Force all the callbacks on an evbuffer to be run, not immediately after
1049b29b875dSNick Mathewson    the evbuffer is altered, but instead from inside the event loop.
1050b29b875dSNick Mathewson 
1051b29b875dSNick Mathewson    This can be used to serialize all the callbacks to a single thread
1052b29b875dSNick Mathewson    of execution.
1053b29b875dSNick Mathewson  */
10544545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1055b29b875dSNick Mathewson int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
1056b29b875dSNick Mathewson 
1057aaec5acaSMark Ellzey /**
1058aaec5acaSMark Ellzey   Append data from 1 or more iovec's to an evbuffer
1059aaec5acaSMark Ellzey 
1060aaec5acaSMark Ellzey   Calculates the number of bytes needed for an iovec structure and guarantees
1061aaec5acaSMark Ellzey   all data will fit into a single chain. Can be used in lieu of functionality
1062aaec5acaSMark Ellzey   which calls evbuffer_add() constantly before being used to increase
1063aaec5acaSMark Ellzey   performance.
1064aaec5acaSMark Ellzey 
1065aaec5acaSMark Ellzey   @param buffer the destination buffer
1066aaec5acaSMark Ellzey   @param vec the source iovec
1067aaec5acaSMark Ellzey   @param n_vec the number of iovec structures.
1068aaec5acaSMark Ellzey   @return the number of bytes successfully written to the output buffer.
1069aaec5acaSMark Ellzey */
10704545fa9bSTrond Norbye EVENT2_EXPORT_SYMBOL
1071aaec5acaSMark Ellzey size_t evbuffer_add_iovec(struct evbuffer * buffer, struct evbuffer_iovec * vec, int n_vec);
1072aaec5acaSMark Ellzey 
10735c70ea4cSNiels Provos #ifdef __cplusplus
10745c70ea4cSNiels Provos }
10755c70ea4cSNiels Provos #endif
10765c70ea4cSNiels Provos 
10773f8c7cd0SNick Mathewson #endif /* EVENT2_BUFFER_H_INCLUDED_ */
1078