]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/isc/unix/socket.c
MFC r254651:
[FreeBSD/stable/9.git] / contrib / bind9 / lib / isc / unix / socket.c
1 /*
2  * Copyright (C) 2004-2013  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2003  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 /* $Id$ */
19
20 /*! \file */
21
22 #include <config.h>
23
24 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/uio.h>
30
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37
38 #include <isc/buffer.h>
39 #include <isc/bufferlist.h>
40 #include <isc/condition.h>
41 #include <isc/formatcheck.h>
42 #include <isc/list.h>
43 #include <isc/log.h>
44 #include <isc/mem.h>
45 #include <isc/msgs.h>
46 #include <isc/mutex.h>
47 #include <isc/net.h>
48 #include <isc/once.h>
49 #include <isc/platform.h>
50 #include <isc/print.h>
51 #include <isc/region.h>
52 #include <isc/socket.h>
53 #include <isc/stats.h>
54 #include <isc/strerror.h>
55 #include <isc/task.h>
56 #include <isc/thread.h>
57 #include <isc/util.h>
58 #include <isc/xml.h>
59
60 #ifdef ISC_PLATFORM_HAVESYSUNH
61 #include <sys/un.h>
62 #endif
63 #ifdef ISC_PLATFORM_HAVEKQUEUE
64 #include <sys/event.h>
65 #endif
66 #ifdef ISC_PLATFORM_HAVEEPOLL
67 #include <sys/epoll.h>
68 #endif
69 #ifdef ISC_PLATFORM_HAVEDEVPOLL
70 #if defined(HAVE_SYS_DEVPOLL_H)
71 #include <sys/devpoll.h>
72 #elif defined(HAVE_DEVPOLL_H)
73 #include <devpoll.h>
74 #endif
75 #endif
76
77 #include "errno2result.h"
78
79 /* See task.c about the following definition: */
80 #ifdef BIND9
81 #ifdef ISC_PLATFORM_USETHREADS
82 #define USE_WATCHER_THREAD
83 #else
84 #define USE_SHARED_MANAGER
85 #endif  /* ISC_PLATFORM_USETHREADS */
86 #endif  /* BIND9 */
87
88 #ifndef USE_WATCHER_THREAD
89 #include "socket_p.h"
90 #include "../task_p.h"
91 #endif /* USE_WATCHER_THREAD */
92
93 #if defined(SO_BSDCOMPAT) && defined(__linux__)
94 #include <sys/utsname.h>
95 #endif
96
97 /*%
98  * Choose the most preferable multiplex method.
99  */
100 #ifdef ISC_PLATFORM_HAVEKQUEUE
101 #define USE_KQUEUE
102 #elif defined (ISC_PLATFORM_HAVEEPOLL)
103 #define USE_EPOLL
104 #elif defined (ISC_PLATFORM_HAVEDEVPOLL)
105 #define USE_DEVPOLL
106 typedef struct {
107         unsigned int want_read : 1,
108                 want_write : 1;
109 } pollinfo_t;
110 #else
111 #define USE_SELECT
112 #endif  /* ISC_PLATFORM_HAVEKQUEUE */
113
114 #ifndef USE_WATCHER_THREAD
115 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
116 struct isc_socketwait {
117         int nevents;
118 };
119 #elif defined (USE_SELECT)
120 struct isc_socketwait {
121         fd_set *readset;
122         fd_set *writeset;
123         int nfds;
124         int maxfd;
125 };
126 #endif  /* USE_KQUEUE */
127 #endif /* !USE_WATCHER_THREAD */
128
129 /*%
130  * Maximum number of allowable open sockets.  This is also the maximum
131  * allowable socket file descriptor.
132  *
133  * Care should be taken before modifying this value for select():
134  * The API standard doesn't ensure select() accept more than (the system default
135  * of) FD_SETSIZE descriptors, and the default size should in fact be fine in
136  * the vast majority of cases.  This constant should therefore be increased only
137  * when absolutely necessary and possible, i.e., the server is exhausting all
138  * available file descriptors (up to FD_SETSIZE) and the select() function
139  * and FD_xxx macros support larger values than FD_SETSIZE (which may not
140  * always by true, but we keep using some of them to ensure as much
141  * portability as possible).  Note also that overall server performance
142  * may be rather worsened with a larger value of this constant due to
143  * inherent scalability problems of select().
144  *
145  * As a special note, this value shouldn't have to be touched if
146  * this is a build for an authoritative only DNS server.
147  */
148 #ifndef ISC_SOCKET_MAXSOCKETS
149 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
150 #define ISC_SOCKET_MAXSOCKETS 4096
151 #elif defined(USE_SELECT)
152 #define ISC_SOCKET_MAXSOCKETS FD_SETSIZE
153 #endif  /* USE_KQUEUE... */
154 #endif  /* ISC_SOCKET_MAXSOCKETS */
155
156 #ifdef USE_SELECT
157 /*%
158  * Mac OS X needs a special definition to support larger values in select().
159  * We always define this because a larger value can be specified run-time.
160  */
161 #ifdef __APPLE__
162 #define _DARWIN_UNLIMITED_SELECT
163 #endif  /* __APPLE__ */
164 #endif  /* USE_SELECT */
165
166 #ifdef ISC_SOCKET_USE_POLLWATCH
167 /*%
168  * If this macro is defined, enable workaround for a Solaris /dev/poll kernel
169  * bug: DP_POLL ioctl could keep sleeping even if socket I/O is possible for
170  * some of the specified FD.  The idea is based on the observation that it's
171  * likely for a busy server to keep receiving packets.  It specifically works
172  * as follows: the socket watcher is first initialized with the state of
173  * "poll_idle".  While it's in the idle state it keeps sleeping until a socket
174  * event occurs.  When it wakes up for a socket I/O event, it moves to the
175  * poll_active state, and sets the poll timeout to a short period
176  * (ISC_SOCKET_POLLWATCH_TIMEOUT msec).  If timeout occurs in this state, the
177  * watcher goes to the poll_checking state with the same timeout period.
178  * In this state, the watcher tries to detect whether this is a break
179  * during intermittent events or the kernel bug is triggered.  If the next
180  * polling reports an event within the short period, the previous timeout is
181  * likely to be a kernel bug, and so the watcher goes back to the active state.
182  * Otherwise, it moves to the idle state again.
183  *
184  * It's not clear whether this is a thread-related bug, but since we've only
185  * seen this with threads, this workaround is used only when enabling threads.
186  */
187
188 typedef enum { poll_idle, poll_active, poll_checking } pollstate_t;
189
190 #ifndef ISC_SOCKET_POLLWATCH_TIMEOUT
191 #define ISC_SOCKET_POLLWATCH_TIMEOUT 10
192 #endif  /* ISC_SOCKET_POLLWATCH_TIMEOUT */
193 #endif  /* ISC_SOCKET_USE_POLLWATCH */
194
195 /*%
196  * Size of per-FD lock buckets.
197  */
198 #ifdef ISC_PLATFORM_USETHREADS
199 #define FDLOCK_COUNT            1024
200 #define FDLOCK_ID(fd)           ((fd) % FDLOCK_COUNT)
201 #else
202 #define FDLOCK_COUNT            1
203 #define FDLOCK_ID(fd)           0
204 #endif  /* ISC_PLATFORM_USETHREADS */
205
206 /*%
207  * Maximum number of events communicated with the kernel.  There should normally
208  * be no need for having a large number.
209  */
210 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
211 #ifndef ISC_SOCKET_MAXEVENTS
212 #define ISC_SOCKET_MAXEVENTS    64
213 #endif
214 #endif
215
216 /*%
217  * Some systems define the socket length argument as an int, some as size_t,
218  * some as socklen_t.  This is here so it can be easily changed if needed.
219  */
220 #ifndef ISC_SOCKADDR_LEN_T
221 #define ISC_SOCKADDR_LEN_T unsigned int
222 #endif
223
224 /*%
225  * Define what the possible "soft" errors can be.  These are non-fatal returns
226  * of various network related functions, like recv() and so on.
227  *
228  * For some reason, BSDI (and perhaps others) will sometimes return <0
229  * from recv() but will have errno==0.  This is broken, but we have to
230  * work around it here.
231  */
232 #define SOFT_ERROR(e)   ((e) == EAGAIN || \
233                          (e) == EWOULDBLOCK || \
234                          (e) == EINTR || \
235                          (e) == 0)
236
237 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
238
239 /*!<
240  * DLVL(90)  --  Function entry/exit and other tracing.
241  * DLVL(70)  --  Socket "correctness" -- including returning of events, etc.
242  * DLVL(60)  --  Socket data send/receive
243  * DLVL(50)  --  Event tracing, including receiving/sending completion events.
244  * DLVL(20)  --  Socket creation/destruction.
245  */
246 #define TRACE_LEVEL             90
247 #define CORRECTNESS_LEVEL       70
248 #define IOEVENT_LEVEL           60
249 #define EVENT_LEVEL             50
250 #define CREATION_LEVEL          20
251
252 #define TRACE           DLVL(TRACE_LEVEL)
253 #define CORRECTNESS     DLVL(CORRECTNESS_LEVEL)
254 #define IOEVENT         DLVL(IOEVENT_LEVEL)
255 #define EVENT           DLVL(EVENT_LEVEL)
256 #define CREATION        DLVL(CREATION_LEVEL)
257
258 typedef isc_event_t intev_t;
259
260 #define SOCKET_MAGIC            ISC_MAGIC('I', 'O', 'i', 'o')
261 #define VALID_SOCKET(s)         ISC_MAGIC_VALID(s, SOCKET_MAGIC)
262
263 /*!
264  * IPv6 control information.  If the socket is an IPv6 socket we want
265  * to collect the destination address and interface so the client can
266  * set them on outgoing packets.
267  */
268 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
269 #ifndef USE_CMSG
270 #define USE_CMSG        1
271 #endif
272 #endif
273
274 /*%
275  * NetBSD and FreeBSD can timestamp packets.  XXXMLG Should we have
276  * a setsockopt() like interface to request timestamps, and if the OS
277  * doesn't do it for us, call gettimeofday() on every UDP receive?
278  */
279 #ifdef SO_TIMESTAMP
280 #ifndef USE_CMSG
281 #define USE_CMSG        1
282 #endif
283 #endif
284
285 /*%
286  * The size to raise the receive buffer to (from BIND 8).
287  */
288 #define RCVBUFSIZE (32*1024)
289
290 /*%
291  * The number of times a send operation is repeated if the result is EINTR.
292  */
293 #define NRETRIES 10
294
295 typedef struct isc__socket isc__socket_t;
296 typedef struct isc__socketmgr isc__socketmgr_t;
297
298 #define NEWCONNSOCK(ev) ((isc__socket_t *)(ev)->newsocket)
299
300 struct isc__socket {
301         /* Not locked. */
302         isc_socket_t            common;
303         isc__socketmgr_t        *manager;
304         isc_mutex_t             lock;
305         isc_sockettype_t        type;
306         const isc_statscounter_t        *statsindex;
307
308         /* Locked by socket lock. */
309         ISC_LINK(isc__socket_t) link;
310         unsigned int            references;
311         int                     fd;
312         int                     pf;
313         char                            name[16];
314         void *                          tag;
315
316         ISC_LIST(isc_socketevent_t)             send_list;
317         ISC_LIST(isc_socketevent_t)             recv_list;
318         ISC_LIST(isc_socket_newconnev_t)        accept_list;
319         isc_socket_connev_t                    *connect_ev;
320
321         /*
322          * Internal events.  Posted when a descriptor is readable or
323          * writable.  These are statically allocated and never freed.
324          * They will be set to non-purgable before use.
325          */
326         intev_t                 readable_ev;
327         intev_t                 writable_ev;
328
329         isc_sockaddr_t          peer_address;  /* remote address */
330
331         unsigned int            pending_recv : 1,
332                                 pending_send : 1,
333                                 pending_accept : 1,
334                                 listener : 1, /* listener socket */
335                                 connected : 1,
336                                 connecting : 1, /* connect pending */
337                                 bound : 1, /* bound to local addr */
338                                 dupped : 1;
339
340 #ifdef ISC_NET_RECVOVERFLOW
341         unsigned char           overflow; /* used for MSG_TRUNC fake */
342 #endif
343
344         char                    *recvcmsgbuf;
345         ISC_SOCKADDR_LEN_T      recvcmsgbuflen;
346         char                    *sendcmsgbuf;
347         ISC_SOCKADDR_LEN_T      sendcmsgbuflen;
348
349         void                    *fdwatcharg;
350         isc_sockfdwatch_t       fdwatchcb;
351         int                     fdwatchflags;
352         isc_task_t              *fdwatchtask;
353 };
354
355 #define SOCKET_MANAGER_MAGIC    ISC_MAGIC('I', 'O', 'm', 'g')
356 #define VALID_MANAGER(m)        ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
357
358 struct isc__socketmgr {
359         /* Not locked. */
360         isc_socketmgr_t         common;
361         isc_mem_t              *mctx;
362         isc_mutex_t             lock;
363         isc_mutex_t             *fdlock;
364         isc_stats_t             *stats;
365 #ifdef USE_KQUEUE
366         int                     kqueue_fd;
367         int                     nevents;
368         struct kevent           *events;
369 #endif  /* USE_KQUEUE */
370 #ifdef USE_EPOLL
371         int                     epoll_fd;
372         int                     nevents;
373         struct epoll_event      *events;
374 #endif  /* USE_EPOLL */
375 #ifdef USE_DEVPOLL
376         int                     devpoll_fd;
377         int                     nevents;
378         struct pollfd           *events;
379 #endif  /* USE_DEVPOLL */
380 #ifdef USE_SELECT
381         int                     fd_bufsize;
382 #endif  /* USE_SELECT */
383         unsigned int            maxsocks;
384 #ifdef ISC_PLATFORM_USETHREADS
385         int                     pipe_fds[2];
386 #endif
387
388         /* Locked by fdlock. */
389         isc__socket_t          **fds;
390         int                     *fdstate;
391 #ifdef USE_DEVPOLL
392         pollinfo_t              *fdpollinfo;
393 #endif
394
395         /* Locked by manager lock. */
396         ISC_LIST(isc__socket_t) socklist;
397 #ifdef USE_SELECT
398         fd_set                  *read_fds;
399         fd_set                  *read_fds_copy;
400         fd_set                  *write_fds;
401         fd_set                  *write_fds_copy;
402         int                     maxfd;
403 #endif  /* USE_SELECT */
404         int                     reserved;       /* unlocked */
405 #ifdef USE_WATCHER_THREAD
406         isc_thread_t            watcher;
407         isc_condition_t         shutdown_ok;
408 #else /* USE_WATCHER_THREAD */
409         unsigned int            refs;
410 #endif /* USE_WATCHER_THREAD */
411         int                     maxudp;
412 };
413
414 #ifdef USE_SHARED_MANAGER
415 static isc__socketmgr_t *socketmgr = NULL;
416 #endif /* USE_SHARED_MANAGER */
417
418 #define CLOSED                  0       /* this one must be zero */
419 #define MANAGED                 1
420 #define CLOSE_PENDING           2
421
422 /*
423  * send() and recv() iovec counts
424  */
425 #define MAXSCATTERGATHER_SEND   (ISC_SOCKET_MAXSCATTERGATHER)
426 #ifdef ISC_NET_RECVOVERFLOW
427 # define MAXSCATTERGATHER_RECV  (ISC_SOCKET_MAXSCATTERGATHER + 1)
428 #else
429 # define MAXSCATTERGATHER_RECV  (ISC_SOCKET_MAXSCATTERGATHER)
430 #endif
431
432 static isc_result_t socket_create(isc_socketmgr_t *manager0, int pf,
433                                   isc_sockettype_t type,
434                                   isc_socket_t **socketp,
435                                   isc_socket_t *dup_socket);
436 static void send_recvdone_event(isc__socket_t *, isc_socketevent_t **);
437 static void send_senddone_event(isc__socket_t *, isc_socketevent_t **);
438 static void free_socket(isc__socket_t **);
439 static isc_result_t allocate_socket(isc__socketmgr_t *, isc_sockettype_t,
440                                     isc__socket_t **);
441 static void destroy(isc__socket_t **);
442 static void internal_accept(isc_task_t *, isc_event_t *);
443 static void internal_connect(isc_task_t *, isc_event_t *);
444 static void internal_recv(isc_task_t *, isc_event_t *);
445 static void internal_send(isc_task_t *, isc_event_t *);
446 static void internal_fdwatch_write(isc_task_t *, isc_event_t *);
447 static void internal_fdwatch_read(isc_task_t *, isc_event_t *);
448 static void process_cmsg(isc__socket_t *, struct msghdr *, isc_socketevent_t *);
449 static void build_msghdr_send(isc__socket_t *, isc_socketevent_t *,
450                               struct msghdr *, struct iovec *, size_t *);
451 static void build_msghdr_recv(isc__socket_t *, isc_socketevent_t *,
452                               struct msghdr *, struct iovec *, size_t *);
453 #ifdef USE_WATCHER_THREAD
454 static isc_boolean_t process_ctlfd(isc__socketmgr_t *manager);
455 #endif
456
457 /*%
458  * The following can be either static or public, depending on build environment.
459  */
460
461 #ifdef BIND9
462 #define ISC_SOCKETFUNC_SCOPE
463 #else
464 #define ISC_SOCKETFUNC_SCOPE static
465 #endif
466
467 ISC_SOCKETFUNC_SCOPE isc_result_t
468 isc__socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
469                    isc_socket_t **socketp);
470 ISC_SOCKETFUNC_SCOPE void
471 isc__socket_attach(isc_socket_t *sock, isc_socket_t **socketp);
472 ISC_SOCKETFUNC_SCOPE void
473 isc__socket_detach(isc_socket_t **socketp);
474 ISC_SOCKETFUNC_SCOPE isc_result_t
475 isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
476 ISC_SOCKETFUNC_SCOPE isc_result_t
477 isc__socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
478                        unsigned int maxsocks);
479 ISC_SOCKETFUNC_SCOPE void
480 isc__socketmgr_destroy(isc_socketmgr_t **managerp);
481 ISC_SOCKETFUNC_SCOPE isc_result_t
482 isc__socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
483                  unsigned int minimum, isc_task_t *task,
484                   isc_taskaction_t action, const void *arg);
485 ISC_SOCKETFUNC_SCOPE isc_result_t
486 isc__socket_recv(isc_socket_t *sock, isc_region_t *region,
487                  unsigned int minimum, isc_task_t *task,
488                  isc_taskaction_t action, const void *arg);
489 ISC_SOCKETFUNC_SCOPE isc_result_t
490 isc__socket_recv2(isc_socket_t *sock, isc_region_t *region,
491                   unsigned int minimum, isc_task_t *task,
492                   isc_socketevent_t *event, unsigned int flags);
493 ISC_SOCKETFUNC_SCOPE isc_result_t
494 isc__socket_send(isc_socket_t *sock, isc_region_t *region,
495                  isc_task_t *task, isc_taskaction_t action, const void *arg);
496 ISC_SOCKETFUNC_SCOPE isc_result_t
497 isc__socket_sendto(isc_socket_t *sock, isc_region_t *region,
498                    isc_task_t *task, isc_taskaction_t action, const void *arg,
499                    isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
500 ISC_SOCKETFUNC_SCOPE isc_result_t
501 isc__socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
502                   isc_task_t *task, isc_taskaction_t action, const void *arg);
503 ISC_SOCKETFUNC_SCOPE isc_result_t
504 isc__socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
505                     isc_task_t *task, isc_taskaction_t action, const void *arg,
506                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
507 ISC_SOCKETFUNC_SCOPE isc_result_t
508 isc__socket_sendto2(isc_socket_t *sock, isc_region_t *region,
509                     isc_task_t *task,
510                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
511                     isc_socketevent_t *event, unsigned int flags);
512 ISC_SOCKETFUNC_SCOPE void
513 isc__socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active);
514 ISC_SOCKETFUNC_SCOPE isc_result_t
515 isc__socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
516                      isc_uint32_t owner, isc_uint32_t group);
517 ISC_SOCKETFUNC_SCOPE isc_result_t
518 isc__socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
519                  unsigned int options);
520 ISC_SOCKETFUNC_SCOPE isc_result_t
521 isc__socket_filter(isc_socket_t *sock, const char *filter);
522 ISC_SOCKETFUNC_SCOPE isc_result_t
523 isc__socket_listen(isc_socket_t *sock, unsigned int backlog);
524 ISC_SOCKETFUNC_SCOPE isc_result_t
525 isc__socket_accept(isc_socket_t *sock,
526                    isc_task_t *task, isc_taskaction_t action, const void *arg);
527 ISC_SOCKETFUNC_SCOPE isc_result_t
528 isc__socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
529                     isc_task_t *task, isc_taskaction_t action,
530                     const void *arg);
531 ISC_SOCKETFUNC_SCOPE isc_result_t
532 isc__socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp);
533 ISC_SOCKETFUNC_SCOPE isc_result_t
534 isc__socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp);
535 ISC_SOCKETFUNC_SCOPE void
536 isc__socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how);
537 ISC_SOCKETFUNC_SCOPE isc_sockettype_t
538 isc__socket_gettype(isc_socket_t *sock);
539 ISC_SOCKETFUNC_SCOPE isc_boolean_t
540 isc__socket_isbound(isc_socket_t *sock);
541 ISC_SOCKETFUNC_SCOPE void
542 isc__socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes);
543 #if defined(HAVE_LIBXML2) && defined(BIND9)
544 ISC_SOCKETFUNC_SCOPE void
545 isc__socketmgr_renderxml(isc_socketmgr_t *mgr0, xmlTextWriterPtr writer);
546 #endif
547
548 ISC_SOCKETFUNC_SCOPE isc_result_t
549 isc__socket_fdwatchcreate(isc_socketmgr_t *manager, int fd, int flags,
550                           isc_sockfdwatch_t callback, void *cbarg,
551                           isc_task_t *task, isc_socket_t **socketp);
552 ISC_SOCKETFUNC_SCOPE isc_result_t
553 isc__socket_fdwatchpoke(isc_socket_t *sock, int flags);
554 ISC_SOCKETFUNC_SCOPE isc_result_t
555 isc__socket_dup(isc_socket_t *sock, isc_socket_t **socketp);
556 ISC_SOCKETFUNC_SCOPE int
557 isc__socket_getfd(isc_socket_t *sock);
558
559 static struct {
560         isc_socketmethods_t methods;
561
562         /*%
563          * The following are defined just for avoiding unused static functions.
564          */
565 #ifndef BIND9
566         void *recvv, *send, *sendv, *sendto2, *cleanunix, *permunix, *filter,
567                 *listen, *accept, *getpeername, *isbound;
568 #endif
569 } socketmethods = {
570         {
571                 isc__socket_attach,
572                 isc__socket_detach,
573                 isc__socket_bind,
574                 isc__socket_sendto,
575                 isc__socket_sendto2,
576                 isc__socket_connect,
577                 isc__socket_recv,
578                 isc__socket_recv2,
579                 isc__socket_cancel,
580                 isc__socket_getsockname,
581                 isc__socket_gettype,
582                 isc__socket_ipv6only,
583                 isc__socket_fdwatchpoke,
584                 isc__socket_dup,
585                 isc__socket_getfd
586         }
587 #ifndef BIND9
588         ,
589         (void *)isc__socket_recvv, (void *)isc__socket_send,
590         (void *)isc__socket_sendv, (void *)isc__socket_sendto2,
591         (void *)isc__socket_cleanunix, (void *)isc__socket_permunix,
592         (void *)isc__socket_filter, (void *)isc__socket_listen,
593         (void *)isc__socket_accept, (void *)isc__socket_getpeername,
594         (void *)isc__socket_isbound
595 #endif
596 };
597
598 static isc_socketmgrmethods_t socketmgrmethods = {
599         isc__socketmgr_destroy,
600         isc__socket_create,
601         isc__socket_fdwatchcreate
602 };
603
604 #define SELECT_POKE_SHUTDOWN            (-1)
605 #define SELECT_POKE_NOTHING             (-2)
606 #define SELECT_POKE_READ                (-3)
607 #define SELECT_POKE_ACCEPT              (-3) /*%< Same as _READ */
608 #define SELECT_POKE_WRITE               (-4)
609 #define SELECT_POKE_CONNECT             (-4) /*%< Same as _WRITE */
610 #define SELECT_POKE_CLOSE               (-5)
611
612 #define SOCK_DEAD(s)                    ((s)->references == 0)
613
614 /*%
615  * Shortcut index arrays to get access to statistics counters.
616  */
617 enum {
618         STATID_OPEN = 0,
619         STATID_OPENFAIL = 1,
620         STATID_CLOSE = 2,
621         STATID_BINDFAIL = 3,
622         STATID_CONNECTFAIL = 4,
623         STATID_CONNECT = 5,
624         STATID_ACCEPTFAIL = 6,
625         STATID_ACCEPT = 7,
626         STATID_SENDFAIL = 8,
627         STATID_RECVFAIL = 9
628 };
629 static const isc_statscounter_t udp4statsindex[] = {
630         isc_sockstatscounter_udp4open,
631         isc_sockstatscounter_udp4openfail,
632         isc_sockstatscounter_udp4close,
633         isc_sockstatscounter_udp4bindfail,
634         isc_sockstatscounter_udp4connectfail,
635         isc_sockstatscounter_udp4connect,
636         -1,
637         -1,
638         isc_sockstatscounter_udp4sendfail,
639         isc_sockstatscounter_udp4recvfail
640 };
641 static const isc_statscounter_t udp6statsindex[] = {
642         isc_sockstatscounter_udp6open,
643         isc_sockstatscounter_udp6openfail,
644         isc_sockstatscounter_udp6close,
645         isc_sockstatscounter_udp6bindfail,
646         isc_sockstatscounter_udp6connectfail,
647         isc_sockstatscounter_udp6connect,
648         -1,
649         -1,
650         isc_sockstatscounter_udp6sendfail,
651         isc_sockstatscounter_udp6recvfail
652 };
653 static const isc_statscounter_t tcp4statsindex[] = {
654         isc_sockstatscounter_tcp4open,
655         isc_sockstatscounter_tcp4openfail,
656         isc_sockstatscounter_tcp4close,
657         isc_sockstatscounter_tcp4bindfail,
658         isc_sockstatscounter_tcp4connectfail,
659         isc_sockstatscounter_tcp4connect,
660         isc_sockstatscounter_tcp4acceptfail,
661         isc_sockstatscounter_tcp4accept,
662         isc_sockstatscounter_tcp4sendfail,
663         isc_sockstatscounter_tcp4recvfail
664 };
665 static const isc_statscounter_t tcp6statsindex[] = {
666         isc_sockstatscounter_tcp6open,
667         isc_sockstatscounter_tcp6openfail,
668         isc_sockstatscounter_tcp6close,
669         isc_sockstatscounter_tcp6bindfail,
670         isc_sockstatscounter_tcp6connectfail,
671         isc_sockstatscounter_tcp6connect,
672         isc_sockstatscounter_tcp6acceptfail,
673         isc_sockstatscounter_tcp6accept,
674         isc_sockstatscounter_tcp6sendfail,
675         isc_sockstatscounter_tcp6recvfail
676 };
677 static const isc_statscounter_t unixstatsindex[] = {
678         isc_sockstatscounter_unixopen,
679         isc_sockstatscounter_unixopenfail,
680         isc_sockstatscounter_unixclose,
681         isc_sockstatscounter_unixbindfail,
682         isc_sockstatscounter_unixconnectfail,
683         isc_sockstatscounter_unixconnect,
684         isc_sockstatscounter_unixacceptfail,
685         isc_sockstatscounter_unixaccept,
686         isc_sockstatscounter_unixsendfail,
687         isc_sockstatscounter_unixrecvfail
688 };
689 static const isc_statscounter_t fdwatchstatsindex[] = {
690         -1,
691         -1,
692         isc_sockstatscounter_fdwatchclose,
693         isc_sockstatscounter_fdwatchbindfail,
694         isc_sockstatscounter_fdwatchconnectfail,
695         isc_sockstatscounter_fdwatchconnect,
696         -1,
697         -1,
698         isc_sockstatscounter_fdwatchsendfail,
699         isc_sockstatscounter_fdwatchrecvfail
700 };
701
702 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL) || \
703     defined(USE_WATCHER_THREAD)
704 static void
705 manager_log(isc__socketmgr_t *sockmgr,
706             isc_logcategory_t *category, isc_logmodule_t *module, int level,
707             const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
708 static void
709 manager_log(isc__socketmgr_t *sockmgr,
710             isc_logcategory_t *category, isc_logmodule_t *module, int level,
711             const char *fmt, ...)
712 {
713         char msgbuf[2048];
714         va_list ap;
715
716         if (! isc_log_wouldlog(isc_lctx, level))
717                 return;
718
719         va_start(ap, fmt);
720         vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
721         va_end(ap);
722
723         isc_log_write(isc_lctx, category, module, level,
724                       "sockmgr %p: %s", sockmgr, msgbuf);
725 }
726 #endif
727
728 static void
729 socket_log(isc__socket_t *sock, isc_sockaddr_t *address,
730            isc_logcategory_t *category, isc_logmodule_t *module, int level,
731            isc_msgcat_t *msgcat, int msgset, int message,
732            const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
733 static void
734 socket_log(isc__socket_t *sock, isc_sockaddr_t *address,
735            isc_logcategory_t *category, isc_logmodule_t *module, int level,
736            isc_msgcat_t *msgcat, int msgset, int message,
737            const char *fmt, ...)
738 {
739         char msgbuf[2048];
740         char peerbuf[ISC_SOCKADDR_FORMATSIZE];
741         va_list ap;
742
743         if (! isc_log_wouldlog(isc_lctx, level))
744                 return;
745
746         va_start(ap, fmt);
747         vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
748         va_end(ap);
749
750         if (address == NULL) {
751                 isc_log_iwrite(isc_lctx, category, module, level,
752                                msgcat, msgset, message,
753                                "socket %p: %s", sock, msgbuf);
754         } else {
755                 isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
756                 isc_log_iwrite(isc_lctx, category, module, level,
757                                msgcat, msgset, message,
758                                "socket %p %s: %s", sock, peerbuf, msgbuf);
759         }
760 }
761
762 #if defined(_AIX) && defined(ISC_NET_BSD44MSGHDR) && \
763     defined(USE_CMSG) && defined(IPV6_RECVPKTINFO)
764 /*
765  * AIX has a kernel bug where IPV6_RECVPKTINFO gets cleared by
766  * setting IPV6_V6ONLY.
767  */
768 static void
769 FIX_IPV6_RECVPKTINFO(isc__socket_t *sock)
770 {
771         char strbuf[ISC_STRERRORSIZE];
772         int on = 1;
773
774         if (sock->pf != AF_INET6 || sock->type != isc_sockettype_udp)
775                 return;
776
777         if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
778                        (void *)&on, sizeof(on)) < 0) {
779
780                 isc__strerror(errno, strbuf, sizeof(strbuf));
781                 UNEXPECTED_ERROR(__FILE__, __LINE__,
782                                  "setsockopt(%d, IPV6_RECVPKTINFO) "
783                                  "%s: %s", sock->fd,
784                                  isc_msgcat_get(isc_msgcat,
785                                                 ISC_MSGSET_GENERAL,
786                                                 ISC_MSG_FAILED,
787                                                 "failed"),
788                                  strbuf);
789         }
790 }
791 #else
792 #define FIX_IPV6_RECVPKTINFO(sock) (void)0
793 #endif
794
795 /*%
796  * Increment socket-related statistics counters.
797  */
798 static inline void
799 inc_stats(isc_stats_t *stats, isc_statscounter_t counterid) {
800         REQUIRE(counterid != -1);
801
802         if (stats != NULL)
803                 isc_stats_increment(stats, counterid);
804 }
805
806 static inline isc_result_t
807 watch_fd(isc__socketmgr_t *manager, int fd, int msg) {
808         isc_result_t result = ISC_R_SUCCESS;
809
810 #ifdef USE_KQUEUE
811         struct kevent evchange;
812
813         memset(&evchange, 0, sizeof(evchange));
814         if (msg == SELECT_POKE_READ)
815                 evchange.filter = EVFILT_READ;
816         else
817                 evchange.filter = EVFILT_WRITE;
818         evchange.flags = EV_ADD;
819         evchange.ident = fd;
820         if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
821                 result = isc__errno2result(errno);
822
823         return (result);
824 #elif defined(USE_EPOLL)
825         struct epoll_event event;
826
827         if (msg == SELECT_POKE_READ)
828                 event.events = EPOLLIN;
829         else
830                 event.events = EPOLLOUT;
831         memset(&event.data, 0, sizeof(event.data));
832         event.data.fd = fd;
833         if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1 &&
834             errno != EEXIST) {
835                 result = isc__errno2result(errno);
836         }
837
838         return (result);
839 #elif defined(USE_DEVPOLL)
840         struct pollfd pfd;
841         int lockid = FDLOCK_ID(fd);
842
843         memset(&pfd, 0, sizeof(pfd));
844         if (msg == SELECT_POKE_READ)
845                 pfd.events = POLLIN;
846         else
847                 pfd.events = POLLOUT;
848         pfd.fd = fd;
849         pfd.revents = 0;
850         LOCK(&manager->fdlock[lockid]);
851         if (write(manager->devpoll_fd, &pfd, sizeof(pfd)) == -1)
852                 result = isc__errno2result(errno);
853         else {
854                 if (msg == SELECT_POKE_READ)
855                         manager->fdpollinfo[fd].want_read = 1;
856                 else
857                         manager->fdpollinfo[fd].want_write = 1;
858         }
859         UNLOCK(&manager->fdlock[lockid]);
860
861         return (result);
862 #elif defined(USE_SELECT)
863         LOCK(&manager->lock);
864         if (msg == SELECT_POKE_READ)
865                 FD_SET(fd, manager->read_fds);
866         if (msg == SELECT_POKE_WRITE)
867                 FD_SET(fd, manager->write_fds);
868         UNLOCK(&manager->lock);
869
870         return (result);
871 #endif
872 }
873
874 static inline isc_result_t
875 unwatch_fd(isc__socketmgr_t *manager, int fd, int msg) {
876         isc_result_t result = ISC_R_SUCCESS;
877
878 #ifdef USE_KQUEUE
879         struct kevent evchange;
880
881         memset(&evchange, 0, sizeof(evchange));
882         if (msg == SELECT_POKE_READ)
883                 evchange.filter = EVFILT_READ;
884         else
885                 evchange.filter = EVFILT_WRITE;
886         evchange.flags = EV_DELETE;
887         evchange.ident = fd;
888         if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
889                 result = isc__errno2result(errno);
890
891         return (result);
892 #elif defined(USE_EPOLL)
893         struct epoll_event event;
894
895         if (msg == SELECT_POKE_READ)
896                 event.events = EPOLLIN;
897         else
898                 event.events = EPOLLOUT;
899         memset(&event.data, 0, sizeof(event.data));
900         event.data.fd = fd;
901         if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_DEL, fd, &event) == -1 &&
902             errno != ENOENT) {
903                 char strbuf[ISC_STRERRORSIZE];
904                 isc__strerror(errno, strbuf, sizeof(strbuf));
905                 UNEXPECTED_ERROR(__FILE__, __LINE__,
906                                  "epoll_ctl(DEL), %d: %s", fd, strbuf);
907                 result = ISC_R_UNEXPECTED;
908         }
909         return (result);
910 #elif defined(USE_DEVPOLL)
911         struct pollfd pfds[2];
912         size_t writelen = sizeof(pfds[0]);
913         int lockid = FDLOCK_ID(fd);
914
915         memset(pfds, 0, sizeof(pfds));
916         pfds[0].events = POLLREMOVE;
917         pfds[0].fd = fd;
918
919         /*
920          * Canceling read or write polling via /dev/poll is tricky.  Since it
921          * only provides a way of canceling per FD, we may need to re-poll the
922          * socket for the other operation.
923          */
924         LOCK(&manager->fdlock[lockid]);
925         if (msg == SELECT_POKE_READ &&
926             manager->fdpollinfo[fd].want_write == 1) {
927                 pfds[1].events = POLLOUT;
928                 pfds[1].fd = fd;
929                 writelen += sizeof(pfds[1]);
930         }
931         if (msg == SELECT_POKE_WRITE &&
932             manager->fdpollinfo[fd].want_read == 1) {
933                 pfds[1].events = POLLIN;
934                 pfds[1].fd = fd;
935                 writelen += sizeof(pfds[1]);
936         }
937
938         if (write(manager->devpoll_fd, pfds, writelen) == -1)
939                 result = isc__errno2result(errno);
940         else {
941                 if (msg == SELECT_POKE_READ)
942                         manager->fdpollinfo[fd].want_read = 0;
943                 else
944                         manager->fdpollinfo[fd].want_write = 0;
945         }
946         UNLOCK(&manager->fdlock[lockid]);
947
948         return (result);
949 #elif defined(USE_SELECT)
950         LOCK(&manager->lock);
951         if (msg == SELECT_POKE_READ)
952                 FD_CLR(fd, manager->read_fds);
953         else if (msg == SELECT_POKE_WRITE)
954                 FD_CLR(fd, manager->write_fds);
955         UNLOCK(&manager->lock);
956
957         return (result);
958 #endif
959 }
960
961 static void
962 wakeup_socket(isc__socketmgr_t *manager, int fd, int msg) {
963         isc_result_t result;
964         int lockid = FDLOCK_ID(fd);
965
966         /*
967          * This is a wakeup on a socket.  If the socket is not in the
968          * process of being closed, start watching it for either reads
969          * or writes.
970          */
971
972         INSIST(fd >= 0 && fd < (int)manager->maxsocks);
973
974         if (msg == SELECT_POKE_CLOSE) {
975                 /* No one should be updating fdstate, so no need to lock it */
976                 INSIST(manager->fdstate[fd] == CLOSE_PENDING);
977                 manager->fdstate[fd] = CLOSED;
978                 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
979                 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
980                 (void)close(fd);
981                 return;
982         }
983
984         LOCK(&manager->fdlock[lockid]);
985         if (manager->fdstate[fd] == CLOSE_PENDING) {
986                 UNLOCK(&manager->fdlock[lockid]);
987
988                 /*
989                  * We accept (and ignore) any error from unwatch_fd() as we are
990                  * closing the socket, hoping it doesn't leave dangling state in
991                  * the kernel.
992                  * Note that unwatch_fd() must be called after releasing the
993                  * fdlock; otherwise it could cause deadlock due to a lock order
994                  * reversal.
995                  */
996                 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
997                 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
998                 return;
999         }
1000         if (manager->fdstate[fd] != MANAGED) {
1001                 UNLOCK(&manager->fdlock[lockid]);
1002                 return;
1003         }
1004         UNLOCK(&manager->fdlock[lockid]);
1005
1006         /*
1007          * Set requested bit.
1008          */
1009         result = watch_fd(manager, fd, msg);
1010         if (result != ISC_R_SUCCESS) {
1011                 /*
1012                  * XXXJT: what should we do?  Ignoring the failure of watching
1013                  * a socket will make the application dysfunctional, but there
1014                  * seems to be no reasonable recovery process.
1015                  */
1016                 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
1017                               ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
1018                               "failed to start watching FD (%d): %s",
1019                               fd, isc_result_totext(result));
1020         }
1021 }
1022
1023 #ifdef USE_WATCHER_THREAD
1024 /*
1025  * Poke the select loop when there is something for us to do.
1026  * The write is required (by POSIX) to complete.  That is, we
1027  * will not get partial writes.
1028  */
1029 static void
1030 select_poke(isc__socketmgr_t *mgr, int fd, int msg) {
1031         int cc;
1032         int buf[2];
1033         char strbuf[ISC_STRERRORSIZE];
1034
1035         buf[0] = fd;
1036         buf[1] = msg;
1037
1038         do {
1039                 cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
1040 #ifdef ENOSR
1041                 /*
1042                  * Treat ENOSR as EAGAIN but loop slowly as it is
1043                  * unlikely to clear fast.
1044                  */
1045                 if (cc < 0 && errno == ENOSR) {
1046                         sleep(1);
1047                         errno = EAGAIN;
1048                 }
1049 #endif
1050         } while (cc < 0 && SOFT_ERROR(errno));
1051
1052         if (cc < 0) {
1053                 isc__strerror(errno, strbuf, sizeof(strbuf));
1054                 FATAL_ERROR(__FILE__, __LINE__,
1055                             isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
1056                                            ISC_MSG_WRITEFAILED,
1057                                            "write() failed "
1058                                            "during watcher poke: %s"),
1059                             strbuf);
1060         }
1061
1062         INSIST(cc == sizeof(buf));
1063 }
1064
1065 /*
1066  * Read a message on the internal fd.
1067  */
1068 static void
1069 select_readmsg(isc__socketmgr_t *mgr, int *fd, int *msg) {
1070         int buf[2];
1071         int cc;
1072         char strbuf[ISC_STRERRORSIZE];
1073
1074         cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
1075         if (cc < 0) {
1076                 *msg = SELECT_POKE_NOTHING;
1077                 *fd = -1;       /* Silence compiler. */
1078                 if (SOFT_ERROR(errno))
1079                         return;
1080
1081                 isc__strerror(errno, strbuf, sizeof(strbuf));
1082                 FATAL_ERROR(__FILE__, __LINE__,
1083                             isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
1084                                            ISC_MSG_READFAILED,
1085                                            "read() failed "
1086                                            "during watcher poke: %s"),
1087                             strbuf);
1088
1089                 return;
1090         }
1091         INSIST(cc == sizeof(buf));
1092
1093         *fd = buf[0];
1094         *msg = buf[1];
1095 }
1096 #else /* USE_WATCHER_THREAD */
1097 /*
1098  * Update the state of the socketmgr when something changes.
1099  */
1100 static void
1101 select_poke(isc__socketmgr_t *manager, int fd, int msg) {
1102         if (msg == SELECT_POKE_SHUTDOWN)
1103                 return;
1104         else if (fd >= 0)
1105                 wakeup_socket(manager, fd, msg);
1106         return;
1107 }
1108 #endif /* USE_WATCHER_THREAD */
1109
1110 /*
1111  * Make a fd non-blocking.
1112  */
1113 static isc_result_t
1114 make_nonblock(int fd) {
1115         int ret;
1116         int flags;
1117         char strbuf[ISC_STRERRORSIZE];
1118 #ifdef USE_FIONBIO_IOCTL
1119         int on = 1;
1120
1121         ret = ioctl(fd, FIONBIO, (char *)&on);
1122 #else
1123         flags = fcntl(fd, F_GETFL, 0);
1124         flags |= PORT_NONBLOCK;
1125         ret = fcntl(fd, F_SETFL, flags);
1126 #endif
1127
1128         if (ret == -1) {
1129                 isc__strerror(errno, strbuf, sizeof(strbuf));
1130                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1131 #ifdef USE_FIONBIO_IOCTL
1132                                  "ioctl(%d, FIONBIO, &on): %s", fd,
1133 #else
1134                                  "fcntl(%d, F_SETFL, %d): %s", fd, flags,
1135 #endif
1136                                  strbuf);
1137
1138                 return (ISC_R_UNEXPECTED);
1139         }
1140
1141         return (ISC_R_SUCCESS);
1142 }
1143
1144 #ifdef USE_CMSG
1145 /*
1146  * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
1147  * In order to ensure as much portability as possible, we provide wrapper
1148  * functions of these macros.
1149  * Note that cmsg_space() could run slow on OSes that do not have
1150  * CMSG_SPACE.
1151  */
1152 static inline ISC_SOCKADDR_LEN_T
1153 cmsg_len(ISC_SOCKADDR_LEN_T len) {
1154 #ifdef CMSG_LEN
1155         return (CMSG_LEN(len));
1156 #else
1157         ISC_SOCKADDR_LEN_T hdrlen;
1158
1159         /*
1160          * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
1161          * is correct.
1162          */
1163         hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
1164         return (hdrlen + len);
1165 #endif
1166 }
1167
1168 static inline ISC_SOCKADDR_LEN_T
1169 cmsg_space(ISC_SOCKADDR_LEN_T len) {
1170 #ifdef CMSG_SPACE
1171         return (CMSG_SPACE(len));
1172 #else
1173         struct msghdr msg;
1174         struct cmsghdr *cmsgp;
1175         /*
1176          * XXX: The buffer length is an ad-hoc value, but should be enough
1177          * in a practical sense.
1178          */
1179         char dummybuf[sizeof(struct cmsghdr) + 1024];
1180
1181         memset(&msg, 0, sizeof(msg));
1182         msg.msg_control = dummybuf;
1183         msg.msg_controllen = sizeof(dummybuf);
1184
1185         cmsgp = (struct cmsghdr *)dummybuf;
1186         cmsgp->cmsg_len = cmsg_len(len);
1187
1188         cmsgp = CMSG_NXTHDR(&msg, cmsgp);
1189         if (cmsgp != NULL)
1190                 return ((char *)cmsgp - (char *)msg.msg_control);
1191         else
1192                 return (0);
1193 #endif
1194 }
1195 #endif /* USE_CMSG */
1196
1197 /*
1198  * Process control messages received on a socket.
1199  */
1200 static void
1201 process_cmsg(isc__socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
1202 #ifdef USE_CMSG
1203         struct cmsghdr *cmsgp;
1204 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1205         struct in6_pktinfo *pktinfop;
1206 #endif
1207 #ifdef SO_TIMESTAMP
1208         void *timevalp;
1209 #endif
1210 #endif
1211
1212         /*
1213          * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
1214          * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
1215          * They are all here, outside of the CPP tests, because it is
1216          * more consistent with the usual ISC coding style.
1217          */
1218         UNUSED(sock);
1219         UNUSED(msg);
1220         UNUSED(dev);
1221
1222 #ifdef ISC_NET_BSD44MSGHDR
1223
1224 #ifdef MSG_TRUNC
1225         if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
1226                 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
1227 #endif
1228
1229 #ifdef MSG_CTRUNC
1230         if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
1231                 dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
1232 #endif
1233
1234 #ifndef USE_CMSG
1235         return;
1236 #else
1237         if (msg->msg_controllen == 0U || msg->msg_control == NULL)
1238                 return;
1239
1240 #ifdef SO_TIMESTAMP
1241         timevalp = NULL;
1242 #endif
1243 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1244         pktinfop = NULL;
1245 #endif
1246
1247         cmsgp = CMSG_FIRSTHDR(msg);
1248         while (cmsgp != NULL) {
1249                 socket_log(sock, NULL, TRACE,
1250                            isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
1251                            "processing cmsg %p", cmsgp);
1252
1253 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1254                 if (cmsgp->cmsg_level == IPPROTO_IPV6
1255                     && cmsgp->cmsg_type == IPV6_PKTINFO) {
1256
1257                         pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
1258                         memcpy(&dev->pktinfo, pktinfop,
1259                                sizeof(struct in6_pktinfo));
1260                         dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
1261                         socket_log(sock, NULL, TRACE,
1262                                    isc_msgcat, ISC_MSGSET_SOCKET,
1263                                    ISC_MSG_IFRECEIVED,
1264                                    "interface received on ifindex %u",
1265                                    dev->pktinfo.ipi6_ifindex);
1266                         if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
1267                                 dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
1268                         goto next;
1269                 }
1270 #endif
1271
1272 #ifdef SO_TIMESTAMP
1273                 if (cmsgp->cmsg_level == SOL_SOCKET
1274                     && cmsgp->cmsg_type == SCM_TIMESTAMP) {
1275                         struct timeval tv;
1276                         timevalp = CMSG_DATA(cmsgp);
1277                         memcpy(&tv, timevalp, sizeof(tv));
1278                         dev->timestamp.seconds = tv.tv_sec;
1279                         dev->timestamp.nanoseconds = tv.tv_usec * 1000;
1280                         dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
1281                         goto next;
1282                 }
1283 #endif
1284
1285         next:
1286                 cmsgp = CMSG_NXTHDR(msg, cmsgp);
1287         }
1288 #endif /* USE_CMSG */
1289
1290 #endif /* ISC_NET_BSD44MSGHDR */
1291 }
1292
1293 /*
1294  * Construct an iov array and attach it to the msghdr passed in.  This is
1295  * the SEND constructor, which will use the used region of the buffer
1296  * (if using a buffer list) or will use the internal region (if a single
1297  * buffer I/O is requested).
1298  *
1299  * Nothing can be NULL, and the done event must list at least one buffer
1300  * on the buffer linked list for this function to be meaningful.
1301  *
1302  * If write_countp != NULL, *write_countp will hold the number of bytes
1303  * this transaction can send.
1304  */
1305 static void
1306 build_msghdr_send(isc__socket_t *sock, isc_socketevent_t *dev,
1307                   struct msghdr *msg, struct iovec *iov, size_t *write_countp)
1308 {
1309         unsigned int iovcount;
1310         isc_buffer_t *buffer;
1311         isc_region_t used;
1312         size_t write_count;
1313         size_t skip_count;
1314
1315         memset(msg, 0, sizeof(*msg));
1316
1317         if (!sock->connected) {
1318                 msg->msg_name = (void *)&dev->address.type.sa;
1319                 msg->msg_namelen = dev->address.length;
1320         } else {
1321                 msg->msg_name = NULL;
1322                 msg->msg_namelen = 0;
1323         }
1324
1325         buffer = ISC_LIST_HEAD(dev->bufferlist);
1326         write_count = 0;
1327         iovcount = 0;
1328
1329         /*
1330          * Single buffer I/O?  Skip what we've done so far in this region.
1331          */
1332         if (buffer == NULL) {
1333                 write_count = dev->region.length - dev->n;
1334                 iov[0].iov_base = (void *)(dev->region.base + dev->n);
1335                 iov[0].iov_len = write_count;
1336                 iovcount = 1;
1337
1338                 goto config;
1339         }
1340
1341         /*
1342          * Multibuffer I/O.
1343          * Skip the data in the buffer list that we have already written.
1344          */
1345         skip_count = dev->n;
1346         while (buffer != NULL) {
1347                 REQUIRE(ISC_BUFFER_VALID(buffer));
1348                 if (skip_count < isc_buffer_usedlength(buffer))
1349                         break;
1350                 skip_count -= isc_buffer_usedlength(buffer);
1351                 buffer = ISC_LIST_NEXT(buffer, link);
1352         }
1353
1354         while (buffer != NULL) {
1355                 INSIST(iovcount < MAXSCATTERGATHER_SEND);
1356
1357                 isc_buffer_usedregion(buffer, &used);
1358
1359                 if (used.length > 0) {
1360                         iov[iovcount].iov_base = (void *)(used.base
1361                                                           + skip_count);
1362                         iov[iovcount].iov_len = used.length - skip_count;
1363                         write_count += (used.length - skip_count);
1364                         skip_count = 0;
1365                         iovcount++;
1366                 }
1367                 buffer = ISC_LIST_NEXT(buffer, link);
1368         }
1369
1370         INSIST(skip_count == 0U);
1371
1372  config:
1373         msg->msg_iov = iov;
1374         msg->msg_iovlen = iovcount;
1375
1376 #ifdef ISC_NET_BSD44MSGHDR
1377         msg->msg_control = NULL;
1378         msg->msg_controllen = 0;
1379         msg->msg_flags = 0;
1380 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1381         if ((sock->type == isc_sockettype_udp)
1382             && ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
1383 #if defined(IPV6_USE_MIN_MTU)
1384                 int use_min_mtu = 1;    /* -1, 0, 1 */
1385 #endif
1386                 struct cmsghdr *cmsgp;
1387                 struct in6_pktinfo *pktinfop;
1388
1389                 socket_log(sock, NULL, TRACE,
1390                            isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
1391                            "sendto pktinfo data, ifindex %u",
1392                            dev->pktinfo.ipi6_ifindex);
1393
1394                 msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
1395                 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
1396                 msg->msg_control = (void *)sock->sendcmsgbuf;
1397
1398                 cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
1399                 cmsgp->cmsg_level = IPPROTO_IPV6;
1400                 cmsgp->cmsg_type = IPV6_PKTINFO;
1401                 cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
1402                 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
1403                 memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
1404 #if defined(IPV6_USE_MIN_MTU)
1405                 /*
1406                  * Set IPV6_USE_MIN_MTU as a per packet option as FreeBSD
1407                  * ignores setsockopt(IPV6_USE_MIN_MTU) when IPV6_PKTINFO
1408                  * is used.
1409                  */
1410                 cmsgp = (struct cmsghdr *)(sock->sendcmsgbuf +
1411                                            msg->msg_controllen);
1412                 msg->msg_controllen += cmsg_space(sizeof(use_min_mtu));
1413                 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
1414
1415                 cmsgp->cmsg_level = IPPROTO_IPV6;
1416                 cmsgp->cmsg_type = IPV6_USE_MIN_MTU;
1417                 cmsgp->cmsg_len = cmsg_len(sizeof(use_min_mtu));
1418                 memcpy(CMSG_DATA(cmsgp), &use_min_mtu, sizeof(use_min_mtu));
1419 #endif
1420         }
1421 #endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
1422 #else /* ISC_NET_BSD44MSGHDR */
1423         msg->msg_accrights = NULL;
1424         msg->msg_accrightslen = 0;
1425 #endif /* ISC_NET_BSD44MSGHDR */
1426
1427         if (write_countp != NULL)
1428                 *write_countp = write_count;
1429 }
1430
1431 /*
1432  * Construct an iov array and attach it to the msghdr passed in.  This is
1433  * the RECV constructor, which will use the available region of the buffer
1434  * (if using a buffer list) or will use the internal region (if a single
1435  * buffer I/O is requested).
1436  *
1437  * Nothing can be NULL, and the done event must list at least one buffer
1438  * on the buffer linked list for this function to be meaningful.
1439  *
1440  * If read_countp != NULL, *read_countp will hold the number of bytes
1441  * this transaction can receive.
1442  */
1443 static void
1444 build_msghdr_recv(isc__socket_t *sock, isc_socketevent_t *dev,
1445                   struct msghdr *msg, struct iovec *iov, size_t *read_countp)
1446 {
1447         unsigned int iovcount;
1448         isc_buffer_t *buffer;
1449         isc_region_t available;
1450         size_t read_count;
1451
1452         memset(msg, 0, sizeof(struct msghdr));
1453
1454         if (sock->type == isc_sockettype_udp) {
1455                 memset(&dev->address, 0, sizeof(dev->address));
1456 #ifdef BROKEN_RECVMSG
1457                 if (sock->pf == AF_INET) {
1458                         msg->msg_name = (void *)&dev->address.type.sin;
1459                         msg->msg_namelen = sizeof(dev->address.type.sin6);
1460                 } else if (sock->pf == AF_INET6) {
1461                         msg->msg_name = (void *)&dev->address.type.sin6;
1462                         msg->msg_namelen = sizeof(dev->address.type.sin6);
1463 #ifdef ISC_PLATFORM_HAVESYSUNH
1464                 } else if (sock->pf == AF_UNIX) {
1465                         msg->msg_name = (void *)&dev->address.type.sunix;
1466                         msg->msg_namelen = sizeof(dev->address.type.sunix);
1467 #endif
1468                 } else {
1469                         msg->msg_name = (void *)&dev->address.type.sa;
1470                         msg->msg_namelen = sizeof(dev->address.type);
1471                 }
1472 #else
1473                 msg->msg_name = (void *)&dev->address.type.sa;
1474                 msg->msg_namelen = sizeof(dev->address.type);
1475 #endif
1476 #ifdef ISC_NET_RECVOVERFLOW
1477                 /* If needed, steal one iovec for overflow detection. */
1478                 maxiov--;
1479 #endif
1480         } else { /* TCP */
1481                 msg->msg_name = NULL;
1482                 msg->msg_namelen = 0;
1483                 dev->address = sock->peer_address;
1484         }
1485
1486         buffer = ISC_LIST_HEAD(dev->bufferlist);
1487         read_count = 0;
1488
1489         /*
1490          * Single buffer I/O?  Skip what we've done so far in this region.
1491          */
1492         if (buffer == NULL) {
1493                 read_count = dev->region.length - dev->n;
1494                 iov[0].iov_base = (void *)(dev->region.base + dev->n);
1495                 iov[0].iov_len = read_count;
1496                 iovcount = 1;
1497
1498                 goto config;
1499         }
1500
1501         /*
1502          * Multibuffer I/O.
1503          * Skip empty buffers.
1504          */
1505         while (buffer != NULL) {
1506                 REQUIRE(ISC_BUFFER_VALID(buffer));
1507                 if (isc_buffer_availablelength(buffer) != 0)
1508                         break;
1509                 buffer = ISC_LIST_NEXT(buffer, link);
1510         }
1511
1512         iovcount = 0;
1513         while (buffer != NULL) {
1514                 INSIST(iovcount < MAXSCATTERGATHER_RECV);
1515
1516                 isc_buffer_availableregion(buffer, &available);
1517
1518                 if (available.length > 0) {
1519                         iov[iovcount].iov_base = (void *)(available.base);
1520                         iov[iovcount].iov_len = available.length;
1521                         read_count += available.length;
1522                         iovcount++;
1523                 }
1524                 buffer = ISC_LIST_NEXT(buffer, link);
1525         }
1526
1527  config:
1528
1529         /*
1530          * If needed, set up to receive that one extra byte.  Note that
1531          * we know there is at least one iov left, since we stole it
1532          * at the top of this function.
1533          */
1534 #ifdef ISC_NET_RECVOVERFLOW
1535         if (sock->type == isc_sockettype_udp) {
1536                 iov[iovcount].iov_base = (void *)(&sock->overflow);
1537                 iov[iovcount].iov_len = 1;
1538                 iovcount++;
1539         }
1540 #endif
1541
1542         msg->msg_iov = iov;
1543         msg->msg_iovlen = iovcount;
1544
1545 #ifdef ISC_NET_BSD44MSGHDR
1546         msg->msg_control = NULL;
1547         msg->msg_controllen = 0;
1548         msg->msg_flags = 0;
1549 #if defined(USE_CMSG)
1550         if (sock->type == isc_sockettype_udp) {
1551                 msg->msg_control = sock->recvcmsgbuf;
1552                 msg->msg_controllen = sock->recvcmsgbuflen;
1553         }
1554 #endif /* USE_CMSG */
1555 #else /* ISC_NET_BSD44MSGHDR */
1556         msg->msg_accrights = NULL;
1557         msg->msg_accrightslen = 0;
1558 #endif /* ISC_NET_BSD44MSGHDR */
1559
1560         if (read_countp != NULL)
1561                 *read_countp = read_count;
1562 }
1563
1564 static void
1565 set_dev_address(isc_sockaddr_t *address, isc__socket_t *sock,
1566                 isc_socketevent_t *dev)
1567 {
1568         if (sock->type == isc_sockettype_udp) {
1569                 if (address != NULL)
1570                         dev->address = *address;
1571                 else
1572                         dev->address = sock->peer_address;
1573         } else if (sock->type == isc_sockettype_tcp) {
1574                 INSIST(address == NULL);
1575                 dev->address = sock->peer_address;
1576         }
1577 }
1578
1579 static void
1580 destroy_socketevent(isc_event_t *event) {
1581         isc_socketevent_t *ev = (isc_socketevent_t *)event;
1582
1583         INSIST(ISC_LIST_EMPTY(ev->bufferlist));
1584
1585         (ev->destroy)(event);
1586 }
1587
1588 static isc_socketevent_t *
1589 allocate_socketevent(isc__socket_t *sock, isc_eventtype_t eventtype,
1590                      isc_taskaction_t action, const void *arg)
1591 {
1592         isc_socketevent_t *ev;
1593
1594         ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
1595                                                      sock, eventtype,
1596                                                      action, arg,
1597                                                      sizeof(*ev));
1598
1599         if (ev == NULL)
1600                 return (NULL);
1601
1602         ev->result = ISC_R_UNSET;
1603         ISC_LINK_INIT(ev, ev_link);
1604         ISC_LIST_INIT(ev->bufferlist);
1605         ev->region.base = NULL;
1606         ev->n = 0;
1607         ev->offset = 0;
1608         ev->attributes = 0;
1609         ev->destroy = ev->ev_destroy;
1610         ev->ev_destroy = destroy_socketevent;
1611
1612         return (ev);
1613 }
1614
1615 #if defined(ISC_SOCKET_DEBUG)
1616 static void
1617 dump_msg(struct msghdr *msg) {
1618         unsigned int i;
1619
1620         printf("MSGHDR %p\n", msg);
1621         printf("\tname %p, namelen %ld\n", msg->msg_name,
1622                (long) msg->msg_namelen);
1623         printf("\tiov %p, iovlen %ld\n", msg->msg_iov,
1624                (long) msg->msg_iovlen);
1625         for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
1626                 printf("\t\t%d\tbase %p, len %ld\n", i,
1627                        msg->msg_iov[i].iov_base,
1628                        (long) msg->msg_iov[i].iov_len);
1629 #ifdef ISC_NET_BSD44MSGHDR
1630         printf("\tcontrol %p, controllen %ld\n", msg->msg_control,
1631                (long) msg->msg_controllen);
1632 #endif
1633 }
1634 #endif
1635
1636 #define DOIO_SUCCESS            0       /* i/o ok, event sent */
1637 #define DOIO_SOFT               1       /* i/o ok, soft error, no event sent */
1638 #define DOIO_HARD               2       /* i/o error, event sent */
1639 #define DOIO_EOF                3       /* EOF, no event sent */
1640
1641 static int
1642 doio_recv(isc__socket_t *sock, isc_socketevent_t *dev) {
1643         int cc;
1644         struct iovec iov[MAXSCATTERGATHER_RECV];
1645         size_t read_count;
1646         size_t actual_count;
1647         struct msghdr msghdr;
1648         isc_buffer_t *buffer;
1649         int recv_errno;
1650         char strbuf[ISC_STRERRORSIZE];
1651
1652         build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
1653
1654 #if defined(ISC_SOCKET_DEBUG)
1655         dump_msg(&msghdr);
1656 #endif
1657
1658         cc = recvmsg(sock->fd, &msghdr, 0);
1659         recv_errno = errno;
1660
1661 #if defined(ISC_SOCKET_DEBUG)
1662         dump_msg(&msghdr);
1663 #endif
1664
1665         if (cc < 0) {
1666                 if (SOFT_ERROR(recv_errno))
1667                         return (DOIO_SOFT);
1668
1669                 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1670                         isc__strerror(recv_errno, strbuf, sizeof(strbuf));
1671                         socket_log(sock, NULL, IOEVENT,
1672                                    isc_msgcat, ISC_MSGSET_SOCKET,
1673                                    ISC_MSG_DOIORECV,
1674                                   "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
1675                                    sock->fd, cc, recv_errno, strbuf);
1676                 }
1677
1678 #define SOFT_OR_HARD(_system, _isc) \
1679         if (recv_errno == _system) { \
1680                 if (sock->connected) { \
1681                         dev->result = _isc; \
1682                         inc_stats(sock->manager->stats, \
1683                                   sock->statsindex[STATID_RECVFAIL]); \
1684                         return (DOIO_HARD); \
1685                 } \
1686                 return (DOIO_SOFT); \
1687         }
1688 #define ALWAYS_HARD(_system, _isc) \
1689         if (recv_errno == _system) { \
1690                 dev->result = _isc; \
1691                 inc_stats(sock->manager->stats, \
1692                           sock->statsindex[STATID_RECVFAIL]); \
1693                 return (DOIO_HARD); \
1694         }
1695
1696                 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1697                 SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1698                 SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1699                 SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
1700                 /* HPUX 11.11 can return EADDRNOTAVAIL. */
1701                 SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1702                 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1703                 /*
1704                  * HPUX returns EPROTO and EINVAL on receiving some ICMP/ICMPv6
1705                  * errors.
1706                  */
1707 #ifdef EPROTO
1708                 SOFT_OR_HARD(EPROTO, ISC_R_HOSTUNREACH);
1709 #endif
1710                 SOFT_OR_HARD(EINVAL, ISC_R_HOSTUNREACH);
1711
1712 #undef SOFT_OR_HARD
1713 #undef ALWAYS_HARD
1714
1715                 dev->result = isc__errno2result(recv_errno);
1716                 inc_stats(sock->manager->stats,
1717                           sock->statsindex[STATID_RECVFAIL]);
1718                 return (DOIO_HARD);
1719         }
1720
1721         /*
1722          * On TCP and UNIX sockets, zero length reads indicate EOF,
1723          * while on UDP sockets, zero length reads are perfectly valid,
1724          * although strange.
1725          */
1726         switch (sock->type) {
1727         case isc_sockettype_tcp:
1728         case isc_sockettype_unix:
1729                 if (cc == 0)
1730                         return (DOIO_EOF);
1731                 break;
1732         case isc_sockettype_udp:
1733                 break;
1734         case isc_sockettype_fdwatch:
1735         default:
1736                 INSIST(0);
1737         }
1738
1739         if (sock->type == isc_sockettype_udp) {
1740                 dev->address.length = msghdr.msg_namelen;
1741                 if (isc_sockaddr_getport(&dev->address) == 0) {
1742                         if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1743                                 socket_log(sock, &dev->address, IOEVENT,
1744                                            isc_msgcat, ISC_MSGSET_SOCKET,
1745                                            ISC_MSG_ZEROPORT,
1746                                            "dropping source port zero packet");
1747                         }
1748                         return (DOIO_SOFT);
1749                 }
1750                 /*
1751                  * Simulate a firewall blocking UDP responses bigger than
1752                  * 512 bytes.
1753                  */
1754                 if (sock->manager->maxudp != 0 && cc > sock->manager->maxudp)
1755                         return (DOIO_SOFT);
1756         }
1757
1758         socket_log(sock, &dev->address, IOEVENT,
1759                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
1760                    "packet received correctly");
1761
1762         /*
1763          * Overflow bit detection.  If we received MORE bytes than we should,
1764          * this indicates an overflow situation.  Set the flag in the
1765          * dev entry and adjust how much we read by one.
1766          */
1767 #ifdef ISC_NET_RECVOVERFLOW
1768         if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
1769                 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
1770                 cc--;
1771         }
1772 #endif
1773
1774         /*
1775          * If there are control messages attached, run through them and pull
1776          * out the interesting bits.
1777          */
1778         if (sock->type == isc_sockettype_udp)
1779                 process_cmsg(sock, &msghdr, dev);
1780
1781         /*
1782          * update the buffers (if any) and the i/o count
1783          */
1784         dev->n += cc;
1785         actual_count = cc;
1786         buffer = ISC_LIST_HEAD(dev->bufferlist);
1787         while (buffer != NULL && actual_count > 0U) {
1788                 REQUIRE(ISC_BUFFER_VALID(buffer));
1789                 if (isc_buffer_availablelength(buffer) <= actual_count) {
1790                         actual_count -= isc_buffer_availablelength(buffer);
1791                         isc_buffer_add(buffer,
1792                                        isc_buffer_availablelength(buffer));
1793                 } else {
1794                         isc_buffer_add(buffer, actual_count);
1795                         actual_count = 0;
1796                         POST(actual_count);
1797                         break;
1798                 }
1799                 buffer = ISC_LIST_NEXT(buffer, link);
1800                 if (buffer == NULL) {
1801                         INSIST(actual_count == 0U);
1802                 }
1803         }
1804
1805         /*
1806          * If we read less than we expected, update counters,
1807          * and let the upper layer poke the descriptor.
1808          */
1809         if (((size_t)cc != read_count) && (dev->n < dev->minimum))
1810                 return (DOIO_SOFT);
1811
1812         /*
1813          * Full reads are posted, or partials if partials are ok.
1814          */
1815         dev->result = ISC_R_SUCCESS;
1816         return (DOIO_SUCCESS);
1817 }
1818
1819 /*
1820  * Returns:
1821  *      DOIO_SUCCESS    The operation succeeded.  dev->result contains
1822  *                      ISC_R_SUCCESS.
1823  *
1824  *      DOIO_HARD       A hard or unexpected I/O error was encountered.
1825  *                      dev->result contains the appropriate error.
1826  *
1827  *      DOIO_SOFT       A soft I/O error was encountered.  No senddone
1828  *                      event was sent.  The operation should be retried.
1829  *
1830  *      No other return values are possible.
1831  */
1832 static int
1833 doio_send(isc__socket_t *sock, isc_socketevent_t *dev) {
1834         int cc;
1835         struct iovec iov[MAXSCATTERGATHER_SEND];
1836         size_t write_count;
1837         struct msghdr msghdr;
1838         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1839         int attempts = 0;
1840         int send_errno;
1841         char strbuf[ISC_STRERRORSIZE];
1842
1843         build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
1844
1845  resend:
1846         cc = sendmsg(sock->fd, &msghdr, 0);
1847         send_errno = errno;
1848
1849         /*
1850          * Check for error or block condition.
1851          */
1852         if (cc < 0) {
1853                 if (send_errno == EINTR && ++attempts < NRETRIES)
1854                         goto resend;
1855
1856                 if (SOFT_ERROR(send_errno))
1857                         return (DOIO_SOFT);
1858
1859 #define SOFT_OR_HARD(_system, _isc) \
1860         if (send_errno == _system) { \
1861                 if (sock->connected) { \
1862                         dev->result = _isc; \
1863                         inc_stats(sock->manager->stats, \
1864                                   sock->statsindex[STATID_SENDFAIL]); \
1865                         return (DOIO_HARD); \
1866                 } \
1867                 return (DOIO_SOFT); \
1868         }
1869 #define ALWAYS_HARD(_system, _isc) \
1870         if (send_errno == _system) { \
1871                 dev->result = _isc; \
1872                 inc_stats(sock->manager->stats, \
1873                           sock->statsindex[STATID_SENDFAIL]); \
1874                 return (DOIO_HARD); \
1875         }
1876
1877                 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1878                 ALWAYS_HARD(EACCES, ISC_R_NOPERM);
1879                 ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
1880                 ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1881                 ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1882 #ifdef EHOSTDOWN
1883                 ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
1884 #endif
1885                 ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1886                 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1887                 ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
1888                 ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
1889                 ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
1890
1891 #undef SOFT_OR_HARD
1892 #undef ALWAYS_HARD
1893
1894                 /*
1895                  * The other error types depend on whether or not the
1896                  * socket is UDP or TCP.  If it is UDP, some errors
1897                  * that we expect to be fatal under TCP are merely
1898                  * annoying, and are really soft errors.
1899                  *
1900                  * However, these soft errors are still returned as
1901                  * a status.
1902                  */
1903                 isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
1904                 isc__strerror(send_errno, strbuf, sizeof(strbuf));
1905                 UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
1906                                  addrbuf, strbuf);
1907                 dev->result = isc__errno2result(send_errno);
1908                 inc_stats(sock->manager->stats,
1909                           sock->statsindex[STATID_SENDFAIL]);
1910                 return (DOIO_HARD);
1911         }
1912
1913         if (cc == 0) {
1914                 inc_stats(sock->manager->stats,
1915                           sock->statsindex[STATID_SENDFAIL]);
1916                 UNEXPECTED_ERROR(__FILE__, __LINE__,
1917                                  "doio_send: send() %s 0",
1918                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1919                                                 ISC_MSG_RETURNED, "returned"));
1920         }
1921
1922         /*
1923          * If we write less than we expected, update counters, poke.
1924          */
1925         dev->n += cc;
1926         if ((size_t)cc != write_count)
1927                 return (DOIO_SOFT);
1928
1929         /*
1930          * Exactly what we wanted to write.  We're done with this
1931          * entry.  Post its completion event.
1932          */
1933         dev->result = ISC_R_SUCCESS;
1934         return (DOIO_SUCCESS);
1935 }
1936
1937 /*
1938  * Kill.
1939  *
1940  * Caller must ensure that the socket is not locked and no external
1941  * references exist.
1942  */
1943 static void
1944 closesocket(isc__socketmgr_t *manager, isc__socket_t *sock, int fd) {
1945         isc_sockettype_t type = sock->type;
1946         int lockid = FDLOCK_ID(fd);
1947
1948         /*
1949          * No one has this socket open, so the watcher doesn't have to be
1950          * poked, and the socket doesn't have to be locked.
1951          */
1952         LOCK(&manager->fdlock[lockid]);
1953         manager->fds[fd] = NULL;
1954         if (type == isc_sockettype_fdwatch)
1955                 manager->fdstate[fd] = CLOSED;
1956         else
1957                 manager->fdstate[fd] = CLOSE_PENDING;
1958         UNLOCK(&manager->fdlock[lockid]);
1959         if (type == isc_sockettype_fdwatch) {
1960                 /*
1961                  * The caller may close the socket once this function returns,
1962                  * and `fd' may be reassigned for a new socket.  So we do
1963                  * unwatch_fd() here, rather than defer it via select_poke().
1964                  * Note: this may complicate data protection among threads and
1965                  * may reduce performance due to additional locks.  One way to
1966                  * solve this would be to dup() the watched descriptor, but we
1967                  * take a simpler approach at this moment.
1968                  */
1969                 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
1970                 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
1971         } else
1972                 select_poke(manager, fd, SELECT_POKE_CLOSE);
1973
1974         inc_stats(manager->stats, sock->statsindex[STATID_CLOSE]);
1975
1976         /*
1977          * update manager->maxfd here (XXX: this should be implemented more
1978          * efficiently)
1979          */
1980 #ifdef USE_SELECT
1981         LOCK(&manager->lock);
1982         if (manager->maxfd == fd) {
1983                 int i;
1984
1985                 manager->maxfd = 0;
1986                 for (i = fd - 1; i >= 0; i--) {
1987                         lockid = FDLOCK_ID(i);
1988
1989                         LOCK(&manager->fdlock[lockid]);
1990                         if (manager->fdstate[i] == MANAGED) {
1991                                 manager->maxfd = i;
1992                                 UNLOCK(&manager->fdlock[lockid]);
1993                                 break;
1994                         }
1995                         UNLOCK(&manager->fdlock[lockid]);
1996                 }
1997 #ifdef ISC_PLATFORM_USETHREADS
1998                 if (manager->maxfd < manager->pipe_fds[0])
1999                         manager->maxfd = manager->pipe_fds[0];
2000 #endif
2001         }
2002         UNLOCK(&manager->lock);
2003 #endif  /* USE_SELECT */
2004 }
2005
2006 static void
2007 destroy(isc__socket_t **sockp) {
2008         int fd;
2009         isc__socket_t *sock = *sockp;
2010         isc__socketmgr_t *manager = sock->manager;
2011
2012         socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2013                    ISC_MSG_DESTROYING, "destroying");
2014
2015         INSIST(ISC_LIST_EMPTY(sock->accept_list));
2016         INSIST(ISC_LIST_EMPTY(sock->recv_list));
2017         INSIST(ISC_LIST_EMPTY(sock->send_list));
2018         INSIST(sock->connect_ev == NULL);
2019         REQUIRE(sock->fd == -1 || sock->fd < (int)manager->maxsocks);
2020
2021         if (sock->fd >= 0) {
2022                 fd = sock->fd;
2023                 sock->fd = -1;
2024                 closesocket(manager, sock, fd);
2025         }
2026
2027         LOCK(&manager->lock);
2028
2029         ISC_LIST_UNLINK(manager->socklist, sock, link);
2030
2031 #ifdef USE_WATCHER_THREAD
2032         if (ISC_LIST_EMPTY(manager->socklist))
2033                 SIGNAL(&manager->shutdown_ok);
2034 #endif /* USE_WATCHER_THREAD */
2035
2036         /* can't unlock manager as its memory context is still used */
2037         free_socket(sockp);
2038
2039         UNLOCK(&manager->lock);
2040 }
2041
2042 static isc_result_t
2043 allocate_socket(isc__socketmgr_t *manager, isc_sockettype_t type,
2044                 isc__socket_t **socketp)
2045 {
2046         isc__socket_t *sock;
2047         isc_result_t result;
2048         ISC_SOCKADDR_LEN_T cmsgbuflen;
2049
2050         sock = isc_mem_get(manager->mctx, sizeof(*sock));
2051
2052         if (sock == NULL)
2053                 return (ISC_R_NOMEMORY);
2054
2055         sock->common.magic = 0;
2056         sock->common.impmagic = 0;
2057         sock->references = 0;
2058
2059         sock->manager = manager;
2060         sock->type = type;
2061         sock->fd = -1;
2062         sock->dupped = 0;
2063         sock->statsindex = NULL;
2064
2065         ISC_LINK_INIT(sock, link);
2066
2067         sock->recvcmsgbuf = NULL;
2068         sock->sendcmsgbuf = NULL;
2069
2070         /*
2071          * Set up cmsg buffers.
2072          */
2073         cmsgbuflen = 0;
2074 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
2075         cmsgbuflen += cmsg_space(sizeof(struct in6_pktinfo));
2076 #endif
2077 #if defined(USE_CMSG) && defined(SO_TIMESTAMP)
2078         cmsgbuflen += cmsg_space(sizeof(struct timeval));
2079 #endif
2080         sock->recvcmsgbuflen = cmsgbuflen;
2081         if (sock->recvcmsgbuflen != 0U) {
2082                 sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
2083                 if (sock->recvcmsgbuf == NULL) {
2084                         result = ISC_R_NOMEMORY;
2085                         goto error;
2086                 }
2087         }
2088
2089         cmsgbuflen = 0;
2090 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
2091         cmsgbuflen += cmsg_space(sizeof(struct in6_pktinfo));
2092 #if defined(IPV6_USE_MIN_MTU)
2093         /*
2094          * Provide space for working around FreeBSD's broken IPV6_USE_MIN_MTU
2095          * support.
2096          */
2097         cmsgbuflen += cmsg_space(sizeof(int));
2098 #endif
2099 #endif
2100         sock->sendcmsgbuflen = cmsgbuflen;
2101         if (sock->sendcmsgbuflen != 0U) {
2102                 sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
2103                 if (sock->sendcmsgbuf == NULL) {
2104                         result = ISC_R_NOMEMORY;
2105                         goto error;
2106                 }
2107         }
2108
2109         memset(sock->name, 0, sizeof(sock->name));
2110         sock->tag = NULL;
2111
2112         /*
2113          * Set up list of readers and writers to be initially empty.
2114          */
2115         ISC_LIST_INIT(sock->recv_list);
2116         ISC_LIST_INIT(sock->send_list);
2117         ISC_LIST_INIT(sock->accept_list);
2118         sock->connect_ev = NULL;
2119         sock->pending_recv = 0;
2120         sock->pending_send = 0;
2121         sock->pending_accept = 0;
2122         sock->listener = 0;
2123         sock->connected = 0;
2124         sock->connecting = 0;
2125         sock->bound = 0;
2126
2127         /*
2128          * Initialize the lock.
2129          */
2130         result = isc_mutex_init(&sock->lock);
2131         if (result != ISC_R_SUCCESS) {
2132                 sock->common.magic = 0;
2133                 sock->common.impmagic = 0;
2134                 goto error;
2135         }
2136
2137         /*
2138          * Initialize readable and writable events.
2139          */
2140         ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
2141                        ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTR,
2142                        NULL, sock, sock, NULL, NULL);
2143         ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
2144                        ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTW,
2145                        NULL, sock, sock, NULL, NULL);
2146
2147         sock->common.magic = ISCAPI_SOCKET_MAGIC;
2148         sock->common.impmagic = SOCKET_MAGIC;
2149         *socketp = sock;
2150
2151         return (ISC_R_SUCCESS);
2152
2153  error:
2154         if (sock->recvcmsgbuf != NULL)
2155                 isc_mem_put(manager->mctx, sock->recvcmsgbuf,
2156                             sock->recvcmsgbuflen);
2157         if (sock->sendcmsgbuf != NULL)
2158                 isc_mem_put(manager->mctx, sock->sendcmsgbuf,
2159                             sock->sendcmsgbuflen);
2160         isc_mem_put(manager->mctx, sock, sizeof(*sock));
2161
2162         return (result);
2163 }
2164
2165 /*
2166  * This event requires that the various lists be empty, that the reference
2167  * count be 1, and that the magic number is valid.  The other socket bits,
2168  * like the lock, must be initialized as well.  The fd associated must be
2169  * marked as closed, by setting it to -1 on close, or this routine will
2170  * also close the socket.
2171  */
2172 static void
2173 free_socket(isc__socket_t **socketp) {
2174         isc__socket_t *sock = *socketp;
2175
2176         INSIST(sock->references == 0);
2177         INSIST(VALID_SOCKET(sock));
2178         INSIST(!sock->connecting);
2179         INSIST(!sock->pending_recv);
2180         INSIST(!sock->pending_send);
2181         INSIST(!sock->pending_accept);
2182         INSIST(ISC_LIST_EMPTY(sock->recv_list));
2183         INSIST(ISC_LIST_EMPTY(sock->send_list));
2184         INSIST(ISC_LIST_EMPTY(sock->accept_list));
2185         INSIST(!ISC_LINK_LINKED(sock, link));
2186
2187         if (sock->recvcmsgbuf != NULL)
2188                 isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
2189                             sock->recvcmsgbuflen);
2190         if (sock->sendcmsgbuf != NULL)
2191                 isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
2192                             sock->sendcmsgbuflen);
2193
2194         sock->common.magic = 0;
2195         sock->common.impmagic = 0;
2196
2197         DESTROYLOCK(&sock->lock);
2198
2199         isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
2200
2201         *socketp = NULL;
2202 }
2203
2204 #ifdef SO_BSDCOMPAT
2205 /*
2206  * This really should not be necessary to do.  Having to workout
2207  * which kernel version we are on at run time so that we don't cause
2208  * the kernel to issue a warning about us using a deprecated socket option.
2209  * Such warnings should *never* be on by default in production kernels.
2210  *
2211  * We can't do this a build time because executables are moved between
2212  * machines and hence kernels.
2213  *
2214  * We can't just not set SO_BSDCOMAT because some kernels require it.
2215  */
2216
2217 static isc_once_t         bsdcompat_once = ISC_ONCE_INIT;
2218 isc_boolean_t bsdcompat = ISC_TRUE;
2219
2220 static void
2221 clear_bsdcompat(void) {
2222 #ifdef __linux__
2223          struct utsname buf;
2224          char *endp;
2225          long int major;
2226          long int minor;
2227
2228          uname(&buf);    /* Can only fail if buf is bad in Linux. */
2229
2230          /* Paranoia in parsing can be increased, but we trust uname(). */
2231          major = strtol(buf.release, &endp, 10);
2232          if (*endp == '.') {
2233                 minor = strtol(endp+1, &endp, 10);
2234                 if ((major > 2) || ((major == 2) && (minor >= 4))) {
2235                         bsdcompat = ISC_FALSE;
2236                 }
2237          }
2238 #endif /* __linux __ */
2239 }
2240 #endif
2241
2242 static void
2243 use_min_mtu(isc__socket_t *sock) {
2244 #if !defined(IPV6_USE_MIN_MTU) && !defined(IPV6_MTU)
2245         UNUSED(sock);
2246 #endif
2247 #ifdef IPV6_USE_MIN_MTU
2248         /* use minimum MTU */
2249         if (sock->pf == AF_INET6) {
2250                 int on = 1;
2251                 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_USE_MIN_MTU,
2252                                 (void *)&on, sizeof(on));
2253         }
2254 #endif
2255 #if defined(IPV6_MTU)
2256         /*
2257          * Use minimum MTU on IPv6 sockets.
2258          */
2259         if (sock->pf == AF_INET6) {
2260                 int mtu = 1280;
2261                 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_MTU,
2262                                  &mtu, sizeof(mtu));
2263         }
2264 #endif
2265 }
2266
2267 static isc_result_t
2268 opensocket(isc__socketmgr_t *manager, isc__socket_t *sock,
2269            isc__socket_t *dup_socket)
2270 {
2271         isc_result_t result;
2272         char strbuf[ISC_STRERRORSIZE];
2273         const char *err = "socket";
2274         int tries = 0;
2275 #if defined(USE_CMSG) || defined(SO_BSDCOMPAT) || defined(SO_NOSIGPIPE)
2276         int on = 1;
2277 #endif
2278 #if defined(SO_RCVBUF)
2279         ISC_SOCKADDR_LEN_T optlen;
2280         int size;
2281 #endif
2282
2283  again:
2284         if (dup_socket == NULL) {
2285                 switch (sock->type) {
2286                 case isc_sockettype_udp:
2287                         sock->fd = socket(sock->pf, SOCK_DGRAM, IPPROTO_UDP);
2288                         break;
2289                 case isc_sockettype_tcp:
2290                         sock->fd = socket(sock->pf, SOCK_STREAM, IPPROTO_TCP);
2291                         break;
2292                 case isc_sockettype_unix:
2293                         sock->fd = socket(sock->pf, SOCK_STREAM, 0);
2294                         break;
2295                 case isc_sockettype_fdwatch:
2296                         /*
2297                          * We should not be called for isc_sockettype_fdwatch
2298                          * sockets.
2299                          */
2300                         INSIST(0);
2301                         break;
2302                 }
2303         } else {
2304                 sock->fd = dup(dup_socket->fd);
2305                 sock->dupped = 1;
2306                 sock->bound = dup_socket->bound;
2307         }
2308         if (sock->fd == -1 && errno == EINTR && tries++ < 42)
2309                 goto again;
2310
2311 #ifdef F_DUPFD
2312         /*
2313          * Leave a space for stdio and TCP to work in.
2314          */
2315         if (manager->reserved != 0 && sock->type == isc_sockettype_udp &&
2316             sock->fd >= 0 && sock->fd < manager->reserved) {
2317                 int new, tmp;
2318                 new = fcntl(sock->fd, F_DUPFD, manager->reserved);
2319                 tmp = errno;
2320                 (void)close(sock->fd);
2321                 errno = tmp;
2322                 sock->fd = new;
2323                 err = "isc_socket_create: fcntl/reserved";
2324         } else if (sock->fd >= 0 && sock->fd < 20) {
2325                 int new, tmp;
2326                 new = fcntl(sock->fd, F_DUPFD, 20);
2327                 tmp = errno;
2328                 (void)close(sock->fd);
2329                 errno = tmp;
2330                 sock->fd = new;
2331                 err = "isc_socket_create: fcntl";
2332         }
2333 #endif
2334
2335         if (sock->fd >= (int)manager->maxsocks) {
2336                 (void)close(sock->fd);
2337                 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2338                                ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2339                                isc_msgcat, ISC_MSGSET_SOCKET,
2340                                ISC_MSG_TOOMANYFDS,
2341                                "socket: file descriptor exceeds limit (%d/%u)",
2342                                sock->fd, manager->maxsocks);
2343                 return (ISC_R_NORESOURCES);
2344         }
2345
2346         if (sock->fd < 0) {
2347                 switch (errno) {
2348                 case EMFILE:
2349                 case ENFILE:
2350                         isc__strerror(errno, strbuf, sizeof(strbuf));
2351                         isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2352                                        ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2353                                        isc_msgcat, ISC_MSGSET_SOCKET,
2354                                        ISC_MSG_TOOMANYFDS,
2355                                        "%s: %s", err, strbuf);
2356                         /* fallthrough */
2357                 case ENOBUFS:
2358                         return (ISC_R_NORESOURCES);
2359
2360                 case EPROTONOSUPPORT:
2361                 case EPFNOSUPPORT:
2362                 case EAFNOSUPPORT:
2363                 /*
2364                  * Linux 2.2 (and maybe others) return EINVAL instead of
2365                  * EAFNOSUPPORT.
2366                  */
2367                 case EINVAL:
2368                         return (ISC_R_FAMILYNOSUPPORT);
2369
2370                 default:
2371                         isc__strerror(errno, strbuf, sizeof(strbuf));
2372                         UNEXPECTED_ERROR(__FILE__, __LINE__,
2373                                          "%s() %s: %s", err,
2374                                          isc_msgcat_get(isc_msgcat,
2375                                                         ISC_MSGSET_GENERAL,
2376                                                         ISC_MSG_FAILED,
2377                                                         "failed"),
2378                                          strbuf);
2379                         return (ISC_R_UNEXPECTED);
2380                 }
2381         }
2382
2383         if (dup_socket != NULL)
2384                 goto setup_done;
2385
2386         result = make_nonblock(sock->fd);
2387         if (result != ISC_R_SUCCESS) {
2388                 (void)close(sock->fd);
2389                 return (result);
2390         }
2391
2392 #ifdef SO_BSDCOMPAT
2393         RUNTIME_CHECK(isc_once_do(&bsdcompat_once,
2394                                   clear_bsdcompat) == ISC_R_SUCCESS);
2395         if (sock->type != isc_sockettype_unix && bsdcompat &&
2396             setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
2397                        (void *)&on, sizeof(on)) < 0) {
2398                 isc__strerror(errno, strbuf, sizeof(strbuf));
2399                 UNEXPECTED_ERROR(__FILE__, __LINE__,
2400                                  "setsockopt(%d, SO_BSDCOMPAT) %s: %s",
2401                                  sock->fd,
2402                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2403                                                 ISC_MSG_FAILED, "failed"),
2404                                  strbuf);
2405                 /* Press on... */
2406         }
2407 #endif
2408
2409 #ifdef SO_NOSIGPIPE
2410         if (setsockopt(sock->fd, SOL_SOCKET, SO_NOSIGPIPE,
2411                        (void *)&on, sizeof(on)) < 0) {
2412                 isc__strerror(errno, strbuf, sizeof(strbuf));
2413                 UNEXPECTED_ERROR(__FILE__, __LINE__,
2414                                  "setsockopt(%d, SO_NOSIGPIPE) %s: %s",
2415                                  sock->fd,
2416                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2417                                                 ISC_MSG_FAILED, "failed"),
2418                                  strbuf);
2419                 /* Press on... */
2420         }
2421 #endif
2422
2423         /*
2424          * Use minimum mtu if possible.
2425          */
2426         use_min_mtu(sock);
2427
2428 #if defined(USE_CMSG) || defined(SO_RCVBUF)
2429         if (sock->type == isc_sockettype_udp) {
2430
2431 #if defined(USE_CMSG)
2432 #if defined(SO_TIMESTAMP)
2433                 if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
2434                                (void *)&on, sizeof(on)) < 0
2435                     && errno != ENOPROTOOPT) {
2436                         isc__strerror(errno, strbuf, sizeof(strbuf));
2437                         UNEXPECTED_ERROR(__FILE__, __LINE__,
2438                                          "setsockopt(%d, SO_TIMESTAMP) %s: %s",
2439                                          sock->fd,
2440                                          isc_msgcat_get(isc_msgcat,
2441                                                         ISC_MSGSET_GENERAL,
2442                                                         ISC_MSG_FAILED,
2443                                                         "failed"),
2444                                          strbuf);
2445                         /* Press on... */
2446                 }
2447 #endif /* SO_TIMESTAMP */
2448
2449 #if defined(ISC_PLATFORM_HAVEIPV6)
2450                 if (sock->pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
2451                         /*
2452                          * Warn explicitly because this anomaly can be hidden
2453                          * in usual operation (and unexpectedly appear later).
2454                          */
2455                         UNEXPECTED_ERROR(__FILE__, __LINE__,
2456                                          "No buffer available to receive "
2457                                          "IPv6 destination");
2458                 }
2459 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
2460 #ifdef IPV6_RECVPKTINFO
2461                 /* RFC 3542 */
2462                 if ((sock->pf == AF_INET6)
2463                     && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
2464                                    (void *)&on, sizeof(on)) < 0)) {
2465                         isc__strerror(errno, strbuf, sizeof(strbuf));
2466                         UNEXPECTED_ERROR(__FILE__, __LINE__,
2467                                          "setsockopt(%d, IPV6_RECVPKTINFO) "
2468                                          "%s: %s", sock->fd,
2469                                          isc_msgcat_get(isc_msgcat,
2470                                                         ISC_MSGSET_GENERAL,
2471                                                         ISC_MSG_FAILED,
2472                                                         "failed"),
2473                                          strbuf);
2474                 }
2475 #else
2476                 /* RFC 2292 */
2477                 if ((sock->pf == AF_INET6)
2478                     && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
2479                                    (void *)&on, sizeof(on)) < 0)) {
2480                         isc__strerror(errno, strbuf, sizeof(strbuf));
2481                         UNEXPECTED_ERROR(__FILE__, __LINE__,
2482                                          "setsockopt(%d, IPV6_PKTINFO) %s: %s",
2483                                          sock->fd,
2484                                          isc_msgcat_get(isc_msgcat,
2485                                                         ISC_MSGSET_GENERAL,
2486                                                         ISC_MSG_FAILED,
2487                                                         "failed"),
2488                                          strbuf);
2489                 }
2490 #endif /* IPV6_RECVPKTINFO */
2491 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
2492 #if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT)
2493                 /*
2494                  * Turn off Path MTU discovery on IPv6/UDP sockets.
2495                  */
2496                 if (sock->pf == AF_INET6) {
2497                         int action = IPV6_PMTUDISC_DONT;
2498                         (void)setsockopt(sock->fd, IPPROTO_IPV6,
2499                                          IPV6_MTU_DISCOVER, &action,
2500                                          sizeof(action));
2501                 }
2502 #endif
2503 #endif /* ISC_PLATFORM_HAVEIPV6 */
2504 #endif /* defined(USE_CMSG) */
2505
2506 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2507                 /*
2508                  * Turn off Path MTU discovery on IPv4/UDP sockets.
2509                  */
2510                 if (sock->pf == AF_INET) {
2511                         int action = IP_PMTUDISC_DONT;
2512                         (void)setsockopt(sock->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2513                                          &action, sizeof(action));
2514                 }
2515 #endif
2516 #if defined(IP_DONTFRAG)
2517                 /*
2518                  * Turn off Path MTU discovery on IPv4/UDP sockets.
2519                  */
2520                 if (sock->pf == AF_INET) {
2521                         int off = 0;
2522                         (void)setsockopt(sock->fd, IPPROTO_IP, IP_DONTFRAG,
2523                                          &off, sizeof(off));
2524                 }
2525 #endif
2526
2527 #if defined(SO_RCVBUF)
2528                 optlen = sizeof(size);
2529                 if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
2530                                (void *)&size, &optlen) >= 0 &&
2531                      size < RCVBUFSIZE) {
2532                         size = RCVBUFSIZE;
2533                         if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
2534                                        (void *)&size, sizeof(size)) == -1) {
2535                                 isc__strerror(errno, strbuf, sizeof(strbuf));
2536                                 UNEXPECTED_ERROR(__FILE__, __LINE__,
2537                                         "setsockopt(%d, SO_RCVBUF, %d) %s: %s",
2538                                         sock->fd, size,
2539                                         isc_msgcat_get(isc_msgcat,
2540                                                        ISC_MSGSET_GENERAL,
2541                                                        ISC_MSG_FAILED,
2542                                                        "failed"),
2543                                         strbuf);
2544                         }
2545                 }
2546 #endif
2547         }
2548 #endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
2549
2550 setup_done:
2551         inc_stats(manager->stats, sock->statsindex[STATID_OPEN]);
2552
2553         return (ISC_R_SUCCESS);
2554 }
2555
2556 /*
2557  * Create a 'type' socket or duplicate an existing socket, managed
2558  * by 'manager'.  Events will be posted to 'task' and when dispatched
2559  * 'action' will be called with 'arg' as the arg value.  The new
2560  * socket is returned in 'socketp'.
2561  */
2562 static isc_result_t
2563 socket_create(isc_socketmgr_t *manager0, int pf, isc_sockettype_t type,
2564               isc_socket_t **socketp, isc_socket_t *dup_socket)
2565 {
2566         isc__socket_t *sock = NULL;
2567         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
2568         isc_result_t result;
2569         int lockid;
2570
2571         REQUIRE(VALID_MANAGER(manager));
2572         REQUIRE(socketp != NULL && *socketp == NULL);
2573         REQUIRE(type != isc_sockettype_fdwatch);
2574
2575         result = allocate_socket(manager, type, &sock);
2576         if (result != ISC_R_SUCCESS)
2577                 return (result);
2578
2579         switch (sock->type) {
2580         case isc_sockettype_udp:
2581                 sock->statsindex =
2582                         (pf == AF_INET) ? udp4statsindex : udp6statsindex;
2583                 break;
2584         case isc_sockettype_tcp:
2585                 sock->statsindex =
2586                         (pf == AF_INET) ? tcp4statsindex : tcp6statsindex;
2587                 break;
2588         case isc_sockettype_unix:
2589                 sock->statsindex = unixstatsindex;
2590                 break;
2591         default:
2592                 INSIST(0);
2593         }
2594
2595         sock->pf = pf;
2596
2597         result = opensocket(manager, sock, (isc__socket_t *)dup_socket);
2598         if (result != ISC_R_SUCCESS) {
2599                 inc_stats(manager->stats, sock->statsindex[STATID_OPENFAIL]);
2600                 free_socket(&sock);
2601                 return (result);
2602         }
2603
2604         sock->common.methods = (isc_socketmethods_t *)&socketmethods;
2605         sock->references = 1;
2606         *socketp = (isc_socket_t *)sock;
2607
2608         /*
2609          * Note we don't have to lock the socket like we normally would because
2610          * there are no external references to it yet.
2611          */
2612
2613         lockid = FDLOCK_ID(sock->fd);
2614         LOCK(&manager->fdlock[lockid]);
2615         manager->fds[sock->fd] = sock;
2616         manager->fdstate[sock->fd] = MANAGED;
2617 #ifdef USE_DEVPOLL
2618         INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
2619                sock->manager->fdpollinfo[sock->fd].want_write == 0);
2620 #endif
2621         UNLOCK(&manager->fdlock[lockid]);
2622
2623         LOCK(&manager->lock);
2624         ISC_LIST_APPEND(manager->socklist, sock, link);
2625 #ifdef USE_SELECT
2626         if (manager->maxfd < sock->fd)
2627                 manager->maxfd = sock->fd;
2628 #endif
2629         UNLOCK(&manager->lock);
2630
2631         socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2632                    ISC_MSG_CREATED, dup_socket != NULL ? "dupped" : "created");
2633
2634         return (ISC_R_SUCCESS);
2635 }
2636
2637 /*%
2638  * Create a new 'type' socket managed by 'manager'.  Events
2639  * will be posted to 'task' and when dispatched 'action' will be
2640  * called with 'arg' as the arg value.  The new socket is returned
2641  * in 'socketp'.
2642  */
2643 ISC_SOCKETFUNC_SCOPE isc_result_t
2644 isc__socket_create(isc_socketmgr_t *manager0, int pf, isc_sockettype_t type,
2645                    isc_socket_t **socketp)
2646 {
2647         return (socket_create(manager0, pf, type, socketp, NULL));
2648 }
2649
2650 /*%
2651  * Duplicate an existing socket.  The new socket is returned
2652  * in 'socketp'.
2653  */
2654 ISC_SOCKETFUNC_SCOPE isc_result_t
2655 isc__socket_dup(isc_socket_t *sock0, isc_socket_t **socketp) {
2656         isc__socket_t *sock = (isc__socket_t *)sock0;
2657
2658         REQUIRE(VALID_SOCKET(sock));
2659         REQUIRE(socketp != NULL && *socketp == NULL);
2660
2661         return (socket_create((isc_socketmgr_t *) sock->manager,
2662                               sock->pf, sock->type, socketp,
2663                               sock0));
2664 }
2665
2666 #ifdef BIND9
2667 ISC_SOCKETFUNC_SCOPE isc_result_t
2668 isc__socket_open(isc_socket_t *sock0) {
2669         isc_result_t result;
2670         isc__socket_t *sock = (isc__socket_t *)sock0;
2671
2672         REQUIRE(VALID_SOCKET(sock));
2673
2674         LOCK(&sock->lock);
2675         REQUIRE(sock->references == 1);
2676         REQUIRE(sock->type != isc_sockettype_fdwatch);
2677         UNLOCK(&sock->lock);
2678         /*
2679          * We don't need to retain the lock hereafter, since no one else has
2680          * this socket.
2681          */
2682         REQUIRE(sock->fd == -1);
2683
2684         result = opensocket(sock->manager, sock, NULL);
2685         if (result != ISC_R_SUCCESS)
2686                 sock->fd = -1;
2687
2688         if (result == ISC_R_SUCCESS) {
2689                 int lockid = FDLOCK_ID(sock->fd);
2690
2691                 LOCK(&sock->manager->fdlock[lockid]);
2692                 sock->manager->fds[sock->fd] = sock;
2693                 sock->manager->fdstate[sock->fd] = MANAGED;
2694 #ifdef USE_DEVPOLL
2695                 INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
2696                        sock->manager->fdpollinfo[sock->fd].want_write == 0);
2697 #endif
2698                 UNLOCK(&sock->manager->fdlock[lockid]);
2699
2700 #ifdef USE_SELECT
2701                 LOCK(&sock->manager->lock);
2702                 if (sock->manager->maxfd < sock->fd)
2703                         sock->manager->maxfd = sock->fd;
2704                 UNLOCK(&sock->manager->lock);
2705 #endif
2706         }
2707
2708         return (result);
2709 }
2710 #endif  /* BIND9 */
2711
2712 /*
2713  * Create a new 'type' socket managed by 'manager'.  Events
2714  * will be posted to 'task' and when dispatched 'action' will be
2715  * called with 'arg' as the arg value.  The new socket is returned
2716  * in 'socketp'.
2717  */
2718 ISC_SOCKETFUNC_SCOPE isc_result_t
2719 isc__socket_fdwatchcreate(isc_socketmgr_t *manager0, int fd, int flags,
2720                           isc_sockfdwatch_t callback, void *cbarg,
2721                           isc_task_t *task, isc_socket_t **socketp)
2722 {
2723         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
2724         isc__socket_t *sock = NULL;
2725         isc_result_t result;
2726         int lockid;
2727
2728         REQUIRE(VALID_MANAGER(manager));
2729         REQUIRE(socketp != NULL && *socketp == NULL);
2730
2731         result = allocate_socket(manager, isc_sockettype_fdwatch, &sock);
2732         if (result != ISC_R_SUCCESS)
2733                 return (result);
2734
2735         sock->fd = fd;
2736         sock->fdwatcharg = cbarg;
2737         sock->fdwatchcb = callback;
2738         sock->fdwatchflags = flags;
2739         sock->fdwatchtask = task;
2740         sock->statsindex = fdwatchstatsindex;
2741
2742         sock->common.methods = (isc_socketmethods_t *)&socketmethods;
2743         sock->references = 1;
2744         *socketp = (isc_socket_t *)sock;
2745
2746         /*
2747          * Note we don't have to lock the socket like we normally would because
2748          * there are no external references to it yet.
2749          */
2750
2751         lockid = FDLOCK_ID(sock->fd);
2752         LOCK(&manager->fdlock[lockid]);
2753         manager->fds[sock->fd] = sock;
2754         manager->fdstate[sock->fd] = MANAGED;
2755         UNLOCK(&manager->fdlock[lockid]);
2756
2757         LOCK(&manager->lock);
2758         ISC_LIST_APPEND(manager->socklist, sock, link);
2759 #ifdef USE_SELECT
2760         if (manager->maxfd < sock->fd)
2761                 manager->maxfd = sock->fd;
2762 #endif
2763         UNLOCK(&manager->lock);
2764
2765         if (flags & ISC_SOCKFDWATCH_READ)
2766                 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2767         if (flags & ISC_SOCKFDWATCH_WRITE)
2768                 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
2769
2770         socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2771                    ISC_MSG_CREATED, "fdwatch-created");
2772
2773         return (ISC_R_SUCCESS);
2774 }
2775
2776 /*
2777  * Indicate to the manager that it should watch the socket again.
2778  * This can be used to restart watching if the previous event handler
2779  * didn't indicate there was more data to be processed.  Primarily
2780  * it is for writing but could be used for reading if desired
2781  */
2782
2783 ISC_SOCKETFUNC_SCOPE isc_result_t
2784 isc__socket_fdwatchpoke(isc_socket_t *sock0, int flags)
2785 {
2786         isc__socket_t *sock = (isc__socket_t *)sock0;
2787
2788         REQUIRE(VALID_SOCKET(sock));
2789
2790         /*
2791          * We check both flags first to allow us to get the lock
2792          * once but only if we need it.
2793          */
2794
2795         if ((flags & (ISC_SOCKFDWATCH_READ | ISC_SOCKFDWATCH_WRITE)) != 0) {
2796                 LOCK(&sock->lock);
2797                 if (((flags & ISC_SOCKFDWATCH_READ) != 0) &&
2798                     !sock->pending_recv)
2799                         select_poke(sock->manager, sock->fd,
2800                                     SELECT_POKE_READ);
2801                 if (((flags & ISC_SOCKFDWATCH_WRITE) != 0) &&
2802                     !sock->pending_send)
2803                         select_poke(sock->manager, sock->fd,
2804                                     SELECT_POKE_WRITE);
2805                 UNLOCK(&sock->lock);
2806         }
2807
2808         socket_log(sock, NULL, TRACE, isc_msgcat, ISC_MSGSET_SOCKET,
2809                    ISC_MSG_POKED, "fdwatch-poked flags: %d", flags);
2810
2811         return (ISC_R_SUCCESS);
2812 }
2813
2814 /*
2815  * Attach to a socket.  Caller must explicitly detach when it is done.
2816  */
2817 ISC_SOCKETFUNC_SCOPE void
2818 isc__socket_attach(isc_socket_t *sock0, isc_socket_t **socketp) {
2819         isc__socket_t *sock = (isc__socket_t *)sock0;
2820
2821         REQUIRE(VALID_SOCKET(sock));
2822         REQUIRE(socketp != NULL && *socketp == NULL);
2823
2824         LOCK(&sock->lock);
2825         sock->references++;
2826         UNLOCK(&sock->lock);
2827
2828         *socketp = (isc_socket_t *)sock;
2829 }
2830
2831 /*
2832  * Dereference a socket.  If this is the last reference to it, clean things
2833  * up by destroying the socket.
2834  */
2835 ISC_SOCKETFUNC_SCOPE void
2836 isc__socket_detach(isc_socket_t **socketp) {
2837         isc__socket_t *sock;
2838         isc_boolean_t kill_socket = ISC_FALSE;
2839
2840         REQUIRE(socketp != NULL);
2841         sock = (isc__socket_t *)*socketp;
2842         REQUIRE(VALID_SOCKET(sock));
2843
2844         LOCK(&sock->lock);
2845         REQUIRE(sock->references > 0);
2846         sock->references--;
2847         if (sock->references == 0)
2848                 kill_socket = ISC_TRUE;
2849         UNLOCK(&sock->lock);
2850
2851         if (kill_socket)
2852                 destroy(&sock);
2853
2854         *socketp = NULL;
2855 }
2856
2857 #ifdef BIND9
2858 ISC_SOCKETFUNC_SCOPE isc_result_t
2859 isc__socket_close(isc_socket_t *sock0) {
2860         isc__socket_t *sock = (isc__socket_t *)sock0;
2861         int fd;
2862         isc__socketmgr_t *manager;
2863
2864         fflush(stdout);
2865         REQUIRE(VALID_SOCKET(sock));
2866
2867         LOCK(&sock->lock);
2868
2869         REQUIRE(sock->references == 1);
2870         REQUIRE(sock->type != isc_sockettype_fdwatch);
2871         REQUIRE(sock->fd >= 0 && sock->fd < (int)sock->manager->maxsocks);
2872
2873         INSIST(!sock->connecting);
2874         INSIST(!sock->pending_recv);
2875         INSIST(!sock->pending_send);
2876         INSIST(!sock->pending_accept);
2877         INSIST(ISC_LIST_EMPTY(sock->recv_list));
2878         INSIST(ISC_LIST_EMPTY(sock->send_list));
2879         INSIST(ISC_LIST_EMPTY(sock->accept_list));
2880         INSIST(sock->connect_ev == NULL);
2881
2882         manager = sock->manager;
2883         fd = sock->fd;
2884         sock->fd = -1;
2885         sock->dupped = 0;
2886         memset(sock->name, 0, sizeof(sock->name));
2887         sock->tag = NULL;
2888         sock->listener = 0;
2889         sock->connected = 0;
2890         sock->connecting = 0;
2891         sock->bound = 0;
2892         isc_sockaddr_any(&sock->peer_address);
2893
2894         UNLOCK(&sock->lock);
2895
2896         closesocket(manager, sock, fd);
2897
2898         return (ISC_R_SUCCESS);
2899 }
2900 #endif  /* BIND9 */
2901
2902 /*
2903  * I/O is possible on a given socket.  Schedule an event to this task that
2904  * will call an internal function to do the I/O.  This will charge the
2905  * task with the I/O operation and let our select loop handler get back
2906  * to doing something real as fast as possible.
2907  *
2908  * The socket and manager must be locked before calling this function.
2909  */
2910 static void
2911 dispatch_recv(isc__socket_t *sock) {
2912         intev_t *iev;
2913         isc_socketevent_t *ev;
2914         isc_task_t *sender;
2915
2916         INSIST(!sock->pending_recv);
2917
2918         if (sock->type != isc_sockettype_fdwatch) {
2919                 ev = ISC_LIST_HEAD(sock->recv_list);
2920                 if (ev == NULL)
2921                         return;
2922                 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2923                            "dispatch_recv:  event %p -> task %p",
2924                            ev, ev->ev_sender);
2925                 sender = ev->ev_sender;
2926         } else {
2927                 sender = sock->fdwatchtask;
2928         }
2929
2930         sock->pending_recv = 1;
2931         iev = &sock->readable_ev;
2932
2933         sock->references++;
2934         iev->ev_sender = sock;
2935         if (sock->type == isc_sockettype_fdwatch)
2936                 iev->ev_action = internal_fdwatch_read;
2937         else
2938                 iev->ev_action = internal_recv;
2939         iev->ev_arg = sock;
2940
2941         isc_task_send(sender, (isc_event_t **)&iev);
2942 }
2943
2944 static void
2945 dispatch_send(isc__socket_t *sock) {
2946         intev_t *iev;
2947         isc_socketevent_t *ev;
2948         isc_task_t *sender;
2949
2950         INSIST(!sock->pending_send);
2951
2952         if (sock->type != isc_sockettype_fdwatch) {
2953                 ev = ISC_LIST_HEAD(sock->send_list);
2954                 if (ev == NULL)
2955                         return;
2956                 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2957                            "dispatch_send:  event %p -> task %p",
2958                            ev, ev->ev_sender);
2959                 sender = ev->ev_sender;
2960         } else {
2961                 sender = sock->fdwatchtask;
2962         }
2963
2964         sock->pending_send = 1;
2965         iev = &sock->writable_ev;
2966
2967         sock->references++;
2968         iev->ev_sender = sock;
2969         if (sock->type == isc_sockettype_fdwatch)
2970                 iev->ev_action = internal_fdwatch_write;
2971         else
2972                 iev->ev_action = internal_send;
2973         iev->ev_arg = sock;
2974
2975         isc_task_send(sender, (isc_event_t **)&iev);
2976 }
2977
2978 /*
2979  * Dispatch an internal accept event.
2980  */
2981 static void
2982 dispatch_accept(isc__socket_t *sock) {
2983         intev_t *iev;
2984         isc_socket_newconnev_t *ev;
2985
2986         INSIST(!sock->pending_accept);
2987
2988         /*
2989          * Are there any done events left, or were they all canceled
2990          * before the manager got the socket lock?
2991          */
2992         ev = ISC_LIST_HEAD(sock->accept_list);
2993         if (ev == NULL)
2994                 return;
2995
2996         sock->pending_accept = 1;
2997         iev = &sock->readable_ev;
2998
2999         sock->references++;  /* keep socket around for this internal event */
3000         iev->ev_sender = sock;
3001         iev->ev_action = internal_accept;
3002         iev->ev_arg = sock;
3003
3004         isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
3005 }
3006
3007 static void
3008 dispatch_connect(isc__socket_t *sock) {
3009         intev_t *iev;
3010         isc_socket_connev_t *ev;
3011
3012         iev = &sock->writable_ev;
3013
3014         ev = sock->connect_ev;
3015         INSIST(ev != NULL); /* XXX */
3016
3017         INSIST(sock->connecting);
3018
3019         sock->references++;  /* keep socket around for this internal event */
3020         iev->ev_sender = sock;
3021         iev->ev_action = internal_connect;
3022         iev->ev_arg = sock;
3023
3024         isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
3025 }
3026
3027 /*
3028  * Dequeue an item off the given socket's read queue, set the result code
3029  * in the done event to the one provided, and send it to the task it was
3030  * destined for.
3031  *
3032  * If the event to be sent is on a list, remove it before sending.  If
3033  * asked to, send and detach from the socket as well.
3034  *
3035  * Caller must have the socket locked if the event is attached to the socket.
3036  */
3037 static void
3038 send_recvdone_event(isc__socket_t *sock, isc_socketevent_t **dev) {
3039         isc_task_t *task;
3040
3041         task = (*dev)->ev_sender;
3042
3043         (*dev)->ev_sender = sock;
3044
3045         if (ISC_LINK_LINKED(*dev, ev_link))
3046                 ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
3047
3048         if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
3049             == ISC_SOCKEVENTATTR_ATTACHED)
3050                 isc_task_sendanddetach(&task, (isc_event_t **)dev);
3051         else
3052                 isc_task_send(task, (isc_event_t **)dev);
3053 }
3054
3055 /*
3056  * See comments for send_recvdone_event() above.
3057  *
3058  * Caller must have the socket locked if the event is attached to the socket.
3059  */
3060 static void
3061 send_senddone_event(isc__socket_t *sock, isc_socketevent_t **dev) {
3062         isc_task_t *task;
3063
3064         INSIST(dev != NULL && *dev != NULL);
3065
3066         task = (*dev)->ev_sender;
3067         (*dev)->ev_sender = sock;
3068
3069         if (ISC_LINK_LINKED(*dev, ev_link))
3070                 ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
3071
3072         if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
3073             == ISC_SOCKEVENTATTR_ATTACHED)
3074                 isc_task_sendanddetach(&task, (isc_event_t **)dev);
3075         else
3076                 isc_task_send(task, (isc_event_t **)dev);
3077 }
3078
3079 /*
3080  * Call accept() on a socket, to get the new file descriptor.  The listen
3081  * socket is used as a prototype to create a new isc_socket_t.  The new
3082  * socket has one outstanding reference.  The task receiving the event
3083  * will be detached from just after the event is delivered.
3084  *
3085  * On entry to this function, the event delivered is the internal
3086  * readable event, and the first item on the accept_list should be
3087  * the done event we want to send.  If the list is empty, this is a no-op,
3088  * so just unlock and return.
3089  */
3090 static void
3091 internal_accept(isc_task_t *me, isc_event_t *ev) {
3092         isc__socket_t *sock;
3093         isc__socketmgr_t *manager;
3094         isc_socket_newconnev_t *dev;
3095         isc_task_t *task;
3096         ISC_SOCKADDR_LEN_T addrlen;
3097         int fd;
3098         isc_result_t result = ISC_R_SUCCESS;
3099         char strbuf[ISC_STRERRORSIZE];
3100         const char *err = "accept";
3101
3102         UNUSED(me);
3103
3104         sock = ev->ev_sender;
3105         INSIST(VALID_SOCKET(sock));
3106
3107         LOCK(&sock->lock);
3108         socket_log(sock, NULL, TRACE,
3109                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
3110                    "internal_accept called, locked socket");
3111
3112         manager = sock->manager;
3113         INSIST(VALID_MANAGER(manager));
3114
3115         INSIST(sock->listener);
3116         INSIST(sock->pending_accept == 1);
3117         sock->pending_accept = 0;
3118
3119         INSIST(sock->references > 0);
3120         sock->references--;  /* the internal event is done with this socket */
3121         if (sock->references == 0) {
3122                 UNLOCK(&sock->lock);
3123                 destroy(&sock);
3124                 return;
3125         }
3126
3127         /*
3128          * Get the first item off the accept list.
3129          * If it is empty, unlock the socket and return.
3130          */
3131         dev = ISC_LIST_HEAD(sock->accept_list);
3132         if (dev == NULL) {
3133                 UNLOCK(&sock->lock);
3134                 return;
3135         }
3136
3137         /*
3138          * Try to accept the new connection.  If the accept fails with
3139          * EAGAIN or EINTR, simply poke the watcher to watch this socket
3140          * again.  Also ignore ECONNRESET, which has been reported to
3141          * be spuriously returned on Linux 2.2.19 although it is not
3142          * a documented error for accept().  ECONNABORTED has been
3143          * reported for Solaris 8.  The rest are thrown in not because
3144          * we have seen them but because they are ignored by other
3145          * daemons such as BIND 8 and Apache.
3146          */
3147
3148         addrlen = sizeof(NEWCONNSOCK(dev)->peer_address.type);
3149         memset(&NEWCONNSOCK(dev)->peer_address.type, 0, addrlen);
3150         fd = accept(sock->fd, &NEWCONNSOCK(dev)->peer_address.type.sa,
3151                     (void *)&addrlen);
3152
3153 #ifdef F_DUPFD
3154         /*
3155          * Leave a space for stdio to work in.
3156          */
3157         if (fd >= 0 && fd < 20) {
3158                 int new, tmp;
3159                 new = fcntl(fd, F_DUPFD, 20);
3160                 tmp = errno;
3161                 (void)close(fd);
3162                 errno = tmp;
3163                 fd = new;
3164                 err = "accept/fcntl";
3165         }
3166 #endif
3167
3168         if (fd < 0) {
3169                 if (SOFT_ERROR(errno))
3170                         goto soft_error;
3171                 switch (errno) {
3172                 case ENFILE:
3173                 case EMFILE:
3174                         isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3175                                        ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3176                                        isc_msgcat, ISC_MSGSET_SOCKET,
3177                                        ISC_MSG_TOOMANYFDS,
3178                                        "%s: too many open file descriptors",
3179                                        err);
3180                         goto soft_error;
3181
3182                 case ENOBUFS:
3183                 case ENOMEM:
3184                 case ECONNRESET:
3185                 case ECONNABORTED:
3186                 case EHOSTUNREACH:
3187                 case EHOSTDOWN:
3188                 case ENETUNREACH:
3189                 case ENETDOWN:
3190                 case ECONNREFUSED:
3191 #ifdef EPROTO
3192                 case EPROTO:
3193 #endif
3194 #ifdef ENONET
3195                 case ENONET:
3196 #endif
3197                         goto soft_error;
3198                 default:
3199                         break;
3200                 }
3201                 isc__strerror(errno, strbuf, sizeof(strbuf));
3202                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3203                                  "internal_accept: %s() %s: %s", err,
3204                                  isc_msgcat_get(isc_msgcat,
3205                                                 ISC_MSGSET_GENERAL,
3206                                                 ISC_MSG_FAILED,
3207                                                 "failed"),
3208                                  strbuf);
3209                 fd = -1;
3210                 result = ISC_R_UNEXPECTED;
3211         } else {
3212                 if (addrlen == 0U) {
3213                         UNEXPECTED_ERROR(__FILE__, __LINE__,
3214                                          "internal_accept(): "
3215                                          "accept() failed to return "
3216                                          "remote address");
3217
3218                         (void)close(fd);
3219                         goto soft_error;
3220                 } else if (NEWCONNSOCK(dev)->peer_address.type.sa.sa_family !=
3221                            sock->pf)
3222                 {
3223                         UNEXPECTED_ERROR(__FILE__, __LINE__,
3224                                          "internal_accept(): "
3225                                          "accept() returned peer address "
3226                                          "family %u (expected %u)",
3227                                          NEWCONNSOCK(dev)->peer_address.
3228                                          type.sa.sa_family,
3229                                          sock->pf);
3230                         (void)close(fd);
3231                         goto soft_error;
3232                 } else if (fd >= (int)manager->maxsocks) {
3233                         isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3234                                        ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3235                                        isc_msgcat, ISC_MSGSET_SOCKET,
3236                                        ISC_MSG_TOOMANYFDS,
3237                                        "accept: "
3238                                        "file descriptor exceeds limit (%d/%u)",
3239                                        fd, manager->maxsocks);
3240                         (void)close(fd);
3241                         goto soft_error;
3242                 }
3243         }
3244
3245         if (fd != -1) {
3246                 NEWCONNSOCK(dev)->peer_address.length = addrlen;
3247                 NEWCONNSOCK(dev)->pf = sock->pf;
3248         }
3249
3250         /*
3251          * Pull off the done event.
3252          */
3253         ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
3254
3255         /*
3256          * Poke watcher if there are more pending accepts.
3257          */
3258         if (!ISC_LIST_EMPTY(sock->accept_list))
3259                 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
3260
3261         UNLOCK(&sock->lock);
3262
3263         if (fd != -1) {
3264                 result = make_nonblock(fd);
3265                 if (result != ISC_R_SUCCESS) {
3266                         (void)close(fd);
3267                         fd = -1;
3268                 }
3269         }
3270
3271         /*
3272          * -1 means the new socket didn't happen.
3273          */
3274         if (fd != -1) {
3275                 int lockid = FDLOCK_ID(fd);
3276
3277                 NEWCONNSOCK(dev)->fd = fd;
3278                 NEWCONNSOCK(dev)->bound = 1;
3279                 NEWCONNSOCK(dev)->connected = 1;
3280
3281                 /*
3282                  * Use minimum mtu if possible.
3283                  */
3284                 use_min_mtu(NEWCONNSOCK(dev));
3285
3286                 /*
3287                  * Save away the remote address
3288                  */
3289                 dev->address = NEWCONNSOCK(dev)->peer_address;
3290
3291                 LOCK(&manager->fdlock[lockid]);
3292                 manager->fds[fd] = NEWCONNSOCK(dev);
3293                 manager->fdstate[fd] = MANAGED;
3294                 UNLOCK(&manager->fdlock[lockid]);
3295
3296                 LOCK(&manager->lock);
3297
3298 #ifdef USE_SELECT
3299                 if (manager->maxfd < fd)
3300                         manager->maxfd = fd;
3301 #endif
3302
3303                 socket_log(sock, &NEWCONNSOCK(dev)->peer_address, CREATION,
3304                            isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
3305                            "accepted connection, new socket %p",
3306                            dev->newsocket);
3307
3308                 ISC_LIST_APPEND(manager->socklist, NEWCONNSOCK(dev), link);
3309
3310                 UNLOCK(&manager->lock);
3311
3312                 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPT]);
3313         } else {
3314                 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
3315                 NEWCONNSOCK(dev)->references--;
3316                 free_socket((isc__socket_t **)&dev->newsocket);
3317         }
3318
3319         /*
3320          * Fill in the done event details and send it off.
3321          */
3322         dev->result = result;
3323         task = dev->ev_sender;
3324         dev->ev_sender = sock;
3325
3326         isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
3327         return;
3328
3329  soft_error:
3330         select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
3331         UNLOCK(&sock->lock);
3332
3333         inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
3334         return;
3335 }
3336
3337 static void
3338 internal_recv(isc_task_t *me, isc_event_t *ev) {
3339         isc_socketevent_t *dev;
3340         isc__socket_t *sock;
3341
3342         INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
3343
3344         sock = ev->ev_sender;
3345         INSIST(VALID_SOCKET(sock));
3346
3347         LOCK(&sock->lock);
3348         socket_log(sock, NULL, IOEVENT,
3349                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
3350                    "internal_recv: task %p got event %p", me, ev);
3351
3352         INSIST(sock->pending_recv == 1);
3353         sock->pending_recv = 0;
3354
3355         INSIST(sock->references > 0);
3356         sock->references--;  /* the internal event is done with this socket */
3357         if (sock->references == 0) {
3358                 UNLOCK(&sock->lock);
3359                 destroy(&sock);
3360                 return;
3361         }
3362
3363         /*
3364          * Try to do as much I/O as possible on this socket.  There are no
3365          * limits here, currently.
3366          */
3367         dev = ISC_LIST_HEAD(sock->recv_list);
3368         while (dev != NULL) {
3369                 switch (doio_recv(sock, dev)) {
3370                 case DOIO_SOFT:
3371                         goto poke;
3372
3373                 case DOIO_EOF:
3374                         /*
3375                          * read of 0 means the remote end was closed.
3376                          * Run through the event queue and dispatch all
3377                          * the events with an EOF result code.
3378                          */
3379                         do {
3380                                 dev->result = ISC_R_EOF;
3381                                 send_recvdone_event(sock, &dev);
3382                                 dev = ISC_LIST_HEAD(sock->recv_list);
3383                         } while (dev != NULL);
3384                         goto poke;
3385
3386                 case DOIO_SUCCESS:
3387                 case DOIO_HARD:
3388                         send_recvdone_event(sock, &dev);
3389                         break;
3390                 }
3391
3392                 dev = ISC_LIST_HEAD(sock->recv_list);
3393         }
3394
3395  poke:
3396         if (!ISC_LIST_EMPTY(sock->recv_list))
3397                 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
3398
3399         UNLOCK(&sock->lock);
3400 }
3401
3402 static void
3403 internal_send(isc_task_t *me, isc_event_t *ev) {
3404         isc_socketevent_t *dev;
3405         isc__socket_t *sock;
3406
3407         INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
3408
3409         /*
3410          * Find out what socket this is and lock it.
3411          */
3412         sock = (isc__socket_t *)ev->ev_sender;
3413         INSIST(VALID_SOCKET(sock));
3414
3415         LOCK(&sock->lock);
3416         socket_log(sock, NULL, IOEVENT,
3417                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
3418                    "internal_send: task %p got event %p", me, ev);
3419
3420         INSIST(sock->pending_send == 1);
3421         sock->pending_send = 0;
3422
3423         INSIST(sock->references > 0);
3424         sock->references--;  /* the internal event is done with this socket */
3425         if (sock->references == 0) {
3426                 UNLOCK(&sock->lock);
3427                 destroy(&sock);
3428                 return;
3429         }
3430
3431         /*
3432          * Try to do as much I/O as possible on this socket.  There are no
3433          * limits here, currently.
3434          */
3435         dev = ISC_LIST_HEAD(sock->send_list);
3436         while (dev != NULL) {
3437                 switch (doio_send(sock, dev)) {
3438                 case DOIO_SOFT:
3439                         goto poke;
3440
3441                 case DOIO_HARD:
3442                 case DOIO_SUCCESS:
3443                         send_senddone_event(sock, &dev);
3444                         break;
3445                 }
3446
3447                 dev = ISC_LIST_HEAD(sock->send_list);
3448         }
3449
3450  poke:
3451         if (!ISC_LIST_EMPTY(sock->send_list))
3452                 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
3453
3454         UNLOCK(&sock->lock);
3455 }
3456
3457 static void
3458 internal_fdwatch_write(isc_task_t *me, isc_event_t *ev) {
3459         isc__socket_t *sock;
3460         int more_data;
3461
3462         INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
3463
3464         /*
3465          * Find out what socket this is and lock it.
3466          */
3467         sock = (isc__socket_t *)ev->ev_sender;
3468         INSIST(VALID_SOCKET(sock));
3469
3470         LOCK(&sock->lock);
3471         socket_log(sock, NULL, IOEVENT,
3472                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
3473                    "internal_fdwatch_write: task %p got event %p", me, ev);
3474
3475         INSIST(sock->pending_send == 1);
3476
3477         UNLOCK(&sock->lock);
3478         more_data = (sock->fdwatchcb)(me, (isc_socket_t *)sock,
3479                                       sock->fdwatcharg, ISC_SOCKFDWATCH_WRITE);
3480         LOCK(&sock->lock);
3481
3482         sock->pending_send = 0;
3483
3484         INSIST(sock->references > 0);
3485         sock->references--;  /* the internal event is done with this socket */
3486         if (sock->references == 0) {
3487                 UNLOCK(&sock->lock);
3488                 destroy(&sock);
3489                 return;
3490         }
3491
3492         if (more_data)
3493                 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
3494
3495         UNLOCK(&sock->lock);
3496 }
3497
3498 static void
3499 internal_fdwatch_read(isc_task_t *me, isc_event_t *ev) {
3500         isc__socket_t *sock;
3501         int more_data;
3502
3503         INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
3504
3505         /*
3506          * Find out what socket this is and lock it.
3507          */
3508         sock = (isc__socket_t *)ev->ev_sender;
3509         INSIST(VALID_SOCKET(sock));
3510
3511         LOCK(&sock->lock);
3512         socket_log(sock, NULL, IOEVENT,
3513                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
3514                    "internal_fdwatch_read: task %p got event %p", me, ev);
3515
3516         INSIST(sock->pending_recv == 1);
3517
3518         UNLOCK(&sock->lock);
3519         more_data = (sock->fdwatchcb)(me, (isc_socket_t *)sock,
3520                                       sock->fdwatcharg, ISC_SOCKFDWATCH_READ);
3521         LOCK(&sock->lock);
3522
3523         sock->pending_recv = 0;
3524
3525         INSIST(sock->references > 0);
3526         sock->references--;  /* the internal event is done with this socket */
3527         if (sock->references == 0) {
3528                 UNLOCK(&sock->lock);
3529                 destroy(&sock);
3530                 return;
3531         }
3532
3533         if (more_data)
3534                 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
3535
3536         UNLOCK(&sock->lock);
3537 }
3538
3539 /*
3540  * Process read/writes on each fd here.  Avoid locking
3541  * and unlocking twice if both reads and writes are possible.
3542  */
3543 static void
3544 process_fd(isc__socketmgr_t *manager, int fd, isc_boolean_t readable,
3545            isc_boolean_t writeable)
3546 {
3547         isc__socket_t *sock;
3548         isc_boolean_t unlock_sock;
3549         isc_boolean_t unwatch_read = ISC_FALSE, unwatch_write = ISC_FALSE;
3550         int lockid = FDLOCK_ID(fd);
3551
3552         /*
3553          * If the socket is going to be closed, don't do more I/O.
3554          */
3555         LOCK(&manager->fdlock[lockid]);
3556         if (manager->fdstate[fd] == CLOSE_PENDING) {
3557                 UNLOCK(&manager->fdlock[lockid]);
3558
3559                 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
3560                 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
3561                 return;
3562         }
3563
3564         sock = manager->fds[fd];
3565         unlock_sock = ISC_FALSE;
3566         if (readable) {
3567                 if (sock == NULL) {
3568                         unwatch_read = ISC_TRUE;
3569                         goto check_write;
3570                 }
3571                 unlock_sock = ISC_TRUE;
3572                 LOCK(&sock->lock);
3573                 if (!SOCK_DEAD(sock)) {
3574                         if (sock->listener)
3575                                 dispatch_accept(sock);
3576                         else
3577                                 dispatch_recv(sock);
3578                 }
3579                 unwatch_read = ISC_TRUE;
3580         }
3581 check_write:
3582         if (writeable) {
3583                 if (sock == NULL) {
3584                         unwatch_write = ISC_TRUE;
3585                         goto unlock_fd;
3586                 }
3587                 if (!unlock_sock) {
3588                         unlock_sock = ISC_TRUE;
3589                         LOCK(&sock->lock);
3590                 }
3591                 if (!SOCK_DEAD(sock)) {
3592                         if (sock->connecting)
3593                                 dispatch_connect(sock);
3594                         else
3595                                 dispatch_send(sock);
3596                 }
3597                 unwatch_write = ISC_TRUE;
3598         }
3599         if (unlock_sock)
3600                 UNLOCK(&sock->lock);
3601
3602  unlock_fd:
3603         UNLOCK(&manager->fdlock[lockid]);
3604         if (unwatch_read)
3605                 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
3606         if (unwatch_write)
3607                 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
3608
3609 }
3610
3611 #ifdef USE_KQUEUE
3612 static isc_boolean_t
3613 process_fds(isc__socketmgr_t *manager, struct kevent *events, int nevents) {
3614         int i;
3615         isc_boolean_t readable, writable;
3616         isc_boolean_t done = ISC_FALSE;
3617 #ifdef USE_WATCHER_THREAD
3618         isc_boolean_t have_ctlevent = ISC_FALSE;
3619 #endif
3620
3621         if (nevents == manager->nevents) {
3622                 /*
3623                  * This is not an error, but something unexpected.  If this
3624                  * happens, it may indicate the need for increasing
3625                  * ISC_SOCKET_MAXEVENTS.
3626                  */
3627                 manager_log(manager, ISC_LOGCATEGORY_GENERAL,
3628                             ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
3629                             "maximum number of FD events (%d) received",
3630                             nevents);
3631         }
3632
3633         for (i = 0; i < nevents; i++) {
3634                 REQUIRE(events[i].ident < manager->maxsocks);
3635 #ifdef USE_WATCHER_THREAD
3636                 if (events[i].ident == (uintptr_t)manager->pipe_fds[0]) {
3637                         have_ctlevent = ISC_TRUE;
3638                         continue;
3639                 }
3640 #endif
3641                 readable = ISC_TF(events[i].filter == EVFILT_READ);
3642                 writable = ISC_TF(events[i].filter == EVFILT_WRITE);
3643                 process_fd(manager, events[i].ident, readable, writable);
3644         }
3645
3646 #ifdef USE_WATCHER_THREAD
3647         if (have_ctlevent)
3648                 done = process_ctlfd(manager);
3649 #endif
3650
3651         return (done);
3652 }
3653 #elif defined(USE_EPOLL)
3654 static isc_boolean_t
3655 process_fds(isc__socketmgr_t *manager, struct epoll_event *events, int nevents)
3656 {
3657         int i;
3658         isc_boolean_t done = ISC_FALSE;
3659 #ifdef USE_WATCHER_THREAD
3660         isc_boolean_t have_ctlevent = ISC_FALSE;
3661 #endif
3662
3663         if (nevents == manager->nevents) {
3664                 manager_log(manager, ISC_LOGCATEGORY_GENERAL,
3665                             ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
3666                             "maximum number of FD events (%d) received",
3667                             nevents);
3668         }
3669
3670         for (i = 0; i < nevents; i++) {
3671                 REQUIRE(events[i].data.fd < (int)manager->maxsocks);
3672 #ifdef USE_WATCHER_THREAD
3673                 if (events[i].data.fd == manager->pipe_fds[0]) {
3674                         have_ctlevent = ISC_TRUE;
3675                         continue;
3676                 }
3677 #endif
3678                 if ((events[i].events & EPOLLERR) != 0 ||
3679                     (events[i].events & EPOLLHUP) != 0) {
3680                         /*
3681                          * epoll does not set IN/OUT bits on an erroneous
3682                          * condition, so we need to try both anyway.  This is a
3683                          * bit inefficient, but should be okay for such rare
3684                          * events.  Note also that the read or write attempt
3685                          * won't block because we use non-blocking sockets.
3686                          */
3687                         events[i].events |= (EPOLLIN | EPOLLOUT);
3688                 }
3689                 process_fd(manager, events[i].data.fd,
3690                            (events[i].events & EPOLLIN) != 0,
3691                            (events[i].events & EPOLLOUT) != 0);
3692         }
3693
3694 #ifdef USE_WATCHER_THREAD
3695         if (have_ctlevent)
3696                 done = process_ctlfd(manager);
3697 #endif
3698
3699         return (done);
3700 }
3701 #elif defined(USE_DEVPOLL)
3702 static isc_boolean_t
3703 process_fds(isc__socketmgr_t *manager, struct pollfd *events, int nevents) {
3704         int i;
3705         isc_boolean_t done = ISC_FALSE;
3706 #ifdef USE_WATCHER_THREAD
3707         isc_boolean_t have_ctlevent = ISC_FALSE;
3708 #endif
3709
3710         if (nevents == manager->nevents) {
3711                 manager_log(manager, ISC_LOGCATEGORY_GENERAL,
3712                             ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
3713                             "maximum number of FD events (%d) received",
3714                             nevents);
3715         }
3716
3717         for (i = 0; i < nevents; i++) {
3718                 REQUIRE(events[i].fd < (int)manager->maxsocks);
3719 #ifdef USE_WATCHER_THREAD
3720                 if (events[i].fd == manager->pipe_fds[0]) {
3721                         have_ctlevent = ISC_TRUE;
3722                         continue;
3723                 }
3724 #endif
3725                 process_fd(manager, events[i].fd,
3726                            (events[i].events & POLLIN) != 0,
3727                            (events[i].events & POLLOUT) != 0);
3728         }
3729
3730 #ifdef USE_WATCHER_THREAD
3731         if (have_ctlevent)
3732                 done = process_ctlfd(manager);
3733 #endif
3734
3735         return (done);
3736 }
3737 #elif defined(USE_SELECT)
3738 static void
3739 process_fds(isc__socketmgr_t *manager, int maxfd, fd_set *readfds,
3740             fd_set *writefds)
3741 {
3742         int i;
3743
3744         REQUIRE(maxfd <= (int)manager->maxsocks);
3745
3746         for (i = 0; i < maxfd; i++) {
3747 #ifdef USE_WATCHER_THREAD
3748                 if (i == manager->pipe_fds[0] || i == manager->pipe_fds[1])
3749                         continue;
3750 #endif /* USE_WATCHER_THREAD */
3751                 process_fd(manager, i, FD_ISSET(i, readfds),
3752                            FD_ISSET(i, writefds));
3753         }
3754 }
3755 #endif
3756
3757 #ifdef USE_WATCHER_THREAD
3758 static isc_boolean_t
3759 process_ctlfd(isc__socketmgr_t *manager) {
3760         int msg, fd;
3761
3762         for (;;) {
3763                 select_readmsg(manager, &fd, &msg);
3764
3765                 manager_log(manager, IOEVENT,
3766                             isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
3767                                            ISC_MSG_WATCHERMSG,
3768                                            "watcher got message %d "
3769                                            "for socket %d"), msg, fd);
3770
3771                 /*
3772                  * Nothing to read?
3773                  */
3774                 if (msg == SELECT_POKE_NOTHING)
3775                         break;
3776
3777                 /*
3778                  * Handle shutdown message.  We really should
3779                  * jump out of this loop right away, but
3780                  * it doesn't matter if we have to do a little
3781                  * more work first.
3782                  */
3783                 if (msg == SELECT_POKE_SHUTDOWN)
3784                         return (ISC_TRUE);
3785
3786                 /*
3787                  * This is a wakeup on a socket.  Look
3788                  * at the event queue for both read and write,
3789                  * and decide if we need to watch on it now
3790                  * or not.
3791                  */
3792                 wakeup_socket(manager, fd, msg);
3793         }
3794
3795         return (ISC_FALSE);
3796 }
3797
3798 /*
3799  * This is the thread that will loop forever, always in a select or poll
3800  * call.
3801  *
3802  * When select returns something to do, track down what thread gets to do
3803  * this I/O and post the event to it.
3804  */
3805 static isc_threadresult_t
3806 watcher(void *uap) {
3807         isc__socketmgr_t *manager = uap;
3808         isc_boolean_t done;
3809         int cc;
3810 #ifdef USE_KQUEUE
3811         const char *fnname = "kevent()";
3812 #elif defined (USE_EPOLL)
3813         const char *fnname = "epoll_wait()";
3814 #elif defined(USE_DEVPOLL)
3815         const char *fnname = "ioctl(DP_POLL)";
3816         struct dvpoll dvp;
3817 #elif defined (USE_SELECT)
3818         const char *fnname = "select()";
3819         int maxfd;
3820         int ctlfd;
3821 #endif
3822         char strbuf[ISC_STRERRORSIZE];
3823 #ifdef ISC_SOCKET_USE_POLLWATCH
3824         pollstate_t pollstate = poll_idle;
3825 #endif
3826
3827 #if defined (USE_SELECT)
3828         /*
3829          * Get the control fd here.  This will never change.
3830          */
3831         ctlfd = manager->pipe_fds[0];
3832 #endif
3833         done = ISC_FALSE;
3834         while (!done) {
3835                 do {
3836 #ifdef USE_KQUEUE
3837                         cc = kevent(manager->kqueue_fd, NULL, 0,
3838                                     manager->events, manager->nevents, NULL);
3839 #elif defined(USE_EPOLL)
3840                         cc = epoll_wait(manager->epoll_fd, manager->events,
3841                                         manager->nevents, -1);
3842 #elif defined(USE_DEVPOLL)
3843                         dvp.dp_fds = manager->events;
3844                         dvp.dp_nfds = manager->nevents;
3845 #ifndef ISC_SOCKET_USE_POLLWATCH
3846                         dvp.dp_timeout = -1;
3847 #else
3848                         if (pollstate == poll_idle)
3849                                 dvp.dp_timeout = -1;
3850                         else
3851                                 dvp.dp_timeout = ISC_SOCKET_POLLWATCH_TIMEOUT;
3852 #endif  /* ISC_SOCKET_USE_POLLWATCH */
3853                         cc = ioctl(manager->devpoll_fd, DP_POLL, &dvp);
3854 #elif defined(USE_SELECT)
3855                         LOCK(&manager->lock);
3856                         memcpy(manager->read_fds_copy, manager->read_fds,
3857                                manager->fd_bufsize);
3858                         memcpy(manager->write_fds_copy, manager->write_fds,
3859                                manager->fd_bufsize);
3860                         maxfd = manager->maxfd + 1;
3861                         UNLOCK(&manager->lock);
3862
3863                         cc = select(maxfd, manager->read_fds_copy,
3864                                     manager->write_fds_copy, NULL, NULL);
3865 #endif  /* USE_KQUEUE */
3866
3867                         if (cc < 0 && !SOFT_ERROR(errno)) {
3868                                 isc__strerror(errno, strbuf, sizeof(strbuf));
3869                                 FATAL_ERROR(__FILE__, __LINE__,
3870                                             "%s %s: %s", fnname,
3871                                             isc_msgcat_get(isc_msgcat,
3872                                                            ISC_MSGSET_GENERAL,
3873                                                            ISC_MSG_FAILED,
3874                                                            "failed"), strbuf);
3875                         }
3876
3877 #if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH)
3878                         if (cc == 0) {
3879                                 if (pollstate == poll_active)
3880                                         pollstate = poll_checking;
3881                                 else if (pollstate == poll_checking)
3882                                         pollstate = poll_idle;
3883                         } else if (cc > 0) {
3884                                 if (pollstate == poll_checking) {
3885                                         /*
3886                                          * XXX: We'd like to use a more
3887                                          * verbose log level as it's actually an
3888                                          * unexpected event, but the kernel bug
3889                                          * reportedly happens pretty frequently
3890                                          * (and it can also be a false positive)
3891                                          * so it would be just too noisy.
3892                                          */
3893                                         manager_log(manager,
3894                                                     ISC_LOGCATEGORY_GENERAL,
3895                                                     ISC_LOGMODULE_SOCKET,
3896                                                     ISC_LOG_DEBUG(1),
3897                                                     "unexpected POLL timeout");
3898                                 }
3899                                 pollstate = poll_active;
3900                         }
3901 #endif
3902                 } while (cc < 0);
3903
3904 #if defined(USE_KQUEUE) || defined (USE_EPOLL) || defined (USE_DEVPOLL)
3905                 done = process_fds(manager, manager->events, cc);
3906 #elif defined(USE_SELECT)
3907                 process_fds(manager, maxfd, manager->read_fds_copy,
3908                             manager->write_fds_copy);
3909
3910                 /*
3911                  * Process reads on internal, control fd.
3912                  */
3913                 if (FD_ISSET(ctlfd, manager->read_fds_copy))
3914                         done = process_ctlfd(manager);
3915 #endif
3916         }
3917
3918         manager_log(manager, TRACE, "%s",
3919                     isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3920                                    ISC_MSG_EXITING, "watcher exiting"));
3921
3922         return ((isc_threadresult_t)0);
3923 }
3924 #endif /* USE_WATCHER_THREAD */
3925
3926 #ifdef BIND9
3927 ISC_SOCKETFUNC_SCOPE void
3928 isc__socketmgr_setreserved(isc_socketmgr_t *manager0, isc_uint32_t reserved) {
3929         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
3930
3931         REQUIRE(VALID_MANAGER(manager));
3932
3933         manager->reserved = reserved;
3934 }
3935
3936 ISC_SOCKETFUNC_SCOPE void
3937 isc___socketmgr_maxudp(isc_socketmgr_t *manager0, int maxudp) {
3938         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
3939
3940         REQUIRE(VALID_MANAGER(manager));
3941
3942         manager->maxudp = maxudp;
3943 }
3944 #endif  /* BIND9 */
3945
3946 /*
3947  * Create a new socket manager.
3948  */
3949
3950 static isc_result_t
3951 setup_watcher(isc_mem_t *mctx, isc__socketmgr_t *manager) {
3952         isc_result_t result;
3953 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
3954         char strbuf[ISC_STRERRORSIZE];
3955 #endif
3956
3957 #ifdef USE_KQUEUE
3958         manager->nevents = ISC_SOCKET_MAXEVENTS;
3959         manager->events = isc_mem_get(mctx, sizeof(struct kevent) *
3960                                       manager->nevents);
3961         if (manager->events == NULL)
3962                 return (ISC_R_NOMEMORY);
3963         manager->kqueue_fd = kqueue();
3964         if (manager->kqueue_fd == -1) {
3965                 result = isc__errno2result(errno);
3966                 isc__strerror(errno, strbuf, sizeof(strbuf));
3967                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3968                                  "kqueue %s: %s",
3969                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3970                                                 ISC_MSG_FAILED, "failed"),
3971                                  strbuf);
3972                 isc_mem_put(mctx, manager->events,
3973                             sizeof(struct kevent) * manager->nevents);
3974                 return (result);
3975         }
3976
3977 #ifdef USE_WATCHER_THREAD
3978         result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
3979         if (result != ISC_R_SUCCESS) {
3980                 close(manager->kqueue_fd);
3981                 isc_mem_put(mctx, manager->events,
3982                             sizeof(struct kevent) * manager->nevents);
3983                 return (result);
3984         }
3985 #endif  /* USE_WATCHER_THREAD */
3986 #elif defined(USE_EPOLL)
3987         manager->nevents = ISC_SOCKET_MAXEVENTS;
3988         manager->events = isc_mem_get(mctx, sizeof(struct epoll_event) *
3989                                       manager->nevents);
3990         if (manager->events == NULL)
3991                 return (ISC_R_NOMEMORY);
3992         manager->epoll_fd = epoll_create(manager->nevents);
3993         if (manager->epoll_fd == -1) {
3994                 result = isc__errno2result(errno);
3995                 isc__strerror(errno, strbuf, sizeof(strbuf));
3996                 UNEXPECTED_ERROR(__FILE__, __LINE__,
3997                                  "epoll_create %s: %s",
3998                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3999                                                 ISC_MSG_FAILED, "failed"),
4000                                  strbuf);
4001                 isc_mem_put(mctx, manager->events,
4002                             sizeof(struct epoll_event) * manager->nevents);
4003                 return (result);
4004         }
4005 #ifdef USE_WATCHER_THREAD
4006         result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
4007         if (result != ISC_R_SUCCESS) {
4008                 close(manager->epoll_fd);
4009                 isc_mem_put(mctx, manager->events,
4010                             sizeof(struct epoll_event) * manager->nevents);
4011                 return (result);
4012         }
4013 #endif  /* USE_WATCHER_THREAD */
4014 #elif defined(USE_DEVPOLL)
4015         /*
4016          * XXXJT: /dev/poll seems to reject large numbers of events,
4017          * so we should be careful about redefining ISC_SOCKET_MAXEVENTS.
4018          */
4019         manager->nevents = ISC_SOCKET_MAXEVENTS;
4020         manager->events = isc_mem_get(mctx, sizeof(struct pollfd) *
4021                                       manager->nevents);
4022         if (manager->events == NULL)
4023                 return (ISC_R_NOMEMORY);
4024         /*
4025          * Note: fdpollinfo should be able to support all possible FDs, so
4026          * it must have maxsocks entries (not nevents).
4027          */
4028         manager->fdpollinfo = isc_mem_get(mctx, sizeof(pollinfo_t) *
4029                                           manager->maxsocks);
4030         if (manager->fdpollinfo == NULL) {
4031                 isc_mem_put(mctx, manager->events,
4032                             sizeof(struct pollfd) * manager->nevents);
4033                 return (ISC_R_NOMEMORY);
4034         }
4035         memset(manager->fdpollinfo, 0, sizeof(pollinfo_t) * manager->maxsocks);
4036         manager->devpoll_fd = open("/dev/poll", O_RDWR);
4037         if (manager->devpoll_fd == -1) {
4038                 result = isc__errno2result(errno);
4039                 isc__strerror(errno, strbuf, sizeof(strbuf));
4040                 UNEXPECTED_ERROR(__FILE__, __LINE__,
4041                                  "open(/dev/poll) %s: %s",
4042                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4043                                                 ISC_MSG_FAILED, "failed"),
4044                                  strbuf);
4045                 isc_mem_put(mctx, manager->events,
4046                             sizeof(struct pollfd) * manager->nevents);
4047                 isc_mem_put(mctx, manager->fdpollinfo,
4048                             sizeof(pollinfo_t) * manager->maxsocks);
4049                 return (result);
4050         }
4051 #ifdef USE_WATCHER_THREAD
4052         result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
4053         if (result != ISC_R_SUCCESS) {
4054                 close(manager->devpoll_fd);
4055                 isc_mem_put(mctx, manager->events,
4056                             sizeof(struct pollfd) * manager->nevents);
4057                 isc_mem_put(mctx, manager->fdpollinfo,
4058                             sizeof(pollinfo_t) * manager->maxsocks);
4059                 return (result);
4060         }
4061 #endif  /* USE_WATCHER_THREAD */
4062 #elif defined(USE_SELECT)
4063         UNUSED(result);
4064
4065 #if ISC_SOCKET_MAXSOCKETS > FD_SETSIZE
4066         /*
4067          * Note: this code should also cover the case of MAXSOCKETS <=
4068          * FD_SETSIZE, but we separate the cases to avoid possible portability
4069          * issues regarding howmany() and the actual representation of fd_set.
4070          */
4071         manager->fd_bufsize = howmany(manager->maxsocks, NFDBITS) *
4072                 sizeof(fd_mask);
4073 #else
4074         manager->fd_bufsize = sizeof(fd_set);
4075 #endif
4076
4077         manager->read_fds = NULL;
4078         manager->read_fds_copy = NULL;
4079         manager->write_fds = NULL;
4080         manager->write_fds_copy = NULL;
4081
4082         manager->read_fds = isc_mem_get(mctx, manager->fd_bufsize);
4083         if (manager->read_fds != NULL)
4084                 manager->read_fds_copy = isc_mem_get(mctx, manager->fd_bufsize);
4085         if (manager->read_fds_copy != NULL)
4086                 manager->write_fds = isc_mem_get(mctx, manager->fd_bufsize);
4087         if (manager->write_fds != NULL) {
4088                 manager->write_fds_copy = isc_mem_get(mctx,
4089                                                       manager->fd_bufsize);
4090         }
4091         if (manager->write_fds_copy == NULL) {
4092                 if (manager->write_fds != NULL) {
4093                         isc_mem_put(mctx, manager->write_fds,
4094                                     manager->fd_bufsize);
4095                 }
4096                 if (manager->read_fds_copy != NULL) {
4097                         isc_mem_put(mctx, manager->read_fds_copy,
4098                                     manager->fd_bufsize);
4099                 }
4100                 if (manager->read_fds != NULL) {
4101                         isc_mem_put(mctx, manager->read_fds,
4102                                     manager->fd_bufsize);
4103                 }
4104                 return (ISC_R_NOMEMORY);
4105         }
4106         memset(manager->read_fds, 0, manager->fd_bufsize);
4107         memset(manager->write_fds, 0, manager->fd_bufsize);
4108
4109 #ifdef USE_WATCHER_THREAD
4110         (void)watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
4111         manager->maxfd = manager->pipe_fds[0];
4112 #else /* USE_WATCHER_THREAD */
4113         manager->maxfd = 0;
4114 #endif /* USE_WATCHER_THREAD */
4115 #endif  /* USE_KQUEUE */
4116
4117         return (ISC_R_SUCCESS);
4118 }
4119
4120 static void
4121 cleanup_watcher(isc_mem_t *mctx, isc__socketmgr_t *manager) {
4122 #ifdef USE_WATCHER_THREAD
4123         isc_result_t result;
4124
4125         result = unwatch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
4126         if (result != ISC_R_SUCCESS) {
4127                 UNEXPECTED_ERROR(__FILE__, __LINE__,
4128                                  "epoll_ctl(DEL) %s",
4129                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4130                                                 ISC_MSG_FAILED, "failed"));
4131         }
4132 #endif  /* USE_WATCHER_THREAD */
4133
4134 #ifdef USE_KQUEUE
4135         close(manager->kqueue_fd);
4136         isc_mem_put(mctx, manager->events,
4137                     sizeof(struct kevent) * manager->nevents);
4138 #elif defined(USE_EPOLL)
4139         close(manager->epoll_fd);
4140         isc_mem_put(mctx, manager->events,
4141                     sizeof(struct epoll_event) * manager->nevents);
4142 #elif defined(USE_DEVPOLL)
4143         close(manager->devpoll_fd);
4144         isc_mem_put(mctx, manager->events,
4145                     sizeof(struct pollfd) * manager->nevents);
4146         isc_mem_put(mctx, manager->fdpollinfo,
4147                     sizeof(pollinfo_t) * manager->maxsocks);
4148 #elif defined(USE_SELECT)
4149         if (manager->read_fds != NULL)
4150                 isc_mem_put(mctx, manager->read_fds, manager->fd_bufsize);
4151         if (manager->read_fds_copy != NULL)
4152                 isc_mem_put(mctx, manager->read_fds_copy, manager->fd_bufsize);
4153         if (manager->write_fds != NULL)
4154                 isc_mem_put(mctx, manager->write_fds, manager->fd_bufsize);
4155         if (manager->write_fds_copy != NULL)
4156                 isc_mem_put(mctx, manager->write_fds_copy, manager->fd_bufsize);
4157 #endif  /* USE_KQUEUE */
4158 }
4159
4160 ISC_SOCKETFUNC_SCOPE isc_result_t
4161 isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
4162         return (isc__socketmgr_create2(mctx, managerp, 0));
4163 }
4164
4165 ISC_SOCKETFUNC_SCOPE isc_result_t
4166 isc__socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
4167                        unsigned int maxsocks)
4168 {
4169         int i;
4170         isc__socketmgr_t *manager;
4171 #ifdef USE_WATCHER_THREAD
4172         char strbuf[ISC_STRERRORSIZE];
4173 #endif
4174         isc_result_t result;
4175
4176         REQUIRE(managerp != NULL && *managerp == NULL);
4177
4178 #ifdef USE_SHARED_MANAGER
4179         if (socketmgr != NULL) {
4180                 /* Don't allow maxsocks to be updated */
4181                 if (maxsocks > 0 && socketmgr->maxsocks != maxsocks)
4182                         return (ISC_R_EXISTS);
4183
4184                 socketmgr->refs++;
4185                 *managerp = (isc_socketmgr_t *)socketmgr;
4186                 return (ISC_R_SUCCESS);
4187         }
4188 #endif /* USE_SHARED_MANAGER */
4189
4190         if (maxsocks == 0)
4191                 maxsocks = ISC_SOCKET_MAXSOCKETS;
4192
4193         manager = isc_mem_get(mctx, sizeof(*manager));
4194         if (manager == NULL)
4195                 return (ISC_R_NOMEMORY);
4196
4197         /* zero-clear so that necessary cleanup on failure will be easy */
4198         memset(manager, 0, sizeof(*manager));
4199         manager->maxsocks = maxsocks;
4200         manager->reserved = 0;
4201         manager->maxudp = 0;
4202         manager->fds = isc_mem_get(mctx,
4203                                    manager->maxsocks * sizeof(isc__socket_t *));
4204         if (manager->fds == NULL) {
4205                 result = ISC_R_NOMEMORY;
4206                 goto free_manager;
4207         }
4208         manager->fdstate = isc_mem_get(mctx, manager->maxsocks * sizeof(int));
4209         if (manager->fdstate == NULL) {
4210                 result = ISC_R_NOMEMORY;
4211                 goto free_manager;
4212         }
4213         manager->stats = NULL;
4214
4215         manager->common.methods = &socketmgrmethods;
4216         manager->common.magic = ISCAPI_SOCKETMGR_MAGIC;
4217         manager->common.impmagic = SOCKET_MANAGER_MAGIC;
4218         manager->mctx = NULL;
4219         memset(manager->fds, 0, manager->maxsocks * sizeof(isc_socket_t *));
4220         ISC_LIST_INIT(manager->socklist);
4221         result = isc_mutex_init(&manager->lock);
4222         if (result != ISC_R_SUCCESS)
4223                 goto free_manager;
4224         manager->fdlock = isc_mem_get(mctx, FDLOCK_COUNT * sizeof(isc_mutex_t));
4225         if (manager->fdlock == NULL) {
4226                 result = ISC_R_NOMEMORY;
4227                 goto cleanup_lock;
4228         }
4229         for (i = 0; i < FDLOCK_COUNT; i++) {
4230                 result = isc_mutex_init(&manager->fdlock[i]);
4231                 if (result != ISC_R_SUCCESS) {
4232                         while (--i >= 0)
4233                                 DESTROYLOCK(&manager->fdlock[i]);
4234                         isc_mem_put(mctx, manager->fdlock,
4235                                     FDLOCK_COUNT * sizeof(isc_mutex_t));
4236                         manager->fdlock = NULL;
4237                         goto cleanup_lock;
4238                 }
4239         }
4240
4241 #ifdef USE_WATCHER_THREAD
4242         if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
4243                 UNEXPECTED_ERROR(__FILE__, __LINE__,
4244                                  "isc_condition_init() %s",
4245                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4246                                                 ISC_MSG_FAILED, "failed"));
4247                 result = ISC_R_UNEXPECTED;
4248                 goto cleanup_lock;
4249         }
4250
4251         /*
4252          * Create the special fds that will be used to wake up the
4253          * select/poll loop when something internal needs to be done.
4254          */
4255         if (pipe(manager->pipe_fds) != 0) {
4256                 isc__strerror(errno, strbuf, sizeof(strbuf));
4257                 UNEXPECTED_ERROR(__FILE__, __LINE__,
4258                                  "pipe() %s: %s",
4259                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4260                                                 ISC_MSG_FAILED, "failed"),
4261                                  strbuf);
4262                 result = ISC_R_UNEXPECTED;
4263                 goto cleanup_condition;
4264         }
4265
4266         RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
4267 #if 0
4268         RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
4269 #endif
4270 #endif  /* USE_WATCHER_THREAD */
4271
4272 #ifdef USE_SHARED_MANAGER
4273         manager->refs = 1;
4274 #endif /* USE_SHARED_MANAGER */
4275
4276         /*
4277          * Set up initial state for the select loop
4278          */
4279         result = setup_watcher(mctx, manager);
4280         if (result != ISC_R_SUCCESS)
4281                 goto cleanup;
4282         memset(manager->fdstate, 0, manager->maxsocks * sizeof(int));
4283 #ifdef USE_WATCHER_THREAD
4284         /*
4285          * Start up the select/poll thread.
4286          */
4287         if (isc_thread_create(watcher, manager, &manager->watcher) !=
4288             ISC_R_SUCCESS) {
4289                 UNEXPECTED_ERROR(__FILE__, __LINE__,
4290                                  "isc_thread_create() %s",
4291                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4292                                                 ISC_MSG_FAILED, "failed"));
4293                 cleanup_watcher(mctx, manager);
4294                 result = ISC_R_UNEXPECTED;
4295                 goto cleanup;
4296         }
4297 #endif /* USE_WATCHER_THREAD */
4298         isc_mem_attach(mctx, &manager->mctx);
4299
4300 #ifdef USE_SHARED_MANAGER
4301         socketmgr = manager;
4302 #endif /* USE_SHARED_MANAGER */
4303         *managerp = (isc_socketmgr_t *)manager;
4304
4305         return (ISC_R_SUCCESS);
4306
4307 cleanup:
4308 #ifdef USE_WATCHER_THREAD
4309         (void)close(manager->pipe_fds[0]);
4310         (void)close(manager->pipe_fds[1]);
4311 #endif  /* USE_WATCHER_THREAD */
4312
4313 #ifdef USE_WATCHER_THREAD
4314 cleanup_condition:
4315         (void)isc_condition_destroy(&manager->shutdown_ok);
4316 #endif  /* USE_WATCHER_THREAD */
4317
4318
4319 cleanup_lock:
4320         if (manager->fdlock != NULL) {
4321                 for (i = 0; i < FDLOCK_COUNT; i++)
4322                         DESTROYLOCK(&manager->fdlock[i]);
4323         }
4324         DESTROYLOCK(&manager->lock);
4325
4326 free_manager:
4327         if (manager->fdlock != NULL) {
4328                 isc_mem_put(mctx, manager->fdlock,
4329                             FDLOCK_COUNT * sizeof(isc_mutex_t));
4330         }
4331         if (manager->fdstate != NULL) {
4332                 isc_mem_put(mctx, manager->fdstate,
4333                             manager->maxsocks * sizeof(int));
4334         }
4335         if (manager->fds != NULL) {
4336                 isc_mem_put(mctx, manager->fds,
4337                             manager->maxsocks * sizeof(isc_socket_t *));
4338         }
4339         isc_mem_put(mctx, manager, sizeof(*manager));
4340
4341         return (result);
4342 }
4343
4344 #ifdef BIND9
4345 isc_result_t
4346 isc__socketmgr_getmaxsockets(isc_socketmgr_t *manager0, unsigned int *nsockp) {
4347         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
4348         REQUIRE(VALID_MANAGER(manager));
4349         REQUIRE(nsockp != NULL);
4350
4351         *nsockp = manager->maxsocks;
4352
4353         return (ISC_R_SUCCESS);
4354 }
4355
4356 void
4357 isc__socketmgr_setstats(isc_socketmgr_t *manager0, isc_stats_t *stats) {
4358         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
4359
4360         REQUIRE(VALID_MANAGER(manager));
4361         REQUIRE(ISC_LIST_EMPTY(manager->socklist));
4362         REQUIRE(manager->stats == NULL);
4363         REQUIRE(isc_stats_ncounters(stats) == isc_sockstatscounter_max);
4364
4365         isc_stats_attach(stats, &manager->stats);
4366 }
4367 #endif
4368
4369 ISC_SOCKETFUNC_SCOPE void
4370 isc__socketmgr_destroy(isc_socketmgr_t **managerp) {
4371         isc__socketmgr_t *manager;
4372         int i;
4373         isc_mem_t *mctx;
4374
4375         /*
4376          * Destroy a socket manager.
4377          */
4378
4379         REQUIRE(managerp != NULL);
4380         manager = (isc__socketmgr_t *)*managerp;
4381         REQUIRE(VALID_MANAGER(manager));
4382
4383 #ifdef USE_SHARED_MANAGER
4384         manager->refs--;
4385         if (manager->refs > 0) {
4386                 *managerp = NULL;
4387                 return;
4388         }
4389         socketmgr = NULL;
4390 #endif /* USE_SHARED_MANAGER */
4391
4392         LOCK(&manager->lock);
4393
4394         /*
4395          * Wait for all sockets to be destroyed.
4396          */
4397         while (!ISC_LIST_EMPTY(manager->socklist)) {
4398 #ifdef USE_WATCHER_THREAD
4399                 manager_log(manager, CREATION, "%s",
4400                             isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
4401                                            ISC_MSG_SOCKETSREMAIN,
4402                                            "sockets exist"));
4403                 WAIT(&manager->shutdown_ok, &manager->lock);
4404 #else /* USE_WATCHER_THREAD */
4405                 UNLOCK(&manager->lock);
4406                 isc__taskmgr_dispatch(NULL);
4407                 LOCK(&manager->lock);
4408 #endif /* USE_WATCHER_THREAD */
4409         }
4410
4411         UNLOCK(&manager->lock);
4412
4413         /*
4414          * Here, poke our select/poll thread.  Do this by closing the write
4415          * half of the pipe, which will send EOF to the read half.
4416          * This is currently a no-op in the non-threaded case.
4417          */
4418         select_poke(manager, 0, SELECT_POKE_SHUTDOWN);
4419
4420 #ifdef USE_WATCHER_THREAD
4421         /*
4422          * Wait for thread to exit.
4423          */
4424         if (isc_thread_join(manager->watcher, NULL) != ISC_R_SUCCESS)
4425                 UNEXPECTED_ERROR(__FILE__, __LINE__,
4426                                  "isc_thread_join() %s",
4427                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4428                                                 ISC_MSG_FAILED, "failed"));
4429 #endif /* USE_WATCHER_THREAD */
4430
4431         /*
4432          * Clean up.
4433          */
4434         cleanup_watcher(manager->mctx, manager);
4435
4436 #ifdef USE_WATCHER_THREAD
4437         (void)close(manager->pipe_fds[0]);
4438         (void)close(manager->pipe_fds[1]);
4439         (void)isc_condition_destroy(&manager->shutdown_ok);
4440 #endif /* USE_WATCHER_THREAD */
4441
4442         for (i = 0; i < (int)manager->maxsocks; i++)
4443                 if (manager->fdstate[i] == CLOSE_PENDING) /* no need to lock */
4444                         (void)close(i);
4445
4446         isc_mem_put(manager->mctx, manager->fds,
4447                     manager->maxsocks * sizeof(isc__socket_t *));
4448         isc_mem_put(manager->mctx, manager->fdstate,
4449                     manager->maxsocks * sizeof(int));
4450
4451         if (manager->stats != NULL)
4452                 isc_stats_detach(&manager->stats);
4453
4454         if (manager->fdlock != NULL) {
4455                 for (i = 0; i < FDLOCK_COUNT; i++)
4456                         DESTROYLOCK(&manager->fdlock[i]);
4457                 isc_mem_put(manager->mctx, manager->fdlock,
4458                             FDLOCK_COUNT * sizeof(isc_mutex_t));
4459         }
4460         DESTROYLOCK(&manager->lock);
4461         manager->common.magic = 0;
4462         manager->common.impmagic = 0;
4463         mctx= manager->mctx;
4464         isc_mem_put(mctx, manager, sizeof(*manager));
4465
4466         isc_mem_detach(&mctx);
4467
4468         *managerp = NULL;
4469
4470 #ifdef USE_SHARED_MANAGER
4471         socketmgr = NULL;
4472 #endif
4473 }
4474
4475 static isc_result_t
4476 socket_recv(isc__socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
4477             unsigned int flags)
4478 {
4479         int io_state;
4480         isc_boolean_t have_lock = ISC_FALSE;
4481         isc_task_t *ntask = NULL;
4482         isc_result_t result = ISC_R_SUCCESS;
4483
4484         dev->ev_sender = task;
4485
4486         if (sock->type == isc_sockettype_udp) {
4487                 io_state = doio_recv(sock, dev);
4488         } else {
4489                 LOCK(&sock->lock);
4490                 have_lock = ISC_TRUE;
4491
4492                 if (ISC_LIST_EMPTY(sock->recv_list))
4493                         io_state = doio_recv(sock, dev);
4494                 else
4495                         io_state = DOIO_SOFT;
4496         }
4497
4498         switch (io_state) {
4499         case DOIO_SOFT:
4500                 /*
4501                  * We couldn't read all or part of the request right now, so
4502                  * queue it.
4503                  *
4504                  * Attach to socket and to task
4505                  */
4506                 isc_task_attach(task, &ntask);
4507                 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
4508
4509                 if (!have_lock) {
4510                         LOCK(&sock->lock);
4511                         have_lock = ISC_TRUE;
4512                 }
4513
4514                 /*
4515                  * Enqueue the request.  If the socket was previously not being
4516                  * watched, poke the watcher to start paying attention to it.
4517                  */
4518                 if (ISC_LIST_EMPTY(sock->recv_list) && !sock->pending_recv)
4519                         select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
4520                 ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
4521
4522                 socket_log(sock, NULL, EVENT, NULL, 0, 0,
4523                            "socket_recv: event %p -> task %p",
4524                            dev, ntask);
4525
4526                 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
4527                         result = ISC_R_INPROGRESS;
4528                 break;
4529
4530         case DOIO_EOF:
4531                 dev->result = ISC_R_EOF;
4532                 /* fallthrough */
4533
4534         case DOIO_HARD:
4535         case DOIO_SUCCESS:
4536                 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
4537                         send_recvdone_event(sock, &dev);
4538                 break;
4539         }
4540
4541         if (have_lock)
4542                 UNLOCK(&sock->lock);
4543
4544         return (result);
4545 }
4546
4547 ISC_SOCKETFUNC_SCOPE isc_result_t
4548 isc__socket_recvv(isc_socket_t *sock0, isc_bufferlist_t *buflist,
4549                   unsigned int minimum, isc_task_t *task,
4550                   isc_taskaction_t action, const void *arg)
4551 {
4552         isc__socket_t *sock = (isc__socket_t *)sock0;
4553         isc_socketevent_t *dev;
4554         isc__socketmgr_t *manager;
4555         unsigned int iocount;
4556         isc_buffer_t *buffer;
4557
4558         REQUIRE(VALID_SOCKET(sock));
4559         REQUIRE(buflist != NULL);
4560         REQUIRE(!ISC_LIST_EMPTY(*buflist));
4561         REQUIRE(task != NULL);
4562         REQUIRE(action != NULL);
4563
4564         manager = sock->manager;
4565         REQUIRE(VALID_MANAGER(manager));
4566
4567         iocount = isc_bufferlist_availablecount(buflist);
4568         REQUIRE(iocount > 0);
4569
4570         INSIST(sock->bound);
4571
4572         dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
4573         if (dev == NULL)
4574                 return (ISC_R_NOMEMORY);
4575
4576         /*
4577          * UDP sockets are always partial read
4578          */
4579         if (sock->type == isc_sockettype_udp)
4580                 dev->minimum = 1;
4581         else {
4582                 if (minimum == 0)
4583                         dev->minimum = iocount;
4584                 else
4585                         dev->minimum = minimum;
4586         }
4587
4588         /*
4589          * Move each buffer from the passed in list to our internal one.
4590          */
4591         buffer = ISC_LIST_HEAD(*buflist);
4592         while (buffer != NULL) {
4593                 ISC_LIST_DEQUEUE(*buflist, buffer, link);
4594                 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
4595                 buffer = ISC_LIST_HEAD(*buflist);
4596         }
4597
4598         return (socket_recv(sock, dev, task, 0));
4599 }
4600
4601 ISC_SOCKETFUNC_SCOPE isc_result_t
4602 isc__socket_recv(isc_socket_t *sock0, isc_region_t *region,
4603                  unsigned int minimum, isc_task_t *task,
4604                  isc_taskaction_t action, const void *arg)
4605 {
4606         isc__socket_t *sock = (isc__socket_t *)sock0;
4607         isc_socketevent_t *dev;
4608         isc__socketmgr_t *manager;
4609
4610         REQUIRE(VALID_SOCKET(sock));
4611         REQUIRE(action != NULL);
4612
4613         manager = sock->manager;
4614         REQUIRE(VALID_MANAGER(manager));
4615
4616         INSIST(sock->bound);
4617
4618         dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
4619         if (dev == NULL)
4620                 return (ISC_R_NOMEMORY);
4621
4622         return (isc__socket_recv2(sock0, region, minimum, task, dev, 0));
4623 }
4624
4625 ISC_SOCKETFUNC_SCOPE isc_result_t
4626 isc__socket_recv2(isc_socket_t *sock0, isc_region_t *region,
4627                   unsigned int minimum, isc_task_t *task,
4628                   isc_socketevent_t *event, unsigned int flags)
4629 {
4630         isc__socket_t *sock = (isc__socket_t *)sock0;
4631
4632         event->ev_sender = sock;
4633         event->result = ISC_R_UNSET;
4634         ISC_LIST_INIT(event->bufferlist);
4635         event->region = *region;
4636         event->n = 0;
4637         event->offset = 0;
4638         event->attributes = 0;
4639
4640         /*
4641          * UDP sockets are always partial read.
4642          */
4643         if (sock->type == isc_sockettype_udp)
4644                 event->minimum = 1;
4645         else {
4646                 if (minimum == 0)
4647                         event->minimum = region->length;
4648                 else
4649                         event->minimum = minimum;
4650         }
4651
4652         return (socket_recv(sock, event, task, flags));
4653 }
4654
4655 static isc_result_t
4656 socket_send(isc__socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
4657             isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
4658             unsigned int flags)
4659 {
4660         int io_state;
4661         isc_boolean_t have_lock = ISC_FALSE;
4662         isc_task_t *ntask = NULL;
4663         isc_result_t result = ISC_R_SUCCESS;
4664
4665         dev->ev_sender = task;
4666
4667         set_dev_address(address, sock, dev);
4668         if (pktinfo != NULL) {
4669                 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
4670                 dev->pktinfo = *pktinfo;
4671
4672                 if (!isc_sockaddr_issitelocal(&dev->address) &&
4673                     !isc_sockaddr_islinklocal(&dev->address)) {
4674                         socket_log(sock, NULL, TRACE, isc_msgcat,
4675                                    ISC_MSGSET_SOCKET, ISC_MSG_PKTINFOPROVIDED,
4676                                    "pktinfo structure provided, ifindex %u "
4677                                    "(set to 0)", pktinfo->ipi6_ifindex);
4678
4679                         /*
4680                          * Set the pktinfo index to 0 here, to let the
4681                          * kernel decide what interface it should send on.
4682                          */
4683                         dev->pktinfo.ipi6_ifindex = 0;
4684                 }
4685         }
4686
4687         if (sock->type == isc_sockettype_udp)
4688                 io_state = doio_send(sock, dev);
4689         else {
4690                 LOCK(&sock->lock);
4691                 have_lock = ISC_TRUE;
4692
4693                 if (ISC_LIST_EMPTY(sock->send_list))
4694                         io_state = doio_send(sock, dev);
4695                 else
4696                         io_state = DOIO_SOFT;
4697         }
4698
4699         switch (io_state) {
4700         case DOIO_SOFT:
4701                 /*
4702                  * We couldn't send all or part of the request right now, so
4703                  * queue it unless ISC_SOCKFLAG_NORETRY is set.
4704                  */
4705                 if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
4706                         isc_task_attach(task, &ntask);
4707                         dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
4708
4709                         if (!have_lock) {
4710                                 LOCK(&sock->lock);
4711                                 have_lock = ISC_TRUE;
4712                         }
4713
4714                         /*
4715                          * Enqueue the request.  If the socket was previously
4716                          * not being watched, poke the watcher to start
4717                          * paying attention to it.
4718                          */
4719                         if (ISC_LIST_EMPTY(sock->send_list) &&
4720                             !sock->pending_send)
4721                                 select_poke(sock->manager, sock->fd,
4722                                             SELECT_POKE_WRITE);
4723                         ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
4724
4725                         socket_log(sock, NULL, EVENT, NULL, 0, 0,
4726                                    "socket_send: event %p -> task %p",
4727                                    dev, ntask);
4728
4729                         if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
4730                                 result = ISC_R_INPROGRESS;
4731                         break;
4732                 }
4733
4734         case DOIO_HARD:
4735         case DOIO_SUCCESS:
4736                 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
4737                         send_senddone_event(sock, &dev);
4738                 break;
4739         }
4740
4741         if (have_lock)
4742                 UNLOCK(&sock->lock);
4743
4744         return (result);
4745 }
4746
4747 ISC_SOCKETFUNC_SCOPE isc_result_t
4748 isc__socket_send(isc_socket_t *sock, isc_region_t *region,
4749                  isc_task_t *task, isc_taskaction_t action, const void *arg)
4750 {
4751         /*
4752          * REQUIRE() checking is performed in isc_socket_sendto().
4753          */
4754         return (isc__socket_sendto(sock, region, task, action, arg, NULL,
4755                                    NULL));
4756 }
4757
4758 ISC_SOCKETFUNC_SCOPE isc_result_t
4759 isc__socket_sendto(isc_socket_t *sock0, isc_region_t *region,
4760                    isc_task_t *task, isc_taskaction_t action, const void *arg,
4761                    isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
4762 {
4763         isc__socket_t *sock = (isc__socket_t *)sock0;
4764         isc_socketevent_t *dev;
4765         isc__socketmgr_t *manager;
4766
4767         REQUIRE(VALID_SOCKET(sock));
4768         REQUIRE(region != NULL);
4769         REQUIRE(task != NULL);
4770         REQUIRE(action != NULL);
4771
4772         manager = sock->manager;
4773         REQUIRE(VALID_MANAGER(manager));
4774
4775         INSIST(sock->bound);
4776
4777         dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
4778         if (dev == NULL)
4779                 return (ISC_R_NOMEMORY);
4780
4781         dev->region = *region;
4782
4783         return (socket_send(sock, dev, task, address, pktinfo, 0));
4784 }
4785
4786 ISC_SOCKETFUNC_SCOPE isc_result_t
4787 isc__socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
4788                   isc_task_t *task, isc_taskaction_t action, const void *arg)
4789 {
4790         return (isc__socket_sendtov(sock, buflist, task, action, arg, NULL,
4791                                     NULL));
4792 }
4793
4794 ISC_SOCKETFUNC_SCOPE isc_result_t
4795 isc__socket_sendtov(isc_socket_t *sock0, isc_bufferlist_t *buflist,
4796                     isc_task_t *task, isc_taskaction_t action, const void *arg,
4797                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
4798 {
4799         isc__socket_t *sock = (isc__socket_t *)sock0;
4800         isc_socketevent_t *dev;
4801         isc__socketmgr_t *manager;
4802         unsigned int iocount;
4803         isc_buffer_t *buffer;
4804
4805         REQUIRE(VALID_SOCKET(sock));
4806         REQUIRE(buflist != NULL);
4807         REQUIRE(!ISC_LIST_EMPTY(*buflist));
4808         REQUIRE(task != NULL);
4809         REQUIRE(action != NULL);
4810
4811         manager = sock->manager;
4812         REQUIRE(VALID_MANAGER(manager));
4813
4814         iocount = isc_bufferlist_usedcount(buflist);
4815         REQUIRE(iocount > 0);
4816
4817         dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
4818         if (dev == NULL)
4819                 return (ISC_R_NOMEMORY);
4820
4821         /*
4822          * Move each buffer from the passed in list to our internal one.
4823          */
4824         buffer = ISC_LIST_HEAD(*buflist);
4825         while (buffer != NULL) {
4826                 ISC_LIST_DEQUEUE(*buflist, buffer, link);
4827                 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
4828                 buffer = ISC_LIST_HEAD(*buflist);
4829         }
4830
4831         return (socket_send(sock, dev, task, address, pktinfo, 0));
4832 }
4833
4834 ISC_SOCKETFUNC_SCOPE isc_result_t
4835 isc__socket_sendto2(isc_socket_t *sock0, isc_region_t *region,
4836                     isc_task_t *task,
4837                     isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
4838                     isc_socketevent_t *event, unsigned int flags)
4839 {
4840         isc__socket_t *sock = (isc__socket_t *)sock0;
4841
4842         REQUIRE(VALID_SOCKET(sock));
4843         REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
4844         if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
4845                 REQUIRE(sock->type == isc_sockettype_udp);
4846         event->ev_sender = sock;
4847         event->result = ISC_R_UNSET;
4848         ISC_LIST_INIT(event->bufferlist);
4849         event->region = *region;
4850         event->n = 0;
4851         event->offset = 0;
4852         event->attributes = 0;
4853
4854         return (socket_send(sock, event, task, address, pktinfo, flags));
4855 }
4856
4857 ISC_SOCKETFUNC_SCOPE void
4858 isc__socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
4859 #ifdef ISC_PLATFORM_HAVESYSUNH
4860         int s;
4861         struct stat sb;
4862         char strbuf[ISC_STRERRORSIZE];
4863
4864         if (sockaddr->type.sa.sa_family != AF_UNIX)
4865                 return;
4866
4867 #ifndef S_ISSOCK
4868 #if defined(S_IFMT) && defined(S_IFSOCK)
4869 #define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
4870 #elif defined(_S_IFMT) && defined(S_IFSOCK)
4871 #define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
4872 #endif
4873 #endif
4874
4875 #ifndef S_ISFIFO
4876 #if defined(S_IFMT) && defined(S_IFIFO)
4877 #define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
4878 #elif defined(_S_IFMT) && defined(S_IFIFO)
4879 #define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
4880 #endif
4881 #endif
4882
4883 #if !defined(S_ISFIFO) && !defined(S_ISSOCK)
4884 #error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform.  See <sys/stat.h>.
4885 #endif
4886
4887 #ifndef S_ISFIFO
4888 #define S_ISFIFO(mode) 0
4889 #endif
4890
4891 #ifndef S_ISSOCK
4892 #define S_ISSOCK(mode) 0
4893 #endif
4894
4895         if (active) {
4896                 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
4897                         isc__strerror(errno, strbuf, sizeof(strbuf));
4898                         isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4899                                       ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4900                                       "isc_socket_cleanunix: stat(%s): %s",
4901                                       sockaddr->type.sunix.sun_path, strbuf);
4902                         return;
4903                 }
4904                 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
4905                         isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4906                                       ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4907                                       "isc_socket_cleanunix: %s: not a socket",
4908                                       sockaddr->type.sunix.sun_path);
4909                         return;
4910                 }
4911                 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
4912                         isc__strerror(errno, strbuf, sizeof(strbuf));
4913                         isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4914                                       ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4915                                       "isc_socket_cleanunix: unlink(%s): %s",
4916                                       sockaddr->type.sunix.sun_path, strbuf);
4917                 }
4918                 return;
4919         }
4920
4921         s = socket(AF_UNIX, SOCK_STREAM, 0);
4922         if (s < 0) {
4923                 isc__strerror(errno, strbuf, sizeof(strbuf));
4924                 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4925                               ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4926                               "isc_socket_cleanunix: socket(%s): %s",
4927                               sockaddr->type.sunix.sun_path, strbuf);
4928                 return;
4929         }
4930
4931         if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
4932                 switch (errno) {
4933                 case ENOENT:    /* We exited cleanly last time */
4934                         break;
4935                 default:
4936                         isc__strerror(errno, strbuf, sizeof(strbuf));
4937                         isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4938                                       ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4939                                       "isc_socket_cleanunix: stat(%s): %s",
4940                                       sockaddr->type.sunix.sun_path, strbuf);
4941                         break;
4942                 }
4943                 goto cleanup;
4944         }
4945
4946         if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
4947                 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4948                               ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4949                               "isc_socket_cleanunix: %s: not a socket",
4950                               sockaddr->type.sunix.sun_path);
4951                 goto cleanup;
4952         }
4953
4954         if (connect(s, (struct sockaddr *)&sockaddr->type.sunix,
4955                     sizeof(sockaddr->type.sunix)) < 0) {
4956                 switch (errno) {
4957                 case ECONNREFUSED:
4958                 case ECONNRESET:
4959                         if (unlink(sockaddr->type.sunix.sun_path) < 0) {
4960                                 isc__strerror(errno, strbuf, sizeof(strbuf));
4961                                 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4962                                               ISC_LOGMODULE_SOCKET,
4963                                               ISC_LOG_WARNING,
4964                                               "isc_socket_cleanunix: "
4965                                               "unlink(%s): %s",
4966                                               sockaddr->type.sunix.sun_path,
4967                                               strbuf);
4968                         }
4969                         break;
4970                 default:
4971                         isc__strerror(errno, strbuf, sizeof(strbuf));
4972                         isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4973                                       ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4974                                       "isc_socket_cleanunix: connect(%s): %s",
4975                                       sockaddr->type.sunix.sun_path, strbuf);
4976                         break;
4977                 }
4978         }
4979  cleanup:
4980         close(s);
4981 #else
4982         UNUSED(sockaddr);
4983         UNUSED(active);
4984 #endif
4985 }
4986
4987 ISC_SOCKETFUNC_SCOPE isc_result_t
4988 isc__socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
4989                     isc_uint32_t owner, isc_uint32_t group)
4990 {
4991 #ifdef ISC_PLATFORM_HAVESYSUNH
4992         isc_result_t result = ISC_R_SUCCESS;
4993         char strbuf[ISC_STRERRORSIZE];
4994         char path[sizeof(sockaddr->type.sunix.sun_path)];
4995 #ifdef NEED_SECURE_DIRECTORY
4996         char *slash;
4997 #endif
4998
4999         REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
5000         INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
5001         strcpy(path, sockaddr->type.sunix.sun_path);
5002
5003 #ifdef NEED_SECURE_DIRECTORY
5004         slash = strrchr(path, '/');
5005         if (slash != NULL) {
5006                 if (slash != path)
5007                         *slash = '\0';
5008                 else
5009                         strcpy(path, "/");
5010         } else
5011                 strcpy(path, ".");
5012 #endif
5013
5014         if (chmod(path, perm) < 0) {
5015                 isc__strerror(errno, strbuf, sizeof(strbuf));
5016                 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
5017                               ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
5018                               "isc_socket_permunix: chmod(%s, %d): %s",
5019                               path, perm, strbuf);
5020                 result = ISC_R_FAILURE;
5021         }
5022         if (chown(path, owner, group) < 0) {
5023                 isc__strerror(errno, strbuf, sizeof(strbuf));
5024                 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
5025                               ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
5026                               "isc_socket_permunix: chown(%s, %d, %d): %s",
5027                               path, owner, group,
5028                               strbuf);
5029                 result = ISC_R_FAILURE;
5030         }
5031         return (result);
5032 #else
5033         UNUSED(sockaddr);
5034         UNUSED(perm);
5035         UNUSED(owner);
5036         UNUSED(group);
5037         return (ISC_R_NOTIMPLEMENTED);
5038 #endif
5039 }
5040
5041 ISC_SOCKETFUNC_SCOPE isc_result_t
5042 isc__socket_bind(isc_socket_t *sock0, isc_sockaddr_t *sockaddr,
5043                  unsigned int options) {
5044         isc__socket_t *sock = (isc__socket_t *)sock0;
5045         char strbuf[ISC_STRERRORSIZE];
5046         int on = 1;
5047
5048         REQUIRE(VALID_SOCKET(sock));
5049
5050         LOCK(&sock->lock);
5051
5052         INSIST(!sock->bound);
5053         INSIST(!sock->dupped);
5054
5055         if (sock->pf != sockaddr->type.sa.sa_family) {
5056                 UNLOCK(&sock->lock);
5057                 return (ISC_R_FAMILYMISMATCH);
5058         }
5059
5060         /*
5061          * Only set SO_REUSEADDR when we want a specific port.
5062          */
5063 #ifdef AF_UNIX
5064         if (sock->pf == AF_UNIX)
5065                 goto bind_socket;
5066 #endif
5067         if ((options & ISC_SOCKET_REUSEADDRESS) != 0 &&
5068             isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
5069             setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
5070                        sizeof(on)) < 0) {
5071                 UNEXPECTED_ERROR(__FILE__, __LINE__,
5072                                  "setsockopt(%d) %s", sock->fd,
5073                                  isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
5074                                                 ISC_MSG_FAILED, "failed"));
5075                 /* Press on... */
5076         }
5077 #ifdef AF_UNIX
5078  bind_socket:
5079 #endif
5080         if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
5081                 inc_stats(sock->manager->stats,
5082                           sock->statsindex[STATID_BINDFAIL]);
5083
5084                 UNLOCK(&sock->lock);
5085                 switch (errno) {
5086                 case EACCES:
5087                         return (ISC_R_NOPERM);
5088                 case EADDRNOTAVAIL:
5089                         return (ISC_R_ADDRNOTAVAIL);
5090                 case EADDRINUSE:
5091                         return (ISC_R_ADDRINUSE);
5092                 case EINVAL:
5093                         return (ISC_R_BOUND);
5094                 default:
5095                         isc__strerror(errno, strbuf, sizeof(strbuf));
5096                         UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
5097                                          strbuf);
5098                         return (ISC_R_UNEXPECTED);
5099                 }
5100         }
5101
5102         socket_log(sock, sockaddr, TRACE,
5103                    isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
5104         sock->bound = 1;
5105
5106         UNLOCK(&sock->lock);
5107         return (ISC_R_SUCCESS);
5108 }
5109
5110 /*
5111  * Enable this only for specific OS versions, and only when they have repaired
5112  * their problems with it.  Until then, this is is broken and needs to be
5113  * diabled by default.  See RT22589 for details.
5114  */
5115 #undef ENABLE_ACCEPTFILTER
5116
5117 ISC_SOCKETFUNC_SCOPE isc_result_t
5118 isc__socket_filter(isc_socket_t *sock0, const char *filter) {
5119         isc__socket_t *sock = (isc__socket_t *)sock0;
5120 #if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
5121         char strbuf[ISC_STRERRORSIZE];
5122         struct accept_filter_arg afa;
5123 #else
5124         UNUSED(sock);
5125         UNUSED(filter);
5126 #endif
5127
5128         REQUIRE(VALID_SOCKET(sock));
5129
5130 #if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
5131         bzero(&afa, sizeof(afa));
5132         strncpy(afa.af_name, filter, sizeof(afa.af_name));
5133         if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
5134                          &afa, sizeof(afa)) == -1) {
5135                 isc__strerror(errno, strbuf, sizeof(strbuf));
5136                 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
5137                            ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
5138                            strbuf);
5139                 return (ISC_R_FAILURE);
5140         }
5141         return (ISC_R_SUCCESS);
5142 #else
5143         return (ISC_R_NOTIMPLEMENTED);
5144 #endif
5145 }
5146
5147 /*
5148  * Set up to listen on a given socket.  We do this by creating an internal
5149  * event that will be dispatched when the socket has read activity.  The
5150  * watcher will send the internal event to the task when there is a new
5151  * connection.
5152  *
5153  * Unlike in read, we don't preallocate a done event here.  Every time there
5154  * is a new connection we'll have to allocate a new one anyway, so we might
5155  * as well keep things simple rather than having to track them.
5156  */
5157 ISC_SOCKETFUNC_SCOPE isc_result_t
5158 isc__socket_listen(isc_socket_t *sock0, unsigned int backlog) {
5159         isc__socket_t *sock = (isc__socket_t *)sock0;
5160         char strbuf[ISC_STRERRORSIZE];
5161
5162         REQUIRE(VALID_SOCKET(sock));
5163
5164         LOCK(&sock->lock);
5165
5166         REQUIRE(!sock->listener);
5167         REQUIRE(sock->bound);
5168         REQUIRE(sock->type == isc_sockettype_tcp ||
5169                 sock->type == isc_sockettype_unix);
5170
5171         if (backlog == 0)
5172                 backlog = SOMAXCONN;
5173
5174         if (listen(sock->fd, (int)backlog) < 0) {
5175                 UNLOCK(&sock->lock);
5176                 isc__strerror(errno, strbuf, sizeof(strbuf));
5177
5178                 UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
5179
5180                 return (ISC_R_UNEXPECTED);
5181         }
5182
5183         sock->listener = 1;
5184
5185         UNLOCK(&sock->lock);
5186         return (ISC_R_SUCCESS);
5187 }
5188
5189 /*
5190  * This should try to do aggressive accept() XXXMLG
5191  */
5192 ISC_SOCKETFUNC_SCOPE isc_result_t
5193 isc__socket_accept(isc_socket_t *sock0,
5194                   isc_task_t *task, isc_taskaction_t action, const void *arg)
5195 {
5196         isc__socket_t *sock = (isc__socket_t *)sock0;
5197         isc_socket_newconnev_t *dev;
5198         isc__socketmgr_t *manager;
5199         isc_task_t *ntask = NULL;
5200         isc__socket_t *nsock;
5201         isc_result_t result;
5202         isc_boolean_t do_poke = ISC_FALSE;
5203
5204         REQUIRE(VALID_SOCKET(sock));
5205         manager = sock->manager;
5206         REQUIRE(VALID_MANAGER(manager));
5207
5208         LOCK(&sock->lock);
5209
5210         REQUIRE(sock->listener);
5211
5212         /*
5213          * Sender field is overloaded here with the task we will be sending
5214          * this event to.  Just before the actual event is delivered the
5215          * actual ev_sender will be touched up to be the socket.
5216          */
5217         dev = (isc_socket_newconnev_t *)
5218                 isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
5219                                    action, arg, sizeof(*dev));
5220         if (dev == NULL) {
5221                 UNLOCK(&sock->lock);
5222                 return (ISC_R_NOMEMORY);
5223         }
5224         ISC_LINK_INIT(dev, ev_link);
5225
5226         result = allocate_socket(manager, sock->type, &nsock);
5227         if (result != ISC_R_SUCCESS) {
5228                 isc_event_free(ISC_EVENT_PTR(&dev));
5229                 UNLOCK(&sock->lock);
5230                 return (result);
5231         }
5232
5233         /*
5234          * Attach to socket and to task.
5235          */
5236         isc_task_attach(task, &ntask);
5237         if (isc_task_exiting(ntask)) {
5238                 free_socket(&nsock);
5239                 isc_task_detach(&ntask);
5240                 isc_event_free(ISC_EVENT_PTR(&dev));
5241                 UNLOCK(&sock->lock);
5242                 return (ISC_R_SHUTTINGDOWN);
5243         }
5244         nsock->references++;
5245         nsock->statsindex = sock->statsindex;
5246
5247         dev->ev_sender = ntask;
5248         dev->newsocket = (isc_socket_t *)nsock;
5249
5250         /*
5251          * Poke watcher here.  We still have the socket locked, so there
5252          * is no race condition.  We will keep the lock for such a short
5253          * bit of time waking it up now or later won't matter all that much.
5254          */
5255         if (ISC_LIST_EMPTY(sock->accept_list))
5256                 do_poke = ISC_TRUE;
5257
5258         ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
5259
5260         if (do_poke)
5261                 select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
5262
5263         UNLOCK(&sock->lock);
5264         return (ISC_R_SUCCESS);
5265 }
5266
5267 ISC_SOCKETFUNC_SCOPE isc_result_t
5268 isc__socket_connect(isc_socket_t *sock0, isc_sockaddr_t *addr,
5269                    isc_task_t *task, isc_taskaction_t action, const void *arg)
5270 {
5271         isc__socket_t *sock = (isc__socket_t *)sock0;
5272         isc_socket_connev_t *dev;
5273         isc_task_t *ntask = NULL;
5274         isc__socketmgr_t *manager;
5275         int cc;
5276         char strbuf[ISC_STRERRORSIZE];
5277         char addrbuf[ISC_SOCKADDR_FORMATSIZE];
5278
5279         REQUIRE(VALID_SOCKET(sock));
5280         REQUIRE(addr != NULL);
5281         REQUIRE(task != NULL);
5282         REQUIRE(action != NULL);
5283
5284         manager = sock->manager;
5285         REQUIRE(VALID_MANAGER(manager));
5286         REQUIRE(addr != NULL);
5287
5288         if (isc_sockaddr_ismulticast(addr))
5289                 return (ISC_R_MULTICAST);
5290
5291         LOCK(&sock->lock);
5292
5293         REQUIRE(!sock->connecting);
5294
5295         dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
5296                                                         ISC_SOCKEVENT_CONNECT,
5297                                                         action, arg,
5298                                                         sizeof(*dev));
5299         if (dev == NULL) {
5300                 UNLOCK(&sock->lock);
5301                 return (ISC_R_NOMEMORY);
5302         }
5303         ISC_LINK_INIT(dev, ev_link);
5304
5305         /*
5306          * Try to do the connect right away, as there can be only one
5307          * outstanding, and it might happen to complete.
5308          */
5309         sock->peer_address = *addr;
5310         cc = connect(sock->fd, &addr->type.sa, addr->length);
5311         if (cc < 0) {
5312                 /*
5313                  * HP-UX "fails" to connect a UDP socket and sets errno to
5314                  * EINPROGRESS if it's non-blocking.  We'd rather regard this as
5315                  * a success and let the user detect it if it's really an error
5316                  * at the time of sending a packet on the socket.
5317                  */
5318                 if (sock->type == isc_sockettype_udp && errno == EINPROGRESS) {
5319                         cc = 0;
5320                         goto success;
5321                 }
5322                 if (SOFT_ERROR(errno) || errno == EINPROGRESS)
5323                         goto queue;
5324
5325                 switch (errno) {
5326 #define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
5327                         ERROR_MATCH(EACCES, ISC_R_NOPERM);
5328                         ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
5329                         ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
5330                         ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
5331                         ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
5332 #ifdef EHOSTDOWN
5333                         ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
5334 #endif
5335                         ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
5336                         ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
5337                         ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
5338                         ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
5339                         ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
5340 #undef ERROR_MATCH
5341                 }
5342
5343                 sock->connected = 0;
5344
5345                 isc__strerror(errno, strbuf, sizeof(strbuf));
5346                 isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
5347                 UNEXPECTED_ERROR(__FILE__, __LINE__, "connect(%s) %d/%s",
5348                                  addrbuf, errno, strbuf);
5349
5350                 UNLOCK(&sock->lock);
5351                 inc_stats(sock->manager->stats,
5352                           sock->statsindex[STATID_CONNECTFAIL]);
5353                 isc_event_free(ISC_EVENT_PTR(&dev));
5354                 return (ISC_R_UNEXPECTED);
5355
5356         err_exit:
5357                 sock->connected = 0;
5358                 isc_task_send(task, ISC_EVENT_PTR(&dev));
5359
5360                 UNLOCK(&sock->lock);
5361                 inc_stats(sock->manager->stats,
5362                           sock->statsindex[STATID_CONNECTFAIL]);
5363                 return (ISC_R_SUCCESS);
5364         }
5365
5366         /*
5367          * If connect completed, fire off the done event.
5368          */
5369  success:
5370         if (cc == 0) {
5371                 sock->connected = 1;
5372                 sock->bound = 1;
5373                 dev->result = ISC_R_SUCCESS;
5374                 isc_task_send(task, ISC_EVENT_PTR(&dev));
5375
5376                 UNLOCK(&sock->lock);
5377
5378                 inc_stats(sock->manager->stats,
5379                           sock->statsindex[STATID_CONNECT]);
5380
5381                 return (ISC_R_SUCCESS);
5382         }
5383
5384  queue:
5385
5386         /*
5387          * Attach to task.
5388          */
5389         isc_task_attach(task, &ntask);
5390
5391         sock->connecting = 1;
5392
5393         dev->ev_sender = ntask;
5394
5395         /*
5396          * Poke watcher here.  We still have the socket locked, so there
5397          * is no race condition.  We will keep the lock for such a short
5398          * bit of time waking it up now or later won't matter all that much.
5399          */
5400         if (sock->connect_ev == NULL)
5401                 select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
5402
5403         sock->connect_ev = dev;
5404
5405         UNLOCK(&sock->lock);
5406         return (ISC_R_SUCCESS);
5407 }
5408
5409 /*
5410  * Called when a socket with a pending connect() finishes.
5411  */
5412 static void
5413 internal_connect(isc_task_t *me, isc_event_t *ev) {
5414         isc__socket_t *sock;
5415         isc_socket_connev_t *dev;
5416         isc_task_t *task;
5417         int cc;
5418         ISC_SOCKADDR_LEN_T optlen;
5419         char strbuf[ISC_STRERRORSIZE];
5420         char peerbuf[ISC_SOCKADDR_FORMATSIZE];
5421
5422         UNUSED(me);
5423         INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
5424
5425         sock = ev->ev_sender;
5426         INSIST(VALID_SOCKET(sock));
5427
5428         LOCK(&sock->lock);
5429
5430         /*
5431          * When the internal event was sent the reference count was bumped
5432          * to keep the socket around for us.  Decrement the count here.
5433          */
5434         INSIST(sock->references > 0);
5435         sock->references--;
5436         if (sock->references == 0) {
5437                 UNLOCK(&sock->lock);
5438                 destroy(&sock);
5439                 return;
5440         }
5441
5442         /*
5443          * Has this event been canceled?
5444          */
5445         dev = sock->connect_ev;
5446         if (dev == NULL) {
5447                 INSIST(!sock->connecting);
5448                 UNLOCK(&sock->lock);
5449                 return;
5450         }
5451
5452         INSIST(sock->connecting);
5453         sock->connecting = 0;
5454
5455         /*
5456          * Get any possible error status here.
5457          */
5458         optlen = sizeof(cc);
5459         if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
5460                        (void *)&cc, (void *)&optlen) < 0)
5461                 cc = errno;
5462         else
5463                 errno = cc;
5464
5465         if (errno != 0) {
5466                 /*
5467                  * If the error is EAGAIN, just re-select on this
5468                  * fd and pretend nothing strange happened.
5469                  */
5470                 if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
5471                         sock->connecting = 1;
5472                         select_poke(sock->manager, sock->fd,
5473                                     SELECT_POKE_CONNECT);
5474                         UNLOCK(&sock->lock);
5475
5476                         return;
5477                 }
5478
5479                 inc_stats(sock->manager->stats,
5480                           sock->statsindex[STATID_CONNECTFAIL]);
5481
5482                 /*
5483                  * Translate other errors into ISC_R_* flavors.
5484                  */
5485                 switch (errno) {
5486 #define ERROR_MATCH(a, b) case a: dev->result = b; break;
5487                         ERROR_MATCH(EACCES, ISC_R_NOPERM);
5488                         ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
5489                         ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
5490                         ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
5491                         ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
5492 #ifdef EHOSTDOWN
5493                         ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
5494 #endif
5495                         ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
5496                         ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
5497                         ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
5498                         ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
5499                         ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
5500                         ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
5501 #undef ERROR_MATCH
5502                 default:
5503                         dev->result = ISC_R_UNEXPECTED;
5504                         isc_sockaddr_format(&sock->peer_address, peerbuf,
5505                                             sizeof(peerbuf));
5506                         isc__strerror(errno, strbuf, sizeof(strbuf));
5507                         UNEXPECTED_ERROR(__FILE__, __LINE__,
5508                                          "internal_connect: connect(%s) %s",
5509                                          peerbuf, strbuf);
5510                 }
5511         } else {
5512                 inc_stats(sock->manager->stats,
5513                           sock->statsindex[STATID_CONNECT]);
5514                 dev->result = ISC_R_SUCCESS;
5515                 sock->connected = 1;
5516                 sock->bound = 1;
5517         }
5518
5519         sock->connect_ev = NULL;
5520
5521         UNLOCK(&sock->lock);
5522
5523         task = dev->ev_sender;
5524         dev->ev_sender = sock;
5525         isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
5526 }
5527
5528 ISC_SOCKETFUNC_SCOPE isc_result_t
5529 isc__socket_getpeername(isc_socket_t *sock0, isc_sockaddr_t *addressp) {
5530         isc__socket_t *sock = (isc__socket_t *)sock0;
5531         isc_result_t result;
5532
5533         REQUIRE(VALID_SOCKET(sock));
5534         REQUIRE(addressp != NULL);
5535
5536         LOCK(&sock->lock);
5537
5538         if (sock->connected) {
5539                 *addressp = sock->peer_address;
5540                 result = ISC_R_SUCCESS;
5541         } else {
5542                 result = ISC_R_NOTCONNECTED;
5543         }
5544
5545         UNLOCK(&sock->lock);
5546
5547         return (result);
5548 }
5549
5550 ISC_SOCKETFUNC_SCOPE isc_result_t
5551 isc__socket_getsockname(isc_socket_t *sock0, isc_sockaddr_t *addressp) {
5552         isc__socket_t *sock = (isc__socket_t *)sock0;
5553         ISC_SOCKADDR_LEN_T len;
5554         isc_result_t result;
5555         char strbuf[ISC_STRERRORSIZE];
5556
5557         REQUIRE(VALID_SOCKET(sock));
5558         REQUIRE(addressp != NULL);
5559
5560         LOCK(&sock->lock);
5561
5562         if (!sock->bound) {
5563                 result = ISC_R_NOTBOUND;
5564                 goto out;
5565         }
5566
5567         result = ISC_R_SUCCESS;
5568
5569         len = sizeof(addressp->type);
5570         if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
5571                 isc__strerror(errno, strbuf, sizeof(strbuf));
5572                 UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
5573                                  strbuf);
5574                 result = ISC_R_UNEXPECTED;
5575                 goto out;
5576         }
5577         addressp->length = (unsigned int)len;
5578
5579  out:
5580         UNLOCK(&sock->lock);
5581
5582         return (result);
5583 }
5584
5585 /*
5586  * Run through the list of events on this socket, and cancel the ones
5587  * queued for task "task" of type "how".  "how" is a bitmask.
5588  */
5589 ISC_SOCKETFUNC_SCOPE void
5590 isc__socket_cancel(isc_socket_t *sock0, isc_task_t *task, unsigned int how) {
5591         isc__socket_t *sock = (isc__socket_t *)sock0;
5592
5593         REQUIRE(VALID_SOCKET(sock));
5594
5595         /*
5596          * Quick exit if there is nothing to do.  Don't even bother locking
5597          * in this case.
5598          */
5599         if (how == 0)
5600                 return;
5601
5602         LOCK(&sock->lock);
5603
5604         /*
5605          * All of these do the same thing, more or less.
5606          * Each will:
5607          *      o If the internal event is marked as "posted" try to
5608          *        remove it from the task's queue.  If this fails, mark it
5609          *        as canceled instead, and let the task clean it up later.
5610          *      o For each I/O request for that task of that type, post
5611          *        its done event with status of "ISC_R_CANCELED".
5612          *      o Reset any state needed.
5613          */
5614         if (((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV)
5615             && !ISC_LIST_EMPTY(sock->recv_list)) {
5616                 isc_socketevent_t      *dev;
5617                 isc_socketevent_t      *next;
5618                 isc_task_t             *current_task;
5619
5620                 dev = ISC_LIST_HEAD(sock->recv_list);
5621
5622                 while (dev != NULL) {
5623                         current_task = dev->ev_sender;
5624                         next = ISC_LIST_NEXT(dev, ev_link);
5625
5626                         if ((task == NULL) || (task == current_task)) {
5627                                 dev->result = ISC_R_CANCELED;
5628                                 send_recvdone_event(sock, &dev);
5629                         }
5630                         dev = next;
5631                 }
5632         }
5633
5634         if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
5635             && !ISC_LIST_EMPTY(sock->send_list)) {
5636                 isc_socketevent_t      *dev;
5637                 isc_socketevent_t      *next;
5638                 isc_task_t             *current_task;
5639
5640                 dev = ISC_LIST_HEAD(sock->send_list);
5641
5642                 while (dev != NULL) {
5643                         current_task = dev->ev_sender;
5644                         next = ISC_LIST_NEXT(dev, ev_link);
5645
5646                         if ((task == NULL) || (task == current_task)) {
5647                                 dev->result = ISC_R_CANCELED;
5648                                 send_senddone_event(sock, &dev);
5649                         }
5650                         dev = next;
5651                 }
5652         }
5653
5654         if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
5655             && !ISC_LIST_EMPTY(sock->accept_list)) {
5656                 isc_socket_newconnev_t *dev;
5657                 isc_socket_newconnev_t *next;
5658                 isc_task_t             *current_task;
5659
5660                 dev = ISC_LIST_HEAD(sock->accept_list);
5661                 while (dev != NULL) {
5662                         current_task = dev->ev_sender;
5663                         next = ISC_LIST_NEXT(dev, ev_link);
5664
5665                         if ((task == NULL) || (task == current_task)) {
5666
5667                                 ISC_LIST_UNLINK(sock->accept_list, dev,
5668                                                 ev_link);
5669
5670                                 NEWCONNSOCK(dev)->references--;
5671                                 free_socket((isc__socket_t **)&dev->newsocket);
5672
5673                                 dev->result = ISC_R_CANCELED;
5674                                 dev->ev_sender = sock;
5675                                 isc_task_sendanddetach(&current_task,
5676                                                        ISC_EVENT_PTR(&dev));
5677                         }
5678
5679                         dev = next;
5680                 }
5681         }
5682
5683         /*
5684          * Connecting is not a list.
5685          */
5686         if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
5687             && sock->connect_ev != NULL) {
5688                 isc_socket_connev_t    *dev;
5689                 isc_task_t             *current_task;
5690
5691                 INSIST(sock->connecting);
5692                 sock->connecting = 0;
5693
5694                 dev = sock->connect_ev;
5695                 current_task = dev->ev_sender;
5696
5697                 if ((task == NULL) || (task == current_task)) {
5698                         sock->connect_ev = NULL;
5699
5700                         dev->result = ISC_R_CANCELED;
5701                         dev->ev_sender = sock;
5702                         isc_task_sendanddetach(&current_task,
5703                                                ISC_EVENT_PTR(&dev));
5704                 }
5705         }
5706
5707         UNLOCK(&sock->lock);
5708 }
5709
5710 ISC_SOCKETFUNC_SCOPE isc_sockettype_t
5711 isc__socket_gettype(isc_socket_t *sock0) {
5712         isc__socket_t *sock = (isc__socket_t *)sock0;
5713
5714         REQUIRE(VALID_SOCKET(sock));
5715
5716         return (sock->type);
5717 }
5718
5719 ISC_SOCKETFUNC_SCOPE isc_boolean_t
5720 isc__socket_isbound(isc_socket_t *sock0) {
5721         isc__socket_t *sock = (isc__socket_t *)sock0;
5722         isc_boolean_t val;
5723
5724         REQUIRE(VALID_SOCKET(sock));
5725
5726         LOCK(&sock->lock);
5727         val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
5728         UNLOCK(&sock->lock);
5729
5730         return (val);
5731 }
5732
5733 ISC_SOCKETFUNC_SCOPE void
5734 isc__socket_ipv6only(isc_socket_t *sock0, isc_boolean_t yes) {
5735         isc__socket_t *sock = (isc__socket_t *)sock0;
5736 #if defined(IPV6_V6ONLY)
5737         int onoff = yes ? 1 : 0;
5738 #else
5739         UNUSED(yes);
5740         UNUSED(sock);
5741 #endif
5742
5743         REQUIRE(VALID_SOCKET(sock));
5744         INSIST(!sock->dupped);
5745
5746 #ifdef IPV6_V6ONLY
5747         if (sock->pf == AF_INET6) {
5748                 if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
5749                                (void *)&onoff, sizeof(int)) < 0) {
5750                         char strbuf[ISC_STRERRORSIZE];
5751                         isc__strerror(errno, strbuf, sizeof(strbuf));
5752                         UNEXPECTED_ERROR(__FILE__, __LINE__,
5753                                          "setsockopt(%d, IPV6_V6ONLY) "
5754                                          "%s: %s", sock->fd,
5755                                          isc_msgcat_get(isc_msgcat,
5756                                                         ISC_MSGSET_GENERAL,
5757                                                         ISC_MSG_FAILED,
5758                                                         "failed"),
5759                                          strbuf);
5760                 }
5761         }
5762         FIX_IPV6_RECVPKTINFO(sock);     /* AIX */
5763 #endif
5764 }
5765
5766 #ifndef USE_WATCHER_THREAD
5767 /*
5768  * In our assumed scenario, we can simply use a single static object.
5769  * XXX: this is not true if the application uses multiple threads with
5770  *      'multi-context' mode.  Fixing this is a future TODO item.
5771  */
5772 static isc_socketwait_t swait_private;
5773
5774 int
5775 isc__socketmgr_waitevents(isc_socketmgr_t *manager0, struct timeval *tvp,
5776                           isc_socketwait_t **swaitp)
5777 {
5778         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
5779
5780
5781         int n;
5782 #ifdef USE_KQUEUE
5783         struct timespec ts, *tsp;
5784 #endif
5785 #ifdef USE_EPOLL
5786         int timeout;
5787 #endif
5788 #ifdef USE_DEVPOLL
5789         struct dvpoll dvp;
5790 #endif
5791
5792         REQUIRE(swaitp != NULL && *swaitp == NULL);
5793
5794 #ifdef USE_SHARED_MANAGER
5795         if (manager == NULL)
5796                 manager = socketmgr;
5797 #endif
5798         if (manager == NULL)
5799                 return (0);
5800
5801 #ifdef USE_KQUEUE
5802         if (tvp != NULL) {
5803                 ts.tv_sec = tvp->tv_sec;
5804                 ts.tv_nsec = tvp->tv_usec * 1000;
5805                 tsp = &ts;
5806         } else
5807                 tsp = NULL;
5808         swait_private.nevents = kevent(manager->kqueue_fd, NULL, 0,
5809                                        manager->events, manager->nevents,
5810                                        tsp);
5811         n = swait_private.nevents;
5812 #elif defined(USE_EPOLL)
5813         if (tvp != NULL)
5814                 timeout = tvp->tv_sec * 1000 + (tvp->tv_usec + 999) / 1000;
5815         else
5816                 timeout = -1;
5817         swait_private.nevents = epoll_wait(manager->epoll_fd,
5818                                            manager->events,
5819                                            manager->nevents, timeout);
5820         n = swait_private.nevents;
5821 #elif defined(USE_DEVPOLL)
5822         dvp.dp_fds = manager->events;
5823         dvp.dp_nfds = manager->nevents;
5824         if (tvp != NULL) {
5825                 dvp.dp_timeout = tvp->tv_sec * 1000 +
5826                         (tvp->tv_usec + 999) / 1000;
5827         } else
5828                 dvp.dp_timeout = -1;
5829         swait_private.nevents = ioctl(manager->devpoll_fd, DP_POLL, &dvp);
5830         n = swait_private.nevents;
5831 #elif defined(USE_SELECT)
5832         memcpy(manager->read_fds_copy, manager->read_fds,  manager->fd_bufsize);
5833         memcpy(manager->write_fds_copy, manager->write_fds,
5834                manager->fd_bufsize);
5835
5836         swait_private.readset = manager->read_fds_copy;
5837         swait_private.writeset = manager->write_fds_copy;
5838         swait_private.maxfd = manager->maxfd + 1;
5839
5840         n = select(swait_private.maxfd, swait_private.readset,
5841                    swait_private.writeset, NULL, tvp);
5842 #endif
5843
5844         *swaitp = &swait_private;
5845         return (n);
5846 }
5847
5848 isc_result_t
5849 isc__socketmgr_dispatch(isc_socketmgr_t *manager0, isc_socketwait_t *swait) {
5850         isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
5851
5852         REQUIRE(swait == &swait_private);
5853
5854 #ifdef USE_SHARED_MANAGER
5855         if (manager == NULL)
5856                 manager = socketmgr;
5857 #endif
5858         if (manager == NULL)
5859                 return (ISC_R_NOTFOUND);
5860
5861 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
5862         (void)process_fds(manager, manager->events, swait->nevents);
5863         return (ISC_R_SUCCESS);
5864 #elif defined(USE_SELECT)
5865         process_fds(manager, swait->maxfd, swait->readset, swait->writeset);
5866         return (ISC_R_SUCCESS);
5867 #endif
5868 }
5869 #endif /* USE_WATCHER_THREAD */
5870
5871 #ifdef BIND9
5872 void
5873 isc__socket_setname(isc_socket_t *socket0, const char *name, void *tag) {
5874         isc__socket_t *socket = (isc__socket_t *)socket0;
5875
5876         /*
5877          * Name 'socket'.
5878          */
5879
5880         REQUIRE(VALID_SOCKET(socket));
5881
5882         LOCK(&socket->lock);
5883         memset(socket->name, 0, sizeof(socket->name));
5884         strncpy(socket->name, name, sizeof(socket->name) - 1);
5885         socket->tag = tag;
5886         UNLOCK(&socket->lock);
5887 }
5888
5889 ISC_SOCKETFUNC_SCOPE const char *
5890 isc__socket_getname(isc_socket_t *socket0) {
5891         isc__socket_t *socket = (isc__socket_t *)socket0;
5892
5893         return (socket->name);
5894 }
5895
5896 void *
5897 isc__socket_gettag(isc_socket_t *socket0) {
5898         isc__socket_t *socket = (isc__socket_t *)socket0;
5899
5900         return (socket->tag);
5901 }
5902 #endif  /* BIND9 */
5903
5904 #ifdef USE_SOCKETIMPREGISTER
5905 isc_result_t
5906 isc__socket_register() {
5907         return (isc_socket_register(isc__socketmgr_create));
5908 }
5909 #endif
5910
5911 ISC_SOCKETFUNC_SCOPE int
5912 isc__socket_getfd(isc_socket_t *socket0) {
5913         isc__socket_t *socket = (isc__socket_t *)socket0;
5914
5915         return ((short) socket->fd);
5916 }
5917
5918 #if defined(HAVE_LIBXML2) && defined(BIND9)
5919
5920 static const char *
5921 _socktype(isc_sockettype_t type)
5922 {
5923         if (type == isc_sockettype_udp)
5924                 return ("udp");
5925         else if (type == isc_sockettype_tcp)
5926                 return ("tcp");
5927         else if (type == isc_sockettype_unix)
5928                 return ("unix");
5929         else if (type == isc_sockettype_fdwatch)
5930                 return ("fdwatch");
5931         else
5932                 return ("not-initialized");
5933 }
5934
5935 #define TRY0(a) do { xmlrc = (a); if (xmlrc < 0) goto error; } while(0)
5936 ISC_SOCKETFUNC_SCOPE int
5937 isc_socketmgr_renderxml(isc_socketmgr_t *mgr0, xmlTextWriterPtr writer) {
5938         isc__socketmgr_t *mgr = (isc__socketmgr_t *)mgr0;
5939         isc__socket_t *sock = NULL;
5940         char peerbuf[ISC_SOCKADDR_FORMATSIZE];
5941         isc_sockaddr_t addr;
5942         ISC_SOCKADDR_LEN_T len;
5943         int xmlrc;
5944
5945         LOCK(&mgr->lock);
5946
5947 #ifdef USE_SHARED_MANAGER
5948         TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "references"));
5949         TRY0(xmlTextWriterWriteFormatString(writer, "%d", mgr->refs));
5950         TRY0(xmlTextWriterEndElement(writer));
5951 #endif  /* USE_SHARED_MANAGER */
5952
5953         TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "sockets"));
5954         sock = ISC_LIST_HEAD(mgr->socklist);
5955         while (sock != NULL) {
5956                 LOCK(&sock->lock);
5957                 TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "socket"));
5958
5959                 TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "id"));
5960                 TRY0(xmlTextWriterWriteFormatString(writer, "%p", sock));
5961                 TRY0(xmlTextWriterEndElement(writer));
5962
5963                 if (sock->name[0] != 0) {
5964                         TRY0(xmlTextWriterStartElement(writer,
5965                                                        ISC_XMLCHAR "name"));
5966                         TRY0(xmlTextWriterWriteFormatString(writer, "%s",
5967                                                             sock->name));
5968                         TRY0(xmlTextWriterEndElement(writer)); /* name */
5969                 }
5970
5971                 TRY0(xmlTextWriterStartElement(writer,
5972                                                ISC_XMLCHAR "references"));
5973                 TRY0(xmlTextWriterWriteFormatString(writer, "%d",
5974                                                     sock->references));
5975                 TRY0(xmlTextWriterEndElement(writer));
5976
5977                 TRY0(xmlTextWriterWriteElement(writer, ISC_XMLCHAR "type",
5978                                           ISC_XMLCHAR _socktype(sock->type)));
5979
5980                 if (sock->connected) {
5981                         isc_sockaddr_format(&sock->peer_address, peerbuf,
5982                                             sizeof(peerbuf));
5983                         TRY0(xmlTextWriterWriteElement(writer,
5984                                                   ISC_XMLCHAR "peer-address",
5985                                                   ISC_XMLCHAR peerbuf));
5986                 }
5987
5988                 len = sizeof(addr);
5989                 if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
5990                         isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
5991                         TRY0(xmlTextWriterWriteElement(writer,
5992                                                   ISC_XMLCHAR "local-address",
5993                                                   ISC_XMLCHAR peerbuf));
5994                 }
5995
5996                 TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "states"));
5997                 if (sock->pending_recv)
5998                         TRY0(xmlTextWriterWriteElement(writer,
5999                                                 ISC_XMLCHAR "state",
6000                                                 ISC_XMLCHAR "pending-receive"));
6001                 if (sock->pending_send)
6002                         TRY0(xmlTextWriterWriteElement(writer,
6003                                                   ISC_XMLCHAR "state",
6004                                                   ISC_XMLCHAR "pending-send"));
6005                 if (sock->pending_accept)
6006                         TRY0(xmlTextWriterWriteElement(writer,
6007                                                  ISC_XMLCHAR "state",
6008                                                  ISC_XMLCHAR "pending_accept"));
6009                 if (sock->listener)
6010                         TRY0(xmlTextWriterWriteElement(writer,
6011                                                        ISC_XMLCHAR "state",
6012                                                        ISC_XMLCHAR "listener"));
6013                 if (sock->connected)
6014                         TRY0(xmlTextWriterWriteElement(writer,
6015                                                      ISC_XMLCHAR "state",
6016                                                      ISC_XMLCHAR "connected"));
6017                 if (sock->connecting)
6018                         TRY0(xmlTextWriterWriteElement(writer,
6019                                                     ISC_XMLCHAR "state",
6020                                                     ISC_XMLCHAR "connecting"));
6021                 if (sock->bound)
6022                         TRY0(xmlTextWriterWriteElement(writer,
6023                                                        ISC_XMLCHAR "state",
6024                                                        ISC_XMLCHAR "bound"));
6025
6026                 TRY0(xmlTextWriterEndElement(writer)); /* states */
6027
6028                 TRY0(xmlTextWriterEndElement(writer)); /* socket */
6029
6030                 UNLOCK(&sock->lock);
6031                 sock = ISC_LIST_NEXT(sock, link);
6032         }
6033         TRY0(xmlTextWriterEndElement(writer)); /* sockets */
6034
6035  error:
6036         if (sock != NULL)
6037                 UNLOCK(&sock->lock);
6038
6039         UNLOCK(&mgr->lock);
6040
6041         return (xmlrc);
6042 }
6043 #endif /* HAVE_LIBXML2 */