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.308.12.8 2009/04/18 01:29:26 jinmei 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 #ifndef ISC_PLATFORM_USETHREADS
79 #endif /* ISC_PLATFORM_USETHREADS */
81 #if defined(SO_BSDCOMPAT) && defined(__linux__)
82 #include <sys/utsname.h>
86 * Choose the most preferable multiplex method.
88 #ifdef ISC_PLATFORM_HAVEKQUEUE
90 #elif defined (ISC_PLATFORM_HAVEEPOLL)
92 #elif defined (ISC_PLATFORM_HAVEDEVPOLL)
95 unsigned int want_read
: 1,
100 #endif /* ISC_PLATFORM_HAVEKQUEUE */
102 #ifndef ISC_PLATFORM_USETHREADS
103 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
104 struct isc_socketwait
{
107 #elif defined (USE_SELECT)
108 struct isc_socketwait
{
114 #endif /* USE_KQUEUE */
115 #endif /* !ISC_PLATFORM_USETHREADS */
118 * Maximum number of allowable open sockets. This is also the maximum
119 * allowable socket file descriptor.
121 * Care should be taken before modifying this value for select():
122 * The API standard doesn't ensure select() accept more than (the system default
123 * of) FD_SETSIZE descriptors, and the default size should in fact be fine in
124 * the vast majority of cases. This constant should therefore be increased only
125 * when absolutely necessary and possible, i.e., the server is exhausting all
126 * available file descriptors (up to FD_SETSIZE) and the select() function
127 * and FD_xxx macros support larger values than FD_SETSIZE (which may not
128 * always by true, but we keep using some of them to ensure as much
129 * portability as possible). Note also that overall server performance
130 * may be rather worsened with a larger value of this constant due to
131 * inherent scalability problems of select().
133 * As a special note, this value shouldn't have to be touched if
134 * this is a build for an authoritative only DNS server.
136 #ifndef ISC_SOCKET_MAXSOCKETS
137 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
138 #define ISC_SOCKET_MAXSOCKETS 4096
139 #elif defined(USE_SELECT)
140 #define ISC_SOCKET_MAXSOCKETS FD_SETSIZE
141 #endif /* USE_KQUEUE... */
142 #endif /* ISC_SOCKET_MAXSOCKETS */
146 * Mac OS X needs a special definition to support larger values in select().
147 * We always define this because a larger value can be specified run-time.
150 #define _DARWIN_UNLIMITED_SELECT
151 #endif /* __APPLE__ */
152 #endif /* USE_SELECT */
154 #ifdef ISC_SOCKET_USE_POLLWATCH
156 * If this macro is defined, enable workaround for a Solaris /dev/poll kernel
157 * bug: DP_POLL ioctl could keep sleeping even if socket I/O is possible for
158 * some of the specified FD. The idea is based on the observation that it's
159 * likely for a busy server to keep receiving packets. It specifically works
160 * as follows: the socket watcher is first initialized with the state of
161 * "poll_idle". While it's in the idle state it keeps sleeping until a socket
162 * event occurs. When it wakes up for a socket I/O event, it moves to the
163 * poll_active state, and sets the poll timeout to a short period
164 * (ISC_SOCKET_POLLWATCH_TIMEOUT msec). If timeout occurs in this state, the
165 * watcher goes to the poll_checking state with the same timeout period.
166 * In this state, the watcher tries to detect whether this is a break
167 * during intermittent events or the kernel bug is triggered. If the next
168 * polling reports an event within the short period, the previous timeout is
169 * likely to be a kernel bug, and so the watcher goes back to the active state.
170 * Otherwise, it moves to the idle state again.
172 * It's not clear whether this is a thread-related bug, but since we've only
173 * seen this with threads, this workaround is used only when enabling threads.
176 typedef enum { poll_idle
, poll_active
, poll_checking
} pollstate_t
;
178 #ifndef ISC_SOCKET_POLLWATCH_TIMEOUT
179 #define ISC_SOCKET_POLLWATCH_TIMEOUT 10
180 #endif /* ISC_SOCKET_POLLWATCH_TIMEOUT */
181 #endif /* ISC_SOCKET_USE_POLLWATCH */
184 * Size of per-FD lock buckets.
186 #ifdef ISC_PLATFORM_USETHREADS
187 #define FDLOCK_COUNT 1024
188 #define FDLOCK_ID(fd) ((fd) % FDLOCK_COUNT)
190 #define FDLOCK_COUNT 1
191 #define FDLOCK_ID(fd) 0
192 #endif /* ISC_PLATFORM_USETHREADS */
195 * Maximum number of events communicated with the kernel. There should normally
196 * be no need for having a large number.
198 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
199 #ifndef ISC_SOCKET_MAXEVENTS
200 #define ISC_SOCKET_MAXEVENTS 64
205 * Some systems define the socket length argument as an int, some as size_t,
206 * some as socklen_t. This is here so it can be easily changed if needed.
208 #ifndef ISC_SOCKADDR_LEN_T
209 #define ISC_SOCKADDR_LEN_T unsigned int
213 * Define what the possible "soft" errors can be. These are non-fatal returns
214 * of various network related functions, like recv() and so on.
216 * For some reason, BSDI (and perhaps others) will sometimes return <0
217 * from recv() but will have errno==0. This is broken, but we have to
218 * work around it here.
220 #define SOFT_ERROR(e) ((e) == EAGAIN || \
221 (e) == EWOULDBLOCK || \
225 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
228 * DLVL(90) -- Function entry/exit and other tracing.
229 * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
230 * DLVL(60) -- Socket data send/receive
231 * DLVL(50) -- Event tracing, including receiving/sending completion events.
232 * DLVL(20) -- Socket creation/destruction.
234 #define TRACE_LEVEL 90
235 #define CORRECTNESS_LEVEL 70
236 #define IOEVENT_LEVEL 60
237 #define EVENT_LEVEL 50
238 #define CREATION_LEVEL 20
240 #define TRACE DLVL(TRACE_LEVEL)
241 #define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
242 #define IOEVENT DLVL(IOEVENT_LEVEL)
243 #define EVENT DLVL(EVENT_LEVEL)
244 #define CREATION DLVL(CREATION_LEVEL)
246 typedef isc_event_t intev_t
;
248 #define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
249 #define VALID_SOCKET(t) ISC_MAGIC_VALID(t, SOCKET_MAGIC)
252 * IPv6 control information. If the socket is an IPv6 socket we want
253 * to collect the destination address and interface so the client can
254 * set them on outgoing packets.
256 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
263 * NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
264 * a setsockopt() like interface to request timestamps, and if the OS
265 * doesn't do it for us, call gettimeofday() on every UDP receive?
274 * The size to raise the receive buffer to (from BIND 8).
276 #define RCVBUFSIZE (32*1024)
279 * The number of times a send operation is repeated if the result is EINTR.
286 isc_socketmgr_t
*manager
;
288 isc_sockettype_t type
;
289 const isc_statscounter_t
*statsindex
;
291 /* Locked by socket lock. */
292 ISC_LINK(isc_socket_t
) link
;
293 unsigned int references
;
299 ISC_LIST(isc_socketevent_t
) send_list
;
300 ISC_LIST(isc_socketevent_t
) recv_list
;
301 ISC_LIST(isc_socket_newconnev_t
) accept_list
;
302 isc_socket_connev_t
*connect_ev
;
305 * Internal events. Posted when a descriptor is readable or
306 * writable. These are statically allocated and never freed.
307 * They will be set to non-purgable before use.
312 isc_sockaddr_t peer_address
; /* remote address */
314 unsigned int pending_recv
: 1,
317 listener
: 1, /* listener socket */
319 connecting
: 1, /* connect pending */
320 bound
: 1; /* bound to local addr */
322 #ifdef ISC_NET_RECVOVERFLOW
323 unsigned char overflow
; /* used for MSG_TRUNC fake */
327 ISC_SOCKADDR_LEN_T recvcmsgbuflen
;
329 ISC_SOCKADDR_LEN_T sendcmsgbuflen
;
332 isc_sockfdwatch_t fdwatchcb
;
334 isc_task_t
*fdwatchtask
;
337 #define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
338 #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
340 struct isc_socketmgr
{
350 struct kevent
*events
;
351 #endif /* USE_KQUEUE */
355 struct epoll_event
*events
;
356 #endif /* USE_EPOLL */
360 struct pollfd
*events
;
361 #endif /* USE_DEVPOLL */
364 #endif /* USE_SELECT */
365 unsigned int maxsocks
;
366 #ifdef ISC_PLATFORM_USETHREADS
370 /* Locked by fdlock. */
374 pollinfo_t
*fdpollinfo
;
377 /* Locked by manager lock. */
378 ISC_LIST(isc_socket_t
) socklist
;
381 fd_set
*read_fds_copy
;
383 fd_set
*write_fds_copy
;
385 #endif /* USE_SELECT */
386 int reserved
; /* unlocked */
387 #ifdef ISC_PLATFORM_USETHREADS
388 isc_thread_t watcher
;
389 isc_condition_t shutdown_ok
;
390 #else /* ISC_PLATFORM_USETHREADS */
392 #endif /* ISC_PLATFORM_USETHREADS */
395 #ifndef ISC_PLATFORM_USETHREADS
396 static isc_socketmgr_t
*socketmgr
= NULL
;
397 #endif /* ISC_PLATFORM_USETHREADS */
399 #define CLOSED 0 /* this one must be zero */
401 #define CLOSE_PENDING 2
404 * send() and recv() iovec counts
406 #define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
407 #ifdef ISC_NET_RECVOVERFLOW
408 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
410 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
413 static void send_recvdone_event(isc_socket_t
*, isc_socketevent_t
**);
414 static void send_senddone_event(isc_socket_t
*, isc_socketevent_t
**);
415 static void free_socket(isc_socket_t
**);
416 static isc_result_t
allocate_socket(isc_socketmgr_t
*, isc_sockettype_t
,
418 static void destroy(isc_socket_t
**);
419 static void internal_accept(isc_task_t
*, isc_event_t
*);
420 static void internal_connect(isc_task_t
*, isc_event_t
*);
421 static void internal_recv(isc_task_t
*, isc_event_t
*);
422 static void internal_send(isc_task_t
*, isc_event_t
*);
423 static void internal_fdwatch_write(isc_task_t
*, isc_event_t
*);
424 static void internal_fdwatch_read(isc_task_t
*, isc_event_t
*);
425 static void process_cmsg(isc_socket_t
*, struct msghdr
*, isc_socketevent_t
*);
426 static void build_msghdr_send(isc_socket_t
*, isc_socketevent_t
*,
427 struct msghdr
*, struct iovec
*, size_t *);
428 static void build_msghdr_recv(isc_socket_t
*, isc_socketevent_t
*,
429 struct msghdr
*, struct iovec
*, size_t *);
430 #ifdef ISC_PLATFORM_USETHREADS
431 static isc_boolean_t
process_ctlfd(isc_socketmgr_t
*manager
);
434 #define SELECT_POKE_SHUTDOWN (-1)
435 #define SELECT_POKE_NOTHING (-2)
436 #define SELECT_POKE_READ (-3)
437 #define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
438 #define SELECT_POKE_WRITE (-4)
439 #define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
440 #define SELECT_POKE_CLOSE (-5)
442 #define SOCK_DEAD(s) ((s)->references == 0)
445 * Shortcut index arrays to get access to statistics counters.
452 STATID_CONNECTFAIL
= 4,
454 STATID_ACCEPTFAIL
= 6,
459 static const isc_statscounter_t upd4statsindex
[] = {
460 isc_sockstatscounter_udp4open
,
461 isc_sockstatscounter_udp4openfail
,
462 isc_sockstatscounter_udp4close
,
463 isc_sockstatscounter_udp4bindfail
,
464 isc_sockstatscounter_udp4connectfail
,
465 isc_sockstatscounter_udp4connect
,
468 isc_sockstatscounter_udp4sendfail
,
469 isc_sockstatscounter_udp4recvfail
471 static const isc_statscounter_t upd6statsindex
[] = {
472 isc_sockstatscounter_udp6open
,
473 isc_sockstatscounter_udp6openfail
,
474 isc_sockstatscounter_udp6close
,
475 isc_sockstatscounter_udp6bindfail
,
476 isc_sockstatscounter_udp6connectfail
,
477 isc_sockstatscounter_udp6connect
,
480 isc_sockstatscounter_udp6sendfail
,
481 isc_sockstatscounter_udp6recvfail
483 static const isc_statscounter_t tcp4statsindex
[] = {
484 isc_sockstatscounter_tcp4open
,
485 isc_sockstatscounter_tcp4openfail
,
486 isc_sockstatscounter_tcp4close
,
487 isc_sockstatscounter_tcp4bindfail
,
488 isc_sockstatscounter_tcp4connectfail
,
489 isc_sockstatscounter_tcp4connect
,
490 isc_sockstatscounter_tcp4acceptfail
,
491 isc_sockstatscounter_tcp4accept
,
492 isc_sockstatscounter_tcp4sendfail
,
493 isc_sockstatscounter_tcp4recvfail
495 static const isc_statscounter_t tcp6statsindex
[] = {
496 isc_sockstatscounter_tcp6open
,
497 isc_sockstatscounter_tcp6openfail
,
498 isc_sockstatscounter_tcp6close
,
499 isc_sockstatscounter_tcp6bindfail
,
500 isc_sockstatscounter_tcp6connectfail
,
501 isc_sockstatscounter_tcp6connect
,
502 isc_sockstatscounter_tcp6acceptfail
,
503 isc_sockstatscounter_tcp6accept
,
504 isc_sockstatscounter_tcp6sendfail
,
505 isc_sockstatscounter_tcp6recvfail
507 static const isc_statscounter_t unixstatsindex
[] = {
508 isc_sockstatscounter_unixopen
,
509 isc_sockstatscounter_unixopenfail
,
510 isc_sockstatscounter_unixclose
,
511 isc_sockstatscounter_unixbindfail
,
512 isc_sockstatscounter_unixconnectfail
,
513 isc_sockstatscounter_unixconnect
,
514 isc_sockstatscounter_unixacceptfail
,
515 isc_sockstatscounter_unixaccept
,
516 isc_sockstatscounter_unixsendfail
,
517 isc_sockstatscounter_unixrecvfail
519 static const isc_statscounter_t fdwatchstatsindex
[] = {
522 isc_sockstatscounter_fdwatchclose
,
523 isc_sockstatscounter_fdwatchbindfail
,
524 isc_sockstatscounter_fdwatchconnectfail
,
525 isc_sockstatscounter_fdwatchconnect
,
528 isc_sockstatscounter_fdwatchsendfail
,
529 isc_sockstatscounter_fdwatchrecvfail
533 manager_log(isc_socketmgr_t
*sockmgr
,
534 isc_logcategory_t
*category
, isc_logmodule_t
*module
, int level
,
535 const char *fmt
, ...) ISC_FORMAT_PRINTF(5, 6);
537 manager_log(isc_socketmgr_t
*sockmgr
,
538 isc_logcategory_t
*category
, isc_logmodule_t
*module
, int level
,
539 const char *fmt
, ...)
544 if (! isc_log_wouldlog(isc_lctx
, level
))
548 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, ap
);
551 isc_log_write(isc_lctx
, category
, module
, level
,
552 "sockmgr %p: %s", sockmgr
, msgbuf
);
556 socket_log(isc_socket_t
*sock
, isc_sockaddr_t
*address
,
557 isc_logcategory_t
*category
, isc_logmodule_t
*module
, int level
,
558 isc_msgcat_t
*msgcat
, int msgset
, int message
,
559 const char *fmt
, ...) ISC_FORMAT_PRINTF(9, 10);
561 socket_log(isc_socket_t
*sock
, isc_sockaddr_t
*address
,
562 isc_logcategory_t
*category
, isc_logmodule_t
*module
, int level
,
563 isc_msgcat_t
*msgcat
, int msgset
, int message
,
564 const char *fmt
, ...)
567 char peerbuf
[ISC_SOCKADDR_FORMATSIZE
];
570 if (! isc_log_wouldlog(isc_lctx
, level
))
574 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, ap
);
577 if (address
== NULL
) {
578 isc_log_iwrite(isc_lctx
, category
, module
, level
,
579 msgcat
, msgset
, message
,
580 "socket %p: %s", sock
, msgbuf
);
582 isc_sockaddr_format(address
, peerbuf
, sizeof(peerbuf
));
583 isc_log_iwrite(isc_lctx
, category
, module
, level
,
584 msgcat
, msgset
, message
,
585 "socket %p %s: %s", sock
, peerbuf
, msgbuf
);
589 #if defined(_AIX) && defined(ISC_NET_BSD44MSGHDR) && \
590 defined(USE_CMSG) && defined(IPV6_RECVPKTINFO)
592 * AIX has a kernel bug where IPV6_RECVPKTINFO gets cleared by
593 * setting IPV6_V6ONLY.
596 FIX_IPV6_RECVPKTINFO(isc_socket_t
*sock
)
598 char strbuf
[ISC_STRERRORSIZE
];
601 if (sock
->pf
!= AF_INET6
|| sock
->type
!= isc_sockettype_udp
)
604 if (setsockopt(sock
->fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
605 (void *)&on
, sizeof(on
)) < 0) {
607 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
608 "setsockopt(%d, IPV6_RECVPKTINFO) "
610 isc_msgcat_get(isc_msgcat
,
618 #define FIX_IPV6_RECVPKTINFO(sock) (void)0
622 * Increment socket-related statistics counters.
625 inc_stats(isc_stats_t
*stats
, isc_statscounter_t counterid
) {
626 REQUIRE(counterid
!= -1);
629 isc_stats_increment(stats
, counterid
);
632 static inline isc_result_t
633 watch_fd(isc_socketmgr_t
*manager
, int fd
, int msg
) {
634 isc_result_t result
= ISC_R_SUCCESS
;
637 struct kevent evchange
;
639 memset(&evchange
, 0, sizeof(evchange
));
640 if (msg
== SELECT_POKE_READ
)
641 evchange
.filter
= EVFILT_READ
;
643 evchange
.filter
= EVFILT_WRITE
;
644 evchange
.flags
= EV_ADD
;
646 if (kevent(manager
->kqueue_fd
, &evchange
, 1, NULL
, 0, NULL
) != 0)
647 result
= isc__errno2result(errno
);
650 #elif defined(USE_EPOLL)
651 struct epoll_event event
;
653 if (msg
== SELECT_POKE_READ
)
654 event
.events
= EPOLLIN
;
656 event
.events
= EPOLLOUT
;
658 if (epoll_ctl(manager
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &event
) == -1 &&
660 result
= isc__errno2result(errno
);
664 #elif defined(USE_DEVPOLL)
666 int lockid
= FDLOCK_ID(fd
);
668 memset(&pfd
, 0, sizeof(pfd
));
669 if (msg
== SELECT_POKE_READ
)
672 pfd
.events
= POLLOUT
;
675 LOCK(&manager
->fdlock
[lockid
]);
676 if (write(manager
->devpoll_fd
, &pfd
, sizeof(pfd
)) == -1)
677 result
= isc__errno2result(errno
);
679 if (msg
== SELECT_POKE_READ
)
680 manager
->fdpollinfo
[fd
].want_read
= 1;
682 manager
->fdpollinfo
[fd
].want_write
= 1;
684 UNLOCK(&manager
->fdlock
[lockid
]);
687 #elif defined(USE_SELECT)
688 LOCK(&manager
->lock
);
689 if (msg
== SELECT_POKE_READ
)
690 FD_SET(fd
, manager
->read_fds
);
691 if (msg
== SELECT_POKE_WRITE
)
692 FD_SET(fd
, manager
->write_fds
);
693 UNLOCK(&manager
->lock
);
699 static inline isc_result_t
700 unwatch_fd(isc_socketmgr_t
*manager
, int fd
, int msg
) {
701 isc_result_t result
= ISC_R_SUCCESS
;
704 struct kevent evchange
;
706 memset(&evchange
, 0, sizeof(evchange
));
707 if (msg
== SELECT_POKE_READ
)
708 evchange
.filter
= EVFILT_READ
;
710 evchange
.filter
= EVFILT_WRITE
;
711 evchange
.flags
= EV_DELETE
;
713 if (kevent(manager
->kqueue_fd
, &evchange
, 1, NULL
, 0, NULL
) != 0)
714 result
= isc__errno2result(errno
);
717 #elif defined(USE_EPOLL)
718 struct epoll_event event
;
720 if (msg
== SELECT_POKE_READ
)
721 event
.events
= EPOLLIN
;
723 event
.events
= EPOLLOUT
;
725 if (epoll_ctl(manager
->epoll_fd
, EPOLL_CTL_DEL
, fd
, &event
) == -1 &&
727 char strbuf
[ISC_STRERRORSIZE
];
728 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
729 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
730 "epoll_ctl(DEL), %d: %s", fd
, strbuf
);
731 result
= ISC_R_UNEXPECTED
;
734 #elif defined(USE_DEVPOLL)
735 struct pollfd pfds
[2];
736 size_t writelen
= sizeof(pfds
[0]);
737 int lockid
= FDLOCK_ID(fd
);
739 memset(pfds
, 0, sizeof(pfds
));
740 pfds
[0].events
= POLLREMOVE
;
744 * Canceling read or write polling via /dev/poll is tricky. Since it
745 * only provides a way of canceling per FD, we may need to re-poll the
746 * socket for the other operation.
748 LOCK(&manager
->fdlock
[lockid
]);
749 if (msg
== SELECT_POKE_READ
&&
750 manager
->fdpollinfo
[fd
].want_write
== 1) {
751 pfds
[1].events
= POLLOUT
;
753 writelen
+= sizeof(pfds
[1]);
755 if (msg
== SELECT_POKE_WRITE
&&
756 manager
->fdpollinfo
[fd
].want_read
== 1) {
757 pfds
[1].events
= POLLIN
;
759 writelen
+= sizeof(pfds
[1]);
762 if (write(manager
->devpoll_fd
, pfds
, writelen
) == -1)
763 result
= isc__errno2result(errno
);
765 if (msg
== SELECT_POKE_READ
)
766 manager
->fdpollinfo
[fd
].want_read
= 0;
768 manager
->fdpollinfo
[fd
].want_write
= 0;
770 UNLOCK(&manager
->fdlock
[lockid
]);
773 #elif defined(USE_SELECT)
774 LOCK(&manager
->lock
);
775 if (msg
== SELECT_POKE_READ
)
776 FD_CLR(fd
, manager
->read_fds
);
777 else if (msg
== SELECT_POKE_WRITE
)
778 FD_CLR(fd
, manager
->write_fds
);
779 UNLOCK(&manager
->lock
);
786 wakeup_socket(isc_socketmgr_t
*manager
, int fd
, int msg
) {
788 int lockid
= FDLOCK_ID(fd
);
791 * This is a wakeup on a socket. If the socket is not in the
792 * process of being closed, start watching it for either reads
796 INSIST(fd
>= 0 && fd
< (int)manager
->maxsocks
);
798 if (msg
== SELECT_POKE_CLOSE
) {
799 /* No one should be updating fdstate, so no need to lock it */
800 INSIST(manager
->fdstate
[fd
] == CLOSE_PENDING
);
801 manager
->fdstate
[fd
] = CLOSED
;
802 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
803 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
808 LOCK(&manager
->fdlock
[lockid
]);
809 if (manager
->fdstate
[fd
] == CLOSE_PENDING
) {
810 UNLOCK(&manager
->fdlock
[lockid
]);
813 * We accept (and ignore) any error from unwatch_fd() as we are
814 * closing the socket, hoping it doesn't leave dangling state in
816 * Note that unwatch_fd() must be called after releasing the
817 * fdlock; otherwise it could cause deadlock due to a lock order
820 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
821 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
824 if (manager
->fdstate
[fd
] != MANAGED
) {
825 UNLOCK(&manager
->fdlock
[lockid
]);
828 UNLOCK(&manager
->fdlock
[lockid
]);
833 result
= watch_fd(manager
, fd
, msg
);
834 if (result
!= ISC_R_SUCCESS
) {
836 * XXXJT: what should we do? Ignoring the failure of watching
837 * a socket will make the application dysfunctional, but there
838 * seems to be no reasonable recovery process.
840 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
841 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
842 "failed to start watching FD (%d): %s",
843 fd
, isc_result_totext(result
));
847 #ifdef ISC_PLATFORM_USETHREADS
849 * Poke the select loop when there is something for us to do.
850 * The write is required (by POSIX) to complete. That is, we
851 * will not get partial writes.
854 select_poke(isc_socketmgr_t
*mgr
, int fd
, int msg
) {
857 char strbuf
[ISC_STRERRORSIZE
];
863 cc
= write(mgr
->pipe_fds
[1], buf
, sizeof(buf
));
866 * Treat ENOSR as EAGAIN but loop slowly as it is
867 * unlikely to clear fast.
869 if (cc
< 0 && errno
== ENOSR
) {
874 } while (cc
< 0 && SOFT_ERROR(errno
));
877 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
878 FATAL_ERROR(__FILE__
, __LINE__
,
879 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
882 "during watcher poke: %s"),
886 INSIST(cc
== sizeof(buf
));
890 * Read a message on the internal fd.
893 select_readmsg(isc_socketmgr_t
*mgr
, int *fd
, int *msg
) {
896 char strbuf
[ISC_STRERRORSIZE
];
898 cc
= read(mgr
->pipe_fds
[0], buf
, sizeof(buf
));
900 *msg
= SELECT_POKE_NOTHING
;
901 *fd
= -1; /* Silence compiler. */
902 if (SOFT_ERROR(errno
))
905 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
906 FATAL_ERROR(__FILE__
, __LINE__
,
907 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
910 "during watcher poke: %s"),
915 INSIST(cc
== sizeof(buf
));
920 #else /* ISC_PLATFORM_USETHREADS */
922 * Update the state of the socketmgr when something changes.
925 select_poke(isc_socketmgr_t
*manager
, int fd
, int msg
) {
926 if (msg
== SELECT_POKE_SHUTDOWN
)
929 wakeup_socket(manager
, fd
, msg
);
932 #endif /* ISC_PLATFORM_USETHREADS */
935 * Make a fd non-blocking.
938 make_nonblock(int fd
) {
941 char strbuf
[ISC_STRERRORSIZE
];
942 #ifdef USE_FIONBIO_IOCTL
945 ret
= ioctl(fd
, FIONBIO
, (char *)&on
);
947 flags
= fcntl(fd
, F_GETFL
, 0);
948 flags
|= PORT_NONBLOCK
;
949 ret
= fcntl(fd
, F_SETFL
, flags
);
953 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
954 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
955 #ifdef USE_FIONBIO_IOCTL
956 "ioctl(%d, FIONBIO, &on): %s", fd
,
958 "fcntl(%d, F_SETFL, %d): %s", fd
, flags
,
962 return (ISC_R_UNEXPECTED
);
965 return (ISC_R_SUCCESS
);
970 * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
971 * In order to ensure as much portability as possible, we provide wrapper
972 * functions of these macros.
973 * Note that cmsg_space() could run slow on OSes that do not have
976 static inline ISC_SOCKADDR_LEN_T
977 cmsg_len(ISC_SOCKADDR_LEN_T len
) {
979 return (CMSG_LEN(len
));
981 ISC_SOCKADDR_LEN_T hdrlen
;
984 * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
987 hdrlen
= (ISC_SOCKADDR_LEN_T
)CMSG_DATA(((struct cmsghdr
*)NULL
));
988 return (hdrlen
+ len
);
992 static inline ISC_SOCKADDR_LEN_T
993 cmsg_space(ISC_SOCKADDR_LEN_T len
) {
995 return (CMSG_SPACE(len
));
998 struct cmsghdr
*cmsgp
;
1000 * XXX: The buffer length is an ad-hoc value, but should be enough
1001 * in a practical sense.
1003 char dummybuf
[sizeof(struct cmsghdr
) + 1024];
1005 memset(&msg
, 0, sizeof(msg
));
1006 msg
.msg_control
= dummybuf
;
1007 msg
.msg_controllen
= sizeof(dummybuf
);
1009 cmsgp
= (struct cmsghdr
*)dummybuf
;
1010 cmsgp
->cmsg_len
= cmsg_len(len
);
1012 cmsgp
= CMSG_NXTHDR(&msg
, cmsgp
);
1014 return ((char *)cmsgp
- (char *)msg
.msg_control
);
1019 #endif /* USE_CMSG */
1022 * Process control messages received on a socket.
1025 process_cmsg(isc_socket_t
*sock
, struct msghdr
*msg
, isc_socketevent_t
*dev
) {
1027 struct cmsghdr
*cmsgp
;
1028 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1029 struct in6_pktinfo
*pktinfop
;
1032 struct timeval
*timevalp
;
1037 * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
1038 * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
1039 * They are all here, outside of the CPP tests, because it is
1040 * more consistent with the usual ISC coding style.
1046 #ifdef ISC_NET_BSD44MSGHDR
1049 if ((msg
->msg_flags
& MSG_TRUNC
) == MSG_TRUNC
)
1050 dev
->attributes
|= ISC_SOCKEVENTATTR_TRUNC
;
1054 if ((msg
->msg_flags
& MSG_CTRUNC
) == MSG_CTRUNC
)
1055 dev
->attributes
|= ISC_SOCKEVENTATTR_CTRUNC
;
1061 if (msg
->msg_controllen
== 0U || msg
->msg_control
== NULL
)
1067 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1071 cmsgp
= CMSG_FIRSTHDR(msg
);
1072 while (cmsgp
!= NULL
) {
1073 socket_log(sock
, NULL
, TRACE
,
1074 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_PROCESSCMSG
,
1075 "processing cmsg %p", cmsgp
);
1077 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1078 if (cmsgp
->cmsg_level
== IPPROTO_IPV6
1079 && cmsgp
->cmsg_type
== IPV6_PKTINFO
) {
1081 pktinfop
= (struct in6_pktinfo
*)CMSG_DATA(cmsgp
);
1082 memcpy(&dev
->pktinfo
, pktinfop
,
1083 sizeof(struct in6_pktinfo
));
1084 dev
->attributes
|= ISC_SOCKEVENTATTR_PKTINFO
;
1085 socket_log(sock
, NULL
, TRACE
,
1086 isc_msgcat
, ISC_MSGSET_SOCKET
,
1088 "interface received on ifindex %u",
1089 dev
->pktinfo
.ipi6_ifindex
);
1090 if (IN6_IS_ADDR_MULTICAST(&pktinfop
->ipi6_addr
))
1091 dev
->attributes
|= ISC_SOCKEVENTATTR_MULTICAST
;
1097 if (cmsgp
->cmsg_level
== SOL_SOCKET
1098 && cmsgp
->cmsg_type
== SCM_TIMESTAMP
) {
1099 timevalp
= (struct timeval
*)CMSG_DATA(cmsgp
);
1100 dev
->timestamp
.seconds
= timevalp
->tv_sec
;
1101 dev
->timestamp
.nanoseconds
= timevalp
->tv_usec
* 1000;
1102 dev
->attributes
|= ISC_SOCKEVENTATTR_TIMESTAMP
;
1108 cmsgp
= CMSG_NXTHDR(msg
, cmsgp
);
1110 #endif /* USE_CMSG */
1112 #endif /* ISC_NET_BSD44MSGHDR */
1116 * Construct an iov array and attach it to the msghdr passed in. This is
1117 * the SEND constructor, which will use the used region of the buffer
1118 * (if using a buffer list) or will use the internal region (if a single
1119 * buffer I/O is requested).
1121 * Nothing can be NULL, and the done event must list at least one buffer
1122 * on the buffer linked list for this function to be meaningful.
1124 * If write_countp != NULL, *write_countp will hold the number of bytes
1125 * this transaction can send.
1128 build_msghdr_send(isc_socket_t
*sock
, isc_socketevent_t
*dev
,
1129 struct msghdr
*msg
, struct iovec
*iov
, size_t *write_countp
)
1131 unsigned int iovcount
;
1132 isc_buffer_t
*buffer
;
1137 memset(msg
, 0, sizeof(*msg
));
1139 if (!sock
->connected
) {
1140 msg
->msg_name
= (void *)&dev
->address
.type
.sa
;
1141 msg
->msg_namelen
= dev
->address
.length
;
1143 msg
->msg_name
= NULL
;
1144 msg
->msg_namelen
= 0;
1147 buffer
= ISC_LIST_HEAD(dev
->bufferlist
);
1152 * Single buffer I/O? Skip what we've done so far in this region.
1154 if (buffer
== NULL
) {
1155 write_count
= dev
->region
.length
- dev
->n
;
1156 iov
[0].iov_base
= (void *)(dev
->region
.base
+ dev
->n
);
1157 iov
[0].iov_len
= write_count
;
1165 * Skip the data in the buffer list that we have already written.
1167 skip_count
= dev
->n
;
1168 while (buffer
!= NULL
) {
1169 REQUIRE(ISC_BUFFER_VALID(buffer
));
1170 if (skip_count
< isc_buffer_usedlength(buffer
))
1172 skip_count
-= isc_buffer_usedlength(buffer
);
1173 buffer
= ISC_LIST_NEXT(buffer
, link
);
1176 while (buffer
!= NULL
) {
1177 INSIST(iovcount
< MAXSCATTERGATHER_SEND
);
1179 isc_buffer_usedregion(buffer
, &used
);
1181 if (used
.length
> 0) {
1182 iov
[iovcount
].iov_base
= (void *)(used
.base
1184 iov
[iovcount
].iov_len
= used
.length
- skip_count
;
1185 write_count
+= (used
.length
- skip_count
);
1189 buffer
= ISC_LIST_NEXT(buffer
, link
);
1192 INSIST(skip_count
== 0U);
1196 msg
->msg_iovlen
= iovcount
;
1198 #ifdef ISC_NET_BSD44MSGHDR
1199 msg
->msg_control
= NULL
;
1200 msg
->msg_controllen
= 0;
1202 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1203 if ((sock
->type
== isc_sockettype_udp
)
1204 && ((dev
->attributes
& ISC_SOCKEVENTATTR_PKTINFO
) != 0)) {
1205 struct cmsghdr
*cmsgp
;
1206 struct in6_pktinfo
*pktinfop
;
1208 socket_log(sock
, NULL
, TRACE
,
1209 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_SENDTODATA
,
1210 "sendto pktinfo data, ifindex %u",
1211 dev
->pktinfo
.ipi6_ifindex
);
1213 msg
->msg_controllen
= cmsg_space(sizeof(struct in6_pktinfo
));
1214 INSIST(msg
->msg_controllen
<= sock
->sendcmsgbuflen
);
1215 msg
->msg_control
= (void *)sock
->sendcmsgbuf
;
1217 cmsgp
= (struct cmsghdr
*)sock
->sendcmsgbuf
;
1218 cmsgp
->cmsg_level
= IPPROTO_IPV6
;
1219 cmsgp
->cmsg_type
= IPV6_PKTINFO
;
1220 cmsgp
->cmsg_len
= cmsg_len(sizeof(struct in6_pktinfo
));
1221 pktinfop
= (struct in6_pktinfo
*)CMSG_DATA(cmsgp
);
1222 memcpy(pktinfop
, &dev
->pktinfo
, sizeof(struct in6_pktinfo
));
1224 #endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
1225 #else /* ISC_NET_BSD44MSGHDR */
1226 msg
->msg_accrights
= NULL
;
1227 msg
->msg_accrightslen
= 0;
1228 #endif /* ISC_NET_BSD44MSGHDR */
1230 if (write_countp
!= NULL
)
1231 *write_countp
= write_count
;
1235 * Construct an iov array and attach it to the msghdr passed in. This is
1236 * the RECV constructor, which will use the available region of the buffer
1237 * (if using a buffer list) or will use the internal region (if a single
1238 * buffer I/O is requested).
1240 * Nothing can be NULL, and the done event must list at least one buffer
1241 * on the buffer linked list for this function to be meaningful.
1243 * If read_countp != NULL, *read_countp will hold the number of bytes
1244 * this transaction can receive.
1247 build_msghdr_recv(isc_socket_t
*sock
, isc_socketevent_t
*dev
,
1248 struct msghdr
*msg
, struct iovec
*iov
, size_t *read_countp
)
1250 unsigned int iovcount
;
1251 isc_buffer_t
*buffer
;
1252 isc_region_t available
;
1255 memset(msg
, 0, sizeof(struct msghdr
));
1257 if (sock
->type
== isc_sockettype_udp
) {
1258 memset(&dev
->address
, 0, sizeof(dev
->address
));
1259 #ifdef BROKEN_RECVMSG
1260 if (sock
->pf
== AF_INET
) {
1261 msg
->msg_name
= (void *)&dev
->address
.type
.sin
;
1262 msg
->msg_namelen
= sizeof(dev
->address
.type
.sin6
);
1263 } else if (sock
->pf
== AF_INET6
) {
1264 msg
->msg_name
= (void *)&dev
->address
.type
.sin6
;
1265 msg
->msg_namelen
= sizeof(dev
->address
.type
.sin6
);
1266 #ifdef ISC_PLATFORM_HAVESYSUNH
1267 } else if (sock
->pf
== AF_UNIX
) {
1268 msg
->msg_name
= (void *)&dev
->address
.type
.sunix
;
1269 msg
->msg_namelen
= sizeof(dev
->address
.type
.sunix
);
1272 msg
->msg_name
= (void *)&dev
->address
.type
.sa
;
1273 msg
->msg_namelen
= sizeof(dev
->address
.type
);
1276 msg
->msg_name
= (void *)&dev
->address
.type
.sa
;
1277 msg
->msg_namelen
= sizeof(dev
->address
.type
);
1279 #ifdef ISC_NET_RECVOVERFLOW
1280 /* If needed, steal one iovec for overflow detection. */
1284 msg
->msg_name
= NULL
;
1285 msg
->msg_namelen
= 0;
1286 dev
->address
= sock
->peer_address
;
1289 buffer
= ISC_LIST_HEAD(dev
->bufferlist
);
1293 * Single buffer I/O? Skip what we've done so far in this region.
1295 if (buffer
== NULL
) {
1296 read_count
= dev
->region
.length
- dev
->n
;
1297 iov
[0].iov_base
= (void *)(dev
->region
.base
+ dev
->n
);
1298 iov
[0].iov_len
= read_count
;
1306 * Skip empty buffers.
1308 while (buffer
!= NULL
) {
1309 REQUIRE(ISC_BUFFER_VALID(buffer
));
1310 if (isc_buffer_availablelength(buffer
) != 0)
1312 buffer
= ISC_LIST_NEXT(buffer
, link
);
1316 while (buffer
!= NULL
) {
1317 INSIST(iovcount
< MAXSCATTERGATHER_RECV
);
1319 isc_buffer_availableregion(buffer
, &available
);
1321 if (available
.length
> 0) {
1322 iov
[iovcount
].iov_base
= (void *)(available
.base
);
1323 iov
[iovcount
].iov_len
= available
.length
;
1324 read_count
+= available
.length
;
1327 buffer
= ISC_LIST_NEXT(buffer
, link
);
1333 * If needed, set up to receive that one extra byte. Note that
1334 * we know there is at least one iov left, since we stole it
1335 * at the top of this function.
1337 #ifdef ISC_NET_RECVOVERFLOW
1338 if (sock
->type
== isc_sockettype_udp
) {
1339 iov
[iovcount
].iov_base
= (void *)(&sock
->overflow
);
1340 iov
[iovcount
].iov_len
= 1;
1346 msg
->msg_iovlen
= iovcount
;
1348 #ifdef ISC_NET_BSD44MSGHDR
1349 msg
->msg_control
= NULL
;
1350 msg
->msg_controllen
= 0;
1352 #if defined(USE_CMSG)
1353 if (sock
->type
== isc_sockettype_udp
) {
1354 msg
->msg_control
= sock
->recvcmsgbuf
;
1355 msg
->msg_controllen
= sock
->recvcmsgbuflen
;
1357 #endif /* USE_CMSG */
1358 #else /* ISC_NET_BSD44MSGHDR */
1359 msg
->msg_accrights
= NULL
;
1360 msg
->msg_accrightslen
= 0;
1361 #endif /* ISC_NET_BSD44MSGHDR */
1363 if (read_countp
!= NULL
)
1364 *read_countp
= read_count
;
1368 set_dev_address(isc_sockaddr_t
*address
, isc_socket_t
*sock
,
1369 isc_socketevent_t
*dev
)
1371 if (sock
->type
== isc_sockettype_udp
) {
1372 if (address
!= NULL
)
1373 dev
->address
= *address
;
1375 dev
->address
= sock
->peer_address
;
1376 } else if (sock
->type
== isc_sockettype_tcp
) {
1377 INSIST(address
== NULL
);
1378 dev
->address
= sock
->peer_address
;
1383 destroy_socketevent(isc_event_t
*event
) {
1384 isc_socketevent_t
*ev
= (isc_socketevent_t
*)event
;
1386 INSIST(ISC_LIST_EMPTY(ev
->bufferlist
));
1388 (ev
->destroy
)(event
);
1391 static isc_socketevent_t
*
1392 allocate_socketevent(isc_socket_t
*sock
, isc_eventtype_t eventtype
,
1393 isc_taskaction_t action
, const void *arg
)
1395 isc_socketevent_t
*ev
;
1397 ev
= (isc_socketevent_t
*)isc_event_allocate(sock
->manager
->mctx
,
1405 ev
->result
= ISC_R_UNEXPECTED
;
1406 ISC_LINK_INIT(ev
, ev_link
);
1407 ISC_LIST_INIT(ev
->bufferlist
);
1408 ev
->region
.base
= NULL
;
1412 ev
->destroy
= ev
->ev_destroy
;
1413 ev
->ev_destroy
= destroy_socketevent
;
1418 #if defined(ISC_SOCKET_DEBUG)
1420 dump_msg(struct msghdr
*msg
) {
1423 printf("MSGHDR %p\n", msg
);
1424 printf("\tname %p, namelen %ld\n", msg
->msg_name
,
1425 (long) msg
->msg_namelen
);
1426 printf("\tiov %p, iovlen %ld\n", msg
->msg_iov
,
1427 (long) msg
->msg_iovlen
);
1428 for (i
= 0; i
< (unsigned int)msg
->msg_iovlen
; i
++)
1429 printf("\t\t%d\tbase %p, len %ld\n", i
,
1430 msg
->msg_iov
[i
].iov_base
,
1431 (long) msg
->msg_iov
[i
].iov_len
);
1432 #ifdef ISC_NET_BSD44MSGHDR
1433 printf("\tcontrol %p, controllen %ld\n", msg
->msg_control
,
1434 (long) msg
->msg_controllen
);
1439 #define DOIO_SUCCESS 0 /* i/o ok, event sent */
1440 #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
1441 #define DOIO_HARD 2 /* i/o error, event sent */
1442 #define DOIO_EOF 3 /* EOF, no event sent */
1445 doio_recv(isc_socket_t
*sock
, isc_socketevent_t
*dev
) {
1447 struct iovec iov
[MAXSCATTERGATHER_RECV
];
1449 size_t actual_count
;
1450 struct msghdr msghdr
;
1451 isc_buffer_t
*buffer
;
1453 char strbuf
[ISC_STRERRORSIZE
];
1455 build_msghdr_recv(sock
, dev
, &msghdr
, iov
, &read_count
);
1457 #if defined(ISC_SOCKET_DEBUG)
1461 cc
= recvmsg(sock
->fd
, &msghdr
, 0);
1464 #if defined(ISC_SOCKET_DEBUG)
1469 if (SOFT_ERROR(recv_errno
))
1472 if (isc_log_wouldlog(isc_lctx
, IOEVENT_LEVEL
)) {
1473 isc__strerror(recv_errno
, strbuf
, sizeof(strbuf
));
1474 socket_log(sock
, NULL
, IOEVENT
,
1475 isc_msgcat
, ISC_MSGSET_SOCKET
,
1477 "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
1478 sock
->fd
, cc
, recv_errno
, strbuf
);
1481 #define SOFT_OR_HARD(_system, _isc) \
1482 if (recv_errno == _system) { \
1483 if (sock->connected) { \
1484 dev->result = _isc; \
1485 inc_stats(sock->manager->stats, \
1486 sock->statsindex[STATID_RECVFAIL]); \
1487 return (DOIO_HARD); \
1489 return (DOIO_SOFT); \
1491 #define ALWAYS_HARD(_system, _isc) \
1492 if (recv_errno == _system) { \
1493 dev->result = _isc; \
1494 inc_stats(sock->manager->stats, \
1495 sock->statsindex[STATID_RECVFAIL]); \
1496 return (DOIO_HARD); \
1499 SOFT_OR_HARD(ECONNREFUSED
, ISC_R_CONNREFUSED
);
1500 SOFT_OR_HARD(ENETUNREACH
, ISC_R_NETUNREACH
);
1501 SOFT_OR_HARD(EHOSTUNREACH
, ISC_R_HOSTUNREACH
);
1502 SOFT_OR_HARD(EHOSTDOWN
, ISC_R_HOSTDOWN
);
1503 /* HPUX 11.11 can return EADDRNOTAVAIL. */
1504 SOFT_OR_HARD(EADDRNOTAVAIL
, ISC_R_ADDRNOTAVAIL
);
1505 ALWAYS_HARD(ENOBUFS
, ISC_R_NORESOURCES
);
1507 * HPUX returns EPROTO and EINVAL on receiving some ICMP/ICMPv6
1511 SOFT_OR_HARD(EPROTO
, ISC_R_HOSTUNREACH
);
1513 SOFT_OR_HARD(EINVAL
, ISC_R_HOSTUNREACH
);
1518 dev
->result
= isc__errno2result(recv_errno
);
1519 inc_stats(sock
->manager
->stats
,
1520 sock
->statsindex
[STATID_RECVFAIL
]);
1525 * On TCP, zero length reads indicate EOF, while on
1526 * UDP, zero length reads are perfectly valid, although
1529 if ((sock
->type
== isc_sockettype_tcp
) && (cc
== 0))
1532 if (sock
->type
== isc_sockettype_udp
) {
1533 dev
->address
.length
= msghdr
.msg_namelen
;
1534 if (isc_sockaddr_getport(&dev
->address
) == 0) {
1535 if (isc_log_wouldlog(isc_lctx
, IOEVENT_LEVEL
)) {
1536 socket_log(sock
, &dev
->address
, IOEVENT
,
1537 isc_msgcat
, ISC_MSGSET_SOCKET
,
1539 "dropping source port zero packet");
1545 socket_log(sock
, &dev
->address
, IOEVENT
,
1546 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_PKTRECV
,
1547 "packet received correctly");
1550 * Overflow bit detection. If we received MORE bytes than we should,
1551 * this indicates an overflow situation. Set the flag in the
1552 * dev entry and adjust how much we read by one.
1554 #ifdef ISC_NET_RECVOVERFLOW
1555 if ((sock
->type
== isc_sockettype_udp
) && ((size_t)cc
> read_count
)) {
1556 dev
->attributes
|= ISC_SOCKEVENTATTR_TRUNC
;
1562 * If there are control messages attached, run through them and pull
1563 * out the interesting bits.
1565 if (sock
->type
== isc_sockettype_udp
)
1566 process_cmsg(sock
, &msghdr
, dev
);
1569 * update the buffers (if any) and the i/o count
1573 buffer
= ISC_LIST_HEAD(dev
->bufferlist
);
1574 while (buffer
!= NULL
&& actual_count
> 0U) {
1575 REQUIRE(ISC_BUFFER_VALID(buffer
));
1576 if (isc_buffer_availablelength(buffer
) <= actual_count
) {
1577 actual_count
-= isc_buffer_availablelength(buffer
);
1578 isc_buffer_add(buffer
,
1579 isc_buffer_availablelength(buffer
));
1581 isc_buffer_add(buffer
, actual_count
);
1585 buffer
= ISC_LIST_NEXT(buffer
, link
);
1586 if (buffer
== NULL
) {
1587 INSIST(actual_count
== 0U);
1592 * If we read less than we expected, update counters,
1593 * and let the upper layer poke the descriptor.
1595 if (((size_t)cc
!= read_count
) && (dev
->n
< dev
->minimum
))
1599 * Full reads are posted, or partials if partials are ok.
1601 dev
->result
= ISC_R_SUCCESS
;
1602 return (DOIO_SUCCESS
);
1607 * DOIO_SUCCESS The operation succeeded. dev->result contains
1610 * DOIO_HARD A hard or unexpected I/O error was encountered.
1611 * dev->result contains the appropriate error.
1613 * DOIO_SOFT A soft I/O error was encountered. No senddone
1614 * event was sent. The operation should be retried.
1616 * No other return values are possible.
1619 doio_send(isc_socket_t
*sock
, isc_socketevent_t
*dev
) {
1621 struct iovec iov
[MAXSCATTERGATHER_SEND
];
1623 struct msghdr msghdr
;
1624 char addrbuf
[ISC_SOCKADDR_FORMATSIZE
];
1627 char strbuf
[ISC_STRERRORSIZE
];
1629 build_msghdr_send(sock
, dev
, &msghdr
, iov
, &write_count
);
1632 cc
= sendmsg(sock
->fd
, &msghdr
, 0);
1636 * Check for error or block condition.
1639 if (send_errno
== EINTR
&& ++attempts
< NRETRIES
)
1642 if (SOFT_ERROR(send_errno
))
1645 #define SOFT_OR_HARD(_system, _isc) \
1646 if (send_errno == _system) { \
1647 if (sock->connected) { \
1648 dev->result = _isc; \
1649 inc_stats(sock->manager->stats, \
1650 sock->statsindex[STATID_SENDFAIL]); \
1651 return (DOIO_HARD); \
1653 return (DOIO_SOFT); \
1655 #define ALWAYS_HARD(_system, _isc) \
1656 if (send_errno == _system) { \
1657 dev->result = _isc; \
1658 inc_stats(sock->manager->stats, \
1659 sock->statsindex[STATID_SENDFAIL]); \
1660 return (DOIO_HARD); \
1663 SOFT_OR_HARD(ECONNREFUSED
, ISC_R_CONNREFUSED
);
1664 ALWAYS_HARD(EACCES
, ISC_R_NOPERM
);
1665 ALWAYS_HARD(EAFNOSUPPORT
, ISC_R_ADDRNOTAVAIL
);
1666 ALWAYS_HARD(EADDRNOTAVAIL
, ISC_R_ADDRNOTAVAIL
);
1667 ALWAYS_HARD(EHOSTUNREACH
, ISC_R_HOSTUNREACH
);
1669 ALWAYS_HARD(EHOSTDOWN
, ISC_R_HOSTUNREACH
);
1671 ALWAYS_HARD(ENETUNREACH
, ISC_R_NETUNREACH
);
1672 ALWAYS_HARD(ENOBUFS
, ISC_R_NORESOURCES
);
1673 ALWAYS_HARD(EPERM
, ISC_R_HOSTUNREACH
);
1674 ALWAYS_HARD(EPIPE
, ISC_R_NOTCONNECTED
);
1675 ALWAYS_HARD(ECONNRESET
, ISC_R_CONNECTIONRESET
);
1681 * The other error types depend on whether or not the
1682 * socket is UDP or TCP. If it is UDP, some errors
1683 * that we expect to be fatal under TCP are merely
1684 * annoying, and are really soft errors.
1686 * However, these soft errors are still returned as
1689 isc_sockaddr_format(&dev
->address
, addrbuf
, sizeof(addrbuf
));
1690 isc__strerror(send_errno
, strbuf
, sizeof(strbuf
));
1691 UNEXPECTED_ERROR(__FILE__
, __LINE__
, "internal_send: %s: %s",
1693 dev
->result
= isc__errno2result(send_errno
);
1694 inc_stats(sock
->manager
->stats
,
1695 sock
->statsindex
[STATID_SENDFAIL
]);
1700 inc_stats(sock
->manager
->stats
,
1701 sock
->statsindex
[STATID_SENDFAIL
]);
1702 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
1703 "doio_send: send() %s 0",
1704 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
1705 ISC_MSG_RETURNED
, "returned"));
1709 * If we write less than we expected, update counters, poke.
1712 if ((size_t)cc
!= write_count
)
1716 * Exactly what we wanted to write. We're done with this
1717 * entry. Post its completion event.
1719 dev
->result
= ISC_R_SUCCESS
;
1720 return (DOIO_SUCCESS
);
1726 * Caller must ensure that the socket is not locked and no external
1730 closesocket(isc_socketmgr_t
*manager
, isc_socket_t
*sock
, int fd
) {
1731 isc_sockettype_t type
= sock
->type
;
1732 int lockid
= FDLOCK_ID(fd
);
1735 * No one has this socket open, so the watcher doesn't have to be
1736 * poked, and the socket doesn't have to be locked.
1738 LOCK(&manager
->fdlock
[lockid
]);
1739 manager
->fds
[fd
] = NULL
;
1740 if (type
== isc_sockettype_fdwatch
)
1741 manager
->fdstate
[fd
] = CLOSED
;
1743 manager
->fdstate
[fd
] = CLOSE_PENDING
;
1744 UNLOCK(&manager
->fdlock
[lockid
]);
1745 if (type
== isc_sockettype_fdwatch
) {
1747 * The caller may close the socket once this function returns,
1748 * and `fd' may be reassigned for a new socket. So we do
1749 * unwatch_fd() here, rather than defer it via select_poke().
1750 * Note: this may complicate data protection among threads and
1751 * may reduce performance due to additional locks. One way to
1752 * solve this would be to dup() the watched descriptor, but we
1753 * take a simpler approach at this moment.
1755 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
1756 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
1758 select_poke(manager
, fd
, SELECT_POKE_CLOSE
);
1760 inc_stats(manager
->stats
, sock
->statsindex
[STATID_CLOSE
]);
1763 * update manager->maxfd here (XXX: this should be implemented more
1767 LOCK(&manager
->lock
);
1768 if (manager
->maxfd
== fd
) {
1772 for (i
= fd
- 1; i
>= 0; i
--) {
1773 lockid
= FDLOCK_ID(i
);
1775 LOCK(&manager
->fdlock
[lockid
]);
1776 if (manager
->fdstate
[i
] == MANAGED
) {
1778 UNLOCK(&manager
->fdlock
[lockid
]);
1781 UNLOCK(&manager
->fdlock
[lockid
]);
1783 #ifdef ISC_PLATFORM_USETHREADS
1784 if (manager
->maxfd
< manager
->pipe_fds
[0])
1785 manager
->maxfd
= manager
->pipe_fds
[0];
1788 UNLOCK(&manager
->lock
);
1789 #endif /* USE_SELECT */
1793 destroy(isc_socket_t
**sockp
) {
1795 isc_socket_t
*sock
= *sockp
;
1796 isc_socketmgr_t
*manager
= sock
->manager
;
1798 socket_log(sock
, NULL
, CREATION
, isc_msgcat
, ISC_MSGSET_SOCKET
,
1799 ISC_MSG_DESTROYING
, "destroying");
1801 INSIST(ISC_LIST_EMPTY(sock
->accept_list
));
1802 INSIST(ISC_LIST_EMPTY(sock
->recv_list
));
1803 INSIST(ISC_LIST_EMPTY(sock
->send_list
));
1804 INSIST(sock
->connect_ev
== NULL
);
1805 REQUIRE(sock
->fd
== -1 || sock
->fd
< (int)manager
->maxsocks
);
1807 if (sock
->fd
>= 0) {
1810 closesocket(manager
, sock
, fd
);
1813 LOCK(&manager
->lock
);
1815 ISC_LIST_UNLINK(manager
->socklist
, sock
, link
);
1817 #ifdef ISC_PLATFORM_USETHREADS
1818 if (ISC_LIST_EMPTY(manager
->socklist
))
1819 SIGNAL(&manager
->shutdown_ok
);
1820 #endif /* ISC_PLATFORM_USETHREADS */
1822 UNLOCK(&manager
->lock
);
1828 allocate_socket(isc_socketmgr_t
*manager
, isc_sockettype_t type
,
1829 isc_socket_t
**socketp
)
1832 isc_result_t result
;
1833 ISC_SOCKADDR_LEN_T cmsgbuflen
;
1835 sock
= isc_mem_get(manager
->mctx
, sizeof(*sock
));
1838 return (ISC_R_NOMEMORY
);
1840 result
= ISC_R_UNEXPECTED
;
1843 sock
->references
= 0;
1845 sock
->manager
= manager
;
1848 sock
->statsindex
= NULL
;
1850 ISC_LINK_INIT(sock
, link
);
1852 sock
->recvcmsgbuf
= NULL
;
1853 sock
->sendcmsgbuf
= NULL
;
1856 * set up cmsg buffers
1859 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1860 cmsgbuflen
= cmsg_space(sizeof(struct in6_pktinfo
));
1862 #if defined(USE_CMSG) && defined(SO_TIMESTAMP)
1863 cmsgbuflen
+= cmsg_space(sizeof(struct timeval
));
1865 sock
->recvcmsgbuflen
= cmsgbuflen
;
1866 if (sock
->recvcmsgbuflen
!= 0U) {
1867 sock
->recvcmsgbuf
= isc_mem_get(manager
->mctx
, cmsgbuflen
);
1868 if (sock
->recvcmsgbuf
== NULL
)
1873 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1874 cmsgbuflen
= cmsg_space(sizeof(struct in6_pktinfo
));
1876 sock
->sendcmsgbuflen
= cmsgbuflen
;
1877 if (sock
->sendcmsgbuflen
!= 0U) {
1878 sock
->sendcmsgbuf
= isc_mem_get(manager
->mctx
, cmsgbuflen
);
1879 if (sock
->sendcmsgbuf
== NULL
)
1883 memset(sock
->name
, 0, sizeof(sock
->name
));
1887 * set up list of readers and writers to be initially empty
1889 ISC_LIST_INIT(sock
->recv_list
);
1890 ISC_LIST_INIT(sock
->send_list
);
1891 ISC_LIST_INIT(sock
->accept_list
);
1892 sock
->connect_ev
= NULL
;
1893 sock
->pending_recv
= 0;
1894 sock
->pending_send
= 0;
1895 sock
->pending_accept
= 0;
1897 sock
->connected
= 0;
1898 sock
->connecting
= 0;
1902 * initialize the lock
1904 result
= isc_mutex_init(&sock
->lock
);
1905 if (result
!= ISC_R_SUCCESS
) {
1911 * Initialize readable and writable events
1913 ISC_EVENT_INIT(&sock
->readable_ev
, sizeof(intev_t
),
1914 ISC_EVENTATTR_NOPURGE
, NULL
, ISC_SOCKEVENT_INTR
,
1915 NULL
, sock
, sock
, NULL
, NULL
);
1916 ISC_EVENT_INIT(&sock
->writable_ev
, sizeof(intev_t
),
1917 ISC_EVENTATTR_NOPURGE
, NULL
, ISC_SOCKEVENT_INTW
,
1918 NULL
, sock
, sock
, NULL
, NULL
);
1920 sock
->magic
= SOCKET_MAGIC
;
1923 return (ISC_R_SUCCESS
);
1926 if (sock
->recvcmsgbuf
!= NULL
)
1927 isc_mem_put(manager
->mctx
, sock
->recvcmsgbuf
,
1928 sock
->recvcmsgbuflen
);
1929 if (sock
->sendcmsgbuf
!= NULL
)
1930 isc_mem_put(manager
->mctx
, sock
->sendcmsgbuf
,
1931 sock
->sendcmsgbuflen
);
1932 isc_mem_put(manager
->mctx
, sock
, sizeof(*sock
));
1938 * This event requires that the various lists be empty, that the reference
1939 * count be 1, and that the magic number is valid. The other socket bits,
1940 * like the lock, must be initialized as well. The fd associated must be
1941 * marked as closed, by setting it to -1 on close, or this routine will
1942 * also close the socket.
1945 free_socket(isc_socket_t
**socketp
) {
1946 isc_socket_t
*sock
= *socketp
;
1948 INSIST(sock
->references
== 0);
1949 INSIST(VALID_SOCKET(sock
));
1950 INSIST(!sock
->connecting
);
1951 INSIST(!sock
->pending_recv
);
1952 INSIST(!sock
->pending_send
);
1953 INSIST(!sock
->pending_accept
);
1954 INSIST(ISC_LIST_EMPTY(sock
->recv_list
));
1955 INSIST(ISC_LIST_EMPTY(sock
->send_list
));
1956 INSIST(ISC_LIST_EMPTY(sock
->accept_list
));
1957 INSIST(!ISC_LINK_LINKED(sock
, link
));
1959 if (sock
->recvcmsgbuf
!= NULL
)
1960 isc_mem_put(sock
->manager
->mctx
, sock
->recvcmsgbuf
,
1961 sock
->recvcmsgbuflen
);
1962 if (sock
->sendcmsgbuf
!= NULL
)
1963 isc_mem_put(sock
->manager
->mctx
, sock
->sendcmsgbuf
,
1964 sock
->sendcmsgbuflen
);
1968 DESTROYLOCK(&sock
->lock
);
1970 isc_mem_put(sock
->manager
->mctx
, sock
, sizeof(*sock
));
1977 * This really should not be necessary to do. Having to workout
1978 * which kernel version we are on at run time so that we don't cause
1979 * the kernel to issue a warning about us using a deprecated socket option.
1980 * Such warnings should *never* be on by default in production kernels.
1982 * We can't do this a build time because executables are moved between
1983 * machines and hence kernels.
1985 * We can't just not set SO_BSDCOMAT because some kernels require it.
1988 static isc_once_t bsdcompat_once
= ISC_ONCE_INIT
;
1989 isc_boolean_t bsdcompat
= ISC_TRUE
;
1992 clear_bsdcompat(void) {
1999 uname(&buf
); /* Can only fail if buf is bad in Linux. */
2001 /* Paranoia in parsing can be increased, but we trust uname(). */
2002 major
= strtol(buf
.release
, &endp
, 10);
2004 minor
= strtol(endp
+1, &endp
, 10);
2005 if ((major
> 2) || ((major
== 2) && (minor
>= 4))) {
2006 bsdcompat
= ISC_FALSE
;
2009 #endif /* __linux __ */
2014 opensocket(isc_socketmgr_t
*manager
, isc_socket_t
*sock
) {
2015 char strbuf
[ISC_STRERRORSIZE
];
2016 const char *err
= "socket";
2018 #if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
2021 #if defined(SO_RCVBUF)
2022 ISC_SOCKADDR_LEN_T optlen
;
2027 switch (sock
->type
) {
2028 case isc_sockettype_udp
:
2029 sock
->fd
= socket(sock
->pf
, SOCK_DGRAM
, IPPROTO_UDP
);
2031 case isc_sockettype_tcp
:
2032 sock
->fd
= socket(sock
->pf
, SOCK_STREAM
, IPPROTO_TCP
);
2034 case isc_sockettype_unix
:
2035 sock
->fd
= socket(sock
->pf
, SOCK_STREAM
, 0);
2037 case isc_sockettype_fdwatch
:
2039 * We should not be called for isc_sockettype_fdwatch sockets.
2044 if (sock
->fd
== -1 && errno
== EINTR
&& tries
++ < 42)
2049 * Leave a space for stdio and TCP to work in.
2051 if (manager
->reserved
!= 0 && sock
->type
== isc_sockettype_udp
&&
2052 sock
->fd
>= 0 && sock
->fd
< manager
->reserved
) {
2054 new = fcntl(sock
->fd
, F_DUPFD
, manager
->reserved
);
2056 (void)close(sock
->fd
);
2059 err
= "isc_socket_create: fcntl/reserved";
2060 } else if (sock
->fd
>= 0 && sock
->fd
< 20) {
2062 new = fcntl(sock
->fd
, F_DUPFD
, 20);
2064 (void)close(sock
->fd
);
2067 err
= "isc_socket_create: fcntl";
2071 if (sock
->fd
>= (int)manager
->maxsocks
) {
2072 (void)close(sock
->fd
);
2073 isc_log_iwrite(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
2074 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
2075 isc_msgcat
, ISC_MSGSET_SOCKET
,
2077 "socket: file descriptor exceeds limit (%d/%u)",
2078 sock
->fd
, manager
->maxsocks
);
2079 return (ISC_R_NORESOURCES
);
2086 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2087 isc_log_iwrite(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
2088 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
2089 isc_msgcat
, ISC_MSGSET_SOCKET
,
2091 "%s: %s", err
, strbuf
);
2094 return (ISC_R_NORESOURCES
);
2096 case EPROTONOSUPPORT
:
2100 * Linux 2.2 (and maybe others) return EINVAL instead of
2104 return (ISC_R_FAMILYNOSUPPORT
);
2107 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2108 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2110 isc_msgcat_get(isc_msgcat
,
2115 return (ISC_R_UNEXPECTED
);
2119 if (make_nonblock(sock
->fd
) != ISC_R_SUCCESS
) {
2120 (void)close(sock
->fd
);
2121 return (ISC_R_UNEXPECTED
);
2125 RUNTIME_CHECK(isc_once_do(&bsdcompat_once
,
2126 clear_bsdcompat
) == ISC_R_SUCCESS
);
2127 if (sock
->type
!= isc_sockettype_unix
&& bsdcompat
&&
2128 setsockopt(sock
->fd
, SOL_SOCKET
, SO_BSDCOMPAT
,
2129 (void *)&on
, sizeof(on
)) < 0) {
2130 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2131 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2132 "setsockopt(%d, SO_BSDCOMPAT) %s: %s",
2134 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
2135 ISC_MSG_FAILED
, "failed"),
2142 if (setsockopt(sock
->fd
, SOL_SOCKET
, SO_NOSIGPIPE
,
2143 (void *)&on
, sizeof(on
)) < 0) {
2144 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2145 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2146 "setsockopt(%d, SO_NOSIGPIPE) %s: %s",
2148 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
2149 ISC_MSG_FAILED
, "failed"),
2155 #if defined(USE_CMSG) || defined(SO_RCVBUF)
2156 if (sock
->type
== isc_sockettype_udp
) {
2158 #if defined(USE_CMSG)
2159 #if defined(SO_TIMESTAMP)
2160 if (setsockopt(sock
->fd
, SOL_SOCKET
, SO_TIMESTAMP
,
2161 (void *)&on
, sizeof(on
)) < 0
2162 && errno
!= ENOPROTOOPT
) {
2163 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2164 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2165 "setsockopt(%d, SO_TIMESTAMP) %s: %s",
2167 isc_msgcat_get(isc_msgcat
,
2174 #endif /* SO_TIMESTAMP */
2176 #if defined(ISC_PLATFORM_HAVEIPV6)
2177 if (sock
->pf
== AF_INET6
&& sock
->recvcmsgbuflen
== 0U) {
2179 * Warn explicitly because this anomaly can be hidden
2180 * in usual operation (and unexpectedly appear later).
2182 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2183 "No buffer available to receive "
2184 "IPv6 destination");
2186 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
2187 #ifdef IPV6_RECVPKTINFO
2189 if ((sock
->pf
== AF_INET6
)
2190 && (setsockopt(sock
->fd
, IPPROTO_IPV6
, IPV6_RECVPKTINFO
,
2191 (void *)&on
, sizeof(on
)) < 0)) {
2192 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2193 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2194 "setsockopt(%d, IPV6_RECVPKTINFO) "
2196 isc_msgcat_get(isc_msgcat
,
2204 if ((sock
->pf
== AF_INET6
)
2205 && (setsockopt(sock
->fd
, IPPROTO_IPV6
, IPV6_PKTINFO
,
2206 (void *)&on
, sizeof(on
)) < 0)) {
2207 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2208 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2209 "setsockopt(%d, IPV6_PKTINFO) %s: %s",
2211 isc_msgcat_get(isc_msgcat
,
2217 #endif /* IPV6_RECVPKTINFO */
2218 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
2219 #ifdef IPV6_USE_MIN_MTU /* RFC 3542, not too common yet*/
2220 /* use minimum MTU */
2221 if (sock
->pf
== AF_INET6
) {
2222 (void)setsockopt(sock
->fd
, IPPROTO_IPV6
,
2224 (void *)&on
, sizeof(on
));
2227 #endif /* ISC_PLATFORM_HAVEIPV6 */
2228 #endif /* defined(USE_CMSG) */
2230 #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
2232 * Turn off Path MTU discovery on IPv4/UDP sockets.
2234 if (sock
->pf
== AF_INET
) {
2235 int action
= IP_PMTUDISC_DONT
;
2236 (void)setsockopt(sock
->fd
, IPPROTO_IP
, IP_MTU_DISCOVER
,
2237 &action
, sizeof(action
));
2240 #if defined(IP_DONTFRAG)
2242 * Turn off Path MTU discovery on IPv4/UDP sockets.
2244 if (sock
->pf
== AF_INET
) {
2246 (void)setsockopt(sock
->fd
, IPPROTO_IP
, IP_DONTFRAG
,
2251 #if defined(SO_RCVBUF)
2252 optlen
= sizeof(size
);
2253 if (getsockopt(sock
->fd
, SOL_SOCKET
, SO_RCVBUF
,
2254 (void *)&size
, &optlen
) >= 0 &&
2255 size
< RCVBUFSIZE
) {
2257 if (setsockopt(sock
->fd
, SOL_SOCKET
, SO_RCVBUF
,
2258 (void *)&size
, sizeof(size
)) == -1) {
2259 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2260 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2261 "setsockopt(%d, SO_RCVBUF, %d) %s: %s",
2263 isc_msgcat_get(isc_msgcat
,
2272 #endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
2274 inc_stats(manager
->stats
, sock
->statsindex
[STATID_OPEN
]);
2276 return (ISC_R_SUCCESS
);
2280 * Create a new 'type' socket managed by 'manager'. Events
2281 * will be posted to 'task' and when dispatched 'action' will be
2282 * called with 'arg' as the arg value. The new socket is returned
2286 isc_socket_create(isc_socketmgr_t
*manager
, int pf
, isc_sockettype_t type
,
2287 isc_socket_t
**socketp
)
2289 isc_socket_t
*sock
= NULL
;
2290 isc_result_t result
;
2293 REQUIRE(VALID_MANAGER(manager
));
2294 REQUIRE(socketp
!= NULL
&& *socketp
== NULL
);
2295 REQUIRE(type
!= isc_sockettype_fdwatch
);
2297 result
= allocate_socket(manager
, type
, &sock
);
2298 if (result
!= ISC_R_SUCCESS
)
2301 switch (sock
->type
) {
2302 case isc_sockettype_udp
:
2304 (pf
== AF_INET
) ? upd4statsindex
: upd6statsindex
;
2306 case isc_sockettype_tcp
:
2308 (pf
== AF_INET
) ? tcp4statsindex
: tcp6statsindex
;
2310 case isc_sockettype_unix
:
2311 sock
->statsindex
= unixstatsindex
;
2318 result
= opensocket(manager
, sock
);
2319 if (result
!= ISC_R_SUCCESS
) {
2320 inc_stats(manager
->stats
, sock
->statsindex
[STATID_OPENFAIL
]);
2325 sock
->references
= 1;
2329 * Note we don't have to lock the socket like we normally would because
2330 * there are no external references to it yet.
2333 lockid
= FDLOCK_ID(sock
->fd
);
2334 LOCK(&manager
->fdlock
[lockid
]);
2335 manager
->fds
[sock
->fd
] = sock
;
2336 manager
->fdstate
[sock
->fd
] = MANAGED
;
2338 INSIST(sock
->manager
->fdpollinfo
[sock
->fd
].want_read
== 0 &&
2339 sock
->manager
->fdpollinfo
[sock
->fd
].want_write
== 0);
2341 UNLOCK(&manager
->fdlock
[lockid
]);
2343 LOCK(&manager
->lock
);
2344 ISC_LIST_APPEND(manager
->socklist
, sock
, link
);
2346 if (manager
->maxfd
< sock
->fd
)
2347 manager
->maxfd
= sock
->fd
;
2349 UNLOCK(&manager
->lock
);
2351 socket_log(sock
, NULL
, CREATION
, isc_msgcat
, ISC_MSGSET_SOCKET
,
2352 ISC_MSG_CREATED
, "created");
2354 return (ISC_R_SUCCESS
);
2358 isc_socket_open(isc_socket_t
*sock
) {
2359 isc_result_t result
;
2361 REQUIRE(VALID_SOCKET(sock
));
2364 REQUIRE(sock
->references
== 1);
2365 REQUIRE(sock
->type
!= isc_sockettype_fdwatch
);
2366 UNLOCK(&sock
->lock
);
2368 * We don't need to retain the lock hereafter, since no one else has
2371 REQUIRE(sock
->fd
== -1);
2373 result
= opensocket(sock
->manager
, sock
);
2374 if (result
!= ISC_R_SUCCESS
)
2377 if (result
== ISC_R_SUCCESS
) {
2378 int lockid
= FDLOCK_ID(sock
->fd
);
2380 LOCK(&sock
->manager
->fdlock
[lockid
]);
2381 sock
->manager
->fds
[sock
->fd
] = sock
;
2382 sock
->manager
->fdstate
[sock
->fd
] = MANAGED
;
2384 INSIST(sock
->manager
->fdpollinfo
[sock
->fd
].want_read
== 0 &&
2385 sock
->manager
->fdpollinfo
[sock
->fd
].want_write
== 0);
2387 UNLOCK(&sock
->manager
->fdlock
[lockid
]);
2390 LOCK(&sock
->manager
->lock
);
2391 if (sock
->manager
->maxfd
< sock
->fd
)
2392 sock
->manager
->maxfd
= sock
->fd
;
2393 UNLOCK(&sock
->manager
->lock
);
2401 * Create a new 'type' socket managed by 'manager'. Events
2402 * will be posted to 'task' and when dispatched 'action' will be
2403 * called with 'arg' as the arg value. The new socket is returned
2407 isc_socket_fdwatchcreate(isc_socketmgr_t
*manager
, int fd
, int flags
,
2408 isc_sockfdwatch_t callback
, void *cbarg
,
2409 isc_task_t
*task
, isc_socket_t
**socketp
)
2411 isc_socket_t
*sock
= NULL
;
2412 isc_result_t result
;
2415 REQUIRE(VALID_MANAGER(manager
));
2416 REQUIRE(socketp
!= NULL
&& *socketp
== NULL
);
2418 result
= allocate_socket(manager
, isc_sockettype_fdwatch
, &sock
);
2419 if (result
!= ISC_R_SUCCESS
)
2423 sock
->fdwatcharg
= cbarg
;
2424 sock
->fdwatchcb
= callback
;
2425 sock
->fdwatchflags
= flags
;
2426 sock
->fdwatchtask
= task
;
2427 sock
->statsindex
= fdwatchstatsindex
;
2429 sock
->references
= 1;
2433 * Note we don't have to lock the socket like we normally would because
2434 * there are no external references to it yet.
2437 lockid
= FDLOCK_ID(sock
->fd
);
2438 LOCK(&manager
->fdlock
[lockid
]);
2439 manager
->fds
[sock
->fd
] = sock
;
2440 manager
->fdstate
[sock
->fd
] = MANAGED
;
2441 UNLOCK(&manager
->fdlock
[lockid
]);
2443 LOCK(&manager
->lock
);
2444 ISC_LIST_APPEND(manager
->socklist
, sock
, link
);
2446 if (manager
->maxfd
< sock
->fd
)
2447 manager
->maxfd
= sock
->fd
;
2449 UNLOCK(&manager
->lock
);
2451 if (flags
& ISC_SOCKFDWATCH_READ
)
2452 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_READ
);
2453 if (flags
& ISC_SOCKFDWATCH_WRITE
)
2454 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_WRITE
);
2456 socket_log(sock
, NULL
, CREATION
, isc_msgcat
, ISC_MSGSET_SOCKET
,
2457 ISC_MSG_CREATED
, "fdwatch-created");
2459 return (ISC_R_SUCCESS
);
2463 * Attach to a socket. Caller must explicitly detach when it is done.
2466 isc_socket_attach(isc_socket_t
*sock
, isc_socket_t
**socketp
) {
2467 REQUIRE(VALID_SOCKET(sock
));
2468 REQUIRE(socketp
!= NULL
&& *socketp
== NULL
);
2472 UNLOCK(&sock
->lock
);
2478 * Dereference a socket. If this is the last reference to it, clean things
2479 * up by destroying the socket.
2482 isc_socket_detach(isc_socket_t
**socketp
) {
2484 isc_boolean_t kill_socket
= ISC_FALSE
;
2486 REQUIRE(socketp
!= NULL
);
2488 REQUIRE(VALID_SOCKET(sock
));
2491 REQUIRE(sock
->references
> 0);
2493 if (sock
->references
== 0)
2494 kill_socket
= ISC_TRUE
;
2495 UNLOCK(&sock
->lock
);
2504 isc_socket_close(isc_socket_t
*sock
) {
2506 isc_socketmgr_t
*manager
;
2507 isc_sockettype_t type
;
2509 REQUIRE(VALID_SOCKET(sock
));
2513 REQUIRE(sock
->references
== 1);
2514 REQUIRE(sock
->type
!= isc_sockettype_fdwatch
);
2515 REQUIRE(sock
->fd
>= 0 && sock
->fd
< (int)sock
->manager
->maxsocks
);
2517 INSIST(!sock
->connecting
);
2518 INSIST(!sock
->pending_recv
);
2519 INSIST(!sock
->pending_send
);
2520 INSIST(!sock
->pending_accept
);
2521 INSIST(ISC_LIST_EMPTY(sock
->recv_list
));
2522 INSIST(ISC_LIST_EMPTY(sock
->send_list
));
2523 INSIST(ISC_LIST_EMPTY(sock
->accept_list
));
2524 INSIST(sock
->connect_ev
== NULL
);
2526 manager
= sock
->manager
;
2530 memset(sock
->name
, 0, sizeof(sock
->name
));
2533 sock
->connected
= 0;
2534 sock
->connecting
= 0;
2536 isc_sockaddr_any(&sock
->peer_address
);
2538 UNLOCK(&sock
->lock
);
2540 closesocket(manager
, sock
, fd
);
2542 return (ISC_R_SUCCESS
);
2546 * I/O is possible on a given socket. Schedule an event to this task that
2547 * will call an internal function to do the I/O. This will charge the
2548 * task with the I/O operation and let our select loop handler get back
2549 * to doing something real as fast as possible.
2551 * The socket and manager must be locked before calling this function.
2554 dispatch_recv(isc_socket_t
*sock
) {
2556 isc_socketevent_t
*ev
;
2559 INSIST(!sock
->pending_recv
);
2561 if (sock
->type
!= isc_sockettype_fdwatch
) {
2562 ev
= ISC_LIST_HEAD(sock
->recv_list
);
2565 socket_log(sock
, NULL
, EVENT
, NULL
, 0, 0,
2566 "dispatch_recv: event %p -> task %p",
2568 sender
= ev
->ev_sender
;
2570 sender
= sock
->fdwatchtask
;
2573 sock
->pending_recv
= 1;
2574 iev
= &sock
->readable_ev
;
2577 iev
->ev_sender
= sock
;
2578 if (sock
->type
== isc_sockettype_fdwatch
)
2579 iev
->ev_action
= internal_fdwatch_read
;
2581 iev
->ev_action
= internal_recv
;
2584 isc_task_send(sender
, (isc_event_t
**)&iev
);
2588 dispatch_send(isc_socket_t
*sock
) {
2590 isc_socketevent_t
*ev
;
2593 INSIST(!sock
->pending_send
);
2595 if (sock
->type
!= isc_sockettype_fdwatch
) {
2596 ev
= ISC_LIST_HEAD(sock
->send_list
);
2599 socket_log(sock
, NULL
, EVENT
, NULL
, 0, 0,
2600 "dispatch_send: event %p -> task %p",
2602 sender
= ev
->ev_sender
;
2604 sender
= sock
->fdwatchtask
;
2607 sock
->pending_send
= 1;
2608 iev
= &sock
->writable_ev
;
2611 iev
->ev_sender
= sock
;
2612 if (sock
->type
== isc_sockettype_fdwatch
)
2613 iev
->ev_action
= internal_fdwatch_write
;
2615 iev
->ev_action
= internal_send
;
2618 isc_task_send(sender
, (isc_event_t
**)&iev
);
2622 * Dispatch an internal accept event.
2625 dispatch_accept(isc_socket_t
*sock
) {
2627 isc_socket_newconnev_t
*ev
;
2629 INSIST(!sock
->pending_accept
);
2632 * Are there any done events left, or were they all canceled
2633 * before the manager got the socket lock?
2635 ev
= ISC_LIST_HEAD(sock
->accept_list
);
2639 sock
->pending_accept
= 1;
2640 iev
= &sock
->readable_ev
;
2642 sock
->references
++; /* keep socket around for this internal event */
2643 iev
->ev_sender
= sock
;
2644 iev
->ev_action
= internal_accept
;
2647 isc_task_send(ev
->ev_sender
, (isc_event_t
**)&iev
);
2651 dispatch_connect(isc_socket_t
*sock
) {
2653 isc_socket_connev_t
*ev
;
2655 iev
= &sock
->writable_ev
;
2657 ev
= sock
->connect_ev
;
2658 INSIST(ev
!= NULL
); /* XXX */
2660 INSIST(sock
->connecting
);
2662 sock
->references
++; /* keep socket around for this internal event */
2663 iev
->ev_sender
= sock
;
2664 iev
->ev_action
= internal_connect
;
2667 isc_task_send(ev
->ev_sender
, (isc_event_t
**)&iev
);
2671 * Dequeue an item off the given socket's read queue, set the result code
2672 * in the done event to the one provided, and send it to the task it was
2675 * If the event to be sent is on a list, remove it before sending. If
2676 * asked to, send and detach from the socket as well.
2678 * Caller must have the socket locked if the event is attached to the socket.
2681 send_recvdone_event(isc_socket_t
*sock
, isc_socketevent_t
**dev
) {
2684 task
= (*dev
)->ev_sender
;
2686 (*dev
)->ev_sender
= sock
;
2688 if (ISC_LINK_LINKED(*dev
, ev_link
))
2689 ISC_LIST_DEQUEUE(sock
->recv_list
, *dev
, ev_link
);
2691 if (((*dev
)->attributes
& ISC_SOCKEVENTATTR_ATTACHED
)
2692 == ISC_SOCKEVENTATTR_ATTACHED
)
2693 isc_task_sendanddetach(&task
, (isc_event_t
**)dev
);
2695 isc_task_send(task
, (isc_event_t
**)dev
);
2699 * See comments for send_recvdone_event() above.
2701 * Caller must have the socket locked if the event is attached to the socket.
2704 send_senddone_event(isc_socket_t
*sock
, isc_socketevent_t
**dev
) {
2707 INSIST(dev
!= NULL
&& *dev
!= NULL
);
2709 task
= (*dev
)->ev_sender
;
2710 (*dev
)->ev_sender
= sock
;
2712 if (ISC_LINK_LINKED(*dev
, ev_link
))
2713 ISC_LIST_DEQUEUE(sock
->send_list
, *dev
, ev_link
);
2715 if (((*dev
)->attributes
& ISC_SOCKEVENTATTR_ATTACHED
)
2716 == ISC_SOCKEVENTATTR_ATTACHED
)
2717 isc_task_sendanddetach(&task
, (isc_event_t
**)dev
);
2719 isc_task_send(task
, (isc_event_t
**)dev
);
2723 * Call accept() on a socket, to get the new file descriptor. The listen
2724 * socket is used as a prototype to create a new isc_socket_t. The new
2725 * socket has one outstanding reference. The task receiving the event
2726 * will be detached from just after the event is delivered.
2728 * On entry to this function, the event delivered is the internal
2729 * readable event, and the first item on the accept_list should be
2730 * the done event we want to send. If the list is empty, this is a no-op,
2731 * so just unlock and return.
2734 internal_accept(isc_task_t
*me
, isc_event_t
*ev
) {
2736 isc_socketmgr_t
*manager
;
2737 isc_socket_newconnev_t
*dev
;
2739 ISC_SOCKADDR_LEN_T addrlen
;
2741 isc_result_t result
= ISC_R_SUCCESS
;
2742 char strbuf
[ISC_STRERRORSIZE
];
2743 const char *err
= "accept";
2747 sock
= ev
->ev_sender
;
2748 INSIST(VALID_SOCKET(sock
));
2751 socket_log(sock
, NULL
, TRACE
,
2752 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_ACCEPTLOCK
,
2753 "internal_accept called, locked socket");
2755 manager
= sock
->manager
;
2756 INSIST(VALID_MANAGER(manager
));
2758 INSIST(sock
->listener
);
2759 INSIST(sock
->pending_accept
== 1);
2760 sock
->pending_accept
= 0;
2762 INSIST(sock
->references
> 0);
2763 sock
->references
--; /* the internal event is done with this socket */
2764 if (sock
->references
== 0) {
2765 UNLOCK(&sock
->lock
);
2771 * Get the first item off the accept list.
2772 * If it is empty, unlock the socket and return.
2774 dev
= ISC_LIST_HEAD(sock
->accept_list
);
2776 UNLOCK(&sock
->lock
);
2781 * Try to accept the new connection. If the accept fails with
2782 * EAGAIN or EINTR, simply poke the watcher to watch this socket
2783 * again. Also ignore ECONNRESET, which has been reported to
2784 * be spuriously returned on Linux 2.2.19 although it is not
2785 * a documented error for accept(). ECONNABORTED has been
2786 * reported for Solaris 8. The rest are thrown in not because
2787 * we have seen them but because they are ignored by other
2788 * daemons such as BIND 8 and Apache.
2791 addrlen
= sizeof(dev
->newsocket
->peer_address
.type
);
2792 memset(&dev
->newsocket
->peer_address
.type
, 0, addrlen
);
2793 fd
= accept(sock
->fd
, &dev
->newsocket
->peer_address
.type
.sa
,
2798 * Leave a space for stdio to work in.
2800 if (fd
>= 0 && fd
< 20) {
2802 new = fcntl(fd
, F_DUPFD
, 20);
2807 err
= "accept/fcntl";
2812 if (SOFT_ERROR(errno
))
2817 isc_log_iwrite(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
2818 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
2819 isc_msgcat
, ISC_MSGSET_SOCKET
,
2821 "%s: too many open file descriptors",
2844 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
2845 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2846 "internal_accept: %s() %s: %s", err
,
2847 isc_msgcat_get(isc_msgcat
,
2853 result
= ISC_R_UNEXPECTED
;
2855 if (addrlen
== 0U) {
2856 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2857 "internal_accept(): "
2858 "accept() failed to return "
2863 } else if (dev
->newsocket
->peer_address
.type
.sa
.sa_family
!=
2866 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
2867 "internal_accept(): "
2868 "accept() returned peer address "
2869 "family %u (expected %u)",
2870 dev
->newsocket
->peer_address
.
2875 } else if (fd
>= (int)manager
->maxsocks
) {
2876 isc_log_iwrite(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
2877 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
2878 isc_msgcat
, ISC_MSGSET_SOCKET
,
2881 "file descriptor exceeds limit (%d/%u)",
2882 fd
, manager
->maxsocks
);
2889 dev
->newsocket
->peer_address
.length
= addrlen
;
2890 dev
->newsocket
->pf
= sock
->pf
;
2894 * Pull off the done event.
2896 ISC_LIST_UNLINK(sock
->accept_list
, dev
, ev_link
);
2899 * Poke watcher if there are more pending accepts.
2901 if (!ISC_LIST_EMPTY(sock
->accept_list
))
2902 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_ACCEPT
);
2904 UNLOCK(&sock
->lock
);
2906 if (fd
!= -1 && (make_nonblock(fd
) != ISC_R_SUCCESS
)) {
2909 result
= ISC_R_UNEXPECTED
;
2913 * -1 means the new socket didn't happen.
2916 int lockid
= FDLOCK_ID(fd
);
2918 LOCK(&manager
->fdlock
[lockid
]);
2919 manager
->fds
[fd
] = dev
->newsocket
;
2920 manager
->fdstate
[fd
] = MANAGED
;
2921 UNLOCK(&manager
->fdlock
[lockid
]);
2923 LOCK(&manager
->lock
);
2924 ISC_LIST_APPEND(manager
->socklist
, dev
->newsocket
, link
);
2926 dev
->newsocket
->fd
= fd
;
2927 dev
->newsocket
->bound
= 1;
2928 dev
->newsocket
->connected
= 1;
2931 * Save away the remote address
2933 dev
->address
= dev
->newsocket
->peer_address
;
2936 if (manager
->maxfd
< fd
)
2937 manager
->maxfd
= fd
;
2940 socket_log(sock
, &dev
->newsocket
->peer_address
, CREATION
,
2941 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_ACCEPTEDCXN
,
2942 "accepted connection, new socket %p",
2945 UNLOCK(&manager
->lock
);
2947 inc_stats(manager
->stats
, sock
->statsindex
[STATID_ACCEPT
]);
2949 inc_stats(manager
->stats
, sock
->statsindex
[STATID_ACCEPTFAIL
]);
2950 dev
->newsocket
->references
--;
2951 free_socket(&dev
->newsocket
);
2955 * Fill in the done event details and send it off.
2957 dev
->result
= result
;
2958 task
= dev
->ev_sender
;
2959 dev
->ev_sender
= sock
;
2961 isc_task_sendanddetach(&task
, ISC_EVENT_PTR(&dev
));
2965 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_ACCEPT
);
2966 UNLOCK(&sock
->lock
);
2968 inc_stats(manager
->stats
, sock
->statsindex
[STATID_ACCEPTFAIL
]);
2973 internal_recv(isc_task_t
*me
, isc_event_t
*ev
) {
2974 isc_socketevent_t
*dev
;
2977 INSIST(ev
->ev_type
== ISC_SOCKEVENT_INTR
);
2979 sock
= ev
->ev_sender
;
2980 INSIST(VALID_SOCKET(sock
));
2983 socket_log(sock
, NULL
, IOEVENT
,
2984 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_INTERNALRECV
,
2985 "internal_recv: task %p got event %p", me
, ev
);
2987 INSIST(sock
->pending_recv
== 1);
2988 sock
->pending_recv
= 0;
2990 INSIST(sock
->references
> 0);
2991 sock
->references
--; /* the internal event is done with this socket */
2992 if (sock
->references
== 0) {
2993 UNLOCK(&sock
->lock
);
2999 * Try to do as much I/O as possible on this socket. There are no
3000 * limits here, currently.
3002 dev
= ISC_LIST_HEAD(sock
->recv_list
);
3003 while (dev
!= NULL
) {
3004 switch (doio_recv(sock
, dev
)) {
3010 * read of 0 means the remote end was closed.
3011 * Run through the event queue and dispatch all
3012 * the events with an EOF result code.
3015 dev
->result
= ISC_R_EOF
;
3016 send_recvdone_event(sock
, &dev
);
3017 dev
= ISC_LIST_HEAD(sock
->recv_list
);
3018 } while (dev
!= NULL
);
3023 send_recvdone_event(sock
, &dev
);
3027 dev
= ISC_LIST_HEAD(sock
->recv_list
);
3031 if (!ISC_LIST_EMPTY(sock
->recv_list
))
3032 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_READ
);
3034 UNLOCK(&sock
->lock
);
3038 internal_send(isc_task_t
*me
, isc_event_t
*ev
) {
3039 isc_socketevent_t
*dev
;
3042 INSIST(ev
->ev_type
== ISC_SOCKEVENT_INTW
);
3045 * Find out what socket this is and lock it.
3047 sock
= (isc_socket_t
*)ev
->ev_sender
;
3048 INSIST(VALID_SOCKET(sock
));
3051 socket_log(sock
, NULL
, IOEVENT
,
3052 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_INTERNALSEND
,
3053 "internal_send: task %p got event %p", me
, ev
);
3055 INSIST(sock
->pending_send
== 1);
3056 sock
->pending_send
= 0;
3058 INSIST(sock
->references
> 0);
3059 sock
->references
--; /* the internal event is done with this socket */
3060 if (sock
->references
== 0) {
3061 UNLOCK(&sock
->lock
);
3067 * Try to do as much I/O as possible on this socket. There are no
3068 * limits here, currently.
3070 dev
= ISC_LIST_HEAD(sock
->send_list
);
3071 while (dev
!= NULL
) {
3072 switch (doio_send(sock
, dev
)) {
3078 send_senddone_event(sock
, &dev
);
3082 dev
= ISC_LIST_HEAD(sock
->send_list
);
3086 if (!ISC_LIST_EMPTY(sock
->send_list
))
3087 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_WRITE
);
3089 UNLOCK(&sock
->lock
);
3093 internal_fdwatch_write(isc_task_t
*me
, isc_event_t
*ev
) {
3097 INSIST(ev
->ev_type
== ISC_SOCKEVENT_INTW
);
3100 * Find out what socket this is and lock it.
3102 sock
= (isc_socket_t
*)ev
->ev_sender
;
3103 INSIST(VALID_SOCKET(sock
));
3106 socket_log(sock
, NULL
, IOEVENT
,
3107 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_INTERNALSEND
,
3108 "internal_fdwatch_write: task %p got event %p", me
, ev
);
3110 INSIST(sock
->pending_send
== 1);
3112 UNLOCK(&sock
->lock
);
3113 more_data
= (sock
->fdwatchcb
)(me
, sock
, sock
->fdwatcharg
);
3116 sock
->pending_send
= 0;
3118 INSIST(sock
->references
> 0);
3119 sock
->references
--; /* the internal event is done with this socket */
3120 if (sock
->references
== 0) {
3121 UNLOCK(&sock
->lock
);
3127 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_WRITE
);
3129 UNLOCK(&sock
->lock
);
3133 internal_fdwatch_read(isc_task_t
*me
, isc_event_t
*ev
) {
3137 INSIST(ev
->ev_type
== ISC_SOCKEVENT_INTR
);
3140 * Find out what socket this is and lock it.
3142 sock
= (isc_socket_t
*)ev
->ev_sender
;
3143 INSIST(VALID_SOCKET(sock
));
3146 socket_log(sock
, NULL
, IOEVENT
,
3147 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_INTERNALRECV
,
3148 "internal_fdwatch_read: task %p got event %p", me
, ev
);
3150 INSIST(sock
->pending_recv
== 1);
3152 UNLOCK(&sock
->lock
);
3153 more_data
= (sock
->fdwatchcb
)(me
, sock
, sock
->fdwatcharg
);
3156 sock
->pending_recv
= 0;
3158 INSIST(sock
->references
> 0);
3159 sock
->references
--; /* the internal event is done with this socket */
3160 if (sock
->references
== 0) {
3161 UNLOCK(&sock
->lock
);
3167 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_READ
);
3169 UNLOCK(&sock
->lock
);
3173 * Process read/writes on each fd here. Avoid locking
3174 * and unlocking twice if both reads and writes are possible.
3177 process_fd(isc_socketmgr_t
*manager
, int fd
, isc_boolean_t readable
,
3178 isc_boolean_t writeable
)
3181 isc_boolean_t unlock_sock
;
3182 isc_boolean_t unwatch_read
= ISC_FALSE
, unwatch_write
= ISC_FALSE
;
3183 int lockid
= FDLOCK_ID(fd
);
3186 * If the socket is going to be closed, don't do more I/O.
3188 LOCK(&manager
->fdlock
[lockid
]);
3189 if (manager
->fdstate
[fd
] == CLOSE_PENDING
) {
3190 UNLOCK(&manager
->fdlock
[lockid
]);
3192 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
3193 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
3197 sock
= manager
->fds
[fd
];
3198 unlock_sock
= ISC_FALSE
;
3201 unwatch_read
= ISC_TRUE
;
3204 unlock_sock
= ISC_TRUE
;
3206 if (!SOCK_DEAD(sock
)) {
3208 dispatch_accept(sock
);
3210 dispatch_recv(sock
);
3212 unwatch_read
= ISC_TRUE
;
3217 unwatch_write
= ISC_TRUE
;
3221 unlock_sock
= ISC_TRUE
;
3224 if (!SOCK_DEAD(sock
)) {
3225 if (sock
->connecting
)
3226 dispatch_connect(sock
);
3228 dispatch_send(sock
);
3230 unwatch_write
= ISC_TRUE
;
3233 UNLOCK(&sock
->lock
);
3236 UNLOCK(&manager
->fdlock
[lockid
]);
3238 (void)unwatch_fd(manager
, fd
, SELECT_POKE_READ
);
3240 (void)unwatch_fd(manager
, fd
, SELECT_POKE_WRITE
);
3245 static isc_boolean_t
3246 process_fds(isc_socketmgr_t
*manager
, struct kevent
*events
, int nevents
) {
3248 isc_boolean_t readable
, writable
;
3249 isc_boolean_t done
= ISC_FALSE
;
3250 #ifdef ISC_PLATFORM_USETHREADS
3251 isc_boolean_t have_ctlevent
= ISC_FALSE
;
3254 if (nevents
== manager
->nevents
) {
3256 * This is not an error, but something unexpected. If this
3257 * happens, it may indicate the need for increasing
3258 * ISC_SOCKET_MAXEVENTS.
3260 manager_log(manager
, ISC_LOGCATEGORY_GENERAL
,
3261 ISC_LOGMODULE_SOCKET
, ISC_LOG_INFO
,
3262 "maximum number of FD events (%d) received",
3266 for (i
= 0; i
< nevents
; i
++) {
3267 REQUIRE(events
[i
].ident
< manager
->maxsocks
);
3268 #ifdef ISC_PLATFORM_USETHREADS
3269 if (events
[i
].ident
== (uintptr_t)manager
->pipe_fds
[0]) {
3270 have_ctlevent
= ISC_TRUE
;
3274 readable
= ISC_TF(events
[i
].filter
== EVFILT_READ
);
3275 writable
= ISC_TF(events
[i
].filter
== EVFILT_WRITE
);
3276 process_fd(manager
, events
[i
].ident
, readable
, writable
);
3279 #ifdef ISC_PLATFORM_USETHREADS
3281 done
= process_ctlfd(manager
);
3286 #elif defined(USE_EPOLL)
3287 static isc_boolean_t
3288 process_fds(isc_socketmgr_t
*manager
, struct epoll_event
*events
, int nevents
) {
3290 isc_boolean_t done
= ISC_FALSE
;
3291 #ifdef ISC_PLATFORM_USETHREADS
3292 isc_boolean_t have_ctlevent
= ISC_FALSE
;
3295 if (nevents
== manager
->nevents
) {
3296 manager_log(manager
, ISC_LOGCATEGORY_GENERAL
,
3297 ISC_LOGMODULE_SOCKET
, ISC_LOG_INFO
,
3298 "maximum number of FD events (%d) received",
3302 for (i
= 0; i
< nevents
; i
++) {
3303 REQUIRE(events
[i
].data
.fd
< (int)manager
->maxsocks
);
3304 #ifdef ISC_PLATFORM_USETHREADS
3305 if (events
[i
].data
.fd
== manager
->pipe_fds
[0]) {
3306 have_ctlevent
= ISC_TRUE
;
3310 if ((events
[i
].events
& EPOLLERR
) != 0 ||
3311 (events
[i
].events
& EPOLLHUP
) != 0) {
3313 * epoll does not set IN/OUT bits on an erroneous
3314 * condition, so we need to try both anyway. This is a
3315 * bit inefficient, but should be okay for such rare
3316 * events. Note also that the read or write attempt
3317 * won't block because we use non-blocking sockets.
3319 events
[i
].events
|= (EPOLLIN
| EPOLLOUT
);
3321 process_fd(manager
, events
[i
].data
.fd
,
3322 (events
[i
].events
& EPOLLIN
) != 0,
3323 (events
[i
].events
& EPOLLOUT
) != 0);
3326 #ifdef ISC_PLATFORM_USETHREADS
3328 done
= process_ctlfd(manager
);
3333 #elif defined(USE_DEVPOLL)
3334 static isc_boolean_t
3335 process_fds(isc_socketmgr_t
*manager
, struct pollfd
*events
, int nevents
) {
3337 isc_boolean_t done
= ISC_FALSE
;
3338 #ifdef ISC_PLATFORM_USETHREADS
3339 isc_boolean_t have_ctlevent
= ISC_FALSE
;
3342 if (nevents
== manager
->nevents
) {
3343 manager_log(manager
, ISC_LOGCATEGORY_GENERAL
,
3344 ISC_LOGMODULE_SOCKET
, ISC_LOG_INFO
,
3345 "maximum number of FD events (%d) received",
3349 for (i
= 0; i
< nevents
; i
++) {
3350 REQUIRE(events
[i
].fd
< (int)manager
->maxsocks
);
3351 #ifdef ISC_PLATFORM_USETHREADS
3352 if (events
[i
].fd
== manager
->pipe_fds
[0]) {
3353 have_ctlevent
= ISC_TRUE
;
3357 process_fd(manager
, events
[i
].fd
,
3358 (events
[i
].events
& POLLIN
) != 0,
3359 (events
[i
].events
& POLLOUT
) != 0);
3362 #ifdef ISC_PLATFORM_USETHREADS
3364 done
= process_ctlfd(manager
);
3369 #elif defined(USE_SELECT)
3371 process_fds(isc_socketmgr_t
*manager
, int maxfd
,
3372 fd_set
*readfds
, fd_set
*writefds
)
3376 REQUIRE(maxfd
<= (int)manager
->maxsocks
);
3378 for (i
= 0; i
< maxfd
; i
++) {
3379 #ifdef ISC_PLATFORM_USETHREADS
3380 if (i
== manager
->pipe_fds
[0] || i
== manager
->pipe_fds
[1])
3382 #endif /* ISC_PLATFORM_USETHREADS */
3383 process_fd(manager
, i
, FD_ISSET(i
, readfds
),
3384 FD_ISSET(i
, writefds
));
3389 #ifdef ISC_PLATFORM_USETHREADS
3390 static isc_boolean_t
3391 process_ctlfd(isc_socketmgr_t
*manager
) {
3395 select_readmsg(manager
, &fd
, &msg
);
3397 manager_log(manager
, IOEVENT
,
3398 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
3400 "watcher got message %d "
3401 "for socket %d"), msg
, fd
);
3406 if (msg
== SELECT_POKE_NOTHING
)
3410 * Handle shutdown message. We really should
3411 * jump out of this loop right away, but
3412 * it doesn't matter if we have to do a little
3415 if (msg
== SELECT_POKE_SHUTDOWN
)
3419 * This is a wakeup on a socket. Look
3420 * at the event queue for both read and write,
3421 * and decide if we need to watch on it now
3424 wakeup_socket(manager
, fd
, msg
);
3431 * This is the thread that will loop forever, always in a select or poll
3434 * When select returns something to do, track down what thread gets to do
3435 * this I/O and post the event to it.
3437 static isc_threadresult_t
3438 watcher(void *uap
) {
3439 isc_socketmgr_t
*manager
= uap
;
3444 const char *fnname
= "kevent()";
3445 #elif defined (USE_EPOLL)
3446 const char *fnname
= "epoll_wait()";
3447 #elif defined(USE_DEVPOLL)
3448 const char *fnname
= "ioctl(DP_POLL)";
3450 #elif defined (USE_SELECT)
3451 const char *fnname
= "select()";
3454 char strbuf
[ISC_STRERRORSIZE
];
3455 #ifdef ISC_SOCKET_USE_POLLWATCH
3456 pollstate_t pollstate
= poll_idle
;
3460 * Get the control fd here. This will never change.
3462 ctlfd
= manager
->pipe_fds
[0];
3467 cc
= kevent(manager
->kqueue_fd
, NULL
, 0,
3468 manager
->events
, manager
->nevents
, NULL
);
3469 #elif defined(USE_EPOLL)
3470 cc
= epoll_wait(manager
->epoll_fd
, manager
->events
,
3471 manager
->nevents
, -1);
3472 #elif defined(USE_DEVPOLL)
3473 dvp
.dp_fds
= manager
->events
;
3474 dvp
.dp_nfds
= manager
->nevents
;
3475 #ifndef ISC_SOCKET_USE_POLLWATCH
3476 dvp
.dp_timeout
= -1;
3478 if (pollstate
== poll_idle
)
3479 dvp
.dp_timeout
= -1;
3481 dvp
.dp_timeout
= ISC_SOCKET_POLLWATCH_TIMEOUT
;
3482 #endif /* ISC_SOCKET_USE_POLLWATCH */
3483 cc
= ioctl(manager
->devpoll_fd
, DP_POLL
, &dvp
);
3484 #elif defined(USE_SELECT)
3485 LOCK(&manager
->lock
);
3486 memcpy(manager
->read_fds_copy
, manager
->read_fds
,
3487 manager
->fd_bufsize
);
3488 memcpy(manager
->write_fds_copy
, manager
->write_fds
,
3489 manager
->fd_bufsize
);
3490 maxfd
= manager
->maxfd
+ 1;
3491 UNLOCK(&manager
->lock
);
3493 cc
= select(maxfd
, manager
->read_fds_copy
,
3494 manager
->write_fds_copy
, NULL
, NULL
);
3495 #endif /* USE_KQUEUE */
3497 if (cc
< 0 && !SOFT_ERROR(errno
)) {
3498 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
3499 FATAL_ERROR(__FILE__
, __LINE__
,
3500 "%s %s: %s", fnname
,
3501 isc_msgcat_get(isc_msgcat
,
3507 #if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH)
3509 if (pollstate
== poll_active
)
3510 pollstate
= poll_checking
;
3511 else if (pollstate
== poll_checking
)
3512 pollstate
= poll_idle
;
3513 } else if (cc
> 0) {
3514 if (pollstate
== poll_checking
) {
3516 * XXX: We'd like to use a more
3517 * verbose log level as it's actually an
3518 * unexpected event, but the kernel bug
3519 * reportedly happens pretty frequently
3520 * (and it can also be a false positive)
3521 * so it would be just too noisy.
3523 manager_log(manager
,
3524 ISC_LOGCATEGORY_GENERAL
,
3525 ISC_LOGMODULE_SOCKET
,
3527 "unexpected POLL timeout");
3529 pollstate
= poll_active
;
3534 #if defined(USE_KQUEUE) || defined (USE_EPOLL) || defined (USE_DEVPOLL)
3535 done
= process_fds(manager
, manager
->events
, cc
);
3536 #elif defined(USE_SELECT)
3537 process_fds(manager
, maxfd
, manager
->read_fds_copy
,
3538 manager
->write_fds_copy
);
3541 * Process reads on internal, control fd.
3543 if (FD_ISSET(ctlfd
, manager
->read_fds_copy
))
3544 done
= process_ctlfd(manager
);
3548 manager_log(manager
, TRACE
, "%s",
3549 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3550 ISC_MSG_EXITING
, "watcher exiting"));
3552 return ((isc_threadresult_t
)0);
3554 #endif /* ISC_PLATFORM_USETHREADS */
3557 isc__socketmgr_setreserved(isc_socketmgr_t
*manager
, isc_uint32_t reserved
) {
3559 REQUIRE(VALID_MANAGER(manager
));
3561 manager
->reserved
= reserved
;
3565 * Create a new socket manager.
3569 setup_watcher(isc_mem_t
*mctx
, isc_socketmgr_t
*manager
) {
3570 isc_result_t result
;
3571 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
3572 char strbuf
[ISC_STRERRORSIZE
];
3576 manager
->nevents
= ISC_SOCKET_MAXEVENTS
;
3577 manager
->events
= isc_mem_get(mctx
, sizeof(struct kevent
) *
3579 if (manager
->events
== NULL
)
3580 return (ISC_R_NOMEMORY
);
3581 manager
->kqueue_fd
= kqueue();
3582 if (manager
->kqueue_fd
== -1) {
3583 result
= isc__errno2result(errno
);
3584 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
3585 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3587 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3588 ISC_MSG_FAILED
, "failed"),
3590 isc_mem_put(mctx
, manager
->events
,
3591 sizeof(struct kevent
) * manager
->nevents
);
3595 #ifdef ISC_PLATFORM_USETHREADS
3596 result
= watch_fd(manager
, manager
->pipe_fds
[0], SELECT_POKE_READ
);
3597 if (result
!= ISC_R_SUCCESS
) {
3598 close(manager
->kqueue_fd
);
3599 isc_mem_put(mctx
, manager
->events
,
3600 sizeof(struct kevent
) * manager
->nevents
);
3603 #endif /* ISC_PLATFORM_USETHREADS */
3604 #elif defined(USE_EPOLL)
3605 manager
->nevents
= ISC_SOCKET_MAXEVENTS
;
3606 manager
->events
= isc_mem_get(mctx
, sizeof(struct epoll_event
) *
3608 if (manager
->events
== NULL
)
3609 return (ISC_R_NOMEMORY
);
3610 manager
->epoll_fd
= epoll_create(manager
->nevents
);
3611 if (manager
->epoll_fd
== -1) {
3612 result
= isc__errno2result(errno
);
3613 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
3614 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3615 "epoll_create %s: %s",
3616 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3617 ISC_MSG_FAILED
, "failed"),
3619 isc_mem_put(mctx
, manager
->events
,
3620 sizeof(struct epoll_event
) * manager
->nevents
);
3623 #ifdef ISC_PLATFORM_USETHREADS
3624 result
= watch_fd(manager
, manager
->pipe_fds
[0], SELECT_POKE_READ
);
3625 if (result
!= ISC_R_SUCCESS
) {
3626 close(manager
->epoll_fd
);
3627 isc_mem_put(mctx
, manager
->events
,
3628 sizeof(struct epoll_event
) * manager
->nevents
);
3631 #endif /* ISC_PLATFORM_USETHREADS */
3632 #elif defined(USE_DEVPOLL)
3634 * XXXJT: /dev/poll seems to reject large numbers of events,
3635 * so we should be careful about redefining ISC_SOCKET_MAXEVENTS.
3637 manager
->nevents
= ISC_SOCKET_MAXEVENTS
;
3638 manager
->events
= isc_mem_get(mctx
, sizeof(struct pollfd
) *
3640 if (manager
->events
== NULL
)
3641 return (ISC_R_NOMEMORY
);
3643 * Note: fdpollinfo should be able to support all possible FDs, so
3644 * it must have maxsocks entries (not nevents).
3646 manager
->fdpollinfo
= isc_mem_get(mctx
, sizeof(pollinfo_t
) *
3648 if (manager
->fdpollinfo
== NULL
) {
3649 isc_mem_put(mctx
, manager
->events
,
3650 sizeof(pollinfo_t
) * manager
->maxsocks
);
3651 return (ISC_R_NOMEMORY
);
3653 memset(manager
->fdpollinfo
, 0, sizeof(pollinfo_t
) * manager
->maxsocks
);
3654 manager
->devpoll_fd
= open("/dev/poll", O_RDWR
);
3655 if (manager
->devpoll_fd
== -1) {
3656 result
= isc__errno2result(errno
);
3657 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
3658 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3659 "open(/dev/poll) %s: %s",
3660 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3661 ISC_MSG_FAILED
, "failed"),
3663 isc_mem_put(mctx
, manager
->events
,
3664 sizeof(struct pollfd
) * manager
->nevents
);
3665 isc_mem_put(mctx
, manager
->fdpollinfo
,
3666 sizeof(pollinfo_t
) * manager
->maxsocks
);
3669 #ifdef ISC_PLATFORM_USETHREADS
3670 result
= watch_fd(manager
, manager
->pipe_fds
[0], SELECT_POKE_READ
);
3671 if (result
!= ISC_R_SUCCESS
) {
3672 close(manager
->devpoll_fd
);
3673 isc_mem_put(mctx
, manager
->events
,
3674 sizeof(struct pollfd
) * manager
->nevents
);
3675 isc_mem_put(mctx
, manager
->fdpollinfo
,
3676 sizeof(pollinfo_t
) * manager
->maxsocks
);
3679 #endif /* ISC_PLATFORM_USETHREADS */
3680 #elif defined(USE_SELECT)
3683 #if ISC_SOCKET_MAXSOCKETS > FD_SETSIZE
3685 * Note: this code should also cover the case of MAXSOCKETS <=
3686 * FD_SETSIZE, but we separate the cases to avoid possible portability
3687 * issues regarding howmany() and the actual representation of fd_set.
3689 manager
->fd_bufsize
= howmany(manager
->maxsocks
, NFDBITS
) *
3692 manager
->fd_bufsize
= sizeof(fd_set
);
3695 manager
->read_fds
= NULL
;
3696 manager
->read_fds_copy
= NULL
;
3697 manager
->write_fds
= NULL
;
3698 manager
->write_fds_copy
= NULL
;
3700 manager
->read_fds
= isc_mem_get(mctx
, manager
->fd_bufsize
);
3701 if (manager
->read_fds
!= NULL
)
3702 manager
->read_fds_copy
= isc_mem_get(mctx
, manager
->fd_bufsize
);
3703 if (manager
->read_fds_copy
!= NULL
)
3704 manager
->write_fds
= isc_mem_get(mctx
, manager
->fd_bufsize
);
3705 if (manager
->write_fds
!= NULL
) {
3706 manager
->write_fds_copy
= isc_mem_get(mctx
,
3707 manager
->fd_bufsize
);
3709 if (manager
->write_fds_copy
== NULL
) {
3710 if (manager
->write_fds
!= NULL
) {
3711 isc_mem_put(mctx
, manager
->write_fds
,
3712 manager
->fd_bufsize
);
3714 if (manager
->read_fds_copy
!= NULL
) {
3715 isc_mem_put(mctx
, manager
->read_fds_copy
,
3716 manager
->fd_bufsize
);
3718 if (manager
->read_fds
!= NULL
) {
3719 isc_mem_put(mctx
, manager
->read_fds
,
3720 manager
->fd_bufsize
);
3722 return (ISC_R_NOMEMORY
);
3724 memset(manager
->read_fds
, 0, manager
->fd_bufsize
);
3725 memset(manager
->write_fds
, 0, manager
->fd_bufsize
);
3727 #ifdef ISC_PLATFORM_USETHREADS
3728 (void)watch_fd(manager
, manager
->pipe_fds
[0], SELECT_POKE_READ
);
3729 manager
->maxfd
= manager
->pipe_fds
[0];
3730 #else /* ISC_PLATFORM_USETHREADS */
3732 #endif /* ISC_PLATFORM_USETHREADS */
3733 #endif /* USE_KQUEUE */
3735 return (ISC_R_SUCCESS
);
3739 cleanup_watcher(isc_mem_t
*mctx
, isc_socketmgr_t
*manager
) {
3740 #ifdef ISC_PLATFORM_USETHREADS
3741 isc_result_t result
;
3743 result
= unwatch_fd(manager
, manager
->pipe_fds
[0], SELECT_POKE_READ
);
3744 if (result
!= ISC_R_SUCCESS
) {
3745 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3746 "epoll_ctl(DEL) %s",
3747 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3748 ISC_MSG_FAILED
, "failed"));
3750 #endif /* ISC_PLATFORM_USETHREADS */
3753 close(manager
->kqueue_fd
);
3754 isc_mem_put(mctx
, manager
->events
,
3755 sizeof(struct kevent
) * manager
->nevents
);
3756 #elif defined(USE_EPOLL)
3757 close(manager
->epoll_fd
);
3758 isc_mem_put(mctx
, manager
->events
,
3759 sizeof(struct epoll_event
) * manager
->nevents
);
3760 #elif defined(USE_DEVPOLL)
3761 close(manager
->devpoll_fd
);
3762 isc_mem_put(mctx
, manager
->events
,
3763 sizeof(struct pollfd
) * manager
->nevents
);
3764 isc_mem_put(mctx
, manager
->fdpollinfo
,
3765 sizeof(pollinfo_t
) * manager
->maxsocks
);
3766 #elif defined(USE_SELECT)
3767 if (manager
->read_fds
!= NULL
)
3768 isc_mem_put(mctx
, manager
->read_fds
, manager
->fd_bufsize
);
3769 if (manager
->read_fds_copy
!= NULL
)
3770 isc_mem_put(mctx
, manager
->read_fds_copy
, manager
->fd_bufsize
);
3771 if (manager
->write_fds
!= NULL
)
3772 isc_mem_put(mctx
, manager
->write_fds
, manager
->fd_bufsize
);
3773 if (manager
->write_fds_copy
!= NULL
)
3774 isc_mem_put(mctx
, manager
->write_fds_copy
, manager
->fd_bufsize
);
3775 #endif /* USE_KQUEUE */
3779 isc_socketmgr_create(isc_mem_t
*mctx
, isc_socketmgr_t
**managerp
) {
3780 return (isc_socketmgr_create2(mctx
, managerp
, 0));
3784 isc_socketmgr_create2(isc_mem_t
*mctx
, isc_socketmgr_t
**managerp
,
3785 unsigned int maxsocks
)
3788 isc_socketmgr_t
*manager
;
3789 #ifdef ISC_PLATFORM_USETHREADS
3790 char strbuf
[ISC_STRERRORSIZE
];
3792 isc_result_t result
;
3794 REQUIRE(managerp
!= NULL
&& *managerp
== NULL
);
3796 #ifndef ISC_PLATFORM_USETHREADS
3797 if (socketmgr
!= NULL
) {
3798 /* Don't allow maxsocks to be updated */
3799 if (maxsocks
> 0 && socketmgr
->maxsocks
!= maxsocks
)
3800 return (ISC_R_EXISTS
);
3803 *managerp
= socketmgr
;
3804 return (ISC_R_SUCCESS
);
3806 #endif /* ISC_PLATFORM_USETHREADS */
3809 maxsocks
= ISC_SOCKET_MAXSOCKETS
;
3811 manager
= isc_mem_get(mctx
, sizeof(*manager
));
3812 if (manager
== NULL
)
3813 return (ISC_R_NOMEMORY
);
3815 /* zero-clear so that necessary cleanup on failure will be easy */
3816 memset(manager
, 0, sizeof(*manager
));
3817 manager
->maxsocks
= maxsocks
;
3818 manager
->reserved
= 0;
3819 manager
->fds
= isc_mem_get(mctx
,
3820 manager
->maxsocks
* sizeof(isc_socket_t
*));
3821 if (manager
->fds
== NULL
) {
3822 result
= ISC_R_NOMEMORY
;
3825 manager
->fdstate
= isc_mem_get(mctx
, manager
->maxsocks
* sizeof(int));
3826 if (manager
->fdstate
== NULL
) {
3827 result
= ISC_R_NOMEMORY
;
3830 manager
->stats
= NULL
;
3832 manager
->magic
= SOCKET_MANAGER_MAGIC
;
3833 manager
->mctx
= NULL
;
3834 memset(manager
->fds
, 0, manager
->maxsocks
* sizeof(isc_socket_t
*));
3835 ISC_LIST_INIT(manager
->socklist
);
3836 result
= isc_mutex_init(&manager
->lock
);
3837 if (result
!= ISC_R_SUCCESS
)
3839 manager
->fdlock
= isc_mem_get(mctx
, FDLOCK_COUNT
* sizeof(isc_mutex_t
));
3840 if (manager
->fdlock
== NULL
) {
3841 result
= ISC_R_NOMEMORY
;
3844 for (i
= 0; i
< FDLOCK_COUNT
; i
++) {
3845 result
= isc_mutex_init(&manager
->fdlock
[i
]);
3846 if (result
!= ISC_R_SUCCESS
) {
3848 DESTROYLOCK(&manager
->fdlock
[i
]);
3849 isc_mem_put(mctx
, manager
->fdlock
,
3850 FDLOCK_COUNT
* sizeof(isc_mutex_t
));
3851 manager
->fdlock
= NULL
;
3856 #ifdef ISC_PLATFORM_USETHREADS
3857 if (isc_condition_init(&manager
->shutdown_ok
) != ISC_R_SUCCESS
) {
3858 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3859 "isc_condition_init() %s",
3860 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3861 ISC_MSG_FAILED
, "failed"));
3862 result
= ISC_R_UNEXPECTED
;
3867 * Create the special fds that will be used to wake up the
3868 * select/poll loop when something internal needs to be done.
3870 if (pipe(manager
->pipe_fds
) != 0) {
3871 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
3872 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3874 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3875 ISC_MSG_FAILED
, "failed"),
3877 result
= ISC_R_UNEXPECTED
;
3878 goto cleanup_condition
;
3881 RUNTIME_CHECK(make_nonblock(manager
->pipe_fds
[0]) == ISC_R_SUCCESS
);
3883 RUNTIME_CHECK(make_nonblock(manager
->pipe_fds
[1]) == ISC_R_SUCCESS
);
3885 #else /* ISC_PLATFORM_USETHREADS */
3887 #endif /* ISC_PLATFORM_USETHREADS */
3890 * Set up initial state for the select loop
3892 result
= setup_watcher(mctx
, manager
);
3893 if (result
!= ISC_R_SUCCESS
)
3895 memset(manager
->fdstate
, 0, manager
->maxsocks
* sizeof(int));
3896 #ifdef ISC_PLATFORM_USETHREADS
3898 * Start up the select/poll thread.
3900 if (isc_thread_create(watcher
, manager
, &manager
->watcher
) !=
3902 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
3903 "isc_thread_create() %s",
3904 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
3905 ISC_MSG_FAILED
, "failed"));
3906 cleanup_watcher(mctx
, manager
);
3907 result
= ISC_R_UNEXPECTED
;
3910 #endif /* ISC_PLATFORM_USETHREADS */
3911 isc_mem_attach(mctx
, &manager
->mctx
);
3913 #ifndef ISC_PLATFORM_USETHREADS
3914 socketmgr
= manager
;
3915 #endif /* ISC_PLATFORM_USETHREADS */
3916 *managerp
= manager
;
3918 return (ISC_R_SUCCESS
);
3921 #ifdef ISC_PLATFORM_USETHREADS
3922 (void)close(manager
->pipe_fds
[0]);
3923 (void)close(manager
->pipe_fds
[1]);
3924 #endif /* ISC_PLATFORM_USETHREADS */
3926 #ifdef ISC_PLATFORM_USETHREADS
3928 (void)isc_condition_destroy(&manager
->shutdown_ok
);
3929 #endif /* ISC_PLATFORM_USETHREADS */
3933 if (manager
->fdlock
!= NULL
) {
3934 for (i
= 0; i
< FDLOCK_COUNT
; i
++)
3935 DESTROYLOCK(&manager
->fdlock
[i
]);
3937 DESTROYLOCK(&manager
->lock
);
3940 if (manager
->fdlock
!= NULL
) {
3941 isc_mem_put(mctx
, manager
->fdlock
,
3942 FDLOCK_COUNT
* sizeof(isc_mutex_t
));
3944 if (manager
->fdstate
!= NULL
) {
3945 isc_mem_put(mctx
, manager
->fdstate
,
3946 manager
->maxsocks
* sizeof(int));
3948 if (manager
->fds
!= NULL
) {
3949 isc_mem_put(mctx
, manager
->fds
,
3950 manager
->maxsocks
* sizeof(isc_socket_t
*));
3952 isc_mem_put(mctx
, manager
, sizeof(*manager
));
3958 isc_socketmgr_getmaxsockets(isc_socketmgr_t
*manager
, unsigned int *nsockp
) {
3959 REQUIRE(VALID_MANAGER(manager
));
3960 REQUIRE(nsockp
!= NULL
);
3962 *nsockp
= manager
->maxsocks
;
3964 return (ISC_R_SUCCESS
);
3968 isc_socketmgr_setstats(isc_socketmgr_t
*manager
, isc_stats_t
*stats
) {
3969 REQUIRE(VALID_MANAGER(manager
));
3970 REQUIRE(ISC_LIST_EMPTY(manager
->socklist
));
3971 REQUIRE(manager
->stats
== NULL
);
3972 REQUIRE(isc_stats_ncounters(stats
) == isc_sockstatscounter_max
);
3974 isc_stats_attach(stats
, &manager
->stats
);
3978 isc_socketmgr_destroy(isc_socketmgr_t
**managerp
) {
3979 isc_socketmgr_t
*manager
;
3984 * Destroy a socket manager.
3987 REQUIRE(managerp
!= NULL
);
3988 manager
= *managerp
;
3989 REQUIRE(VALID_MANAGER(manager
));
3991 #ifndef ISC_PLATFORM_USETHREADS
3992 if (manager
->refs
> 1) {
3997 #endif /* ISC_PLATFORM_USETHREADS */
3999 LOCK(&manager
->lock
);
4001 #ifdef ISC_PLATFORM_USETHREADS
4003 * Wait for all sockets to be destroyed.
4005 while (!ISC_LIST_EMPTY(manager
->socklist
)) {
4006 manager_log(manager
, CREATION
, "%s",
4007 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
4008 ISC_MSG_SOCKETSREMAIN
,
4010 WAIT(&manager
->shutdown_ok
, &manager
->lock
);
4012 #else /* ISC_PLATFORM_USETHREADS */
4014 * Hope all sockets have been destroyed.
4016 if (!ISC_LIST_EMPTY(manager
->socklist
)) {
4017 manager_log(manager
, CREATION
, "%s",
4018 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_SOCKET
,
4019 ISC_MSG_SOCKETSREMAIN
,
4023 #endif /* ISC_PLATFORM_USETHREADS */
4025 UNLOCK(&manager
->lock
);
4028 * Here, poke our select/poll thread. Do this by closing the write
4029 * half of the pipe, which will send EOF to the read half.
4030 * This is currently a no-op in the non-threaded case.
4032 select_poke(manager
, 0, SELECT_POKE_SHUTDOWN
);
4034 #ifdef ISC_PLATFORM_USETHREADS
4036 * Wait for thread to exit.
4038 if (isc_thread_join(manager
->watcher
, NULL
) != ISC_R_SUCCESS
)
4039 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
4040 "isc_thread_join() %s",
4041 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
4042 ISC_MSG_FAILED
, "failed"));
4043 #endif /* ISC_PLATFORM_USETHREADS */
4048 cleanup_watcher(manager
->mctx
, manager
);
4050 #ifdef ISC_PLATFORM_USETHREADS
4051 (void)close(manager
->pipe_fds
[0]);
4052 (void)close(manager
->pipe_fds
[1]);
4053 (void)isc_condition_destroy(&manager
->shutdown_ok
);
4054 #endif /* ISC_PLATFORM_USETHREADS */
4056 for (i
= 0; i
< (int)manager
->maxsocks
; i
++)
4057 if (manager
->fdstate
[i
] == CLOSE_PENDING
) /* no need to lock */
4060 isc_mem_put(manager
->mctx
, manager
->fds
,
4061 manager
->maxsocks
* sizeof(isc_socket_t
*));
4062 isc_mem_put(manager
->mctx
, manager
->fdstate
,
4063 manager
->maxsocks
* sizeof(int));
4065 if (manager
->stats
!= NULL
)
4066 isc_stats_detach(&manager
->stats
);
4068 if (manager
->fdlock
!= NULL
) {
4069 for (i
= 0; i
< FDLOCK_COUNT
; i
++)
4070 DESTROYLOCK(&manager
->fdlock
[i
]);
4071 isc_mem_put(manager
->mctx
, manager
->fdlock
,
4072 FDLOCK_COUNT
* sizeof(isc_mutex_t
));
4074 DESTROYLOCK(&manager
->lock
);
4076 mctx
= manager
->mctx
;
4077 isc_mem_put(mctx
, manager
, sizeof(*manager
));
4079 isc_mem_detach(&mctx
);
4085 socket_recv(isc_socket_t
*sock
, isc_socketevent_t
*dev
, isc_task_t
*task
,
4089 isc_boolean_t have_lock
= ISC_FALSE
;
4090 isc_task_t
*ntask
= NULL
;
4091 isc_result_t result
= ISC_R_SUCCESS
;
4093 dev
->ev_sender
= task
;
4095 if (sock
->type
== isc_sockettype_udp
) {
4096 io_state
= doio_recv(sock
, dev
);
4099 have_lock
= ISC_TRUE
;
4101 if (ISC_LIST_EMPTY(sock
->recv_list
))
4102 io_state
= doio_recv(sock
, dev
);
4104 io_state
= DOIO_SOFT
;
4110 * We couldn't read all or part of the request right now, so
4113 * Attach to socket and to task
4115 isc_task_attach(task
, &ntask
);
4116 dev
->attributes
|= ISC_SOCKEVENTATTR_ATTACHED
;
4120 have_lock
= ISC_TRUE
;
4124 * Enqueue the request. If the socket was previously not being
4125 * watched, poke the watcher to start paying attention to it.
4127 if (ISC_LIST_EMPTY(sock
->recv_list
) && !sock
->pending_recv
)
4128 select_poke(sock
->manager
, sock
->fd
, SELECT_POKE_READ
);
4129 ISC_LIST_ENQUEUE(sock
->recv_list
, dev
, ev_link
);
4131 socket_log(sock
, NULL
, EVENT
, NULL
, 0, 0,
4132 "socket_recv: event %p -> task %p",
4135 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) != 0)
4136 result
= ISC_R_INPROGRESS
;
4140 dev
->result
= ISC_R_EOF
;
4145 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) == 0)
4146 send_recvdone_event(sock
, &dev
);
4151 UNLOCK(&sock
->lock
);
4157 isc_socket_recvv(isc_socket_t
*sock
, isc_bufferlist_t
*buflist
,
4158 unsigned int minimum
, isc_task_t
*task
,
4159 isc_taskaction_t action
, const void *arg
)
4161 isc_socketevent_t
*dev
;
4162 isc_socketmgr_t
*manager
;
4163 unsigned int iocount
;
4164 isc_buffer_t
*buffer
;
4166 REQUIRE(VALID_SOCKET(sock
));
4167 REQUIRE(buflist
!= NULL
);
4168 REQUIRE(!ISC_LIST_EMPTY(*buflist
));
4169 REQUIRE(task
!= NULL
);
4170 REQUIRE(action
!= NULL
);
4172 manager
= sock
->manager
;
4173 REQUIRE(VALID_MANAGER(manager
));
4175 iocount
= isc_bufferlist_availablecount(buflist
);
4176 REQUIRE(iocount
> 0);
4178 INSIST(sock
->bound
);
4180 dev
= allocate_socketevent(sock
, ISC_SOCKEVENT_RECVDONE
, action
, arg
);
4182 return (ISC_R_NOMEMORY
);
4186 * UDP sockets are always partial read
4188 if (sock
->type
== isc_sockettype_udp
)
4192 dev
->minimum
= iocount
;
4194 dev
->minimum
= minimum
;
4198 * Move each buffer from the passed in list to our internal one.
4200 buffer
= ISC_LIST_HEAD(*buflist
);
4201 while (buffer
!= NULL
) {
4202 ISC_LIST_DEQUEUE(*buflist
, buffer
, link
);
4203 ISC_LIST_ENQUEUE(dev
->bufferlist
, buffer
, link
);
4204 buffer
= ISC_LIST_HEAD(*buflist
);
4207 return (socket_recv(sock
, dev
, task
, 0));
4211 isc_socket_recv(isc_socket_t
*sock
, isc_region_t
*region
, unsigned int minimum
,
4212 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
)
4214 isc_socketevent_t
*dev
;
4215 isc_socketmgr_t
*manager
;
4217 REQUIRE(VALID_SOCKET(sock
));
4218 REQUIRE(action
!= NULL
);
4220 manager
= sock
->manager
;
4221 REQUIRE(VALID_MANAGER(manager
));
4223 INSIST(sock
->bound
);
4225 dev
= allocate_socketevent(sock
, ISC_SOCKEVENT_RECVDONE
, action
, arg
);
4227 return (ISC_R_NOMEMORY
);
4229 return (isc_socket_recv2(sock
, region
, minimum
, task
, dev
, 0));
4233 isc_socket_recv2(isc_socket_t
*sock
, isc_region_t
*region
,
4234 unsigned int minimum
, isc_task_t
*task
,
4235 isc_socketevent_t
*event
, unsigned int flags
)
4237 event
->ev_sender
= sock
;
4238 event
->result
= ISC_R_UNEXPECTED
;
4239 ISC_LIST_INIT(event
->bufferlist
);
4240 event
->region
= *region
;
4243 event
->attributes
= 0;
4246 * UDP sockets are always partial read.
4248 if (sock
->type
== isc_sockettype_udp
)
4252 event
->minimum
= region
->length
;
4254 event
->minimum
= minimum
;
4257 return (socket_recv(sock
, event
, task
, flags
));
4261 socket_send(isc_socket_t
*sock
, isc_socketevent_t
*dev
, isc_task_t
*task
,
4262 isc_sockaddr_t
*address
, struct in6_pktinfo
*pktinfo
,
4266 isc_boolean_t have_lock
= ISC_FALSE
;
4267 isc_task_t
*ntask
= NULL
;
4268 isc_result_t result
= ISC_R_SUCCESS
;
4270 dev
->ev_sender
= task
;
4272 set_dev_address(address
, sock
, dev
);
4273 if (pktinfo
!= NULL
) {
4274 dev
->attributes
|= ISC_SOCKEVENTATTR_PKTINFO
;
4275 dev
->pktinfo
= *pktinfo
;
4277 if (!isc_sockaddr_issitelocal(&dev
->address
) &&
4278 !isc_sockaddr_islinklocal(&dev
->address
)) {
4279 socket_log(sock
, NULL
, TRACE
, isc_msgcat
,
4280 ISC_MSGSET_SOCKET
, ISC_MSG_PKTINFOPROVIDED
,
4281 "pktinfo structure provided, ifindex %u "
4282 "(set to 0)", pktinfo
->ipi6_ifindex
);
4285 * Set the pktinfo index to 0 here, to let the
4286 * kernel decide what interface it should send on.
4288 dev
->pktinfo
.ipi6_ifindex
= 0;
4292 if (sock
->type
== isc_sockettype_udp
)
4293 io_state
= doio_send(sock
, dev
);
4296 have_lock
= ISC_TRUE
;
4298 if (ISC_LIST_EMPTY(sock
->send_list
))
4299 io_state
= doio_send(sock
, dev
);
4301 io_state
= DOIO_SOFT
;
4307 * We couldn't send all or part of the request right now, so
4308 * queue it unless ISC_SOCKFLAG_NORETRY is set.
4310 if ((flags
& ISC_SOCKFLAG_NORETRY
) == 0) {
4311 isc_task_attach(task
, &ntask
);
4312 dev
->attributes
|= ISC_SOCKEVENTATTR_ATTACHED
;
4316 have_lock
= ISC_TRUE
;
4320 * Enqueue the request. If the socket was previously
4321 * not being watched, poke the watcher to start
4322 * paying attention to it.
4324 if (ISC_LIST_EMPTY(sock
->send_list
) &&
4325 !sock
->pending_send
)
4326 select_poke(sock
->manager
, sock
->fd
,
4328 ISC_LIST_ENQUEUE(sock
->send_list
, dev
, ev_link
);
4330 socket_log(sock
, NULL
, EVENT
, NULL
, 0, 0,
4331 "socket_send: event %p -> task %p",
4334 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) != 0)
4335 result
= ISC_R_INPROGRESS
;
4341 if ((flags
& ISC_SOCKFLAG_IMMEDIATE
) == 0)
4342 send_senddone_event(sock
, &dev
);
4347 UNLOCK(&sock
->lock
);
4353 isc_socket_send(isc_socket_t
*sock
, isc_region_t
*region
,
4354 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
)
4357 * REQUIRE() checking is performed in isc_socket_sendto().
4359 return (isc_socket_sendto(sock
, region
, task
, action
, arg
, NULL
,
4364 isc_socket_sendto(isc_socket_t
*sock
, isc_region_t
*region
,
4365 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
,
4366 isc_sockaddr_t
*address
, struct in6_pktinfo
*pktinfo
)
4368 isc_socketevent_t
*dev
;
4369 isc_socketmgr_t
*manager
;
4371 REQUIRE(VALID_SOCKET(sock
));
4372 REQUIRE(region
!= NULL
);
4373 REQUIRE(task
!= NULL
);
4374 REQUIRE(action
!= NULL
);
4376 manager
= sock
->manager
;
4377 REQUIRE(VALID_MANAGER(manager
));
4379 INSIST(sock
->bound
);
4381 dev
= allocate_socketevent(sock
, ISC_SOCKEVENT_SENDDONE
, action
, arg
);
4383 return (ISC_R_NOMEMORY
);
4386 dev
->region
= *region
;
4388 return (socket_send(sock
, dev
, task
, address
, pktinfo
, 0));
4392 isc_socket_sendv(isc_socket_t
*sock
, isc_bufferlist_t
*buflist
,
4393 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
)
4395 return (isc_socket_sendtov(sock
, buflist
, task
, action
, arg
, NULL
,
4400 isc_socket_sendtov(isc_socket_t
*sock
, isc_bufferlist_t
*buflist
,
4401 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
,
4402 isc_sockaddr_t
*address
, struct in6_pktinfo
*pktinfo
)
4404 isc_socketevent_t
*dev
;
4405 isc_socketmgr_t
*manager
;
4406 unsigned int iocount
;
4407 isc_buffer_t
*buffer
;
4409 REQUIRE(VALID_SOCKET(sock
));
4410 REQUIRE(buflist
!= NULL
);
4411 REQUIRE(!ISC_LIST_EMPTY(*buflist
));
4412 REQUIRE(task
!= NULL
);
4413 REQUIRE(action
!= NULL
);
4415 manager
= sock
->manager
;
4416 REQUIRE(VALID_MANAGER(manager
));
4418 iocount
= isc_bufferlist_usedcount(buflist
);
4419 REQUIRE(iocount
> 0);
4421 dev
= allocate_socketevent(sock
, ISC_SOCKEVENT_SENDDONE
, action
, arg
);
4423 return (ISC_R_NOMEMORY
);
4427 * Move each buffer from the passed in list to our internal one.
4429 buffer
= ISC_LIST_HEAD(*buflist
);
4430 while (buffer
!= NULL
) {
4431 ISC_LIST_DEQUEUE(*buflist
, buffer
, link
);
4432 ISC_LIST_ENQUEUE(dev
->bufferlist
, buffer
, link
);
4433 buffer
= ISC_LIST_HEAD(*buflist
);
4436 return (socket_send(sock
, dev
, task
, address
, pktinfo
, 0));
4440 isc_socket_sendto2(isc_socket_t
*sock
, isc_region_t
*region
,
4442 isc_sockaddr_t
*address
, struct in6_pktinfo
*pktinfo
,
4443 isc_socketevent_t
*event
, unsigned int flags
)
4445 REQUIRE((flags
& ~(ISC_SOCKFLAG_IMMEDIATE
|ISC_SOCKFLAG_NORETRY
)) == 0);
4446 if ((flags
& ISC_SOCKFLAG_NORETRY
) != 0)
4447 REQUIRE(sock
->type
== isc_sockettype_udp
);
4448 event
->ev_sender
= sock
;
4449 event
->result
= ISC_R_UNEXPECTED
;
4450 ISC_LIST_INIT(event
->bufferlist
);
4451 event
->region
= *region
;
4454 event
->attributes
= 0;
4456 return (socket_send(sock
, event
, task
, address
, pktinfo
, flags
));
4460 isc_socket_cleanunix(isc_sockaddr_t
*sockaddr
, isc_boolean_t active
) {
4461 #ifdef ISC_PLATFORM_HAVESYSUNH
4464 char strbuf
[ISC_STRERRORSIZE
];
4466 if (sockaddr
->type
.sa
.sa_family
!= AF_UNIX
)
4470 #if defined(S_IFMT) && defined(S_IFSOCK)
4471 #define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
4472 #elif defined(_S_IFMT) && defined(S_IFSOCK)
4473 #define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
4478 #if defined(S_IFMT) && defined(S_IFIFO)
4479 #define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
4480 #elif defined(_S_IFMT) && defined(S_IFIFO)
4481 #define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
4485 #if !defined(S_ISFIFO) && !defined(S_ISSOCK)
4486 #error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
4490 #define S_ISFIFO(mode) 0
4494 #define S_ISSOCK(mode) 0
4498 if (stat(sockaddr
->type
.sunix
.sun_path
, &sb
) < 0) {
4499 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4500 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4501 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
4502 "isc_socket_cleanunix: stat(%s): %s",
4503 sockaddr
->type
.sunix
.sun_path
, strbuf
);
4506 if (!(S_ISSOCK(sb
.st_mode
) || S_ISFIFO(sb
.st_mode
))) {
4507 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4508 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
4509 "isc_socket_cleanunix: %s: not a socket",
4510 sockaddr
->type
.sunix
.sun_path
);
4513 if (unlink(sockaddr
->type
.sunix
.sun_path
) < 0) {
4514 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4515 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4516 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
4517 "isc_socket_cleanunix: unlink(%s): %s",
4518 sockaddr
->type
.sunix
.sun_path
, strbuf
);
4523 s
= socket(AF_UNIX
, SOCK_STREAM
, 0);
4525 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4526 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4527 ISC_LOGMODULE_SOCKET
, ISC_LOG_WARNING
,
4528 "isc_socket_cleanunix: socket(%s): %s",
4529 sockaddr
->type
.sunix
.sun_path
, strbuf
);
4533 if (stat(sockaddr
->type
.sunix
.sun_path
, &sb
) < 0) {
4535 case ENOENT
: /* We exited cleanly last time */
4538 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4539 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4540 ISC_LOGMODULE_SOCKET
, ISC_LOG_WARNING
,
4541 "isc_socket_cleanunix: stat(%s): %s",
4542 sockaddr
->type
.sunix
.sun_path
, strbuf
);
4548 if (!(S_ISSOCK(sb
.st_mode
) || S_ISFIFO(sb
.st_mode
))) {
4549 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4550 ISC_LOGMODULE_SOCKET
, ISC_LOG_WARNING
,
4551 "isc_socket_cleanunix: %s: not a socket",
4552 sockaddr
->type
.sunix
.sun_path
);
4556 if (connect(s
, (struct sockaddr
*)&sockaddr
->type
.sunix
,
4557 sizeof(sockaddr
->type
.sunix
)) < 0) {
4561 if (unlink(sockaddr
->type
.sunix
.sun_path
) < 0) {
4562 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4563 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4564 ISC_LOGMODULE_SOCKET
,
4566 "isc_socket_cleanunix: "
4568 sockaddr
->type
.sunix
.sun_path
,
4573 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4574 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4575 ISC_LOGMODULE_SOCKET
, ISC_LOG_WARNING
,
4576 "isc_socket_cleanunix: connect(%s): %s",
4577 sockaddr
->type
.sunix
.sun_path
, strbuf
);
4590 isc_socket_permunix(isc_sockaddr_t
*sockaddr
, isc_uint32_t perm
,
4591 isc_uint32_t owner
, isc_uint32_t group
)
4593 #ifdef ISC_PLATFORM_HAVESYSUNH
4594 isc_result_t result
= ISC_R_SUCCESS
;
4595 char strbuf
[ISC_STRERRORSIZE
];
4596 char path
[sizeof(sockaddr
->type
.sunix
.sun_path
)];
4597 #ifdef NEED_SECURE_DIRECTORY
4601 REQUIRE(sockaddr
->type
.sa
.sa_family
== AF_UNIX
);
4602 INSIST(strlen(sockaddr
->type
.sunix
.sun_path
) < sizeof(path
));
4603 strcpy(path
, sockaddr
->type
.sunix
.sun_path
);
4605 #ifdef NEED_SECURE_DIRECTORY
4606 slash
= strrchr(path
, '/');
4607 if (slash
!= NULL
) {
4616 if (chmod(path
, perm
) < 0) {
4617 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4618 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4619 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
4620 "isc_socket_permunix: chmod(%s, %d): %s",
4621 path
, perm
, strbuf
);
4622 result
= ISC_R_FAILURE
;
4624 if (chown(path
, owner
, group
) < 0) {
4625 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4626 isc_log_write(isc_lctx
, ISC_LOGCATEGORY_GENERAL
,
4627 ISC_LOGMODULE_SOCKET
, ISC_LOG_ERROR
,
4628 "isc_socket_permunix: chown(%s, %d, %d): %s",
4631 result
= ISC_R_FAILURE
;
4639 return (ISC_R_NOTIMPLEMENTED
);
4644 isc_socket_bind(isc_socket_t
*sock
, isc_sockaddr_t
*sockaddr
,
4645 unsigned int options
) {
4646 char strbuf
[ISC_STRERRORSIZE
];
4651 INSIST(!sock
->bound
);
4653 if (sock
->pf
!= sockaddr
->type
.sa
.sa_family
) {
4654 UNLOCK(&sock
->lock
);
4655 return (ISC_R_FAMILYMISMATCH
);
4658 * Only set SO_REUSEADDR when we want a specific port.
4661 if (sock
->pf
== AF_UNIX
)
4664 if ((options
& ISC_SOCKET_REUSEADDRESS
) != 0 &&
4665 isc_sockaddr_getport(sockaddr
) != (in_port_t
)0 &&
4666 setsockopt(sock
->fd
, SOL_SOCKET
, SO_REUSEADDR
, (void *)&on
,
4668 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
4669 "setsockopt(%d) %s", sock
->fd
,
4670 isc_msgcat_get(isc_msgcat
, ISC_MSGSET_GENERAL
,
4671 ISC_MSG_FAILED
, "failed"));
4677 if (bind(sock
->fd
, &sockaddr
->type
.sa
, sockaddr
->length
) < 0) {
4678 inc_stats(sock
->manager
->stats
,
4679 sock
->statsindex
[STATID_BINDFAIL
]);
4681 UNLOCK(&sock
->lock
);
4684 return (ISC_R_NOPERM
);
4686 return (ISC_R_ADDRNOTAVAIL
);
4688 return (ISC_R_ADDRINUSE
);
4690 return (ISC_R_BOUND
);
4692 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4693 UNEXPECTED_ERROR(__FILE__
, __LINE__
, "bind: %s",
4695 return (ISC_R_UNEXPECTED
);
4699 socket_log(sock
, sockaddr
, TRACE
,
4700 isc_msgcat
, ISC_MSGSET_SOCKET
, ISC_MSG_BOUND
, "bound");
4703 UNLOCK(&sock
->lock
);
4704 return (ISC_R_SUCCESS
);
4708 isc_socket_filter(isc_socket_t
*sock
, const char *filter
) {
4709 #ifdef SO_ACCEPTFILTER
4710 char strbuf
[ISC_STRERRORSIZE
];
4711 struct accept_filter_arg afa
;
4717 REQUIRE(VALID_SOCKET(sock
));
4719 #ifdef SO_ACCEPTFILTER
4720 bzero(&afa
, sizeof(afa
));
4721 strncpy(afa
.af_name
, filter
, sizeof(afa
.af_name
));
4722 if (setsockopt(sock
->fd
, SOL_SOCKET
, SO_ACCEPTFILTER
,
4723 &afa
, sizeof(afa
)) == -1) {
4724 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4725 socket_log(sock
, NULL
, CREATION
, isc_msgcat
, ISC_MSGSET_SOCKET
,
4726 ISC_MSG_FILTER
, "setsockopt(SO_ACCEPTFILTER): %s",
4728 return (ISC_R_FAILURE
);
4730 return (ISC_R_SUCCESS
);
4732 return (ISC_R_NOTIMPLEMENTED
);
4737 * Set up to listen on a given socket. We do this by creating an internal
4738 * event that will be dispatched when the socket has read activity. The
4739 * watcher will send the internal event to the task when there is a new
4742 * Unlike in read, we don't preallocate a done event here. Every time there
4743 * is a new connection we'll have to allocate a new one anyway, so we might
4744 * as well keep things simple rather than having to track them.
4747 isc_socket_listen(isc_socket_t
*sock
, unsigned int backlog
) {
4748 char strbuf
[ISC_STRERRORSIZE
];
4750 REQUIRE(VALID_SOCKET(sock
));
4754 REQUIRE(!sock
->listener
);
4755 REQUIRE(sock
->bound
);
4756 REQUIRE(sock
->type
== isc_sockettype_tcp
||
4757 sock
->type
== isc_sockettype_unix
);
4760 backlog
= SOMAXCONN
;
4762 if (listen(sock
->fd
, (int)backlog
) < 0) {
4763 UNLOCK(&sock
->lock
);
4764 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4766 UNEXPECTED_ERROR(__FILE__
, __LINE__
, "listen: %s", strbuf
);
4768 return (ISC_R_UNEXPECTED
);
4773 UNLOCK(&sock
->lock
);
4774 return (ISC_R_SUCCESS
);
4778 * This should try to do aggressive accept() XXXMLG
4781 isc_socket_accept(isc_socket_t
*sock
,
4782 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
)
4784 isc_socket_newconnev_t
*dev
;
4785 isc_socketmgr_t
*manager
;
4786 isc_task_t
*ntask
= NULL
;
4787 isc_socket_t
*nsock
;
4788 isc_result_t result
;
4789 isc_boolean_t do_poke
= ISC_FALSE
;
4791 REQUIRE(VALID_SOCKET(sock
));
4792 manager
= sock
->manager
;
4793 REQUIRE(VALID_MANAGER(manager
));
4797 REQUIRE(sock
->listener
);
4800 * Sender field is overloaded here with the task we will be sending
4801 * this event to. Just before the actual event is delivered the
4802 * actual ev_sender will be touched up to be the socket.
4804 dev
= (isc_socket_newconnev_t
*)
4805 isc_event_allocate(manager
->mctx
, task
, ISC_SOCKEVENT_NEWCONN
,
4806 action
, arg
, sizeof(*dev
));
4808 UNLOCK(&sock
->lock
);
4809 return (ISC_R_NOMEMORY
);
4811 ISC_LINK_INIT(dev
, ev_link
);
4813 result
= allocate_socket(manager
, sock
->type
, &nsock
);
4814 if (result
!= ISC_R_SUCCESS
) {
4815 isc_event_free(ISC_EVENT_PTR(&dev
));
4816 UNLOCK(&sock
->lock
);
4821 * Attach to socket and to task.
4823 isc_task_attach(task
, &ntask
);
4824 nsock
->references
++;
4825 nsock
->statsindex
= sock
->statsindex
;
4827 dev
->ev_sender
= ntask
;
4828 dev
->newsocket
= nsock
;
4831 * Poke watcher here. We still have the socket locked, so there
4832 * is no race condition. We will keep the lock for such a short
4833 * bit of time waking it up now or later won't matter all that much.
4835 if (ISC_LIST_EMPTY(sock
->accept_list
))
4838 ISC_LIST_ENQUEUE(sock
->accept_list
, dev
, ev_link
);
4841 select_poke(manager
, sock
->fd
, SELECT_POKE_ACCEPT
);
4843 UNLOCK(&sock
->lock
);
4844 return (ISC_R_SUCCESS
);
4848 isc_socket_connect(isc_socket_t
*sock
, isc_sockaddr_t
*addr
,
4849 isc_task_t
*task
, isc_taskaction_t action
, const void *arg
)
4851 isc_socket_connev_t
*dev
;
4852 isc_task_t
*ntask
= NULL
;
4853 isc_socketmgr_t
*manager
;
4855 char strbuf
[ISC_STRERRORSIZE
];
4857 REQUIRE(VALID_SOCKET(sock
));
4858 REQUIRE(addr
!= NULL
);
4859 REQUIRE(task
!= NULL
);
4860 REQUIRE(action
!= NULL
);
4862 manager
= sock
->manager
;
4863 REQUIRE(VALID_MANAGER(manager
));
4864 REQUIRE(addr
!= NULL
);
4866 if (isc_sockaddr_ismulticast(addr
))
4867 return (ISC_R_MULTICAST
);
4871 REQUIRE(!sock
->connecting
);
4873 dev
= (isc_socket_connev_t
*)isc_event_allocate(manager
->mctx
, sock
,
4874 ISC_SOCKEVENT_CONNECT
,
4878 UNLOCK(&sock
->lock
);
4879 return (ISC_R_NOMEMORY
);
4881 ISC_LINK_INIT(dev
, ev_link
);
4884 * Try to do the connect right away, as there can be only one
4885 * outstanding, and it might happen to complete.
4887 sock
->peer_address
= *addr
;
4888 cc
= connect(sock
->fd
, &addr
->type
.sa
, addr
->length
);
4891 * HP-UX "fails" to connect a UDP socket and sets errno to
4892 * EINPROGRESS if it's non-blocking. We'd rather regard this as
4893 * a success and let the user detect it if it's really an error
4894 * at the time of sending a packet on the socket.
4896 if (sock
->type
== isc_sockettype_udp
&& errno
== EINPROGRESS
) {
4900 if (SOFT_ERROR(errno
) || errno
== EINPROGRESS
)
4904 #define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
4905 ERROR_MATCH(EACCES
, ISC_R_NOPERM
);
4906 ERROR_MATCH(EADDRNOTAVAIL
, ISC_R_ADDRNOTAVAIL
);
4907 ERROR_MATCH(EAFNOSUPPORT
, ISC_R_ADDRNOTAVAIL
);
4908 ERROR_MATCH(ECONNREFUSED
, ISC_R_CONNREFUSED
);
4909 ERROR_MATCH(EHOSTUNREACH
, ISC_R_HOSTUNREACH
);
4911 ERROR_MATCH(EHOSTDOWN
, ISC_R_HOSTUNREACH
);
4913 ERROR_MATCH(ENETUNREACH
, ISC_R_NETUNREACH
);
4914 ERROR_MATCH(ENOBUFS
, ISC_R_NORESOURCES
);
4915 ERROR_MATCH(EPERM
, ISC_R_HOSTUNREACH
);
4916 ERROR_MATCH(EPIPE
, ISC_R_NOTCONNECTED
);
4917 ERROR_MATCH(ECONNRESET
, ISC_R_CONNECTIONRESET
);
4921 sock
->connected
= 0;
4923 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
4924 UNEXPECTED_ERROR(__FILE__
, __LINE__
, "%d/%s", errno
, strbuf
);
4926 UNLOCK(&sock
->lock
);
4927 inc_stats(sock
->manager
->stats
,
4928 sock
->statsindex
[STATID_CONNECTFAIL
]);
4929 isc_event_free(ISC_EVENT_PTR(&dev
));
4930 return (ISC_R_UNEXPECTED
);
4933 sock
->connected
= 0;
4934 isc_task_send(task
, ISC_EVENT_PTR(&dev
));
4936 UNLOCK(&sock
->lock
);
4937 inc_stats(sock
->manager
->stats
,
4938 sock
->statsindex
[STATID_CONNECTFAIL
]);
4939 return (ISC_R_SUCCESS
);
4943 * If connect completed, fire off the done event.
4947 sock
->connected
= 1;
4949 dev
->result
= ISC_R_SUCCESS
;
4950 isc_task_send(task
, ISC_EVENT_PTR(&dev
));
4952 UNLOCK(&sock
->lock
);
4954 inc_stats(sock
->manager
->stats
,
4955 sock
->statsindex
[STATID_CONNECT
]);
4957 return (ISC_R_SUCCESS
);
4965 isc_task_attach(task
, &ntask
);
4967 sock
->connecting
= 1;
4969 dev
->ev_sender
= ntask
;
4972 * Poke watcher here. We still have the socket locked, so there
4973 * is no race condition. We will keep the lock for such a short
4974 * bit of time waking it up now or later won't matter all that much.
4976 if (sock
->connect_ev
== NULL
)
4977 select_poke(manager
, sock
->fd
, SELECT_POKE_CONNECT
);
4979 sock
->connect_ev
= dev
;
4981 UNLOCK(&sock
->lock
);
4982 return (ISC_R_SUCCESS
);
4986 * Called when a socket with a pending connect() finishes.
4989 internal_connect(isc_task_t
*me
, isc_event_t
*ev
) {
4991 isc_socket_connev_t
*dev
;
4994 ISC_SOCKADDR_LEN_T optlen
;
4995 char strbuf
[ISC_STRERRORSIZE
];
4996 char peerbuf
[ISC_SOCKADDR_FORMATSIZE
];
4999 INSIST(ev
->ev_type
== ISC_SOCKEVENT_INTW
);
5001 sock
= ev
->ev_sender
;
5002 INSIST(VALID_SOCKET(sock
));
5007 * When the internal event was sent the reference count was bumped
5008 * to keep the socket around for us. Decrement the count here.
5010 INSIST(sock
->references
> 0);
5012 if (sock
->references
== 0) {
5013 UNLOCK(&sock
->lock
);
5019 * Has this event been canceled?
5021 dev
= sock
->connect_ev
;
5023 INSIST(!sock
->connecting
);
5024 UNLOCK(&sock
->lock
);
5028 INSIST(sock
->connecting
);
5029 sock
->connecting
= 0;
5032 * Get any possible error status here.
5034 optlen
= sizeof(cc
);
5035 if (getsockopt(sock
->fd
, SOL_SOCKET
, SO_ERROR
,
5036 (void *)&cc
, (void *)&optlen
) < 0)
5043 * If the error is EAGAIN, just re-select on this
5044 * fd and pretend nothing strange happened.
5046 if (SOFT_ERROR(errno
) || errno
== EINPROGRESS
) {
5047 sock
->connecting
= 1;
5048 select_poke(sock
->manager
, sock
->fd
,
5049 SELECT_POKE_CONNECT
);
5050 UNLOCK(&sock
->lock
);
5055 inc_stats(sock
->manager
->stats
,
5056 sock
->statsindex
[STATID_CONNECTFAIL
]);
5059 * Translate other errors into ISC_R_* flavors.
5062 #define ERROR_MATCH(a, b) case a: dev->result = b; break;
5063 ERROR_MATCH(EACCES
, ISC_R_NOPERM
);
5064 ERROR_MATCH(EADDRNOTAVAIL
, ISC_R_ADDRNOTAVAIL
);
5065 ERROR_MATCH(EAFNOSUPPORT
, ISC_R_ADDRNOTAVAIL
);
5066 ERROR_MATCH(ECONNREFUSED
, ISC_R_CONNREFUSED
);
5067 ERROR_MATCH(EHOSTUNREACH
, ISC_R_HOSTUNREACH
);
5069 ERROR_MATCH(EHOSTDOWN
, ISC_R_HOSTUNREACH
);
5071 ERROR_MATCH(ENETUNREACH
, ISC_R_NETUNREACH
);
5072 ERROR_MATCH(ENOBUFS
, ISC_R_NORESOURCES
);
5073 ERROR_MATCH(EPERM
, ISC_R_HOSTUNREACH
);
5074 ERROR_MATCH(EPIPE
, ISC_R_NOTCONNECTED
);
5075 ERROR_MATCH(ETIMEDOUT
, ISC_R_TIMEDOUT
);
5076 ERROR_MATCH(ECONNRESET
, ISC_R_CONNECTIONRESET
);
5079 dev
->result
= ISC_R_UNEXPECTED
;
5080 isc_sockaddr_format(&sock
->peer_address
, peerbuf
,
5082 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
5083 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
5084 "internal_connect: connect(%s) %s",
5088 inc_stats(sock
->manager
->stats
,
5089 sock
->statsindex
[STATID_CONNECT
]);
5090 dev
->result
= ISC_R_SUCCESS
;
5091 sock
->connected
= 1;
5095 sock
->connect_ev
= NULL
;
5097 UNLOCK(&sock
->lock
);
5099 task
= dev
->ev_sender
;
5100 dev
->ev_sender
= sock
;
5101 isc_task_sendanddetach(&task
, ISC_EVENT_PTR(&dev
));
5105 isc_socket_getpeername(isc_socket_t
*sock
, isc_sockaddr_t
*addressp
) {
5106 isc_result_t result
;
5108 REQUIRE(VALID_SOCKET(sock
));
5109 REQUIRE(addressp
!= NULL
);
5113 if (sock
->connected
) {
5114 *addressp
= sock
->peer_address
;
5115 result
= ISC_R_SUCCESS
;
5117 result
= ISC_R_NOTCONNECTED
;
5120 UNLOCK(&sock
->lock
);
5126 isc_socket_getsockname(isc_socket_t
*sock
, isc_sockaddr_t
*addressp
) {
5127 ISC_SOCKADDR_LEN_T len
;
5128 isc_result_t result
;
5129 char strbuf
[ISC_STRERRORSIZE
];
5131 REQUIRE(VALID_SOCKET(sock
));
5132 REQUIRE(addressp
!= NULL
);
5137 result
= ISC_R_NOTBOUND
;
5141 result
= ISC_R_SUCCESS
;
5143 len
= sizeof(addressp
->type
);
5144 if (getsockname(sock
->fd
, &addressp
->type
.sa
, (void *)&len
) < 0) {
5145 isc__strerror(errno
, strbuf
, sizeof(strbuf
));
5146 UNEXPECTED_ERROR(__FILE__
, __LINE__
, "getsockname: %s",
5148 result
= ISC_R_UNEXPECTED
;
5151 addressp
->length
= (unsigned int)len
;
5154 UNLOCK(&sock
->lock
);
5160 * Run through the list of events on this socket, and cancel the ones
5161 * queued for task "task" of type "how". "how" is a bitmask.
5164 isc_socket_cancel(isc_socket_t
*sock
, isc_task_t
*task
, unsigned int how
) {
5166 REQUIRE(VALID_SOCKET(sock
));
5169 * Quick exit if there is nothing to do. Don't even bother locking
5178 * All of these do the same thing, more or less.
5180 * o If the internal event is marked as "posted" try to
5181 * remove it from the task's queue. If this fails, mark it
5182 * as canceled instead, and let the task clean it up later.
5183 * o For each I/O request for that task of that type, post
5184 * its done event with status of "ISC_R_CANCELED".
5185 * o Reset any state needed.
5187 if (((how
& ISC_SOCKCANCEL_RECV
) == ISC_SOCKCANCEL_RECV
)
5188 && !ISC_LIST_EMPTY(sock
->recv_list
)) {
5189 isc_socketevent_t
*dev
;
5190 isc_socketevent_t
*next
;
5191 isc_task_t
*current_task
;
5193 dev
= ISC_LIST_HEAD(sock
->recv_list
);
5195 while (dev
!= NULL
) {
5196 current_task
= dev
->ev_sender
;
5197 next
= ISC_LIST_NEXT(dev
, ev_link
);
5199 if ((task
== NULL
) || (task
== current_task
)) {
5200 dev
->result
= ISC_R_CANCELED
;
5201 send_recvdone_event(sock
, &dev
);
5207 if (((how
& ISC_SOCKCANCEL_SEND
) == ISC_SOCKCANCEL_SEND
)
5208 && !ISC_LIST_EMPTY(sock
->send_list
)) {
5209 isc_socketevent_t
*dev
;
5210 isc_socketevent_t
*next
;
5211 isc_task_t
*current_task
;
5213 dev
= ISC_LIST_HEAD(sock
->send_list
);
5215 while (dev
!= NULL
) {
5216 current_task
= dev
->ev_sender
;
5217 next
= ISC_LIST_NEXT(dev
, ev_link
);
5219 if ((task
== NULL
) || (task
== current_task
)) {
5220 dev
->result
= ISC_R_CANCELED
;
5221 send_senddone_event(sock
, &dev
);
5227 if (((how
& ISC_SOCKCANCEL_ACCEPT
) == ISC_SOCKCANCEL_ACCEPT
)
5228 && !ISC_LIST_EMPTY(sock
->accept_list
)) {
5229 isc_socket_newconnev_t
*dev
;
5230 isc_socket_newconnev_t
*next
;
5231 isc_task_t
*current_task
;
5233 dev
= ISC_LIST_HEAD(sock
->accept_list
);
5234 while (dev
!= NULL
) {
5235 current_task
= dev
->ev_sender
;
5236 next
= ISC_LIST_NEXT(dev
, ev_link
);
5238 if ((task
== NULL
) || (task
== current_task
)) {
5240 ISC_LIST_UNLINK(sock
->accept_list
, dev
,
5243 dev
->newsocket
->references
--;
5244 free_socket(&dev
->newsocket
);
5246 dev
->result
= ISC_R_CANCELED
;
5247 dev
->ev_sender
= sock
;
5248 isc_task_sendanddetach(¤t_task
,
5249 ISC_EVENT_PTR(&dev
));
5257 * Connecting is not a list.
5259 if (((how
& ISC_SOCKCANCEL_CONNECT
) == ISC_SOCKCANCEL_CONNECT
)
5260 && sock
->connect_ev
!= NULL
) {
5261 isc_socket_connev_t
*dev
;
5262 isc_task_t
*current_task
;
5264 INSIST(sock
->connecting
);
5265 sock
->connecting
= 0;
5267 dev
= sock
->connect_ev
;
5268 current_task
= dev
->ev_sender
;
5270 if ((task
== NULL
) || (task
== current_task
)) {
5271 sock
->connect_ev
= NULL
;
5273 dev
->result
= ISC_R_CANCELED
;
5274 dev
->ev_sender
= sock
;
5275 isc_task_sendanddetach(¤t_task
,
5276 ISC_EVENT_PTR(&dev
));
5280 UNLOCK(&sock
->lock
);
5284 isc_socket_gettype(isc_socket_t
*sock
) {
5285 REQUIRE(VALID_SOCKET(sock
));
5287 return (sock
->type
);
5291 isc_socket_isbound(isc_socket_t
*sock
) {
5295 val
= ((sock
->bound
) ? ISC_TRUE
: ISC_FALSE
);
5296 UNLOCK(&sock
->lock
);
5302 isc_socket_ipv6only(isc_socket_t
*sock
, isc_boolean_t yes
) {
5303 #if defined(IPV6_V6ONLY)
5304 int onoff
= yes
? 1 : 0;
5310 REQUIRE(VALID_SOCKET(sock
));
5313 if (sock
->pf
== AF_INET6
) {
5314 if (setsockopt(sock
->fd
, IPPROTO_IPV6
, IPV6_V6ONLY
,
5315 (void *)&onoff
, sizeof(int)) < 0) {
5316 char strbuf
[ISC_STRERRORSIZE
];
5318 UNEXPECTED_ERROR(__FILE__
, __LINE__
,
5319 "setsockopt(%d, IPV6_V6ONLY) "
5321 isc_msgcat_get(isc_msgcat
,
5328 FIX_IPV6_RECVPKTINFO(sock
); /* AIX */
5332 #ifndef ISC_PLATFORM_USETHREADS
5333 /* In our assumed scenario, we can simply use a single static object. */
5334 static isc_socketwait_t swait_private
;
5337 isc__socketmgr_waitevents(struct timeval
*tvp
, isc_socketwait_t
**swaitp
) {
5340 struct timespec ts
, *tsp
;
5349 REQUIRE(swaitp
!= NULL
&& *swaitp
== NULL
);
5351 if (socketmgr
== NULL
)
5356 ts
.tv_sec
= tvp
->tv_sec
;
5357 ts
.tv_nsec
= tvp
->tv_usec
* 1000;
5361 swait_private
.nevents
= kevent(socketmgr
->kqueue_fd
, NULL
, 0,
5362 socketmgr
->events
, socketmgr
->nevents
,
5364 n
= swait_private
.nevents
;
5365 #elif defined(USE_EPOLL)
5367 timeout
= tvp
->tv_sec
* 1000 + (tvp
->tv_usec
+ 999) / 1000;
5370 swait_private
.nevents
= epoll_wait(socketmgr
->epoll_fd
,
5372 socketmgr
->nevents
, timeout
);
5373 n
= swait_private
.nevents
;
5374 #elif defined(USE_DEVPOLL)
5375 dvp
.dp_fds
= socketmgr
->events
;
5376 dvp
.dp_nfds
= socketmgr
->nevents
;
5378 dvp
.dp_timeout
= tvp
->tv_sec
* 1000 +
5379 (tvp
->tv_usec
+ 999) / 1000;
5381 dvp
.dp_timeout
= -1;
5382 swait_private
.nevents
= ioctl(socketmgr
->devpoll_fd
, DP_POLL
, &dvp
);
5383 n
= swait_private
.nevents
;
5384 #elif defined(USE_SELECT)
5385 memcpy(socketmgr
->read_fds_copy
, socketmgr
->read_fds
,
5386 socketmgr
->fd_bufsize
);
5387 memcpy(socketmgr
->write_fds_copy
, socketmgr
->write_fds
,
5388 socketmgr
->fd_bufsize
);
5390 swait_private
.readset
= socketmgr
->read_fds_copy
;
5391 swait_private
.writeset
= socketmgr
->write_fds_copy
;
5392 swait_private
.maxfd
= socketmgr
->maxfd
+ 1;
5394 n
= select(swait_private
.maxfd
, swait_private
.readset
,
5395 swait_private
.writeset
, NULL
, tvp
);
5398 *swaitp
= &swait_private
;
5403 isc__socketmgr_dispatch(isc_socketwait_t
*swait
) {
5404 REQUIRE(swait
== &swait_private
);
5406 if (socketmgr
== NULL
)
5407 return (ISC_R_NOTFOUND
);
5409 #if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
5410 (void)process_fds(socketmgr
, socketmgr
->events
, swait
->nevents
);
5411 return (ISC_R_SUCCESS
);
5412 #elif defined(USE_SELECT)
5413 process_fds(socketmgr
, swait
->maxfd
, swait
->readset
, swait
->writeset
);
5414 return (ISC_R_SUCCESS
);
5417 #endif /* ISC_PLATFORM_USETHREADS */
5420 isc_socket_setname(isc_socket_t
*socket
, const char *name
, void *tag
) {
5426 REQUIRE(VALID_SOCKET(socket
));
5428 LOCK(&socket
->lock
);
5429 memset(socket
->name
, 0, sizeof(socket
->name
));
5430 strncpy(socket
->name
, name
, sizeof(socket
->name
) - 1);
5432 UNLOCK(&socket
->lock
);
5436 isc_socket_getname(isc_socket_t
*socket
) {
5437 return (socket
->name
);
5441 isc_socket_gettag(isc_socket_t
*socket
) {
5442 return (socket
->tag
);
5448 _socktype(isc_sockettype_t type
)
5450 if (type
== isc_sockettype_udp
)
5452 else if (type
== isc_sockettype_tcp
)
5454 else if (type
== isc_sockettype_unix
)
5456 else if (type
== isc_sockettype_fdwatch
)
5459 return ("not-initialized");
5463 isc_socketmgr_renderxml(isc_socketmgr_t
*mgr
, xmlTextWriterPtr writer
)
5466 char peerbuf
[ISC_SOCKADDR_FORMATSIZE
];
5467 isc_sockaddr_t addr
;
5468 ISC_SOCKADDR_LEN_T len
;
5472 #ifndef ISC_PLATFORM_USETHREADS
5473 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"references");
5474 xmlTextWriterWriteFormatString(writer
, "%d", mgr
->refs
);
5475 xmlTextWriterEndElement(writer
);
5478 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"sockets");
5479 sock
= ISC_LIST_HEAD(mgr
->socklist
);
5480 while (sock
!= NULL
) {
5482 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"socket");
5484 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"id");
5485 xmlTextWriterWriteFormatString(writer
, "%p", sock
);
5486 xmlTextWriterEndElement(writer
);
5488 if (sock
->name
[0] != 0) {
5489 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"name");
5490 xmlTextWriterWriteFormatString(writer
, "%s",
5492 xmlTextWriterEndElement(writer
); /* name */
5495 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"references");
5496 xmlTextWriterWriteFormatString(writer
, "%d", sock
->references
);
5497 xmlTextWriterEndElement(writer
);
5499 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"type",
5500 ISC_XMLCHAR
_socktype(sock
->type
));
5502 if (sock
->connected
) {
5503 isc_sockaddr_format(&sock
->peer_address
, peerbuf
,
5505 xmlTextWriterWriteElement(writer
,
5506 ISC_XMLCHAR
"peer-address",
5507 ISC_XMLCHAR peerbuf
);
5511 if (getsockname(sock
->fd
, &addr
.type
.sa
, (void *)&len
) == 0) {
5512 isc_sockaddr_format(&addr
, peerbuf
, sizeof(peerbuf
));
5513 xmlTextWriterWriteElement(writer
,
5514 ISC_XMLCHAR
"local-address",
5515 ISC_XMLCHAR peerbuf
);
5518 xmlTextWriterStartElement(writer
, ISC_XMLCHAR
"states");
5519 if (sock
->pending_recv
)
5520 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5521 ISC_XMLCHAR
"pending-receive");
5522 if (sock
->pending_send
)
5523 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5524 ISC_XMLCHAR
"pending-send");
5525 if (sock
->pending_accept
)
5526 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5527 ISC_XMLCHAR
"pending_accept");
5529 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5530 ISC_XMLCHAR
"listener");
5531 if (sock
->connected
)
5532 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5533 ISC_XMLCHAR
"connected");
5534 if (sock
->connecting
)
5535 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5536 ISC_XMLCHAR
"connecting");
5538 xmlTextWriterWriteElement(writer
, ISC_XMLCHAR
"state",
5539 ISC_XMLCHAR
"bound");
5541 xmlTextWriterEndElement(writer
); /* states */
5543 xmlTextWriterEndElement(writer
); /* socket */
5545 UNLOCK(&sock
->lock
);
5546 sock
= ISC_LIST_NEXT(sock
, link
);
5548 xmlTextWriterEndElement(writer
); /* sockets */
5552 #endif /* HAVE_LIBXML2 */