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