xref: /freebsd/contrib/ntp/sntp/libevent/include/event2/buffer.h (revision a466cc55373fc3cf86837f09da729535b57e69a1)
12b15cb3dSCy Schubert /*
22b15cb3dSCy Schubert  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
32b15cb3dSCy Schubert  *
42b15cb3dSCy Schubert  * Redistribution and use in source and binary forms, with or without
52b15cb3dSCy Schubert  * modification, are permitted provided that the following conditions
62b15cb3dSCy Schubert  * are met:
72b15cb3dSCy Schubert  * 1. Redistributions of source code must retain the above copyright
82b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer.
92b15cb3dSCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
102b15cb3dSCy Schubert  *    notice, this list of conditions and the following disclaimer in the
112b15cb3dSCy Schubert  *    documentation and/or other materials provided with the distribution.
122b15cb3dSCy Schubert  * 3. The name of the author may not be used to endorse or promote products
132b15cb3dSCy Schubert  *    derived from this software without specific prior written permission.
142b15cb3dSCy Schubert  *
152b15cb3dSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162b15cb3dSCy Schubert  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172b15cb3dSCy Schubert  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
182b15cb3dSCy Schubert  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
192b15cb3dSCy Schubert  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
202b15cb3dSCy Schubert  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
212b15cb3dSCy Schubert  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
222b15cb3dSCy Schubert  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
232b15cb3dSCy Schubert  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
242b15cb3dSCy Schubert  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
252b15cb3dSCy Schubert  */
262b15cb3dSCy Schubert #ifndef EVENT2_BUFFER_H_INCLUDED_
272b15cb3dSCy Schubert #define EVENT2_BUFFER_H_INCLUDED_
282b15cb3dSCy Schubert 
292b15cb3dSCy Schubert /** @file event2/buffer.h
302b15cb3dSCy Schubert 
312b15cb3dSCy Schubert   Functions for buffering data for network sending or receiving.
322b15cb3dSCy Schubert 
332b15cb3dSCy Schubert   An evbuffer can be used for preparing data before sending it to
342b15cb3dSCy Schubert   the network or conversely for reading data from the network.
352b15cb3dSCy Schubert   Evbuffers try to avoid memory copies as much as possible.  As a
362b15cb3dSCy Schubert   result, evbuffers can be used to pass data around without actually
372b15cb3dSCy Schubert   incurring the overhead of copying the data.
382b15cb3dSCy Schubert 
392b15cb3dSCy Schubert   A new evbuffer can be allocated with evbuffer_new(), and can be
402b15cb3dSCy Schubert   freed with evbuffer_free().  Most users will be using evbuffers via
412b15cb3dSCy Schubert   the bufferevent interface.  To access a bufferevent's evbuffers, use
422b15cb3dSCy Schubert   bufferevent_get_input() and bufferevent_get_output().
432b15cb3dSCy Schubert 
442b15cb3dSCy Schubert   There are several guidelines for using evbuffers.
452b15cb3dSCy Schubert 
462b15cb3dSCy Schubert   - if you already know how much data you are going to add as a result
472b15cb3dSCy Schubert     of calling evbuffer_add() multiple times, it makes sense to use
482b15cb3dSCy Schubert     evbuffer_expand() first to make sure that enough memory is allocated
492b15cb3dSCy Schubert     before hand.
502b15cb3dSCy Schubert 
512b15cb3dSCy Schubert   - evbuffer_add_buffer() adds the contents of one buffer to the other
522b15cb3dSCy Schubert     without incurring any unnecessary memory copies.
532b15cb3dSCy Schubert 
542b15cb3dSCy Schubert   - evbuffer_add() and evbuffer_add_buffer() do not mix very well:
552b15cb3dSCy Schubert     if you use them, you will wind up with fragmented memory in your
562b15cb3dSCy Schubert 	buffer.
572b15cb3dSCy Schubert 
582b15cb3dSCy Schubert   - For high-performance code, you may want to avoid copying data into and out
592b15cb3dSCy Schubert     of buffers.  You can skip the copy step by using
602b15cb3dSCy Schubert     evbuffer_reserve_space()/evbuffer_commit_space() when writing into a
612b15cb3dSCy Schubert     buffer, and evbuffer_peek() when reading.
622b15cb3dSCy Schubert 
632b15cb3dSCy Schubert   In Libevent 2.0 and later, evbuffers are represented using a linked
642b15cb3dSCy Schubert   list of memory chunks, with pointers to the first and last chunk in
652b15cb3dSCy Schubert   the chain.
662b15cb3dSCy Schubert 
672b15cb3dSCy Schubert   As the contents of an evbuffer can be stored in multiple different
682b15cb3dSCy Schubert   memory blocks, it cannot be accessed directly.  Instead, evbuffer_pullup()
692b15cb3dSCy Schubert   can be used to force a specified number of bytes to be contiguous. This
702b15cb3dSCy Schubert   will cause memory reallocation and memory copies if the data is split
712b15cb3dSCy Schubert   across multiple blocks.  It is more efficient, however, to use
722b15cb3dSCy Schubert   evbuffer_peek() if you don't require that the memory to be contiguous.
732b15cb3dSCy Schubert  */
742b15cb3dSCy Schubert 
752b15cb3dSCy Schubert #include <event2/visibility.h>
762b15cb3dSCy Schubert 
772b15cb3dSCy Schubert #ifdef __cplusplus
782b15cb3dSCy Schubert extern "C" {
792b15cb3dSCy Schubert #endif
802b15cb3dSCy Schubert 
812b15cb3dSCy Schubert #include <event2/event-config.h>
822b15cb3dSCy Schubert #include <stdarg.h>
832b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_TYPES_H
842b15cb3dSCy Schubert #include <sys/types.h>
852b15cb3dSCy Schubert #endif
862b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_UIO_H
872b15cb3dSCy Schubert #include <sys/uio.h>
882b15cb3dSCy Schubert #endif
892b15cb3dSCy Schubert #include <event2/util.h>
902b15cb3dSCy Schubert 
912b15cb3dSCy Schubert /**
922b15cb3dSCy Schubert    An evbuffer is an opaque data type for efficiently buffering data to be
932b15cb3dSCy Schubert    sent or received on the network.
942b15cb3dSCy Schubert 
952b15cb3dSCy Schubert    @see event2/event.h for more information
962b15cb3dSCy Schubert */
972b15cb3dSCy Schubert struct evbuffer
982b15cb3dSCy Schubert #ifdef EVENT_IN_DOXYGEN_
992b15cb3dSCy Schubert {}
1002b15cb3dSCy Schubert #endif
1012b15cb3dSCy Schubert ;
1022b15cb3dSCy Schubert 
1032b15cb3dSCy Schubert /**
1042b15cb3dSCy Schubert     Pointer to a position within an evbuffer.
1052b15cb3dSCy Schubert 
1062b15cb3dSCy Schubert     Used when repeatedly searching through a buffer.  Calling any function
1072b15cb3dSCy Schubert     that modifies or re-packs the buffer contents may invalidate all
108a25439b6SCy Schubert     evbuffer_ptrs for that buffer.  Do not modify or contruct these values
109a25439b6SCy Schubert     except with evbuffer_ptr_set.
1102b15cb3dSCy Schubert 
1112b15cb3dSCy Schubert     An evbuffer_ptr can represent any position from the start of a buffer up
1122b15cb3dSCy Schubert     to a position immediately after the end of a buffer.
1132b15cb3dSCy Schubert 
1142b15cb3dSCy Schubert     @see evbuffer_ptr_set()
1152b15cb3dSCy Schubert  */
1162b15cb3dSCy Schubert struct evbuffer_ptr {
1172b15cb3dSCy Schubert 	ev_ssize_t pos;
1182b15cb3dSCy Schubert 
1192b15cb3dSCy Schubert 	/* Do not alter or rely on the values of fields: they are for internal
1202b15cb3dSCy Schubert 	 * use */
1212b15cb3dSCy Schubert 	struct {
1222b15cb3dSCy Schubert 		void *chain;
1232b15cb3dSCy Schubert 		size_t pos_in_chain;
1242b15cb3dSCy Schubert 	} internal_;
1252b15cb3dSCy Schubert };
1262b15cb3dSCy Schubert 
1272b15cb3dSCy Schubert /** Describes a single extent of memory inside an evbuffer.  Used for
1282b15cb3dSCy Schubert     direct-access functions.
1292b15cb3dSCy Schubert 
1302b15cb3dSCy Schubert     @see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
1312b15cb3dSCy Schubert  */
1322b15cb3dSCy Schubert #ifdef EVENT__HAVE_SYS_UIO_H
1332b15cb3dSCy Schubert #define evbuffer_iovec iovec
1342b15cb3dSCy Schubert /* Internal use -- defined only if we are using the native struct iovec */
1352b15cb3dSCy Schubert #define EVBUFFER_IOVEC_IS_NATIVE_
1362b15cb3dSCy Schubert #else
1372b15cb3dSCy Schubert struct evbuffer_iovec {
1382b15cb3dSCy Schubert 	/** The start of the extent of memory. */
1392b15cb3dSCy Schubert 	void *iov_base;
1402b15cb3dSCy Schubert 	/** The length of the extent of memory. */
1412b15cb3dSCy Schubert 	size_t iov_len;
1422b15cb3dSCy Schubert };
1432b15cb3dSCy Schubert #endif
1442b15cb3dSCy Schubert 
1452b15cb3dSCy Schubert /**
1462b15cb3dSCy Schubert   Allocate storage for a new evbuffer.
1472b15cb3dSCy Schubert 
1482b15cb3dSCy Schubert   @return a pointer to a newly allocated evbuffer struct, or NULL if an error
1492b15cb3dSCy Schubert 	occurred
1502b15cb3dSCy Schubert  */
1512b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
1522b15cb3dSCy Schubert struct evbuffer *evbuffer_new(void);
1532b15cb3dSCy Schubert /**
1542b15cb3dSCy Schubert   Deallocate storage for an evbuffer.
1552b15cb3dSCy Schubert 
1562b15cb3dSCy Schubert   @param buf pointer to the evbuffer to be freed
1572b15cb3dSCy Schubert  */
1582b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
1592b15cb3dSCy Schubert void evbuffer_free(struct evbuffer *buf);
1602b15cb3dSCy Schubert 
1612b15cb3dSCy Schubert /**
1622b15cb3dSCy Schubert    Enable locking on an evbuffer so that it can safely be used by multiple
1632b15cb3dSCy Schubert    threads at the same time.
1642b15cb3dSCy Schubert 
1652b15cb3dSCy Schubert    NOTE: when locking is enabled, the lock will be held when callbacks are
1662b15cb3dSCy Schubert    invoked.  This could result in deadlock if you aren't careful.  Plan
1672b15cb3dSCy Schubert    accordingly!
1682b15cb3dSCy Schubert 
1692b15cb3dSCy Schubert    @param buf An evbuffer to make lockable.
1702b15cb3dSCy Schubert    @param lock A lock object, or NULL if we should allocate our own.
1712b15cb3dSCy Schubert    @return 0 on success, -1 on failure.
1722b15cb3dSCy Schubert  */
1732b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
1742b15cb3dSCy Schubert int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
1752b15cb3dSCy Schubert 
1762b15cb3dSCy Schubert /**
1772b15cb3dSCy Schubert    Acquire the lock on an evbuffer.  Has no effect if locking was not enabled
1782b15cb3dSCy Schubert    with evbuffer_enable_locking.
1792b15cb3dSCy Schubert */
1802b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
1812b15cb3dSCy Schubert void evbuffer_lock(struct evbuffer *buf);
1822b15cb3dSCy Schubert 
1832b15cb3dSCy Schubert /**
1842b15cb3dSCy Schubert    Release the lock on an evbuffer.  Has no effect if locking was not enabled
1852b15cb3dSCy Schubert    with evbuffer_enable_locking.
1862b15cb3dSCy Schubert */
1872b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
1882b15cb3dSCy Schubert void evbuffer_unlock(struct evbuffer *buf);
1892b15cb3dSCy Schubert 
1902b15cb3dSCy Schubert 
1912b15cb3dSCy Schubert /** If this flag is set, then we will not use evbuffer_peek(),
1922b15cb3dSCy Schubert  * evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes
1932b15cb3dSCy Schubert  * from this buffer: we'll only take bytes out of this buffer by
1942b15cb3dSCy Schubert  * writing them to the network (as with evbuffer_write_atmost), by
1952b15cb3dSCy Schubert  * removing them without observing them (as with evbuffer_drain),
1962b15cb3dSCy Schubert  * or by copying them all out at once (as with evbuffer_add_buffer).
1972b15cb3dSCy Schubert  *
1982b15cb3dSCy Schubert  * Using this option allows the implementation to use sendfile-based
1992b15cb3dSCy Schubert  * operations for evbuffer_add_file(); see that function for more
2002b15cb3dSCy Schubert  * information.
2012b15cb3dSCy Schubert  *
2022b15cb3dSCy Schubert  * This flag is on by default for bufferevents that can take advantage
2032b15cb3dSCy Schubert  * of it; you should never actually need to set it on a bufferevent's
2042b15cb3dSCy Schubert  * output buffer.
2052b15cb3dSCy Schubert  */
2062b15cb3dSCy Schubert #define EVBUFFER_FLAG_DRAINS_TO_FD 1
2072b15cb3dSCy Schubert 
2082b15cb3dSCy Schubert /** Change the flags that are set for an evbuffer by adding more.
2092b15cb3dSCy Schubert  *
2102b15cb3dSCy Schubert  * @param buffer the evbuffer that the callback is watching.
2112b15cb3dSCy Schubert  * @param cb the callback whose status we want to change.
2122b15cb3dSCy Schubert  * @param flags One or more EVBUFFER_FLAG_* options
2132b15cb3dSCy Schubert  * @return 0 on success, -1 on failure.
2142b15cb3dSCy Schubert  */
2152b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
2162b15cb3dSCy Schubert int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
2172b15cb3dSCy Schubert /** Change the flags that are set for an evbuffer by removing some.
2182b15cb3dSCy Schubert  *
2192b15cb3dSCy Schubert  * @param buffer the evbuffer that the callback is watching.
2202b15cb3dSCy Schubert  * @param cb the callback whose status we want to change.
2212b15cb3dSCy Schubert  * @param flags One or more EVBUFFER_FLAG_* options
2222b15cb3dSCy Schubert  * @return 0 on success, -1 on failure.
2232b15cb3dSCy Schubert  */
2242b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
2252b15cb3dSCy Schubert int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
2262b15cb3dSCy Schubert 
2272b15cb3dSCy Schubert /**
2282b15cb3dSCy Schubert   Returns the total number of bytes stored in the evbuffer
2292b15cb3dSCy Schubert 
2302b15cb3dSCy Schubert   @param buf pointer to the evbuffer
2312b15cb3dSCy Schubert   @return the number of bytes stored in the evbuffer
2322b15cb3dSCy Schubert */
2332b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
2342b15cb3dSCy Schubert size_t evbuffer_get_length(const struct evbuffer *buf);
2352b15cb3dSCy Schubert 
2362b15cb3dSCy Schubert /**
2372b15cb3dSCy Schubert    Returns the number of contiguous available bytes in the first buffer chain.
2382b15cb3dSCy Schubert 
2392b15cb3dSCy Schubert    This is useful when processing data that might be split into multiple
2402b15cb3dSCy Schubert    chains, or that might all be in the first chain.  Calls to
2412b15cb3dSCy Schubert    evbuffer_pullup() that cause reallocation and copying of data can thus be
2422b15cb3dSCy Schubert    avoided.
2432b15cb3dSCy Schubert 
2442b15cb3dSCy Schubert    @param buf pointer to the evbuffer
2452b15cb3dSCy Schubert    @return 0 if no data is available, otherwise the number of available bytes
2462b15cb3dSCy Schubert      in the first buffer chain.
2472b15cb3dSCy Schubert */
2482b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
2492b15cb3dSCy Schubert size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
2502b15cb3dSCy Schubert 
2512b15cb3dSCy Schubert /**
2522b15cb3dSCy Schubert   Expands the available space in an evbuffer.
2532b15cb3dSCy Schubert 
2542b15cb3dSCy Schubert   Expands the available space in the evbuffer to at least datlen, so that
2552b15cb3dSCy Schubert   appending datlen additional bytes will not require any new allocations.
2562b15cb3dSCy Schubert 
2572b15cb3dSCy Schubert   @param buf the evbuffer to be expanded
2582b15cb3dSCy Schubert   @param datlen the new minimum length requirement
2592b15cb3dSCy Schubert   @return 0 if successful, or -1 if an error occurred
2602b15cb3dSCy Schubert */
2612b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
2622b15cb3dSCy Schubert int evbuffer_expand(struct evbuffer *buf, size_t datlen);
2632b15cb3dSCy Schubert 
2642b15cb3dSCy Schubert /**
2652b15cb3dSCy Schubert    Reserves space in the last chain or chains of an evbuffer.
2662b15cb3dSCy Schubert 
2672b15cb3dSCy Schubert    Makes space available in the last chain or chains of an evbuffer that can
2682b15cb3dSCy Schubert    be arbitrarily written to by a user.  The space does not become
2692b15cb3dSCy Schubert    available for reading until it has been committed with
2702b15cb3dSCy Schubert    evbuffer_commit_space().
2712b15cb3dSCy Schubert 
2722b15cb3dSCy Schubert    The space is made available as one or more extents, represented by
2732b15cb3dSCy Schubert    an initial pointer and a length.  You can force the memory to be
2742b15cb3dSCy Schubert    available as only one extent.  Allowing more extents, however, makes the
2752b15cb3dSCy Schubert    function more efficient.
2762b15cb3dSCy Schubert 
2772b15cb3dSCy Schubert    Multiple subsequent calls to this function will make the same space
2782b15cb3dSCy Schubert    available until evbuffer_commit_space() has been called.
2792b15cb3dSCy Schubert 
2802b15cb3dSCy Schubert    It is an error to do anything that moves around the buffer's internal
2812b15cb3dSCy Schubert    memory structures before committing the space.
2822b15cb3dSCy Schubert 
2832b15cb3dSCy Schubert    NOTE: The code currently does not ever use more than two extents.
2842b15cb3dSCy Schubert    This may change in future versions.
2852b15cb3dSCy Schubert 
2862b15cb3dSCy Schubert    @param buf the evbuffer in which to reserve space.
2872b15cb3dSCy Schubert    @param size how much space to make available, at minimum.  The
2882b15cb3dSCy Schubert       total length of the extents may be greater than the requested
2892b15cb3dSCy Schubert       length.
2902b15cb3dSCy Schubert    @param vec an array of one or more evbuffer_iovec structures to
2912b15cb3dSCy Schubert       hold pointers to the reserved extents of memory.
2922b15cb3dSCy Schubert    @param n_vec The length of the vec array.  Must be at least 1;
2932b15cb3dSCy Schubert        2 is more efficient.
2942b15cb3dSCy Schubert    @return the number of provided extents, or -1 on error.
2952b15cb3dSCy Schubert    @see evbuffer_commit_space()
2962b15cb3dSCy Schubert */
2972b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
2982b15cb3dSCy Schubert int
2992b15cb3dSCy Schubert evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
3002b15cb3dSCy Schubert     struct evbuffer_iovec *vec, int n_vec);
3012b15cb3dSCy Schubert 
3022b15cb3dSCy Schubert /**
3032b15cb3dSCy Schubert    Commits previously reserved space.
3042b15cb3dSCy Schubert 
3052b15cb3dSCy Schubert    Commits some of the space previously reserved with
3062b15cb3dSCy Schubert    evbuffer_reserve_space().  It then becomes available for reading.
3072b15cb3dSCy Schubert 
3082b15cb3dSCy Schubert    This function may return an error if the pointer in the extents do
3092b15cb3dSCy Schubert    not match those returned from evbuffer_reserve_space, or if data
3102b15cb3dSCy Schubert    has been added to the buffer since the space was reserved.
3112b15cb3dSCy Schubert 
3122b15cb3dSCy Schubert    If you want to commit less data than you got reserved space for,
3132b15cb3dSCy Schubert    modify the iov_len pointer of the appropriate extent to a smaller
3142b15cb3dSCy Schubert    value.  Note that you may have received more space than you
3152b15cb3dSCy Schubert    requested if it was available!
3162b15cb3dSCy Schubert 
3172b15cb3dSCy Schubert    @param buf the evbuffer in which to reserve space.
3182b15cb3dSCy Schubert    @param vec one or two extents returned by evbuffer_reserve_space.
3192b15cb3dSCy Schubert    @param n_vecs the number of extents.
3202b15cb3dSCy Schubert    @return 0 on success, -1 on error
3212b15cb3dSCy Schubert    @see evbuffer_reserve_space()
3222b15cb3dSCy Schubert */
3232b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
3242b15cb3dSCy Schubert int evbuffer_commit_space(struct evbuffer *buf,
3252b15cb3dSCy Schubert     struct evbuffer_iovec *vec, int n_vecs);
3262b15cb3dSCy Schubert 
3272b15cb3dSCy Schubert /**
3282b15cb3dSCy Schubert   Append data to the end of an evbuffer.
3292b15cb3dSCy Schubert 
3302b15cb3dSCy Schubert   @param buf the evbuffer to be appended to
3312b15cb3dSCy Schubert   @param data pointer to the beginning of the data buffer
3322b15cb3dSCy Schubert   @param datlen the number of bytes to be copied from the data buffer
3332b15cb3dSCy Schubert   @return 0 on success, -1 on failure.
3342b15cb3dSCy Schubert  */
3352b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
3362b15cb3dSCy Schubert int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
3372b15cb3dSCy Schubert 
3382b15cb3dSCy Schubert 
3392b15cb3dSCy Schubert /**
3402b15cb3dSCy Schubert   Read data from an evbuffer and drain the bytes read.
3412b15cb3dSCy Schubert 
3422b15cb3dSCy Schubert   If more bytes are requested than are available in the evbuffer, we
3432b15cb3dSCy Schubert   only extract as many bytes as were available.
3442b15cb3dSCy Schubert 
3452b15cb3dSCy Schubert   @param buf the evbuffer to be read from
3462b15cb3dSCy Schubert   @param data the destination buffer to store the result
3472b15cb3dSCy Schubert   @param datlen the maximum size of the destination buffer
3482b15cb3dSCy Schubert   @return the number of bytes read, or -1 if we can't drain the buffer.
3492b15cb3dSCy Schubert  */
3502b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
3512b15cb3dSCy Schubert int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
3522b15cb3dSCy Schubert 
3532b15cb3dSCy Schubert /**
3542b15cb3dSCy Schubert   Read data from an evbuffer, and leave the buffer unchanged.
3552b15cb3dSCy Schubert 
3562b15cb3dSCy Schubert   If more bytes are requested than are available in the evbuffer, we
3572b15cb3dSCy Schubert   only extract as many bytes as were available.
3582b15cb3dSCy Schubert 
3592b15cb3dSCy Schubert   @param buf the evbuffer to be read from
3602b15cb3dSCy Schubert   @param data_out the destination buffer to store the result
3612b15cb3dSCy Schubert   @param datlen the maximum size of the destination buffer
3622b15cb3dSCy Schubert   @return the number of bytes read, or -1 if we can't drain the buffer.
3632b15cb3dSCy Schubert  */
3642b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
3652b15cb3dSCy Schubert ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
3662b15cb3dSCy Schubert 
3672b15cb3dSCy Schubert /**
3682b15cb3dSCy Schubert   Read data from the middle of an evbuffer, and leave the buffer unchanged.
3692b15cb3dSCy Schubert 
3702b15cb3dSCy Schubert   If more bytes are requested than are available in the evbuffer, we
3712b15cb3dSCy Schubert   only extract as many bytes as were available.
3722b15cb3dSCy Schubert 
3732b15cb3dSCy Schubert   @param buf the evbuffer to be read from
3742b15cb3dSCy Schubert   @param pos the position to start reading from
3752b15cb3dSCy Schubert   @param data_out the destination buffer to store the result
3762b15cb3dSCy Schubert   @param datlen the maximum size of the destination buffer
3772b15cb3dSCy Schubert   @return the number of bytes read, or -1 if we can't drain the buffer.
3782b15cb3dSCy Schubert  */
3792b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
3802b15cb3dSCy Schubert ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
3812b15cb3dSCy Schubert 
3822b15cb3dSCy Schubert /**
3832b15cb3dSCy Schubert   Read data from an evbuffer into another evbuffer, draining
3842b15cb3dSCy Schubert   the bytes from the source buffer.  This function avoids copy
3852b15cb3dSCy Schubert   operations to the extent possible.
3862b15cb3dSCy Schubert 
3872b15cb3dSCy Schubert   If more bytes are requested than are available in src, the src
3882b15cb3dSCy Schubert   buffer is drained completely.
3892b15cb3dSCy Schubert 
3902b15cb3dSCy Schubert   @param src the evbuffer to be read from
3912b15cb3dSCy Schubert   @param dst the destination evbuffer to store the result into
3922b15cb3dSCy Schubert   @param datlen the maximum numbers of bytes to transfer
3932b15cb3dSCy Schubert   @return the number of bytes read
3942b15cb3dSCy Schubert  */
3952b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
3962b15cb3dSCy Schubert int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
3972b15cb3dSCy Schubert     size_t datlen);
3982b15cb3dSCy Schubert 
3992b15cb3dSCy Schubert /** Used to tell evbuffer_readln what kind of line-ending to look for.
4002b15cb3dSCy Schubert  */
4012b15cb3dSCy Schubert enum evbuffer_eol_style {
4022b15cb3dSCy Schubert 	/** Any sequence of CR and LF characters is acceptable as an
4032b15cb3dSCy Schubert 	 * EOL.
4042b15cb3dSCy Schubert 	 *
4052b15cb3dSCy Schubert 	 * Note that this style can produce ambiguous results: the
4062b15cb3dSCy Schubert 	 * sequence "CRLF" will be treated as a single EOL if it is
4072b15cb3dSCy Schubert 	 * all in the buffer at once, but if you first read a CR from
4082b15cb3dSCy Schubert 	 * the network and later read an LF from the network, it will
4092b15cb3dSCy Schubert 	 * be treated as two EOLs.
4102b15cb3dSCy Schubert 	 */
4112b15cb3dSCy Schubert 	EVBUFFER_EOL_ANY,
4122b15cb3dSCy Schubert 	/** An EOL is an LF, optionally preceded by a CR.  This style is
4132b15cb3dSCy Schubert 	 * most useful for implementing text-based internet protocols. */
4142b15cb3dSCy Schubert 	EVBUFFER_EOL_CRLF,
4152b15cb3dSCy Schubert 	/** An EOL is a CR followed by an LF. */
4162b15cb3dSCy Schubert 	EVBUFFER_EOL_CRLF_STRICT,
4172b15cb3dSCy Schubert 	/** An EOL is a LF. */
4182b15cb3dSCy Schubert 	EVBUFFER_EOL_LF,
4192b15cb3dSCy Schubert 	/** An EOL is a NUL character (that is, a single byte with value 0) */
4202b15cb3dSCy Schubert 	EVBUFFER_EOL_NUL
4212b15cb3dSCy Schubert };
4222b15cb3dSCy Schubert 
4232b15cb3dSCy Schubert /**
4242b15cb3dSCy Schubert  * Read a single line from an evbuffer.
4252b15cb3dSCy Schubert  *
4262b15cb3dSCy Schubert  * Reads a line terminated by an EOL as determined by the evbuffer_eol_style
4272b15cb3dSCy Schubert  * argument.  Returns a newly allocated nul-terminated string; the caller must
4282b15cb3dSCy Schubert  * free the returned value.  The EOL is not included in the returned string.
4292b15cb3dSCy Schubert  *
4302b15cb3dSCy Schubert  * @param buffer the evbuffer to read from
4312b15cb3dSCy Schubert  * @param n_read_out if non-NULL, points to a size_t that is set to the
4322b15cb3dSCy Schubert  *       number of characters in the returned string.  This is useful for
4332b15cb3dSCy Schubert  *       strings that can contain NUL characters.
4342b15cb3dSCy Schubert  * @param eol_style the style of line-ending to use.
4352b15cb3dSCy Schubert  * @return pointer to a single line, or NULL if an error occurred
4362b15cb3dSCy Schubert  */
4372b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
4382b15cb3dSCy Schubert char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
4392b15cb3dSCy Schubert     enum evbuffer_eol_style eol_style);
4402b15cb3dSCy Schubert 
4412b15cb3dSCy Schubert /**
4422b15cb3dSCy Schubert   Move all data from one evbuffer into another evbuffer.
4432b15cb3dSCy Schubert 
4442b15cb3dSCy Schubert   This is a destructive add.  The data from one buffer moves into
4452b15cb3dSCy Schubert   the other buffer.  However, no unnecessary memory copies occur.
4462b15cb3dSCy Schubert 
4472b15cb3dSCy Schubert   @param outbuf the output buffer
4482b15cb3dSCy Schubert   @param inbuf the input buffer
4492b15cb3dSCy Schubert   @return 0 if successful, or -1 if an error occurred
4502b15cb3dSCy Schubert 
4512b15cb3dSCy Schubert   @see evbuffer_remove_buffer()
4522b15cb3dSCy Schubert  */
4532b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
4542b15cb3dSCy Schubert int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
4552b15cb3dSCy Schubert 
4562b15cb3dSCy Schubert /**
4572b15cb3dSCy Schubert   Copy data from one evbuffer into another evbuffer.
4582b15cb3dSCy Schubert 
4592b15cb3dSCy Schubert   This is a non-destructive add.  The data from one buffer is copied
4602b15cb3dSCy Schubert   into the other buffer.  However, no unnecessary memory copies occur.
4612b15cb3dSCy Schubert 
4622b15cb3dSCy Schubert   Note that buffers already containing buffer references can't be added
4632b15cb3dSCy Schubert   to other buffers.
4642b15cb3dSCy Schubert 
4652b15cb3dSCy Schubert   @param outbuf the output buffer
4662b15cb3dSCy Schubert   @param inbuf the input buffer
4672b15cb3dSCy Schubert   @return 0 if successful, or -1 if an error occurred
4682b15cb3dSCy Schubert  */
4692b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
4702b15cb3dSCy Schubert int evbuffer_add_buffer_reference(struct evbuffer *outbuf,
4712b15cb3dSCy Schubert     struct evbuffer *inbuf);
4722b15cb3dSCy Schubert 
4732b15cb3dSCy Schubert /**
4742b15cb3dSCy Schubert    A cleanup function for a piece of memory added to an evbuffer by
4752b15cb3dSCy Schubert    reference.
4762b15cb3dSCy Schubert 
4772b15cb3dSCy Schubert    @see evbuffer_add_reference()
4782b15cb3dSCy Schubert  */
4792b15cb3dSCy Schubert typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
4802b15cb3dSCy Schubert     size_t datalen, void *extra);
4812b15cb3dSCy Schubert 
4822b15cb3dSCy Schubert /**
4832b15cb3dSCy Schubert   Reference memory into an evbuffer without copying.
4842b15cb3dSCy Schubert 
4852b15cb3dSCy Schubert   The memory needs to remain valid until all the added data has been
4862b15cb3dSCy Schubert   read.  This function keeps just a reference to the memory without
4872b15cb3dSCy Schubert   actually incurring the overhead of a copy.
4882b15cb3dSCy Schubert 
4892b15cb3dSCy Schubert   @param outbuf the output buffer
4902b15cb3dSCy Schubert   @param data the memory to reference
4912b15cb3dSCy Schubert   @param datlen how memory to reference
4922b15cb3dSCy Schubert   @param cleanupfn callback to be invoked when the memory is no longer
4932b15cb3dSCy Schubert 	referenced by this evbuffer.
4942b15cb3dSCy Schubert   @param cleanupfn_arg optional argument to the cleanup callback
4952b15cb3dSCy Schubert   @return 0 if successful, or -1 if an error occurred
4962b15cb3dSCy Schubert  */
4972b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
4982b15cb3dSCy Schubert int evbuffer_add_reference(struct evbuffer *outbuf,
4992b15cb3dSCy Schubert     const void *data, size_t datlen,
5002b15cb3dSCy Schubert     evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
5012b15cb3dSCy Schubert 
5022b15cb3dSCy Schubert /**
5032b15cb3dSCy Schubert   Copy data from a file into the evbuffer for writing to a socket.
5042b15cb3dSCy Schubert 
5052b15cb3dSCy Schubert   This function avoids unnecessary data copies between userland and
5062b15cb3dSCy Schubert   kernel.  If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD
5072b15cb3dSCy Schubert   flag is set, it uses those functions.  Otherwise, it tries to use
5082b15cb3dSCy Schubert   mmap (or CreateFileMapping on Windows).
5092b15cb3dSCy Schubert 
5102b15cb3dSCy Schubert   The function owns the resulting file descriptor and will close it
5112b15cb3dSCy Schubert   when finished transferring data.
5122b15cb3dSCy Schubert 
5132b15cb3dSCy Schubert   The results of using evbuffer_remove() or evbuffer_pullup() on
5142b15cb3dSCy Schubert   evbuffers whose data was added using this function are undefined.
5152b15cb3dSCy Schubert 
5162b15cb3dSCy Schubert   For more fine-grained control, use evbuffer_add_file_segment.
5172b15cb3dSCy Schubert 
5182b15cb3dSCy Schubert   @param outbuf the output buffer
5192b15cb3dSCy Schubert   @param fd the file descriptor
5202b15cb3dSCy Schubert   @param offset the offset from which to read data
5212b15cb3dSCy Schubert   @param length how much data to read, or -1 to read as much as possible.
5222b15cb3dSCy Schubert     (-1 requires that 'fd' support fstat.)
5232b15cb3dSCy Schubert   @return 0 if successful, or -1 if an error occurred
5242b15cb3dSCy Schubert */
5252b15cb3dSCy Schubert 
5262b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
5272b15cb3dSCy Schubert int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
5282b15cb3dSCy Schubert     ev_off_t length);
5292b15cb3dSCy Schubert 
5302b15cb3dSCy Schubert /**
5312b15cb3dSCy Schubert   An evbuffer_file_segment holds a reference to a range of a file --
5322b15cb3dSCy Schubert   possibly the whole file! -- for use in writing from an evbuffer to a
5332b15cb3dSCy Schubert   socket.  It could be implemented with mmap, sendfile, splice, or (if all
5342b15cb3dSCy Schubert   else fails) by just pulling all the data into RAM.  A single
5352b15cb3dSCy Schubert   evbuffer_file_segment can be added more than once, and to more than one
5362b15cb3dSCy Schubert   evbuffer.
5372b15cb3dSCy Schubert  */
5382b15cb3dSCy Schubert struct evbuffer_file_segment;
5392b15cb3dSCy Schubert 
5402b15cb3dSCy Schubert /**
5412b15cb3dSCy Schubert     Flag for creating evbuffer_file_segment: If this flag is set, then when
5422b15cb3dSCy Schubert     the evbuffer_file_segment is freed and no longer in use by any
5432b15cb3dSCy Schubert     evbuffer, the underlying fd is closed.
5442b15cb3dSCy Schubert  */
5452b15cb3dSCy Schubert #define EVBUF_FS_CLOSE_ON_FREE    0x01
5462b15cb3dSCy Schubert /**
5472b15cb3dSCy Schubert    Flag for creating evbuffer_file_segment: Disable memory-map based
5482b15cb3dSCy Schubert    implementations.
5492b15cb3dSCy Schubert  */
5502b15cb3dSCy Schubert #define EVBUF_FS_DISABLE_MMAP     0x02
5512b15cb3dSCy Schubert /**
5522b15cb3dSCy Schubert    Flag for creating evbuffer_file_segment: Disable direct fd-to-fd
5532b15cb3dSCy Schubert    implementations (including sendfile and splice).
5542b15cb3dSCy Schubert 
5552b15cb3dSCy Schubert    You might want to use this option if data needs to be taken from the
5562b15cb3dSCy Schubert    evbuffer by any means other than writing it to the network: the sendfile
5572b15cb3dSCy Schubert    backend is fast, but it only works for sending files directly to the
5582b15cb3dSCy Schubert    network.
5592b15cb3dSCy Schubert  */
5602b15cb3dSCy Schubert #define EVBUF_FS_DISABLE_SENDFILE 0x04
5612b15cb3dSCy Schubert /**
5622b15cb3dSCy Schubert    Flag for creating evbuffer_file_segment: Do not allocate a lock for this
5632b15cb3dSCy Schubert    segment.  If this option is set, then neither the segment nor any
5642b15cb3dSCy Schubert    evbuffer it is added to may ever be accessed from more than one thread
5652b15cb3dSCy Schubert    at a time.
5662b15cb3dSCy Schubert  */
5672b15cb3dSCy Schubert #define EVBUF_FS_DISABLE_LOCKING  0x08
5682b15cb3dSCy Schubert 
5692b15cb3dSCy Schubert /**
5702b15cb3dSCy Schubert    A cleanup function for a evbuffer_file_segment added to an evbuffer
5712b15cb3dSCy Schubert    for reference.
5722b15cb3dSCy Schubert  */
5732b15cb3dSCy Schubert typedef void (*evbuffer_file_segment_cleanup_cb)(
5742b15cb3dSCy Schubert     struct evbuffer_file_segment const* seg, int flags, void* arg);
5752b15cb3dSCy Schubert 
5762b15cb3dSCy Schubert /**
5772b15cb3dSCy Schubert    Create and return a new evbuffer_file_segment for reading data from a
5782b15cb3dSCy Schubert    file and sending it out via an evbuffer.
5792b15cb3dSCy Schubert 
5802b15cb3dSCy Schubert    This function avoids unnecessary data copies between userland and
5812b15cb3dSCy Schubert    kernel.  Where available, it uses sendfile or splice.
5822b15cb3dSCy Schubert 
5832b15cb3dSCy Schubert    The file descriptor must not be closed so long as any evbuffer is using
5842b15cb3dSCy Schubert    this segment.
5852b15cb3dSCy Schubert 
5862b15cb3dSCy Schubert    The results of using evbuffer_remove() or evbuffer_pullup() or any other
5872b15cb3dSCy Schubert    function that reads bytes from an evbuffer on any evbuffer containing
5882b15cb3dSCy Schubert    the newly returned segment are undefined, unless you pass the
5892b15cb3dSCy Schubert    EVBUF_FS_DISABLE_SENDFILE flag to this function.
5902b15cb3dSCy Schubert 
5912b15cb3dSCy Schubert    @param fd an open file to read from.
5922b15cb3dSCy Schubert    @param offset an index within the file at which to start reading
5932b15cb3dSCy Schubert    @param length how much data to read, or -1 to read as much as possible.
5942b15cb3dSCy Schubert       (-1 requires that 'fd' support fstat.)
5952b15cb3dSCy Schubert    @param flags any number of the EVBUF_FS_* flags
5962b15cb3dSCy Schubert    @return a new evbuffer_file_segment, or NULL on failure.
5972b15cb3dSCy Schubert  **/
5982b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
5992b15cb3dSCy Schubert struct evbuffer_file_segment *evbuffer_file_segment_new(
6002b15cb3dSCy Schubert 	int fd, ev_off_t offset, ev_off_t length, unsigned flags);
6012b15cb3dSCy Schubert 
6022b15cb3dSCy Schubert /**
6032b15cb3dSCy Schubert    Free an evbuffer_file_segment
6042b15cb3dSCy Schubert 
6052b15cb3dSCy Schubert    It is safe to call this function even if the segment has been added to
6062b15cb3dSCy Schubert    one or more evbuffers.  The evbuffer_file_segment will not be freed
6072b15cb3dSCy Schubert    until no more references to it exist.
6082b15cb3dSCy Schubert  */
6092b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
6102b15cb3dSCy Schubert void evbuffer_file_segment_free(struct evbuffer_file_segment *seg);
6112b15cb3dSCy Schubert 
6122b15cb3dSCy Schubert /**
6132b15cb3dSCy Schubert    Add cleanup callback and argument for the callback to an
6142b15cb3dSCy Schubert    evbuffer_file_segment.
6152b15cb3dSCy Schubert 
6162b15cb3dSCy Schubert    The cleanup callback will be invoked when no more references to the
6172b15cb3dSCy Schubert    evbuffer_file_segment exist.
6182b15cb3dSCy Schubert  **/
6192b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
6202b15cb3dSCy Schubert void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
6212b15cb3dSCy Schubert 	evbuffer_file_segment_cleanup_cb cb, void* arg);
6222b15cb3dSCy Schubert 
6232b15cb3dSCy Schubert /**
6242b15cb3dSCy Schubert    Insert some or all of an evbuffer_file_segment at the end of an evbuffer
6252b15cb3dSCy Schubert 
6262b15cb3dSCy Schubert    Note that the offset and length parameters of this function have a
6272b15cb3dSCy Schubert    different meaning from those provided to evbuffer_file_segment_new: When
6282b15cb3dSCy Schubert    you create the segment, the offset is the offset _within the file_, and
6292b15cb3dSCy Schubert    the length is the length _of the segment_, whereas when you add a
6302b15cb3dSCy Schubert    segment to an evbuffer, the offset is _within the segment_ and the
6312b15cb3dSCy Schubert    length is the length of the _part of the segment you want to use.
6322b15cb3dSCy Schubert 
6332b15cb3dSCy Schubert    In other words, if you have a 10 KiB file, and you create an
6342b15cb3dSCy Schubert    evbuffer_file_segment for it with offset 20 and length 1000, it will
6352b15cb3dSCy Schubert    refer to bytes 20..1019 inclusive.  If you then pass this segment to
6362b15cb3dSCy Schubert    evbuffer_add_file_segment and specify an offset of 20 and a length of
6372b15cb3dSCy Schubert    50, you will be adding bytes 40..99 inclusive.
6382b15cb3dSCy Schubert 
6392b15cb3dSCy Schubert    @param buf the evbuffer to append to
6402b15cb3dSCy Schubert    @param seg the segment to add
6412b15cb3dSCy Schubert    @param offset the offset within the segment to start from
6422b15cb3dSCy Schubert    @param length the amount of data to add, or -1 to add it all.
6432b15cb3dSCy Schubert    @return 0 on success, -1 on failure.
6442b15cb3dSCy Schubert  */
6452b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
6462b15cb3dSCy Schubert int evbuffer_add_file_segment(struct evbuffer *buf,
6472b15cb3dSCy Schubert     struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length);
6482b15cb3dSCy Schubert 
6492b15cb3dSCy Schubert /**
6502b15cb3dSCy Schubert   Append a formatted string to the end of an evbuffer.
6512b15cb3dSCy Schubert 
6522b15cb3dSCy Schubert   The string is formated as printf.
6532b15cb3dSCy Schubert 
6542b15cb3dSCy Schubert   @param buf the evbuffer that will be appended to
6552b15cb3dSCy Schubert   @param fmt a format string
6562b15cb3dSCy Schubert   @param ... arguments that will be passed to printf(3)
6572b15cb3dSCy Schubert   @return The number of bytes added if successful, or -1 if an error occurred.
6582b15cb3dSCy Schubert 
6592b15cb3dSCy Schubert   @see evutil_printf(), evbuffer_add_vprintf()
6602b15cb3dSCy Schubert  */
6612b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
6622b15cb3dSCy Schubert int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
6632b15cb3dSCy Schubert #ifdef __GNUC__
6642b15cb3dSCy Schubert   __attribute__((format(printf, 2, 3)))
6652b15cb3dSCy Schubert #endif
6662b15cb3dSCy Schubert ;
6672b15cb3dSCy Schubert 
6682b15cb3dSCy Schubert /**
6692b15cb3dSCy Schubert   Append a va_list formatted string to the end of an evbuffer.
6702b15cb3dSCy Schubert 
6712b15cb3dSCy Schubert   @param buf the evbuffer that will be appended to
6722b15cb3dSCy Schubert   @param fmt a format string
6732b15cb3dSCy Schubert   @param ap a varargs va_list argument array that will be passed to vprintf(3)
6742b15cb3dSCy Schubert   @return The number of bytes added if successful, or -1 if an error occurred.
6752b15cb3dSCy Schubert  */
6762b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
6772b15cb3dSCy Schubert int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
6782b15cb3dSCy Schubert #ifdef __GNUC__
6792b15cb3dSCy Schubert 	__attribute__((format(printf, 2, 0)))
6802b15cb3dSCy Schubert #endif
6812b15cb3dSCy Schubert ;
6822b15cb3dSCy Schubert 
6832b15cb3dSCy Schubert 
6842b15cb3dSCy Schubert /**
6852b15cb3dSCy Schubert   Remove a specified number of bytes data from the beginning of an evbuffer.
6862b15cb3dSCy Schubert 
6872b15cb3dSCy Schubert   @param buf the evbuffer to be drained
6882b15cb3dSCy Schubert   @param len the number of bytes to drain from the beginning of the buffer
6892b15cb3dSCy Schubert   @return 0 on success, -1 on failure.
6902b15cb3dSCy Schubert  */
6912b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
6922b15cb3dSCy Schubert int evbuffer_drain(struct evbuffer *buf, size_t len);
6932b15cb3dSCy Schubert 
6942b15cb3dSCy Schubert 
6952b15cb3dSCy Schubert /**
6962b15cb3dSCy Schubert   Write the contents of an evbuffer to a file descriptor.
6972b15cb3dSCy Schubert 
6982b15cb3dSCy Schubert   The evbuffer will be drained after the bytes have been successfully written.
6992b15cb3dSCy Schubert 
7002b15cb3dSCy Schubert   @param buffer the evbuffer to be written and drained
7012b15cb3dSCy Schubert   @param fd the file descriptor to be written to
7022b15cb3dSCy Schubert   @return the number of bytes written, or -1 if an error occurred
7032b15cb3dSCy Schubert   @see evbuffer_read()
7042b15cb3dSCy Schubert  */
7052b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
7062b15cb3dSCy Schubert int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
7072b15cb3dSCy Schubert 
7082b15cb3dSCy Schubert /**
7092b15cb3dSCy Schubert   Write some of the contents of an evbuffer to a file descriptor.
7102b15cb3dSCy Schubert 
7112b15cb3dSCy Schubert   The evbuffer will be drained after the bytes have been successfully written.
7122b15cb3dSCy Schubert 
7132b15cb3dSCy Schubert   @param buffer the evbuffer to be written and drained
7142b15cb3dSCy Schubert   @param fd the file descriptor to be written to
7152b15cb3dSCy Schubert   @param howmuch the largest allowable number of bytes to write, or -1
7162b15cb3dSCy Schubert 	to write as many bytes as we can.
7172b15cb3dSCy Schubert   @return the number of bytes written, or -1 if an error occurred
7182b15cb3dSCy Schubert   @see evbuffer_read()
7192b15cb3dSCy Schubert  */
7202b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
7212b15cb3dSCy Schubert int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
7222b15cb3dSCy Schubert 						  ev_ssize_t howmuch);
7232b15cb3dSCy Schubert 
7242b15cb3dSCy Schubert /**
7252b15cb3dSCy Schubert   Read from a file descriptor and store the result in an evbuffer.
7262b15cb3dSCy Schubert 
7272b15cb3dSCy Schubert   @param buffer the evbuffer to store the result
7282b15cb3dSCy Schubert   @param fd the file descriptor to read from
729*a466cc55SCy Schubert   @param howmuch the number of bytes to be read. If the given number is negative
730*a466cc55SCy Schubert   or out of maximum bytes per one read, as many bytes as we can will be read.
7312b15cb3dSCy Schubert   @return the number of bytes read, or -1 if an error occurred
7322b15cb3dSCy Schubert   @see evbuffer_write()
7332b15cb3dSCy Schubert  */
7342b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
7352b15cb3dSCy Schubert int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
7362b15cb3dSCy Schubert 
7372b15cb3dSCy Schubert /**
7382b15cb3dSCy Schubert    Search for a string within an evbuffer.
7392b15cb3dSCy Schubert 
7402b15cb3dSCy Schubert    @param buffer the evbuffer to be searched
7412b15cb3dSCy Schubert    @param what the string to be searched for
7422b15cb3dSCy Schubert    @param len the length of the search string
7432b15cb3dSCy Schubert    @param start NULL or a pointer to a valid struct evbuffer_ptr.
7442b15cb3dSCy Schubert    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
7452b15cb3dSCy Schubert      first occurrence of the string in the buffer after 'start'.  The 'pos'
7462b15cb3dSCy Schubert      field of the result is -1 if the string was not found.
7472b15cb3dSCy Schubert  */
7482b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
7492b15cb3dSCy Schubert struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
7502b15cb3dSCy Schubert 
7512b15cb3dSCy Schubert /**
7522b15cb3dSCy Schubert    Search for a string within part of an evbuffer.
7532b15cb3dSCy Schubert 
7542b15cb3dSCy Schubert    @param buffer the evbuffer to be searched
7552b15cb3dSCy Schubert    @param what the string to be searched for
7562b15cb3dSCy Schubert    @param len the length of the search string
7572b15cb3dSCy Schubert    @param start NULL or a pointer to a valid struct evbuffer_ptr that
7582b15cb3dSCy Schubert      indicates where we should start searching.
7592b15cb3dSCy Schubert    @param end NULL or a pointer to a valid struct evbuffer_ptr that
7602b15cb3dSCy Schubert      indicates where we should stop searching.
7612b15cb3dSCy Schubert    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
7622b15cb3dSCy Schubert      first occurrence of the string in the buffer after 'start'.  The 'pos'
7632b15cb3dSCy Schubert      field of the result is -1 if the string was not found.
7642b15cb3dSCy Schubert  */
7652b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
7662b15cb3dSCy Schubert 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);
7672b15cb3dSCy Schubert 
7682b15cb3dSCy Schubert /**
7692b15cb3dSCy Schubert    Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()
7702b15cb3dSCy Schubert 
7712b15cb3dSCy Schubert    @see evbuffer_ptr_set() */
7722b15cb3dSCy Schubert enum evbuffer_ptr_how {
7732b15cb3dSCy Schubert 	/** Sets the pointer to the position; can be called on with an
7742b15cb3dSCy Schubert 	    uninitialized evbuffer_ptr. */
7752b15cb3dSCy Schubert 	EVBUFFER_PTR_SET,
7762b15cb3dSCy Schubert 	/** Advances the pointer by adding to the current position. */
7772b15cb3dSCy Schubert 	EVBUFFER_PTR_ADD
7782b15cb3dSCy Schubert };
7792b15cb3dSCy Schubert 
7802b15cb3dSCy Schubert /**
7812b15cb3dSCy Schubert    Sets the search pointer in the buffer to position.
7822b15cb3dSCy Schubert 
7832b15cb3dSCy Schubert    There are two ways to use this function: you can call
7842b15cb3dSCy Schubert       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
7852b15cb3dSCy Schubert    to move 'pos' to a position 'N' bytes after the start of the buffer, or
786a25439b6SCy Schubert       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_ADD)
7872b15cb3dSCy Schubert    to move 'pos' forward by 'N' bytes.
7882b15cb3dSCy Schubert 
7892b15cb3dSCy Schubert    If evbuffer_ptr is not initialized, this function can only be called
7902b15cb3dSCy Schubert    with EVBUFFER_PTR_SET.
7912b15cb3dSCy Schubert 
7922b15cb3dSCy Schubert    An evbuffer_ptr can represent any position from the start of the buffer to
7932b15cb3dSCy Schubert    a position immediately after the end of the buffer.
7942b15cb3dSCy Schubert 
7952b15cb3dSCy Schubert    @param buffer the evbuffer to be search
7962b15cb3dSCy Schubert    @param ptr a pointer to a struct evbuffer_ptr
7972b15cb3dSCy Schubert    @param position the position at which to start the next search
7982b15cb3dSCy Schubert    @param how determines how the pointer should be manipulated.
7992b15cb3dSCy Schubert    @returns 0 on success or -1 otherwise
8002b15cb3dSCy Schubert */
8012b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
8022b15cb3dSCy Schubert int
8032b15cb3dSCy Schubert evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
8042b15cb3dSCy Schubert     size_t position, enum evbuffer_ptr_how how);
8052b15cb3dSCy Schubert 
8062b15cb3dSCy Schubert /**
8072b15cb3dSCy Schubert    Search for an end-of-line string within an evbuffer.
8082b15cb3dSCy Schubert 
8092b15cb3dSCy Schubert    @param buffer the evbuffer to be searched
8102b15cb3dSCy Schubert    @param start NULL or a pointer to a valid struct evbuffer_ptr to start
8112b15cb3dSCy Schubert       searching at.
8122b15cb3dSCy Schubert    @param eol_len_out If non-NULL, the pointed-to value will be set to
8132b15cb3dSCy Schubert       the length of the end-of-line string.
8142b15cb3dSCy Schubert    @param eol_style The kind of EOL to look for; see evbuffer_readln() for
8152b15cb3dSCy Schubert       more information
8162b15cb3dSCy Schubert    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
8172b15cb3dSCy Schubert      first occurrence EOL in the buffer after 'start'.  The 'pos'
8182b15cb3dSCy Schubert      field of the result is -1 if the string was not found.
8192b15cb3dSCy Schubert  */
8202b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
8212b15cb3dSCy Schubert struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
8222b15cb3dSCy Schubert     struct evbuffer_ptr *start, size_t *eol_len_out,
8232b15cb3dSCy Schubert     enum evbuffer_eol_style eol_style);
8242b15cb3dSCy Schubert 
8252b15cb3dSCy Schubert /** Function to peek at data inside an evbuffer without removing it or
8262b15cb3dSCy Schubert     copying it out.
8272b15cb3dSCy Schubert 
8282b15cb3dSCy Schubert     Pointers to the data are returned by filling the 'vec_out' array
8292b15cb3dSCy Schubert     with pointers to one or more extents of data inside the buffer.
8302b15cb3dSCy Schubert 
8312b15cb3dSCy Schubert     The total data in the extents that you get back may be more than
8322b15cb3dSCy Schubert     you requested (if there is more data last extent than you asked
8332b15cb3dSCy Schubert     for), or less (if you do not provide enough evbuffer_iovecs, or if
8342b15cb3dSCy Schubert     the buffer does not have as much data as you asked to see).
8352b15cb3dSCy Schubert 
8362b15cb3dSCy Schubert     @param buffer the evbuffer to peek into,
8372b15cb3dSCy Schubert     @param len the number of bytes to try to peek.  If len is negative, we
8382b15cb3dSCy Schubert        will try to fill as much of vec_out as we can.  If len is negative
8392b15cb3dSCy Schubert        and vec_out is not provided, we return the number of evbuffer_iovecs
8402b15cb3dSCy Schubert        that would be needed to get all the data in the buffer.
8412b15cb3dSCy Schubert     @param start_at an evbuffer_ptr indicating the point at which we
8422b15cb3dSCy Schubert        should start looking for data.  NULL means, "At the start of the
8432b15cb3dSCy Schubert        buffer."
8442b15cb3dSCy Schubert     @param vec_out an array of evbuffer_iovec
8452b15cb3dSCy Schubert     @param n_vec the length of vec_out.  If 0, we only count how many
8462b15cb3dSCy Schubert        extents would be necessary to point to the requested amount of
8472b15cb3dSCy Schubert        data.
8482b15cb3dSCy Schubert     @return The number of extents needed.  This may be less than n_vec
8492b15cb3dSCy Schubert        if we didn't need all the evbuffer_iovecs we were given, or more
8502b15cb3dSCy Schubert        than n_vec if we would need more to return all the data that was
8512b15cb3dSCy Schubert        requested.
8522b15cb3dSCy Schubert  */
8532b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
8542b15cb3dSCy Schubert int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
8552b15cb3dSCy Schubert     struct evbuffer_ptr *start_at,
8562b15cb3dSCy Schubert     struct evbuffer_iovec *vec_out, int n_vec);
8572b15cb3dSCy Schubert 
8582b15cb3dSCy Schubert 
8592b15cb3dSCy Schubert /** Structure passed to an evbuffer_cb_func evbuffer callback
8602b15cb3dSCy Schubert 
8612b15cb3dSCy Schubert     @see evbuffer_cb_func, evbuffer_add_cb()
8622b15cb3dSCy Schubert  */
8632b15cb3dSCy Schubert struct evbuffer_cb_info {
8642b15cb3dSCy Schubert 	/** The number of bytes in this evbuffer when callbacks were last
8652b15cb3dSCy Schubert 	 * invoked. */
8662b15cb3dSCy Schubert 	size_t orig_size;
8672b15cb3dSCy Schubert 	/** The number of bytes added since callbacks were last invoked. */
8682b15cb3dSCy Schubert 	size_t n_added;
8692b15cb3dSCy Schubert 	/** The number of bytes removed since callbacks were last invoked. */
8702b15cb3dSCy Schubert 	size_t n_deleted;
8712b15cb3dSCy Schubert };
8722b15cb3dSCy Schubert 
8732b15cb3dSCy Schubert /** Type definition for a callback that is invoked whenever data is added or
8742b15cb3dSCy Schubert     removed from an evbuffer.
8752b15cb3dSCy Schubert 
8762b15cb3dSCy Schubert     An evbuffer may have one or more callbacks set at a time.  The order
8772b15cb3dSCy Schubert     in which they are executed is undefined.
8782b15cb3dSCy Schubert 
8792b15cb3dSCy Schubert     A callback function may add more callbacks, or remove itself from the
8802b15cb3dSCy Schubert     list of callbacks, or add or remove data from the buffer.  It may not
8812b15cb3dSCy Schubert     remove another callback from the list.
8822b15cb3dSCy Schubert 
8832b15cb3dSCy Schubert     If a callback adds or removes data from the buffer or from another
8842b15cb3dSCy Schubert     buffer, this can cause a recursive invocation of your callback or
8852b15cb3dSCy Schubert     other callbacks.  If you ask for an infinite loop, you might just get
8862b15cb3dSCy Schubert     one: watch out!
8872b15cb3dSCy Schubert 
8882b15cb3dSCy Schubert     @param buffer the buffer whose size has changed
8892b15cb3dSCy Schubert     @param info a structure describing how the buffer changed.
8902b15cb3dSCy Schubert     @param arg a pointer to user data
8912b15cb3dSCy Schubert */
8922b15cb3dSCy Schubert typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
8932b15cb3dSCy Schubert 
8942b15cb3dSCy Schubert struct evbuffer_cb_entry;
8952b15cb3dSCy Schubert /** Add a new callback to an evbuffer.
8962b15cb3dSCy Schubert 
8972b15cb3dSCy Schubert   Subsequent calls to evbuffer_add_cb() add new callbacks.  To remove this
8982b15cb3dSCy Schubert   callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
8992b15cb3dSCy Schubert 
9002b15cb3dSCy Schubert   @param buffer the evbuffer to be monitored
9012b15cb3dSCy Schubert   @param cb the callback function to invoke when the evbuffer is modified,
9022b15cb3dSCy Schubert 	or NULL to remove all callbacks.
9032b15cb3dSCy Schubert   @param cbarg an argument to be provided to the callback function
9042b15cb3dSCy Schubert   @return a handle to the callback on success, or NULL on failure.
9052b15cb3dSCy Schubert  */
9062b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9072b15cb3dSCy Schubert struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
9082b15cb3dSCy Schubert 
9092b15cb3dSCy Schubert /** Remove a callback from an evbuffer, given a handle returned from
9102b15cb3dSCy Schubert     evbuffer_add_cb.
9112b15cb3dSCy Schubert 
9122b15cb3dSCy Schubert     Calling this function invalidates the handle.
9132b15cb3dSCy Schubert 
9142b15cb3dSCy Schubert     @return 0 if a callback was removed, or -1 if no matching callback was
9152b15cb3dSCy Schubert     found.
9162b15cb3dSCy Schubert  */
9172b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9182b15cb3dSCy Schubert int evbuffer_remove_cb_entry(struct evbuffer *buffer,
9192b15cb3dSCy Schubert 			     struct evbuffer_cb_entry *ent);
9202b15cb3dSCy Schubert 
9212b15cb3dSCy Schubert /** Remove a callback from an evbuffer, given the function and argument
9222b15cb3dSCy Schubert     used to add it.
9232b15cb3dSCy Schubert 
9242b15cb3dSCy Schubert     @return 0 if a callback was removed, or -1 if no matching callback was
9252b15cb3dSCy Schubert     found.
9262b15cb3dSCy Schubert  */
9272b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9282b15cb3dSCy Schubert int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
9292b15cb3dSCy Schubert 
9302b15cb3dSCy Schubert /** If this flag is not set, then a callback is temporarily disabled, and
9312b15cb3dSCy Schubert  * should not be invoked.
9322b15cb3dSCy Schubert  *
9332b15cb3dSCy Schubert  * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()
9342b15cb3dSCy Schubert  */
9352b15cb3dSCy Schubert #define EVBUFFER_CB_ENABLED 1
9362b15cb3dSCy Schubert 
9372b15cb3dSCy Schubert /** Change the flags that are set for a callback on a buffer by adding more.
9382b15cb3dSCy Schubert 
9392b15cb3dSCy Schubert     @param buffer the evbuffer that the callback is watching.
9402b15cb3dSCy Schubert     @param cb the callback whose status we want to change.
9412b15cb3dSCy Schubert     @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
9422b15cb3dSCy Schubert     @return 0 on success, -1 on failure.
9432b15cb3dSCy Schubert  */
9442b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9452b15cb3dSCy Schubert int evbuffer_cb_set_flags(struct evbuffer *buffer,
9462b15cb3dSCy Schubert 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
9472b15cb3dSCy Schubert 
9482b15cb3dSCy Schubert /** Change the flags that are set for a callback on a buffer by removing some
9492b15cb3dSCy Schubert 
9502b15cb3dSCy Schubert     @param buffer the evbuffer that the callback is watching.
9512b15cb3dSCy Schubert     @param cb the callback whose status we want to change.
9522b15cb3dSCy Schubert     @param flags EVBUFFER_CB_ENABLED to disable the callback.
9532b15cb3dSCy Schubert     @return 0 on success, -1 on failure.
9542b15cb3dSCy Schubert  */
9552b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9562b15cb3dSCy Schubert int evbuffer_cb_clear_flags(struct evbuffer *buffer,
9572b15cb3dSCy Schubert 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
9582b15cb3dSCy Schubert 
9592b15cb3dSCy Schubert #if 0
9602b15cb3dSCy Schubert /** Postpone calling a given callback until unsuspend is called later.
9612b15cb3dSCy Schubert 
9622b15cb3dSCy Schubert     This is different from disabling the callback, since the callback will get
9632b15cb3dSCy Schubert 	invoked later if the buffer size changes between now and when we unsuspend
9642b15cb3dSCy Schubert 	it.
9652b15cb3dSCy Schubert 
9662b15cb3dSCy Schubert 	@param the buffer that the callback is watching.
9672b15cb3dSCy Schubert 	@param cb the callback we want to suspend.
9682b15cb3dSCy Schubert  */
9692b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9702b15cb3dSCy Schubert void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
9712b15cb3dSCy Schubert /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
9722b15cb3dSCy Schubert 
9732b15cb3dSCy Schubert 	If data was added to or removed from the buffer while the callback was
9742b15cb3dSCy Schubert 	suspended, the callback will get called once now.
9752b15cb3dSCy Schubert 
9762b15cb3dSCy Schubert 	@param the buffer that the callback is watching.
9772b15cb3dSCy Schubert 	@param cb the callback we want to stop suspending.
9782b15cb3dSCy Schubert  */
9792b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9802b15cb3dSCy Schubert void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
9812b15cb3dSCy Schubert #endif
9822b15cb3dSCy Schubert 
9832b15cb3dSCy Schubert /**
9842b15cb3dSCy Schubert   Makes the data at the beginning of an evbuffer contiguous.
9852b15cb3dSCy Schubert 
9862b15cb3dSCy Schubert   @param buf the evbuffer to make contiguous
9872b15cb3dSCy Schubert   @param size the number of bytes to make contiguous, or -1 to make the
9882b15cb3dSCy Schubert 	entire buffer contiguous.
9892b15cb3dSCy Schubert   @return a pointer to the contiguous memory array, or NULL if param size
9902b15cb3dSCy Schubert 	requested more data than is present in the buffer.
9912b15cb3dSCy Schubert */
9922b15cb3dSCy Schubert 
9932b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
9942b15cb3dSCy Schubert unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
9952b15cb3dSCy Schubert 
9962b15cb3dSCy Schubert /**
9972b15cb3dSCy Schubert   Prepends data to the beginning of the evbuffer
9982b15cb3dSCy Schubert 
9992b15cb3dSCy Schubert   @param buf the evbuffer to which to prepend data
10002b15cb3dSCy Schubert   @param data a pointer to the memory to prepend
10012b15cb3dSCy Schubert   @param size the number of bytes to prepend
10022b15cb3dSCy Schubert   @return 0 if successful, or -1 otherwise
10032b15cb3dSCy Schubert */
10042b15cb3dSCy Schubert 
10052b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
10062b15cb3dSCy Schubert int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
10072b15cb3dSCy Schubert 
10082b15cb3dSCy Schubert /**
10092b15cb3dSCy Schubert   Prepends all data from the src evbuffer to the beginning of the dst
10102b15cb3dSCy Schubert   evbuffer.
10112b15cb3dSCy Schubert 
10122b15cb3dSCy Schubert   @param dst the evbuffer to which to prepend data
10132b15cb3dSCy Schubert   @param src the evbuffer to prepend; it will be emptied as a result
10142b15cb3dSCy Schubert   @return 0 if successful, or -1 otherwise
10152b15cb3dSCy Schubert */
10162b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
10172b15cb3dSCy Schubert int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
10182b15cb3dSCy Schubert 
10192b15cb3dSCy Schubert /**
10202b15cb3dSCy Schubert    Prevent calls that modify an evbuffer from succeeding. A buffer may
10212b15cb3dSCy Schubert    frozen at the front, at the back, or at both the front and the back.
10222b15cb3dSCy Schubert 
10232b15cb3dSCy Schubert    If the front of a buffer is frozen, operations that drain data from
10242b15cb3dSCy Schubert    the front of the buffer, or that prepend data to the buffer, will
10252b15cb3dSCy Schubert    fail until it is unfrozen.   If the back a buffer is frozen, operations
10262b15cb3dSCy Schubert    that append data from the buffer will fail until it is unfrozen.
10272b15cb3dSCy Schubert 
10282b15cb3dSCy Schubert    @param buf The buffer to freeze
10292b15cb3dSCy Schubert    @param at_front If true, we freeze the front of the buffer.  If false,
10302b15cb3dSCy Schubert       we freeze the back.
10312b15cb3dSCy Schubert    @return 0 on success, -1 on failure.
10322b15cb3dSCy Schubert */
10332b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
10342b15cb3dSCy Schubert int evbuffer_freeze(struct evbuffer *buf, int at_front);
10352b15cb3dSCy Schubert /**
10362b15cb3dSCy Schubert    Re-enable calls that modify an evbuffer.
10372b15cb3dSCy Schubert 
10382b15cb3dSCy Schubert    @param buf The buffer to un-freeze
10392b15cb3dSCy Schubert    @param at_front If true, we unfreeze the front of the buffer.  If false,
10402b15cb3dSCy Schubert       we unfreeze the back.
10412b15cb3dSCy Schubert    @return 0 on success, -1 on failure.
10422b15cb3dSCy Schubert  */
10432b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
10442b15cb3dSCy Schubert int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
10452b15cb3dSCy Schubert 
10462b15cb3dSCy Schubert struct event_base;
10472b15cb3dSCy Schubert /**
10482b15cb3dSCy Schubert    Force all the callbacks on an evbuffer to be run, not immediately after
10492b15cb3dSCy Schubert    the evbuffer is altered, but instead from inside the event loop.
10502b15cb3dSCy Schubert 
10512b15cb3dSCy Schubert    This can be used to serialize all the callbacks to a single thread
10522b15cb3dSCy Schubert    of execution.
10532b15cb3dSCy Schubert  */
10542b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
10552b15cb3dSCy Schubert int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
10562b15cb3dSCy Schubert 
10572b15cb3dSCy Schubert /**
10582b15cb3dSCy Schubert   Append data from 1 or more iovec's to an evbuffer
10592b15cb3dSCy Schubert 
10602b15cb3dSCy Schubert   Calculates the number of bytes needed for an iovec structure and guarantees
10612b15cb3dSCy Schubert   all data will fit into a single chain. Can be used in lieu of functionality
10622b15cb3dSCy Schubert   which calls evbuffer_add() constantly before being used to increase
10632b15cb3dSCy Schubert   performance.
10642b15cb3dSCy Schubert 
10652b15cb3dSCy Schubert   @param buffer the destination buffer
10662b15cb3dSCy Schubert   @param vec the source iovec
10672b15cb3dSCy Schubert   @param n_vec the number of iovec structures.
10682b15cb3dSCy Schubert   @return the number of bytes successfully written to the output buffer.
10692b15cb3dSCy Schubert */
10702b15cb3dSCy Schubert EVENT2_EXPORT_SYMBOL
10712b15cb3dSCy Schubert size_t evbuffer_add_iovec(struct evbuffer * buffer, struct evbuffer_iovec * vec, int n_vec);
10722b15cb3dSCy Schubert 
10732b15cb3dSCy Schubert #ifdef __cplusplus
10742b15cb3dSCy Schubert }
10752b15cb3dSCy Schubert #endif
10762b15cb3dSCy Schubert 
10772b15cb3dSCy Schubert #endif /* EVENT2_BUFFER_H_INCLUDED_ */
1078