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