xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/include/event2/buffer.h (revision eabc0478de71e4e011a5b4e0392741e01d491794)
1 /*	$NetBSD: buffer.h,v 1.7 2024/08/18 20:47:22 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 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  */
118 struct 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
139 struct 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  */
153 EVENT2_EXPORT_SYMBOL
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 EVENT2_EXPORT_SYMBOL
161 void 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  */
175 EVENT2_EXPORT_SYMBOL
176 int 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 */
182 EVENT2_EXPORT_SYMBOL
183 void 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 */
189 EVENT2_EXPORT_SYMBOL
190 void 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  */
217 EVENT2_EXPORT_SYMBOL
218 int 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  */
226 EVENT2_EXPORT_SYMBOL
227 int 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 */
235 EVENT2_EXPORT_SYMBOL
236 size_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 */
250 EVENT2_EXPORT_SYMBOL
251 size_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 */
263 EVENT2_EXPORT_SYMBOL
264 int 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 */
299 EVENT2_EXPORT_SYMBOL
300 int
301 evbuffer_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 */
325 EVENT2_EXPORT_SYMBOL
326 int 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  */
337 EVENT2_EXPORT_SYMBOL
338 int 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  */
352 EVENT2_EXPORT_SYMBOL
353 int 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  */
366 EVENT2_EXPORT_SYMBOL
367 ev_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  */
381 EVENT2_EXPORT_SYMBOL
382 ev_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  */
397 EVENT2_EXPORT_SYMBOL
398 int 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  */
403 enum 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  */
439 EVENT2_EXPORT_SYMBOL
440 char *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  */
455 EVENT2_EXPORT_SYMBOL
456 int 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  */
471 EVENT2_EXPORT_SYMBOL
472 int 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  */
481 typedef 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  */
499 EVENT2_EXPORT_SYMBOL
500 int 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 
528 EVENT2_EXPORT_SYMBOL
529 int 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  */
540 struct 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  */
575 typedef 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  **/
600 EVENT2_EXPORT_SYMBOL
601 struct 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  */
611 EVENT2_EXPORT_SYMBOL
612 void 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  **/
621 EVENT2_EXPORT_SYMBOL
622 void 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  */
647 EVENT2_EXPORT_SYMBOL
648 int 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  */
663 EVENT2_EXPORT_SYMBOL
664 int 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  */
678 EVENT2_EXPORT_SYMBOL
679 int 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  */
693 EVENT2_EXPORT_SYMBOL
694 int 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  */
707 EVENT2_EXPORT_SYMBOL
708 int 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  */
722 EVENT2_EXPORT_SYMBOL
723 int 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. If the given number is negative
732   or out of maximum bytes per one read, as many bytes as we can will be read.
733   @return the number of bytes read, or -1 if an error occurred
734   @see evbuffer_write()
735  */
736 EVENT2_EXPORT_SYMBOL
737 int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
738 
739 /**
740    Search for a string within an evbuffer.
741 
742    @param buffer the evbuffer to be searched
743    @param what the string to be searched for
744    @param len the length of the search string
745    @param start NULL or a pointer to a valid struct evbuffer_ptr.
746    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
747      first occurrence of the string in the buffer after 'start'.  The 'pos'
748      field of the result is -1 if the string was not found.
749  */
750 EVENT2_EXPORT_SYMBOL
751 struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
752 
753 /**
754    Search for a string within part of an evbuffer.
755 
756    @param buffer the evbuffer to be searched
757    @param what the string to be searched for
758    @param len the length of the search string
759    @param start NULL or a pointer to a valid struct evbuffer_ptr that
760      indicates where we should start searching.
761    @param end NULL or a pointer to a valid struct evbuffer_ptr that
762      indicates where we should stop searching.
763    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
764      first occurrence of the string in the buffer after 'start'.  The 'pos'
765      field of the result is -1 if the string was not found.
766  */
767 EVENT2_EXPORT_SYMBOL
768 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);
769 
770 /**
771    Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()
772 
773    @see evbuffer_ptr_set() */
774 enum evbuffer_ptr_how {
775 	/** Sets the pointer to the position; can be called on with an
776 	    uninitialized evbuffer_ptr. */
777 	EVBUFFER_PTR_SET,
778 	/** Advances the pointer by adding to the current position. */
779 	EVBUFFER_PTR_ADD
780 };
781 
782 /**
783    Sets the search pointer in the buffer to position.
784 
785    There are two ways to use this function: you can call
786       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_SET)
787    to move 'pos' to a position 'N' bytes after the start of the buffer, or
788       evbuffer_ptr_set(buf, &pos, N, EVBUFFER_PTR_ADD)
789    to move 'pos' forward by 'N' bytes.
790 
791    If evbuffer_ptr is not initialized, this function can only be called
792    with EVBUFFER_PTR_SET.
793 
794    An evbuffer_ptr can represent any position from the start of the buffer to
795    a position immediately after the end of the buffer.
796 
797    @param buffer the evbuffer to be search
798    @param ptr a pointer to a struct evbuffer_ptr
799    @param position the position at which to start the next search
800    @param how determines how the pointer should be manipulated.
801    @returns 0 on success or -1 otherwise
802 */
803 EVENT2_EXPORT_SYMBOL
804 int
805 evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
806     size_t position, enum evbuffer_ptr_how how);
807 
808 /**
809    Search for an end-of-line string within an evbuffer.
810 
811    @param buffer the evbuffer to be searched
812    @param start NULL or a pointer to a valid struct evbuffer_ptr to start
813       searching at.
814    @param eol_len_out If non-NULL, the pointed-to value will be set to
815       the length of the end-of-line string.
816    @param eol_style The kind of EOL to look for; see evbuffer_readln() for
817       more information
818    @return a struct evbuffer_ptr whose 'pos' field has the offset of the
819      first occurrence EOL in the buffer after 'start'.  The 'pos'
820      field of the result is -1 if the string was not found.
821  */
822 EVENT2_EXPORT_SYMBOL
823 struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
824     struct evbuffer_ptr *start, size_t *eol_len_out,
825     enum evbuffer_eol_style eol_style);
826 
827 /** Function to peek at data inside an evbuffer without removing it or
828     copying it out.
829 
830     Pointers to the data are returned by filling the 'vec_out' array
831     with pointers to one or more extents of data inside the buffer.
832 
833     The total data in the extents that you get back may be more than
834     you requested (if there is more data last extent than you asked
835     for), or less (if you do not provide enough evbuffer_iovecs, or if
836     the buffer does not have as much data as you asked to see).
837 
838     @param buffer the evbuffer to peek into,
839     @param len the number of bytes to try to peek.  If len is negative, we
840        will try to fill as much of vec_out as we can.  If len is negative
841        and vec_out is not provided, we return the number of evbuffer_iovecs
842        that would be needed to get all the data in the buffer.
843     @param start_at an evbuffer_ptr indicating the point at which we
844        should start looking for data.  NULL means, "At the start of the
845        buffer."
846     @param vec_out an array of evbuffer_iovec
847     @param n_vec the length of vec_out.  If 0, we only count how many
848        extents would be necessary to point to the requested amount of
849        data.
850     @return The number of extents needed.  This may be less than n_vec
851        if we didn't need all the evbuffer_iovecs we were given, or more
852        than n_vec if we would need more to return all the data that was
853        requested.
854  */
855 EVENT2_EXPORT_SYMBOL
856 int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
857     struct evbuffer_ptr *start_at,
858     struct evbuffer_iovec *vec_out, int n_vec);
859 
860 
861 /** Structure passed to an evbuffer_cb_func evbuffer callback
862 
863     @see evbuffer_cb_func, evbuffer_add_cb()
864  */
865 struct evbuffer_cb_info {
866 	/** The number of bytes in this evbuffer when callbacks were last
867 	 * invoked. */
868 	size_t orig_size;
869 	/** The number of bytes added since callbacks were last invoked. */
870 	size_t n_added;
871 	/** The number of bytes removed since callbacks were last invoked. */
872 	size_t n_deleted;
873 };
874 
875 /** Type definition for a callback that is invoked whenever data is added or
876     removed from an evbuffer.
877 
878     An evbuffer may have one or more callbacks set at a time.  The order
879     in which they are executed is undefined.
880 
881     A callback function may add more callbacks, or remove itself from the
882     list of callbacks, or add or remove data from the buffer.  It may not
883     remove another callback from the list.
884 
885     If a callback adds or removes data from the buffer or from another
886     buffer, this can cause a recursive invocation of your callback or
887     other callbacks.  If you ask for an infinite loop, you might just get
888     one: watch out!
889 
890     @param buffer the buffer whose size has changed
891     @param info a structure describing how the buffer changed.
892     @param arg a pointer to user data
893 */
894 typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
895 
896 struct evbuffer_cb_entry;
897 /** Add a new callback to an evbuffer.
898 
899   Subsequent calls to evbuffer_add_cb() add new callbacks.  To remove this
900   callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
901 
902   @param buffer the evbuffer to be monitored
903   @param cb the callback function to invoke when the evbuffer is modified,
904 	or NULL to remove all callbacks.
905   @param cbarg an argument to be provided to the callback function
906   @return a handle to the callback on success, or NULL on failure.
907  */
908 EVENT2_EXPORT_SYMBOL
909 struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
910 
911 /** Remove a callback from an evbuffer, given a handle returned from
912     evbuffer_add_cb.
913 
914     Calling this function invalidates the handle.
915 
916     @return 0 if a callback was removed, or -1 if no matching callback was
917     found.
918  */
919 EVENT2_EXPORT_SYMBOL
920 int evbuffer_remove_cb_entry(struct evbuffer *buffer,
921 			     struct evbuffer_cb_entry *ent);
922 
923 /** Remove a callback from an evbuffer, given the function and argument
924     used to add it.
925 
926     @return 0 if a callback was removed, or -1 if no matching callback was
927     found.
928  */
929 EVENT2_EXPORT_SYMBOL
930 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
931 
932 /** If this flag is not set, then a callback is temporarily disabled, and
933  * should not be invoked.
934  *
935  * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()
936  */
937 #define EVBUFFER_CB_ENABLED 1
938 
939 /** Change the flags that are set for a callback on a buffer by adding more.
940 
941     @param buffer the evbuffer that the callback is watching.
942     @param cb the callback whose status we want to change.
943     @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
944     @return 0 on success, -1 on failure.
945  */
946 EVENT2_EXPORT_SYMBOL
947 int evbuffer_cb_set_flags(struct evbuffer *buffer,
948 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
949 
950 /** Change the flags that are set for a callback on a buffer by removing some
951 
952     @param buffer the evbuffer that the callback is watching.
953     @param cb the callback whose status we want to change.
954     @param flags EVBUFFER_CB_ENABLED to disable the callback.
955     @return 0 on success, -1 on failure.
956  */
957 EVENT2_EXPORT_SYMBOL
958 int evbuffer_cb_clear_flags(struct evbuffer *buffer,
959 			  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
960 
961 #if 0
962 /** Postpone calling a given callback until unsuspend is called later.
963 
964     This is different from disabling the callback, since the callback will get
965 	invoked later if the buffer size changes between now and when we unsuspend
966 	it.
967 
968 	@param the buffer that the callback is watching.
969 	@param cb the callback we want to suspend.
970  */
971 EVENT2_EXPORT_SYMBOL
972 void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
973 /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
974 
975 	If data was added to or removed from the buffer while the callback was
976 	suspended, the callback will get called once now.
977 
978 	@param the buffer that the callback is watching.
979 	@param cb the callback we want to stop suspending.
980  */
981 EVENT2_EXPORT_SYMBOL
982 void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
983 #endif
984 
985 /**
986   Makes the data at the beginning of an evbuffer contiguous.
987 
988   @param buf the evbuffer to make contiguous
989   @param size the number of bytes to make contiguous, or -1 to make the
990 	entire buffer contiguous.
991   @return a pointer to the contiguous memory array, or NULL if param size
992 	requested more data than is present in the buffer.
993 */
994 
995 EVENT2_EXPORT_SYMBOL
996 unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
997 
998 /**
999   Prepends data to the beginning of the evbuffer
1000 
1001   @param buf the evbuffer to which to prepend data
1002   @param data a pointer to the memory to prepend
1003   @param size the number of bytes to prepend
1004   @return 0 if successful, or -1 otherwise
1005 */
1006 
1007 EVENT2_EXPORT_SYMBOL
1008 int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
1009 
1010 /**
1011   Prepends all data from the src evbuffer to the beginning of the dst
1012   evbuffer.
1013 
1014   @param dst the evbuffer to which to prepend data
1015   @param src the evbuffer to prepend; it will be emptied as a result
1016   @return 0 if successful, or -1 otherwise
1017 */
1018 EVENT2_EXPORT_SYMBOL
1019 int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
1020 
1021 /**
1022    Prevent calls that modify an evbuffer from succeeding. A buffer may
1023    frozen at the front, at the back, or at both the front and the back.
1024 
1025    If the front of a buffer is frozen, operations that drain data from
1026    the front of the buffer, or that prepend data to the buffer, will
1027    fail until it is unfrozen.   If the back a buffer is frozen, operations
1028    that append data from the buffer will fail until it is unfrozen.
1029 
1030    @param buf The buffer to freeze
1031    @param at_front If true, we freeze the front of the buffer.  If false,
1032       we freeze the back.
1033    @return 0 on success, -1 on failure.
1034 */
1035 EVENT2_EXPORT_SYMBOL
1036 int evbuffer_freeze(struct evbuffer *buf, int at_front);
1037 /**
1038    Re-enable calls that modify an evbuffer.
1039 
1040    @param buf The buffer to un-freeze
1041    @param at_front If true, we unfreeze the front of the buffer.  If false,
1042       we unfreeze the back.
1043    @return 0 on success, -1 on failure.
1044  */
1045 EVENT2_EXPORT_SYMBOL
1046 int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
1047 
1048 struct event_base;
1049 /**
1050    Force all the callbacks on an evbuffer to be run, not immediately after
1051    the evbuffer is altered, but instead from inside the event loop.
1052 
1053    This can be used to serialize all the callbacks to a single thread
1054    of execution.
1055  */
1056 EVENT2_EXPORT_SYMBOL
1057 int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
1058 
1059 /**
1060   Append data from 1 or more iovec's to an evbuffer
1061 
1062   Calculates the number of bytes needed for an iovec structure and guarantees
1063   all data will fit into a single chain. Can be used in lieu of functionality
1064   which calls evbuffer_add() constantly before being used to increase
1065   performance.
1066 
1067   @param buffer the destination buffer
1068   @param vec the source iovec
1069   @param n_vec the number of iovec structures.
1070   @return the number of bytes successfully written to the output buffer.
1071 */
1072 EVENT2_EXPORT_SYMBOL
1073 size_t evbuffer_add_iovec(struct evbuffer * buffer, struct evbuffer_iovec * vec, int n_vec);
1074 
1075 #ifdef __cplusplus
1076 }
1077 #endif
1078 
1079 #endif /* EVENT2_BUFFER_H_INCLUDED_ */
1080