No empty .Rs/.Re
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / unix / socket.c
blob8a737c06519c68ce7199bc13afd2fc8961abae1f
1 /* $NetBSD: socket.c,v 1.1.1.4 2009/12/26 22:26:00 christos Exp $ */
3 /*
4 * Copyright (C) 2004-2009 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: socket.c,v 1.326 2009/11/13 00:41:58 each Exp */
22 /*! \file */
24 #include <config.h>
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/uio.h>
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include <isc/buffer.h>
41 #include <isc/bufferlist.h>
42 #include <isc/condition.h>
43 #include <isc/formatcheck.h>
44 #include <isc/list.h>
45 #include <isc/log.h>
46 #include <isc/mem.h>
47 #include <isc/msgs.h>
48 #include <isc/mutex.h>
49 #include <isc/net.h>
50 #include <isc/once.h>
51 #include <isc/platform.h>
52 #include <isc/print.h>
53 #include <isc/region.h>
54 #include <isc/socket.h>
55 #include <isc/stats.h>
56 #include <isc/strerror.h>
57 #include <isc/task.h>
58 #include <isc/thread.h>
59 #include <isc/util.h>
60 #include <isc/xml.h>
62 #ifdef ISC_PLATFORM_HAVESYSUNH
63 #include <sys/un.h>
64 #endif
65 #ifdef ISC_PLATFORM_HAVEKQUEUE
66 #include <sys/event.h>
67 #endif
68 #ifdef ISC_PLATFORM_HAVEEPOLL
69 #include <sys/epoll.h>
70 #endif
71 #ifdef ISC_PLATFORM_HAVEDEVPOLL
72 #include <sys/devpoll.h>
73 #endif
75 #include "errno2result.h"
77 /* See task.c about the following definition: */
78 #ifdef BIND9
79 #ifdef ISC_PLATFORM_USETHREADS
80 #define USE_WATCHER_THREAD
81 #else
82 #define USE_SHARED_MANAGER
83 #endif /* ISC_PLATFORM_USETHREADS */
84 #endif /* BIND9 */
86 #ifndef USE_WATCHER_THREAD
87 #include "socket_p.h"
88 #endif /* USE_WATCHER_THREAD */
90 #if defined(SO_BSDCOMPAT) && defined(__linux__)
91 #include <sys/utsname.h>
92 #endif
94 /*%
95 * Choose the most preferable multiplex method.
97 #ifdef ISC_PLATFORM_HAVEKQUEUE
98 #define USE_KQUEUE
99 #elif defined (ISC_PLATFORM_HAVEEPOLL)
100 #define USE_EPOLL
101 #elif defined (ISC_PLATFORM_HAVEDEVPOLL)
102 #define USE_DEVPOLL
103 typedef struct {
104 unsigned int want_read : 1,
105 want_write : 1;
106 } pollinfo_t;
107 #else
108 #define USE_SELECT
109 #endif /* ISC_PLATFORM_HAVEKQUEUE */
111 #ifndef USE_WATCHER_THREAD
112 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
113 struct isc_socketwait {
114 int nevents;
116 #elif defined (USE_SELECT)
117 struct isc_socketwait {
118 fd_set *readset;
119 fd_set *writeset;
120 int nfds;
121 int maxfd;
123 #endif /* USE_KQUEUE */
124 #endif /* !USE_WATCHER_THREAD */
127 * Maximum number of allowable open sockets. This is also the maximum
128 * allowable socket file descriptor.
130 * Care should be taken before modifying this value for select():
131 * The API standard doesn't ensure select() accept more than (the system default
132 * of) FD_SETSIZE descriptors, and the default size should in fact be fine in
133 * the vast majority of cases. This constant should therefore be increased only
134 * when absolutely necessary and possible, i.e., the server is exhausting all
135 * available file descriptors (up to FD_SETSIZE) and the select() function
136 * and FD_xxx macros support larger values than FD_SETSIZE (which may not
137 * always by true, but we keep using some of them to ensure as much
138 * portability as possible). Note also that overall server performance
139 * may be rather worsened with a larger value of this constant due to
140 * inherent scalability problems of select().
142 * As a special note, this value shouldn't have to be touched if
143 * this is a build for an authoritative only DNS server.
145 #ifndef ISC_SOCKET_MAXSOCKETS
146 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
147 #define ISC_SOCKET_MAXSOCKETS 4096
148 #elif defined(USE_SELECT)
149 #define ISC_SOCKET_MAXSOCKETS FD_SETSIZE
150 #endif /* USE_KQUEUE... */
151 #endif /* ISC_SOCKET_MAXSOCKETS */
153 #ifdef USE_SELECT
155 * Mac OS X needs a special definition to support larger values in select().
156 * We always define this because a larger value can be specified run-time.
158 #ifdef __APPLE__
159 #define _DARWIN_UNLIMITED_SELECT
160 #endif /* __APPLE__ */
161 #endif /* USE_SELECT */
163 #ifdef ISC_SOCKET_USE_POLLWATCH
165 * If this macro is defined, enable workaround for a Solaris /dev/poll kernel
166 * bug: DP_POLL ioctl could keep sleeping even if socket I/O is possible for
167 * some of the specified FD. The idea is based on the observation that it's
168 * likely for a busy server to keep receiving packets. It specifically works
169 * as follows: the socket watcher is first initialized with the state of
170 * "poll_idle". While it's in the idle state it keeps sleeping until a socket
171 * event occurs. When it wakes up for a socket I/O event, it moves to the
172 * poll_active state, and sets the poll timeout to a short period
173 * (ISC_SOCKET_POLLWATCH_TIMEOUT msec). If timeout occurs in this state, the
174 * watcher goes to the poll_checking state with the same timeout period.
175 * In this state, the watcher tries to detect whether this is a break
176 * during intermittent events or the kernel bug is triggered. If the next
177 * polling reports an event within the short period, the previous timeout is
178 * likely to be a kernel bug, and so the watcher goes back to the active state.
179 * Otherwise, it moves to the idle state again.
181 * It's not clear whether this is a thread-related bug, but since we've only
182 * seen this with threads, this workaround is used only when enabling threads.
185 typedef enum { poll_idle, poll_active, poll_checking } pollstate_t;
187 #ifndef ISC_SOCKET_POLLWATCH_TIMEOUT
188 #define ISC_SOCKET_POLLWATCH_TIMEOUT 10
189 #endif /* ISC_SOCKET_POLLWATCH_TIMEOUT */
190 #endif /* ISC_SOCKET_USE_POLLWATCH */
193 * Size of per-FD lock buckets.
195 #ifdef ISC_PLATFORM_USETHREADS
196 #define FDLOCK_COUNT 1024
197 #define FDLOCK_ID(fd) ((fd) % FDLOCK_COUNT)
198 #else
199 #define FDLOCK_COUNT 1
200 #define FDLOCK_ID(fd) 0
201 #endif /* ISC_PLATFORM_USETHREADS */
204 * Maximum number of events communicated with the kernel. There should normally
205 * be no need for having a large number.
207 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
208 #ifndef ISC_SOCKET_MAXEVENTS
209 #define ISC_SOCKET_MAXEVENTS 64
210 #endif
211 #endif
214 * Some systems define the socket length argument as an int, some as size_t,
215 * some as socklen_t. This is here so it can be easily changed if needed.
217 #ifndef ISC_SOCKADDR_LEN_T
218 #define ISC_SOCKADDR_LEN_T unsigned int
219 #endif
222 * Define what the possible "soft" errors can be. These are non-fatal returns
223 * of various network related functions, like recv() and so on.
225 * For some reason, BSDI (and perhaps others) will sometimes return <0
226 * from recv() but will have errno==0. This is broken, but we have to
227 * work around it here.
229 #define SOFT_ERROR(e) ((e) == EAGAIN || \
230 (e) == EWOULDBLOCK || \
231 (e) == EINTR || \
232 (e) == 0)
234 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
236 /*!<
237 * DLVL(90) -- Function entry/exit and other tracing.
238 * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
239 * DLVL(60) -- Socket data send/receive
240 * DLVL(50) -- Event tracing, including receiving/sending completion events.
241 * DLVL(20) -- Socket creation/destruction.
243 #define TRACE_LEVEL 90
244 #define CORRECTNESS_LEVEL 70
245 #define IOEVENT_LEVEL 60
246 #define EVENT_LEVEL 50
247 #define CREATION_LEVEL 20
249 #define TRACE DLVL(TRACE_LEVEL)
250 #define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
251 #define IOEVENT DLVL(IOEVENT_LEVEL)
252 #define EVENT DLVL(EVENT_LEVEL)
253 #define CREATION DLVL(CREATION_LEVEL)
255 typedef isc_event_t intev_t;
257 #define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
258 #define VALID_SOCKET(s) ISC_MAGIC_VALID(s, SOCKET_MAGIC)
261 * IPv6 control information. If the socket is an IPv6 socket we want
262 * to collect the destination address and interface so the client can
263 * set them on outgoing packets.
265 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
266 #ifndef USE_CMSG
267 #define USE_CMSG 1
268 #endif
269 #endif
272 * NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
273 * a setsockopt() like interface to request timestamps, and if the OS
274 * doesn't do it for us, call gettimeofday() on every UDP receive?
276 #ifdef SO_TIMESTAMP
277 #ifndef USE_CMSG
278 #define USE_CMSG 1
279 #endif
280 #endif
283 * The size to raise the receive buffer to (from BIND 8).
285 #define RCVBUFSIZE (32*1024)
288 * The number of times a send operation is repeated if the result is EINTR.
290 #define NRETRIES 10
292 typedef struct isc__socket isc__socket_t;
293 typedef struct isc__socketmgr isc__socketmgr_t;
295 #define NEWCONNSOCK(ev) ((isc__socket_t *)(ev)->newsocket)
297 struct isc__socket {
298 /* Not locked. */
299 isc_socket_t common;
300 isc__socketmgr_t *manager;
301 isc_mutex_t lock;
302 isc_sockettype_t type;
303 const isc_statscounter_t *statsindex;
305 /* Locked by socket lock. */
306 ISC_LINK(isc__socket_t) link;
307 unsigned int references;
308 int fd;
309 int pf;
310 char name[16];
311 void * tag;
313 ISC_LIST(isc_socketevent_t) send_list;
314 ISC_LIST(isc_socketevent_t) recv_list;
315 ISC_LIST(isc_socket_newconnev_t) accept_list;
316 isc_socket_connev_t *connect_ev;
319 * Internal events. Posted when a descriptor is readable or
320 * writable. These are statically allocated and never freed.
321 * They will be set to non-purgable before use.
323 intev_t readable_ev;
324 intev_t writable_ev;
326 isc_sockaddr_t peer_address; /* remote address */
328 unsigned int pending_recv : 1,
329 pending_send : 1,
330 pending_accept : 1,
331 listener : 1, /* listener socket */
332 connected : 1,
333 connecting : 1, /* connect pending */
334 bound : 1; /* bound to local addr */
336 #ifdef ISC_NET_RECVOVERFLOW
337 unsigned char overflow; /* used for MSG_TRUNC fake */
338 #endif
340 char *recvcmsgbuf;
341 ISC_SOCKADDR_LEN_T recvcmsgbuflen;
342 char *sendcmsgbuf;
343 ISC_SOCKADDR_LEN_T sendcmsgbuflen;
345 void *fdwatcharg;
346 isc_sockfdwatch_t fdwatchcb;
347 int fdwatchflags;
348 isc_task_t *fdwatchtask;
351 #define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
352 #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
354 struct isc__socketmgr {
355 /* Not locked. */
356 isc_socketmgr_t common;
357 isc_mem_t *mctx;
358 isc_mutex_t lock;
359 isc_mutex_t *fdlock;
360 isc_stats_t *stats;
361 #ifdef USE_KQUEUE
362 int kqueue_fd;
363 int nevents;
364 struct kevent *events;
365 #endif /* USE_KQUEUE */
366 #ifdef USE_EPOLL
367 int epoll_fd;
368 int nevents;
369 struct epoll_event *events;
370 #endif /* USE_EPOLL */
371 #ifdef USE_DEVPOLL
372 int devpoll_fd;
373 int nevents;
374 struct pollfd *events;
375 #endif /* USE_DEVPOLL */
376 #ifdef USE_SELECT
377 int fd_bufsize;
378 #endif /* USE_SELECT */
379 unsigned int maxsocks;
380 #ifdef ISC_PLATFORM_USETHREADS
381 int pipe_fds[2];
382 #endif
384 /* Locked by fdlock. */
385 isc__socket_t **fds;
386 int *fdstate;
387 #ifdef USE_DEVPOLL
388 pollinfo_t *fdpollinfo;
389 #endif
391 /* Locked by manager lock. */
392 ISC_LIST(isc__socket_t) socklist;
393 #ifdef USE_SELECT
394 fd_set *read_fds;
395 fd_set *read_fds_copy;
396 fd_set *write_fds;
397 fd_set *write_fds_copy;
398 int maxfd;
399 #endif /* USE_SELECT */
400 int reserved; /* unlocked */
401 #ifdef USE_WATCHER_THREAD
402 isc_thread_t watcher;
403 isc_condition_t shutdown_ok;
404 #else /* USE_WATCHER_THREAD */
405 unsigned int refs;
406 #endif /* USE_WATCHER_THREAD */
407 int maxudp;
410 #ifdef USE_SHARED_MANAGER
411 static isc__socketmgr_t *socketmgr = NULL;
412 #endif /* USE_SHARED_MANAGER */
414 #define CLOSED 0 /* this one must be zero */
415 #define MANAGED 1
416 #define CLOSE_PENDING 2
419 * send() and recv() iovec counts
421 #define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
422 #ifdef ISC_NET_RECVOVERFLOW
423 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
424 #else
425 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
426 #endif
428 static void send_recvdone_event(isc__socket_t *, isc_socketevent_t **);
429 static void send_senddone_event(isc__socket_t *, isc_socketevent_t **);
430 static void free_socket(isc__socket_t **);
431 static isc_result_t allocate_socket(isc__socketmgr_t *, isc_sockettype_t,
432 isc__socket_t **);
433 static void destroy(isc__socket_t **);
434 static void internal_accept(isc_task_t *, isc_event_t *);
435 static void internal_connect(isc_task_t *, isc_event_t *);
436 static void internal_recv(isc_task_t *, isc_event_t *);
437 static void internal_send(isc_task_t *, isc_event_t *);
438 static void internal_fdwatch_write(isc_task_t *, isc_event_t *);
439 static void internal_fdwatch_read(isc_task_t *, isc_event_t *);
440 static void process_cmsg(isc__socket_t *, struct msghdr *, isc_socketevent_t *);
441 static void build_msghdr_send(isc__socket_t *, isc_socketevent_t *,
442 struct msghdr *, struct iovec *, size_t *);
443 static void build_msghdr_recv(isc__socket_t *, isc_socketevent_t *,
444 struct msghdr *, struct iovec *, size_t *);
445 #ifdef USE_WATCHER_THREAD
446 static isc_boolean_t process_ctlfd(isc__socketmgr_t *manager);
447 #endif
450 * The following can be either static or public, depending on build environment.
453 #ifdef BIND9
454 #define ISC_SOCKETFUNC_SCOPE
455 #else
456 #define ISC_SOCKETFUNC_SCOPE static
457 #endif
459 ISC_SOCKETFUNC_SCOPE isc_result_t
460 isc__socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
461 isc_socket_t **socketp);
462 ISC_SOCKETFUNC_SCOPE void
463 isc__socket_attach(isc_socket_t *sock, isc_socket_t **socketp);
464 ISC_SOCKETFUNC_SCOPE void
465 isc__socket_detach(isc_socket_t **socketp);
466 ISC_SOCKETFUNC_SCOPE isc_result_t
467 isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
468 ISC_SOCKETFUNC_SCOPE isc_result_t
469 isc__socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
470 unsigned int maxsocks);
471 ISC_SOCKETFUNC_SCOPE void
472 isc__socketmgr_destroy(isc_socketmgr_t **managerp);
473 ISC_SOCKETFUNC_SCOPE isc_result_t
474 isc__socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
475 unsigned int minimum, isc_task_t *task,
476 isc_taskaction_t action, const void *arg);
477 ISC_SOCKETFUNC_SCOPE isc_result_t
478 isc__socket_recv(isc_socket_t *sock, isc_region_t *region,
479 unsigned int minimum, isc_task_t *task,
480 isc_taskaction_t action, const void *arg);
481 ISC_SOCKETFUNC_SCOPE isc_result_t
482 isc__socket_recv2(isc_socket_t *sock, isc_region_t *region,
483 unsigned int minimum, isc_task_t *task,
484 isc_socketevent_t *event, unsigned int flags);
485 ISC_SOCKETFUNC_SCOPE isc_result_t
486 isc__socket_send(isc_socket_t *sock, isc_region_t *region,
487 isc_task_t *task, isc_taskaction_t action, const void *arg);
488 ISC_SOCKETFUNC_SCOPE isc_result_t
489 isc__socket_sendto(isc_socket_t *sock, isc_region_t *region,
490 isc_task_t *task, isc_taskaction_t action, const void *arg,
491 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
492 ISC_SOCKETFUNC_SCOPE isc_result_t
493 isc__socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
494 isc_task_t *task, isc_taskaction_t action, const void *arg);
495 ISC_SOCKETFUNC_SCOPE isc_result_t
496 isc__socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
497 isc_task_t *task, isc_taskaction_t action, const void *arg,
498 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
499 ISC_SOCKETFUNC_SCOPE isc_result_t
500 isc__socket_sendto2(isc_socket_t *sock, isc_region_t *region,
501 isc_task_t *task,
502 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
503 isc_socketevent_t *event, unsigned int flags);
504 ISC_SOCKETFUNC_SCOPE void
505 isc__socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active);
506 ISC_SOCKETFUNC_SCOPE isc_result_t
507 isc__socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
508 isc_uint32_t owner, isc_uint32_t group);
509 ISC_SOCKETFUNC_SCOPE isc_result_t
510 isc__socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
511 unsigned int options);
512 ISC_SOCKETFUNC_SCOPE isc_result_t
513 isc__socket_filter(isc_socket_t *sock, const char *filter);
514 ISC_SOCKETFUNC_SCOPE isc_result_t
515 isc__socket_listen(isc_socket_t *sock, unsigned int backlog);
516 ISC_SOCKETFUNC_SCOPE isc_result_t
517 isc__socket_accept(isc_socket_t *sock,
518 isc_task_t *task, isc_taskaction_t action, const void *arg);
519 ISC_SOCKETFUNC_SCOPE isc_result_t
520 isc__socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
521 isc_task_t *task, isc_taskaction_t action,
522 const void *arg);
523 ISC_SOCKETFUNC_SCOPE isc_result_t
524 isc__socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp);
525 ISC_SOCKETFUNC_SCOPE isc_result_t
526 isc__socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp);
527 ISC_SOCKETFUNC_SCOPE void
528 isc__socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how);
529 ISC_SOCKETFUNC_SCOPE isc_sockettype_t
530 isc__socket_gettype(isc_socket_t *sock);
531 ISC_SOCKETFUNC_SCOPE isc_boolean_t
532 isc__socket_isbound(isc_socket_t *sock);
533 ISC_SOCKETFUNC_SCOPE void
534 isc__socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes);
535 #if defined(HAVE_LIBXML2) && defined(BIND9)
536 ISC_SOCKETFUNC_SCOPE void
537 isc__socketmgr_renderxml(isc_socketmgr_t *mgr0, xmlTextWriterPtr writer);
538 #endif
540 ISC_SOCKETFUNC_SCOPE isc_result_t
541 isc__socket_fdwatchcreate(isc_socketmgr_t *manager, int fd, int flags,
542 isc_sockfdwatch_t callback, void *cbarg,
543 isc_task_t *task, isc_socket_t **socketp);
544 ISC_SOCKETFUNC_SCOPE isc_result_t
545 isc__socket_fdwatchpoke(isc_socket_t *sock, int flags);
547 static struct {
548 isc_socketmethods_t methods;
551 * The following are defined just for avoiding unused static functions.
553 #ifndef BIND9
554 void *recvv, *send, *sendv, *sendto2, *cleanunix, *permunix, *filter,
555 *listen, *accept, *getpeername, *isbound;
556 #endif
557 } socketmethods = {
559 isc__socket_attach,
560 isc__socket_detach,
561 isc__socket_bind,
562 isc__socket_sendto,
563 isc__socket_connect,
564 isc__socket_recv,
565 isc__socket_cancel,
566 isc__socket_getsockname,
567 isc__socket_gettype,
568 isc__socket_ipv6only,
569 isc__socket_fdwatchpoke
571 #ifndef BIND9
573 (void *)isc__socket_recvv, (void *)isc__socket_send,
574 (void *)isc__socket_sendv, (void *)isc__socket_sendto2,
575 (void *)isc__socket_cleanunix, (void *)isc__socket_permunix,
576 (void *)isc__socket_filter, (void *)isc__socket_listen,
577 (void *)isc__socket_accept, (void *)isc__socket_getpeername,
578 (void *)isc__socket_isbound
579 #endif
582 static isc_socketmgrmethods_t socketmgrmethods = {
583 isc__socketmgr_destroy,
584 isc__socket_create,
585 isc__socket_fdwatchcreate
588 #define SELECT_POKE_SHUTDOWN (-1)
589 #define SELECT_POKE_NOTHING (-2)
590 #define SELECT_POKE_READ (-3)
591 #define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
592 #define SELECT_POKE_WRITE (-4)
593 #define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
594 #define SELECT_POKE_CLOSE (-5)
596 #define SOCK_DEAD(s) ((s)->references == 0)
599 * Shortcut index arrays to get access to statistics counters.
601 enum {
602 STATID_OPEN = 0,
603 STATID_OPENFAIL = 1,
604 STATID_CLOSE = 2,
605 STATID_BINDFAIL = 3,
606 STATID_CONNECTFAIL = 4,
607 STATID_CONNECT = 5,
608 STATID_ACCEPTFAIL = 6,
609 STATID_ACCEPT = 7,
610 STATID_SENDFAIL = 8,
611 STATID_RECVFAIL = 9
613 static const isc_statscounter_t upd4statsindex[] = {
614 isc_sockstatscounter_udp4open,
615 isc_sockstatscounter_udp4openfail,
616 isc_sockstatscounter_udp4close,
617 isc_sockstatscounter_udp4bindfail,
618 isc_sockstatscounter_udp4connectfail,
619 isc_sockstatscounter_udp4connect,
622 isc_sockstatscounter_udp4sendfail,
623 isc_sockstatscounter_udp4recvfail
625 static const isc_statscounter_t upd6statsindex[] = {
626 isc_sockstatscounter_udp6open,
627 isc_sockstatscounter_udp6openfail,
628 isc_sockstatscounter_udp6close,
629 isc_sockstatscounter_udp6bindfail,
630 isc_sockstatscounter_udp6connectfail,
631 isc_sockstatscounter_udp6connect,
634 isc_sockstatscounter_udp6sendfail,
635 isc_sockstatscounter_udp6recvfail
637 static const isc_statscounter_t tcp4statsindex[] = {
638 isc_sockstatscounter_tcp4open,
639 isc_sockstatscounter_tcp4openfail,
640 isc_sockstatscounter_tcp4close,
641 isc_sockstatscounter_tcp4bindfail,
642 isc_sockstatscounter_tcp4connectfail,
643 isc_sockstatscounter_tcp4connect,
644 isc_sockstatscounter_tcp4acceptfail,
645 isc_sockstatscounter_tcp4accept,
646 isc_sockstatscounter_tcp4sendfail,
647 isc_sockstatscounter_tcp4recvfail
649 static const isc_statscounter_t tcp6statsindex[] = {
650 isc_sockstatscounter_tcp6open,
651 isc_sockstatscounter_tcp6openfail,
652 isc_sockstatscounter_tcp6close,
653 isc_sockstatscounter_tcp6bindfail,
654 isc_sockstatscounter_tcp6connectfail,
655 isc_sockstatscounter_tcp6connect,
656 isc_sockstatscounter_tcp6acceptfail,
657 isc_sockstatscounter_tcp6accept,
658 isc_sockstatscounter_tcp6sendfail,
659 isc_sockstatscounter_tcp6recvfail
661 static const isc_statscounter_t unixstatsindex[] = {
662 isc_sockstatscounter_unixopen,
663 isc_sockstatscounter_unixopenfail,
664 isc_sockstatscounter_unixclose,
665 isc_sockstatscounter_unixbindfail,
666 isc_sockstatscounter_unixconnectfail,
667 isc_sockstatscounter_unixconnect,
668 isc_sockstatscounter_unixacceptfail,
669 isc_sockstatscounter_unixaccept,
670 isc_sockstatscounter_unixsendfail,
671 isc_sockstatscounter_unixrecvfail
673 static const isc_statscounter_t fdwatchstatsindex[] = {
676 isc_sockstatscounter_fdwatchclose,
677 isc_sockstatscounter_fdwatchbindfail,
678 isc_sockstatscounter_fdwatchconnectfail,
679 isc_sockstatscounter_fdwatchconnect,
682 isc_sockstatscounter_fdwatchsendfail,
683 isc_sockstatscounter_fdwatchrecvfail
686 static void
687 manager_log(isc__socketmgr_t *sockmgr,
688 isc_logcategory_t *category, isc_logmodule_t *module, int level,
689 const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
690 static void
691 manager_log(isc__socketmgr_t *sockmgr,
692 isc_logcategory_t *category, isc_logmodule_t *module, int level,
693 const char *fmt, ...)
695 char msgbuf[2048];
696 va_list ap;
698 if (! isc_log_wouldlog(isc_lctx, level))
699 return;
701 va_start(ap, fmt);
702 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
703 va_end(ap);
705 isc_log_write(isc_lctx, category, module, level,
706 "sockmgr %p: %s", sockmgr, msgbuf);
709 static void
710 socket_log(isc__socket_t *sock, isc_sockaddr_t *address,
711 isc_logcategory_t *category, isc_logmodule_t *module, int level,
712 isc_msgcat_t *msgcat, int msgset, int message,
713 const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
714 static void
715 socket_log(isc__socket_t *sock, isc_sockaddr_t *address,
716 isc_logcategory_t *category, isc_logmodule_t *module, int level,
717 isc_msgcat_t *msgcat, int msgset, int message,
718 const char *fmt, ...)
720 char msgbuf[2048];
721 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
722 va_list ap;
724 if (! isc_log_wouldlog(isc_lctx, level))
725 return;
727 va_start(ap, fmt);
728 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
729 va_end(ap);
731 if (address == NULL) {
732 isc_log_iwrite(isc_lctx, category, module, level,
733 msgcat, msgset, message,
734 "socket %p: %s", sock, msgbuf);
735 } else {
736 isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
737 isc_log_iwrite(isc_lctx, category, module, level,
738 msgcat, msgset, message,
739 "socket %p %s: %s", sock, peerbuf, msgbuf);
743 #if defined(_AIX) && defined(ISC_NET_BSD44MSGHDR) && \
744 defined(USE_CMSG) && defined(IPV6_RECVPKTINFO)
746 * AIX has a kernel bug where IPV6_RECVPKTINFO gets cleared by
747 * setting IPV6_V6ONLY.
749 static void
750 FIX_IPV6_RECVPKTINFO(isc__socket_t *sock)
752 char strbuf[ISC_STRERRORSIZE];
753 int on = 1;
755 if (sock->pf != AF_INET6 || sock->type != isc_sockettype_udp)
756 return;
758 if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
759 (void *)&on, sizeof(on)) < 0) {
761 UNEXPECTED_ERROR(__FILE__, __LINE__,
762 "setsockopt(%d, IPV6_RECVPKTINFO) "
763 "%s: %s", sock->fd,
764 isc_msgcat_get(isc_msgcat,
765 ISC_MSGSET_GENERAL,
766 ISC_MSG_FAILED,
767 "failed"),
768 strbuf);
771 #else
772 #define FIX_IPV6_RECVPKTINFO(sock) (void)0
773 #endif
776 * Increment socket-related statistics counters.
778 static inline void
779 inc_stats(isc_stats_t *stats, isc_statscounter_t counterid) {
780 REQUIRE(counterid != -1);
782 if (stats != NULL)
783 isc_stats_increment(stats, counterid);
786 static inline isc_result_t
787 watch_fd(isc__socketmgr_t *manager, int fd, int msg) {
788 isc_result_t result = ISC_R_SUCCESS;
790 #ifdef USE_KQUEUE
791 struct kevent evchange;
793 memset(&evchange, 0, sizeof(evchange));
794 if (msg == SELECT_POKE_READ)
795 evchange.filter = EVFILT_READ;
796 else
797 evchange.filter = EVFILT_WRITE;
798 evchange.flags = EV_ADD;
799 evchange.ident = fd;
800 if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
801 result = isc__errno2result(errno);
803 return (result);
804 #elif defined(USE_EPOLL)
805 struct epoll_event event;
807 if (msg == SELECT_POKE_READ)
808 event.events = EPOLLIN;
809 else
810 event.events = EPOLLOUT;
811 event.data.fd = fd;
812 if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1 &&
813 errno != EEXIST) {
814 result = isc__errno2result(errno);
817 return (result);
818 #elif defined(USE_DEVPOLL)
819 struct pollfd pfd;
820 int lockid = FDLOCK_ID(fd);
822 memset(&pfd, 0, sizeof(pfd));
823 if (msg == SELECT_POKE_READ)
824 pfd.events = POLLIN;
825 else
826 pfd.events = POLLOUT;
827 pfd.fd = fd;
828 pfd.revents = 0;
829 LOCK(&manager->fdlock[lockid]);
830 if (write(manager->devpoll_fd, &pfd, sizeof(pfd)) == -1)
831 result = isc__errno2result(errno);
832 else {
833 if (msg == SELECT_POKE_READ)
834 manager->fdpollinfo[fd].want_read = 1;
835 else
836 manager->fdpollinfo[fd].want_write = 1;
838 UNLOCK(&manager->fdlock[lockid]);
840 return (result);
841 #elif defined(USE_SELECT)
842 LOCK(&manager->lock);
843 if (msg == SELECT_POKE_READ)
844 FD_SET(fd, manager->read_fds);
845 if (msg == SELECT_POKE_WRITE)
846 FD_SET(fd, manager->write_fds);
847 UNLOCK(&manager->lock);
849 return (result);
850 #endif
853 static inline isc_result_t
854 unwatch_fd(isc__socketmgr_t *manager, int fd, int msg) {
855 isc_result_t result = ISC_R_SUCCESS;
857 #ifdef USE_KQUEUE
858 struct kevent evchange;
860 memset(&evchange, 0, sizeof(evchange));
861 if (msg == SELECT_POKE_READ)
862 evchange.filter = EVFILT_READ;
863 else
864 evchange.filter = EVFILT_WRITE;
865 evchange.flags = EV_DELETE;
866 evchange.ident = fd;
867 if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
868 result = isc__errno2result(errno);
870 return (result);
871 #elif defined(USE_EPOLL)
872 struct epoll_event event;
874 if (msg == SELECT_POKE_READ)
875 event.events = EPOLLIN;
876 else
877 event.events = EPOLLOUT;
878 event.data.fd = fd;
879 if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_DEL, fd, &event) == -1 &&
880 errno != ENOENT) {
881 char strbuf[ISC_STRERRORSIZE];
882 isc__strerror(errno, strbuf, sizeof(strbuf));
883 UNEXPECTED_ERROR(__FILE__, __LINE__,
884 "epoll_ctl(DEL), %d: %s", fd, strbuf);
885 result = ISC_R_UNEXPECTED;
887 return (result);
888 #elif defined(USE_DEVPOLL)
889 struct pollfd pfds[2];
890 size_t writelen = sizeof(pfds[0]);
891 int lockid = FDLOCK_ID(fd);
893 memset(pfds, 0, sizeof(pfds));
894 pfds[0].events = POLLREMOVE;
895 pfds[0].fd = fd;
898 * Canceling read or write polling via /dev/poll is tricky. Since it
899 * only provides a way of canceling per FD, we may need to re-poll the
900 * socket for the other operation.
902 LOCK(&manager->fdlock[lockid]);
903 if (msg == SELECT_POKE_READ &&
904 manager->fdpollinfo[fd].want_write == 1) {
905 pfds[1].events = POLLOUT;
906 pfds[1].fd = fd;
907 writelen += sizeof(pfds[1]);
909 if (msg == SELECT_POKE_WRITE &&
910 manager->fdpollinfo[fd].want_read == 1) {
911 pfds[1].events = POLLIN;
912 pfds[1].fd = fd;
913 writelen += sizeof(pfds[1]);
916 if (write(manager->devpoll_fd, pfds, writelen) == -1)
917 result = isc__errno2result(errno);
918 else {
919 if (msg == SELECT_POKE_READ)
920 manager->fdpollinfo[fd].want_read = 0;
921 else
922 manager->fdpollinfo[fd].want_write = 0;
924 UNLOCK(&manager->fdlock[lockid]);
926 return (result);
927 #elif defined(USE_SELECT)
928 LOCK(&manager->lock);
929 if (msg == SELECT_POKE_READ)
930 FD_CLR(fd, manager->read_fds);
931 else if (msg == SELECT_POKE_WRITE)
932 FD_CLR(fd, manager->write_fds);
933 UNLOCK(&manager->lock);
935 return (result);
936 #endif
939 static void
940 wakeup_socket(isc__socketmgr_t *manager, int fd, int msg) {
941 isc_result_t result;
942 int lockid = FDLOCK_ID(fd);
945 * This is a wakeup on a socket. If the socket is not in the
946 * process of being closed, start watching it for either reads
947 * or writes.
950 INSIST(fd >= 0 && fd < (int)manager->maxsocks);
952 if (msg == SELECT_POKE_CLOSE) {
953 /* No one should be updating fdstate, so no need to lock it */
954 INSIST(manager->fdstate[fd] == CLOSE_PENDING);
955 manager->fdstate[fd] = CLOSED;
956 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
957 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
958 (void)close(fd);
959 return;
962 LOCK(&manager->fdlock[lockid]);
963 if (manager->fdstate[fd] == CLOSE_PENDING) {
964 UNLOCK(&manager->fdlock[lockid]);
967 * We accept (and ignore) any error from unwatch_fd() as we are
968 * closing the socket, hoping it doesn't leave dangling state in
969 * the kernel.
970 * Note that unwatch_fd() must be called after releasing the
971 * fdlock; otherwise it could cause deadlock due to a lock order
972 * reversal.
974 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
975 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
976 return;
978 if (manager->fdstate[fd] != MANAGED) {
979 UNLOCK(&manager->fdlock[lockid]);
980 return;
982 UNLOCK(&manager->fdlock[lockid]);
985 * Set requested bit.
987 result = watch_fd(manager, fd, msg);
988 if (result != ISC_R_SUCCESS) {
990 * XXXJT: what should we do? Ignoring the failure of watching
991 * a socket will make the application dysfunctional, but there
992 * seems to be no reasonable recovery process.
994 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
995 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
996 "failed to start watching FD (%d): %s",
997 fd, isc_result_totext(result));
1001 #ifdef USE_WATCHER_THREAD
1003 * Poke the select loop when there is something for us to do.
1004 * The write is required (by POSIX) to complete. That is, we
1005 * will not get partial writes.
1007 static void
1008 select_poke(isc__socketmgr_t *mgr, int fd, int msg) {
1009 int cc;
1010 int buf[2];
1011 char strbuf[ISC_STRERRORSIZE];
1013 buf[0] = fd;
1014 buf[1] = msg;
1016 do {
1017 cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
1018 #ifdef ENOSR
1020 * Treat ENOSR as EAGAIN but loop slowly as it is
1021 * unlikely to clear fast.
1023 if (cc < 0 && errno == ENOSR) {
1024 sleep(1);
1025 errno = EAGAIN;
1027 #endif
1028 } while (cc < 0 && SOFT_ERROR(errno));
1030 if (cc < 0) {
1031 isc__strerror(errno, strbuf, sizeof(strbuf));
1032 FATAL_ERROR(__FILE__, __LINE__,
1033 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
1034 ISC_MSG_WRITEFAILED,
1035 "write() failed "
1036 "during watcher poke: %s"),
1037 strbuf);
1040 INSIST(cc == sizeof(buf));
1044 * Read a message on the internal fd.
1046 static void
1047 select_readmsg(isc__socketmgr_t *mgr, int *fd, int *msg) {
1048 int buf[2];
1049 int cc;
1050 char strbuf[ISC_STRERRORSIZE];
1052 cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
1053 if (cc < 0) {
1054 *msg = SELECT_POKE_NOTHING;
1055 *fd = -1; /* Silence compiler. */
1056 if (SOFT_ERROR(errno))
1057 return;
1059 isc__strerror(errno, strbuf, sizeof(strbuf));
1060 FATAL_ERROR(__FILE__, __LINE__,
1061 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
1062 ISC_MSG_READFAILED,
1063 "read() failed "
1064 "during watcher poke: %s"),
1065 strbuf);
1067 return;
1069 INSIST(cc == sizeof(buf));
1071 *fd = buf[0];
1072 *msg = buf[1];
1074 #else /* USE_WATCHER_THREAD */
1076 * Update the state of the socketmgr when something changes.
1078 static void
1079 select_poke(isc__socketmgr_t *manager, int fd, int msg) {
1080 if (msg == SELECT_POKE_SHUTDOWN)
1081 return;
1082 else if (fd >= 0)
1083 wakeup_socket(manager, fd, msg);
1084 return;
1086 #endif /* USE_WATCHER_THREAD */
1089 * Make a fd non-blocking.
1091 static isc_result_t
1092 make_nonblock(int fd) {
1093 int ret;
1094 int flags;
1095 char strbuf[ISC_STRERRORSIZE];
1096 #ifdef USE_FIONBIO_IOCTL
1097 int on = 1;
1099 ret = ioctl(fd, FIONBIO, (char *)&on);
1100 #else
1101 flags = fcntl(fd, F_GETFL, 0);
1102 flags |= PORT_NONBLOCK;
1103 ret = fcntl(fd, F_SETFL, flags);
1104 #endif
1106 if (ret == -1) {
1107 isc__strerror(errno, strbuf, sizeof(strbuf));
1108 UNEXPECTED_ERROR(__FILE__, __LINE__,
1109 #ifdef USE_FIONBIO_IOCTL
1110 "ioctl(%d, FIONBIO, &on): %s", fd,
1111 #else
1112 "fcntl(%d, F_SETFL, %d): %s", fd, flags,
1113 #endif
1114 strbuf);
1116 return (ISC_R_UNEXPECTED);
1119 return (ISC_R_SUCCESS);
1122 #ifdef USE_CMSG
1124 * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
1125 * In order to ensure as much portability as possible, we provide wrapper
1126 * functions of these macros.
1127 * Note that cmsg_space() could run slow on OSes that do not have
1128 * CMSG_SPACE.
1130 static inline ISC_SOCKADDR_LEN_T
1131 cmsg_len(ISC_SOCKADDR_LEN_T len) {
1132 #ifdef CMSG_LEN
1133 return (CMSG_LEN(len));
1134 #else
1135 ISC_SOCKADDR_LEN_T hdrlen;
1138 * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
1139 * is correct.
1141 hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
1142 return (hdrlen + len);
1143 #endif
1146 static inline ISC_SOCKADDR_LEN_T
1147 cmsg_space(ISC_SOCKADDR_LEN_T len) {
1148 #ifdef CMSG_SPACE
1149 return (CMSG_SPACE(len));
1150 #else
1151 struct msghdr msg;
1152 struct cmsghdr *cmsgp;
1154 * XXX: The buffer length is an ad-hoc value, but should be enough
1155 * in a practical sense.
1157 char dummybuf[sizeof(struct cmsghdr) + 1024];
1159 memset(&msg, 0, sizeof(msg));
1160 msg.msg_control = dummybuf;
1161 msg.msg_controllen = sizeof(dummybuf);
1163 cmsgp = (struct cmsghdr *)dummybuf;
1164 cmsgp->cmsg_len = cmsg_len(len);
1166 cmsgp = CMSG_NXTHDR(&msg, cmsgp);
1167 if (cmsgp != NULL)
1168 return ((char *)cmsgp - (char *)msg.msg_control);
1169 else
1170 return (0);
1171 #endif
1173 #endif /* USE_CMSG */
1176 * Process control messages received on a socket.
1178 static void
1179 process_cmsg(isc__socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
1180 #ifdef USE_CMSG
1181 struct cmsghdr *cmsgp;
1182 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1183 struct in6_pktinfo *pktinfop;
1184 #endif
1185 #ifdef SO_TIMESTAMP
1186 struct timeval *timevalp;
1187 #endif
1188 #endif
1191 * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
1192 * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
1193 * They are all here, outside of the CPP tests, because it is
1194 * more consistent with the usual ISC coding style.
1196 UNUSED(sock);
1197 UNUSED(msg);
1198 UNUSED(dev);
1200 #ifdef ISC_NET_BSD44MSGHDR
1202 #ifdef MSG_TRUNC
1203 if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
1204 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
1205 #endif
1207 #ifdef MSG_CTRUNC
1208 if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
1209 dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
1210 #endif
1212 #ifndef USE_CMSG
1213 return;
1214 #else
1215 if (msg->msg_controllen == 0U || msg->msg_control == NULL)
1216 return;
1218 #ifdef SO_TIMESTAMP
1219 timevalp = NULL;
1220 #endif
1221 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1222 pktinfop = NULL;
1223 #endif
1225 cmsgp = CMSG_FIRSTHDR(msg);
1226 while (cmsgp != NULL) {
1227 socket_log(sock, NULL, TRACE,
1228 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
1229 "processing cmsg %p", cmsgp);
1231 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1232 if (cmsgp->cmsg_level == IPPROTO_IPV6
1233 && cmsgp->cmsg_type == IPV6_PKTINFO) {
1235 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
1236 memcpy(&dev->pktinfo, pktinfop,
1237 sizeof(struct in6_pktinfo));
1238 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
1239 socket_log(sock, NULL, TRACE,
1240 isc_msgcat, ISC_MSGSET_SOCKET,
1241 ISC_MSG_IFRECEIVED,
1242 "interface received on ifindex %u",
1243 dev->pktinfo.ipi6_ifindex);
1244 if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
1245 dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
1246 goto next;
1248 #endif
1250 #ifdef SO_TIMESTAMP
1251 if (cmsgp->cmsg_level == SOL_SOCKET
1252 && cmsgp->cmsg_type == SCM_TIMESTAMP) {
1253 timevalp = (struct timeval *)CMSG_DATA(cmsgp);
1254 dev->timestamp.seconds = timevalp->tv_sec;
1255 dev->timestamp.nanoseconds = timevalp->tv_usec * 1000;
1256 dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
1257 goto next;
1259 #endif
1261 next:
1262 cmsgp = CMSG_NXTHDR(msg, cmsgp);
1264 #endif /* USE_CMSG */
1266 #endif /* ISC_NET_BSD44MSGHDR */
1270 * Construct an iov array and attach it to the msghdr passed in. This is
1271 * the SEND constructor, which will use the used region of the buffer
1272 * (if using a buffer list) or will use the internal region (if a single
1273 * buffer I/O is requested).
1275 * Nothing can be NULL, and the done event must list at least one buffer
1276 * on the buffer linked list for this function to be meaningful.
1278 * If write_countp != NULL, *write_countp will hold the number of bytes
1279 * this transaction can send.
1281 static void
1282 build_msghdr_send(isc__socket_t *sock, isc_socketevent_t *dev,
1283 struct msghdr *msg, struct iovec *iov, size_t *write_countp)
1285 unsigned int iovcount;
1286 isc_buffer_t *buffer;
1287 isc_region_t used;
1288 size_t write_count;
1289 size_t skip_count;
1291 memset(msg, 0, sizeof(*msg));
1293 if (!sock->connected) {
1294 msg->msg_name = (void *)&dev->address.type.sa;
1295 msg->msg_namelen = dev->address.length;
1296 } else {
1297 msg->msg_name = NULL;
1298 msg->msg_namelen = 0;
1301 buffer = ISC_LIST_HEAD(dev->bufferlist);
1302 write_count = 0;
1303 iovcount = 0;
1306 * Single buffer I/O? Skip what we've done so far in this region.
1308 if (buffer == NULL) {
1309 write_count = dev->region.length - dev->n;
1310 iov[0].iov_base = (void *)(dev->region.base + dev->n);
1311 iov[0].iov_len = write_count;
1312 iovcount = 1;
1314 goto config;
1318 * Multibuffer I/O.
1319 * Skip the data in the buffer list that we have already written.
1321 skip_count = dev->n;
1322 while (buffer != NULL) {
1323 REQUIRE(ISC_BUFFER_VALID(buffer));
1324 if (skip_count < isc_buffer_usedlength(buffer))
1325 break;
1326 skip_count -= isc_buffer_usedlength(buffer);
1327 buffer = ISC_LIST_NEXT(buffer, link);
1330 while (buffer != NULL) {
1331 INSIST(iovcount < MAXSCATTERGATHER_SEND);
1333 isc_buffer_usedregion(buffer, &used);
1335 if (used.length > 0) {
1336 iov[iovcount].iov_base = (void *)(used.base
1337 + skip_count);
1338 iov[iovcount].iov_len = used.length - skip_count;
1339 write_count += (used.length - skip_count);
1340 skip_count = 0;
1341 iovcount++;
1343 buffer = ISC_LIST_NEXT(buffer, link);
1346 INSIST(skip_count == 0U);
1348 config:
1349 msg->msg_iov = iov;
1350 msg->msg_iovlen = iovcount;
1352 #ifdef ISC_NET_BSD44MSGHDR
1353 msg->msg_control = NULL;
1354 msg->msg_controllen = 0;
1355 msg->msg_flags = 0;
1356 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1357 if ((sock->type == isc_sockettype_udp)
1358 && ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
1359 struct cmsghdr *cmsgp;
1360 struct in6_pktinfo *pktinfop;
1362 socket_log(sock, NULL, TRACE,
1363 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
1364 "sendto pktinfo data, ifindex %u",
1365 dev->pktinfo.ipi6_ifindex);
1367 msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
1368 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
1369 msg->msg_control = (void *)sock->sendcmsgbuf;
1371 cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
1372 cmsgp->cmsg_level = IPPROTO_IPV6;
1373 cmsgp->cmsg_type = IPV6_PKTINFO;
1374 cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
1375 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
1376 memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
1378 #endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
1379 #else /* ISC_NET_BSD44MSGHDR */
1380 msg->msg_accrights = NULL;
1381 msg->msg_accrightslen = 0;
1382 #endif /* ISC_NET_BSD44MSGHDR */
1384 if (write_countp != NULL)
1385 *write_countp = write_count;
1389 * Construct an iov array and attach it to the msghdr passed in. This is
1390 * the RECV constructor, which will use the available region of the buffer
1391 * (if using a buffer list) or will use the internal region (if a single
1392 * buffer I/O is requested).
1394 * Nothing can be NULL, and the done event must list at least one buffer
1395 * on the buffer linked list for this function to be meaningful.
1397 * If read_countp != NULL, *read_countp will hold the number of bytes
1398 * this transaction can receive.
1400 static void
1401 build_msghdr_recv(isc__socket_t *sock, isc_socketevent_t *dev,
1402 struct msghdr *msg, struct iovec *iov, size_t *read_countp)
1404 unsigned int iovcount;
1405 isc_buffer_t *buffer;
1406 isc_region_t available;
1407 size_t read_count;
1409 memset(msg, 0, sizeof(struct msghdr));
1411 if (sock->type == isc_sockettype_udp) {
1412 memset(&dev->address, 0, sizeof(dev->address));
1413 #ifdef BROKEN_RECVMSG
1414 if (sock->pf == AF_INET) {
1415 msg->msg_name = (void *)&dev->address.type.sin;
1416 msg->msg_namelen = sizeof(dev->address.type.sin6);
1417 } else if (sock->pf == AF_INET6) {
1418 msg->msg_name = (void *)&dev->address.type.sin6;
1419 msg->msg_namelen = sizeof(dev->address.type.sin6);
1420 #ifdef ISC_PLATFORM_HAVESYSUNH
1421 } else if (sock->pf == AF_UNIX) {
1422 msg->msg_name = (void *)&dev->address.type.sunix;
1423 msg->msg_namelen = sizeof(dev->address.type.sunix);
1424 #endif
1425 } else {
1426 msg->msg_name = (void *)&dev->address.type.sa;
1427 msg->msg_namelen = sizeof(dev->address.type);
1429 #else
1430 msg->msg_name = (void *)&dev->address.type.sa;
1431 msg->msg_namelen = sizeof(dev->address.type);
1432 #endif
1433 #ifdef ISC_NET_RECVOVERFLOW
1434 /* If needed, steal one iovec for overflow detection. */
1435 maxiov--;
1436 #endif
1437 } else { /* TCP */
1438 msg->msg_name = NULL;
1439 msg->msg_namelen = 0;
1440 dev->address = sock->peer_address;
1443 buffer = ISC_LIST_HEAD(dev->bufferlist);
1444 read_count = 0;
1447 * Single buffer I/O? Skip what we've done so far in this region.
1449 if (buffer == NULL) {
1450 read_count = dev->region.length - dev->n;
1451 iov[0].iov_base = (void *)(dev->region.base + dev->n);
1452 iov[0].iov_len = read_count;
1453 iovcount = 1;
1455 goto config;
1459 * Multibuffer I/O.
1460 * Skip empty buffers.
1462 while (buffer != NULL) {
1463 REQUIRE(ISC_BUFFER_VALID(buffer));
1464 if (isc_buffer_availablelength(buffer) != 0)
1465 break;
1466 buffer = ISC_LIST_NEXT(buffer, link);
1469 iovcount = 0;
1470 while (buffer != NULL) {
1471 INSIST(iovcount < MAXSCATTERGATHER_RECV);
1473 isc_buffer_availableregion(buffer, &available);
1475 if (available.length > 0) {
1476 iov[iovcount].iov_base = (void *)(available.base);
1477 iov[iovcount].iov_len = available.length;
1478 read_count += available.length;
1479 iovcount++;
1481 buffer = ISC_LIST_NEXT(buffer, link);
1484 config:
1487 * If needed, set up to receive that one extra byte. Note that
1488 * we know there is at least one iov left, since we stole it
1489 * at the top of this function.
1491 #ifdef ISC_NET_RECVOVERFLOW
1492 if (sock->type == isc_sockettype_udp) {
1493 iov[iovcount].iov_base = (void *)(&sock->overflow);
1494 iov[iovcount].iov_len = 1;
1495 iovcount++;
1497 #endif
1499 msg->msg_iov = iov;
1500 msg->msg_iovlen = iovcount;
1502 #ifdef ISC_NET_BSD44MSGHDR
1503 msg->msg_control = NULL;
1504 msg->msg_controllen = 0;
1505 msg->msg_flags = 0;
1506 #if defined(USE_CMSG)
1507 if (sock->type == isc_sockettype_udp) {
1508 msg->msg_control = sock->recvcmsgbuf;
1509 msg->msg_controllen = sock->recvcmsgbuflen;
1511 #endif /* USE_CMSG */
1512 #else /* ISC_NET_BSD44MSGHDR */
1513 msg->msg_accrights = NULL;
1514 msg->msg_accrightslen = 0;
1515 #endif /* ISC_NET_BSD44MSGHDR */
1517 if (read_countp != NULL)
1518 *read_countp = read_count;
1521 static void
1522 set_dev_address(isc_sockaddr_t *address, isc__socket_t *sock,
1523 isc_socketevent_t *dev)
1525 if (sock->type == isc_sockettype_udp) {
1526 if (address != NULL)
1527 dev->address = *address;
1528 else
1529 dev->address = sock->peer_address;
1530 } else if (sock->type == isc_sockettype_tcp) {
1531 INSIST(address == NULL);
1532 dev->address = sock->peer_address;
1536 static void
1537 destroy_socketevent(isc_event_t *event) {
1538 isc_socketevent_t *ev = (isc_socketevent_t *)event;
1540 INSIST(ISC_LIST_EMPTY(ev->bufferlist));
1542 (ev->destroy)(event);
1545 static isc_socketevent_t *
1546 allocate_socketevent(isc__socket_t *sock, isc_eventtype_t eventtype,
1547 isc_taskaction_t action, const void *arg)
1549 isc_socketevent_t *ev;
1551 ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
1552 sock, eventtype,
1553 action, arg,
1554 sizeof(*ev));
1556 if (ev == NULL)
1557 return (NULL);
1559 ev->result = ISC_R_UNEXPECTED;
1560 ISC_LINK_INIT(ev, ev_link);
1561 ISC_LIST_INIT(ev->bufferlist);
1562 ev->region.base = NULL;
1563 ev->n = 0;
1564 ev->offset = 0;
1565 ev->attributes = 0;
1566 ev->destroy = ev->ev_destroy;
1567 ev->ev_destroy = destroy_socketevent;
1569 return (ev);
1572 #if defined(ISC_SOCKET_DEBUG)
1573 static void
1574 dump_msg(struct msghdr *msg) {
1575 unsigned int i;
1577 printf("MSGHDR %p\n", msg);
1578 printf("\tname %p, namelen %ld\n", msg->msg_name,
1579 (long) msg->msg_namelen);
1580 printf("\tiov %p, iovlen %ld\n", msg->msg_iov,
1581 (long) msg->msg_iovlen);
1582 for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
1583 printf("\t\t%d\tbase %p, len %ld\n", i,
1584 msg->msg_iov[i].iov_base,
1585 (long) msg->msg_iov[i].iov_len);
1586 #ifdef ISC_NET_BSD44MSGHDR
1587 printf("\tcontrol %p, controllen %ld\n", msg->msg_control,
1588 (long) msg->msg_controllen);
1589 #endif
1591 #endif
1593 #define DOIO_SUCCESS 0 /* i/o ok, event sent */
1594 #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
1595 #define DOIO_HARD 2 /* i/o error, event sent */
1596 #define DOIO_EOF 3 /* EOF, no event sent */
1598 static int
1599 doio_recv(isc__socket_t *sock, isc_socketevent_t *dev) {
1600 int cc;
1601 struct iovec iov[MAXSCATTERGATHER_RECV];
1602 size_t read_count;
1603 size_t actual_count;
1604 struct msghdr msghdr;
1605 isc_buffer_t *buffer;
1606 int recv_errno;
1607 char strbuf[ISC_STRERRORSIZE];
1609 build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
1611 #if defined(ISC_SOCKET_DEBUG)
1612 dump_msg(&msghdr);
1613 #endif
1615 cc = recvmsg(sock->fd, &msghdr, 0);
1616 recv_errno = errno;
1618 #if defined(ISC_SOCKET_DEBUG)
1619 dump_msg(&msghdr);
1620 #endif
1622 if (cc < 0) {
1623 if (SOFT_ERROR(recv_errno))
1624 return (DOIO_SOFT);
1626 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1627 isc__strerror(recv_errno, strbuf, sizeof(strbuf));
1628 socket_log(sock, NULL, IOEVENT,
1629 isc_msgcat, ISC_MSGSET_SOCKET,
1630 ISC_MSG_DOIORECV,
1631 "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
1632 sock->fd, cc, recv_errno, strbuf);
1635 #define SOFT_OR_HARD(_system, _isc) \
1636 if (recv_errno == _system) { \
1637 if (sock->connected) { \
1638 dev->result = _isc; \
1639 inc_stats(sock->manager->stats, \
1640 sock->statsindex[STATID_RECVFAIL]); \
1641 return (DOIO_HARD); \
1643 return (DOIO_SOFT); \
1645 #define ALWAYS_HARD(_system, _isc) \
1646 if (recv_errno == _system) { \
1647 dev->result = _isc; \
1648 inc_stats(sock->manager->stats, \
1649 sock->statsindex[STATID_RECVFAIL]); \
1650 return (DOIO_HARD); \
1653 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1654 SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1655 SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1656 SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
1657 /* HPUX 11.11 can return EADDRNOTAVAIL. */
1658 SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1659 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1661 * HPUX returns EPROTO and EINVAL on receiving some ICMP/ICMPv6
1662 * errors.
1664 #ifdef EPROTO
1665 SOFT_OR_HARD(EPROTO, ISC_R_HOSTUNREACH);
1666 #endif
1667 SOFT_OR_HARD(EINVAL, ISC_R_HOSTUNREACH);
1669 #undef SOFT_OR_HARD
1670 #undef ALWAYS_HARD
1672 dev->result = isc__errno2result(recv_errno);
1673 inc_stats(sock->manager->stats,
1674 sock->statsindex[STATID_RECVFAIL]);
1675 return (DOIO_HARD);
1679 * On TCP, zero length reads indicate EOF, while on
1680 * UDP, zero length reads are perfectly valid, although
1681 * strange.
1683 if ((sock->type == isc_sockettype_tcp) && (cc == 0))
1684 return (DOIO_EOF);
1686 if (sock->type == isc_sockettype_udp) {
1687 dev->address.length = msghdr.msg_namelen;
1688 if (isc_sockaddr_getport(&dev->address) == 0) {
1689 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1690 socket_log(sock, &dev->address, IOEVENT,
1691 isc_msgcat, ISC_MSGSET_SOCKET,
1692 ISC_MSG_ZEROPORT,
1693 "dropping source port zero packet");
1695 return (DOIO_SOFT);
1698 * Simulate a firewall blocking UDP responses bigger than
1699 * 512 bytes.
1701 if (sock->manager->maxudp != 0 && cc > sock->manager->maxudp)
1702 return (DOIO_SOFT);
1705 socket_log(sock, &dev->address, IOEVENT,
1706 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
1707 "packet received correctly");
1710 * Overflow bit detection. If we received MORE bytes than we should,
1711 * this indicates an overflow situation. Set the flag in the
1712 * dev entry and adjust how much we read by one.
1714 #ifdef ISC_NET_RECVOVERFLOW
1715 if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
1716 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
1717 cc--;
1719 #endif
1722 * If there are control messages attached, run through them and pull
1723 * out the interesting bits.
1725 if (sock->type == isc_sockettype_udp)
1726 process_cmsg(sock, &msghdr, dev);
1729 * update the buffers (if any) and the i/o count
1731 dev->n += cc;
1732 actual_count = cc;
1733 buffer = ISC_LIST_HEAD(dev->bufferlist);
1734 while (buffer != NULL && actual_count > 0U) {
1735 REQUIRE(ISC_BUFFER_VALID(buffer));
1736 if (isc_buffer_availablelength(buffer) <= actual_count) {
1737 actual_count -= isc_buffer_availablelength(buffer);
1738 isc_buffer_add(buffer,
1739 isc_buffer_availablelength(buffer));
1740 } else {
1741 isc_buffer_add(buffer, actual_count);
1742 actual_count = 0;
1743 break;
1745 buffer = ISC_LIST_NEXT(buffer, link);
1746 if (buffer == NULL) {
1747 INSIST(actual_count == 0U);
1752 * If we read less than we expected, update counters,
1753 * and let the upper layer poke the descriptor.
1755 if (((size_t)cc != read_count) && (dev->n < dev->minimum))
1756 return (DOIO_SOFT);
1759 * Full reads are posted, or partials if partials are ok.
1761 dev->result = ISC_R_SUCCESS;
1762 return (DOIO_SUCCESS);
1766 * Returns:
1767 * DOIO_SUCCESS The operation succeeded. dev->result contains
1768 * ISC_R_SUCCESS.
1770 * DOIO_HARD A hard or unexpected I/O error was encountered.
1771 * dev->result contains the appropriate error.
1773 * DOIO_SOFT A soft I/O error was encountered. No senddone
1774 * event was sent. The operation should be retried.
1776 * No other return values are possible.
1778 static int
1779 doio_send(isc__socket_t *sock, isc_socketevent_t *dev) {
1780 int cc;
1781 struct iovec iov[MAXSCATTERGATHER_SEND];
1782 size_t write_count;
1783 struct msghdr msghdr;
1784 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1785 int attempts = 0;
1786 int send_errno;
1787 char strbuf[ISC_STRERRORSIZE];
1789 build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
1791 resend:
1792 cc = sendmsg(sock->fd, &msghdr, 0);
1793 send_errno = errno;
1796 * Check for error or block condition.
1798 if (cc < 0) {
1799 if (send_errno == EINTR && ++attempts < NRETRIES)
1800 goto resend;
1802 if (SOFT_ERROR(send_errno))
1803 return (DOIO_SOFT);
1805 #define SOFT_OR_HARD(_system, _isc) \
1806 if (send_errno == _system) { \
1807 if (sock->connected) { \
1808 dev->result = _isc; \
1809 inc_stats(sock->manager->stats, \
1810 sock->statsindex[STATID_SENDFAIL]); \
1811 return (DOIO_HARD); \
1813 return (DOIO_SOFT); \
1815 #define ALWAYS_HARD(_system, _isc) \
1816 if (send_errno == _system) { \
1817 dev->result = _isc; \
1818 inc_stats(sock->manager->stats, \
1819 sock->statsindex[STATID_SENDFAIL]); \
1820 return (DOIO_HARD); \
1823 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1824 ALWAYS_HARD(EACCES, ISC_R_NOPERM);
1825 ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
1826 ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1827 ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1828 #ifdef EHOSTDOWN
1829 ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
1830 #endif
1831 ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1832 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1833 ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
1834 ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
1835 ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
1837 #undef SOFT_OR_HARD
1838 #undef ALWAYS_HARD
1841 * The other error types depend on whether or not the
1842 * socket is UDP or TCP. If it is UDP, some errors
1843 * that we expect to be fatal under TCP are merely
1844 * annoying, and are really soft errors.
1846 * However, these soft errors are still returned as
1847 * a status.
1849 isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
1850 isc__strerror(send_errno, strbuf, sizeof(strbuf));
1851 UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
1852 addrbuf, strbuf);
1853 dev->result = isc__errno2result(send_errno);
1854 inc_stats(sock->manager->stats,
1855 sock->statsindex[STATID_SENDFAIL]);
1856 return (DOIO_HARD);
1859 if (cc == 0) {
1860 inc_stats(sock->manager->stats,
1861 sock->statsindex[STATID_SENDFAIL]);
1862 UNEXPECTED_ERROR(__FILE__, __LINE__,
1863 "doio_send: send() %s 0",
1864 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1865 ISC_MSG_RETURNED, "returned"));
1869 * If we write less than we expected, update counters, poke.
1871 dev->n += cc;
1872 if ((size_t)cc != write_count)
1873 return (DOIO_SOFT);
1876 * Exactly what we wanted to write. We're done with this
1877 * entry. Post its completion event.
1879 dev->result = ISC_R_SUCCESS;
1880 return (DOIO_SUCCESS);
1884 * Kill.
1886 * Caller must ensure that the socket is not locked and no external
1887 * references exist.
1889 static void
1890 closesocket(isc__socketmgr_t *manager, isc__socket_t *sock, int fd) {
1891 isc_sockettype_t type = sock->type;
1892 int lockid = FDLOCK_ID(fd);
1895 * No one has this socket open, so the watcher doesn't have to be
1896 * poked, and the socket doesn't have to be locked.
1898 LOCK(&manager->fdlock[lockid]);
1899 manager->fds[fd] = NULL;
1900 if (type == isc_sockettype_fdwatch)
1901 manager->fdstate[fd] = CLOSED;
1902 else
1903 manager->fdstate[fd] = CLOSE_PENDING;
1904 UNLOCK(&manager->fdlock[lockid]);
1905 if (type == isc_sockettype_fdwatch) {
1907 * The caller may close the socket once this function returns,
1908 * and `fd' may be reassigned for a new socket. So we do
1909 * unwatch_fd() here, rather than defer it via select_poke().
1910 * Note: this may complicate data protection among threads and
1911 * may reduce performance due to additional locks. One way to
1912 * solve this would be to dup() the watched descriptor, but we
1913 * take a simpler approach at this moment.
1915 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
1916 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
1917 } else
1918 select_poke(manager, fd, SELECT_POKE_CLOSE);
1920 inc_stats(manager->stats, sock->statsindex[STATID_CLOSE]);
1923 * update manager->maxfd here (XXX: this should be implemented more
1924 * efficiently)
1926 #ifdef USE_SELECT
1927 LOCK(&manager->lock);
1928 if (manager->maxfd == fd) {
1929 int i;
1931 manager->maxfd = 0;
1932 for (i = fd - 1; i >= 0; i--) {
1933 lockid = FDLOCK_ID(i);
1935 LOCK(&manager->fdlock[lockid]);
1936 if (manager->fdstate[i] == MANAGED) {
1937 manager->maxfd = i;
1938 UNLOCK(&manager->fdlock[lockid]);
1939 break;
1941 UNLOCK(&manager->fdlock[lockid]);
1943 #ifdef ISC_PLATFORM_USETHREADS
1944 if (manager->maxfd < manager->pipe_fds[0])
1945 manager->maxfd = manager->pipe_fds[0];
1946 #endif
1948 UNLOCK(&manager->lock);
1949 #endif /* USE_SELECT */
1952 static void
1953 destroy(isc__socket_t **sockp) {
1954 int fd;
1955 isc__socket_t *sock = *sockp;
1956 isc__socketmgr_t *manager = sock->manager;
1958 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1959 ISC_MSG_DESTROYING, "destroying");
1961 INSIST(ISC_LIST_EMPTY(sock->accept_list));
1962 INSIST(ISC_LIST_EMPTY(sock->recv_list));
1963 INSIST(ISC_LIST_EMPTY(sock->send_list));
1964 INSIST(sock->connect_ev == NULL);
1965 REQUIRE(sock->fd == -1 || sock->fd < (int)manager->maxsocks);
1967 if (sock->fd >= 0) {
1968 fd = sock->fd;
1969 sock->fd = -1;
1970 closesocket(manager, sock, fd);
1973 LOCK(&manager->lock);
1975 ISC_LIST_UNLINK(manager->socklist, sock, link);
1977 #ifdef USE_WATCHER_THREAD
1978 if (ISC_LIST_EMPTY(manager->socklist))
1979 SIGNAL(&manager->shutdown_ok);
1980 #endif /* USE_WATCHER_THREAD */
1982 UNLOCK(&manager->lock);
1984 free_socket(sockp);
1987 static isc_result_t
1988 allocate_socket(isc__socketmgr_t *manager, isc_sockettype_t type,
1989 isc__socket_t **socketp)
1991 isc__socket_t *sock;
1992 isc_result_t result;
1993 ISC_SOCKADDR_LEN_T cmsgbuflen;
1995 sock = isc_mem_get(manager->mctx, sizeof(*sock));
1997 if (sock == NULL)
1998 return (ISC_R_NOMEMORY);
2000 result = ISC_R_UNEXPECTED;
2002 sock->common.magic = 0;
2003 sock->common.impmagic = 0;
2004 sock->references = 0;
2006 sock->manager = manager;
2007 sock->type = type;
2008 sock->fd = -1;
2009 sock->statsindex = NULL;
2011 ISC_LINK_INIT(sock, link);
2013 sock->recvcmsgbuf = NULL;
2014 sock->sendcmsgbuf = NULL;
2017 * set up cmsg buffers
2019 cmsgbuflen = 0;
2020 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
2021 cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
2022 #endif
2023 #if defined(USE_CMSG) && defined(SO_TIMESTAMP)
2024 cmsgbuflen += cmsg_space(sizeof(struct timeval));
2025 #endif
2026 sock->recvcmsgbuflen = cmsgbuflen;
2027 if (sock->recvcmsgbuflen != 0U) {
2028 sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
2029 if (sock->recvcmsgbuf == NULL)
2030 goto error;
2033 cmsgbuflen = 0;
2034 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
2035 cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
2036 #endif
2037 sock->sendcmsgbuflen = cmsgbuflen;
2038 if (sock->sendcmsgbuflen != 0U) {
2039 sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
2040 if (sock->sendcmsgbuf == NULL)
2041 goto error;
2044 memset(sock->name, 0, sizeof(sock->name));
2045 sock->tag = NULL;
2048 * set up list of readers and writers to be initially empty
2050 ISC_LIST_INIT(sock->recv_list);
2051 ISC_LIST_INIT(sock->send_list);
2052 ISC_LIST_INIT(sock->accept_list);
2053 sock->connect_ev = NULL;
2054 sock->pending_recv = 0;
2055 sock->pending_send = 0;
2056 sock->pending_accept = 0;
2057 sock->listener = 0;
2058 sock->connected = 0;
2059 sock->connecting = 0;
2060 sock->bound = 0;
2063 * initialize the lock
2065 result = isc_mutex_init(&sock->lock);
2066 if (result != ISC_R_SUCCESS) {
2067 sock->common.magic = 0;
2068 sock->common.impmagic = 0;
2069 goto error;
2073 * Initialize readable and writable events
2075 ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
2076 ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTR,
2077 NULL, sock, sock, NULL, NULL);
2078 ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
2079 ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTW,
2080 NULL, sock, sock, NULL, NULL);
2082 sock->common.magic = ISCAPI_SOCKET_MAGIC;
2083 sock->common.impmagic = SOCKET_MAGIC;
2084 *socketp = sock;
2086 return (ISC_R_SUCCESS);
2088 error:
2089 if (sock->recvcmsgbuf != NULL)
2090 isc_mem_put(manager->mctx, sock->recvcmsgbuf,
2091 sock->recvcmsgbuflen);
2092 if (sock->sendcmsgbuf != NULL)
2093 isc_mem_put(manager->mctx, sock->sendcmsgbuf,
2094 sock->sendcmsgbuflen);
2095 isc_mem_put(manager->mctx, sock, sizeof(*sock));
2097 return (result);
2101 * This event requires that the various lists be empty, that the reference
2102 * count be 1, and that the magic number is valid. The other socket bits,
2103 * like the lock, must be initialized as well. The fd associated must be
2104 * marked as closed, by setting it to -1 on close, or this routine will
2105 * also close the socket.
2107 static void
2108 free_socket(isc__socket_t **socketp) {
2109 isc__socket_t *sock = *socketp;
2111 INSIST(sock->references == 0);
2112 INSIST(VALID_SOCKET(sock));
2113 INSIST(!sock->connecting);
2114 INSIST(!sock->pending_recv);
2115 INSIST(!sock->pending_send);
2116 INSIST(!sock->pending_accept);
2117 INSIST(ISC_LIST_EMPTY(sock->recv_list));
2118 INSIST(ISC_LIST_EMPTY(sock->send_list));
2119 INSIST(ISC_LIST_EMPTY(sock->accept_list));
2120 INSIST(!ISC_LINK_LINKED(sock, link));
2122 if (sock->recvcmsgbuf != NULL)
2123 isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
2124 sock->recvcmsgbuflen);
2125 if (sock->sendcmsgbuf != NULL)
2126 isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
2127 sock->sendcmsgbuflen);
2129 sock->common.magic = 0;
2130 sock->common.impmagic = 0;
2132 DESTROYLOCK(&sock->lock);
2134 isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
2136 *socketp = NULL;
2139 #ifdef SO_BSDCOMPAT
2141 * This really should not be necessary to do. Having to workout
2142 * which kernel version we are on at run time so that we don't cause
2143 * the kernel to issue a warning about us using a deprecated socket option.
2144 * Such warnings should *never* be on by default in production kernels.
2146 * We can't do this a build time because executables are moved between
2147 * machines and hence kernels.
2149 * We can't just not set SO_BSDCOMAT because some kernels require it.
2152 static isc_once_t bsdcompat_once = ISC_ONCE_INIT;
2153 isc_boolean_t bsdcompat = ISC_TRUE;
2155 static void
2156 clear_bsdcompat(void) {
2157 #ifdef __linux__
2158 struct utsname buf;
2159 char *endp;
2160 long int major;
2161 long int minor;
2163 uname(&buf); /* Can only fail if buf is bad in Linux. */
2165 /* Paranoia in parsing can be increased, but we trust uname(). */
2166 major = strtol(buf.release, &endp, 10);
2167 if (*endp == '.') {
2168 minor = strtol(endp+1, &endp, 10);
2169 if ((major > 2) || ((major == 2) && (minor >= 4))) {
2170 bsdcompat = ISC_FALSE;
2173 #endif /* __linux __ */
2175 #endif
2177 static isc_result_t
2178 opensocket(isc__socketmgr_t *manager, isc__socket_t *sock) {
2179 char strbuf[ISC_STRERRORSIZE];
2180 const char *err = "socket";
2181 int tries = 0;
2182 #if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
2183 int on = 1;
2184 #endif
2185 #if defined(SO_RCVBUF)
2186 ISC_SOCKADDR_LEN_T optlen;
2187 int size;
2188 #endif
2190 again:
2191 switch (sock->type) {
2192 case isc_sockettype_udp:
2193 sock->fd = socket(sock->pf, SOCK_DGRAM, IPPROTO_UDP);
2194 break;
2195 case isc_sockettype_tcp:
2196 sock->fd = socket(sock->pf, SOCK_STREAM, IPPROTO_TCP);
2197 break;
2198 case isc_sockettype_unix:
2199 sock->fd = socket(sock->pf, SOCK_STREAM, 0);
2200 break;
2201 case isc_sockettype_fdwatch:
2203 * We should not be called for isc_sockettype_fdwatch sockets.
2205 INSIST(0);
2206 break;
2208 if (sock->fd == -1 && errno == EINTR && tries++ < 42)
2209 goto again;
2211 #ifdef F_DUPFD
2213 * Leave a space for stdio and TCP to work in.
2215 if (manager->reserved != 0 && sock->type == isc_sockettype_udp &&
2216 sock->fd >= 0 && sock->fd < manager->reserved) {
2217 int new, tmp;
2218 new = fcntl(sock->fd, F_DUPFD, manager->reserved);
2219 tmp = errno;
2220 (void)close(sock->fd);
2221 errno = tmp;
2222 sock->fd = new;
2223 err = "isc_socket_create: fcntl/reserved";
2224 } else if (sock->fd >= 0 && sock->fd < 20) {
2225 int new, tmp;
2226 new = fcntl(sock->fd, F_DUPFD, 20);
2227 tmp = errno;
2228 (void)close(sock->fd);
2229 errno = tmp;
2230 sock->fd = new;
2231 err = "isc_socket_create: fcntl";
2233 #endif
2235 if (sock->fd >= (int)manager->maxsocks) {
2236 (void)close(sock->fd);
2237 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2238 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2239 isc_msgcat, ISC_MSGSET_SOCKET,
2240 ISC_MSG_TOOMANYFDS,
2241 "socket: file descriptor exceeds limit (%d/%u)",
2242 sock->fd, manager->maxsocks);
2243 return (ISC_R_NORESOURCES);
2246 if (sock->fd < 0) {
2247 switch (errno) {
2248 case EMFILE:
2249 case ENFILE:
2250 isc__strerror(errno, strbuf, sizeof(strbuf));
2251 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2252 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2253 isc_msgcat, ISC_MSGSET_SOCKET,
2254 ISC_MSG_TOOMANYFDS,
2255 "%s: %s", err, strbuf);
2256 /* fallthrough */
2257 case ENOBUFS:
2258 return (ISC_R_NORESOURCES);
2260 case EPROTONOSUPPORT:
2261 case EPFNOSUPPORT:
2262 case EAFNOSUPPORT:
2264 * Linux 2.2 (and maybe others) return EINVAL instead of
2265 * EAFNOSUPPORT.
2267 case EINVAL:
2268 return (ISC_R_FAMILYNOSUPPORT);
2270 default:
2271 isc__strerror(errno, strbuf, sizeof(strbuf));
2272 UNEXPECTED_ERROR(__FILE__, __LINE__,
2273 "%s() %s: %s", err,
2274 isc_msgcat_get(isc_msgcat,
2275 ISC_MSGSET_GENERAL,
2276 ISC_MSG_FAILED,
2277 "failed"),
2278 strbuf);
2279 return (ISC_R_UNEXPECTED);
2283 if (make_nonblock(sock->fd) != ISC_R_SUCCESS) {
2284 (void)close(sock->fd);
2285 return (ISC_R_UNEXPECTED);
2288 #ifdef SO_BSDCOMPAT
2289 RUNTIME_CHECK(isc_once_do(&bsdcompat_once,
2290 clear_bsdcompat) == ISC_R_SUCCESS);
2291 if (sock->type != isc_sockettype_unix && bsdcompat &&
2292 setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
2293 (void *)&on, sizeof(on)) < 0) {
2294 isc__strerror(errno, strbuf, sizeof(strbuf));
2295 UNEXPECTED_ERROR(__FILE__, __LINE__,
2296 "setsockopt(%d, SO_BSDCOMPAT) %s: %s",
2297 sock->fd,
2298 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2299 ISC_MSG_FAILED, "failed"),
2300 strbuf);
2301 /* Press on... */
2303 #endif
2305 #ifdef SO_NOSIGPIPE
2306 if (setsockopt(sock->fd, SOL_SOCKET, SO_NOSIGPIPE,
2307 (void *)&on, sizeof(on)) < 0) {
2308 isc__strerror(errno, strbuf, sizeof(strbuf));
2309 UNEXPECTED_ERROR(__FILE__, __LINE__,
2310 "setsockopt(%d, SO_NOSIGPIPE) %s: %s",
2311 sock->fd,
2312 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2313 ISC_MSG_FAILED, "failed"),
2314 strbuf);
2315 /* Press on... */
2317 #endif
2319 #if defined(USE_CMSG) || defined(SO_RCVBUF)
2320 if (sock->type == isc_sockettype_udp) {
2322 #if defined(USE_CMSG)
2323 #if defined(SO_TIMESTAMP)
2324 if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
2325 (void *)&on, sizeof(on)) < 0
2326 && errno != ENOPROTOOPT) {
2327 isc__strerror(errno, strbuf, sizeof(strbuf));
2328 UNEXPECTED_ERROR(__FILE__, __LINE__,
2329 "setsockopt(%d, SO_TIMESTAMP) %s: %s",
2330 sock->fd,
2331 isc_msgcat_get(isc_msgcat,
2332 ISC_MSGSET_GENERAL,
2333 ISC_MSG_FAILED,
2334 "failed"),
2335 strbuf);
2336 /* Press on... */
2338 #endif /* SO_TIMESTAMP */
2340 #if defined(ISC_PLATFORM_HAVEIPV6)
2341 if (sock->pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
2343 * Warn explicitly because this anomaly can be hidden
2344 * in usual operation (and unexpectedly appear later).
2346 UNEXPECTED_ERROR(__FILE__, __LINE__,
2347 "No buffer available to receive "
2348 "IPv6 destination");
2350 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
2351 #ifdef IPV6_RECVPKTINFO
2352 /* RFC 3542 */
2353 if ((sock->pf == AF_INET6)
2354 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
2355 (void *)&on, sizeof(on)) < 0)) {
2356 isc__strerror(errno, strbuf, sizeof(strbuf));
2357 UNEXPECTED_ERROR(__FILE__, __LINE__,
2358 "setsockopt(%d, IPV6_RECVPKTINFO) "
2359 "%s: %s", sock->fd,
2360 isc_msgcat_get(isc_msgcat,
2361 ISC_MSGSET_GENERAL,
2362 ISC_MSG_FAILED,
2363 "failed"),
2364 strbuf);
2366 #else
2367 /* RFC 2292 */
2368 if ((sock->pf == AF_INET6)
2369 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
2370 (void *)&on, sizeof(on)) < 0)) {
2371 isc__strerror(errno, strbuf, sizeof(strbuf));
2372 UNEXPECTED_ERROR(__FILE__, __LINE__,
2373 "setsockopt(%d, IPV6_PKTINFO) %s: %s",
2374 sock->fd,
2375 isc_msgcat_get(isc_msgcat,
2376 ISC_MSGSET_GENERAL,
2377 ISC_MSG_FAILED,
2378 "failed"),
2379 strbuf);
2381 #endif /* IPV6_RECVPKTINFO */
2382 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
2383 #ifdef IPV6_USE_MIN_MTU /* RFC 3542, not too common yet*/
2384 /* use minimum MTU */
2385 if (sock->pf == AF_INET6) {
2386 (void)setsockopt(sock->fd, IPPROTO_IPV6,
2387 IPV6_USE_MIN_MTU,
2388 (void *)&on, sizeof(on));
2390 #endif
2391 #endif /* ISC_PLATFORM_HAVEIPV6 */
2392 #endif /* defined(USE_CMSG) */
2394 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2396 * Turn off Path MTU discovery on IPv4/UDP sockets.
2398 if (sock->pf == AF_INET) {
2399 int action = IP_PMTUDISC_DONT;
2400 (void)setsockopt(sock->fd, IPPROTO_IP, IP_MTU_DISCOVER,
2401 &action, sizeof(action));
2403 #endif
2404 #if defined(IP_DONTFRAG)
2406 * Turn off Path MTU discovery on IPv4/UDP sockets.
2408 if (sock->pf == AF_INET) {
2409 int off = 0;
2410 (void)setsockopt(sock->fd, IPPROTO_IP, IP_DONTFRAG,
2411 &off, sizeof(off));
2413 #endif
2415 #if defined(SO_RCVBUF)
2416 optlen = sizeof(size);
2417 if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
2418 (void *)&size, &optlen) >= 0 &&
2419 size < RCVBUFSIZE) {
2420 size = RCVBUFSIZE;
2421 if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
2422 (void *)&size, sizeof(size)) == -1) {
2423 isc__strerror(errno, strbuf, sizeof(strbuf));
2424 UNEXPECTED_ERROR(__FILE__, __LINE__,
2425 "setsockopt(%d, SO_RCVBUF, %d) %s: %s",
2426 sock->fd, size,
2427 isc_msgcat_get(isc_msgcat,
2428 ISC_MSGSET_GENERAL,
2429 ISC_MSG_FAILED,
2430 "failed"),
2431 strbuf);
2434 #endif
2436 #endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
2438 inc_stats(manager->stats, sock->statsindex[STATID_OPEN]);
2440 return (ISC_R_SUCCESS);
2444 * Create a new 'type' socket managed by 'manager'. Events
2445 * will be posted to 'task' and when dispatched 'action' will be
2446 * called with 'arg' as the arg value. The new socket is returned
2447 * in 'socketp'.
2449 ISC_SOCKETFUNC_SCOPE isc_result_t
2450 isc__socket_create(isc_socketmgr_t *manager0, int pf, isc_sockettype_t type,
2451 isc_socket_t **socketp)
2453 isc__socket_t *sock = NULL;
2454 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
2455 isc_result_t result;
2456 int lockid;
2458 REQUIRE(VALID_MANAGER(manager));
2459 REQUIRE(socketp != NULL && *socketp == NULL);
2460 REQUIRE(type != isc_sockettype_fdwatch);
2462 result = allocate_socket(manager, type, &sock);
2463 if (result != ISC_R_SUCCESS)
2464 return (result);
2466 switch (sock->type) {
2467 case isc_sockettype_udp:
2468 sock->statsindex =
2469 (pf == AF_INET) ? upd4statsindex : upd6statsindex;
2470 break;
2471 case isc_sockettype_tcp:
2472 sock->statsindex =
2473 (pf == AF_INET) ? tcp4statsindex : tcp6statsindex;
2474 break;
2475 case isc_sockettype_unix:
2476 sock->statsindex = unixstatsindex;
2477 break;
2478 default:
2479 INSIST(0);
2482 sock->pf = pf;
2483 result = opensocket(manager, sock);
2484 if (result != ISC_R_SUCCESS) {
2485 inc_stats(manager->stats, sock->statsindex[STATID_OPENFAIL]);
2486 free_socket(&sock);
2487 return (result);
2490 sock->common.methods = (isc_socketmethods_t *)&socketmethods;
2491 sock->references = 1;
2492 *socketp = (isc_socket_t *)sock;
2495 * Note we don't have to lock the socket like we normally would because
2496 * there are no external references to it yet.
2499 lockid = FDLOCK_ID(sock->fd);
2500 LOCK(&manager->fdlock[lockid]);
2501 manager->fds[sock->fd] = sock;
2502 manager->fdstate[sock->fd] = MANAGED;
2503 #ifdef USE_DEVPOLL
2504 INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
2505 sock->manager->fdpollinfo[sock->fd].want_write == 0);
2506 #endif
2507 UNLOCK(&manager->fdlock[lockid]);
2509 LOCK(&manager->lock);
2510 ISC_LIST_APPEND(manager->socklist, sock, link);
2511 #ifdef USE_SELECT
2512 if (manager->maxfd < sock->fd)
2513 manager->maxfd = sock->fd;
2514 #endif
2515 UNLOCK(&manager->lock);
2517 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2518 ISC_MSG_CREATED, "created");
2520 return (ISC_R_SUCCESS);
2523 #ifdef BIND9
2524 ISC_SOCKETFUNC_SCOPE isc_result_t
2525 isc__socket_open(isc_socket_t *sock0) {
2526 isc_result_t result;
2527 isc__socket_t *sock = (isc__socket_t *)sock0;
2529 REQUIRE(VALID_SOCKET(sock));
2531 LOCK(&sock->lock);
2532 REQUIRE(sock->references == 1);
2533 REQUIRE(sock->type != isc_sockettype_fdwatch);
2534 UNLOCK(&sock->lock);
2536 * We don't need to retain the lock hereafter, since no one else has
2537 * this socket.
2539 REQUIRE(sock->fd == -1);
2541 result = opensocket(sock->manager, sock);
2542 if (result != ISC_R_SUCCESS)
2543 sock->fd = -1;
2545 if (result == ISC_R_SUCCESS) {
2546 int lockid = FDLOCK_ID(sock->fd);
2548 LOCK(&sock->manager->fdlock[lockid]);
2549 sock->manager->fds[sock->fd] = sock;
2550 sock->manager->fdstate[sock->fd] = MANAGED;
2551 #ifdef USE_DEVPOLL
2552 INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
2553 sock->manager->fdpollinfo[sock->fd].want_write == 0);
2554 #endif
2555 UNLOCK(&sock->manager->fdlock[lockid]);
2557 #ifdef USE_SELECT
2558 LOCK(&sock->manager->lock);
2559 if (sock->manager->maxfd < sock->fd)
2560 sock->manager->maxfd = sock->fd;
2561 UNLOCK(&sock->manager->lock);
2562 #endif
2565 return (result);
2567 #endif /* BIND9 */
2570 * Create a new 'type' socket managed by 'manager'. Events
2571 * will be posted to 'task' and when dispatched 'action' will be
2572 * called with 'arg' as the arg value. The new socket is returned
2573 * in 'socketp'.
2575 ISC_SOCKETFUNC_SCOPE isc_result_t
2576 isc__socket_fdwatchcreate(isc_socketmgr_t *manager0, int fd, int flags,
2577 isc_sockfdwatch_t callback, void *cbarg,
2578 isc_task_t *task, isc_socket_t **socketp)
2580 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
2581 isc__socket_t *sock = NULL;
2582 isc_result_t result;
2583 int lockid;
2585 REQUIRE(VALID_MANAGER(manager));
2586 REQUIRE(socketp != NULL && *socketp == NULL);
2588 result = allocate_socket(manager, isc_sockettype_fdwatch, &sock);
2589 if (result != ISC_R_SUCCESS)
2590 return (result);
2592 sock->fd = fd;
2593 sock->fdwatcharg = cbarg;
2594 sock->fdwatchcb = callback;
2595 sock->fdwatchflags = flags;
2596 sock->fdwatchtask = task;
2597 sock->statsindex = fdwatchstatsindex;
2599 sock->common.methods = (isc_socketmethods_t *)&socketmethods;
2600 sock->references = 1;
2601 *socketp = (isc_socket_t *)sock;
2604 * Note we don't have to lock the socket like we normally would because
2605 * there are no external references to it yet.
2608 lockid = FDLOCK_ID(sock->fd);
2609 LOCK(&manager->fdlock[lockid]);
2610 manager->fds[sock->fd] = sock;
2611 manager->fdstate[sock->fd] = MANAGED;
2612 UNLOCK(&manager->fdlock[lockid]);
2614 LOCK(&manager->lock);
2615 ISC_LIST_APPEND(manager->socklist, sock, link);
2616 #ifdef USE_SELECT
2617 if (manager->maxfd < sock->fd)
2618 manager->maxfd = sock->fd;
2619 #endif
2620 UNLOCK(&manager->lock);
2622 if (flags & ISC_SOCKFDWATCH_READ)
2623 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2624 if (flags & ISC_SOCKFDWATCH_WRITE)
2625 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
2627 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
2628 ISC_MSG_CREATED, "fdwatch-created");
2630 return (ISC_R_SUCCESS);
2634 * Indicate to the manager that it should watch the socket again.
2635 * This can be used to restart watching if the previous event handler
2636 * didn't indicate there was more data to be processed. Primarily
2637 * it is for writing but could be used for reading if desired
2640 ISC_SOCKETFUNC_SCOPE isc_result_t
2641 isc__socket_fdwatchpoke(isc_socket_t *sock0, int flags)
2643 isc__socket_t *sock = (isc__socket_t *)sock0;
2645 REQUIRE(VALID_SOCKET(sock));
2648 * We check both flags first to allow us to get the lock
2649 * once but only if we need it.
2652 if ((flags & (ISC_SOCKFDWATCH_READ | ISC_SOCKFDWATCH_WRITE)) != 0) {
2653 LOCK(&sock->lock);
2654 if (((flags & ISC_SOCKFDWATCH_READ) != 0) &&
2655 !sock->pending_recv)
2656 select_poke(sock->manager, sock->fd,
2657 SELECT_POKE_READ);
2658 if (((flags & ISC_SOCKFDWATCH_WRITE) != 0) &&
2659 !sock->pending_send)
2660 select_poke(sock->manager, sock->fd,
2661 SELECT_POKE_WRITE);
2662 UNLOCK(&sock->lock);
2665 socket_log(sock, NULL, TRACE, isc_msgcat, ISC_MSGSET_SOCKET,
2666 ISC_MSG_POKED, "fdwatch-poked flags: %d", flags);
2668 return (ISC_R_SUCCESS);
2672 * Attach to a socket. Caller must explicitly detach when it is done.
2674 ISC_SOCKETFUNC_SCOPE void
2675 isc__socket_attach(isc_socket_t *sock0, isc_socket_t **socketp) {
2676 isc__socket_t *sock = (isc__socket_t *)sock0;
2678 REQUIRE(VALID_SOCKET(sock));
2679 REQUIRE(socketp != NULL && *socketp == NULL);
2681 LOCK(&sock->lock);
2682 sock->references++;
2683 UNLOCK(&sock->lock);
2685 *socketp = (isc_socket_t *)sock;
2689 * Dereference a socket. If this is the last reference to it, clean things
2690 * up by destroying the socket.
2692 ISC_SOCKETFUNC_SCOPE void
2693 isc__socket_detach(isc_socket_t **socketp) {
2694 isc__socket_t *sock;
2695 isc_boolean_t kill_socket = ISC_FALSE;
2697 REQUIRE(socketp != NULL);
2698 sock = (isc__socket_t *)*socketp;
2699 REQUIRE(VALID_SOCKET(sock));
2701 LOCK(&sock->lock);
2702 REQUIRE(sock->references > 0);
2703 sock->references--;
2704 if (sock->references == 0)
2705 kill_socket = ISC_TRUE;
2706 UNLOCK(&sock->lock);
2708 if (kill_socket)
2709 destroy(&sock);
2711 *socketp = NULL;
2714 #ifdef BIND9
2715 ISC_SOCKETFUNC_SCOPE isc_result_t
2716 isc__socket_close(isc_socket_t *sock0) {
2717 isc__socket_t *sock = (isc__socket_t *)sock0;
2718 int fd;
2719 isc__socketmgr_t *manager;
2720 isc_sockettype_t type;
2722 REQUIRE(VALID_SOCKET(sock));
2724 LOCK(&sock->lock);
2726 REQUIRE(sock->references == 1);
2727 REQUIRE(sock->type != isc_sockettype_fdwatch);
2728 REQUIRE(sock->fd >= 0 && sock->fd < (int)sock->manager->maxsocks);
2730 INSIST(!sock->connecting);
2731 INSIST(!sock->pending_recv);
2732 INSIST(!sock->pending_send);
2733 INSIST(!sock->pending_accept);
2734 INSIST(ISC_LIST_EMPTY(sock->recv_list));
2735 INSIST(ISC_LIST_EMPTY(sock->send_list));
2736 INSIST(ISC_LIST_EMPTY(sock->accept_list));
2737 INSIST(sock->connect_ev == NULL);
2739 manager = sock->manager;
2740 type = sock->type;
2741 fd = sock->fd;
2742 sock->fd = -1;
2743 memset(sock->name, 0, sizeof(sock->name));
2744 sock->tag = NULL;
2745 sock->listener = 0;
2746 sock->connected = 0;
2747 sock->connecting = 0;
2748 sock->bound = 0;
2749 isc_sockaddr_any(&sock->peer_address);
2751 UNLOCK(&sock->lock);
2753 closesocket(manager, sock, fd);
2755 return (ISC_R_SUCCESS);
2757 #endif /* BIND9 */
2760 * I/O is possible on a given socket. Schedule an event to this task that
2761 * will call an internal function to do the I/O. This will charge the
2762 * task with the I/O operation and let our select loop handler get back
2763 * to doing something real as fast as possible.
2765 * The socket and manager must be locked before calling this function.
2767 static void
2768 dispatch_recv(isc__socket_t *sock) {
2769 intev_t *iev;
2770 isc_socketevent_t *ev;
2771 isc_task_t *sender;
2773 INSIST(!sock->pending_recv);
2775 if (sock->type != isc_sockettype_fdwatch) {
2776 ev = ISC_LIST_HEAD(sock->recv_list);
2777 if (ev == NULL)
2778 return;
2779 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2780 "dispatch_recv: event %p -> task %p",
2781 ev, ev->ev_sender);
2782 sender = ev->ev_sender;
2783 } else {
2784 sender = sock->fdwatchtask;
2787 sock->pending_recv = 1;
2788 iev = &sock->readable_ev;
2790 sock->references++;
2791 iev->ev_sender = sock;
2792 if (sock->type == isc_sockettype_fdwatch)
2793 iev->ev_action = internal_fdwatch_read;
2794 else
2795 iev->ev_action = internal_recv;
2796 iev->ev_arg = sock;
2798 isc_task_send(sender, (isc_event_t **)&iev);
2801 static void
2802 dispatch_send(isc__socket_t *sock) {
2803 intev_t *iev;
2804 isc_socketevent_t *ev;
2805 isc_task_t *sender;
2807 INSIST(!sock->pending_send);
2809 if (sock->type != isc_sockettype_fdwatch) {
2810 ev = ISC_LIST_HEAD(sock->send_list);
2811 if (ev == NULL)
2812 return;
2813 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2814 "dispatch_send: event %p -> task %p",
2815 ev, ev->ev_sender);
2816 sender = ev->ev_sender;
2817 } else {
2818 sender = sock->fdwatchtask;
2821 sock->pending_send = 1;
2822 iev = &sock->writable_ev;
2824 sock->references++;
2825 iev->ev_sender = sock;
2826 if (sock->type == isc_sockettype_fdwatch)
2827 iev->ev_action = internal_fdwatch_write;
2828 else
2829 iev->ev_action = internal_send;
2830 iev->ev_arg = sock;
2832 isc_task_send(sender, (isc_event_t **)&iev);
2836 * Dispatch an internal accept event.
2838 static void
2839 dispatch_accept(isc__socket_t *sock) {
2840 intev_t *iev;
2841 isc_socket_newconnev_t *ev;
2843 INSIST(!sock->pending_accept);
2846 * Are there any done events left, or were they all canceled
2847 * before the manager got the socket lock?
2849 ev = ISC_LIST_HEAD(sock->accept_list);
2850 if (ev == NULL)
2851 return;
2853 sock->pending_accept = 1;
2854 iev = &sock->readable_ev;
2856 sock->references++; /* keep socket around for this internal event */
2857 iev->ev_sender = sock;
2858 iev->ev_action = internal_accept;
2859 iev->ev_arg = sock;
2861 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
2864 static void
2865 dispatch_connect(isc__socket_t *sock) {
2866 intev_t *iev;
2867 isc_socket_connev_t *ev;
2869 iev = &sock->writable_ev;
2871 ev = sock->connect_ev;
2872 INSIST(ev != NULL); /* XXX */
2874 INSIST(sock->connecting);
2876 sock->references++; /* keep socket around for this internal event */
2877 iev->ev_sender = sock;
2878 iev->ev_action = internal_connect;
2879 iev->ev_arg = sock;
2881 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
2885 * Dequeue an item off the given socket's read queue, set the result code
2886 * in the done event to the one provided, and send it to the task it was
2887 * destined for.
2889 * If the event to be sent is on a list, remove it before sending. If
2890 * asked to, send and detach from the socket as well.
2892 * Caller must have the socket locked if the event is attached to the socket.
2894 static void
2895 send_recvdone_event(isc__socket_t *sock, isc_socketevent_t **dev) {
2896 isc_task_t *task;
2898 task = (*dev)->ev_sender;
2900 (*dev)->ev_sender = sock;
2902 if (ISC_LINK_LINKED(*dev, ev_link))
2903 ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
2905 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
2906 == ISC_SOCKEVENTATTR_ATTACHED)
2907 isc_task_sendanddetach(&task, (isc_event_t **)dev);
2908 else
2909 isc_task_send(task, (isc_event_t **)dev);
2913 * See comments for send_recvdone_event() above.
2915 * Caller must have the socket locked if the event is attached to the socket.
2917 static void
2918 send_senddone_event(isc__socket_t *sock, isc_socketevent_t **dev) {
2919 isc_task_t *task;
2921 INSIST(dev != NULL && *dev != NULL);
2923 task = (*dev)->ev_sender;
2924 (*dev)->ev_sender = sock;
2926 if (ISC_LINK_LINKED(*dev, ev_link))
2927 ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
2929 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
2930 == ISC_SOCKEVENTATTR_ATTACHED)
2931 isc_task_sendanddetach(&task, (isc_event_t **)dev);
2932 else
2933 isc_task_send(task, (isc_event_t **)dev);
2937 * Call accept() on a socket, to get the new file descriptor. The listen
2938 * socket is used as a prototype to create a new isc_socket_t. The new
2939 * socket has one outstanding reference. The task receiving the event
2940 * will be detached from just after the event is delivered.
2942 * On entry to this function, the event delivered is the internal
2943 * readable event, and the first item on the accept_list should be
2944 * the done event we want to send. If the list is empty, this is a no-op,
2945 * so just unlock and return.
2947 static void
2948 internal_accept(isc_task_t *me, isc_event_t *ev) {
2949 isc__socket_t *sock;
2950 isc__socketmgr_t *manager;
2951 isc_socket_newconnev_t *dev;
2952 isc_task_t *task;
2953 ISC_SOCKADDR_LEN_T addrlen;
2954 int fd;
2955 isc_result_t result = ISC_R_SUCCESS;
2956 char strbuf[ISC_STRERRORSIZE];
2957 const char *err = "accept";
2959 UNUSED(me);
2961 sock = ev->ev_sender;
2962 INSIST(VALID_SOCKET(sock));
2964 LOCK(&sock->lock);
2965 socket_log(sock, NULL, TRACE,
2966 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
2967 "internal_accept called, locked socket");
2969 manager = sock->manager;
2970 INSIST(VALID_MANAGER(manager));
2972 INSIST(sock->listener);
2973 INSIST(sock->pending_accept == 1);
2974 sock->pending_accept = 0;
2976 INSIST(sock->references > 0);
2977 sock->references--; /* the internal event is done with this socket */
2978 if (sock->references == 0) {
2979 UNLOCK(&sock->lock);
2980 destroy(&sock);
2981 return;
2985 * Get the first item off the accept list.
2986 * If it is empty, unlock the socket and return.
2988 dev = ISC_LIST_HEAD(sock->accept_list);
2989 if (dev == NULL) {
2990 UNLOCK(&sock->lock);
2991 return;
2995 * Try to accept the new connection. If the accept fails with
2996 * EAGAIN or EINTR, simply poke the watcher to watch this socket
2997 * again. Also ignore ECONNRESET, which has been reported to
2998 * be spuriously returned on Linux 2.2.19 although it is not
2999 * a documented error for accept(). ECONNABORTED has been
3000 * reported for Solaris 8. The rest are thrown in not because
3001 * we have seen them but because they are ignored by other
3002 * daemons such as BIND 8 and Apache.
3005 addrlen = sizeof(NEWCONNSOCK(dev)->peer_address.type);
3006 memset(&NEWCONNSOCK(dev)->peer_address.type, 0, addrlen);
3007 fd = accept(sock->fd, &NEWCONNSOCK(dev)->peer_address.type.sa,
3008 (void *)&addrlen);
3010 #ifdef F_DUPFD
3012 * Leave a space for stdio to work in.
3014 if (fd >= 0 && fd < 20) {
3015 int new, tmp;
3016 new = fcntl(fd, F_DUPFD, 20);
3017 tmp = errno;
3018 (void)close(fd);
3019 errno = tmp;
3020 fd = new;
3021 err = "accept/fcntl";
3023 #endif
3025 if (fd < 0) {
3026 if (SOFT_ERROR(errno))
3027 goto soft_error;
3028 switch (errno) {
3029 case ENFILE:
3030 case EMFILE:
3031 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3032 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3033 isc_msgcat, ISC_MSGSET_SOCKET,
3034 ISC_MSG_TOOMANYFDS,
3035 "%s: too many open file descriptors",
3036 err);
3037 goto soft_error;
3039 case ENOBUFS:
3040 case ENOMEM:
3041 case ECONNRESET:
3042 case ECONNABORTED:
3043 case EHOSTUNREACH:
3044 case EHOSTDOWN:
3045 case ENETUNREACH:
3046 case ENETDOWN:
3047 case ECONNREFUSED:
3048 #ifdef EPROTO
3049 case EPROTO:
3050 #endif
3051 #ifdef ENONET
3052 case ENONET:
3053 #endif
3054 goto soft_error;
3055 default:
3056 break;
3058 isc__strerror(errno, strbuf, sizeof(strbuf));
3059 UNEXPECTED_ERROR(__FILE__, __LINE__,
3060 "internal_accept: %s() %s: %s", err,
3061 isc_msgcat_get(isc_msgcat,
3062 ISC_MSGSET_GENERAL,
3063 ISC_MSG_FAILED,
3064 "failed"),
3065 strbuf);
3066 fd = -1;
3067 result = ISC_R_UNEXPECTED;
3068 } else {
3069 if (addrlen == 0U) {
3070 UNEXPECTED_ERROR(__FILE__, __LINE__,
3071 "internal_accept(): "
3072 "accept() failed to return "
3073 "remote address");
3075 (void)close(fd);
3076 goto soft_error;
3077 } else if (NEWCONNSOCK(dev)->peer_address.type.sa.sa_family !=
3078 sock->pf)
3080 UNEXPECTED_ERROR(__FILE__, __LINE__,
3081 "internal_accept(): "
3082 "accept() returned peer address "
3083 "family %u (expected %u)",
3084 NEWCONNSOCK(dev)->peer_address.
3085 type.sa.sa_family,
3086 sock->pf);
3087 (void)close(fd);
3088 goto soft_error;
3089 } else if (fd >= (int)manager->maxsocks) {
3090 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3091 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3092 isc_msgcat, ISC_MSGSET_SOCKET,
3093 ISC_MSG_TOOMANYFDS,
3094 "accept: "
3095 "file descriptor exceeds limit (%d/%u)",
3096 fd, manager->maxsocks);
3097 (void)close(fd);
3098 goto soft_error;
3102 if (fd != -1) {
3103 NEWCONNSOCK(dev)->peer_address.length = addrlen;
3104 NEWCONNSOCK(dev)->pf = sock->pf;
3108 * Pull off the done event.
3110 ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
3113 * Poke watcher if there are more pending accepts.
3115 if (!ISC_LIST_EMPTY(sock->accept_list))
3116 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
3118 UNLOCK(&sock->lock);
3120 if (fd != -1 && (make_nonblock(fd) != ISC_R_SUCCESS)) {
3121 (void)close(fd);
3122 fd = -1;
3123 result = ISC_R_UNEXPECTED;
3127 * -1 means the new socket didn't happen.
3129 if (fd != -1) {
3130 int lockid = FDLOCK_ID(fd);
3132 LOCK(&manager->fdlock[lockid]);
3133 manager->fds[fd] = NEWCONNSOCK(dev);
3134 manager->fdstate[fd] = MANAGED;
3135 UNLOCK(&manager->fdlock[lockid]);
3137 LOCK(&manager->lock);
3138 ISC_LIST_APPEND(manager->socklist, NEWCONNSOCK(dev), link);
3140 NEWCONNSOCK(dev)->fd = fd;
3141 NEWCONNSOCK(dev)->bound = 1;
3142 NEWCONNSOCK(dev)->connected = 1;
3145 * Save away the remote address
3147 dev->address = NEWCONNSOCK(dev)->peer_address;
3149 #ifdef USE_SELECT
3150 if (manager->maxfd < fd)
3151 manager->maxfd = fd;
3152 #endif
3154 socket_log(sock, &NEWCONNSOCK(dev)->peer_address, CREATION,
3155 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
3156 "accepted connection, new socket %p",
3157 dev->newsocket);
3159 UNLOCK(&manager->lock);
3161 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPT]);
3162 } else {
3163 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
3164 NEWCONNSOCK(dev)->references--;
3165 free_socket((isc__socket_t **)(void *)&dev->newsocket);
3169 * Fill in the done event details and send it off.
3171 dev->result = result;
3172 task = dev->ev_sender;
3173 dev->ev_sender = sock;
3175 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
3176 return;
3178 soft_error:
3179 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
3180 UNLOCK(&sock->lock);
3182 inc_stats(manager->stats, sock->statsindex[STATID_ACCEPTFAIL]);
3183 return;
3186 static void
3187 internal_recv(isc_task_t *me, isc_event_t *ev) {
3188 isc_socketevent_t *dev;
3189 isc__socket_t *sock;
3191 INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
3193 sock = ev->ev_sender;
3194 INSIST(VALID_SOCKET(sock));
3196 LOCK(&sock->lock);
3197 socket_log(sock, NULL, IOEVENT,
3198 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
3199 "internal_recv: task %p got event %p", me, ev);
3201 INSIST(sock->pending_recv == 1);
3202 sock->pending_recv = 0;
3204 INSIST(sock->references > 0);
3205 sock->references--; /* the internal event is done with this socket */
3206 if (sock->references == 0) {
3207 UNLOCK(&sock->lock);
3208 destroy(&sock);
3209 return;
3213 * Try to do as much I/O as possible on this socket. There are no
3214 * limits here, currently.
3216 dev = ISC_LIST_HEAD(sock->recv_list);
3217 while (dev != NULL) {
3218 switch (doio_recv(sock, dev)) {
3219 case DOIO_SOFT:
3220 goto poke;
3222 case DOIO_EOF:
3224 * read of 0 means the remote end was closed.
3225 * Run through the event queue and dispatch all
3226 * the events with an EOF result code.
3228 do {
3229 dev->result = ISC_R_EOF;
3230 send_recvdone_event(sock, &dev);
3231 dev = ISC_LIST_HEAD(sock->recv_list);
3232 } while (dev != NULL);
3233 goto poke;
3235 case DOIO_SUCCESS:
3236 case DOIO_HARD:
3237 send_recvdone_event(sock, &dev);
3238 break;
3241 dev = ISC_LIST_HEAD(sock->recv_list);
3244 poke:
3245 if (!ISC_LIST_EMPTY(sock->recv_list))
3246 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
3248 UNLOCK(&sock->lock);
3251 static void
3252 internal_send(isc_task_t *me, isc_event_t *ev) {
3253 isc_socketevent_t *dev;
3254 isc__socket_t *sock;
3256 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
3259 * Find out what socket this is and lock it.
3261 sock = (isc__socket_t *)ev->ev_sender;
3262 INSIST(VALID_SOCKET(sock));
3264 LOCK(&sock->lock);
3265 socket_log(sock, NULL, IOEVENT,
3266 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
3267 "internal_send: task %p got event %p", me, ev);
3269 INSIST(sock->pending_send == 1);
3270 sock->pending_send = 0;
3272 INSIST(sock->references > 0);
3273 sock->references--; /* the internal event is done with this socket */
3274 if (sock->references == 0) {
3275 UNLOCK(&sock->lock);
3276 destroy(&sock);
3277 return;
3281 * Try to do as much I/O as possible on this socket. There are no
3282 * limits here, currently.
3284 dev = ISC_LIST_HEAD(sock->send_list);
3285 while (dev != NULL) {
3286 switch (doio_send(sock, dev)) {
3287 case DOIO_SOFT:
3288 goto poke;
3290 case DOIO_HARD:
3291 case DOIO_SUCCESS:
3292 send_senddone_event(sock, &dev);
3293 break;
3296 dev = ISC_LIST_HEAD(sock->send_list);
3299 poke:
3300 if (!ISC_LIST_EMPTY(sock->send_list))
3301 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
3303 UNLOCK(&sock->lock);
3306 static void
3307 internal_fdwatch_write(isc_task_t *me, isc_event_t *ev) {
3308 isc__socket_t *sock;
3309 int more_data;
3311 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
3314 * Find out what socket this is and lock it.
3316 sock = (isc__socket_t *)ev->ev_sender;
3317 INSIST(VALID_SOCKET(sock));
3319 LOCK(&sock->lock);
3320 socket_log(sock, NULL, IOEVENT,
3321 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
3322 "internal_fdwatch_write: task %p got event %p", me, ev);
3324 INSIST(sock->pending_send == 1);
3326 UNLOCK(&sock->lock);
3327 more_data = (sock->fdwatchcb)(me, (isc_socket_t *)sock,
3328 sock->fdwatcharg, ISC_SOCKFDWATCH_WRITE);
3329 LOCK(&sock->lock);
3331 sock->pending_send = 0;
3333 INSIST(sock->references > 0);
3334 sock->references--; /* the internal event is done with this socket */
3335 if (sock->references == 0) {
3336 UNLOCK(&sock->lock);
3337 destroy(&sock);
3338 return;
3341 if (more_data)
3342 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
3344 UNLOCK(&sock->lock);
3347 static void
3348 internal_fdwatch_read(isc_task_t *me, isc_event_t *ev) {
3349 isc__socket_t *sock;
3350 int more_data;
3352 INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
3355 * Find out what socket this is and lock it.
3357 sock = (isc__socket_t *)ev->ev_sender;
3358 INSIST(VALID_SOCKET(sock));
3360 LOCK(&sock->lock);
3361 socket_log(sock, NULL, IOEVENT,
3362 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
3363 "internal_fdwatch_read: task %p got event %p", me, ev);
3365 INSIST(sock->pending_recv == 1);
3367 UNLOCK(&sock->lock);
3368 more_data = (sock->fdwatchcb)(me, (isc_socket_t *)sock,
3369 sock->fdwatcharg, ISC_SOCKFDWATCH_READ);
3370 LOCK(&sock->lock);
3372 sock->pending_recv = 0;
3374 INSIST(sock->references > 0);
3375 sock->references--; /* the internal event is done with this socket */
3376 if (sock->references == 0) {
3377 UNLOCK(&sock->lock);
3378 destroy(&sock);
3379 return;
3382 if (more_data)
3383 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
3385 UNLOCK(&sock->lock);
3389 * Process read/writes on each fd here. Avoid locking
3390 * and unlocking twice if both reads and writes are possible.
3392 static void
3393 process_fd(isc__socketmgr_t *manager, int fd, isc_boolean_t readable,
3394 isc_boolean_t writeable)
3396 isc__socket_t *sock;
3397 isc_boolean_t unlock_sock;
3398 isc_boolean_t unwatch_read = ISC_FALSE, unwatch_write = ISC_FALSE;
3399 int lockid = FDLOCK_ID(fd);
3402 * If the socket is going to be closed, don't do more I/O.
3404 LOCK(&manager->fdlock[lockid]);
3405 if (manager->fdstate[fd] == CLOSE_PENDING) {
3406 UNLOCK(&manager->fdlock[lockid]);
3408 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
3409 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
3410 return;
3413 sock = manager->fds[fd];
3414 unlock_sock = ISC_FALSE;
3415 if (readable) {
3416 if (sock == NULL) {
3417 unwatch_read = ISC_TRUE;
3418 goto check_write;
3420 unlock_sock = ISC_TRUE;
3421 LOCK(&sock->lock);
3422 if (!SOCK_DEAD(sock)) {
3423 if (sock->listener)
3424 dispatch_accept(sock);
3425 else
3426 dispatch_recv(sock);
3428 unwatch_read = ISC_TRUE;
3430 check_write:
3431 if (writeable) {
3432 if (sock == NULL) {
3433 unwatch_write = ISC_TRUE;
3434 goto unlock_fd;
3436 if (!unlock_sock) {
3437 unlock_sock = ISC_TRUE;
3438 LOCK(&sock->lock);
3440 if (!SOCK_DEAD(sock)) {
3441 if (sock->connecting)
3442 dispatch_connect(sock);
3443 else
3444 dispatch_send(sock);
3446 unwatch_write = ISC_TRUE;
3448 if (unlock_sock)
3449 UNLOCK(&sock->lock);
3451 unlock_fd:
3452 UNLOCK(&manager->fdlock[lockid]);
3453 if (unwatch_read)
3454 (void)unwatch_fd(manager, fd, SELECT_POKE_READ);
3455 if (unwatch_write)
3456 (void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
3460 #ifdef USE_KQUEUE
3461 static isc_boolean_t
3462 process_fds(isc__socketmgr_t *manager, struct kevent *events, int nevents) {
3463 int i;
3464 isc_boolean_t readable, writable;
3465 isc_boolean_t done = ISC_FALSE;
3466 #ifdef USE_WATCHER_THREAD
3467 isc_boolean_t have_ctlevent = ISC_FALSE;
3468 #endif
3470 if (nevents == manager->nevents) {
3472 * This is not an error, but something unexpected. If this
3473 * happens, it may indicate the need for increasing
3474 * ISC_SOCKET_MAXEVENTS.
3476 manager_log(manager, ISC_LOGCATEGORY_GENERAL,
3477 ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
3478 "maximum number of FD events (%d) received",
3479 nevents);
3482 for (i = 0; i < nevents; i++) {
3483 REQUIRE(events[i].ident < manager->maxsocks);
3484 #ifdef USE_WATCHER_THREAD
3485 if (events[i].ident == (uintptr_t)manager->pipe_fds[0]) {
3486 have_ctlevent = ISC_TRUE;
3487 continue;
3489 #endif
3490 readable = ISC_TF(events[i].filter == EVFILT_READ);
3491 writable = ISC_TF(events[i].filter == EVFILT_WRITE);
3492 process_fd(manager, events[i].ident, readable, writable);
3495 #ifdef USE_WATCHER_THREAD
3496 if (have_ctlevent)
3497 done = process_ctlfd(manager);
3498 #endif
3500 return (done);
3502 #elif defined(USE_EPOLL)
3503 static isc_boolean_t
3504 process_fds(isc__socketmgr_t *manager, struct epoll_event *events, int nevents)
3506 int i;
3507 isc_boolean_t done = ISC_FALSE;
3508 #ifdef USE_WATCHER_THREAD
3509 isc_boolean_t have_ctlevent = ISC_FALSE;
3510 #endif
3512 if (nevents == manager->nevents) {
3513 manager_log(manager, ISC_LOGCATEGORY_GENERAL,
3514 ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
3515 "maximum number of FD events (%d) received",
3516 nevents);
3519 for (i = 0; i < nevents; i++) {
3520 REQUIRE(events[i].data.fd < (int)manager->maxsocks);
3521 #ifdef USE_WATCHER_THREAD
3522 if (events[i].data.fd == manager->pipe_fds[0]) {
3523 have_ctlevent = ISC_TRUE;
3524 continue;
3526 #endif
3527 if ((events[i].events & EPOLLERR) != 0 ||
3528 (events[i].events & EPOLLHUP) != 0) {
3530 * epoll does not set IN/OUT bits on an erroneous
3531 * condition, so we need to try both anyway. This is a
3532 * bit inefficient, but should be okay for such rare
3533 * events. Note also that the read or write attempt
3534 * won't block because we use non-blocking sockets.
3536 events[i].events |= (EPOLLIN | EPOLLOUT);
3538 process_fd(manager, events[i].data.fd,
3539 (events[i].events & EPOLLIN) != 0,
3540 (events[i].events & EPOLLOUT) != 0);
3543 #ifdef USE_WATCHER_THREAD
3544 if (have_ctlevent)
3545 done = process_ctlfd(manager);
3546 #endif
3548 return (done);
3550 #elif defined(USE_DEVPOLL)
3551 static isc_boolean_t
3552 process_fds(isc__socketmgr_t *manager, struct pollfd *events, int nevents) {
3553 int i;
3554 isc_boolean_t done = ISC_FALSE;
3555 #ifdef USE_WATCHER_THREAD
3556 isc_boolean_t have_ctlevent = ISC_FALSE;
3557 #endif
3559 if (nevents == manager->nevents) {
3560 manager_log(manager, ISC_LOGCATEGORY_GENERAL,
3561 ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
3562 "maximum number of FD events (%d) received",
3563 nevents);
3566 for (i = 0; i < nevents; i++) {
3567 REQUIRE(events[i].fd < (int)manager->maxsocks);
3568 #ifdef USE_WATCHER_THREAD
3569 if (events[i].fd == manager->pipe_fds[0]) {
3570 have_ctlevent = ISC_TRUE;
3571 continue;
3573 #endif
3574 process_fd(manager, events[i].fd,
3575 (events[i].events & POLLIN) != 0,
3576 (events[i].events & POLLOUT) != 0);
3579 #ifdef USE_WATCHER_THREAD
3580 if (have_ctlevent)
3581 done = process_ctlfd(manager);
3582 #endif
3584 return (done);
3586 #elif defined(USE_SELECT)
3587 static void
3588 process_fds(isc__socketmgr_t *manager, int maxfd, fd_set *readfds,
3589 fd_set *writefds)
3591 int i;
3593 REQUIRE(maxfd <= (int)manager->maxsocks);
3595 for (i = 0; i < maxfd; i++) {
3596 #ifdef USE_WATCHER_THREAD
3597 if (i == manager->pipe_fds[0] || i == manager->pipe_fds[1])
3598 continue;
3599 #endif /* USE_WATCHER_THREAD */
3600 process_fd(manager, i, FD_ISSET(i, readfds),
3601 FD_ISSET(i, writefds));
3604 #endif
3606 #ifdef USE_WATCHER_THREAD
3607 static isc_boolean_t
3608 process_ctlfd(isc__socketmgr_t *manager) {
3609 int msg, fd;
3611 for (;;) {
3612 select_readmsg(manager, &fd, &msg);
3614 manager_log(manager, IOEVENT,
3615 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
3616 ISC_MSG_WATCHERMSG,
3617 "watcher got message %d "
3618 "for socket %d"), msg, fd);
3621 * Nothing to read?
3623 if (msg == SELECT_POKE_NOTHING)
3624 break;
3627 * Handle shutdown message. We really should
3628 * jump out of this loop right away, but
3629 * it doesn't matter if we have to do a little
3630 * more work first.
3632 if (msg == SELECT_POKE_SHUTDOWN)
3633 return (ISC_TRUE);
3636 * This is a wakeup on a socket. Look
3637 * at the event queue for both read and write,
3638 * and decide if we need to watch on it now
3639 * or not.
3641 wakeup_socket(manager, fd, msg);
3644 return (ISC_FALSE);
3648 * This is the thread that will loop forever, always in a select or poll
3649 * call.
3651 * When select returns something to do, track down what thread gets to do
3652 * this I/O and post the event to it.
3654 static isc_threadresult_t
3655 watcher(void *uap) {
3656 isc__socketmgr_t *manager = uap;
3657 isc_boolean_t done;
3658 int ctlfd;
3659 int cc;
3660 #ifdef USE_KQUEUE
3661 const char *fnname = "kevent()";
3662 #elif defined (USE_EPOLL)
3663 const char *fnname = "epoll_wait()";
3664 #elif defined(USE_DEVPOLL)
3665 const char *fnname = "ioctl(DP_POLL)";
3666 struct dvpoll dvp;
3667 #elif defined (USE_SELECT)
3668 const char *fnname = "select()";
3669 int maxfd;
3670 #endif
3671 char strbuf[ISC_STRERRORSIZE];
3672 #ifdef ISC_SOCKET_USE_POLLWATCH
3673 pollstate_t pollstate = poll_idle;
3674 #endif
3677 * Get the control fd here. This will never change.
3679 ctlfd = manager->pipe_fds[0];
3680 done = ISC_FALSE;
3681 while (!done) {
3682 do {
3683 #ifdef USE_KQUEUE
3684 cc = kevent(manager->kqueue_fd, NULL, 0,
3685 manager->events, manager->nevents, NULL);
3686 #elif defined(USE_EPOLL)
3687 cc = epoll_wait(manager->epoll_fd, manager->events,
3688 manager->nevents, -1);
3689 #elif defined(USE_DEVPOLL)
3690 dvp.dp_fds = manager->events;
3691 dvp.dp_nfds = manager->nevents;
3692 #ifndef ISC_SOCKET_USE_POLLWATCH
3693 dvp.dp_timeout = -1;
3694 #else
3695 if (pollstate == poll_idle)
3696 dvp.dp_timeout = -1;
3697 else
3698 dvp.dp_timeout = ISC_SOCKET_POLLWATCH_TIMEOUT;
3699 #endif /* ISC_SOCKET_USE_POLLWATCH */
3700 cc = ioctl(manager->devpoll_fd, DP_POLL, &dvp);
3701 #elif defined(USE_SELECT)
3702 LOCK(&manager->lock);
3703 memcpy(manager->read_fds_copy, manager->read_fds,
3704 manager->fd_bufsize);
3705 memcpy(manager->write_fds_copy, manager->write_fds,
3706 manager->fd_bufsize);
3707 maxfd = manager->maxfd + 1;
3708 UNLOCK(&manager->lock);
3710 cc = select(maxfd, manager->read_fds_copy,
3711 manager->write_fds_copy, NULL, NULL);
3712 #endif /* USE_KQUEUE */
3714 if (cc < 0 && !SOFT_ERROR(errno)) {
3715 isc__strerror(errno, strbuf, sizeof(strbuf));
3716 FATAL_ERROR(__FILE__, __LINE__,
3717 "%s %s: %s", fnname,
3718 isc_msgcat_get(isc_msgcat,
3719 ISC_MSGSET_GENERAL,
3720 ISC_MSG_FAILED,
3721 "failed"), strbuf);
3724 #if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH)
3725 if (cc == 0) {
3726 if (pollstate == poll_active)
3727 pollstate = poll_checking;
3728 else if (pollstate == poll_checking)
3729 pollstate = poll_idle;
3730 } else if (cc > 0) {
3731 if (pollstate == poll_checking) {
3733 * XXX: We'd like to use a more
3734 * verbose log level as it's actually an
3735 * unexpected event, but the kernel bug
3736 * reportedly happens pretty frequently
3737 * (and it can also be a false positive)
3738 * so it would be just too noisy.
3740 manager_log(manager,
3741 ISC_LOGCATEGORY_GENERAL,
3742 ISC_LOGMODULE_SOCKET,
3743 ISC_LOG_DEBUG(1),
3744 "unexpected POLL timeout");
3746 pollstate = poll_active;
3748 #endif
3749 } while (cc < 0);
3751 #if defined(USE_KQUEUE) || defined (USE_EPOLL) || defined (USE_DEVPOLL)
3752 done = process_fds(manager, manager->events, cc);
3753 #elif defined(USE_SELECT)
3754 process_fds(manager, maxfd, manager->read_fds_copy,
3755 manager->write_fds_copy);
3758 * Process reads on internal, control fd.
3760 if (FD_ISSET(ctlfd, manager->read_fds_copy))
3761 done = process_ctlfd(manager);
3762 #endif
3765 manager_log(manager, TRACE, "%s",
3766 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3767 ISC_MSG_EXITING, "watcher exiting"));
3769 return ((isc_threadresult_t)0);
3771 #endif /* USE_WATCHER_THREAD */
3773 #ifdef BIND9
3774 ISC_SOCKETFUNC_SCOPE void
3775 isc__socketmgr_setreserved(isc_socketmgr_t *manager0, isc_uint32_t reserved) {
3776 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
3778 REQUIRE(VALID_MANAGER(manager));
3780 manager->reserved = reserved;
3783 ISC_SOCKETFUNC_SCOPE void
3784 isc___socketmgr_maxudp(isc_socketmgr_t *manager0, int maxudp) {
3785 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
3787 REQUIRE(VALID_MANAGER(manager));
3789 manager->maxudp = maxudp;
3791 #endif /* BIND9 */
3794 * Create a new socket manager.
3797 static isc_result_t
3798 setup_watcher(isc_mem_t *mctx, isc__socketmgr_t *manager) {
3799 isc_result_t result;
3800 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
3801 char strbuf[ISC_STRERRORSIZE];
3802 #endif
3804 #ifdef USE_KQUEUE
3805 manager->nevents = ISC_SOCKET_MAXEVENTS;
3806 manager->events = isc_mem_get(mctx, sizeof(struct kevent) *
3807 manager->nevents);
3808 if (manager->events == NULL)
3809 return (ISC_R_NOMEMORY);
3810 manager->kqueue_fd = kqueue();
3811 if (manager->kqueue_fd == -1) {
3812 result = isc__errno2result(errno);
3813 isc__strerror(errno, strbuf, sizeof(strbuf));
3814 UNEXPECTED_ERROR(__FILE__, __LINE__,
3815 "kqueue %s: %s",
3816 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3817 ISC_MSG_FAILED, "failed"),
3818 strbuf);
3819 isc_mem_put(mctx, manager->events,
3820 sizeof(struct kevent) * manager->nevents);
3821 return (result);
3824 #ifdef USE_WATCHER_THREAD
3825 result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
3826 if (result != ISC_R_SUCCESS) {
3827 close(manager->kqueue_fd);
3828 isc_mem_put(mctx, manager->events,
3829 sizeof(struct kevent) * manager->nevents);
3830 return (result);
3832 #endif /* USE_WATCHER_THREAD */
3833 #elif defined(USE_EPOLL)
3834 manager->nevents = ISC_SOCKET_MAXEVENTS;
3835 manager->events = isc_mem_get(mctx, sizeof(struct epoll_event) *
3836 manager->nevents);
3837 if (manager->events == NULL)
3838 return (ISC_R_NOMEMORY);
3839 manager->epoll_fd = epoll_create(manager->nevents);
3840 if (manager->epoll_fd == -1) {
3841 result = isc__errno2result(errno);
3842 isc__strerror(errno, strbuf, sizeof(strbuf));
3843 UNEXPECTED_ERROR(__FILE__, __LINE__,
3844 "epoll_create %s: %s",
3845 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3846 ISC_MSG_FAILED, "failed"),
3847 strbuf);
3848 isc_mem_put(mctx, manager->events,
3849 sizeof(struct epoll_event) * manager->nevents);
3850 return (result);
3852 #ifdef USE_WATCHER_THREAD
3853 result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
3854 if (result != ISC_R_SUCCESS) {
3855 close(manager->epoll_fd);
3856 isc_mem_put(mctx, manager->events,
3857 sizeof(struct epoll_event) * manager->nevents);
3858 return (result);
3860 #endif /* USE_WATCHER_THREAD */
3861 #elif defined(USE_DEVPOLL)
3863 * XXXJT: /dev/poll seems to reject large numbers of events,
3864 * so we should be careful about redefining ISC_SOCKET_MAXEVENTS.
3866 manager->nevents = ISC_SOCKET_MAXEVENTS;
3867 manager->events = isc_mem_get(mctx, sizeof(struct pollfd) *
3868 manager->nevents);
3869 if (manager->events == NULL)
3870 return (ISC_R_NOMEMORY);
3872 * Note: fdpollinfo should be able to support all possible FDs, so
3873 * it must have maxsocks entries (not nevents).
3875 manager->fdpollinfo = isc_mem_get(mctx, sizeof(pollinfo_t) *
3876 manager->maxsocks);
3877 if (manager->fdpollinfo == NULL) {
3878 isc_mem_put(mctx, manager->events,
3879 sizeof(struct pollfd) * manager->nevents);
3880 return (ISC_R_NOMEMORY);
3882 memset(manager->fdpollinfo, 0, sizeof(pollinfo_t) * manager->maxsocks);
3883 manager->devpoll_fd = open("/dev/poll", O_RDWR);
3884 if (manager->devpoll_fd == -1) {
3885 result = isc__errno2result(errno);
3886 isc__strerror(errno, strbuf, sizeof(strbuf));
3887 UNEXPECTED_ERROR(__FILE__, __LINE__,
3888 "open(/dev/poll) %s: %s",
3889 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3890 ISC_MSG_FAILED, "failed"),
3891 strbuf);
3892 isc_mem_put(mctx, manager->events,
3893 sizeof(struct pollfd) * manager->nevents);
3894 isc_mem_put(mctx, manager->fdpollinfo,
3895 sizeof(pollinfo_t) * manager->maxsocks);
3896 return (result);
3898 #ifdef USE_WATCHER_THREAD
3899 result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
3900 if (result != ISC_R_SUCCESS) {
3901 close(manager->devpoll_fd);
3902 isc_mem_put(mctx, manager->events,
3903 sizeof(struct pollfd) * manager->nevents);
3904 isc_mem_put(mctx, manager->fdpollinfo,
3905 sizeof(pollinfo_t) * manager->maxsocks);
3906 return (result);
3908 #endif /* USE_WATCHER_THREAD */
3909 #elif defined(USE_SELECT)
3910 UNUSED(result);
3912 #if ISC_SOCKET_MAXSOCKETS > FD_SETSIZE
3914 * Note: this code should also cover the case of MAXSOCKETS <=
3915 * FD_SETSIZE, but we separate the cases to avoid possible portability
3916 * issues regarding howmany() and the actual representation of fd_set.
3918 manager->fd_bufsize = howmany(manager->maxsocks, NFDBITS) *
3919 sizeof(fd_mask);
3920 #else
3921 manager->fd_bufsize = sizeof(fd_set);
3922 #endif
3924 manager->read_fds = NULL;
3925 manager->read_fds_copy = NULL;
3926 manager->write_fds = NULL;
3927 manager->write_fds_copy = NULL;
3929 manager->read_fds = isc_mem_get(mctx, manager->fd_bufsize);
3930 if (manager->read_fds != NULL)
3931 manager->read_fds_copy = isc_mem_get(mctx, manager->fd_bufsize);
3932 if (manager->read_fds_copy != NULL)
3933 manager->write_fds = isc_mem_get(mctx, manager->fd_bufsize);
3934 if (manager->write_fds != NULL) {
3935 manager->write_fds_copy = isc_mem_get(mctx,
3936 manager->fd_bufsize);
3938 if (manager->write_fds_copy == NULL) {
3939 if (manager->write_fds != NULL) {
3940 isc_mem_put(mctx, manager->write_fds,
3941 manager->fd_bufsize);
3943 if (manager->read_fds_copy != NULL) {
3944 isc_mem_put(mctx, manager->read_fds_copy,
3945 manager->fd_bufsize);
3947 if (manager->read_fds != NULL) {
3948 isc_mem_put(mctx, manager->read_fds,
3949 manager->fd_bufsize);
3951 return (ISC_R_NOMEMORY);
3953 memset(manager->read_fds, 0, manager->fd_bufsize);
3954 memset(manager->write_fds, 0, manager->fd_bufsize);
3956 #ifdef USE_WATCHER_THREAD
3957 (void)watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
3958 manager->maxfd = manager->pipe_fds[0];
3959 #else /* USE_WATCHER_THREAD */
3960 manager->maxfd = 0;
3961 #endif /* USE_WATCHER_THREAD */
3962 #endif /* USE_KQUEUE */
3964 return (ISC_R_SUCCESS);
3967 static void
3968 cleanup_watcher(isc_mem_t *mctx, isc__socketmgr_t *manager) {
3969 #ifdef USE_WATCHER_THREAD
3970 isc_result_t result;
3972 result = unwatch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
3973 if (result != ISC_R_SUCCESS) {
3974 UNEXPECTED_ERROR(__FILE__, __LINE__,
3975 "epoll_ctl(DEL) %s",
3976 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3977 ISC_MSG_FAILED, "failed"));
3979 #endif /* USE_WATCHER_THREAD */
3981 #ifdef USE_KQUEUE
3982 close(manager->kqueue_fd);
3983 isc_mem_put(mctx, manager->events,
3984 sizeof(struct kevent) * manager->nevents);
3985 #elif defined(USE_EPOLL)
3986 close(manager->epoll_fd);
3987 isc_mem_put(mctx, manager->events,
3988 sizeof(struct epoll_event) * manager->nevents);
3989 #elif defined(USE_DEVPOLL)
3990 close(manager->devpoll_fd);
3991 isc_mem_put(mctx, manager->events,
3992 sizeof(struct pollfd) * manager->nevents);
3993 isc_mem_put(mctx, manager->fdpollinfo,
3994 sizeof(pollinfo_t) * manager->maxsocks);
3995 #elif defined(USE_SELECT)
3996 if (manager->read_fds != NULL)
3997 isc_mem_put(mctx, manager->read_fds, manager->fd_bufsize);
3998 if (manager->read_fds_copy != NULL)
3999 isc_mem_put(mctx, manager->read_fds_copy, manager->fd_bufsize);
4000 if (manager->write_fds != NULL)
4001 isc_mem_put(mctx, manager->write_fds, manager->fd_bufsize);
4002 if (manager->write_fds_copy != NULL)
4003 isc_mem_put(mctx, manager->write_fds_copy, manager->fd_bufsize);
4004 #endif /* USE_KQUEUE */
4007 ISC_SOCKETFUNC_SCOPE isc_result_t
4008 isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
4009 return (isc__socketmgr_create2(mctx, managerp, 0));
4012 ISC_SOCKETFUNC_SCOPE isc_result_t
4013 isc__socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
4014 unsigned int maxsocks)
4016 int i;
4017 isc__socketmgr_t *manager;
4018 #ifdef USE_WATCHER_THREAD
4019 char strbuf[ISC_STRERRORSIZE];
4020 #endif
4021 isc_result_t result;
4023 REQUIRE(managerp != NULL && *managerp == NULL);
4025 #ifdef USE_SHARED_MANAGER
4026 if (socketmgr != NULL) {
4027 /* Don't allow maxsocks to be updated */
4028 if (maxsocks > 0 && socketmgr->maxsocks != maxsocks)
4029 return (ISC_R_EXISTS);
4031 socketmgr->refs++;
4032 *managerp = (isc_socketmgr_t *)socketmgr;
4033 return (ISC_R_SUCCESS);
4035 #endif /* USE_SHARED_MANAGER */
4037 if (maxsocks == 0)
4038 maxsocks = ISC_SOCKET_MAXSOCKETS;
4040 manager = isc_mem_get(mctx, sizeof(*manager));
4041 if (manager == NULL)
4042 return (ISC_R_NOMEMORY);
4044 /* zero-clear so that necessary cleanup on failure will be easy */
4045 memset(manager, 0, sizeof(*manager));
4046 manager->maxsocks = maxsocks;
4047 manager->reserved = 0;
4048 manager->maxudp = 0;
4049 manager->fds = isc_mem_get(mctx,
4050 manager->maxsocks * sizeof(isc__socket_t *));
4051 if (manager->fds == NULL) {
4052 result = ISC_R_NOMEMORY;
4053 goto free_manager;
4055 manager->fdstate = isc_mem_get(mctx, manager->maxsocks * sizeof(int));
4056 if (manager->fdstate == NULL) {
4057 result = ISC_R_NOMEMORY;
4058 goto free_manager;
4060 manager->stats = NULL;
4062 manager->common.methods = &socketmgrmethods;
4063 manager->common.magic = ISCAPI_SOCKETMGR_MAGIC;
4064 manager->common.impmagic = SOCKET_MANAGER_MAGIC;
4065 manager->mctx = NULL;
4066 memset(manager->fds, 0, manager->maxsocks * sizeof(isc_socket_t *));
4067 ISC_LIST_INIT(manager->socklist);
4068 result = isc_mutex_init(&manager->lock);
4069 if (result != ISC_R_SUCCESS)
4070 goto free_manager;
4071 manager->fdlock = isc_mem_get(mctx, FDLOCK_COUNT * sizeof(isc_mutex_t));
4072 if (manager->fdlock == NULL) {
4073 result = ISC_R_NOMEMORY;
4074 goto cleanup_lock;
4076 for (i = 0; i < FDLOCK_COUNT; i++) {
4077 result = isc_mutex_init(&manager->fdlock[i]);
4078 if (result != ISC_R_SUCCESS) {
4079 while (--i >= 0)
4080 DESTROYLOCK(&manager->fdlock[i]);
4081 isc_mem_put(mctx, manager->fdlock,
4082 FDLOCK_COUNT * sizeof(isc_mutex_t));
4083 manager->fdlock = NULL;
4084 goto cleanup_lock;
4088 #ifdef USE_WATCHER_THREAD
4089 if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
4090 UNEXPECTED_ERROR(__FILE__, __LINE__,
4091 "isc_condition_init() %s",
4092 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4093 ISC_MSG_FAILED, "failed"));
4094 result = ISC_R_UNEXPECTED;
4095 goto cleanup_lock;
4099 * Create the special fds that will be used to wake up the
4100 * select/poll loop when something internal needs to be done.
4102 if (pipe(manager->pipe_fds) != 0) {
4103 isc__strerror(errno, strbuf, sizeof(strbuf));
4104 UNEXPECTED_ERROR(__FILE__, __LINE__,
4105 "pipe() %s: %s",
4106 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4107 ISC_MSG_FAILED, "failed"),
4108 strbuf);
4109 result = ISC_R_UNEXPECTED;
4110 goto cleanup_condition;
4113 RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
4114 #if 0
4115 RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
4116 #endif
4117 #endif /* USE_WATCHER_THREAD */
4119 #ifdef USE_SHARED_MANAGER
4120 manager->refs = 1;
4121 #endif /* USE_SHARED_MANAGER */
4124 * Set up initial state for the select loop
4126 result = setup_watcher(mctx, manager);
4127 if (result != ISC_R_SUCCESS)
4128 goto cleanup;
4129 memset(manager->fdstate, 0, manager->maxsocks * sizeof(int));
4130 #ifdef USE_WATCHER_THREAD
4132 * Start up the select/poll thread.
4134 if (isc_thread_create(watcher, manager, &manager->watcher) !=
4135 ISC_R_SUCCESS) {
4136 UNEXPECTED_ERROR(__FILE__, __LINE__,
4137 "isc_thread_create() %s",
4138 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4139 ISC_MSG_FAILED, "failed"));
4140 cleanup_watcher(mctx, manager);
4141 result = ISC_R_UNEXPECTED;
4142 goto cleanup;
4144 #endif /* USE_WATCHER_THREAD */
4145 isc_mem_attach(mctx, &manager->mctx);
4147 #ifdef USE_SHARED_MANAGER
4148 socketmgr = manager;
4149 #endif /* USE_SHARED_MANAGER */
4150 *managerp = (isc_socketmgr_t *)manager;
4152 return (ISC_R_SUCCESS);
4154 cleanup:
4155 #ifdef USE_WATCHER_THREAD
4156 (void)close(manager->pipe_fds[0]);
4157 (void)close(manager->pipe_fds[1]);
4158 #endif /* USE_WATCHER_THREAD */
4160 #ifdef USE_WATCHER_THREAD
4161 cleanup_condition:
4162 (void)isc_condition_destroy(&manager->shutdown_ok);
4163 #endif /* USE_WATCHER_THREAD */
4166 cleanup_lock:
4167 if (manager->fdlock != NULL) {
4168 for (i = 0; i < FDLOCK_COUNT; i++)
4169 DESTROYLOCK(&manager->fdlock[i]);
4171 DESTROYLOCK(&manager->lock);
4173 free_manager:
4174 if (manager->fdlock != NULL) {
4175 isc_mem_put(mctx, manager->fdlock,
4176 FDLOCK_COUNT * sizeof(isc_mutex_t));
4178 if (manager->fdstate != NULL) {
4179 isc_mem_put(mctx, manager->fdstate,
4180 manager->maxsocks * sizeof(int));
4182 if (manager->fds != NULL) {
4183 isc_mem_put(mctx, manager->fds,
4184 manager->maxsocks * sizeof(isc_socket_t *));
4186 isc_mem_put(mctx, manager, sizeof(*manager));
4188 return (result);
4191 #ifdef BIND9
4192 isc_result_t
4193 isc__socketmgr_getmaxsockets(isc_socketmgr_t *manager0, unsigned int *nsockp) {
4194 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
4195 REQUIRE(VALID_MANAGER(manager));
4196 REQUIRE(nsockp != NULL);
4198 *nsockp = manager->maxsocks;
4200 return (ISC_R_SUCCESS);
4203 void
4204 isc__socketmgr_setstats(isc_socketmgr_t *manager0, isc_stats_t *stats) {
4205 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
4207 REQUIRE(VALID_MANAGER(manager));
4208 REQUIRE(ISC_LIST_EMPTY(manager->socklist));
4209 REQUIRE(manager->stats == NULL);
4210 REQUIRE(isc_stats_ncounters(stats) == isc_sockstatscounter_max);
4212 isc_stats_attach(stats, &manager->stats);
4214 #endif
4216 ISC_SOCKETFUNC_SCOPE void
4217 isc__socketmgr_destroy(isc_socketmgr_t **managerp) {
4218 isc__socketmgr_t *manager;
4219 int i;
4220 isc_mem_t *mctx;
4223 * Destroy a socket manager.
4226 REQUIRE(managerp != NULL);
4227 manager = (isc__socketmgr_t *)*managerp;
4228 REQUIRE(VALID_MANAGER(manager));
4230 #ifdef USE_SHARED_MANAGER
4231 if (manager->refs > 1) {
4232 manager->refs--;
4233 *managerp = NULL;
4234 return;
4236 #endif /* USE_SHARED_MANAGER */
4238 LOCK(&manager->lock);
4240 #ifdef USE_WATCHER_THREAD
4242 * Wait for all sockets to be destroyed.
4244 while (!ISC_LIST_EMPTY(manager->socklist)) {
4245 manager_log(manager, CREATION, "%s",
4246 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
4247 ISC_MSG_SOCKETSREMAIN,
4248 "sockets exist"));
4249 WAIT(&manager->shutdown_ok, &manager->lock);
4251 #else /* USE_WATCHER_THREAD */
4253 * Hope all sockets have been destroyed.
4255 if (!ISC_LIST_EMPTY(manager->socklist)) {
4256 manager_log(manager, CREATION, "%s",
4257 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
4258 ISC_MSG_SOCKETSREMAIN,
4259 "sockets exist"));
4260 INSIST(0);
4262 #endif /* USE_WATCHER_THREAD */
4264 UNLOCK(&manager->lock);
4267 * Here, poke our select/poll thread. Do this by closing the write
4268 * half of the pipe, which will send EOF to the read half.
4269 * This is currently a no-op in the non-threaded case.
4271 select_poke(manager, 0, SELECT_POKE_SHUTDOWN);
4273 #ifdef USE_WATCHER_THREAD
4275 * Wait for thread to exit.
4277 if (isc_thread_join(manager->watcher, NULL) != ISC_R_SUCCESS)
4278 UNEXPECTED_ERROR(__FILE__, __LINE__,
4279 "isc_thread_join() %s",
4280 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4281 ISC_MSG_FAILED, "failed"));
4282 #endif /* USE_WATCHER_THREAD */
4285 * Clean up.
4287 cleanup_watcher(manager->mctx, manager);
4289 #ifdef USE_WATCHER_THREAD
4290 (void)close(manager->pipe_fds[0]);
4291 (void)close(manager->pipe_fds[1]);
4292 (void)isc_condition_destroy(&manager->shutdown_ok);
4293 #endif /* USE_WATCHER_THREAD */
4295 for (i = 0; i < (int)manager->maxsocks; i++)
4296 if (manager->fdstate[i] == CLOSE_PENDING) /* no need to lock */
4297 (void)close(i);
4299 isc_mem_put(manager->mctx, manager->fds,
4300 manager->maxsocks * sizeof(isc__socket_t *));
4301 isc_mem_put(manager->mctx, manager->fdstate,
4302 manager->maxsocks * sizeof(int));
4304 if (manager->stats != NULL)
4305 isc_stats_detach(&manager->stats);
4307 if (manager->fdlock != NULL) {
4308 for (i = 0; i < FDLOCK_COUNT; i++)
4309 DESTROYLOCK(&manager->fdlock[i]);
4310 isc_mem_put(manager->mctx, manager->fdlock,
4311 FDLOCK_COUNT * sizeof(isc_mutex_t));
4313 DESTROYLOCK(&manager->lock);
4314 manager->common.magic = 0;
4315 manager->common.impmagic = 0;
4316 mctx= manager->mctx;
4317 isc_mem_put(mctx, manager, sizeof(*manager));
4319 isc_mem_detach(&mctx);
4321 *managerp = NULL;
4323 #ifdef USE_SHARED_MANAGER
4324 socketmgr = NULL;
4325 #endif
4328 static isc_result_t
4329 socket_recv(isc__socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
4330 unsigned int flags)
4332 int io_state;
4333 isc_boolean_t have_lock = ISC_FALSE;
4334 isc_task_t *ntask = NULL;
4335 isc_result_t result = ISC_R_SUCCESS;
4337 dev->ev_sender = task;
4339 if (sock->type == isc_sockettype_udp) {
4340 io_state = doio_recv(sock, dev);
4341 } else {
4342 LOCK(&sock->lock);
4343 have_lock = ISC_TRUE;
4345 if (ISC_LIST_EMPTY(sock->recv_list))
4346 io_state = doio_recv(sock, dev);
4347 else
4348 io_state = DOIO_SOFT;
4351 switch (io_state) {
4352 case DOIO_SOFT:
4354 * We couldn't read all or part of the request right now, so
4355 * queue it.
4357 * Attach to socket and to task
4359 isc_task_attach(task, &ntask);
4360 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
4362 if (!have_lock) {
4363 LOCK(&sock->lock);
4364 have_lock = ISC_TRUE;
4368 * Enqueue the request. If the socket was previously not being
4369 * watched, poke the watcher to start paying attention to it.
4371 if (ISC_LIST_EMPTY(sock->recv_list) && !sock->pending_recv)
4372 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
4373 ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
4375 socket_log(sock, NULL, EVENT, NULL, 0, 0,
4376 "socket_recv: event %p -> task %p",
4377 dev, ntask);
4379 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
4380 result = ISC_R_INPROGRESS;
4381 break;
4383 case DOIO_EOF:
4384 dev->result = ISC_R_EOF;
4385 /* fallthrough */
4387 case DOIO_HARD:
4388 case DOIO_SUCCESS:
4389 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
4390 send_recvdone_event(sock, &dev);
4391 break;
4394 if (have_lock)
4395 UNLOCK(&sock->lock);
4397 return (result);
4400 ISC_SOCKETFUNC_SCOPE isc_result_t
4401 isc__socket_recvv(isc_socket_t *sock0, isc_bufferlist_t *buflist,
4402 unsigned int minimum, isc_task_t *task,
4403 isc_taskaction_t action, const void *arg)
4405 isc__socket_t *sock = (isc__socket_t *)sock0;
4406 isc_socketevent_t *dev;
4407 isc__socketmgr_t *manager;
4408 unsigned int iocount;
4409 isc_buffer_t *buffer;
4411 REQUIRE(VALID_SOCKET(sock));
4412 REQUIRE(buflist != NULL);
4413 REQUIRE(!ISC_LIST_EMPTY(*buflist));
4414 REQUIRE(task != NULL);
4415 REQUIRE(action != NULL);
4417 manager = sock->manager;
4418 REQUIRE(VALID_MANAGER(manager));
4420 iocount = isc_bufferlist_availablecount(buflist);
4421 REQUIRE(iocount > 0);
4423 INSIST(sock->bound);
4425 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
4426 if (dev == NULL) {
4427 return (ISC_R_NOMEMORY);
4431 * UDP sockets are always partial read
4433 if (sock->type == isc_sockettype_udp)
4434 dev->minimum = 1;
4435 else {
4436 if (minimum == 0)
4437 dev->minimum = iocount;
4438 else
4439 dev->minimum = minimum;
4443 * Move each buffer from the passed in list to our internal one.
4445 buffer = ISC_LIST_HEAD(*buflist);
4446 while (buffer != NULL) {
4447 ISC_LIST_DEQUEUE(*buflist, buffer, link);
4448 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
4449 buffer = ISC_LIST_HEAD(*buflist);
4452 return (socket_recv(sock, dev, task, 0));
4455 ISC_SOCKETFUNC_SCOPE isc_result_t
4456 isc__socket_recv(isc_socket_t *sock0, isc_region_t *region,
4457 unsigned int minimum, isc_task_t *task,
4458 isc_taskaction_t action, const void *arg)
4460 isc__socket_t *sock = (isc__socket_t *)sock0;
4461 isc_socketevent_t *dev;
4462 isc__socketmgr_t *manager;
4464 REQUIRE(VALID_SOCKET(sock));
4465 REQUIRE(action != NULL);
4467 manager = sock->manager;
4468 REQUIRE(VALID_MANAGER(manager));
4470 INSIST(sock->bound);
4472 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
4473 if (dev == NULL)
4474 return (ISC_R_NOMEMORY);
4476 return (isc__socket_recv2(sock0, region, minimum, task, dev, 0));
4479 ISC_SOCKETFUNC_SCOPE isc_result_t
4480 isc__socket_recv2(isc_socket_t *sock0, isc_region_t *region,
4481 unsigned int minimum, isc_task_t *task,
4482 isc_socketevent_t *event, unsigned int flags)
4484 isc__socket_t *sock = (isc__socket_t *)sock0;
4486 event->ev_sender = sock;
4487 event->result = ISC_R_UNEXPECTED;
4488 ISC_LIST_INIT(event->bufferlist);
4489 event->region = *region;
4490 event->n = 0;
4491 event->offset = 0;
4492 event->attributes = 0;
4495 * UDP sockets are always partial read.
4497 if (sock->type == isc_sockettype_udp)
4498 event->minimum = 1;
4499 else {
4500 if (minimum == 0)
4501 event->minimum = region->length;
4502 else
4503 event->minimum = minimum;
4506 return (socket_recv(sock, event, task, flags));
4509 static isc_result_t
4510 socket_send(isc__socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
4511 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
4512 unsigned int flags)
4514 int io_state;
4515 isc_boolean_t have_lock = ISC_FALSE;
4516 isc_task_t *ntask = NULL;
4517 isc_result_t result = ISC_R_SUCCESS;
4519 dev->ev_sender = task;
4521 set_dev_address(address, sock, dev);
4522 if (pktinfo != NULL) {
4523 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
4524 dev->pktinfo = *pktinfo;
4526 if (!isc_sockaddr_issitelocal(&dev->address) &&
4527 !isc_sockaddr_islinklocal(&dev->address)) {
4528 socket_log(sock, NULL, TRACE, isc_msgcat,
4529 ISC_MSGSET_SOCKET, ISC_MSG_PKTINFOPROVIDED,
4530 "pktinfo structure provided, ifindex %u "
4531 "(set to 0)", pktinfo->ipi6_ifindex);
4534 * Set the pktinfo index to 0 here, to let the
4535 * kernel decide what interface it should send on.
4537 dev->pktinfo.ipi6_ifindex = 0;
4541 if (sock->type == isc_sockettype_udp)
4542 io_state = doio_send(sock, dev);
4543 else {
4544 LOCK(&sock->lock);
4545 have_lock = ISC_TRUE;
4547 if (ISC_LIST_EMPTY(sock->send_list))
4548 io_state = doio_send(sock, dev);
4549 else
4550 io_state = DOIO_SOFT;
4553 switch (io_state) {
4554 case DOIO_SOFT:
4556 * We couldn't send all or part of the request right now, so
4557 * queue it unless ISC_SOCKFLAG_NORETRY is set.
4559 if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
4560 isc_task_attach(task, &ntask);
4561 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
4563 if (!have_lock) {
4564 LOCK(&sock->lock);
4565 have_lock = ISC_TRUE;
4569 * Enqueue the request. If the socket was previously
4570 * not being watched, poke the watcher to start
4571 * paying attention to it.
4573 if (ISC_LIST_EMPTY(sock->send_list) &&
4574 !sock->pending_send)
4575 select_poke(sock->manager, sock->fd,
4576 SELECT_POKE_WRITE);
4577 ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
4579 socket_log(sock, NULL, EVENT, NULL, 0, 0,
4580 "socket_send: event %p -> task %p",
4581 dev, ntask);
4583 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
4584 result = ISC_R_INPROGRESS;
4585 break;
4588 case DOIO_HARD:
4589 case DOIO_SUCCESS:
4590 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
4591 send_senddone_event(sock, &dev);
4592 break;
4595 if (have_lock)
4596 UNLOCK(&sock->lock);
4598 return (result);
4601 ISC_SOCKETFUNC_SCOPE isc_result_t
4602 isc__socket_send(isc_socket_t *sock, isc_region_t *region,
4603 isc_task_t *task, isc_taskaction_t action, const void *arg)
4606 * REQUIRE() checking is performed in isc_socket_sendto().
4608 return (isc__socket_sendto(sock, region, task, action, arg, NULL,
4609 NULL));
4612 ISC_SOCKETFUNC_SCOPE isc_result_t
4613 isc__socket_sendto(isc_socket_t *sock0, isc_region_t *region,
4614 isc_task_t *task, isc_taskaction_t action, const void *arg,
4615 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
4617 isc__socket_t *sock = (isc__socket_t *)sock0;
4618 isc_socketevent_t *dev;
4619 isc__socketmgr_t *manager;
4621 REQUIRE(VALID_SOCKET(sock));
4622 REQUIRE(region != NULL);
4623 REQUIRE(task != NULL);
4624 REQUIRE(action != NULL);
4626 manager = sock->manager;
4627 REQUIRE(VALID_MANAGER(manager));
4629 INSIST(sock->bound);
4631 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
4632 if (dev == NULL) {
4633 return (ISC_R_NOMEMORY);
4636 dev->region = *region;
4638 return (socket_send(sock, dev, task, address, pktinfo, 0));
4641 ISC_SOCKETFUNC_SCOPE isc_result_t
4642 isc__socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
4643 isc_task_t *task, isc_taskaction_t action, const void *arg)
4645 return (isc__socket_sendtov(sock, buflist, task, action, arg, NULL,
4646 NULL));
4649 ISC_SOCKETFUNC_SCOPE isc_result_t
4650 isc__socket_sendtov(isc_socket_t *sock0, isc_bufferlist_t *buflist,
4651 isc_task_t *task, isc_taskaction_t action, const void *arg,
4652 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
4654 isc__socket_t *sock = (isc__socket_t *)sock0;
4655 isc_socketevent_t *dev;
4656 isc__socketmgr_t *manager;
4657 unsigned int iocount;
4658 isc_buffer_t *buffer;
4660 REQUIRE(VALID_SOCKET(sock));
4661 REQUIRE(buflist != NULL);
4662 REQUIRE(!ISC_LIST_EMPTY(*buflist));
4663 REQUIRE(task != NULL);
4664 REQUIRE(action != NULL);
4666 manager = sock->manager;
4667 REQUIRE(VALID_MANAGER(manager));
4669 iocount = isc_bufferlist_usedcount(buflist);
4670 REQUIRE(iocount > 0);
4672 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
4673 if (dev == NULL) {
4674 return (ISC_R_NOMEMORY);
4678 * Move each buffer from the passed in list to our internal one.
4680 buffer = ISC_LIST_HEAD(*buflist);
4681 while (buffer != NULL) {
4682 ISC_LIST_DEQUEUE(*buflist, buffer, link);
4683 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
4684 buffer = ISC_LIST_HEAD(*buflist);
4687 return (socket_send(sock, dev, task, address, pktinfo, 0));
4690 ISC_SOCKETFUNC_SCOPE isc_result_t
4691 isc__socket_sendto2(isc_socket_t *sock0, isc_region_t *region,
4692 isc_task_t *task,
4693 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
4694 isc_socketevent_t *event, unsigned int flags)
4696 isc__socket_t *sock = (isc__socket_t *)sock0;
4698 REQUIRE(VALID_SOCKET(sock));
4699 REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
4700 if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
4701 REQUIRE(sock->type == isc_sockettype_udp);
4702 event->ev_sender = sock;
4703 event->result = ISC_R_UNEXPECTED;
4704 ISC_LIST_INIT(event->bufferlist);
4705 event->region = *region;
4706 event->n = 0;
4707 event->offset = 0;
4708 event->attributes = 0;
4710 return (socket_send(sock, event, task, address, pktinfo, flags));
4713 ISC_SOCKETFUNC_SCOPE void
4714 isc__socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
4715 #ifdef ISC_PLATFORM_HAVESYSUNH
4716 int s;
4717 struct stat sb;
4718 char strbuf[ISC_STRERRORSIZE];
4720 if (sockaddr->type.sa.sa_family != AF_UNIX)
4721 return;
4723 #ifndef S_ISSOCK
4724 #if defined(S_IFMT) && defined(S_IFSOCK)
4725 #define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
4726 #elif defined(_S_IFMT) && defined(S_IFSOCK)
4727 #define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
4728 #endif
4729 #endif
4731 #ifndef S_ISFIFO
4732 #if defined(S_IFMT) && defined(S_IFIFO)
4733 #define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
4734 #elif defined(_S_IFMT) && defined(S_IFIFO)
4735 #define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
4736 #endif
4737 #endif
4739 #if !defined(S_ISFIFO) && !defined(S_ISSOCK)
4740 #error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
4741 #endif
4743 #ifndef S_ISFIFO
4744 #define S_ISFIFO(mode) 0
4745 #endif
4747 #ifndef S_ISSOCK
4748 #define S_ISSOCK(mode) 0
4749 #endif
4751 if (active) {
4752 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
4753 isc__strerror(errno, strbuf, sizeof(strbuf));
4754 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4755 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4756 "isc_socket_cleanunix: stat(%s): %s",
4757 sockaddr->type.sunix.sun_path, strbuf);
4758 return;
4760 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
4761 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4762 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4763 "isc_socket_cleanunix: %s: not a socket",
4764 sockaddr->type.sunix.sun_path);
4765 return;
4767 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
4768 isc__strerror(errno, strbuf, sizeof(strbuf));
4769 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4770 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4771 "isc_socket_cleanunix: unlink(%s): %s",
4772 sockaddr->type.sunix.sun_path, strbuf);
4774 return;
4777 s = socket(AF_UNIX, SOCK_STREAM, 0);
4778 if (s < 0) {
4779 isc__strerror(errno, strbuf, sizeof(strbuf));
4780 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4781 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4782 "isc_socket_cleanunix: socket(%s): %s",
4783 sockaddr->type.sunix.sun_path, strbuf);
4784 return;
4787 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
4788 switch (errno) {
4789 case ENOENT: /* We exited cleanly last time */
4790 break;
4791 default:
4792 isc__strerror(errno, strbuf, sizeof(strbuf));
4793 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4794 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4795 "isc_socket_cleanunix: stat(%s): %s",
4796 sockaddr->type.sunix.sun_path, strbuf);
4797 break;
4799 goto cleanup;
4802 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
4803 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4804 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4805 "isc_socket_cleanunix: %s: not a socket",
4806 sockaddr->type.sunix.sun_path);
4807 goto cleanup;
4810 if (connect(s, (struct sockaddr *)&sockaddr->type.sunix,
4811 sizeof(sockaddr->type.sunix)) < 0) {
4812 switch (errno) {
4813 case ECONNREFUSED:
4814 case ECONNRESET:
4815 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
4816 isc__strerror(errno, strbuf, sizeof(strbuf));
4817 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4818 ISC_LOGMODULE_SOCKET,
4819 ISC_LOG_WARNING,
4820 "isc_socket_cleanunix: "
4821 "unlink(%s): %s",
4822 sockaddr->type.sunix.sun_path,
4823 strbuf);
4825 break;
4826 default:
4827 isc__strerror(errno, strbuf, sizeof(strbuf));
4828 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4829 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
4830 "isc_socket_cleanunix: connect(%s): %s",
4831 sockaddr->type.sunix.sun_path, strbuf);
4832 break;
4835 cleanup:
4836 close(s);
4837 #else
4838 UNUSED(sockaddr);
4839 UNUSED(active);
4840 #endif
4843 ISC_SOCKETFUNC_SCOPE isc_result_t
4844 isc__socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
4845 isc_uint32_t owner, isc_uint32_t group)
4847 #ifdef ISC_PLATFORM_HAVESYSUNH
4848 isc_result_t result = ISC_R_SUCCESS;
4849 char strbuf[ISC_STRERRORSIZE];
4850 char path[sizeof(sockaddr->type.sunix.sun_path)];
4851 #ifdef NEED_SECURE_DIRECTORY
4852 char *slash;
4853 #endif
4855 REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
4856 INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
4857 strcpy(path, sockaddr->type.sunix.sun_path);
4859 #ifdef NEED_SECURE_DIRECTORY
4860 slash = strrchr(path, '/');
4861 if (slash != NULL) {
4862 if (slash != path)
4863 *slash = '\0';
4864 else
4865 strcpy(path, "/");
4866 } else
4867 strcpy(path, ".");
4868 #endif
4870 if (chmod(path, perm) < 0) {
4871 isc__strerror(errno, strbuf, sizeof(strbuf));
4872 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4873 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4874 "isc_socket_permunix: chmod(%s, %d): %s",
4875 path, perm, strbuf);
4876 result = ISC_R_FAILURE;
4878 if (chown(path, owner, group) < 0) {
4879 isc__strerror(errno, strbuf, sizeof(strbuf));
4880 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
4881 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
4882 "isc_socket_permunix: chown(%s, %d, %d): %s",
4883 path, owner, group,
4884 strbuf);
4885 result = ISC_R_FAILURE;
4887 return (result);
4888 #else
4889 UNUSED(sockaddr);
4890 UNUSED(perm);
4891 UNUSED(owner);
4892 UNUSED(group);
4893 return (ISC_R_NOTIMPLEMENTED);
4894 #endif
4897 ISC_SOCKETFUNC_SCOPE isc_result_t
4898 isc__socket_bind(isc_socket_t *sock0, isc_sockaddr_t *sockaddr,
4899 unsigned int options) {
4900 isc__socket_t *sock = (isc__socket_t *)sock0;
4901 char strbuf[ISC_STRERRORSIZE];
4902 int on = 1;
4904 REQUIRE(VALID_SOCKET(sock));
4906 LOCK(&sock->lock);
4908 INSIST(!sock->bound);
4910 if (sock->pf != sockaddr->type.sa.sa_family) {
4911 UNLOCK(&sock->lock);
4912 return (ISC_R_FAMILYMISMATCH);
4915 * Only set SO_REUSEADDR when we want a specific port.
4917 #ifdef AF_UNIX
4918 if (sock->pf == AF_UNIX)
4919 goto bind_socket;
4920 #endif
4921 if ((options & ISC_SOCKET_REUSEADDRESS) != 0 &&
4922 isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
4923 setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
4924 sizeof(on)) < 0) {
4925 UNEXPECTED_ERROR(__FILE__, __LINE__,
4926 "setsockopt(%d) %s", sock->fd,
4927 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
4928 ISC_MSG_FAILED, "failed"));
4929 /* Press on... */
4931 #ifdef AF_UNIX
4932 bind_socket:
4933 #endif
4934 if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
4935 inc_stats(sock->manager->stats,
4936 sock->statsindex[STATID_BINDFAIL]);
4938 UNLOCK(&sock->lock);
4939 switch (errno) {
4940 case EACCES:
4941 return (ISC_R_NOPERM);
4942 case EADDRNOTAVAIL:
4943 return (ISC_R_ADDRNOTAVAIL);
4944 case EADDRINUSE:
4945 return (ISC_R_ADDRINUSE);
4946 case EINVAL:
4947 return (ISC_R_BOUND);
4948 default:
4949 isc__strerror(errno, strbuf, sizeof(strbuf));
4950 UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
4951 strbuf);
4952 return (ISC_R_UNEXPECTED);
4956 socket_log(sock, sockaddr, TRACE,
4957 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
4958 sock->bound = 1;
4960 UNLOCK(&sock->lock);
4961 return (ISC_R_SUCCESS);
4964 ISC_SOCKETFUNC_SCOPE isc_result_t
4965 isc__socket_filter(isc_socket_t *sock0, const char *filter) {
4966 isc__socket_t *sock = (isc__socket_t *)sock0;
4967 #ifdef SO_ACCEPTFILTER
4968 char strbuf[ISC_STRERRORSIZE];
4969 struct accept_filter_arg afa;
4970 #else
4971 UNUSED(sock);
4972 UNUSED(filter);
4973 #endif
4975 REQUIRE(VALID_SOCKET(sock));
4977 #ifdef SO_ACCEPTFILTER
4978 bzero(&afa, sizeof(afa));
4979 strncpy(afa.af_name, filter, sizeof(afa.af_name));
4980 if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
4981 &afa, sizeof(afa)) == -1) {
4982 isc__strerror(errno, strbuf, sizeof(strbuf));
4983 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
4984 ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
4985 strbuf);
4986 return (ISC_R_FAILURE);
4988 return (ISC_R_SUCCESS);
4989 #else
4990 return (ISC_R_NOTIMPLEMENTED);
4991 #endif
4995 * Set up to listen on a given socket. We do this by creating an internal
4996 * event that will be dispatched when the socket has read activity. The
4997 * watcher will send the internal event to the task when there is a new
4998 * connection.
5000 * Unlike in read, we don't preallocate a done event here. Every time there
5001 * is a new connection we'll have to allocate a new one anyway, so we might
5002 * as well keep things simple rather than having to track them.
5004 ISC_SOCKETFUNC_SCOPE isc_result_t
5005 isc__socket_listen(isc_socket_t *sock0, unsigned int backlog) {
5006 isc__socket_t *sock = (isc__socket_t *)sock0;
5007 char strbuf[ISC_STRERRORSIZE];
5009 REQUIRE(VALID_SOCKET(sock));
5011 LOCK(&sock->lock);
5013 REQUIRE(!sock->listener);
5014 REQUIRE(sock->bound);
5015 REQUIRE(sock->type == isc_sockettype_tcp ||
5016 sock->type == isc_sockettype_unix);
5018 if (backlog == 0)
5019 backlog = SOMAXCONN;
5021 if (listen(sock->fd, (int)backlog) < 0) {
5022 UNLOCK(&sock->lock);
5023 isc__strerror(errno, strbuf, sizeof(strbuf));
5025 UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
5027 return (ISC_R_UNEXPECTED);
5030 sock->listener = 1;
5032 UNLOCK(&sock->lock);
5033 return (ISC_R_SUCCESS);
5037 * This should try to do aggressive accept() XXXMLG
5039 ISC_SOCKETFUNC_SCOPE isc_result_t
5040 isc__socket_accept(isc_socket_t *sock0,
5041 isc_task_t *task, isc_taskaction_t action, const void *arg)
5043 isc__socket_t *sock = (isc__socket_t *)sock0;
5044 isc_socket_newconnev_t *dev;
5045 isc__socketmgr_t *manager;
5046 isc_task_t *ntask = NULL;
5047 isc__socket_t *nsock;
5048 isc_result_t result;
5049 isc_boolean_t do_poke = ISC_FALSE;
5051 REQUIRE(VALID_SOCKET(sock));
5052 manager = sock->manager;
5053 REQUIRE(VALID_MANAGER(manager));
5055 LOCK(&sock->lock);
5057 REQUIRE(sock->listener);
5060 * Sender field is overloaded here with the task we will be sending
5061 * this event to. Just before the actual event is delivered the
5062 * actual ev_sender will be touched up to be the socket.
5064 dev = (isc_socket_newconnev_t *)
5065 isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
5066 action, arg, sizeof(*dev));
5067 if (dev == NULL) {
5068 UNLOCK(&sock->lock);
5069 return (ISC_R_NOMEMORY);
5071 ISC_LINK_INIT(dev, ev_link);
5073 result = allocate_socket(manager, sock->type, &nsock);
5074 if (result != ISC_R_SUCCESS) {
5075 isc_event_free(ISC_EVENT_PTR(&dev));
5076 UNLOCK(&sock->lock);
5077 return (result);
5081 * Attach to socket and to task.
5083 isc_task_attach(task, &ntask);
5084 nsock->references++;
5085 nsock->statsindex = sock->statsindex;
5087 dev->ev_sender = ntask;
5088 dev->newsocket = (isc_socket_t *)nsock;
5091 * Poke watcher here. We still have the socket locked, so there
5092 * is no race condition. We will keep the lock for such a short
5093 * bit of time waking it up now or later won't matter all that much.
5095 if (ISC_LIST_EMPTY(sock->accept_list))
5096 do_poke = ISC_TRUE;
5098 ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
5100 if (do_poke)
5101 select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
5103 UNLOCK(&sock->lock);
5104 return (ISC_R_SUCCESS);
5107 ISC_SOCKETFUNC_SCOPE isc_result_t
5108 isc__socket_connect(isc_socket_t *sock0, isc_sockaddr_t *addr,
5109 isc_task_t *task, isc_taskaction_t action, const void *arg)
5111 isc__socket_t *sock = (isc__socket_t *)sock0;
5112 isc_socket_connev_t *dev;
5113 isc_task_t *ntask = NULL;
5114 isc__socketmgr_t *manager;
5115 int cc;
5116 char strbuf[ISC_STRERRORSIZE];
5117 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
5119 REQUIRE(VALID_SOCKET(sock));
5120 REQUIRE(addr != NULL);
5121 REQUIRE(task != NULL);
5122 REQUIRE(action != NULL);
5124 manager = sock->manager;
5125 REQUIRE(VALID_MANAGER(manager));
5126 REQUIRE(addr != NULL);
5128 if (isc_sockaddr_ismulticast(addr))
5129 return (ISC_R_MULTICAST);
5131 LOCK(&sock->lock);
5133 REQUIRE(!sock->connecting);
5135 dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
5136 ISC_SOCKEVENT_CONNECT,
5137 action, arg,
5138 sizeof(*dev));
5139 if (dev == NULL) {
5140 UNLOCK(&sock->lock);
5141 return (ISC_R_NOMEMORY);
5143 ISC_LINK_INIT(dev, ev_link);
5146 * Try to do the connect right away, as there can be only one
5147 * outstanding, and it might happen to complete.
5149 sock->peer_address = *addr;
5150 cc = connect(sock->fd, &addr->type.sa, addr->length);
5151 if (cc < 0) {
5153 * HP-UX "fails" to connect a UDP socket and sets errno to
5154 * EINPROGRESS if it's non-blocking. We'd rather regard this as
5155 * a success and let the user detect it if it's really an error
5156 * at the time of sending a packet on the socket.
5158 if (sock->type == isc_sockettype_udp && errno == EINPROGRESS) {
5159 cc = 0;
5160 goto success;
5162 if (SOFT_ERROR(errno) || errno == EINPROGRESS)
5163 goto queue;
5165 switch (errno) {
5166 #define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
5167 ERROR_MATCH(EACCES, ISC_R_NOPERM);
5168 ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
5169 ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
5170 ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
5171 ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
5172 #ifdef EHOSTDOWN
5173 ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
5174 #endif
5175 ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
5176 ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
5177 ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
5178 ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
5179 ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
5180 #undef ERROR_MATCH
5183 sock->connected = 0;
5185 isc__strerror(errno, strbuf, sizeof(strbuf));
5186 isc_sockaddr_format(addr, addrbuf, sizeof(addrbuf));
5187 UNEXPECTED_ERROR(__FILE__, __LINE__, "connect(%s) %d/%s",
5188 addrbuf, errno, strbuf);
5190 UNLOCK(&sock->lock);
5191 inc_stats(sock->manager->stats,
5192 sock->statsindex[STATID_CONNECTFAIL]);
5193 isc_event_free(ISC_EVENT_PTR(&dev));
5194 return (ISC_R_UNEXPECTED);
5196 err_exit:
5197 sock->connected = 0;
5198 isc_task_send(task, ISC_EVENT_PTR(&dev));
5200 UNLOCK(&sock->lock);
5201 inc_stats(sock->manager->stats,
5202 sock->statsindex[STATID_CONNECTFAIL]);
5203 return (ISC_R_SUCCESS);
5207 * If connect completed, fire off the done event.
5209 success:
5210 if (cc == 0) {
5211 sock->connected = 1;
5212 sock->bound = 1;
5213 dev->result = ISC_R_SUCCESS;
5214 isc_task_send(task, ISC_EVENT_PTR(&dev));
5216 UNLOCK(&sock->lock);
5218 inc_stats(sock->manager->stats,
5219 sock->statsindex[STATID_CONNECT]);
5221 return (ISC_R_SUCCESS);
5224 queue:
5227 * Attach to task.
5229 isc_task_attach(task, &ntask);
5231 sock->connecting = 1;
5233 dev->ev_sender = ntask;
5236 * Poke watcher here. We still have the socket locked, so there
5237 * is no race condition. We will keep the lock for such a short
5238 * bit of time waking it up now or later won't matter all that much.
5240 if (sock->connect_ev == NULL)
5241 select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
5243 sock->connect_ev = dev;
5245 UNLOCK(&sock->lock);
5246 return (ISC_R_SUCCESS);
5250 * Called when a socket with a pending connect() finishes.
5252 static void
5253 internal_connect(isc_task_t *me, isc_event_t *ev) {
5254 isc__socket_t *sock;
5255 isc_socket_connev_t *dev;
5256 isc_task_t *task;
5257 int cc;
5258 ISC_SOCKADDR_LEN_T optlen;
5259 char strbuf[ISC_STRERRORSIZE];
5260 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
5262 UNUSED(me);
5263 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
5265 sock = ev->ev_sender;
5266 INSIST(VALID_SOCKET(sock));
5268 LOCK(&sock->lock);
5271 * When the internal event was sent the reference count was bumped
5272 * to keep the socket around for us. Decrement the count here.
5274 INSIST(sock->references > 0);
5275 sock->references--;
5276 if (sock->references == 0) {
5277 UNLOCK(&sock->lock);
5278 destroy(&sock);
5279 return;
5283 * Has this event been canceled?
5285 dev = sock->connect_ev;
5286 if (dev == NULL) {
5287 INSIST(!sock->connecting);
5288 UNLOCK(&sock->lock);
5289 return;
5292 INSIST(sock->connecting);
5293 sock->connecting = 0;
5296 * Get any possible error status here.
5298 optlen = sizeof(cc);
5299 if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
5300 (void *)&cc, (void *)&optlen) < 0)
5301 cc = errno;
5302 else
5303 errno = cc;
5305 if (errno != 0) {
5307 * If the error is EAGAIN, just re-select on this
5308 * fd and pretend nothing strange happened.
5310 if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
5311 sock->connecting = 1;
5312 select_poke(sock->manager, sock->fd,
5313 SELECT_POKE_CONNECT);
5314 UNLOCK(&sock->lock);
5316 return;
5319 inc_stats(sock->manager->stats,
5320 sock->statsindex[STATID_CONNECTFAIL]);
5323 * Translate other errors into ISC_R_* flavors.
5325 switch (errno) {
5326 #define ERROR_MATCH(a, b) case a: dev->result = b; break;
5327 ERROR_MATCH(EACCES, ISC_R_NOPERM);
5328 ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
5329 ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
5330 ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
5331 ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
5332 #ifdef EHOSTDOWN
5333 ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
5334 #endif
5335 ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
5336 ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
5337 ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
5338 ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
5339 ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
5340 ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
5341 #undef ERROR_MATCH
5342 default:
5343 dev->result = ISC_R_UNEXPECTED;
5344 isc_sockaddr_format(&sock->peer_address, peerbuf,
5345 sizeof(peerbuf));
5346 isc__strerror(errno, strbuf, sizeof(strbuf));
5347 UNEXPECTED_ERROR(__FILE__, __LINE__,
5348 "internal_connect: connect(%s) %s",
5349 peerbuf, strbuf);
5351 } else {
5352 inc_stats(sock->manager->stats,
5353 sock->statsindex[STATID_CONNECT]);
5354 dev->result = ISC_R_SUCCESS;
5355 sock->connected = 1;
5356 sock->bound = 1;
5359 sock->connect_ev = NULL;
5361 UNLOCK(&sock->lock);
5363 task = dev->ev_sender;
5364 dev->ev_sender = sock;
5365 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
5368 ISC_SOCKETFUNC_SCOPE isc_result_t
5369 isc__socket_getpeername(isc_socket_t *sock0, isc_sockaddr_t *addressp) {
5370 isc__socket_t *sock = (isc__socket_t *)sock0;
5371 isc_result_t result;
5373 REQUIRE(VALID_SOCKET(sock));
5374 REQUIRE(addressp != NULL);
5376 LOCK(&sock->lock);
5378 if (sock->connected) {
5379 *addressp = sock->peer_address;
5380 result = ISC_R_SUCCESS;
5381 } else {
5382 result = ISC_R_NOTCONNECTED;
5385 UNLOCK(&sock->lock);
5387 return (result);
5390 ISC_SOCKETFUNC_SCOPE isc_result_t
5391 isc__socket_getsockname(isc_socket_t *sock0, isc_sockaddr_t *addressp) {
5392 isc__socket_t *sock = (isc__socket_t *)sock0;
5393 ISC_SOCKADDR_LEN_T len;
5394 isc_result_t result;
5395 char strbuf[ISC_STRERRORSIZE];
5397 REQUIRE(VALID_SOCKET(sock));
5398 REQUIRE(addressp != NULL);
5400 LOCK(&sock->lock);
5402 if (!sock->bound) {
5403 result = ISC_R_NOTBOUND;
5404 goto out;
5407 result = ISC_R_SUCCESS;
5409 len = sizeof(addressp->type);
5410 if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
5411 isc__strerror(errno, strbuf, sizeof(strbuf));
5412 UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
5413 strbuf);
5414 result = ISC_R_UNEXPECTED;
5415 goto out;
5417 addressp->length = (unsigned int)len;
5419 out:
5420 UNLOCK(&sock->lock);
5422 return (result);
5426 * Run through the list of events on this socket, and cancel the ones
5427 * queued for task "task" of type "how". "how" is a bitmask.
5429 ISC_SOCKETFUNC_SCOPE void
5430 isc__socket_cancel(isc_socket_t *sock0, isc_task_t *task, unsigned int how) {
5431 isc__socket_t *sock = (isc__socket_t *)sock0;
5433 REQUIRE(VALID_SOCKET(sock));
5436 * Quick exit if there is nothing to do. Don't even bother locking
5437 * in this case.
5439 if (how == 0)
5440 return;
5442 LOCK(&sock->lock);
5445 * All of these do the same thing, more or less.
5446 * Each will:
5447 * o If the internal event is marked as "posted" try to
5448 * remove it from the task's queue. If this fails, mark it
5449 * as canceled instead, and let the task clean it up later.
5450 * o For each I/O request for that task of that type, post
5451 * its done event with status of "ISC_R_CANCELED".
5452 * o Reset any state needed.
5454 if (((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV)
5455 && !ISC_LIST_EMPTY(sock->recv_list)) {
5456 isc_socketevent_t *dev;
5457 isc_socketevent_t *next;
5458 isc_task_t *current_task;
5460 dev = ISC_LIST_HEAD(sock->recv_list);
5462 while (dev != NULL) {
5463 current_task = dev->ev_sender;
5464 next = ISC_LIST_NEXT(dev, ev_link);
5466 if ((task == NULL) || (task == current_task)) {
5467 dev->result = ISC_R_CANCELED;
5468 send_recvdone_event(sock, &dev);
5470 dev = next;
5474 if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
5475 && !ISC_LIST_EMPTY(sock->send_list)) {
5476 isc_socketevent_t *dev;
5477 isc_socketevent_t *next;
5478 isc_task_t *current_task;
5480 dev = ISC_LIST_HEAD(sock->send_list);
5482 while (dev != NULL) {
5483 current_task = dev->ev_sender;
5484 next = ISC_LIST_NEXT(dev, ev_link);
5486 if ((task == NULL) || (task == current_task)) {
5487 dev->result = ISC_R_CANCELED;
5488 send_senddone_event(sock, &dev);
5490 dev = next;
5494 if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
5495 && !ISC_LIST_EMPTY(sock->accept_list)) {
5496 isc_socket_newconnev_t *dev;
5497 isc_socket_newconnev_t *next;
5498 isc_task_t *current_task;
5500 dev = ISC_LIST_HEAD(sock->accept_list);
5501 while (dev != NULL) {
5502 current_task = dev->ev_sender;
5503 next = ISC_LIST_NEXT(dev, ev_link);
5505 if ((task == NULL) || (task == current_task)) {
5507 ISC_LIST_UNLINK(sock->accept_list, dev,
5508 ev_link);
5510 NEWCONNSOCK(dev)->references--;
5511 free_socket((isc__socket_t **)(void *)&dev->newsocket);
5513 dev->result = ISC_R_CANCELED;
5514 dev->ev_sender = sock;
5515 isc_task_sendanddetach(&current_task,
5516 ISC_EVENT_PTR(&dev));
5519 dev = next;
5524 * Connecting is not a list.
5526 if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
5527 && sock->connect_ev != NULL) {
5528 isc_socket_connev_t *dev;
5529 isc_task_t *current_task;
5531 INSIST(sock->connecting);
5532 sock->connecting = 0;
5534 dev = sock->connect_ev;
5535 current_task = dev->ev_sender;
5537 if ((task == NULL) || (task == current_task)) {
5538 sock->connect_ev = NULL;
5540 dev->result = ISC_R_CANCELED;
5541 dev->ev_sender = sock;
5542 isc_task_sendanddetach(&current_task,
5543 ISC_EVENT_PTR(&dev));
5547 UNLOCK(&sock->lock);
5550 ISC_SOCKETFUNC_SCOPE isc_sockettype_t
5551 isc__socket_gettype(isc_socket_t *sock0) {
5552 isc__socket_t *sock = (isc__socket_t *)sock0;
5554 REQUIRE(VALID_SOCKET(sock));
5556 return (sock->type);
5559 ISC_SOCKETFUNC_SCOPE isc_boolean_t
5560 isc__socket_isbound(isc_socket_t *sock0) {
5561 isc__socket_t *sock = (isc__socket_t *)sock0;
5562 isc_boolean_t val;
5564 REQUIRE(VALID_SOCKET(sock));
5566 LOCK(&sock->lock);
5567 val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
5568 UNLOCK(&sock->lock);
5570 return (val);
5573 ISC_SOCKETFUNC_SCOPE void
5574 isc__socket_ipv6only(isc_socket_t *sock0, isc_boolean_t yes) {
5575 isc__socket_t *sock = (isc__socket_t *)sock0;
5576 #if defined(IPV6_V6ONLY)
5577 int onoff = yes ? 1 : 0;
5578 #else
5579 UNUSED(yes);
5580 UNUSED(sock);
5581 #endif
5583 REQUIRE(VALID_SOCKET(sock));
5585 #ifdef IPV6_V6ONLY
5586 if (sock->pf == AF_INET6) {
5587 if (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
5588 (void *)&onoff, sizeof(int)) < 0) {
5589 char strbuf[ISC_STRERRORSIZE];
5591 UNEXPECTED_ERROR(__FILE__, __LINE__,
5592 "setsockopt(%d, IPV6_V6ONLY) "
5593 "%s: %s", sock->fd,
5594 isc_msgcat_get(isc_msgcat,
5595 ISC_MSGSET_GENERAL,
5596 ISC_MSG_FAILED,
5597 "failed"),
5598 strbuf);
5601 FIX_IPV6_RECVPKTINFO(sock); /* AIX */
5602 #endif
5605 #ifndef USE_WATCHER_THREAD
5607 * In our assumed scenario, we can simply use a single static object.
5608 * XXX: this is not true if the application uses multiple threads with
5609 * 'multi-context' mode. Fixing this is a future TODO item.
5611 static isc_socketwait_t swait_private;
5614 isc__socketmgr_waitevents(isc_socketmgr_t *manager0, struct timeval *tvp,
5615 isc_socketwait_t **swaitp)
5617 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
5620 int n;
5621 #ifdef USE_KQUEUE
5622 struct timespec ts, *tsp;
5623 #endif
5624 #ifdef USE_EPOLL
5625 int timeout;
5626 #endif
5627 #ifdef USE_DEVPOLL
5628 struct dvpoll dvp;
5629 #endif
5631 REQUIRE(swaitp != NULL && *swaitp == NULL);
5633 #ifdef USE_SHARED_MANAGER
5634 if (manager == NULL)
5635 manager = socketmgr;
5636 #endif
5637 if (manager == NULL)
5638 return (0);
5640 #ifdef USE_KQUEUE
5641 if (tvp != NULL) {
5642 ts.tv_sec = tvp->tv_sec;
5643 ts.tv_nsec = tvp->tv_usec * 1000;
5644 tsp = &ts;
5645 } else
5646 tsp = NULL;
5647 swait_private.nevents = kevent(manager->kqueue_fd, NULL, 0,
5648 manager->events, manager->nevents,
5649 tsp);
5650 n = swait_private.nevents;
5651 #elif defined(USE_EPOLL)
5652 if (tvp != NULL)
5653 timeout = tvp->tv_sec * 1000 + (tvp->tv_usec + 999) / 1000;
5654 else
5655 timeout = -1;
5656 swait_private.nevents = epoll_wait(manager->epoll_fd,
5657 manager->events,
5658 manager->nevents, timeout);
5659 n = swait_private.nevents;
5660 #elif defined(USE_DEVPOLL)
5661 dvp.dp_fds = manager->events;
5662 dvp.dp_nfds = manager->nevents;
5663 if (tvp != NULL) {
5664 dvp.dp_timeout = tvp->tv_sec * 1000 +
5665 (tvp->tv_usec + 999) / 1000;
5666 } else
5667 dvp.dp_timeout = -1;
5668 swait_private.nevents = ioctl(manager->devpoll_fd, DP_POLL, &dvp);
5669 n = swait_private.nevents;
5670 #elif defined(USE_SELECT)
5671 memcpy(manager->read_fds_copy, manager->read_fds, manager->fd_bufsize);
5672 memcpy(manager->write_fds_copy, manager->write_fds,
5673 manager->fd_bufsize);
5675 swait_private.readset = manager->read_fds_copy;
5676 swait_private.writeset = manager->write_fds_copy;
5677 swait_private.maxfd = manager->maxfd + 1;
5679 n = select(swait_private.maxfd, swait_private.readset,
5680 swait_private.writeset, NULL, tvp);
5681 #endif
5683 *swaitp = &swait_private;
5684 return (n);
5687 isc_result_t
5688 isc__socketmgr_dispatch(isc_socketmgr_t *manager0, isc_socketwait_t *swait) {
5689 isc__socketmgr_t *manager = (isc__socketmgr_t *)manager0;
5691 REQUIRE(swait == &swait_private);
5693 #ifdef USE_SHARED_MANAGER
5694 if (manager == NULL)
5695 manager = socketmgr;
5696 #endif
5697 if (manager == NULL)
5698 return (ISC_R_NOTFOUND);
5700 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
5701 (void)process_fds(manager, manager->events, swait->nevents);
5702 return (ISC_R_SUCCESS);
5703 #elif defined(USE_SELECT)
5704 process_fds(manager, swait->maxfd, swait->readset, swait->writeset);
5705 return (ISC_R_SUCCESS);
5706 #endif
5708 #endif /* USE_WATCHER_THREAD */
5710 #ifdef BIND9
5711 void
5712 isc__socket_setname(isc_socket_t *socket0, const char *name, void *tag) {
5713 isc__socket_t *socket = (isc__socket_t *)socket0;
5716 * Name 'socket'.
5719 REQUIRE(VALID_SOCKET(socket));
5721 LOCK(&socket->lock);
5722 memset(socket->name, 0, sizeof(socket->name));
5723 strncpy(socket->name, name, sizeof(socket->name) - 1);
5724 socket->tag = tag;
5725 UNLOCK(&socket->lock);
5728 ISC_SOCKETFUNC_SCOPE const char *
5729 isc__socket_getname(isc_socket_t *socket0) {
5730 isc__socket_t *socket = (isc__socket_t *)socket0;
5732 return (socket->name);
5735 void *
5736 isc__socket_gettag(isc_socket_t *socket0) {
5737 isc__socket_t *socket = (isc__socket_t *)socket0;
5739 return (socket->tag);
5741 #endif /* BIND9 */
5743 #ifdef USE_SOCKETIMPREGISTER
5744 isc_result_t
5745 isc__socket_register() {
5746 return (isc_socket_register(isc__socketmgr_create));
5748 #endif
5750 #if defined(HAVE_LIBXML2) && defined(BIND9)
5752 static const char *
5753 _socktype(isc_sockettype_t type)
5755 if (type == isc_sockettype_udp)
5756 return ("udp");
5757 else if (type == isc_sockettype_tcp)
5758 return ("tcp");
5759 else if (type == isc_sockettype_unix)
5760 return ("unix");
5761 else if (type == isc_sockettype_fdwatch)
5762 return ("fdwatch");
5763 else
5764 return ("not-initialized");
5767 ISC_SOCKETFUNC_SCOPE void
5768 isc_socketmgr_renderxml(isc_socketmgr_t *mgr0, xmlTextWriterPtr writer) {
5769 isc__socketmgr_t *mgr = (isc__socketmgr_t *)mgr0;
5770 isc__socket_t *sock;
5771 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
5772 isc_sockaddr_t addr;
5773 ISC_SOCKADDR_LEN_T len;
5775 LOCK(&mgr->lock);
5777 #ifdef USE_SHARED_MANAGER
5778 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
5779 xmlTextWriterWriteFormatString(writer, "%d", mgr->refs);
5780 xmlTextWriterEndElement(writer);
5781 #endif /* USE_SHARED_MANAGER */
5783 xmlTextWriterStartElement(writer, ISC_XMLCHAR "sockets");
5784 sock = ISC_LIST_HEAD(mgr->socklist);
5785 while (sock != NULL) {
5786 LOCK(&sock->lock);
5787 xmlTextWriterStartElement(writer, ISC_XMLCHAR "socket");
5789 xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
5790 xmlTextWriterWriteFormatString(writer, "%p", sock);
5791 xmlTextWriterEndElement(writer);
5793 if (sock->name[0] != 0) {
5794 xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
5795 xmlTextWriterWriteFormatString(writer, "%s",
5796 sock->name);
5797 xmlTextWriterEndElement(writer); /* name */
5800 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
5801 xmlTextWriterWriteFormatString(writer, "%d", sock->references);
5802 xmlTextWriterEndElement(writer);
5804 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "type",
5805 ISC_XMLCHAR _socktype(sock->type));
5807 if (sock->connected) {
5808 isc_sockaddr_format(&sock->peer_address, peerbuf,
5809 sizeof(peerbuf));
5810 xmlTextWriterWriteElement(writer,
5811 ISC_XMLCHAR "peer-address",
5812 ISC_XMLCHAR peerbuf);
5815 len = sizeof(addr);
5816 if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
5817 isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
5818 xmlTextWriterWriteElement(writer,
5819 ISC_XMLCHAR "local-address",
5820 ISC_XMLCHAR peerbuf);
5823 xmlTextWriterStartElement(writer, ISC_XMLCHAR "states");
5824 if (sock->pending_recv)
5825 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5826 ISC_XMLCHAR "pending-receive");
5827 if (sock->pending_send)
5828 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5829 ISC_XMLCHAR "pending-send");
5830 if (sock->pending_accept)
5831 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5832 ISC_XMLCHAR "pending_accept");
5833 if (sock->listener)
5834 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5835 ISC_XMLCHAR "listener");
5836 if (sock->connected)
5837 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5838 ISC_XMLCHAR "connected");
5839 if (sock->connecting)
5840 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5841 ISC_XMLCHAR "connecting");
5842 if (sock->bound)
5843 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
5844 ISC_XMLCHAR "bound");
5846 xmlTextWriterEndElement(writer); /* states */
5848 xmlTextWriterEndElement(writer); /* socket */
5850 UNLOCK(&sock->lock);
5851 sock = ISC_LIST_NEXT(sock, link);
5853 xmlTextWriterEndElement(writer); /* sockets */
5855 UNLOCK(&mgr->lock);
5857 #endif /* HAVE_LIBXML2 */