1*9034ec65Schristos /* $NetBSD: bufferevent.h,v 1.6 2020/05/25 20:47:34 christos Exp $ */ 22b3787f6Schristos 32b3787f6Schristos /* 42b3787f6Schristos * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 52b3787f6Schristos * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 62b3787f6Schristos * 72b3787f6Schristos * Redistribution and use in source and binary forms, with or without 82b3787f6Schristos * modification, are permitted provided that the following conditions 92b3787f6Schristos * are met: 102b3787f6Schristos * 1. Redistributions of source code must retain the above copyright 112b3787f6Schristos * notice, this list of conditions and the following disclaimer. 122b3787f6Schristos * 2. Redistributions in binary form must reproduce the above copyright 132b3787f6Schristos * notice, this list of conditions and the following disclaimer in the 142b3787f6Schristos * documentation and/or other materials provided with the distribution. 152b3787f6Schristos * 3. The name of the author may not be used to endorse or promote products 162b3787f6Schristos * derived from this software without specific prior written permission. 172b3787f6Schristos * 182b3787f6Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 192b3787f6Schristos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 202b3787f6Schristos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 212b3787f6Schristos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 222b3787f6Schristos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 232b3787f6Schristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 242b3787f6Schristos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 252b3787f6Schristos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 262b3787f6Schristos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 272b3787f6Schristos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 282b3787f6Schristos */ 292b3787f6Schristos #ifndef EVENT2_BUFFEREVENT_H_INCLUDED_ 302b3787f6Schristos #define EVENT2_BUFFEREVENT_H_INCLUDED_ 312b3787f6Schristos 322b3787f6Schristos /** 332b3787f6Schristos @file event2/bufferevent.h 342b3787f6Schristos 352b3787f6Schristos Functions for buffering data for network sending or receiving. Bufferevents 362b3787f6Schristos are higher level than evbuffers: each has an underlying evbuffer for reading 372b3787f6Schristos and one for writing, and callbacks that are invoked under certain 382b3787f6Schristos circumstances. 392b3787f6Schristos 402b3787f6Schristos A bufferevent provides input and output buffers that get filled and 412b3787f6Schristos drained automatically. The user of a bufferevent no longer deals 422b3787f6Schristos directly with the I/O, but instead is reading from input and writing 432b3787f6Schristos to output buffers. 442b3787f6Schristos 452b3787f6Schristos Once initialized, the bufferevent structure can be used repeatedly 462b3787f6Schristos with bufferevent_enable() and bufferevent_disable(). 472b3787f6Schristos 482b3787f6Schristos When reading is enabled, the bufferevent will try to read from the 491b6f2cd4Schristos file descriptor onto its input buffer, and call the read callback. 502b3787f6Schristos When writing is enabled, the bufferevent will try to write data onto its 511b6f2cd4Schristos file descriptor when the output buffer has enough data, and call the write 521b6f2cd4Schristos callback when the output buffer is sufficiently drained. 532b3787f6Schristos 542b3787f6Schristos Bufferevents come in several flavors, including: 552b3787f6Schristos 562b3787f6Schristos <dl> 572b3787f6Schristos <dt>Socket-based bufferevents</dt> 582b3787f6Schristos <dd>A bufferevent that reads and writes data onto a network 592b3787f6Schristos socket. Created with bufferevent_socket_new().</dd> 602b3787f6Schristos 612b3787f6Schristos <dt>Paired bufferevents</dt> 622b3787f6Schristos <dd>A pair of bufferevents that send and receive data to one 632b3787f6Schristos another without touching the network. Created with 642b3787f6Schristos bufferevent_pair_new().</dd> 652b3787f6Schristos 662b3787f6Schristos <dt>Filtering bufferevents</dt> 672b3787f6Schristos <dd>A bufferevent that transforms data, and sends or receives it 682b3787f6Schristos over another underlying bufferevent. Created with 692b3787f6Schristos bufferevent_filter_new().</dd> 702b3787f6Schristos 712b3787f6Schristos <dt>SSL-backed bufferevents</dt> 722b3787f6Schristos <dd>A bufferevent that uses the openssl library to send and 732b3787f6Schristos receive data over an encrypted connection. Created with 742b3787f6Schristos bufferevent_openssl_socket_new() or 752b3787f6Schristos bufferevent_openssl_filter_new().</dd> 762b3787f6Schristos </dl> 772b3787f6Schristos */ 782b3787f6Schristos 791b6f2cd4Schristos #include <event2/visibility.h> 801b6f2cd4Schristos 812b3787f6Schristos #ifdef __cplusplus 822b3787f6Schristos extern "C" { 832b3787f6Schristos #endif 842b3787f6Schristos 852b3787f6Schristos #include <event2/event-config.h> 862b3787f6Schristos #ifdef EVENT__HAVE_SYS_TYPES_H 872b3787f6Schristos #include <sys/types.h> 882b3787f6Schristos #endif 892b3787f6Schristos #ifdef EVENT__HAVE_SYS_TIME_H 902b3787f6Schristos #include <sys/time.h> 912b3787f6Schristos #endif 922b3787f6Schristos 932b3787f6Schristos /* For int types. */ 942b3787f6Schristos #include <event2/util.h> 952b3787f6Schristos 962b3787f6Schristos /** @name Bufferevent event codes 972b3787f6Schristos 982b3787f6Schristos These flags are passed as arguments to a bufferevent's event callback. 992b3787f6Schristos 1002b3787f6Schristos @{ 1012b3787f6Schristos */ 1022b3787f6Schristos #define BEV_EVENT_READING 0x01 /**< error encountered while reading */ 1032b3787f6Schristos #define BEV_EVENT_WRITING 0x02 /**< error encountered while writing */ 1042b3787f6Schristos #define BEV_EVENT_EOF 0x10 /**< eof file reached */ 1052b3787f6Schristos #define BEV_EVENT_ERROR 0x20 /**< unrecoverable error encountered */ 1062b3787f6Schristos #define BEV_EVENT_TIMEOUT 0x40 /**< user-specified timeout reached */ 1072b3787f6Schristos #define BEV_EVENT_CONNECTED 0x80 /**< connect operation finished. */ 1082b3787f6Schristos /**@}*/ 1092b3787f6Schristos 1102b3787f6Schristos /** 1112b3787f6Schristos An opaque type for handling buffered IO 1122b3787f6Schristos 1132b3787f6Schristos @see event2/bufferevent.h 1142b3787f6Schristos */ 1152b3787f6Schristos struct bufferevent 1162b3787f6Schristos #ifdef EVENT_IN_DOXYGEN_ 1172b3787f6Schristos {} 1182b3787f6Schristos #endif 1192b3787f6Schristos ; 1202b3787f6Schristos struct event_base; 1212b3787f6Schristos struct evbuffer; 1222b3787f6Schristos struct sockaddr; 1232b3787f6Schristos 1242b3787f6Schristos /** 1252b3787f6Schristos A read or write callback for a bufferevent. 1262b3787f6Schristos 1272b3787f6Schristos The read callback is triggered when new data arrives in the input 1282b3787f6Schristos buffer and the amount of readable data exceed the low watermark 1292b3787f6Schristos which is 0 by default. 1302b3787f6Schristos 1312b3787f6Schristos The write callback is triggered if the write buffer has been 1322b3787f6Schristos exhausted or fell below its low watermark. 1332b3787f6Schristos 1342b3787f6Schristos @param bev the bufferevent that triggered the callback 1352b3787f6Schristos @param ctx the user-specified context for this bufferevent 1362b3787f6Schristos */ 1372b3787f6Schristos typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx); 1382b3787f6Schristos 1392b3787f6Schristos /** 1402b3787f6Schristos An event/error callback for a bufferevent. 1412b3787f6Schristos 1422b3787f6Schristos The event callback is triggered if either an EOF condition or another 1432b3787f6Schristos unrecoverable error was encountered. 1442b3787f6Schristos 1451b6f2cd4Schristos For bufferevents with deferred callbacks, this is a bitwise OR of all errors 1461b6f2cd4Schristos that have happened on the bufferevent since the last callback invocation. 1471b6f2cd4Schristos 1482b3787f6Schristos @param bev the bufferevent for which the error condition was reached 1492b3787f6Schristos @param what a conjunction of flags: BEV_EVENT_READING or BEV_EVENT_WRITING 1502b3787f6Schristos to indicate if the error was encountered on the read or write path, 1512b3787f6Schristos and one of the following flags: BEV_EVENT_EOF, BEV_EVENT_ERROR, 1522b3787f6Schristos BEV_EVENT_TIMEOUT, BEV_EVENT_CONNECTED. 1532b3787f6Schristos 1542b3787f6Schristos @param ctx the user-specified context for this bufferevent 1552b3787f6Schristos */ 1562b3787f6Schristos typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx); 1572b3787f6Schristos 1582b3787f6Schristos /** Options that can be specified when creating a bufferevent */ 1592b3787f6Schristos enum bufferevent_options { 1602b3787f6Schristos /** If set, we close the underlying file 1612b3787f6Schristos * descriptor/bufferevent/whatever when this bufferevent is freed. */ 1622b3787f6Schristos BEV_OPT_CLOSE_ON_FREE = (1<<0), 1632b3787f6Schristos 1642b3787f6Schristos /** If set, and threading is enabled, operations on this bufferevent 1652b3787f6Schristos * are protected by a lock */ 1662b3787f6Schristos BEV_OPT_THREADSAFE = (1<<1), 1672b3787f6Schristos 1682b3787f6Schristos /** If set, callbacks are run deferred in the event loop. */ 1692b3787f6Schristos BEV_OPT_DEFER_CALLBACKS = (1<<2), 1702b3787f6Schristos 1712b3787f6Schristos /** If set, callbacks are executed without locks being held on the 1722b3787f6Schristos * bufferevent. This option currently requires that 1732b3787f6Schristos * BEV_OPT_DEFER_CALLBACKS also be set; a future version of Libevent 1742b3787f6Schristos * might remove the requirement.*/ 1752b3787f6Schristos BEV_OPT_UNLOCK_CALLBACKS = (1<<3) 1762b3787f6Schristos }; 1772b3787f6Schristos 1782b3787f6Schristos /** 1792b3787f6Schristos Create a new socket bufferevent over an existing socket. 1802b3787f6Schristos 1812b3787f6Schristos @param base the event base to associate with the new bufferevent. 1822b3787f6Schristos @param fd the file descriptor from which data is read and written to. 1832b3787f6Schristos This file descriptor is not allowed to be a pipe(2). 1842b3787f6Schristos It is safe to set the fd to -1, so long as you later 1852b3787f6Schristos set it with bufferevent_setfd or bufferevent_socket_connect(). 1862b3787f6Schristos @param options Zero or more BEV_OPT_* flags 1872b3787f6Schristos @return a pointer to a newly allocated bufferevent struct, or NULL if an 1882b3787f6Schristos error occurred 1892b3787f6Schristos @see bufferevent_free() 1902b3787f6Schristos */ 1911b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 1922b3787f6Schristos struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options); 1932b3787f6Schristos 1942b3787f6Schristos /** 1952b3787f6Schristos Launch a connect() attempt with a socket-based bufferevent. 1962b3787f6Schristos 1972b3787f6Schristos When the connect succeeds, the eventcb will be invoked with 1982b3787f6Schristos BEV_EVENT_CONNECTED set. 1992b3787f6Schristos 2002b3787f6Schristos If the bufferevent does not already have a socket set, we allocate a new 2012b3787f6Schristos socket here and make it nonblocking before we begin. 2022b3787f6Schristos 2032b3787f6Schristos If no address is provided, we assume that the socket is already connecting, 2042b3787f6Schristos and configure the bufferevent so that a BEV_EVENT_CONNECTED event will be 2052b3787f6Schristos yielded when it is done connecting. 2062b3787f6Schristos 2072b3787f6Schristos @param bufev an existing bufferevent allocated with 2082b3787f6Schristos bufferevent_socket_new(). 2092b3787f6Schristos @param addr the address we should connect to 2102b3787f6Schristos @param socklen The length of the address 2112b3787f6Schristos @return 0 on success, -1 on failure. 2122b3787f6Schristos */ 2131b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2142b3787f6Schristos int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int); 2152b3787f6Schristos 2162b3787f6Schristos struct evdns_base; 2172b3787f6Schristos /** 2182b3787f6Schristos Resolve the hostname 'hostname' and connect to it as with 2192b3787f6Schristos bufferevent_socket_connect(). 2202b3787f6Schristos 2212b3787f6Schristos @param bufev An existing bufferevent allocated with bufferevent_socket_new() 2222b3787f6Schristos @param evdns_base Optionally, an evdns_base to use for resolving hostnames 2232b3787f6Schristos asynchronously. May be set to NULL for a blocking resolve. 2242b3787f6Schristos @param family A preferred address family to resolve addresses to, or 2252b3787f6Schristos AF_UNSPEC for no preference. Only AF_INET, AF_INET6, and AF_UNSPEC are 2262b3787f6Schristos supported. 2272b3787f6Schristos @param hostname The hostname to resolve; see below for notes on recognized 2282b3787f6Schristos formats 2292b3787f6Schristos @param port The port to connect to on the resolved address. 2302b3787f6Schristos @return 0 if successful, -1 on failure. 2312b3787f6Schristos 2322b3787f6Schristos Recognized hostname formats are: 2332b3787f6Schristos 2342b3787f6Schristos www.example.com (hostname) 2352b3787f6Schristos 1.2.3.4 (ipv4address) 2362b3787f6Schristos ::1 (ipv6address) 2372b3787f6Schristos [::1] ([ipv6address]) 2382b3787f6Schristos 2392b3787f6Schristos Performance note: If you do not provide an evdns_base, this function 2402b3787f6Schristos may block while it waits for a DNS response. This is probably not 2412b3787f6Schristos what you want. 2422b3787f6Schristos */ 2431b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2442b3787f6Schristos int bufferevent_socket_connect_hostname(struct bufferevent *, 2452b3787f6Schristos struct evdns_base *, int, const char *, int); 2462b3787f6Schristos 2472b3787f6Schristos /** 2482b3787f6Schristos Return the error code for the last failed DNS lookup attempt made by 2492b3787f6Schristos bufferevent_socket_connect_hostname(). 2502b3787f6Schristos 2512b3787f6Schristos @param bev The bufferevent object. 2522b3787f6Schristos @return DNS error code. 2532b3787f6Schristos @see evutil_gai_strerror() 2542b3787f6Schristos */ 2551b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2562b3787f6Schristos int bufferevent_socket_get_dns_error(struct bufferevent *bev); 2572b3787f6Schristos 2582b3787f6Schristos /** 2592b3787f6Schristos Assign a bufferevent to a specific event_base. 2602b3787f6Schristos 2612b3787f6Schristos NOTE that only socket bufferevents support this function. 2622b3787f6Schristos 2632b3787f6Schristos @param base an event_base returned by event_init() 2642b3787f6Schristos @param bufev a bufferevent struct returned by bufferevent_new() 2652b3787f6Schristos or bufferevent_socket_new() 2662b3787f6Schristos @return 0 if successful, or -1 if an error occurred 2672b3787f6Schristos @see bufferevent_new() 2682b3787f6Schristos */ 2691b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2702b3787f6Schristos int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); 2712b3787f6Schristos 2722b3787f6Schristos /** 2732b3787f6Schristos Return the event_base used by a bufferevent 2742b3787f6Schristos */ 2751b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2762b3787f6Schristos struct event_base *bufferevent_get_base(struct bufferevent *bev); 2772b3787f6Schristos 2782b3787f6Schristos /** 2792b3787f6Schristos Assign a priority to a bufferevent. 2802b3787f6Schristos 2812b3787f6Schristos Only supported for socket bufferevents. 2822b3787f6Schristos 2832b3787f6Schristos @param bufev a bufferevent struct 2842b3787f6Schristos @param pri the priority to be assigned 2852b3787f6Schristos @return 0 if successful, or -1 if an error occurred 2862b3787f6Schristos */ 2871b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2882b3787f6Schristos int bufferevent_priority_set(struct bufferevent *bufev, int pri); 2892b3787f6Schristos 2902b3787f6Schristos /** 2912b3787f6Schristos Return the priority of a bufferevent. 2922b3787f6Schristos 2932b3787f6Schristos Only supported for socket bufferevents 2942b3787f6Schristos */ 2951b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 2962b3787f6Schristos int bufferevent_get_priority(const struct bufferevent *bufev); 2972b3787f6Schristos 2982b3787f6Schristos /** 2992b3787f6Schristos Deallocate the storage associated with a bufferevent structure. 3002b3787f6Schristos 3012b3787f6Schristos If there is pending data to write on the bufferevent, it probably won't be 3022b3787f6Schristos flushed before the bufferevent is freed. 3032b3787f6Schristos 3042b3787f6Schristos @param bufev the bufferevent structure to be freed. 3052b3787f6Schristos */ 3061b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3072b3787f6Schristos void bufferevent_free(struct bufferevent *bufev); 3082b3787f6Schristos 3092b3787f6Schristos 3102b3787f6Schristos /** 3112b3787f6Schristos Changes the callbacks for a bufferevent. 3122b3787f6Schristos 3132b3787f6Schristos @param bufev the bufferevent object for which to change callbacks 3142b3787f6Schristos @param readcb callback to invoke when there is data to be read, or NULL if 3152b3787f6Schristos no callback is desired 3162b3787f6Schristos @param writecb callback to invoke when the file descriptor is ready for 3172b3787f6Schristos writing, or NULL if no callback is desired 3182b3787f6Schristos @param eventcb callback to invoke when there is an event on the file 3192b3787f6Schristos descriptor 3202b3787f6Schristos @param cbarg an argument that will be supplied to each of the callbacks 3212b3787f6Schristos (readcb, writecb, and errorcb) 3222b3787f6Schristos @see bufferevent_new() 3232b3787f6Schristos */ 3241b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3252b3787f6Schristos void bufferevent_setcb(struct bufferevent *bufev, 3262b3787f6Schristos bufferevent_data_cb readcb, bufferevent_data_cb writecb, 3272b3787f6Schristos bufferevent_event_cb eventcb, void *cbarg); 3282b3787f6Schristos 3292b3787f6Schristos /** 3302b3787f6Schristos Retrieves the callbacks for a bufferevent. 3312b3787f6Schristos 3322b3787f6Schristos @param bufev the bufferevent to examine. 3332b3787f6Schristos @param readcb_ptr if readcb_ptr is nonnull, *readcb_ptr is set to the current 3342b3787f6Schristos read callback for the bufferevent. 3352b3787f6Schristos @param writecb_ptr if writecb_ptr is nonnull, *writecb_ptr is set to the 3362b3787f6Schristos current write callback for the bufferevent. 3372b3787f6Schristos @param eventcb_ptr if eventcb_ptr is nonnull, *eventcb_ptr is set to the 3382b3787f6Schristos current event callback for the bufferevent. 3392b3787f6Schristos @param cbarg_ptr if cbarg_ptr is nonnull, *cbarg_ptr is set to the current 3402b3787f6Schristos callback argument for the bufferevent. 3412b3787f6Schristos @see buffervent_setcb() 3422b3787f6Schristos */ 3431b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3442b3787f6Schristos void bufferevent_getcb(struct bufferevent *bufev, 3452b3787f6Schristos bufferevent_data_cb *readcb_ptr, 3462b3787f6Schristos bufferevent_data_cb *writecb_ptr, 3472b3787f6Schristos bufferevent_event_cb *eventcb_ptr, 3482b3787f6Schristos void **cbarg_ptr); 3492b3787f6Schristos 3502b3787f6Schristos /** 3512b3787f6Schristos Changes the file descriptor on which the bufferevent operates. 3522b3787f6Schristos Not supported for all bufferevent types. 3532b3787f6Schristos 3542b3787f6Schristos @param bufev the bufferevent object for which to change the file descriptor 3552b3787f6Schristos @param fd the file descriptor to operate on 3562b3787f6Schristos */ 3571b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3582b3787f6Schristos int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd); 3592b3787f6Schristos 3602b3787f6Schristos /** 3612b3787f6Schristos Returns the file descriptor associated with a bufferevent, or -1 if 3622b3787f6Schristos no file descriptor is associated with the bufferevent. 3632b3787f6Schristos */ 3641b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3652b3787f6Schristos evutil_socket_t bufferevent_getfd(struct bufferevent *bufev); 3662b3787f6Schristos 3672b3787f6Schristos /** 3682b3787f6Schristos Returns the underlying bufferevent associated with a bufferevent (if 3692b3787f6Schristos the bufferevent is a wrapper), or NULL if there is no underlying bufferevent. 3702b3787f6Schristos */ 3711b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3722b3787f6Schristos struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev); 3732b3787f6Schristos 3742b3787f6Schristos /** 3752b3787f6Schristos Write data to a bufferevent buffer. 3762b3787f6Schristos 3772b3787f6Schristos The bufferevent_write() function can be used to write data to the file 3782b3787f6Schristos descriptor. The data is appended to the output buffer and written to the 3792b3787f6Schristos descriptor automatically as it becomes available for writing. 3802b3787f6Schristos 3812b3787f6Schristos @param bufev the bufferevent to be written to 3822b3787f6Schristos @param data a pointer to the data to be written 3832b3787f6Schristos @param size the length of the data, in bytes 3842b3787f6Schristos @return 0 if successful, or -1 if an error occurred 3852b3787f6Schristos @see bufferevent_write_buffer() 3862b3787f6Schristos */ 3871b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 3882b3787f6Schristos int bufferevent_write(struct bufferevent *bufev, 3892b3787f6Schristos const void *data, size_t size); 3902b3787f6Schristos 3912b3787f6Schristos 3922b3787f6Schristos /** 3932b3787f6Schristos Write data from an evbuffer to a bufferevent buffer. The evbuffer is 3942b3787f6Schristos being drained as a result. 3952b3787f6Schristos 3962b3787f6Schristos @param bufev the bufferevent to be written to 3972b3787f6Schristos @param buf the evbuffer to be written 3982b3787f6Schristos @return 0 if successful, or -1 if an error occurred 3992b3787f6Schristos @see bufferevent_write() 4002b3787f6Schristos */ 4011b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4022b3787f6Schristos int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); 4032b3787f6Schristos 4042b3787f6Schristos 4052b3787f6Schristos /** 4062b3787f6Schristos Read data from a bufferevent buffer. 4072b3787f6Schristos 4082b3787f6Schristos The bufferevent_read() function is used to read data from the input buffer. 4092b3787f6Schristos 4102b3787f6Schristos @param bufev the bufferevent to be read from 4112b3787f6Schristos @param data pointer to a buffer that will store the data 4122b3787f6Schristos @param size the size of the data buffer, in bytes 4132b3787f6Schristos @return the amount of data read, in bytes. 4142b3787f6Schristos */ 4151b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4162b3787f6Schristos size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); 4172b3787f6Schristos 4182b3787f6Schristos /** 4192b3787f6Schristos Read data from a bufferevent buffer into an evbuffer. This avoids 4202b3787f6Schristos memory copies. 4212b3787f6Schristos 4222b3787f6Schristos @param bufev the bufferevent to be read from 4232b3787f6Schristos @param buf the evbuffer to which to add data 4242b3787f6Schristos @return 0 if successful, or -1 if an error occurred. 4252b3787f6Schristos */ 4261b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4272b3787f6Schristos int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf); 4282b3787f6Schristos 4292b3787f6Schristos /** 4302b3787f6Schristos Returns the input buffer. 4312b3787f6Schristos 4322b3787f6Schristos The user MUST NOT set the callback on this buffer. 4332b3787f6Schristos 4342b3787f6Schristos @param bufev the bufferevent from which to get the evbuffer 4352b3787f6Schristos @return the evbuffer object for the input buffer 4362b3787f6Schristos */ 4372b3787f6Schristos 4381b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4392b3787f6Schristos struct evbuffer *bufferevent_get_input(struct bufferevent *bufev); 4402b3787f6Schristos 4412b3787f6Schristos /** 4422b3787f6Schristos Returns the output buffer. 4432b3787f6Schristos 4442b3787f6Schristos The user MUST NOT set the callback on this buffer. 4452b3787f6Schristos 4462b3787f6Schristos When filters are being used, the filters need to be manually 4472b3787f6Schristos triggered if the output buffer was manipulated. 4482b3787f6Schristos 4492b3787f6Schristos @param bufev the bufferevent from which to get the evbuffer 4502b3787f6Schristos @return the evbuffer object for the output buffer 4512b3787f6Schristos */ 4522b3787f6Schristos 4531b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4542b3787f6Schristos struct evbuffer *bufferevent_get_output(struct bufferevent *bufev); 4552b3787f6Schristos 4562b3787f6Schristos /** 4572b3787f6Schristos Enable a bufferevent. 4582b3787f6Schristos 4592b3787f6Schristos @param bufev the bufferevent to be enabled 4602b3787f6Schristos @param event any combination of EV_READ | EV_WRITE. 4612b3787f6Schristos @return 0 if successful, or -1 if an error occurred 4622b3787f6Schristos @see bufferevent_disable() 4632b3787f6Schristos */ 4641b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4652b3787f6Schristos int bufferevent_enable(struct bufferevent *bufev, short event); 4662b3787f6Schristos 4672b3787f6Schristos /** 4682b3787f6Schristos Disable a bufferevent. 4692b3787f6Schristos 4702b3787f6Schristos @param bufev the bufferevent to be disabled 4712b3787f6Schristos @param event any combination of EV_READ | EV_WRITE. 4722b3787f6Schristos @return 0 if successful, or -1 if an error occurred 4732b3787f6Schristos @see bufferevent_enable() 4742b3787f6Schristos */ 4751b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4762b3787f6Schristos int bufferevent_disable(struct bufferevent *bufev, short event); 4772b3787f6Schristos 4782b3787f6Schristos /** 4792b3787f6Schristos Return the events that are enabled on a given bufferevent. 4802b3787f6Schristos 4812b3787f6Schristos @param bufev the bufferevent to inspect 4822b3787f6Schristos @return A combination of EV_READ | EV_WRITE 4832b3787f6Schristos */ 4841b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 4852b3787f6Schristos short bufferevent_get_enabled(struct bufferevent *bufev); 4862b3787f6Schristos 4872b3787f6Schristos /** 4882b3787f6Schristos Set the read and write timeout for a bufferevent. 4892b3787f6Schristos 4902b3787f6Schristos A bufferevent's timeout will fire the first time that the indicated 4912b3787f6Schristos amount of time has elapsed since a successful read or write operation, 4922b3787f6Schristos during which the bufferevent was trying to read or write. 4932b3787f6Schristos 4942b3787f6Schristos (In other words, if reading or writing is disabled, or if the 4952b3787f6Schristos bufferevent's read or write operation has been suspended because 4962b3787f6Schristos there's no data to write, or not enough banwidth, or so on, the 4972b3787f6Schristos timeout isn't active. The timeout only becomes active when we we're 4982b3787f6Schristos willing to actually read or write.) 4992b3787f6Schristos 5002b3787f6Schristos Calling bufferevent_enable or setting a timeout for a bufferevent 5012b3787f6Schristos whose timeout is already pending resets its timeout. 5022b3787f6Schristos 5032b3787f6Schristos If the timeout elapses, the corresponding operation (EV_READ or 5042b3787f6Schristos EV_WRITE) becomes disabled until you re-enable it again. The 5052b3787f6Schristos bufferevent's event callback is called with the 5062b3787f6Schristos BEV_EVENT_TIMEOUT|BEV_EVENT_READING or 5072b3787f6Schristos BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING. 5082b3787f6Schristos 5092b3787f6Schristos @param bufev the bufferevent to be modified 5102b3787f6Schristos @param timeout_read the read timeout, or NULL 5112b3787f6Schristos @param timeout_write the write timeout, or NULL 5122b3787f6Schristos */ 5131b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 5142b3787f6Schristos int bufferevent_set_timeouts(struct bufferevent *bufev, 5152b3787f6Schristos const struct timeval *timeout_read, const struct timeval *timeout_write); 5162b3787f6Schristos 5172b3787f6Schristos /** 5182b3787f6Schristos Sets the watermarks for read and write events. 5192b3787f6Schristos 5202b3787f6Schristos On input, a bufferevent does not invoke the user read callback unless 5212b3787f6Schristos there is at least low watermark data in the buffer. If the read buffer 5222b3787f6Schristos is beyond the high watermark, the bufferevent stops reading from the network. 5232b3787f6Schristos 5242b3787f6Schristos On output, the user write callback is invoked whenever the buffered data 5252b3787f6Schristos falls below the low watermark. Filters that write to this bufev will try 5262b3787f6Schristos not to write more bytes to this buffer than the high watermark would allow, 5272b3787f6Schristos except when flushing. 5282b3787f6Schristos 5292b3787f6Schristos @param bufev the bufferevent to be modified 5302b3787f6Schristos @param events EV_READ, EV_WRITE or both 5312b3787f6Schristos @param lowmark the lower watermark to set 5322b3787f6Schristos @param highmark the high watermark to set 5332b3787f6Schristos */ 5342b3787f6Schristos 5351b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 5362b3787f6Schristos void bufferevent_setwatermark(struct bufferevent *bufev, short events, 5372b3787f6Schristos size_t lowmark, size_t highmark); 5382b3787f6Schristos 5392b3787f6Schristos /** 54050cc4415Schristos Retrieves the watermarks for read or write events. 54150cc4415Schristos Returns non-zero if events contains not only EV_READ or EV_WRITE. 54250cc4415Schristos Returns zero if events equal EV_READ or EV_WRITE 5431b6f2cd4Schristos 5441b6f2cd4Schristos @param bufev the bufferevent to be examined 5451b6f2cd4Schristos @param events EV_READ or EV_WRITE 5461b6f2cd4Schristos @param lowmark receives the lower watermark if not NULL 5471b6f2cd4Schristos @param highmark receives the high watermark if not NULL 5481b6f2cd4Schristos */ 5491b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 55050cc4415Schristos int bufferevent_getwatermark(struct bufferevent *bufev, short events, 5511b6f2cd4Schristos size_t *lowmark, size_t *highmark); 5521b6f2cd4Schristos 5531b6f2cd4Schristos /** 5542b3787f6Schristos Acquire the lock on a bufferevent. Has no effect if locking was not 5552b3787f6Schristos enabled with BEV_OPT_THREADSAFE. 5562b3787f6Schristos */ 5571b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 5582b3787f6Schristos void bufferevent_lock(struct bufferevent *bufev); 5592b3787f6Schristos 5602b3787f6Schristos /** 5612b3787f6Schristos Release the lock on a bufferevent. Has no effect if locking was not 5622b3787f6Schristos enabled with BEV_OPT_THREADSAFE. 5632b3787f6Schristos */ 5641b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 5652b3787f6Schristos void bufferevent_unlock(struct bufferevent *bufev); 5662b3787f6Schristos 5672b3787f6Schristos /** 5682b3787f6Schristos Flags that can be passed into filters to let them know how to 5692b3787f6Schristos deal with the incoming data. 5702b3787f6Schristos */ 5712b3787f6Schristos enum bufferevent_flush_mode { 5722b3787f6Schristos /** usually set when processing data */ 5732b3787f6Schristos BEV_NORMAL = 0, 5742b3787f6Schristos 5752b3787f6Schristos /** want to checkpoint all data sent. */ 5762b3787f6Schristos BEV_FLUSH = 1, 5772b3787f6Schristos 5782b3787f6Schristos /** encountered EOF on read or done sending data */ 5792b3787f6Schristos BEV_FINISHED = 2 5802b3787f6Schristos }; 5812b3787f6Schristos 5822b3787f6Schristos /** 5832b3787f6Schristos Triggers the bufferevent to produce more data if possible. 5842b3787f6Schristos 5852b3787f6Schristos @param bufev the bufferevent object 5862b3787f6Schristos @param iotype either EV_READ or EV_WRITE or both. 5872b3787f6Schristos @param mode either BEV_NORMAL or BEV_FLUSH or BEV_FINISHED 5882b3787f6Schristos @return -1 on failure, 0 if no data was produces, 1 if data was produced 5892b3787f6Schristos */ 5901b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 5912b3787f6Schristos int bufferevent_flush(struct bufferevent *bufev, 5922b3787f6Schristos short iotype, 5932b3787f6Schristos enum bufferevent_flush_mode mode); 5942b3787f6Schristos 5952b3787f6Schristos /** 5961b6f2cd4Schristos Flags for bufferevent_trigger(_event) that modify when and how to trigger 5971b6f2cd4Schristos the callback. 5981b6f2cd4Schristos */ 5991b6f2cd4Schristos enum bufferevent_trigger_options { 6001b6f2cd4Schristos /** trigger the callback regardless of the watermarks */ 6011b6f2cd4Schristos BEV_TRIG_IGNORE_WATERMARKS = (1<<16), 6021b6f2cd4Schristos 6031b6f2cd4Schristos /** defer even if the callbacks are not */ 60450cc4415Schristos BEV_TRIG_DEFER_CALLBACKS = BEV_OPT_DEFER_CALLBACKS 6051b6f2cd4Schristos 6061b6f2cd4Schristos /* (Note: for internal reasons, these need to be disjoint from 6071b6f2cd4Schristos * bufferevent_options, except when they mean the same thing. */ 6081b6f2cd4Schristos }; 6091b6f2cd4Schristos 6101b6f2cd4Schristos /** 6111b6f2cd4Schristos Triggers bufferevent data callbacks. 6121b6f2cd4Schristos 6131b6f2cd4Schristos The function will honor watermarks unless options contain 6141b6f2cd4Schristos BEV_TRIG_IGNORE_WATERMARKS. If the options contain BEV_OPT_DEFER_CALLBACKS, 6151b6f2cd4Schristos the callbacks are deferred. 6161b6f2cd4Schristos 6171b6f2cd4Schristos @param bufev the bufferevent object 6181b6f2cd4Schristos @param iotype either EV_READ or EV_WRITE or both. 6191b6f2cd4Schristos @param options 6201b6f2cd4Schristos */ 6211b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 6221b6f2cd4Schristos void bufferevent_trigger(struct bufferevent *bufev, short iotype, 6231b6f2cd4Schristos int options); 6241b6f2cd4Schristos 6251b6f2cd4Schristos /** 6261b6f2cd4Schristos Triggers the bufferevent event callback. 6271b6f2cd4Schristos 6281b6f2cd4Schristos If the options contain BEV_OPT_DEFER_CALLBACKS, the callbacks are deferred. 6291b6f2cd4Schristos 6301b6f2cd4Schristos @param bufev the bufferevent object 6311b6f2cd4Schristos @param what the flags to pass onto the event callback 6321b6f2cd4Schristos @param options 6331b6f2cd4Schristos */ 6341b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 6351b6f2cd4Schristos void bufferevent_trigger_event(struct bufferevent *bufev, short what, 6361b6f2cd4Schristos int options); 6371b6f2cd4Schristos 6381b6f2cd4Schristos /** 6392b3787f6Schristos @name Filtering support 6402b3787f6Schristos 6412b3787f6Schristos @{ 6422b3787f6Schristos */ 6432b3787f6Schristos /** 6442b3787f6Schristos Values that filters can return. 6452b3787f6Schristos */ 6462b3787f6Schristos enum bufferevent_filter_result { 6472b3787f6Schristos /** everything is okay */ 6482b3787f6Schristos BEV_OK = 0, 6492b3787f6Schristos 6502b3787f6Schristos /** the filter needs to read more data before output */ 6512b3787f6Schristos BEV_NEED_MORE = 1, 6522b3787f6Schristos 6532b3787f6Schristos /** the filter encountered a critical error, no further data 6542b3787f6Schristos can be processed. */ 6552b3787f6Schristos BEV_ERROR = 2 6562b3787f6Schristos }; 6572b3787f6Schristos 6582b3787f6Schristos /** A callback function to implement a filter for a bufferevent. 6592b3787f6Schristos 6602b3787f6Schristos @param src An evbuffer to drain data from. 6612b3787f6Schristos @param dst An evbuffer to add data to. 6622b3787f6Schristos @param limit A suggested upper bound of bytes to write to dst. 6632b3787f6Schristos The filter may ignore this value, but doing so means that 6642b3787f6Schristos it will overflow the high-water mark associated with dst. 6652b3787f6Schristos -1 means "no limit". 6662b3787f6Schristos @param mode Whether we should write data as may be convenient 6672b3787f6Schristos (BEV_NORMAL), or flush as much data as we can (BEV_FLUSH), 6682b3787f6Schristos or flush as much as we can, possibly including an end-of-stream 6692b3787f6Schristos marker (BEV_FINISH). 6702b3787f6Schristos @param ctx A user-supplied pointer. 6712b3787f6Schristos 6722b3787f6Schristos @return BEV_OK if we wrote some data; BEV_NEED_MORE if we can't 6732b3787f6Schristos produce any more output until we get some input; and BEV_ERROR 6742b3787f6Schristos on an error. 6752b3787f6Schristos */ 6762b3787f6Schristos typedef enum bufferevent_filter_result (*bufferevent_filter_cb)( 6772b3787f6Schristos struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, 6782b3787f6Schristos enum bufferevent_flush_mode mode, void *ctx); 6792b3787f6Schristos 6802b3787f6Schristos /** 6812b3787f6Schristos Allocate a new filtering bufferevent on top of an existing bufferevent. 6822b3787f6Schristos 6832b3787f6Schristos @param underlying the underlying bufferevent. 6842b3787f6Schristos @param input_filter The filter to apply to data we read from the underlying 6852b3787f6Schristos bufferevent 6862b3787f6Schristos @param output_filter The filer to apply to data we write to the underlying 6872b3787f6Schristos bufferevent 6882b3787f6Schristos @param options A bitfield of bufferevent options. 6892b3787f6Schristos @param free_context A function to use to free the filter context when 6902b3787f6Schristos this bufferevent is freed. 6912b3787f6Schristos @param ctx A context pointer to pass to the filter functions. 6922b3787f6Schristos */ 6931b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 6942b3787f6Schristos struct bufferevent * 6952b3787f6Schristos bufferevent_filter_new(struct bufferevent *underlying, 6962b3787f6Schristos bufferevent_filter_cb input_filter, 6972b3787f6Schristos bufferevent_filter_cb output_filter, 6982b3787f6Schristos int options, 6992b3787f6Schristos void (*free_context)(void *), 7002b3787f6Schristos void *ctx); 7012b3787f6Schristos /**@}*/ 7022b3787f6Schristos 7032b3787f6Schristos /** 7042b3787f6Schristos Allocate a pair of linked bufferevents. The bufferevents behave as would 7052b3787f6Schristos two bufferevent_sock instances connected to opposite ends of a 7062b3787f6Schristos socketpair(), except that no internal socketpair is allocated. 7072b3787f6Schristos 7082b3787f6Schristos @param base The event base to associate with the socketpair. 7092b3787f6Schristos @param options A set of options for this bufferevent 7102b3787f6Schristos @param pair A pointer to an array to hold the two new bufferevent objects. 7112b3787f6Schristos @return 0 on success, -1 on failure. 7122b3787f6Schristos */ 7131b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 7142b3787f6Schristos int bufferevent_pair_new(struct event_base *base, int options, 7152b3787f6Schristos struct bufferevent *pair[2]); 7162b3787f6Schristos 7172b3787f6Schristos /** 7182b3787f6Schristos Given one bufferevent returned by bufferevent_pair_new(), returns the 7192b3787f6Schristos other one if it still exists. Otherwise returns NULL. 7202b3787f6Schristos */ 7211b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 7222b3787f6Schristos struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev); 7232b3787f6Schristos 7242b3787f6Schristos /** 7252b3787f6Schristos Abstract type used to configure rate-limiting on a bufferevent or a group 7262b3787f6Schristos of bufferevents. 7272b3787f6Schristos */ 7282b3787f6Schristos struct ev_token_bucket_cfg; 7292b3787f6Schristos 7302b3787f6Schristos /** 7312b3787f6Schristos A group of bufferevents which are configured to respect the same rate 7322b3787f6Schristos limit. 7332b3787f6Schristos */ 7342b3787f6Schristos struct bufferevent_rate_limit_group; 7352b3787f6Schristos 7362b3787f6Schristos /** Maximum configurable rate- or burst-limit. */ 7372b3787f6Schristos #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX 7382b3787f6Schristos 7392b3787f6Schristos /** 7402b3787f6Schristos Initialize and return a new object to configure the rate-limiting behavior 7412b3787f6Schristos of bufferevents. 7422b3787f6Schristos 7432b3787f6Schristos @param read_rate The maximum number of bytes to read per tick on 7442b3787f6Schristos average. 7452b3787f6Schristos @param read_burst The maximum number of bytes to read in any single tick. 7462b3787f6Schristos @param write_rate The maximum number of bytes to write per tick on 7472b3787f6Schristos average. 7482b3787f6Schristos @param write_burst The maximum number of bytes to write in any single tick. 7492b3787f6Schristos @param tick_len The length of a single tick. Defaults to one second. 7502b3787f6Schristos Any fractions of a millisecond are ignored. 7512b3787f6Schristos 7522b3787f6Schristos Note that all rate-limits hare are currently best-effort: future versions 7532b3787f6Schristos of Libevent may implement them more tightly. 7542b3787f6Schristos */ 7551b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 7562b3787f6Schristos struct ev_token_bucket_cfg *ev_token_bucket_cfg_new( 7572b3787f6Schristos size_t read_rate, size_t read_burst, 7582b3787f6Schristos size_t write_rate, size_t write_burst, 7592b3787f6Schristos const struct timeval *tick_len); 7602b3787f6Schristos 7612b3787f6Schristos /** Free all storage held in 'cfg'. 7622b3787f6Schristos 7632b3787f6Schristos Note: 'cfg' is not currently reference-counted; it is not safe to free it 7642b3787f6Schristos until no bufferevent is using it. 7652b3787f6Schristos */ 7661b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 7672b3787f6Schristos void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg); 7682b3787f6Schristos 7692b3787f6Schristos /** 7702b3787f6Schristos Set the rate-limit of a the bufferevent 'bev' to the one specified in 7712b3787f6Schristos 'cfg'. If 'cfg' is NULL, disable any per-bufferevent rate-limiting on 7722b3787f6Schristos 'bev'. 7732b3787f6Schristos 7742b3787f6Schristos Note that only some bufferevent types currently respect rate-limiting. 7752b3787f6Schristos They are: socket-based bufferevents (normal and IOCP-based), and SSL-based 7762b3787f6Schristos bufferevents. 7772b3787f6Schristos 7782b3787f6Schristos Return 0 on sucess, -1 on failure. 7792b3787f6Schristos */ 7801b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 7812b3787f6Schristos int bufferevent_set_rate_limit(struct bufferevent *bev, 7822b3787f6Schristos struct ev_token_bucket_cfg *cfg); 7832b3787f6Schristos 7842b3787f6Schristos /** 7852b3787f6Schristos Create a new rate-limit group for bufferevents. A rate-limit group 7862b3787f6Schristos constrains the maximum number of bytes sent and received, in toto, 7872b3787f6Schristos by all of its bufferevents. 7882b3787f6Schristos 7892b3787f6Schristos @param base An event_base to run any necessary timeouts for the group. 7902b3787f6Schristos Note that all bufferevents in the group do not necessarily need to share 7912b3787f6Schristos this event_base. 7922b3787f6Schristos @param cfg The rate-limit for this group. 7932b3787f6Schristos 7942b3787f6Schristos Note that all rate-limits hare are currently best-effort: future versions 7952b3787f6Schristos of Libevent may implement them more tightly. 7962b3787f6Schristos 7972b3787f6Schristos Note also that only some bufferevent types currently respect rate-limiting. 7982b3787f6Schristos They are: socket-based bufferevents (normal and IOCP-based), and SSL-based 7992b3787f6Schristos bufferevents. 8002b3787f6Schristos */ 8011b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8022b3787f6Schristos struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new( 8032b3787f6Schristos struct event_base *base, 8042b3787f6Schristos const struct ev_token_bucket_cfg *cfg); 8052b3787f6Schristos /** 8062b3787f6Schristos Change the rate-limiting settings for a given rate-limiting group. 8072b3787f6Schristos 8082b3787f6Schristos Return 0 on success, -1 on failure. 8092b3787f6Schristos */ 8101b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8112b3787f6Schristos int bufferevent_rate_limit_group_set_cfg( 8122b3787f6Schristos struct bufferevent_rate_limit_group *, 8132b3787f6Schristos const struct ev_token_bucket_cfg *); 8142b3787f6Schristos 8152b3787f6Schristos /** 8162b3787f6Schristos Change the smallest quantum we're willing to allocate to any single 8172b3787f6Schristos bufferevent in a group for reading or writing at a time. 8182b3787f6Schristos 8192b3787f6Schristos The rationale is that, because of TCP/IP protocol overheads and kernel 8202b3787f6Schristos behavior, if a rate-limiting group is so tight on bandwidth that you're 8212b3787f6Schristos only willing to send 1 byte per tick per bufferevent, you might instead 8222b3787f6Schristos want to batch up the reads and writes so that you send N bytes per 8232b3787f6Schristos 1/N of the bufferevents (chosen at random) each tick, so you still wind 8242b3787f6Schristos up send 1 byte per tick per bufferevent on average, but you don't send 8252b3787f6Schristos so many tiny packets. 8262b3787f6Schristos 8272b3787f6Schristos The default min-share is currently 64 bytes. 8282b3787f6Schristos 8292b3787f6Schristos Returns 0 on success, -1 on faulre. 8302b3787f6Schristos */ 8311b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8322b3787f6Schristos int bufferevent_rate_limit_group_set_min_share( 8332b3787f6Schristos struct bufferevent_rate_limit_group *, size_t); 8342b3787f6Schristos 8352b3787f6Schristos /** 8362b3787f6Schristos Free a rate-limiting group. The group must have no members when 8372b3787f6Schristos this function is called. 8382b3787f6Schristos */ 8391b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8402b3787f6Schristos void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *); 8412b3787f6Schristos 8422b3787f6Schristos /** 8432b3787f6Schristos Add 'bev' to the list of bufferevents whose aggregate reading and writing 8442b3787f6Schristos is restricted by 'g'. If 'g' is NULL, remove 'bev' from its current group. 8452b3787f6Schristos 8462b3787f6Schristos A bufferevent may belong to no more than one rate-limit group at a time. 8472b3787f6Schristos If 'bev' is already a member of a group, it will be removed from its old 8482b3787f6Schristos group before being added to 'g'. 8492b3787f6Schristos 8502b3787f6Schristos Return 0 on success and -1 on failure. 8512b3787f6Schristos */ 8521b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8532b3787f6Schristos int bufferevent_add_to_rate_limit_group(struct bufferevent *bev, 8542b3787f6Schristos struct bufferevent_rate_limit_group *g); 8552b3787f6Schristos 8562b3787f6Schristos /** Remove 'bev' from its current rate-limit group (if any). */ 8571b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8582b3787f6Schristos int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev); 8592b3787f6Schristos 8602b3787f6Schristos /** 8612b3787f6Schristos Set the size limit for single read operation. 8622b3787f6Schristos 8632b3787f6Schristos Set to 0 for a reasonable default. 8642b3787f6Schristos 8652b3787f6Schristos Return 0 on success and -1 on failure. 8662b3787f6Schristos */ 8671b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8682b3787f6Schristos int bufferevent_set_max_single_read(struct bufferevent *bev, size_t size); 8692b3787f6Schristos 8702b3787f6Schristos /** 8712b3787f6Schristos Set the size limit for single write operation. 8722b3787f6Schristos 8732b3787f6Schristos Set to 0 for a reasonable default. 8742b3787f6Schristos 8752b3787f6Schristos Return 0 on success and -1 on failure. 8762b3787f6Schristos */ 8771b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8782b3787f6Schristos int bufferevent_set_max_single_write(struct bufferevent *bev, size_t size); 8792b3787f6Schristos 8802b3787f6Schristos /** Get the current size limit for single read operation. */ 8811b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8822b3787f6Schristos ev_ssize_t bufferevent_get_max_single_read(struct bufferevent *bev); 8832b3787f6Schristos 8842b3787f6Schristos /** Get the current size limit for single write operation. */ 8851b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 8862b3787f6Schristos ev_ssize_t bufferevent_get_max_single_write(struct bufferevent *bev); 8872b3787f6Schristos 8882b3787f6Schristos /** 8892b3787f6Schristos @name Rate limit inspection 8902b3787f6Schristos 8912b3787f6Schristos Return the current read or write bucket size for a bufferevent. 8922b3787f6Schristos If it is not configured with a per-bufferevent ratelimit, return 8932b3787f6Schristos EV_SSIZE_MAX. This function does not inspect the group limit, if any. 8942b3787f6Schristos Note that it can return a negative value if the bufferevent has been 8952b3787f6Schristos made to read or write more than its limit. 8962b3787f6Schristos 8972b3787f6Schristos @{ 8982b3787f6Schristos */ 8991b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9002b3787f6Schristos ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev); 9011b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9022b3787f6Schristos ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev); 9032b3787f6Schristos /*@}*/ 9042b3787f6Schristos 9051b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9062b3787f6Schristos ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev); 9071b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9082b3787f6Schristos ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev); 9092b3787f6Schristos 9101b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9111b6f2cd4Schristos const struct ev_token_bucket_cfg *bufferevent_get_token_bucket_cfg(const struct bufferevent * bev); 9121b6f2cd4Schristos 9132b3787f6Schristos /** 9142b3787f6Schristos @name Group Rate limit inspection 9152b3787f6Schristos 9162b3787f6Schristos Return the read or write bucket size for a bufferevent rate limit 9172b3787f6Schristos group. Note that it can return a negative value if bufferevents in 9182b3787f6Schristos the group have been made to read or write more than their limits. 9192b3787f6Schristos 9202b3787f6Schristos @{ 9212b3787f6Schristos */ 9221b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9232b3787f6Schristos ev_ssize_t bufferevent_rate_limit_group_get_read_limit( 9242b3787f6Schristos struct bufferevent_rate_limit_group *); 9251b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9262b3787f6Schristos ev_ssize_t bufferevent_rate_limit_group_get_write_limit( 9272b3787f6Schristos struct bufferevent_rate_limit_group *); 9282b3787f6Schristos /*@}*/ 9292b3787f6Schristos 9302b3787f6Schristos /** 9312b3787f6Schristos @name Rate limit manipulation 9322b3787f6Schristos 9332b3787f6Schristos Subtract a number of bytes from a bufferevent's read or write bucket. 9342b3787f6Schristos The decrement value can be negative, if you want to manually refill 9352b3787f6Schristos the bucket. If the change puts the bucket above or below zero, the 9362b3787f6Schristos bufferevent will resume or suspend reading writing as appropriate. 9372b3787f6Schristos These functions make no change in the buckets for the bufferevent's 9382b3787f6Schristos group, if any. 9392b3787f6Schristos 9402b3787f6Schristos Returns 0 on success, -1 on internal error. 9412b3787f6Schristos 9422b3787f6Schristos @{ 9432b3787f6Schristos */ 9441b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9452b3787f6Schristos int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr); 9461b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9472b3787f6Schristos int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr); 9482b3787f6Schristos /*@}*/ 9492b3787f6Schristos 9502b3787f6Schristos /** 9512b3787f6Schristos @name Group rate limit manipulation 9522b3787f6Schristos 9532b3787f6Schristos Subtract a number of bytes from a bufferevent rate-limiting group's 9542b3787f6Schristos read or write bucket. The decrement value can be negative, if you 9552b3787f6Schristos want to manually refill the bucket. If the change puts the bucket 9562b3787f6Schristos above or below zero, the bufferevents in the group will resume or 9572b3787f6Schristos suspend reading writing as appropriate. 9582b3787f6Schristos 9592b3787f6Schristos Returns 0 on success, -1 on internal error. 9602b3787f6Schristos 9612b3787f6Schristos @{ 9622b3787f6Schristos */ 9631b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9642b3787f6Schristos int bufferevent_rate_limit_group_decrement_read( 9652b3787f6Schristos struct bufferevent_rate_limit_group *, ev_ssize_t); 9661b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9672b3787f6Schristos int bufferevent_rate_limit_group_decrement_write( 9682b3787f6Schristos struct bufferevent_rate_limit_group *, ev_ssize_t); 9692b3787f6Schristos /*@}*/ 9702b3787f6Schristos 9712b3787f6Schristos 9722b3787f6Schristos /** 9732b3787f6Schristos * Inspect the total bytes read/written on a group. 9742b3787f6Schristos * 9752b3787f6Schristos * Set the variable pointed to by total_read_out to the total number of bytes 9762b3787f6Schristos * ever read on grp, and the variable pointed to by total_written_out to the 9772b3787f6Schristos * total number of bytes ever written on grp. */ 9781b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9792b3787f6Schristos void bufferevent_rate_limit_group_get_totals( 9802b3787f6Schristos struct bufferevent_rate_limit_group *grp, 9812b3787f6Schristos ev_uint64_t *total_read_out, ev_uint64_t *total_written_out); 9822b3787f6Schristos 9832b3787f6Schristos /** 9842b3787f6Schristos * Reset the total bytes read/written on a group. 9852b3787f6Schristos * 9862b3787f6Schristos * Reset the number of bytes read or written on grp as given by 9872b3787f6Schristos * bufferevent_rate_limit_group_reset_totals(). */ 9881b6f2cd4Schristos EVENT2_EXPORT_SYMBOL 9892b3787f6Schristos void 9902b3787f6Schristos bufferevent_rate_limit_group_reset_totals( 9912b3787f6Schristos struct bufferevent_rate_limit_group *grp); 9922b3787f6Schristos 9932b3787f6Schristos #ifdef __cplusplus 9942b3787f6Schristos } 9952b3787f6Schristos #endif 9962b3787f6Schristos 9972b3787f6Schristos #endif /* EVENT2_BUFFEREVENT_H_INCLUDED_ */ 998