2 * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-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.
20 /* This code uses functions which are only available on Server 2003 and
21 * higher, and Windows XP and higher.
23 * This code is by nature multithreaded and takes advantage of various
24 * features to pass on information through the completion port for
25 * when I/O is completed. All sends, receives, accepts, and connects are
26 * completed through the completion port.
28 * The number of Completion Port Worker threads used is the total number
29 * of CPU's + 1. This increases the likelihood that a Worker Thread is
30 * available for processing a completed request.
32 * XXXPDM 5 August, 2002
35 #define MAKE_EXTERNAL 1
38 #include <sys/types.h>
41 #define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
53 #include <isc/buffer.h>
54 #include <isc/bufferlist.h>
55 #include <isc/condition.h>
60 #include <isc/mutex.h>
64 #include <isc/platform.h>
65 #include <isc/print.h>
66 #include <isc/region.h>
67 #include <isc/socket.h>
68 #include <isc/stats.h>
69 #include <isc/strerror.h>
70 #include <isc/syslog.h>
72 #include <isc/thread.h>
74 #include <isc/win32os.h>
78 #include "errno2result.h"
81 * How in the world can Microsoft exist with APIs like this?
82 * We can't actually call this directly, because it turns out
83 * no library exports this function. Instead, we need to
84 * issue a runtime call to get the address.
86 LPFN_CONNECTEX ISCConnectEx;
87 LPFN_ACCEPTEX ISCAcceptEx;
88 LPFN_GETACCEPTEXSOCKADDRS ISCGetAcceptExSockaddrs;
91 * Run expensive internal consistency checks.
93 #ifdef ISC_SOCKET_CONSISTENCY_CHECKS
94 #define CONSISTENT(sock) consistent(sock)
96 #define CONSISTENT(sock) do {} while (0)
98 static void consistent(isc_socket_t *sock);
101 * Define this macro to control the behavior of connection
102 * resets on UDP sockets. See Microsoft KnowledgeBase Article Q263823
104 * NOTE: This requires that Windows 2000 systems install Service Pack 2
107 #ifndef SIO_UDP_CONNRESET
108 #define SIO_UDP_CONNRESET _WSAIOW(IOC_VENDOR,12)
112 * Some systems define the socket length argument as an int, some as size_t,
113 * some as socklen_t. This is here so it can be easily changed if needed.
115 #ifndef ISC_SOCKADDR_LEN_T
116 #define ISC_SOCKADDR_LEN_T unsigned int
120 * Define what the possible "soft" errors can be. These are non-fatal returns
121 * of various network related functions, like recv() and so on.
123 #define SOFT_ERROR(e) ((e) == WSAEINTR || \
124 (e) == WSAEWOULDBLOCK || \
125 (e) == EWOULDBLOCK || \
131 * Pending errors are not really errors and should be
134 #define PENDING_ERROR(e) ((e) == WSA_IO_PENDING || (e) == 0)
136 #define DOIO_SUCCESS 0 /* i/o ok, event sent */
137 #define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
138 #define DOIO_HARD 2 /* i/o error, event sent */
139 #define DOIO_EOF 3 /* EOF, no event sent */
140 #define DOIO_PENDING 4 /* status when i/o is in process */
141 #define DOIO_NEEDMORE 5 /* IO was processed, but we need more due to minimum */
143 #define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
146 * DLVL(90) -- Function entry/exit and other tracing.
147 * DLVL(70) -- Socket "correctness" -- including returning of events, etc.
148 * DLVL(60) -- Socket data send/receive
149 * DLVL(50) -- Event tracing, including receiving/sending completion events.
150 * DLVL(20) -- Socket creation/destruction.
152 #define TRACE_LEVEL 90
153 #define CORRECTNESS_LEVEL 70
154 #define IOEVENT_LEVEL 60
155 #define EVENT_LEVEL 50
156 #define CREATION_LEVEL 20
158 #define TRACE DLVL(TRACE_LEVEL)
159 #define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
160 #define IOEVENT DLVL(IOEVENT_LEVEL)
161 #define EVENT DLVL(EVENT_LEVEL)
162 #define CREATION DLVL(CREATION_LEVEL)
164 typedef isc_event_t intev_t;
170 SOCK_INITIALIZED, /* Socket Initialized */
171 SOCK_OPEN, /* Socket opened but nothing yet to do */
172 SOCK_DATA, /* Socket sending or receiving data */
173 SOCK_LISTEN, /* TCP Socket listening for connects */
174 SOCK_ACCEPT, /* TCP socket is waiting to accept */
175 SOCK_CONNECT, /* TCP Socket connecting */
176 SOCK_CLOSED, /* Socket has been closed */
179 #define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
180 #define VALID_SOCKET(t) ISC_MAGIC_VALID(t, SOCKET_MAGIC)
183 * IPv6 control information. If the socket is an IPv6 socket we want
184 * to collect the destination address and interface so the client can
185 * set them on outgoing packets.
187 #ifdef ISC_PLATFORM_HAVEIPV6
194 * We really don't want to try and use these control messages. Win32
195 * doesn't have this mechanism before XP.
200 * Message header for recvmsg and sendmsg calls.
201 * Used value-result for recvmsg, value only for sendmsg.
204 SOCKADDR_STORAGE to_addr; /* UDP send/recv address */
205 int to_addr_len; /* length of the address */
206 WSABUF *msg_iov; /* scatter/gather array */
207 u_int msg_iovlen; /* # elements in msg_iov */
208 void *msg_control; /* ancillary data, see below */
209 u_int msg_controllen; /* ancillary data buffer len */
210 int msg_totallen; /* total length of this message */
214 * The size to raise the receive buffer to.
216 #define RCVBUFSIZE (32*1024)
219 * The number of times a send operation is repeated if the result
227 isc_socketmgr_t *manager;
229 isc_sockettype_t type;
231 /* Pointers to scatter/gather buffers */
232 WSABUF iov[ISC_SOCKET_MAXSCATTERGATHER];
234 /* Locked by socket lock. */
235 ISC_LINK(isc_socket_t) link;
236 unsigned int references; /* EXTERNAL references */
237 SOCKET fd; /* file handle */
238 int pf; /* protocol family */
243 * Each recv() call uses this buffer. It is a per-socket receive
244 * buffer that allows us to decouple the system recv() from the
245 * recv_list done events. This means the items on the recv_list
246 * can be removed without having to cancel pending system recv()
247 * calls. It also allows us to read-ahead in some cases.
250 SOCKADDR_STORAGE from_addr; // UDP send/recv address
251 int from_addr_len; // length of the address
252 char *base; // the base of the buffer
253 char *consume_position; // where to start copying data from next
254 unsigned int len; // the actual size of this buffer
255 unsigned int remaining; // the number of bytes remaining
258 ISC_LIST(isc_socketevent_t) send_list;
259 ISC_LIST(isc_socketevent_t) recv_list;
260 ISC_LIST(isc_socket_newconnev_t) accept_list;
261 isc_socket_connev_t *connect_ev;
263 isc_sockaddr_t address; /* remote address */
265 unsigned int listener : 1, /* listener socket */
267 pending_connect : 1, /* connect pending */
268 bound : 1, /* bound to local addr */
269 dupped : 1; /* created by isc_socket_dup() */
270 unsigned int pending_iocp; /* Should equal the counters below. Debug. */
271 unsigned int pending_recv; /* Number of outstanding recv() calls. */
272 unsigned int pending_send; /* Number of outstanding send() calls. */
273 unsigned int pending_accept; /* Number of outstanding accept() calls. */
274 unsigned int state; /* Socket state. Debugging and consistency checking. */
275 int state_lineno; /* line which last touched state */
278 #define _set_state(sock, _state) do { (sock)->state = (_state); (sock)->state_lineno = __LINE__; } while (0)
283 typedef struct buflist buflist_t;
288 ISC_LINK(buflist_t) link;
292 * I/O Completion ports Info structures
295 static HANDLE hHeapHandle = NULL;
296 typedef struct IoCompletionInfo {
297 OVERLAPPED overlapped;
298 isc_socketevent_t *dev; /* send()/recv() done event */
299 isc_socket_connev_t *cdev; /* connect() done event */
300 isc_socket_newconnev_t *adev; /* accept() done event */
302 DWORD received_bytes;
304 struct msghdr messagehdr;
305 ISC_LIST(buflist_t) bufferlist; /*%< list of buffers */
309 * Define a maximum number of I/O Completion Port worker threads
310 * to handle the load on the Completion Port. The actual number
311 * used is the number of CPU's + 1.
313 #define MAX_IOCPTHREADS 20
315 #define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
316 #define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
318 struct isc_socketmgr {
325 /* Locked by manager lock. */
326 ISC_LIST(isc_socket_t) socklist;
327 isc_boolean_t bShutdown;
328 isc_condition_t shutdown_ok;
329 HANDLE hIoCompletionPort;
331 HANDLE hIOCPThreads[MAX_IOCPTHREADS];
332 DWORD dwIOCPThreadIds[MAX_IOCPTHREADS];
336 * Modified by InterlockedIncrement() and InterlockedDecrement()
350 * send() and recv() iovec counts
352 #define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
353 #define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
355 static isc_result_t socket_create(isc_socketmgr_t *manager0, int pf,
356 isc_sockettype_t type,
357 isc_socket_t **socketp,
358 isc_socket_t *dup_socket);
359 static isc_threadresult_t WINAPI SocketIoThread(LPVOID ThreadContext);
360 static void maybe_free_socket(isc_socket_t **, int);
361 static void free_socket(isc_socket_t **, int);
362 static isc_boolean_t senddone_is_active(isc_socket_t *sock, isc_socketevent_t *dev);
363 static isc_boolean_t acceptdone_is_active(isc_socket_t *sock, isc_socket_newconnev_t *dev);
364 static isc_boolean_t connectdone_is_active(isc_socket_t *sock, isc_socket_connev_t *dev);
365 static void send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev);
366 static void send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev);
367 static void send_acceptdone_event(isc_socket_t *sock, isc_socket_newconnev_t **adev);
368 static void send_connectdone_event(isc_socket_t *sock, isc_socket_connev_t **cdev);
369 static void send_recvdone_abort(isc_socket_t *sock, isc_result_t result);
370 static void queue_receive_event(isc_socket_t *sock, isc_task_t *task, isc_socketevent_t *dev);
371 static void queue_receive_request(isc_socket_t *sock);
374 * This is used to dump the contents of the sock structure
375 * You should make sure that the sock is locked before
376 * dumping it. Since the code uses simple printf() statements
377 * it should only be used interactively.
380 sock_dump(isc_socket_t *sock) {
381 isc_socketevent_t *ldev;
382 isc_socket_newconnev_t *ndev;
388 isc_socket_getpeername(sock, &addr);
389 isc_sockaddr_format(&addr, socktext, sizeof(socktext));
390 printf("Remote Socket: %s\n", socktext);
391 isc_socket_getsockname(sock, &addr);
392 isc_sockaddr_format(&addr, socktext, sizeof(socktext));
393 printf("This Socket: %s\n", socktext);
396 printf("\n\t\tSock Dump\n");
397 printf("\t\tfd: %u\n", sock->fd);
398 printf("\t\treferences: %d\n", sock->references);
399 printf("\t\tpending_accept: %d\n", sock->pending_accept);
400 printf("\t\tconnecting: %d\n", sock->pending_connect);
401 printf("\t\tconnected: %d\n", sock->connected);
402 printf("\t\tbound: %d\n", sock->bound);
403 printf("\t\tpending_iocp: %d\n", sock->pending_iocp);
404 printf("\t\tsocket type: %d\n", sock->type);
406 printf("\n\t\tSock Recv List\n");
407 ldev = ISC_LIST_HEAD(sock->recv_list);
408 while (ldev != NULL) {
409 printf("\t\tdev: %p\n", ldev);
410 ldev = ISC_LIST_NEXT(ldev, ev_link);
413 printf("\n\t\tSock Send List\n");
414 ldev = ISC_LIST_HEAD(sock->send_list);
415 while (ldev != NULL) {
416 printf("\t\tdev: %p\n", ldev);
417 ldev = ISC_LIST_NEXT(ldev, ev_link);
420 printf("\n\t\tSock Accept List\n");
421 ndev = ISC_LIST_HEAD(sock->accept_list);
422 while (ndev != NULL) {
423 printf("\t\tdev: %p\n", ldev);
424 ndev = ISC_LIST_NEXT(ndev, ev_link);
429 socket_log(int lineno, isc_socket_t *sock, isc_sockaddr_t *address,
430 isc_logcategory_t *category, isc_logmodule_t *module, int level,
431 isc_msgcat_t *msgcat, int msgset, int message,
432 const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
434 /* This function will add an entry to the I/O completion port
435 * that will signal the I/O thread to exit (gracefully)
438 signal_iocompletionport_exit(isc_socketmgr_t *manager) {
441 char strbuf[ISC_STRERRORSIZE];
443 REQUIRE(VALID_MANAGER(manager));
444 for (i = 0; i < manager->maxIOCPThreads; i++) {
445 if (!PostQueuedCompletionStatus(manager->hIoCompletionPort,
447 errval = GetLastError();
448 isc__strerror(errval, strbuf, sizeof(strbuf));
449 FATAL_ERROR(__FILE__, __LINE__,
450 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
452 "Can't request service thread to exit: %s"),
459 * Create the worker threads for the I/O Completion Port
462 iocompletionport_createthreads(int total_threads, isc_socketmgr_t *manager) {
464 char strbuf[ISC_STRERRORSIZE];
467 INSIST(total_threads > 0);
468 REQUIRE(VALID_MANAGER(manager));
470 * We need at least one
472 for (i = 0; i < total_threads; i++) {
473 manager->hIOCPThreads[i] = CreateThread(NULL, 0, SocketIoThread,
475 &manager->dwIOCPThreadIds[i]);
476 if (manager->hIOCPThreads[i] == NULL) {
477 errval = GetLastError();
478 isc__strerror(errval, strbuf, sizeof(strbuf));
479 FATAL_ERROR(__FILE__, __LINE__,
480 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
482 "Can't create IOCP thread: %s"),
490 * Create/initialise the I/O completion port
493 iocompletionport_init(isc_socketmgr_t *manager) {
495 char strbuf[ISC_STRERRORSIZE];
497 REQUIRE(VALID_MANAGER(manager));
499 * Create a private heap to handle the socket overlapped structure
500 * The minimum number of structures is 10, there is no maximum
502 hHeapHandle = HeapCreate(0, 10 * sizeof(IoCompletionInfo), 0);
503 if (hHeapHandle == NULL) {
504 errval = GetLastError();
505 isc__strerror(errval, strbuf, sizeof(strbuf));
506 FATAL_ERROR(__FILE__, __LINE__,
507 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
509 "HeapCreate() failed during "
510 "initialization: %s"),
515 manager->maxIOCPThreads = min(isc_os_ncpus() + 1, MAX_IOCPTHREADS);
517 /* Now Create the Completion Port */
518 manager->hIoCompletionPort = CreateIoCompletionPort(
519 INVALID_HANDLE_VALUE, NULL,
520 0, manager->maxIOCPThreads);
521 if (manager->hIoCompletionPort == NULL) {
522 errval = GetLastError();
523 isc__strerror(errval, strbuf, sizeof(strbuf));
524 FATAL_ERROR(__FILE__, __LINE__,
525 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
527 "CreateIoCompletionPort() failed "
528 "during initialization: %s"),
534 * Worker threads for servicing the I/O
536 iocompletionport_createthreads(manager->maxIOCPThreads, manager);
540 * Associate a socket with an IO Completion Port. This allows us to queue events for it
541 * and have our worker pool of threads process them.
544 iocompletionport_update(isc_socket_t *sock) {
546 char strbuf[ISC_STRERRORSIZE];
548 REQUIRE(VALID_SOCKET(sock));
550 hiocp = CreateIoCompletionPort((HANDLE)sock->fd,
551 sock->manager->hIoCompletionPort, (ULONG_PTR)sock, 0);
554 DWORD errval = GetLastError();
555 isc__strerror(errval, strbuf, sizeof(strbuf));
556 isc_log_iwrite(isc_lctx,
557 ISC_LOGCATEGORY_GENERAL,
558 ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
559 isc_msgcat, ISC_MSGSET_SOCKET,
560 ISC_MSG_TOOMANYHANDLES,
561 "iocompletionport_update: failed to open"
562 " io completion port: %s",
565 /* XXXMLG temporary hack to make failures detected.
566 * This function should return errors to the caller, not
569 FATAL_ERROR(__FILE__, __LINE__,
570 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
572 "CreateIoCompletionPort() failed "
573 "during initialization: %s"),
578 InterlockedIncrement(&sock->manager->iocp_total);
582 * Routine to cleanup and then close the socket.
583 * Only close the socket here if it is NOT associated
584 * with an event, otherwise the WSAWaitForMultipleEvents
585 * may fail due to the fact that the Wait should not
586 * be running while closing an event or a socket.
587 * The socket is locked before calling this function
590 socket_close(isc_socket_t *sock) {
592 REQUIRE(sock != NULL);
594 if (sock->fd != INVALID_SOCKET) {
595 closesocket(sock->fd);
596 sock->fd = INVALID_SOCKET;
597 _set_state(sock, SOCK_CLOSED);
598 InterlockedDecrement(&sock->manager->totalSockets);
602 static isc_once_t initialise_once = ISC_ONCE_INIT;
603 static isc_boolean_t initialised = ISC_FALSE;
607 WORD wVersionRequested;
611 GUID GUIDConnectEx = WSAID_CONNECTEX;
612 GUID GUIDAcceptEx = WSAID_ACCEPTEX;
613 GUID GUIDGetAcceptExSockaddrs = WSAID_GETACCEPTEXSOCKADDRS;
616 /* Need Winsock 2.2 or better */
617 wVersionRequested = MAKEWORD(2, 2);
619 err = WSAStartup(wVersionRequested, &wsaData);
621 char strbuf[ISC_STRERRORSIZE];
622 isc__strerror(err, strbuf, sizeof(strbuf));
623 FATAL_ERROR(__FILE__, __LINE__, "WSAStartup() %s: %s",
624 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
625 ISC_MSG_FAILED, "failed"),
630 * The following APIs do not exist as functions in a library, but we must
631 * ask winsock for them. They are "extensions" -- but why they cannot be
632 * actual functions is beyond me. So, ask winsock for the pointers to the
635 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
636 INSIST(sock != INVALID_SOCKET);
637 err = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
638 &GUIDConnectEx, sizeof(GUIDConnectEx),
639 &ISCConnectEx, sizeof(ISCConnectEx),
640 &dwBytes, NULL, NULL);
643 err = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
644 &GUIDAcceptEx, sizeof(GUIDAcceptEx),
645 &ISCAcceptEx, sizeof(ISCAcceptEx),
646 &dwBytes, NULL, NULL);
649 err = WSAIoctl(sock, SIO_GET_EXTENSION_FUNCTION_POINTER,
650 &GUIDGetAcceptExSockaddrs, sizeof(GUIDGetAcceptExSockaddrs),
651 &ISCGetAcceptExSockaddrs, sizeof(ISCGetAcceptExSockaddrs),
652 &dwBytes, NULL, NULL);
657 initialised = ISC_TRUE;
661 * Initialize socket services
665 RUNTIME_CHECK(isc_once_do(&initialise_once,
666 initialise) == ISC_R_SUCCESS);
672 internal_sendmsg(isc_socket_t *sock, IoCompletionInfo *lpo,
673 struct msghdr *messagehdr, int flags, int *Error)
681 Result = WSASendTo(sock->fd, messagehdr->msg_iov,
682 messagehdr->msg_iovlen, &BytesSent,
683 Flags, (SOCKADDR *)&messagehdr->to_addr,
684 messagehdr->to_addr_len, (LPWSAOVERLAPPED)lpo,
687 total_sent = (int)BytesSent;
689 /* Check for errors.*/
690 if (Result == SOCKET_ERROR) {
691 *Error = WSAGetLastError();
694 case WSA_IO_INCOMPLETE:
695 case WSA_WAIT_IO_COMPLETION:
697 case NO_ERROR: /* Strange, but okay */
698 sock->pending_iocp++;
699 sock->pending_send++;
707 sock->pending_iocp++;
708 sock->pending_send++;
718 queue_receive_request(isc_socket_t *sock) {
726 IoCompletionInfo *lpo = NULL;
727 isc_result_t isc_result;
730 need_retry = ISC_FALSE;
733 * If we already have a receive pending, do nothing.
735 if (sock->pending_recv > 0) {
737 HeapFree(hHeapHandle, 0, lpo);
742 * If no one is waiting, do nothing.
744 if (ISC_LIST_EMPTY(sock->recv_list)) {
746 HeapFree(hHeapHandle, 0, lpo);
750 INSIST(sock->recvbuf.remaining == 0);
751 INSIST(sock->fd != INVALID_SOCKET);
753 iov[0].len = sock->recvbuf.len;
754 iov[0].buf = sock->recvbuf.base;
757 lpo = (IoCompletionInfo *)HeapAlloc(hHeapHandle,
759 sizeof(IoCompletionInfo));
760 RUNTIME_CHECK(lpo != NULL);
762 ZeroMemory(lpo, sizeof(IoCompletionInfo));
763 lpo->request_type = SOCKET_RECV;
765 sock->recvbuf.from_addr_len = sizeof(sock->recvbuf.from_addr);
768 Result = WSARecvFrom((SOCKET)sock->fd, iov, 1,
770 (SOCKADDR *)&sock->recvbuf.from_addr,
771 &sock->recvbuf.from_addr_len,
772 (LPWSAOVERLAPPED)lpo, NULL);
774 /* Check for errors. */
775 if (Result == SOCKET_ERROR) {
776 Error = WSAGetLastError();
780 sock->pending_iocp++;
781 sock->pending_recv++;
784 /* direct error: no completion event */
785 case ERROR_HOST_UNREACHABLE:
788 if (!sock->connected) {
790 need_retry = ISC_TRUE;
796 isc_result = isc__errno2result(Error);
797 if (isc_result == ISC_R_UNEXPECTED)
798 UNEXPECTED_ERROR(__FILE__, __LINE__,
799 "WSARecvFrom: Windows error code: %d, isc result %d",
801 send_recvdone_abort(sock, isc_result);
802 HeapFree(hHeapHandle, 0, lpo);
808 * The recv() finished immediately, but we will still get
809 * a completion event. Rather than duplicate code, let
810 * that thread handle sending the data along its way.
812 sock->pending_iocp++;
813 sock->pending_recv++;
816 socket_log(__LINE__, sock, NULL, IOEVENT,
817 isc_msgcat, ISC_MSGSET_SOCKET,
819 "queue_io_request: fd %d result %d error %d",
820 sock->fd, Result, Error);
829 manager_log(isc_socketmgr_t *sockmgr, isc_logcategory_t *category,
830 isc_logmodule_t *module, int level, const char *fmt, ...)
835 if (!isc_log_wouldlog(isc_lctx, level))
839 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
842 isc_log_write(isc_lctx, category, module, level,
843 "sockmgr %p: %s", sockmgr, msgbuf);
847 socket_log(int lineno, isc_socket_t *sock, isc_sockaddr_t *address,
848 isc_logcategory_t *category, isc_logmodule_t *module, int level,
849 isc_msgcat_t *msgcat, int msgset, int message,
850 const char *fmt, ...)
857 if (!isc_log_wouldlog(isc_lctx, level))
861 vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
864 if (address == NULL) {
865 isc_log_iwrite(isc_lctx, category, module, level,
866 msgcat, msgset, message,
867 "socket %p line %d: %s", sock, lineno, msgbuf);
869 isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
870 isc_log_iwrite(isc_lctx, category, module, level,
871 msgcat, msgset, message,
872 "socket %p line %d peer %s: %s", sock, lineno,
879 * Make an fd SOCKET non-blocking.
882 make_nonblock(SOCKET fd) {
884 unsigned long flags = 1;
885 char strbuf[ISC_STRERRORSIZE];
887 /* Set the socket to non-blocking */
888 ret = ioctlsocket(fd, FIONBIO, &flags);
891 isc__strerror(errno, strbuf, sizeof(strbuf));
892 UNEXPECTED_ERROR(__FILE__, __LINE__,
893 "ioctlsocket(%d, FIOBIO, %d): %s",
896 return (ISC_R_UNEXPECTED);
899 return (ISC_R_SUCCESS);
903 * Windows 2000 systems incorrectly cause UDP sockets using WSARecvFrom
904 * to not work correctly, returning a WSACONNRESET error when a WSASendTo
905 * fails with an "ICMP port unreachable" response and preventing the
906 * socket from using the WSARecvFrom in subsequent operations.
907 * The function below fixes this, but requires that Windows 2000
908 * Service Pack 2 or later be installed on the system. NT 4.0
909 * systems are not affected by this and work correctly.
910 * See Microsoft Knowledge Base Article Q263823 for details of this.
913 connection_reset_fix(SOCKET fd) {
914 DWORD dwBytesReturned = 0;
915 BOOL bNewBehavior = FALSE;
918 if (isc_win32os_majorversion() < 5)
919 return (ISC_R_SUCCESS); /* NT 4.0 has no problem */
921 /* disable bad behavior using IOCTL: SIO_UDP_CONNRESET */
922 status = WSAIoctl(fd, SIO_UDP_CONNRESET, &bNewBehavior,
923 sizeof(bNewBehavior), NULL, 0,
924 &dwBytesReturned, NULL, NULL);
925 if (status != SOCKET_ERROR)
926 return (ISC_R_SUCCESS);
928 UNEXPECTED_ERROR(__FILE__, __LINE__,
929 "WSAIoctl(SIO_UDP_CONNRESET, oldBehaviour) %s",
930 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
931 ISC_MSG_FAILED, "failed"));
932 return (ISC_R_UNEXPECTED);
937 * Construct an iov array and attach it to the msghdr passed in. This is
938 * the SEND constructor, which will use the used region of the buffer
939 * (if using a buffer list) or will use the internal region (if a single
940 * buffer I/O is requested).
942 * Nothing can be NULL, and the done event must list at least one buffer
943 * on the buffer linked list for this function to be meaningful.
946 build_msghdr_send(isc_socket_t *sock, isc_socketevent_t *dev,
947 struct msghdr *msg, char *cmsg, WSABUF *iov,
948 IoCompletionInfo *lpo)
950 unsigned int iovcount;
951 isc_buffer_t *buffer;
957 memset(msg, 0, sizeof(*msg));
959 memcpy(&msg->to_addr, &dev->address.type, dev->address.length);
960 msg->to_addr_len = dev->address.length;
962 buffer = ISC_LIST_HEAD(dev->bufferlist);
967 * Single buffer I/O? Skip what we've done so far in this region.
969 if (buffer == NULL) {
970 write_count = dev->region.length - dev->n;
971 cpbuffer = HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY, sizeof(buflist_t));
972 RUNTIME_CHECK(cpbuffer != NULL);
973 cpbuffer->buf = HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY, write_count);
974 RUNTIME_CHECK(cpbuffer->buf != NULL);
976 socket_log(__LINE__, sock, NULL, TRACE,
977 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
978 "alloc_buffer %p %d %p %d", cpbuffer, sizeof(buflist_t),
979 cpbuffer->buf, write_count);
981 memcpy(cpbuffer->buf,(dev->region.base + dev->n), write_count);
982 cpbuffer->buflen = write_count;
983 ISC_LIST_ENQUEUE(lpo->bufferlist, cpbuffer, link);
984 iov[0].buf = cpbuffer->buf;
985 iov[0].len = write_count;
993 * Skip the data in the buffer list that we have already written.
996 while (buffer != NULL) {
997 REQUIRE(ISC_BUFFER_VALID(buffer));
998 if (skip_count < isc_buffer_usedlength(buffer))
1000 skip_count -= isc_buffer_usedlength(buffer);
1001 buffer = ISC_LIST_NEXT(buffer, link);
1004 while (buffer != NULL) {
1005 INSIST(iovcount < MAXSCATTERGATHER_SEND);
1007 isc_buffer_usedregion(buffer, &used);
1009 if (used.length > 0) {
1010 int uselen = used.length - skip_count;
1011 cpbuffer = HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY, sizeof(buflist_t));
1012 RUNTIME_CHECK(cpbuffer != NULL);
1013 cpbuffer->buf = HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY, uselen);
1014 RUNTIME_CHECK(cpbuffer->buf != NULL);
1016 socket_log(__LINE__, sock, NULL, TRACE,
1017 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
1018 "alloc_buffer %p %d %p %d", cpbuffer, sizeof(buflist_t),
1019 cpbuffer->buf, write_count);
1021 memcpy(cpbuffer->buf,(used.base + skip_count), uselen);
1022 cpbuffer->buflen = uselen;
1023 iov[iovcount].buf = cpbuffer->buf;
1024 iov[iovcount].len = used.length - skip_count;
1025 write_count += uselen;
1029 buffer = ISC_LIST_NEXT(buffer, link);
1032 INSIST(skip_count == 0);
1036 msg->msg_iovlen = iovcount;
1037 msg->msg_totallen = write_count;
1041 set_dev_address(isc_sockaddr_t *address, isc_socket_t *sock,
1042 isc_socketevent_t *dev)
1044 if (sock->type == isc_sockettype_udp) {
1045 if (address != NULL)
1046 dev->address = *address;
1048 dev->address = sock->address;
1049 } else if (sock->type == isc_sockettype_tcp) {
1050 INSIST(address == NULL);
1051 dev->address = sock->address;
1056 destroy_socketevent(isc_event_t *event) {
1057 isc_socketevent_t *ev = (isc_socketevent_t *)event;
1059 INSIST(ISC_LIST_EMPTY(ev->bufferlist));
1061 (ev->destroy)(event);
1064 static isc_socketevent_t *
1065 allocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype,
1066 isc_taskaction_t action, const void *arg)
1068 isc_socketevent_t *ev;
1070 ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
1077 ev->result = ISC_R_IOERROR; // XXXMLG temporary change to detect failure to set
1078 ISC_LINK_INIT(ev, ev_link);
1079 ISC_LIST_INIT(ev->bufferlist);
1080 ev->region.base = NULL;
1084 ev->destroy = ev->ev_destroy;
1085 ev->ev_destroy = destroy_socketevent;
1090 #if defined(ISC_SOCKET_DEBUG)
1092 dump_msg(struct msghdr *msg, isc_socket_t *sock) {
1095 printf("MSGHDR %p, Socket #: %u\n", msg, sock->fd);
1096 printf("\tname %p, namelen %d\n", msg->msg_name, msg->msg_namelen);
1097 printf("\tiov %p, iovlen %d\n", msg->msg_iov, msg->msg_iovlen);
1098 for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
1099 printf("\t\t%d\tbase %p, len %d\n", i,
1100 msg->msg_iov[i].buf,
1101 msg->msg_iov[i].len);
1106 * map the error code
1109 map_socket_error(isc_socket_t *sock, int windows_errno, int *isc_errno,
1110 char *errorstring, size_t bufsize) {
1113 switch (windows_errno) {
1114 case WSAECONNREFUSED:
1115 *isc_errno = ISC_R_CONNREFUSED;
1116 if (sock->connected)
1117 doreturn = DOIO_HARD;
1119 doreturn = DOIO_SOFT;
1121 case WSAENETUNREACH:
1122 case ERROR_NETWORK_UNREACHABLE:
1123 *isc_errno = ISC_R_NETUNREACH;
1124 if (sock->connected)
1125 doreturn = DOIO_HARD;
1127 doreturn = DOIO_SOFT;
1129 case ERROR_PORT_UNREACHABLE:
1130 case ERROR_HOST_UNREACHABLE:
1131 case WSAEHOSTUNREACH:
1132 *isc_errno = ISC_R_HOSTUNREACH;
1133 if (sock->connected)
1134 doreturn = DOIO_HARD;
1136 doreturn = DOIO_SOFT;
1139 *isc_errno = ISC_R_NETDOWN;
1140 if (sock->connected)
1141 doreturn = DOIO_HARD;
1143 doreturn = DOIO_SOFT;
1146 *isc_errno = ISC_R_HOSTDOWN;
1147 if (sock->connected)
1148 doreturn = DOIO_HARD;
1150 doreturn = DOIO_SOFT;
1153 *isc_errno = ISC_R_NOPERM;
1154 if (sock->connected)
1155 doreturn = DOIO_HARD;
1157 doreturn = DOIO_SOFT;
1161 case WSAECONNABORTED:
1163 *isc_errno = ISC_R_CONNECTIONRESET;
1164 if (sock->connected)
1165 doreturn = DOIO_HARD;
1167 doreturn = DOIO_SOFT;
1170 *isc_errno = ISC_R_NOTCONNECTED;
1171 if (sock->connected)
1172 doreturn = DOIO_HARD;
1174 doreturn = DOIO_SOFT;
1176 case ERROR_OPERATION_ABORTED:
1177 case ERROR_CONNECTION_ABORTED:
1178 case ERROR_REQUEST_ABORTED:
1179 *isc_errno = ISC_R_CONNECTIONRESET;
1180 doreturn = DOIO_HARD;
1183 *isc_errno = ISC_R_NORESOURCES;
1184 doreturn = DOIO_HARD;
1186 case WSAEAFNOSUPPORT:
1187 *isc_errno = ISC_R_FAMILYNOSUPPORT;
1188 doreturn = DOIO_HARD;
1190 case WSAEADDRNOTAVAIL:
1191 *isc_errno = ISC_R_ADDRNOTAVAIL;
1192 doreturn = DOIO_HARD;
1194 case WSAEDESTADDRREQ:
1195 *isc_errno = ISC_R_BADADDRESSFORM;
1196 doreturn = DOIO_HARD;
1198 case ERROR_NETNAME_DELETED:
1199 *isc_errno = ISC_R_NETDOWN;
1200 doreturn = DOIO_HARD;
1203 *isc_errno = ISC_R_IOERROR;
1204 doreturn = DOIO_HARD;
1207 if (doreturn == DOIO_HARD) {
1208 isc__strerror(windows_errno, errorstring, bufsize);
1214 fill_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
1217 isc_buffer_t *buffer;
1219 INSIST(dev->n < dev->minimum);
1220 INSIST(sock->recvbuf.remaining > 0);
1221 INSIST(sock->pending_recv == 0);
1223 if (sock->type == isc_sockettype_udp) {
1224 dev->address.length = sock->recvbuf.from_addr_len;
1225 memcpy(&dev->address.type, &sock->recvbuf.from_addr,
1226 sock->recvbuf.from_addr_len);
1227 if (isc_sockaddr_getport(&dev->address) == 0) {
1228 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1229 socket_log(__LINE__, sock, &dev->address, IOEVENT,
1230 isc_msgcat, ISC_MSGSET_SOCKET,
1232 "dropping source port zero packet");
1234 sock->recvbuf.remaining = 0;
1237 } else if (sock->type == isc_sockettype_tcp) {
1238 dev->address = sock->address;
1242 * Run through the list of buffers we were given, and find the
1243 * first one with space. Once it is found, loop through, filling
1244 * the buffers as much as possible.
1246 buffer = ISC_LIST_HEAD(dev->bufferlist);
1247 if (buffer != NULL) { // Multi-buffer receive
1248 while (buffer != NULL && sock->recvbuf.remaining > 0) {
1249 REQUIRE(ISC_BUFFER_VALID(buffer));
1250 if (isc_buffer_availablelength(buffer) > 0) {
1251 isc_buffer_availableregion(buffer, &r);
1252 copylen = min(r.length, sock->recvbuf.remaining);
1253 memcpy(r.base, sock->recvbuf.consume_position, copylen);
1254 sock->recvbuf.consume_position += copylen;
1255 sock->recvbuf.remaining -= copylen;
1256 isc_buffer_add(buffer, copylen);
1259 buffer = ISC_LIST_NEXT(buffer, link);
1261 } else { // Single-buffer receive
1262 copylen = min(dev->region.length - dev->n, sock->recvbuf.remaining);
1263 memcpy(dev->region.base + dev->n, sock->recvbuf.consume_position, copylen);
1264 sock->recvbuf.consume_position += copylen;
1265 sock->recvbuf.remaining -= copylen;
1270 * UDP receives are all-consuming. That is, if we have 4k worth of
1271 * data in our receive buffer, and the caller only gave us
1272 * 1k of space, we will toss the remaining 3k of data. TCP
1273 * will keep the extra data around and use it for later requests.
1275 if (sock->type == isc_sockettype_udp)
1276 sock->recvbuf.remaining = 0;
1280 * Copy out as much data from the internal buffer to done events.
1281 * As each done event is filled, send it along its way.
1284 completeio_recv(isc_socket_t *sock)
1286 isc_socketevent_t *dev;
1289 * If we are in the process of filling our buffer, we cannot
1290 * touch it yet, so don't.
1292 if (sock->pending_recv > 0)
1295 while (sock->recvbuf.remaining > 0 && !ISC_LIST_EMPTY(sock->recv_list)) {
1296 dev = ISC_LIST_HEAD(sock->recv_list);
1299 * See if we have sufficient data in our receive buffer
1300 * to handle this. If we do, copy out the data.
1302 fill_recv(sock, dev);
1305 * Did we satisfy it?
1307 if (dev->n >= dev->minimum) {
1308 dev->result = ISC_R_SUCCESS;
1309 send_recvdone_event(sock, &dev);
1316 * DOIO_SUCCESS The operation succeeded. dev->result contains
1319 * DOIO_HARD A hard or unexpected I/O error was encountered.
1320 * dev->result contains the appropriate error.
1322 * DOIO_SOFT A soft I/O error was encountered. No senddone
1323 * event was sent. The operation should be retried.
1325 * No other return values are possible.
1328 completeio_send(isc_socket_t *sock, isc_socketevent_t *dev,
1329 struct msghdr *messagehdr, int cc, int send_errno)
1331 char addrbuf[ISC_SOCKADDR_FORMATSIZE];
1332 char strbuf[ISC_STRERRORSIZE];
1334 if (send_errno != 0) {
1335 if (SOFT_ERROR(send_errno))
1338 return (map_socket_error(sock, send_errno, &dev->result,
1339 strbuf, sizeof(strbuf)));
1342 * The other error types depend on whether or not the
1343 * socket is UDP or TCP. If it is UDP, some errors
1344 * that we expect to be fatal under TCP are merely
1345 * annoying, and are really soft errors.
1347 * However, these soft errors are still returned as
1350 isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
1351 isc__strerror(send_errno, strbuf, sizeof(strbuf));
1352 UNEXPECTED_ERROR(__FILE__, __LINE__, "completeio_send: %s: %s",
1354 dev->result = isc__errno2result(send_errno);
1359 * If we write less than we expected, update counters, poke.
1362 if (cc != messagehdr->msg_totallen)
1366 * Exactly what we wanted to write. We're done with this
1367 * entry. Post its completion event.
1369 dev->result = ISC_R_SUCCESS;
1370 return (DOIO_SUCCESS);
1374 startio_send(isc_socket_t *sock, isc_socketevent_t *dev, int *nbytes,
1378 char strbuf[ISC_STRERRORSIZE];
1379 IoCompletionInfo *lpo;
1381 struct msghdr *msghdr;
1383 lpo = (IoCompletionInfo *)HeapAlloc(hHeapHandle,
1385 sizeof(IoCompletionInfo));
1386 RUNTIME_CHECK(lpo != NULL);
1387 lpo->request_type = SOCKET_SEND;
1389 msghdr = &lpo->messagehdr;
1390 memset(msghdr, 0, sizeof(struct msghdr));
1391 ISC_LIST_INIT(lpo->bufferlist);
1393 build_msghdr_send(sock, dev, msghdr, cmsg, sock->iov, lpo);
1395 *nbytes = internal_sendmsg(sock, lpo, msghdr, 0, send_errno);
1399 * I/O has been initiated
1400 * completion will be through the completion port
1402 if (PENDING_ERROR(*send_errno)) {
1403 status = DOIO_PENDING;
1407 if (SOFT_ERROR(*send_errno)) {
1413 * If we got this far then something is wrong
1415 if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
1416 isc__strerror(*send_errno, strbuf, sizeof(strbuf));
1417 socket_log(__LINE__, sock, NULL, IOEVENT,
1418 isc_msgcat, ISC_MSGSET_SOCKET,
1419 ISC_MSG_INTERNALSEND,
1420 "startio_send: internal_sendmsg(%d) %d "
1422 sock->fd, *nbytes, *send_errno, strbuf);
1427 dev->result = ISC_R_SUCCESS;
1430 _set_state(sock, SOCK_DATA);
1435 allocate_socket(isc_socketmgr_t *manager, isc_sockettype_t type,
1436 isc_socket_t **socketp) {
1438 isc_result_t result;
1440 sock = isc_mem_get(manager->mctx, sizeof(*sock));
1443 return (ISC_R_NOMEMORY);
1446 sock->references = 0;
1448 sock->manager = manager;
1450 sock->fd = INVALID_SOCKET;
1452 ISC_LINK_INIT(sock, link);
1455 * set up list of readers and writers to be initially empty
1457 ISC_LIST_INIT(sock->recv_list);
1458 ISC_LIST_INIT(sock->send_list);
1459 ISC_LIST_INIT(sock->accept_list);
1460 sock->connect_ev = NULL;
1461 sock->pending_accept = 0;
1462 sock->pending_recv = 0;
1463 sock->pending_send = 0;
1464 sock->pending_iocp = 0;
1466 sock->connected = 0;
1467 sock->pending_connect = 0;
1470 memset(sock->name, 0, sizeof(sock->name)); // zero the name field
1471 _set_state(sock, SOCK_INITIALIZED);
1473 sock->recvbuf.len = 65536;
1474 sock->recvbuf.consume_position = sock->recvbuf.base;
1475 sock->recvbuf.remaining = 0;
1476 sock->recvbuf.base = isc_mem_get(manager->mctx, sock->recvbuf.len); // max buffer size
1477 if (sock->recvbuf.base == NULL) {
1483 * initialize the lock
1485 result = isc_mutex_init(&sock->lock);
1486 if (result != ISC_R_SUCCESS) {
1488 isc_mem_put(manager->mctx, sock->recvbuf.base, sock->recvbuf.len);
1489 sock->recvbuf.base = NULL;
1493 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
1496 sock->magic = SOCKET_MAGIC;
1499 return (ISC_R_SUCCESS);
1502 isc_mem_put(manager->mctx, sock, sizeof(*sock));
1508 * Verify that the socket state is consistent.
1511 consistent(isc_socket_t *sock) {
1513 isc_socketevent_t *dev;
1514 isc_socket_newconnev_t *nev;
1517 isc_boolean_t crash = ISC_FALSE;
1519 REQUIRE(sock->pending_iocp == sock->pending_recv + sock->pending_send
1520 + sock->pending_accept + sock->pending_connect);
1522 dev = ISC_LIST_HEAD(sock->send_list);
1524 while (dev != NULL) {
1526 dev = ISC_LIST_NEXT(dev, ev_link);
1528 if (count > sock->pending_send) {
1530 crash_reason = "send_list > sock->pending_send";
1533 nev = ISC_LIST_HEAD(sock->accept_list);
1535 while (nev != NULL) {
1537 nev = ISC_LIST_NEXT(nev, ev_link);
1539 if (count > sock->pending_accept) {
1541 crash_reason = "send_list > sock->pending_send";
1545 socket_log(__LINE__, sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1546 ISC_MSG_DESTROYING, "SOCKET INCONSISTENT: %s",
1549 INSIST(crash == ISC_FALSE);
1554 * Maybe free the socket.
1556 * This function will verify tht the socket is no longer in use in any way,
1557 * either internally or externally. This is the only place where this
1558 * check is to be made; if some bit of code believes that IT is done with
1559 * the socket (e.g., some reference counter reaches zero), it should call
1562 * When calling this function, the socket must be locked, and the manager
1565 * When this function returns, *socketp will be NULL. No tricks to try
1566 * to hold on to this pointer are allowed.
1569 maybe_free_socket(isc_socket_t **socketp, int lineno) {
1570 isc_socket_t *sock = *socketp;
1573 INSIST(VALID_SOCKET(sock));
1576 if (sock->pending_iocp > 0
1577 || sock->pending_recv > 0
1578 || sock->pending_send > 0
1579 || sock->pending_accept > 0
1580 || sock->references > 0
1581 || sock->pending_connect == 1
1582 || !ISC_LIST_EMPTY(sock->recv_list)
1583 || !ISC_LIST_EMPTY(sock->send_list)
1584 || !ISC_LIST_EMPTY(sock->accept_list)
1585 || sock->fd != INVALID_SOCKET) {
1586 UNLOCK(&sock->lock);
1589 UNLOCK(&sock->lock);
1591 free_socket(&sock, lineno);
1595 free_socket(isc_socket_t **sockp, int lineno) {
1596 isc_socketmgr_t *manager;
1597 isc_socket_t *sock = *sockp;
1600 manager = sock->manager;
1603 * Seems we can free the socket after all.
1605 manager = sock->manager;
1606 socket_log(__LINE__, sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
1607 ISC_MSG_DESTROYING, "freeing socket line %d fd %d lock %p semaphore %p",
1608 lineno, sock->fd, &sock->lock, sock->lock.LockSemaphore);
1611 DESTROYLOCK(&sock->lock);
1613 if (sock->recvbuf.base != NULL)
1614 isc_mem_put(manager->mctx, sock->recvbuf.base, sock->recvbuf.len);
1616 LOCK(&manager->lock);
1617 if (ISC_LINK_LINKED(sock, link))
1618 ISC_LIST_UNLINK(manager->socklist, sock, link);
1619 isc_mem_put(manager->mctx, sock, sizeof(*sock));
1621 if (ISC_LIST_EMPTY(manager->socklist))
1622 SIGNAL(&manager->shutdown_ok);
1623 UNLOCK(&manager->lock);
1627 * Create a new 'type' socket managed by 'manager'. Events
1628 * will be posted to 'task' and when dispatched 'action' will be
1629 * called with 'arg' as the arg value. The new socket is returned
1633 socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
1634 isc_socket_t **socketp, isc_socket_t *dup_socket)
1636 isc_socket_t *sock = NULL;
1637 isc_result_t result;
1638 #if defined(USE_CMSG)
1641 #if defined(SO_RCVBUF)
1642 ISC_SOCKADDR_LEN_T optlen;
1646 char strbuf[ISC_STRERRORSIZE];
1648 REQUIRE(VALID_MANAGER(manager));
1649 REQUIRE(socketp != NULL && *socketp == NULL);
1650 REQUIRE(type != isc_sockettype_fdwatch);
1652 if (dup_socket != NULL)
1653 return (ISC_R_NOTIMPLEMENTED);
1655 result = allocate_socket(manager, type, &sock);
1656 if (result != ISC_R_SUCCESS)
1661 if (dup_socket == NULL) {
1664 case isc_sockettype_udp:
1665 sock->fd = socket(pf, SOCK_DGRAM, IPPROTO_UDP);
1666 if (sock->fd != INVALID_SOCKET) {
1667 result = connection_reset_fix(sock->fd);
1668 if (result != ISC_R_SUCCESS) {
1669 socket_log(__LINE__, sock,
1670 NULL, EVENT, NULL, 0, 0,
1672 "con_reset_fix_failed",
1676 closesocket(sock->fd);
1677 _set_state(sock, SOCK_CLOSED);
1678 sock->fd = INVALID_SOCKET;
1679 free_socket(&sock, __LINE__);
1684 case isc_sockettype_tcp:
1685 sock->fd = socket(pf, SOCK_STREAM, IPPROTO_TCP);
1691 * XXX: dup() is deprecated in windows, use _dup()
1692 * instead. In future we may want to investigate
1693 * WSADuplicateSocket().
1695 sock->fd = _dup(dup_socket->fd);
1697 sock->bound = dup_socket->bound;
1701 if (sock->fd == INVALID_SOCKET) {
1702 socket_errno = WSAGetLastError();
1703 free_socket(&sock, __LINE__);
1705 switch (socket_errno) {
1708 return (ISC_R_NORESOURCES);
1710 case WSAEPROTONOSUPPORT:
1711 case WSAEPFNOSUPPORT:
1712 case WSAEAFNOSUPPORT:
1713 return (ISC_R_FAMILYNOSUPPORT);
1716 isc__strerror(socket_errno, strbuf, sizeof(strbuf));
1717 UNEXPECTED_ERROR(__FILE__, __LINE__,
1719 isc_msgcat_get(isc_msgcat,
1724 return (ISC_R_UNEXPECTED);
1728 result = make_nonblock(sock->fd);
1729 if (result != ISC_R_SUCCESS) {
1730 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
1731 "closed %d %d %d make_nonblock_failed",
1732 sock->pending_recv, sock->pending_send,
1734 closesocket(sock->fd);
1735 sock->fd = INVALID_SOCKET;
1736 free_socket(&sock, __LINE__);
1741 #if defined(USE_CMSG) || defined(SO_RCVBUF)
1742 if (type == isc_sockettype_udp) {
1744 #if defined(USE_CMSG)
1745 #if defined(ISC_PLATFORM_HAVEIPV6)
1746 #ifdef IPV6_RECVPKTINFO
1748 if ((pf == AF_INET6)
1749 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
1750 (char *)&on, sizeof(on)) < 0)) {
1751 isc__strerror(WSAGetLastError(), strbuf, sizeof(strbuf));
1752 UNEXPECTED_ERROR(__FILE__, __LINE__,
1753 "setsockopt(%d, IPV6_RECVPKTINFO) "
1755 isc_msgcat_get(isc_msgcat,
1763 if ((pf == AF_INET6)
1764 && (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
1765 (char *)&on, sizeof(on)) < 0)) {
1766 isc__strerror(WSAGetLastError(), strbuf, sizeof(strbuf));
1767 UNEXPECTED_ERROR(__FILE__, __LINE__,
1768 "setsockopt(%d, IPV6_PKTINFO) %s: %s",
1770 isc_msgcat_get(isc_msgcat,
1776 #endif /* IPV6_RECVPKTINFO */
1777 #ifdef IPV6_USE_MIN_MTU /*2292bis, not too common yet*/
1778 /* use minimum MTU */
1779 if (pf == AF_INET6) {
1780 (void)setsockopt(sock->fd, IPPROTO_IPV6,
1782 (char *)&on, sizeof(on));
1785 #endif /* ISC_PLATFORM_HAVEIPV6 */
1786 #endif /* defined(USE_CMSG) */
1788 #if defined(SO_RCVBUF)
1789 optlen = sizeof(size);
1790 if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
1791 (char *)&size, &optlen) >= 0 &&
1792 size < RCVBUFSIZE) {
1794 (void)setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
1795 (char *)&size, sizeof(size));
1800 #endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
1802 _set_state(sock, SOCK_OPEN);
1803 sock->references = 1;
1806 iocompletionport_update(sock);
1809 * Note we don't have to lock the socket like we normally would because
1810 * there are no external references to it yet.
1812 LOCK(&manager->lock);
1813 ISC_LIST_APPEND(manager->socklist, sock, link);
1814 InterlockedIncrement(&manager->totalSockets);
1815 UNLOCK(&manager->lock);
1817 socket_log(__LINE__, sock, NULL, CREATION, isc_msgcat,
1818 ISC_MSGSET_SOCKET, ISC_MSG_CREATED,
1819 "created %u type %u", sock->fd, type);
1821 return (ISC_R_SUCCESS);
1825 isc__socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
1826 isc_socket_t **socketp)
1828 return (socket_create(manager, pf, type, socketp, NULL));
1832 isc__socket_dup(isc_socket_t *sock, isc_socket_t **socketp) {
1833 REQUIRE(VALID_SOCKET(sock));
1834 REQUIRE(socketp != NULL && *socketp == NULL);
1837 return (ISC_R_NOTIMPLEMENTED);
1839 return (socket_create(sock->manager, sock->pf, sock->type,
1845 isc_socket_open(isc_socket_t *sock) {
1846 REQUIRE(VALID_SOCKET(sock));
1847 REQUIRE(sock->type != isc_sockettype_fdwatch);
1849 return (ISC_R_NOTIMPLEMENTED);
1853 * Attach to a socket. Caller must explicitly detach when it is done.
1856 isc__socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
1857 REQUIRE(VALID_SOCKET(sock));
1858 REQUIRE(socketp != NULL && *socketp == NULL);
1863 UNLOCK(&sock->lock);
1869 * Dereference a socket. If this is the last reference to it, clean things
1870 * up by destroying the socket.
1873 isc__socket_detach(isc_socket_t **socketp) {
1875 isc_boolean_t kill_socket = ISC_FALSE;
1877 REQUIRE(socketp != NULL);
1879 REQUIRE(VALID_SOCKET(sock));
1880 REQUIRE(sock->type != isc_sockettype_fdwatch);
1884 REQUIRE(sock->references > 0);
1887 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
1888 "detach_socket %d %d %d",
1889 sock->pending_recv, sock->pending_send,
1892 if (sock->references == 0 && sock->fd != INVALID_SOCKET) {
1893 closesocket(sock->fd);
1894 sock->fd = INVALID_SOCKET;
1895 _set_state(sock, SOCK_CLOSED);
1898 maybe_free_socket(&sock, __LINE__);
1904 isc_socket_close(isc_socket_t *sock) {
1905 REQUIRE(VALID_SOCKET(sock));
1906 REQUIRE(sock->type != isc_sockettype_fdwatch);
1908 return (ISC_R_NOTIMPLEMENTED);
1912 * Dequeue an item off the given socket's read queue, set the result code
1913 * in the done event to the one provided, and send it to the task it was
1916 * If the event to be sent is on a list, remove it before sending. If
1917 * asked to, send and detach from the task as well.
1919 * Caller must have the socket locked if the event is attached to the socket.
1922 send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
1925 task = (*dev)->ev_sender;
1926 (*dev)->ev_sender = sock;
1928 if (ISC_LINK_LINKED(*dev, ev_link))
1929 ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
1931 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
1932 == ISC_SOCKEVENTATTR_ATTACHED)
1933 isc_task_sendanddetach(&task, (isc_event_t **)dev);
1935 isc_task_send(task, (isc_event_t **)dev);
1941 * See comments for send_recvdone_event() above.
1944 send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
1947 INSIST(dev != NULL && *dev != NULL);
1949 task = (*dev)->ev_sender;
1950 (*dev)->ev_sender = sock;
1952 if (ISC_LINK_LINKED(*dev, ev_link))
1953 ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
1955 if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
1956 == ISC_SOCKEVENTATTR_ATTACHED)
1957 isc_task_sendanddetach(&task, (isc_event_t **)dev);
1959 isc_task_send(task, (isc_event_t **)dev);
1965 * See comments for send_recvdone_event() above.
1968 send_acceptdone_event(isc_socket_t *sock, isc_socket_newconnev_t **adev) {
1971 INSIST(adev != NULL && *adev != NULL);
1973 task = (*adev)->ev_sender;
1974 (*adev)->ev_sender = sock;
1976 if (ISC_LINK_LINKED(*adev, ev_link))
1977 ISC_LIST_DEQUEUE(sock->accept_list, *adev, ev_link);
1979 isc_task_sendanddetach(&task, (isc_event_t **)adev);
1985 * See comments for send_recvdone_event() above.
1988 send_connectdone_event(isc_socket_t *sock, isc_socket_connev_t **cdev) {
1991 INSIST(cdev != NULL && *cdev != NULL);
1993 task = (*cdev)->ev_sender;
1994 (*cdev)->ev_sender = sock;
1996 sock->connect_ev = NULL;
1998 isc_task_sendanddetach(&task, (isc_event_t **)cdev);
2004 * On entry to this function, the event delivered is the internal
2005 * readable event, and the first item on the accept_list should be
2006 * the done event we want to send. If the list is empty, this is a no-op,
2007 * so just close the new connection, unlock, and return.
2009 * Note the socket is locked before entering here
2012 internal_accept(isc_socket_t *sock, IoCompletionInfo *lpo, int accept_errno) {
2013 isc_socket_newconnev_t *adev;
2014 isc_result_t result = ISC_R_SUCCESS;
2015 isc_socket_t *nsock;
2016 struct sockaddr *localaddr;
2017 int localaddr_len = sizeof(*localaddr);
2018 struct sockaddr *remoteaddr;
2019 int remoteaddr_len = sizeof(*remoteaddr);
2021 INSIST(VALID_SOCKET(sock));
2025 socket_log(__LINE__, sock, NULL, TRACE,
2026 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
2027 "internal_accept called");
2029 INSIST(sock->listener);
2031 INSIST(sock->pending_iocp > 0);
2032 sock->pending_iocp--;
2033 INSIST(sock->pending_accept > 0);
2034 sock->pending_accept--;
2039 * If the event is no longer in the list we can just return.
2041 if (!acceptdone_is_active(sock, adev))
2044 nsock = adev->newsocket;
2047 * Pull off the done event.
2049 ISC_LIST_UNLINK(sock->accept_list, adev, ev_link);
2052 * Extract the addresses from the socket, copy them into the structure,
2053 * and return the new socket.
2055 ISCGetAcceptExSockaddrs(lpo->acceptbuffer, 0,
2056 sizeof(SOCKADDR_STORAGE) + 16, sizeof(SOCKADDR_STORAGE) + 16,
2057 (LPSOCKADDR *)&localaddr, &localaddr_len,
2058 (LPSOCKADDR *)&remoteaddr, &remoteaddr_len);
2059 memcpy(&adev->address.type, remoteaddr, remoteaddr_len);
2060 adev->address.length = remoteaddr_len;
2061 nsock->address = adev->address;
2062 nsock->pf = adev->address.type.sa.sa_family;
2064 socket_log(__LINE__, nsock, &nsock->address, TRACE,
2065 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
2066 "internal_accept parent %p", sock);
2068 result = make_nonblock(adev->newsocket->fd);
2069 INSIST(result == ISC_R_SUCCESS);
2071 INSIST(setsockopt(nsock->fd, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT,
2072 (char *)&sock->fd, sizeof(sock->fd)) == 0);
2075 * Hook it up into the manager.
2078 nsock->connected = 1;
2079 _set_state(nsock, SOCK_OPEN);
2081 LOCK(&nsock->manager->lock);
2082 ISC_LIST_APPEND(nsock->manager->socklist, nsock, link);
2083 InterlockedIncrement(&nsock->manager->totalSockets);
2084 UNLOCK(&nsock->manager->lock);
2086 socket_log(__LINE__, sock, &nsock->address, CREATION,
2087 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
2088 "accepted_connection new_socket %p fd %d",
2091 adev->result = result;
2092 send_acceptdone_event(sock, &adev);
2096 UNLOCK(&sock->lock);
2098 HeapFree(hHeapHandle, 0, lpo->acceptbuffer);
2099 lpo->acceptbuffer = NULL;
2103 * Called when a socket with a pending connect() finishes.
2104 * Note that the socket is locked before entering.
2107 internal_connect(isc_socket_t *sock, IoCompletionInfo *lpo, int connect_errno) {
2108 isc_socket_connev_t *cdev;
2109 char strbuf[ISC_STRERRORSIZE];
2111 INSIST(VALID_SOCKET(sock));
2115 INSIST(sock->pending_iocp > 0);
2116 sock->pending_iocp--;
2117 INSIST(sock->pending_connect == 1);
2118 sock->pending_connect = 0;
2121 * Has this event been canceled?
2124 if (!connectdone_is_active(sock, cdev)) {
2125 sock->pending_connect = 0;
2126 if (sock->fd != INVALID_SOCKET) {
2127 closesocket(sock->fd);
2128 sock->fd = INVALID_SOCKET;
2129 _set_state(sock, SOCK_CLOSED);
2132 UNLOCK(&sock->lock);
2137 * Check possible Windows network event error status here.
2139 if (connect_errno != 0) {
2141 * If the error is SOFT, just try again on this
2142 * fd and pretend nothing strange happened.
2144 if (SOFT_ERROR(connect_errno) ||
2145 connect_errno == WSAEINPROGRESS) {
2146 sock->pending_connect = 1;
2148 UNLOCK(&sock->lock);
2153 * Translate other errors into ISC_R_* flavors.
2155 switch (connect_errno) {
2156 #define ERROR_MATCH(a, b) case a: cdev->result = b; break;
2157 ERROR_MATCH(WSAEACCES, ISC_R_NOPERM);
2158 ERROR_MATCH(WSAEADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
2159 ERROR_MATCH(WSAEAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
2160 ERROR_MATCH(WSAECONNREFUSED, ISC_R_CONNREFUSED);
2161 ERROR_MATCH(WSAEHOSTUNREACH, ISC_R_HOSTUNREACH);
2162 ERROR_MATCH(WSAEHOSTDOWN, ISC_R_HOSTDOWN);
2163 ERROR_MATCH(WSAENETUNREACH, ISC_R_NETUNREACH);
2164 ERROR_MATCH(WSAENETDOWN, ISC_R_NETDOWN);
2165 ERROR_MATCH(WSAENOBUFS, ISC_R_NORESOURCES);
2166 ERROR_MATCH(WSAECONNRESET, ISC_R_CONNECTIONRESET);
2167 ERROR_MATCH(WSAECONNABORTED, ISC_R_CONNECTIONRESET);
2168 ERROR_MATCH(WSAETIMEDOUT, ISC_R_TIMEDOUT);
2171 cdev->result = ISC_R_UNEXPECTED;
2172 isc__strerror(connect_errno, strbuf, sizeof(strbuf));
2173 UNEXPECTED_ERROR(__FILE__, __LINE__,
2174 "internal_connect: connect() %s",
2178 INSIST(setsockopt(sock->fd, SOL_SOCKET,
2179 SO_UPDATE_CONNECT_CONTEXT, NULL, 0) == 0);
2180 cdev->result = ISC_R_SUCCESS;
2181 sock->connected = 1;
2182 socket_log(__LINE__, sock, &sock->address, IOEVENT,
2183 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
2184 "internal_connect: success");
2187 send_connectdone_event(sock, &cdev);
2189 UNLOCK(&sock->lock);
2193 * Loop through the socket, returning ISC_R_EOF for each done event pending.
2196 send_recvdone_abort(isc_socket_t *sock, isc_result_t result) {
2197 isc_socketevent_t *dev;
2199 while (!ISC_LIST_EMPTY(sock->recv_list)) {
2200 dev = ISC_LIST_HEAD(sock->recv_list);
2201 dev->result = result;
2202 send_recvdone_event(sock, &dev);
2207 * Take the data we received in our private buffer, and if any recv() calls on
2208 * our list are satisfied, send the corresponding done event.
2210 * If we need more data (there are still items on the recv_list after we consume all
2211 * our data) then arrange for another system recv() call to fill our buffers.
2214 internal_recv(isc_socket_t *sock, int nbytes)
2216 INSIST(VALID_SOCKET(sock));
2221 socket_log(__LINE__, sock, NULL, IOEVENT,
2222 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
2223 "internal_recv: %d bytes received", nbytes);
2226 * If we got here, the I/O operation succeeded. However, we might still have removed this
2227 * event from our notification list (or never placed it on it due to immediate completion.)
2228 * Handle the reference counting here, and handle the cancellation event just after.
2230 INSIST(sock->pending_iocp > 0);
2231 sock->pending_iocp--;
2232 INSIST(sock->pending_recv > 0);
2233 sock->pending_recv--;
2236 * The only way we could have gotten here is that our I/O has successfully completed.
2237 * Update our pointers, and move on. The only odd case here is that we might not
2238 * have received enough data on a TCP stream to satisfy the minimum requirements. If
2239 * this is the case, we will re-issue the recv() call for what we need.
2241 * We do check for a recv() of 0 bytes on a TCP stream. This means the remote end
2244 if (nbytes == 0 && sock->type == isc_sockettype_tcp) {
2245 send_recvdone_abort(sock, ISC_R_EOF);
2246 maybe_free_socket(&sock, __LINE__);
2249 sock->recvbuf.remaining = nbytes;
2250 sock->recvbuf.consume_position = sock->recvbuf.base;
2251 completeio_recv(sock);
2254 * If there are more receivers waiting for data, queue another receive
2257 queue_receive_request(sock);
2260 * Unlock and/or destroy if we are the last thing this socket has left to do.
2262 maybe_free_socket(&sock, __LINE__);
2266 internal_send(isc_socket_t *sock, isc_socketevent_t *dev,
2267 struct msghdr *messagehdr, int nbytes, int send_errno, IoCompletionInfo *lpo)
2272 * Find out what socket this is and lock it.
2274 INSIST(VALID_SOCKET(sock));
2279 socket_log(__LINE__, sock, NULL, IOEVENT,
2280 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
2281 "internal_send: task got socket event %p", dev);
2283 buffer = ISC_LIST_HEAD(lpo->bufferlist);
2284 while (buffer != NULL) {
2285 ISC_LIST_DEQUEUE(lpo->bufferlist, buffer, link);
2287 socket_log(__LINE__, sock, NULL, TRACE,
2288 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
2289 "free_buffer %p %p", buffer, buffer->buf);
2291 HeapFree(hHeapHandle, 0, buffer->buf);
2292 HeapFree(hHeapHandle, 0, buffer);
2293 buffer = ISC_LIST_HEAD(lpo->bufferlist);
2296 INSIST(sock->pending_iocp > 0);
2297 sock->pending_iocp--;
2298 INSIST(sock->pending_send > 0);
2299 sock->pending_send--;
2301 /* If the event is no longer in the list we can just return */
2302 if (!senddone_is_active(sock, dev))
2306 * Set the error code and send things on its way.
2308 switch (completeio_send(sock, dev, messagehdr, nbytes, send_errno)) {
2313 send_senddone_event(sock, &dev);
2318 maybe_free_socket(&sock, __LINE__);
2322 * These return if the done event passed in is on the list (or for connect, is
2323 * the one we're waiting for. Using these ensures we will not double-send an
2326 static isc_boolean_t
2327 senddone_is_active(isc_socket_t *sock, isc_socketevent_t *dev)
2329 isc_socketevent_t *ldev;
2331 ldev = ISC_LIST_HEAD(sock->send_list);
2332 while (ldev != NULL && ldev != dev)
2333 ldev = ISC_LIST_NEXT(ldev, ev_link);
2335 return (ldev == NULL ? ISC_FALSE : ISC_TRUE);
2338 static isc_boolean_t
2339 acceptdone_is_active(isc_socket_t *sock, isc_socket_newconnev_t *dev)
2341 isc_socket_newconnev_t *ldev;
2343 ldev = ISC_LIST_HEAD(sock->accept_list);
2344 while (ldev != NULL && ldev != dev)
2345 ldev = ISC_LIST_NEXT(ldev, ev_link);
2347 return (ldev == NULL ? ISC_FALSE : ISC_TRUE);
2350 static isc_boolean_t
2351 connectdone_is_active(isc_socket_t *sock, isc_socket_connev_t *dev)
2353 return (sock->connect_ev == dev ? ISC_TRUE : ISC_FALSE);
2357 // The Windows network stack seems to have two very distinct paths depending
2358 // on what is installed. Specifically, if something is looking at network
2359 // connections (like an anti-virus or anti-malware application, such as
2360 // McAfee products) Windows may return additional error conditions which
2361 // were not previously returned.
2363 // One specific one is when a TCP SYN scan is used. In this situation,
2364 // Windows responds with the SYN-ACK, but the scanner never responds with
2365 // the 3rd packet, the ACK. Windows consiers this a partially open connection.
2366 // Most Unix networking stacks, and Windows without McAfee installed, will
2367 // not return this to the caller. However, with this product installed,
2368 // Windows returns this as a failed status on the Accept() call. Here, we
2369 // will just re-issue the ISCAcceptEx() call as if nothing had happened.
2371 // This code should only be called when the listening socket has received
2372 // such an error. Additionally, the "parent" socket must be locked.
2373 // Additionally, the lpo argument is re-used here, and must not be freed
2377 restart_accept(isc_socket_t *parent, IoCompletionInfo *lpo)
2379 isc_socket_t *nsock = lpo->adev->newsocket;
2383 * AcceptEx() requires we pass in a socket. Note that we carefully
2384 * do not close the previous socket in case of an error message returned by
2385 * our new socket() call. If we return an error here, our caller will
2388 new_fd = socket(parent->pf, SOCK_STREAM, IPPROTO_TCP);
2389 if (nsock->fd == INVALID_SOCKET) {
2390 return (ISC_R_FAILURE); // parent will ask windows for error message
2392 closesocket(nsock->fd);
2395 memset(&lpo->overlapped, 0, sizeof(lpo->overlapped));
2397 ISCAcceptEx(parent->fd,
2398 nsock->fd, /* Accepted Socket */
2399 lpo->acceptbuffer, /* Buffer for initial Recv */
2400 0, /* Length of Buffer */
2401 sizeof(SOCKADDR_STORAGE) + 16, /* Local address length + 16 */
2402 sizeof(SOCKADDR_STORAGE) + 16, /* Remote address lengh + 16 */
2403 (LPDWORD)&lpo->received_bytes, /* Bytes Recved */
2404 (LPOVERLAPPED)lpo /* Overlapped structure */
2407 InterlockedDecrement(&nsock->manager->iocp_total);
2408 iocompletionport_update(nsock);
2410 return (ISC_R_SUCCESS);
2414 * This is the I/O Completion Port Worker Function. It loops forever
2415 * waiting for I/O to complete and then forwards them for further
2416 * processing. There are a number of these in separate threads.
2418 static isc_threadresult_t WINAPI
2419 SocketIoThread(LPVOID ThreadContext) {
2420 isc_socketmgr_t *manager = ThreadContext;
2421 BOOL bSuccess = FALSE;
2423 IoCompletionInfo *lpo = NULL;
2424 isc_socket_t *sock = NULL;
2426 struct msghdr *messagehdr = NULL;
2428 char strbuf[ISC_STRERRORSIZE];
2431 REQUIRE(VALID_MANAGER(manager));
2434 * Set the thread priority high enough so I/O will
2435 * preempt normal recv packet processing, but not
2436 * higher than the timer sync thread.
2438 if (!SetThreadPriority(GetCurrentThread(),
2439 THREAD_PRIORITY_ABOVE_NORMAL)) {
2440 errval = GetLastError();
2441 isc__strerror(errval, strbuf, sizeof(strbuf));
2442 FATAL_ERROR(__FILE__, __LINE__,
2443 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
2445 "Can't set thread priority: %s"),
2450 * Loop forever waiting on I/O Completions and then processing them
2454 bSuccess = GetQueuedCompletionStatus(manager->hIoCompletionPort,
2455 &nbytes, (LPDWORD)&sock,
2456 (LPWSAOVERLAPPED *)&lpo,
2458 if (lpo == NULL) /* Received request to exit */
2461 REQUIRE(VALID_SOCKET(sock));
2463 request = lpo->request_type;
2467 isc_result_t isc_result;
2470 * Did the I/O operation complete?
2472 errstatus = GetLastError();
2473 isc_result = isc__errno2resultx(errstatus, __FILE__, __LINE__);
2479 INSIST(sock->pending_iocp > 0);
2480 sock->pending_iocp--;
2481 INSIST(sock->pending_recv > 0);
2482 sock->pending_recv--;
2483 if (!sock->connected &&
2484 ((errstatus == ERROR_HOST_UNREACHABLE) ||
2485 (errstatus == WSAENETRESET) ||
2486 (errstatus == WSAECONNRESET))) {
2487 /* ignore soft errors */
2488 queue_receive_request(sock);
2491 send_recvdone_abort(sock, isc_result);
2492 if (isc_result == ISC_R_UNEXPECTED) {
2493 UNEXPECTED_ERROR(__FILE__, __LINE__,
2494 "SOCKET_RECV: Windows error code: %d, returning ISC error %d",
2495 errstatus, isc_result);
2500 INSIST(sock->pending_iocp > 0);
2501 sock->pending_iocp--;
2502 INSIST(sock->pending_send > 0);
2503 sock->pending_send--;
2504 if (senddone_is_active(sock, lpo->dev)) {
2505 lpo->dev->result = isc_result;
2506 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
2508 send_senddone_event(sock, &lpo->dev);
2513 INSIST(sock->pending_iocp > 0);
2514 INSIST(sock->pending_accept > 0);
2516 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
2517 "Accept: errstatus=%d isc_result=%d", errstatus, isc_result);
2519 if (acceptdone_is_active(sock, lpo->adev)) {
2520 if (restart_accept(sock, lpo) == ISC_R_SUCCESS) {
2521 UNLOCK(&sock->lock);
2524 errstatus = GetLastError();
2525 isc_result = isc__errno2resultx(errstatus, __FILE__, __LINE__);
2526 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
2527 "restart_accept() failed: errstatus=%d isc_result=%d",
2528 errstatus, isc_result);
2532 sock->pending_iocp--;
2533 sock->pending_accept--;
2534 if (acceptdone_is_active(sock, lpo->adev)) {
2535 closesocket(lpo->adev->newsocket->fd);
2536 lpo->adev->newsocket->fd = INVALID_SOCKET;
2537 lpo->adev->newsocket->references--;
2538 free_socket(&lpo->adev->newsocket, __LINE__);
2539 lpo->adev->result = isc_result;
2540 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
2542 send_acceptdone_event(sock, &lpo->adev);
2546 case SOCKET_CONNECT:
2547 INSIST(sock->pending_iocp > 0);
2548 sock->pending_iocp--;
2549 INSIST(sock->pending_connect == 1);
2550 sock->pending_connect = 0;
2551 if (connectdone_is_active(sock, lpo->cdev)) {
2552 lpo->cdev->result = isc_result;
2553 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
2554 "canceled_connect");
2555 send_connectdone_event(sock, &lpo->cdev);
2559 maybe_free_socket(&sock, __LINE__);
2562 HeapFree(hHeapHandle, 0, lpo);
2566 messagehdr = &lpo->messagehdr;
2570 internal_recv(sock, nbytes);
2573 internal_send(sock, lpo->dev, messagehdr, nbytes, errstatus, lpo);
2576 internal_accept(sock, lpo, errstatus);
2578 case SOCKET_CONNECT:
2579 internal_connect(sock, lpo, errstatus);
2584 HeapFree(hHeapHandle, 0, lpo);
2588 * Exit Completion Port Thread
2590 manager_log(manager, TRACE,
2591 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2592 ISC_MSG_EXITING, "SocketIoThread exiting"));
2593 return ((isc_threadresult_t)0);
2597 * Create a new socket manager.
2600 isc__socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
2601 return (isc_socketmgr_create2(mctx, managerp, 0));
2605 isc__socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
2606 unsigned int maxsocks)
2608 isc_socketmgr_t *manager;
2609 isc_result_t result;
2611 REQUIRE(managerp != NULL && *managerp == NULL);
2614 return (ISC_R_NOTIMPLEMENTED);
2616 manager = isc_mem_get(mctx, sizeof(*manager));
2617 if (manager == NULL)
2618 return (ISC_R_NOMEMORY);
2622 manager->magic = SOCKET_MANAGER_MAGIC;
2623 manager->mctx = NULL;
2624 manager->stats = NULL;
2625 ISC_LIST_INIT(manager->socklist);
2626 result = isc_mutex_init(&manager->lock);
2627 if (result != ISC_R_SUCCESS) {
2628 isc_mem_put(mctx, manager, sizeof(*manager));
2631 if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
2632 DESTROYLOCK(&manager->lock);
2633 isc_mem_put(mctx, manager, sizeof(*manager));
2634 UNEXPECTED_ERROR(__FILE__, __LINE__,
2635 "isc_condition_init() %s",
2636 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2637 ISC_MSG_FAILED, "failed"));
2638 return (ISC_R_UNEXPECTED);
2641 isc_mem_attach(mctx, &manager->mctx);
2643 iocompletionport_init(manager); /* Create the Completion Ports */
2645 manager->bShutdown = ISC_FALSE;
2646 manager->totalSockets = 0;
2647 manager->iocp_total = 0;
2649 *managerp = manager;
2651 return (ISC_R_SUCCESS);
2655 isc__socketmgr_getmaxsockets(isc_socketmgr_t *manager, unsigned int *nsockp) {
2656 REQUIRE(VALID_MANAGER(manager));
2657 REQUIRE(nsockp != NULL);
2659 return (ISC_R_NOTIMPLEMENTED);
2663 isc__socketmgr_setstats(isc_socketmgr_t *manager, isc_stats_t *stats) {
2664 REQUIRE(VALID_MANAGER(manager));
2665 REQUIRE(ISC_LIST_EMPTY(manager->socklist));
2666 REQUIRE(manager->stats == NULL);
2667 REQUIRE(isc_stats_ncounters(stats) == isc_sockstatscounter_max);
2669 isc_stats_attach(stats, &manager->stats);
2673 isc__socketmgr_destroy(isc_socketmgr_t **managerp) {
2674 isc_socketmgr_t *manager;
2679 * Destroy a socket manager.
2682 REQUIRE(managerp != NULL);
2683 manager = *managerp;
2684 REQUIRE(VALID_MANAGER(manager));
2686 LOCK(&manager->lock);
2689 * Wait for all sockets to be destroyed.
2691 while (!ISC_LIST_EMPTY(manager->socklist)) {
2692 manager_log(manager, CREATION,
2693 isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
2694 ISC_MSG_SOCKETSREMAIN,
2696 WAIT(&manager->shutdown_ok, &manager->lock);
2699 UNLOCK(&manager->lock);
2702 * Here, we need to had some wait code for the completion port
2705 signal_iocompletionport_exit(manager);
2706 manager->bShutdown = ISC_TRUE;
2709 * Wait for threads to exit.
2711 for (i = 0; i < manager->maxIOCPThreads; i++) {
2712 if (isc_thread_join((isc_thread_t) manager->hIOCPThreads[i],
2713 NULL) != ISC_R_SUCCESS)
2714 UNEXPECTED_ERROR(__FILE__, __LINE__,
2715 "isc_thread_join() for Completion Port %s",
2716 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
2717 ISC_MSG_FAILED, "failed"));
2723 CloseHandle(manager->hIoCompletionPort);
2725 (void)isc_condition_destroy(&manager->shutdown_ok);
2727 DESTROYLOCK(&manager->lock);
2728 if (manager->stats != NULL)
2729 isc_stats_detach(&manager->stats);
2731 mctx= manager->mctx;
2732 isc_mem_put(mctx, manager, sizeof(*manager));
2734 isc_mem_detach(&mctx);
2740 queue_receive_event(isc_socket_t *sock, isc_task_t *task, isc_socketevent_t *dev)
2742 isc_task_t *ntask = NULL;
2744 isc_task_attach(task, &ntask);
2745 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
2748 * Enqueue the request.
2750 INSIST(!ISC_LINK_LINKED(dev, ev_link));
2751 ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
2753 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
2754 "queue_receive_event: event %p -> task %p",
2759 * Check the pending receive queue, and if we have data pending, give it to this
2760 * caller. If we have none, queue an I/O request. If this caller is not the first
2761 * on the list, then we will just queue this event and return.
2763 * Caller must have the socket locked.
2766 socket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
2770 isc_task_t *ntask = NULL;
2771 isc_result_t result = ISC_R_SUCCESS;
2774 dev->ev_sender = task;
2776 if (sock->fd == INVALID_SOCKET)
2780 * Queue our event on the list of things to do. Call our function to
2781 * attempt to fill buffers as much as possible, and return done events.
2782 * We are going to lie about our handling of the ISC_SOCKFLAG_IMMEDIATE
2783 * here and tell our caller that we could not satisfy it immediately.
2785 queue_receive_event(sock, task, dev);
2786 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
2787 result = ISC_R_INPROGRESS;
2789 completeio_recv(sock);
2792 * If there are more receivers waiting for data, queue another receive
2795 queue_receive_request(sock);
2801 isc__socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
2802 unsigned int minimum, isc_task_t *task,
2803 isc_taskaction_t action, const void *arg)
2805 isc_socketevent_t *dev;
2806 isc_socketmgr_t *manager;
2807 unsigned int iocount;
2808 isc_buffer_t *buffer;
2811 REQUIRE(VALID_SOCKET(sock));
2816 * Make sure that the socket is not closed. XXXMLG change error here?
2818 if (sock->fd == INVALID_SOCKET) {
2819 UNLOCK(&sock->lock);
2820 return (ISC_R_CONNREFUSED);
2823 REQUIRE(buflist != NULL);
2824 REQUIRE(!ISC_LIST_EMPTY(*buflist));
2825 REQUIRE(task != NULL);
2826 REQUIRE(action != NULL);
2828 manager = sock->manager;
2829 REQUIRE(VALID_MANAGER(manager));
2831 iocount = isc_bufferlist_availablecount(buflist);
2832 REQUIRE(iocount > 0);
2834 INSIST(sock->bound);
2836 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
2838 UNLOCK(&sock->lock);
2839 return (ISC_R_NOMEMORY);
2843 * UDP sockets are always partial read
2845 if (sock->type == isc_sockettype_udp)
2849 dev->minimum = iocount;
2851 dev->minimum = minimum;
2855 * Move each buffer from the passed in list to our internal one.
2857 buffer = ISC_LIST_HEAD(*buflist);
2858 while (buffer != NULL) {
2859 ISC_LIST_DEQUEUE(*buflist, buffer, link);
2860 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
2861 buffer = ISC_LIST_HEAD(*buflist);
2864 ret = socket_recv(sock, dev, task, 0);
2866 UNLOCK(&sock->lock);
2871 isc__socket_recv(isc_socket_t *sock, isc_region_t *region,
2872 unsigned int minimum, isc_task_t *task,
2873 isc_taskaction_t action, const void *arg)
2875 isc_socketevent_t *dev;
2876 isc_socketmgr_t *manager;
2879 REQUIRE(VALID_SOCKET(sock));
2884 * make sure that the socket's not closed
2886 if (sock->fd == INVALID_SOCKET) {
2887 UNLOCK(&sock->lock);
2888 return (ISC_R_CONNREFUSED);
2890 REQUIRE(action != NULL);
2892 manager = sock->manager;
2893 REQUIRE(VALID_MANAGER(manager));
2895 INSIST(sock->bound);
2897 dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
2899 UNLOCK(&sock->lock);
2900 return (ISC_R_NOMEMORY);
2903 ret = isc_socket_recv2(sock, region, minimum, task, dev, 0);
2904 UNLOCK(&sock->lock);
2909 isc__socket_recv2(isc_socket_t *sock, isc_region_t *region,
2910 unsigned int minimum, isc_task_t *task,
2911 isc_socketevent_t *event, unsigned int flags)
2915 REQUIRE(VALID_SOCKET(sock));
2919 event->result = ISC_R_UNEXPECTED;
2920 event->ev_sender = sock;
2922 * make sure that the socket's not closed
2924 if (sock->fd == INVALID_SOCKET) {
2925 UNLOCK(&sock->lock);
2926 return (ISC_R_CONNREFUSED);
2929 ISC_LIST_INIT(event->bufferlist);
2930 event->region = *region;
2933 event->attributes = 0;
2936 * UDP sockets are always partial read.
2938 if (sock->type == isc_sockettype_udp)
2942 event->minimum = region->length;
2944 event->minimum = minimum;
2947 ret = socket_recv(sock, event, task, flags);
2948 UNLOCK(&sock->lock);
2953 * Caller must have the socket locked.
2956 socket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
2957 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
2963 isc_task_t *ntask = NULL;
2964 isc_result_t result = ISC_R_SUCCESS;
2966 dev->ev_sender = task;
2968 set_dev_address(address, sock, dev);
2969 if (pktinfo != NULL) {
2970 socket_log(__LINE__, sock, NULL, TRACE, isc_msgcat, ISC_MSGSET_SOCKET,
2971 ISC_MSG_PKTINFOPROVIDED,
2972 "pktinfo structure provided, ifindex %u (set to 0)",
2973 pktinfo->ipi6_ifindex);
2975 dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
2976 dev->pktinfo = *pktinfo;
2978 * Set the pktinfo index to 0 here, to let the kernel decide
2979 * what interface it should send on.
2981 dev->pktinfo.ipi6_ifindex = 0;
2984 io_state = startio_send(sock, dev, &cc, &send_errno);
2986 case DOIO_PENDING: /* I/O started. Nothing more to do */
2989 * We couldn't send all or part of the request right now, so
2990 * queue it unless ISC_SOCKFLAG_NORETRY is set.
2992 if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
2993 isc_task_attach(task, &ntask);
2994 dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
2997 * Enqueue the request.
2999 INSIST(!ISC_LINK_LINKED(dev, ev_link));
3000 ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
3002 socket_log(__LINE__, sock, NULL, EVENT, NULL, 0, 0,
3003 "socket_send: event %p -> task %p",
3006 if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
3007 result = ISC_R_INPROGRESS;
3019 isc__socket_send(isc_socket_t *sock, isc_region_t *region,
3020 isc_task_t *task, isc_taskaction_t action, const void *arg)
3023 * REQUIRE() checking is performed in isc_socket_sendto().
3025 return (isc_socket_sendto(sock, region, task, action, arg, NULL,
3030 isc__socket_sendto(isc_socket_t *sock, isc_region_t *region,
3031 isc_task_t *task, isc_taskaction_t action, const void *arg,
3032 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
3034 isc_socketevent_t *dev;
3035 isc_socketmgr_t *manager;
3038 REQUIRE(VALID_SOCKET(sock));
3039 REQUIRE(sock->type != isc_sockettype_fdwatch);
3045 * make sure that the socket's not closed
3047 if (sock->fd == INVALID_SOCKET) {
3048 UNLOCK(&sock->lock);
3049 return (ISC_R_CONNREFUSED);
3051 REQUIRE(region != NULL);
3052 REQUIRE(task != NULL);
3053 REQUIRE(action != NULL);
3055 manager = sock->manager;
3056 REQUIRE(VALID_MANAGER(manager));
3058 INSIST(sock->bound);
3060 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
3062 UNLOCK(&sock->lock);
3063 return (ISC_R_NOMEMORY);
3065 dev->region = *region;
3067 ret = socket_send(sock, dev, task, address, pktinfo, 0);
3068 UNLOCK(&sock->lock);
3073 isc__socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
3074 isc_task_t *task, isc_taskaction_t action, const void *arg)
3076 return (isc_socket_sendtov(sock, buflist, task, action, arg, NULL,
3081 isc__socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
3082 isc_task_t *task, isc_taskaction_t action, const void *arg,
3083 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
3085 isc_socketevent_t *dev;
3086 isc_socketmgr_t *manager;
3087 unsigned int iocount;
3088 isc_buffer_t *buffer;
3091 REQUIRE(VALID_SOCKET(sock));
3097 * make sure that the socket's not closed
3099 if (sock->fd == INVALID_SOCKET) {
3100 UNLOCK(&sock->lock);
3101 return (ISC_R_CONNREFUSED);
3103 REQUIRE(buflist != NULL);
3104 REQUIRE(!ISC_LIST_EMPTY(*buflist));
3105 REQUIRE(task != NULL);
3106 REQUIRE(action != NULL);
3108 manager = sock->manager;
3109 REQUIRE(VALID_MANAGER(manager));
3111 iocount = isc_bufferlist_usedcount(buflist);
3112 REQUIRE(iocount > 0);
3114 dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
3116 UNLOCK(&sock->lock);
3117 return (ISC_R_NOMEMORY);
3121 * Move each buffer from the passed in list to our internal one.
3123 buffer = ISC_LIST_HEAD(*buflist);
3124 while (buffer != NULL) {
3125 ISC_LIST_DEQUEUE(*buflist, buffer, link);
3126 ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
3127 buffer = ISC_LIST_HEAD(*buflist);
3130 ret = socket_send(sock, dev, task, address, pktinfo, 0);
3131 UNLOCK(&sock->lock);
3136 isc__socket_sendto2(isc_socket_t *sock, isc_region_t *region,
3138 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
3139 isc_socketevent_t *event, unsigned int flags)
3143 REQUIRE(VALID_SOCKET(sock));
3147 REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
3148 if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
3149 REQUIRE(sock->type == isc_sockettype_udp);
3150 event->ev_sender = sock;
3151 event->result = ISC_R_UNEXPECTED;
3153 * make sure that the socket's not closed
3155 if (sock->fd == INVALID_SOCKET) {
3156 UNLOCK(&sock->lock);
3157 return (ISC_R_CONNREFUSED);
3159 ISC_LIST_INIT(event->bufferlist);
3160 event->region = *region;
3163 event->attributes = 0;
3165 ret = socket_send(sock, event, task, address, pktinfo, flags);
3166 UNLOCK(&sock->lock);
3171 isc__socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
3172 unsigned int options) {
3174 char strbuf[ISC_STRERRORSIZE];
3177 REQUIRE(VALID_SOCKET(sock));
3182 * make sure that the socket's not closed
3184 if (sock->fd == INVALID_SOCKET) {
3185 UNLOCK(&sock->lock);
3186 return (ISC_R_CONNREFUSED);
3189 INSIST(!sock->bound);
3190 INSIST(!sock->dupped);
3192 if (sock->pf != sockaddr->type.sa.sa_family) {
3193 UNLOCK(&sock->lock);
3194 return (ISC_R_FAMILYMISMATCH);
3197 * Only set SO_REUSEADDR when we want a specific port.
3199 if ((options & ISC_SOCKET_REUSEADDRESS) != 0 &&
3200 isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
3201 setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (char *)&on,
3203 UNEXPECTED_ERROR(__FILE__, __LINE__,
3204 "setsockopt(%d) %s", sock->fd,
3205 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
3206 ISC_MSG_FAILED, "failed"));
3209 if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
3210 bind_errno = WSAGetLastError();
3211 UNLOCK(&sock->lock);
3212 switch (bind_errno) {
3214 return (ISC_R_NOPERM);
3215 case WSAEADDRNOTAVAIL:
3216 return (ISC_R_ADDRNOTAVAIL);
3218 return (ISC_R_ADDRINUSE);
3220 return (ISC_R_BOUND);
3222 isc__strerror(bind_errno, strbuf, sizeof(strbuf));
3223 UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
3225 return (ISC_R_UNEXPECTED);
3229 socket_log(__LINE__, sock, sockaddr, TRACE,
3230 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
3233 UNLOCK(&sock->lock);
3234 return (ISC_R_SUCCESS);
3238 isc__socket_filter(isc_socket_t *sock, const char *filter) {
3242 REQUIRE(VALID_SOCKET(sock));
3243 return (ISC_R_NOTIMPLEMENTED);
3247 * Set up to listen on a given socket. We do this by creating an internal
3248 * event that will be dispatched when the socket has read activity. The
3249 * watcher will send the internal event to the task when there is a new
3252 * Unlike in read, we don't preallocate a done event here. Every time there
3253 * is a new connection we'll have to allocate a new one anyway, so we might
3254 * as well keep things simple rather than having to track them.
3257 isc__socket_listen(isc_socket_t *sock, unsigned int backlog) {
3258 char strbuf[ISC_STRERRORSIZE];
3260 REQUIRE(VALID_SOCKET(sock));
3266 * make sure that the socket's not closed
3268 if (sock->fd == INVALID_SOCKET) {
3269 UNLOCK(&sock->lock);
3270 return (ISC_R_CONNREFUSED);
3273 REQUIRE(!sock->listener);
3274 REQUIRE(sock->bound);
3275 REQUIRE(sock->type == isc_sockettype_tcp);
3278 backlog = SOMAXCONN;
3280 if (listen(sock->fd, (int)backlog) < 0) {
3281 UNLOCK(&sock->lock);
3282 isc__strerror(WSAGetLastError(), strbuf, sizeof(strbuf));
3284 UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
3286 return (ISC_R_UNEXPECTED);
3289 socket_log(__LINE__, sock, NULL, TRACE,
3290 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "listening");
3292 _set_state(sock, SOCK_LISTEN);
3294 UNLOCK(&sock->lock);
3295 return (ISC_R_SUCCESS);
3299 * This should try to do aggressive accept() XXXMLG
3302 isc__socket_accept(isc_socket_t *sock,
3303 isc_task_t *task, isc_taskaction_t action, const void *arg)
3305 isc_socket_newconnev_t *adev;
3306 isc_socketmgr_t *manager;
3307 isc_task_t *ntask = NULL;
3308 isc_socket_t *nsock;
3309 isc_result_t result;
3310 IoCompletionInfo *lpo;
3312 REQUIRE(VALID_SOCKET(sock));
3314 manager = sock->manager;
3315 REQUIRE(VALID_MANAGER(manager));
3321 * make sure that the socket's not closed
3323 if (sock->fd == INVALID_SOCKET) {
3324 UNLOCK(&sock->lock);
3325 return (ISC_R_CONNREFUSED);
3328 REQUIRE(sock->listener);
3331 * Sender field is overloaded here with the task we will be sending
3332 * this event to. Just before the actual event is delivered the
3333 * actual ev_sender will be touched up to be the socket.
3335 adev = (isc_socket_newconnev_t *)
3336 isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
3337 action, arg, sizeof(*adev));
3339 UNLOCK(&sock->lock);
3340 return (ISC_R_NOMEMORY);
3342 ISC_LINK_INIT(adev, ev_link);
3344 result = allocate_socket(manager, sock->type, &nsock);
3345 if (result != ISC_R_SUCCESS) {
3346 isc_event_free((isc_event_t **)&adev);
3347 UNLOCK(&sock->lock);
3352 * AcceptEx() requires we pass in a socket.
3354 nsock->fd = socket(sock->pf, SOCK_STREAM, IPPROTO_TCP);
3355 if (nsock->fd == INVALID_SOCKET) {
3356 free_socket(&nsock, __LINE__);
3357 isc_event_free((isc_event_t **)&adev);
3358 UNLOCK(&sock->lock);
3359 return (ISC_R_FAILURE); // XXXMLG need real error message
3363 * Attach to socket and to task.
3365 isc_task_attach(task, &ntask);
3366 if (isc_task_exiting(ntask)) {
3367 free_socket(&nsock, __LINE__);
3368 isc_task_detach(&ntask);
3369 isc_event_free(ISC_EVENT_PTR(&adev));
3370 UNLOCK(&sock->lock);
3371 return (ISC_R_SHUTTINGDOWN);
3373 nsock->references++;
3375 adev->ev_sender = ntask;
3376 adev->newsocket = nsock;
3377 _set_state(nsock, SOCK_ACCEPT);
3380 * Queue io completion for an accept().
3382 lpo = (IoCompletionInfo *)HeapAlloc(hHeapHandle,
3384 sizeof(IoCompletionInfo));
3385 RUNTIME_CHECK(lpo != NULL);
3386 lpo->acceptbuffer = (void *)HeapAlloc(hHeapHandle, HEAP_ZERO_MEMORY,
3387 (sizeof(SOCKADDR_STORAGE) + 16) * 2);
3388 RUNTIME_CHECK(lpo->acceptbuffer != NULL);
3391 lpo->request_type = SOCKET_ACCEPT;
3393 ISCAcceptEx(sock->fd,
3394 nsock->fd, /* Accepted Socket */
3395 lpo->acceptbuffer, /* Buffer for initial Recv */
3396 0, /* Length of Buffer */
3397 sizeof(SOCKADDR_STORAGE) + 16, /* Local address length + 16 */
3398 sizeof(SOCKADDR_STORAGE) + 16, /* Remote address lengh + 16 */
3399 (LPDWORD)&lpo->received_bytes, /* Bytes Recved */
3400 (LPOVERLAPPED)lpo /* Overlapped structure */
3402 iocompletionport_update(nsock);
3404 socket_log(__LINE__, sock, NULL, TRACE,
3405 isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND,
3406 "accepting for nsock %p fd %d", nsock, nsock->fd);
3411 ISC_LIST_ENQUEUE(sock->accept_list, adev, ev_link);
3412 sock->pending_accept++;
3413 sock->pending_iocp++;
3415 UNLOCK(&sock->lock);
3416 return (ISC_R_SUCCESS);
3420 isc__socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
3421 isc_task_t *task, isc_taskaction_t action, const void *arg)
3423 char strbuf[ISC_STRERRORSIZE];
3424 isc_socket_connev_t *cdev;
3425 isc_task_t *ntask = NULL;
3426 isc_socketmgr_t *manager;
3427 IoCompletionInfo *lpo;
3430 REQUIRE(VALID_SOCKET(sock));
3431 REQUIRE(addr != NULL);
3432 REQUIRE(task != NULL);
3433 REQUIRE(action != NULL);
3435 manager = sock->manager;
3436 REQUIRE(VALID_MANAGER(manager));
3437 REQUIRE(addr != NULL);
3439 if (isc_sockaddr_ismulticast(addr))
3440 return (ISC_R_MULTICAST);
3446 * make sure that the socket's not closed
3448 if (sock->fd == INVALID_SOCKET) {
3449 UNLOCK(&sock->lock);
3450 return (ISC_R_CONNREFUSED);
3454 * Windows sockets won't connect unless the socket is bound.
3459 isc_sockaddr_anyofpf(&any, isc_sockaddr_pf(addr));
3460 if (bind(sock->fd, &any.type.sa, any.length) < 0) {
3461 bind_errno = WSAGetLastError();
3462 UNLOCK(&sock->lock);
3463 switch (bind_errno) {
3465 return (ISC_R_NOPERM);
3466 case WSAEADDRNOTAVAIL:
3467 return (ISC_R_ADDRNOTAVAIL);
3469 return (ISC_R_ADDRINUSE);
3471 return (ISC_R_BOUND);
3473 isc__strerror(bind_errno, strbuf,
3475 UNEXPECTED_ERROR(__FILE__, __LINE__,
3476 "bind: %s", strbuf);
3477 return (ISC_R_UNEXPECTED);
3483 REQUIRE(!sock->pending_connect);
3485 cdev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
3486 ISC_SOCKEVENT_CONNECT,
3490 UNLOCK(&sock->lock);
3491 return (ISC_R_NOMEMORY);
3493 ISC_LINK_INIT(cdev, ev_link);
3495 if (sock->type == isc_sockettype_tcp) {
3497 * Queue io completion for an accept().
3499 lpo = (IoCompletionInfo *)HeapAlloc(hHeapHandle,
3501 sizeof(IoCompletionInfo));
3503 lpo->request_type = SOCKET_CONNECT;
3505 sock->address = *addr;
3506 ISCConnectEx(sock->fd, &addr->type.sa, addr->length,
3507 NULL, 0, NULL, (LPOVERLAPPED)lpo);
3512 isc_task_attach(task, &ntask);
3513 cdev->ev_sender = ntask;
3515 sock->pending_connect = 1;
3516 _set_state(sock, SOCK_CONNECT);
3519 * Enqueue the request.
3521 sock->connect_ev = cdev;
3522 sock->pending_iocp++;
3524 WSAConnect(sock->fd, &addr->type.sa, addr->length, NULL, NULL, NULL, NULL);
3525 cdev->result = ISC_R_SUCCESS;
3526 isc_task_send(task, (isc_event_t **)&cdev);
3529 UNLOCK(&sock->lock);
3531 return (ISC_R_SUCCESS);
3535 isc__socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
3536 isc_result_t result;
3538 REQUIRE(VALID_SOCKET(sock));
3539 REQUIRE(addressp != NULL);
3545 * make sure that the socket's not closed
3547 if (sock->fd == INVALID_SOCKET) {
3548 UNLOCK(&sock->lock);
3549 return (ISC_R_CONNREFUSED);
3552 if (sock->connected) {
3553 *addressp = sock->address;
3554 result = ISC_R_SUCCESS;
3556 result = ISC_R_NOTCONNECTED;
3559 UNLOCK(&sock->lock);
3565 isc__socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
3566 ISC_SOCKADDR_LEN_T len;
3567 isc_result_t result;
3568 char strbuf[ISC_STRERRORSIZE];
3570 REQUIRE(VALID_SOCKET(sock));
3571 REQUIRE(addressp != NULL);
3577 * make sure that the socket's not closed
3579 if (sock->fd == INVALID_SOCKET) {
3580 UNLOCK(&sock->lock);
3581 return (ISC_R_CONNREFUSED);
3585 result = ISC_R_NOTBOUND;
3589 result = ISC_R_SUCCESS;
3591 len = sizeof(addressp->type);
3592 if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
3593 isc__strerror(WSAGetLastError(), strbuf, sizeof(strbuf));
3594 UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
3596 result = ISC_R_UNEXPECTED;
3599 addressp->length = (unsigned int)len;
3602 UNLOCK(&sock->lock);
3608 * Run through the list of events on this socket, and cancel the ones
3609 * queued for task "task" of type "how". "how" is a bitmask.
3612 isc__socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
3614 REQUIRE(VALID_SOCKET(sock));
3617 * Quick exit if there is nothing to do. Don't even bother locking
3627 * make sure that the socket's not closed
3629 if (sock->fd == INVALID_SOCKET) {
3630 UNLOCK(&sock->lock);
3635 * All of these do the same thing, more or less.
3637 * o If the internal event is marked as "posted" try to
3638 * remove it from the task's queue. If this fails, mark it
3639 * as canceled instead, and let the task clean it up later.
3640 * o For each I/O request for that task of that type, post
3641 * its done event with status of "ISC_R_CANCELED".
3642 * o Reset any state needed.
3645 if ((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV) {
3646 isc_socketevent_t *dev;
3647 isc_socketevent_t *next;
3648 isc_task_t *current_task;
3650 dev = ISC_LIST_HEAD(sock->recv_list);
3651 while (dev != NULL) {
3652 current_task = dev->ev_sender;
3653 next = ISC_LIST_NEXT(dev, ev_link);
3654 if ((task == NULL) || (task == current_task)) {
3655 dev->result = ISC_R_CANCELED;
3656 send_recvdone_event(sock, &dev);
3661 how &= ~ISC_SOCKCANCEL_RECV;
3663 if ((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND) {
3664 isc_socketevent_t *dev;
3665 isc_socketevent_t *next;
3666 isc_task_t *current_task;
3668 dev = ISC_LIST_HEAD(sock->send_list);
3670 while (dev != NULL) {
3671 current_task = dev->ev_sender;
3672 next = ISC_LIST_NEXT(dev, ev_link);
3673 if ((task == NULL) || (task == current_task)) {
3674 dev->result = ISC_R_CANCELED;
3675 send_senddone_event(sock, &dev);
3680 how &= ~ISC_SOCKCANCEL_SEND;
3682 if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
3683 && !ISC_LIST_EMPTY(sock->accept_list)) {
3684 isc_socket_newconnev_t *dev;
3685 isc_socket_newconnev_t *next;
3686 isc_task_t *current_task;
3688 dev = ISC_LIST_HEAD(sock->accept_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)) {
3695 dev->newsocket->references--;
3696 closesocket(dev->newsocket->fd);
3697 dev->newsocket->fd = INVALID_SOCKET;
3698 free_socket(&dev->newsocket, __LINE__);
3700 dev->result = ISC_R_CANCELED;
3701 send_acceptdone_event(sock, &dev);
3707 how &= ~ISC_SOCKCANCEL_ACCEPT;
3710 * Connecting is not a list.
3712 if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
3713 && sock->connect_ev != NULL) {
3714 isc_socket_connev_t *dev;
3715 isc_task_t *current_task;
3717 INSIST(sock->pending_connect);
3719 dev = sock->connect_ev;
3720 current_task = dev->ev_sender;
3722 if ((task == NULL) || (task == current_task)) {
3723 closesocket(sock->fd);
3724 sock->fd = INVALID_SOCKET;
3725 _set_state(sock, SOCK_CLOSED);
3727 sock->connect_ev = NULL;
3728 dev->result = ISC_R_CANCELED;
3729 send_connectdone_event(sock, &dev);
3732 how &= ~ISC_SOCKCANCEL_CONNECT;
3734 maybe_free_socket(&sock, __LINE__);
3738 isc__socket_gettype(isc_socket_t *sock) {
3739 isc_sockettype_t type;
3741 REQUIRE(VALID_SOCKET(sock));
3746 * make sure that the socket's not closed
3748 if (sock->fd == INVALID_SOCKET) {
3749 UNLOCK(&sock->lock);
3750 return (ISC_R_CONNREFUSED);
3754 UNLOCK(&sock->lock);
3759 isc__socket_isbound(isc_socket_t *sock) {
3762 REQUIRE(VALID_SOCKET(sock));
3768 * make sure that the socket's not closed
3770 if (sock->fd == INVALID_SOCKET) {
3771 UNLOCK(&sock->lock);
3775 val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
3776 UNLOCK(&sock->lock);
3782 isc__socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
3783 #if defined(IPV6_V6ONLY)
3784 int onoff = yes ? 1 : 0;
3789 REQUIRE(VALID_SOCKET(sock));
3792 if (sock->pf == AF_INET6) {
3793 (void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
3794 (char *)&onoff, sizeof(onoff));
3800 isc__socket_cleanunix(isc_sockaddr_t *addr, isc_boolean_t active) {
3806 isc__socket_permunix(isc_sockaddr_t *addr, isc_uint32_t perm,
3807 isc_uint32_t owner, isc_uint32_t group)
3813 return (ISC_R_NOTIMPLEMENTED);
3817 isc__socket_setname(isc_socket_t *socket, const char *name, void *tag) {
3823 REQUIRE(VALID_SOCKET(socket));
3825 LOCK(&socket->lock);
3826 memset(socket->name, 0, sizeof(socket->name));
3827 strncpy(socket->name, name, sizeof(socket->name) - 1);
3829 UNLOCK(&socket->lock);
3833 isc__socket_getname(isc_socket_t *socket) {
3834 return (socket->name);
3838 isc__socket_gettag(isc_socket_t *socket) {
3839 return (socket->tag);
3843 isc__socket_getfd(isc_socket_t *socket) {
3844 return ((short) socket->fd);
3848 isc__socketmgr_setreserved(isc_socketmgr_t *manager, isc_uint32_t reserved) {
3854 isc___socketmgr_maxudp(isc_socketmgr_t *manager, int maxudp) {
3863 _socktype(isc_sockettype_t type)
3865 if (type == isc_sockettype_udp)
3867 else if (type == isc_sockettype_tcp)
3869 else if (type == isc_sockettype_unix)
3871 else if (type == isc_sockettype_fdwatch)
3874 return ("not-initialized");
3878 isc_socketmgr_renderxml(isc_socketmgr_t *mgr, xmlTextWriterPtr writer)
3881 char peerbuf[ISC_SOCKADDR_FORMATSIZE];
3882 isc_sockaddr_t addr;
3883 ISC_SOCKADDR_LEN_T len;
3887 #ifndef ISC_PLATFORM_USETHREADS
3888 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
3889 xmlTextWriterWriteFormatString(writer, "%d", mgr->refs);
3890 xmlTextWriterEndElement(writer);
3893 xmlTextWriterStartElement(writer, ISC_XMLCHAR "sockets");
3894 sock = ISC_LIST_HEAD(mgr->socklist);
3895 while (sock != NULL) {
3897 xmlTextWriterStartElement(writer, ISC_XMLCHAR "socket");
3899 xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
3900 xmlTextWriterWriteFormatString(writer, "%p", sock);
3901 xmlTextWriterEndElement(writer);
3903 if (sock->name[0] != 0) {
3904 xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
3905 xmlTextWriterWriteFormatString(writer, "%s",
3907 xmlTextWriterEndElement(writer); /* name */
3910 xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
3911 xmlTextWriterWriteFormatString(writer, "%d", sock->references);
3912 xmlTextWriterEndElement(writer);
3914 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "type",
3915 ISC_XMLCHAR _socktype(sock->type));
3917 if (sock->connected) {
3918 isc_sockaddr_format(&sock->address, peerbuf,
3920 xmlTextWriterWriteElement(writer,
3921 ISC_XMLCHAR "peer-address",
3922 ISC_XMLCHAR peerbuf);
3926 if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
3927 isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
3928 xmlTextWriterWriteElement(writer,
3929 ISC_XMLCHAR "local-address",
3930 ISC_XMLCHAR peerbuf);
3933 xmlTextWriterStartElement(writer, ISC_XMLCHAR "states");
3934 if (sock->pending_recv)
3935 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3936 ISC_XMLCHAR "pending-receive");
3937 if (sock->pending_send)
3938 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3939 ISC_XMLCHAR "pending-send");
3940 if (sock->pending_accept)
3941 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3942 ISC_XMLCHAR "pending_accept");
3944 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3945 ISC_XMLCHAR "listener");
3946 if (sock->connected)
3947 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3948 ISC_XMLCHAR "connected");
3949 if (sock->pending_connect)
3950 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3951 ISC_XMLCHAR "connecting");
3953 xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
3954 ISC_XMLCHAR "bound");
3956 xmlTextWriterEndElement(writer); /* states */
3958 xmlTextWriterEndElement(writer); /* socket */
3960 UNLOCK(&sock->lock);
3961 sock = ISC_LIST_NEXT(sock, link);
3963 xmlTextWriterEndElement(writer); /* sockets */
3967 #endif /* HAVE_LIBXML2 */