2 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: socket.c,v 1.237.18.29 2007/08/28 07:20:06 tbox Exp $ */
24 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #ifdef ISC_PLATFORM_HAVESYSUNH
41 #include <isc/buffer.h>
42 #include <isc/bufferlist.h>
43 #include <isc/condition.h>
44 #include <isc/formatcheck.h>
49 #include <isc/mutex.h>
52 #include <isc/platform.h>
53 #include <isc/print.h>
54 #include <isc/region.h>
55 #include <isc/socket.h>
56 #include <isc/strerror.h>
58 #include <isc/thread.h>
61 #include "errno2result.h"
63 #ifndef ISC_PLATFORM_USETHREADS
65 #endif /* ISC_PLATFORM_USETHREADS */
68 * Some systems define the socket length argument as an int, some as size_t,
69 * some as socklen_t. This is here so it can be easily changed if needed.
71 #ifndef ISC_SOCKADDR_LEN_T
72 #define ISC_SOCKADDR_LEN_T unsigned int
76 #if defined(SO_BSDCOMPAT) && defined(__linux__)
77 #include <sys/utsname.h>
81 * Define what the possible "soft" errors can be. These are non-fatal returns
82 * of various network related functions, like recv() and so on.
84 * For some reason, BSDI (and perhaps others) will sometimes return <0
85 * from recv() but will have errno==0. This is broken, but we have to
86 * work around it here.
88 #define SOFT_ERROR(e) ((e) == EAGAIN || \
89 (e) == EWOULDBLOCK || \
93 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
96 * DLVL(90) -- Function entry/exit and other tracing.
97 * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
98 * DLVL(60) -- Socket data send/receive
99 * DLVL(50) -- Event tracing, including receiving/sending completion events.
100 * DLVL(20) -- Socket creation/destruction.
102 #define TRACE_LEVEL 90
103 #define CORRECTNESS_LEVEL 70
104 #define IOEVENT_LEVEL 60
105 #define EVENT_LEVEL 50
106 #define CREATION_LEVEL 20
108 #define TRACE DLVL(TRACE_LEVEL)
109 #define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
110 #define IOEVENT DLVL(IOEVENT_LEVEL)
111 #define EVENT DLVL(EVENT_LEVEL)
112 #define CREATION DLVL(CREATION_LEVEL)
114 typedef isc_event_t intev_t;
116 #define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
117 #define VALID_SOCKET(t) ISC_MAGIC_VALID(t, SOCKET_MAGIC)
120 * IPv6 control information. If the socket is an IPv6 socket we want
121 * to collect the destination address and interface so the client can
122 * set them on outgoing packets.
124 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
131 * NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
132 * a setsockopt() like interface to request timestamps, and if the OS
133 * doesn't do it for us, call gettimeofday() on every UDP receive?
142 * The size to raise the recieve buffer to (from BIND 8).
144 #define RCVBUFSIZE (32*1024)
147 * The number of times a send operation is repeated if the result is EINTR.
154 isc_socketmgr_t *manager;
156 isc_sockettype_t type;
158 /* Locked by socket lock. */
159 ISC_LINK(isc_socket_t) link;
160 unsigned int references;
164 ISC_LIST(isc_socketevent_t) send_list;
165 ISC_LIST(isc_socketevent_t) recv_list;
166 ISC_LIST(isc_socket_newconnev_t) accept_list;
167 isc_socket_connev_t *connect_ev;
170 * Internal events. Posted when a descriptor is readable or
171 * writable. These are statically allocated and never freed.
172 * They will be set to non-purgable before use.
177 isc_sockaddr_t address; /* remote address */
179 unsigned int pending_recv : 1,
182 listener : 1, /* listener socket */
184 connecting : 1, /* connect pending */
185 bound : 1; /* bound to local addr */
187 #ifdef ISC_NET_RECVOVERFLOW
188 unsigned char overflow; /* used for MSG_TRUNC fake */
192 ISC_SOCKADDR_LEN_T recvcmsgbuflen;
194 ISC_SOCKADDR_LEN_T sendcmsgbuflen;
197 #define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
198 #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
200 struct isc_socketmgr {
205 /* Locked by manager lock. */
206 ISC_LIST(isc_socket_t) socklist;
209 isc_socket_t *fds[FD_SETSIZE];
210 int fdstate[FD_SETSIZE];
212 #ifdef ISC_PLATFORM_USETHREADS
213 isc_thread_t watcher;
214 isc_condition_t shutdown_ok;
216 #else /* ISC_PLATFORM_USETHREADS */
218 #endif /* ISC_PLATFORM_USETHREADS */
221 #ifndef ISC_PLATFORM_USETHREADS
222 static isc_socketmgr_t *socketmgr = NULL;
223 #endif /* ISC_PLATFORM_USETHREADS */
225 #define CLOSED 0 /* this one must be zero */
227 #define CLOSE_PENDING 2
230 * send() and recv() iovec counts
232 #define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
233 #ifdef ISC_NET_RECVOVERFLOW
234 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
236 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
239 static void send_recvdone_event(isc_socket_t *, isc_socketevent_t **);
240 static void send_senddone_event(isc_socket_t *, isc_socketevent_t **);
241 static void free_socket(isc_socket_t **);
242 static isc_result_t allocate_socket(isc_socketmgr_t *, isc_sockettype_t,
244 static void destroy(isc_socket_t **);
245 static void internal_accept(isc_task_t *, isc_event_t *);
246 static void internal_connect(isc_task_t *, isc_event_t *);
247 static void internal_recv(isc_task_t *, isc_event_t *);
248 static void internal_send(isc_task_t *, isc_event_t *);
249 static void process_cmsg(isc_socket_t *, struct msghdr *, isc_socketevent_t *);
250 static void build_msghdr_send(isc_socket_t *, isc_socketevent_t *,
251 struct msghdr *, struct iovec *, size_t *);
252 static void build_msghdr_recv(isc_socket_t *, isc_socketevent_t *,
253 struct msghdr *, struct iovec *, size_t *);
255 #define SELECT_POKE_SHUTDOWN (-1)
256 #define SELECT_POKE_NOTHING (-2)
257 #define SELECT_POKE_READ (-3)
258 #define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
259 #define SELECT_POKE_WRITE (-4)
260 #define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
261 #define SELECT_POKE_CLOSE (-5)
263 #define SOCK_DEAD(s) ((s)->references == 0)
266 manager_log(isc_socketmgr_t *sockmgr,
267 isc_logcategory_t *category, isc_logmodule_t *module, int level,
268 const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
270 manager_log(isc_socketmgr_t *sockmgr,
271 isc_logcategory_t *category, isc_logmodule_t *module, int level,
272 const char *fmt, ...)
277 if (! isc_log_wouldlog(isc_lctx, level))
281 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
284 isc_log_write(isc_lctx, category, module, level,
285 "sockmgr %p: %s", sockmgr, msgbuf);
289 socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
290 isc_logcategory_t *category, isc_logmodule_t *module, int level,
291 isc_msgcat_t *msgcat, int msgset, int message,
292 const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
294 socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
295 isc_logcategory_t *category, isc_logmodule_t *module, int level,
296 isc_msgcat_t *msgcat, int msgset, int message,
297 const char *fmt, ...)
300 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
303 if (! isc_log_wouldlog(isc_lctx, level))
307 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
310 if (address == NULL) {
311 isc_log_iwrite(isc_lctx, category, module, level,
312 msgcat, msgset, message,
313 "socket %p: %s", sock, msgbuf);
315 isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
316 isc_log_iwrite(isc_lctx, category, module, level,
317 msgcat, msgset, message,
318 "socket %p %s: %s", sock, peerbuf, msgbuf);
323 wakeup_socket(isc_socketmgr_t *manager, int fd, int msg) {
327 * This is a wakeup on a socket. If the socket is not in the
328 * process of being closed, start watching it for either reads
332 INSIST(fd >= 0 && fd < (int)FD_SETSIZE);
334 if (manager->fdstate[fd] == CLOSE_PENDING) {
335 manager->fdstate[fd] = CLOSED;
336 FD_CLR(fd, &manager->read_fds);
337 FD_CLR(fd, &manager->write_fds);
341 if (manager->fdstate[fd] != MANAGED)
344 sock = manager->fds[fd];
349 if (msg == SELECT_POKE_READ)
350 FD_SET(sock->fd, &manager->read_fds);
351 if (msg == SELECT_POKE_WRITE)
352 FD_SET(sock->fd, &manager->write_fds);
355 #ifdef ISC_PLATFORM_USETHREADS
357 * Poke the select loop when there is something for us to do.
358 * The write is required (by POSIX) to complete. That is, we
359 * will not get partial writes.
362 select_poke(isc_socketmgr_t *mgr, int fd, int msg) {
365 char strbuf[ISC_STRERRORSIZE];
371 cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
374 * Treat ENOSR as EAGAIN but loop slowly as it is
375 * unlikely to clear fast.
377 if (cc < 0 && errno == ENOSR) {
382 } while (cc < 0 && SOFT_ERROR(errno));
385 isc__strerror(errno, strbuf, sizeof(strbuf));
386 FATAL_ERROR(__FILE__, __LINE__,
387 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
390 "during watcher poke: %s"),
394 INSIST(cc == sizeof(buf));
398 * Read a message on the internal fd.
401 select_readmsg(isc_socketmgr_t *mgr, int *fd, int *msg) {
404 char strbuf[ISC_STRERRORSIZE];
406 cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
408 *msg = SELECT_POKE_NOTHING;
409 *fd = -1; /* Silence compiler. */
410 if (SOFT_ERROR(errno))
413 isc__strerror(errno, strbuf, sizeof(strbuf));
414 FATAL_ERROR(__FILE__, __LINE__,
415 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
418 "during watcher poke: %s"),
423 INSIST(cc == sizeof(buf));
428 #else /* ISC_PLATFORM_USETHREADS */
430 * Update the state of the socketmgr when something changes.
433 select_poke(isc_socketmgr_t *manager, int fd, int msg) {
434 if (msg == SELECT_POKE_SHUTDOWN)
437 wakeup_socket(manager, fd, msg);
440 #endif /* ISC_PLATFORM_USETHREADS */
443 * Make a fd non-blocking.
446 make_nonblock(int fd) {
449 char strbuf[ISC_STRERRORSIZE];
450 #ifdef USE_FIONBIO_IOCTL
453 ret = ioctl(fd, FIONBIO, (char *)&on);
455 flags = fcntl(fd, F_GETFL, 0);
456 flags |= PORT_NONBLOCK;
457 ret = fcntl(fd, F_SETFL, flags);
461 isc__strerror(errno, strbuf, sizeof(strbuf));
462 UNEXPECTED_ERROR(__FILE__, __LINE__,
463 #ifdef USE_FIONBIO_IOCTL
464 "ioctl(%d, FIONBIO, &on): %s", fd,
466 "fcntl(%d, F_SETFL, %d): %s", fd, flags,
470 return (ISC_R_UNEXPECTED);
473 return (ISC_R_SUCCESS);
478 * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
479 * In order to ensure as much portability as possible, we provide wrapper
480 * functions of these macros.
481 * Note that cmsg_space() could run slow on OSes that do not have
484 static inline ISC_SOCKADDR_LEN_T
485 cmsg_len(ISC_SOCKADDR_LEN_T len) {
487 return (CMSG_LEN(len));
489 ISC_SOCKADDR_LEN_T hdrlen;
492 * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
495 hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
496 return (hdrlen + len);
500 static inline ISC_SOCKADDR_LEN_T
501 cmsg_space(ISC_SOCKADDR_LEN_T len) {
503 return (CMSG_SPACE(len));
506 struct cmsghdr *cmsgp;
508 * XXX: The buffer length is an ad-hoc value, but should be enough
509 * in a practical sense.
511 char dummybuf[sizeof(struct cmsghdr) + 1024];
513 memset(&msg, 0, sizeof(msg));
514 msg.msg_control = dummybuf;
515 msg.msg_controllen = sizeof(dummybuf);
517 cmsgp = (struct cmsghdr *)dummybuf;
518 cmsgp->cmsg_len = cmsg_len(len);
520 cmsgp = CMSG_NXTHDR(&msg, cmsgp);
522 return ((char *)cmsgp - (char *)msg.msg_control);
527 #endif /* USE_CMSG */
530 * Process control messages received on a socket.
533 process_cmsg(isc_socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
535 struct cmsghdr *cmsgp;
536 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
537 struct in6_pktinfo *pktinfop;
540 struct timeval *timevalp;
545 * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
546 * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
547 * They are all here, outside of the CPP tests, because it is
548 * more consistent with the usual ISC coding style.
554 #ifdef ISC_NET_BSD44MSGHDR
557 if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
558 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
562 if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
563 dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
569 if (msg->msg_controllen == 0U || msg->msg_control == NULL)
575 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
579 cmsgp = CMSG_FIRSTHDR(msg);
580 while (cmsgp != NULL) {
581 socket_log(sock, NULL, TRACE,
582 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
583 "processing cmsg %p", cmsgp);
585 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
586 if (cmsgp->cmsg_level == IPPROTO_IPV6
587 && cmsgp->cmsg_type == IPV6_PKTINFO) {
589 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
590 memcpy(&dev->pktinfo, pktinfop,
591 sizeof(struct in6_pktinfo));
592 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
593 socket_log(sock, NULL, TRACE,
594 isc_msgcat, ISC_MSGSET_SOCKET,
596 "interface received on ifindex %u",
597 dev->pktinfo.ipi6_ifindex);
598 if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
599 dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
605 if (cmsgp->cmsg_level == SOL_SOCKET
606 && cmsgp->cmsg_type == SCM_TIMESTAMP) {
607 timevalp = (struct timeval *)CMSG_DATA(cmsgp);
608 dev->timestamp.seconds = timevalp->tv_sec;
609 dev->timestamp.nanoseconds = timevalp->tv_usec * 1000;
610 dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
616 cmsgp = CMSG_NXTHDR(msg, cmsgp);
618 #endif /* USE_CMSG */
620 #endif /* ISC_NET_BSD44MSGHDR */
624 * Construct an iov array and attach it to the msghdr passed in. This is
625 * the SEND constructor, which will use the used region of the buffer
626 * (if using a buffer list) or will use the internal region (if a single
627 * buffer I/O is requested).
629 * Nothing can be NULL, and the done event must list at least one buffer
630 * on the buffer linked list for this function to be meaningful.
632 * If write_countp != NULL, *write_countp will hold the number of bytes
633 * this transaction can send.
636 build_msghdr_send(isc_socket_t *sock, isc_socketevent_t *dev,
637 struct msghdr *msg, struct iovec *iov, size_t *write_countp)
639 unsigned int iovcount;
640 isc_buffer_t *buffer;
645 memset(msg, 0, sizeof(*msg));
647 if (sock->type == isc_sockettype_udp) {
648 msg->msg_name = (void *)&dev->address.type.sa;
649 msg->msg_namelen = dev->address.length;
651 msg->msg_name = NULL;
652 msg->msg_namelen = 0;
655 buffer = ISC_LIST_HEAD(dev->bufferlist);
660 * Single buffer I/O? Skip what we've done so far in this region.
662 if (buffer == NULL) {
663 write_count = dev->region.length - dev->n;
664 iov[0].iov_base = (void *)(dev->region.base + dev->n);
665 iov[0].iov_len = write_count;
673 * Skip the data in the buffer list that we have already written.
676 while (buffer != NULL) {
677 REQUIRE(ISC_BUFFER_VALID(buffer));
678 if (skip_count < isc_buffer_usedlength(buffer))
680 skip_count -= isc_buffer_usedlength(buffer);
681 buffer = ISC_LIST_NEXT(buffer, link);
684 while (buffer != NULL) {
685 INSIST(iovcount < MAXSCATTERGATHER_SEND);
687 isc_buffer_usedregion(buffer, &used);
689 if (used.length > 0) {
690 iov[iovcount].iov_base = (void *)(used.base
692 iov[iovcount].iov_len = used.length - skip_count;
693 write_count += (used.length - skip_count);
697 buffer = ISC_LIST_NEXT(buffer, link);
700 INSIST(skip_count == 0U);
704 msg->msg_iovlen = iovcount;
706 #ifdef ISC_NET_BSD44MSGHDR
707 msg->msg_control = NULL;
708 msg->msg_controllen = 0;
710 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
711 if ((sock->type == isc_sockettype_udp)
712 && ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
713 struct cmsghdr *cmsgp;
714 struct in6_pktinfo *pktinfop;
716 socket_log(sock, NULL, TRACE,
717 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
718 "sendto pktinfo data, ifindex %u",
719 dev->pktinfo.ipi6_ifindex);
721 msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
722 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
723 msg->msg_control = (void *)sock->sendcmsgbuf;
725 cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
726 cmsgp->cmsg_level = IPPROTO_IPV6;
727 cmsgp->cmsg_type = IPV6_PKTINFO;
728 cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
729 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
730 memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
732 #endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
733 #else /* ISC_NET_BSD44MSGHDR */
734 msg->msg_accrights = NULL;
735 msg->msg_accrightslen = 0;
736 #endif /* ISC_NET_BSD44MSGHDR */
738 if (write_countp != NULL)
739 *write_countp = write_count;
743 * Construct an iov array and attach it to the msghdr passed in. This is
744 * the RECV constructor, which will use the avialable region of the buffer
745 * (if using a buffer list) or will use the internal region (if a single
746 * buffer I/O is requested).
748 * Nothing can be NULL, and the done event must list at least one buffer
749 * on the buffer linked list for this function to be meaningful.
751 * If read_countp != NULL, *read_countp will hold the number of bytes
752 * this transaction can receive.
755 build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev,
756 struct msghdr *msg, struct iovec *iov, size_t *read_countp)
758 unsigned int iovcount;
759 isc_buffer_t *buffer;
760 isc_region_t available;
763 memset(msg, 0, sizeof(struct msghdr));
765 if (sock->type == isc_sockettype_udp) {
766 memset(&dev->address, 0, sizeof(dev->address));
767 #ifdef BROKEN_RECVMSG
768 if (sock->pf == AF_INET) {
769 msg->msg_name = (void *)&dev->address.type.sin;
770 msg->msg_namelen = sizeof(dev->address.type.sin6);
771 } else if (sock->pf == AF_INET6) {
772 msg->msg_name = (void *)&dev->address.type.sin6;
773 msg->msg_namelen = sizeof(dev->address.type.sin6);
774 #ifdef ISC_PLATFORM_HAVESYSUNH
775 } else if (sock->pf == AF_UNIX) {
776 msg->msg_name = (void *)&dev->address.type.sunix;
777 msg->msg_namelen = sizeof(dev->address.type.sunix);
780 msg->msg_name = (void *)&dev->address.type.sa;
781 msg->msg_namelen = sizeof(dev->address.type);
784 msg->msg_name = (void *)&dev->address.type.sa;
785 msg->msg_namelen = sizeof(dev->address.type);
787 #ifdef ISC_NET_RECVOVERFLOW
788 /* If needed, steal one iovec for overflow detection. */
792 msg->msg_name = NULL;
793 msg->msg_namelen = 0;
794 dev->address = sock->address;
797 buffer = ISC_LIST_HEAD(dev->bufferlist);
801 * Single buffer I/O? Skip what we've done so far in this region.
803 if (buffer == NULL) {
804 read_count = dev->region.length - dev->n;
805 iov[0].iov_base = (void *)(dev->region.base + dev->n);
806 iov[0].iov_len = read_count;
814 * Skip empty buffers.
816 while (buffer != NULL) {
817 REQUIRE(ISC_BUFFER_VALID(buffer));
818 if (isc_buffer_availablelength(buffer) != 0)
820 buffer = ISC_LIST_NEXT(buffer, link);
824 while (buffer != NULL) {
825 INSIST(iovcount < MAXSCATTERGATHER_RECV);
827 isc_buffer_availableregion(buffer, &available);
829 if (available.length > 0) {
830 iov[iovcount].iov_base = (void *)(available.base);
831 iov[iovcount].iov_len = available.length;
832 read_count += available.length;
835 buffer = ISC_LIST_NEXT(buffer, link);
841 * If needed, set up to receive that one extra byte. Note that
842 * we know there is at least one iov left, since we stole it
843 * at the top of this function.
845 #ifdef ISC_NET_RECVOVERFLOW
846 if (sock->type == isc_sockettype_udp) {
847 iov[iovcount].iov_base = (void *)(&sock->overflow);
848 iov[iovcount].iov_len = 1;
854 msg->msg_iovlen = iovcount;
856 #ifdef ISC_NET_BSD44MSGHDR
857 msg->msg_control = NULL;
858 msg->msg_controllen = 0;
860 #if defined(USE_CMSG)
861 if (sock->type == isc_sockettype_udp) {
862 msg->msg_control = sock->recvcmsgbuf;
863 msg->msg_controllen = sock->recvcmsgbuflen;
865 #endif /* USE_CMSG */
866 #else /* ISC_NET_BSD44MSGHDR */
867 msg->msg_accrights = NULL;
868 msg->msg_accrightslen = 0;
869 #endif /* ISC_NET_BSD44MSGHDR */
871 if (read_countp != NULL)
872 *read_countp = read_count;
876 set_dev_address(isc_sockaddr_t *address, isc_socket_t *sock,
877 isc_socketevent_t *dev)
879 if (sock->type == isc_sockettype_udp) {
881 dev->address = *address;
883 dev->address = sock->address;
884 } else if (sock->type == isc_sockettype_tcp) {
885 INSIST(address == NULL);
886 dev->address = sock->address;
891 destroy_socketevent(isc_event_t *event) {
892 isc_socketevent_t *ev = (isc_socketevent_t *)event;
894 INSIST(ISC_LIST_EMPTY(ev->bufferlist));
896 (ev->destroy)(event);
899 static isc_socketevent_t *
900 allocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype,
901 isc_taskaction_t action, const void *arg)
903 isc_socketevent_t *ev;
905 ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
913 ev->result = ISC_R_UNEXPECTED;
914 ISC_LINK_INIT(ev, ev_link);
915 ISC_LIST_INIT(ev->bufferlist);
916 ev->region.base = NULL;
920 ev->destroy = ev->ev_destroy;
921 ev->ev_destroy = destroy_socketevent;
926 #if defined(ISC_SOCKET_DEBUG)
928 dump_msg(struct msghdr *msg) {
931 printf("MSGHDR %p\n", msg);
932 printf("\tname %p, namelen %d\n", msg->msg_name, msg->msg_namelen);
933 printf("\tiov %p, iovlen %d\n", msg->msg_iov, msg->msg_iovlen);
934 for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
935 printf("\t\t%d\tbase %p, len %d\n", i,
936 msg->msg_iov[i].iov_base,
937 msg->msg_iov[i].iov_len);
938 #ifdef ISC_NET_BSD44MSGHDR
939 printf("\tcontrol %p, controllen %d\n", msg->msg_control,
940 msg->msg_controllen);
945 #define DOIO_SUCCESS 0 /* i/o ok, event sent */
946 #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
947 #define DOIO_HARD 2 /* i/o error, event sent */
948 #define DOIO_EOF 3 /* EOF, no event sent */
951 doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
953 struct iovec iov[MAXSCATTERGATHER_RECV];
956 struct msghdr msghdr;
957 isc_buffer_t *buffer;
959 char strbuf[ISC_STRERRORSIZE];
961 build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
963 #if defined(ISC_SOCKET_DEBUG)
967 cc = recvmsg(sock->fd, &msghdr, 0);
970 #if defined(ISC_SOCKET_DEBUG)
975 if (SOFT_ERROR(recv_errno))
978 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
979 isc__strerror(recv_errno, strbuf, sizeof(strbuf));
980 socket_log(sock, NULL, IOEVENT,
981 isc_msgcat, ISC_MSGSET_SOCKET,
983 "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
984 sock->fd, cc, recv_errno, strbuf);
987 #define SOFT_OR_HARD(_system, _isc) \
988 if (recv_errno == _system) { \
989 if (sock->connected) { \
990 dev->result = _isc; \
991 return (DOIO_HARD); \
993 return (DOIO_SOFT); \
995 #define ALWAYS_HARD(_system, _isc) \
996 if (recv_errno == _system) { \
997 dev->result = _isc; \
998 return (DOIO_HARD); \
1001 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1002 SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1003 SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1004 SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
1005 /* HPUX 11.11 can return EADDRNOTAVAIL. */
1006 SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1007 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1012 dev->result = isc__errno2result(recv_errno);
1017 * On TCP, zero length reads indicate EOF, while on
1018 * UDP, zero length reads are perfectly valid, although
1021 if ((sock->type == isc_sockettype_tcp) && (cc == 0))
1024 if (sock->type == isc_sockettype_udp) {
1025 dev->address.length = msghdr.msg_namelen;
1026 if (isc_sockaddr_getport(&dev->address) == 0) {
1027 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1028 socket_log(sock, &dev->address, IOEVENT,
1029 isc_msgcat, ISC_MSGSET_SOCKET,
1031 "dropping source port zero packet");
1037 socket_log(sock, &dev->address, IOEVENT,
1038 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
1039 "packet received correctly");
1042 * Overflow bit detection. If we received MORE bytes than we should,
1043 * this indicates an overflow situation. Set the flag in the
1044 * dev entry and adjust how much we read by one.
1046 #ifdef ISC_NET_RECVOVERFLOW
1047 if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
1048 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
1054 * If there are control messages attached, run through them and pull
1055 * out the interesting bits.
1057 if (sock->type == isc_sockettype_udp)
1058 process_cmsg(sock, &msghdr, dev);
1061 * update the buffers (if any) and the i/o count
1065 buffer = ISC_LIST_HEAD(dev->bufferlist);
1066 while (buffer != NULL && actual_count > 0U) {
1067 REQUIRE(ISC_BUFFER_VALID(buffer));
1068 if (isc_buffer_availablelength(buffer) <= actual_count) {
1069 actual_count -= isc_buffer_availablelength(buffer);
1070 isc_buffer_add(buffer,
1071 isc_buffer_availablelength(buffer));
1073 isc_buffer_add(buffer, actual_count);
1077 buffer = ISC_LIST_NEXT(buffer, link);
1078 if (buffer == NULL) {
1079 INSIST(actual_count == 0U);
1084 * If we read less than we expected, update counters,
1085 * and let the upper layer poke the descriptor.
1087 if (((size_t)cc != read_count) && (dev->n < dev->minimum))
1091 * Full reads are posted, or partials if partials are ok.
1093 dev->result = ISC_R_SUCCESS;
1094 return (DOIO_SUCCESS);
1099 * DOIO_SUCCESS The operation succeeded. dev->result contains
1102 * DOIO_HARD A hard or unexpected I/O error was encountered.
1103 * dev->result contains the appropriate error.
1105 * DOIO_SOFT A soft I/O error was encountered. No senddone
1106 * event was sent. The operation should be retried.
1108 * No other return values are possible.
1111 doio_send(isc_socket_t *sock, isc_socketevent_t *dev) {
1113 struct iovec iov[MAXSCATTERGATHER_SEND];
1115 struct msghdr msghdr;
1116 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1119 char strbuf[ISC_STRERRORSIZE];
1121 build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
1124 cc = sendmsg(sock->fd, &msghdr, 0);
1128 * Check for error or block condition.
1131 if (send_errno == EINTR && ++attempts < NRETRIES)
1134 if (SOFT_ERROR(send_errno))
1137 #define SOFT_OR_HARD(_system, _isc) \
1138 if (send_errno == _system) { \
1139 if (sock->connected) { \
1140 dev->result = _isc; \
1141 return (DOIO_HARD); \
1143 return (DOIO_SOFT); \
1145 #define ALWAYS_HARD(_system, _isc) \
1146 if (send_errno == _system) { \
1147 dev->result = _isc; \
1148 return (DOIO_HARD); \
1151 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1152 ALWAYS_HARD(EACCES, ISC_R_NOPERM);
1153 ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
1154 ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1155 ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1157 ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
1159 ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1160 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1161 ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
1162 ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
1163 ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
1169 * The other error types depend on whether or not the
1170 * socket is UDP or TCP. If it is UDP, some errors
1171 * that we expect to be fatal under TCP are merely
1172 * annoying, and are really soft errors.
1174 * However, these soft errors are still returned as
1177 isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
1178 isc__strerror(send_errno, strbuf, sizeof(strbuf));
1179 UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
1181 dev->result = isc__errno2result(send_errno);
1186 UNEXPECTED_ERROR(__FILE__, __LINE__,
1187 "internal_send: send() %s 0",
1188 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1189 ISC_MSG_RETURNED, "returned"));
1192 * If we write less than we expected, update counters, poke.
1195 if ((size_t)cc != write_count)
1199 * Exactly what we wanted to write. We're done with this
1200 * entry. Post its completion event.
1202 dev->result = ISC_R_SUCCESS;
1203 return (DOIO_SUCCESS);
1209 * Caller must ensure that the socket is not locked and no external
1213 destroy(isc_socket_t **sockp) {
1214 isc_socket_t *sock = *sockp;
1215 isc_socketmgr_t *manager = sock->manager;
1217 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1218 ISC_MSG_DESTROYING, "destroying");
1220 INSIST(ISC_LIST_EMPTY(sock->accept_list));
1221 INSIST(ISC_LIST_EMPTY(sock->recv_list));
1222 INSIST(ISC_LIST_EMPTY(sock->send_list));
1223 INSIST(sock->connect_ev == NULL);
1224 REQUIRE(sock->fd >= 0 && sock->fd < (int)FD_SETSIZE);
1226 LOCK(&manager->lock);
1229 * No one has this socket open, so the watcher doesn't have to be
1230 * poked, and the socket doesn't have to be locked.
1232 manager->fds[sock->fd] = NULL;
1233 manager->fdstate[sock->fd] = CLOSE_PENDING;
1234 select_poke(manager, sock->fd, SELECT_POKE_CLOSE);
1235 ISC_LIST_UNLINK(manager->socklist, sock, link);
1237 #ifdef ISC_PLATFORM_USETHREADS
1238 if (ISC_LIST_EMPTY(manager->socklist))
1239 SIGNAL(&manager->shutdown_ok);
1240 #endif /* ISC_PLATFORM_USETHREADS */
1243 * XXX should reset manager->maxfd here
1246 UNLOCK(&manager->lock);
1252 allocate_socket(isc_socketmgr_t *manager, isc_sockettype_t type,
1253 isc_socket_t **socketp)
1256 isc_result_t result;
1257 ISC_SOCKADDR_LEN_T cmsgbuflen;
1259 sock = isc_mem_get(manager->mctx, sizeof(*sock));
1262 return (ISC_R_NOMEMORY);
1264 result = ISC_R_UNEXPECTED;
1267 sock->references = 0;
1269 sock->manager = manager;
1273 ISC_LINK_INIT(sock, link);
1275 sock->recvcmsgbuf = NULL;
1276 sock->sendcmsgbuf = NULL;
1279 * set up cmsg buffers
1282 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1283 cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
1285 #if defined(USE_CMSG) && defined(SO_TIMESTAMP)
1286 cmsgbuflen += cmsg_space(sizeof(struct timeval));
1288 sock->recvcmsgbuflen = cmsgbuflen;
1289 if (sock->recvcmsgbuflen != 0U) {
1290 sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
1291 if (sock->recvcmsgbuf == NULL)
1296 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1297 cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
1299 sock->sendcmsgbuflen = cmsgbuflen;
1300 if (sock->sendcmsgbuflen != 0U) {
1301 sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
1302 if (sock->sendcmsgbuf == NULL)
1307 * set up list of readers and writers to be initially empty
1309 ISC_LIST_INIT(sock->recv_list);
1310 ISC_LIST_INIT(sock->send_list);
1311 ISC_LIST_INIT(sock->accept_list);
1312 sock->connect_ev = NULL;
1313 sock->pending_recv = 0;
1314 sock->pending_send = 0;
1315 sock->pending_accept = 0;
1317 sock->connected = 0;
1318 sock->connecting = 0;
1322 * initialize the lock
1324 result = isc_mutex_init(&sock->lock);
1325 if (result != ISC_R_SUCCESS) {
1331 * Initialize readable and writable events
1333 ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
1334 ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTR,
1335 NULL, sock, sock, NULL, NULL);
1336 ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
1337 ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTW,
1338 NULL, sock, sock, NULL, NULL);
1340 sock->magic = SOCKET_MAGIC;
1343 return (ISC_R_SUCCESS);
1346 if (sock->recvcmsgbuf != NULL)
1347 isc_mem_put(manager->mctx, sock->recvcmsgbuf,
1348 sock->recvcmsgbuflen);
1349 if (sock->sendcmsgbuf != NULL)
1350 isc_mem_put(manager->mctx, sock->sendcmsgbuf,
1351 sock->sendcmsgbuflen);
1352 isc_mem_put(manager->mctx, sock, sizeof(*sock));
1358 * This event requires that the various lists be empty, that the reference
1359 * count be 1, and that the magic number is valid. The other socket bits,
1360 * like the lock, must be initialized as well. The fd associated must be
1361 * marked as closed, by setting it to -1 on close, or this routine will
1362 * also close the socket.
1365 free_socket(isc_socket_t **socketp) {
1366 isc_socket_t *sock = *socketp;
1368 INSIST(sock->references == 0);
1369 INSIST(VALID_SOCKET(sock));
1370 INSIST(!sock->connecting);
1371 INSIST(!sock->pending_recv);
1372 INSIST(!sock->pending_send);
1373 INSIST(!sock->pending_accept);
1374 INSIST(ISC_LIST_EMPTY(sock->recv_list));
1375 INSIST(ISC_LIST_EMPTY(sock->send_list));
1376 INSIST(ISC_LIST_EMPTY(sock->accept_list));
1377 INSIST(!ISC_LINK_LINKED(sock, link));
1379 if (sock->recvcmsgbuf != NULL)
1380 isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
1381 sock->recvcmsgbuflen);
1382 if (sock->sendcmsgbuf != NULL)
1383 isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
1384 sock->sendcmsgbuflen);
1388 DESTROYLOCK(&sock->lock);
1390 isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
1397 * This really should not be necessary to do. Having to workout
1398 * which kernel version we are on at run time so that we don't cause
1399 * the kernel to issue a warning about us using a deprecated socket option.
1400 * Such warnings should *never* be on by default in production kernels.
1402 * We can't do this a build time because executables are moved between
1403 * machines and hence kernels.
1405 * We can't just not set SO_BSDCOMAT because some kernels require it.
1408 static isc_once_t bsdcompat_once = ISC_ONCE_INIT;
1409 isc_boolean_t bsdcompat = ISC_TRUE;
1412 clear_bsdcompat(void) {
1419 uname(&buf); /* Can only fail if buf is bad in Linux. */
1421 /* Paranoia in parsing can be increased, but we trust uname(). */
1422 major = strtol(buf.release, &endp, 10);
1424 minor = strtol(endp+1, &endp, 10);
1425 if ((major > 2) || ((major == 2) && (minor >= 4))) {
1426 bsdcompat = ISC_FALSE;
1429 #endif /* __linux __ */
1434 * Create a new 'type' socket managed by 'manager'. Events
1435 * will be posted to 'task' and when dispatched 'action' will be
1436 * called with 'arg' as the arg value. The new socket is returned
1440 isc_socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
1441 isc_socket_t **socketp)
1443 isc_socket_t *sock = NULL;
1444 isc_result_t result;
1445 #if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
1448 #if defined(SO_RCVBUF)
1449 ISC_SOCKADDR_LEN_T optlen;
1452 char strbuf[ISC_STRERRORSIZE];
1453 const char *err = "socket";
1456 REQUIRE(VALID_MANAGER(manager));
1457 REQUIRE(socketp != NULL && *socketp == NULL);
1459 result = allocate_socket(manager, type, &sock);
1460 if (result != ISC_R_SUCCESS)
1466 case isc_sockettype_udp:
1467 sock->fd = socket(pf, SOCK_DGRAM, IPPROTO_UDP);
1469 case isc_sockettype_tcp:
1470 sock->fd = socket(pf, SOCK_STREAM, IPPROTO_TCP);
1472 case isc_sockettype_unix:
1473 sock->fd = socket(pf, SOCK_STREAM, 0);
1476 if (sock->fd == -1 && errno == EINTR && try++ < 42)
1481 * Leave a space for stdio to work in.
1483 if (sock->fd >= 0 && sock->fd < 20) {
1485 new = fcntl(sock->fd, F_DUPFD, 20);
1487 (void)close(sock->fd);
1490 err = "isc_socket_create: fcntl";
1494 if (sock->fd >= (int)FD_SETSIZE) {
1495 (void)close(sock->fd);
1496 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
1497 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
1498 isc_msgcat, ISC_MSGSET_SOCKET,
1500 "%s: too many open file descriptors", "socket");
1502 return (ISC_R_NORESOURCES);
1512 return (ISC_R_NORESOURCES);
1514 case EPROTONOSUPPORT:
1518 * Linux 2.2 (and maybe others) return EINVAL instead of
1522 return (ISC_R_FAMILYNOSUPPORT);
1525 isc__strerror(errno, strbuf, sizeof(strbuf));
1526 UNEXPECTED_ERROR(__FILE__, __LINE__,
1528 isc_msgcat_get(isc_msgcat,
1533 return (ISC_R_UNEXPECTED);
1537 if (make_nonblock(sock->fd) != ISC_R_SUCCESS) {
1538 (void)close(sock->fd);
1540 return (ISC_R_UNEXPECTED);
1544 RUNTIME_CHECK(isc_once_do(&bsdcompat_once,
1545 clear_bsdcompat) == ISC_R_SUCCESS);
1546 if (type != isc_sockettype_unix && bsdcompat &&
1547 setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
1548 (void *)&on, sizeof(on)) < 0) {
1549 isc__strerror(errno, strbuf, sizeof(strbuf));
1550 UNEXPECTED_ERROR(__FILE__, __LINE__,
1551 "setsockopt(%d, SO_BSDCOMPAT) %s: %s",
1553 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1554 ISC_MSG_FAILED, "failed"),
1560 #if defined(USE_CMSG) || defined(SO_RCVBUF)
1561 if (type == isc_sockettype_udp) {
1563 #if defined(USE_CMSG)
1564 #if defined(SO_TIMESTAMP)
1565 if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
1566 (void *)&on, sizeof(on)) < 0
1567 && errno != ENOPROTOOPT) {
1568 isc__strerror(errno, strbuf, sizeof(strbuf));
1569 UNEXPECTED_ERROR(__FILE__, __LINE__,
1570 "setsockopt(%d, SO_TIMESTAMP) %s: %s",
1572 isc_msgcat_get(isc_msgcat,
1579 #endif /* SO_TIMESTAMP */
1581 #if defined(ISC_PLATFORM_HAVEIPV6)
1582 if (pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
1584 * Warn explicitly because this anomaly can be hidden
1585 * in usual operation (and unexpectedly appear later).
1587 UNEXPECTED_ERROR(__FILE__, __LINE__,
1588 "No buffer available to receive "
1589 "IPv6 destination");
1591 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1592 #ifdef IPV6_RECVPKTINFO
1594 if ((pf == AF_INET6)
1595 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1596 (void *)&on, sizeof(on)) < 0)) {
1597 isc__strerror(errno, strbuf, sizeof(strbuf));
1598 UNEXPECTED_ERROR(__FILE__, __LINE__,
1599 "setsockopt(%d, IPV6_RECVPKTINFO) "
1601 isc_msgcat_get(isc_msgcat,
1609 if ((pf == AF_INET6)
1610 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
1611 (void *)&on, sizeof(on)) < 0)) {
1612 isc__strerror(errno, strbuf, sizeof(strbuf));
1613 UNEXPECTED_ERROR(__FILE__, __LINE__,
1614 "setsockopt(%d, IPV6_PKTINFO) %s: %s",
1616 isc_msgcat_get(isc_msgcat,
1622 #endif /* IPV6_RECVPKTINFO */
1623 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
1624 #ifdef IPV6_USE_MIN_MTU /* RFC 3542, not too common yet*/
1625 /* use minimum MTU */
1626 if (pf == AF_INET6) {
1627 (void)setsockopt(sock->fd, IPPROTO_IPV6,
1629 (void *)&on, sizeof(on));
1632 #endif /* ISC_PLATFORM_HAVEIPV6 */
1633 #endif /* defined(USE_CMSG) */
1635 #if defined(SO_RCVBUF)
1636 optlen = sizeof(size);
1637 if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
1638 (void *)&size, &optlen) >= 0 &&
1639 size < RCVBUFSIZE) {
1641 if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
1642 (void *)&size, sizeof(size)) == -1) {
1643 isc__strerror(errno, strbuf, sizeof(strbuf));
1644 UNEXPECTED_ERROR(__FILE__, __LINE__,
1645 "setsockopt(%d, SO_RCVBUF, %d) %s: %s",
1647 isc_msgcat_get(isc_msgcat,
1656 #endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
1658 sock->references = 1;
1661 LOCK(&manager->lock);
1664 * Note we don't have to lock the socket like we normally would because
1665 * there are no external references to it yet.
1668 manager->fds[sock->fd] = sock;
1669 manager->fdstate[sock->fd] = MANAGED;
1670 ISC_LIST_APPEND(manager->socklist, sock, link);
1671 if (manager->maxfd < sock->fd)
1672 manager->maxfd = sock->fd;
1674 UNLOCK(&manager->lock);
1676 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1677 ISC_MSG_CREATED, "created");
1679 return (ISC_R_SUCCESS);
1683 * Attach to a socket. Caller must explicitly detach when it is done.
1686 isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
1687 REQUIRE(VALID_SOCKET(sock));
1688 REQUIRE(socketp != NULL && *socketp == NULL);
1692 UNLOCK(&sock->lock);
1698 * Dereference a socket. If this is the last reference to it, clean things
1699 * up by destroying the socket.
1702 isc_socket_detach(isc_socket_t **socketp) {
1704 isc_boolean_t kill_socket = ISC_FALSE;
1706 REQUIRE(socketp != NULL);
1708 REQUIRE(VALID_SOCKET(sock));
1711 REQUIRE(sock->references > 0);
1713 if (sock->references == 0)
1714 kill_socket = ISC_TRUE;
1715 UNLOCK(&sock->lock);
1724 * I/O is possible on a given socket. Schedule an event to this task that
1725 * will call an internal function to do the I/O. This will charge the
1726 * task with the I/O operation and let our select loop handler get back
1727 * to doing something real as fast as possible.
1729 * The socket and manager must be locked before calling this function.
1732 dispatch_recv(isc_socket_t *sock) {
1734 isc_socketevent_t *ev;
1736 INSIST(!sock->pending_recv);
1738 ev = ISC_LIST_HEAD(sock->recv_list);
1742 sock->pending_recv = 1;
1743 iev = &sock->readable_ev;
1745 socket_log(sock, NULL, EVENT, NULL, 0, 0,
1746 "dispatch_recv: event %p -> task %p", ev, ev->ev_sender);
1749 iev->ev_sender = sock;
1750 iev->ev_action = internal_recv;
1753 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1757 dispatch_send(isc_socket_t *sock) {
1759 isc_socketevent_t *ev;
1761 INSIST(!sock->pending_send);
1763 ev = ISC_LIST_HEAD(sock->send_list);
1767 sock->pending_send = 1;
1768 iev = &sock->writable_ev;
1770 socket_log(sock, NULL, EVENT, NULL, 0, 0,
1771 "dispatch_send: event %p -> task %p", ev, ev->ev_sender);
1774 iev->ev_sender = sock;
1775 iev->ev_action = internal_send;
1778 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1782 * Dispatch an internal accept event.
1785 dispatch_accept(isc_socket_t *sock) {
1787 isc_socket_newconnev_t *ev;
1789 INSIST(!sock->pending_accept);
1792 * Are there any done events left, or were they all canceled
1793 * before the manager got the socket lock?
1795 ev = ISC_LIST_HEAD(sock->accept_list);
1799 sock->pending_accept = 1;
1800 iev = &sock->readable_ev;
1802 sock->references++; /* keep socket around for this internal event */
1803 iev->ev_sender = sock;
1804 iev->ev_action = internal_accept;
1807 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1811 dispatch_connect(isc_socket_t *sock) {
1813 isc_socket_connev_t *ev;
1815 iev = &sock->writable_ev;
1817 ev = sock->connect_ev;
1818 INSIST(ev != NULL); /* XXX */
1820 INSIST(sock->connecting);
1822 sock->references++; /* keep socket around for this internal event */
1823 iev->ev_sender = sock;
1824 iev->ev_action = internal_connect;
1827 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1831 * Dequeue an item off the given socket's read queue, set the result code
1832 * in the done event to the one provided, and send it to the task it was
1835 * If the event to be sent is on a list, remove it before sending. If
1836 * asked to, send and detach from the socket as well.
1838 * Caller must have the socket locked if the event is attached to the socket.
1841 send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
1844 task = (*dev)->ev_sender;
1846 (*dev)->ev_sender = sock;
1848 if (ISC_LINK_LINKED(*dev, ev_link))
1849 ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
1851 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
1852 == ISC_SOCKEVENTATTR_ATTACHED)
1853 isc_task_sendanddetach(&task, (isc_event_t **)dev);
1855 isc_task_send(task, (isc_event_t **)dev);
1859 * See comments for send_recvdone_event() above.
1861 * Caller must have the socket locked if the event is attached to the socket.
1864 send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
1867 INSIST(dev != NULL && *dev != NULL);
1869 task = (*dev)->ev_sender;
1870 (*dev)->ev_sender = sock;
1872 if (ISC_LINK_LINKED(*dev, ev_link))
1873 ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
1875 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
1876 == ISC_SOCKEVENTATTR_ATTACHED)
1877 isc_task_sendanddetach(&task, (isc_event_t **)dev);
1879 isc_task_send(task, (isc_event_t **)dev);
1883 * Call accept() on a socket, to get the new file descriptor. The listen
1884 * socket is used as a prototype to create a new isc_socket_t. The new
1885 * socket has one outstanding reference. The task receiving the event
1886 * will be detached from just after the event is delivered.
1888 * On entry to this function, the event delivered is the internal
1889 * readable event, and the first item on the accept_list should be
1890 * the done event we want to send. If the list is empty, this is a no-op,
1891 * so just unlock and return.
1894 internal_accept(isc_task_t *me, isc_event_t *ev) {
1896 isc_socketmgr_t *manager;
1897 isc_socket_newconnev_t *dev;
1899 ISC_SOCKADDR_LEN_T addrlen;
1901 isc_result_t result = ISC_R_SUCCESS;
1902 char strbuf[ISC_STRERRORSIZE];
1903 const char *err = "accept";
1907 sock = ev->ev_sender;
1908 INSIST(VALID_SOCKET(sock));
1911 socket_log(sock, NULL, TRACE,
1912 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
1913 "internal_accept called, locked socket");
1915 manager = sock->manager;
1916 INSIST(VALID_MANAGER(manager));
1918 INSIST(sock->listener);
1919 INSIST(sock->pending_accept == 1);
1920 sock->pending_accept = 0;
1922 INSIST(sock->references > 0);
1923 sock->references--; /* the internal event is done with this socket */
1924 if (sock->references == 0) {
1925 UNLOCK(&sock->lock);
1931 * Get the first item off the accept list.
1932 * If it is empty, unlock the socket and return.
1934 dev = ISC_LIST_HEAD(sock->accept_list);
1936 UNLOCK(&sock->lock);
1941 * Try to accept the new connection. If the accept fails with
1942 * EAGAIN or EINTR, simply poke the watcher to watch this socket
1943 * again. Also ignore ECONNRESET, which has been reported to
1944 * be spuriously returned on Linux 2.2.19 although it is not
1945 * a documented error for accept(). ECONNABORTED has been
1946 * reported for Solaris 8. The rest are thrown in not because
1947 * we have seen them but because they are ignored by other
1948 * deamons such as BIND 8 and Apache.
1951 addrlen = sizeof(dev->newsocket->address.type);
1952 memset(&dev->newsocket->address.type.sa, 0, addrlen);
1953 fd = accept(sock->fd, &dev->newsocket->address.type.sa,
1958 * Leave a space for stdio to work in.
1960 if (fd >= 0 && fd < 20) {
1962 new = fcntl(fd, F_DUPFD, 20);
1972 if (SOFT_ERROR(errno))
1995 isc__strerror(errno, strbuf, sizeof(strbuf));
1996 UNEXPECTED_ERROR(__FILE__, __LINE__,
1997 "internal_accept: %s() %s: %s", err,
1998 isc_msgcat_get(isc_msgcat,
2004 result = ISC_R_UNEXPECTED;
2006 if (addrlen == 0U) {
2007 UNEXPECTED_ERROR(__FILE__, __LINE__,
2008 "internal_accept(): "
2009 "accept() failed to return "
2014 } else if (dev->newsocket->address.type.sa.sa_family !=
2017 UNEXPECTED_ERROR(__FILE__, __LINE__,
2018 "internal_accept(): "
2019 "accept() returned peer address "
2020 "family %u (expected %u)",
2021 dev->newsocket->address.
2026 } else if (fd >= (int)FD_SETSIZE) {
2027 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2028 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2029 isc_msgcat, ISC_MSGSET_SOCKET,
2031 "%s: too many open file descriptors",
2039 dev->newsocket->address.length = addrlen;
2040 dev->newsocket->pf = sock->pf;
2044 * Pull off the done event.
2046 ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
2049 * Poke watcher if there are more pending accepts.
2051 if (!ISC_LIST_EMPTY(sock->accept_list))
2052 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
2054 UNLOCK(&sock->lock);
2056 if (fd != -1 && (make_nonblock(fd) != ISC_R_SUCCESS)) {
2059 result = ISC_R_UNEXPECTED;
2063 * -1 means the new socket didn't happen.
2066 LOCK(&manager->lock);
2067 ISC_LIST_APPEND(manager->socklist, dev->newsocket, link);
2069 dev->newsocket->fd = fd;
2070 dev->newsocket->bound = 1;
2071 dev->newsocket->connected = 1;
2074 * Save away the remote address
2076 dev->address = dev->newsocket->address;
2078 manager->fds[fd] = dev->newsocket;
2079 manager->fdstate[fd] = MANAGED;
2080 if (manager->maxfd < fd)
2081 manager->maxfd = fd;
2083 socket_log(sock, &dev->newsocket->address, CREATION,
2084 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
2085 "accepted connection, new socket %p",
2088 UNLOCK(&manager->lock);
2090 dev->newsocket->references--;
2091 free_socket(&dev->newsocket);
2095 * Fill in the done event details and send it off.
2097 dev->result = result;
2098 task = dev->ev_sender;
2099 dev->ev_sender = sock;
2101 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
2105 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
2106 UNLOCK(&sock->lock);
2111 internal_recv(isc_task_t *me, isc_event_t *ev) {
2112 isc_socketevent_t *dev;
2115 INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
2117 sock = ev->ev_sender;
2118 INSIST(VALID_SOCKET(sock));
2121 socket_log(sock, NULL, IOEVENT,
2122 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
2123 "internal_recv: task %p got event %p", me, ev);
2125 INSIST(sock->pending_recv == 1);
2126 sock->pending_recv = 0;
2128 INSIST(sock->references > 0);
2129 sock->references--; /* the internal event is done with this socket */
2130 if (sock->references == 0) {
2131 UNLOCK(&sock->lock);
2137 * Try to do as much I/O as possible on this socket. There are no
2138 * limits here, currently.
2140 dev = ISC_LIST_HEAD(sock->recv_list);
2141 while (dev != NULL) {
2142 switch (doio_recv(sock, dev)) {
2148 * read of 0 means the remote end was closed.
2149 * Run through the event queue and dispatch all
2150 * the events with an EOF result code.
2153 dev->result = ISC_R_EOF;
2154 send_recvdone_event(sock, &dev);
2155 dev = ISC_LIST_HEAD(sock->recv_list);
2156 } while (dev != NULL);
2161 send_recvdone_event(sock, &dev);
2165 dev = ISC_LIST_HEAD(sock->recv_list);
2169 if (!ISC_LIST_EMPTY(sock->recv_list))
2170 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2172 UNLOCK(&sock->lock);
2176 internal_send(isc_task_t *me, isc_event_t *ev) {
2177 isc_socketevent_t *dev;
2180 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
2183 * Find out what socket this is and lock it.
2185 sock = (isc_socket_t *)ev->ev_sender;
2186 INSIST(VALID_SOCKET(sock));
2189 socket_log(sock, NULL, IOEVENT,
2190 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
2191 "internal_send: task %p got event %p", me, ev);
2193 INSIST(sock->pending_send == 1);
2194 sock->pending_send = 0;
2196 INSIST(sock->references > 0);
2197 sock->references--; /* the internal event is done with this socket */
2198 if (sock->references == 0) {
2199 UNLOCK(&sock->lock);
2205 * Try to do as much I/O as possible on this socket. There are no
2206 * limits here, currently.
2208 dev = ISC_LIST_HEAD(sock->send_list);
2209 while (dev != NULL) {
2210 switch (doio_send(sock, dev)) {
2216 send_senddone_event(sock, &dev);
2220 dev = ISC_LIST_HEAD(sock->send_list);
2224 if (!ISC_LIST_EMPTY(sock->send_list))
2225 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
2227 UNLOCK(&sock->lock);
2231 process_fds(isc_socketmgr_t *manager, int maxfd,
2232 fd_set *readfds, fd_set *writefds)
2236 isc_boolean_t unlock_sock;
2238 REQUIRE(maxfd <= (int)FD_SETSIZE);
2241 * Process read/writes on other fds here. Avoid locking
2242 * and unlocking twice if both reads and writes are possible.
2244 for (i = 0; i < maxfd; i++) {
2245 #ifdef ISC_PLATFORM_USETHREADS
2246 if (i == manager->pipe_fds[0] || i == manager->pipe_fds[1])
2248 #endif /* ISC_PLATFORM_USETHREADS */
2250 if (manager->fdstate[i] == CLOSE_PENDING) {
2251 manager->fdstate[i] = CLOSED;
2252 FD_CLR(i, &manager->read_fds);
2253 FD_CLR(i, &manager->write_fds);
2260 sock = manager->fds[i];
2261 unlock_sock = ISC_FALSE;
2262 if (FD_ISSET(i, readfds)) {
2264 FD_CLR(i, &manager->read_fds);
2267 unlock_sock = ISC_TRUE;
2269 if (!SOCK_DEAD(sock)) {
2271 dispatch_accept(sock);
2273 dispatch_recv(sock);
2275 FD_CLR(i, &manager->read_fds);
2278 if (FD_ISSET(i, writefds)) {
2280 FD_CLR(i, &manager->write_fds);
2284 unlock_sock = ISC_TRUE;
2287 if (!SOCK_DEAD(sock)) {
2288 if (sock->connecting)
2289 dispatch_connect(sock);
2291 dispatch_send(sock);
2293 FD_CLR(i, &manager->write_fds);
2296 UNLOCK(&sock->lock);
2300 #ifdef ISC_PLATFORM_USETHREADS
2302 * This is the thread that will loop forever, always in a select or poll
2305 * When select returns something to do, track down what thread gets to do
2306 * this I/O and post the event to it.
2308 static isc_threadresult_t
2309 watcher(void *uap) {
2310 isc_socketmgr_t *manager = uap;
2318 char strbuf[ISC_STRERRORSIZE];
2321 * Get the control fd here. This will never change.
2323 LOCK(&manager->lock);
2324 ctlfd = manager->pipe_fds[0];
2329 readfds = manager->read_fds;
2330 writefds = manager->write_fds;
2331 maxfd = manager->maxfd + 1;
2333 UNLOCK(&manager->lock);
2335 cc = select(maxfd, &readfds, &writefds, NULL, NULL);
2337 if (!SOFT_ERROR(errno)) {
2338 isc__strerror(errno, strbuf,
2340 FATAL_ERROR(__FILE__, __LINE__,
2342 isc_msgcat_get(isc_msgcat,
2350 LOCK(&manager->lock);
2355 * Process reads on internal, control fd.
2357 if (FD_ISSET(ctlfd, &readfds)) {
2359 select_readmsg(manager, &fd, &msg);
2361 manager_log(manager, IOEVENT,
2362 isc_msgcat_get(isc_msgcat,
2365 "watcher got message %d"),
2371 if (msg == SELECT_POKE_NOTHING)
2375 * Handle shutdown message. We really should
2376 * jump out of this loop right away, but
2377 * it doesn't matter if we have to do a little
2380 if (msg == SELECT_POKE_SHUTDOWN) {
2387 * This is a wakeup on a socket. Look
2388 * at the event queue for both read and write,
2389 * and decide if we need to watch on it now
2392 wakeup_socket(manager, fd, msg);
2396 process_fds(manager, maxfd, &readfds, &writefds);
2399 manager_log(manager, TRACE,
2400 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2401 ISC_MSG_EXITING, "watcher exiting"));
2403 UNLOCK(&manager->lock);
2404 return ((isc_threadresult_t)0);
2406 #endif /* ISC_PLATFORM_USETHREADS */
2409 * Create a new socket manager.
2412 isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
2413 isc_socketmgr_t *manager;
2414 #ifdef ISC_PLATFORM_USETHREADS
2415 char strbuf[ISC_STRERRORSIZE];
2417 isc_result_t result;
2419 REQUIRE(managerp != NULL && *managerp == NULL);
2421 #ifndef ISC_PLATFORM_USETHREADS
2422 if (socketmgr != NULL) {
2424 *managerp = socketmgr;
2425 return (ISC_R_SUCCESS);
2427 #endif /* ISC_PLATFORM_USETHREADS */
2429 manager = isc_mem_get(mctx, sizeof(*manager));
2430 if (manager == NULL)
2431 return (ISC_R_NOMEMORY);
2433 manager->magic = SOCKET_MANAGER_MAGIC;
2434 manager->mctx = NULL;
2435 memset(manager->fds, 0, sizeof(manager->fds));
2436 ISC_LIST_INIT(manager->socklist);
2437 result = isc_mutex_init(&manager->lock);
2438 if (result != ISC_R_SUCCESS) {
2439 isc_mem_put(mctx, manager, sizeof(*manager));
2442 #ifdef ISC_PLATFORM_USETHREADS
2443 if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
2444 DESTROYLOCK(&manager->lock);
2445 isc_mem_put(mctx, manager, sizeof(*manager));
2446 UNEXPECTED_ERROR(__FILE__, __LINE__,
2447 "isc_condition_init() %s",
2448 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2449 ISC_MSG_FAILED, "failed"));
2450 return (ISC_R_UNEXPECTED);
2454 * Create the special fds that will be used to wake up the
2455 * select/poll loop when something internal needs to be done.
2457 if (pipe(manager->pipe_fds) != 0) {
2458 DESTROYLOCK(&manager->lock);
2459 isc_mem_put(mctx, manager, sizeof(*manager));
2460 isc__strerror(errno, strbuf, sizeof(strbuf));
2461 UNEXPECTED_ERROR(__FILE__, __LINE__,
2463 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2464 ISC_MSG_FAILED, "failed"),
2467 return (ISC_R_UNEXPECTED);
2470 RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
2472 RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
2474 #else /* ISC_PLATFORM_USETHREADS */
2476 #endif /* ISC_PLATFORM_USETHREADS */
2479 * Set up initial state for the select loop
2481 FD_ZERO(&manager->read_fds);
2482 FD_ZERO(&manager->write_fds);
2483 #ifdef ISC_PLATFORM_USETHREADS
2484 FD_SET(manager->pipe_fds[0], &manager->read_fds);
2485 manager->maxfd = manager->pipe_fds[0];
2486 #else /* ISC_PLATFORM_USETHREADS */
2488 #endif /* ISC_PLATFORM_USETHREADS */
2489 memset(manager->fdstate, 0, sizeof(manager->fdstate));
2491 #ifdef ISC_PLATFORM_USETHREADS
2493 * Start up the select/poll thread.
2495 if (isc_thread_create(watcher, manager, &manager->watcher) !=
2497 (void)close(manager->pipe_fds[0]);
2498 (void)close(manager->pipe_fds[1]);
2499 DESTROYLOCK(&manager->lock);
2500 isc_mem_put(mctx, manager, sizeof(*manager));
2501 UNEXPECTED_ERROR(__FILE__, __LINE__,
2502 "isc_thread_create() %s",
2503 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2504 ISC_MSG_FAILED, "failed"));
2505 return (ISC_R_UNEXPECTED);
2507 #endif /* ISC_PLATFORM_USETHREADS */
2508 isc_mem_attach(mctx, &manager->mctx);
2510 #ifndef ISC_PLATFORM_USETHREADS
2511 socketmgr = manager;
2512 #endif /* ISC_PLATFORM_USETHREADS */
2513 *managerp = manager;
2515 return (ISC_R_SUCCESS);
2519 isc_socketmgr_destroy(isc_socketmgr_t **managerp) {
2520 isc_socketmgr_t *manager;
2525 * Destroy a socket manager.
2528 REQUIRE(managerp != NULL);
2529 manager = *managerp;
2530 REQUIRE(VALID_MANAGER(manager));
2532 #ifndef ISC_PLATFORM_USETHREADS
2533 if (manager->refs > 1) {
2538 #endif /* ISC_PLATFORM_USETHREADS */
2540 LOCK(&manager->lock);
2542 #ifdef ISC_PLATFORM_USETHREADS
2544 * Wait for all sockets to be destroyed.
2546 while (!ISC_LIST_EMPTY(manager->socklist)) {
2547 manager_log(manager, CREATION,
2548 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
2549 ISC_MSG_SOCKETSREMAIN,
2551 WAIT(&manager->shutdown_ok, &manager->lock);
2553 #else /* ISC_PLATFORM_USETHREADS */
2555 * Hope all sockets have been destroyed.
2557 if (!ISC_LIST_EMPTY(manager->socklist)) {
2558 manager_log(manager, CREATION,
2559 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
2560 ISC_MSG_SOCKETSREMAIN,
2564 #endif /* ISC_PLATFORM_USETHREADS */
2566 UNLOCK(&manager->lock);
2569 * Here, poke our select/poll thread. Do this by closing the write
2570 * half of the pipe, which will send EOF to the read half.
2571 * This is currently a no-op in the non-threaded case.
2573 select_poke(manager, 0, SELECT_POKE_SHUTDOWN);
2575 #ifdef ISC_PLATFORM_USETHREADS
2577 * Wait for thread to exit.
2579 if (isc_thread_join(manager->watcher, NULL) != ISC_R_SUCCESS)
2580 UNEXPECTED_ERROR(__FILE__, __LINE__,
2581 "isc_thread_join() %s",
2582 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2583 ISC_MSG_FAILED, "failed"));
2584 #endif /* ISC_PLATFORM_USETHREADS */
2589 #ifdef ISC_PLATFORM_USETHREADS
2590 (void)close(manager->pipe_fds[0]);
2591 (void)close(manager->pipe_fds[1]);
2592 (void)isc_condition_destroy(&manager->shutdown_ok);
2593 #endif /* ISC_PLATFORM_USETHREADS */
2595 for (i = 0; i < (int)FD_SETSIZE; i++)
2596 if (manager->fdstate[i] == CLOSE_PENDING)
2599 DESTROYLOCK(&manager->lock);
2601 mctx= manager->mctx;
2602 isc_mem_put(mctx, manager, sizeof(*manager));
2604 isc_mem_detach(&mctx);
2610 socket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
2614 isc_boolean_t have_lock = ISC_FALSE;
2615 isc_task_t *ntask = NULL;
2616 isc_result_t result = ISC_R_SUCCESS;
2618 dev->ev_sender = task;
2620 if (sock->type == isc_sockettype_udp) {
2621 io_state = doio_recv(sock, dev);
2624 have_lock = ISC_TRUE;
2626 if (ISC_LIST_EMPTY(sock->recv_list))
2627 io_state = doio_recv(sock, dev);
2629 io_state = DOIO_SOFT;
2635 * We couldn't read all or part of the request right now, so
2638 * Attach to socket and to task
2640 isc_task_attach(task, &ntask);
2641 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
2645 have_lock = ISC_TRUE;
2649 * Enqueue the request. If the socket was previously not being
2650 * watched, poke the watcher to start paying attention to it.
2652 if (ISC_LIST_EMPTY(sock->recv_list))
2653 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2654 ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
2656 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2657 "socket_recv: event %p -> task %p",
2660 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
2661 result = ISC_R_INPROGRESS;
2665 dev->result = ISC_R_EOF;
2670 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
2671 send_recvdone_event(sock, &dev);
2676 UNLOCK(&sock->lock);
2682 isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
2683 unsigned int minimum, isc_task_t *task,
2684 isc_taskaction_t action, const void *arg)
2686 isc_socketevent_t *dev;
2687 isc_socketmgr_t *manager;
2688 unsigned int iocount;
2689 isc_buffer_t *buffer;
2691 REQUIRE(VALID_SOCKET(sock));
2692 REQUIRE(buflist != NULL);
2693 REQUIRE(!ISC_LIST_EMPTY(*buflist));
2694 REQUIRE(task != NULL);
2695 REQUIRE(action != NULL);
2697 manager = sock->manager;
2698 REQUIRE(VALID_MANAGER(manager));
2700 iocount = isc_bufferlist_availablecount(buflist);
2701 REQUIRE(iocount > 0);
2703 INSIST(sock->bound);
2705 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
2707 return (ISC_R_NOMEMORY);
2711 * UDP sockets are always partial read
2713 if (sock->type == isc_sockettype_udp)
2717 dev->minimum = iocount;
2719 dev->minimum = minimum;
2723 * Move each buffer from the passed in list to our internal one.
2725 buffer = ISC_LIST_HEAD(*buflist);
2726 while (buffer != NULL) {
2727 ISC_LIST_DEQUEUE(*buflist, buffer, link);
2728 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
2729 buffer = ISC_LIST_HEAD(*buflist);
2732 return (socket_recv(sock, dev, task, 0));
2736 isc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
2737 isc_task_t *task, isc_taskaction_t action, const void *arg)
2739 isc_socketevent_t *dev;
2740 isc_socketmgr_t *manager;
2742 REQUIRE(VALID_SOCKET(sock));
2743 REQUIRE(action != NULL);
2745 manager = sock->manager;
2746 REQUIRE(VALID_MANAGER(manager));
2748 INSIST(sock->bound);
2750 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
2752 return (ISC_R_NOMEMORY);
2754 return (isc_socket_recv2(sock, region, minimum, task, dev, 0));
2758 isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
2759 unsigned int minimum, isc_task_t *task,
2760 isc_socketevent_t *event, unsigned int flags)
2762 event->ev_sender = sock;
2763 event->result = ISC_R_UNEXPECTED;
2764 ISC_LIST_INIT(event->bufferlist);
2765 event->region = *region;
2768 event->attributes = 0;
2771 * UDP sockets are always partial read.
2773 if (sock->type == isc_sockettype_udp)
2777 event->minimum = region->length;
2779 event->minimum = minimum;
2782 return (socket_recv(sock, event, task, flags));
2786 socket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
2787 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
2791 isc_boolean_t have_lock = ISC_FALSE;
2792 isc_task_t *ntask = NULL;
2793 isc_result_t result = ISC_R_SUCCESS;
2795 dev->ev_sender = task;
2797 set_dev_address(address, sock, dev);
2798 if (pktinfo != NULL) {
2799 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
2800 dev->pktinfo = *pktinfo;
2802 if (!isc_sockaddr_issitelocal(&dev->address) &&
2803 !isc_sockaddr_islinklocal(&dev->address)) {
2804 socket_log(sock, NULL, TRACE, isc_msgcat,
2805 ISC_MSGSET_SOCKET, ISC_MSG_PKTINFOPROVIDED,
2806 "pktinfo structure provided, ifindex %u "
2807 "(set to 0)", pktinfo->ipi6_ifindex);
2810 * Set the pktinfo index to 0 here, to let the
2811 * kernel decide what interface it should send on.
2813 dev->pktinfo.ipi6_ifindex = 0;
2817 if (sock->type == isc_sockettype_udp)
2818 io_state = doio_send(sock, dev);
2821 have_lock = ISC_TRUE;
2823 if (ISC_LIST_EMPTY(sock->send_list))
2824 io_state = doio_send(sock, dev);
2826 io_state = DOIO_SOFT;
2832 * We couldn't send all or part of the request right now, so
2833 * queue it unless ISC_SOCKFLAG_NORETRY is set.
2835 if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
2836 isc_task_attach(task, &ntask);
2837 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
2841 have_lock = ISC_TRUE;
2845 * Enqueue the request. If the socket was previously
2846 * not being watched, poke the watcher to start
2847 * paying attention to it.
2849 if (ISC_LIST_EMPTY(sock->send_list))
2850 select_poke(sock->manager, sock->fd,
2852 ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
2854 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2855 "socket_send: event %p -> task %p",
2858 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
2859 result = ISC_R_INPROGRESS;
2865 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
2866 send_senddone_event(sock, &dev);
2871 UNLOCK(&sock->lock);
2877 isc_socket_send(isc_socket_t *sock, isc_region_t *region,
2878 isc_task_t *task, isc_taskaction_t action, const void *arg)
2881 * REQUIRE() checking is performed in isc_socket_sendto().
2883 return (isc_socket_sendto(sock, region, task, action, arg, NULL,
2888 isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
2889 isc_task_t *task, isc_taskaction_t action, const void *arg,
2890 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
2892 isc_socketevent_t *dev;
2893 isc_socketmgr_t *manager;
2895 REQUIRE(VALID_SOCKET(sock));
2896 REQUIRE(region != NULL);
2897 REQUIRE(task != NULL);
2898 REQUIRE(action != NULL);
2900 manager = sock->manager;
2901 REQUIRE(VALID_MANAGER(manager));
2903 INSIST(sock->bound);
2905 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
2907 return (ISC_R_NOMEMORY);
2910 dev->region = *region;
2912 return (socket_send(sock, dev, task, address, pktinfo, 0));
2916 isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
2917 isc_task_t *task, isc_taskaction_t action, const void *arg)
2919 return (isc_socket_sendtov(sock, buflist, task, action, arg, NULL,
2924 isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
2925 isc_task_t *task, isc_taskaction_t action, const void *arg,
2926 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
2928 isc_socketevent_t *dev;
2929 isc_socketmgr_t *manager;
2930 unsigned int iocount;
2931 isc_buffer_t *buffer;
2933 REQUIRE(VALID_SOCKET(sock));
2934 REQUIRE(buflist != NULL);
2935 REQUIRE(!ISC_LIST_EMPTY(*buflist));
2936 REQUIRE(task != NULL);
2937 REQUIRE(action != NULL);
2939 manager = sock->manager;
2940 REQUIRE(VALID_MANAGER(manager));
2942 iocount = isc_bufferlist_usedcount(buflist);
2943 REQUIRE(iocount > 0);
2945 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
2947 return (ISC_R_NOMEMORY);
2951 * Move each buffer from the passed in list to our internal one.
2953 buffer = ISC_LIST_HEAD(*buflist);
2954 while (buffer != NULL) {
2955 ISC_LIST_DEQUEUE(*buflist, buffer, link);
2956 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
2957 buffer = ISC_LIST_HEAD(*buflist);
2960 return (socket_send(sock, dev, task, address, pktinfo, 0));
2964 isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
2966 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
2967 isc_socketevent_t *event, unsigned int flags)
2969 REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
2970 if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
2971 REQUIRE(sock->type == isc_sockettype_udp);
2972 event->ev_sender = sock;
2973 event->result = ISC_R_UNEXPECTED;
2974 ISC_LIST_INIT(event->bufferlist);
2975 event->region = *region;
2978 event->attributes = 0;
2980 return (socket_send(sock, event, task, address, pktinfo, flags));
2984 isc_socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
2985 #ifdef ISC_PLATFORM_HAVESYSUNH
2988 char strbuf[ISC_STRERRORSIZE];
2990 if (sockaddr->type.sa.sa_family != AF_UNIX)
2994 #if defined(S_IFMT) && defined(S_IFSOCK)
2995 #define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
2996 #elif defined(_S_IFMT) && defined(S_IFSOCK)
2997 #define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
3002 #if defined(S_IFMT) && defined(S_IFIFO)
3003 #define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
3004 #elif defined(_S_IFMT) && defined(S_IFIFO)
3005 #define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
3009 #if !defined(S_ISFIFO) && !defined(S_ISSOCK)
3010 #error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
3014 #define S_ISFIFO(mode) 0
3018 #define S_ISSOCK(mode) 0
3022 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
3023 isc__strerror(errno, strbuf, sizeof(strbuf));
3024 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3025 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3026 "isc_socket_cleanunix: stat(%s): %s",
3027 sockaddr->type.sunix.sun_path, strbuf);
3030 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
3031 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3032 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3033 "isc_socket_cleanunix: %s: not a socket",
3034 sockaddr->type.sunix.sun_path);
3037 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
3038 isc__strerror(errno, strbuf, sizeof(strbuf));
3039 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3040 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3041 "isc_socket_cleanunix: unlink(%s): %s",
3042 sockaddr->type.sunix.sun_path, strbuf);
3047 s = socket(AF_UNIX, SOCK_STREAM, 0);
3049 isc__strerror(errno, strbuf, sizeof(strbuf));
3050 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3051 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3052 "isc_socket_cleanunix: socket(%s): %s",
3053 sockaddr->type.sunix.sun_path, strbuf);
3057 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
3059 case ENOENT: /* We exited cleanly last time */
3062 isc__strerror(errno, strbuf, sizeof(strbuf));
3063 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3064 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3065 "isc_socket_cleanunix: stat(%s): %s",
3066 sockaddr->type.sunix.sun_path, strbuf);
3072 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
3073 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3074 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3075 "isc_socket_cleanunix: %s: not a socket",
3076 sockaddr->type.sunix.sun_path);
3080 if (connect(s, (struct sockaddr *)&sockaddr->type.sunix,
3081 sizeof(sockaddr->type.sunix)) < 0) {
3085 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
3086 isc__strerror(errno, strbuf, sizeof(strbuf));
3087 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3088 ISC_LOGMODULE_SOCKET,
3090 "isc_socket_cleanunix: "
3092 sockaddr->type.sunix.sun_path,
3097 isc__strerror(errno, strbuf, sizeof(strbuf));
3098 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3099 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3100 "isc_socket_cleanunix: connect(%s): %s",
3101 sockaddr->type.sunix.sun_path, strbuf);
3114 isc_socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
3115 isc_uint32_t owner, isc_uint32_t group)
3117 #ifdef ISC_PLATFORM_HAVESYSUNH
3118 isc_result_t result = ISC_R_SUCCESS;
3119 char strbuf[ISC_STRERRORSIZE];
3120 char path[sizeof(sockaddr->type.sunix.sun_path)];
3121 #ifdef NEED_SECURE_DIRECTORY
3125 REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
3126 INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
3127 strcpy(path, sockaddr->type.sunix.sun_path);
3129 #ifdef NEED_SECURE_DIRECTORY
3130 slash = strrchr(path, '/');
3131 if (slash != NULL) {
3140 if (chmod(path, perm) < 0) {
3141 isc__strerror(errno, strbuf, sizeof(strbuf));
3142 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3143 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3144 "isc_socket_permunix: chmod(%s, %d): %s",
3145 path, perm, strbuf);
3146 result = ISC_R_FAILURE;
3148 if (chown(path, owner, group) < 0) {
3149 isc__strerror(errno, strbuf, sizeof(strbuf));
3150 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3151 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3152 "isc_socket_permunix: chown(%s, %d, %d): %s",
3155 result = ISC_R_FAILURE;
3163 return (ISC_R_NOTIMPLEMENTED);
3168 isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr) {
3169 char strbuf[ISC_STRERRORSIZE];
3174 INSIST(!sock->bound);
3176 if (sock->pf != sockaddr->type.sa.sa_family) {
3177 UNLOCK(&sock->lock);
3178 return (ISC_R_FAMILYMISMATCH);
3181 * Only set SO_REUSEADDR when we want a specific port.
3184 if (sock->pf == AF_UNIX)
3187 if (isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
3188 setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
3190 UNEXPECTED_ERROR(__FILE__, __LINE__,
3191 "setsockopt(%d) %s", sock->fd,
3192 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3193 ISC_MSG_FAILED, "failed"));
3199 if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
3200 UNLOCK(&sock->lock);
3203 return (ISC_R_NOPERM);
3205 return (ISC_R_ADDRNOTAVAIL);
3207 return (ISC_R_ADDRINUSE);
3209 return (ISC_R_BOUND);
3211 isc__strerror(errno, strbuf, sizeof(strbuf));
3212 UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
3214 return (ISC_R_UNEXPECTED);
3218 socket_log(sock, sockaddr, TRACE,
3219 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
3222 UNLOCK(&sock->lock);
3223 return (ISC_R_SUCCESS);
3227 isc_socket_filter(isc_socket_t *sock, const char *filter) {
3228 #ifdef SO_ACCEPTFILTER
3229 char strbuf[ISC_STRERRORSIZE];
3230 struct accept_filter_arg afa;
3236 REQUIRE(VALID_SOCKET(sock));
3238 #ifdef SO_ACCEPTFILTER
3239 bzero(&afa, sizeof(afa));
3240 strncpy(afa.af_name, filter, sizeof(afa.af_name));
3241 if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
3242 &afa, sizeof(afa)) == -1) {
3243 isc__strerror(errno, strbuf, sizeof(strbuf));
3244 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
3245 ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
3247 return (ISC_R_FAILURE);
3249 return (ISC_R_SUCCESS);
3251 return (ISC_R_NOTIMPLEMENTED);
3256 * Set up to listen on a given socket. We do this by creating an internal
3257 * event that will be dispatched when the socket has read activity. The
3258 * watcher will send the internal event to the task when there is a new
3261 * Unlike in read, we don't preallocate a done event here. Every time there
3262 * is a new connection we'll have to allocate a new one anyway, so we might
3263 * as well keep things simple rather than having to track them.
3266 isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
3267 char strbuf[ISC_STRERRORSIZE];
3269 REQUIRE(VALID_SOCKET(sock));
3273 REQUIRE(!sock->listener);
3274 REQUIRE(sock->bound);
3275 REQUIRE(sock->type == isc_sockettype_tcp ||
3276 sock->type == isc_sockettype_unix);
3279 backlog = SOMAXCONN;
3281 if (listen(sock->fd, (int)backlog) < 0) {
3282 UNLOCK(&sock->lock);
3283 isc__strerror(errno, strbuf, sizeof(strbuf));
3285 UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
3287 return (ISC_R_UNEXPECTED);
3292 UNLOCK(&sock->lock);
3293 return (ISC_R_SUCCESS);
3297 * This should try to do agressive accept() XXXMLG
3300 isc_socket_accept(isc_socket_t *sock,
3301 isc_task_t *task, isc_taskaction_t action, const void *arg)
3303 isc_socket_newconnev_t *dev;
3304 isc_socketmgr_t *manager;
3305 isc_task_t *ntask = NULL;
3306 isc_socket_t *nsock;
3307 isc_result_t result;
3308 isc_boolean_t do_poke = ISC_FALSE;
3310 REQUIRE(VALID_SOCKET(sock));
3311 manager = sock->manager;
3312 REQUIRE(VALID_MANAGER(manager));
3316 REQUIRE(sock->listener);
3319 * Sender field is overloaded here with the task we will be sending
3320 * this event to. Just before the actual event is delivered the
3321 * actual ev_sender will be touched up to be the socket.
3323 dev = (isc_socket_newconnev_t *)
3324 isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
3325 action, arg, sizeof(*dev));
3327 UNLOCK(&sock->lock);
3328 return (ISC_R_NOMEMORY);
3330 ISC_LINK_INIT(dev, ev_link);
3332 result = allocate_socket(manager, sock->type, &nsock);
3333 if (result != ISC_R_SUCCESS) {
3334 isc_event_free(ISC_EVENT_PTR(&dev));
3335 UNLOCK(&sock->lock);
3340 * Attach to socket and to task.
3342 isc_task_attach(task, &ntask);
3343 nsock->references++;
3345 dev->ev_sender = ntask;
3346 dev->newsocket = nsock;
3349 * Poke watcher here. We still have the socket locked, so there
3350 * is no race condition. We will keep the lock for such a short
3351 * bit of time waking it up now or later won't matter all that much.
3353 if (ISC_LIST_EMPTY(sock->accept_list))
3356 ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
3359 select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
3361 UNLOCK(&sock->lock);
3362 return (ISC_R_SUCCESS);
3366 isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
3367 isc_task_t *task, isc_taskaction_t action, const void *arg)
3369 isc_socket_connev_t *dev;
3370 isc_task_t *ntask = NULL;
3371 isc_socketmgr_t *manager;
3373 char strbuf[ISC_STRERRORSIZE];
3375 REQUIRE(VALID_SOCKET(sock));
3376 REQUIRE(addr != NULL);
3377 REQUIRE(task != NULL);
3378 REQUIRE(action != NULL);
3380 manager = sock->manager;
3381 REQUIRE(VALID_MANAGER(manager));
3382 REQUIRE(addr != NULL);
3384 if (isc_sockaddr_ismulticast(addr))
3385 return (ISC_R_MULTICAST);
3389 REQUIRE(!sock->connecting);
3391 dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
3392 ISC_SOCKEVENT_CONNECT,
3396 UNLOCK(&sock->lock);
3397 return (ISC_R_NOMEMORY);
3399 ISC_LINK_INIT(dev, ev_link);
3402 * Try to do the connect right away, as there can be only one
3403 * outstanding, and it might happen to complete.
3405 sock->address = *addr;
3406 cc = connect(sock->fd, &addr->type.sa, addr->length);
3408 if (SOFT_ERROR(errno) || errno == EINPROGRESS)
3412 #define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
3413 ERROR_MATCH(EACCES, ISC_R_NOPERM);
3414 ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
3415 ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
3416 ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
3417 ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
3419 ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
3421 ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
3422 ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
3423 ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
3424 ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
3425 ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
3429 sock->connected = 0;
3431 isc__strerror(errno, strbuf, sizeof(strbuf));
3432 UNEXPECTED_ERROR(__FILE__, __LINE__, "%d/%s", errno, strbuf);
3434 UNLOCK(&sock->lock);
3435 isc_event_free(ISC_EVENT_PTR(&dev));
3436 return (ISC_R_UNEXPECTED);
3439 sock->connected = 0;
3440 isc_task_send(task, ISC_EVENT_PTR(&dev));
3442 UNLOCK(&sock->lock);
3443 return (ISC_R_SUCCESS);
3447 * If connect completed, fire off the done event.
3450 sock->connected = 1;
3452 dev->result = ISC_R_SUCCESS;
3453 isc_task_send(task, ISC_EVENT_PTR(&dev));
3455 UNLOCK(&sock->lock);
3456 return (ISC_R_SUCCESS);
3464 isc_task_attach(task, &ntask);
3466 sock->connecting = 1;
3468 dev->ev_sender = ntask;
3471 * Poke watcher here. We still have the socket locked, so there
3472 * is no race condition. We will keep the lock for such a short
3473 * bit of time waking it up now or later won't matter all that much.
3475 if (sock->connect_ev == NULL)
3476 select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
3478 sock->connect_ev = dev;
3480 UNLOCK(&sock->lock);
3481 return (ISC_R_SUCCESS);
3485 * Called when a socket with a pending connect() finishes.
3488 internal_connect(isc_task_t *me, isc_event_t *ev) {
3490 isc_socket_connev_t *dev;
3493 ISC_SOCKADDR_LEN_T optlen;
3494 char strbuf[ISC_STRERRORSIZE];
3495 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
3498 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
3500 sock = ev->ev_sender;
3501 INSIST(VALID_SOCKET(sock));
3506 * When the internal event was sent the reference count was bumped
3507 * to keep the socket around for us. Decrement the count here.
3509 INSIST(sock->references > 0);
3511 if (sock->references == 0) {
3512 UNLOCK(&sock->lock);
3518 * Has this event been canceled?
3520 dev = sock->connect_ev;
3522 INSIST(!sock->connecting);
3523 UNLOCK(&sock->lock);
3527 INSIST(sock->connecting);
3528 sock->connecting = 0;
3531 * Get any possible error status here.
3533 optlen = sizeof(cc);
3534 if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
3535 (void *)&cc, (void *)&optlen) < 0)
3542 * If the error is EAGAIN, just re-select on this
3543 * fd and pretend nothing strange happened.
3545 if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
3546 sock->connecting = 1;
3547 select_poke(sock->manager, sock->fd,
3548 SELECT_POKE_CONNECT);
3549 UNLOCK(&sock->lock);
3555 * Translate other errors into ISC_R_* flavors.
3558 #define ERROR_MATCH(a, b) case a: dev->result = b; break;
3559 ERROR_MATCH(EACCES, ISC_R_NOPERM);
3560 ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
3561 ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
3562 ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
3563 ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
3565 ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
3567 ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
3568 ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
3569 ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
3570 ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
3571 ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
3572 ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
3575 dev->result = ISC_R_UNEXPECTED;
3576 isc_sockaddr_format(&sock->address, peerbuf,
3578 isc__strerror(errno, strbuf, sizeof(strbuf));
3579 UNEXPECTED_ERROR(__FILE__, __LINE__,
3580 "internal_connect: connect(%s) %s",
3584 dev->result = ISC_R_SUCCESS;
3585 sock->connected = 1;
3589 sock->connect_ev = NULL;
3591 UNLOCK(&sock->lock);
3593 task = dev->ev_sender;
3594 dev->ev_sender = sock;
3595 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
3599 isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
3600 isc_result_t result;
3602 REQUIRE(VALID_SOCKET(sock));
3603 REQUIRE(addressp != NULL);
3607 if (sock->connected) {
3608 *addressp = sock->address;
3609 result = ISC_R_SUCCESS;
3611 result = ISC_R_NOTCONNECTED;
3614 UNLOCK(&sock->lock);
3620 isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
3621 ISC_SOCKADDR_LEN_T len;
3622 isc_result_t result;
3623 char strbuf[ISC_STRERRORSIZE];
3625 REQUIRE(VALID_SOCKET(sock));
3626 REQUIRE(addressp != NULL);
3631 result = ISC_R_NOTBOUND;
3635 result = ISC_R_SUCCESS;
3637 len = sizeof(addressp->type);
3638 if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
3639 isc__strerror(errno, strbuf, sizeof(strbuf));
3640 UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
3642 result = ISC_R_UNEXPECTED;
3645 addressp->length = (unsigned int)len;
3648 UNLOCK(&sock->lock);
3654 * Run through the list of events on this socket, and cancel the ones
3655 * queued for task "task" of type "how". "how" is a bitmask.
3658 isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
3660 REQUIRE(VALID_SOCKET(sock));
3663 * Quick exit if there is nothing to do. Don't even bother locking
3672 * All of these do the same thing, more or less.
3674 * o If the internal event is marked as "posted" try to
3675 * remove it from the task's queue. If this fails, mark it
3676 * as canceled instead, and let the task clean it up later.
3677 * o For each I/O request for that task of that type, post
3678 * its done event with status of "ISC_R_CANCELED".
3679 * o Reset any state needed.
3681 if (((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV)
3682 && !ISC_LIST_EMPTY(sock->recv_list)) {
3683 isc_socketevent_t *dev;
3684 isc_socketevent_t *next;
3685 isc_task_t *current_task;
3687 dev = ISC_LIST_HEAD(sock->recv_list);
3689 while (dev != NULL) {
3690 current_task = dev->ev_sender;
3691 next = ISC_LIST_NEXT(dev, ev_link);
3693 if ((task == NULL) || (task == current_task)) {
3694 dev->result = ISC_R_CANCELED;
3695 send_recvdone_event(sock, &dev);
3701 if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
3702 && !ISC_LIST_EMPTY(sock->send_list)) {
3703 isc_socketevent_t *dev;
3704 isc_socketevent_t *next;
3705 isc_task_t *current_task;
3707 dev = ISC_LIST_HEAD(sock->send_list);
3709 while (dev != NULL) {
3710 current_task = dev->ev_sender;
3711 next = ISC_LIST_NEXT(dev, ev_link);
3713 if ((task == NULL) || (task == current_task)) {
3714 dev->result = ISC_R_CANCELED;
3715 send_senddone_event(sock, &dev);
3721 if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
3722 && !ISC_LIST_EMPTY(sock->accept_list)) {
3723 isc_socket_newconnev_t *dev;
3724 isc_socket_newconnev_t *next;
3725 isc_task_t *current_task;
3727 dev = ISC_LIST_HEAD(sock->accept_list);
3728 while (dev != NULL) {
3729 current_task = dev->ev_sender;
3730 next = ISC_LIST_NEXT(dev, ev_link);
3732 if ((task == NULL) || (task == current_task)) {
3734 ISC_LIST_UNLINK(sock->accept_list, dev,
3737 dev->newsocket->references--;
3738 free_socket(&dev->newsocket);
3740 dev->result = ISC_R_CANCELED;
3741 dev->ev_sender = sock;
3742 isc_task_sendanddetach(¤t_task,
3743 ISC_EVENT_PTR(&dev));
3751 * Connecting is not a list.
3753 if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
3754 && sock->connect_ev != NULL) {
3755 isc_socket_connev_t *dev;
3756 isc_task_t *current_task;
3758 INSIST(sock->connecting);
3759 sock->connecting = 0;
3761 dev = sock->connect_ev;
3762 current_task = dev->ev_sender;
3764 if ((task == NULL) || (task == current_task)) {
3765 sock->connect_ev = NULL;
3767 dev->result = ISC_R_CANCELED;
3768 dev->ev_sender = sock;
3769 isc_task_sendanddetach(¤t_task,
3770 ISC_EVENT_PTR(&dev));
3774 UNLOCK(&sock->lock);
3778 isc_socket_gettype(isc_socket_t *sock) {
3779 REQUIRE(VALID_SOCKET(sock));
3781 return (sock->type);
3785 isc_socket_isbound(isc_socket_t *sock) {
3789 val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
3790 UNLOCK(&sock->lock);
3796 isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
3797 #if defined(IPV6_V6ONLY)
3798 int onoff = yes ? 1 : 0;
3804 REQUIRE(VALID_SOCKET(sock));
3807 if (sock->pf == AF_INET6) {
3808 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
3809 (void *)&onoff, sizeof(onoff));
3814 #ifndef ISC_PLATFORM_USETHREADS
3816 isc__socketmgr_getfdsets(fd_set *readset, fd_set *writeset, int *maxfd) {
3817 if (socketmgr == NULL)
3820 *readset = socketmgr->read_fds;
3821 *writeset = socketmgr->write_fds;
3822 *maxfd = socketmgr->maxfd + 1;
3827 isc__socketmgr_dispatch(fd_set *readset, fd_set *writeset, int maxfd) {
3828 isc_socketmgr_t *manager = socketmgr;
3830 if (manager == NULL)
3831 return (ISC_R_NOTFOUND);
3833 process_fds(manager, maxfd, readset, writeset);
3834 return (ISC_R_SUCCESS);
3836 #endif /* ISC_PLATFORM_USETHREADS */