bufferevent_sock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
  3. * Copyright (c) 2002-2006 Niels Provos <provos@citi.umich.edu>
  4. * All rights reserved.
  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. #include <sys/types.h>
  31. #ifdef EVENT__HAVE_SYS_TIME_H
  32. #include <sys/time.h>
  33. #endif
  34. #include <errno.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #ifdef EVENT__HAVE_STDARG_H
  39. #include <stdarg.h>
  40. #endif
  41. #ifdef EVENT__HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44. #ifdef _WIN32
  45. #include <winsock2.h>
  46. #include <ws2tcpip.h>
  47. #endif
  48. #ifdef EVENT__HAVE_SYS_SOCKET_H
  49. #include <sys/socket.h>
  50. #endif
  51. #ifdef EVENT__HAVE_NETINET_IN_H
  52. #include <netinet/in.h>
  53. #endif
  54. #ifdef EVENT__HAVE_NETINET_IN6_H
  55. #error #include <netinet/in6.h>
  56. #endif
  57. #include "event2/util.h"
  58. #include "event2/bufferevent.h"
  59. #include "event2/buffer.h"
  60. #include "event2/bufferevent_struct.h"
  61. #include "event2/bufferevent_compat.h"
  62. #include "event2/event.h"
  63. #include "log-internal.h"
  64. #include "mm-internal.h"
  65. #include "bufferevent-internal.h"
  66. #include "util-internal.h"
  67. #ifdef _WIN32
  68. #include "iocp-internal.h"
  69. #endif
  70. /* prototypes */
  71. static int be_socket_enable(struct bufferevent *, short);
  72. static int be_socket_disable(struct bufferevent *, short);
  73. static void be_socket_destruct(struct bufferevent *);
  74. static int be_socket_flush(struct bufferevent *, short, enum bufferevent_flush_mode);
  75. static int be_socket_ctrl(struct bufferevent *, enum bufferevent_ctrl_op, union bufferevent_ctrl_data *);
  76. static void be_socket_setfd(struct bufferevent *, evutil_socket_t);
  77. const struct bufferevent_ops bufferevent_ops_socket = {
  78. "socket",
  79. evutil_offsetof(struct bufferevent_private, bev),
  80. be_socket_enable,
  81. be_socket_disable,
  82. NULL, /* unlink */
  83. be_socket_destruct,
  84. bufferevent_generic_adj_existing_timeouts_,
  85. be_socket_flush,
  86. be_socket_ctrl,
  87. };
  88. const struct sockaddr*
  89. bufferevent_socket_get_conn_address_(struct bufferevent *bev)
  90. {
  91. struct bufferevent_private *bev_p = BEV_UPCAST(bev);
  92. return (struct sockaddr *)&bev_p->conn_address;
  93. }
  94. void
  95. bufferevent_socket_set_conn_address_fd_(struct bufferevent *bev,
  96. evutil_socket_t fd)
  97. {
  98. struct bufferevent_private *bev_p = BEV_UPCAST(bev);
  99. socklen_t len = sizeof(bev_p->conn_address);
  100. struct sockaddr *addr = (struct sockaddr *)&bev_p->conn_address;
  101. if (addr->sa_family != AF_UNSPEC)
  102. getpeername(fd, addr, &len);
  103. }
  104. void
  105. bufferevent_socket_set_conn_address_(struct bufferevent *bev,
  106. struct sockaddr *addr, size_t addrlen)
  107. {
  108. struct bufferevent_private *bev_p = BEV_UPCAST(bev);
  109. EVUTIL_ASSERT(addrlen <= sizeof(bev_p->conn_address));
  110. memcpy(&bev_p->conn_address, addr, addrlen);
  111. }
  112. static void
  113. bufferevent_socket_outbuf_cb(struct evbuffer *buf,
  114. const struct evbuffer_cb_info *cbinfo,
  115. void *arg)
  116. {
  117. struct bufferevent *bufev = arg;
  118. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  119. if (cbinfo->n_added &&
  120. (bufev->enabled & EV_WRITE) &&
  121. !event_pending(&bufev->ev_write, EV_WRITE, NULL) &&
  122. !bufev_p->write_suspended) {
  123. /* Somebody added data to the buffer, and we would like to
  124. * write, and we were not writing. So, start writing. */
  125. if (bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1) {
  126. /* Should we log this? */
  127. }
  128. }
  129. }
  130. static void
  131. bufferevent_readcb(evutil_socket_t fd, short event, void *arg)
  132. {
  133. struct bufferevent *bufev = arg;
  134. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  135. struct evbuffer *input;
  136. int res = 0;
  137. short what = BEV_EVENT_READING;
  138. ev_ssize_t howmuch = -1, readmax=-1;
  139. bufferevent_incref_and_lock_(bufev);
  140. if (event == EV_TIMEOUT) {
  141. /* Note that we only check for event==EV_TIMEOUT. If
  142. * event==EV_TIMEOUT|EV_READ, we can safely ignore the
  143. * timeout, since a read has occurred */
  144. what |= BEV_EVENT_TIMEOUT;
  145. goto error;
  146. }
  147. input = bufev->input;
  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. evbuffer_unfreeze(input, 0);
  167. res = evbuffer_read(input, fd, (int)howmuch); /* XXXX evbuffer_read would do better to take and return ev_ssize_t */
  168. evbuffer_freeze(input, 0);
  169. if (res == -1) {
  170. int err = evutil_socket_geterror(fd);
  171. if (EVUTIL_ERR_RW_RETRIABLE(err))
  172. goto reschedule;
  173. if (EVUTIL_ERR_CONNECT_REFUSED(err)) {
  174. bufev_p->connection_refused = 1;
  175. goto done;
  176. }
  177. /* error case */
  178. what |= BEV_EVENT_ERROR;
  179. } else if (res == 0) {
  180. /* eof case */
  181. what |= BEV_EVENT_EOF;
  182. }
  183. if (res <= 0)
  184. goto error;
  185. bufferevent_decrement_read_buckets_(bufev_p, res);
  186. /* Invoke the user callback - must always be called last */
  187. bufferevent_trigger_nolock_(bufev, EV_READ, 0);
  188. goto done;
  189. reschedule:
  190. goto done;
  191. error:
  192. bufferevent_disable(bufev, EV_READ);
  193. bufferevent_run_eventcb_(bufev, what, 0);
  194. done:
  195. bufferevent_decref_and_unlock_(bufev);
  196. }
  197. static void
  198. bufferevent_writecb(evutil_socket_t fd, short event, void *arg)
  199. {
  200. struct bufferevent *bufev = arg;
  201. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  202. int res = 0;
  203. short what = BEV_EVENT_WRITING;
  204. int connected = 0;
  205. ev_ssize_t atmost = -1;
  206. bufferevent_incref_and_lock_(bufev);
  207. if (event == EV_TIMEOUT) {
  208. /* Note that we only check for event==EV_TIMEOUT. If
  209. * event==EV_TIMEOUT|EV_WRITE, we can safely ignore the
  210. * timeout, since a read has occurred */
  211. what |= BEV_EVENT_TIMEOUT;
  212. goto error;
  213. }
  214. if (bufev_p->connecting) {
  215. int c = evutil_socket_finished_connecting_(fd);
  216. /* we need to fake the error if the connection was refused
  217. * immediately - usually connection to localhost on BSD */
  218. if (bufev_p->connection_refused) {
  219. bufev_p->connection_refused = 0;
  220. c = -1;
  221. }
  222. if (c == 0)
  223. goto done;
  224. bufev_p->connecting = 0;
  225. if (c < 0) {
  226. event_del(&bufev->ev_write);
  227. event_del(&bufev->ev_read);
  228. bufferevent_run_eventcb_(bufev, BEV_EVENT_ERROR, 0);
  229. goto done;
  230. } else {
  231. connected = 1;
  232. bufferevent_socket_set_conn_address_fd_(bufev, fd);
  233. #ifdef _WIN32
  234. if (BEV_IS_ASYNC(bufev)) {
  235. event_del(&bufev->ev_write);
  236. bufferevent_async_set_connected_(bufev);
  237. bufferevent_run_eventcb_(bufev,
  238. BEV_EVENT_CONNECTED, 0);
  239. goto done;
  240. }
  241. #endif
  242. bufferevent_run_eventcb_(bufev,
  243. BEV_EVENT_CONNECTED, 0);
  244. if (!(bufev->enabled & EV_WRITE) ||
  245. bufev_p->write_suspended) {
  246. event_del(&bufev->ev_write);
  247. goto done;
  248. }
  249. }
  250. }
  251. atmost = bufferevent_get_write_max_(bufev_p);
  252. if (bufev_p->write_suspended)
  253. goto done;
  254. if (evbuffer_get_length(bufev->output)) {
  255. evbuffer_unfreeze(bufev->output, 1);
  256. res = evbuffer_write_atmost(bufev->output, fd, atmost);
  257. evbuffer_freeze(bufev->output, 1);
  258. if (res == -1) {
  259. int err = evutil_socket_geterror(fd);
  260. if (EVUTIL_ERR_RW_RETRIABLE(err))
  261. goto reschedule;
  262. what |= BEV_EVENT_ERROR;
  263. } else if (res == 0) {
  264. /* eof case
  265. XXXX Actually, a 0 on write doesn't indicate
  266. an EOF. An ECONNRESET might be more typical.
  267. */
  268. what |= BEV_EVENT_EOF;
  269. }
  270. if (res <= 0)
  271. goto error;
  272. bufferevent_decrement_write_buckets_(bufev_p, res);
  273. }
  274. if (evbuffer_get_length(bufev->output) == 0) {
  275. event_del(&bufev->ev_write);
  276. }
  277. /*
  278. * Invoke the user callback if our buffer is drained or below the
  279. * low watermark.
  280. */
  281. if (res || !connected) {
  282. bufferevent_trigger_nolock_(bufev, EV_WRITE, 0);
  283. }
  284. goto done;
  285. reschedule:
  286. if (evbuffer_get_length(bufev->output) == 0) {
  287. event_del(&bufev->ev_write);
  288. }
  289. goto done;
  290. error:
  291. bufferevent_disable(bufev, EV_WRITE);
  292. bufferevent_run_eventcb_(bufev, what, 0);
  293. done:
  294. bufferevent_decref_and_unlock_(bufev);
  295. }
  296. struct bufferevent *
  297. bufferevent_socket_new(struct event_base *base, evutil_socket_t fd,
  298. int options)
  299. {
  300. struct bufferevent_private *bufev_p;
  301. struct bufferevent *bufev;
  302. #ifdef _WIN32
  303. if (base && event_base_get_iocp_(base))
  304. return bufferevent_async_new_(base, fd, options);
  305. #endif
  306. if ((bufev_p = mm_calloc(1, sizeof(struct bufferevent_private)))== NULL)
  307. return NULL;
  308. if (bufferevent_init_common_(bufev_p, base, &bufferevent_ops_socket,
  309. options) < 0) {
  310. mm_free(bufev_p);
  311. return NULL;
  312. }
  313. bufev = &bufev_p->bev;
  314. evbuffer_set_flags(bufev->output, EVBUFFER_FLAG_DRAINS_TO_FD);
  315. event_assign(&bufev->ev_read, bufev->ev_base, fd,
  316. EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
  317. event_assign(&bufev->ev_write, bufev->ev_base, fd,
  318. EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
  319. evbuffer_add_cb(bufev->output, bufferevent_socket_outbuf_cb, bufev);
  320. evbuffer_freeze(bufev->input, 0);
  321. evbuffer_freeze(bufev->output, 1);
  322. return bufev;
  323. }
  324. int
  325. bufferevent_socket_connect(struct bufferevent *bev,
  326. const struct sockaddr *sa, int socklen)
  327. {
  328. struct bufferevent_private *bufev_p = BEV_UPCAST(bev);
  329. evutil_socket_t fd;
  330. int r = 0;
  331. int result=-1;
  332. int ownfd = 0;
  333. bufferevent_incref_and_lock_(bev);
  334. fd = bufferevent_getfd(bev);
  335. if (fd < 0) {
  336. if (!sa)
  337. goto done;
  338. fd = evutil_socket_(sa->sa_family,
  339. SOCK_STREAM|EVUTIL_SOCK_NONBLOCK, 0);
  340. if (fd < 0)
  341. goto freesock;
  342. ownfd = 1;
  343. }
  344. if (sa) {
  345. #ifdef _WIN32
  346. if (bufferevent_async_can_connect_(bev)) {
  347. bufferevent_setfd(bev, fd);
  348. r = bufferevent_async_connect_(bev, fd, sa, socklen);
  349. if (r < 0)
  350. goto freesock;
  351. bufev_p->connecting = 1;
  352. result = 0;
  353. goto done;
  354. } else
  355. #endif
  356. r = evutil_socket_connect_(&fd, sa, socklen);
  357. if (r < 0)
  358. goto freesock;
  359. }
  360. #ifdef _WIN32
  361. /* ConnectEx() isn't always around, even when IOCP is enabled.
  362. * Here, we borrow the socket object's write handler to fall back
  363. * on a non-blocking connect() when ConnectEx() is unavailable. */
  364. if (BEV_IS_ASYNC(bev)) {
  365. event_assign(&bev->ev_write, bev->ev_base, fd,
  366. EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bev);
  367. }
  368. #endif
  369. bufferevent_setfd(bev, fd);
  370. if (r == 0) {
  371. if (! be_socket_enable(bev, EV_WRITE)) {
  372. bufev_p->connecting = 1;
  373. result = 0;
  374. goto done;
  375. }
  376. } else if (r == 1) {
  377. /* The connect succeeded already. How very BSD of it. */
  378. result = 0;
  379. bufev_p->connecting = 1;
  380. bufferevent_trigger_nolock_(bev, EV_WRITE, BEV_OPT_DEFER_CALLBACKS);
  381. } else {
  382. /* The connect failed already. How very BSD of it. */
  383. result = 0;
  384. bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, BEV_OPT_DEFER_CALLBACKS);
  385. bufferevent_disable(bev, EV_WRITE|EV_READ);
  386. }
  387. goto done;
  388. freesock:
  389. if (ownfd)
  390. evutil_closesocket(fd);
  391. done:
  392. bufferevent_decref_and_unlock_(bev);
  393. return result;
  394. }
  395. static void
  396. bufferevent_connect_getaddrinfo_cb(int result, struct evutil_addrinfo *ai,
  397. void *arg)
  398. {
  399. struct bufferevent *bev = arg;
  400. struct bufferevent_private *bev_p = BEV_UPCAST(bev);
  401. int r;
  402. BEV_LOCK(bev);
  403. bufferevent_unsuspend_write_(bev, BEV_SUSPEND_LOOKUP);
  404. bufferevent_unsuspend_read_(bev, BEV_SUSPEND_LOOKUP);
  405. bev_p->dns_request = NULL;
  406. if (result == EVUTIL_EAI_CANCEL) {
  407. bev_p->dns_error = result;
  408. bufferevent_decref_and_unlock_(bev);
  409. return;
  410. }
  411. if (result != 0) {
  412. bev_p->dns_error = result;
  413. bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
  414. bufferevent_decref_and_unlock_(bev);
  415. if (ai)
  416. evutil_freeaddrinfo(ai);
  417. return;
  418. }
  419. /* XXX use the other addrinfos? */
  420. struct evutil_addrinfo *found_ai = ai;
  421. while (found_ai->ai_addr->sa_family != AF_INET6 && found_ai->ai_next != NULL) {
  422. found_ai = found_ai->ai_next;
  423. }
  424. bufferevent_socket_set_conn_address_(bev, found_ai->ai_addr, (int)found_ai->ai_addrlen);
  425. r = bufferevent_socket_connect(bev, found_ai->ai_addr, (int)found_ai->ai_addrlen);
  426. if (r < 0)
  427. bufferevent_run_eventcb_(bev, BEV_EVENT_ERROR, 0);
  428. bufferevent_decref_and_unlock_(bev);
  429. evutil_freeaddrinfo(ai);
  430. }
  431. int
  432. bufferevent_socket_connect_hostname(struct bufferevent *bev,
  433. struct evdns_base *evdns_base, int family, const char *hostname, int port)
  434. {
  435. char portbuf[10];
  436. struct evutil_addrinfo hint;
  437. struct bufferevent_private *bev_p = BEV_UPCAST(bev);
  438. if (family != AF_INET && family != AF_INET6 && family != AF_UNSPEC)
  439. return -1;
  440. if (port < 1 || port > 65535)
  441. return -1;
  442. memset(&hint, 0, sizeof(hint));
  443. hint.ai_family = family;
  444. hint.ai_protocol = IPPROTO_TCP;
  445. hint.ai_socktype = SOCK_STREAM;
  446. evutil_snprintf(portbuf, sizeof(portbuf), "%d", port);
  447. BEV_LOCK(bev);
  448. bev_p->dns_error = 0;
  449. bufferevent_suspend_write_(bev, BEV_SUSPEND_LOOKUP);
  450. bufferevent_suspend_read_(bev, BEV_SUSPEND_LOOKUP);
  451. bufferevent_incref_(bev);
  452. bev_p->dns_request = evutil_getaddrinfo_async_(evdns_base, hostname,
  453. portbuf, &hint, bufferevent_connect_getaddrinfo_cb, bev);
  454. BEV_UNLOCK(bev);
  455. return 0;
  456. }
  457. int
  458. bufferevent_socket_get_dns_error(struct bufferevent *bev)
  459. {
  460. int rv;
  461. struct bufferevent_private *bev_p = BEV_UPCAST(bev);
  462. BEV_LOCK(bev);
  463. rv = bev_p->dns_error;
  464. BEV_UNLOCK(bev);
  465. return rv;
  466. }
  467. /*
  468. * Create a new buffered event object.
  469. *
  470. * The read callback is invoked whenever we read new data.
  471. * The write callback is invoked whenever the output buffer is drained.
  472. * The error callback is invoked on a write/read error or on EOF.
  473. *
  474. * Both read and write callbacks maybe NULL. The error callback is not
  475. * allowed to be NULL and have to be provided always.
  476. */
  477. struct bufferevent *
  478. bufferevent_new(evutil_socket_t fd,
  479. bufferevent_data_cb readcb, bufferevent_data_cb writecb,
  480. bufferevent_event_cb eventcb, void *cbarg)
  481. {
  482. struct bufferevent *bufev;
  483. if (!(bufev = bufferevent_socket_new(NULL, fd, 0)))
  484. return NULL;
  485. bufferevent_setcb(bufev, readcb, writecb, eventcb, cbarg);
  486. return bufev;
  487. }
  488. static int
  489. be_socket_enable(struct bufferevent *bufev, short event)
  490. {
  491. if (event & EV_READ &&
  492. bufferevent_add_event_(&bufev->ev_read, &bufev->timeout_read) == -1)
  493. return -1;
  494. if (event & EV_WRITE &&
  495. bufferevent_add_event_(&bufev->ev_write, &bufev->timeout_write) == -1)
  496. return -1;
  497. return 0;
  498. }
  499. static int
  500. be_socket_disable(struct bufferevent *bufev, short event)
  501. {
  502. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  503. if (event & EV_READ) {
  504. if (event_del(&bufev->ev_read) == -1)
  505. return -1;
  506. }
  507. /* Don't actually disable the write if we are trying to connect. */
  508. if ((event & EV_WRITE) && ! bufev_p->connecting) {
  509. if (event_del(&bufev->ev_write) == -1)
  510. return -1;
  511. }
  512. return 0;
  513. }
  514. static void
  515. be_socket_destruct(struct bufferevent *bufev)
  516. {
  517. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  518. evutil_socket_t fd;
  519. EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
  520. fd = event_get_fd(&bufev->ev_read);
  521. if ((bufev_p->options & BEV_OPT_CLOSE_ON_FREE) && fd >= 0)
  522. EVUTIL_CLOSESOCKET(fd);
  523. evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
  524. }
  525. static int
  526. be_socket_flush(struct bufferevent *bev, short iotype,
  527. enum bufferevent_flush_mode mode)
  528. {
  529. return 0;
  530. }
  531. static void
  532. be_socket_setfd(struct bufferevent *bufev, evutil_socket_t fd)
  533. {
  534. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  535. BEV_LOCK(bufev);
  536. EVUTIL_ASSERT(BEV_IS_SOCKET(bufev));
  537. event_del(&bufev->ev_read);
  538. event_del(&bufev->ev_write);
  539. evbuffer_unfreeze(bufev->input, 0);
  540. evbuffer_unfreeze(bufev->output, 1);
  541. event_assign(&bufev->ev_read, bufev->ev_base, fd,
  542. EV_READ|EV_PERSIST|EV_FINALIZE, bufferevent_readcb, bufev);
  543. event_assign(&bufev->ev_write, bufev->ev_base, fd,
  544. EV_WRITE|EV_PERSIST|EV_FINALIZE, bufferevent_writecb, bufev);
  545. if (fd >= 0)
  546. bufferevent_enable(bufev, bufev->enabled);
  547. evutil_getaddrinfo_cancel_async_(bufev_p->dns_request);
  548. BEV_UNLOCK(bufev);
  549. }
  550. /* XXXX Should non-socket bufferevents support this? */
  551. int
  552. bufferevent_priority_set(struct bufferevent *bufev, int priority)
  553. {
  554. int r = -1;
  555. struct bufferevent_private *bufev_p = BEV_UPCAST(bufev);
  556. BEV_LOCK(bufev);
  557. if (BEV_IS_ASYNC(bufev) || BEV_IS_FILTER(bufev) || BEV_IS_PAIR(bufev))
  558. goto done;
  559. if (event_priority_set(&bufev->ev_read, priority) == -1)
  560. goto done;
  561. if (event_priority_set(&bufev->ev_write, priority) == -1)
  562. goto done;
  563. event_deferred_cb_set_priority_(&bufev_p->deferred, priority);
  564. r = 0;
  565. done:
  566. BEV_UNLOCK(bufev);
  567. return r;
  568. }
  569. /* XXXX Should non-socket bufferevents support this? */
  570. int
  571. bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
  572. {
  573. int res = -1;
  574. BEV_LOCK(bufev);
  575. if (!BEV_IS_SOCKET(bufev))
  576. goto done;
  577. bufev->ev_base = base;
  578. res = event_base_set(base, &bufev->ev_read);
  579. if (res == -1)
  580. goto done;
  581. res = event_base_set(base, &bufev->ev_write);
  582. done:
  583. BEV_UNLOCK(bufev);
  584. return res;
  585. }
  586. static int
  587. be_socket_ctrl(struct bufferevent *bev, enum bufferevent_ctrl_op op,
  588. union bufferevent_ctrl_data *data)
  589. {
  590. switch (op) {
  591. case BEV_CTRL_SET_FD:
  592. be_socket_setfd(bev, data->fd);
  593. return 0;
  594. case BEV_CTRL_GET_FD:
  595. data->fd = event_get_fd(&bev->ev_read);
  596. return 0;
  597. case BEV_CTRL_GET_UNDERLYING:
  598. case BEV_CTRL_CANCEL_ALL:
  599. default:
  600. return -1;
  601. }
  602. }