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