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