buffer.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * Copyright (c) 2007-2012 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. /** @file event2/buffer.h
  29. Functions for buffering data for network sending or receiving.
  30. An evbuffer can be used for preparing data before sending it to
  31. the network or conversely for reading data from the network.
  32. Evbuffers try to avoid memory copies as much as possible. As a
  33. result, evbuffers can be used to pass data around without actually
  34. incurring the overhead of copying the data.
  35. A new evbuffer can be allocated with evbuffer_new(), and can be
  36. freed with evbuffer_free(). Most users will be using evbuffers via
  37. the bufferevent interface. To access a bufferevent's evbuffers, use
  38. bufferevent_get_input() and bufferevent_get_output().
  39. There are several guidelines for using evbuffers.
  40. - if you already know how much data you are going to add as a result
  41. of calling evbuffer_add() multiple times, it makes sense to use
  42. evbuffer_expand() first to make sure that enough memory is allocated
  43. before hand.
  44. - evbuffer_add_buffer() adds the contents of one buffer to the other
  45. without incurring any unnecessary memory copies.
  46. - evbuffer_add() and evbuffer_add_buffer() do not mix very well:
  47. if you use them, you will wind up with fragmented memory in your
  48. buffer.
  49. - For high-performance code, you may want to avoid copying data into and out
  50. of buffers. You can skip the copy step by using
  51. evbuffer_reserve_space()/evbuffer_commit_space() when writing into a
  52. buffer, and evbuffer_peek() when reading.
  53. In Libevent 2.0 and later, evbuffers are represented using a linked
  54. list of memory chunks, with pointers to the first and last chunk in
  55. the chain.
  56. As the contents of an evbuffer can be stored in 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 contiguous. This
  59. will cause memory reallocation and memory copies if the data is split
  60. across multiple blocks. It is more efficient, however, to use
  61. evbuffer_peek() if you don't require that the memory to be contiguous.
  62. */
  63. #ifdef __cplusplus
  64. extern "C" {
  65. #endif
  66. #include <event2/event-config.h>
  67. #include <stdarg.h>
  68. #ifdef _EVENT_HAVE_SYS_TYPES_H
  69. #include <sys/types.h>
  70. #endif
  71. #ifdef _EVENT_HAVE_SYS_UIO_H
  72. #include <sys/uio.h>
  73. #endif
  74. #include <event2/util.h>
  75. /**
  76. An evbuffer is an opaque data type for efficiently buffering data to be
  77. sent or received on the network.
  78. @see event2/event.h for more information
  79. */
  80. struct evbuffer
  81. #ifdef _EVENT_IN_DOXYGEN
  82. {}
  83. #endif
  84. ;
  85. /**
  86. Pointer to a position within an evbuffer.
  87. Used when repeatedly searching through a buffer. Calling any function
  88. that modifies or re-packs the buffer contents may invalidate all
  89. evbuffer_ptrs for that buffer. Do not modify these values except with
  90. evbuffer_ptr_set.
  91. */
  92. struct evbuffer_ptr {
  93. ev_ssize_t pos;
  94. /* Do not alter the values of fields. */
  95. struct {
  96. void *chain;
  97. size_t pos_in_chain;
  98. } _internal;
  99. };
  100. /** Describes a single extent of memory inside an evbuffer. Used for
  101. direct-access functions.
  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. Allocate storage for a new evbuffer.
  118. @return a pointer to a newly allocated evbuffer struct, or NULL if an error
  119. occurred
  120. */
  121. struct evbuffer *evbuffer_new(void);
  122. /**
  123. Deallocate storage for an evbuffer.
  124. @param buf pointer to the evbuffer to be freed
  125. */
  126. void evbuffer_free(struct evbuffer *buf);
  127. /**
  128. Enable locking on an evbuffer so that it can safely be used by multiple
  129. threads at the same time.
  130. NOTE: when locking is enabled, the lock will be held when callbacks are
  131. invoked. This could result in deadlock if you aren't careful. Plan
  132. accordingly!
  133. @param buf An evbuffer to make lockable.
  134. @param lock A lock object, or NULL if we should allocate our own.
  135. @return 0 on success, -1 on failure.
  136. */
  137. int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
  138. /**
  139. Acquire the lock on an evbuffer. Has no effect if locking was not enabled
  140. with evbuffer_enable_locking.
  141. */
  142. void evbuffer_lock(struct evbuffer *buf);
  143. /**
  144. Release the lock on an evbuffer. Has no effect if locking was not enabled
  145. with evbuffer_enable_locking.
  146. */
  147. void evbuffer_unlock(struct evbuffer *buf);
  148. /** If this flag is set, then we will not use evbuffer_peek(),
  149. * evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes
  150. * from this buffer: we'll only take bytes out of this buffer by
  151. * writing them to the network (as with evbuffer_write_atmost), by
  152. * removing them without observing them (as with evbuffer_drain),
  153. * or by copying them all out at once (as with evbuffer_add_buffer).
  154. *
  155. * Using this option allows the implementation to use sendfile-based
  156. * operations for evbuffer_add_file(); see that function for more
  157. * information.
  158. *
  159. * This flag is on by default for bufferevents that can take advantage
  160. * of it; you should never actually need to set it on a bufferevent's
  161. * output buffer.
  162. */
  163. #define EVBUFFER_FLAG_DRAINS_TO_FD 1
  164. /** Change the flags that are set for an evbuffer by adding more.
  165. *
  166. * @param buffer the evbuffer that the callback is watching.
  167. * @param cb the callback whose status we want to change.
  168. * @param flags One or more EVBUFFER_FLAG_* options
  169. * @return 0 on success, -1 on failure.
  170. */
  171. int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
  172. /** Change the flags that are set for an evbuffer by removing some.
  173. *
  174. * @param buffer the evbuffer that the callback is watching.
  175. * @param cb the callback whose status we want to change.
  176. * @param flags One or more EVBUFFER_FLAG_* options
  177. * @return 0 on success, -1 on failure.
  178. */
  179. int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
  180. /**
  181. Returns the total number of bytes stored in the evbuffer
  182. @param buf pointer to the evbuffer
  183. @return the number of bytes stored in the evbuffer
  184. */
  185. size_t evbuffer_get_length(const struct evbuffer *buf);
  186. /**
  187. Returns the number of contiguous available bytes in the first buffer chain.
  188. This is useful when processing data that might be split into multiple
  189. chains, or that might all be in the first chain. Calls to
  190. evbuffer_pullup() that cause reallocation and copying of data can thus be
  191. avoided.
  192. @param buf pointer to the evbuffer
  193. @return 0 if no data is available, otherwise the number of available bytes
  194. in the first buffer chain.
  195. */
  196. size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
  197. /**
  198. Expands the available space in an evbuffer.
  199. Expands the available space in the evbuffer to at least datlen, so that
  200. appending datlen additional bytes will not require any new allocations.
  201. @param buf the evbuffer to be expanded
  202. @param datlen the new minimum length requirement
  203. @return 0 if successful, or -1 if an error occurred
  204. */
  205. int evbuffer_expand(struct evbuffer *buf, size_t datlen);
  206. /**
  207. Reserves space in the last chain or chains of an evbuffer.
  208. Makes space available in the last chain or chains of an evbuffer that can
  209. be arbitrarily written to by a user. The space does not become
  210. available for reading until it has been committed with
  211. evbuffer_commit_space().
  212. The space is made available as one or more extents, represented by
  213. an initial pointer and a length. You can force the memory to be
  214. available as only one extent. Allowing more extents, however, makes the
  215. function more efficient.
  216. Multiple subsequent calls to this function will make the same space
  217. available until evbuffer_commit_space() has been called.
  218. It is an error to do anything that moves around the buffer's internal
  219. memory structures before committing the space.
  220. NOTE: The code currently does not ever use more than two extents.
  221. This may change in future versions.
  222. @param buf the evbuffer in which to reserve space.
  223. @param size how much space to make available, at minimum. The
  224. total length of the extents may be greater than the requested
  225. length.
  226. @param vec an array of one or more evbuffer_iovec structures to
  227. hold pointers to the reserved extents of memory.
  228. @param n_vec The length of the vec array. Must be at least 1;
  229. 2 is more efficient.
  230. @return the number of provided extents, or -1 on error.
  231. @see evbuffer_commit_space()
  232. */
  233. int
  234. evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
  235. struct evbuffer_iovec *vec, int n_vec);
  236. /**
  237. Commits previously reserved space.
  238. Commits some of the space previously reserved with
  239. evbuffer_reserve_space(). It then becomes available for reading.
  240. This function may return an error if the pointer in the extents do
  241. not match those returned from evbuffer_reserve_space, or if data
  242. has been added to the buffer since the space was reserved.
  243. If you want to commit less data than you got reserved space for,
  244. modify the iov_len pointer of the appropriate extent to a smaller
  245. value. Note that you may have received more space than you
  246. requested if it was available!
  247. @param buf the evbuffer in which to reserve space.
  248. @param vec one or two extents returned by evbuffer_reserve_space.
  249. @param n_vecs the number of extents.
  250. @return 0 on success, -1 on error
  251. @see evbuffer_reserve_space()
  252. */
  253. int evbuffer_commit_space(struct evbuffer *buf,
  254. struct evbuffer_iovec *vec, int n_vecs);
  255. /**
  256. Append data to the end of an evbuffer.
  257. @param buf the evbuffer to be appended to
  258. @param data pointer to the beginning of the data buffer
  259. @param datlen the number of bytes to be copied from the data buffer
  260. @return 0 on success, -1 on failure.
  261. */
  262. int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
  263. /**
  264. Read data from an evbuffer and drain the bytes read.
  265. If more bytes are requested than are available in the evbuffer, we
  266. only extract as many bytes as were available.
  267. @param buf the evbuffer 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. Read data from an evbuffer, and leave the buffer unchanged.
  275. If more bytes are requested than are available in the evbuffer, we
  276. only extract as many bytes as were available.
  277. @param buf the evbuffer to be read from
  278. @param data_out 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. Read data from an evbuffer into another evbuffer, draining
  285. the bytes from the source buffer. This function avoids copy
  286. operations to the extent possible.
  287. If more bytes are requested than are available in src, the src
  288. buffer is drained completely.
  289. @param src the evbuffer to be read from
  290. @param dst the destination evbuffer 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. /** Used to tell evbuffer_readln what kind of line-ending to look for.
  297. */
  298. enum evbuffer_eol_style {
  299. /** Any sequence of CR and LF characters is acceptable as an
  300. * EOL.
  301. *
  302. * Note that this style can produce ambiguous results: the
  303. * sequence "CRLF" will be treated as a single EOL if it is
  304. * all in the buffer at once, but if you first read a CR from
  305. * the network and later read an LF from the network, it will
  306. * be treated as two EOLs.
  307. */
  308. EVBUFFER_EOL_ANY,
  309. /** An EOL is an LF, optionally preceded by a CR. This style is
  310. * most useful for implementing text-based internet protocols. */
  311. EVBUFFER_EOL_CRLF,
  312. /** An EOL is a CR followed by an LF. */
  313. EVBUFFER_EOL_CRLF_STRICT,
  314. /** An EOL is a LF. */
  315. EVBUFFER_EOL_LF
  316. };
  317. /**
  318. * Read a single line from an evbuffer.
  319. *
  320. * Reads a line terminated by an EOL as determined by the evbuffer_eol_style
  321. * argument. Returns a newly allocated nul-terminated string; the caller must
  322. * free the returned value. The EOL is not included in the returned string.
  323. *
  324. * @param buffer the evbuffer to read from
  325. * @param n_read_out if non-NULL, points to a size_t that is set to the
  326. * number of characters in the returned string. This is useful for
  327. * strings that can contain NUL characters.
  328. * @param eol_style the style of line-ending to use.
  329. * @return pointer to a single line, or NULL if an error occurred
  330. */
  331. char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
  332. enum evbuffer_eol_style eol_style);
  333. /**
  334. Move all data from one evbuffer into another evbuffer.
  335. This is a destructive add. The data from one buffer moves into
  336. the other buffer. However, no unnecessary memory copies occur.
  337. @param outbuf the output buffer
  338. @param inbuf the input buffer
  339. @return 0 if successful, or -1 if an error occurred
  340. @see evbuffer_remove_buffer()
  341. */
  342. int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
  343. /**
  344. A cleanup function for a piece of memory added to an evbuffer by
  345. reference.
  346. @see evbuffer_add_reference()
  347. */
  348. typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
  349. size_t datalen, void *extra);
  350. /**
  351. Reference memory into an evbuffer without copying.
  352. The memory needs to remain valid until all the added data has been
  353. read. This function keeps just a reference to the memory without
  354. actually incurring the overhead of a copy.
  355. @param outbuf the output buffer
  356. @param data the memory to reference
  357. @param datlen how memory to reference
  358. @param cleanupfn callback to be invoked when the memory is no longer
  359. referenced by this evbuffer.
  360. @param cleanupfn_arg optional argument to the cleanup callback
  361. @return 0 if successful, or -1 if an error occurred
  362. */
  363. int evbuffer_add_reference(struct evbuffer *outbuf,
  364. const void *data, size_t datlen,
  365. evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
  366. /**
  367. Copy data from a file into the evbuffer for writing to a socket.
  368. This function avoids unnecessary data copies between userland and
  369. kernel. If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD
  370. flag is set, it uses those functions. Otherwise, it tries to use
  371. mmap (or CreateFileMapping on Windows).
  372. The function owns the resulting file descriptor and will close it
  373. when finished transferring data.
  374. The results of using evbuffer_remove() or evbuffer_pullup() on
  375. evbuffers whose data was added using this function are undefined.
  376. @param outbuf the output buffer
  377. @param fd the file descriptor
  378. @param offset the offset from which to read data
  379. @param length how much data to read
  380. @return 0 if successful, or -1 if an error occurred
  381. */
  382. int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
  383. ev_off_t length);
  384. /**
  385. Append a formatted string to the end of an evbuffer.
  386. The string is formated as printf.
  387. @param buf the evbuffer that will be appended to
  388. @param fmt a format string
  389. @param ... arguments that will be passed to printf(3)
  390. @return The number of bytes added if successful, or -1 if an error occurred.
  391. @see evutil_printf(), evbuffer_add_vprintf()
  392. */
  393. int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
  394. #ifdef __GNUC__
  395. __attribute__((format(printf, 2, 3)))
  396. #endif
  397. ;
  398. /**
  399. Append a va_list formatted string to the end of an evbuffer.
  400. @param buf the evbuffer that will be appended to
  401. @param fmt a format string
  402. @param ap a varargs va_list argument array that will be passed to vprintf(3)
  403. @return The number of bytes added if successful, or -1 if an error occurred.
  404. */
  405. int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
  406. #ifdef __GNUC__
  407. __attribute__((format(printf, 2, 0)))
  408. #endif
  409. ;
  410. /**
  411. Remove a specified number of bytes data from the beginning of an evbuffer.
  412. @param buf the evbuffer to be drained
  413. @param len the number of bytes to drain from the beginning of the buffer
  414. @return 0 on success, -1 on failure.
  415. */
  416. int evbuffer_drain(struct evbuffer *buf, size_t len);
  417. /**
  418. Write the contents of an evbuffer to a file descriptor.
  419. The evbuffer will be drained after the bytes have been successfully written.
  420. @param buffer the evbuffer to be written and drained
  421. @param fd the file descriptor to be written to
  422. @return the number of bytes written, or -1 if an error occurred
  423. @see evbuffer_read()
  424. */
  425. int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
  426. /**
  427. Write some of the contents of an evbuffer to a file descriptor.
  428. The evbuffer will be drained after the bytes have been successfully written.
  429. @param buffer the evbuffer to be written and drained
  430. @param fd the file descriptor to be written to
  431. @param howmuch the largest allowable number of bytes to write, or -1
  432. to write as many bytes as we can.
  433. @return the number of bytes written, or -1 if an error occurred
  434. @see evbuffer_read()
  435. */
  436. int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
  437. ev_ssize_t howmuch);
  438. /**
  439. Read from a file descriptor and store the result in an evbuffer.
  440. @param buffer the evbuffer to store the result
  441. @param fd the file descriptor to read from
  442. @param howmuch the number of bytes to be read
  443. @return the number of bytes read, or -1 if an error occurred
  444. @see evbuffer_write()
  445. */
  446. int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
  447. /**
  448. Search for a string within an evbuffer.
  449. @param buffer the evbuffer to be searched
  450. @param what the string to be searched for
  451. @param len the length of the search string
  452. @param start NULL or a pointer to a valid struct evbuffer_ptr.
  453. @return a struct evbuffer_ptr whose 'pos' field has the offset of the
  454. first occurrence of the string in the buffer after 'start'. The 'pos'
  455. field of the result is -1 if the string was not found.
  456. */
  457. struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
  458. /**
  459. Search for a string within part of an evbuffer.
  460. @param buffer the evbuffer to be searched
  461. @param what the string to be searched for
  462. @param len the length of the search string
  463. @param start NULL or a pointer to a valid struct evbuffer_ptr that
  464. indicates where we should start searching.
  465. @param end NULL or a pointer to a valid struct evbuffer_ptr that
  466. indicates where we should stop searching.
  467. @return a struct evbuffer_ptr whose 'pos' field has the offset of the
  468. first occurrence of the string in the buffer after 'start'. The 'pos'
  469. field of the result is -1 if the string was not found.
  470. */
  471. 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);
  472. /**
  473. Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set()
  474. @see evbuffer_ptr_set() */
  475. enum evbuffer_ptr_how {
  476. /** Sets the pointer to the position; can be called on with an
  477. uninitialized evbuffer_ptr. */
  478. EVBUFFER_PTR_SET,
  479. /** Advances the pointer by adding to the current position. */
  480. EVBUFFER_PTR_ADD
  481. };
  482. /**
  483. Sets the search pointer in the buffer to position.
  484. If evbuffer_ptr is not initialized. This function can only be called
  485. with EVBUFFER_PTR_SET.
  486. @param buffer the evbuffer to be search
  487. @param ptr a pointer to a struct evbuffer_ptr
  488. @param position the position at which to start the next search
  489. @param how determines how the pointer should be manipulated.
  490. @returns 0 on success or -1 otherwise
  491. */
  492. int
  493. evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
  494. size_t position, enum evbuffer_ptr_how how);
  495. /**
  496. Search for an end-of-line string within an evbuffer.
  497. @param buffer the evbuffer to be searched
  498. @param start NULL or a pointer to a valid struct evbuffer_ptr to start
  499. searching at.
  500. @param eol_len_out If non-NULL, the pointed-to value will be set to
  501. the length of the end-of-line string.
  502. @param eol_style The kind of EOL to look for; see evbuffer_readln() for
  503. more information
  504. @return a struct evbuffer_ptr whose 'pos' field has the offset of the
  505. first occurrence EOL in the buffer after 'start'. The 'pos'
  506. field of the result is -1 if the string was not found.
  507. */
  508. struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
  509. struct evbuffer_ptr *start, size_t *eol_len_out,
  510. enum evbuffer_eol_style eol_style);
  511. /** Function to peek at data inside an evbuffer without removing it or
  512. copying it out.
  513. Pointers to the data are returned by filling the 'vec_out' array
  514. with pointers to one or more extents of data inside the buffer.
  515. The total data in the extents that you get back may be more than
  516. you requested (if there is more data last extent than you asked
  517. for), or less (if you do not provide enough evbuffer_iovecs, or if
  518. the buffer does not have as much data as you asked to see).
  519. @param buffer the evbuffer to peek into,
  520. @param len the number of bytes to try to peek. If len is negative, we
  521. will try to fill as much of vec_out as we can. If len is negative
  522. and vec_out is not provided, we return the number of evbuffer_iovecs
  523. that would be needed to get all the data in the buffer.
  524. @param start_at an evbuffer_ptr indicating the point at which we
  525. should start looking for data. NULL means, "At the start of the
  526. buffer."
  527. @param vec_out an array of evbuffer_iovec
  528. @param n_vec the length of vec_out. If 0, we only count how many
  529. extents would be necessary to point to the requested amount of
  530. data.
  531. @return The number of extents needed. This may be less than n_vec
  532. if we didn't need all the evbuffer_iovecs we were given, or more
  533. than n_vec if we would need more to return all the data that was
  534. requested.
  535. */
  536. int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
  537. struct evbuffer_ptr *start_at,
  538. struct evbuffer_iovec *vec_out, int n_vec);
  539. /** Structure passed to an evbuffer_cb_func evbuffer callback
  540. @see evbuffer_cb_func, evbuffer_add_cb()
  541. */
  542. struct evbuffer_cb_info {
  543. /** The number of bytes in this evbuffer when callbacks were last
  544. * invoked. */
  545. size_t orig_size;
  546. /** The number of bytes added since callbacks were last invoked. */
  547. size_t n_added;
  548. /** The number of bytes removed since callbacks were last invoked. */
  549. size_t n_deleted;
  550. };
  551. /** Type definition for a callback that is invoked whenever data is added or
  552. removed from an evbuffer.
  553. An evbuffer may have one or more callbacks set at a time. The order
  554. in which they are executed is undefined.
  555. A callback function may add more callbacks, or remove itself from the
  556. list of callbacks, or add or remove data from the buffer. It may not
  557. remove another callback from the list.
  558. If a callback adds or removes data from the buffer or from another
  559. buffer, this can cause a recursive invocation of your callback or
  560. other callbacks. If you ask for an infinite loop, you might just get
  561. one: watch out!
  562. @param buffer the buffer whose size has changed
  563. @param info a structure describing how the buffer changed.
  564. @param arg a pointer to user data
  565. */
  566. typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
  567. struct evbuffer_cb_entry;
  568. /** Add a new callback to an evbuffer.
  569. Subsequent calls to evbuffer_add_cb() add new callbacks. To remove this
  570. callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
  571. @param buffer the evbuffer to be monitored
  572. @param cb the callback function to invoke when the evbuffer is modified,
  573. or NULL to remove all callbacks.
  574. @param cbarg an argument to be provided to the callback function
  575. @return a handle to the callback on success, or NULL on failure.
  576. */
  577. struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
  578. /** Remove a callback from an evbuffer, given a handle returned from
  579. evbuffer_add_cb.
  580. Calling this function invalidates the handle.
  581. @return 0 if a callback was removed, or -1 if no matching callback was
  582. found.
  583. */
  584. int evbuffer_remove_cb_entry(struct evbuffer *buffer,
  585. struct evbuffer_cb_entry *ent);
  586. /** Remove a callback from an evbuffer, given the function and argument
  587. used to add it.
  588. @return 0 if a callback was removed, or -1 if no matching callback was
  589. found.
  590. */
  591. int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
  592. /** If this flag is not set, then a callback is temporarily disabled, and
  593. * should not be invoked.
  594. *
  595. * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags()
  596. */
  597. #define EVBUFFER_CB_ENABLED 1
  598. /** Change the flags that are set for a callback on a buffer by adding more.
  599. @param buffer the evbuffer that the callback is watching.
  600. @param cb the callback whose status we want to change.
  601. @param flags EVBUFFER_CB_ENABLED to re-enable the callback.
  602. @return 0 on success, -1 on failure.
  603. */
  604. int evbuffer_cb_set_flags(struct evbuffer *buffer,
  605. struct evbuffer_cb_entry *cb, ev_uint32_t flags);
  606. /** Change the flags that are set for a callback on a buffer by removing some
  607. @param buffer the evbuffer that the callback is watching.
  608. @param cb the callback whose status we want to change.
  609. @param flags EVBUFFER_CB_ENABLED to disable the callback.
  610. @return 0 on success, -1 on failure.
  611. */
  612. int evbuffer_cb_clear_flags(struct evbuffer *buffer,
  613. struct evbuffer_cb_entry *cb, ev_uint32_t flags);
  614. #if 0
  615. /** Postpone calling a given callback until unsuspend is called later.
  616. This is different from disabling the callback, since the callback will get
  617. invoked later if the buffer size changes between now and when we unsuspend
  618. it.
  619. @param the buffer that the callback is watching.
  620. @param cb the callback we want to suspend.
  621. */
  622. void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
  623. /** Stop postponing a callback that we postponed with evbuffer_cb_suspend.
  624. If data was added to or removed from the buffer while the callback was
  625. suspended, the callback will get called once now.
  626. @param the buffer that the callback is watching.
  627. @param cb the callback we want to stop suspending.
  628. */
  629. void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
  630. #endif
  631. /**
  632. Makes the data at the beginning of an evbuffer contiguous.
  633. @param buf the evbuffer to make contiguous
  634. @param size the number of bytes to make contiguous, or -1 to make the
  635. entire buffer contiguous.
  636. @return a pointer to the contiguous memory array
  637. */
  638. unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
  639. /**
  640. Prepends data to the beginning of the evbuffer
  641. @param buf the evbuffer to which to prepend data
  642. @param data a pointer to the memory to prepend
  643. @param size the number of bytes to prepend
  644. @return 0 if successful, or -1 otherwise
  645. */
  646. int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
  647. /**
  648. Prepends all data from the src evbuffer to the beginning of the dst
  649. evbuffer.
  650. @param dst the evbuffer to which to prepend data
  651. @param src the evbuffer to prepend; it will be emptied as a result
  652. @return 0 if successful, or -1 otherwise
  653. */
  654. int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
  655. /**
  656. Prevent calls that modify an evbuffer from succeeding. A buffer may
  657. frozen at the front, at the back, or at both the front and the back.
  658. If the front of a buffer is frozen, operations that drain data from
  659. the front of the buffer, or that prepend data to the buffer, will
  660. fail until it is unfrozen. If the back a buffer is frozen, operations
  661. that append data from the buffer will fail until it is unfrozen.
  662. @param buf The buffer to freeze
  663. @param at_front If true, we freeze the front of the buffer. If false,
  664. we freeze the back.
  665. @return 0 on success, -1 on failure.
  666. */
  667. int evbuffer_freeze(struct evbuffer *buf, int at_front);
  668. /**
  669. Re-enable calls that modify an evbuffer.
  670. @param buf The buffer to un-freeze
  671. @param at_front If true, we unfreeze the front of the buffer. If false,
  672. we unfreeze the back.
  673. @return 0 on success, -1 on failure.
  674. */
  675. int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
  676. struct event_base;
  677. /**
  678. Force all the callbacks on an evbuffer to be run, not immediately after
  679. the evbuffer is altered, but instead from inside the event loop.
  680. This can be used to serialize all the callbacks to a single thread
  681. of execution.
  682. */
  683. int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
  684. #ifdef __cplusplus
  685. }
  686. #endif
  687. #endif /* _EVENT2_BUFFER_H_ */