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