1*c43e99fdSEd Maste /* 2*c43e99fdSEd Maste * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 3*c43e99fdSEd Maste * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 4*c43e99fdSEd Maste * 5*c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without 6*c43e99fdSEd Maste * modification, are permitted provided that the following conditions 7*c43e99fdSEd Maste * are met: 8*c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright 9*c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer. 10*c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright 11*c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the 12*c43e99fdSEd Maste * documentation and/or other materials provided with the distribution. 13*c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products 14*c43e99fdSEd Maste * derived from this software without specific prior written permission. 15*c43e99fdSEd Maste * 16*c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17*c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18*c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19*c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20*c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21*c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22*c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23*c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24*c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25*c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26*c43e99fdSEd Maste */ 27*c43e99fdSEd Maste #ifndef EVENT2_BUFFEREVENT_H_INCLUDED_ 28*c43e99fdSEd Maste #define EVENT2_BUFFEREVENT_H_INCLUDED_ 29*c43e99fdSEd Maste 30*c43e99fdSEd Maste /** 31*c43e99fdSEd Maste @file event2/bufferevent.h 32*c43e99fdSEd Maste 33*c43e99fdSEd Maste Functions for buffering data for network sending or receiving. Bufferevents 34*c43e99fdSEd Maste are higher level than evbuffers: each has an underlying evbuffer for reading 35*c43e99fdSEd Maste and one for writing, and callbacks that are invoked under certain 36*c43e99fdSEd Maste circumstances. 37*c43e99fdSEd Maste 38*c43e99fdSEd Maste A bufferevent provides input and output buffers that get filled and 39*c43e99fdSEd Maste drained automatically. The user of a bufferevent no longer deals 40*c43e99fdSEd Maste directly with the I/O, but instead is reading from input and writing 41*c43e99fdSEd Maste to output buffers. 42*c43e99fdSEd Maste 43*c43e99fdSEd Maste Once initialized, the bufferevent structure can be used repeatedly 44*c43e99fdSEd Maste with bufferevent_enable() and bufferevent_disable(). 45*c43e99fdSEd Maste 46*c43e99fdSEd Maste When reading is enabled, the bufferevent will try to read from the 47*c43e99fdSEd Maste file descriptor onto its input buffer, and call the read callback. 48*c43e99fdSEd Maste When writing is enabled, the bufferevent will try to write data onto its 49*c43e99fdSEd Maste file descriptor when the output buffer has enough data, and call the write 50*c43e99fdSEd Maste callback when the output buffer is sufficiently drained. 51*c43e99fdSEd Maste 52*c43e99fdSEd Maste Bufferevents come in several flavors, including: 53*c43e99fdSEd Maste 54*c43e99fdSEd Maste <dl> 55*c43e99fdSEd Maste <dt>Socket-based bufferevents</dt> 56*c43e99fdSEd Maste <dd>A bufferevent that reads and writes data onto a network 57*c43e99fdSEd Maste socket. Created with bufferevent_socket_new().</dd> 58*c43e99fdSEd Maste 59*c43e99fdSEd Maste <dt>Paired bufferevents</dt> 60*c43e99fdSEd Maste <dd>A pair of bufferevents that send and receive data to one 61*c43e99fdSEd Maste another without touching the network. Created with 62*c43e99fdSEd Maste bufferevent_pair_new().</dd> 63*c43e99fdSEd Maste 64*c43e99fdSEd Maste <dt>Filtering bufferevents</dt> 65*c43e99fdSEd Maste <dd>A bufferevent that transforms data, and sends or receives it 66*c43e99fdSEd Maste over another underlying bufferevent. Created with 67*c43e99fdSEd Maste bufferevent_filter_new().</dd> 68*c43e99fdSEd Maste 69*c43e99fdSEd Maste <dt>SSL-backed bufferevents</dt> 70*c43e99fdSEd Maste <dd>A bufferevent that uses the openssl library to send and 71*c43e99fdSEd Maste receive data over an encrypted connection. Created with 72*c43e99fdSEd Maste bufferevent_openssl_socket_new() or 73*c43e99fdSEd Maste bufferevent_openssl_filter_new().</dd> 74*c43e99fdSEd Maste </dl> 75*c43e99fdSEd Maste */ 76*c43e99fdSEd Maste 77*c43e99fdSEd Maste #include <event2/visibility.h> 78*c43e99fdSEd Maste 79*c43e99fdSEd Maste #ifdef __cplusplus 80*c43e99fdSEd Maste extern "C" { 81*c43e99fdSEd Maste #endif 82*c43e99fdSEd Maste 83*c43e99fdSEd Maste #include <event2/event-config.h> 84*c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TYPES_H 85*c43e99fdSEd Maste #include <sys/types.h> 86*c43e99fdSEd Maste #endif 87*c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_TIME_H 88*c43e99fdSEd Maste #include <sys/time.h> 89*c43e99fdSEd Maste #endif 90*c43e99fdSEd Maste 91*c43e99fdSEd Maste /* For int types. */ 92*c43e99fdSEd Maste #include <event2/util.h> 93*c43e99fdSEd Maste 94*c43e99fdSEd Maste /** @name Bufferevent event codes 95*c43e99fdSEd Maste 96*c43e99fdSEd Maste These flags are passed as arguments to a bufferevent's event callback. 97*c43e99fdSEd Maste 98*c43e99fdSEd Maste @{ 99*c43e99fdSEd Maste */ 100*c43e99fdSEd Maste #define BEV_EVENT_READING 0x01 /**< error encountered while reading */ 101*c43e99fdSEd Maste #define BEV_EVENT_WRITING 0x02 /**< error encountered while writing */ 102*c43e99fdSEd Maste #define BEV_EVENT_EOF 0x10 /**< eof file reached */ 103*c43e99fdSEd Maste #define BEV_EVENT_ERROR 0x20 /**< unrecoverable error encountered */ 104*c43e99fdSEd Maste #define BEV_EVENT_TIMEOUT 0x40 /**< user-specified timeout reached */ 105*c43e99fdSEd Maste #define BEV_EVENT_CONNECTED 0x80 /**< connect operation finished. */ 106*c43e99fdSEd Maste /**@}*/ 107*c43e99fdSEd Maste 108*c43e99fdSEd Maste /** 109*c43e99fdSEd Maste An opaque type for handling buffered IO 110*c43e99fdSEd Maste 111*c43e99fdSEd Maste @see event2/bufferevent.h 112*c43e99fdSEd Maste */ 113*c43e99fdSEd Maste struct bufferevent 114*c43e99fdSEd Maste #ifdef EVENT_IN_DOXYGEN_ 115*c43e99fdSEd Maste {} 116*c43e99fdSEd Maste #endif 117*c43e99fdSEd Maste ; 118*c43e99fdSEd Maste struct event_base; 119*c43e99fdSEd Maste struct evbuffer; 120*c43e99fdSEd Maste struct sockaddr; 121*c43e99fdSEd Maste 122*c43e99fdSEd Maste /** 123*c43e99fdSEd Maste A read or write callback for a bufferevent. 124*c43e99fdSEd Maste 125*c43e99fdSEd Maste The read callback is triggered when new data arrives in the input 126*c43e99fdSEd Maste buffer and the amount of readable data exceed the low watermark 127*c43e99fdSEd Maste which is 0 by default. 128*c43e99fdSEd Maste 129*c43e99fdSEd Maste The write callback is triggered if the write buffer has been 130*c43e99fdSEd Maste exhausted or fell below its low watermark. 131*c43e99fdSEd Maste 132*c43e99fdSEd Maste @param bev the bufferevent that triggered the callback 133*c43e99fdSEd Maste @param ctx the user-specified context for this bufferevent 134*c43e99fdSEd Maste */ 135*c43e99fdSEd Maste typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx); 136*c43e99fdSEd Maste 137*c43e99fdSEd Maste /** 138*c43e99fdSEd Maste An event/error callback for a bufferevent. 139*c43e99fdSEd Maste 140*c43e99fdSEd Maste The event callback is triggered if either an EOF condition or another 141*c43e99fdSEd Maste unrecoverable error was encountered. 142*c43e99fdSEd Maste 143*c43e99fdSEd Maste For bufferevents with deferred callbacks, this is a bitwise OR of all errors 144*c43e99fdSEd Maste that have happened on the bufferevent since the last callback invocation. 145*c43e99fdSEd Maste 146*c43e99fdSEd Maste @param bev the bufferevent for which the error condition was reached 147*c43e99fdSEd Maste @param what a conjunction of flags: BEV_EVENT_READING or BEV_EVENT_WRITING 148*c43e99fdSEd Maste to indicate if the error was encountered on the read or write path, 149*c43e99fdSEd Maste and one of the following flags: BEV_EVENT_EOF, BEV_EVENT_ERROR, 150*c43e99fdSEd Maste BEV_EVENT_TIMEOUT, BEV_EVENT_CONNECTED. 151*c43e99fdSEd Maste 152*c43e99fdSEd Maste @param ctx the user-specified context for this bufferevent 153*c43e99fdSEd Maste */ 154*c43e99fdSEd Maste typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx); 155*c43e99fdSEd Maste 156*c43e99fdSEd Maste /** Options that can be specified when creating a bufferevent */ 157*c43e99fdSEd Maste enum bufferevent_options { 158*c43e99fdSEd Maste /** If set, we close the underlying file 159*c43e99fdSEd Maste * descriptor/bufferevent/whatever when this bufferevent is freed. */ 160*c43e99fdSEd Maste BEV_OPT_CLOSE_ON_FREE = (1<<0), 161*c43e99fdSEd Maste 162*c43e99fdSEd Maste /** If set, and threading is enabled, operations on this bufferevent 163*c43e99fdSEd Maste * are protected by a lock */ 164*c43e99fdSEd Maste BEV_OPT_THREADSAFE = (1<<1), 165*c43e99fdSEd Maste 166*c43e99fdSEd Maste /** If set, callbacks are run deferred in the event loop. */ 167*c43e99fdSEd Maste BEV_OPT_DEFER_CALLBACKS = (1<<2), 168*c43e99fdSEd Maste 169*c43e99fdSEd Maste /** If set, callbacks are executed without locks being held on the 170*c43e99fdSEd Maste * bufferevent. This option currently requires that 171*c43e99fdSEd Maste * BEV_OPT_DEFER_CALLBACKS also be set; a future version of Libevent 172*c43e99fdSEd Maste * might remove the requirement.*/ 173*c43e99fdSEd Maste BEV_OPT_UNLOCK_CALLBACKS = (1<<3) 174*c43e99fdSEd Maste }; 175*c43e99fdSEd Maste 176*c43e99fdSEd Maste /** 177*c43e99fdSEd Maste Create a new socket bufferevent over an existing socket. 178*c43e99fdSEd Maste 179*c43e99fdSEd Maste @param base the event base to associate with the new bufferevent. 180*c43e99fdSEd Maste @param fd the file descriptor from which data is read and written to. 181*c43e99fdSEd Maste This file descriptor is not allowed to be a pipe(2). 182*c43e99fdSEd Maste It is safe to set the fd to -1, so long as you later 183*c43e99fdSEd Maste set it with bufferevent_setfd or bufferevent_socket_connect(). 184*c43e99fdSEd Maste @param options Zero or more BEV_OPT_* flags 185*c43e99fdSEd Maste @return a pointer to a newly allocated bufferevent struct, or NULL if an 186*c43e99fdSEd Maste error occurred 187*c43e99fdSEd Maste @see bufferevent_free() 188*c43e99fdSEd Maste */ 189*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 190*c43e99fdSEd Maste struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options); 191*c43e99fdSEd Maste 192*c43e99fdSEd Maste /** 193*c43e99fdSEd Maste Launch a connect() attempt with a socket-based bufferevent. 194*c43e99fdSEd Maste 195*c43e99fdSEd Maste When the connect succeeds, the eventcb will be invoked with 196*c43e99fdSEd Maste BEV_EVENT_CONNECTED set. 197*c43e99fdSEd Maste 198*c43e99fdSEd Maste If the bufferevent does not already have a socket set, we allocate a new 199*c43e99fdSEd Maste socket here and make it nonblocking before we begin. 200*c43e99fdSEd Maste 201*c43e99fdSEd Maste If no address is provided, we assume that the socket is already connecting, 202*c43e99fdSEd Maste and configure the bufferevent so that a BEV_EVENT_CONNECTED event will be 203*c43e99fdSEd Maste yielded when it is done connecting. 204*c43e99fdSEd Maste 205*c43e99fdSEd Maste @param bufev an existing bufferevent allocated with 206*c43e99fdSEd Maste bufferevent_socket_new(). 207*c43e99fdSEd Maste @param addr the address we should connect to 208*c43e99fdSEd Maste @param socklen The length of the address 209*c43e99fdSEd Maste @return 0 on success, -1 on failure. 210*c43e99fdSEd Maste */ 211*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 212*c43e99fdSEd Maste int bufferevent_socket_connect(struct bufferevent *, const struct sockaddr *, int); 213*c43e99fdSEd Maste 214*c43e99fdSEd Maste struct evdns_base; 215*c43e99fdSEd Maste /** 216*c43e99fdSEd Maste Resolve the hostname 'hostname' and connect to it as with 217*c43e99fdSEd Maste bufferevent_socket_connect(). 218*c43e99fdSEd Maste 219*c43e99fdSEd Maste @param bufev An existing bufferevent allocated with bufferevent_socket_new() 220*c43e99fdSEd Maste @param evdns_base Optionally, an evdns_base to use for resolving hostnames 221*c43e99fdSEd Maste asynchronously. May be set to NULL for a blocking resolve. 222*c43e99fdSEd Maste @param family A preferred address family to resolve addresses to, or 223*c43e99fdSEd Maste AF_UNSPEC for no preference. Only AF_INET, AF_INET6, and AF_UNSPEC are 224*c43e99fdSEd Maste supported. 225*c43e99fdSEd Maste @param hostname The hostname to resolve; see below for notes on recognized 226*c43e99fdSEd Maste formats 227*c43e99fdSEd Maste @param port The port to connect to on the resolved address. 228*c43e99fdSEd Maste @return 0 if successful, -1 on failure. 229*c43e99fdSEd Maste 230*c43e99fdSEd Maste Recognized hostname formats are: 231*c43e99fdSEd Maste 232*c43e99fdSEd Maste www.example.com (hostname) 233*c43e99fdSEd Maste 1.2.3.4 (ipv4address) 234*c43e99fdSEd Maste ::1 (ipv6address) 235*c43e99fdSEd Maste [::1] ([ipv6address]) 236*c43e99fdSEd Maste 237*c43e99fdSEd Maste Performance note: If you do not provide an evdns_base, this function 238*c43e99fdSEd Maste may block while it waits for a DNS response. This is probably not 239*c43e99fdSEd Maste what you want. 240*c43e99fdSEd Maste */ 241*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 242*c43e99fdSEd Maste int bufferevent_socket_connect_hostname(struct bufferevent *, 243*c43e99fdSEd Maste struct evdns_base *, int, const char *, int); 244*c43e99fdSEd Maste 245*c43e99fdSEd Maste /** 246*c43e99fdSEd Maste Return the error code for the last failed DNS lookup attempt made by 247*c43e99fdSEd Maste bufferevent_socket_connect_hostname(). 248*c43e99fdSEd Maste 249*c43e99fdSEd Maste @param bev The bufferevent object. 250*c43e99fdSEd Maste @return DNS error code. 251*c43e99fdSEd Maste @see evutil_gai_strerror() 252*c43e99fdSEd Maste */ 253*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 254*c43e99fdSEd Maste int bufferevent_socket_get_dns_error(struct bufferevent *bev); 255*c43e99fdSEd Maste 256*c43e99fdSEd Maste /** 257*c43e99fdSEd Maste Assign a bufferevent to a specific event_base. 258*c43e99fdSEd Maste 259*c43e99fdSEd Maste NOTE that only socket bufferevents support this function. 260*c43e99fdSEd Maste 261*c43e99fdSEd Maste @param base an event_base returned by event_init() 262*c43e99fdSEd Maste @param bufev a bufferevent struct returned by bufferevent_new() 263*c43e99fdSEd Maste or bufferevent_socket_new() 264*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 265*c43e99fdSEd Maste @see bufferevent_new() 266*c43e99fdSEd Maste */ 267*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 268*c43e99fdSEd Maste int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); 269*c43e99fdSEd Maste 270*c43e99fdSEd Maste /** 271*c43e99fdSEd Maste Return the event_base used by a bufferevent 272*c43e99fdSEd Maste */ 273*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 274*c43e99fdSEd Maste struct event_base *bufferevent_get_base(struct bufferevent *bev); 275*c43e99fdSEd Maste 276*c43e99fdSEd Maste /** 277*c43e99fdSEd Maste Assign a priority to a bufferevent. 278*c43e99fdSEd Maste 279*c43e99fdSEd Maste Only supported for socket bufferevents. 280*c43e99fdSEd Maste 281*c43e99fdSEd Maste @param bufev a bufferevent struct 282*c43e99fdSEd Maste @param pri the priority to be assigned 283*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 284*c43e99fdSEd Maste */ 285*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 286*c43e99fdSEd Maste int bufferevent_priority_set(struct bufferevent *bufev, int pri); 287*c43e99fdSEd Maste 288*c43e99fdSEd Maste /** 289*c43e99fdSEd Maste Return the priority of a bufferevent. 290*c43e99fdSEd Maste 291*c43e99fdSEd Maste Only supported for socket bufferevents 292*c43e99fdSEd Maste */ 293*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 294*c43e99fdSEd Maste int bufferevent_get_priority(const struct bufferevent *bufev); 295*c43e99fdSEd Maste 296*c43e99fdSEd Maste /** 297*c43e99fdSEd Maste Deallocate the storage associated with a bufferevent structure. 298*c43e99fdSEd Maste 299*c43e99fdSEd Maste If there is pending data to write on the bufferevent, it probably won't be 300*c43e99fdSEd Maste flushed before the bufferevent is freed. 301*c43e99fdSEd Maste 302*c43e99fdSEd Maste @param bufev the bufferevent structure to be freed. 303*c43e99fdSEd Maste */ 304*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 305*c43e99fdSEd Maste void bufferevent_free(struct bufferevent *bufev); 306*c43e99fdSEd Maste 307*c43e99fdSEd Maste 308*c43e99fdSEd Maste /** 309*c43e99fdSEd Maste Changes the callbacks for a bufferevent. 310*c43e99fdSEd Maste 311*c43e99fdSEd Maste @param bufev the bufferevent object for which to change callbacks 312*c43e99fdSEd Maste @param readcb callback to invoke when there is data to be read, or NULL if 313*c43e99fdSEd Maste no callback is desired 314*c43e99fdSEd Maste @param writecb callback to invoke when the file descriptor is ready for 315*c43e99fdSEd Maste writing, or NULL if no callback is desired 316*c43e99fdSEd Maste @param eventcb callback to invoke when there is an event on the file 317*c43e99fdSEd Maste descriptor 318*c43e99fdSEd Maste @param cbarg an argument that will be supplied to each of the callbacks 319*c43e99fdSEd Maste (readcb, writecb, and errorcb) 320*c43e99fdSEd Maste @see bufferevent_new() 321*c43e99fdSEd Maste */ 322*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 323*c43e99fdSEd Maste void bufferevent_setcb(struct bufferevent *bufev, 324*c43e99fdSEd Maste bufferevent_data_cb readcb, bufferevent_data_cb writecb, 325*c43e99fdSEd Maste bufferevent_event_cb eventcb, void *cbarg); 326*c43e99fdSEd Maste 327*c43e99fdSEd Maste /** 328*c43e99fdSEd Maste Retrieves the callbacks for a bufferevent. 329*c43e99fdSEd Maste 330*c43e99fdSEd Maste @param bufev the bufferevent to examine. 331*c43e99fdSEd Maste @param readcb_ptr if readcb_ptr is nonnull, *readcb_ptr is set to the current 332*c43e99fdSEd Maste read callback for the bufferevent. 333*c43e99fdSEd Maste @param writecb_ptr if writecb_ptr is nonnull, *writecb_ptr is set to the 334*c43e99fdSEd Maste current write callback for the bufferevent. 335*c43e99fdSEd Maste @param eventcb_ptr if eventcb_ptr is nonnull, *eventcb_ptr is set to the 336*c43e99fdSEd Maste current event callback for the bufferevent. 337*c43e99fdSEd Maste @param cbarg_ptr if cbarg_ptr is nonnull, *cbarg_ptr is set to the current 338*c43e99fdSEd Maste callback argument for the bufferevent. 339*c43e99fdSEd Maste @see buffervent_setcb() 340*c43e99fdSEd Maste */ 341*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 342*c43e99fdSEd Maste void bufferevent_getcb(struct bufferevent *bufev, 343*c43e99fdSEd Maste bufferevent_data_cb *readcb_ptr, 344*c43e99fdSEd Maste bufferevent_data_cb *writecb_ptr, 345*c43e99fdSEd Maste bufferevent_event_cb *eventcb_ptr, 346*c43e99fdSEd Maste void **cbarg_ptr); 347*c43e99fdSEd Maste 348*c43e99fdSEd Maste /** 349*c43e99fdSEd Maste Changes the file descriptor on which the bufferevent operates. 350*c43e99fdSEd Maste Not supported for all bufferevent types. 351*c43e99fdSEd Maste 352*c43e99fdSEd Maste @param bufev the bufferevent object for which to change the file descriptor 353*c43e99fdSEd Maste @param fd the file descriptor to operate on 354*c43e99fdSEd Maste */ 355*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 356*c43e99fdSEd Maste int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd); 357*c43e99fdSEd Maste 358*c43e99fdSEd Maste /** 359*c43e99fdSEd Maste Returns the file descriptor associated with a bufferevent, or -1 if 360*c43e99fdSEd Maste no file descriptor is associated with the bufferevent. 361*c43e99fdSEd Maste */ 362*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 363*c43e99fdSEd Maste evutil_socket_t bufferevent_getfd(struct bufferevent *bufev); 364*c43e99fdSEd Maste 365*c43e99fdSEd Maste /** 366*c43e99fdSEd Maste Returns the underlying bufferevent associated with a bufferevent (if 367*c43e99fdSEd Maste the bufferevent is a wrapper), or NULL if there is no underlying bufferevent. 368*c43e99fdSEd Maste */ 369*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 370*c43e99fdSEd Maste struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev); 371*c43e99fdSEd Maste 372*c43e99fdSEd Maste /** 373*c43e99fdSEd Maste Write data to a bufferevent buffer. 374*c43e99fdSEd Maste 375*c43e99fdSEd Maste The bufferevent_write() function can be used to write data to the file 376*c43e99fdSEd Maste descriptor. The data is appended to the output buffer and written to the 377*c43e99fdSEd Maste descriptor automatically as it becomes available for writing. 378*c43e99fdSEd Maste 379*c43e99fdSEd Maste @param bufev the bufferevent to be written to 380*c43e99fdSEd Maste @param data a pointer to the data to be written 381*c43e99fdSEd Maste @param size the length of the data, in bytes 382*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 383*c43e99fdSEd Maste @see bufferevent_write_buffer() 384*c43e99fdSEd Maste */ 385*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 386*c43e99fdSEd Maste int bufferevent_write(struct bufferevent *bufev, 387*c43e99fdSEd Maste const void *data, size_t size); 388*c43e99fdSEd Maste 389*c43e99fdSEd Maste 390*c43e99fdSEd Maste /** 391*c43e99fdSEd Maste Write data from an evbuffer to a bufferevent buffer. The evbuffer is 392*c43e99fdSEd Maste being drained as a result. 393*c43e99fdSEd Maste 394*c43e99fdSEd Maste @param bufev the bufferevent to be written to 395*c43e99fdSEd Maste @param buf the evbuffer to be written 396*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 397*c43e99fdSEd Maste @see bufferevent_write() 398*c43e99fdSEd Maste */ 399*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 400*c43e99fdSEd Maste int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); 401*c43e99fdSEd Maste 402*c43e99fdSEd Maste 403*c43e99fdSEd Maste /** 404*c43e99fdSEd Maste Read data from a bufferevent buffer. 405*c43e99fdSEd Maste 406*c43e99fdSEd Maste The bufferevent_read() function is used to read data from the input buffer. 407*c43e99fdSEd Maste 408*c43e99fdSEd Maste @param bufev the bufferevent to be read from 409*c43e99fdSEd Maste @param data pointer to a buffer that will store the data 410*c43e99fdSEd Maste @param size the size of the data buffer, in bytes 411*c43e99fdSEd Maste @return the amount of data read, in bytes. 412*c43e99fdSEd Maste */ 413*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 414*c43e99fdSEd Maste size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); 415*c43e99fdSEd Maste 416*c43e99fdSEd Maste /** 417*c43e99fdSEd Maste Read data from a bufferevent buffer into an evbuffer. This avoids 418*c43e99fdSEd Maste memory copies. 419*c43e99fdSEd Maste 420*c43e99fdSEd Maste @param bufev the bufferevent to be read from 421*c43e99fdSEd Maste @param buf the evbuffer to which to add data 422*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred. 423*c43e99fdSEd Maste */ 424*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 425*c43e99fdSEd Maste int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf); 426*c43e99fdSEd Maste 427*c43e99fdSEd Maste /** 428*c43e99fdSEd Maste Returns the input buffer. 429*c43e99fdSEd Maste 430*c43e99fdSEd Maste The user MUST NOT set the callback on this buffer. 431*c43e99fdSEd Maste 432*c43e99fdSEd Maste @param bufev the bufferevent from which to get the evbuffer 433*c43e99fdSEd Maste @return the evbuffer object for the input buffer 434*c43e99fdSEd Maste */ 435*c43e99fdSEd Maste 436*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 437*c43e99fdSEd Maste struct evbuffer *bufferevent_get_input(struct bufferevent *bufev); 438*c43e99fdSEd Maste 439*c43e99fdSEd Maste /** 440*c43e99fdSEd Maste Returns the output buffer. 441*c43e99fdSEd Maste 442*c43e99fdSEd Maste The user MUST NOT set the callback on this buffer. 443*c43e99fdSEd Maste 444*c43e99fdSEd Maste When filters are being used, the filters need to be manually 445*c43e99fdSEd Maste triggered if the output buffer was manipulated. 446*c43e99fdSEd Maste 447*c43e99fdSEd Maste @param bufev the bufferevent from which to get the evbuffer 448*c43e99fdSEd Maste @return the evbuffer object for the output buffer 449*c43e99fdSEd Maste */ 450*c43e99fdSEd Maste 451*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 452*c43e99fdSEd Maste struct evbuffer *bufferevent_get_output(struct bufferevent *bufev); 453*c43e99fdSEd Maste 454*c43e99fdSEd Maste /** 455*c43e99fdSEd Maste Enable a bufferevent. 456*c43e99fdSEd Maste 457*c43e99fdSEd Maste @param bufev the bufferevent to be enabled 458*c43e99fdSEd Maste @param event any combination of EV_READ | EV_WRITE. 459*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 460*c43e99fdSEd Maste @see bufferevent_disable() 461*c43e99fdSEd Maste */ 462*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 463*c43e99fdSEd Maste int bufferevent_enable(struct bufferevent *bufev, short event); 464*c43e99fdSEd Maste 465*c43e99fdSEd Maste /** 466*c43e99fdSEd Maste Disable a bufferevent. 467*c43e99fdSEd Maste 468*c43e99fdSEd Maste @param bufev the bufferevent to be disabled 469*c43e99fdSEd Maste @param event any combination of EV_READ | EV_WRITE. 470*c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred 471*c43e99fdSEd Maste @see bufferevent_enable() 472*c43e99fdSEd Maste */ 473*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 474*c43e99fdSEd Maste int bufferevent_disable(struct bufferevent *bufev, short event); 475*c43e99fdSEd Maste 476*c43e99fdSEd Maste /** 477*c43e99fdSEd Maste Return the events that are enabled on a given bufferevent. 478*c43e99fdSEd Maste 479*c43e99fdSEd Maste @param bufev the bufferevent to inspect 480*c43e99fdSEd Maste @return A combination of EV_READ | EV_WRITE 481*c43e99fdSEd Maste */ 482*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 483*c43e99fdSEd Maste short bufferevent_get_enabled(struct bufferevent *bufev); 484*c43e99fdSEd Maste 485*c43e99fdSEd Maste /** 486*c43e99fdSEd Maste Set the read and write timeout for a bufferevent. 487*c43e99fdSEd Maste 488*c43e99fdSEd Maste A bufferevent's timeout will fire the first time that the indicated 489*c43e99fdSEd Maste amount of time has elapsed since a successful read or write operation, 490*c43e99fdSEd Maste during which the bufferevent was trying to read or write. 491*c43e99fdSEd Maste 492*c43e99fdSEd Maste (In other words, if reading or writing is disabled, or if the 493*c43e99fdSEd Maste bufferevent's read or write operation has been suspended because 494*c43e99fdSEd Maste there's no data to write, or not enough banwidth, or so on, the 495*c43e99fdSEd Maste timeout isn't active. The timeout only becomes active when we we're 496*c43e99fdSEd Maste willing to actually read or write.) 497*c43e99fdSEd Maste 498*c43e99fdSEd Maste Calling bufferevent_enable or setting a timeout for a bufferevent 499*c43e99fdSEd Maste whose timeout is already pending resets its timeout. 500*c43e99fdSEd Maste 501*c43e99fdSEd Maste If the timeout elapses, the corresponding operation (EV_READ or 502*c43e99fdSEd Maste EV_WRITE) becomes disabled until you re-enable it again. The 503*c43e99fdSEd Maste bufferevent's event callback is called with the 504*c43e99fdSEd Maste BEV_EVENT_TIMEOUT|BEV_EVENT_READING or 505*c43e99fdSEd Maste BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING. 506*c43e99fdSEd Maste 507*c43e99fdSEd Maste @param bufev the bufferevent to be modified 508*c43e99fdSEd Maste @param timeout_read the read timeout, or NULL 509*c43e99fdSEd Maste @param timeout_write the write timeout, or NULL 510*c43e99fdSEd Maste */ 511*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 512*c43e99fdSEd Maste int bufferevent_set_timeouts(struct bufferevent *bufev, 513*c43e99fdSEd Maste const struct timeval *timeout_read, const struct timeval *timeout_write); 514*c43e99fdSEd Maste 515*c43e99fdSEd Maste /** 516*c43e99fdSEd Maste Sets the watermarks for read and write events. 517*c43e99fdSEd Maste 518*c43e99fdSEd Maste On input, a bufferevent does not invoke the user read callback unless 519*c43e99fdSEd Maste there is at least low watermark data in the buffer. If the read buffer 520*c43e99fdSEd Maste is beyond the high watermark, the bufferevent stops reading from the network. 521*c43e99fdSEd Maste 522*c43e99fdSEd Maste On output, the user write callback is invoked whenever the buffered data 523*c43e99fdSEd Maste falls below the low watermark. Filters that write to this bufev will try 524*c43e99fdSEd Maste not to write more bytes to this buffer than the high watermark would allow, 525*c43e99fdSEd Maste except when flushing. 526*c43e99fdSEd Maste 527*c43e99fdSEd Maste @param bufev the bufferevent to be modified 528*c43e99fdSEd Maste @param events EV_READ, EV_WRITE or both 529*c43e99fdSEd Maste @param lowmark the lower watermark to set 530*c43e99fdSEd Maste @param highmark the high watermark to set 531*c43e99fdSEd Maste */ 532*c43e99fdSEd Maste 533*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 534*c43e99fdSEd Maste void bufferevent_setwatermark(struct bufferevent *bufev, short events, 535*c43e99fdSEd Maste size_t lowmark, size_t highmark); 536*c43e99fdSEd Maste 537*c43e99fdSEd Maste /** 538*c43e99fdSEd Maste Retrieves the watermarks for read or write events. 539*c43e99fdSEd Maste Returns non-zero if events contains not only EV_READ or EV_WRITE. 540*c43e99fdSEd Maste Returns zero if events equal EV_READ or EV_WRITE 541*c43e99fdSEd Maste 542*c43e99fdSEd Maste @param bufev the bufferevent to be examined 543*c43e99fdSEd Maste @param events EV_READ or EV_WRITE 544*c43e99fdSEd Maste @param lowmark receives the lower watermark if not NULL 545*c43e99fdSEd Maste @param highmark receives the high watermark if not NULL 546*c43e99fdSEd Maste */ 547*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 548*c43e99fdSEd Maste int bufferevent_getwatermark(struct bufferevent *bufev, short events, 549*c43e99fdSEd Maste size_t *lowmark, size_t *highmark); 550*c43e99fdSEd Maste 551*c43e99fdSEd Maste /** 552*c43e99fdSEd Maste Acquire the lock on a bufferevent. Has no effect if locking was not 553*c43e99fdSEd Maste enabled with BEV_OPT_THREADSAFE. 554*c43e99fdSEd Maste */ 555*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 556*c43e99fdSEd Maste void bufferevent_lock(struct bufferevent *bufev); 557*c43e99fdSEd Maste 558*c43e99fdSEd Maste /** 559*c43e99fdSEd Maste Release the lock on a bufferevent. Has no effect if locking was not 560*c43e99fdSEd Maste enabled with BEV_OPT_THREADSAFE. 561*c43e99fdSEd Maste */ 562*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 563*c43e99fdSEd Maste void bufferevent_unlock(struct bufferevent *bufev); 564*c43e99fdSEd Maste 565*c43e99fdSEd Maste 566*c43e99fdSEd Maste /** 567*c43e99fdSEd Maste * Public interface to manually increase the reference count of a bufferevent 568*c43e99fdSEd Maste * this is useful in situations where a user may reference the bufferevent 569*c43e99fdSEd Maste * somewhere eles (unknown to libevent) 570*c43e99fdSEd Maste * 571*c43e99fdSEd Maste * @param bufev the bufferevent to increase the refcount on 572*c43e99fdSEd Maste * 573*c43e99fdSEd Maste */ 574*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 575*c43e99fdSEd Maste void bufferevent_incref(struct bufferevent *bufev); 576*c43e99fdSEd Maste 577*c43e99fdSEd Maste /** 578*c43e99fdSEd Maste * Public interface to manually decrement the reference count of a bufferevent 579*c43e99fdSEd Maste * 580*c43e99fdSEd Maste * Warning: make sure you know what you're doing. This is mainly used in 581*c43e99fdSEd Maste * conjunction with bufferevent_incref(). This will free up all data associated 582*c43e99fdSEd Maste * with a bufferevent if the reference count hits 0. 583*c43e99fdSEd Maste * 584*c43e99fdSEd Maste * @param bufev the bufferevent to decrement the refcount on 585*c43e99fdSEd Maste * 586*c43e99fdSEd Maste * @return 1 if the bufferevent was freed, otherwise 0 (still referenced) 587*c43e99fdSEd Maste */ 588*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 589*c43e99fdSEd Maste int bufferevent_decref(struct bufferevent *bufev); 590*c43e99fdSEd Maste 591*c43e99fdSEd Maste /** 592*c43e99fdSEd Maste Flags that can be passed into filters to let them know how to 593*c43e99fdSEd Maste deal with the incoming data. 594*c43e99fdSEd Maste */ 595*c43e99fdSEd Maste enum bufferevent_flush_mode { 596*c43e99fdSEd Maste /** usually set when processing data */ 597*c43e99fdSEd Maste BEV_NORMAL = 0, 598*c43e99fdSEd Maste 599*c43e99fdSEd Maste /** want to checkpoint all data sent. */ 600*c43e99fdSEd Maste BEV_FLUSH = 1, 601*c43e99fdSEd Maste 602*c43e99fdSEd Maste /** encountered EOF on read or done sending data */ 603*c43e99fdSEd Maste BEV_FINISHED = 2 604*c43e99fdSEd Maste }; 605*c43e99fdSEd Maste 606*c43e99fdSEd Maste /** 607*c43e99fdSEd Maste Triggers the bufferevent to produce more data if possible. 608*c43e99fdSEd Maste 609*c43e99fdSEd Maste @param bufev the bufferevent object 610*c43e99fdSEd Maste @param iotype either EV_READ or EV_WRITE or both. 611*c43e99fdSEd Maste @param mode either BEV_NORMAL or BEV_FLUSH or BEV_FINISHED 612*c43e99fdSEd Maste @return -1 on failure, 0 if no data was produces, 1 if data was produced 613*c43e99fdSEd Maste */ 614*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 615*c43e99fdSEd Maste int bufferevent_flush(struct bufferevent *bufev, 616*c43e99fdSEd Maste short iotype, 617*c43e99fdSEd Maste enum bufferevent_flush_mode mode); 618*c43e99fdSEd Maste 619*c43e99fdSEd Maste /** 620*c43e99fdSEd Maste Flags for bufferevent_trigger(_event) that modify when and how to trigger 621*c43e99fdSEd Maste the callback. 622*c43e99fdSEd Maste */ 623*c43e99fdSEd Maste enum bufferevent_trigger_options { 624*c43e99fdSEd Maste /** trigger the callback regardless of the watermarks */ 625*c43e99fdSEd Maste BEV_TRIG_IGNORE_WATERMARKS = (1<<16), 626*c43e99fdSEd Maste 627*c43e99fdSEd Maste /** defer even if the callbacks are not */ 628*c43e99fdSEd Maste BEV_TRIG_DEFER_CALLBACKS = BEV_OPT_DEFER_CALLBACKS 629*c43e99fdSEd Maste 630*c43e99fdSEd Maste /* (Note: for internal reasons, these need to be disjoint from 631*c43e99fdSEd Maste * bufferevent_options, except when they mean the same thing. */ 632*c43e99fdSEd Maste }; 633*c43e99fdSEd Maste 634*c43e99fdSEd Maste /** 635*c43e99fdSEd Maste Triggers bufferevent data callbacks. 636*c43e99fdSEd Maste 637*c43e99fdSEd Maste The function will honor watermarks unless options contain 638*c43e99fdSEd Maste BEV_TRIG_IGNORE_WATERMARKS. If the options contain BEV_OPT_DEFER_CALLBACKS, 639*c43e99fdSEd Maste the callbacks are deferred. 640*c43e99fdSEd Maste 641*c43e99fdSEd Maste @param bufev the bufferevent object 642*c43e99fdSEd Maste @param iotype either EV_READ or EV_WRITE or both. 643*c43e99fdSEd Maste @param options 644*c43e99fdSEd Maste */ 645*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 646*c43e99fdSEd Maste void bufferevent_trigger(struct bufferevent *bufev, short iotype, 647*c43e99fdSEd Maste int options); 648*c43e99fdSEd Maste 649*c43e99fdSEd Maste /** 650*c43e99fdSEd Maste Triggers the bufferevent event callback. 651*c43e99fdSEd Maste 652*c43e99fdSEd Maste If the options contain BEV_OPT_DEFER_CALLBACKS, the callbacks are deferred. 653*c43e99fdSEd Maste 654*c43e99fdSEd Maste @param bufev the bufferevent object 655*c43e99fdSEd Maste @param what the flags to pass onto the event callback 656*c43e99fdSEd Maste @param options 657*c43e99fdSEd Maste */ 658*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 659*c43e99fdSEd Maste void bufferevent_trigger_event(struct bufferevent *bufev, short what, 660*c43e99fdSEd Maste int options); 661*c43e99fdSEd Maste 662*c43e99fdSEd Maste /** 663*c43e99fdSEd Maste @name Filtering support 664*c43e99fdSEd Maste 665*c43e99fdSEd Maste @{ 666*c43e99fdSEd Maste */ 667*c43e99fdSEd Maste /** 668*c43e99fdSEd Maste Values that filters can return. 669*c43e99fdSEd Maste */ 670*c43e99fdSEd Maste enum bufferevent_filter_result { 671*c43e99fdSEd Maste /** everything is okay */ 672*c43e99fdSEd Maste BEV_OK = 0, 673*c43e99fdSEd Maste 674*c43e99fdSEd Maste /** the filter needs to read more data before output */ 675*c43e99fdSEd Maste BEV_NEED_MORE = 1, 676*c43e99fdSEd Maste 677*c43e99fdSEd Maste /** the filter encountered a critical error, no further data 678*c43e99fdSEd Maste can be processed. */ 679*c43e99fdSEd Maste BEV_ERROR = 2 680*c43e99fdSEd Maste }; 681*c43e99fdSEd Maste 682*c43e99fdSEd Maste /** A callback function to implement a filter for a bufferevent. 683*c43e99fdSEd Maste 684*c43e99fdSEd Maste @param src An evbuffer to drain data from. 685*c43e99fdSEd Maste @param dst An evbuffer to add data to. 686*c43e99fdSEd Maste @param limit A suggested upper bound of bytes to write to dst. 687*c43e99fdSEd Maste The filter may ignore this value, but doing so means that 688*c43e99fdSEd Maste it will overflow the high-water mark associated with dst. 689*c43e99fdSEd Maste -1 means "no limit". 690*c43e99fdSEd Maste @param mode Whether we should write data as may be convenient 691*c43e99fdSEd Maste (BEV_NORMAL), or flush as much data as we can (BEV_FLUSH), 692*c43e99fdSEd Maste or flush as much as we can, possibly including an end-of-stream 693*c43e99fdSEd Maste marker (BEV_FINISH). 694*c43e99fdSEd Maste @param ctx A user-supplied pointer. 695*c43e99fdSEd Maste 696*c43e99fdSEd Maste @return BEV_OK if we wrote some data; BEV_NEED_MORE if we can't 697*c43e99fdSEd Maste produce any more output until we get some input; and BEV_ERROR 698*c43e99fdSEd Maste on an error. 699*c43e99fdSEd Maste */ 700*c43e99fdSEd Maste typedef enum bufferevent_filter_result (*bufferevent_filter_cb)( 701*c43e99fdSEd Maste struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, 702*c43e99fdSEd Maste enum bufferevent_flush_mode mode, void *ctx); 703*c43e99fdSEd Maste 704*c43e99fdSEd Maste /** 705*c43e99fdSEd Maste Allocate a new filtering bufferevent on top of an existing bufferevent. 706*c43e99fdSEd Maste 707*c43e99fdSEd Maste @param underlying the underlying bufferevent. 708*c43e99fdSEd Maste @param input_filter The filter to apply to data we read from the underlying 709*c43e99fdSEd Maste bufferevent 710*c43e99fdSEd Maste @param output_filter The filer to apply to data we write to the underlying 711*c43e99fdSEd Maste bufferevent 712*c43e99fdSEd Maste @param options A bitfield of bufferevent options. 713*c43e99fdSEd Maste @param free_context A function to use to free the filter context when 714*c43e99fdSEd Maste this bufferevent is freed. 715*c43e99fdSEd Maste @param ctx A context pointer to pass to the filter functions. 716*c43e99fdSEd Maste */ 717*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 718*c43e99fdSEd Maste struct bufferevent * 719*c43e99fdSEd Maste bufferevent_filter_new(struct bufferevent *underlying, 720*c43e99fdSEd Maste bufferevent_filter_cb input_filter, 721*c43e99fdSEd Maste bufferevent_filter_cb output_filter, 722*c43e99fdSEd Maste int options, 723*c43e99fdSEd Maste void (*free_context)(void *), 724*c43e99fdSEd Maste void *ctx); 725*c43e99fdSEd Maste /**@}*/ 726*c43e99fdSEd Maste 727*c43e99fdSEd Maste /** 728*c43e99fdSEd Maste Allocate a pair of linked bufferevents. The bufferevents behave as would 729*c43e99fdSEd Maste two bufferevent_sock instances connected to opposite ends of a 730*c43e99fdSEd Maste socketpair(), except that no internal socketpair is allocated. 731*c43e99fdSEd Maste 732*c43e99fdSEd Maste @param base The event base to associate with the socketpair. 733*c43e99fdSEd Maste @param options A set of options for this bufferevent 734*c43e99fdSEd Maste @param pair A pointer to an array to hold the two new bufferevent objects. 735*c43e99fdSEd Maste @return 0 on success, -1 on failure. 736*c43e99fdSEd Maste */ 737*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 738*c43e99fdSEd Maste int bufferevent_pair_new(struct event_base *base, int options, 739*c43e99fdSEd Maste struct bufferevent *pair[2]); 740*c43e99fdSEd Maste 741*c43e99fdSEd Maste /** 742*c43e99fdSEd Maste Given one bufferevent returned by bufferevent_pair_new(), returns the 743*c43e99fdSEd Maste other one if it still exists. Otherwise returns NULL. 744*c43e99fdSEd Maste */ 745*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 746*c43e99fdSEd Maste struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev); 747*c43e99fdSEd Maste 748*c43e99fdSEd Maste /** 749*c43e99fdSEd Maste Abstract type used to configure rate-limiting on a bufferevent or a group 750*c43e99fdSEd Maste of bufferevents. 751*c43e99fdSEd Maste */ 752*c43e99fdSEd Maste struct ev_token_bucket_cfg; 753*c43e99fdSEd Maste 754*c43e99fdSEd Maste /** 755*c43e99fdSEd Maste A group of bufferevents which are configured to respect the same rate 756*c43e99fdSEd Maste limit. 757*c43e99fdSEd Maste */ 758*c43e99fdSEd Maste struct bufferevent_rate_limit_group; 759*c43e99fdSEd Maste 760*c43e99fdSEd Maste /** Maximum configurable rate- or burst-limit. */ 761*c43e99fdSEd Maste #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX 762*c43e99fdSEd Maste 763*c43e99fdSEd Maste /** 764*c43e99fdSEd Maste Initialize and return a new object to configure the rate-limiting behavior 765*c43e99fdSEd Maste of bufferevents. 766*c43e99fdSEd Maste 767*c43e99fdSEd Maste @param read_rate The maximum number of bytes to read per tick on 768*c43e99fdSEd Maste average. 769*c43e99fdSEd Maste @param read_burst The maximum number of bytes to read in any single tick. 770*c43e99fdSEd Maste @param write_rate The maximum number of bytes to write per tick on 771*c43e99fdSEd Maste average. 772*c43e99fdSEd Maste @param write_burst The maximum number of bytes to write in any single tick. 773*c43e99fdSEd Maste @param tick_len The length of a single tick. Defaults to one second. 774*c43e99fdSEd Maste Any fractions of a millisecond are ignored. 775*c43e99fdSEd Maste 776*c43e99fdSEd Maste Note that all rate-limits hare are currently best-effort: future versions 777*c43e99fdSEd Maste of Libevent may implement them more tightly. 778*c43e99fdSEd Maste */ 779*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 780*c43e99fdSEd Maste struct ev_token_bucket_cfg *ev_token_bucket_cfg_new( 781*c43e99fdSEd Maste size_t read_rate, size_t read_burst, 782*c43e99fdSEd Maste size_t write_rate, size_t write_burst, 783*c43e99fdSEd Maste const struct timeval *tick_len); 784*c43e99fdSEd Maste 785*c43e99fdSEd Maste /** Free all storage held in 'cfg'. 786*c43e99fdSEd Maste 787*c43e99fdSEd Maste Note: 'cfg' is not currently reference-counted; it is not safe to free it 788*c43e99fdSEd Maste until no bufferevent is using it. 789*c43e99fdSEd Maste */ 790*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 791*c43e99fdSEd Maste void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg); 792*c43e99fdSEd Maste 793*c43e99fdSEd Maste /** 794*c43e99fdSEd Maste Set the rate-limit of a the bufferevent 'bev' to the one specified in 795*c43e99fdSEd Maste 'cfg'. If 'cfg' is NULL, disable any per-bufferevent rate-limiting on 796*c43e99fdSEd Maste 'bev'. 797*c43e99fdSEd Maste 798*c43e99fdSEd Maste Note that only some bufferevent types currently respect rate-limiting. 799*c43e99fdSEd Maste They are: socket-based bufferevents (normal and IOCP-based), and SSL-based 800*c43e99fdSEd Maste bufferevents. 801*c43e99fdSEd Maste 802*c43e99fdSEd Maste Return 0 on sucess, -1 on failure. 803*c43e99fdSEd Maste */ 804*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 805*c43e99fdSEd Maste int bufferevent_set_rate_limit(struct bufferevent *bev, 806*c43e99fdSEd Maste struct ev_token_bucket_cfg *cfg); 807*c43e99fdSEd Maste 808*c43e99fdSEd Maste /** 809*c43e99fdSEd Maste Create a new rate-limit group for bufferevents. A rate-limit group 810*c43e99fdSEd Maste constrains the maximum number of bytes sent and received, in toto, 811*c43e99fdSEd Maste by all of its bufferevents. 812*c43e99fdSEd Maste 813*c43e99fdSEd Maste @param base An event_base to run any necessary timeouts for the group. 814*c43e99fdSEd Maste Note that all bufferevents in the group do not necessarily need to share 815*c43e99fdSEd Maste this event_base. 816*c43e99fdSEd Maste @param cfg The rate-limit for this group. 817*c43e99fdSEd Maste 818*c43e99fdSEd Maste Note that all rate-limits hare are currently best-effort: future versions 819*c43e99fdSEd Maste of Libevent may implement them more tightly. 820*c43e99fdSEd Maste 821*c43e99fdSEd Maste Note also that only some bufferevent types currently respect rate-limiting. 822*c43e99fdSEd Maste They are: socket-based bufferevents (normal and IOCP-based), and SSL-based 823*c43e99fdSEd Maste bufferevents. 824*c43e99fdSEd Maste */ 825*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 826*c43e99fdSEd Maste struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new( 827*c43e99fdSEd Maste struct event_base *base, 828*c43e99fdSEd Maste const struct ev_token_bucket_cfg *cfg); 829*c43e99fdSEd Maste /** 830*c43e99fdSEd Maste Change the rate-limiting settings for a given rate-limiting group. 831*c43e99fdSEd Maste 832*c43e99fdSEd Maste Return 0 on success, -1 on failure. 833*c43e99fdSEd Maste */ 834*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 835*c43e99fdSEd Maste int bufferevent_rate_limit_group_set_cfg( 836*c43e99fdSEd Maste struct bufferevent_rate_limit_group *, 837*c43e99fdSEd Maste const struct ev_token_bucket_cfg *); 838*c43e99fdSEd Maste 839*c43e99fdSEd Maste /** 840*c43e99fdSEd Maste Change the smallest quantum we're willing to allocate to any single 841*c43e99fdSEd Maste bufferevent in a group for reading or writing at a time. 842*c43e99fdSEd Maste 843*c43e99fdSEd Maste The rationale is that, because of TCP/IP protocol overheads and kernel 844*c43e99fdSEd Maste behavior, if a rate-limiting group is so tight on bandwidth that you're 845*c43e99fdSEd Maste only willing to send 1 byte per tick per bufferevent, you might instead 846*c43e99fdSEd Maste want to batch up the reads and writes so that you send N bytes per 847*c43e99fdSEd Maste 1/N of the bufferevents (chosen at random) each tick, so you still wind 848*c43e99fdSEd Maste up send 1 byte per tick per bufferevent on average, but you don't send 849*c43e99fdSEd Maste so many tiny packets. 850*c43e99fdSEd Maste 851*c43e99fdSEd Maste The default min-share is currently 64 bytes. 852*c43e99fdSEd Maste 853*c43e99fdSEd Maste Returns 0 on success, -1 on faulre. 854*c43e99fdSEd Maste */ 855*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 856*c43e99fdSEd Maste int bufferevent_rate_limit_group_set_min_share( 857*c43e99fdSEd Maste struct bufferevent_rate_limit_group *, size_t); 858*c43e99fdSEd Maste 859*c43e99fdSEd Maste /** 860*c43e99fdSEd Maste Free a rate-limiting group. The group must have no members when 861*c43e99fdSEd Maste this function is called. 862*c43e99fdSEd Maste */ 863*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 864*c43e99fdSEd Maste void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *); 865*c43e99fdSEd Maste 866*c43e99fdSEd Maste /** 867*c43e99fdSEd Maste Add 'bev' to the list of bufferevents whose aggregate reading and writing 868*c43e99fdSEd Maste is restricted by 'g'. If 'g' is NULL, remove 'bev' from its current group. 869*c43e99fdSEd Maste 870*c43e99fdSEd Maste A bufferevent may belong to no more than one rate-limit group at a time. 871*c43e99fdSEd Maste If 'bev' is already a member of a group, it will be removed from its old 872*c43e99fdSEd Maste group before being added to 'g'. 873*c43e99fdSEd Maste 874*c43e99fdSEd Maste Return 0 on success and -1 on failure. 875*c43e99fdSEd Maste */ 876*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 877*c43e99fdSEd Maste int bufferevent_add_to_rate_limit_group(struct bufferevent *bev, 878*c43e99fdSEd Maste struct bufferevent_rate_limit_group *g); 879*c43e99fdSEd Maste 880*c43e99fdSEd Maste /** Remove 'bev' from its current rate-limit group (if any). */ 881*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 882*c43e99fdSEd Maste int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev); 883*c43e99fdSEd Maste 884*c43e99fdSEd Maste /** 885*c43e99fdSEd Maste Set the size limit for single read operation. 886*c43e99fdSEd Maste 887*c43e99fdSEd Maste Set to 0 for a reasonable default. 888*c43e99fdSEd Maste 889*c43e99fdSEd Maste Return 0 on success and -1 on failure. 890*c43e99fdSEd Maste */ 891*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 892*c43e99fdSEd Maste int bufferevent_set_max_single_read(struct bufferevent *bev, size_t size); 893*c43e99fdSEd Maste 894*c43e99fdSEd Maste /** 895*c43e99fdSEd Maste Set the size limit for single write operation. 896*c43e99fdSEd Maste 897*c43e99fdSEd Maste Set to 0 for a reasonable default. 898*c43e99fdSEd Maste 899*c43e99fdSEd Maste Return 0 on success and -1 on failure. 900*c43e99fdSEd Maste */ 901*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 902*c43e99fdSEd Maste int bufferevent_set_max_single_write(struct bufferevent *bev, size_t size); 903*c43e99fdSEd Maste 904*c43e99fdSEd Maste /** Get the current size limit for single read operation. */ 905*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 906*c43e99fdSEd Maste ev_ssize_t bufferevent_get_max_single_read(struct bufferevent *bev); 907*c43e99fdSEd Maste 908*c43e99fdSEd Maste /** Get the current size limit for single write operation. */ 909*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 910*c43e99fdSEd Maste ev_ssize_t bufferevent_get_max_single_write(struct bufferevent *bev); 911*c43e99fdSEd Maste 912*c43e99fdSEd Maste /** 913*c43e99fdSEd Maste @name Rate limit inspection 914*c43e99fdSEd Maste 915*c43e99fdSEd Maste Return the current read or write bucket size for a bufferevent. 916*c43e99fdSEd Maste If it is not configured with a per-bufferevent ratelimit, return 917*c43e99fdSEd Maste EV_SSIZE_MAX. This function does not inspect the group limit, if any. 918*c43e99fdSEd Maste Note that it can return a negative value if the bufferevent has been 919*c43e99fdSEd Maste made to read or write more than its limit. 920*c43e99fdSEd Maste 921*c43e99fdSEd Maste @{ 922*c43e99fdSEd Maste */ 923*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 924*c43e99fdSEd Maste ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev); 925*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 926*c43e99fdSEd Maste ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev); 927*c43e99fdSEd Maste /*@}*/ 928*c43e99fdSEd Maste 929*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 930*c43e99fdSEd Maste ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev); 931*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 932*c43e99fdSEd Maste ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev); 933*c43e99fdSEd Maste 934*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 935*c43e99fdSEd Maste const struct ev_token_bucket_cfg *bufferevent_get_token_bucket_cfg(const struct bufferevent * bev); 936*c43e99fdSEd Maste 937*c43e99fdSEd Maste /** 938*c43e99fdSEd Maste @name Group Rate limit inspection 939*c43e99fdSEd Maste 940*c43e99fdSEd Maste Return the read or write bucket size for a bufferevent rate limit 941*c43e99fdSEd Maste group. Note that it can return a negative value if bufferevents in 942*c43e99fdSEd Maste the group have been made to read or write more than their limits. 943*c43e99fdSEd Maste 944*c43e99fdSEd Maste @{ 945*c43e99fdSEd Maste */ 946*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 947*c43e99fdSEd Maste ev_ssize_t bufferevent_rate_limit_group_get_read_limit( 948*c43e99fdSEd Maste struct bufferevent_rate_limit_group *); 949*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 950*c43e99fdSEd Maste ev_ssize_t bufferevent_rate_limit_group_get_write_limit( 951*c43e99fdSEd Maste struct bufferevent_rate_limit_group *); 952*c43e99fdSEd Maste /*@}*/ 953*c43e99fdSEd Maste 954*c43e99fdSEd Maste /** 955*c43e99fdSEd Maste @name Rate limit manipulation 956*c43e99fdSEd Maste 957*c43e99fdSEd Maste Subtract a number of bytes from a bufferevent's read or write bucket. 958*c43e99fdSEd Maste The decrement value can be negative, if you want to manually refill 959*c43e99fdSEd Maste the bucket. If the change puts the bucket above or below zero, the 960*c43e99fdSEd Maste bufferevent will resume or suspend reading writing as appropriate. 961*c43e99fdSEd Maste These functions make no change in the buckets for the bufferevent's 962*c43e99fdSEd Maste group, if any. 963*c43e99fdSEd Maste 964*c43e99fdSEd Maste Returns 0 on success, -1 on internal error. 965*c43e99fdSEd Maste 966*c43e99fdSEd Maste @{ 967*c43e99fdSEd Maste */ 968*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 969*c43e99fdSEd Maste int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr); 970*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 971*c43e99fdSEd Maste int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr); 972*c43e99fdSEd Maste /*@}*/ 973*c43e99fdSEd Maste 974*c43e99fdSEd Maste /** 975*c43e99fdSEd Maste @name Group rate limit manipulation 976*c43e99fdSEd Maste 977*c43e99fdSEd Maste Subtract a number of bytes from a bufferevent rate-limiting group's 978*c43e99fdSEd Maste read or write bucket. The decrement value can be negative, if you 979*c43e99fdSEd Maste want to manually refill the bucket. If the change puts the bucket 980*c43e99fdSEd Maste above or below zero, the bufferevents in the group will resume or 981*c43e99fdSEd Maste suspend reading writing as appropriate. 982*c43e99fdSEd Maste 983*c43e99fdSEd Maste Returns 0 on success, -1 on internal error. 984*c43e99fdSEd Maste 985*c43e99fdSEd Maste @{ 986*c43e99fdSEd Maste */ 987*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 988*c43e99fdSEd Maste int bufferevent_rate_limit_group_decrement_read( 989*c43e99fdSEd Maste struct bufferevent_rate_limit_group *, ev_ssize_t); 990*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 991*c43e99fdSEd Maste int bufferevent_rate_limit_group_decrement_write( 992*c43e99fdSEd Maste struct bufferevent_rate_limit_group *, ev_ssize_t); 993*c43e99fdSEd Maste /*@}*/ 994*c43e99fdSEd Maste 995*c43e99fdSEd Maste 996*c43e99fdSEd Maste /** 997*c43e99fdSEd Maste * Inspect the total bytes read/written on a group. 998*c43e99fdSEd Maste * 999*c43e99fdSEd Maste * Set the variable pointed to by total_read_out to the total number of bytes 1000*c43e99fdSEd Maste * ever read on grp, and the variable pointed to by total_written_out to the 1001*c43e99fdSEd Maste * total number of bytes ever written on grp. */ 1002*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1003*c43e99fdSEd Maste void bufferevent_rate_limit_group_get_totals( 1004*c43e99fdSEd Maste struct bufferevent_rate_limit_group *grp, 1005*c43e99fdSEd Maste ev_uint64_t *total_read_out, ev_uint64_t *total_written_out); 1006*c43e99fdSEd Maste 1007*c43e99fdSEd Maste /** 1008*c43e99fdSEd Maste * Reset the total bytes read/written on a group. 1009*c43e99fdSEd Maste * 1010*c43e99fdSEd Maste * Reset the number of bytes read or written on grp as given by 1011*c43e99fdSEd Maste * bufferevent_rate_limit_group_reset_totals(). */ 1012*c43e99fdSEd Maste EVENT2_EXPORT_SYMBOL 1013*c43e99fdSEd Maste void 1014*c43e99fdSEd Maste bufferevent_rate_limit_group_reset_totals( 1015*c43e99fdSEd Maste struct bufferevent_rate_limit_group *grp); 1016*c43e99fdSEd Maste 1017*c43e99fdSEd Maste #ifdef __cplusplus 1018*c43e99fdSEd Maste } 1019*c43e99fdSEd Maste #endif 1020*c43e99fdSEd Maste 1021*c43e99fdSEd Maste #endif /* EVENT2_BUFFEREVENT_H_INCLUDED_ */ 1022