1 /* $NetBSD: bufferevent_sock.c,v 1.1.1.4 2021/04/07 02:43:13 christos Exp $ */
2 /*
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "event2/event-config.h"
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: bufferevent_sock.c,v 1.1.1.4 2021/04/07 02:43:13 christos Exp $");
33 #include "evconfig-private.h"
34
35 #include <sys/types.h>
36
37 #ifdef EVENT__HAVE_SYS_TIME_H
38 #include <sys/time.h>
39 #endif
40
41 #include <errno.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #ifdef EVENT__HAVE_STDARG_H
46 #include <stdarg.h>
47 #endif
48 #ifdef EVENT__HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51
52 #ifdef _WIN32
53 #include <winsock2.h>
54 #include <ws2tcpip.h>
55 #endif
56
57 #ifdef EVENT__HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef EVENT__HAVE_NETINET_IN_H
61 #include <netinet/in.h>
62 #endif
63 #ifdef EVENT__HAVE_NETINET_IN6_H
64 #include <netinet/in6.h>
65 #endif
66
67 #include "event2/util.h"
68 #include "event2/bufferevent.h"
69 #include "event2/buffer.h"
70 #include "event2/bufferevent_struct.h"
71 #include "event2/bufferevent_compat.h"
72 #include "event2/event.h"
73 #include "log-internal.h"
74 #include "mm-internal.h"
75 #include "bufferevent-internal.h"
76 #include "util-internal.h"
77 #ifdef _WIN32
78 #include "iocp-internal.h"
79 #endif
80
81 /* prototypes */
82 static int be_socket_enable(struct bufferevent *, short);
83 static int be_socket_disable(struct bufferevent *, short);
84 static void be_socket_destruct(struct bufferevent *);
85 static int be_socket_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
86 static int be_socket_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
87
88 static void be_socket_setfd(struct bufferevent *, evutil_socket_t);
89
90 const struct bufferevent_ops bufferevent_ops_socket = {
91 "socket",
92 evutil_offsetof(struct bufferevent_private, bev),
93 be_socket_enable,
94 be_socket_disable,
95 NULL, /* unlink */
96 be_socket_destruct,
97 bufferevent_generic_adj_existing_timeouts_,
98 be_socket_flush,
99 be_socket_ctrl,
100 };
101
102 const struct sockaddr*
bufferevent_socket_get_conn_address_(struct bufferevent * bev)103 bufferevent_socket_get_conn_address_(struct bufferevent *bev)
104 {
105 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
106 return (struct sockaddr *)&bev_p->conn_address;
107 }
108
109 void
bufferevent_socket_set_conn_address_fd_(struct bufferevent * bev,evutil_socket_t fd)110 bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev,
111 evutil_socket_t fd)
112 {
113 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
114
115 socklen_t len = sizeof(bev_p->conn_address);
116
117 struct sockaddr *addr = (struct sockaddr *)&bev_p->conn_address;
118 if (addr->sa_family != AF_UNSPEC)
119 getpeername(fd, addr, &len);
120 }
121
122 void
bufferevent_socket_set_conn_address_(struct bufferevent * bev,struct sockaddr * addr,size_t addrlen)123 bufferevent_socket_set_conn_address_(struct bufferevent *bev,
124 struct sockaddr *addr, size_t addrlen)
125 {
126 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
127 EVUTIL_ASSERT(addrlen <= sizeof(bev_p->conn_address));
128 memcpy(&bev_p->conn_address, addr, addrlen);
129 }
130
131 static void
bufferevent_socket_outbuf_cb(struct evbuffer * buf,const struct evbuffer_cb_info * cbinfo,void * arg)132 bufferevent_socket_outbuf_cb(struct evbuffer *buf,
133 const struct evbuffer_cb_info *cbinfo,
134 void *arg)
135 {
136 struct bufferevent *bufev = arg;
137 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
138
139 if (cbinfo->n_added &&
140 (bufev->enabled & EV_WRITE) &&
141 !event_pending(&bufev->ev_write, EV_WRITE, NULL) &&
142 !bufev_p->write_suspended) {
143 /* Somebody added data to the buffer, and we would like to
144 * write, and we were not writing. So, start writing. */
145 if (bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1) {
146 /* Should we log this? */
147 }
148 }
149 }
150
151 static void
bufferevent_readcb(evutil_socket_t fd,short event,void * arg)152 bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
153 {
154 struct bufferevent *bufev = arg;
155 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
156 struct evbuffer *input;
157 int res = 0;
158 short what = BEV_EVENT_READING;
159 ev_ssize_t howmuch = -1, readmax=-1;
160
161 bufferevent_incref_and_lock_(bufev);
162
163 if (event == EV_TIMEOUT) {
164 /* Note that we only check for event==EV_TIMEOUT. If
165 * event==EV_TIMEOUT|EV_READ, we can safely ignore the
166 * timeout, since a read has occurred */
167 what |= BEV_EVENT_TIMEOUT;
168 goto error;
169 }
170
171 input = bufev->input;
172
173 /*
174 * If we have a high watermark configured then we don't want to
175 * read more data than would make us reach the watermark.
176 */
177 if (bufev->wm_read.high != 0) {
178 howmuch = bufev->wm_read.high - evbuffer_get_length(input);
179 /* we somehow lowered the watermark, stop reading */
180 if (howmuch <= 0) {
181 bufferevent_wm_suspend_read(bufev);
182 goto done;
183 }
184 }
185 readmax = bufferevent_get_read_max_(bufev_p);
186 if (howmuch < 0 || howmuch > readmax) /* The use of -1 for "unlimited"
187 * uglifies this code. XXXX */
188 howmuch = readmax;
189 if (bufev_p->read_suspended)
190 goto done;
191
192 evbuffer_unfreeze(input, 0);
193 res = evbuffer_read(input, fd, (int)howmuch); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
194 evbuffer_freeze(input, 0);
195
196 if (res == -1) {
197 int err = evutil_socket_geterror(fd);
198 if (EVUTIL_ERR_RW_RETRIABLE(err))
199 goto reschedule;
200 if (EVUTIL_ERR_CONNECT_REFUSED(err)) {
201 bufev_p->connection_refused = 1;
202 goto done;
203 }
204 /* error case */
205 what |= BEV_EVENT_ERROR;
206 } else if (res == 0) {
207 /* eof case */
208 what |= BEV_EVENT_EOF;
209 }
210
211 if (res <= 0)
212 goto error;
213
214 bufferevent_decrement_read_buckets_(bufev_p, res);
215
216 /* Invoke the user callback - must always be called last */
217 bufferevent_trigger_nolock_(bufev, EV_READ, 0);
218
219 goto done;
220
221 reschedule:
222 goto done;
223
224 error:
225 bufferevent_disable(bufev, EV_READ);
226 bufferevent_run_eventcb_(bufev, what, 0);
227
228 done:
229 bufferevent_decref_and_unlock_(bufev);
230 }
231
232 static void
bufferevent_writecb(evutil_socket_t fd,short event,void * arg)233 bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
234 {
235 struct bufferevent *bufev = arg;
236 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
237 int res = 0;
238 short what = BEV_EVENT_WRITING;
239 int connected = 0;
240 ev_ssize_t atmost = -1;
241
242 bufferevent_incref_and_lock_(bufev);
243
244 if (event == EV_TIMEOUT) {
245 /* Note that we only check for event==EV_TIMEOUT. If
246 * event==EV_TIMEOUT|EV_WRITE, we can safely ignore the
247 * timeout, since a read has occurred */
248 what |= BEV_EVENT_TIMEOUT;
249 goto error;
250 }
251 if (bufev_p->connecting) {
252 int c = evutil_socket_finished_connecting_(fd);
253 /* we need to fake the error if the connection was refused
254 * immediately - usually connection to localhost on BSD */
255 if (bufev_p->connection_refused) {
256 bufev_p->connection_refused = 0;
257 c = -1;
258 }
259
260 if (c == 0)
261 goto done;
262
263 bufev_p->connecting = 0;
264 if (c < 0) {
265 event_del(&bufev->ev_write);
266 event_del(&bufev->ev_read);
267 bufferevent_run_eventcb_(bufev, BEV_EVENT_ERROR, 0);
268 goto done;
269 } else {
270 connected = 1;
271 bufferevent_socket_set_conn_address_fd_(bufev, fd);
272 #ifdef _WIN32
273 if (BEV_IS_ASYNC(bufev)) {
274 event_del(&bufev->ev_write);
275 bufferevent_async_set_connected_(bufev);
276 bufferevent_run_eventcb_(bufev,
277 BEV_EVENT_CONNECTED, 0);
278 goto done;
279 }
280 #endif
281 bufferevent_run_eventcb_(bufev,
282 BEV_EVENT_CONNECTED, 0);
283 if (!(bufev->enabled & EV_WRITE) ||
284 bufev_p->write_suspended) {
285 event_del(&bufev->ev_write);
286 goto done;
287 }
288 }
289 }
290
291 atmost = bufferevent_get_write_max_(bufev_p);
292
293 if (bufev_p->write_suspended)
294 goto done;
295
296 if (evbuffer_get_length(bufev->output)) {
297 evbuffer_unfreeze(bufev->output, 1);
298 res = evbuffer_write_atmost(bufev->output, fd, atmost);
299 evbuffer_freeze(bufev->output, 1);
300 if (res == -1) {
301 int err = evutil_socket_geterror(fd);
302 if (EVUTIL_ERR_RW_RETRIABLE(err))
303 goto reschedule;
304 what |= BEV_EVENT_ERROR;
305 } else if (res == 0) {
306 /* eof case
307 XXXX Actually, a 0 on write doesn't indicate
308 an EOF. An ECONNRESET might be more typical.
309 */
310 what |= BEV_EVENT_EOF;
311 }
312 if (res <= 0)
313 goto error;
314
315 bufferevent_decrement_write_buckets_(bufev_p, res);
316 }
317
318 if (evbuffer_get_length(bufev->output) == 0) {
319 event_del(&bufev->ev_write);
320 }
321
322 /*
323 * Invoke the user callback if our buffer is drained or below the
324 * low watermark.
325 */
326 if (res || !connected) {
327 bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
328 }
329
330 goto done;
331
332 reschedule:
333 if (evbuffer_get_length(bufev->output) == 0) {
334 event_del(&bufev->ev_write);
335 }
336 goto done;
337
338 error:
339 bufferevent_disable(bufev, EV_WRITE);
340 bufferevent_run_eventcb_(bufev, what, 0);
341
342 done:
343 bufferevent_decref_and_unlock_(bufev);
344 }
345
346 struct bufferevent *
bufferevent_socket_new(struct event_base * base,evutil_socket_t fd,int options)347 bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
348 int options)
349 {
350 struct bufferevent_private *bufev_p;
351 struct bufferevent *bufev;
352
353 #ifdef _WIN32
354 if (base && event_base_get_iocp_(base))
355 return bufferevent_async_new_(base, fd, options);
356 #endif
357
358 if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL)
359 return NULL;
360
361 if (bufferevent_init_common_(bufev_p, base, &bufferevent_ops_socket,
362 options) < 0) {
363 mm_free(bufev_p);
364 return NULL;
365 }
366 bufev = &bufev_p->bev;
367 evbuffer_set_flags(bufev->output, EVBUFFER_FLAG_DRAINS_TO_FD);
368
369 event_assign(&bufev->ev_read, bufev->ev_base, fd,
370 EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
371 event_assign(&bufev->ev_write, bufev->ev_base, fd,
372 EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
373
374 evbuffer_add_cb(bufev->output, bufferevent_socket_outbuf_cb, bufev);
375
376 evbuffer_freeze(bufev->input, 0);
377 evbuffer_freeze(bufev->output, 1);
378
379 return bufev;
380 }
381
382 int
bufferevent_socket_connect(struct bufferevent * bev,const struct sockaddr * sa,int socklen)383 bufferevent_socket_connect(struct bufferevent *bev,
384 const struct sockaddr *sa, int socklen)
385 {
386 struct bufferevent_private *bufev_p = BEV_UPCAST(bev);
387
388 evutil_socket_t fd;
389 int r = 0;
390 int result=-1;
391 int ownfd = 0;
392
393 bufferevent_incref_and_lock_(bev);
394
395 fd = bufferevent_getfd(bev);
396 if (fd < 0) {
397 if (!sa)
398 goto done;
399 fd = evutil_socket_(sa->sa_family,
400 SOCK_STREAM|EVUTIL_SOCK_NONBLOCK, 0);
401 if (fd < 0)
402 goto freesock;
403 ownfd = 1;
404 }
405 if (sa) {
406 #ifdef _WIN32
407 if (bufferevent_async_can_connect_(bev)) {
408 bufferevent_setfd(bev, fd);
409 r = bufferevent_async_connect_(bev, fd, sa, socklen);
410 if (r < 0)
411 goto freesock;
412 bufev_p->connecting = 1;
413 result = 0;
414 goto done;
415 } else
416 #endif
417 r = evutil_socket_connect_(&fd, sa, socklen);
418 if (r < 0)
419 goto freesock;
420 }
421 #ifdef _WIN32
422 /* ConnectEx() isn't always around, even when IOCP is enabled.
423 * Here, we borrow the socket object's write handler to fall back
424 * on a non-blocking connect() when ConnectEx() is unavailable. */
425 if (BEV_IS_ASYNC(bev)) {
426 event_assign(&bev->ev_write, bev->ev_base, fd,
427 EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bev);
428 }
429 #endif
430 bufferevent_setfd(bev, fd);
431 if (r == 0) {
432 if (! be_socket_enable(bev, EV_WRITE)) {
433 bufev_p->connecting = 1;
434 result = 0;
435 goto done;
436 }
437 } else if (r == 1) {
438 /* The connect succeeded already. How very BSD of it. */
439 result = 0;
440 bufev_p->connecting = 1;
441 bufferevent_trigger_nolock_(bev, EV_WRITE, BEV_OPT_DEFER_CALLBACKS);
442 } else {
443 /* The connect failed already. How very BSD of it. */
444 result = 0;
445 bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, BEV_OPT_DEFER_CALLBACKS);
446 bufferevent_disable(bev, EV_WRITE|EV_READ);
447 }
448
449 goto done;
450
451 freesock:
452 if (ownfd)
453 evutil_closesocket(fd);
454 done:
455 bufferevent_decref_and_unlock_(bev);
456 return result;
457 }
458
459 static void
bufferevent_connect_getaddrinfo_cb(int result,struct evutil_addrinfo * ai,void * arg)460 bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
461 void *arg)
462 {
463 struct bufferevent *bev = arg;
464 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
465 int r;
466 BEV_LOCK(bev);
467
468 bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
469 bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
470
471 bev_p->dns_request = NULL;
472
473 if (result == EVUTIL_EAI_CANCEL) {
474 bev_p->dns_error = result;
475 bufferevent_decref_and_unlock_(bev);
476 return;
477 }
478 if (result != 0) {
479 bev_p->dns_error = result;
480 bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
481 bufferevent_decref_and_unlock_(bev);
482 if (ai)
483 evutil_freeaddrinfo(ai);
484 return;
485 }
486
487 /* XXX use the other addrinfos? */
488 bufferevent_socket_set_conn_address_(bev, ai->ai_addr, (int)ai->ai_addrlen);
489 r = bufferevent_socket_connect(bev, ai->ai_addr, (int)ai->ai_addrlen);
490 if (r < 0)
491 bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
492 bufferevent_decref_and_unlock_(bev);
493 evutil_freeaddrinfo(ai);
494 }
495
496 int
bufferevent_socket_connect_hostname(struct bufferevent * bev,struct evdns_base * evdns_base,int family,const char * hostname,int port)497 bufferevent_socket_connect_hostname(struct bufferevent *bev,
498 struct evdns_base *evdns_base, int family, const char *hostname, int port)
499 {
500 char portbuf[10];
501 struct evutil_addrinfo hint;
502 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
503
504 if (family != AF_INET && family != AF_INET6 && family != AF_UNSPEC)
505 return -1;
506 if (port < 1 || port > 65535)
507 return -1;
508
509 memset(&hint, 0, sizeof(hint));
510 hint.ai_family = family;
511 hint.ai_protocol = IPPROTO_TCP;
512 hint.ai_socktype = SOCK_STREAM;
513
514 evutil_snprintf(portbuf, sizeof(portbuf), "%d", port);
515
516 BEV_LOCK(bev);
517 bev_p->dns_error = 0;
518
519 bufferevent_suspend_write_(bev, BEV_SUSPEND_LOOKUP);
520 bufferevent_suspend_read_(bev, BEV_SUSPEND_LOOKUP);
521
522 bufferevent_incref_(bev);
523 bev_p->dns_request = evutil_getaddrinfo_async_(evdns_base, hostname,
524 portbuf, &hint, bufferevent_connect_getaddrinfo_cb, bev);
525 BEV_UNLOCK(bev);
526
527 return 0;
528 }
529
530 int
bufferevent_socket_get_dns_error(struct bufferevent * bev)531 bufferevent_socket_get_dns_error(struct bufferevent *bev)
532 {
533 int rv;
534 struct bufferevent_private *bev_p = BEV_UPCAST(bev);
535
536 BEV_LOCK(bev);
537 rv = bev_p->dns_error;
538 BEV_UNLOCK(bev);
539
540 return rv;
541 }
542
543 /*
544 * Create a new buffered event object.
545 *
546 * The read callback is invoked whenever we read new data.
547 * The write callback is invoked whenever the output buffer is drained.
548 * The error callback is invoked on a write/read error or on EOF.
549 *
550 * Both read and write callbacks maybe NULL. The error callback is not
551 * allowed to be NULL and have to be provided always.
552 */
553
554 struct bufferevent *
bufferevent_new(evutil_socket_t fd,bufferevent_data_cb readcb,bufferevent_data_cb writecb,bufferevent_event_cb eventcb,void * cbarg)555 bufferevent_new(evutil_socket_t fd,
556 bufferevent_data_cb readcb, bufferevent_data_cb writecb,
557 bufferevent_event_cb eventcb, void *cbarg)
558 {
559 struct bufferevent *bufev;
560
561 if (!(bufev = bufferevent_socket_new(NULL, fd, 0)))
562 return NULL;
563
564 bufferevent_setcb(bufev, readcb, writecb, eventcb, cbarg);
565
566 return bufev;
567 }
568
569
570 static int
be_socket_enable(struct bufferevent * bufev,short event)571 be_socket_enable(struct bufferevent *bufev, short event)
572 {
573 if (event & EV_READ &&
574 bufferevent_add_event_(&bufev->ev_read, &bufev->timeout_read) == -1)
575 return -1;
576 if (event & EV_WRITE &&
577 bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1)
578 return -1;
579 return 0;
580 }
581
582 static int
be_socket_disable(struct bufferevent * bufev,short event)583 be_socket_disable(struct bufferevent *bufev, short event)
584 {
585 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
586 if (event & EV_READ) {
587 if (event_del(&bufev->ev_read) == -1)
588 return -1;
589 }
590 /* Don't actually disable the write if we are trying to connect. */
591 if ((event & EV_WRITE) && ! bufev_p->connecting) {
592 if (event_del(&bufev->ev_write) == -1)
593 return -1;
594 }
595 return 0;
596 }
597
598 static void
be_socket_destruct(struct bufferevent * bufev)599 be_socket_destruct(struct bufferevent *bufev)
600 {
601 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
602 evutil_socket_t fd;
603 EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
604
605 fd = event_get_fd(&bufev->ev_read);
606
607 if ((bufev_p->options & BEV_OPT_CLOSE_ON_FREE) && fd >= 0)
608 EVUTIL_CLOSESOCKET(fd);
609
610 evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
611 }
612
613 static int
be_socket_flush(struct bufferevent * bev,short iotype,enum bufferevent_flush_mode mode)614 be_socket_flush(struct bufferevent *bev, short iotype,
615 enum bufferevent_flush_mode mode)
616 {
617 return 0;
618 }
619
620
621 static void
be_socket_setfd(struct bufferevent * bufev,evutil_socket_t fd)622 be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
623 {
624 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
625
626 BEV_LOCK(bufev);
627 EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
628
629 event_del(&bufev->ev_read);
630 event_del(&bufev->ev_write);
631
632 evbuffer_unfreeze(bufev->input, 0);
633 evbuffer_unfreeze(bufev->output, 1);
634
635 event_assign(&bufev->ev_read, bufev->ev_base, fd,
636 EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
637 event_assign(&bufev->ev_write, bufev->ev_base, fd,
638 EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
639
640 if (fd >= 0)
641 bufferevent_enable(bufev, bufev->enabled);
642
643 evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
644
645 BEV_UNLOCK(bufev);
646 }
647
648 /* XXXX Should non-socket bufferevents support this? */
649 int
bufferevent_priority_set(struct bufferevent * bufev,int priority)650 bufferevent_priority_set(struct bufferevent *bufev, int priority)
651 {
652 int r = -1;
653 struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
654
655 BEV_LOCK(bufev);
656 if (BEV_IS_ASYNC(bufev) || BEV_IS_FILTER(bufev) || BEV_IS_PAIR(bufev))
657 goto done;
658
659 if (event_priority_set(&bufev->ev_read, priority) == -1)
660 goto done;
661 if (event_priority_set(&bufev->ev_write, priority) == -1)
662 goto done;
663
664 event_deferred_cb_set_priority_(&bufev_p->deferred, priority);
665
666 r = 0;
667 done:
668 BEV_UNLOCK(bufev);
669 return r;
670 }
671
672 /* XXXX Should non-socket bufferevents support this? */
673 int
bufferevent_base_set(struct event_base * base,struct bufferevent * bufev)674 bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
675 {
676 int res = -1;
677
678 BEV_LOCK(bufev);
679 if (!BEV_IS_SOCKET(bufev))
680 goto done;
681
682 bufev->ev_base = base;
683
684 res = event_base_set(base, &bufev->ev_read);
685 if (res == -1)
686 goto done;
687
688 res = event_base_set(base, &bufev->ev_write);
689 done:
690 BEV_UNLOCK(bufev);
691 return res;
692 }
693
694 static int
be_socket_ctrl(struct bufferevent * bev,enum bufferevent_ctrl_op op,union bufferevent_ctrl_data * data)695 be_socket_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
696 union bufferevent_ctrl_data *data)
697 {
698 switch (op) {
699 case BEV_CTRL_SET_FD:
700 be_socket_setfd(bev, data->fd);
701 return 0;
702 case BEV_CTRL_GET_FD:
703 data->fd = event_get_fd(&bev->ev_read);
704 return 0;
705 case BEV_CTRL_GET_UNDERLYING:
706 case BEV_CTRL_CANCEL_ALL:
707 default:
708 return -1;
709 }
710 }
711