Libevent Code Doxygen document
bufferevent-internal.h
1 /*
2  * Copyright (c) 2008-2012 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef BUFFEREVENT_INTERNAL_H_INCLUDED_
27 #define BUFFEREVENT_INTERNAL_H_INCLUDED_
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 #include "event2/event-config.h"
34 #include "event2/event_struct.h"
35 #include "evconfig-private.h"
36 #include "event2/util.h"
37 #include "defer-internal.h"
38 #include "evthread-internal.h"
39 #include "event2/thread.h"
40 #include "ratelim-internal.h"
42 
43 #include "ipv6-internal.h"
44 #ifdef _WIN32
45 #include <ws2tcpip.h>
46 #endif
47 #ifdef EVENT__HAVE_NETINET_IN_H
48 #include <netinet/in.h>
49 #endif
50 #ifdef EVENT__HAVE_NETINET_IN6_H
51 #include <netinet/in6.h>
52 #endif
53 
54 /* These flags are reasons that we might be declining to actually enable
55  reading or writing on a bufferevent.
56  */
57 
58 /* On a all bufferevents, for reading: used when we have read up to the
59  watermark value.
60 
61  On a filtering bufferevent, for writing: used when the underlying
62  bufferevent's write buffer has been filled up to its watermark
63  value.
64 */
65 #define BEV_SUSPEND_WM 0x01
66 /* On a base bufferevent: when we have emptied a bandwidth buckets */
67 #define BEV_SUSPEND_BW 0x02
68 /* On a base bufferevent: when we have emptied the group's bandwidth bucket. */
69 #define BEV_SUSPEND_BW_GROUP 0x04
70 /* On a socket bufferevent: can't do any operations while we're waiting for
71  * name lookup to finish. */
72 #define BEV_SUSPEND_LOOKUP 0x08
73 /* On a base bufferevent, for reading: used when a filter has choked this
74  * (underlying) bufferevent because it has stopped reading from it. */
75 #define BEV_SUSPEND_FILT_READ 0x10
76 
77 typedef ev_uint16_t bufferevent_suspend_flags;
78 
81  LIST_HEAD(rlim_group_member_list, bufferevent_private) members;
84  struct ev_token_bucket_cfg rate_limit_cfg;
85 
88  unsigned read_suspended : 1;
91  unsigned write_suspended : 1;
95  unsigned pending_unsuspend_read : 1;
100 
104  ev_uint64_t total_read;
105  ev_uint64_t total_written;
110 
113  ev_ssize_t min_share;
114  ev_ssize_t configured_min_share;
115 
118  struct event master_refill_event;
119 
122 
126  void *lock;
127 };
128 
131  /* Linked-list elements for storing this bufferevent_private in a
132  * group.
133  *
134  * Note that this field is supposed to be protected by the group
135  * lock */
136  LIST_ENTRY(bufferevent_private) next_in_group;
140 
141  /* This bufferevent's current limits. */
142  struct ev_token_bucket limit;
143  /* Pointer to the rate-limit configuration for this bufferevent.
144  * Can be shared. XXX reference-count this? */
145  struct ev_token_bucket_cfg *cfg;
146 
147  /* Timeout event used when one this bufferevent's buckets are
148  * empty. */
149  struct event refill_bucket_event;
150 };
151 
156  struct bufferevent bev;
157 
160 
162  unsigned own_lock : 1;
163 
166  unsigned readcb_pending : 1;
169  unsigned writecb_pending : 1;
171  unsigned connecting : 1;
174  unsigned connection_refused : 1;
178 
182  bufferevent_suspend_flags read_suspended;
183 
187  bufferevent_suspend_flags write_suspended;
188 
192 
195 
197  struct event_callback deferred;
198 
201 
203  int refcnt;
204 
207  void *lock;
208 
211  ev_ssize_t max_single_read;
212 
215  ev_ssize_t max_single_write;
216 
219 
220  /* Saved conn_addr, to extract IP address from it.
221  *
222  * Because some servers may reset/close connection without waiting clients,
223  * in that case we can't extract IP address even in close_cb.
224  * So we need to save it, just after we connected to remote server, or
225  * after resolving (to avoid extra dns requests during retrying, since UDP
226  * is slow) */
227  union {
228  struct sockaddr_in6 in6;
229  struct sockaddr_in in;
230  } conn_address;
231 
232  struct evdns_getaddrinfo_request *dns_request;
233 };
234 
236 enum bufferevent_ctrl_op {
237  BEV_CTRL_SET_FD,
238  BEV_CTRL_GET_FD,
239  BEV_CTRL_GET_UNDERLYING,
240  BEV_CTRL_CANCEL_ALL
241 };
242 
245  void *ptr;
246  evutil_socket_t fd;
247 };
248 
255  const char *type;
266  off_t mem_offset;
267 
272  int (*enable)(struct bufferevent *, short);
273 
278  int (*disable)(struct bufferevent *, short);
279 
282  void (*unlink)(struct bufferevent *);
283 
288  void (*destruct)(struct bufferevent *);
289 
291  int (*adj_timeouts)(struct bufferevent *);
292 
294  int (*flush)(struct bufferevent *, short, enum bufferevent_flush_mode);
295 
297  int (*ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
298 
299 };
300 
301 extern const struct bufferevent_ops bufferevent_ops_socket;
302 extern const struct bufferevent_ops bufferevent_ops_filter;
303 extern const struct bufferevent_ops bufferevent_ops_pair;
304 
305 #define BEV_IS_SOCKET(bevp) ((bevp)->be_ops == &bufferevent_ops_socket)
306 #define BEV_IS_FILTER(bevp) ((bevp)->be_ops == &bufferevent_ops_filter)
307 #define BEV_IS_PAIR(bevp) ((bevp)->be_ops == &bufferevent_ops_pair)
308 
309 #if defined(EVENT__HAVE_OPENSSL)
310 extern const struct bufferevent_ops bufferevent_ops_openssl;
311 #define BEV_IS_OPENSSL(bevp) ((bevp)->be_ops == &bufferevent_ops_openssl)
312 #else
313 #define BEV_IS_OPENSSL(bevp) 0
314 #endif
315 
316 #ifdef _WIN32
317 extern const struct bufferevent_ops bufferevent_ops_async;
318 #define BEV_IS_ASYNC(bevp) ((bevp)->be_ops == &bufferevent_ops_async)
319 #else
320 #define BEV_IS_ASYNC(bevp) 0
321 #endif
322 
324 EVENT2_EXPORT_SYMBOL
325 int bufferevent_init_common_(struct bufferevent_private *, struct event_base *, const struct bufferevent_ops *, enum bufferevent_options options);
326 
329 EVENT2_EXPORT_SYMBOL
330 void bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
333 EVENT2_EXPORT_SYMBOL
334 void bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what);
335 
338 void bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
341 void bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what);
342 
343 #define bufferevent_wm_suspend_read(b) \
344  bufferevent_suspend_read_((b), BEV_SUSPEND_WM)
345 #define bufferevent_wm_unsuspend_read(b) \
346  bufferevent_unsuspend_read_((b), BEV_SUSPEND_WM)
347 
348 /*
349  Disable a bufferevent. Equivalent to bufferevent_disable(), but
350  first resets 'connecting' flag to force EV_WRITE down for sure.
351 
352  XXXX this method will go away in the future; try not to add new users.
353  See comment in evhttp_connection_reset_() for discussion.
354 
355  @param bufev the bufferevent to be disabled
356  @param event any combination of EV_READ | EV_WRITE.
357  @return 0 if successful, or -1 if an error occurred
358  @see bufferevent_disable()
359  */
360 EVENT2_EXPORT_SYMBOL
361 int bufferevent_disable_hard_(struct bufferevent *bufev, short event);
362 
365 EVENT2_EXPORT_SYMBOL
366 int bufferevent_enable_locking_(struct bufferevent *bufev, void *lock);
369 #define bufferevent_incref_(bufev) bufferevent_incref(bufev)
372 EVENT2_EXPORT_SYMBOL
373 void bufferevent_incref_and_lock_(struct bufferevent *bufev);
377 #define bufferevent_decref_(bufev) bufferevent_decref(bufev)
378 
381 EVENT2_EXPORT_SYMBOL
382 int bufferevent_decref_and_unlock_(struct bufferevent *bufev);
383 
386 EVENT2_EXPORT_SYMBOL
387 void bufferevent_run_readcb_(struct bufferevent *bufev, int options);
390 EVENT2_EXPORT_SYMBOL
391 void bufferevent_run_writecb_(struct bufferevent *bufev, int options);
395 EVENT2_EXPORT_SYMBOL
396 void bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options);
397 
402 static inline void bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options);
403 
404 /* Making this inline since all of the common-case calls to this function in
405  * libevent use constant arguments. */
406 static inline void
407 bufferevent_trigger_nolock_(struct bufferevent *bufev, short iotype, int options)
408 {
409  if ((iotype & EV_READ) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
410  evbuffer_get_length(bufev->input) >= bufev->wm_read.low))
411  bufferevent_run_readcb_(bufev, options);
412  if ((iotype & EV_WRITE) && ((options & BEV_TRIG_IGNORE_WATERMARKS) ||
413  evbuffer_get_length(bufev->output) <= bufev->wm_write.low))
414  bufferevent_run_writecb_(bufev, options);
415 }
416 
419 EVENT2_EXPORT_SYMBOL
420 int bufferevent_add_event_(struct event *ev, const struct timeval *tv);
421 
422 /* =========
423  * These next functions implement timeouts for bufferevents that aren't doing
424  * anything else with ev_read and ev_write, to handle timeouts.
425  * ========= */
429 EVENT2_EXPORT_SYMBOL
430 void bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev);
435 EVENT2_EXPORT_SYMBOL
436 int bufferevent_generic_adj_timeouts_(struct bufferevent *bev);
437 EVENT2_EXPORT_SYMBOL
438 int bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev);
439 
440 EVENT2_EXPORT_SYMBOL
441 enum bufferevent_options bufferevent_get_options_(struct bufferevent *bev);
442 
443 EVENT2_EXPORT_SYMBOL
444 const struct sockaddr*
445 bufferevent_socket_get_conn_address_(struct bufferevent *bev);
446 
447 EVENT2_EXPORT_SYMBOL
448 void
449 bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev, evutil_socket_t fd);
450 
451 EVENT2_EXPORT_SYMBOL
452 void
453 bufferevent_socket_set_conn_address_(struct bufferevent *bev, struct sockaddr *addr, size_t addrlen);
454 
455 
458 #define BEV_RESET_GENERIC_READ_TIMEOUT(bev) \
459  do { \
460  if (evutil_timerisset(&(bev)->timeout_read)) \
461  event_add(&(bev)->ev_read, &(bev)->timeout_read); \
462  } while (0)
465 #define BEV_RESET_GENERIC_WRITE_TIMEOUT(bev) \
466  do { \
467  if (evutil_timerisset(&(bev)->timeout_write)) \
468  event_add(&(bev)->ev_write, &(bev)->timeout_write); \
469  } while (0)
470 #define BEV_DEL_GENERIC_READ_TIMEOUT(bev) \
471  event_del(&(bev)->ev_read)
472 #define BEV_DEL_GENERIC_WRITE_TIMEOUT(bev) \
473  event_del(&(bev)->ev_write)
474 
475 
478 #define BEV_UPCAST(b) EVUTIL_UPCAST((b), struct bufferevent_private, bev)
479 
480 #ifdef EVENT__DISABLE_THREAD_SUPPORT
481 #define BEV_LOCK(b) EVUTIL_NIL_STMT_
482 #define BEV_UNLOCK(b) EVUTIL_NIL_STMT_
483 #else
485 #define BEV_LOCK(b) do { \
486  struct bufferevent_private *locking = BEV_UPCAST(b); \
487  EVLOCK_LOCK(locking->lock, 0); \
488  } while (0)
489 
491 #define BEV_UNLOCK(b) do { \
492  struct bufferevent_private *locking = BEV_UPCAST(b); \
493  EVLOCK_UNLOCK(locking->lock, 0); \
494  } while (0)
495 #endif
496 
497 
498 /* ==== For rate-limiting. */
499 
500 EVENT2_EXPORT_SYMBOL
501 int bufferevent_decrement_write_buckets_(struct bufferevent_private *bev,
502  ev_ssize_t bytes);
503 EVENT2_EXPORT_SYMBOL
504 int bufferevent_decrement_read_buckets_(struct bufferevent_private *bev,
505  ev_ssize_t bytes);
506 EVENT2_EXPORT_SYMBOL
507 ev_ssize_t bufferevent_get_read_max_(struct bufferevent_private *bev);
508 EVENT2_EXPORT_SYMBOL
509 ev_ssize_t bufferevent_get_write_max_(struct bufferevent_private *bev);
510 
511 int bufferevent_ratelim_init_(struct bufferevent_private *bev);
512 
513 #ifdef __cplusplus
514 }
515 #endif
516 
517 
518 #endif /* BUFFEREVENT_INTERNAL_H_INCLUDED_ */
EVENT2_EXPORT_SYMBOL size_t evbuffer_get_length(const struct evbuffer *buf)
Definition: buffer.c:607
@ BEV_TRIG_IGNORE_WATERMARKS
Definition: bufferevent.h:624
bufferevent_options
Definition: bufferevent.h:153
bufferevent_flush_mode
Definition: bufferevent.h:594
#define EV_WRITE
Definition: event.h:913
#define EV_READ
Definition: event.h:911
Definition: bufferevent-internal.h:253
void(* unlink)(struct bufferevent *)
Definition: bufferevent-internal.h:282
int(* disable)(struct bufferevent *, short)
Definition: bufferevent-internal.h:278
int(* enable)(struct bufferevent *, short)
Definition: bufferevent-internal.h:272
const char * type
Definition: bufferevent-internal.h:255
int(* adj_timeouts)(struct bufferevent *)
Definition: bufferevent-internal.h:291
void(* destruct)(struct bufferevent *)
Definition: bufferevent-internal.h:288
int(* ctrl)(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *)
Definition: bufferevent-internal.h:297
int(* flush)(struct bufferevent *, short, enum bufferevent_flush_mode)
Definition: bufferevent-internal.h:294
off_t mem_offset
Definition: bufferevent-internal.h:266
Definition: bufferevent-internal.h:154
unsigned own_lock
Definition: bufferevent-internal.h:162
bufferevent_suspend_flags write_suspended
Definition: bufferevent-internal.h:187
short eventcb_pending
Definition: bufferevent-internal.h:177
unsigned connecting
Definition: bufferevent-internal.h:171
ev_ssize_t max_single_read
Definition: bufferevent-internal.h:211
ev_ssize_t max_single_write
Definition: bufferevent-internal.h:215
int refcnt
Definition: bufferevent-internal.h:203
struct bufferevent_rate_limit * rate_limiting
Definition: bufferevent-internal.h:218
struct event_callback deferred
Definition: bufferevent-internal.h:197
struct evbuffer_cb_entry * read_watermarks_cb
Definition: bufferevent-internal.h:159
bufferevent_suspend_flags read_suspended
Definition: bufferevent-internal.h:182
enum bufferevent_options options
Definition: bufferevent-internal.h:200
unsigned connection_refused
Definition: bufferevent-internal.h:174
struct bufferevent bev
Definition: bufferevent-internal.h:156
unsigned readcb_pending
Definition: bufferevent-internal.h:166
int errno_pending
Definition: bufferevent-internal.h:191
int dns_error
Definition: bufferevent-internal.h:194
unsigned writecb_pending
Definition: bufferevent-internal.h:169
void * lock
Definition: bufferevent-internal.h:207
Definition: bufferevent-internal.h:79
unsigned pending_unsuspend_write
Definition: bufferevent-internal.h:99
int n_members
Definition: bufferevent-internal.h:109
struct evutil_weakrand_state weakrand_seed
Definition: bufferevent-internal.h:121
void * lock
Definition: bufferevent-internal.h:126
ev_ssize_t min_share
Definition: bufferevent-internal.h:113
struct event master_refill_event
Definition: bufferevent-internal.h:118
LIST_HEAD(rlim_group_member_list, bufferevent_private) members
unsigned read_suspended
Definition: bufferevent-internal.h:88
struct ev_token_bucket rate_limit
Definition: bufferevent-internal.h:83
unsigned write_suspended
Definition: bufferevent-internal.h:91
unsigned pending_unsuspend_read
Definition: bufferevent-internal.h:95
ev_uint64_t total_read
Definition: bufferevent-internal.h:104
Definition: bufferevent-internal.h:130
struct bufferevent_rate_limit_group * group
Definition: bufferevent-internal.h:139
Definition: bufferevent_struct.h:70
struct evbuffer * input
Definition: bufferevent_struct.h:88
struct evbuffer * output
Definition: bufferevent_struct.h:92
Definition: ratelim-internal.h:48
Definition: ratelim-internal.h:38
Definition: evbuffer-internal.h:62
Definition: evdns.c:4304
Definition: event-internal.h:208
Definition: event_struct.h:107
Definition: event_struct.h:123
Definition: util-internal.h:315
Definition: http-server.c:102
Definition: ipv6-internal.h:63
Definition: bufferevent-internal.h:244
#define evutil_socket_t
Definition: util.h:310