1c43e99fdSEd Maste /*
2c43e99fdSEd Maste * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
3c43e99fdSEd Maste *
4c43e99fdSEd Maste * Redistribution and use in source and binary forms, with or without
5c43e99fdSEd Maste * modification, are permitted provided that the following conditions
6c43e99fdSEd Maste * are met:
7c43e99fdSEd Maste * 1. Redistributions of source code must retain the above copyright
8c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer.
9c43e99fdSEd Maste * 2. Redistributions in binary form must reproduce the above copyright
10c43e99fdSEd Maste * notice, this list of conditions and the following disclaimer in the
11c43e99fdSEd Maste * documentation and/or other materials provided with the distribution.
12c43e99fdSEd Maste * 3. The name of the author may not be used to endorse or promote products
13c43e99fdSEd Maste * derived from this software without specific prior written permission.
14c43e99fdSEd Maste *
15c43e99fdSEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c43e99fdSEd Maste * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c43e99fdSEd Maste * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c43e99fdSEd Maste * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c43e99fdSEd Maste * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c43e99fdSEd Maste * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c43e99fdSEd Maste * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c43e99fdSEd Maste * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c43e99fdSEd Maste * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c43e99fdSEd Maste * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c43e99fdSEd Maste */
26c43e99fdSEd Maste #ifndef BUFFEREVENT_INTERNAL_H_INCLUDED_
27c43e99fdSEd Maste #define BUFFEREVENT_INTERNAL_H_INCLUDED_
28c43e99fdSEd Maste
29c43e99fdSEd Maste #ifdef __cplusplus
30c43e99fdSEd Maste extern "C" {
31c43e99fdSEd Maste #endif
32c43e99fdSEd Maste
33c43e99fdSEd Maste #include "event2/event-config.h"
34c43e99fdSEd Maste #include "event2/event_struct.h"
35c43e99fdSEd Maste #include "evconfig-private.h"
36c43e99fdSEd Maste #include "event2/util.h"
37c43e99fdSEd Maste #include "defer-internal.h"
38c43e99fdSEd Maste #include "evthread-internal.h"
39c43e99fdSEd Maste #include "event2/thread.h"
40c43e99fdSEd Maste #include "ratelim-internal.h"
41c43e99fdSEd Maste #include "event2/bufferevent_struct.h"
42c43e99fdSEd Maste
43c43e99fdSEd Maste #include "ipv6-internal.h"
44c43e99fdSEd Maste #ifdef _WIN32
45c43e99fdSEd Maste #include <ws2tcpip.h>
46c43e99fdSEd Maste #endif
47c43e99fdSEd Maste #ifdef EVENT__HAVE_NETINET_IN_H
48c43e99fdSEd Maste #include <netinet/in.h>
49c43e99fdSEd Maste #endif
50c43e99fdSEd Maste #ifdef EVENT__HAVE_NETINET_IN6_H
51c43e99fdSEd Maste #include <netinet/in6.h>
52c43e99fdSEd Maste #endif
53c43e99fdSEd Maste
54c43e99fdSEd Maste /* These flags are reasons that we might be declining to actually enable
55c43e99fdSEd Maste reading or writing on a bufferevent.
56c43e99fdSEd Maste */
57c43e99fdSEd Maste
58c43e99fdSEd Maste /* On a all bufferevents, for reading: used when we have read up to the
59c43e99fdSEd Maste watermark value.
60c43e99fdSEd Maste
61c43e99fdSEd Maste On a filtering bufferevent, for writing: used when the underlying
62c43e99fdSEd Maste bufferevent's write buffer has been filled up to its watermark
63c43e99fdSEd Maste value.
64c43e99fdSEd Maste */
65c43e99fdSEd Maste #define BEV_SUSPEND_WM 0x01
66c43e99fdSEd Maste /* On a base bufferevent: when we have emptied a bandwidth buckets */
67c43e99fdSEd Maste #define BEV_SUSPEND_BW 0x02
68c43e99fdSEd Maste /* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
69c43e99fdSEd Maste #define BEV_SUSPEND_BW_GROUP 0x04
70c43e99fdSEd Maste /* On a socket bufferevent: can't do any operations while we're waiting for
71c43e99fdSEd Maste * name lookup to finish. */
72c43e99fdSEd Maste #define BEV_SUSPEND_LOOKUP 0x08
73c43e99fdSEd Maste /* On a base bufferevent, for reading: used when a filter has choked this
74c43e99fdSEd Maste * (underlying) bufferevent because it has stopped reading from it. */
75c43e99fdSEd Maste #define BEV_SUSPEND_FILT_READ 0x10
76c43e99fdSEd Maste
77c43e99fdSEd Maste typedef ev_uint16_t bufferevent_suspend_flags;
78c43e99fdSEd Maste
79c43e99fdSEd Maste struct bufferevent_rate_limit_group {
80c43e99fdSEd Maste /** List of all members in the group */
81c43e99fdSEd Maste LIST_HEAD(rlim_group_member_list, bufferevent_private) members;
82c43e99fdSEd Maste /** Current limits for the group. */
83c43e99fdSEd Maste struct ev_token_bucket rate_limit;
84c43e99fdSEd Maste struct ev_token_bucket_cfg rate_limit_cfg;
85c43e99fdSEd Maste
86c43e99fdSEd Maste /** True iff we don't want to read from any member of the group.until
87c43e99fdSEd Maste * the token bucket refills. */
88c43e99fdSEd Maste unsigned read_suspended : 1;
89c43e99fdSEd Maste /** True iff we don't want to write from any member of the group.until
90c43e99fdSEd Maste * the token bucket refills. */
91c43e99fdSEd Maste unsigned write_suspended : 1;
92c43e99fdSEd Maste /** True iff we were unable to suspend one of the bufferevents in the
93c43e99fdSEd Maste * group for reading the last time we tried, and we should try
94c43e99fdSEd Maste * again. */
95c43e99fdSEd Maste unsigned pending_unsuspend_read : 1;
96c43e99fdSEd Maste /** True iff we were unable to suspend one of the bufferevents in the
97c43e99fdSEd Maste * group for writing the last time we tried, and we should try
98c43e99fdSEd Maste * again. */
99c43e99fdSEd Maste unsigned pending_unsuspend_write : 1;
100c43e99fdSEd Maste
101c43e99fdSEd Maste /*@{*/
102c43e99fdSEd Maste /** Total number of bytes read or written in this group since last
103c43e99fdSEd Maste * reset. */
104c43e99fdSEd Maste ev_uint64_t total_read;
105c43e99fdSEd Maste ev_uint64_t total_written;
106c43e99fdSEd Maste /*@}*/
107c43e99fdSEd Maste
108c43e99fdSEd Maste /** The number of bufferevents in the group. */
109c43e99fdSEd Maste int n_members;
110c43e99fdSEd Maste
111c43e99fdSEd Maste /** The smallest number of bytes that any member of the group should
112c43e99fdSEd Maste * be limited to read or write at a time. */
113c43e99fdSEd Maste ev_ssize_t min_share;
114c43e99fdSEd Maste ev_ssize_t configured_min_share;
115c43e99fdSEd Maste
116c43e99fdSEd Maste /** Timeout event that goes off once a tick, when the bucket is ready
117c43e99fdSEd Maste * to refill. */
118c43e99fdSEd Maste struct event master_refill_event;
119c43e99fdSEd Maste
120c43e99fdSEd Maste /** Seed for weak random number generator. Protected by 'lock' */
121c43e99fdSEd Maste struct evutil_weakrand_state weakrand_seed;
122c43e99fdSEd Maste
123c43e99fdSEd Maste /** Lock to protect the members of this group. This lock should nest
124c43e99fdSEd Maste * within every bufferevent lock: if you are holding this lock, do
125c43e99fdSEd Maste * not assume you can lock another bufferevent. */
126c43e99fdSEd Maste void *lock;
127c43e99fdSEd Maste };
128c43e99fdSEd Maste
129c43e99fdSEd Maste /** Fields for rate-limiting a single bufferevent. */
130c43e99fdSEd Maste struct bufferevent_rate_limit {
131c43e99fdSEd Maste /* Linked-list elements for storing this bufferevent_private in a
132c43e99fdSEd Maste * group.
133c43e99fdSEd Maste *
134c43e99fdSEd Maste * Note that this field is supposed to be protected by the group
135c43e99fdSEd Maste * lock */
136c43e99fdSEd Maste LIST_ENTRY(bufferevent_private) next_in_group;
137c43e99fdSEd Maste /** The rate-limiting group for this bufferevent, or NULL if it is
138c43e99fdSEd Maste * only rate-limited on its own. */
139c43e99fdSEd Maste struct bufferevent_rate_limit_group *group;
140c43e99fdSEd Maste
141c43e99fdSEd Maste /* This bufferevent's current limits. */
142c43e99fdSEd Maste struct ev_token_bucket limit;
143c43e99fdSEd Maste /* Pointer to the rate-limit configuration for this bufferevent.
144c43e99fdSEd Maste * Can be shared. XXX reference-count this? */
145c43e99fdSEd Maste struct ev_token_bucket_cfg *cfg;
146c43e99fdSEd Maste
147c43e99fdSEd Maste /* Timeout event used when one this bufferevent's buckets are
148c43e99fdSEd Maste * empty. */
149c43e99fdSEd Maste struct event refill_bucket_event;
150c43e99fdSEd Maste };
151c43e99fdSEd Maste
152c43e99fdSEd Maste /** Parts of the bufferevent structure that are shared among all bufferevent
153c43e99fdSEd Maste * types, but not exposed in bufferevent_struct.h. */
154c43e99fdSEd Maste struct bufferevent_private {
155c43e99fdSEd Maste /** The underlying bufferevent structure. */
156c43e99fdSEd Maste struct bufferevent bev;
157c43e99fdSEd Maste
158c43e99fdSEd Maste /** Evbuffer callback to enforce watermarks on input. */
159c43e99fdSEd Maste struct evbuffer_cb_entry *read_watermarks_cb;
160c43e99fdSEd Maste
161c43e99fdSEd Maste /** If set, we should free the lock when we free the bufferevent. */
162c43e99fdSEd Maste unsigned own_lock : 1;
163c43e99fdSEd Maste
164c43e99fdSEd Maste /** Flag: set if we have deferred callbacks and a read callback is
165c43e99fdSEd Maste * pending. */
166c43e99fdSEd Maste unsigned readcb_pending : 1;
167c43e99fdSEd Maste /** Flag: set if we have deferred callbacks and a write callback is
168c43e99fdSEd Maste * pending. */
169c43e99fdSEd Maste unsigned writecb_pending : 1;
170c43e99fdSEd Maste /** Flag: set if we are currently busy connecting. */
171c43e99fdSEd Maste unsigned connecting : 1;
172c43e99fdSEd Maste /** Flag: set if a connect failed prematurely; this is a hack for
173c43e99fdSEd Maste * getting around the bufferevent abstraction. */
174c43e99fdSEd Maste unsigned connection_refused : 1;
175c43e99fdSEd Maste /** Set to the events pending if we have deferred callbacks and
176c43e99fdSEd Maste * an events callback is pending. */
177c43e99fdSEd Maste short eventcb_pending;
178c43e99fdSEd Maste
179c43e99fdSEd Maste /** If set, read is suspended until one or more conditions are over.
180c43e99fdSEd Maste * The actual value here is a bitfield of those conditions; see the
181c43e99fdSEd Maste * BEV_SUSPEND_* flags above. */
182c43e99fdSEd Maste bufferevent_suspend_flags read_suspended;
183c43e99fdSEd Maste
184c43e99fdSEd Maste /** If set, writing is suspended until one or more conditions are over.
185c43e99fdSEd Maste * The actual value here is a bitfield of those conditions; see the
186c43e99fdSEd Maste * BEV_SUSPEND_* flags above. */
187c43e99fdSEd Maste bufferevent_suspend_flags write_suspended;
188c43e99fdSEd Maste
189c43e99fdSEd Maste /** Set to the current socket errno if we have deferred callbacks and
190c43e99fdSEd Maste * an events callback is pending. */
191c43e99fdSEd Maste int errno_pending;
192c43e99fdSEd Maste
193c43e99fdSEd Maste /** The DNS error code for bufferevent_socket_connect_hostname */
194c43e99fdSEd Maste int dns_error;
195c43e99fdSEd Maste
196c43e99fdSEd Maste /** Used to implement deferred callbacks */
197c43e99fdSEd Maste struct event_callback deferred;
198c43e99fdSEd Maste
199c43e99fdSEd Maste /** The options this bufferevent was constructed with */
200c43e99fdSEd Maste enum bufferevent_options options;
201c43e99fdSEd Maste
202c43e99fdSEd Maste /** Current reference count for this bufferevent. */
203c43e99fdSEd Maste int refcnt;
204c43e99fdSEd Maste
205c43e99fdSEd Maste /** Lock for this bufferevent. Shared by the inbuf and the outbuf.
206c43e99fdSEd Maste * If NULL, locking is disabled. */
207c43e99fdSEd Maste void *lock;
208c43e99fdSEd Maste
209c43e99fdSEd Maste /** No matter how big our bucket gets, don't try to read more than this
210c43e99fdSEd Maste * much in a single read operation. */
211c43e99fdSEd Maste ev_ssize_t max_single_read;
212c43e99fdSEd Maste
213c43e99fdSEd Maste /** No matter how big our bucket gets, don't try to write more than this
214c43e99fdSEd Maste * much in a single write operation. */
215c43e99fdSEd Maste ev_ssize_t max_single_write;
216c43e99fdSEd Maste
217c43e99fdSEd Maste /** Rate-limiting information for this bufferevent */
218c43e99fdSEd Maste struct bufferevent_rate_limit *rate_limiting;
219c43e99fdSEd Maste
220c43e99fdSEd Maste /* Saved conn_addr, to extract IP address from it.
221c43e99fdSEd Maste *
222c43e99fdSEd Maste * Because some servers may reset/close connection without waiting clients,
223c43e99fdSEd Maste * in that case we can't extract IP address even in close_cb.
224c43e99fdSEd Maste * So we need to save it, just after we connected to remote server, or
225c43e99fdSEd Maste * after resolving (to avoid extra dns requests during retrying, since UDP
226c43e99fdSEd Maste * is slow) */
227c43e99fdSEd Maste union {
228c43e99fdSEd Maste struct sockaddr_in6 in6;
229c43e99fdSEd Maste struct sockaddr_in in;
230c43e99fdSEd Maste } conn_address;
231c43e99fdSEd Maste
232c43e99fdSEd Maste struct evdns_getaddrinfo_request *dns_request;
233c43e99fdSEd Maste };
234c43e99fdSEd Maste
235c43e99fdSEd Maste /** Possible operations for a control callback. */
236c43e99fdSEd Maste enum bufferevent_ctrl_op {
237c43e99fdSEd Maste BEV_CTRL_SET_FD,
238c43e99fdSEd Maste BEV_CTRL_GET_FD,
239c43e99fdSEd Maste BEV_CTRL_GET_UNDERLYING,
240c43e99fdSEd Maste BEV_CTRL_CANCEL_ALL
241c43e99fdSEd Maste };
242c43e99fdSEd Maste
243c43e99fdSEd Maste /** Possible data types for a control callback */
244c43e99fdSEd Maste union bufferevent_ctrl_data {
245c43e99fdSEd Maste void *ptr;
246c43e99fdSEd Maste evutil_socket_t fd;
247c43e99fdSEd Maste };
248c43e99fdSEd Maste
249c43e99fdSEd Maste /**
250c43e99fdSEd Maste Implementation table for a bufferevent: holds function pointers and other
251c43e99fdSEd Maste information to make the various bufferevent types work.
252c43e99fdSEd Maste */
253c43e99fdSEd Maste struct bufferevent_ops {
254c43e99fdSEd Maste /** The name of the bufferevent's type. */
255c43e99fdSEd Maste const char *type;
256c43e99fdSEd Maste /** At what offset into the implementation type will we find a
257c43e99fdSEd Maste bufferevent structure?
258c43e99fdSEd Maste
259c43e99fdSEd Maste Example: if the type is implemented as
260c43e99fdSEd Maste struct bufferevent_x {
261c43e99fdSEd Maste int extra_data;
262c43e99fdSEd Maste struct bufferevent bev;
263c43e99fdSEd Maste }
264c43e99fdSEd Maste then mem_offset should be offsetof(struct bufferevent_x, bev)
265c43e99fdSEd Maste */
266c43e99fdSEd Maste off_t mem_offset;
267c43e99fdSEd Maste
268c43e99fdSEd Maste /** Enables one or more of EV_READ|EV_WRITE on a bufferevent. Does
269c43e99fdSEd Maste not need to adjust the 'enabled' field. Returns 0 on success, -1
270c43e99fdSEd Maste on failure.
271c43e99fdSEd Maste */
272c43e99fdSEd Maste int (*enable)(struct bufferevent *, short);
273c43e99fdSEd Maste
274c43e99fdSEd Maste /** Disables one or more of EV_READ|EV_WRITE on a bufferevent. Does
275c43e99fdSEd Maste not need to adjust the 'enabled' field. Returns 0 on success, -1
276c43e99fdSEd Maste on failure.
277c43e99fdSEd Maste */
278c43e99fdSEd Maste int (*disable)(struct bufferevent *, short);
279c43e99fdSEd Maste
280c43e99fdSEd Maste /** Detatches the bufferevent from related data structures. Called as
281c43e99fdSEd Maste * soon as its reference count reaches 0. */
282c43e99fdSEd Maste void (*unlink)(struct bufferevent *);
283c43e99fdSEd Maste
284c43e99fdSEd Maste /** Free any storage and deallocate any extra data or structures used
285c43e99fdSEd Maste in this implementation. Called when the bufferevent is
286c43e99fdSEd Maste finalized.
287c43e99fdSEd Maste */
288c43e99fdSEd Maste void (*destruct)(struct bufferevent *);
289c43e99fdSEd Maste
290c43e99fdSEd Maste /** Called when the timeouts on the bufferevent have changed.*/
291c43e99fdSEd Maste int (*adj_timeouts)(struct bufferevent *);
292c43e99fdSEd Maste
293c43e99fdSEd Maste /** Called to flush data. */
294c43e99fdSEd Maste int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
295c43e99fdSEd Maste
296c43e99fdSEd Maste /** Called to access miscellaneous fields. */
297c43e99fdSEd Maste int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
298c43e99fdSEd Maste
299c43e99fdSEd Maste };
300c43e99fdSEd Maste
301c43e99fdSEd Maste extern const struct bufferevent_ops bufferevent_ops_socket;
302c43e99fdSEd Maste extern const struct bufferevent_ops bufferevent_ops_filter;
303c43e99fdSEd Maste extern const struct bufferevent_ops bufferevent_ops_pair;
304c43e99fdSEd Maste
305c43e99fdSEd Maste #define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
306c43e99fdSEd Maste #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
307c43e99fdSEd Maste #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
308c43e99fdSEd Maste
309*b50261e2SCy Schubert #if defined(EVENT__HAVE_OPENSSL)
310*b50261e2SCy Schubert extern const struct bufferevent_ops bufferevent_ops_openssl;
311*b50261e2SCy Schubert #define BEV_IS_OPENSSL(bevp) ((bevp)->be_ops == &bufferevent_ops_openssl)
312*b50261e2SCy Schubert #else
313*b50261e2SCy Schubert #define BEV_IS_OPENSSL(bevp) 0
314*b50261e2SCy Schubert #endif
315*b50261e2SCy Schubert
316c43e99fdSEd Maste #ifdef _WIN32
317c43e99fdSEd Maste extern const struct bufferevent_ops bufferevent_ops_async;
318c43e99fdSEd Maste #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
319c43e99fdSEd Maste #else
320c43e99fdSEd Maste #define BEV_IS_ASYNC(bevp) 0
321c43e99fdSEd Maste #endif
322c43e99fdSEd Maste
323c43e99fdSEd Maste /** Initialize the shared parts of a bufferevent. */
324*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
325c43e99fdSEd Maste int bufferevent_init_common_(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
326c43e99fdSEd Maste
327c43e99fdSEd Maste /** For internal use: temporarily stop all reads on bufev, until the conditions
328c43e99fdSEd Maste * in 'what' are over. */
329*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
330c43e99fdSEd Maste void bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
331c43e99fdSEd Maste /** For internal use: clear the conditions 'what' on bufev, and re-enable
332c43e99fdSEd Maste * reading if there are no conditions left. */
333*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
334c43e99fdSEd Maste void bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
335c43e99fdSEd Maste
336c43e99fdSEd Maste /** For internal use: temporarily stop all writes on bufev, until the conditions
337c43e99fdSEd Maste * in 'what' are over. */
338c43e99fdSEd Maste void bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
339c43e99fdSEd Maste /** For internal use: clear the conditions 'what' on bufev, and re-enable
340c43e99fdSEd Maste * writing if there are no conditions left. */
341c43e99fdSEd Maste void bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
342c43e99fdSEd Maste
343c43e99fdSEd Maste #define bufferevent_wm_suspend_read(b) \
344c43e99fdSEd Maste bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
345c43e99fdSEd Maste #define bufferevent_wm_unsuspend_read(b) \
346c43e99fdSEd Maste bufferevent_unsuspend_read_((b), BEV_SUSPEND_WM)
347c43e99fdSEd Maste
348c43e99fdSEd Maste /*
349c43e99fdSEd Maste Disable a bufferevent. Equivalent to bufferevent_disable(), but
350c43e99fdSEd Maste first resets 'connecting' flag to force EV_WRITE down for sure.
351c43e99fdSEd Maste
352c43e99fdSEd Maste XXXX this method will go away in the future; try not to add new users.
353c43e99fdSEd Maste See comment in evhttp_connection_reset_() for discussion.
354c43e99fdSEd Maste
355c43e99fdSEd Maste @param bufev the bufferevent to be disabled
356c43e99fdSEd Maste @param event any combination of EV_READ | EV_WRITE.
357c43e99fdSEd Maste @return 0 if successful, or -1 if an error occurred
358c43e99fdSEd Maste @see bufferevent_disable()
359c43e99fdSEd Maste */
360*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
361c43e99fdSEd Maste int bufferevent_disable_hard_(struct bufferevent *bufev, short event);
362c43e99fdSEd Maste
363c43e99fdSEd Maste /** Internal: Set up locking on a bufferevent. If lock is set, use it.
364c43e99fdSEd Maste * Otherwise, use a new lock. */
365*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
366c43e99fdSEd Maste int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
367c43e99fdSEd Maste /** Internal: backwards compat macro for the now public function
368c43e99fdSEd Maste * Increment the reference count on bufev. */
369c43e99fdSEd Maste #define bufferevent_incref_(bufev) bufferevent_incref(bufev)
370c43e99fdSEd Maste /** Internal: Lock bufev and increase its reference count.
371c43e99fdSEd Maste * unlocking it otherwise. */
372*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
373c43e99fdSEd Maste void bufferevent_incref_and_lock_(struct bufferevent *bufev);
374c43e99fdSEd Maste /** Internal: backwards compat macro for the now public function
375c43e99fdSEd Maste * Decrement the reference count on bufev. Returns 1 if it freed
376c43e99fdSEd Maste * the bufferevent.*/
377c43e99fdSEd Maste #define bufferevent_decref_(bufev) bufferevent_decref(bufev)
378c43e99fdSEd Maste
379c43e99fdSEd Maste /** Internal: Drop the reference count on bufev, freeing as necessary, and
380c43e99fdSEd Maste * unlocking it otherwise. Returns 1 if it freed the bufferevent. */
381*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
382c43e99fdSEd Maste int bufferevent_decref_and_unlock_(struct bufferevent *bufev);
383c43e99fdSEd Maste
384c43e99fdSEd Maste /** Internal: If callbacks are deferred and we have a read callback, schedule
385c43e99fdSEd Maste * a readcb. Otherwise just run the readcb. Ignores watermarks. */
386*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
387c43e99fdSEd Maste void bufferevent_run_readcb_(struct bufferevent *bufev, int options);
388c43e99fdSEd Maste /** Internal: If callbacks are deferred and we have a write callback, schedule
389c43e99fdSEd Maste * a writecb. Otherwise just run the writecb. Ignores watermarks. */
390*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
391c43e99fdSEd Maste void bufferevent_run_writecb_(struct bufferevent *bufev, int options);
392c43e99fdSEd Maste /** Internal: If callbacks are deferred and we have an eventcb, schedule
393c43e99fdSEd Maste * it to run with events "what". Otherwise just run the eventcb.
394c43e99fdSEd Maste * See bufferevent_trigger_event for meaning of "options". */
395*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
396c43e99fdSEd Maste void bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options);
397c43e99fdSEd Maste
398c43e99fdSEd Maste /** Internal: Run or schedule (if deferred or options contain
399c43e99fdSEd Maste * BEV_TRIG_DEFER_CALLBACKS) I/O callbacks specified in iotype.
400c43e99fdSEd Maste * Must already hold the bufev lock. Honors watermarks unless
401c43e99fdSEd Maste * BEV_TRIG_IGNORE_WATERMARKS is in options. */
402c43e99fdSEd Maste static inline void bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options);
403c43e99fdSEd Maste
404c43e99fdSEd Maste /* Making this inline since all of the common-case calls to this function in
405c43e99fdSEd Maste * libevent use constant arguments. */
406c43e99fdSEd Maste static inline void
bufferevent_trigger_nolock_(struct bufferevent * bufev,short iotype,int options)407c43e99fdSEd Maste bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options)
408c43e99fdSEd Maste {
409c43e99fdSEd Maste if ((iotype & EV_READ) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
410c43e99fdSEd Maste evbuffer_get_length(bufev->input) >= bufev->wm_read.low))
411c43e99fdSEd Maste bufferevent_run_readcb_(bufev, options);
412c43e99fdSEd Maste if ((iotype & EV_WRITE) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
413c43e99fdSEd Maste evbuffer_get_length(bufev->output) <= bufev->wm_write.low))
414c43e99fdSEd Maste bufferevent_run_writecb_(bufev, options);
415c43e99fdSEd Maste }
416c43e99fdSEd Maste
417c43e99fdSEd Maste /** Internal: Add the event 'ev' with timeout tv, unless tv is set to 0, in
418c43e99fdSEd Maste * which case add ev with no timeout. */
419*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
420c43e99fdSEd Maste int bufferevent_add_event_(struct event *ev, const struct timeval *tv);
421c43e99fdSEd Maste
422c43e99fdSEd Maste /* =========
423c43e99fdSEd Maste * These next functions implement timeouts for bufferevents that aren't doing
424c43e99fdSEd Maste * anything else with ev_read and ev_write, to handle timeouts.
425c43e99fdSEd Maste * ========= */
426c43e99fdSEd Maste /** Internal use: Set up the ev_read and ev_write callbacks so that
427c43e99fdSEd Maste * the other "generic_timeout" functions will work on it. Call this from
428c43e99fdSEd Maste * the constructor function. */
429*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
430c43e99fdSEd Maste void bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
431c43e99fdSEd Maste /** Internal use: Add or delete the generic timeout events as appropriate.
432c43e99fdSEd Maste * (If an event is enabled and a timeout is set, we add the event. Otherwise
433c43e99fdSEd Maste * we delete it.) Call this from anything that changes the timeout values,
434c43e99fdSEd Maste * that enabled EV_READ or EV_WRITE, or that disables EV_READ or EV_WRITE. */
435*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
436c43e99fdSEd Maste int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
437*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
438c43e99fdSEd Maste int bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev);
439c43e99fdSEd Maste
440*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
441c43e99fdSEd Maste enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
442c43e99fdSEd Maste
443*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
444c43e99fdSEd Maste const struct sockaddr*
445c43e99fdSEd Maste bufferevent_socket_get_conn_address_(struct bufferevent *bev);
446c43e99fdSEd Maste
447*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
448*b50261e2SCy Schubert void
449*b50261e2SCy Schubert bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev, evutil_socket_t fd);
450*b50261e2SCy Schubert
451*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
452*b50261e2SCy Schubert void
453*b50261e2SCy Schubert bufferevent_socket_set_conn_address_(struct bufferevent *bev, struct sockaddr *addr, size_t addrlen);
454*b50261e2SCy Schubert
455*b50261e2SCy Schubert
456c43e99fdSEd Maste /** Internal use: We have just successfully read data into an inbuf, so
457c43e99fdSEd Maste * reset the read timeout (if any). */
458c43e99fdSEd Maste #define BEV_RESET_GENERIC_READ_TIMEOUT(bev) \
459c43e99fdSEd Maste do { \
460c43e99fdSEd Maste if (evutil_timerisset(&(bev)->timeout_read)) \
461c43e99fdSEd Maste event_add(&(bev)->ev_read, &(bev)->timeout_read); \
462c43e99fdSEd Maste } while (0)
463c43e99fdSEd Maste /** Internal use: We have just successfully written data from an inbuf, so
464c43e99fdSEd Maste * reset the read timeout (if any). */
465c43e99fdSEd Maste #define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev) \
466c43e99fdSEd Maste do { \
467c43e99fdSEd Maste if (evutil_timerisset(&(bev)->timeout_write)) \
468c43e99fdSEd Maste event_add(&(bev)->ev_write, &(bev)->timeout_write); \
469c43e99fdSEd Maste } while (0)
470c43e99fdSEd Maste #define BEV_DEL_GENERIC_READ_TIMEOUT(bev) \
471c43e99fdSEd Maste event_del(&(bev)->ev_read)
472c43e99fdSEd Maste #define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev) \
473c43e99fdSEd Maste event_del(&(bev)->ev_write)
474c43e99fdSEd Maste
475c43e99fdSEd Maste
476c43e99fdSEd Maste /** Internal: Given a bufferevent, return its corresponding
477c43e99fdSEd Maste * bufferevent_private. */
478c43e99fdSEd Maste #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
479c43e99fdSEd Maste
480c43e99fdSEd Maste #ifdef EVENT__DISABLE_THREAD_SUPPORT
481c43e99fdSEd Maste #define BEV_LOCK(b) EVUTIL_NIL_STMT_
482c43e99fdSEd Maste #define BEV_UNLOCK(b) EVUTIL_NIL_STMT_
483c43e99fdSEd Maste #else
484c43e99fdSEd Maste /** Internal: Grab the lock (if any) on a bufferevent */
485c43e99fdSEd Maste #define BEV_LOCK(b) do { \
486c43e99fdSEd Maste struct bufferevent_private *locking = BEV_UPCAST(b); \
487c43e99fdSEd Maste EVLOCK_LOCK(locking->lock, 0); \
488c43e99fdSEd Maste } while (0)
489c43e99fdSEd Maste
490c43e99fdSEd Maste /** Internal: Release the lock (if any) on a bufferevent */
491c43e99fdSEd Maste #define BEV_UNLOCK(b) do { \
492c43e99fdSEd Maste struct bufferevent_private *locking = BEV_UPCAST(b); \
493c43e99fdSEd Maste EVLOCK_UNLOCK(locking->lock, 0); \
494c43e99fdSEd Maste } while (0)
495c43e99fdSEd Maste #endif
496c43e99fdSEd Maste
497c43e99fdSEd Maste
498c43e99fdSEd Maste /* ==== For rate-limiting. */
499c43e99fdSEd Maste
500*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
501c43e99fdSEd Maste int bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
502c43e99fdSEd Maste ev_ssize_t bytes);
503*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
504c43e99fdSEd Maste int bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
505c43e99fdSEd Maste ev_ssize_t bytes);
506*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
507c43e99fdSEd Maste ev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
508*b50261e2SCy Schubert EVENT2_EXPORT_SYMBOL
509c43e99fdSEd Maste ev_ssize_t bufferevent_get_write_max_(struct bufferevent_private *bev);
510c43e99fdSEd Maste
511c43e99fdSEd Maste int bufferevent_ratelim_init_(struct bufferevent_private *bev);
512c43e99fdSEd Maste
513c43e99fdSEd Maste #ifdef __cplusplus
514c43e99fdSEd Maste }
515c43e99fdSEd Maste #endif
516c43e99fdSEd Maste
517c43e99fdSEd Maste
518c43e99fdSEd Maste #endif /* BUFFEREVENT_INTERNAL_H_INCLUDED_ */
519