1 /* $NetBSD: bufferevent_sock.c,v 1.1.1.1 2013/04/11 16:43:24 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 <sys/types.h> 31 32 #include "event2/event-config.h" 33 #include <sys/cdefs.h> 34 __RCSID("$NetBSD: bufferevent_sock.c,v 1.1.1.1 2013/04/11 16:43:24 christos Exp $"); 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 = socket(sa->sa_family, SOCK_STREAM, 0); 377 if (fd < 0) 378 goto done; 379 if (evutil_make_socket_nonblocking(fd)<0) 380 goto done; 381 ownfd = 1; 382 } 383 if (sa) { 384 #ifdef WIN32 385 if (bufferevent_async_can_connect(bev)) { 386 bufferevent_setfd(bev, fd); 387 r = bufferevent_async_connect(bev, fd, sa, socklen); 388 if (r < 0) 389 goto freesock; 390 bufev_p->connecting = 1; 391 result = 0; 392 goto done; 393 } else 394 #endif 395 r = evutil_socket_connect(&fd, sa, socklen); 396 if (r < 0) 397 goto freesock; 398 } 399 #ifdef WIN32 400 /* ConnectEx() isn't always around, even when IOCP is enabled. 401 * Here, we borrow the socket object's write handler to fall back 402 * on a non-blocking connect() when ConnectEx() is unavailable. */ 403 if (BEV_IS_ASYNC(bev)) { 404 event_assign(&bev->ev_write, bev->ev_base, fd, 405 EV_WRITE|EV_PERSIST, bufferevent_writecb, bev); 406 } 407 #endif 408 bufferevent_setfd(bev, fd); 409 if (r == 0) { 410 if (! be_socket_enable(bev, EV_WRITE)) { 411 bufev_p->connecting = 1; 412 result = 0; 413 goto done; 414 } 415 } else if (r == 1) { 416 /* The connect succeeded already. How very BSD of it. */ 417 result = 0; 418 bufev_p->connecting = 1; 419 event_active(&bev->ev_write, EV_WRITE, 1); 420 } else { 421 /* The connect failed already. How very BSD of it. */ 422 bufev_p->connection_refused = 1; 423 bufev_p->connecting = 1; 424 result = 0; 425 event_active(&bev->ev_write, EV_WRITE, 1); 426 } 427 428 goto done; 429 430 freesock: 431 _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR); 432 if (ownfd) 433 evutil_closesocket(fd); 434 /* do something about the error? */ 435 done: 436 _bufferevent_decref_and_unlock(bev); 437 return result; 438 } 439 440 static void 441 bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai, 442 void *arg) 443 { 444 struct bufferevent *bev = arg; 445 struct bufferevent_private *bev_p = 446 EVUTIL_UPCAST(bev, struct bufferevent_private, bev); 447 int r; 448 BEV_LOCK(bev); 449 450 bufferevent_unsuspend_write(bev, BEV_SUSPEND_LOOKUP); 451 bufferevent_unsuspend_read(bev, BEV_SUSPEND_LOOKUP); 452 453 if (result != 0) { 454 bev_p->dns_error = result; 455 _bufferevent_run_eventcb(bev, BEV_EVENT_ERROR); 456 _bufferevent_decref_and_unlock(bev); 457 if (ai) 458 evutil_freeaddrinfo(ai); 459 return; 460 } 461 462 /* XXX use the other addrinfos? */ 463 /* XXX use this return value */ 464 r = bufferevent_socket_connect(bev, ai->ai_addr, (int)ai->ai_addrlen); 465 (void)r; 466 _bufferevent_decref_and_unlock(bev); 467 evutil_freeaddrinfo(ai); 468 } 469 470 int 471 bufferevent_socket_connect_hostname(struct bufferevent *bev, 472 struct evdns_base *evdns_base, int family, const char *hostname, int port) 473 { 474 char portbuf[10]; 475 struct evutil_addrinfo hint; 476 int err; 477 struct bufferevent_private *bev_p = 478 EVUTIL_UPCAST(bev, struct bufferevent_private, bev); 479 480 if (family != AF_INET && family != AF_INET6 && family != AF_UNSPEC) 481 return -1; 482 if (port < 1 || port > 65535) 483 return -1; 484 485 BEV_LOCK(bev); 486 bev_p->dns_error = 0; 487 BEV_UNLOCK(bev); 488 489 evutil_snprintf(portbuf, sizeof(portbuf), "%d", port); 490 491 memset(&hint, 0, sizeof(hint)); 492 hint.ai_family = family; 493 hint.ai_protocol = IPPROTO_TCP; 494 hint.ai_socktype = SOCK_STREAM; 495 496 bufferevent_suspend_write(bev, BEV_SUSPEND_LOOKUP); 497 bufferevent_suspend_read(bev, BEV_SUSPEND_LOOKUP); 498 499 bufferevent_incref(bev); 500 err = evutil_getaddrinfo_async(evdns_base, hostname, portbuf, 501 &hint, bufferevent_connect_getaddrinfo_cb, bev); 502 503 if (err == 0) { 504 return 0; 505 } else { 506 bufferevent_unsuspend_write(bev, BEV_SUSPEND_LOOKUP); 507 bufferevent_unsuspend_read(bev, BEV_SUSPEND_LOOKUP); 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_LOCK(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 (be_socket_add(&bufev->ev_read, &bufev->timeout_read) < 0) 607 r = -1; 608 if (event_pending(&bufev->ev_write, EV_WRITE, NULL)) { 609 if (be_socket_add(&bufev->ev_write, &bufev->timeout_write) < 0) 610 r = -1; 611 } 612 return r; 613 } 614 615 static int 616 be_socket_flush(struct bufferevent *bev, short iotype, 617 enum bufferevent_flush_mode mode) 618 { 619 return 0; 620 } 621 622 623 static void 624 be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd) 625 { 626 BEV_LOCK(bufev); 627 EVUTIL_ASSERT(bufev->be_ops == &bufferevent_ops_socket); 628 629 event_del(&bufev->ev_read); 630 event_del(&bufev->ev_write); 631 632 event_assign(&bufev->ev_read, bufev->ev_base, fd, 633 EV_READ|EV_PERSIST, bufferevent_readcb, bufev); 634 event_assign(&bufev->ev_write, bufev->ev_base, fd, 635 EV_WRITE|EV_PERSIST, bufferevent_writecb, bufev); 636 637 if (fd >= 0) 638 bufferevent_enable(bufev, bufev->enabled); 639 640 BEV_UNLOCK(bufev); 641 } 642 643 /* XXXX Should non-socket bufferevents support this? */ 644 int 645 bufferevent_priority_set(struct bufferevent *bufev, int priority) 646 { 647 int r = -1; 648 649 BEV_LOCK(bufev); 650 if (bufev->be_ops != &bufferevent_ops_socket) 651 goto done; 652 653 if (event_priority_set(&bufev->ev_read, priority) == -1) 654 goto done; 655 if (event_priority_set(&bufev->ev_write, priority) == -1) 656 goto done; 657 658 r = 0; 659 done: 660 BEV_UNLOCK(bufev); 661 return r; 662 } 663 664 /* XXXX Should non-socket bufferevents support this? */ 665 int 666 bufferevent_base_set(struct event_base *base, struct bufferevent *bufev) 667 { 668 int res = -1; 669 670 BEV_LOCK(bufev); 671 if (bufev->be_ops != &bufferevent_ops_socket) 672 goto done; 673 674 bufev->ev_base = base; 675 676 res = event_base_set(base, &bufev->ev_read); 677 if (res == -1) 678 goto done; 679 680 res = event_base_set(base, &bufev->ev_write); 681 done: 682 BEV_UNLOCK(bufev); 683 return res; 684 } 685 686 static int 687 be_socket_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op, 688 union bufferevent_ctrl_data *data) 689 { 690 switch (op) { 691 case BEV_CTRL_SET_FD: 692 be_socket_setfd(bev, data->fd); 693 return 0; 694 case BEV_CTRL_GET_FD: 695 data->fd = event_get_fd(&bev->ev_read); 696 return 0; 697 case BEV_CTRL_GET_UNDERLYING: 698 case BEV_CTRL_CANCEL_ALL: 699 default: 700 return -1; 701 } 702 } 703