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