Home | History | Annotate | Line # | Download | only in event2
      1  1.6  christos /*	$NetBSD: buffer.h,v 1.7 2024/08/18 20:47:22 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
      5  1.1  christos  *
      6  1.1  christos  * Redistribution and use in source and binary forms, with or without
      7  1.1  christos  * modification, are permitted provided that the following conditions
      8  1.1  christos  * are met:
      9  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  christos  *    documentation and/or other materials provided with the distribution.
     14  1.1  christos  * 3. The name of the author may not be used to endorse or promote products
     15  1.1  christos  *    derived from this software without specific prior written permission.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos #ifndef EVENT2_BUFFER_H_INCLUDED_
     29  1.1  christos #define EVENT2_BUFFER_H_INCLUDED_
     30  1.1  christos 
     31  1.1  christos /** @file event2/buffer.h
     32  1.1  christos 
     33  1.1  christos   Functions for buffering data for network sending or receiving.
     34  1.1  christos 
     35  1.1  christos   An evbuffer can be used for preparing data before sending it to
     36  1.1  christos   the network or conversely for reading data from the network.
     37  1.1  christos   Evbuffers try to avoid memory copies as much as possible.  As a
     38  1.1  christos   result, evbuffers can be used to pass data around without actually
     39  1.1  christos   incurring the overhead of copying the data.
     40  1.1  christos 
     41  1.1  christos   A new evbuffer can be allocated with evbuffer_new(), and can be
     42  1.1  christos   freed with evbuffer_free().  Most users will be using evbuffers via
     43  1.1  christos   the bufferevent interface.  To access a bufferevent's evbuffers, use
     44  1.1  christos   bufferevent_get_input() and bufferevent_get_output().
     45  1.1  christos 
     46  1.1  christos   There are several guidelines for using evbuffers.
     47  1.1  christos 
     48  1.1  christos   - if you already know how much data you are going to add as a result
     49  1.1  christos     of calling evbuffer_add() multiple times, it makes sense to use
     50  1.1  christos     evbuffer_expand() first to make sure that enough memory is allocated
     51  1.1  christos     before hand.
     52  1.1  christos 
     53  1.1  christos   - evbuffer_add_buffer() adds the contents of one buffer to the other
     54  1.1  christos     without incurring any unnecessary memory copies.
     55  1.1  christos 
     56  1.1  christos   - evbuffer_add() and evbuffer_add_buffer() do not mix very well:
     57  1.1  christos     if you use them, you will wind up with fragmented memory in your
     58  1.1  christos 	buffer.
     59  1.1  christos 
     60  1.1  christos   - For high-performance code, you may want to avoid copying data into and out
     61  1.1  christos     of buffers.  You can skip the copy step by using
     62  1.1  christos     evbuffer_reserve_space()/evbuffer_commit_space() when writing into a
     63  1.1  christos     buffer, and evbuffer_peek() when reading.
     64  1.1  christos 
     65  1.1  christos   In Libevent 2.0 and later, evbuffers are represented using a linked
     66  1.1  christos   list of memory chunks, with pointers to the first and last chunk in
     67  1.1  christos   the chain.
     68  1.1  christos 
     69  1.1  christos   As the contents of an evbuffer can be stored in multiple different
     70  1.1  christos   memory blocks, it cannot be accessed directly.  Instead, evbuffer_pullup()
     71  1.1  christos   can be used to force a specified number of bytes to be contiguous. This
     72  1.1  christos   will cause memory reallocation and memory copies if the data is split
     73  1.1  christos   across multiple blocks.  It is more efficient, however, to use
     74  1.1  christos   evbuffer_peek() if you don't require that the memory to be contiguous.
     75  1.1  christos  */
     76  1.1  christos 
     77  1.2  christos #include <event2/visibility.h>
     78  1.2  christos 
     79  1.1  christos #ifdef __cplusplus
     80  1.1  christos extern "C" {
     81  1.1  christos #endif
     82  1.1  christos 
     83  1.1  christos #include <event2/event-config.h>
     84  1.1  christos #include <stdarg.h>
     85  1.1  christos #ifdef EVENT__HAVE_SYS_TYPES_H
     86  1.1  christos #include <sys/types.h>
     87  1.1  christos #endif
     88  1.1  christos #ifdef EVENT__HAVE_SYS_UIO_H
     89  1.1  christos #include <sys/uio.h>
     90  1.1  christos #endif
     91  1.1  christos #include <event2/util.h>
     92  1.1  christos 
     93  1.1  christos /**
     94  1.1  christos    An evbuffer is an opaque data type for efficiently buffering data to be
     95  1.1  christos    sent or received on the network.
     96  1.1  christos 
     97  1.1  christos    @see event2/event.h for more information
     98  1.1  christos */
     99  1.1  christos struct evbuffer
    100  1.1  christos #ifdef EVENT_IN_DOXYGEN_
    101  1.1  christos {}
    102  1.1  christos #endif
    103  1.1  christos ;
    104  1.1  christos 
    105  1.1  christos /**
    106  1.1  christos     Pointer to a position within an evbuffer.
    107  1.1  christos 
    108  1.1  christos     Used when repeatedly searching through a buffer.  Calling any function
    109  1.1  christos     that modifies or re-packs the buffer contents may invalidate all
    110  1.3  christos     evbuffer_ptrs for that buffer.  Do not modify or contruct these values
    111  1.3  christos     except with evbuffer_ptr_set.
    112  1.1  christos 
    113  1.1  christos     An evbuffer_ptr can represent any position from the start of a buffer up
    114  1.1  christos     to a position immediately after the end of a buffer.
    115  1.1  christos 
    116  1.1  christos     @see evbuffer_ptr_set()
    117  1.1  christos  */
    118  1.1  christos struct evbuffer_ptr {
    119  1.1  christos 	ev_ssize_t pos;
    120  1.1  christos 
    121  1.1  christos 	/* Do not alter or rely on the values of fields: they are for internal
    122  1.1  christos 	 * use */
    123  1.1  christos 	struct {
    124  1.1  christos 		void *chain;
    125  1.1  christos 		size_t pos_in_chain;
    126  1.1  christos 	} internal_;
    127  1.1  christos };
    128  1.1  christos 
    129  1.1  christos /** Describes a single extent of memory inside an evbuffer.  Used for
    130  1.1  christos     direct-access functions.
    131  1.1  christos 
    132  1.1  christos     @see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
    133  1.1  christos  */
    134  1.1  christos #ifdef EVENT__HAVE_SYS_UIO_H
    135  1.1  christos #define evbuffer_iovec iovec
    136  1.1  christos /* Internal use -- defined only if we are using the native struct iovec */
    137  1.1  christos #define EVBUFFER_IOVEC_IS_NATIVE_
    138  1.1  christos #else
    139  1.1  christos struct evbuffer_iovec {
    140  1.1  christos 	/** The start of the extent of memory. */
    141  1.1  christos 	void *iov_base;
    142  1.1  christos 	/** The length of the extent of memory. */
    143  1.1  christos 	size_t iov_len;
    144  1.1  christos };
    145  1.1  christos #endif
    146  1.1  christos 
    147  1.1  christos /**
    148  1.1  christos   Allocate storage for a new evbuffer.
    149  1.1  christos 
    150  1.1  christos   @return a pointer to a newly allocated evbuffer struct, or NULL if an error
    151  1.1  christos 	occurred
    152  1.1  christos  */
    153  1.2  christos EVENT2_EXPORT_SYMBOL
    154  1.1  christos struct evbuffer *evbuffer_new(void);
    155  1.1  christos /**
    156  1.1  christos   Deallocate storage for an evbuffer.
    157  1.1  christos 
    158  1.1  christos   @param buf pointer to the evbuffer to be freed
    159  1.1  christos  */
    160  1.2  christos EVENT2_EXPORT_SYMBOL
    161  1.1  christos void evbuffer_free(struct evbuffer *buf);
    162  1.1  christos 
    163  1.1  christos /**
    164  1.1  christos    Enable locking on an evbuffer so that it can safely be used by multiple
    165  1.1  christos    threads at the same time.
    166  1.1  christos 
    167  1.1  christos    NOTE: when locking is enabled, the lock will be held when callbacks are
    168  1.1  christos    invoked.  This could result in deadlock if you aren't careful.  Plan
    169  1.1  christos    accordingly!
    170  1.1  christos 
    171  1.1  christos    @param buf An evbuffer to make lockable.
    172  1.1  christos    @param lock A lock object, or NULL if we should allocate our own.
    173  1.1  christos    @return 0 on success, -1 on failure.
    174  1.1  christos  */
    175  1.2  christos EVENT2_EXPORT_SYMBOL
    176  1.1  christos int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
    177  1.1  christos 
    178  1.1  christos /**
    179  1.1  christos    Acquire the lock on an evbuffer.  Has no effect if locking was not enabled
    180  1.1  christos    with evbuffer_enable_locking.
    181  1.1  christos */
    182  1.2  christos EVENT2_EXPORT_SYMBOL
    183  1.1  christos void evbuffer_lock(struct evbuffer *buf);
    184  1.1  christos 
    185  1.1  christos /**
    186  1.1  christos    Release the lock on an evbuffer.  Has no effect if locking was not enabled
    187  1.1  christos    with evbuffer_enable_locking.
    188  1.1  christos */
    189  1.2  christos EVENT2_EXPORT_SYMBOL
    190  1.1  christos void evbuffer_unlock(struct evbuffer *buf);
    191  1.1  christos 
    192  1.1  christos 
    193  1.1  christos /** If this flag is set, then we will not use evbuffer_peek(),
    194  1.1  christos  * evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes
    195  1.1  christos  * from this buffer: we'll only take bytes out of this buffer by
    196  1.1  christos  * writing them to the network (as with evbuffer_write_atmost), by
    197  1.1  christos  * removing them without observing them (as with evbuffer_drain),
    198  1.1  christos  * or by copying them all out at once (as with evbuffer_add_buffer).
    199  1.1  christos  *
    200  1.1  christos  * Using this option allows the implementation to use sendfile-based
    201  1.1  christos  * operations for evbuffer_add_file(); see that function for more
    202  1.1  christos  * information.
    203  1.1  christos  *
    204  1.1  christos  * This flag is on by default for bufferevents that can take advantage
    205  1.1  christos  * of it; you should never actually need to set it on a bufferevent's
    206  1.1  christos  * output buffer.
    207  1.1  christos  */
    208  1.1  christos #define EVBUFFER_FLAG_DRAINS_TO_FD 1
    209  1.1  christos 
    210  1.1  christos /** Change the flags that are set for an evbuffer by adding more.
    211  1.1  christos  *
    212  1.1  christos  * @param buffer the evbuffer that the callback is watching.
    213  1.1  christos  * @param cb the callback whose status we want to change.
    214  1.1  christos  * @param flags One or more EVBUFFER_FLAG_* options
    215  1.1  christos  * @return 0 on success, -1 on failure.
    216  1.1  christos  */
    217  1.2  christos EVENT2_EXPORT_SYMBOL
    218  1.1  christos int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
    219  1.1  christos /** Change the flags that are set for an evbuffer by removing some.
    220  1.1  christos  *
    221  1.1  christos  * @param buffer the evbuffer that the callback is watching.
    222  1.1  christos  * @param cb the callback whose status we want to change.
    223  1.1  christos  * @param flags One or more EVBUFFER_FLAG_* options
    224  1.1  christos  * @return 0 on success, -1 on failure.
    225  1.1  christos  */
    226  1.2  christos EVENT2_EXPORT_SYMBOL
    227  1.1  christos int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
    228  1.1  christos 
    229  1.1  christos /**
    230  1.1  christos   Returns the total number of bytes stored in the evbuffer
    231  1.1  christos 
    232  1.1  christos   @param buf pointer to the evbuffer
    233  1.1  christos   @return the number of bytes stored in the evbuffer
    234  1.1  christos */
    235  1.2  christos EVENT2_EXPORT_SYMBOL
    236  1.1  christos size_t evbuffer_get_length(const struct evbuffer *buf);
    237  1.1  christos 
    238  1.1  christos /**
    239  1.1  christos    Returns the number of contiguous available bytes in the first buffer chain.
    240  1.1  christos 
    241  1.1  christos    This is useful when processing data that might be split into multiple
    242  1.1  christos    chains, or that might all be in the first chain.  Calls to
    243  1.1  christos    evbuffer_pullup() that cause reallocation and copying of data can thus be
    244  1.1  christos    avoided.
    245  1.1  christos 
    246  1.1  christos    @param buf pointer to the evbuffer
    247  1.1  christos    @return 0 if no data is available, otherwise the number of available bytes
    248  1.1  christos      in the first buffer chain.
    249  1.1  christos */
    250  1.2  christos EVENT2_EXPORT_SYMBOL
    251  1.1  christos size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
    252  1.1  christos 
    253  1.1  christos /**
    254  1.1  christos   Expands the available space in an evbuffer.
    255  1.1  christos 
    256  1.1  christos   Expands the available space in the evbuffer to at least datlen, so that
    257  1.1  christos   appending datlen additional bytes will not require any new allocations.
    258  1.1  christos 
    259  1.1  christos   @param buf the evbuffer to be expanded
    260  1.1  christos   @param datlen the new minimum length requirement
    261  1.1  christos   @return 0 if successful, or -1 if an error occurred
    262  1.1  christos */
    263  1.2  christos EVENT2_EXPORT_SYMBOL
    264  1.1  christos int evbuffer_expand(struct evbuffer *buf, size_t datlen);
    265  1.1  christos 
    266  1.1  christos /**
    267  1.1  christos    Reserves space in the last chain or chains of an evbuffer.
    268  1.1  christos 
    269  1.1  christos    Makes space available in the last chain or chains of an evbuffer that can
    270  1.1  christos    be arbitrarily written to by a user.  The space does not become
    271  1.1  christos    available for reading until it has been committed with
    272  1.1  christos    evbuffer_commit_space().
    273  1.1  christos 
    274  1.1  christos    The space is made available as one or more extents, represented by
    275  1.1  christos    an initial pointer and a length.  You can force the memory to be
    276  1.1  christos    available as only one extent.  Allowing more extents, however, makes the
    277  1.1  christos    function more efficient.
    278  1.1  christos 
    279  1.1  christos    Multiple subsequent calls to this function will make the same space
    280  1.1  christos    available until evbuffer_commit_space() has been called.
    281  1.1  christos 
    282  1.1  christos    It is an error to do anything that moves around the buffer's internal
    283  1.1  christos    memory structures before committing the space.
    284  1.1  christos 
    285  1.1  christos    NOTE: The code currently does not ever use more than two extents.
    286  1.1  christos    This may change in future versions.
    287  1.1  christos 
    288  1.1  christos    @param buf the evbuffer in which to reserve space.
    289  1.1  christos    @param size how much space to make available, at minimum.  The
    290  1.1  christos       total length of the extents may be greater than the requested
    291  1.1  christos       length.
    292  1.1  christos    @param vec an array of one or more evbuffer_iovec structures to
    293  1.1  christos       hold pointers to the reserved extents of memory.
    294  1.1  christos    @param n_vec The length of the vec array.  Must be at least 1;
    295  1.1  christos        2 is more efficient.
    296  1.1  christos    @return the number of provided extents, or -1 on error.
    297  1.1  christos    @see evbuffer_commit_space()
    298  1.1  christos */
    299  1.2  christos EVENT2_EXPORT_SYMBOL
    300  1.1  christos int
    301  1.1  christos evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
    302  1.1  christos     struct evbuffer_iovec *vec, int n_vec);
    303  1.1  christos 
    304  1.1  christos /**
    305  1.1  christos    Commits previously reserved space.
    306  1.1  christos 
    307  1.1  christos    Commits some of the space previously reserved with
    308  1.1  christos    evbuffer_reserve_space().  It then becomes available for reading.
    309  1.1  christos 
    310  1.1  christos    This function may return an error if the pointer in the extents do
    311  1.1  christos    not match those returned from evbuffer_reserve_space, or if data
    312  1.1  christos    has been added to the buffer since the space was reserved.
    313  1.1  christos 
    314  1.1  christos    If you want to commit less data than you got reserved space for,
    315  1.1  christos    modify the iov_len pointer of the appropriate extent to a smaller
    316  1.1  christos    value.  Note that you may have received more space than you
    317  1.1  christos    requested if it was available!
    318  1.1  christos 
    319  1.1  christos    @param buf the evbuffer in which to reserve space.
    320  1.1  christos    @param vec one or two extents returned by evbuffer_reserve_space.
    321  1.1  christos    @param n_vecs the number of extents.
    322  1.1  christos    @return 0 on success, -1 on error
    323  1.1  christos    @see evbuffer_reserve_space()
    324  1.1  christos */
    325  1.2  christos EVENT2_EXPORT_SYMBOL
    326  1.1  christos int evbuffer_commit_space(struct evbuffer *buf,
    327  1.1  christos     struct evbuffer_iovec *vec, int n_vecs);
    328  1.1  christos 
    329  1.1  christos /**
    330  1.1  christos   Append data to the end of an evbuffer.
    331  1.1  christos 
    332  1.1  christos   @param buf the evbuffer to be appended to
    333  1.1  christos   @param data pointer to the beginning of the data buffer
    334  1.1  christos   @param datlen the number of bytes to be copied from the data buffer
    335  1.1  christos   @return 0 on success, -1 on failure.
    336  1.1  christos  */
    337  1.2  christos EVENT2_EXPORT_SYMBOL
    338  1.1  christos int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
    339  1.1  christos 
    340  1.1  christos 
    341  1.1  christos /**
    342  1.1  christos   Read data from an evbuffer and drain the bytes read.
    343  1.1  christos 
    344  1.1  christos   If more bytes are requested than are available in the evbuffer, we
    345  1.1  christos   only extract as many bytes as were available.
    346  1.1  christos 
    347  1.1  christos   @param buf the evbuffer to be read from
    348  1.1  christos   @param data the destination buffer to store the result
    349  1.1  christos   @param datlen the maximum size of the destination buffer
    350  1.1  christos   @return the number of bytes read, or -1 if we can't drain the buffer.
    351  1.1  christos  */
    352  1.2  christos EVENT2_EXPORT_SYMBOL
    353  1.1  christos int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
    354  1.1  christos 
    355  1.1  christos /**
    356  1.1  christos   Read data from an evbuffer, and leave the buffer unchanged.
    357  1.1  christos 
    358  1.1  christos   If more bytes are requested than are available in the evbuffer, we
    359  1.1  christos   only extract as many bytes as were available.
    360  1.1  christos 
    361  1.1  christos   @param buf the evbuffer to be read from
    362  1.1  christos   @param data_out the destination buffer to store the result
    363  1.1  christos   @param datlen the maximum size of the destination buffer
    364  1.1  christos   @return the number of bytes read, or -1 if we can't drain the buffer.
    365  1.1  christos  */
    366  1.2  christos EVENT2_EXPORT_SYMBOL
    367  1.1  christos ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
    368  1.1  christos 
    369  1.1  christos /**
    370  1.1  christos   Read data from the middle of an evbuffer, and leave the buffer unchanged.
    371  1.1  christos 
    372  1.1  christos   If more bytes are requested than are available in the evbuffer, we
    373  1.1  christos   only extract as many bytes as were available.
    374  1.1  christos 
    375  1.1  christos   @param buf the evbuffer to be read from
    376  1.1  christos   @param pos the position to start reading from
    377  1.1  christos   @param data_out the destination buffer to store the result
    378  1.1  christos   @param datlen the maximum size of the destination buffer
    379  1.1  christos   @return the number of bytes read, or -1 if we can't drain the buffer.
    380  1.1  christos  */
    381  1.2  christos EVENT2_EXPORT_SYMBOL
    382  1.1  christos ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
    383  1.1  christos 
    384  1.1  christos /**
    385  1.1  christos   Read data from an evbuffer into another evbuffer, draining
    386  1.1  christos   the bytes from the source buffer.  This function avoids copy
    387  1.1  christos   operations to the extent possible.
    388  1.1  christos 
    389  1.1  christos   If more bytes are requested than are available in src, the src
    390  1.1  christos   buffer is drained completely.
    391  1.1  christos 
    392  1.1  christos   @param src the evbuffer to be read from
    393  1.1  christos   @param dst the destination evbuffer to store the result into
    394  1.1  christos   @param datlen the maximum numbers of bytes to transfer
    395  1.1  christos   @return the number of bytes read
    396  1.1  christos  */
    397  1.2  christos EVENT2_EXPORT_SYMBOL
    398  1.1  christos int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
    399  1.1  christos     size_t datlen);
    400  1.1  christos 
    401  1.1  christos /** Used to tell evbuffer_readln what kind of line-ending to look for.
    402  1.1  christos  */
    403  1.1  christos enum evbuffer_eol_style {
    404  1.1  christos 	/** Any sequence of CR and LF characters is acceptable as an
    405  1.1  christos 	 * EOL.
    406  1.1  christos 	 *
    407  1.1  christos 	 * Note that this style can produce ambiguous results: the
    408  1.1  christos 	 * sequence "CRLF" will be treated as a single EOL if it is
    409  1.1  christos 	 * all in the buffer at once, but if you first read a CR from
    410  1.1  christos 	 * the network and later read an LF from the network, it will
    411  1.1  christos 	 * be treated as two EOLs.
    412  1.1  christos 	 */
    413  1.1  christos 	EVBUFFER_EOL_ANY,
    414  1.1  christos 	/** An EOL is an LF, optionally preceded by a CR.  This style is
    415  1.1  christos 	 * most useful for implementing text-based internet protocols. */
    416  1.1  christos 	EVBUFFER_EOL_CRLF,
    417  1.1  christos 	/** An EOL is a CR followed by an LF. */
    418  1.1  christos 	EVBUFFER_EOL_CRLF_STRICT,
    419  1.1  christos 	/** An EOL is a LF. */
    420  1.1  christos 	EVBUFFER_EOL_LF,
    421  1.1  christos 	/** An EOL is a NUL character (that is, a single byte with value 0) */
    422  1.1  christos 	EVBUFFER_EOL_NUL
    423  1.1  christos };
    424  1.1  christos 
    425  1.1  christos /**
    426  1.1  christos  * Read a single line from an evbuffer.
    427  1.1  christos  *
    428  1.1  christos  * Reads a line terminated by an EOL as determined by the evbuffer_eol_style
    429  1.1  christos  * argument.  Returns a newly allocated nul-terminated string; the caller must
    430  1.1  christos  * free the returned value.  The EOL is not included in the returned string.
    431  1.1  christos  *
    432  1.1  christos  * @param buffer the evbuffer to read from
    433  1.1  christos  * @param n_read_out if non-NULL, points to a size_t that is set to the
    434  1.1  christos  *       number of characters in the returned string.  This is useful for
    435  1.1  christos  *       strings that can contain NUL characters.
    436  1.1  christos  * @param eol_style the style of line-ending to use.
    437  1.1  christos  * @return pointer to a single line, or NULL if an error occurred
    438  1.1  christos  */
    439  1.2  christos EVENT2_EXPORT_SYMBOL
    440  1.1  christos char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
    441  1.1  christos     enum evbuffer_eol_style eol_style);
    442  1.1  christos 
    443  1.1  christos /**
    444  1.1  christos   Move all data from one evbuffer into another evbuffer.
    445  1.1  christos 
    446  1.1  christos   This is a destructive add.  The data from one buffer moves into
    447  1.1  christos   the other buffer.  However, no unnecessary memory copies occur.
    448  1.1  christos 
    449  1.1  christos   @param outbuf the output buffer
    450  1.1  christos   @param inbuf the input buffer
    451  1.1  christos   @return 0 if successful, or -1 if an error occurred
    452  1.1  christos 
    453  1.1  christos   @see evbuffer_remove_buffer()
    454  1.1  christos  */
    455  1.2  christos EVENT2_EXPORT_SYMBOL
    456  1.1  christos int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
    457  1.1  christos 
    458  1.1  christos /**
    459  1.1  christos   Copy data from one evbuffer into another evbuffer.
    460  1.2  christos 
    461  1.1  christos   This is a non-destructive add.  The data from one buffer is copied
    462  1.1  christos   into the other buffer.  However, no unnecessary memory copies occur.
    463  1.2  christos 
    464  1.1  christos   Note that buffers already containing buffer references can't be added
    465  1.1  christos   to other buffers.
    466  1.2  christos 
    467  1.1  christos   @param outbuf the output buffer
    468  1.1  christos   @param inbuf the input buffer
    469  1.1  christos   @return 0 if successful, or -1 if an error occurred
    470  1.1  christos  */
    471  1.2  christos EVENT2_EXPORT_SYMBOL
    472  1.1  christos int evbuffer_add_buffer_reference(struct evbuffer *outbuf,
    473  1.1  christos     struct evbuffer *inbuf);
    474  1.1  christos 
    475  1.1  christos /**
    476  1.1  christos    A cleanup function for a piece of memory added to an evbuffer by
    477  1.1  christos    reference.
    478  1.1  christos 
    479  1.1  christos    @see evbuffer_add_reference()
    480  1.1  christos  */
    481  1.1  christos typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
    482  1.1  christos     size_t datalen, void *extra);
    483  1.1  christos 
    484  1.1  christos /**
    485  1.1  christos   Reference memory into an evbuffer without copying.
    486  1.1  christos 
    487  1.1  christos   The memory needs to remain valid until all the added data has been
    488  1.1  christos   read.  This function keeps just a reference to the memory without
    489  1.1  christos   actually incurring the overhead of a copy.
    490  1.1  christos 
    491  1.1  christos   @param outbuf the output buffer
    492  1.1  christos   @param data the memory to reference
    493  1.1  christos   @param datlen how memory to reference
    494  1.1  christos   @param cleanupfn callback to be invoked when the memory is no longer
    495  1.1  christos 	referenced by this evbuffer.
    496  1.1  christos   @param cleanupfn_arg optional argument to the cleanup callback
    497  1.1  christos   @return 0 if successful, or -1 if an error occurred
    498  1.1  christos  */
    499  1.2  christos EVENT2_EXPORT_SYMBOL
    500  1.1  christos int evbuffer_add_reference(struct evbuffer *outbuf,
    501  1.1  christos     const void *data, size_t datlen,
    502  1.1  christos     evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
    503  1.1  christos 
    504  1.1  christos /**
    505  1.1  christos   Copy data from a file into the evbuffer for writing to a socket.
    506  1.1  christos 
    507  1.1  christos   This function avoids unnecessary data copies between userland and
    508  1.1  christos   kernel.  If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD
    509  1.1  christos   flag is set, it uses those functions.  Otherwise, it tries to use
    510  1.1  christos   mmap (or CreateFileMapping on Windows).
    511  1.1  christos 
    512  1.1  christos   The function owns the resulting file descriptor and will close it
    513  1.1  christos   when finished transferring data.
    514  1.1  christos 
    515  1.1  christos   The results of using evbuffer_remove() or evbuffer_pullup() on
    516  1.1  christos   evbuffers whose data was added using this function are undefined.
    517  1.1  christos 
    518  1.1  christos   For more fine-grained control, use evbuffer_add_file_segment.
    519  1.1  christos 
    520  1.1  christos   @param outbuf the output buffer
    521  1.1  christos   @param fd the file descriptor
    522  1.1  christos   @param offset the offset from which to read data
    523  1.1  christos   @param length how much data to read, or -1 to read as much as possible.
    524  1.1  christos     (-1 requires that 'fd' support fstat.)
    525  1.1  christos   @return 0 if successful, or -1 if an error occurred
    526  1.1  christos */
    527  1.1  christos 
    528  1.2  christos EVENT2_EXPORT_SYMBOL
    529  1.1  christos int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
    530  1.1  christos     ev_off_t length);
    531  1.1  christos 
    532  1.1  christos /**
    533  1.1  christos   An evbuffer_file_segment holds a reference to a range of a file --
    534  1.1  christos   possibly the whole file! -- for use in writing from an evbuffer to a
    535  1.1  christos   socket.  It could be implemented with mmap, sendfile, splice, or (if all
    536  1.1  christos   else fails) by just pulling all the data into RAM.  A single
    537  1.1  christos   evbuffer_file_segment can be added more than once, and to more than one
    538  1.1  christos   evbuffer.
    539  1.1  christos  */
    540  1.1  christos struct evbuffer_file_segment;
    541  1.1  christos 
    542  1.1  christos /**
    543  1.1  christos     Flag for creating evbuffer_file_segment: If this flag is set, then when
    544  1.1  christos     the evbuffer_file_segment is freed and no longer in use by any
    545  1.1  christos     evbuffer, the underlying fd is closed.
    546  1.1  christos  */
    547  1.1  christos #define EVBUF_FS_CLOSE_ON_FREE    0x01
    548  1.1  christos /**
    549  1.1  christos    Flag for creating evbuffer_file_segment: Disable memory-map based
    550  1.1  christos    implementations.
    551  1.1  christos  */
    552  1.1  christos #define EVBUF_FS_DISABLE_MMAP     0x02
    553  1.1  christos /**
    554  1.1  christos    Flag for creating evbuffer_file_segment: Disable direct fd-to-fd
    555  1.1  christos    implementations (including sendfile and splice).
    556  1.1  christos 
    557  1.1  christos    You might want to use this option if data needs to be taken from the
    558  1.1  christos    evbuffer by any means other than writing it to the network: the sendfile
    559  1.1  christos    backend is fast, but it only works for sending files directly to the
    560  1.1  christos    network.
    561  1.1  christos  */
    562  1.1  christos #define EVBUF_FS_DISABLE_SENDFILE 0x04
    563  1.1  christos /**
    564  1.1  christos    Flag for creating evbuffer_file_segment: Do not allocate a lock for this
    565  1.1  christos    segment.  If this option is set, then neither the segment nor any
    566  1.1  christos    evbuffer it is added to may ever be accessed from more than one thread
    567  1.1  christos    at a time.
    568  1.1  christos  */
    569  1.1  christos #define EVBUF_FS_DISABLE_LOCKING  0x08
    570  1.1  christos 
    571  1.1  christos /**
    572  1.1  christos    A cleanup function for a evbuffer_file_segment added to an evbuffer
    573  1.1  christos    for reference.
    574  1.1  christos  */
    575  1.1  christos typedef void (*evbuffer_file_segment_cleanup_cb)(
    576  1.1  christos     struct evbuffer_file_segment const* seg, int flags, void* arg);
    577  1.1  christos 
    578  1.1  christos /**
    579  1.1  christos    Create and return a new evbuffer_file_segment for reading data from a
    580  1.1  christos    file and sending it out via an evbuffer.
    581  1.1  christos 
    582  1.1  christos    This function avoids unnecessary data copies between userland and
    583  1.1  christos    kernel.  Where available, it uses sendfile or splice.
    584  1.1  christos 
    585  1.1  christos    The file descriptor must not be closed so long as any evbuffer is using
    586  1.1  christos    this segment.
    587  1.1  christos 
    588  1.1  christos    The results of using evbuffer_remove() or evbuffer_pullup() or any other
    589  1.1  christos    function that reads bytes from an evbuffer on any evbuffer containing
    590  1.1  christos    the newly returned segment are undefined, unless you pass the
    591  1.1  christos    EVBUF_FS_DISABLE_SENDFILE flag to this function.
    592  1.1  christos 
    593  1.1  christos    @param fd an open file to read from.
    594  1.1  christos    @param offset an index within the file at which to start reading
    595  1.1  christos    @param length how much data to read, or -1 to read as much as possible.
    596  1.1  christos       (-1 requires that 'fd' support fstat.)
    597  1.1  christos    @param flags any number of the EVBUF_FS_* flags
    598  1.1  christos    @return a new evbuffer_file_segment, or NULL on failure.
    599  1.1  christos  **/
    600  1.2  christos EVENT2_EXPORT_SYMBOL
    601  1.1  christos struct evbuffer_file_segment *evbuffer_file_segment_new(
    602  1.1  christos 	int fd, ev_off_t offset, ev_off_t length, unsigned flags);
    603  1.1  christos 
    604  1.1  christos /**
    605  1.1  christos    Free an evbuffer_file_segment
    606  1.1  christos 
    607  1.1  christos    It is safe to call this function even if the segment has been added to
    608  1.1  christos    one or more evbuffers.  The evbuffer_file_segment will not be freed
    609  1.1  christos    until no more references to it exist.
    610  1.1  christos  */
    611  1.2  christos EVENT2_EXPORT_SYMBOL
    612  1.1  christos void evbuffer_file_segment_free(struct evbuffer_file_segment *seg);
    613  1.1  christos 
    614  1.1  christos /**
    615  1.1  christos    Add cleanup callback and argument for the callback to an
    616  1.1  christos    evbuffer_file_segment.
    617  1.1  christos 
    618  1.1  christos    The cleanup callback will be invoked when no more references to the
    619  1.1  christos    evbuffer_file_segment exist.
    620  1.1  christos  **/
    621  1.2  christos EVENT2_EXPORT_SYMBOL
    622  1.1  christos void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
    623  1.1  christos 	evbuffer_file_segment_cleanup_cb cb, void* arg);
    624  1.1  christos 
    625  1.1  christos /**
    626  1.1  christos    Insert some or all of an evbuffer_file_segment at the end of an evbuffer
    627  1.1  christos 
    628  1.1  christos    Note that the offset and length parameters of this function have a
    629  1.1  christos    different meaning from those provided to evbuffer_file_segment_new: When
    630  1.1  christos    you create the segment, the offset is the offset _within the file_, and
    631  1.1  christos    the length is the length _of the segment_, whereas when you add a
    632  1.1  christos    segment to an evbuffer, the offset is _within the segment_ and the
    633  1.1  christos    length is the length of the _part of the segment you want to use.
    634  1.1  christos 
    635  1.1  christos    In other words, if you have a 10 KiB file, and you create an
    636  1.1  christos    evbuffer_file_segment for it with offset 20 and length 1000, it will
    637  1.1  christos    refer to bytes 20..1019 inclusive.  If you then pass this segment to
    638  1.1  christos    evbuffer_add_file_segment and specify an offset of 20 and a length of
    639  1.1  christos    50, you will be adding bytes 40..99 inclusive.
    640  1.1  christos 
    641  1.1  christos    @param buf the evbuffer to append to
    642  1.1  christos    @param seg the segment to add
    643  1.1  christos    @param offset the offset within the segment to start from
    644  1.1  christos    @param length the amount of data to add, or -1 to add it all.
    645  1.1  christos    @return 0 on success, -1 on failure.
    646  1.1  christos  */
    647  1.2  christos EVENT2_EXPORT_SYMBOL
    648  1.1  christos int evbuffer_add_file_segment(struct evbuffer *buf,
    649  1.1  christos     struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length);
    650  1.1  christos 
    651  1.1  christos /**
    652  1.1  christos   Append a formatted string to the end of an evbuffer.
    653  1.1  christos 
    654  1.1  christos   The string is formated as printf.
    655  1.1  christos 
    656  1.1  christos   @param buf the evbuffer that will be appended to
    657  1.1  christos   @param fmt a format string
    658  1.1  christos   @param ... arguments that will be passed to printf(3)
    659  1.1  christos   @return The number of bytes added if successful, or -1 if an error occurred.
    660  1.1  christos 
    661  1.1  christos   @see evutil_printf(), evbuffer_add_vprintf()
    662  1.1  christos  */
    663  1.2  christos EVENT2_EXPORT_SYMBOL
    664  1.1  christos int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
    665  1.1  christos #ifdef __GNUC__
    666  1.1  christos   __attribute__((format(printf, 2, 3)))
    667  1.1  christos #endif
    668  1.1  christos ;
    669  1.1  christos 
    670  1.1  christos /**
    671  1.1  christos   Append a va_list formatted string to the end of an evbuffer.
    672  1.1  christos 
    673  1.1  christos   @param buf the evbuffer that will be appended to
    674  1.1  christos   @param fmt a format string
    675  1.1  christos   @param ap a varargs va_list argument array that will be passed to vprintf(3)
    676  1.1  christos   @return The number of bytes added if successful, or -1 if an error occurred.
    677  1.1  christos  */
    678  1.2  christos EVENT2_EXPORT_SYMBOL
    679  1.1  christos int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
    680  1.1  christos #ifdef __GNUC__
    681  1.1  christos 	__attribute__((format(printf, 2, 0)))
    682  1.1  christos #endif
    683  1.1  christos ;
    684  1.1  christos 
    685  1.1  christos 
    686  1.1  christos /**
    687  1.1  christos   Remove a specified number of bytes data from the beginning of an evbuffer.
    688  1.1  christos 
    689  1.1  christos   @param buf the evbuffer to be drained
    690  1.1  christos   @param len the number of bytes to drain from the beginning of the buffer
    691  1.1  christos   @return 0 on success, -1 on failure.
    692  1.1  christos  */
    693  1.2  christos EVENT2_EXPORT_SYMBOL
    694  1.1  christos int evbuffer_drain(struct evbuffer *buf, size_t len);
    695  1.1  christos 
    696  1.1  christos 
    697  1.1  christos /**
    698  1.1  christos   Write the contents of an evbuffer to a file descriptor.
    699  1.1  christos 
    700  1.1  christos   The evbuffer will be drained after the bytes have been successfully written.
    701  1.1  christos 
    702  1.1  christos   @param buffer the evbuffer to be written and drained
    703  1.1  christos   @param fd the file descriptor to be written to
    704  1.1  christos   @return the number of bytes written, or -1 if an error occurred
    705  1.1  christos   @see evbuffer_read()
    706  1.1  christos  */
    707  1.2  christos EVENT2_EXPORT_SYMBOL
    708  1.1  christos int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
    709  1.1  christos 
    710  1.1  christos /**
    711  1.1  christos   Write some of the contents of an evbuffer to a file descriptor.
    712  1.1  christos 
    713  1.1  christos   The evbuffer will be drained after the bytes have been successfully written.
    714  1.1  christos 
    715  1.1  christos   @param buffer the evbuffer to be written and drained
    716  1.1  christos   @param fd the file descriptor to be written to
    717  1.1  christos   @param howmuch the largest allowable number of bytes to write, or -1
    718  1.1  christos 	to write as many bytes as we can.
    719  1.1  christos   @return the number of bytes written, or -1 if an error occurred
    720  1.1  christos   @see evbuffer_read()
    721  1.1  christos  */
    722  1.2  christos EVENT2_EXPORT_SYMBOL
    723  1.1  christos int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
    724  1.1  christos 						  ev_ssize_t howmuch);
    725  1.1  christos 
    726  1.1  christos /**
    727  1.1  christos   Read from a file descriptor and store the result in an evbuffer.
    728  1.1  christos 
    729  1.1  christos   @param buffer the evbuffer to store the result
    730  1.1  christos   @param fd the file descriptor to read from
    731  1.7  christos   @param howmuch the number of bytes to be read. If the given number is negative
    732  1.7  christos   or out of maximum bytes per one read, as many bytes as we can will be read.
    733  1.1  christos   @return the number of bytes read, or -1 if an error occurred
    734  1.1  christos   @see evbuffer_write()
    735  1.1  christos  */
    736  1.2  christos EVENT2_EXPORT_SYMBOL
    737  1.1  christos int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
    738  1.1  christos 
    739  1.1  christos /**
    740  1.1  christos    Search for a string within an evbuffer.
    741  1.1  christos 
    742  1.1  christos    @param buffer the evbuffer to be searched
    743  1.1  christos    @param what the string to be searched for
    744  1.1  christos    @param len the length of the search string
    745  1.1  christos    @param start NULL or a pointer to a valid struct evbuffer_ptr.
    746  1.1  christos    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
    747  1.1  christos      first occurrence of the string in the buffer after 'start'.  The 'pos'
    748  1.1  christos      field of the result is -1 if the string was not found.
    749  1.1  christos  */
    750  1.2  christos EVENT2_EXPORT_SYMBOL
    751  1.1  christos struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
    752  1.1  christos 
    753  1.1  christos /**
    754  1.1  christos    Search for a string within part of an evbuffer.
    755  1.1  christos 
    756  1.1  christos    @param buffer the evbuffer to be searched
    757  1.1  christos    @param what the string to be searched for
    758  1.1  christos    @param len the length of the search string
    759  1.1  christos    @param start NULL or a pointer to a valid struct evbuffer_ptr that
    760  1.1  christos      indicates where we should start searching.
    761  1.1  christos    @param end NULL or a pointer to a valid struct evbuffer_ptr that
    762  1.1  christos      indicates where we should stop searching.
    763  1.1  christos    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
    764  1.1  christos      first occurrence of the string in the buffer after 'start'.  The 'pos'
    765  1.1  christos      field of the result is -1 if the string was not found.
    766  1.1  christos  */
    767  1.2  christos EVENT2_EXPORT_SYMBOL
    768  1.1  christos 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);
    769  1.1  christos 
    770  1.1  christos /**
    771  1.1  christos    Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()
    772  1.1  christos 
    773  1.1  christos    @see evbuffer_ptr_set() */
    774  1.1  christos enum evbuffer_ptr_how {
    775  1.1  christos 	/** Sets the pointer to the position; can be called on with an
    776  1.1  christos 	    uninitialized evbuffer_ptr. */
    777  1.1  christos 	EVBUFFER_PTR_SET,
    778  1.1  christos 	/** Advances the pointer by adding to the current position. */
    779  1.1  christos 	EVBUFFER_PTR_ADD
    780  1.1  christos };
    781  1.1  christos 
    782  1.1  christos /**
    783  1.1  christos    Sets the search pointer in the buffer to position.
    784  1.1  christos 
    785  1.1  christos    There are two ways to use this function: you can call
    786  1.1  christos       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
    787  1.1  christos    to move 'pos' to a position 'N' bytes after the start of the buffer, or
    788  1.3  christos       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_ADD)
    789  1.1  christos    to move 'pos' forward by 'N' bytes.
    790  1.1  christos 
    791  1.1  christos    If evbuffer_ptr is not initialized, this function can only be called
    792  1.1  christos    with EVBUFFER_PTR_SET.
    793  1.1  christos 
    794  1.1  christos    An evbuffer_ptr can represent any position from the start of the buffer to
    795  1.1  christos    a position immediately after the end of the buffer.
    796  1.1  christos 
    797  1.1  christos    @param buffer the evbuffer to be search
    798  1.1  christos    @param ptr a pointer to a struct evbuffer_ptr
    799  1.1  christos    @param position the position at which to start the next search
    800  1.1  christos    @param how determines how the pointer should be manipulated.
    801  1.1  christos    @returns 0 on success or -1 otherwise
    802  1.1  christos */
    803  1.2  christos EVENT2_EXPORT_SYMBOL
    804  1.1  christos int
    805  1.1  christos evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
    806  1.1  christos     size_t position, enum evbuffer_ptr_how how);
    807  1.1  christos 
    808  1.1  christos /**
    809  1.1  christos    Search for an end-of-line string within an evbuffer.
    810  1.1  christos 
    811  1.1  christos    @param buffer the evbuffer to be searched
    812  1.1  christos    @param start NULL or a pointer to a valid struct evbuffer_ptr to start
    813  1.1  christos       searching at.
    814  1.1  christos    @param eol_len_out If non-NULL, the pointed-to value will be set to
    815  1.1  christos       the length of the end-of-line string.
    816  1.1  christos    @param eol_style The kind of EOL to look for; see evbuffer_readln() for
    817  1.1  christos       more information
    818  1.1  christos    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
    819  1.1  christos      first occurrence EOL in the buffer after 'start'.  The 'pos'
    820  1.1  christos      field of the result is -1 if the string was not found.
    821  1.1  christos  */
    822  1.2  christos EVENT2_EXPORT_SYMBOL
    823  1.1  christos struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
    824  1.1  christos     struct evbuffer_ptr *start, size_t *eol_len_out,
    825  1.1  christos     enum evbuffer_eol_style eol_style);
    826  1.1  christos 
    827  1.1  christos /** Function to peek at data inside an evbuffer without removing it or
    828  1.1  christos     copying it out.
    829  1.1  christos 
    830  1.1  christos     Pointers to the data are returned by filling the 'vec_out' array
    831  1.1  christos     with pointers to one or more extents of data inside the buffer.
    832  1.1  christos 
    833  1.1  christos     The total data in the extents that you get back may be more than
    834  1.1  christos     you requested (if there is more data last extent than you asked
    835  1.1  christos     for), or less (if you do not provide enough evbuffer_iovecs, or if
    836  1.1  christos     the buffer does not have as much data as you asked to see).
    837  1.1  christos 
    838  1.1  christos     @param buffer the evbuffer to peek into,
    839  1.1  christos     @param len the number of bytes to try to peek.  If len is negative, we
    840  1.1  christos        will try to fill as much of vec_out as we can.  If len is negative
    841  1.1  christos        and vec_out is not provided, we return the number of evbuffer_iovecs
    842  1.1  christos        that would be needed to get all the data in the buffer.
    843  1.1  christos     @param start_at an evbuffer_ptr indicating the point at which we
    844  1.1  christos        should start looking for data.  NULL means, "At the start of the
    845  1.1  christos        buffer."
    846  1.1  christos     @param vec_out an array of evbuffer_iovec
    847  1.1  christos     @param n_vec the length of vec_out.  If 0, we only count how many
    848  1.1  christos        extents would be necessary to point to the requested amount of
    849  1.1  christos        data.
    850  1.1  christos     @return The number of extents needed.  This may be less than n_vec
    851  1.1  christos        if we didn't need all the evbuffer_iovecs we were given, or more
    852  1.1  christos        than n_vec if we would need more to return all the data that was
    853  1.1  christos        requested.
    854  1.1  christos  */
    855  1.2  christos EVENT2_EXPORT_SYMBOL
    856  1.1  christos int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
    857  1.1  christos     struct evbuffer_ptr *start_at,
    858  1.1  christos     struct evbuffer_iovec *vec_out, int n_vec);
    859  1.1  christos 
    860  1.1  christos 
    861  1.1  christos /** Structure passed to an evbuffer_cb_func evbuffer callback
    862  1.1  christos 
    863  1.1  christos     @see evbuffer_cb_func, evbuffer_add_cb()
    864  1.1  christos  */
    865  1.1  christos struct evbuffer_cb_info {
    866  1.1  christos 	/** The number of bytes in this evbuffer when callbacks were last
    867  1.1  christos 	 * invoked. */
    868  1.1  christos 	size_t orig_size;
    869  1.1  christos 	/** The number of bytes added since callbacks were last invoked. */
    870  1.1  christos 	size_t n_added;
    871  1.1  christos 	/** The number of bytes removed since callbacks were last invoked. */
    872  1.1  christos 	size_t n_deleted;
    873  1.1  christos };
    874  1.1  christos 
    875  1.1  christos /** Type definition for a callback that is invoked whenever data is added or
    876  1.1  christos     removed from an evbuffer.
    877  1.1  christos 
    878  1.1  christos     An evbuffer may have one or more callbacks set at a time.  The order
    879  1.1  christos     in which they are executed is undefined.
    880  1.1  christos 
    881  1.1  christos     A callback function may add more callbacks, or remove itself from the
    882  1.1  christos     list of callbacks, or add or remove data from the buffer.  It may not
    883  1.1  christos     remove another callback from the list.
    884  1.1  christos 
    885  1.1  christos     If a callback adds or removes data from the buffer or from another
    886  1.1  christos     buffer, this can cause a recursive invocation of your callback or
    887  1.1  christos     other callbacks.  If you ask for an infinite loop, you might just get
    888  1.1  christos     one: watch out!
    889  1.1  christos 
    890  1.1  christos     @param buffer the buffer whose size has changed
    891  1.1  christos     @param info a structure describing how the buffer changed.
    892  1.1  christos     @param arg a pointer to user data
    893  1.1  christos */
    894  1.1  christos typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
    895  1.1  christos 
    896  1.1  christos struct evbuffer_cb_entry;
    897  1.1  christos /** Add a new callback to an evbuffer.
    898  1.1  christos 
    899  1.1  christos   Subsequent calls to evbuffer_add_cb() add new callbacks.  To remove this
    900  1.1  christos   callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
    901  1.1  christos 
    902  1.1  christos   @param buffer the evbuffer to be monitored
    903  1.1  christos   @param cb the callback function to invoke when the evbuffer is modified,
    904  1.1  christos 	or NULL to remove all callbacks.
    905  1.1  christos   @param cbarg an argument to be provided to the callback function
    906  1.1  christos   @return a handle to the callback on success, or NULL on failure.
    907  1.1  christos  */
    908  1.2  christos EVENT2_EXPORT_SYMBOL
    909  1.1  christos struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
    910  1.1  christos 
    911  1.1  christos /** Remove a callback from an evbuffer, given a handle returned from
    912  1.1  christos     evbuffer_add_cb.
    913  1.1  christos 
    914  1.1  christos     Calling this function invalidates the handle.
    915  1.1  christos 
    916  1.1  christos     @return 0 if a callback was removed, or -1 if no matching callback was
    917  1.1  christos     found.
    918  1.1  christos  */
    919  1.2  christos EVENT2_EXPORT_SYMBOL
    920  1.1  christos int evbuffer_remove_cb_entry(struct evbuffer *buffer,
    921  1.1  christos 			     struct evbuffer_cb_entry *ent);
    922  1.1  christos 
    923  1.1  christos /** Remove a callback from an evbuffer, given the function and argument
    924  1.1  christos     used to add it.
    925  1.1  christos 
    926  1.1  christos     @return 0 if a callback was removed, or -1 if no matching callback was
    927  1.1  christos     found.
    928  1.1  christos  */
    929  1.2  christos EVENT2_EXPORT_SYMBOL
    930  1.1  christos int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
    931  1.1  christos 
    932  1.1  christos /** If this flag is not set, then a callback is temporarily disabled, and
    933  1.1  christos  * should not be invoked.
    934  1.1  christos  *
    935  1.1  christos  * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()
    936  1.1  christos  */
    937  1.1  christos #define EVBUFFER_CB_ENABLED 1
    938  1.1  christos 
    939  1.1  christos /** Change the flags that are set for a callback on a buffer by adding more.
    940  1.1  christos 
    941  1.1  christos     @param buffer the evbuffer that the callback is watching.
    942  1.1  christos     @param cb the callback whose status we want to change.
    943  1.1  christos     @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
    944  1.1  christos     @return 0 on success, -1 on failure.
    945  1.1  christos  */
    946  1.2  christos EVENT2_EXPORT_SYMBOL
    947  1.1  christos int evbuffer_cb_set_flags(struct evbuffer *buffer,
    948  1.1  christos 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
    949  1.1  christos 
    950  1.1  christos /** Change the flags that are set for a callback on a buffer by removing some
    951  1.1  christos 
    952  1.1  christos     @param buffer the evbuffer that the callback is watching.
    953  1.1  christos     @param cb the callback whose status we want to change.
    954  1.1  christos     @param flags EVBUFFER_CB_ENABLED to disable the callback.
    955  1.1  christos     @return 0 on success, -1 on failure.
    956  1.1  christos  */
    957  1.2  christos EVENT2_EXPORT_SYMBOL
    958  1.1  christos int evbuffer_cb_clear_flags(struct evbuffer *buffer,
    959  1.1  christos 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
    960  1.1  christos 
    961  1.1  christos #if 0
    962  1.1  christos /** Postpone calling a given callback until unsuspend is called later.
    963  1.1  christos 
    964  1.1  christos     This is different from disabling the callback, since the callback will get
    965  1.1  christos 	invoked later if the buffer size changes between now and when we unsuspend
    966  1.1  christos 	it.
    967  1.1  christos 
    968  1.1  christos 	@param the buffer that the callback is watching.
    969  1.1  christos 	@param cb the callback we want to suspend.
    970  1.1  christos  */
    971  1.2  christos EVENT2_EXPORT_SYMBOL
    972  1.1  christos void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
    973  1.1  christos /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
    974  1.1  christos 
    975  1.1  christos 	If data was added to or removed from the buffer while the callback was
    976  1.1  christos 	suspended, the callback will get called once now.
    977  1.1  christos 
    978  1.1  christos 	@param the buffer that the callback is watching.
    979  1.1  christos 	@param cb the callback we want to stop suspending.
    980  1.1  christos  */
    981  1.2  christos EVENT2_EXPORT_SYMBOL
    982  1.1  christos void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
    983  1.1  christos #endif
    984  1.1  christos 
    985  1.1  christos /**
    986  1.2  christos   Makes the data at the beginning of an evbuffer contiguous.
    987  1.1  christos 
    988  1.1  christos   @param buf the evbuffer to make contiguous
    989  1.1  christos   @param size the number of bytes to make contiguous, or -1 to make the
    990  1.1  christos 	entire buffer contiguous.
    991  1.2  christos   @return a pointer to the contiguous memory array, or NULL if param size
    992  1.2  christos 	requested more data than is present in the buffer.
    993  1.1  christos */
    994  1.1  christos 
    995  1.2  christos EVENT2_EXPORT_SYMBOL
    996  1.1  christos unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
    997  1.1  christos 
    998  1.1  christos /**
    999  1.1  christos   Prepends data to the beginning of the evbuffer
   1000  1.1  christos 
   1001  1.1  christos   @param buf the evbuffer to which to prepend data
   1002  1.1  christos   @param data a pointer to the memory to prepend
   1003  1.1  christos   @param size the number of bytes to prepend
   1004  1.1  christos   @return 0 if successful, or -1 otherwise
   1005  1.1  christos */
   1006  1.1  christos 
   1007  1.2  christos EVENT2_EXPORT_SYMBOL
   1008  1.1  christos int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
   1009  1.1  christos 
   1010  1.1  christos /**
   1011  1.1  christos   Prepends all data from the src evbuffer to the beginning of the dst
   1012  1.1  christos   evbuffer.
   1013  1.1  christos 
   1014  1.1  christos   @param dst the evbuffer to which to prepend data
   1015  1.1  christos   @param src the evbuffer to prepend; it will be emptied as a result
   1016  1.1  christos   @return 0 if successful, or -1 otherwise
   1017  1.1  christos */
   1018  1.2  christos EVENT2_EXPORT_SYMBOL
   1019  1.1  christos int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
   1020  1.1  christos 
   1021  1.1  christos /**
   1022  1.1  christos    Prevent calls that modify an evbuffer from succeeding. A buffer may
   1023  1.1  christos    frozen at the front, at the back, or at both the front and the back.
   1024  1.1  christos 
   1025  1.1  christos    If the front of a buffer is frozen, operations that drain data from
   1026  1.1  christos    the front of the buffer, or that prepend data to the buffer, will
   1027  1.1  christos    fail until it is unfrozen.   If the back a buffer is frozen, operations
   1028  1.1  christos    that append data from the buffer will fail until it is unfrozen.
   1029  1.1  christos 
   1030  1.1  christos    @param buf The buffer to freeze
   1031  1.1  christos    @param at_front If true, we freeze the front of the buffer.  If false,
   1032  1.1  christos       we freeze the back.
   1033  1.1  christos    @return 0 on success, -1 on failure.
   1034  1.1  christos */
   1035  1.2  christos EVENT2_EXPORT_SYMBOL
   1036  1.1  christos int evbuffer_freeze(struct evbuffer *buf, int at_front);
   1037  1.1  christos /**
   1038  1.1  christos    Re-enable calls that modify an evbuffer.
   1039  1.1  christos 
   1040  1.1  christos    @param buf The buffer to un-freeze
   1041  1.1  christos    @param at_front If true, we unfreeze the front of the buffer.  If false,
   1042  1.1  christos       we unfreeze the back.
   1043  1.1  christos    @return 0 on success, -1 on failure.
   1044  1.1  christos  */
   1045  1.2  christos EVENT2_EXPORT_SYMBOL
   1046  1.1  christos int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
   1047  1.1  christos 
   1048  1.1  christos struct event_base;
   1049  1.1  christos /**
   1050  1.1  christos    Force all the callbacks on an evbuffer to be run, not immediately after
   1051  1.1  christos    the evbuffer is altered, but instead from inside the event loop.
   1052  1.1  christos 
   1053  1.1  christos    This can be used to serialize all the callbacks to a single thread
   1054  1.1  christos    of execution.
   1055  1.1  christos  */
   1056  1.2  christos EVENT2_EXPORT_SYMBOL
   1057  1.1  christos int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
   1058  1.1  christos 
   1059  1.1  christos /**
   1060  1.1  christos   Append data from 1 or more iovec's to an evbuffer
   1061  1.1  christos 
   1062  1.1  christos   Calculates the number of bytes needed for an iovec structure and guarantees
   1063  1.1  christos   all data will fit into a single chain. Can be used in lieu of functionality
   1064  1.1  christos   which calls evbuffer_add() constantly before being used to increase
   1065  1.1  christos   performance.
   1066  1.1  christos 
   1067  1.1  christos   @param buffer the destination buffer
   1068  1.1  christos   @param vec the source iovec
   1069  1.1  christos   @param n_vec the number of iovec structures.
   1070  1.1  christos   @return the number of bytes successfully written to the output buffer.
   1071  1.1  christos */
   1072  1.2  christos EVENT2_EXPORT_SYMBOL
   1073  1.1  christos size_t evbuffer_add_iovec(struct evbuffer * buffer, struct evbuffer_iovec * vec, int n_vec);
   1074  1.1  christos 
   1075  1.1  christos #ifdef __cplusplus
   1076  1.1  christos }
   1077  1.1  christos #endif
   1078  1.1  christos 
   1079  1.1  christos #endif /* EVENT2_BUFFER_H_INCLUDED_ */
   1080