]> granicus.if.org Git - libevent/blob - bufferevent.c
Add minimal WebSocket server implementation for evhttp (#1322)
[libevent] / bufferevent.c
1 /*
2  * Copyright (c) 2002-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2012 Niels Provos, Nick Mathewson
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
30
31 #include <sys/types.h>
32
33 #ifdef EVENT__HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #endif
36
37 #include <errno.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #ifdef EVENT__HAVE_STDARG_H
42 #include <stdarg.h>
43 #endif
44
45 #ifdef _WIN32
46 #include <winsock2.h>
47 #endif
48
49 #include "event2/util.h"
50 #include "event2/buffer.h"
51 #include "event2/buffer_compat.h"
52 #include "event2/bufferevent.h"
53 #include "event2/bufferevent_struct.h"
54 #include "event2/bufferevent_compat.h"
55 #include "event2/event.h"
56 #include "event-internal.h"
57 #include "log-internal.h"
58 #include "mm-internal.h"
59 #include "bufferevent-internal.h"
60 #include "evbuffer-internal.h"
61 #include "util-internal.h"
62
63 static void bufferevent_cancel_all_(struct bufferevent *bev);
64 static void bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_);
65
66 void
67 bufferevent_suspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
68 {
69         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
70         BEV_LOCK(bufev);
71         if (!bufev_private->read_suspended)
72                 bufev->be_ops->disable(bufev, EV_READ);
73         bufev_private->read_suspended |= what;
74         BEV_UNLOCK(bufev);
75 }
76
77 void
78 bufferevent_unsuspend_read_(struct bufferevent *bufev, bufferevent_suspend_flags what)
79 {
80         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
81         BEV_LOCK(bufev);
82         bufev_private->read_suspended &= ~what;
83         if (!bufev_private->read_suspended && (bufev->enabled & EV_READ))
84                 bufev->be_ops->enable(bufev, EV_READ);
85         BEV_UNLOCK(bufev);
86 }
87
88 void
89 bufferevent_suspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
90 {
91         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
92         BEV_LOCK(bufev);
93         if (!bufev_private->write_suspended)
94                 bufev->be_ops->disable(bufev, EV_WRITE);
95         bufev_private->write_suspended |= what;
96         BEV_UNLOCK(bufev);
97 }
98
99 void
100 bufferevent_unsuspend_write_(struct bufferevent *bufev, bufferevent_suspend_flags what)
101 {
102         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
103         BEV_LOCK(bufev);
104         bufev_private->write_suspended &= ~what;
105         if (!bufev_private->write_suspended && (bufev->enabled & EV_WRITE))
106                 bufev->be_ops->enable(bufev, EV_WRITE);
107         BEV_UNLOCK(bufev);
108 }
109
110 /**
111  * Sometimes bufferevent's implementation can overrun high watermarks
112  * (one of examples is openssl) and in this case if the read callback
113  * will not handle enough data do over condition above the read
114  * callback will never be called again (due to suspend above).
115  *
116  * To avoid this we are scheduling read callback again here, but only
117  * from the user callback to avoid multiple scheduling:
118  * - when the data had been added to it
119  * - when the data had been drained from it (user specified read callback)
120  */
121 static void bufferevent_inbuf_wm_check(struct bufferevent *bev)
122 {
123         if (!bev->wm_read.high)
124                 return;
125         if (!(bev->enabled & EV_READ))
126                 return;
127         if (evbuffer_get_length(bev->input) < bev->wm_read.high)
128                 return;
129
130         bufferevent_trigger(bev, EV_READ, BEV_OPT_DEFER_CALLBACKS);
131 }
132
133 /* Callback to implement watermarks on the input buffer.  Only enabled
134  * if the watermark is set. */
135 static void
136 bufferevent_inbuf_wm_cb(struct evbuffer *buf,
137     const struct evbuffer_cb_info *cbinfo,
138     void *arg)
139 {
140         struct bufferevent *bufev = arg;
141         size_t size;
142
143         size = evbuffer_get_length(buf);
144
145         if (size >= bufev->wm_read.high)
146                 bufferevent_wm_suspend_read(bufev);
147         else
148                 bufferevent_wm_unsuspend_read(bufev);
149 }
150
151 static void
152 bufferevent_run_deferred_callbacks_locked(struct event_callback *cb, void *arg)
153 {
154         struct bufferevent_private *bufev_private = arg;
155         struct bufferevent *bufev = &bufev_private->bev;
156
157         BEV_LOCK(bufev);
158         if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
159             bufev->errorcb) {
160                 /* The "connected" happened before any reads or writes, so
161                    send it first. */
162                 bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
163                 bufev->errorcb(bufev, BEV_EVENT_CONNECTED, bufev->cbarg);
164         }
165         if (bufev_private->readcb_pending && bufev->readcb) {
166                 bufev_private->readcb_pending = 0;
167                 bufev->readcb(bufev, bufev->cbarg);
168                 bufferevent_inbuf_wm_check(bufev);
169         }
170         if (bufev_private->writecb_pending && bufev->writecb) {
171                 bufev_private->writecb_pending = 0;
172                 bufev->writecb(bufev, bufev->cbarg);
173         }
174         if (bufev_private->eventcb_pending && bufev->errorcb) {
175                 short what = bufev_private->eventcb_pending;
176                 int err = bufev_private->errno_pending;
177                 bufev_private->eventcb_pending = 0;
178                 bufev_private->errno_pending = 0;
179                 EVUTIL_SET_SOCKET_ERROR(err);
180                 bufev->errorcb(bufev, what, bufev->cbarg);
181         }
182         bufferevent_decref_and_unlock_(bufev);
183 }
184
185 static void
186 bufferevent_run_deferred_callbacks_unlocked(struct event_callback *cb, void *arg)
187 {
188         struct bufferevent_private *bufev_private = arg;
189         struct bufferevent *bufev = &bufev_private->bev;
190
191         BEV_LOCK(bufev);
192 #define UNLOCKED(stmt) \
193         do { BEV_UNLOCK(bufev); stmt; BEV_LOCK(bufev); } while(0)
194
195         if ((bufev_private->eventcb_pending & BEV_EVENT_CONNECTED) &&
196             bufev->errorcb) {
197                 /* The "connected" happened before any reads or writes, so
198                    send it first. */
199                 bufferevent_event_cb errorcb = bufev->errorcb;
200                 void *cbarg = bufev->cbarg;
201                 bufev_private->eventcb_pending &= ~BEV_EVENT_CONNECTED;
202                 UNLOCKED(errorcb(bufev, BEV_EVENT_CONNECTED, cbarg));
203         }
204         if (bufev_private->readcb_pending && bufev->readcb) {
205                 bufferevent_data_cb readcb = bufev->readcb;
206                 void *cbarg = bufev->cbarg;
207                 bufev_private->readcb_pending = 0;
208                 UNLOCKED(readcb(bufev, cbarg));
209                 bufferevent_inbuf_wm_check(bufev);
210         }
211         if (bufev_private->writecb_pending && bufev->writecb) {
212                 bufferevent_data_cb writecb = bufev->writecb;
213                 void *cbarg = bufev->cbarg;
214                 bufev_private->writecb_pending = 0;
215                 UNLOCKED(writecb(bufev, cbarg));
216         }
217         if (bufev_private->eventcb_pending && bufev->errorcb) {
218                 bufferevent_event_cb errorcb = bufev->errorcb;
219                 void *cbarg = bufev->cbarg;
220                 short what = bufev_private->eventcb_pending;
221                 int err = bufev_private->errno_pending;
222                 bufev_private->eventcb_pending = 0;
223                 bufev_private->errno_pending = 0;
224                 EVUTIL_SET_SOCKET_ERROR(err);
225                 UNLOCKED(errorcb(bufev,what,cbarg));
226         }
227         bufferevent_decref_and_unlock_(bufev);
228 #undef UNLOCKED
229 }
230
231 #define SCHEDULE_DEFERRED(bevp)                                         \
232         do {                                                            \
233                 if (event_deferred_cb_schedule_(                        \
234                             (bevp)->bev.ev_base,                        \
235                         &(bevp)->deferred))                             \
236                         bufferevent_incref_(&(bevp)->bev);              \
237         } while (0)
238
239
240 void
241 bufferevent_run_readcb_(struct bufferevent *bufev, int options)
242 {
243         /* Requires that we hold the lock and a reference */
244         struct bufferevent_private *p = BEV_UPCAST(bufev);
245         if (bufev->readcb == NULL)
246                 return;
247         if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
248                 p->readcb_pending = 1;
249                 SCHEDULE_DEFERRED(p);
250         } else {
251                 bufev->readcb(bufev, bufev->cbarg);
252                 bufferevent_inbuf_wm_check(bufev);
253         }
254 }
255
256 void
257 bufferevent_run_writecb_(struct bufferevent *bufev, int options)
258 {
259         /* Requires that we hold the lock and a reference */
260         struct bufferevent_private *p = BEV_UPCAST(bufev);
261         if (bufev->writecb == NULL)
262                 return;
263         if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
264                 p->writecb_pending = 1;
265                 SCHEDULE_DEFERRED(p);
266         } else {
267                 bufev->writecb(bufev, bufev->cbarg);
268         }
269 }
270
271 #define BEV_TRIG_ALL_OPTS (                     \
272                 BEV_TRIG_IGNORE_WATERMARKS|     \
273                 BEV_TRIG_DEFER_CALLBACKS        \
274         )
275
276 void
277 bufferevent_trigger(struct bufferevent *bufev, short iotype, int options)
278 {
279         bufferevent_incref_and_lock_(bufev);
280         bufferevent_trigger_nolock_(bufev, iotype, options&BEV_TRIG_ALL_OPTS);
281         bufferevent_decref_and_unlock_(bufev);
282 }
283
284 void
285 bufferevent_run_eventcb_(struct bufferevent *bufev, short what, int options)
286 {
287         /* Requires that we hold the lock and a reference */
288         struct bufferevent_private *p = BEV_UPCAST(bufev);
289         if (bufev->errorcb == NULL)
290                 return;
291         if ((p->options|options) & BEV_OPT_DEFER_CALLBACKS) {
292                 p->eventcb_pending |= what;
293                 p->errno_pending = EVUTIL_SOCKET_ERROR();
294                 SCHEDULE_DEFERRED(p);
295         } else {
296                 bufev->errorcb(bufev, what, bufev->cbarg);
297         }
298 }
299
300 void
301 bufferevent_trigger_event(struct bufferevent *bufev, short what, int options)
302 {
303         bufferevent_incref_and_lock_(bufev);
304         bufferevent_run_eventcb_(bufev, what, options&BEV_TRIG_ALL_OPTS);
305         bufferevent_decref_and_unlock_(bufev);
306 }
307
308 int
309 bufferevent_init_common_(struct bufferevent_private *bufev_private,
310     struct event_base *base,
311     const struct bufferevent_ops *ops,
312     enum bufferevent_options options)
313 {
314         struct bufferevent *bufev = &bufev_private->bev;
315
316         if (!bufev->input) {
317                 if ((bufev->input = evbuffer_new()) == NULL)
318                         goto err;
319         }
320
321         if (!bufev->output) {
322                 if ((bufev->output = evbuffer_new()) == NULL)
323                         goto err;
324         }
325
326         bufev_private->refcnt = 1;
327         bufev->ev_base = base;
328
329         /* Disable timeouts. */
330         evutil_timerclear(&bufev->timeout_read);
331         evutil_timerclear(&bufev->timeout_write);
332
333         bufev->be_ops = ops;
334
335         if (bufferevent_ratelim_init_(bufev_private))
336                 goto err;
337
338         /*
339          * Set to EV_WRITE so that using bufferevent_write is going to
340          * trigger a callback.  Reading needs to be explicitly enabled
341          * because otherwise no data will be available.
342          */
343         bufev->enabled = EV_WRITE;
344
345 #ifndef EVENT__DISABLE_THREAD_SUPPORT
346         if (options & BEV_OPT_THREADSAFE) {
347                 if (bufferevent_enable_locking_(bufev, NULL) < 0)
348                         goto err;
349         }
350 #endif
351         if ((options & (BEV_OPT_DEFER_CALLBACKS|BEV_OPT_UNLOCK_CALLBACKS))
352             == BEV_OPT_UNLOCK_CALLBACKS) {
353                 event_warnx("UNLOCK_CALLBACKS requires DEFER_CALLBACKS");
354                 goto err;
355         }
356         if (options & BEV_OPT_UNLOCK_CALLBACKS)
357                 event_deferred_cb_init_(
358                     &bufev_private->deferred,
359                     event_base_get_npriorities(base) / 2,
360                     bufferevent_run_deferred_callbacks_unlocked,
361                     bufev_private);
362         else
363                 event_deferred_cb_init_(
364                     &bufev_private->deferred,
365                     event_base_get_npriorities(base) / 2,
366                     bufferevent_run_deferred_callbacks_locked,
367                     bufev_private);
368
369         bufev_private->options = options;
370
371         evbuffer_set_parent_(bufev->input, bufev);
372         evbuffer_set_parent_(bufev->output, bufev);
373
374         return 0;
375
376 err:
377         if (bufev->input) {
378                 evbuffer_free(bufev->input);
379                 bufev->input = NULL;
380         }
381         if (bufev->output) {
382                 evbuffer_free(bufev->output);
383                 bufev->output = NULL;
384         }
385         return -1;
386 }
387
388 void
389 bufferevent_setcb(struct bufferevent *bufev,
390     bufferevent_data_cb readcb, bufferevent_data_cb writecb,
391     bufferevent_event_cb eventcb, void *cbarg)
392 {
393         BEV_LOCK(bufev);
394
395         bufev->readcb = readcb;
396         bufev->writecb = writecb;
397         bufev->errorcb = eventcb;
398
399         bufev->cbarg = cbarg;
400         BEV_UNLOCK(bufev);
401 }
402
403 void
404 bufferevent_getcb(struct bufferevent *bufev,
405     bufferevent_data_cb *readcb_ptr,
406     bufferevent_data_cb *writecb_ptr,
407     bufferevent_event_cb *eventcb_ptr,
408     void **cbarg_ptr)
409 {
410         BEV_LOCK(bufev);
411         if (readcb_ptr)
412                 *readcb_ptr = bufev->readcb;
413         if (writecb_ptr)
414                 *writecb_ptr = bufev->writecb;
415         if (eventcb_ptr)
416                 *eventcb_ptr = bufev->errorcb;
417         if (cbarg_ptr)
418                 *cbarg_ptr = bufev->cbarg;
419
420         BEV_UNLOCK(bufev);
421 }
422
423 struct evbuffer *
424 bufferevent_get_input(struct bufferevent *bufev)
425 {
426         return bufev->input;
427 }
428
429 struct evbuffer *
430 bufferevent_get_output(struct bufferevent *bufev)
431 {
432         return bufev->output;
433 }
434
435 struct event_base *
436 bufferevent_get_base(struct bufferevent *bufev)
437 {
438         return bufev->ev_base;
439 }
440
441 int
442 bufferevent_get_priority(const struct bufferevent *bufev)
443 {
444         if (event_initialized(&bufev->ev_read)) {
445                 return event_get_priority(&bufev->ev_read);
446         } else {
447                 return event_base_get_npriorities(bufev->ev_base) / 2;
448         }
449 }
450
451 int
452 bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
453 {
454         if (evbuffer_add(bufev->output, data, size) == -1)
455                 return (-1);
456
457         return 0;
458 }
459
460 int
461 bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
462 {
463         if (evbuffer_add_buffer(bufev->output, buf) == -1)
464                 return (-1);
465
466         return 0;
467 }
468
469 size_t
470 bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
471 {
472         int r = evbuffer_remove(bufev->input, data, size);
473
474         if (r == -1)
475                 return 0;
476
477         return r;
478 }
479
480 int
481 bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
482 {
483         return (evbuffer_add_buffer(buf, bufev->input));
484 }
485
486 int
487 bufferevent_enable(struct bufferevent *bufev, short event)
488 {
489         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
490         short impl_events = event;
491         int r = 0;
492
493         bufferevent_incref_and_lock_(bufev);
494         if (bufev_private->read_suspended)
495                 impl_events &= ~EV_READ;
496         if (bufev_private->write_suspended)
497                 impl_events &= ~EV_WRITE;
498
499         bufev->enabled |= event;
500
501         if (impl_events && bufev->be_ops->enable(bufev, impl_events) < 0)
502                 r = -1;
503         if (r)
504                 event_debug(("%s: cannot enable 0x%hx on %p", __func__, event, (void *)bufev));
505
506         bufferevent_decref_and_unlock_(bufev);
507         return r;
508 }
509
510 int
511 bufferevent_set_timeouts(struct bufferevent *bufev,
512                          const struct timeval *tv_read,
513                          const struct timeval *tv_write)
514 {
515         int r = 0;
516         BEV_LOCK(bufev);
517         if (tv_read) {
518                 bufev->timeout_read = *tv_read;
519         } else {
520                 evutil_timerclear(&bufev->timeout_read);
521         }
522         if (tv_write) {
523                 bufev->timeout_write = *tv_write;
524         } else {
525                 evutil_timerclear(&bufev->timeout_write);
526         }
527
528         if (bufev->be_ops->adj_timeouts)
529                 r = bufev->be_ops->adj_timeouts(bufev);
530         BEV_UNLOCK(bufev);
531
532         return r;
533 }
534
535
536 /* Obsolete; use bufferevent_set_timeouts */
537 void
538 bufferevent_settimeout(struct bufferevent *bufev,
539                        int timeout_read, int timeout_write)
540 {
541         struct timeval tv_read, tv_write;
542         struct timeval *ptv_read = NULL, *ptv_write = NULL;
543
544         memset(&tv_read, 0, sizeof(tv_read));
545         memset(&tv_write, 0, sizeof(tv_write));
546
547         if (timeout_read) {
548                 tv_read.tv_sec = timeout_read;
549                 ptv_read = &tv_read;
550         }
551         if (timeout_write) {
552                 tv_write.tv_sec = timeout_write;
553                 ptv_write = &tv_write;
554         }
555
556         bufferevent_set_timeouts(bufev, ptv_read, ptv_write);
557 }
558
559
560 int
561 bufferevent_disable_hard_(struct bufferevent *bufev, short event)
562 {
563         int r = 0;
564         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
565
566         BEV_LOCK(bufev);
567         bufev->enabled &= ~event;
568
569         bufev_private->connecting = 0;
570         if (bufev->be_ops->disable(bufev, event) < 0)
571                 r = -1;
572
573         BEV_UNLOCK(bufev);
574         return r;
575 }
576
577 int
578 bufferevent_disable(struct bufferevent *bufev, short event)
579 {
580         int r = 0;
581
582         BEV_LOCK(bufev);
583         bufev->enabled &= ~event;
584
585         if (bufev->be_ops->disable(bufev, event) < 0)
586                 r = -1;
587         if (r)
588                 event_debug(("%s: cannot disable 0x%hx on %p", __func__, event, (void *)bufev));
589
590         BEV_UNLOCK(bufev);
591         return r;
592 }
593
594 /*
595  * Sets the water marks
596  */
597
598 void
599 bufferevent_setwatermark(struct bufferevent *bufev, short events,
600     size_t lowmark, size_t highmark)
601 {
602         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
603
604         BEV_LOCK(bufev);
605         if (events & EV_WRITE) {
606                 bufev->wm_write.low = lowmark;
607                 bufev->wm_write.high = highmark;
608         }
609
610         if (events & EV_READ) {
611                 bufev->wm_read.low = lowmark;
612                 bufev->wm_read.high = highmark;
613
614                 if (highmark) {
615                         /* There is now a new high-water mark for read.
616                            enable the callback if needed, and see if we should
617                            suspend/bufferevent_wm_unsuspend. */
618
619                         if (bufev_private->read_watermarks_cb == NULL) {
620                                 bufev_private->read_watermarks_cb =
621                                     evbuffer_add_cb(bufev->input,
622                                                     bufferevent_inbuf_wm_cb,
623                                                     bufev);
624                         }
625                         evbuffer_cb_set_flags(bufev->input,
626                                       bufev_private->read_watermarks_cb,
627                                       EVBUFFER_CB_ENABLED|EVBUFFER_CB_NODEFER);
628
629                         if (evbuffer_get_length(bufev->input) >= highmark)
630                                 bufferevent_wm_suspend_read(bufev);
631                         else if (evbuffer_get_length(bufev->input) < highmark)
632                                 bufferevent_wm_unsuspend_read(bufev);
633                 } else {
634                         /* There is now no high-water mark for read. */
635                         if (bufev_private->read_watermarks_cb)
636                                 evbuffer_cb_clear_flags(bufev->input,
637                                     bufev_private->read_watermarks_cb,
638                                     EVBUFFER_CB_ENABLED);
639                         bufferevent_wm_unsuspend_read(bufev);
640                 }
641         }
642         BEV_UNLOCK(bufev);
643 }
644
645 int
646 bufferevent_getwatermark(struct bufferevent *bufev, short events,
647     size_t *lowmark, size_t *highmark)
648 {
649         if (events == EV_WRITE) {
650                 BEV_LOCK(bufev);
651                 if (lowmark)
652                         *lowmark = bufev->wm_write.low;
653                 if (highmark)
654                         *highmark = bufev->wm_write.high;
655                 BEV_UNLOCK(bufev);
656                 return 0;
657         }
658
659         if (events == EV_READ) {
660                 BEV_LOCK(bufev);
661                 if (lowmark)
662                         *lowmark = bufev->wm_read.low;
663                 if (highmark)
664                         *highmark = bufev->wm_read.high;
665                 BEV_UNLOCK(bufev);
666                 return 0;
667         }
668         return -1;
669 }
670
671 int
672 bufferevent_flush(struct bufferevent *bufev,
673     short iotype,
674     enum bufferevent_flush_mode mode)
675 {
676         int r = -1;
677         BEV_LOCK(bufev);
678         if (bufev->be_ops->flush)
679                 r = bufev->be_ops->flush(bufev, iotype, mode);
680         BEV_UNLOCK(bufev);
681         return r;
682 }
683
684 void
685 bufferevent_incref_and_lock_(struct bufferevent *bufev)
686 {
687         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
688         BEV_LOCK(bufev);
689         ++bufev_private->refcnt;
690 }
691
692 #if 0
693 static void
694 bufferevent_transfer_lock_ownership_(struct bufferevent *donor,
695     struct bufferevent *recipient)
696 {
697         struct bufferevent_private *d = BEV_UPCAST(donor);
698         struct bufferevent_private *r = BEV_UPCAST(recipient);
699         if (d->lock != r->lock)
700                 return;
701         if (r->own_lock)
702                 return;
703         if (d->own_lock) {
704                 d->own_lock = 0;
705                 r->own_lock = 1;
706         }
707 }
708 #endif
709
710 int
711 bufferevent_decref_and_unlock_(struct bufferevent *bufev)
712 {
713         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
714         int n_cbs = 0;
715 #define MAX_CBS 16
716         struct event_callback *cbs[MAX_CBS];
717
718         EVUTIL_ASSERT(bufev_private->refcnt > 0);
719
720         if (--bufev_private->refcnt) {
721                 BEV_UNLOCK(bufev);
722                 return 0;
723         }
724
725         if (bufev->be_ops->unlink)
726                 bufev->be_ops->unlink(bufev);
727
728         /* Okay, we're out of references. Let's finalize this once all the
729          * callbacks are done running. */
730         cbs[0] = &bufev->ev_read.ev_evcallback;
731         cbs[1] = &bufev->ev_write.ev_evcallback;
732         cbs[2] = &bufev_private->deferred;
733         n_cbs = 3;
734         if (bufev_private->rate_limiting) {
735                 struct event *e = &bufev_private->rate_limiting->refill_bucket_event;
736                 if (event_initialized(e))
737                         cbs[n_cbs++] = &e->ev_evcallback;
738         }
739         n_cbs += evbuffer_get_callbacks_(bufev->input, cbs+n_cbs, MAX_CBS-n_cbs);
740         n_cbs += evbuffer_get_callbacks_(bufev->output, cbs+n_cbs, MAX_CBS-n_cbs);
741
742         event_callback_finalize_many_(bufev->ev_base, n_cbs, cbs,
743             bufferevent_finalize_cb_);
744
745 #undef MAX_CBS
746         BEV_UNLOCK(bufev);
747
748         return 1;
749 }
750
751 static void
752 bufferevent_finalize_cb_(struct event_callback *evcb, void *arg_)
753 {
754         struct bufferevent *bufev = arg_;
755         struct bufferevent *underlying;
756         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
757
758         BEV_LOCK(bufev);
759         underlying = bufferevent_get_underlying(bufev);
760
761         /* Clean up the shared info */
762         if (bufev->be_ops->destruct)
763                 bufev->be_ops->destruct(bufev);
764
765         /* XXX what happens if refcnt for these buffers is > 1?
766          * The buffers can share a lock with this bufferevent object,
767          * but the lock might be destroyed below. */
768         /* evbuffer will free the callbacks */
769         evbuffer_free(bufev->input);
770         evbuffer_free(bufev->output);
771
772         if (bufev_private->rate_limiting) {
773                 if (bufev_private->rate_limiting->group)
774                         bufferevent_remove_from_rate_limit_group_internal_(bufev,0);
775                 mm_free(bufev_private->rate_limiting);
776                 bufev_private->rate_limiting = NULL;
777         }
778
779
780         BEV_UNLOCK(bufev);
781
782         if (bufev_private->own_lock)
783                 EVTHREAD_FREE_LOCK(bufev_private->lock,
784                     EVTHREAD_LOCKTYPE_RECURSIVE);
785
786         /* Free the actual allocated memory. */
787         mm_free(((char*)bufev) - bufev->be_ops->mem_offset);
788
789         /* Release the reference to underlying now that we no longer need the
790          * reference to it.  We wait this long mainly in case our lock is
791          * shared with underlying.
792          *
793          * The 'destruct' function will also drop a reference to underlying
794          * if BEV_OPT_CLOSE_ON_FREE is set.
795          *
796          * XXX Should we/can we just refcount evbuffer/bufferevent locks?
797          * It would probably save us some headaches.
798          */
799         if (underlying)
800                 bufferevent_decref_(underlying);
801 }
802
803 int
804 bufferevent_decref(struct bufferevent *bufev)
805 {
806         BEV_LOCK(bufev);
807         return bufferevent_decref_and_unlock_(bufev);
808 }
809
810 void
811 bufferevent_free(struct bufferevent *bufev)
812 {
813         BEV_LOCK(bufev);
814         bufferevent_setcb(bufev, NULL, NULL, NULL, NULL);
815         bufferevent_cancel_all_(bufev);
816         bufferevent_decref_and_unlock_(bufev);
817 }
818
819 void
820 bufferevent_incref(struct bufferevent *bufev)
821 {
822         struct bufferevent_private *bufev_private = BEV_UPCAST(bufev);
823
824         /* XXX: now that this function is public, we might want to
825          * - return the count from this function
826          * - create a new function to atomically grab the current refcount
827          */
828         BEV_LOCK(bufev);
829         ++bufev_private->refcnt;
830         BEV_UNLOCK(bufev);
831 }
832
833 int
834 bufferevent_enable_locking_(struct bufferevent *bufev, void *lock)
835 {
836 #ifdef EVENT__DISABLE_THREAD_SUPPORT
837         return -1;
838 #else
839         struct bufferevent *underlying;
840
841         if (BEV_UPCAST(bufev)->lock)
842                 return -1;
843         underlying = bufferevent_get_underlying(bufev);
844
845         if (!lock && underlying && BEV_UPCAST(underlying)->lock) {
846                 lock = BEV_UPCAST(underlying)->lock;
847                 BEV_UPCAST(bufev)->lock = lock;
848                 BEV_UPCAST(bufev)->own_lock = 0;
849         } else if (!lock) {
850                 EVTHREAD_ALLOC_LOCK(lock, EVTHREAD_LOCKTYPE_RECURSIVE);
851                 if (!lock)
852                         return -1;
853                 BEV_UPCAST(bufev)->lock = lock;
854                 BEV_UPCAST(bufev)->own_lock = 1;
855         } else {
856                 BEV_UPCAST(bufev)->lock = lock;
857                 BEV_UPCAST(bufev)->own_lock = 0;
858         }
859         evbuffer_enable_locking(bufev->input, lock);
860         evbuffer_enable_locking(bufev->output, lock);
861
862         if (underlying && !BEV_UPCAST(underlying)->lock)
863                 bufferevent_enable_locking_(underlying, lock);
864
865         return 0;
866 #endif
867 }
868
869 int
870 bufferevent_setfd(struct bufferevent *bev, evutil_socket_t fd)
871 {
872         union bufferevent_ctrl_data d;
873         int res = -1;
874         d.fd = fd;
875         BEV_LOCK(bev);
876         if (bev->be_ops->ctrl)
877                 res = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
878         if (res)
879                 event_debug(("%s: cannot set fd for %p to "EV_SOCK_FMT, __func__, (void *)bev, fd));
880         BEV_UNLOCK(bev);
881         return res;
882 }
883
884 int
885 bufferevent_replacefd(struct bufferevent *bev, evutil_socket_t fd)
886 {
887         union bufferevent_ctrl_data d;
888         int err = -1;
889         evutil_socket_t old_fd = EVUTIL_INVALID_SOCKET;
890
891         BEV_LOCK(bev);
892         if (bev->be_ops->ctrl) {
893                 err = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
894                 if (!err) {
895                         old_fd = d.fd;
896                         if (old_fd != EVUTIL_INVALID_SOCKET) {
897                                 err = evutil_closesocket(old_fd);
898                         }
899                 }
900                 if (!err) {
901                         d.fd = fd;
902                         err = bev->be_ops->ctrl(bev, BEV_CTRL_SET_FD, &d);
903                 }
904         }
905         if (err)
906                 event_debug(("%s: cannot replace fd for %p from "EV_SOCK_FMT" to "EV_SOCK_FMT, __func__, (void *)bev, old_fd, fd));
907         BEV_UNLOCK(bev);
908
909         return err;
910 }
911
912 evutil_socket_t
913 bufferevent_getfd(struct bufferevent *bev)
914 {
915         union bufferevent_ctrl_data d;
916         int res = -1;
917         d.fd = -1;
918         BEV_LOCK(bev);
919         if (bev->be_ops->ctrl)
920                 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_FD, &d);
921         if (res)
922                 event_debug(("%s: cannot get fd for %p", __func__, (void *)bev));
923         BEV_UNLOCK(bev);
924         return (res<0) ? -1 : d.fd;
925 }
926
927 enum bufferevent_options
928 bufferevent_get_options_(struct bufferevent *bev)
929 {
930         struct bufferevent_private *bev_p = BEV_UPCAST(bev);
931         enum bufferevent_options options;
932
933         BEV_LOCK(bev);
934         options = bev_p->options;
935         BEV_UNLOCK(bev);
936         return options;
937 }
938
939
940 static void
941 bufferevent_cancel_all_(struct bufferevent *bev)
942 {
943         union bufferevent_ctrl_data d;
944         memset(&d, 0, sizeof(d));
945         BEV_LOCK(bev);
946         if (bev->be_ops->ctrl)
947                 bev->be_ops->ctrl(bev, BEV_CTRL_CANCEL_ALL, &d);
948         BEV_UNLOCK(bev);
949 }
950
951 short
952 bufferevent_get_enabled(struct bufferevent *bufev)
953 {
954         short r;
955         BEV_LOCK(bufev);
956         r = bufev->enabled;
957         BEV_UNLOCK(bufev);
958         return r;
959 }
960
961 struct bufferevent *
962 bufferevent_get_underlying(struct bufferevent *bev)
963 {
964         union bufferevent_ctrl_data d;
965         int res = -1;
966         d.ptr = NULL;
967         BEV_LOCK(bev);
968         if (bev->be_ops->ctrl)
969                 res = bev->be_ops->ctrl(bev, BEV_CTRL_GET_UNDERLYING, &d);
970         BEV_UNLOCK(bev);
971         return (res<0) ? NULL : d.ptr;
972 }
973
974 static void
975 bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx)
976 {
977         struct bufferevent *bev = ctx;
978         bufferevent_incref_and_lock_(bev);
979         bufferevent_disable(bev, EV_READ);
980         bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING, 0);
981         bufferevent_decref_and_unlock_(bev);
982 }
983 static void
984 bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx)
985 {
986         struct bufferevent *bev = ctx;
987         bufferevent_incref_and_lock_(bev);
988         bufferevent_disable(bev, EV_WRITE);
989         bufferevent_run_eventcb_(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING, 0);
990         bufferevent_decref_and_unlock_(bev);
991 }
992
993 void
994 bufferevent_init_generic_timeout_cbs_(struct bufferevent *bev)
995 {
996         event_assign(&bev->ev_read, bev->ev_base, -1, EV_FINALIZE,
997             bufferevent_generic_read_timeout_cb, bev);
998         event_assign(&bev->ev_write, bev->ev_base, -1, EV_FINALIZE,
999             bufferevent_generic_write_timeout_cb, bev);
1000 }
1001
1002 int
1003 bufferevent_generic_adj_timeouts_(struct bufferevent *bev)
1004 {
1005         const short enabled = bev->enabled;
1006         struct bufferevent_private *bev_p = BEV_UPCAST(bev);
1007         int r1=0, r2=0;
1008         if ((enabled & EV_READ) && !bev_p->read_suspended &&
1009             evutil_timerisset(&bev->timeout_read))
1010                 r1 = event_add(&bev->ev_read, &bev->timeout_read);
1011         else
1012                 r1 = event_del(&bev->ev_read);
1013
1014         if ((enabled & EV_WRITE) && !bev_p->write_suspended &&
1015             evutil_timerisset(&bev->timeout_write) &&
1016             evbuffer_get_length(bev->output))
1017                 r2 = event_add(&bev->ev_write, &bev->timeout_write);
1018         else
1019                 r2 = event_del(&bev->ev_write);
1020         if (r1 < 0 || r2 < 0)
1021                 return -1;
1022         return 0;
1023 }
1024
1025 int
1026 bufferevent_generic_adj_existing_timeouts_(struct bufferevent *bev)
1027 {
1028         int r = 0;
1029         if (event_pending(&bev->ev_read, EV_READ, NULL)) {
1030                 if (evutil_timerisset(&bev->timeout_read)) {
1031                             if (bufferevent_add_event_(&bev->ev_read, &bev->timeout_read) < 0)
1032                                     r = -1;
1033                 } else {
1034                         event_remove_timer(&bev->ev_read);
1035                 }
1036         }
1037         if (event_pending(&bev->ev_write, EV_WRITE, NULL)) {
1038                 if (evutil_timerisset(&bev->timeout_write)) {
1039                         if (bufferevent_add_event_(&bev->ev_write, &bev->timeout_write) < 0)
1040                                 r = -1;
1041                 } else {
1042                         event_remove_timer(&bev->ev_write);
1043                 }
1044         }
1045         return r;
1046 }
1047
1048 int
1049 bufferevent_add_event_(struct event *ev, const struct timeval *tv)
1050 {
1051         if (!evutil_timerisset(tv))
1052                 return event_add(ev, NULL);
1053         else
1054                 return event_add(ev, tv);
1055 }
1056
1057 /* For use by user programs only; internally, we should be calling
1058    either bufferevent_incref_and_lock_(), or BEV_LOCK. */
1059 void
1060 bufferevent_lock(struct bufferevent *bev)
1061 {
1062         bufferevent_incref_and_lock_(bev);
1063 }
1064
1065 void
1066 bufferevent_unlock(struct bufferevent *bev)
1067 {
1068         bufferevent_decref_and_unlock_(bev);
1069 }