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