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