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