2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.24 2006/06/06 00:56:09 marka 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>
51 #include <isc/platform.h>
52 #include <isc/print.h>
53 #include <isc/region.h>
54 #include <isc/socket.h>
55 #include <isc/strerror.h>
57 #include <isc/thread.h>
60 #include "errno2result.h"
62 #ifndef ISC_PLATFORM_USETHREADS
64 #endif /* ISC_PLATFORM_USETHREADS */
67 * Some systems define the socket length argument as an int, some as size_t,
68 * some as socklen_t. This is here so it can be easily changed if needed.
70 #ifndef ISC_SOCKADDR_LEN_T
71 #define ISC_SOCKADDR_LEN_T unsigned int
75 * Define what the possible "soft" errors can be. These are non-fatal returns
76 * of various network related functions, like recv() and so on.
78 * For some reason, BSDI (and perhaps others) will sometimes return <0
79 * from recv() but will have errno==0. This is broken, but we have to
80 * work around it here.
82 #define SOFT_ERROR(e) ((e) == EAGAIN || \
83 (e) == EWOULDBLOCK || \
87 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
90 * DLVL(90) -- Function entry/exit and other tracing.
91 * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
92 * DLVL(60) -- Socket data send/receive
93 * DLVL(50) -- Event tracing, including receiving/sending completion events.
94 * DLVL(20) -- Socket creation/destruction.
96 #define TRACE_LEVEL 90
97 #define CORRECTNESS_LEVEL 70
98 #define IOEVENT_LEVEL 60
99 #define EVENT_LEVEL 50
100 #define CREATION_LEVEL 20
102 #define TRACE DLVL(TRACE_LEVEL)
103 #define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
104 #define IOEVENT DLVL(IOEVENT_LEVEL)
105 #define EVENT DLVL(EVENT_LEVEL)
106 #define CREATION DLVL(CREATION_LEVEL)
108 typedef isc_event_t intev_t;
110 #define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
111 #define VALID_SOCKET(t) ISC_MAGIC_VALID(t, SOCKET_MAGIC)
114 * IPv6 control information. If the socket is an IPv6 socket we want
115 * to collect the destination address and interface so the client can
116 * set them on outgoing packets.
118 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
125 * NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
126 * a setsockopt() like interface to request timestamps, and if the OS
127 * doesn't do it for us, call gettimeofday() on every UDP receive?
136 * The size to raise the recieve buffer to (from BIND 8).
138 #define RCVBUFSIZE (32*1024)
141 * The number of times a send operation is repeated if the result is EINTR.
148 isc_socketmgr_t *manager;
150 isc_sockettype_t type;
152 /* Locked by socket lock. */
153 ISC_LINK(isc_socket_t) link;
154 unsigned int references;
158 ISC_LIST(isc_socketevent_t) send_list;
159 ISC_LIST(isc_socketevent_t) recv_list;
160 ISC_LIST(isc_socket_newconnev_t) accept_list;
161 isc_socket_connev_t *connect_ev;
164 * Internal events. Posted when a descriptor is readable or
165 * writable. These are statically allocated and never freed.
166 * They will be set to non-purgable before use.
171 isc_sockaddr_t address; /* remote address */
173 unsigned int pending_recv : 1,
176 listener : 1, /* listener socket */
178 connecting : 1, /* connect pending */
179 bound : 1; /* bound to local addr */
181 #ifdef ISC_NET_RECVOVERFLOW
182 unsigned char overflow; /* used for MSG_TRUNC fake */
186 ISC_SOCKADDR_LEN_T recvcmsgbuflen;
188 ISC_SOCKADDR_LEN_T sendcmsgbuflen;
191 #define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
192 #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
194 struct isc_socketmgr {
199 /* Locked by manager lock. */
200 ISC_LIST(isc_socket_t) socklist;
203 isc_socket_t *fds[FD_SETSIZE];
204 int fdstate[FD_SETSIZE];
206 #ifdef ISC_PLATFORM_USETHREADS
207 isc_thread_t watcher;
208 isc_condition_t shutdown_ok;
210 #else /* ISC_PLATFORM_USETHREADS */
212 #endif /* ISC_PLATFORM_USETHREADS */
215 #ifndef ISC_PLATFORM_USETHREADS
216 static isc_socketmgr_t *socketmgr = NULL;
217 #endif /* ISC_PLATFORM_USETHREADS */
219 #define CLOSED 0 /* this one must be zero */
221 #define CLOSE_PENDING 2
224 * send() and recv() iovec counts
226 #define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
227 #ifdef ISC_NET_RECVOVERFLOW
228 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
230 # define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
233 static void send_recvdone_event(isc_socket_t *, isc_socketevent_t **);
234 static void send_senddone_event(isc_socket_t *, isc_socketevent_t **);
235 static void free_socket(isc_socket_t **);
236 static isc_result_t allocate_socket(isc_socketmgr_t *, isc_sockettype_t,
238 static void destroy(isc_socket_t **);
239 static void internal_accept(isc_task_t *, isc_event_t *);
240 static void internal_connect(isc_task_t *, isc_event_t *);
241 static void internal_recv(isc_task_t *, isc_event_t *);
242 static void internal_send(isc_task_t *, isc_event_t *);
243 static void process_cmsg(isc_socket_t *, struct msghdr *, isc_socketevent_t *);
244 static void build_msghdr_send(isc_socket_t *, isc_socketevent_t *,
245 struct msghdr *, struct iovec *, size_t *);
246 static void build_msghdr_recv(isc_socket_t *, isc_socketevent_t *,
247 struct msghdr *, struct iovec *, size_t *);
249 #define SELECT_POKE_SHUTDOWN (-1)
250 #define SELECT_POKE_NOTHING (-2)
251 #define SELECT_POKE_READ (-3)
252 #define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
253 #define SELECT_POKE_WRITE (-4)
254 #define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
255 #define SELECT_POKE_CLOSE (-5)
257 #define SOCK_DEAD(s) ((s)->references == 0)
260 manager_log(isc_socketmgr_t *sockmgr,
261 isc_logcategory_t *category, isc_logmodule_t *module, int level,
262 const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
264 manager_log(isc_socketmgr_t *sockmgr,
265 isc_logcategory_t *category, isc_logmodule_t *module, int level,
266 const char *fmt, ...)
271 if (! isc_log_wouldlog(isc_lctx, level))
275 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
278 isc_log_write(isc_lctx, category, module, level,
279 "sockmgr %p: %s", sockmgr, msgbuf);
283 socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
284 isc_logcategory_t *category, isc_logmodule_t *module, int level,
285 isc_msgcat_t *msgcat, int msgset, int message,
286 const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
288 socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
289 isc_logcategory_t *category, isc_logmodule_t *module, int level,
290 isc_msgcat_t *msgcat, int msgset, int message,
291 const char *fmt, ...)
294 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
297 if (! isc_log_wouldlog(isc_lctx, level))
301 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
304 if (address == NULL) {
305 isc_log_iwrite(isc_lctx, category, module, level,
306 msgcat, msgset, message,
307 "socket %p: %s", sock, msgbuf);
309 isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
310 isc_log_iwrite(isc_lctx, category, module, level,
311 msgcat, msgset, message,
312 "socket %p %s: %s", sock, peerbuf, msgbuf);
317 wakeup_socket(isc_socketmgr_t *manager, int fd, int msg) {
321 * This is a wakeup on a socket. If the socket is not in the
322 * process of being closed, start watching it for either reads
326 INSIST(fd >= 0 && fd < (int)FD_SETSIZE);
328 if (manager->fdstate[fd] == CLOSE_PENDING) {
329 manager->fdstate[fd] = CLOSED;
330 FD_CLR(fd, &manager->read_fds);
331 FD_CLR(fd, &manager->write_fds);
335 if (manager->fdstate[fd] != MANAGED)
338 sock = manager->fds[fd];
343 if (msg == SELECT_POKE_READ)
344 FD_SET(sock->fd, &manager->read_fds);
345 if (msg == SELECT_POKE_WRITE)
346 FD_SET(sock->fd, &manager->write_fds);
349 #ifdef ISC_PLATFORM_USETHREADS
351 * Poke the select loop when there is something for us to do.
352 * The write is required (by POSIX) to complete. That is, we
353 * will not get partial writes.
356 select_poke(isc_socketmgr_t *mgr, int fd, int msg) {
359 char strbuf[ISC_STRERRORSIZE];
365 cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
368 * Treat ENOSR as EAGAIN but loop slowly as it is
369 * unlikely to clear fast.
371 if (cc < 0 && errno == ENOSR) {
376 } while (cc < 0 && SOFT_ERROR(errno));
379 isc__strerror(errno, strbuf, sizeof(strbuf));
380 FATAL_ERROR(__FILE__, __LINE__,
381 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
384 "during watcher poke: %s"),
388 INSIST(cc == sizeof(buf));
392 * Read a message on the internal fd.
395 select_readmsg(isc_socketmgr_t *mgr, int *fd, int *msg) {
398 char strbuf[ISC_STRERRORSIZE];
400 cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
402 *msg = SELECT_POKE_NOTHING;
403 *fd = -1; /* Silence compiler. */
404 if (SOFT_ERROR(errno))
407 isc__strerror(errno, strbuf, sizeof(strbuf));
408 FATAL_ERROR(__FILE__, __LINE__,
409 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
412 "during watcher poke: %s"),
417 INSIST(cc == sizeof(buf));
422 #else /* ISC_PLATFORM_USETHREADS */
424 * Update the state of the socketmgr when something changes.
427 select_poke(isc_socketmgr_t *manager, int fd, int msg) {
428 if (msg == SELECT_POKE_SHUTDOWN)
431 wakeup_socket(manager, fd, msg);
434 #endif /* ISC_PLATFORM_USETHREADS */
437 * Make a fd non-blocking.
440 make_nonblock(int fd) {
443 char strbuf[ISC_STRERRORSIZE];
444 #ifdef USE_FIONBIO_IOCTL
447 ret = ioctl(fd, FIONBIO, (char *)&on);
449 flags = fcntl(fd, F_GETFL, 0);
450 flags |= PORT_NONBLOCK;
451 ret = fcntl(fd, F_SETFL, flags);
455 isc__strerror(errno, strbuf, sizeof(strbuf));
456 UNEXPECTED_ERROR(__FILE__, __LINE__,
457 #ifdef USE_FIONBIO_IOCTL
458 "ioctl(%d, FIONBIO, &on): %s", fd,
460 "fcntl(%d, F_SETFL, %d): %s", fd, flags,
464 return (ISC_R_UNEXPECTED);
467 return (ISC_R_SUCCESS);
472 * Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
473 * In order to ensure as much portability as possible, we provide wrapper
474 * functions of these macros.
475 * Note that cmsg_space() could run slow on OSes that do not have
478 static inline ISC_SOCKADDR_LEN_T
479 cmsg_len(ISC_SOCKADDR_LEN_T len) {
481 return (CMSG_LEN(len));
483 ISC_SOCKADDR_LEN_T hdrlen;
486 * Cast NULL so that any pointer arithmetic performed by CMSG_DATA
489 hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
490 return (hdrlen + len);
494 static inline ISC_SOCKADDR_LEN_T
495 cmsg_space(ISC_SOCKADDR_LEN_T len) {
497 return (CMSG_SPACE(len));
500 struct cmsghdr *cmsgp;
502 * XXX: The buffer length is an ad-hoc value, but should be enough
503 * in a practical sense.
505 char dummybuf[sizeof(struct cmsghdr) + 1024];
507 memset(&msg, 0, sizeof(msg));
508 msg.msg_control = dummybuf;
509 msg.msg_controllen = sizeof(dummybuf);
511 cmsgp = (struct cmsghdr *)dummybuf;
512 cmsgp->cmsg_len = cmsg_len(len);
514 cmsgp = CMSG_NXTHDR(&msg, cmsgp);
516 return ((char *)cmsgp - (char *)msg.msg_control);
521 #endif /* USE_CMSG */
524 * Process control messages received on a socket.
527 process_cmsg(isc_socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
529 struct cmsghdr *cmsgp;
530 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
531 struct in6_pktinfo *pktinfop;
534 struct timeval *timevalp;
539 * sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
540 * msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
541 * They are all here, outside of the CPP tests, because it is
542 * more consistent with the usual ISC coding style.
548 #ifdef ISC_NET_BSD44MSGHDR
551 if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
552 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
556 if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
557 dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
563 if (msg->msg_controllen == 0U || msg->msg_control == NULL)
569 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
573 cmsgp = CMSG_FIRSTHDR(msg);
574 while (cmsgp != NULL) {
575 socket_log(sock, NULL, TRACE,
576 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
577 "processing cmsg %p", cmsgp);
579 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
580 if (cmsgp->cmsg_level == IPPROTO_IPV6
581 && cmsgp->cmsg_type == IPV6_PKTINFO) {
583 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
584 memcpy(&dev->pktinfo, pktinfop,
585 sizeof(struct in6_pktinfo));
586 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
587 socket_log(sock, NULL, TRACE,
588 isc_msgcat, ISC_MSGSET_SOCKET,
590 "interface received on ifindex %u",
591 dev->pktinfo.ipi6_ifindex);
592 if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
593 dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
599 if (cmsgp->cmsg_level == SOL_SOCKET
600 && cmsgp->cmsg_type == SCM_TIMESTAMP) {
601 timevalp = (struct timeval *)CMSG_DATA(cmsgp);
602 dev->timestamp.seconds = timevalp->tv_sec;
603 dev->timestamp.nanoseconds = timevalp->tv_usec * 1000;
604 dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
610 cmsgp = CMSG_NXTHDR(msg, cmsgp);
612 #endif /* USE_CMSG */
614 #endif /* ISC_NET_BSD44MSGHDR */
618 * Construct an iov array and attach it to the msghdr passed in. This is
619 * the SEND constructor, which will use the used region of the buffer
620 * (if using a buffer list) or will use the internal region (if a single
621 * buffer I/O is requested).
623 * Nothing can be NULL, and the done event must list at least one buffer
624 * on the buffer linked list for this function to be meaningful.
626 * If write_countp != NULL, *write_countp will hold the number of bytes
627 * this transaction can send.
630 build_msghdr_send(isc_socket_t *sock, isc_socketevent_t *dev,
631 struct msghdr *msg, struct iovec *iov, size_t *write_countp)
633 unsigned int iovcount;
634 isc_buffer_t *buffer;
639 memset(msg, 0, sizeof(*msg));
641 if (sock->type == isc_sockettype_udp) {
642 msg->msg_name = (void *)&dev->address.type.sa;
643 msg->msg_namelen = dev->address.length;
645 msg->msg_name = NULL;
646 msg->msg_namelen = 0;
649 buffer = ISC_LIST_HEAD(dev->bufferlist);
654 * Single buffer I/O? Skip what we've done so far in this region.
656 if (buffer == NULL) {
657 write_count = dev->region.length - dev->n;
658 iov[0].iov_base = (void *)(dev->region.base + dev->n);
659 iov[0].iov_len = write_count;
667 * Skip the data in the buffer list that we have already written.
670 while (buffer != NULL) {
671 REQUIRE(ISC_BUFFER_VALID(buffer));
672 if (skip_count < isc_buffer_usedlength(buffer))
674 skip_count -= isc_buffer_usedlength(buffer);
675 buffer = ISC_LIST_NEXT(buffer, link);
678 while (buffer != NULL) {
679 INSIST(iovcount < MAXSCATTERGATHER_SEND);
681 isc_buffer_usedregion(buffer, &used);
683 if (used.length > 0) {
684 iov[iovcount].iov_base = (void *)(used.base
686 iov[iovcount].iov_len = used.length - skip_count;
687 write_count += (used.length - skip_count);
691 buffer = ISC_LIST_NEXT(buffer, link);
694 INSIST(skip_count == 0U);
698 msg->msg_iovlen = iovcount;
700 #ifdef ISC_NET_BSD44MSGHDR
701 msg->msg_control = NULL;
702 msg->msg_controllen = 0;
704 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
705 if ((sock->type == isc_sockettype_udp)
706 && ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
707 struct cmsghdr *cmsgp;
708 struct in6_pktinfo *pktinfop;
710 socket_log(sock, NULL, TRACE,
711 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
712 "sendto pktinfo data, ifindex %u",
713 dev->pktinfo.ipi6_ifindex);
715 msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
716 INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
717 msg->msg_control = (void *)sock->sendcmsgbuf;
719 cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
720 cmsgp->cmsg_level = IPPROTO_IPV6;
721 cmsgp->cmsg_type = IPV6_PKTINFO;
722 cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
723 pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
724 memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
726 #endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
727 #else /* ISC_NET_BSD44MSGHDR */
728 msg->msg_accrights = NULL;
729 msg->msg_accrightslen = 0;
730 #endif /* ISC_NET_BSD44MSGHDR */
732 if (write_countp != NULL)
733 *write_countp = write_count;
737 * Construct an iov array and attach it to the msghdr passed in. This is
738 * the RECV constructor, which will use the avialable region of the buffer
739 * (if using a buffer list) or will use the internal region (if a single
740 * buffer I/O is requested).
742 * Nothing can be NULL, and the done event must list at least one buffer
743 * on the buffer linked list for this function to be meaningful.
745 * If read_countp != NULL, *read_countp will hold the number of bytes
746 * this transaction can receive.
749 build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev,
750 struct msghdr *msg, struct iovec *iov, size_t *read_countp)
752 unsigned int iovcount;
753 isc_buffer_t *buffer;
754 isc_region_t available;
757 memset(msg, 0, sizeof(struct msghdr));
759 if (sock->type == isc_sockettype_udp) {
760 memset(&dev->address, 0, sizeof(dev->address));
761 #ifdef BROKEN_RECVMSG
762 if (sock->pf == AF_INET) {
763 msg->msg_name = (void *)&dev->address.type.sin;
764 msg->msg_namelen = sizeof(dev->address.type.sin6);
765 } else if (sock->pf == AF_INET6) {
766 msg->msg_name = (void *)&dev->address.type.sin6;
767 msg->msg_namelen = sizeof(dev->address.type.sin6);
768 #ifdef ISC_PLATFORM_HAVESYSUNH
769 } else if (sock->pf == AF_UNIX) {
770 msg->msg_name = (void *)&dev->address.type.sunix;
771 msg->msg_namelen = sizeof(dev->address.type.sunix);
774 msg->msg_name = (void *)&dev->address.type.sa;
775 msg->msg_namelen = sizeof(dev->address.type);
778 msg->msg_name = (void *)&dev->address.type.sa;
779 msg->msg_namelen = sizeof(dev->address.type);
781 #ifdef ISC_NET_RECVOVERFLOW
782 /* If needed, steal one iovec for overflow detection. */
786 msg->msg_name = NULL;
787 msg->msg_namelen = 0;
788 dev->address = sock->address;
791 buffer = ISC_LIST_HEAD(dev->bufferlist);
795 * Single buffer I/O? Skip what we've done so far in this region.
797 if (buffer == NULL) {
798 read_count = dev->region.length - dev->n;
799 iov[0].iov_base = (void *)(dev->region.base + dev->n);
800 iov[0].iov_len = read_count;
808 * Skip empty buffers.
810 while (buffer != NULL) {
811 REQUIRE(ISC_BUFFER_VALID(buffer));
812 if (isc_buffer_availablelength(buffer) != 0)
814 buffer = ISC_LIST_NEXT(buffer, link);
818 while (buffer != NULL) {
819 INSIST(iovcount < MAXSCATTERGATHER_RECV);
821 isc_buffer_availableregion(buffer, &available);
823 if (available.length > 0) {
824 iov[iovcount].iov_base = (void *)(available.base);
825 iov[iovcount].iov_len = available.length;
826 read_count += available.length;
829 buffer = ISC_LIST_NEXT(buffer, link);
835 * If needed, set up to receive that one extra byte. Note that
836 * we know there is at least one iov left, since we stole it
837 * at the top of this function.
839 #ifdef ISC_NET_RECVOVERFLOW
840 if (sock->type == isc_sockettype_udp) {
841 iov[iovcount].iov_base = (void *)(&sock->overflow);
842 iov[iovcount].iov_len = 1;
848 msg->msg_iovlen = iovcount;
850 #ifdef ISC_NET_BSD44MSGHDR
851 msg->msg_control = NULL;
852 msg->msg_controllen = 0;
854 #if defined(USE_CMSG)
855 if (sock->type == isc_sockettype_udp) {
856 msg->msg_control = sock->recvcmsgbuf;
857 msg->msg_controllen = sock->recvcmsgbuflen;
859 #endif /* USE_CMSG */
860 #else /* ISC_NET_BSD44MSGHDR */
861 msg->msg_accrights = NULL;
862 msg->msg_accrightslen = 0;
863 #endif /* ISC_NET_BSD44MSGHDR */
865 if (read_countp != NULL)
866 *read_countp = read_count;
870 set_dev_address(isc_sockaddr_t *address, isc_socket_t *sock,
871 isc_socketevent_t *dev)
873 if (sock->type == isc_sockettype_udp) {
875 dev->address = *address;
877 dev->address = sock->address;
878 } else if (sock->type == isc_sockettype_tcp) {
879 INSIST(address == NULL);
880 dev->address = sock->address;
885 destroy_socketevent(isc_event_t *event) {
886 isc_socketevent_t *ev = (isc_socketevent_t *)event;
888 INSIST(ISC_LIST_EMPTY(ev->bufferlist));
890 (ev->destroy)(event);
893 static isc_socketevent_t *
894 allocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype,
895 isc_taskaction_t action, const void *arg)
897 isc_socketevent_t *ev;
899 ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
907 ev->result = ISC_R_UNEXPECTED;
908 ISC_LINK_INIT(ev, ev_link);
909 ISC_LIST_INIT(ev->bufferlist);
910 ev->region.base = NULL;
914 ev->destroy = ev->ev_destroy;
915 ev->ev_destroy = destroy_socketevent;
920 #if defined(ISC_SOCKET_DEBUG)
922 dump_msg(struct msghdr *msg) {
925 printf("MSGHDR %p\n", msg);
926 printf("\tname %p, namelen %d\n", msg->msg_name, msg->msg_namelen);
927 printf("\tiov %p, iovlen %d\n", msg->msg_iov, msg->msg_iovlen);
928 for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
929 printf("\t\t%d\tbase %p, len %d\n", i,
930 msg->msg_iov[i].iov_base,
931 msg->msg_iov[i].iov_len);
932 #ifdef ISC_NET_BSD44MSGHDR
933 printf("\tcontrol %p, controllen %d\n", msg->msg_control,
934 msg->msg_controllen);
939 #define DOIO_SUCCESS 0 /* i/o ok, event sent */
940 #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
941 #define DOIO_HARD 2 /* i/o error, event sent */
942 #define DOIO_EOF 3 /* EOF, no event sent */
945 doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
947 struct iovec iov[MAXSCATTERGATHER_RECV];
950 struct msghdr msghdr;
951 isc_buffer_t *buffer;
953 char strbuf[ISC_STRERRORSIZE];
955 build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
957 #if defined(ISC_SOCKET_DEBUG)
961 cc = recvmsg(sock->fd, &msghdr, 0);
964 #if defined(ISC_SOCKET_DEBUG)
969 if (SOFT_ERROR(recv_errno))
972 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
973 isc__strerror(recv_errno, strbuf, sizeof(strbuf));
974 socket_log(sock, NULL, IOEVENT,
975 isc_msgcat, ISC_MSGSET_SOCKET,
977 "doio_recv: recvmsg(%d) %d bytes, err %d/%s",
978 sock->fd, cc, recv_errno, strbuf);
981 #define SOFT_OR_HARD(_system, _isc) \
982 if (recv_errno == _system) { \
983 if (sock->connected) { \
984 dev->result = _isc; \
985 return (DOIO_HARD); \
987 return (DOIO_SOFT); \
989 #define ALWAYS_HARD(_system, _isc) \
990 if (recv_errno == _system) { \
991 dev->result = _isc; \
992 return (DOIO_HARD); \
995 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
996 SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
997 SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
998 SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
999 /* HPUX 11.11 can return EADDRNOTAVAIL. */
1000 SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1001 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1006 dev->result = isc__errno2result(recv_errno);
1011 * On TCP, zero length reads indicate EOF, while on
1012 * UDP, zero length reads are perfectly valid, although
1015 if ((sock->type == isc_sockettype_tcp) && (cc == 0))
1018 if (sock->type == isc_sockettype_udp) {
1019 dev->address.length = msghdr.msg_namelen;
1020 if (isc_sockaddr_getport(&dev->address) == 0) {
1021 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1022 socket_log(sock, &dev->address, IOEVENT,
1023 isc_msgcat, ISC_MSGSET_SOCKET,
1025 "dropping source port zero packet");
1031 socket_log(sock, &dev->address, IOEVENT,
1032 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
1033 "packet received correctly");
1036 * Overflow bit detection. If we received MORE bytes than we should,
1037 * this indicates an overflow situation. Set the flag in the
1038 * dev entry and adjust how much we read by one.
1040 #ifdef ISC_NET_RECVOVERFLOW
1041 if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
1042 dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
1048 * If there are control messages attached, run through them and pull
1049 * out the interesting bits.
1051 if (sock->type == isc_sockettype_udp)
1052 process_cmsg(sock, &msghdr, dev);
1055 * update the buffers (if any) and the i/o count
1059 buffer = ISC_LIST_HEAD(dev->bufferlist);
1060 while (buffer != NULL && actual_count > 0U) {
1061 REQUIRE(ISC_BUFFER_VALID(buffer));
1062 if (isc_buffer_availablelength(buffer) <= actual_count) {
1063 actual_count -= isc_buffer_availablelength(buffer);
1064 isc_buffer_add(buffer,
1065 isc_buffer_availablelength(buffer));
1067 isc_buffer_add(buffer, actual_count);
1071 buffer = ISC_LIST_NEXT(buffer, link);
1072 if (buffer == NULL) {
1073 INSIST(actual_count == 0U);
1078 * If we read less than we expected, update counters,
1079 * and let the upper layer poke the descriptor.
1081 if (((size_t)cc != read_count) && (dev->n < dev->minimum))
1085 * Full reads are posted, or partials if partials are ok.
1087 dev->result = ISC_R_SUCCESS;
1088 return (DOIO_SUCCESS);
1093 * DOIO_SUCCESS The operation succeeded. dev->result contains
1096 * DOIO_HARD A hard or unexpected I/O error was encountered.
1097 * dev->result contains the appropriate error.
1099 * DOIO_SOFT A soft I/O error was encountered. No senddone
1100 * event was sent. The operation should be retried.
1102 * No other return values are possible.
1105 doio_send(isc_socket_t *sock, isc_socketevent_t *dev) {
1107 struct iovec iov[MAXSCATTERGATHER_SEND];
1109 struct msghdr msghdr;
1110 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1113 char strbuf[ISC_STRERRORSIZE];
1115 build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
1118 cc = sendmsg(sock->fd, &msghdr, 0);
1122 * Check for error or block condition.
1125 if (send_errno == EINTR && ++attempts < NRETRIES)
1128 if (SOFT_ERROR(send_errno))
1131 #define SOFT_OR_HARD(_system, _isc) \
1132 if (send_errno == _system) { \
1133 if (sock->connected) { \
1134 dev->result = _isc; \
1135 return (DOIO_HARD); \
1137 return (DOIO_SOFT); \
1139 #define ALWAYS_HARD(_system, _isc) \
1140 if (send_errno == _system) { \
1141 dev->result = _isc; \
1142 return (DOIO_HARD); \
1145 SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
1146 ALWAYS_HARD(EACCES, ISC_R_NOPERM);
1147 ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
1148 ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
1149 ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
1151 ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
1153 ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
1154 ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
1155 ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
1156 ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
1157 ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
1163 * The other error types depend on whether or not the
1164 * socket is UDP or TCP. If it is UDP, some errors
1165 * that we expect to be fatal under TCP are merely
1166 * annoying, and are really soft errors.
1168 * However, these soft errors are still returned as
1171 isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
1172 isc__strerror(send_errno, strbuf, sizeof(strbuf));
1173 UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
1175 dev->result = isc__errno2result(send_errno);
1180 UNEXPECTED_ERROR(__FILE__, __LINE__,
1181 "internal_send: send() %s 0",
1182 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1183 ISC_MSG_RETURNED, "returned"));
1186 * If we write less than we expected, update counters, poke.
1189 if ((size_t)cc != write_count)
1193 * Exactly what we wanted to write. We're done with this
1194 * entry. Post its completion event.
1196 dev->result = ISC_R_SUCCESS;
1197 return (DOIO_SUCCESS);
1203 * Caller must ensure that the socket is not locked and no external
1207 destroy(isc_socket_t **sockp) {
1208 isc_socket_t *sock = *sockp;
1209 isc_socketmgr_t *manager = sock->manager;
1211 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1212 ISC_MSG_DESTROYING, "destroying");
1214 INSIST(ISC_LIST_EMPTY(sock->accept_list));
1215 INSIST(ISC_LIST_EMPTY(sock->recv_list));
1216 INSIST(ISC_LIST_EMPTY(sock->send_list));
1217 INSIST(sock->connect_ev == NULL);
1218 REQUIRE(sock->fd >= 0 && sock->fd < (int)FD_SETSIZE);
1220 LOCK(&manager->lock);
1223 * No one has this socket open, so the watcher doesn't have to be
1224 * poked, and the socket doesn't have to be locked.
1226 manager->fds[sock->fd] = NULL;
1227 manager->fdstate[sock->fd] = CLOSE_PENDING;
1228 select_poke(manager, sock->fd, SELECT_POKE_CLOSE);
1229 ISC_LIST_UNLINK(manager->socklist, sock, link);
1231 #ifdef ISC_PLATFORM_USETHREADS
1232 if (ISC_LIST_EMPTY(manager->socklist))
1233 SIGNAL(&manager->shutdown_ok);
1234 #endif /* ISC_PLATFORM_USETHREADS */
1237 * XXX should reset manager->maxfd here
1240 UNLOCK(&manager->lock);
1246 allocate_socket(isc_socketmgr_t *manager, isc_sockettype_t type,
1247 isc_socket_t **socketp)
1250 isc_result_t result;
1251 ISC_SOCKADDR_LEN_T cmsgbuflen;
1253 sock = isc_mem_get(manager->mctx, sizeof(*sock));
1256 return (ISC_R_NOMEMORY);
1258 result = ISC_R_UNEXPECTED;
1261 sock->references = 0;
1263 sock->manager = manager;
1267 ISC_LINK_INIT(sock, link);
1269 sock->recvcmsgbuf = NULL;
1270 sock->sendcmsgbuf = NULL;
1273 * set up cmsg buffers
1276 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1277 cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
1279 #if defined(USE_CMSG) && defined(SO_TIMESTAMP)
1280 cmsgbuflen += cmsg_space(sizeof(struct timeval));
1282 sock->recvcmsgbuflen = cmsgbuflen;
1283 if (sock->recvcmsgbuflen != 0U) {
1284 sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
1285 if (sock->recvcmsgbuf == NULL)
1290 #if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
1291 cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
1293 sock->sendcmsgbuflen = cmsgbuflen;
1294 if (sock->sendcmsgbuflen != 0U) {
1295 sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
1296 if (sock->sendcmsgbuf == NULL)
1301 * set up list of readers and writers to be initially empty
1303 ISC_LIST_INIT(sock->recv_list);
1304 ISC_LIST_INIT(sock->send_list);
1305 ISC_LIST_INIT(sock->accept_list);
1306 sock->connect_ev = NULL;
1307 sock->pending_recv = 0;
1308 sock->pending_send = 0;
1309 sock->pending_accept = 0;
1311 sock->connected = 0;
1312 sock->connecting = 0;
1316 * initialize the lock
1318 result = isc_mutex_init(&sock->lock);
1319 if (result != ISC_R_SUCCESS) {
1325 * Initialize readable and writable events
1327 ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
1328 ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTR,
1329 NULL, sock, sock, NULL, NULL);
1330 ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
1331 ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTW,
1332 NULL, sock, sock, NULL, NULL);
1334 sock->magic = SOCKET_MAGIC;
1337 return (ISC_R_SUCCESS);
1340 if (sock->recvcmsgbuf != NULL)
1341 isc_mem_put(manager->mctx, sock->recvcmsgbuf,
1342 sock->recvcmsgbuflen);
1343 if (sock->sendcmsgbuf != NULL)
1344 isc_mem_put(manager->mctx, sock->sendcmsgbuf,
1345 sock->sendcmsgbuflen);
1346 isc_mem_put(manager->mctx, sock, sizeof(*sock));
1352 * This event requires that the various lists be empty, that the reference
1353 * count be 1, and that the magic number is valid. The other socket bits,
1354 * like the lock, must be initialized as well. The fd associated must be
1355 * marked as closed, by setting it to -1 on close, or this routine will
1356 * also close the socket.
1359 free_socket(isc_socket_t **socketp) {
1360 isc_socket_t *sock = *socketp;
1362 INSIST(sock->references == 0);
1363 INSIST(VALID_SOCKET(sock));
1364 INSIST(!sock->connecting);
1365 INSIST(!sock->pending_recv);
1366 INSIST(!sock->pending_send);
1367 INSIST(!sock->pending_accept);
1368 INSIST(ISC_LIST_EMPTY(sock->recv_list));
1369 INSIST(ISC_LIST_EMPTY(sock->send_list));
1370 INSIST(ISC_LIST_EMPTY(sock->accept_list));
1371 INSIST(!ISC_LINK_LINKED(sock, link));
1373 if (sock->recvcmsgbuf != NULL)
1374 isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
1375 sock->recvcmsgbuflen);
1376 if (sock->sendcmsgbuf != NULL)
1377 isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
1378 sock->sendcmsgbuflen);
1382 DESTROYLOCK(&sock->lock);
1384 isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
1390 * Create a new 'type' socket managed by 'manager'. Events
1391 * will be posted to 'task' and when dispatched 'action' will be
1392 * called with 'arg' as the arg value. The new socket is returned
1396 isc_socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
1397 isc_socket_t **socketp)
1399 isc_socket_t *sock = NULL;
1400 isc_result_t result;
1401 #if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
1404 #if defined(SO_RCVBUF)
1405 ISC_SOCKADDR_LEN_T optlen;
1408 char strbuf[ISC_STRERRORSIZE];
1409 const char *err = "socket";
1411 REQUIRE(VALID_MANAGER(manager));
1412 REQUIRE(socketp != NULL && *socketp == NULL);
1414 result = allocate_socket(manager, type, &sock);
1415 if (result != ISC_R_SUCCESS)
1420 case isc_sockettype_udp:
1421 sock->fd = socket(pf, SOCK_DGRAM, IPPROTO_UDP);
1423 case isc_sockettype_tcp:
1424 sock->fd = socket(pf, SOCK_STREAM, IPPROTO_TCP);
1426 case isc_sockettype_unix:
1427 sock->fd = socket(pf, SOCK_STREAM, 0);
1433 * Leave a space for stdio to work in.
1435 if (sock->fd >= 0 && sock->fd < 20) {
1437 new = fcntl(sock->fd, F_DUPFD, 20);
1439 (void)close(sock->fd);
1442 err = "isc_socket_create: fcntl";
1446 if (sock->fd >= (int)FD_SETSIZE) {
1447 (void)close(sock->fd);
1448 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
1449 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
1450 isc_msgcat, ISC_MSGSET_SOCKET,
1452 "%s: too many open file descriptors", "socket");
1454 return (ISC_R_NORESOURCES);
1464 return (ISC_R_NORESOURCES);
1466 case EPROTONOSUPPORT:
1470 * Linux 2.2 (and maybe others) return EINVAL instead of
1474 return (ISC_R_FAMILYNOSUPPORT);
1477 isc__strerror(errno, strbuf, sizeof(strbuf));
1478 UNEXPECTED_ERROR(__FILE__, __LINE__,
1480 isc_msgcat_get(isc_msgcat,
1485 return (ISC_R_UNEXPECTED);
1489 if (make_nonblock(sock->fd) != ISC_R_SUCCESS) {
1490 (void)close(sock->fd);
1492 return (ISC_R_UNEXPECTED);
1496 if (type != isc_sockettype_unix &&
1497 setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
1498 (void *)&on, sizeof(on)) < 0) {
1499 isc__strerror(errno, strbuf, sizeof(strbuf));
1500 UNEXPECTED_ERROR(__FILE__, __LINE__,
1501 "setsockopt(%d, SO_BSDCOMPAT) %s: %s",
1503 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
1504 ISC_MSG_FAILED, "failed"),
1510 #if defined(USE_CMSG) || defined(SO_RCVBUF)
1511 if (type == isc_sockettype_udp) {
1513 #if defined(USE_CMSG)
1514 #if defined(SO_TIMESTAMP)
1515 if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
1516 (void *)&on, sizeof(on)) < 0
1517 && errno != ENOPROTOOPT) {
1518 isc__strerror(errno, strbuf, sizeof(strbuf));
1519 UNEXPECTED_ERROR(__FILE__, __LINE__,
1520 "setsockopt(%d, SO_TIMESTAMP) %s: %s",
1522 isc_msgcat_get(isc_msgcat,
1529 #endif /* SO_TIMESTAMP */
1531 #if defined(ISC_PLATFORM_HAVEIPV6)
1532 if (pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
1534 * Warn explicitly because this anomaly can be hidden
1535 * in usual operation (and unexpectedly appear later).
1537 UNEXPECTED_ERROR(__FILE__, __LINE__,
1538 "No buffer available to receive "
1539 "IPv6 destination");
1541 #ifdef ISC_PLATFORM_HAVEIN6PKTINFO
1542 #ifdef IPV6_RECVPKTINFO
1544 if ((pf == AF_INET6)
1545 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1546 (void *)&on, sizeof(on)) < 0)) {
1547 isc__strerror(errno, strbuf, sizeof(strbuf));
1548 UNEXPECTED_ERROR(__FILE__, __LINE__,
1549 "setsockopt(%d, IPV6_RECVPKTINFO) "
1551 isc_msgcat_get(isc_msgcat,
1559 if ((pf == AF_INET6)
1560 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
1561 (void *)&on, sizeof(on)) < 0)) {
1562 isc__strerror(errno, strbuf, sizeof(strbuf));
1563 UNEXPECTED_ERROR(__FILE__, __LINE__,
1564 "setsockopt(%d, IPV6_PKTINFO) %s: %s",
1566 isc_msgcat_get(isc_msgcat,
1572 #endif /* IPV6_RECVPKTINFO */
1573 #endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
1574 #ifdef IPV6_USE_MIN_MTU /*2292bis, not too common yet*/
1575 /* use minimum MTU */
1576 if (pf == AF_INET6) {
1577 (void)setsockopt(sock->fd, IPPROTO_IPV6,
1579 (void *)&on, sizeof(on));
1582 #endif /* ISC_PLATFORM_HAVEIPV6 */
1583 #endif /* defined(USE_CMSG) */
1585 #if defined(SO_RCVBUF)
1586 optlen = sizeof(size);
1587 if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
1588 (void *)&size, &optlen) >= 0 &&
1589 size < RCVBUFSIZE) {
1591 if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
1592 (void *)&size, sizeof(size)) == -1) {
1593 isc__strerror(errno, strbuf, sizeof(strbuf));
1594 UNEXPECTED_ERROR(__FILE__, __LINE__,
1595 "setsockopt(%d, SO_RCVBUF, %d) %s: %s",
1597 isc_msgcat_get(isc_msgcat,
1606 #endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
1608 sock->references = 1;
1611 LOCK(&manager->lock);
1614 * Note we don't have to lock the socket like we normally would because
1615 * there are no external references to it yet.
1618 manager->fds[sock->fd] = sock;
1619 manager->fdstate[sock->fd] = MANAGED;
1620 ISC_LIST_APPEND(manager->socklist, sock, link);
1621 if (manager->maxfd < sock->fd)
1622 manager->maxfd = sock->fd;
1624 UNLOCK(&manager->lock);
1626 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1627 ISC_MSG_CREATED, "created");
1629 return (ISC_R_SUCCESS);
1633 * Attach to a socket. Caller must explicitly detach when it is done.
1636 isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
1637 REQUIRE(VALID_SOCKET(sock));
1638 REQUIRE(socketp != NULL && *socketp == NULL);
1642 UNLOCK(&sock->lock);
1648 * Dereference a socket. If this is the last reference to it, clean things
1649 * up by destroying the socket.
1652 isc_socket_detach(isc_socket_t **socketp) {
1654 isc_boolean_t kill_socket = ISC_FALSE;
1656 REQUIRE(socketp != NULL);
1658 REQUIRE(VALID_SOCKET(sock));
1661 REQUIRE(sock->references > 0);
1663 if (sock->references == 0)
1664 kill_socket = ISC_TRUE;
1665 UNLOCK(&sock->lock);
1674 * I/O is possible on a given socket. Schedule an event to this task that
1675 * will call an internal function to do the I/O. This will charge the
1676 * task with the I/O operation and let our select loop handler get back
1677 * to doing something real as fast as possible.
1679 * The socket and manager must be locked before calling this function.
1682 dispatch_recv(isc_socket_t *sock) {
1684 isc_socketevent_t *ev;
1686 INSIST(!sock->pending_recv);
1688 ev = ISC_LIST_HEAD(sock->recv_list);
1692 sock->pending_recv = 1;
1693 iev = &sock->readable_ev;
1695 socket_log(sock, NULL, EVENT, NULL, 0, 0,
1696 "dispatch_recv: event %p -> task %p", ev, ev->ev_sender);
1699 iev->ev_sender = sock;
1700 iev->ev_action = internal_recv;
1703 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1707 dispatch_send(isc_socket_t *sock) {
1709 isc_socketevent_t *ev;
1711 INSIST(!sock->pending_send);
1713 ev = ISC_LIST_HEAD(sock->send_list);
1717 sock->pending_send = 1;
1718 iev = &sock->writable_ev;
1720 socket_log(sock, NULL, EVENT, NULL, 0, 0,
1721 "dispatch_send: event %p -> task %p", ev, ev->ev_sender);
1724 iev->ev_sender = sock;
1725 iev->ev_action = internal_send;
1728 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1732 * Dispatch an internal accept event.
1735 dispatch_accept(isc_socket_t *sock) {
1737 isc_socket_newconnev_t *ev;
1739 INSIST(!sock->pending_accept);
1742 * Are there any done events left, or were they all canceled
1743 * before the manager got the socket lock?
1745 ev = ISC_LIST_HEAD(sock->accept_list);
1749 sock->pending_accept = 1;
1750 iev = &sock->readable_ev;
1752 sock->references++; /* keep socket around for this internal event */
1753 iev->ev_sender = sock;
1754 iev->ev_action = internal_accept;
1757 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1761 dispatch_connect(isc_socket_t *sock) {
1763 isc_socket_connev_t *ev;
1765 iev = &sock->writable_ev;
1767 ev = sock->connect_ev;
1768 INSIST(ev != NULL); /* XXX */
1770 INSIST(sock->connecting);
1772 sock->references++; /* keep socket around for this internal event */
1773 iev->ev_sender = sock;
1774 iev->ev_action = internal_connect;
1777 isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
1781 * Dequeue an item off the given socket's read queue, set the result code
1782 * in the done event to the one provided, and send it to the task it was
1785 * If the event to be sent is on a list, remove it before sending. If
1786 * asked to, send and detach from the socket as well.
1788 * Caller must have the socket locked if the event is attached to the socket.
1791 send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
1794 task = (*dev)->ev_sender;
1796 (*dev)->ev_sender = sock;
1798 if (ISC_LINK_LINKED(*dev, ev_link))
1799 ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
1801 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
1802 == ISC_SOCKEVENTATTR_ATTACHED)
1803 isc_task_sendanddetach(&task, (isc_event_t **)dev);
1805 isc_task_send(task, (isc_event_t **)dev);
1809 * See comments for send_recvdone_event() above.
1811 * Caller must have the socket locked if the event is attached to the socket.
1814 send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
1817 INSIST(dev != NULL && *dev != NULL);
1819 task = (*dev)->ev_sender;
1820 (*dev)->ev_sender = sock;
1822 if (ISC_LINK_LINKED(*dev, ev_link))
1823 ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
1825 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
1826 == ISC_SOCKEVENTATTR_ATTACHED)
1827 isc_task_sendanddetach(&task, (isc_event_t **)dev);
1829 isc_task_send(task, (isc_event_t **)dev);
1833 * Call accept() on a socket, to get the new file descriptor. The listen
1834 * socket is used as a prototype to create a new isc_socket_t. The new
1835 * socket has one outstanding reference. The task receiving the event
1836 * will be detached from just after the event is delivered.
1838 * On entry to this function, the event delivered is the internal
1839 * readable event, and the first item on the accept_list should be
1840 * the done event we want to send. If the list is empty, this is a no-op,
1841 * so just unlock and return.
1844 internal_accept(isc_task_t *me, isc_event_t *ev) {
1846 isc_socketmgr_t *manager;
1847 isc_socket_newconnev_t *dev;
1849 ISC_SOCKADDR_LEN_T addrlen;
1851 isc_result_t result = ISC_R_SUCCESS;
1852 char strbuf[ISC_STRERRORSIZE];
1853 const char *err = "accept";
1857 sock = ev->ev_sender;
1858 INSIST(VALID_SOCKET(sock));
1861 socket_log(sock, NULL, TRACE,
1862 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
1863 "internal_accept called, locked socket");
1865 manager = sock->manager;
1866 INSIST(VALID_MANAGER(manager));
1868 INSIST(sock->listener);
1869 INSIST(sock->pending_accept == 1);
1870 sock->pending_accept = 0;
1872 INSIST(sock->references > 0);
1873 sock->references--; /* the internal event is done with this socket */
1874 if (sock->references == 0) {
1875 UNLOCK(&sock->lock);
1881 * Get the first item off the accept list.
1882 * If it is empty, unlock the socket and return.
1884 dev = ISC_LIST_HEAD(sock->accept_list);
1886 UNLOCK(&sock->lock);
1891 * Try to accept the new connection. If the accept fails with
1892 * EAGAIN or EINTR, simply poke the watcher to watch this socket
1893 * again. Also ignore ECONNRESET, which has been reported to
1894 * be spuriously returned on Linux 2.2.19 although it is not
1895 * a documented error for accept(). ECONNABORTED has been
1896 * reported for Solaris 8. The rest are thrown in not because
1897 * we have seen them but because they are ignored by other
1898 * deamons such as BIND 8 and Apache.
1901 addrlen = sizeof(dev->newsocket->address.type);
1902 memset(&dev->newsocket->address.type.sa, 0, addrlen);
1903 fd = accept(sock->fd, &dev->newsocket->address.type.sa,
1908 * Leave a space for stdio to work in.
1910 if (fd >= 0 && fd < 20) {
1912 new = fcntl(fd, F_DUPFD, 20);
1922 if (SOFT_ERROR(errno))
1945 isc__strerror(errno, strbuf, sizeof(strbuf));
1946 UNEXPECTED_ERROR(__FILE__, __LINE__,
1947 "internal_accept: %s() %s: %s", err,
1948 isc_msgcat_get(isc_msgcat,
1954 result = ISC_R_UNEXPECTED;
1956 if (addrlen == 0U) {
1957 UNEXPECTED_ERROR(__FILE__, __LINE__,
1958 "internal_accept(): "
1959 "accept() failed to return "
1964 } else if (dev->newsocket->address.type.sa.sa_family !=
1967 UNEXPECTED_ERROR(__FILE__, __LINE__,
1968 "internal_accept(): "
1969 "accept() returned peer address "
1970 "family %u (expected %u)",
1971 dev->newsocket->address.
1976 } else if (fd >= (int)FD_SETSIZE) {
1977 isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
1978 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
1979 isc_msgcat, ISC_MSGSET_SOCKET,
1981 "%s: too many open file descriptors",
1989 dev->newsocket->address.length = addrlen;
1990 dev->newsocket->pf = sock->pf;
1994 * Pull off the done event.
1996 ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
1999 * Poke watcher if there are more pending accepts.
2001 if (!ISC_LIST_EMPTY(sock->accept_list))
2002 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
2004 UNLOCK(&sock->lock);
2006 if (fd != -1 && (make_nonblock(fd) != ISC_R_SUCCESS)) {
2009 result = ISC_R_UNEXPECTED;
2013 * -1 means the new socket didn't happen.
2016 LOCK(&manager->lock);
2017 ISC_LIST_APPEND(manager->socklist, dev->newsocket, link);
2019 dev->newsocket->fd = fd;
2020 dev->newsocket->bound = 1;
2021 dev->newsocket->connected = 1;
2024 * Save away the remote address
2026 dev->address = dev->newsocket->address;
2028 manager->fds[fd] = dev->newsocket;
2029 manager->fdstate[fd] = MANAGED;
2030 if (manager->maxfd < fd)
2031 manager->maxfd = fd;
2033 socket_log(sock, &dev->newsocket->address, CREATION,
2034 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
2035 "accepted connection, new socket %p",
2038 UNLOCK(&manager->lock);
2040 dev->newsocket->references--;
2041 free_socket(&dev->newsocket);
2045 * Fill in the done event details and send it off.
2047 dev->result = result;
2048 task = dev->ev_sender;
2049 dev->ev_sender = sock;
2051 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
2055 select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
2056 UNLOCK(&sock->lock);
2061 internal_recv(isc_task_t *me, isc_event_t *ev) {
2062 isc_socketevent_t *dev;
2065 INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
2067 sock = ev->ev_sender;
2068 INSIST(VALID_SOCKET(sock));
2071 socket_log(sock, NULL, IOEVENT,
2072 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
2073 "internal_recv: task %p got event %p", me, ev);
2075 INSIST(sock->pending_recv == 1);
2076 sock->pending_recv = 0;
2078 INSIST(sock->references > 0);
2079 sock->references--; /* the internal event is done with this socket */
2080 if (sock->references == 0) {
2081 UNLOCK(&sock->lock);
2087 * Try to do as much I/O as possible on this socket. There are no
2088 * limits here, currently.
2090 dev = ISC_LIST_HEAD(sock->recv_list);
2091 while (dev != NULL) {
2092 switch (doio_recv(sock, dev)) {
2098 * read of 0 means the remote end was closed.
2099 * Run through the event queue and dispatch all
2100 * the events with an EOF result code.
2103 dev->result = ISC_R_EOF;
2104 send_recvdone_event(sock, &dev);
2105 dev = ISC_LIST_HEAD(sock->recv_list);
2106 } while (dev != NULL);
2111 send_recvdone_event(sock, &dev);
2115 dev = ISC_LIST_HEAD(sock->recv_list);
2119 if (!ISC_LIST_EMPTY(sock->recv_list))
2120 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2122 UNLOCK(&sock->lock);
2126 internal_send(isc_task_t *me, isc_event_t *ev) {
2127 isc_socketevent_t *dev;
2130 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
2133 * Find out what socket this is and lock it.
2135 sock = (isc_socket_t *)ev->ev_sender;
2136 INSIST(VALID_SOCKET(sock));
2139 socket_log(sock, NULL, IOEVENT,
2140 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
2141 "internal_send: task %p got event %p", me, ev);
2143 INSIST(sock->pending_send == 1);
2144 sock->pending_send = 0;
2146 INSIST(sock->references > 0);
2147 sock->references--; /* the internal event is done with this socket */
2148 if (sock->references == 0) {
2149 UNLOCK(&sock->lock);
2155 * Try to do as much I/O as possible on this socket. There are no
2156 * limits here, currently.
2158 dev = ISC_LIST_HEAD(sock->send_list);
2159 while (dev != NULL) {
2160 switch (doio_send(sock, dev)) {
2166 send_senddone_event(sock, &dev);
2170 dev = ISC_LIST_HEAD(sock->send_list);
2174 if (!ISC_LIST_EMPTY(sock->send_list))
2175 select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
2177 UNLOCK(&sock->lock);
2181 process_fds(isc_socketmgr_t *manager, int maxfd,
2182 fd_set *readfds, fd_set *writefds)
2186 isc_boolean_t unlock_sock;
2188 REQUIRE(maxfd <= (int)FD_SETSIZE);
2191 * Process read/writes on other fds here. Avoid locking
2192 * and unlocking twice if both reads and writes are possible.
2194 for (i = 0; i < maxfd; i++) {
2195 #ifdef ISC_PLATFORM_USETHREADS
2196 if (i == manager->pipe_fds[0] || i == manager->pipe_fds[1])
2198 #endif /* ISC_PLATFORM_USETHREADS */
2200 if (manager->fdstate[i] == CLOSE_PENDING) {
2201 manager->fdstate[i] = CLOSED;
2202 FD_CLR(i, &manager->read_fds);
2203 FD_CLR(i, &manager->write_fds);
2210 sock = manager->fds[i];
2211 unlock_sock = ISC_FALSE;
2212 if (FD_ISSET(i, readfds)) {
2214 FD_CLR(i, &manager->read_fds);
2217 unlock_sock = ISC_TRUE;
2219 if (!SOCK_DEAD(sock)) {
2221 dispatch_accept(sock);
2223 dispatch_recv(sock);
2225 FD_CLR(i, &manager->read_fds);
2228 if (FD_ISSET(i, writefds)) {
2230 FD_CLR(i, &manager->write_fds);
2234 unlock_sock = ISC_TRUE;
2237 if (!SOCK_DEAD(sock)) {
2238 if (sock->connecting)
2239 dispatch_connect(sock);
2241 dispatch_send(sock);
2243 FD_CLR(i, &manager->write_fds);
2246 UNLOCK(&sock->lock);
2250 #ifdef ISC_PLATFORM_USETHREADS
2252 * This is the thread that will loop forever, always in a select or poll
2255 * When select returns something to do, track down what thread gets to do
2256 * this I/O and post the event to it.
2258 static isc_threadresult_t
2259 watcher(void *uap) {
2260 isc_socketmgr_t *manager = uap;
2268 char strbuf[ISC_STRERRORSIZE];
2271 * Get the control fd here. This will never change.
2273 LOCK(&manager->lock);
2274 ctlfd = manager->pipe_fds[0];
2279 readfds = manager->read_fds;
2280 writefds = manager->write_fds;
2281 maxfd = manager->maxfd + 1;
2283 UNLOCK(&manager->lock);
2285 cc = select(maxfd, &readfds, &writefds, NULL, NULL);
2287 if (!SOFT_ERROR(errno)) {
2288 isc__strerror(errno, strbuf,
2290 FATAL_ERROR(__FILE__, __LINE__,
2292 isc_msgcat_get(isc_msgcat,
2300 LOCK(&manager->lock);
2305 * Process reads on internal, control fd.
2307 if (FD_ISSET(ctlfd, &readfds)) {
2309 select_readmsg(manager, &fd, &msg);
2311 manager_log(manager, IOEVENT,
2312 isc_msgcat_get(isc_msgcat,
2315 "watcher got message %d"),
2321 if (msg == SELECT_POKE_NOTHING)
2325 * Handle shutdown message. We really should
2326 * jump out of this loop right away, but
2327 * it doesn't matter if we have to do a little
2330 if (msg == SELECT_POKE_SHUTDOWN) {
2337 * This is a wakeup on a socket. Look
2338 * at the event queue for both read and write,
2339 * and decide if we need to watch on it now
2342 wakeup_socket(manager, fd, msg);
2346 process_fds(manager, maxfd, &readfds, &writefds);
2349 manager_log(manager, TRACE,
2350 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2351 ISC_MSG_EXITING, "watcher exiting"));
2353 UNLOCK(&manager->lock);
2354 return ((isc_threadresult_t)0);
2356 #endif /* ISC_PLATFORM_USETHREADS */
2359 * Create a new socket manager.
2362 isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
2363 isc_socketmgr_t *manager;
2364 #ifdef ISC_PLATFORM_USETHREADS
2365 char strbuf[ISC_STRERRORSIZE];
2367 isc_result_t result;
2369 REQUIRE(managerp != NULL && *managerp == NULL);
2371 #ifndef ISC_PLATFORM_USETHREADS
2372 if (socketmgr != NULL) {
2374 *managerp = socketmgr;
2375 return (ISC_R_SUCCESS);
2377 #endif /* ISC_PLATFORM_USETHREADS */
2379 manager = isc_mem_get(mctx, sizeof(*manager));
2380 if (manager == NULL)
2381 return (ISC_R_NOMEMORY);
2383 manager->magic = SOCKET_MANAGER_MAGIC;
2384 manager->mctx = NULL;
2385 memset(manager->fds, 0, sizeof(manager->fds));
2386 ISC_LIST_INIT(manager->socklist);
2387 result = isc_mutex_init(&manager->lock);
2388 if (result != ISC_R_SUCCESS) {
2389 isc_mem_put(mctx, manager, sizeof(*manager));
2392 #ifdef ISC_PLATFORM_USETHREADS
2393 if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
2394 DESTROYLOCK(&manager->lock);
2395 isc_mem_put(mctx, manager, sizeof(*manager));
2396 UNEXPECTED_ERROR(__FILE__, __LINE__,
2397 "isc_condition_init() %s",
2398 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2399 ISC_MSG_FAILED, "failed"));
2400 return (ISC_R_UNEXPECTED);
2404 * Create the special fds that will be used to wake up the
2405 * select/poll loop when something internal needs to be done.
2407 if (pipe(manager->pipe_fds) != 0) {
2408 DESTROYLOCK(&manager->lock);
2409 isc_mem_put(mctx, manager, sizeof(*manager));
2410 isc__strerror(errno, strbuf, sizeof(strbuf));
2411 UNEXPECTED_ERROR(__FILE__, __LINE__,
2413 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2414 ISC_MSG_FAILED, "failed"),
2417 return (ISC_R_UNEXPECTED);
2420 RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
2422 RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
2424 #else /* ISC_PLATFORM_USETHREADS */
2426 #endif /* ISC_PLATFORM_USETHREADS */
2429 * Set up initial state for the select loop
2431 FD_ZERO(&manager->read_fds);
2432 FD_ZERO(&manager->write_fds);
2433 #ifdef ISC_PLATFORM_USETHREADS
2434 FD_SET(manager->pipe_fds[0], &manager->read_fds);
2435 manager->maxfd = manager->pipe_fds[0];
2436 #else /* ISC_PLATFORM_USETHREADS */
2438 #endif /* ISC_PLATFORM_USETHREADS */
2439 memset(manager->fdstate, 0, sizeof(manager->fdstate));
2441 #ifdef ISC_PLATFORM_USETHREADS
2443 * Start up the select/poll thread.
2445 if (isc_thread_create(watcher, manager, &manager->watcher) !=
2447 (void)close(manager->pipe_fds[0]);
2448 (void)close(manager->pipe_fds[1]);
2449 DESTROYLOCK(&manager->lock);
2450 isc_mem_put(mctx, manager, sizeof(*manager));
2451 UNEXPECTED_ERROR(__FILE__, __LINE__,
2452 "isc_thread_create() %s",
2453 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2454 ISC_MSG_FAILED, "failed"));
2455 return (ISC_R_UNEXPECTED);
2457 #endif /* ISC_PLATFORM_USETHREADS */
2458 isc_mem_attach(mctx, &manager->mctx);
2460 #ifndef ISC_PLATFORM_USETHREADS
2461 socketmgr = manager;
2462 #endif /* ISC_PLATFORM_USETHREADS */
2463 *managerp = manager;
2465 return (ISC_R_SUCCESS);
2469 isc_socketmgr_destroy(isc_socketmgr_t **managerp) {
2470 isc_socketmgr_t *manager;
2475 * Destroy a socket manager.
2478 REQUIRE(managerp != NULL);
2479 manager = *managerp;
2480 REQUIRE(VALID_MANAGER(manager));
2482 #ifndef ISC_PLATFORM_USETHREADS
2483 if (manager->refs > 1) {
2488 #endif /* ISC_PLATFORM_USETHREADS */
2490 LOCK(&manager->lock);
2492 #ifdef ISC_PLATFORM_USETHREADS
2494 * Wait for all sockets to be destroyed.
2496 while (!ISC_LIST_EMPTY(manager->socklist)) {
2497 manager_log(manager, CREATION,
2498 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
2499 ISC_MSG_SOCKETSREMAIN,
2501 WAIT(&manager->shutdown_ok, &manager->lock);
2503 #else /* ISC_PLATFORM_USETHREADS */
2505 * Hope all sockets have been destroyed.
2507 if (!ISC_LIST_EMPTY(manager->socklist)) {
2508 manager_log(manager, CREATION,
2509 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
2510 ISC_MSG_SOCKETSREMAIN,
2514 #endif /* ISC_PLATFORM_USETHREADS */
2516 UNLOCK(&manager->lock);
2519 * Here, poke our select/poll thread. Do this by closing the write
2520 * half of the pipe, which will send EOF to the read half.
2521 * This is currently a no-op in the non-threaded case.
2523 select_poke(manager, 0, SELECT_POKE_SHUTDOWN);
2525 #ifdef ISC_PLATFORM_USETHREADS
2527 * Wait for thread to exit.
2529 if (isc_thread_join(manager->watcher, NULL) != ISC_R_SUCCESS)
2530 UNEXPECTED_ERROR(__FILE__, __LINE__,
2531 "isc_thread_join() %s",
2532 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2533 ISC_MSG_FAILED, "failed"));
2534 #endif /* ISC_PLATFORM_USETHREADS */
2539 #ifdef ISC_PLATFORM_USETHREADS
2540 (void)close(manager->pipe_fds[0]);
2541 (void)close(manager->pipe_fds[1]);
2542 (void)isc_condition_destroy(&manager->shutdown_ok);
2543 #endif /* ISC_PLATFORM_USETHREADS */
2545 for (i = 0; i < (int)FD_SETSIZE; i++)
2546 if (manager->fdstate[i] == CLOSE_PENDING)
2549 DESTROYLOCK(&manager->lock);
2551 mctx= manager->mctx;
2552 isc_mem_put(mctx, manager, sizeof(*manager));
2554 isc_mem_detach(&mctx);
2560 socket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
2564 isc_boolean_t have_lock = ISC_FALSE;
2565 isc_task_t *ntask = NULL;
2566 isc_result_t result = ISC_R_SUCCESS;
2568 dev->ev_sender = task;
2570 if (sock->type == isc_sockettype_udp) {
2571 io_state = doio_recv(sock, dev);
2574 have_lock = ISC_TRUE;
2576 if (ISC_LIST_EMPTY(sock->recv_list))
2577 io_state = doio_recv(sock, dev);
2579 io_state = DOIO_SOFT;
2585 * We couldn't read all or part of the request right now, so
2588 * Attach to socket and to task
2590 isc_task_attach(task, &ntask);
2591 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
2595 have_lock = ISC_TRUE;
2599 * Enqueue the request. If the socket was previously not being
2600 * watched, poke the watcher to start paying attention to it.
2602 if (ISC_LIST_EMPTY(sock->recv_list))
2603 select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
2604 ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
2606 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2607 "socket_recv: event %p -> task %p",
2610 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
2611 result = ISC_R_INPROGRESS;
2615 dev->result = ISC_R_EOF;
2620 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
2621 send_recvdone_event(sock, &dev);
2626 UNLOCK(&sock->lock);
2632 isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
2633 unsigned int minimum, isc_task_t *task,
2634 isc_taskaction_t action, const void *arg)
2636 isc_socketevent_t *dev;
2637 isc_socketmgr_t *manager;
2638 unsigned int iocount;
2639 isc_buffer_t *buffer;
2641 REQUIRE(VALID_SOCKET(sock));
2642 REQUIRE(buflist != NULL);
2643 REQUIRE(!ISC_LIST_EMPTY(*buflist));
2644 REQUIRE(task != NULL);
2645 REQUIRE(action != NULL);
2647 manager = sock->manager;
2648 REQUIRE(VALID_MANAGER(manager));
2650 iocount = isc_bufferlist_availablecount(buflist);
2651 REQUIRE(iocount > 0);
2653 INSIST(sock->bound);
2655 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
2657 return (ISC_R_NOMEMORY);
2661 * UDP sockets are always partial read
2663 if (sock->type == isc_sockettype_udp)
2667 dev->minimum = iocount;
2669 dev->minimum = minimum;
2673 * Move each buffer from the passed in list to our internal one.
2675 buffer = ISC_LIST_HEAD(*buflist);
2676 while (buffer != NULL) {
2677 ISC_LIST_DEQUEUE(*buflist, buffer, link);
2678 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
2679 buffer = ISC_LIST_HEAD(*buflist);
2682 return (socket_recv(sock, dev, task, 0));
2686 isc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
2687 isc_task_t *task, isc_taskaction_t action, const void *arg)
2689 isc_socketevent_t *dev;
2690 isc_socketmgr_t *manager;
2692 REQUIRE(VALID_SOCKET(sock));
2693 REQUIRE(action != NULL);
2695 manager = sock->manager;
2696 REQUIRE(VALID_MANAGER(manager));
2698 INSIST(sock->bound);
2700 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
2702 return (ISC_R_NOMEMORY);
2704 return (isc_socket_recv2(sock, region, minimum, task, dev, 0));
2708 isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
2709 unsigned int minimum, isc_task_t *task,
2710 isc_socketevent_t *event, unsigned int flags)
2712 event->ev_sender = sock;
2713 event->result = ISC_R_UNEXPECTED;
2714 ISC_LIST_INIT(event->bufferlist);
2715 event->region = *region;
2718 event->attributes = 0;
2721 * UDP sockets are always partial read.
2723 if (sock->type == isc_sockettype_udp)
2727 event->minimum = region->length;
2729 event->minimum = minimum;
2732 return (socket_recv(sock, event, task, flags));
2736 socket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
2737 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
2741 isc_boolean_t have_lock = ISC_FALSE;
2742 isc_task_t *ntask = NULL;
2743 isc_result_t result = ISC_R_SUCCESS;
2745 dev->ev_sender = task;
2747 set_dev_address(address, sock, dev);
2748 if (pktinfo != NULL) {
2749 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
2750 dev->pktinfo = *pktinfo;
2752 if (!isc_sockaddr_issitelocal(&dev->address) &&
2753 !isc_sockaddr_islinklocal(&dev->address)) {
2754 socket_log(sock, NULL, TRACE, isc_msgcat,
2755 ISC_MSGSET_SOCKET, ISC_MSG_PKTINFOPROVIDED,
2756 "pktinfo structure provided, ifindex %u "
2757 "(set to 0)", pktinfo->ipi6_ifindex);
2760 * Set the pktinfo index to 0 here, to let the
2761 * kernel decide what interface it should send on.
2763 dev->pktinfo.ipi6_ifindex = 0;
2767 if (sock->type == isc_sockettype_udp)
2768 io_state = doio_send(sock, dev);
2771 have_lock = ISC_TRUE;
2773 if (ISC_LIST_EMPTY(sock->send_list))
2774 io_state = doio_send(sock, dev);
2776 io_state = DOIO_SOFT;
2782 * We couldn't send all or part of the request right now, so
2783 * queue it unless ISC_SOCKFLAG_NORETRY is set.
2785 if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
2786 isc_task_attach(task, &ntask);
2787 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
2791 have_lock = ISC_TRUE;
2795 * Enqueue the request. If the socket was previously
2796 * not being watched, poke the watcher to start
2797 * paying attention to it.
2799 if (ISC_LIST_EMPTY(sock->send_list))
2800 select_poke(sock->manager, sock->fd,
2802 ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
2804 socket_log(sock, NULL, EVENT, NULL, 0, 0,
2805 "socket_send: event %p -> task %p",
2808 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
2809 result = ISC_R_INPROGRESS;
2815 if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
2816 send_senddone_event(sock, &dev);
2821 UNLOCK(&sock->lock);
2827 isc_socket_send(isc_socket_t *sock, isc_region_t *region,
2828 isc_task_t *task, isc_taskaction_t action, const void *arg)
2831 * REQUIRE() checking is performed in isc_socket_sendto().
2833 return (isc_socket_sendto(sock, region, task, action, arg, NULL,
2838 isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
2839 isc_task_t *task, isc_taskaction_t action, const void *arg,
2840 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
2842 isc_socketevent_t *dev;
2843 isc_socketmgr_t *manager;
2845 REQUIRE(VALID_SOCKET(sock));
2846 REQUIRE(region != NULL);
2847 REQUIRE(task != NULL);
2848 REQUIRE(action != NULL);
2850 manager = sock->manager;
2851 REQUIRE(VALID_MANAGER(manager));
2853 INSIST(sock->bound);
2855 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
2857 return (ISC_R_NOMEMORY);
2860 dev->region = *region;
2862 return (socket_send(sock, dev, task, address, pktinfo, 0));
2866 isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
2867 isc_task_t *task, isc_taskaction_t action, const void *arg)
2869 return (isc_socket_sendtov(sock, buflist, task, action, arg, NULL,
2874 isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
2875 isc_task_t *task, isc_taskaction_t action, const void *arg,
2876 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
2878 isc_socketevent_t *dev;
2879 isc_socketmgr_t *manager;
2880 unsigned int iocount;
2881 isc_buffer_t *buffer;
2883 REQUIRE(VALID_SOCKET(sock));
2884 REQUIRE(buflist != NULL);
2885 REQUIRE(!ISC_LIST_EMPTY(*buflist));
2886 REQUIRE(task != NULL);
2887 REQUIRE(action != NULL);
2889 manager = sock->manager;
2890 REQUIRE(VALID_MANAGER(manager));
2892 iocount = isc_bufferlist_usedcount(buflist);
2893 REQUIRE(iocount > 0);
2895 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
2897 return (ISC_R_NOMEMORY);
2901 * Move each buffer from the passed in list to our internal one.
2903 buffer = ISC_LIST_HEAD(*buflist);
2904 while (buffer != NULL) {
2905 ISC_LIST_DEQUEUE(*buflist, buffer, link);
2906 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
2907 buffer = ISC_LIST_HEAD(*buflist);
2910 return (socket_send(sock, dev, task, address, pktinfo, 0));
2914 isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
2916 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
2917 isc_socketevent_t *event, unsigned int flags)
2919 REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
2920 if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
2921 REQUIRE(sock->type == isc_sockettype_udp);
2922 event->ev_sender = sock;
2923 event->result = ISC_R_UNEXPECTED;
2924 ISC_LIST_INIT(event->bufferlist);
2925 event->region = *region;
2928 event->attributes = 0;
2930 return (socket_send(sock, event, task, address, pktinfo, flags));
2934 isc_socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
2935 #ifdef ISC_PLATFORM_HAVESYSUNH
2938 char strbuf[ISC_STRERRORSIZE];
2940 if (sockaddr->type.sa.sa_family != AF_UNIX)
2944 #if defined(S_IFMT) && defined(S_IFSOCK)
2945 #define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
2946 #elif defined(_S_IFMT) && defined(S_IFSOCK)
2947 #define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
2952 #if defined(S_IFMT) && defined(S_IFIFO)
2953 #define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
2954 #elif defined(_S_IFMT) && defined(S_IFIFO)
2955 #define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
2959 #if !defined(S_ISFIFO) && !defined(S_ISSOCK)
2960 #error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
2964 #define S_ISFIFO(mode) 0
2968 #define S_ISSOCK(mode) 0
2972 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
2973 isc__strerror(errno, strbuf, sizeof(strbuf));
2974 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2975 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2976 "isc_socket_cleanunix: stat(%s): %s",
2977 sockaddr->type.sunix.sun_path, strbuf);
2980 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
2981 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2982 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2983 "isc_socket_cleanunix: %s: not a socket",
2984 sockaddr->type.sunix.sun_path);
2987 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
2988 isc__strerror(errno, strbuf, sizeof(strbuf));
2989 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
2990 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
2991 "isc_socket_cleanunix: unlink(%s): %s",
2992 sockaddr->type.sunix.sun_path, strbuf);
2997 s = socket(AF_UNIX, SOCK_STREAM, 0);
2999 isc__strerror(errno, strbuf, sizeof(strbuf));
3000 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3001 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3002 "isc_socket_cleanunix: socket(%s): %s",
3003 sockaddr->type.sunix.sun_path, strbuf);
3007 if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
3009 case ENOENT: /* We exited cleanly last time */
3012 isc__strerror(errno, strbuf, sizeof(strbuf));
3013 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3014 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3015 "isc_socket_cleanunix: stat(%s): %s",
3016 sockaddr->type.sunix.sun_path, strbuf);
3022 if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
3023 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3024 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3025 "isc_socket_cleanunix: %s: not a socket",
3026 sockaddr->type.sunix.sun_path);
3030 if (connect(s, (struct sockaddr *)&sockaddr->type.sunix,
3031 sizeof(sockaddr->type.sunix)) < 0) {
3035 if (unlink(sockaddr->type.sunix.sun_path) < 0) {
3036 isc__strerror(errno, strbuf, sizeof(strbuf));
3037 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3038 ISC_LOGMODULE_SOCKET,
3040 "isc_socket_cleanunix: "
3042 sockaddr->type.sunix.sun_path,
3047 isc__strerror(errno, strbuf, sizeof(strbuf));
3048 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3049 ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
3050 "isc_socket_cleanunix: connect(%s): %s",
3051 sockaddr->type.sunix.sun_path, strbuf);
3064 isc_socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
3065 isc_uint32_t owner, isc_uint32_t group)
3067 #ifdef ISC_PLATFORM_HAVESYSUNH
3068 isc_result_t result = ISC_R_SUCCESS;
3069 char strbuf[ISC_STRERRORSIZE];
3070 char path[sizeof(sockaddr->type.sunix.sun_path)];
3071 #ifdef NEED_SECURE_DIRECTORY
3075 REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
3076 INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
3077 strcpy(path, sockaddr->type.sunix.sun_path);
3079 #ifdef NEED_SECURE_DIRECTORY
3080 slash = strrchr(path, '/');
3081 if (slash != NULL) {
3090 if (chmod(path, perm) < 0) {
3091 isc__strerror(errno, strbuf, sizeof(strbuf));
3092 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3093 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3094 "isc_socket_permunix: chmod(%s, %d): %s",
3095 path, perm, strbuf);
3096 result = ISC_R_FAILURE;
3098 if (chown(path, owner, group) < 0) {
3099 isc__strerror(errno, strbuf, sizeof(strbuf));
3100 isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
3101 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
3102 "isc_socket_permunix: chown(%s, %d, %d): %s",
3105 result = ISC_R_FAILURE;
3113 return (ISC_R_NOTIMPLEMENTED);
3118 isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr) {
3119 char strbuf[ISC_STRERRORSIZE];
3124 INSIST(!sock->bound);
3126 if (sock->pf != sockaddr->type.sa.sa_family) {
3127 UNLOCK(&sock->lock);
3128 return (ISC_R_FAMILYMISMATCH);
3131 * Only set SO_REUSEADDR when we want a specific port.
3134 if (sock->pf == AF_UNIX)
3137 if (isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
3138 setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
3140 UNEXPECTED_ERROR(__FILE__, __LINE__,
3141 "setsockopt(%d) %s", sock->fd,
3142 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3143 ISC_MSG_FAILED, "failed"));
3149 if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
3150 UNLOCK(&sock->lock);
3153 return (ISC_R_NOPERM);
3155 return (ISC_R_ADDRNOTAVAIL);
3157 return (ISC_R_ADDRINUSE);
3159 return (ISC_R_BOUND);
3161 isc__strerror(errno, strbuf, sizeof(strbuf));
3162 UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
3164 return (ISC_R_UNEXPECTED);
3168 socket_log(sock, sockaddr, TRACE,
3169 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
3172 UNLOCK(&sock->lock);
3173 return (ISC_R_SUCCESS);
3177 isc_socket_filter(isc_socket_t *sock, const char *filter) {
3178 #ifdef SO_ACCEPTFILTER
3179 char strbuf[ISC_STRERRORSIZE];
3180 struct accept_filter_arg afa;
3186 REQUIRE(VALID_SOCKET(sock));
3188 #ifdef SO_ACCEPTFILTER
3189 bzero(&afa, sizeof(afa));
3190 strncpy(afa.af_name, filter, sizeof(afa.af_name));
3191 if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
3192 &afa, sizeof(afa)) == -1) {
3193 isc__strerror(errno, strbuf, sizeof(strbuf));
3194 socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
3195 ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
3197 return (ISC_R_FAILURE);
3199 return (ISC_R_SUCCESS);
3201 return (ISC_R_NOTIMPLEMENTED);
3206 * Set up to listen on a given socket. We do this by creating an internal
3207 * event that will be dispatched when the socket has read activity. The
3208 * watcher will send the internal event to the task when there is a new
3211 * Unlike in read, we don't preallocate a done event here. Every time there
3212 * is a new connection we'll have to allocate a new one anyway, so we might
3213 * as well keep things simple rather than having to track them.
3216 isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
3217 char strbuf[ISC_STRERRORSIZE];
3219 REQUIRE(VALID_SOCKET(sock));
3223 REQUIRE(!sock->listener);
3224 REQUIRE(sock->bound);
3225 REQUIRE(sock->type == isc_sockettype_tcp ||
3226 sock->type == isc_sockettype_unix);
3229 backlog = SOMAXCONN;
3231 if (listen(sock->fd, (int)backlog) < 0) {
3232 UNLOCK(&sock->lock);
3233 isc__strerror(errno, strbuf, sizeof(strbuf));
3235 UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
3237 return (ISC_R_UNEXPECTED);
3242 UNLOCK(&sock->lock);
3243 return (ISC_R_SUCCESS);
3247 * This should try to do agressive accept() XXXMLG
3250 isc_socket_accept(isc_socket_t *sock,
3251 isc_task_t *task, isc_taskaction_t action, const void *arg)
3253 isc_socket_newconnev_t *dev;
3254 isc_socketmgr_t *manager;
3255 isc_task_t *ntask = NULL;
3256 isc_socket_t *nsock;
3257 isc_result_t result;
3258 isc_boolean_t do_poke = ISC_FALSE;
3260 REQUIRE(VALID_SOCKET(sock));
3261 manager = sock->manager;
3262 REQUIRE(VALID_MANAGER(manager));
3266 REQUIRE(sock->listener);
3269 * Sender field is overloaded here with the task we will be sending
3270 * this event to. Just before the actual event is delivered the
3271 * actual ev_sender will be touched up to be the socket.
3273 dev = (isc_socket_newconnev_t *)
3274 isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
3275 action, arg, sizeof(*dev));
3277 UNLOCK(&sock->lock);
3278 return (ISC_R_NOMEMORY);
3280 ISC_LINK_INIT(dev, ev_link);
3282 result = allocate_socket(manager, sock->type, &nsock);
3283 if (result != ISC_R_SUCCESS) {
3284 isc_event_free(ISC_EVENT_PTR(&dev));
3285 UNLOCK(&sock->lock);
3290 * Attach to socket and to task.
3292 isc_task_attach(task, &ntask);
3293 nsock->references++;
3295 dev->ev_sender = ntask;
3296 dev->newsocket = nsock;
3299 * Poke watcher here. We still have the socket locked, so there
3300 * is no race condition. We will keep the lock for such a short
3301 * bit of time waking it up now or later won't matter all that much.
3303 if (ISC_LIST_EMPTY(sock->accept_list))
3306 ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
3309 select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
3311 UNLOCK(&sock->lock);
3312 return (ISC_R_SUCCESS);
3316 isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
3317 isc_task_t *task, isc_taskaction_t action, const void *arg)
3319 isc_socket_connev_t *dev;
3320 isc_task_t *ntask = NULL;
3321 isc_socketmgr_t *manager;
3323 char strbuf[ISC_STRERRORSIZE];
3325 REQUIRE(VALID_SOCKET(sock));
3326 REQUIRE(addr != NULL);
3327 REQUIRE(task != NULL);
3328 REQUIRE(action != NULL);
3330 manager = sock->manager;
3331 REQUIRE(VALID_MANAGER(manager));
3332 REQUIRE(addr != NULL);
3334 if (isc_sockaddr_ismulticast(addr))
3335 return (ISC_R_MULTICAST);
3339 REQUIRE(!sock->connecting);
3341 dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
3342 ISC_SOCKEVENT_CONNECT,
3346 UNLOCK(&sock->lock);
3347 return (ISC_R_NOMEMORY);
3349 ISC_LINK_INIT(dev, ev_link);
3352 * Try to do the connect right away, as there can be only one
3353 * outstanding, and it might happen to complete.
3355 sock->address = *addr;
3356 cc = connect(sock->fd, &addr->type.sa, addr->length);
3358 if (SOFT_ERROR(errno) || errno == EINPROGRESS)
3362 #define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
3363 ERROR_MATCH(EACCES, ISC_R_NOPERM);
3364 ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
3365 ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
3366 ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
3367 ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
3369 ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
3371 ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
3372 ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
3373 ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
3374 ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
3375 ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
3379 sock->connected = 0;
3381 isc__strerror(errno, strbuf, sizeof(strbuf));
3382 UNEXPECTED_ERROR(__FILE__, __LINE__, "%d/%s", errno, strbuf);
3384 UNLOCK(&sock->lock);
3385 isc_event_free(ISC_EVENT_PTR(&dev));
3386 return (ISC_R_UNEXPECTED);
3389 sock->connected = 0;
3390 isc_task_send(task, ISC_EVENT_PTR(&dev));
3392 UNLOCK(&sock->lock);
3393 return (ISC_R_SUCCESS);
3397 * If connect completed, fire off the done event.
3400 sock->connected = 1;
3402 dev->result = ISC_R_SUCCESS;
3403 isc_task_send(task, ISC_EVENT_PTR(&dev));
3405 UNLOCK(&sock->lock);
3406 return (ISC_R_SUCCESS);
3414 isc_task_attach(task, &ntask);
3416 sock->connecting = 1;
3418 dev->ev_sender = ntask;
3421 * Poke watcher here. We still have the socket locked, so there
3422 * is no race condition. We will keep the lock for such a short
3423 * bit of time waking it up now or later won't matter all that much.
3425 if (sock->connect_ev == NULL)
3426 select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
3428 sock->connect_ev = dev;
3430 UNLOCK(&sock->lock);
3431 return (ISC_R_SUCCESS);
3435 * Called when a socket with a pending connect() finishes.
3438 internal_connect(isc_task_t *me, isc_event_t *ev) {
3440 isc_socket_connev_t *dev;
3443 ISC_SOCKADDR_LEN_T optlen;
3444 char strbuf[ISC_STRERRORSIZE];
3445 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
3448 INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
3450 sock = ev->ev_sender;
3451 INSIST(VALID_SOCKET(sock));
3456 * When the internal event was sent the reference count was bumped
3457 * to keep the socket around for us. Decrement the count here.
3459 INSIST(sock->references > 0);
3461 if (sock->references == 0) {
3462 UNLOCK(&sock->lock);
3468 * Has this event been canceled?
3470 dev = sock->connect_ev;
3472 INSIST(!sock->connecting);
3473 UNLOCK(&sock->lock);
3477 INSIST(sock->connecting);
3478 sock->connecting = 0;
3481 * Get any possible error status here.
3483 optlen = sizeof(cc);
3484 if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
3485 (void *)&cc, (void *)&optlen) < 0)
3492 * If the error is EAGAIN, just re-select on this
3493 * fd and pretend nothing strange happened.
3495 if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
3496 sock->connecting = 1;
3497 select_poke(sock->manager, sock->fd,
3498 SELECT_POKE_CONNECT);
3499 UNLOCK(&sock->lock);
3505 * Translate other errors into ISC_R_* flavors.
3508 #define ERROR_MATCH(a, b) case a: dev->result = b; break;
3509 ERROR_MATCH(EACCES, ISC_R_NOPERM);
3510 ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
3511 ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
3512 ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
3513 ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
3515 ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
3517 ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
3518 ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
3519 ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
3520 ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
3521 ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
3522 ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
3525 dev->result = ISC_R_UNEXPECTED;
3526 isc_sockaddr_format(&sock->address, peerbuf,
3528 isc__strerror(errno, strbuf, sizeof(strbuf));
3529 UNEXPECTED_ERROR(__FILE__, __LINE__,
3530 "internal_connect: connect(%s) %s",
3534 dev->result = ISC_R_SUCCESS;
3535 sock->connected = 1;
3539 sock->connect_ev = NULL;
3541 UNLOCK(&sock->lock);
3543 task = dev->ev_sender;
3544 dev->ev_sender = sock;
3545 isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
3549 isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
3550 isc_result_t result;
3552 REQUIRE(VALID_SOCKET(sock));
3553 REQUIRE(addressp != NULL);
3557 if (sock->connected) {
3558 *addressp = sock->address;
3559 result = ISC_R_SUCCESS;
3561 result = ISC_R_NOTCONNECTED;
3564 UNLOCK(&sock->lock);
3570 isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
3571 ISC_SOCKADDR_LEN_T len;
3572 isc_result_t result;
3573 char strbuf[ISC_STRERRORSIZE];
3575 REQUIRE(VALID_SOCKET(sock));
3576 REQUIRE(addressp != NULL);
3581 result = ISC_R_NOTBOUND;
3585 result = ISC_R_SUCCESS;
3587 len = sizeof(addressp->type);
3588 if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
3589 isc__strerror(errno, strbuf, sizeof(strbuf));
3590 UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
3592 result = ISC_R_UNEXPECTED;
3595 addressp->length = (unsigned int)len;
3598 UNLOCK(&sock->lock);
3604 * Run through the list of events on this socket, and cancel the ones
3605 * queued for task "task" of type "how". "how" is a bitmask.
3608 isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
3610 REQUIRE(VALID_SOCKET(sock));
3613 * Quick exit if there is nothing to do. Don't even bother locking
3622 * All of these do the same thing, more or less.
3624 * o If the internal event is marked as "posted" try to
3625 * remove it from the task's queue. If this fails, mark it
3626 * as canceled instead, and let the task clean it up later.
3627 * o For each I/O request for that task of that type, post
3628 * its done event with status of "ISC_R_CANCELED".
3629 * o Reset any state needed.
3631 if (((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV)
3632 && !ISC_LIST_EMPTY(sock->recv_list)) {
3633 isc_socketevent_t *dev;
3634 isc_socketevent_t *next;
3635 isc_task_t *current_task;
3637 dev = ISC_LIST_HEAD(sock->recv_list);
3639 while (dev != NULL) {
3640 current_task = dev->ev_sender;
3641 next = ISC_LIST_NEXT(dev, ev_link);
3643 if ((task == NULL) || (task == current_task)) {
3644 dev->result = ISC_R_CANCELED;
3645 send_recvdone_event(sock, &dev);
3651 if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
3652 && !ISC_LIST_EMPTY(sock->send_list)) {
3653 isc_socketevent_t *dev;
3654 isc_socketevent_t *next;
3655 isc_task_t *current_task;
3657 dev = ISC_LIST_HEAD(sock->send_list);
3659 while (dev != NULL) {
3660 current_task = dev->ev_sender;
3661 next = ISC_LIST_NEXT(dev, ev_link);
3663 if ((task == NULL) || (task == current_task)) {
3664 dev->result = ISC_R_CANCELED;
3665 send_senddone_event(sock, &dev);
3671 if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
3672 && !ISC_LIST_EMPTY(sock->accept_list)) {
3673 isc_socket_newconnev_t *dev;
3674 isc_socket_newconnev_t *next;
3675 isc_task_t *current_task;
3677 dev = ISC_LIST_HEAD(sock->accept_list);
3678 while (dev != NULL) {
3679 current_task = dev->ev_sender;
3680 next = ISC_LIST_NEXT(dev, ev_link);
3682 if ((task == NULL) || (task == current_task)) {
3684 ISC_LIST_UNLINK(sock->accept_list, dev,
3687 dev->newsocket->references--;
3688 free_socket(&dev->newsocket);
3690 dev->result = ISC_R_CANCELED;
3691 dev->ev_sender = sock;
3692 isc_task_sendanddetach(¤t_task,
3693 ISC_EVENT_PTR(&dev));
3701 * Connecting is not a list.
3703 if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
3704 && sock->connect_ev != NULL) {
3705 isc_socket_connev_t *dev;
3706 isc_task_t *current_task;
3708 INSIST(sock->connecting);
3709 sock->connecting = 0;
3711 dev = sock->connect_ev;
3712 current_task = dev->ev_sender;
3714 if ((task == NULL) || (task == current_task)) {
3715 sock->connect_ev = NULL;
3717 dev->result = ISC_R_CANCELED;
3718 dev->ev_sender = sock;
3719 isc_task_sendanddetach(¤t_task,
3720 ISC_EVENT_PTR(&dev));
3724 UNLOCK(&sock->lock);
3728 isc_socket_gettype(isc_socket_t *sock) {
3729 REQUIRE(VALID_SOCKET(sock));
3731 return (sock->type);
3735 isc_socket_isbound(isc_socket_t *sock) {
3739 val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
3740 UNLOCK(&sock->lock);
3746 isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
3747 #if defined(IPV6_V6ONLY)
3748 int onoff = yes ? 1 : 0;
3754 REQUIRE(VALID_SOCKET(sock));
3757 if (sock->pf == AF_INET6) {
3758 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
3759 (void *)&onoff, sizeof(onoff));
3764 #ifndef ISC_PLATFORM_USETHREADS
3766 isc__socketmgr_getfdsets(fd_set *readset, fd_set *writeset, int *maxfd) {
3767 if (socketmgr == NULL)
3770 *readset = socketmgr->read_fds;
3771 *writeset = socketmgr->write_fds;
3772 *maxfd = socketmgr->maxfd + 1;
3777 isc__socketmgr_dispatch(fd_set *readset, fd_set *writeset, int maxfd) {
3778 isc_socketmgr_t *manager = socketmgr;
3780 if (manager == NULL)
3781 return (ISC_R_NOTFOUND);
3783 process_fds(manager, maxfd, readset, writeset);
3784 return (ISC_R_SUCCESS);
3786 #endif /* ISC_PLATFORM_USETHREADS */