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