xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/bufferevent_pair.c (revision 413d532bcc3f62d122e56d92e13ac64825a40baf)
1 /*	$NetBSD: bufferevent_pair.c,v 1.1.1.1 2013/12/27 23:31:17 christos Exp $	*/
2 
3 /*
4  * Copyright (c) 2009-2012 Niels Provos, Nick Mathewson
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 #include "event2/event-config.h"
29 #include "evconfig-private.h"
30 
31 #include <sys/types.h>
32 
33 #ifdef _WIN32
34 #include <winsock2.h>
35 #endif
36 
37 #include "event2/util.h"
38 #include "event2/buffer.h"
39 #include "event2/bufferevent.h"
40 #include "event2/bufferevent_struct.h"
41 #include "event2/event.h"
42 #include "defer-internal.h"
43 #include "bufferevent-internal.h"
44 #include "mm-internal.h"
45 #include "util-internal.h"
46 
47 struct bufferevent_pair {
48 	struct bufferevent_private bev;
49 	struct bufferevent_pair *partner;
50 };
51 
52 
53 /* Given a bufferevent that's really a bev part of a bufferevent_pair,
54  * return that bufferevent_filtered. Returns NULL otherwise.*/
55 static inline struct bufferevent_pair *
56 upcast(struct bufferevent *bev)
57 {
58 	struct bufferevent_pair *bev_p;
59 	if (bev->be_ops != &bufferevent_ops_pair)
60 		return NULL;
61 	bev_p = EVUTIL_UPCAST(bev, struct bufferevent_pair, bev.bev);
62 	EVUTIL_ASSERT(bev_p->bev.bev.be_ops == &bufferevent_ops_pair);
63 	return bev_p;
64 }
65 
66 #define downcast(bev_pair) (&(bev_pair)->bev.bev)
67 
68 static inline void
69 incref_and_lock(struct bufferevent *b)
70 {
71 	struct bufferevent_pair *bevp;
72 	bufferevent_incref_and_lock_(b);
73 	bevp = upcast(b);
74 	if (bevp->partner)
75 		bufferevent_incref_and_lock_(downcast(bevp->partner));
76 }
77 
78 static inline void
79 decref_and_unlock(struct bufferevent *b)
80 {
81 	struct bufferevent_pair *bevp = upcast(b);
82 	if (bevp->partner)
83 		bufferevent_decref_and_unlock_(downcast(bevp->partner));
84 	bufferevent_decref_and_unlock_(b);
85 }
86 
87 /* XXX Handle close */
88 
89 static void be_pair_outbuf_cb(struct evbuffer *,
90     const struct evbuffer_cb_info *, void *);
91 
92 static struct bufferevent_pair *
93 bufferevent_pair_elt_new(struct event_base *base,
94     int options)
95 {
96 	struct bufferevent_pair *bufev;
97 	if (! (bufev = mm_calloc(1, sizeof(struct bufferevent_pair))))
98 		return NULL;
99 	if (bufferevent_init_common_(&bufev->bev, base, &bufferevent_ops_pair,
100 		options)) {
101 		mm_free(bufev);
102 		return NULL;
103 	}
104 	if (!evbuffer_add_cb(bufev->bev.bev.output, be_pair_outbuf_cb, bufev)) {
105 		bufferevent_free(downcast(bufev));
106 		return NULL;
107 	}
108 
109 	bufferevent_init_generic_timeout_cbs_(&bufev->bev.bev);
110 
111 	return bufev;
112 }
113 
114 int
115 bufferevent_pair_new(struct event_base *base, int options,
116     struct bufferevent *pair[2])
117 {
118 	struct bufferevent_pair *bufev1 = NULL, *bufev2 = NULL;
119 	int tmp_options;
120 
121 	options |= BEV_OPT_DEFER_CALLBACKS;
122 	tmp_options = options & ~BEV_OPT_THREADSAFE;
123 
124 	bufev1 = bufferevent_pair_elt_new(base, options);
125 	if (!bufev1)
126 		return -1;
127 	bufev2 = bufferevent_pair_elt_new(base, tmp_options);
128 	if (!bufev2) {
129 		bufferevent_free(downcast(bufev1));
130 		return -1;
131 	}
132 
133 	if (options & BEV_OPT_THREADSAFE) {
134 		/*XXXX check return */
135 		bufferevent_enable_locking_(downcast(bufev2), bufev1->bev.lock);
136 	}
137 
138 	bufev1->partner = bufev2;
139 	bufev2->partner = bufev1;
140 
141 	evbuffer_freeze(downcast(bufev1)->input, 0);
142 	evbuffer_freeze(downcast(bufev1)->output, 1);
143 	evbuffer_freeze(downcast(bufev2)->input, 0);
144 	evbuffer_freeze(downcast(bufev2)->output, 1);
145 
146 	pair[0] = downcast(bufev1);
147 	pair[1] = downcast(bufev2);
148 
149 	return 0;
150 }
151 
152 static void
153 be_pair_transfer(struct bufferevent *src, struct bufferevent *dst,
154     int ignore_wm)
155 {
156 	size_t src_size, dst_size;
157 	size_t n;
158 
159 	evbuffer_unfreeze(src->output, 1);
160 	evbuffer_unfreeze(dst->input, 0);
161 
162 	if (dst->wm_read.high) {
163 		dst_size = evbuffer_get_length(dst->input);
164 		if (dst_size < dst->wm_read.high) {
165 			n = dst->wm_read.high - dst_size;
166 			evbuffer_remove_buffer(src->output, dst->input, n);
167 		} else {
168 			if (!ignore_wm)
169 				goto done;
170 			n = evbuffer_get_length(src->output);
171 			evbuffer_add_buffer(dst->input, src->output);
172 		}
173 	} else {
174 		n = evbuffer_get_length(src->output);
175 		evbuffer_add_buffer(dst->input, src->output);
176 	}
177 
178 	if (n) {
179 		BEV_RESET_GENERIC_READ_TIMEOUT(dst);
180 
181 		if (evbuffer_get_length(dst->output))
182 			BEV_RESET_GENERIC_WRITE_TIMEOUT(dst);
183 		else
184 			BEV_DEL_GENERIC_WRITE_TIMEOUT(dst);
185 	}
186 
187 	src_size = evbuffer_get_length(src->output);
188 	dst_size = evbuffer_get_length(dst->input);
189 
190 	if (dst_size >= dst->wm_read.low) {
191 		bufferevent_run_readcb_(dst);
192 	}
193 	if (src_size <= src->wm_write.low) {
194 		bufferevent_run_writecb_(src);
195 	}
196 done:
197 	evbuffer_freeze(src->output, 1);
198 	evbuffer_freeze(dst->input, 0);
199 }
200 
201 static inline int
202 be_pair_wants_to_talk(struct bufferevent_pair *src,
203     struct bufferevent_pair *dst)
204 {
205 	return (downcast(src)->enabled & EV_WRITE) &&
206 	    (downcast(dst)->enabled & EV_READ) &&
207 	    !dst->bev.read_suspended &&
208 	    evbuffer_get_length(downcast(src)->output);
209 }
210 
211 static void
212 be_pair_outbuf_cb(struct evbuffer *outbuf,
213     const struct evbuffer_cb_info *info, void *arg)
214 {
215 	struct bufferevent_pair *bev_pair = arg;
216 	struct bufferevent_pair *partner = bev_pair->partner;
217 
218 	incref_and_lock(downcast(bev_pair));
219 
220 	if (info->n_added > info->n_deleted && partner) {
221 		/* We got more data.  If the other side's reading, then
222 		   hand it over. */
223 		if (be_pair_wants_to_talk(bev_pair, partner)) {
224 			be_pair_transfer(downcast(bev_pair), downcast(partner), 0);
225 		}
226 	}
227 
228 	decref_and_unlock(downcast(bev_pair));
229 }
230 
231 static int
232 be_pair_enable(struct bufferevent *bufev, short events)
233 {
234 	struct bufferevent_pair *bev_p = upcast(bufev);
235 	struct bufferevent_pair *partner = bev_p->partner;
236 
237 	incref_and_lock(bufev);
238 
239 	if (events & EV_READ) {
240 		BEV_RESET_GENERIC_READ_TIMEOUT(bufev);
241 	}
242 	if ((events & EV_WRITE) && evbuffer_get_length(bufev->output))
243 		BEV_RESET_GENERIC_WRITE_TIMEOUT(bufev);
244 
245 	/* We're starting to read! Does the other side have anything to write?*/
246 	if ((events & EV_READ) && partner &&
247 	    be_pair_wants_to_talk(partner, bev_p)) {
248 		be_pair_transfer(downcast(partner), bufev, 0);
249 	}
250 	/* We're starting to write! Does the other side want to read? */
251 	if ((events & EV_WRITE) && partner &&
252 	    be_pair_wants_to_talk(bev_p, partner)) {
253 		be_pair_transfer(bufev, downcast(partner), 0);
254 	}
255 	decref_and_unlock(bufev);
256 	return 0;
257 }
258 
259 static int
260 be_pair_disable(struct bufferevent *bev, short events)
261 {
262 	if (events & EV_READ) {
263 		BEV_DEL_GENERIC_READ_TIMEOUT(bev);
264 	}
265 	if (events & EV_WRITE) {
266 		BEV_DEL_GENERIC_WRITE_TIMEOUT(bev);
267 	}
268 	return 0;
269 }
270 
271 static void
272 be_pair_destruct(struct bufferevent *bev)
273 {
274 	struct bufferevent_pair *bev_p = upcast(bev);
275 
276 	if (bev_p->partner) {
277 		bev_p->partner->partner = NULL;
278 		bev_p->partner = NULL;
279 	}
280 
281 	bufferevent_del_generic_timeout_cbs_(bev);
282 }
283 
284 static int
285 be_pair_flush(struct bufferevent *bev, short iotype,
286     enum bufferevent_flush_mode mode)
287 {
288 	struct bufferevent_pair *bev_p = upcast(bev);
289 	struct bufferevent *partner;
290 	incref_and_lock(bev);
291 	if (!bev_p->partner)
292 		return -1;
293 
294 	partner = downcast(bev_p->partner);
295 
296 	if (mode == BEV_NORMAL)
297 		return 0;
298 
299 	if ((iotype & EV_READ) != 0)
300 		be_pair_transfer(partner, bev, 1);
301 
302 	if ((iotype & EV_WRITE) != 0)
303 		be_pair_transfer(bev, partner, 1);
304 
305 	if (mode == BEV_FINISHED) {
306 		bufferevent_run_eventcb_(partner, iotype|BEV_EVENT_EOF);
307 	}
308 	decref_and_unlock(bev);
309 	return 0;
310 }
311 
312 struct bufferevent *
313 bufferevent_pair_get_partner(struct bufferevent *bev)
314 {
315 	struct bufferevent_pair *bev_p;
316 	struct bufferevent *partner;
317 	bev_p = upcast(bev);
318 	if (! bev_p)
319 		return NULL;
320 
321 	incref_and_lock(bev);
322 	partner = downcast(bev_p->partner);
323 	decref_and_unlock(bev);
324 	return partner;
325 }
326 
327 const struct bufferevent_ops bufferevent_ops_pair = {
328 	"pair_elt",
329 	evutil_offsetof(struct bufferevent_pair, bev.bev),
330 	be_pair_enable,
331 	be_pair_disable,
332 	be_pair_destruct,
333 	bufferevent_generic_adj_timeouts_,
334 	be_pair_flush,
335 	NULL, /* ctrl */
336 };
337