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