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