1 /* $NetBSD: socket.c,v 1.1.1.4 2009/12/26 22:26:00 christos Exp $ */
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 */
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
40 #include <isc/buffer.h>
41 #include <isc/bufferlist.h>
42 #include <isc/condition.h>
43 #include <isc/formatcheck.h>
48 #include <isc/mutex.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>
58 #include <isc/thread.h>
62 #ifdef ISC_PLATFORM_HAVESYSUNH
65 #ifdef ISC_PLATFORM_HAVEKQUEUE
66 #include <sys/event.h>
68 #ifdef ISC_PLATFORM_HAVEEPOLL
69 #include <sys/epoll.h>
71 #ifdef ISC_PLATFORM_HAVEDEVPOLL
72 #include <sys/devpoll.h>
75 #include "errno2result.h"
77 /* See task.c about the following definition: */
79 #ifdef ISC_PLATFORM_USETHREADS
80 #define USE_WATCHER_THREAD
82 #define USE_SHARED_MANAGER
83 #endif /* ISC_PLATFORM_USETHREADS */
86 #ifndef USE_WATCHER_THREAD
88 #endif /* USE_WATCHER_THREAD */
90 #if defined(SO_BSDCOMPAT) && defined(__linux__)
91 #include <sys/utsname.h>
95 * Choose the most preferable multiplex method.
97 #ifdef ISC_PLATFORM_HAVEKQUEUE
99 #elif defined (ISC_PLATFORM_HAVEEPOLL)
101 #elif defined (ISC_PLATFORM_HAVEDEVPOLL)
104 unsigned int want_read
: 1,
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
{
116 #elif defined (USE_SELECT)
117 struct isc_socketwait
{
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 */
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.
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)
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
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
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 || \
234 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
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
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?
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.
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)
300 isc__socketmgr_t
*manager
;
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
;
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.
326 isc_sockaddr_t peer_address
; /* remote address */
328 unsigned int pending_recv
: 1,
331 listener
: 1, /* listener socket */
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 */
341 ISC_SOCKADDR_LEN_T recvcmsgbuflen
;
343 ISC_SOCKADDR_LEN_T sendcmsgbuflen
;
346 isc_sockfdwatch_t fdwatchcb
;
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
{
356 isc_socketmgr_t common
;
364 struct kevent
*events
;
365 #endif /* USE_KQUEUE */
369 struct epoll_event
*events
;
370 #endif /* USE_EPOLL */
374 struct pollfd
*events
;
375 #endif /* USE_DEVPOLL */
378 #endif /* USE_SELECT */
379 unsigned int maxsocks
;
380 #ifdef ISC_PLATFORM_USETHREADS
384 /* Locked by fdlock. */
388 pollinfo_t
*fdpollinfo
;
391 /* Locked by manager lock. */
392 ISC_LIST(isc__socket_t
) socklist
;
395 fd_set
*read_fds_copy
;
397 fd_set
*write_fds_copy
;
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 */
406 #endif /* USE_WATCHER_THREAD */
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 */
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)
425 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
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
,
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
);
450 * The following can be either static or public, depending on build environment.
454 #define ISC_SOCKETFUNC_SCOPE
456 #define ISC_SOCKETFUNC_SCOPE static
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
,
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
,
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
);
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
);
548 isc_socketmethods_t methods
;
551 * The following are defined just for avoiding unused static functions.
554 void *recvv
, *send
, *sendv
, *sendto2
, *cleanunix
, *permunix
, *filter
,
555 *listen
, *accept
, *getpeername
, *isbound
;
566 isc__socket_getsockname
,
568 isc__socket_ipv6only
,
569 isc__socket_fdwatchpoke
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
582 static isc_socketmgrmethods_t socketmgrmethods
= {
583 isc__socketmgr_destroy
,
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.
606 STATID_CONNECTFAIL
= 4,
608 STATID_ACCEPTFAIL
= 6,
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
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);
691 manager_log(isc__socketmgr_t
*sockmgr
,
692 isc_logcategory_t
*category
, isc_logmodule_t
*module
, int level
,
693 const char *fmt
, ...)
698 if (! isc_log_wouldlog(isc_lctx
, level
))
702 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, ap
);
705 isc_log_write(isc_lctx
, category
, module
, level
,
706 "sockmgr %p: %s", sockmgr
, msgbuf
);
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);
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
, ...)
721 char peerbuf
[ISC_SOCKADDR_FORMATSIZE
];
724 if (! isc_log_wouldlog(isc_lctx
, level
))
728 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, ap
);
731 if (address
== NULL
) {
732 isc_log_iwrite(isc_lctx
, category
, module
, level
,
733 msgcat
, msgset
, message
,
734 "socket %p: %s", sock
, msgbuf
);
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.
750 FIX_IPV6_RECVPKTINFO(isc__socket_t
*sock
)
752 char strbuf
[ISC_STRERRORSIZE
];
755 if (sock
->pf
!= AF_INET6
|| sock
->type
!= isc_sockettype_udp
)
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) "
764 isc_msgcat_get(isc_msgcat
,
772 #define FIX_IPV6_RECVPKTINFO(sock) (void)0
776 * Increment socket-related statistics counters.
779 inc_stats(isc_stats_t
*stats
, isc_statscounter_t counterid
) {
780 REQUIRE(counterid
!= -1);
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
;
791 struct kevent evchange
;
793 memset(&evchange
, 0, sizeof(evchange
));
794 if (msg
== SELECT_POKE_READ
)
795 evchange
.filter
= EVFILT_READ
;
797 evchange
.filter
= EVFILT_WRITE
;
798 evchange
.flags
= EV_ADD
;
800 if (kevent(manager
->kqueue_fd
, &evchange
, 1, NULL
, 0, NULL
) != 0)
801 result
= isc__errno2result(errno
);
804 #elif defined(USE_EPOLL)
805 struct epoll_event event
;
807 if (msg
== SELECT_POKE_READ
)
808 event
.events
= EPOLLIN
;
810 event
.events
= EPOLLOUT
;
812 if (epoll_ctl(manager
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &event
) == -1 &&
814 result
= isc__errno2result(errno
);
818 #elif defined(USE_DEVPOLL)
820 int lockid
= FDLOCK_ID(fd
);
822 memset(&pfd
, 0, sizeof(pfd
));
823 if (msg
== SELECT_POKE_READ
)
826 pfd
.events
= POLLOUT
;
829 LOCK(&manager
->fdlock
[lockid
]);
830 if (write(manager
->devpoll_fd
, &pfd
, sizeof(pfd
)) == -1)
831 result
= isc__errno2result(errno
);
833 if (msg
== SELECT_POKE_READ
)
834 manager
->fdpollinfo
[fd
].want_read
= 1;
836 manager
->fdpollinfo
[fd
].want_write
= 1;
838 UNLOCK(&manager
->fdlock
[lockid
]);
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
);
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
;
858 struct kevent evchange
;
860 memset(&evchange
, 0, sizeof(evchange
));
861 if (msg
== SELECT_POKE_READ
)
862 evchange
.filter
= EVFILT_READ
;
864 evchange
.filter
= EVFILT_WRITE
;
865 evchange
.flags
= EV_DELETE
;
867 if (kevent(manager
->kqueue_fd
, &evchange
, 1, NULL
, 0, NULL
) != 0)
868 result
= isc__errno2result(errno
);
871 #elif defined(USE_EPOLL)
872 struct epoll_event event
;
874 if (msg
== SELECT_POKE_READ
)
875 event
.events
= EPOLLIN
;
877 event
.events
= EPOLLOUT
;
879 if (epoll_ctl(manager
->epoll_fd
, EPOLL_CTL_DEL
, fd
, &event
) == -1 &&
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
;
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
;
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
;
907 writelen
+= sizeof(pfds
[1]);
909 if (msg
== SELECT_POKE_WRITE
&&
910 manager
->fdpollinfo
[fd
].want_read
== 1) {
911 pfds
[1].events
= POLLIN
;
913 writelen
+= sizeof(pfds
[1]);
916 if (write(manager
->devpoll_fd
, pfds
, writelen
) == -1)
917 result
= isc__errno2result(errno
);
919 if (msg
== SELECT_POKE_READ
)
920 manager
->fdpollinfo
[fd
].want_read
= 0;
922 manager
->fdpollinfo
[fd
].want_write
= 0;
924 UNLOCK(&manager
->fdlock
[lockid
]);
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
);
940 wakeup_socket(isc__socketmgr_t
*manager
, int fd
, int msg
) {
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
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
);
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
970 * Note that unwatch_fd() must be called after releasing the
971 * fdlock; otherwise it could cause deadlock due to a lock order
974 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
975 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
978 if (manager
->fdstate
[fd
] != MANAGED
) {
979 UNLOCK(&manager
->fdlock
[lockid
]);
982 UNLOCK(&manager
->fdlock
[lockid
]);
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.
1008 select_poke(isc__socketmgr_t
*mgr
, int fd
, int msg
) {
1011 char strbuf
[ISC_STRERRORSIZE
];
1017 cc
= write(mgr
->pipe_fds
[1], buf
, sizeof(buf
));
1020 * Treat ENOSR as EAGAIN but loop slowly as it is
1021 * unlikely to clear fast.
1023 if (cc
< 0 && errno
== ENOSR
) {
1028 } while (cc
< 0 && SOFT_ERROR(errno
));
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
,
1036 "during watcher poke: %s"),
1040 INSIST(cc
== sizeof(buf
));
1044 * Read a message on the internal fd.
1047 select_readmsg(isc__socketmgr_t
*mgr
, int *fd
, int *msg
) {
1050 char strbuf
[ISC_STRERRORSIZE
];
1052 cc
= read(mgr
->pipe_fds
[0], buf
, sizeof(buf
));
1054 *msg
= SELECT_POKE_NOTHING
;
1055 *fd
= -1; /* Silence compiler. */
1056 if (SOFT_ERROR(errno
))
1059 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
1060 FATAL_ERROR(__FILE__
, __LINE__
,
1061 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
1064 "during watcher poke: %s"),
1069 INSIST(cc
== sizeof(buf
));
1074 #else /* USE_WATCHER_THREAD */
1076 * Update the state of the socketmgr when something changes.
1079 select_poke(isc__socketmgr_t
*manager
, int fd
, int msg
) {
1080 if (msg
== SELECT_POKE_SHUTDOWN
)
1083 wakeup_socket(manager
, fd
, msg
);
1086 #endif /* USE_WATCHER_THREAD */
1089 * Make a fd non-blocking.
1092 make_nonblock(int fd
) {
1095 char strbuf
[ISC_STRERRORSIZE
];
1096 #ifdef USE_FIONBIO_IOCTL
1099 ret
= ioctl(fd
, FIONBIO
, (char *)&on
);
1101 flags
= fcntl(fd
, F_GETFL
, 0);
1102 flags
|= PORT_NONBLOCK
;
1103 ret
= fcntl(fd
, F_SETFL
, flags
);
1107 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
1108 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
1109 #ifdef USE_FIONBIO_IOCTL
1110 "ioctl(%d, FIONBIO, &on): %s", fd
,
1112 "fcntl(%d, F_SETFL, %d): %s", fd
, flags
,
1116 return (ISC_R_UNEXPECTED
);
1119 return (ISC_R_SUCCESS
);
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
1130 static inline ISC_SOCKADDR_LEN_T
1131 cmsg_len(ISC_SOCKADDR_LEN_T len
) {
1133 return (CMSG_LEN(len
));
1135 ISC_SOCKADDR_LEN_T hdrlen
;
1138 * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
1141 hdrlen
= (ISC_SOCKADDR_LEN_T
)CMSG_DATA(((struct cmsghdr
*)NULL
));
1142 return (hdrlen
+ len
);
1146 static inline ISC_SOCKADDR_LEN_T
1147 cmsg_space(ISC_SOCKADDR_LEN_T len
) {
1149 return (CMSG_SPACE(len
));
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
);
1168 return ((char *)cmsgp
- (char *)msg
.msg_control
);
1173 #endif /* USE_CMSG */
1176 * Process control messages received on a socket.
1179 process_cmsg(isc__socket_t
*sock
, struct msghdr
*msg
, isc_socketevent_t
*dev
) {
1181 struct cmsghdr
*cmsgp
;
1182 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1183 struct in6_pktinfo
*pktinfop
;
1186 struct timeval
*timevalp
;
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.
1200 #ifdef ISC_NET_BSD44MSGHDR
1203 if ((msg
->msg_flags
& MSG_TRUNC
) == MSG_TRUNC
)
1204 dev
->attributes
|= ISC_SOCKEVENTATTR_TRUNC
;
1208 if ((msg
->msg_flags
& MSG_CTRUNC
) == MSG_CTRUNC
)
1209 dev
->attributes
|= ISC_SOCKEVENTATTR_CTRUNC
;
1215 if (msg
->msg_controllen
== 0U || msg
->msg_control
== NULL
)
1221 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
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
,
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
;
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
;
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.
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
;
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
;
1297 msg
->msg_name
= NULL
;
1298 msg
->msg_namelen
= 0;
1301 buffer
= ISC_LIST_HEAD(dev
->bufferlist
);
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
;
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
))
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
1338 iov
[iovcount
].iov_len
= used
.length
- skip_count
;
1339 write_count
+= (used
.length
- skip_count
);
1343 buffer
= ISC_LIST_NEXT(buffer
, link
);
1346 INSIST(skip_count
== 0U);
1350 msg
->msg_iovlen
= iovcount
;
1352 #ifdef ISC_NET_BSD44MSGHDR
1353 msg
->msg_control
= NULL
;
1354 msg
->msg_controllen
= 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.
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
;
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
);
1426 msg
->msg_name
= (void *)&dev
->address
.type
.sa
;
1427 msg
->msg_namelen
= sizeof(dev
->address
.type
);
1430 msg
->msg_name
= (void *)&dev
->address
.type
.sa
;
1431 msg
->msg_namelen
= sizeof(dev
->address
.type
);
1433 #ifdef ISC_NET_RECVOVERFLOW
1434 /* If needed, steal one iovec for overflow detection. */
1438 msg
->msg_name
= NULL
;
1439 msg
->msg_namelen
= 0;
1440 dev
->address
= sock
->peer_address
;
1443 buffer
= ISC_LIST_HEAD(dev
->bufferlist
);
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
;
1460 * Skip empty buffers.
1462 while (buffer
!= NULL
) {
1463 REQUIRE(ISC_BUFFER_VALID(buffer
));
1464 if (isc_buffer_availablelength(buffer
) != 0)
1466 buffer
= ISC_LIST_NEXT(buffer
, link
);
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
;
1481 buffer
= ISC_LIST_NEXT(buffer
, link
);
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;
1500 msg
->msg_iovlen
= iovcount
;
1502 #ifdef ISC_NET_BSD44MSGHDR
1503 msg
->msg_control
= NULL
;
1504 msg
->msg_controllen
= 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
;
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
;
1529 dev
->address
= sock
->peer_address
;
1530 } else if (sock
->type
== isc_sockettype_tcp
) {
1531 INSIST(address
== NULL
);
1532 dev
->address
= sock
->peer_address
;
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
,
1559 ev
->result
= ISC_R_UNEXPECTED
;
1560 ISC_LINK_INIT(ev
, ev_link
);
1561 ISC_LIST_INIT(ev
->bufferlist
);
1562 ev
->region
.base
= NULL
;
1566 ev
->destroy
= ev
->ev_destroy
;
1567 ev
->ev_destroy
= destroy_socketevent
;
1572 #if defined(ISC_SOCKET_DEBUG)
1574 dump_msg(struct msghdr
*msg
) {
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
);
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 */
1599 doio_recv(isc__socket_t
*sock
, isc_socketevent_t
*dev
) {
1601 struct iovec iov
[MAXSCATTERGATHER_RECV
];
1603 size_t actual_count
;
1604 struct msghdr msghdr
;
1605 isc_buffer_t
*buffer
;
1607 char strbuf
[ISC_STRERRORSIZE
];
1609 build_msghdr_recv(sock
, dev
, &msghdr
, iov
, &read_count
);
1611 #if defined(ISC_SOCKET_DEBUG)
1615 cc
= recvmsg(sock
->fd
, &msghdr
, 0);
1618 #if defined(ISC_SOCKET_DEBUG)
1623 if (SOFT_ERROR(recv_errno
))
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
,
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
1665 SOFT_OR_HARD(EPROTO
, ISC_R_HOSTUNREACH
);
1667 SOFT_OR_HARD(EINVAL
, ISC_R_HOSTUNREACH
);
1672 dev
->result
= isc__errno2result(recv_errno
);
1673 inc_stats(sock
->manager
->stats
,
1674 sock
->statsindex
[STATID_RECVFAIL
]);
1679 * On TCP, zero length reads indicate EOF, while on
1680 * UDP, zero length reads are perfectly valid, although
1683 if ((sock
->type
== isc_sockettype_tcp
) && (cc
== 0))
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
,
1693 "dropping source port zero packet");
1698 * Simulate a firewall blocking UDP responses bigger than
1701 if (sock
->manager
->maxudp
!= 0 && cc
> sock
->manager
->maxudp
)
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
;
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
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
));
1741 isc_buffer_add(buffer
, actual_count
);
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
))
1759 * Full reads are posted, or partials if partials are ok.
1761 dev
->result
= ISC_R_SUCCESS
;
1762 return (DOIO_SUCCESS
);
1767 * DOIO_SUCCESS The operation succeeded. dev->result contains
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.
1779 doio_send(isc__socket_t
*sock
, isc_socketevent_t
*dev
) {
1781 struct iovec iov
[MAXSCATTERGATHER_SEND
];
1783 struct msghdr msghdr
;
1784 char addrbuf
[ISC_SOCKADDR_FORMATSIZE
];
1787 char strbuf
[ISC_STRERRORSIZE
];
1789 build_msghdr_send(sock
, dev
, &msghdr
, iov
, &write_count
);
1792 cc
= sendmsg(sock
->fd
, &msghdr
, 0);
1796 * Check for error or block condition.
1799 if (send_errno
== EINTR
&& ++attempts
< NRETRIES
)
1802 if (SOFT_ERROR(send_errno
))
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
);
1829 ALWAYS_HARD(EHOSTDOWN
, ISC_R_HOSTUNREACH
);
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
);
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
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",
1853 dev
->result
= isc__errno2result(send_errno
);
1854 inc_stats(sock
->manager
->stats
,
1855 sock
->statsindex
[STATID_SENDFAIL
]);
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.
1872 if ((size_t)cc
!= write_count
)
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
);
1886 * Caller must ensure that the socket is not locked and no external
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
;
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
);
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
1927 LOCK(&manager
->lock
);
1928 if (manager
->maxfd
== fd
) {
1932 for (i
= fd
- 1; i
>= 0; i
--) {
1933 lockid
= FDLOCK_ID(i
);
1935 LOCK(&manager
->fdlock
[lockid
]);
1936 if (manager
->fdstate
[i
] == MANAGED
) {
1938 UNLOCK(&manager
->fdlock
[lockid
]);
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];
1948 UNLOCK(&manager
->lock
);
1949 #endif /* USE_SELECT */
1953 destroy(isc__socket_t
**sockp
) {
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) {
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
);
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
));
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
;
2009 sock
->statsindex
= NULL
;
2011 ISC_LINK_INIT(sock
, link
);
2013 sock
->recvcmsgbuf
= NULL
;
2014 sock
->sendcmsgbuf
= NULL
;
2017 * set up cmsg buffers
2020 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
2021 cmsgbuflen
= cmsg_space(sizeof(struct in6_pktinfo
));
2023 #if defined(USE_CMSG) && defined(SO_TIMESTAMP)
2024 cmsgbuflen
+= cmsg_space(sizeof(struct timeval
));
2026 sock
->recvcmsgbuflen
= cmsgbuflen
;
2027 if (sock
->recvcmsgbuflen
!= 0U) {
2028 sock
->recvcmsgbuf
= isc_mem_get(manager
->mctx
, cmsgbuflen
);
2029 if (sock
->recvcmsgbuf
== NULL
)
2034 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
2035 cmsgbuflen
= cmsg_space(sizeof(struct in6_pktinfo
));
2037 sock
->sendcmsgbuflen
= cmsgbuflen
;
2038 if (sock
->sendcmsgbuflen
!= 0U) {
2039 sock
->sendcmsgbuf
= isc_mem_get(manager
->mctx
, cmsgbuflen
);
2040 if (sock
->sendcmsgbuf
== NULL
)
2044 memset(sock
->name
, 0, sizeof(sock
->name
));
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;
2058 sock
->connected
= 0;
2059 sock
->connecting
= 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;
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
;
2086 return (ISC_R_SUCCESS
);
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
));
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.
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
));
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
;
2156 clear_bsdcompat(void) {
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);
2168 minor
= strtol(endp
+1, &endp
, 10);
2169 if ((major
> 2) || ((major
== 2) && (minor
>= 4))) {
2170 bsdcompat
= ISC_FALSE
;
2173 #endif /* __linux __ */
2178 opensocket(isc__socketmgr_t
*manager
, isc__socket_t
*sock
) {
2179 char strbuf
[ISC_STRERRORSIZE
];
2180 const char *err
= "socket";
2182 #if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
2185 #if defined(SO_RCVBUF)
2186 ISC_SOCKADDR_LEN_T optlen
;
2191 switch (sock
->type
) {
2192 case isc_sockettype_udp
:
2193 sock
->fd
= socket(sock
->pf
, SOCK_DGRAM
, IPPROTO_UDP
);
2195 case isc_sockettype_tcp
:
2196 sock
->fd
= socket(sock
->pf
, SOCK_STREAM
, IPPROTO_TCP
);
2198 case isc_sockettype_unix
:
2199 sock
->fd
= socket(sock
->pf
, SOCK_STREAM
, 0);
2201 case isc_sockettype_fdwatch
:
2203 * We should not be called for isc_sockettype_fdwatch sockets.
2208 if (sock
->fd
== -1 && errno
== EINTR
&& tries
++ < 42)
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
) {
2218 new = fcntl(sock
->fd
, F_DUPFD
, manager
->reserved
);
2220 (void)close(sock
->fd
);
2223 err
= "isc_socket_create: fcntl/reserved";
2224 } else if (sock
->fd
>= 0 && sock
->fd
< 20) {
2226 new = fcntl(sock
->fd
, F_DUPFD
, 20);
2228 (void)close(sock
->fd
);
2231 err
= "isc_socket_create: fcntl";
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
,
2241 "socket: file descriptor exceeds limit (%d/%u)",
2242 sock
->fd
, manager
->maxsocks
);
2243 return (ISC_R_NORESOURCES
);
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
,
2255 "%s: %s", err
, strbuf
);
2258 return (ISC_R_NORESOURCES
);
2260 case EPROTONOSUPPORT
:
2264 * Linux 2.2 (and maybe others) return EINVAL instead of
2268 return (ISC_R_FAMILYNOSUPPORT
);
2271 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2272 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2274 isc_msgcat_get(isc_msgcat
,
2279 return (ISC_R_UNEXPECTED
);
2283 if (make_nonblock(sock
->fd
) != ISC_R_SUCCESS
) {
2284 (void)close(sock
->fd
);
2285 return (ISC_R_UNEXPECTED
);
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",
2298 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
2299 ISC_MSG_FAILED
, "failed"),
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",
2312 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
2313 ISC_MSG_FAILED
, "failed"),
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",
2331 isc_msgcat_get(isc_msgcat
,
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
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) "
2360 isc_msgcat_get(isc_msgcat
,
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",
2375 isc_msgcat_get(isc_msgcat
,
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
,
2388 (void *)&on
, sizeof(on
));
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
));
2404 #if defined(IP_DONTFRAG)
2406 * Turn off Path MTU discovery on IPv4/UDP sockets.
2408 if (sock
->pf
== AF_INET
) {
2410 (void)setsockopt(sock
->fd
, IPPROTO_IP
, IP_DONTFRAG
,
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
) {
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",
2427 isc_msgcat_get(isc_msgcat
,
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
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
;
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
)
2466 switch (sock
->type
) {
2467 case isc_sockettype_udp
:
2469 (pf
== AF_INET
) ? upd4statsindex
: upd6statsindex
;
2471 case isc_sockettype_tcp
:
2473 (pf
== AF_INET
) ? tcp4statsindex
: tcp6statsindex
;
2475 case isc_sockettype_unix
:
2476 sock
->statsindex
= unixstatsindex
;
2483 result
= opensocket(manager
, sock
);
2484 if (result
!= ISC_R_SUCCESS
) {
2485 inc_stats(manager
->stats
, sock
->statsindex
[STATID_OPENFAIL
]);
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
;
2504 INSIST(sock
->manager
->fdpollinfo
[sock
->fd
].want_read
== 0 &&
2505 sock
->manager
->fdpollinfo
[sock
->fd
].want_write
== 0);
2507 UNLOCK(&manager
->fdlock
[lockid
]);
2509 LOCK(&manager
->lock
);
2510 ISC_LIST_APPEND(manager
->socklist
, sock
, link
);
2512 if (manager
->maxfd
< sock
->fd
)
2513 manager
->maxfd
= sock
->fd
;
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
);
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
));
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
2539 REQUIRE(sock
->fd
== -1);
2541 result
= opensocket(sock
->manager
, sock
);
2542 if (result
!= ISC_R_SUCCESS
)
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
;
2552 INSIST(sock
->manager
->fdpollinfo
[sock
->fd
].want_read
== 0 &&
2553 sock
->manager
->fdpollinfo
[sock
->fd
].want_write
== 0);
2555 UNLOCK(&sock
->manager
->fdlock
[lockid
]);
2558 LOCK(&sock
->manager
->lock
);
2559 if (sock
->manager
->maxfd
< sock
->fd
)
2560 sock
->manager
->maxfd
= sock
->fd
;
2561 UNLOCK(&sock
->manager
->lock
);
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
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
;
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
)
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
);
2617 if (manager
->maxfd
< sock
->fd
)
2618 manager
->maxfd
= sock
->fd
;
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) {
2654 if (((flags
& ISC_SOCKFDWATCH_READ
) != 0) &&
2655 !sock
->pending_recv
)
2656 select_poke(sock
->manager
, sock
->fd
,
2658 if (((flags
& ISC_SOCKFDWATCH_WRITE
) != 0) &&
2659 !sock
->pending_send
)
2660 select_poke(sock
->manager
, sock
->fd
,
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
);
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
));
2702 REQUIRE(sock
->references
> 0);
2704 if (sock
->references
== 0)
2705 kill_socket
= ISC_TRUE
;
2706 UNLOCK(&sock
->lock
);
2715 ISC_SOCKETFUNC_SCOPE isc_result_t
2716 isc__socket_close(isc_socket_t
*sock0
) {
2717 isc__socket_t
*sock
= (isc__socket_t
*)sock0
;
2719 isc__socketmgr_t
*manager
;
2720 isc_sockettype_t type
;
2722 REQUIRE(VALID_SOCKET(sock
));
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
;
2743 memset(sock
->name
, 0, sizeof(sock
->name
));
2746 sock
->connected
= 0;
2747 sock
->connecting
= 0;
2749 isc_sockaddr_any(&sock
->peer_address
);
2751 UNLOCK(&sock
->lock
);
2753 closesocket(manager
, sock
, fd
);
2755 return (ISC_R_SUCCESS
);
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.
2768 dispatch_recv(isc__socket_t
*sock
) {
2770 isc_socketevent_t
*ev
;
2773 INSIST(!sock
->pending_recv
);
2775 if (sock
->type
!= isc_sockettype_fdwatch
) {
2776 ev
= ISC_LIST_HEAD(sock
->recv_list
);
2779 socket_log(sock
, NULL
, EVENT
, NULL
, 0, 0,
2780 "dispatch_recv: event %p -> task %p",
2782 sender
= ev
->ev_sender
;
2784 sender
= sock
->fdwatchtask
;
2787 sock
->pending_recv
= 1;
2788 iev
= &sock
->readable_ev
;
2791 iev
->ev_sender
= sock
;
2792 if (sock
->type
== isc_sockettype_fdwatch
)
2793 iev
->ev_action
= internal_fdwatch_read
;
2795 iev
->ev_action
= internal_recv
;
2798 isc_task_send(sender
, (isc_event_t
**)&iev
);
2802 dispatch_send(isc__socket_t
*sock
) {
2804 isc_socketevent_t
*ev
;
2807 INSIST(!sock
->pending_send
);
2809 if (sock
->type
!= isc_sockettype_fdwatch
) {
2810 ev
= ISC_LIST_HEAD(sock
->send_list
);
2813 socket_log(sock
, NULL
, EVENT
, NULL
, 0, 0,
2814 "dispatch_send: event %p -> task %p",
2816 sender
= ev
->ev_sender
;
2818 sender
= sock
->fdwatchtask
;
2821 sock
->pending_send
= 1;
2822 iev
= &sock
->writable_ev
;
2825 iev
->ev_sender
= sock
;
2826 if (sock
->type
== isc_sockettype_fdwatch
)
2827 iev
->ev_action
= internal_fdwatch_write
;
2829 iev
->ev_action
= internal_send
;
2832 isc_task_send(sender
, (isc_event_t
**)&iev
);
2836 * Dispatch an internal accept event.
2839 dispatch_accept(isc__socket_t
*sock
) {
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
);
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
;
2861 isc_task_send(ev
->ev_sender
, (isc_event_t
**)&iev
);
2865 dispatch_connect(isc__socket_t
*sock
) {
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
;
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
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.
2895 send_recvdone_event(isc__socket_t
*sock
, isc_socketevent_t
**dev
) {
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
);
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.
2918 send_senddone_event(isc__socket_t
*sock
, isc_socketevent_t
**dev
) {
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
);
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.
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
;
2953 ISC_SOCKADDR_LEN_T addrlen
;
2955 isc_result_t result
= ISC_R_SUCCESS
;
2956 char strbuf
[ISC_STRERRORSIZE
];
2957 const char *err
= "accept";
2961 sock
= ev
->ev_sender
;
2962 INSIST(VALID_SOCKET(sock
));
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
);
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
);
2990 UNLOCK(&sock
->lock
);
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
,
3012 * Leave a space for stdio to work in.
3014 if (fd
>= 0 && fd
< 20) {
3016 new = fcntl(fd
, F_DUPFD
, 20);
3021 err
= "accept/fcntl";
3026 if (SOFT_ERROR(errno
))
3031 isc_log_iwrite(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
3032 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
3033 isc_msgcat
, ISC_MSGSET_SOCKET
,
3035 "%s: too many open file descriptors",
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
,
3067 result
= ISC_R_UNEXPECTED
;
3069 if (addrlen
== 0U) {
3070 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3071 "internal_accept(): "
3072 "accept() failed to return "
3077 } else if (NEWCONNSOCK(dev
)->peer_address
.type
.sa
.sa_family
!=
3080 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3081 "internal_accept(): "
3082 "accept() returned peer address "
3083 "family %u (expected %u)",
3084 NEWCONNSOCK(dev
)->peer_address
.
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
,
3095 "file descriptor exceeds limit (%d/%u)",
3096 fd
, manager
->maxsocks
);
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
)) {
3123 result
= ISC_R_UNEXPECTED
;
3127 * -1 means the new socket didn't happen.
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
;
3150 if (manager
->maxfd
< fd
)
3151 manager
->maxfd
= fd
;
3154 socket_log(sock
, &NEWCONNSOCK(dev
)->peer_address
, CREATION
,
3155 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_ACCEPTEDCXN
,
3156 "accepted connection, new socket %p",
3159 UNLOCK(&manager
->lock
);
3161 inc_stats(manager
->stats
, sock
->statsindex
[STATID_ACCEPT
]);
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
));
3179 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_ACCEPT
);
3180 UNLOCK(&sock
->lock
);
3182 inc_stats(manager
->stats
, sock
->statsindex
[STATID_ACCEPTFAIL
]);
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
));
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
);
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
)) {
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.
3229 dev
->result
= ISC_R_EOF
;
3230 send_recvdone_event(sock
, &dev
);
3231 dev
= ISC_LIST_HEAD(sock
->recv_list
);
3232 } while (dev
!= NULL
);
3237 send_recvdone_event(sock
, &dev
);
3241 dev
= ISC_LIST_HEAD(sock
->recv_list
);
3245 if (!ISC_LIST_EMPTY(sock
->recv_list
))
3246 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_READ
);
3248 UNLOCK(&sock
->lock
);
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
));
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
);
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
)) {
3292 send_senddone_event(sock
, &dev
);
3296 dev
= ISC_LIST_HEAD(sock
->send_list
);
3300 if (!ISC_LIST_EMPTY(sock
->send_list
))
3301 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_WRITE
);
3303 UNLOCK(&sock
->lock
);
3307 internal_fdwatch_write(isc_task_t
*me
, isc_event_t
*ev
) {
3308 isc__socket_t
*sock
;
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
));
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
);
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
);
3342 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_WRITE
);
3344 UNLOCK(&sock
->lock
);
3348 internal_fdwatch_read(isc_task_t
*me
, isc_event_t
*ev
) {
3349 isc__socket_t
*sock
;
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
));
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
);
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
);
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.
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
);
3413 sock
= manager
->fds
[fd
];
3414 unlock_sock
= ISC_FALSE
;
3417 unwatch_read
= ISC_TRUE
;
3420 unlock_sock
= ISC_TRUE
;
3422 if (!SOCK_DEAD(sock
)) {
3424 dispatch_accept(sock
);
3426 dispatch_recv(sock
);
3428 unwatch_read
= ISC_TRUE
;
3433 unwatch_write
= ISC_TRUE
;
3437 unlock_sock
= ISC_TRUE
;
3440 if (!SOCK_DEAD(sock
)) {
3441 if (sock
->connecting
)
3442 dispatch_connect(sock
);
3444 dispatch_send(sock
);
3446 unwatch_write
= ISC_TRUE
;
3449 UNLOCK(&sock
->lock
);
3452 UNLOCK(&manager
->fdlock
[lockid
]);
3454 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
3456 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
3461 static isc_boolean_t
3462 process_fds(isc__socketmgr_t
*manager
, struct kevent
*events
, int nevents
) {
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
;
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",
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
;
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
3497 done
= process_ctlfd(manager
);
3502 #elif defined(USE_EPOLL)
3503 static isc_boolean_t
3504 process_fds(isc__socketmgr_t
*manager
, struct epoll_event
*events
, int nevents
)
3507 isc_boolean_t done
= ISC_FALSE
;
3508 #ifdef USE_WATCHER_THREAD
3509 isc_boolean_t have_ctlevent
= ISC_FALSE
;
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",
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
;
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
3545 done
= process_ctlfd(manager
);
3550 #elif defined(USE_DEVPOLL)
3551 static isc_boolean_t
3552 process_fds(isc__socketmgr_t
*manager
, struct pollfd
*events
, int nevents
) {
3554 isc_boolean_t done
= ISC_FALSE
;
3555 #ifdef USE_WATCHER_THREAD
3556 isc_boolean_t have_ctlevent
= ISC_FALSE
;
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",
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
;
3574 process_fd(manager
, events
[i
].fd
,
3575 (events
[i
].events
& POLLIN
) != 0,
3576 (events
[i
].events
& POLLOUT
) != 0);
3579 #ifdef USE_WATCHER_THREAD
3581 done
= process_ctlfd(manager
);
3586 #elif defined(USE_SELECT)
3588 process_fds(isc__socketmgr_t
*manager
, int maxfd
, fd_set
*readfds
,
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])
3599 #endif /* USE_WATCHER_THREAD */
3600 process_fd(manager
, i
, FD_ISSET(i
, readfds
),
3601 FD_ISSET(i
, writefds
));
3606 #ifdef USE_WATCHER_THREAD
3607 static isc_boolean_t
3608 process_ctlfd(isc__socketmgr_t
*manager
) {
3612 select_readmsg(manager
, &fd
, &msg
);
3614 manager_log(manager
, IOEVENT
,
3615 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
3617 "watcher got message %d "
3618 "for socket %d"), msg
, fd
);
3623 if (msg
== SELECT_POKE_NOTHING
)
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
3632 if (msg
== SELECT_POKE_SHUTDOWN
)
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
3641 wakeup_socket(manager
, fd
, msg
);
3648 * This is the thread that will loop forever, always in a select or poll
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
;
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)";
3667 #elif defined (USE_SELECT)
3668 const char *fnname
= "select()";
3671 char strbuf
[ISC_STRERRORSIZE
];
3672 #ifdef ISC_SOCKET_USE_POLLWATCH
3673 pollstate_t pollstate
= poll_idle
;
3677 * Get the control fd here. This will never change.
3679 ctlfd
= manager
->pipe_fds
[0];
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;
3695 if (pollstate
== poll_idle
)
3696 dvp
.dp_timeout
= -1;
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
,
3724 #if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH)
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
,
3744 "unexpected POLL timeout");
3746 pollstate
= poll_active
;
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
);
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 */
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
;
3794 * Create a new socket manager.
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
];
3805 manager
->nevents
= ISC_SOCKET_MAXEVENTS
;
3806 manager
->events
= isc_mem_get(mctx
, sizeof(struct kevent
) *
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__
,
3816 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3817 ISC_MSG_FAILED
, "failed"),
3819 isc_mem_put(mctx
, manager
->events
,
3820 sizeof(struct kevent
) * manager
->nevents
);
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
);
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
) *
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"),
3848 isc_mem_put(mctx
, manager
->events
,
3849 sizeof(struct epoll_event
) * manager
->nevents
);
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
);
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
) *
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
) *
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"),
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
);
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
);
3908 #endif /* USE_WATCHER_THREAD */
3909 #elif defined(USE_SELECT)
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
) *
3921 manager
->fd_bufsize
= sizeof(fd_set
);
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 */
3961 #endif /* USE_WATCHER_THREAD */
3962 #endif /* USE_KQUEUE */
3964 return (ISC_R_SUCCESS
);
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 */
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
)
4017 isc__socketmgr_t
*manager
;
4018 #ifdef USE_WATCHER_THREAD
4019 char strbuf
[ISC_STRERRORSIZE
];
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
);
4032 *managerp
= (isc_socketmgr_t
*)socketmgr
;
4033 return (ISC_R_SUCCESS
);
4035 #endif /* USE_SHARED_MANAGER */
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
;
4055 manager
->fdstate
= isc_mem_get(mctx
, manager
->maxsocks
* sizeof(int));
4056 if (manager
->fdstate
== NULL
) {
4057 result
= ISC_R_NOMEMORY
;
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
)
4071 manager
->fdlock
= isc_mem_get(mctx
, FDLOCK_COUNT
* sizeof(isc_mutex_t
));
4072 if (manager
->fdlock
== NULL
) {
4073 result
= ISC_R_NOMEMORY
;
4076 for (i
= 0; i
< FDLOCK_COUNT
; i
++) {
4077 result
= isc_mutex_init(&manager
->fdlock
[i
]);
4078 if (result
!= ISC_R_SUCCESS
) {
4080 DESTROYLOCK(&manager
->fdlock
[i
]);
4081 isc_mem_put(mctx
, manager
->fdlock
,
4082 FDLOCK_COUNT
* sizeof(isc_mutex_t
));
4083 manager
->fdlock
= NULL
;
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
;
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__
,
4106 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
4107 ISC_MSG_FAILED
, "failed"),
4109 result
= ISC_R_UNEXPECTED
;
4110 goto cleanup_condition
;
4113 RUNTIME_CHECK(make_nonblock(manager
->pipe_fds
[0]) == ISC_R_SUCCESS
);
4115 RUNTIME_CHECK(make_nonblock(manager
->pipe_fds
[1]) == ISC_R_SUCCESS
);
4117 #endif /* USE_WATCHER_THREAD */
4119 #ifdef USE_SHARED_MANAGER
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
)
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
) !=
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
;
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
);
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
4162 (void)isc_condition_destroy(&manager
->shutdown_ok
);
4163 #endif /* USE_WATCHER_THREAD */
4167 if (manager
->fdlock
!= NULL
) {
4168 for (i
= 0; i
< FDLOCK_COUNT
; i
++)
4169 DESTROYLOCK(&manager
->fdlock
[i
]);
4171 DESTROYLOCK(&manager
->lock
);
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
));
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
);
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
);
4216 ISC_SOCKETFUNC_SCOPE
void
4217 isc__socketmgr_destroy(isc_socketmgr_t
**managerp
) {
4218 isc__socketmgr_t
*manager
;
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) {
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
,
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
,
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 */
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 */
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
);
4323 #ifdef USE_SHARED_MANAGER
4329 socket_recv(isc__socket_t
*sock
, isc_socketevent_t
*dev
, isc_task_t
*task
,
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
);
4343 have_lock
= ISC_TRUE
;
4345 if (ISC_LIST_EMPTY(sock
->recv_list
))
4346 io_state
= doio_recv(sock
, dev
);
4348 io_state
= DOIO_SOFT
;
4354 * We couldn't read all or part of the request right now, so
4357 * Attach to socket and to task
4359 isc_task_attach(task
, &ntask
);
4360 dev
->attributes
|= ISC_SOCKEVENTATTR_ATTACHED
;
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",
4379 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) != 0)
4380 result
= ISC_R_INPROGRESS
;
4384 dev
->result
= ISC_R_EOF
;
4389 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) == 0)
4390 send_recvdone_event(sock
, &dev
);
4395 UNLOCK(&sock
->lock
);
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
);
4427 return (ISC_R_NOMEMORY
);
4431 * UDP sockets are always partial read
4433 if (sock
->type
== isc_sockettype_udp
)
4437 dev
->minimum
= iocount
;
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
);
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
;
4492 event
->attributes
= 0;
4495 * UDP sockets are always partial read.
4497 if (sock
->type
== isc_sockettype_udp
)
4501 event
->minimum
= region
->length
;
4503 event
->minimum
= minimum
;
4506 return (socket_recv(sock
, event
, task
, flags
));
4510 socket_send(isc__socket_t
*sock
, isc_socketevent_t
*dev
, isc_task_t
*task
,
4511 isc_sockaddr_t
*address
, struct in6_pktinfo
*pktinfo
,
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
);
4545 have_lock
= ISC_TRUE
;
4547 if (ISC_LIST_EMPTY(sock
->send_list
))
4548 io_state
= doio_send(sock
, dev
);
4550 io_state
= 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
;
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
,
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",
4583 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) != 0)
4584 result
= ISC_R_INPROGRESS
;
4590 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) == 0)
4591 send_senddone_event(sock
, &dev
);
4596 UNLOCK(&sock
->lock
);
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
,
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
);
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
,
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
);
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
,
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
;
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
4718 char strbuf
[ISC_STRERRORSIZE
];
4720 if (sockaddr
->type
.sa
.sa_family
!= AF_UNIX
)
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)
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)
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>.
4744 #define S_ISFIFO(mode) 0
4748 #define S_ISSOCK(mode) 0
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
);
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
);
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
);
4777 s
= socket(AF_UNIX
, SOCK_STREAM
, 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
);
4787 if (stat(sockaddr
->type
.sunix
.sun_path
, &sb
) < 0) {
4789 case ENOENT
: /* We exited cleanly last time */
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
);
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
);
4810 if (connect(s
, (struct sockaddr
*)&sockaddr
->type
.sunix
,
4811 sizeof(sockaddr
->type
.sunix
)) < 0) {
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
,
4820 "isc_socket_cleanunix: "
4822 sockaddr
->type
.sunix
.sun_path
,
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
);
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
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
) {
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",
4885 result
= ISC_R_FAILURE
;
4893 return (ISC_R_NOTIMPLEMENTED
);
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
];
4904 REQUIRE(VALID_SOCKET(sock
));
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.
4918 if (sock
->pf
== AF_UNIX
)
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
,
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"));
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
);
4941 return (ISC_R_NOPERM
);
4943 return (ISC_R_ADDRNOTAVAIL
);
4945 return (ISC_R_ADDRINUSE
);
4947 return (ISC_R_BOUND
);
4949 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4950 UNEXPECTED_ERROR(__FILE__
, __LINE__
, "bind: %s",
4952 return (ISC_R_UNEXPECTED
);
4956 socket_log(sock
, sockaddr
, TRACE
,
4957 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_BOUND
, "bound");
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
;
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",
4986 return (ISC_R_FAILURE
);
4988 return (ISC_R_SUCCESS
);
4990 return (ISC_R_NOTIMPLEMENTED
);
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
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
));
5013 REQUIRE(!sock
->listener
);
5014 REQUIRE(sock
->bound
);
5015 REQUIRE(sock
->type
== isc_sockettype_tcp
||
5016 sock
->type
== isc_sockettype_unix
);
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
);
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
));
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
));
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
);
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
))
5098 ISC_LIST_ENQUEUE(sock
->accept_list
, dev
, ev_link
);
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
;
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
);
5133 REQUIRE(!sock
->connecting
);
5135 dev
= (isc_socket_connev_t
*)isc_event_allocate(manager
->mctx
, sock
,
5136 ISC_SOCKEVENT_CONNECT
,
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
);
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
) {
5162 if (SOFT_ERROR(errno
) || errno
== EINPROGRESS
)
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
);
5173 ERROR_MATCH(EHOSTDOWN
, ISC_R_HOSTUNREACH
);
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
);
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
);
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.
5211 sock
->connected
= 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
);
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.
5253 internal_connect(isc_task_t
*me
, isc_event_t
*ev
) {
5254 isc__socket_t
*sock
;
5255 isc_socket_connev_t
*dev
;
5258 ISC_SOCKADDR_LEN_T optlen
;
5259 char strbuf
[ISC_STRERRORSIZE
];
5260 char peerbuf
[ISC_SOCKADDR_FORMATSIZE
];
5263 INSIST(ev
->ev_type
== ISC_SOCKEVENT_INTW
);
5265 sock
= ev
->ev_sender
;
5266 INSIST(VALID_SOCKET(sock
));
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);
5276 if (sock
->references
== 0) {
5277 UNLOCK(&sock
->lock
);
5283 * Has this event been canceled?
5285 dev
= sock
->connect_ev
;
5287 INSIST(!sock
->connecting
);
5288 UNLOCK(&sock
->lock
);
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)
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
);
5319 inc_stats(sock
->manager
->stats
,
5320 sock
->statsindex
[STATID_CONNECTFAIL
]);
5323 * Translate other errors into ISC_R_* flavors.
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
);
5333 ERROR_MATCH(EHOSTDOWN
, ISC_R_HOSTUNREACH
);
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
);
5343 dev
->result
= ISC_R_UNEXPECTED
;
5344 isc_sockaddr_format(&sock
->peer_address
, peerbuf
,
5346 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
5347 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
5348 "internal_connect: connect(%s) %s",
5352 inc_stats(sock
->manager
->stats
,
5353 sock
->statsindex
[STATID_CONNECT
]);
5354 dev
->result
= ISC_R_SUCCESS
;
5355 sock
->connected
= 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
);
5378 if (sock
->connected
) {
5379 *addressp
= sock
->peer_address
;
5380 result
= ISC_R_SUCCESS
;
5382 result
= ISC_R_NOTCONNECTED
;
5385 UNLOCK(&sock
->lock
);
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
);
5403 result
= ISC_R_NOTBOUND
;
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",
5414 result
= ISC_R_UNEXPECTED
;
5417 addressp
->length
= (unsigned int)len
;
5420 UNLOCK(&sock
->lock
);
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
5445 * All of these do the same thing, more or less.
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
);
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
);
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
,
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(¤t_task
,
5516 ISC_EVENT_PTR(&dev
));
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(¤t_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
;
5564 REQUIRE(VALID_SOCKET(sock
));
5567 val
= ((sock
->bound
) ? ISC_TRUE
: ISC_FALSE
);
5568 UNLOCK(&sock
->lock
);
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;
5583 REQUIRE(VALID_SOCKET(sock
));
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) "
5594 isc_msgcat_get(isc_msgcat
,
5601 FIX_IPV6_RECVPKTINFO(sock
); /* AIX */
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
;
5622 struct timespec ts
, *tsp
;
5631 REQUIRE(swaitp
!= NULL
&& *swaitp
== NULL
);
5633 #ifdef USE_SHARED_MANAGER
5634 if (manager
== NULL
)
5635 manager
= socketmgr
;
5637 if (manager
== NULL
)
5642 ts
.tv_sec
= tvp
->tv_sec
;
5643 ts
.tv_nsec
= tvp
->tv_usec
* 1000;
5647 swait_private
.nevents
= kevent(manager
->kqueue_fd
, NULL
, 0,
5648 manager
->events
, manager
->nevents
,
5650 n
= swait_private
.nevents
;
5651 #elif defined(USE_EPOLL)
5653 timeout
= tvp
->tv_sec
* 1000 + (tvp
->tv_usec
+ 999) / 1000;
5656 swait_private
.nevents
= epoll_wait(manager
->epoll_fd
,
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
;
5664 dvp
.dp_timeout
= tvp
->tv_sec
* 1000 +
5665 (tvp
->tv_usec
+ 999) / 1000;
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
);
5683 *swaitp
= &swait_private
;
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
;
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
);
5708 #endif /* USE_WATCHER_THREAD */
5712 isc__socket_setname(isc_socket_t
*socket0
, const char *name
, void *tag
) {
5713 isc__socket_t
*socket
= (isc__socket_t
*)socket0
;
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);
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
);
5736 isc__socket_gettag(isc_socket_t
*socket0
) {
5737 isc__socket_t
*socket
= (isc__socket_t
*)socket0
;
5739 return (socket
->tag
);
5743 #ifdef USE_SOCKETIMPREGISTER
5745 isc__socket_register() {
5746 return (isc_socket_register(isc__socketmgr_create
));
5750 #if defined(HAVE_LIBXML2) && defined(BIND9)
5753 _socktype(isc_sockettype_t type
)
5755 if (type
== isc_sockettype_udp
)
5757 else if (type
== isc_sockettype_tcp
)
5759 else if (type
== isc_sockettype_unix
)
5761 else if (type
== isc_sockettype_fdwatch
)
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
;
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
) {
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",
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
,
5810 xmlTextWriterWriteElement(writer
,
5811 ISC_XMLCHAR
"peer-address",
5812 ISC_XMLCHAR peerbuf
);
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");
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");
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 */
5857 #endif /* HAVE_LIBXML2 */