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