buffer.h 38 KB

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