listener.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. #include <sys/types.h>
  29. #ifdef _WIN32
  30. #ifndef _WIN32_WINNT
  31. /* Minimum required for InitializeCriticalSectionAndSpinCount */
  32. #define _WIN32_WINNT 0x0403
  33. #endif
  34. #include <winsock2.h>
  35. #include <winerror.h>
  36. #include <ws2tcpip.h>
  37. #include <mswsock.h>
  38. #endif
  39. #include <errno.h>
  40. #ifdef EVENT__HAVE_SYS_SOCKET_H
  41. #include <sys/socket.h>
  42. #endif
  43. #ifdef EVENT__HAVE_FCNTL_H
  44. #include <fcntl.h>
  45. #endif
  46. #ifdef EVENT__HAVE_UNISTD_H
  47. #include <unistd.h>
  48. #endif
  49. #include "event2/listener.h"
  50. #include "event2/util.h"
  51. #include "event2/event.h"
  52. #include "event2/event_struct.h"
  53. #include "mm-internal.h"
  54. #include "util-internal.h"
  55. #include "log-internal.h"
  56. #include "evthread-internal.h"
  57. #ifdef _WIN32
  58. #include "iocp-internal.h"
  59. #include "defer-internal.h"
  60. #include "event-internal.h"
  61. #endif
  62. struct evconnlistener_ops {
  63. int (*enable)(struct evconnlistener *);
  64. int (*disable)(struct evconnlistener *);
  65. void (*destroy)(struct evconnlistener *);
  66. void (*shutdown)(struct evconnlistener *);
  67. evutil_socket_t (*getfd)(struct evconnlistener *);
  68. struct event_base *(*getbase)(struct evconnlistener *);
  69. };
  70. struct evconnlistener {
  71. const struct evconnlistener_ops *ops;
  72. void *lock;
  73. evconnlistener_cb cb;
  74. evconnlistener_errorcb errorcb;
  75. void *user_data;
  76. unsigned flags;
  77. short refcnt;
  78. int accept4_flags;
  79. unsigned enabled : 1;
  80. };
  81. struct evconnlistener_event {
  82. struct evconnlistener base;
  83. struct event listener;
  84. };
  85. #ifdef _WIN32
  86. struct evconnlistener_iocp {
  87. struct evconnlistener base;
  88. evutil_socket_t fd;
  89. struct event_base *event_base;
  90. struct event_iocp_port *port;
  91. short n_accepting;
  92. unsigned shutting_down : 1;
  93. unsigned event_added : 1;
  94. struct accepting_socket **accepting;
  95. };
  96. #endif
  97. #define LOCK(listener) EVLOCK_LOCK((listener)->lock, 0)
  98. #define UNLOCK(listener) EVLOCK_UNLOCK((listener)->lock, 0)
  99. struct evconnlistener *
  100. evconnlistener_new_async(struct event_base *base,
  101. evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
  102. evutil_socket_t fd); /* XXXX export this? */
  103. static int event_listener_enable(struct evconnlistener *);
  104. static int event_listener_disable(struct evconnlistener *);
  105. static void event_listener_destroy(struct evconnlistener *);
  106. static evutil_socket_t event_listener_getfd(struct evconnlistener *);
  107. static struct event_base *event_listener_getbase(struct evconnlistener *);
  108. #if 0
  109. static void
  110. listener_incref_and_lock(struct evconnlistener *listener)
  111. {
  112. LOCK(listener);
  113. ++listener->refcnt;
  114. }
  115. #endif
  116. static int
  117. listener_decref_and_unlock(struct evconnlistener *listener)
  118. {
  119. int refcnt = --listener->refcnt;
  120. if (refcnt == 0) {
  121. listener->ops->destroy(listener);
  122. UNLOCK(listener);
  123. EVTHREAD_FREE_LOCK(listener->lock, EVTHREAD_LOCKTYPE_RECURSIVE);
  124. mm_free(listener);
  125. return 1;
  126. } else {
  127. UNLOCK(listener);
  128. return 0;
  129. }
  130. }
  131. static const struct evconnlistener_ops evconnlistener_event_ops = {
  132. event_listener_enable,
  133. event_listener_disable,
  134. event_listener_destroy,
  135. NULL, /* shutdown */
  136. event_listener_getfd,
  137. event_listener_getbase
  138. };
  139. static void listener_read_cb(evutil_socket_t, short, void *);
  140. struct evconnlistener *
  141. evconnlistener_new(struct event_base *base,
  142. evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
  143. evutil_socket_t fd)
  144. {
  145. struct evconnlistener_event *lev;
  146. #ifdef _WIN32
  147. if (base && event_base_get_iocp_(base)) {
  148. const struct win32_extension_fns *ext =
  149. event_get_win32_extension_fns_();
  150. if (ext->AcceptEx && ext->GetAcceptExSockaddrs)
  151. return evconnlistener_new_async(base, cb, ptr, flags,
  152. backlog, fd);
  153. }
  154. #endif
  155. if (backlog > 0) {
  156. if (listen(fd, backlog) < 0)
  157. return NULL;
  158. } else if (backlog < 0) {
  159. if (listen(fd, 128) < 0)
  160. return NULL;
  161. }
  162. lev = mm_calloc(1, sizeof(struct evconnlistener_event));
  163. if (!lev)
  164. return NULL;
  165. lev->base.ops = &evconnlistener_event_ops;
  166. lev->base.cb = cb;
  167. lev->base.user_data = ptr;
  168. lev->base.flags = flags;
  169. lev->base.refcnt = 1;
  170. lev->base.accept4_flags = 0;
  171. if (!(flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
  172. lev->base.accept4_flags |= EVUTIL_SOCK_NONBLOCK;
  173. if (flags & LEV_OPT_CLOSE_ON_EXEC)
  174. lev->base.accept4_flags |= EVUTIL_SOCK_CLOEXEC;
  175. if (flags & LEV_OPT_THREADSAFE) {
  176. EVTHREAD_ALLOC_LOCK(lev->base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
  177. }
  178. event_assign(&lev->listener, base, fd, EV_READ|EV_PERSIST,
  179. listener_read_cb, lev);
  180. if (!(flags & LEV_OPT_DISABLED))
  181. evconnlistener_enable(&lev->base);
  182. return &lev->base;
  183. }
  184. struct evconnlistener *
  185. evconnlistener_new_bind(struct event_base *base, evconnlistener_cb cb,
  186. void *ptr, unsigned flags, int backlog, const struct sockaddr *sa,
  187. int socklen)
  188. {
  189. struct evconnlistener *listener;
  190. evutil_socket_t fd;
  191. int on = 1;
  192. int family = sa ? sa->sa_family : AF_UNSPEC;
  193. int socktype = SOCK_STREAM | EVUTIL_SOCK_NONBLOCK;
  194. if (backlog == 0)
  195. return NULL;
  196. if (flags & LEV_OPT_CLOSE_ON_EXEC)
  197. socktype |= EVUTIL_SOCK_CLOEXEC;
  198. fd = evutil_socket_(family, socktype, 0);
  199. if (fd == -1)
  200. return NULL;
  201. if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, (void*)&on, sizeof(on))<0)
  202. goto err;
  203. if (flags & LEV_OPT_REUSEABLE) {
  204. if (evutil_make_listen_socket_reuseable(fd) < 0)
  205. goto err;
  206. }
  207. if (flags & LEV_OPT_REUSEABLE_PORT) {
  208. if (evutil_make_listen_socket_reuseable_port(fd) < 0)
  209. goto err;
  210. }
  211. if (flags & LEV_OPT_DEFERRED_ACCEPT) {
  212. if (evutil_make_tcp_listen_socket_deferred(fd) < 0)
  213. goto err;
  214. }
  215. if (flags & LEV_OPT_BIND_IPV6ONLY) {
  216. if (evutil_make_listen_socket_ipv6only(fd) < 0)
  217. goto err;
  218. }
  219. if (sa) {
  220. if (bind(fd, sa, socklen)<0)
  221. goto err;
  222. }
  223. listener = evconnlistener_new(base, cb, ptr, flags, backlog, fd);
  224. if (!listener)
  225. goto err;
  226. return listener;
  227. err:
  228. evutil_closesocket(fd);
  229. return NULL;
  230. }
  231. void
  232. evconnlistener_free(struct evconnlistener *lev)
  233. {
  234. LOCK(lev);
  235. lev->cb = NULL;
  236. lev->errorcb = NULL;
  237. if (lev->ops->shutdown)
  238. lev->ops->shutdown(lev);
  239. listener_decref_and_unlock(lev);
  240. }
  241. static void
  242. event_listener_destroy(struct evconnlistener *lev)
  243. {
  244. struct evconnlistener_event *lev_e =
  245. EVUTIL_UPCAST(lev, struct evconnlistener_event, base);
  246. event_del(&lev_e->listener);
  247. if (lev->flags & LEV_OPT_CLOSE_ON_FREE)
  248. evutil_closesocket(event_get_fd(&lev_e->listener));
  249. event_debug_unassign(&lev_e->listener);
  250. }
  251. int
  252. evconnlistener_enable(struct evconnlistener *lev)
  253. {
  254. int r;
  255. LOCK(lev);
  256. lev->enabled = 1;
  257. if (lev->cb)
  258. r = lev->ops->enable(lev);
  259. else
  260. r = 0;
  261. UNLOCK(lev);
  262. return r;
  263. }
  264. int
  265. evconnlistener_disable(struct evconnlistener *lev)
  266. {
  267. int r;
  268. LOCK(lev);
  269. lev->enabled = 0;
  270. r = lev->ops->disable(lev);
  271. UNLOCK(lev);
  272. return r;
  273. }
  274. static int
  275. event_listener_enable(struct evconnlistener *lev)
  276. {
  277. struct evconnlistener_event *lev_e =
  278. EVUTIL_UPCAST(lev, struct evconnlistener_event, base);
  279. return event_add(&lev_e->listener, NULL);
  280. }
  281. static int
  282. event_listener_disable(struct evconnlistener *lev)
  283. {
  284. struct evconnlistener_event *lev_e =
  285. EVUTIL_UPCAST(lev, struct evconnlistener_event, base);
  286. return event_del(&lev_e->listener);
  287. }
  288. evutil_socket_t
  289. evconnlistener_get_fd(struct evconnlistener *lev)
  290. {
  291. evutil_socket_t fd;
  292. LOCK(lev);
  293. fd = lev->ops->getfd(lev);
  294. UNLOCK(lev);
  295. return fd;
  296. }
  297. static evutil_socket_t
  298. event_listener_getfd(struct evconnlistener *lev)
  299. {
  300. struct evconnlistener_event *lev_e =
  301. EVUTIL_UPCAST(lev, struct evconnlistener_event, base);
  302. return event_get_fd(&lev_e->listener);
  303. }
  304. struct event_base *
  305. evconnlistener_get_base(struct evconnlistener *lev)
  306. {
  307. struct event_base *base;
  308. LOCK(lev);
  309. base = lev->ops->getbase(lev);
  310. UNLOCK(lev);
  311. return base;
  312. }
  313. static struct event_base *
  314. event_listener_getbase(struct evconnlistener *lev)
  315. {
  316. struct evconnlistener_event *lev_e =
  317. EVUTIL_UPCAST(lev, struct evconnlistener_event, base);
  318. return event_get_base(&lev_e->listener);
  319. }
  320. void
  321. evconnlistener_set_cb(struct evconnlistener *lev,
  322. evconnlistener_cb cb, void *arg)
  323. {
  324. int enable = 0;
  325. LOCK(lev);
  326. if (lev->enabled && !lev->cb)
  327. enable = 1;
  328. lev->cb = cb;
  329. lev->user_data = arg;
  330. if (enable)
  331. evconnlistener_enable(lev);
  332. UNLOCK(lev);
  333. }
  334. void
  335. evconnlistener_set_error_cb(struct evconnlistener *lev,
  336. evconnlistener_errorcb errorcb)
  337. {
  338. LOCK(lev);
  339. lev->errorcb = errorcb;
  340. UNLOCK(lev);
  341. }
  342. static void
  343. listener_read_cb(evutil_socket_t fd, short what, void *p)
  344. {
  345. struct evconnlistener *lev = p;
  346. int err;
  347. evconnlistener_cb cb;
  348. evconnlistener_errorcb errorcb;
  349. void *user_data;
  350. LOCK(lev);
  351. while (1) {
  352. struct sockaddr_storage ss;
  353. ev_socklen_t socklen = sizeof(ss);
  354. evutil_socket_t new_fd = evutil_accept4_(fd, (struct sockaddr*)&ss, &socklen, lev->accept4_flags);
  355. if (new_fd < 0)
  356. break;
  357. if (socklen == 0) {
  358. /* This can happen with some older linux kernels in
  359. * response to nmap. */
  360. evutil_closesocket(new_fd);
  361. continue;
  362. }
  363. if (lev->cb == NULL) {
  364. evutil_closesocket(new_fd);
  365. UNLOCK(lev);
  366. return;
  367. }
  368. ++lev->refcnt;
  369. cb = lev->cb;
  370. user_data = lev->user_data;
  371. UNLOCK(lev);
  372. cb(lev, new_fd, (struct sockaddr*)&ss, (int)socklen,
  373. user_data);
  374. LOCK(lev);
  375. if (lev->refcnt == 1) {
  376. int freed = listener_decref_and_unlock(lev);
  377. EVUTIL_ASSERT(freed);
  378. return;
  379. }
  380. --lev->refcnt;
  381. if (!lev->enabled) {
  382. /* the callback could have disabled the listener */
  383. UNLOCK(lev);
  384. return;
  385. }
  386. }
  387. err = evutil_socket_geterror(fd);
  388. if (EVUTIL_ERR_ACCEPT_RETRIABLE(err)) {
  389. UNLOCK(lev);
  390. return;
  391. }
  392. if (lev->errorcb != NULL) {
  393. ++lev->refcnt;
  394. errorcb = lev->errorcb;
  395. user_data = lev->user_data;
  396. UNLOCK(lev);
  397. errorcb(lev, user_data);
  398. LOCK(lev);
  399. listener_decref_and_unlock(lev);
  400. } else {
  401. event_sock_warn(fd, "Error from accept() call");
  402. UNLOCK(lev);
  403. }
  404. }
  405. #ifdef _WIN32
  406. struct accepting_socket {
  407. CRITICAL_SECTION lock;
  408. struct event_overlapped overlapped;
  409. SOCKET s;
  410. int error;
  411. struct event_callback deferred;
  412. struct evconnlistener_iocp *lev;
  413. ev_uint8_t buflen;
  414. ev_uint8_t family;
  415. unsigned free_on_cb:1;
  416. char addrbuf[1];
  417. };
  418. static void accepted_socket_cb(struct event_overlapped *o, ev_uintptr_t key,
  419. ev_ssize_t n, int ok);
  420. static void accepted_socket_invoke_user_cb(struct event_callback *cb, void *arg);
  421. static void
  422. iocp_listener_event_add(struct evconnlistener_iocp *lev)
  423. {
  424. if (lev->event_added)
  425. return;
  426. lev->event_added = 1;
  427. event_base_add_virtual_(lev->event_base);
  428. }
  429. static void
  430. iocp_listener_event_del(struct evconnlistener_iocp *lev)
  431. {
  432. if (!lev->event_added)
  433. return;
  434. lev->event_added = 0;
  435. event_base_del_virtual_(lev->event_base);
  436. }
  437. static struct accepting_socket *
  438. new_accepting_socket(struct evconnlistener_iocp *lev, int family)
  439. {
  440. struct accepting_socket *res;
  441. int addrlen;
  442. int buflen;
  443. if (family == AF_INET)
  444. addrlen = sizeof(struct sockaddr_in);
  445. else if (family == AF_INET6)
  446. addrlen = sizeof(struct sockaddr_in6);
  447. else
  448. return NULL;
  449. buflen = (addrlen+16)*2;
  450. res = mm_calloc(1,sizeof(struct accepting_socket)-1+buflen);
  451. if (!res)
  452. return NULL;
  453. event_overlapped_init_(&res->overlapped, accepted_socket_cb);
  454. res->s = EVUTIL_INVALID_SOCKET;
  455. res->lev = lev;
  456. res->buflen = buflen;
  457. res->family = family;
  458. event_deferred_cb_init_(&res->deferred,
  459. event_base_get_npriorities(lev->event_base) / 2,
  460. accepted_socket_invoke_user_cb, res);
  461. InitializeCriticalSectionAndSpinCount(&res->lock, 1000);
  462. return res;
  463. }
  464. static void
  465. free_and_unlock_accepting_socket(struct accepting_socket *as)
  466. {
  467. /* requires lock. */
  468. if (as->s != EVUTIL_INVALID_SOCKET)
  469. closesocket(as->s);
  470. LeaveCriticalSection(&as->lock);
  471. DeleteCriticalSection(&as->lock);
  472. mm_free(as);
  473. }
  474. static int
  475. start_accepting(struct accepting_socket *as)
  476. {
  477. /* requires lock */
  478. const struct win32_extension_fns *ext = event_get_win32_extension_fns_();
  479. DWORD pending = 0;
  480. SOCKET s = socket(as->family, SOCK_STREAM, 0);
  481. int error = 0;
  482. if (!as->lev->base.enabled)
  483. return 0;
  484. if (s == EVUTIL_INVALID_SOCKET) {
  485. error = WSAGetLastError();
  486. goto report_err;
  487. }
  488. /* XXXX It turns out we need to do this again later. Does this call
  489. * have any effect? */
  490. setsockopt(s, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
  491. (char *)&as->lev->fd, sizeof(&as->lev->fd));
  492. if (!(as->lev->base.flags & LEV_OPT_LEAVE_SOCKETS_BLOCKING))
  493. evutil_make_socket_nonblocking(s);
  494. if (event_iocp_port_associate_(as->lev->port, s, 1) < 0) {
  495. closesocket(s);
  496. return -1;
  497. }
  498. as->s = s;
  499. if (ext->AcceptEx(as->lev->fd, s, as->addrbuf, 0,
  500. as->buflen/2, as->buflen/2, &pending, &as->overlapped.overlapped))
  501. {
  502. /* Immediate success! */
  503. accepted_socket_cb(&as->overlapped, 1, 0, 1);
  504. } else {
  505. error = WSAGetLastError();
  506. if (error != ERROR_IO_PENDING) {
  507. goto report_err;
  508. }
  509. }
  510. return 0;
  511. report_err:
  512. as->error = error;
  513. event_deferred_cb_schedule_(
  514. as->lev->event_base,
  515. &as->deferred);
  516. return 0;
  517. }
  518. static void
  519. stop_accepting(struct accepting_socket *as)
  520. {
  521. /* requires lock. */
  522. SOCKET s = as->s;
  523. as->s = EVUTIL_INVALID_SOCKET;
  524. closesocket(s);
  525. }
  526. static void
  527. accepted_socket_invoke_user_cb(struct event_callback *dcb, void *arg)
  528. {
  529. struct accepting_socket *as = arg;
  530. struct sockaddr *sa_local=NULL, *sa_remote=NULL;
  531. int socklen_local=0, socklen_remote=0;
  532. const struct win32_extension_fns *ext = event_get_win32_extension_fns_();
  533. struct evconnlistener *lev = &as->lev->base;
  534. evutil_socket_t sock=-1;
  535. void *data;
  536. evconnlistener_cb cb=NULL;
  537. evconnlistener_errorcb errorcb=NULL;
  538. int error;
  539. EVUTIL_ASSERT(ext->GetAcceptExSockaddrs);
  540. LOCK(lev);
  541. EnterCriticalSection(&as->lock);
  542. if (as->free_on_cb) {
  543. free_and_unlock_accepting_socket(as);
  544. listener_decref_and_unlock(lev);
  545. return;
  546. }
  547. ++lev->refcnt;
  548. error = as->error;
  549. if (error) {
  550. as->error = 0;
  551. errorcb = lev->errorcb;
  552. } else {
  553. ext->GetAcceptExSockaddrs(
  554. as->addrbuf, 0, as->buflen/2, as->buflen/2,
  555. &sa_local, &socklen_local, &sa_remote,
  556. &socklen_remote);
  557. sock = as->s;
  558. cb = lev->cb;
  559. as->s = EVUTIL_INVALID_SOCKET;
  560. /* We need to call this so getsockname, getpeername, and
  561. * shutdown work correctly on the accepted socket. */
  562. /* XXXX handle error? */
  563. setsockopt(sock, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
  564. (char *)&as->lev->fd, sizeof(&as->lev->fd));
  565. }
  566. data = lev->user_data;
  567. LeaveCriticalSection(&as->lock);
  568. UNLOCK(lev);
  569. if (errorcb) {
  570. WSASetLastError(error);
  571. errorcb(lev, data);
  572. } else if (cb) {
  573. cb(lev, sock, sa_remote, socklen_remote, data);
  574. }
  575. LOCK(lev);
  576. if (listener_decref_and_unlock(lev))
  577. return;
  578. EnterCriticalSection(&as->lock);
  579. start_accepting(as);
  580. LeaveCriticalSection(&as->lock);
  581. }
  582. static void
  583. accepted_socket_cb(struct event_overlapped *o, ev_uintptr_t key, ev_ssize_t n, int ok)
  584. {
  585. struct accepting_socket *as =
  586. EVUTIL_UPCAST(o, struct accepting_socket, overlapped);
  587. LOCK(&as->lev->base);
  588. EnterCriticalSection(&as->lock);
  589. if (ok) {
  590. /* XXXX Don't do this if some EV_MT flag is set. */
  591. event_deferred_cb_schedule_(
  592. as->lev->event_base,
  593. &as->deferred);
  594. LeaveCriticalSection(&as->lock);
  595. } else if (as->free_on_cb) {
  596. struct evconnlistener *lev = &as->lev->base;
  597. free_and_unlock_accepting_socket(as);
  598. listener_decref_and_unlock(lev);
  599. return;
  600. } else if (as->s == EVUTIL_INVALID_SOCKET) {
  601. /* This is okay; we were disabled by iocp_listener_disable. */
  602. LeaveCriticalSection(&as->lock);
  603. } else {
  604. /* Some error on accept that we couldn't actually handle. */
  605. BOOL ok;
  606. DWORD transfer = 0, flags=0;
  607. event_sock_warn(as->s, "Unexpected error on AcceptEx");
  608. ok = WSAGetOverlappedResult(as->s, &o->overlapped,
  609. &transfer, FALSE, &flags);
  610. if (ok) {
  611. /* well, that was confusing! */
  612. as->error = 1;
  613. } else {
  614. as->error = WSAGetLastError();
  615. }
  616. event_deferred_cb_schedule_(
  617. as->lev->event_base,
  618. &as->deferred);
  619. LeaveCriticalSection(&as->lock);
  620. }
  621. UNLOCK(&as->lev->base);
  622. }
  623. static int
  624. iocp_listener_enable(struct evconnlistener *lev)
  625. {
  626. int i;
  627. struct evconnlistener_iocp *lev_iocp =
  628. EVUTIL_UPCAST(lev, struct evconnlistener_iocp, base);
  629. LOCK(lev);
  630. iocp_listener_event_add(lev_iocp);
  631. for (i = 0; i < lev_iocp->n_accepting; ++i) {
  632. struct accepting_socket *as = lev_iocp->accepting[i];
  633. if (!as)
  634. continue;
  635. EnterCriticalSection(&as->lock);
  636. if (!as->free_on_cb && as->s == EVUTIL_INVALID_SOCKET)
  637. start_accepting(as);
  638. LeaveCriticalSection(&as->lock);
  639. }
  640. UNLOCK(lev);
  641. return 0;
  642. }
  643. static int
  644. iocp_listener_disable_impl(struct evconnlistener *lev, int shutdown)
  645. {
  646. int i;
  647. struct evconnlistener_iocp *lev_iocp =
  648. EVUTIL_UPCAST(lev, struct evconnlistener_iocp, base);
  649. LOCK(lev);
  650. iocp_listener_event_del(lev_iocp);
  651. for (i = 0; i < lev_iocp->n_accepting; ++i) {
  652. struct accepting_socket *as = lev_iocp->accepting[i];
  653. if (!as)
  654. continue;
  655. EnterCriticalSection(&as->lock);
  656. if (!as->free_on_cb && as->s != EVUTIL_INVALID_SOCKET) {
  657. if (shutdown)
  658. as->free_on_cb = 1;
  659. stop_accepting(as);
  660. }
  661. LeaveCriticalSection(&as->lock);
  662. }
  663. if (shutdown && lev->flags & LEV_OPT_CLOSE_ON_FREE)
  664. evutil_closesocket(lev_iocp->fd);
  665. UNLOCK(lev);
  666. return 0;
  667. }
  668. static int
  669. iocp_listener_disable(struct evconnlistener *lev)
  670. {
  671. return iocp_listener_disable_impl(lev,0);
  672. }
  673. static void
  674. iocp_listener_destroy(struct evconnlistener *lev)
  675. {
  676. struct evconnlistener_iocp *lev_iocp =
  677. EVUTIL_UPCAST(lev, struct evconnlistener_iocp, base);
  678. if (! lev_iocp->shutting_down) {
  679. lev_iocp->shutting_down = 1;
  680. iocp_listener_disable_impl(lev,1);
  681. }
  682. }
  683. static evutil_socket_t
  684. iocp_listener_getfd(struct evconnlistener *lev)
  685. {
  686. struct evconnlistener_iocp *lev_iocp =
  687. EVUTIL_UPCAST(lev, struct evconnlistener_iocp, base);
  688. return lev_iocp->fd;
  689. }
  690. static struct event_base *
  691. iocp_listener_getbase(struct evconnlistener *lev)
  692. {
  693. struct evconnlistener_iocp *lev_iocp =
  694. EVUTIL_UPCAST(lev, struct evconnlistener_iocp, base);
  695. return lev_iocp->event_base;
  696. }
  697. static const struct evconnlistener_ops evconnlistener_iocp_ops = {
  698. iocp_listener_enable,
  699. iocp_listener_disable,
  700. iocp_listener_destroy,
  701. iocp_listener_destroy, /* shutdown */
  702. iocp_listener_getfd,
  703. iocp_listener_getbase
  704. };
  705. /* XXX define some way to override this. */
  706. #define N_SOCKETS_PER_LISTENER 4
  707. struct evconnlistener *
  708. evconnlistener_new_async(struct event_base *base,
  709. evconnlistener_cb cb, void *ptr, unsigned flags, int backlog,
  710. evutil_socket_t fd)
  711. {
  712. struct sockaddr_storage ss;
  713. int socklen = sizeof(ss);
  714. struct evconnlistener_iocp *lev;
  715. int i;
  716. flags |= LEV_OPT_THREADSAFE;
  717. if (!base || !event_base_get_iocp_(base))
  718. goto err;
  719. /* XXXX duplicate code */
  720. if (backlog > 0) {
  721. if (listen(fd, backlog) < 0)
  722. goto err;
  723. } else if (backlog < 0) {
  724. if (listen(fd, 128) < 0)
  725. goto err;
  726. }
  727. if (getsockname(fd, (struct sockaddr*)&ss, &socklen)) {
  728. event_sock_warn(fd, "getsockname");
  729. goto err;
  730. }
  731. lev = mm_calloc(1, sizeof(struct evconnlistener_iocp));
  732. if (!lev) {
  733. event_warn("calloc");
  734. goto err;
  735. }
  736. lev->base.ops = &evconnlistener_iocp_ops;
  737. lev->base.cb = cb;
  738. lev->base.user_data = ptr;
  739. lev->base.flags = flags;
  740. lev->base.refcnt = 1;
  741. lev->base.enabled = 1;
  742. lev->port = event_base_get_iocp_(base);
  743. lev->fd = fd;
  744. lev->event_base = base;
  745. if (event_iocp_port_associate_(lev->port, fd, 1) < 0)
  746. goto err_free_lev;
  747. EVTHREAD_ALLOC_LOCK(lev->base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
  748. lev->n_accepting = N_SOCKETS_PER_LISTENER;
  749. lev->accepting = mm_calloc(lev->n_accepting,
  750. sizeof(struct accepting_socket *));
  751. if (!lev->accepting) {
  752. event_warn("calloc");
  753. goto err_delete_lock;
  754. }
  755. for (i = 0; i < lev->n_accepting; ++i) {
  756. lev->accepting[i] = new_accepting_socket(lev, ss.ss_family);
  757. if (!lev->accepting[i]) {
  758. event_warnx("Couldn't create accepting socket");
  759. goto err_free_accepting;
  760. }
  761. if (cb && start_accepting(lev->accepting[i]) < 0) {
  762. event_warnx("Couldn't start accepting on socket");
  763. EnterCriticalSection(&lev->accepting[i]->lock);
  764. free_and_unlock_accepting_socket(lev->accepting[i]);
  765. goto err_free_accepting;
  766. }
  767. ++lev->base.refcnt;
  768. }
  769. iocp_listener_event_add(lev);
  770. return &lev->base;
  771. err_free_accepting:
  772. mm_free(lev->accepting);
  773. /* XXXX free the other elements. */
  774. err_delete_lock:
  775. EVTHREAD_FREE_LOCK(lev->base.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
  776. err_free_lev:
  777. mm_free(lev);
  778. err:
  779. /* Don't close the fd, it is caller's responsibility. */
  780. return NULL;
  781. }
  782. #endif