Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:11

0001 /*
0002  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
0003  *
0004  * Redistribution and use in source and binary forms, with or without
0005  * modification, are permitted provided that the following conditions
0006  * are met:
0007  * 1. Redistributions of source code must retain the above copyright
0008  *    notice, this list of conditions and the following disclaimer.
0009  * 2. Redistributions in binary form must reproduce the above copyright
0010  *    notice, this list of conditions and the following disclaimer in the
0011  *    documentation and/or other materials provided with the distribution.
0012  * 3. The name of the author may not be used to endorse or promote products
0013  *    derived from this software without specific prior written permission.
0014  *
0015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0025  */
0026 #ifndef EVENT2_BUFFER_H_INCLUDED_
0027 #define EVENT2_BUFFER_H_INCLUDED_
0028 
0029 /** @file event2/buffer.h
0030 
0031   Functions for buffering data for network sending or receiving.
0032 
0033   An evbuffer can be used for preparing data before sending it to
0034   the network or conversely for reading data from the network.
0035   Evbuffers try to avoid memory copies as much as possible.  As a
0036   result, evbuffers can be used to pass data around without actually
0037   incurring the overhead of copying the data.
0038 
0039   A new evbuffer can be allocated with evbuffer_new(), and can be
0040   freed with evbuffer_free().  Most users will be using evbuffers via
0041   the bufferevent interface.  To access a bufferevent's evbuffers, use
0042   bufferevent_get_input() and bufferevent_get_output().
0043 
0044   There are several guidelines for using evbuffers.
0045 
0046   - if you already know how much data you are going to add as a result
0047     of calling evbuffer_add() multiple times, it makes sense to use
0048     evbuffer_expand() first to make sure that enough memory is allocated
0049     before hand.
0050 
0051   - evbuffer_add_buffer() adds the contents of one buffer to the other
0052     without incurring any unnecessary memory copies.
0053 
0054   - evbuffer_add() and evbuffer_add_buffer() do not mix very well:
0055     if you use them, you will wind up with fragmented memory in your
0056     buffer.
0057 
0058   - For high-performance code, you may want to avoid copying data into and out
0059     of buffers.  You can skip the copy step by using
0060     evbuffer_reserve_space()/evbuffer_commit_space() when writing into a
0061     buffer, and evbuffer_peek() when reading.
0062 
0063   In Libevent 2.0 and later, evbuffers are represented using a linked
0064   list of memory chunks, with pointers to the first and last chunk in
0065   the chain.
0066 
0067   As the contents of an evbuffer can be stored in multiple different
0068   memory blocks, it cannot be accessed directly.  Instead, evbuffer_pullup()
0069   can be used to force a specified number of bytes to be contiguous. This
0070   will cause memory reallocation and memory copies if the data is split
0071   across multiple blocks.  It is more efficient, however, to use
0072   evbuffer_peek() if you don't require that the memory to be contiguous.
0073  */
0074 
0075 #include <event2/visibility.h>
0076 
0077 #ifdef __cplusplus
0078 extern "C" {
0079 #endif
0080 
0081 #include <event2/event-config.h>
0082 #include <stdarg.h>
0083 #ifdef EVENT__HAVE_SYS_TYPES_H
0084 #include <sys/types.h>
0085 #endif
0086 #ifdef EVENT__HAVE_SYS_UIO_H
0087 #include <sys/uio.h>
0088 #endif
0089 #include <event2/util.h>
0090 
0091 /**
0092    An evbuffer is an opaque data type for efficiently buffering data to be
0093    sent or received on the network.
0094 
0095    @see event2/event.h for more information
0096 */
0097 struct evbuffer
0098 #ifdef EVENT_IN_DOXYGEN_
0099 {}
0100 #endif
0101 ;
0102 
0103 /**
0104     Pointer to a position within an evbuffer.
0105 
0106     Used when repeatedly searching through a buffer.  Calling any function
0107     that modifies or re-packs the buffer contents may invalidate all
0108     evbuffer_ptrs for that buffer.  Do not modify or contruct these values
0109     except with evbuffer_ptr_set.
0110 
0111     An evbuffer_ptr can represent any position from the start of a buffer up
0112     to a position immediately after the end of a buffer.
0113 
0114     @see evbuffer_ptr_set()
0115  */
0116 struct evbuffer_ptr {
0117     ev_ssize_t pos;
0118 
0119     /* Do not alter or rely on the values of fields: they are for internal
0120      * use */
0121     struct {
0122         void *chain;
0123         size_t pos_in_chain;
0124     } internal_;
0125 };
0126 
0127 /** Describes a single extent of memory inside an evbuffer.  Used for
0128     direct-access functions.
0129 
0130     @see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek
0131  */
0132 #ifdef EVENT__HAVE_SYS_UIO_H
0133 #define evbuffer_iovec iovec
0134 /* Internal use -- defined only if we are using the native struct iovec */
0135 #define EVBUFFER_IOVEC_IS_NATIVE_
0136 #else
0137 struct evbuffer_iovec {
0138     /** The start of the extent of memory. */
0139     void *iov_base;
0140     /** The length of the extent of memory. */
0141     size_t iov_len;
0142 };
0143 #endif
0144 
0145 /**
0146   Allocate storage for a new evbuffer.
0147 
0148   @return a pointer to a newly allocated evbuffer struct, or NULL if an error
0149     occurred
0150  */
0151 EVENT2_EXPORT_SYMBOL
0152 struct evbuffer *evbuffer_new(void);
0153 /**
0154   Deallocate storage for an evbuffer.
0155 
0156   @param buf pointer to the evbuffer to be freed
0157  */
0158 EVENT2_EXPORT_SYMBOL
0159 void evbuffer_free(struct evbuffer *buf);
0160 
0161 /**
0162    Enable locking on an evbuffer so that it can safely be used by multiple
0163    threads at the same time.
0164 
0165    NOTE: when locking is enabled, the lock will be held when callbacks are
0166    invoked.  This could result in deadlock if you aren't careful.  Plan
0167    accordingly!
0168 
0169    @param buf An evbuffer to make lockable.
0170    @param lock A lock object, or NULL if we should allocate our own.
0171    @return 0 on success, -1 on failure.
0172  */
0173 EVENT2_EXPORT_SYMBOL
0174 int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
0175 
0176 /**
0177    Acquire the lock on an evbuffer.  Has no effect if locking was not enabled
0178    with evbuffer_enable_locking.
0179 */
0180 EVENT2_EXPORT_SYMBOL
0181 void evbuffer_lock(struct evbuffer *buf);
0182 
0183 /**
0184    Release the lock on an evbuffer.  Has no effect if locking was not enabled
0185    with evbuffer_enable_locking.
0186 */
0187 EVENT2_EXPORT_SYMBOL
0188 void evbuffer_unlock(struct evbuffer *buf);
0189 
0190 
0191 /** If this flag is set, then we will not use evbuffer_peek(),
0192  * evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes
0193  * from this buffer: we'll only take bytes out of this buffer by
0194  * writing them to the network (as with evbuffer_write_atmost), by
0195  * removing them without observing them (as with evbuffer_drain),
0196  * or by copying them all out at once (as with evbuffer_add_buffer).
0197  *
0198  * Using this option allows the implementation to use sendfile-based
0199  * operations for evbuffer_add_file(); see that function for more
0200  * information.
0201  *
0202  * This flag is on by default for bufferevents that can take advantage
0203  * of it; you should never actually need to set it on a bufferevent's
0204  * output buffer.
0205  */
0206 #define EVBUFFER_FLAG_DRAINS_TO_FD 1
0207 
0208 /** Change the flags that are set for an evbuffer by adding more.
0209  *
0210  * @param buffer the evbuffer that the callback is watching.
0211  * @param cb the callback whose status we want to change.
0212  * @param flags One or more EVBUFFER_FLAG_* options
0213  * @return 0 on success, -1 on failure.
0214  */
0215 EVENT2_EXPORT_SYMBOL
0216 int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
0217 /** Change the flags that are set for an evbuffer by removing some.
0218  *
0219  * @param buffer the evbuffer that the callback is watching.
0220  * @param cb the callback whose status we want to change.
0221  * @param flags One or more EVBUFFER_FLAG_* options
0222  * @return 0 on success, -1 on failure.
0223  */
0224 EVENT2_EXPORT_SYMBOL
0225 int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
0226 
0227 /**
0228   Returns the total number of bytes stored in the evbuffer
0229 
0230   @param buf pointer to the evbuffer
0231   @return the number of bytes stored in the evbuffer
0232 */
0233 EVENT2_EXPORT_SYMBOL
0234 size_t evbuffer_get_length(const struct evbuffer *buf);
0235 
0236 /**
0237    Returns the number of contiguous available bytes in the first buffer chain.
0238 
0239    This is useful when processing data that might be split into multiple
0240    chains, or that might all be in the first chain.  Calls to
0241    evbuffer_pullup() that cause reallocation and copying of data can thus be
0242    avoided.
0243 
0244    @param buf pointer to the evbuffer
0245    @return 0 if no data is available, otherwise the number of available bytes
0246      in the first buffer chain.
0247 */
0248 EVENT2_EXPORT_SYMBOL
0249 size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
0250 
0251 /**
0252   Expands the available space in an evbuffer.
0253 
0254   Expands the available space in the evbuffer to at least datlen, so that
0255   appending datlen additional bytes will not require any new allocations.
0256 
0257   @param buf the evbuffer to be expanded
0258   @param datlen the new minimum length requirement
0259   @return 0 if successful, or -1 if an error occurred
0260 */
0261 EVENT2_EXPORT_SYMBOL
0262 int evbuffer_expand(struct evbuffer *buf, size_t datlen);
0263 
0264 /**
0265    Reserves space in the last chain or chains of an evbuffer.
0266 
0267    Makes space available in the last chain or chains of an evbuffer that can
0268    be arbitrarily written to by a user.  The space does not become
0269    available for reading until it has been committed with
0270    evbuffer_commit_space().
0271 
0272    The space is made available as one or more extents, represented by
0273    an initial pointer and a length.  You can force the memory to be
0274    available as only one extent.  Allowing more extents, however, makes the
0275    function more efficient.
0276 
0277    Multiple subsequent calls to this function will make the same space
0278    available until evbuffer_commit_space() has been called.
0279 
0280    It is an error to do anything that moves around the buffer's internal
0281    memory structures before committing the space.
0282 
0283    NOTE: The code currently does not ever use more than two extents.
0284    This may change in future versions.
0285 
0286    @param buf the evbuffer in which to reserve space.
0287    @param size how much space to make available, at minimum.  The
0288       total length of the extents may be greater than the requested
0289       length.
0290    @param vec an array of one or more evbuffer_iovec structures to
0291       hold pointers to the reserved extents of memory.
0292    @param n_vec The length of the vec array.  Must be at least 1;
0293        2 is more efficient.
0294    @return the number of provided extents, or -1 on error.
0295    @see evbuffer_commit_space()
0296 */
0297 EVENT2_EXPORT_SYMBOL
0298 int
0299 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
0300     struct evbuffer_iovec *vec, int n_vec);
0301 
0302 /**
0303    Commits previously reserved space.
0304 
0305    Commits some of the space previously reserved with
0306    evbuffer_reserve_space().  It then becomes available for reading.
0307 
0308    This function may return an error if the pointer in the extents do
0309    not match those returned from evbuffer_reserve_space, or if data
0310    has been added to the buffer since the space was reserved.
0311 
0312    If you want to commit less data than you got reserved space for,
0313    modify the iov_len pointer of the appropriate extent to a smaller
0314    value.  Note that you may have received more space than you
0315    requested if it was available!
0316 
0317    @param buf the evbuffer in which to reserve space.
0318    @param vec one or two extents returned by evbuffer_reserve_space.
0319    @param n_vecs the number of extents.
0320    @return 0 on success, -1 on error
0321    @see evbuffer_reserve_space()
0322 */
0323 EVENT2_EXPORT_SYMBOL
0324 int evbuffer_commit_space(struct evbuffer *buf,
0325     struct evbuffer_iovec *vec, int n_vecs);
0326 
0327 /**
0328   Append data to the end of an evbuffer.
0329 
0330   @param buf the evbuffer to be appended to
0331   @param data pointer to the beginning of the data buffer
0332   @param datlen the number of bytes to be copied from the data buffer
0333   @return 0 on success, -1 on failure.
0334  */
0335 EVENT2_EXPORT_SYMBOL
0336 int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
0337 
0338 
0339 /**
0340   Read data from an evbuffer and drain the bytes read.
0341 
0342   If more bytes are requested than are available in the evbuffer, we
0343   only extract as many bytes as were available.
0344 
0345   @param buf the evbuffer to be read from
0346   @param data the destination buffer to store the result
0347   @param datlen the maximum size of the destination buffer
0348   @return the number of bytes read, or -1 if we can't drain the buffer.
0349  */
0350 EVENT2_EXPORT_SYMBOL
0351 int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
0352 
0353 /**
0354   Read data from an evbuffer, and leave the buffer unchanged.
0355 
0356   If more bytes are requested than are available in the evbuffer, we
0357   only extract as many bytes as were available.
0358 
0359   @param buf the evbuffer to be read from
0360   @param data_out the destination buffer to store the result
0361   @param datlen the maximum size of the destination buffer
0362   @return the number of bytes read, or -1 if we can't drain the buffer.
0363  */
0364 EVENT2_EXPORT_SYMBOL
0365 ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
0366 
0367 /**
0368   Read data from the middle of an evbuffer, and leave the buffer unchanged.
0369 
0370   If more bytes are requested than are available in the evbuffer, we
0371   only extract as many bytes as were available.
0372 
0373   @param buf the evbuffer to be read from
0374   @param pos the position to start reading from
0375   @param data_out the destination buffer to store the result
0376   @param datlen the maximum size of the destination buffer
0377   @return the number of bytes read, or -1 if we can't drain the buffer.
0378  */
0379 EVENT2_EXPORT_SYMBOL
0380 ev_ssize_t evbuffer_copyout_from(struct evbuffer *buf, const struct evbuffer_ptr *pos, void *data_out, size_t datlen);
0381 
0382 /**
0383   Read data from an evbuffer into another evbuffer, draining
0384   the bytes from the source buffer.  This function avoids copy
0385   operations to the extent possible.
0386 
0387   If more bytes are requested than are available in src, the src
0388   buffer is drained completely.
0389 
0390   @param src the evbuffer to be read from
0391   @param dst the destination evbuffer to store the result into
0392   @param datlen the maximum numbers of bytes to transfer
0393   @return the number of bytes read
0394  */
0395 EVENT2_EXPORT_SYMBOL
0396 int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
0397     size_t datlen);
0398 
0399 /** Used to tell evbuffer_readln what kind of line-ending to look for.
0400  */
0401 enum evbuffer_eol_style {
0402     /** Any sequence of CR and LF characters is acceptable as an
0403      * EOL.
0404      *
0405      * Note that this style can produce ambiguous results: the
0406      * sequence "CRLF" will be treated as a single EOL if it is
0407      * all in the buffer at once, but if you first read a CR from
0408      * the network and later read an LF from the network, it will
0409      * be treated as two EOLs.
0410      */
0411     EVBUFFER_EOL_ANY,
0412     /** An EOL is an LF, optionally preceded by a CR.  This style is
0413      * most useful for implementing text-based internet protocols. */
0414     EVBUFFER_EOL_CRLF,
0415     /** An EOL is a CR followed by an LF. */
0416     EVBUFFER_EOL_CRLF_STRICT,
0417     /** An EOL is a LF. */
0418     EVBUFFER_EOL_LF,
0419     /** An EOL is a NUL character (that is, a single byte with value 0) */
0420     EVBUFFER_EOL_NUL
0421 };
0422 
0423 /**
0424  * Read a single line from an evbuffer.
0425  *
0426  * Reads a line terminated by an EOL as determined by the evbuffer_eol_style
0427  * argument.  Returns a newly allocated nul-terminated string; the caller must
0428  * free the returned value.  The EOL is not included in the returned string.
0429  *
0430  * @param buffer the evbuffer to read from
0431  * @param n_read_out if non-NULL, points to a size_t that is set to the
0432  *       number of characters in the returned string.  This is useful for
0433  *       strings that can contain NUL characters.
0434  * @param eol_style the style of line-ending to use.
0435  * @return pointer to a single line, or NULL if an error occurred
0436  */
0437 EVENT2_EXPORT_SYMBOL
0438 char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
0439     enum evbuffer_eol_style eol_style);
0440 
0441 /**
0442   Move all data from one evbuffer into another evbuffer.
0443 
0444   This is a destructive add.  The data from one buffer moves into
0445   the other buffer.  However, no unnecessary memory copies occur.
0446 
0447   @param outbuf the output buffer
0448   @param inbuf the input buffer
0449   @return 0 if successful, or -1 if an error occurred
0450 
0451   @see evbuffer_remove_buffer()
0452  */
0453 EVENT2_EXPORT_SYMBOL
0454 int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
0455 
0456 /**
0457   Copy data from one evbuffer into another evbuffer.
0458 
0459   This is a non-destructive add.  The data from one buffer is copied
0460   into the other buffer.  However, no unnecessary memory copies occur.
0461 
0462   Note that buffers already containing buffer references can't be added
0463   to other buffers.
0464 
0465   @param outbuf the output buffer
0466   @param inbuf the input buffer
0467   @return 0 if successful, or -1 if an error occurred
0468  */
0469 EVENT2_EXPORT_SYMBOL
0470 int evbuffer_add_buffer_reference(struct evbuffer *outbuf,
0471     struct evbuffer *inbuf);
0472 
0473 /**
0474    A cleanup function for a piece of memory added to an evbuffer by
0475    reference.
0476 
0477    @see evbuffer_add_reference()
0478  */
0479 typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
0480     size_t datalen, void *extra);
0481 
0482 /**
0483   Reference memory into an evbuffer without copying.
0484 
0485   The memory needs to remain valid until all the added data has been
0486   read.  This function keeps just a reference to the memory without
0487   actually incurring the overhead of a copy.
0488 
0489   @param outbuf the output buffer
0490   @param data the memory to reference
0491   @param datlen how memory to reference
0492   @param cleanupfn callback to be invoked when the memory is no longer
0493     referenced by this evbuffer.
0494   @param cleanupfn_arg optional argument to the cleanup callback
0495   @return 0 if successful, or -1 if an error occurred
0496  */
0497 EVENT2_EXPORT_SYMBOL
0498 int evbuffer_add_reference(struct evbuffer *outbuf,
0499     const void *data, size_t datlen,
0500     evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
0501 
0502 /**
0503   Copy data from a file into the evbuffer for writing to a socket.
0504 
0505   This function avoids unnecessary data copies between userland and
0506   kernel.  If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD
0507   flag is set, it uses those functions.  Otherwise, it tries to use
0508   mmap (or CreateFileMapping on Windows).
0509 
0510   The function owns the resulting file descriptor and will close it
0511   when finished transferring data.
0512 
0513   The results of using evbuffer_remove() or evbuffer_pullup() on
0514   evbuffers whose data was added using this function are undefined.
0515 
0516   For more fine-grained control, use evbuffer_add_file_segment.
0517 
0518   @param outbuf the output buffer
0519   @param fd the file descriptor
0520   @param offset the offset from which to read data
0521   @param length how much data to read, or -1 to read as much as possible.
0522     (-1 requires that 'fd' support fstat.)
0523   @return 0 if successful, or -1 if an error occurred
0524 */
0525 
0526 EVENT2_EXPORT_SYMBOL
0527 int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
0528     ev_off_t length);
0529 
0530 /**
0531   An evbuffer_file_segment holds a reference to a range of a file --
0532   possibly the whole file! -- for use in writing from an evbuffer to a
0533   socket.  It could be implemented with mmap, sendfile, splice, or (if all
0534   else fails) by just pulling all the data into RAM.  A single
0535   evbuffer_file_segment can be added more than once, and to more than one
0536   evbuffer.
0537  */
0538 struct evbuffer_file_segment;
0539 
0540 /**
0541     Flag for creating evbuffer_file_segment: If this flag is set, then when
0542     the evbuffer_file_segment is freed and no longer in use by any
0543     evbuffer, the underlying fd is closed.
0544  */
0545 #define EVBUF_FS_CLOSE_ON_FREE    0x01
0546 /**
0547    Flag for creating evbuffer_file_segment: Disable memory-map based
0548    implementations.
0549  */
0550 #define EVBUF_FS_DISABLE_MMAP     0x02
0551 /**
0552    Flag for creating evbuffer_file_segment: Disable direct fd-to-fd
0553    implementations (including sendfile and splice).
0554 
0555    You might want to use this option if data needs to be taken from the
0556    evbuffer by any means other than writing it to the network: the sendfile
0557    backend is fast, but it only works for sending files directly to the
0558    network.
0559  */
0560 #define EVBUF_FS_DISABLE_SENDFILE 0x04
0561 /**
0562    Flag for creating evbuffer_file_segment: Do not allocate a lock for this
0563    segment.  If this option is set, then neither the segment nor any
0564    evbuffer it is added to may ever be accessed from more than one thread
0565    at a time.
0566  */
0567 #define EVBUF_FS_DISABLE_LOCKING  0x08
0568 
0569 /**
0570    A cleanup function for a evbuffer_file_segment added to an evbuffer
0571    for reference.
0572  */
0573 typedef void (*evbuffer_file_segment_cleanup_cb)(
0574     struct evbuffer_file_segment const* seg, int flags, void* arg);
0575 
0576 /**
0577    Create and return a new evbuffer_file_segment for reading data from a
0578    file and sending it out via an evbuffer.
0579 
0580    This function avoids unnecessary data copies between userland and
0581    kernel.  Where available, it uses sendfile or splice.
0582 
0583    The file descriptor must not be closed so long as any evbuffer is using
0584    this segment.
0585 
0586    The results of using evbuffer_remove() or evbuffer_pullup() or any other
0587    function that reads bytes from an evbuffer on any evbuffer containing
0588    the newly returned segment are undefined, unless you pass the
0589    EVBUF_FS_DISABLE_SENDFILE flag to this function.
0590 
0591    @param fd an open file to read from.
0592    @param offset an index within the file at which to start reading
0593    @param length how much data to read, or -1 to read as much as possible.
0594       (-1 requires that 'fd' support fstat.)
0595    @param flags any number of the EVBUF_FS_* flags
0596    @return a new evbuffer_file_segment, or NULL on failure.
0597  **/
0598 EVENT2_EXPORT_SYMBOL
0599 struct evbuffer_file_segment *evbuffer_file_segment_new(
0600     int fd, ev_off_t offset, ev_off_t length, unsigned flags);
0601 
0602 /**
0603    Free an evbuffer_file_segment
0604 
0605    It is safe to call this function even if the segment has been added to
0606    one or more evbuffers.  The evbuffer_file_segment will not be freed
0607    until no more references to it exist.
0608  */
0609 EVENT2_EXPORT_SYMBOL
0610 void evbuffer_file_segment_free(struct evbuffer_file_segment *seg);
0611 
0612 /**
0613    Add cleanup callback and argument for the callback to an
0614    evbuffer_file_segment.
0615 
0616    The cleanup callback will be invoked when no more references to the
0617    evbuffer_file_segment exist.
0618  **/
0619 EVENT2_EXPORT_SYMBOL
0620 void evbuffer_file_segment_add_cleanup_cb(struct evbuffer_file_segment *seg,
0621     evbuffer_file_segment_cleanup_cb cb, void* arg);
0622 
0623 /**
0624    Insert some or all of an evbuffer_file_segment at the end of an evbuffer
0625 
0626    Note that the offset and length parameters of this function have a
0627    different meaning from those provided to evbuffer_file_segment_new: When
0628    you create the segment, the offset is the offset _within the file_, and
0629    the length is the length _of the segment_, whereas when you add a
0630    segment to an evbuffer, the offset is _within the segment_ and the
0631    length is the length of the _part of the segment you want to use.
0632 
0633    In other words, if you have a 10 KiB file, and you create an
0634    evbuffer_file_segment for it with offset 20 and length 1000, it will
0635    refer to bytes 20..1019 inclusive.  If you then pass this segment to
0636    evbuffer_add_file_segment and specify an offset of 20 and a length of
0637    50, you will be adding bytes 40..99 inclusive.
0638 
0639    @param buf the evbuffer to append to
0640    @param seg the segment to add
0641    @param offset the offset within the segment to start from
0642    @param length the amount of data to add, or -1 to add it all.
0643    @return 0 on success, -1 on failure.
0644  */
0645 EVENT2_EXPORT_SYMBOL
0646 int evbuffer_add_file_segment(struct evbuffer *buf,
0647     struct evbuffer_file_segment *seg, ev_off_t offset, ev_off_t length);
0648 
0649 /**
0650   Append a formatted string to the end of an evbuffer.
0651 
0652   The string is formated as printf.
0653 
0654   @param buf the evbuffer that will be appended to
0655   @param fmt a format string
0656   @param ... arguments that will be passed to printf(3)
0657   @return The number of bytes added if successful, or -1 if an error occurred.
0658 
0659   @see evutil_printf(), evbuffer_add_vprintf()
0660  */
0661 EVENT2_EXPORT_SYMBOL
0662 int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
0663 #ifdef __GNUC__
0664   __attribute__((format(printf, 2, 3)))
0665 #endif
0666 ;
0667 
0668 /**
0669   Append a va_list formatted string to the end of an evbuffer.
0670 
0671   @param buf the evbuffer that will be appended to
0672   @param fmt a format string
0673   @param ap a varargs va_list argument array that will be passed to vprintf(3)
0674   @return The number of bytes added if successful, or -1 if an error occurred.
0675  */
0676 EVENT2_EXPORT_SYMBOL
0677 int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
0678 #ifdef __GNUC__
0679     __attribute__((format(printf, 2, 0)))
0680 #endif
0681 ;
0682 
0683 
0684 /**
0685   Remove a specified number of bytes data from the beginning of an evbuffer.
0686 
0687   @param buf the evbuffer to be drained
0688   @param len the number of bytes to drain from the beginning of the buffer
0689   @return 0 on success, -1 on failure.
0690  */
0691 EVENT2_EXPORT_SYMBOL
0692 int evbuffer_drain(struct evbuffer *buf, size_t len);
0693 
0694 
0695 /**
0696   Write the contents of an evbuffer to a file descriptor.
0697 
0698   The evbuffer will be drained after the bytes have been successfully written.
0699 
0700   @param buffer the evbuffer to be written and drained
0701   @param fd the file descriptor to be written to
0702   @return the number of bytes written, or -1 if an error occurred
0703   @see evbuffer_read()
0704  */
0705 EVENT2_EXPORT_SYMBOL
0706 int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
0707 
0708 /**
0709   Write some of the contents of an evbuffer to a file descriptor.
0710 
0711   The evbuffer will be drained after the bytes have been successfully written.
0712 
0713   @param buffer the evbuffer to be written and drained
0714   @param fd the file descriptor to be written to
0715   @param howmuch the largest allowable number of bytes to write, or -1
0716     to write as many bytes as we can.
0717   @return the number of bytes written, or -1 if an error occurred
0718   @see evbuffer_read()
0719  */
0720 EVENT2_EXPORT_SYMBOL
0721 int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
0722                           ev_ssize_t howmuch);
0723 
0724 /**
0725   Read from a file descriptor and store the result in an evbuffer.
0726 
0727   @param buffer the evbuffer to store the result
0728   @param fd the file descriptor to read from
0729   @param howmuch the number of bytes to be read. If the given number is negative
0730   or out of maximum bytes per one read, as many bytes as we can will be read.
0731   @return the number of bytes read, or -1 if an error occurred
0732   @see evbuffer_write()
0733  */
0734 EVENT2_EXPORT_SYMBOL
0735 int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
0736 
0737 /**
0738    Search for a string within an evbuffer.
0739 
0740    @param buffer the evbuffer to be searched
0741    @param what the string to be searched for
0742    @param len the length of the search string
0743    @param start NULL or a pointer to a valid struct evbuffer_ptr.
0744    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
0745      first occurrence of the string in the buffer after 'start'.  The 'pos'
0746      field of the result is -1 if the string was not found.
0747  */
0748 EVENT2_EXPORT_SYMBOL
0749 struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
0750 
0751 /**
0752    Search for a string within part of an evbuffer.
0753 
0754    @param buffer the evbuffer to be searched
0755    @param what the string to be searched for
0756    @param len the length of the search string
0757    @param start NULL or a pointer to a valid struct evbuffer_ptr that
0758      indicates where we should start searching.
0759    @param end NULL or a pointer to a valid struct evbuffer_ptr that
0760      indicates where we should stop searching.
0761    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
0762      first occurrence of the string in the buffer after 'start'.  The 'pos'
0763      field of the result is -1 if the string was not found.
0764  */
0765 EVENT2_EXPORT_SYMBOL
0766 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);
0767 
0768 /**
0769    Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()
0770 
0771    @see evbuffer_ptr_set() */
0772 enum evbuffer_ptr_how {
0773     /** Sets the pointer to the position; can be called on with an
0774         uninitialized evbuffer_ptr. */
0775     EVBUFFER_PTR_SET,
0776     /** Advances the pointer by adding to the current position. */
0777     EVBUFFER_PTR_ADD
0778 };
0779 
0780 /**
0781    Sets the search pointer in the buffer to position.
0782 
0783    There are two ways to use this function: you can call
0784       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
0785    to move 'pos' to a position 'N' bytes after the start of the buffer, or
0786       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_ADD)
0787    to move 'pos' forward by 'N' bytes.
0788 
0789    If evbuffer_ptr is not initialized, this function can only be called
0790    with EVBUFFER_PTR_SET.
0791 
0792    An evbuffer_ptr can represent any position from the start of the buffer to
0793    a position immediately after the end of the buffer.
0794 
0795    @param buffer the evbuffer to be search
0796    @param ptr a pointer to a struct evbuffer_ptr
0797    @param position the position at which to start the next search
0798    @param how determines how the pointer should be manipulated.
0799    @returns 0 on success or -1 otherwise
0800 */
0801 EVENT2_EXPORT_SYMBOL
0802 int
0803 evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
0804     size_t position, enum evbuffer_ptr_how how);
0805 
0806 /**
0807    Search for an end-of-line string within an evbuffer.
0808 
0809    @param buffer the evbuffer to be searched
0810    @param start NULL or a pointer to a valid struct evbuffer_ptr to start
0811       searching at.
0812    @param eol_len_out If non-NULL, the pointed-to value will be set to
0813       the length of the end-of-line string.
0814    @param eol_style The kind of EOL to look for; see evbuffer_readln() for
0815       more information
0816    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
0817      first occurrence EOL in the buffer after 'start'.  The 'pos'
0818      field of the result is -1 if the string was not found.
0819  */
0820 EVENT2_EXPORT_SYMBOL
0821 struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
0822     struct evbuffer_ptr *start, size_t *eol_len_out,
0823     enum evbuffer_eol_style eol_style);
0824 
0825 /** Function to peek at data inside an evbuffer without removing it or
0826     copying it out.
0827 
0828     Pointers to the data are returned by filling the 'vec_out' array
0829     with pointers to one or more extents of data inside the buffer.
0830 
0831     The total data in the extents that you get back may be more than
0832     you requested (if there is more data last extent than you asked
0833     for), or less (if you do not provide enough evbuffer_iovecs, or if
0834     the buffer does not have as much data as you asked to see).
0835 
0836     @param buffer the evbuffer to peek into,
0837     @param len the number of bytes to try to peek.  If len is negative, we
0838        will try to fill as much of vec_out as we can.  If len is negative
0839        and vec_out is not provided, we return the number of evbuffer_iovecs
0840        that would be needed to get all the data in the buffer.
0841     @param start_at an evbuffer_ptr indicating the point at which we
0842        should start looking for data.  NULL means, "At the start of the
0843        buffer."
0844     @param vec_out an array of evbuffer_iovec
0845     @param n_vec the length of vec_out.  If 0, we only count how many
0846        extents would be necessary to point to the requested amount of
0847        data.
0848     @return The number of extents needed.  This may be less than n_vec
0849        if we didn't need all the evbuffer_iovecs we were given, or more
0850        than n_vec if we would need more to return all the data that was
0851        requested.
0852  */
0853 EVENT2_EXPORT_SYMBOL
0854 int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
0855     struct evbuffer_ptr *start_at,
0856     struct evbuffer_iovec *vec_out, int n_vec);
0857 
0858 
0859 /** Structure passed to an evbuffer_cb_func evbuffer callback
0860 
0861     @see evbuffer_cb_func, evbuffer_add_cb()
0862  */
0863 struct evbuffer_cb_info {
0864     /** The number of bytes in this evbuffer when callbacks were last
0865      * invoked. */
0866     size_t orig_size;
0867     /** The number of bytes added since callbacks were last invoked. */
0868     size_t n_added;
0869     /** The number of bytes removed since callbacks were last invoked. */
0870     size_t n_deleted;
0871 };
0872 
0873 /** Type definition for a callback that is invoked whenever data is added or
0874     removed from an evbuffer.
0875 
0876     An evbuffer may have one or more callbacks set at a time.  The order
0877     in which they are executed is undefined.
0878 
0879     A callback function may add more callbacks, or remove itself from the
0880     list of callbacks, or add or remove data from the buffer.  It may not
0881     remove another callback from the list.
0882 
0883     If a callback adds or removes data from the buffer or from another
0884     buffer, this can cause a recursive invocation of your callback or
0885     other callbacks.  If you ask for an infinite loop, you might just get
0886     one: watch out!
0887 
0888     @param buffer the buffer whose size has changed
0889     @param info a structure describing how the buffer changed.
0890     @param arg a pointer to user data
0891 */
0892 typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
0893 
0894 struct evbuffer_cb_entry;
0895 /** Add a new callback to an evbuffer.
0896 
0897   Subsequent calls to evbuffer_add_cb() add new callbacks.  To remove this
0898   callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
0899 
0900   @param buffer the evbuffer to be monitored
0901   @param cb the callback function to invoke when the evbuffer is modified,
0902     or NULL to remove all callbacks.
0903   @param cbarg an argument to be provided to the callback function
0904   @return a handle to the callback on success, or NULL on failure.
0905  */
0906 EVENT2_EXPORT_SYMBOL
0907 struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
0908 
0909 /** Remove a callback from an evbuffer, given a handle returned from
0910     evbuffer_add_cb.
0911 
0912     Calling this function invalidates the handle.
0913 
0914     @return 0 if a callback was removed, or -1 if no matching callback was
0915     found.
0916  */
0917 EVENT2_EXPORT_SYMBOL
0918 int evbuffer_remove_cb_entry(struct evbuffer *buffer,
0919                  struct evbuffer_cb_entry *ent);
0920 
0921 /** Remove a callback from an evbuffer, given the function and argument
0922     used to add it.
0923 
0924     @return 0 if a callback was removed, or -1 if no matching callback was
0925     found.
0926  */
0927 EVENT2_EXPORT_SYMBOL
0928 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
0929 
0930 /** If this flag is not set, then a callback is temporarily disabled, and
0931  * should not be invoked.
0932  *
0933  * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()
0934  */
0935 #define EVBUFFER_CB_ENABLED 1
0936 
0937 /** Change the flags that are set for a callback on a buffer by adding more.
0938 
0939     @param buffer the evbuffer that the callback is watching.
0940     @param cb the callback whose status we want to change.
0941     @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
0942     @return 0 on success, -1 on failure.
0943  */
0944 EVENT2_EXPORT_SYMBOL
0945 int evbuffer_cb_set_flags(struct evbuffer *buffer,
0946               struct evbuffer_cb_entry *cb, ev_uint32_t flags);
0947 
0948 /** Change the flags that are set for a callback on a buffer by removing some
0949 
0950     @param buffer the evbuffer that the callback is watching.
0951     @param cb the callback whose status we want to change.
0952     @param flags EVBUFFER_CB_ENABLED to disable the callback.
0953     @return 0 on success, -1 on failure.
0954  */
0955 EVENT2_EXPORT_SYMBOL
0956 int evbuffer_cb_clear_flags(struct evbuffer *buffer,
0957               struct evbuffer_cb_entry *cb, ev_uint32_t flags);
0958 
0959 #if 0
0960 /** Postpone calling a given callback until unsuspend is called later.
0961 
0962     This is different from disabling the callback, since the callback will get
0963     invoked later if the buffer size changes between now and when we unsuspend
0964     it.
0965 
0966     @param the buffer that the callback is watching.
0967     @param cb the callback we want to suspend.
0968  */
0969 EVENT2_EXPORT_SYMBOL
0970 void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
0971 /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
0972 
0973     If data was added to or removed from the buffer while the callback was
0974     suspended, the callback will get called once now.
0975 
0976     @param the buffer that the callback is watching.
0977     @param cb the callback we want to stop suspending.
0978  */
0979 EVENT2_EXPORT_SYMBOL
0980 void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
0981 #endif
0982 
0983 /**
0984   Makes the data at the beginning of an evbuffer contiguous.
0985 
0986   @param buf the evbuffer to make contiguous
0987   @param size the number of bytes to make contiguous, or -1 to make the
0988     entire buffer contiguous.
0989   @return a pointer to the contiguous memory array, or NULL if param size
0990     requested more data than is present in the buffer.
0991 */
0992 
0993 EVENT2_EXPORT_SYMBOL
0994 unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
0995 
0996 /**
0997   Prepends data to the beginning of the evbuffer
0998 
0999   @param buf the evbuffer to which to prepend data
1000   @param data a pointer to the memory to prepend
1001   @param size the number of bytes to prepend
1002   @return 0 if successful, or -1 otherwise
1003 */
1004 
1005 EVENT2_EXPORT_SYMBOL
1006 int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
1007 
1008 /**
1009   Prepends all data from the src evbuffer to the beginning of the dst
1010   evbuffer.
1011 
1012   @param dst the evbuffer to which to prepend data
1013   @param src the evbuffer to prepend; it will be emptied as a result
1014   @return 0 if successful, or -1 otherwise
1015 */
1016 EVENT2_EXPORT_SYMBOL
1017 int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
1018 
1019 /**
1020    Prevent calls that modify an evbuffer from succeeding. A buffer may
1021    frozen at the front, at the back, or at both the front and the back.
1022 
1023    If the front of a buffer is frozen, operations that drain data from
1024    the front of the buffer, or that prepend data to the buffer, will
1025    fail until it is unfrozen.   If the back a buffer is frozen, operations
1026    that append data from the buffer will fail until it is unfrozen.
1027 
1028    @param buf The buffer to freeze
1029    @param at_front If true, we freeze the front of the buffer.  If false,
1030       we freeze the back.
1031    @return 0 on success, -1 on failure.
1032 */
1033 EVENT2_EXPORT_SYMBOL
1034 int evbuffer_freeze(struct evbuffer *buf, int at_front);
1035 /**
1036    Re-enable calls that modify an evbuffer.
1037 
1038    @param buf The buffer to un-freeze
1039    @param at_front If true, we unfreeze the front of the buffer.  If false,
1040       we unfreeze the back.
1041    @return 0 on success, -1 on failure.
1042  */
1043 EVENT2_EXPORT_SYMBOL
1044 int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
1045 
1046 struct event_base;
1047 /**
1048    Force all the callbacks on an evbuffer to be run, not immediately after
1049    the evbuffer is altered, but instead from inside the event loop.
1050 
1051    This can be used to serialize all the callbacks to a single thread
1052    of execution.
1053  */
1054 EVENT2_EXPORT_SYMBOL
1055 int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
1056 
1057 /**
1058   Append data from 1 or more iovec's to an evbuffer
1059 
1060   Calculates the number of bytes needed for an iovec structure and guarantees
1061   all data will fit into a single chain. Can be used in lieu of functionality
1062   which calls evbuffer_add() constantly before being used to increase
1063   performance.
1064 
1065   @param buffer the destination buffer
1066   @param vec the source iovec
1067   @param n_vec the number of iovec structures.
1068   @return the number of bytes successfully written to the output buffer.
1069 */
1070 EVENT2_EXPORT_SYMBOL
1071 size_t evbuffer_add_iovec(struct evbuffer * buffer, struct evbuffer_iovec * vec, int n_vec);
1072 
1073 #ifdef __cplusplus
1074 }
1075 #endif
1076 
1077 #endif /* EVENT2_BUFFER_H_INCLUDED_ */