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