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