1682adc44SNiels Provos /*
2e49e2891SNick Mathewson * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
3682adc44SNiels Provos *
4682adc44SNiels Provos * Redistribution and use in source and binary forms, with or without
5682adc44SNiels Provos * modification, are permitted provided that the following conditions
6682adc44SNiels Provos * are met:
7682adc44SNiels Provos * 1. Redistributions of source code must retain the above copyright
8682adc44SNiels Provos * notice, this list of conditions and the following disclaimer.
9682adc44SNiels Provos * 2. Redistributions in binary form must reproduce the above copyright
10682adc44SNiels Provos * notice, this list of conditions and the following disclaimer in the
11682adc44SNiels Provos * documentation and/or other materials provided with the distribution.
12682adc44SNiels Provos * 3. The name of the author may not be used to endorse or promote products
13682adc44SNiels Provos * derived from this software without specific prior written permission.
14682adc44SNiels Provos *
15682adc44SNiels Provos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16682adc44SNiels Provos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17682adc44SNiels Provos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18682adc44SNiels Provos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19682adc44SNiels Provos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20682adc44SNiels Provos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21682adc44SNiels Provos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22682adc44SNiels Provos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23682adc44SNiels Provos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24682adc44SNiels Provos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25682adc44SNiels Provos */
263f8c7cd0SNick Mathewson #ifndef BUFFEREVENT_INTERNAL_H_INCLUDED_
273f8c7cd0SNick Mathewson #define BUFFEREVENT_INTERNAL_H_INCLUDED_
28682adc44SNiels Provos
29682adc44SNiels Provos #ifdef __cplusplus
30682adc44SNiels Provos extern "C" {
31682adc44SNiels Provos #endif
32682adc44SNiels Provos
33ec347b92SNick Mathewson #include "event2/event-config.h"
34ae2b84b2SNick Mathewson #include "event2/event_struct.h"
350915ca0aSKevin Bowling #include "evconfig-private.h"
36ef5e65a4SEvan Jones #include "event2/util.h"
371becc4c4SNick Mathewson #include "defer-internal.h"
38915193e7SNick Mathewson #include "evthread-internal.h"
39915193e7SNick Mathewson #include "event2/thread.h"
40737c9cd8SNick Mathewson #include "ratelim-internal.h"
41c8baac90SNick Mathewson #include "event2/bufferevent_struct.h"
42c2122919SAzat Khuzhin
43c42bc6b2SAzat Khuzhin #include "ipv6-internal.h"
44c2122919SAzat Khuzhin #ifdef _WIN32
45c2122919SAzat Khuzhin #include <ws2tcpip.h>
46c2122919SAzat Khuzhin #endif
47b2c68bc2SEd Schouten #ifdef EVENT__HAVE_NETINET_IN_H
48b2c68bc2SEd Schouten #include <netinet/in.h>
49b2c68bc2SEd Schouten #endif
50c2122919SAzat Khuzhin #ifdef EVENT__HAVE_NETINET_IN6_H
51c2122919SAzat Khuzhin #include <netinet/in6.h>
52c2122919SAzat Khuzhin #endif
531becc4c4SNick Mathewson
540d744aa1SNick Mathewson /* These flags are reasons that we might be declining to actually enable
550d744aa1SNick Mathewson reading or writing on a bufferevent.
560d744aa1SNick Mathewson */
570d744aa1SNick Mathewson
580d744aa1SNick Mathewson /* On a all bufferevents, for reading: used when we have read up to the
590d744aa1SNick Mathewson watermark value.
600d744aa1SNick Mathewson
61737c9cd8SNick Mathewson On a filtering bufferevent, for writing: used when the underlying
620d744aa1SNick Mathewson bufferevent's write buffer has been filled up to its watermark
630d744aa1SNick Mathewson value.
640d744aa1SNick Mathewson */
650d744aa1SNick Mathewson #define BEV_SUSPEND_WM 0x01
66737c9cd8SNick Mathewson /* On a base bufferevent: when we have emptied a bandwidth buckets */
670d744aa1SNick Mathewson #define BEV_SUSPEND_BW 0x02
68737c9cd8SNick Mathewson /* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
69737c9cd8SNick Mathewson #define BEV_SUSPEND_BW_GROUP 0x04
70db08f640SNick Mathewson /* On a socket bufferevent: can't do any operations while we're waiting for
71db08f640SNick Mathewson * name lookup to finish. */
72db08f640SNick Mathewson #define BEV_SUSPEND_LOOKUP 0x08
73ac27eb82SNick Mathewson /* On a base bufferevent, for reading: used when a filter has choked this
74ac27eb82SNick Mathewson * (underlying) bufferevent because it has stopped reading from it. */
75ac27eb82SNick Mathewson #define BEV_SUSPEND_FILT_READ 0x10
76ac27eb82SNick Mathewson
77ac27eb82SNick Mathewson typedef ev_uint16_t bufferevent_suspend_flags;
780d744aa1SNick Mathewson
79737c9cd8SNick Mathewson struct bufferevent_rate_limit_group {
80737c9cd8SNick Mathewson /** List of all members in the group */
81974d004eSNick Mathewson LIST_HEAD(rlim_group_member_list, bufferevent_private) members;
82737c9cd8SNick Mathewson /** Current limits for the group. */
83737c9cd8SNick Mathewson struct ev_token_bucket rate_limit;
84737c9cd8SNick Mathewson struct ev_token_bucket_cfg rate_limit_cfg;
85737c9cd8SNick Mathewson
86737c9cd8SNick Mathewson /** True iff we don't want to read from any member of the group.until
87737c9cd8SNick Mathewson * the token bucket refills. */
88737c9cd8SNick Mathewson unsigned read_suspended : 1;
89737c9cd8SNick Mathewson /** True iff we don't want to write from any member of the group.until
90737c9cd8SNick Mathewson * the token bucket refills. */
91737c9cd8SNick Mathewson unsigned write_suspended : 1;
92737c9cd8SNick Mathewson /** True iff we were unable to suspend one of the bufferevents in the
93737c9cd8SNick Mathewson * group for reading the last time we tried, and we should try
94737c9cd8SNick Mathewson * again. */
95737c9cd8SNick Mathewson unsigned pending_unsuspend_read : 1;
96737c9cd8SNick Mathewson /** True iff we were unable to suspend one of the bufferevents in the
97737c9cd8SNick Mathewson * group for writing the last time we tried, and we should try
98737c9cd8SNick Mathewson * again. */
99737c9cd8SNick Mathewson unsigned pending_unsuspend_write : 1;
100737c9cd8SNick Mathewson
101fb366c1dSNick Mathewson /*@{*/
102fb366c1dSNick Mathewson /** Total number of bytes read or written in this group since last
103fb366c1dSNick Mathewson * reset. */
104fb366c1dSNick Mathewson ev_uint64_t total_read;
105fb366c1dSNick Mathewson ev_uint64_t total_written;
106fb366c1dSNick Mathewson /*@}*/
107fb366c1dSNick Mathewson
108737c9cd8SNick Mathewson /** The number of bufferevents in the group. */
109737c9cd8SNick Mathewson int n_members;
110737c9cd8SNick Mathewson
111737c9cd8SNick Mathewson /** The smallest number of bytes that any member of the group should
112737c9cd8SNick Mathewson * be limited to read or write at a time. */
1132cbb1a16SNick Mathewson ev_ssize_t min_share;
1146d5440e8SNick Mathewson ev_ssize_t configured_min_share;
1156d5440e8SNick Mathewson
116737c9cd8SNick Mathewson /** Timeout event that goes off once a tick, when the bucket is ready
117737c9cd8SNick Mathewson * to refill. */
118737c9cd8SNick Mathewson struct event master_refill_event;
119e86af4b7SNicholas Marriott
1203aa44159SNick Mathewson /** Seed for weak random number generator. Protected by 'lock' */
1213aa44159SNick Mathewson struct evutil_weakrand_state weakrand_seed;
122e86af4b7SNicholas Marriott
123737c9cd8SNick Mathewson /** Lock to protect the members of this group. This lock should nest
124737c9cd8SNick Mathewson * within every bufferevent lock: if you are holding this lock, do
125737c9cd8SNick Mathewson * not assume you can lock another bufferevent. */
126737c9cd8SNick Mathewson void *lock;
127737c9cd8SNick Mathewson };
128737c9cd8SNick Mathewson
129737c9cd8SNick Mathewson /** Fields for rate-limiting a single bufferevent. */
130737c9cd8SNick Mathewson struct bufferevent_rate_limit {
131737c9cd8SNick Mathewson /* Linked-list elements for storing this bufferevent_private in a
132737c9cd8SNick Mathewson * group.
133737c9cd8SNick Mathewson *
134737c9cd8SNick Mathewson * Note that this field is supposed to be protected by the group
135737c9cd8SNick Mathewson * lock */
136974d004eSNick Mathewson LIST_ENTRY(bufferevent_private) next_in_group;
137737c9cd8SNick Mathewson /** The rate-limiting group for this bufferevent, or NULL if it is
138737c9cd8SNick Mathewson * only rate-limited on its own. */
139737c9cd8SNick Mathewson struct bufferevent_rate_limit_group *group;
140737c9cd8SNick Mathewson
141737c9cd8SNick Mathewson /* This bufferevent's current limits. */
142737c9cd8SNick Mathewson struct ev_token_bucket limit;
143737c9cd8SNick Mathewson /* Pointer to the rate-limit configuration for this bufferevent.
144737c9cd8SNick Mathewson * Can be shared. XXX reference-count this? */
145737c9cd8SNick Mathewson struct ev_token_bucket_cfg *cfg;
146737c9cd8SNick Mathewson
147737c9cd8SNick Mathewson /* Timeout event used when one this bufferevent's buckets are
148737c9cd8SNick Mathewson * empty. */
149737c9cd8SNick Mathewson struct event refill_bucket_event;
1500d744aa1SNick Mathewson };
1510d744aa1SNick Mathewson
1522f655f00SNick Mathewson /** Parts of the bufferevent structure that are shared among all bufferevent
1532f655f00SNick Mathewson * types, but not exposed in bufferevent_struct.h. */
1541becc4c4SNick Mathewson struct bufferevent_private {
1552f655f00SNick Mathewson /** The underlying bufferevent structure. */
1561becc4c4SNick Mathewson struct bufferevent bev;
1571becc4c4SNick Mathewson
1581becc4c4SNick Mathewson /** Evbuffer callback to enforce watermarks on input. */
1591becc4c4SNick Mathewson struct evbuffer_cb_entry *read_watermarks_cb;
1601becc4c4SNick Mathewson
161a98a512bSNick Mathewson /** If set, we should free the lock when we free the bufferevent. */
162915193e7SNick Mathewson unsigned own_lock : 1;
1631becc4c4SNick Mathewson
1642f655f00SNick Mathewson /** Flag: set if we have deferred callbacks and a read callback is
1652f655f00SNick Mathewson * pending. */
166a98a512bSNick Mathewson unsigned readcb_pending : 1;
1672f655f00SNick Mathewson /** Flag: set if we have deferred callbacks and a write callback is
1682f655f00SNick Mathewson * pending. */
169a98a512bSNick Mathewson unsigned writecb_pending : 1;
1702f655f00SNick Mathewson /** Flag: set if we are currently busy connecting. */
171659d54d5SNick Mathewson unsigned connecting : 1;
1727bc48bfdSNiels Provos /** Flag: set if a connect failed prematurely; this is a hack for
1737bc48bfdSNiels Provos * getting around the bufferevent abstraction. */
1747bc48bfdSNiels Provos unsigned connection_refused : 1;
1752f655f00SNick Mathewson /** Set to the events pending if we have deferred callbacks and
1762f655f00SNick Mathewson * an events callback is pending. */
1775232cfa3SNick Mathewson short eventcb_pending;
1780d744aa1SNick Mathewson
1790d744aa1SNick Mathewson /** If set, read is suspended until one or more conditions are over.
1800d744aa1SNick Mathewson * The actual value here is a bitfield of those conditions; see the
1810d744aa1SNick Mathewson * BEV_SUSPEND_* flags above. */
182ac27eb82SNick Mathewson bufferevent_suspend_flags read_suspended;
1830d744aa1SNick Mathewson
1840d744aa1SNick Mathewson /** If set, writing is suspended until one or more conditions are over.
1850d744aa1SNick Mathewson * The actual value here is a bitfield of those conditions; see the
1860d744aa1SNick Mathewson * BEV_SUSPEND_* flags above. */
187ac27eb82SNick Mathewson bufferevent_suspend_flags write_suspended;
1880d744aa1SNick Mathewson
1895232cfa3SNick Mathewson /** Set to the current socket errno if we have deferred callbacks and
1902f655f00SNick Mathewson * an events callback is pending. */
191a98a512bSNick Mathewson int errno_pending;
1920d744aa1SNick Mathewson
1930ef40706SChristopher Davis /** The DNS error code for bufferevent_socket_connect_hostname */
1940ef40706SChristopher Davis int dns_error;
1950ef40706SChristopher Davis
1962f655f00SNick Mathewson /** Used to implement deferred callbacks */
197a4079aa8SNick Mathewson struct event_callback deferred;
198a98a512bSNick Mathewson
1992f655f00SNick Mathewson /** The options this bufferevent was constructed with */
2001becc4c4SNick Mathewson enum bufferevent_options options;
2011becc4c4SNick Mathewson
2022f655f00SNick Mathewson /** Current reference count for this bufferevent. */
2031becc4c4SNick Mathewson int refcnt;
2042f655f00SNick Mathewson
2052f655f00SNick Mathewson /** Lock for this bufferevent. Shared by the inbuf and the outbuf.
2062f655f00SNick Mathewson * If NULL, locking is disabled. */
2071becc4c4SNick Mathewson void *lock;
208737c9cd8SNick Mathewson
209998c8138SAlexander Drozdov /** No matter how big our bucket gets, don't try to read more than this
210998c8138SAlexander Drozdov * much in a single read operation. */
211998c8138SAlexander Drozdov ev_ssize_t max_single_read;
212998c8138SAlexander Drozdov
213998c8138SAlexander Drozdov /** No matter how big our bucket gets, don't try to write more than this
214998c8138SAlexander Drozdov * much in a single write operation. */
215998c8138SAlexander Drozdov ev_ssize_t max_single_write;
216998c8138SAlexander Drozdov
217737c9cd8SNick Mathewson /** Rate-limiting information for this bufferevent */
218737c9cd8SNick Mathewson struct bufferevent_rate_limit *rate_limiting;
2198bb38425SAzat Khuzhin
2208bb38425SAzat Khuzhin /* Saved conn_addr, to extract IP address from it.
2218bb38425SAzat Khuzhin *
2228bb38425SAzat Khuzhin * Because some servers may reset/close connection without waiting clients,
2238bb38425SAzat Khuzhin * in that case we can't extract IP address even in close_cb.
2248bb38425SAzat Khuzhin * So we need to save it, just after we connected to remote server, or
2258bb38425SAzat Khuzhin * after resolving (to avoid extra dns requests during retrying, since UDP
2268bb38425SAzat Khuzhin * is slow) */
22738896124SAzat Khuzhin union {
22838896124SAzat Khuzhin struct sockaddr_in6 in6;
22938896124SAzat Khuzhin struct sockaddr_in in;
23038896124SAzat Khuzhin } conn_address;
23186dfd2ceSAzat Khuzhin
23286dfd2ceSAzat Khuzhin struct evdns_getaddrinfo_request *dns_request;
2331becc4c4SNick Mathewson };
234682adc44SNiels Provos
2352f655f00SNick Mathewson /** Possible operations for a control callback. */
23631d89f27SNick Mathewson enum bufferevent_ctrl_op {
23731d89f27SNick Mathewson BEV_CTRL_SET_FD,
23831d89f27SNick Mathewson BEV_CTRL_GET_FD,
239e6af35d7SNick Mathewson BEV_CTRL_GET_UNDERLYING,
240e6af35d7SNick Mathewson BEV_CTRL_CANCEL_ALL
24131d89f27SNick Mathewson };
24231d89f27SNick Mathewson
2432f655f00SNick Mathewson /** Possible data types for a control callback */
24431d89f27SNick Mathewson union bufferevent_ctrl_data {
24531d89f27SNick Mathewson void *ptr;
24631d89f27SNick Mathewson evutil_socket_t fd;
24731d89f27SNick Mathewson };
24831d89f27SNick Mathewson
249ea4b8724SNick Mathewson /**
250ea4b8724SNick Mathewson Implementation table for a bufferevent: holds function pointers and other
251ea4b8724SNick Mathewson information to make the various bufferevent types work.
252ea4b8724SNick Mathewson */
253ea4b8724SNick Mathewson struct bufferevent_ops {
254ea4b8724SNick Mathewson /** The name of the bufferevent's type. */
255ea4b8724SNick Mathewson const char *type;
256ea4b8724SNick Mathewson /** At what offset into the implementation type will we find a
257ea4b8724SNick Mathewson bufferevent structure?
258682adc44SNiels Provos
259ea4b8724SNick Mathewson Example: if the type is implemented as
260ea4b8724SNick Mathewson struct bufferevent_x {
261ea4b8724SNick Mathewson int extra_data;
262ea4b8724SNick Mathewson struct bufferevent bev;
263ea4b8724SNick Mathewson }
264ea4b8724SNick Mathewson then mem_offset should be offsetof(struct bufferevent_x, bev)
265ea4b8724SNick Mathewson */
266ea4b8724SNick Mathewson off_t mem_offset;
267682adc44SNiels Provos
268ea4b8724SNick Mathewson /** Enables one or more of EV_READ|EV_WRITE on a bufferevent. Does
269ea4b8724SNick Mathewson not need to adjust the 'enabled' field. Returns 0 on success, -1
270ea4b8724SNick Mathewson on failure.
271ea4b8724SNick Mathewson */
272ea4b8724SNick Mathewson int (*enable)(struct bufferevent *, short);
273682adc44SNiels Provos
274ea4b8724SNick Mathewson /** Disables one or more of EV_READ|EV_WRITE on a bufferevent. Does
275ea4b8724SNick Mathewson not need to adjust the 'enabled' field. Returns 0 on success, -1
276ea4b8724SNick Mathewson on failure.
277ea4b8724SNick Mathewson */
278ea4b8724SNick Mathewson int (*disable)(struct bufferevent *, short);
279682adc44SNiels Provos
280a800b913SNick Mathewson /** Detatches the bufferevent from related data structures. Called as
281a800b913SNick Mathewson * soon as its reference count reaches 0. */
28202fbf687SNick Mathewson void (*unlink)(struct bufferevent *);
28302fbf687SNick Mathewson
284ea4b8724SNick Mathewson /** Free any storage and deallocate any extra data or structures used
285a800b913SNick Mathewson in this implementation. Called when the bufferevent is
286a800b913SNick Mathewson finalized.
287ea4b8724SNick Mathewson */
288ea4b8724SNick Mathewson void (*destruct)(struct bufferevent *);
289682adc44SNiels Provos
290ea4b8724SNick Mathewson /** Called when the timeouts on the bufferevent have changed.*/
291ff3f6cd4SNick Mathewson int (*adj_timeouts)(struct bufferevent *);
292ea4b8724SNick Mathewson
293ea4b8724SNick Mathewson /** Called to flush data. */
294ea4b8724SNick Mathewson int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
29531d89f27SNick Mathewson
29631d89f27SNick Mathewson /** Called to access miscellaneous fields. */
29731d89f27SNick Mathewson int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
298737c9cd8SNick Mathewson
299682adc44SNiels Provos };
300682adc44SNiels Provos
30123085c92SNick Mathewson extern const struct bufferevent_ops bufferevent_ops_socket;
30223085c92SNick Mathewson extern const struct bufferevent_ops bufferevent_ops_filter;
30323085c92SNick Mathewson extern const struct bufferevent_ops bufferevent_ops_pair;
304ea4b8724SNick Mathewson
30586db1c85SNick Mathewson #define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
30686db1c85SNick Mathewson #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
30786db1c85SNick Mathewson #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
30886db1c85SNick Mathewson
30956faf02bSDominic Chen #if defined(EVENT__HAVE_OPENSSL)
31056faf02bSDominic Chen extern const struct bufferevent_ops bufferevent_ops_openssl;
31156faf02bSDominic Chen #define BEV_IS_OPENSSL(bevp) ((bevp)->be_ops == &bufferevent_ops_openssl)
31256faf02bSDominic Chen #else
31356faf02bSDominic Chen #define BEV_IS_OPENSSL(bevp) 0
31456faf02bSDominic Chen #endif
31556faf02bSDominic Chen
3169f560bfaSNick Mathewson #ifdef _WIN32
31786db1c85SNick Mathewson extern const struct bufferevent_ops bufferevent_ops_async;
31886db1c85SNick Mathewson #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
31986db1c85SNick Mathewson #else
32086db1c85SNick Mathewson #define BEV_IS_ASYNC(bevp) 0
32186db1c85SNick Mathewson #endif
32286db1c85SNick Mathewson
323ea4b8724SNick Mathewson /** Initialize the shared parts of a bufferevent. */
3249806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
3258ac3c4c2SNick Mathewson int bufferevent_init_common_(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
326ea4b8724SNick Mathewson
3270d744aa1SNick Mathewson /** For internal use: temporarily stop all reads on bufev, until the conditions
3280d744aa1SNick Mathewson * in 'what' are over. */
3299806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
3308ac3c4c2SNick Mathewson void bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
3310d744aa1SNick Mathewson /** For internal use: clear the conditions 'what' on bufev, and re-enable
3320d744aa1SNick Mathewson * reading if there are no conditions left. */
3339806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
3348ac3c4c2SNick Mathewson void bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
3350d744aa1SNick Mathewson
3360d744aa1SNick Mathewson /** For internal use: temporarily stop all writes on bufev, until the conditions
3370d744aa1SNick Mathewson * in 'what' are over. */
3388ac3c4c2SNick Mathewson void bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
3390d744aa1SNick Mathewson /** For internal use: clear the conditions 'what' on bufev, and re-enable
3400d744aa1SNick Mathewson * writing if there are no conditions left. */
3418ac3c4c2SNick Mathewson void bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
3420d744aa1SNick Mathewson
3430d744aa1SNick Mathewson #define bufferevent_wm_suspend_read(b) \
3448ac3c4c2SNick Mathewson bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
3450d744aa1SNick Mathewson #define bufferevent_wm_unsuspend_read(b) \
3468ac3c4c2SNick Mathewson bufferevent_unsuspend_read_((b), BEV_SUSPEND_WM)
347ea4b8724SNick Mathewson
348c8baac90SNick Mathewson /*
349c8baac90SNick Mathewson Disable a bufferevent. Equivalent to bufferevent_disable(), but
350c8baac90SNick Mathewson first resets 'connecting' flag to force EV_WRITE down for sure.
351c8baac90SNick Mathewson
352c8baac90SNick Mathewson XXXX this method will go away in the future; try not to add new users.
3538ac3c4c2SNick Mathewson See comment in evhttp_connection_reset_() for discussion.
354c8baac90SNick Mathewson
355c8baac90SNick Mathewson @param bufev the bufferevent to be disabled
356c8baac90SNick Mathewson @param event any combination of EV_READ | EV_WRITE.
357c8baac90SNick Mathewson @return 0 if successful, or -1 if an error occurred
358c8baac90SNick Mathewson @see bufferevent_disable()
359c8baac90SNick Mathewson */
3609806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
3618ac3c4c2SNick Mathewson int bufferevent_disable_hard_(struct bufferevent *bufev, short event);
362c8baac90SNick Mathewson
3632f655f00SNick Mathewson /** Internal: Set up locking on a bufferevent. If lock is set, use it.
3642f655f00SNick Mathewson * Otherwise, use a new lock. */
3659806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
3668ac3c4c2SNick Mathewson int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
3671ed6718dSMark Ellzey /** Internal: backwards compat macro for the now public function
3681ed6718dSMark Ellzey * Increment the reference count on bufev. */
3691ed6718dSMark Ellzey #define bufferevent_incref_(bufev) bufferevent_incref(bufev)
370a62283a9SNick Mathewson /** Internal: Lock bufev and increase its reference count.
371a62283a9SNick Mathewson * unlocking it otherwise. */
3729806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
373cb9da0bfSNick Mathewson void bufferevent_incref_and_lock_(struct bufferevent *bufev);
3741ed6718dSMark Ellzey /** Internal: backwards compat macro for the now public function
3751ed6718dSMark Ellzey * Decrement the reference count on bufev. Returns 1 if it freed
376f1bc125eSNick Mathewson * the bufferevent.*/
3771ed6718dSMark Ellzey #define bufferevent_decref_(bufev) bufferevent_decref(bufev)
3781ed6718dSMark Ellzey
3792f655f00SNick Mathewson /** Internal: Drop the reference count on bufev, freeing as necessary, and
380f1bc125eSNick Mathewson * unlocking it otherwise. Returns 1 if it freed the bufferevent. */
3819806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
382cb9da0bfSNick Mathewson int bufferevent_decref_and_unlock_(struct bufferevent *bufev);
383915193e7SNick Mathewson
3842f655f00SNick Mathewson /** Internal: If callbacks are deferred and we have a read callback, schedule
385bd419471SOndřej Kuzník * a readcb. Otherwise just run the readcb. Ignores watermarks. */
3869806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
38761ee18b8SOndřej Kuzník void bufferevent_run_readcb_(struct bufferevent *bufev, int options);
3882f655f00SNick Mathewson /** Internal: If callbacks are deferred and we have a write callback, schedule
389bd419471SOndřej Kuzník * a writecb. Otherwise just run the writecb. Ignores watermarks. */
3909806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
39161ee18b8SOndřej Kuzník void bufferevent_run_writecb_(struct bufferevent *bufev, int options);
3922f655f00SNick Mathewson /** Internal: If callbacks are deferred and we have an eventcb, schedule
393a7384c78SOndřej Kuzník * it to run with events "what". Otherwise just run the eventcb.
394a7384c78SOndřej Kuzník * See bufferevent_trigger_event for meaning of "options". */
3959806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
396a7384c78SOndřej Kuzník void bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options);
397a98a512bSNick Mathewson
39861ee18b8SOndřej Kuzník /** Internal: Run or schedule (if deferred or options contain
39961ee18b8SOndřej Kuzník * BEV_TRIG_DEFER_CALLBACKS) I/O callbacks specified in iotype.
400bd419471SOndřej Kuzník * Must already hold the bufev lock. Honors watermarks unless
401bd419471SOndřej Kuzník * BEV_TRIG_IGNORE_WATERMARKS is in options. */
4024dd3abd4SNick Mathewson static inline void bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options);
40361ee18b8SOndřej Kuzník
4044dd3abd4SNick Mathewson /* Making this inline since all of the common-case calls to this function in
4054dd3abd4SNick Mathewson * libevent use constant arguments. */
4064dd3abd4SNick Mathewson static inline void
bufferevent_trigger_nolock_(struct bufferevent * bufev,short iotype,int options)4074dd3abd4SNick Mathewson bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options)
4084dd3abd4SNick Mathewson {
4094dd3abd4SNick Mathewson if ((iotype & EV_READ) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
4104dd3abd4SNick Mathewson evbuffer_get_length(bufev->input) >= bufev->wm_read.low))
4114dd3abd4SNick Mathewson bufferevent_run_readcb_(bufev, options);
4124dd3abd4SNick Mathewson if ((iotype & EV_WRITE) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
4134dd3abd4SNick Mathewson evbuffer_get_length(bufev->output) <= bufev->wm_write.low))
4144dd3abd4SNick Mathewson bufferevent_run_writecb_(bufev, options);
4154dd3abd4SNick Mathewson }
41661ee18b8SOndřej Kuzník
417709c21c4SNick Mathewson /** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
418709c21c4SNick Mathewson * which case add ev with no timeout. */
4199806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
420cb9da0bfSNick Mathewson int bufferevent_add_event_(struct event *ev, const struct timeval *tv);
421709c21c4SNick Mathewson
42234574db0SNick Mathewson /* =========
42334574db0SNick Mathewson * These next functions implement timeouts for bufferevents that aren't doing
42434574db0SNick Mathewson * anything else with ev_read and ev_write, to handle timeouts.
42534574db0SNick Mathewson * ========= */
42634574db0SNick Mathewson /** Internal use: Set up the ev_read and ev_write callbacks so that
42734574db0SNick Mathewson * the other "generic_timeout" functions will work on it. Call this from
428e3fd294aSNick Mathewson * the constructor function. */
4299806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
430cb9da0bfSNick Mathewson void bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
43134574db0SNick Mathewson /** Internal use: Add or delete the generic timeout events as appropriate.
43234574db0SNick Mathewson * (If an event is enabled and a timeout is set, we add the event. Otherwise
43334574db0SNick Mathewson * we delete it.) Call this from anything that changes the timeout values,
43434574db0SNick Mathewson * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
4359806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
436cb9da0bfSNick Mathewson int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
4379806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
4383c1f58f5SAzat Khuzhin int bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev);
43934574db0SNick Mathewson
4409806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
44131db8a02SMaxime Henrion enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
44231db8a02SMaxime Henrion
4439806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
4448bb38425SAzat Khuzhin const struct sockaddr*
4458bb38425SAzat Khuzhin bufferevent_socket_get_conn_address_(struct bufferevent *bev);
4468bb38425SAzat Khuzhin
4474215c003SGreg Hazel EVENT2_EXPORT_SYMBOL
4484215c003SGreg Hazel void
449*5362f749SAzat Khuzhin bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev, evutil_socket_t fd);
4504215c003SGreg Hazel
4514215c003SGreg Hazel EVENT2_EXPORT_SYMBOL
4524215c003SGreg Hazel void
4534215c003SGreg Hazel bufferevent_socket_set_conn_address_(struct bufferevent *bev, struct sockaddr *addr, size_t addrlen);
4544215c003SGreg Hazel
4554215c003SGreg Hazel
45634574db0SNick Mathewson /** Internal use: We have just successfully read data into an inbuf, so
457e3fd294aSNick Mathewson * reset the read timeout (if any). */
45834574db0SNick Mathewson #define BEV_RESET_GENERIC_READ_TIMEOUT(bev) \
45934574db0SNick Mathewson do { \
46034574db0SNick Mathewson if (evutil_timerisset(&(bev)->timeout_read)) \
46134574db0SNick Mathewson event_add(&(bev)->ev_read, &(bev)->timeout_read); \
46234574db0SNick Mathewson } while (0)
46334574db0SNick Mathewson /** Internal use: We have just successfully written data from an inbuf, so
464e3fd294aSNick Mathewson * reset the read timeout (if any). */
46534574db0SNick Mathewson #define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev) \
46634574db0SNick Mathewson do { \
46734574db0SNick Mathewson if (evutil_timerisset(&(bev)->timeout_write)) \
46834574db0SNick Mathewson event_add(&(bev)->ev_write, &(bev)->timeout_write); \
46934574db0SNick Mathewson } while (0)
470d3288293SNick Mathewson #define BEV_DEL_GENERIC_READ_TIMEOUT(bev) \
471d3288293SNick Mathewson event_del(&(bev)->ev_read)
472d3288293SNick Mathewson #define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev) \
473d3288293SNick Mathewson event_del(&(bev)->ev_write)
474d3288293SNick Mathewson
47534574db0SNick Mathewson
4762f655f00SNick Mathewson /** Internal: Given a bufferevent, return its corresponding
4772f655f00SNick Mathewson * bufferevent_private. */
478915193e7SNick Mathewson #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
479915193e7SNick Mathewson
48068120d9bSNick Mathewson #ifdef EVENT__DISABLE_THREAD_SUPPORT
481cb9da0bfSNick Mathewson #define BEV_LOCK(b) EVUTIL_NIL_STMT_
482cb9da0bfSNick Mathewson #define BEV_UNLOCK(b) EVUTIL_NIL_STMT_
483ad811cdcSNick Mathewson #else
4842f655f00SNick Mathewson /** Internal: Grab the lock (if any) on a bufferevent */
485915193e7SNick Mathewson #define BEV_LOCK(b) do { \
486915193e7SNick Mathewson struct bufferevent_private *locking = BEV_UPCAST(b); \
48776cd2b70SNick Mathewson EVLOCK_LOCK(locking->lock, 0); \
488915193e7SNick Mathewson } while (0)
489915193e7SNick Mathewson
4902f655f00SNick Mathewson /** Internal: Release the lock (if any) on a bufferevent */
491915193e7SNick Mathewson #define BEV_UNLOCK(b) do { \
492915193e7SNick Mathewson struct bufferevent_private *locking = BEV_UPCAST(b); \
49376cd2b70SNick Mathewson EVLOCK_UNLOCK(locking->lock, 0); \
494915193e7SNick Mathewson } while (0)
495ad811cdcSNick Mathewson #endif
496ad811cdcSNick Mathewson
497915193e7SNick Mathewson
498737c9cd8SNick Mathewson /* ==== For rate-limiting. */
499737c9cd8SNick Mathewson
5009806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
501cb9da0bfSNick Mathewson int bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
502598d1336SNick Mathewson ev_ssize_t bytes);
5039806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
504cb9da0bfSNick Mathewson int bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
505598d1336SNick Mathewson ev_ssize_t bytes);
5069806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
507cb9da0bfSNick Mathewson ev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
5089806b126SAzat Khuzhin EVENT2_EXPORT_SYMBOL
509cb9da0bfSNick Mathewson ev_ssize_t bufferevent_get_write_max_(struct bufferevent_private *bev);
510737c9cd8SNick Mathewson
511cb9da0bfSNick Mathewson int bufferevent_ratelim_init_(struct bufferevent_private *bev);
512998c8138SAlexander Drozdov
513682adc44SNiels Provos #ifdef __cplusplus
514682adc44SNiels Provos }
515682adc44SNiels Provos #endif
516682adc44SNiels Provos
517709c21c4SNick Mathewson
5183f8c7cd0SNick Mathewson #endif /* BUFFEREVENT_INTERNAL_H_INCLUDED_ */
519