socket.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 2000-2016 Internet Systems Consortium, Inc. ("ISC")
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* $Id$ */
/* This code uses functions which are only available on Server 2003 and
* higher, and Windows XP and higher.
*
* This code is by nature multithreaded and takes advantage of various
* features to pass on information through the completion port for
* when I/O is completed. All sends, receives, accepts, and connects are
* completed through the completion port.
*
* The number of Completion Port Worker threads used is the total number
* of CPU's + 1. This increases the likelihood that a Worker Thread is
* available for processing a completed request.
*
* XXXPDM 5 August, 2002
*/
#define MAKE_EXTERNAL 1
#include <config.h>
#ifndef _WINSOCKAPI_
#define _WINSOCKAPI_ /* Prevent inclusion of winsock.h in windows.h */
#endif
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <isc/bufferlist.h>
#include <isc/condition.h>
#include <isc/platform.h>
#include <isc/strerror.h>
#include <mswsock.h>
#include "errno2result.h"
/*
* Set by the -T dscp option on the command line. If set to a value
* other than -1, we check to make sure DSCP values match it, and
* assert if not.
*/
int isc_dscp_check_value = -1;
/*
* How in the world can Microsoft exist with APIs like this?
* We can't actually call this directly, because it turns out
* no library exports this function. Instead, we need to
* issue a runtime call to get the address.
*/
/*
* Run expensive internal consistency checks.
*/
#else
#define CONSISTENT(sock) do {} while (0)
#endif
/*
* Define this macro to control the behavior of connection
* resets on UDP sockets. See Microsoft KnowledgeBase Article Q263823
* for details.
* NOTE: This requires that Windows 2000 systems install Service Pack 2
* or later.
*/
#ifndef SIO_UDP_CONNRESET
#endif
/*
* Some systems define the socket length argument as an int, some as size_t,
* some as socklen_t. This is here so it can be easily changed if needed.
*/
#ifndef ISC_SOCKADDR_LEN_T
#define ISC_SOCKADDR_LEN_T unsigned int
#endif
/*
* Define what the possible "soft" errors can be. These are non-fatal returns
* of various network related functions, like recv() and so on.
*/
#define SOFT_ERROR(e) ((e) == WSAEINTR || \
(e) == WSAEWOULDBLOCK || \
(e) == EWOULDBLOCK || \
(e) == EINTR || \
(e) == EAGAIN || \
(e) == 0)
/*
* Pending errors are not really errors and should be
* kept separate
*/
#define PENDING_ERROR(e) ((e) == WSA_IO_PENDING || (e) == 0)
#define DOIO_SUCCESS 0 /* i/o ok, event sent */
/*
* DLVL(70) -- Socket "correctness" -- including returning of events, etc.
* DLVL(20) -- Socket creation/destruction.
*/
#define TRACE_LEVEL 90
#define CORRECTNESS_LEVEL 70
#define IOEVENT_LEVEL 60
#define EVENT_LEVEL 50
#define CREATION_LEVEL 20
typedef isc_event_t intev_t;
/*
* Socket State
*/
enum {
SOCK_INITIALIZED, /* Socket Initialized */
SOCK_OPEN, /* Socket opened but nothing yet to do */
SOCK_DATA, /* Socket sending or receiving data */
SOCK_LISTEN, /* TCP Socket listening for connects */
SOCK_ACCEPT, /* TCP socket is waiting to accept */
SOCK_CONNECT, /* TCP Socket connecting */
SOCK_CLOSED, /* Socket has been closed */
};
/*
* IPv6 control information. If the socket is an IPv6 socket we want
* to collect the destination address and interface so the client can
* set them on outgoing packets.
*/
#ifdef ISC_PLATFORM_HAVEIPV6
#ifndef USE_CMSG
#define USE_CMSG 1
#endif
#endif
/*
* We really don't want to try and use these control messages. Win32
* doesn't have this mechanism before XP.
*/
/*
* Message header for recvmsg and sendmsg calls.
* Used value-result for recvmsg, value only for sendmsg.
*/
struct msghdr {
int to_addr_len; /* length of the address */
void *msg_control; /* ancillary data, see below */
} msghdr;
/*
* The size to raise the receive buffer to.
*/
/*
* The number of times a send operation is repeated if the result
* is WSAEINTR.
*/
#define NRETRIES 10
struct isc_socket {
/* Not locked. */
unsigned int magic;
/* Locked by socket lock. */
unsigned int references; /* EXTERNAL references */
int pf; /* protocol family */
char name[16];
void * tag;
/*
* Each recv() call uses this buffer. It is a per-socket receive
* buffer that allows us to decouple the system recv() from the
* recv_list done events. This means the items on the recv_list
* can be removed without having to cancel pending system recv()
* calls. It also allows us to read-ahead in some cases.
*/
struct {
int from_addr_len; // length of the address
char *base; // the base of the buffer
char *consume_position; // where to start copying data from next
unsigned int len; // the actual size of this buffer
unsigned int remaining; // the number of bytes remaining
} recvbuf;
connected : 1,
unsigned int pending_iocp; /* Should equal the counters below. Debug. */
unsigned int pending_recv; /* Number of outstanding recv() calls. */
unsigned int pending_send; /* Number of outstanding send() calls. */
unsigned int pending_accept; /* Number of outstanding accept() calls. */
unsigned int state; /* Socket state. Debugging and consistency checking. */
int state_lineno; /* line which last touched state */
};
#define _set_state(sock, _state) do { (sock)->state = (_state); (sock)->state_lineno = __LINE__; } while (0)
/*
* Buffer structure
*/
struct buflist {
void *buf;
unsigned int buflen;
};
/*
* I/O Completion ports Info structures
*/
typedef struct IoCompletionInfo {
void *acceptbuffer;
int request_type;
struct msghdr messagehdr;
/*
* Define a maximum number of I/O Completion Port worker threads
* to handle the load on the Completion Port. The actual number
* used is the number of CPU's + 1.
*/
#define MAX_IOCPTHREADS 20
struct isc_socketmgr {
/* Not locked. */
unsigned int magic;
/* Locked by manager lock. */
int maxIOCPThreads;
/*
* Debugging.
* Modified by InterlockedIncrement() and InterlockedDecrement()
*/
};
enum {
};
/*
* send() and recv() iovec counts
*/
static void maybe_free_socket(isc_socket_t **, int);
static void free_socket(isc_socket_t **, int);
/*
* This is used to dump the contents of the sock structure
* You should make sure that the sock is locked before
* dumping it. Since the code uses simple printf() statements
* it should only be used interactively.
*/
void
#if 0
char socktext[ISC_SOCKADDR_FORMATSIZE];
if (result == ISC_R_SUCCESS) {
}
if (result == ISC_R_SUCCESS) {
}
#endif
printf("\n\t\tSock Dump\n");
printf("\n\t\tSock Recv List\n");
}
printf("\n\t\tSock Send List\n");
}
printf("\n\t\tSock Accept List\n");
}
printf("\n\t\tSock Connect List\n");
}
}
static void
/* This function will add an entry to the I/O completion port
* that will signal the I/O thread to exit (gracefully)
*/
static void
int i;
int errval;
char strbuf[ISC_STRERRORSIZE];
for (i = 0; i < manager->maxIOCPThreads; i++) {
0, 0, 0)) {
errval = GetLastError();
"Can't request service thread to exit: %s"),
strbuf);
}
}
}
/*
* Create the worker threads for the I/O Completion Port
*/
void
int errval;
char strbuf[ISC_STRERRORSIZE];
int i;
INSIST(total_threads > 0);
/*
* We need at least one
*/
for (i = 0; i < total_threads; i++) {
manager, 0,
&manager->dwIOCPThreadIds[i]);
errval = GetLastError();
"Can't create IOCP thread: %s"),
strbuf);
exit(1);
}
}
}
/*
* Create/initialise the I/O completion port
*/
void
int errval;
char strbuf[ISC_STRERRORSIZE];
/*
* Create a private heap to handle the socket overlapped structure
* The minimum number of structures is 10, there is no maximum
*/
if (hHeapHandle == NULL) {
errval = GetLastError();
"HeapCreate() failed during "
"initialization: %s"),
strbuf);
exit(1);
}
/* Now Create the Completion Port */
0, manager->maxIOCPThreads);
errval = GetLastError();
"CreateIoCompletionPort() failed "
"during initialization: %s"),
strbuf);
exit(1);
}
/*
* Worker threads for servicing the I/O
*/
}
/*
* Associate a socket with an IO Completion Port. This allows us to queue events for it
* and have our worker pool of threads process them.
*/
void
char strbuf[ISC_STRERRORSIZE];
"iocompletionport_update: failed to open"
" io completion port: %s",
strbuf);
/* XXXMLG temporary hack to make failures detected.
* This function should return errors to the caller, not
* exit here.
*/
"CreateIoCompletionPort() failed "
"during initialization: %s"),
strbuf);
exit(1);
}
}
/*
* Routine to cleanup and then close the socket.
* Only close the socket here if it is NOT associated
* with an event, otherwise the WSAWaitForMultipleEvents
* may fail due to the fact that the Wait should not
* be running while closing an event or a socket.
* The socket is locked before calling this function
*/
void
}
}
static void
initialise(void) {
int err;
/* Need Winsock 2.2 or better */
if (err != 0) {
char strbuf[ISC_STRERRORSIZE];
ISC_MSG_FAILED, "failed"),
strbuf);
exit(1);
}
/*
* The following APIs do not exist as functions in a library, but we must
* ask winsock for them. They are "extensions" -- but why they cannot be
* actual functions is beyond me. So, ask winsock for the pointers to the
* functions we need.
*/
&GUIDConnectEx, sizeof(GUIDConnectEx),
&ISCConnectEx, sizeof(ISCConnectEx),
&GUIDAcceptEx, sizeof(GUIDAcceptEx),
&ISCAcceptEx, sizeof(ISCAcceptEx),
&GUIDGetAcceptExSockaddrs, sizeof(GUIDGetAcceptExSockaddrs),
&ISCGetAcceptExSockaddrs, sizeof(ISCGetAcceptExSockaddrs),
}
/*
* Initialize socket services
*/
void
InitSockets(void) {
initialise) == ISC_R_SUCCESS);
if (!initialised)
exit(1);
}
int
{
int Result;
int total_sent;
*Error = 0;
NULL);
total_sent = (int)BytesSent;
/* Check for errors.*/
if (Result == SOCKET_ERROR) {
*Error = WSAGetLastError();
switch (*Error) {
case WSA_IO_INCOMPLETE:
case WSA_WAIT_IO_COMPLETION:
case WSA_IO_PENDING:
case NO_ERROR: /* Strange, but okay */
sock->pending_iocp++;
sock->pending_send++;
break;
default:
return (-1);
break;
}
} else {
sock->pending_iocp++;
sock->pending_send++;
}
return (0);
else
return (total_sent);
}
static void
int Result;
int Error;
int need_retry;
/*
* If we already have a receive pending, do nothing.
*/
if (sock->pending_recv > 0) {
return;
}
/*
* If no one is waiting, do nothing.
*/
return;
}
sizeof(IoCompletionInfo));
} else
Error = 0;
/* Check for errors. */
if (Result == SOCKET_ERROR) {
Error = WSAGetLastError();
switch (Error) {
case WSA_IO_PENDING:
sock->pending_iocp++;
sock->pending_recv++;
break;
/* direct error: no completion event */
case ERROR_HOST_UNREACHABLE:
case WSAENETRESET:
case WSAECONNRESET:
/* soft error */
break;
}
/* FALLTHROUGH */
default:
if (isc_result == ISC_R_UNEXPECTED)
"WSARecvFrom: Windows error code: %d, isc result %d",
Error, isc_result);
break;
}
} else {
/*
* The recv() finished immediately, but we will still get
* a completion event. Rather than duplicate code, let
* that thread handle sending the data along its way.
*/
sock->pending_iocp++;
sock->pending_recv++;
}
"queue_io_request: fd %d result %d error %d",
if (need_retry)
goto retry;
}
static void
{
char msgbuf[2048];
return;
}
static void
const char *fmt, ...)
{
char msgbuf[2048];
char peerbuf[256];
return;
} else {
}
}
/*
* Make an fd SOCKET non-blocking.
*/
static isc_result_t
int ret;
unsigned long flags = 1;
char strbuf[ISC_STRERRORSIZE];
/* Set the socket to non-blocking */
if (ret == -1) {
"ioctlsocket(%d, FIOBIO, %d): %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
/*
* Windows 2000 systems incorrectly cause UDP sockets using WSARecvFrom
* to not work correctly, returning a WSACONNRESET error when a WSASendTo
* fails with an "ICMP port unreachable" response and preventing the
* socket from using the WSARecvFrom in subsequent operations.
* The function below fixes this, but requires that Windows 2000
* Service Pack 2 or later be installed on the system. NT 4.0
* systems are not affected by this and work correctly.
* See Microsoft Knowledge Base Article Q263823 for details of this.
*/
DWORD dwBytesReturned = 0;
if (isc_win32os_versioncheck(5, 0, 0, 0) < 0)
return (ISC_R_SUCCESS); /* NT 4.0 has no problem */
/* disable bad behavior using IOCTL: SIO_UDP_CONNRESET */
sizeof(bNewBehavior), NULL, 0,
if (status != SOCKET_ERROR)
return (ISC_R_SUCCESS);
else {
"WSAIoctl(SIO_UDP_CONNRESET, oldBehaviour) %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
}
/*
* Construct an iov array and attach it to the msghdr passed in. This is
* the SEND constructor, which will use the used region of the buffer
* (if using a buffer list) or will use the internal region (if a single
* buffer I/O is requested).
*
* Nothing can be NULL, and the done event must list at least one buffer
* on the buffer linked list for this function to be meaningful.
*/
static void
{
unsigned int iovcount;
write_count = 0;
iovcount = 0;
/*
* Single buffer I/O? Skip what we've done so far in this region.
*/
iovcount = 1;
goto config;
}
/*
* Multibuffer I/O.
* Skip the data in the buffer list that we have already written.
*/
skip_count = dev->n;
break;
}
write_count += uselen;
skip_count = 0;
iovcount++;
}
}
INSIST(skip_count == 0);
}
static void
{
else
}
}
static void
}
static isc_socketevent_t *
void *arg)
{
sizeof(*ev));
return (NULL);
ev->n = 0;
ev->attributes = 0;
return (ev);
}
#if defined(ISC_SOCKET_DEBUG)
static void
unsigned int i;
for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
printf("\t\t%u\tbase %p, len %u\n", i,
}
#endif
/*
* map the error code
*/
int
int doreturn;
switch (windows_errno) {
case WSAECONNREFUSED:
else
break;
case WSAENETUNREACH:
else
break;
case ERROR_PORT_UNREACHABLE:
case ERROR_HOST_UNREACHABLE:
case WSAEHOSTUNREACH:
else
break;
case WSAENETDOWN:
else
break;
case WSAEHOSTDOWN:
else
break;
case WSAEACCES:
else
break;
case WSAECONNRESET:
case WSAENETRESET:
case WSAECONNABORTED:
case WSAEDISCON:
else
break;
case WSAENOTCONN:
else
break;
case ERROR_OPERATION_ABORTED:
case ERROR_CONNECTION_ABORTED:
case ERROR_REQUEST_ABORTED:
break;
case WSAENOBUFS:
break;
case WSAEAFNOSUPPORT:
break;
case WSAEADDRNOTAVAIL:
break;
case WSAEDESTADDRREQ:
break;
case ERROR_NETNAME_DELETED:
break;
default:
break;
}
}
return (doreturn);
}
static void
isc_region_t r;
int copylen;
"dropping source port zero packet");
}
return;
}
}
/*
* Run through the list of buffers we were given, and find the
* first one with space. Once it is found, loop through, filling
* the buffers as much as possible.
*/
if (isc_buffer_availablelength(buffer) > 0) {
copylen);
}
}
} else { // Single-buffer receive
}
/*
* UDP receives are all-consuming. That is, if we have 4k worth of
* data in our receive buffer, and the caller only gave us
* 1k of space, we will toss the remaining 3k of data. TCP
* will keep the extra data around and use it for later requests.
*/
}
/*
* Copy out as much data from the internal buffer to done events.
* As each done event is filled, send it along its way.
*/
static void
{
/*
* If we are in the process of filling our buffer, we cannot
* touch it yet, so don't.
*/
if (sock->pending_recv > 0)
return;
/*
* See if we have sufficient data in our receive buffer
* to handle this. If we do, copy out the data.
*/
/*
* Did we satisfy it?
*/
}
}
}
/*
* Returns:
* DOIO_SUCCESS The operation succeeded. dev->result contains
* ISC_R_SUCCESS.
*
* DOIO_HARD A hard or unexpected I/O error was encountered.
* dev->result contains the appropriate error.
*
* DOIO_SOFT A soft I/O error was encountered. No senddone
* event was sent. The operation should be retried.
*
* No other return values are possible.
*/
static int
{
char strbuf[ISC_STRERRORSIZE];
if (send_errno != 0) {
if (SOFT_ERROR(send_errno))
return (DOIO_SOFT);
}
/*
* If we write less than we expected, update counters, poke.
*/
return (DOIO_SOFT);
/*
* Exactly what we wanted to write. We're done with this
* entry. Post its completion event.
*/
return (DOIO_SUCCESS);
}
static int
int *send_errno)
{
char strbuf[ISC_STRERRORSIZE];
int status;
sizeof(IoCompletionInfo));
if (*nbytes <= 0) {
/*
* I/O has been initiated
* completion will be through the completion port
*/
if (PENDING_ERROR(*send_errno)) {
goto done;
}
if (SOFT_ERROR(*send_errno)) {
goto done;
}
/*
* If we got this far then something is wrong
*/
"startio_send: internal_sendmsg(%d) %d "
"bytes, err %d/%s",
}
goto done;
}
done:
return (status);
}
static void
#ifdef IPV6_USE_MIN_MTU
/* use minimum MTU */
int on = 1;
}
#else
#endif
}
static isc_result_t
isc_socket_t **socketp) {
return (ISC_R_NOMEMORY);
sock->references = 0;
/*
* Set up list of readers and writers to be initially empty.
*/
sock->pending_accept = 0;
sock->pending_recv = 0;
sock->pending_send = 0;
sock->pending_iocp = 0;
sock->pending_connect = 0;
goto error;
}
/*
* Initialize the lock.
*/
if (result != ISC_R_SUCCESS)
goto error;
"allocated");
return (ISC_R_SUCCESS);
return (result);
}
/*
* Verify that the socket state is consistent.
*/
static void
unsigned int count;
char *crash_reason;
count = 0;
count++;
}
crash_reason = "send_list > sock->pending_send";
}
count = 0;
count++;
}
crash_reason = "accept_list > sock->pending_accept";
}
if (crash) {
ISC_MSG_DESTROYING, "SOCKET INCONSISTENT: %s",
}
}
/*
* Maybe free the socket.
*
* This function will verify tht the socket is no longer in use in any way,
* either internally or externally. This is the only place where this
* check is to be made; if some bit of code believes that IT is done with
* the socket (e.g., some reference counter reaches zero), it should call
* this function.
*
* When calling this function, the socket must be locked, and the manager
* must be unlocked.
*
* When this function returns, *socketp will be NULL. No tricks to try
* to hold on to this pointer are allowed.
*/
static void
if (sock->pending_iocp > 0
|| sock->pending_recv > 0
|| sock->pending_send > 0
|| sock->pending_accept > 0
|| sock->references > 0
return;
}
}
void
/*
* Seems we can free the socket after all.
*/
"freeing socket line %d fd %d lock %p semaphore %p",
}
/*
* Create a new 'type' socket managed by 'manager'. Events
* will be posted to 'task' and when dispatched 'action' will be
* called with 'arg' as the arg value. The new socket is returned
* in 'socketp'.
*/
static isc_result_t
{
#if defined(USE_CMSG)
int on = 1;
#endif
#if defined(SO_RCVBUF)
int size;
#endif
int socket_errno;
char strbuf[ISC_STRERRORSIZE];
#ifndef SOCK_RAW
if (type == isc_sockettype_raw)
return (ISC_R_NOTIMPLEMENTED);
#endif
if (result != ISC_R_SUCCESS)
return (result);
switch (type) {
case isc_sockettype_udp:
if (result != ISC_R_SUCCESS) {
"closed %d %d %d "
"con_reset_fix_failed",
sock->references);
return (result);
}
}
break;
case isc_sockettype_tcp:
break;
#ifdef SOCK_RAW
case isc_sockettype_raw:
#ifdef PF_ROUTE
#endif
break;
#endif
}
switch (socket_errno) {
case WSAEMFILE:
case WSAENOBUFS:
return (ISC_R_NORESOURCES);
case WSAEPROTONOSUPPORT:
case WSAEPFNOSUPPORT:
case WSAEAFNOSUPPORT:
return (ISC_R_FAMILYNOSUPPORT);
default:
"socket() %s: %s",
"failed"),
strbuf);
return (ISC_R_UNEXPECTED);
}
}
if (result != ISC_R_SUCCESS) {
"closed %d %d %d make_nonblock_failed",
sock->references);
return (result);
}
/*
* Use minimum mtu if possible.
*/
if (type == isc_sockettype_udp) {
#if defined(USE_CMSG)
#if defined(ISC_PLATFORM_HAVEIPV6)
#ifdef IPV6_RECVPKTINFO
/* 2292bis */
"setsockopt(%d, IPV6_RECVPKTINFO) "
"failed"),
strbuf);
}
#else
/* 2292 */
"setsockopt(%d, IPV6_PKTINFO) %s: %s",
"failed"),
strbuf);
}
#endif /* IPV6_RECVPKTINFO */
#endif /* ISC_PLATFORM_HAVEIPV6 */
#endif /* defined(USE_CMSG) */
#if defined(SO_RCVBUF)
size < RCVBUFSIZE) {
size = RCVBUFSIZE;
}
#endif
}
#endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
if (dup_socket) {
#ifndef ISC_ALLOW_MAPPED
#endif
if (dup_socket->bound) {
if (result != ISC_R_SUCCESS) {
return (result);
}
if (result != ISC_R_SUCCESS) {
return (result);
}
}
}
/*
* Note we don't have to lock the socket like we normally would because
* there are no external references to it yet.
*/
return (ISC_R_SUCCESS);
}
{
}
}
return (ISC_R_NOTIMPLEMENTED);
}
/*
* Attach to a socket. Caller must explicitly detach when it is done.
*/
void
sock->references++;
}
/*
* Dereference a socket. If this is the last reference to it, clean things
* up by destroying the socket.
*/
void
sock->references--;
"detach_socket %d %d %d",
sock->references);
}
}
return (ISC_R_NOTIMPLEMENTED);
}
/*
* Dequeue an item off the given socket's read queue, set the result code
* in the done event to the one provided, and send it to the task it was
* destined for.
*
* If the event to be sent is on a list, remove it before sending. If
* asked to, send and detach from the task as well.
*
* Caller must have the socket locked if the event is attached to the socket.
*/
static void
else
}
/*
* See comments for send_recvdone_event() above.
*/
static void
else
}
/*
* See comments for send_recvdone_event() above.
*/
static void
}
/*
* See comments for send_recvdone_event() above.
*/
static void
}
/*
* On entry to this function, the event delivered is the internal
* readable event, and the first item on the accept_list should be
* the done event we want to send. If the list is empty, this is a no-op,
* so just close the new connection, unlock, and return.
*
* Note the socket is locked before entering here
*/
static void
int localaddr_len = sizeof(*localaddr);
struct sockaddr *remoteaddr;
int remoteaddr_len = sizeof(*remoteaddr);
"internal_accept called");
sock->pending_iocp--;
sock->pending_accept--;
/*
* If the event is no longer in the list we can just return.
*/
goto done;
/*
* Pull off the done event.
*/
/*
* Extract the addresses from the socket, copy them into the structure,
* and return the new socket.
*/
"internal_accept parent %p", sock);
/*
* Use minimum mtu if possible.
*/
/*
* Hook it up into the manager.
*/
"accepted_connection new_socket %p fd %d",
done:
}
/*
* Called when a socket with a pending connect() finishes.
* Note that the socket is locked before entering.
*/
static void
char strbuf[ISC_STRERRORSIZE];
sock->pending_iocp--;
sock->pending_connect = 0;
/*
* If the event is no longer in the list we can just close and return.
*/
sock->pending_connect = 0;
}
return;
}
/*
* Check possible Windows network event error status here.
*/
if (connect_errno != 0) {
/*
* If the error is SOFT, just try again on this
* fd and pretend nothing strange happened.
*/
if (SOFT_ERROR(connect_errno) ||
connect_errno == WSAEINPROGRESS) {
return;
}
/*
* Translate other errors into ISC_R_* flavors.
*/
switch (connect_errno) {
#define ERROR_MATCH(a, b) case a: result = b; break;
default:
"internal_connect: connect() %s",
strbuf);
}
} else {
SO_UPDATE_CONNECT_CONTEXT, NULL, 0) == 0);
"internal_connect: success");
}
do {
}
/*
* Loop through the socket, returning ISC_R_EOF for each done event pending.
*/
static void
}
}
/*
* Loop through the socket, returning result for each done event pending.
*/
static void
}
}
/*
* Take the data we received in our private buffer, and if any recv() calls on
* our list are satisfied, send the corresponding done event.
*
* If we need more data (there are still items on the recv_list after we consume all
* our data) then arrange for another system recv() call to fill our buffers.
*/
static void
{
"internal_recv: %d bytes received", nbytes);
/*
* If we got here, the I/O operation succeeded. However, we might still have removed this
* event from our notification list (or never placed it on it due to immediate completion.)
* Handle the reference counting here, and handle the cancellation event just after.
*/
sock->pending_iocp--;
sock->pending_recv--;
/*
* The only way we could have gotten here is that our I/O has successfully completed.
* Update our pointers, and move on. The only odd case here is that we might not
* have received enough data on a TCP stream to satisfy the minimum requirements. If
* this is the case, we will re-issue the recv() call for what we need.
*
* We do check for a recv() of 0 bytes on a TCP stream. This means the remote end
* has closed.
*/
return;
}
/*
* If there are more receivers waiting for data, queue another receive
* here.
*/
/*
*/
}
static void
{
/*
* Find out what socket this is and lock it.
*/
"internal_send: task got socket event %p", dev);
}
sock->pending_iocp--;
sock->pending_send--;
/* If the event is no longer in the list we can just return */
goto done;
/*
* Set the error code and send things on its way.
*/
case DOIO_SOFT:
break;
case DOIO_HARD:
case DOIO_SUCCESS:
break;
}
done:
}
/*
* These return if the done event passed in is on the list.
* Using these ensures we will not double-send an event.
*/
static isc_boolean_t
{
}
static isc_boolean_t
{
}
static isc_boolean_t
{
}
//
// The Windows network stack seems to have two very distinct paths depending
// on what is installed. Specifically, if something is looking at network
// connections (like an anti-virus or anti-malware application, such as
// McAfee products) Windows may return additional error conditions which
// were not previously returned.
//
// One specific one is when a TCP SYN scan is used. In this situation,
// Windows responds with the SYN-ACK, but the scanner never responds with
// the 3rd packet, the ACK. Windows consiers this a partially open connection.
// Most Unix networking stacks, and Windows without McAfee installed, will
// not return this to the caller. However, with this product installed,
// Windows returns this as a failed status on the Accept() call. Here, we
// will just re-issue the ISCAcceptEx() call as if nothing had happened.
//
// This code should only be called when the listening socket has received
// such an error. Additionally, the "parent" socket must be locked.
// Additionally, the lpo argument is re-used here, and must not be freed
// by the caller.
//
static isc_result_t
{
/*
* AcceptEx() requires we pass in a socket. Note that we carefully
* do not close the previous socket in case of an error message returned by
* our new socket() call. If we return an error here, our caller will
* clean up.
*/
return (ISC_R_FAILURE); // parent will ask windows for error message
}
0, /* Length of Buffer */
);
return (ISC_R_SUCCESS);
}
/*
* This is the I/O Completion Port Worker Function. It loops forever
* waiting for I/O to complete and then forwards them for further
* processing. There are a number of these in separate threads.
*/
static isc_threadresult_t WINAPI
int request;
int errval;
char strbuf[ISC_STRERRORSIZE];
int errstatus;
/*
* Set the thread priority high enough so I/O will
* preempt normal recv packet processing, but not
* higher than the timer sync thread.
*/
if (!SetThreadPriority(GetCurrentThread(),
errval = GetLastError();
"Can't set thread priority: %s"),
strbuf);
}
/*
* Loop forever waiting on I/O Completions and then processing them
*/
while (TRUE) {
&nbytes,
(PULONG_PTR)&sock,
(LPWSAOVERLAPPED *)&lpo,
INFINITE);
break;
errstatus = 0;
if (!bSuccess) {
/*
* Did the I/O operation complete?
*/
errstatus = GetLastError();
switch (request) {
case SOCKET_RECV:
sock->pending_iocp--;
sock->pending_recv--;
((errstatus == ERROR_HOST_UNREACHABLE) ||
(errstatus == WSAENETRESET) ||
(errstatus == WSAECONNRESET))) {
/* ignore soft errors */
break;
}
if (isc_result == ISC_R_UNEXPECTED) {
"SOCKET_RECV: Windows error code: %d, returning ISC error %d",
}
break;
case SOCKET_SEND:
sock->pending_iocp--;
sock->pending_send--;
"canceled_send");
}
break;
case SOCKET_ACCEPT:
goto wait_again;
} else {
errstatus = GetLastError();
"restart_accept() failed: errstatus=%d isc_result=%d",
}
}
sock->pending_iocp--;
sock->pending_accept--;
"canceled_accept");
}
break;
case SOCKET_CONNECT:
sock->pending_iocp--;
sock->pending_connect = 0;
"canceled_connect");
}
break;
}
continue;
}
switch (request) {
case SOCKET_RECV:
break;
case SOCKET_SEND:
break;
case SOCKET_ACCEPT:
break;
case SOCKET_CONNECT:
break;
}
}
/*
* Exit Completion Port Thread
*/
ISC_MSG_EXITING, "SocketIoThread exiting"));
return ((isc_threadresult_t)0);
}
/*
* Create a new socket manager.
*/
}
unsigned int maxsocks)
{
if (maxsocks != 0)
return (ISC_R_NOTIMPLEMENTED);
return (ISC_R_NOMEMORY);
InitSockets();
if (result != ISC_R_SUCCESS) {
return (result);
}
"isc_condition_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
manager->totalSockets = 0;
manager->iocp_total = 0;
return (ISC_R_SUCCESS);
}
return (ISC_R_NOTIMPLEMENTED);
}
void
}
void
int i;
/*
* Destroy a socket manager.
*/
/*
* Wait for all sockets to be destroyed.
*/
"sockets exist"));
}
/*
* Here, we need to had some wait code for the completion port
* thread.
*/
/*
* Wait for threads to exit.
*/
for (i = 0; i < manager->maxIOCPThreads; i++) {
NULL) != ISC_R_SUCCESS)
"isc_thread_join() for Completion Port %s",
ISC_MSG_FAILED, "failed"));
}
/*
* Clean up.
*/
}
static void
{
/*
* Enqueue the request.
*/
"queue_receive_event: event %p -> task %p",
}
/*
* Check the pending receive queue, and if we have data pending, give it to this
* caller. If we have none, queue an I/O request. If this caller is not the first
* on the list, then we will just queue this event and return.
*
* Caller must have the socket locked.
*/
static isc_result_t
unsigned int flags)
{
return (ISC_R_EOF);
/*
* Queue our event on the list of things to do. Call our function to
* attempt to fill buffers as much as possible, and return done events.
* We are going to lie about our handling of the ISC_SOCKFLAG_IMMEDIATE
* here and tell our caller that we could not satisfy it immediately.
*/
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
/*
* If there are more receivers waiting for data, queue another receive
* here. If the
*/
return (result);
}
{
unsigned int iocount;
/*
* Make sure that the socket is not closed. XXXMLG change error here?
*/
return (ISC_R_CONNREFUSED);
}
return (ISC_R_NOMEMORY);
}
/*
* UDP sockets are always partial read
*/
else {
if (minimum == 0)
else
}
/*
* Move each buffer from the passed in list to our internal one.
*/
}
return (ret);
}
{
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
return (ISC_R_NOMEMORY);
}
return (ret);
}
{
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
event->n = 0;
event->attributes = 0;
/*
* UDP sockets are always partial read.
*/
else {
if (minimum == 0)
else
}
return (ret);
}
/*
* Caller must have the socket locked.
*/
static isc_result_t
unsigned int flags)
{
int io_state;
int send_errno = 0;
int cc = 0;
"pktinfo structure provided, ifindex %u (set to 0)",
/*
* Set the pktinfo index to 0 here, to let the kernel decide
* what interface it should send on.
*/
}
switch (io_state) {
case DOIO_PENDING: /* I/O started. Enqueue completion event. */
case DOIO_SOFT:
/*
* We couldn't send all or part of the request right now, so
* queue it unless ISC_SOCKFLAG_NORETRY is set.
*/
if ((flags & ISC_SOCKFLAG_NORETRY) == 0 ||
io_state == DOIO_PENDING) {
/*
* Enqueue the request.
*/
"socket_send: event %p -> task %p",
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
break;
}
case DOIO_SUCCESS:
break;
}
return (result);
}
{
/*
* REQUIRE() checking is performed in isc_socket_sendto().
*/
NULL));
}
{
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
return (ISC_R_NOMEMORY);
}
return (ret);
}
{
NULL, 0));
}
{
pktinfo, 0));
}
unsigned int flags)
{
unsigned int iocount;
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
return (ISC_R_NOMEMORY);
}
/*
* Move each buffer from the passed in list to our internal one.
*/
}
return (ret);
}
{
if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
event->n = 0;
event->attributes = 0;
return (ret);
}
unsigned int options) {
int bind_errno;
char strbuf[ISC_STRERRORSIZE];
int on = 1;
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
return (ISC_R_FAMILYMISMATCH);
}
/*
* Only set SO_REUSEADDR when we want a specific port.
*/
if ((options & ISC_SOCKET_REUSEADDRESS) != 0 &&
sizeof(on)) < 0) {
ISC_MSG_FAILED, "failed"));
/* Press on... */
}
switch (bind_errno) {
case WSAEACCES:
return (ISC_R_NOPERM);
case WSAEADDRNOTAVAIL:
return (ISC_R_ADDRNOTAVAIL);
case WSAEADDRINUSE:
return (ISC_R_ADDRINUSE);
case WSAEINVAL:
return (ISC_R_BOUND);
default:
strbuf);
return (ISC_R_UNEXPECTED);
}
}
return (ISC_R_SUCCESS);
}
return (ISC_R_NOTIMPLEMENTED);
}
/*
* Set up to listen on a given socket. We do this by creating an internal
* event that will be dispatched when the socket has read activity. The
* watcher will send the internal event to the task when there is a new
* connection.
*
* Unlike in read, we don't preallocate a done event here. Every time there
* is a new connection we'll have to allocate a new one anyway, so we might
* as well keep things simple rather than having to track them.
*/
char strbuf[ISC_STRERRORSIZE];
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
if (backlog == 0)
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
/*
* This should try to do aggressive accept() XXXMLG
*/
{
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
/*
* Sender field is overloaded here with the task we will be sending
* this event to. Just before the actual event is delivered the
* actual ev_sender will be touched up to be the socket.
*/
adev = (isc_socket_newconnev_t *)
return (ISC_R_NOMEMORY);
}
if (result != ISC_R_SUCCESS) {
return (result);
}
/*
* AcceptEx() requires we pass in a socket.
*/
return (ISC_R_FAILURE); // XXXMLG need real error message
}
/*
* Attach to socket and to task.
*/
if (isc_task_exiting(ntask)) {
return (ISC_R_SHUTTINGDOWN);
}
nsock->references++;
/*
* Queue io completion for an accept().
*/
sizeof(IoCompletionInfo));
0, /* Length of Buffer */
);
/*
* Enqueue the event
*/
sock->pending_accept++;
sock->pending_iocp++;
return (ISC_R_SUCCESS);
}
{
char strbuf[ISC_STRERRORSIZE];
int bind_errno;
if (isc_sockaddr_ismulticast(addr))
return (ISC_R_MULTICAST);
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
/*
* Windows sockets won't connect unless the socket is bound.
*/
switch (bind_errno) {
case WSAEACCES:
return (ISC_R_NOPERM);
case WSAEADDRNOTAVAIL:
return (ISC_R_ADDRNOTAVAIL);
case WSAEADDRINUSE:
return (ISC_R_ADDRINUSE);
case WSAEINVAL:
return (ISC_R_BOUND);
default:
sizeof(strbuf));
"bind: %s", strbuf);
return (ISC_R_UNEXPECTED);
}
}
}
sizeof(*cdev));
return (ISC_R_NOMEMORY);
}
return (ISC_R_SUCCESS);
}
/*
* Queue io completion for an accept().
*/
sizeof(IoCompletionInfo));
/*
* Attach to task.
*/
/*
* Enqueue the request.
*/
sock->pending_iocp++;
} else {
}
return (ISC_R_SUCCESS);
}
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
} else {
}
return (result);
}
char strbuf[ISC_STRERRORSIZE];
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
goto out;
}
strbuf);
goto out;
}
out:
return (result);
}
/*
* Run through the list of events on this socket, and cancel the ones
* queued for task "task" of type "how". "how" is a bitmask.
*/
void
/*
* Quick exit if there is nothing to do. Don't even bother locking
* in this case.
*/
if (how == 0)
return;
/*
* make sure that the socket's not closed
*/
return;
}
/*
* All of these do the same thing, more or less.
* Each will:
* o If the internal event is marked as "posted" try to
* remove it from the task's queue. If this fails, mark it
* as canceled instead, and let the task clean it up later.
* o For each I/O request for that task of that type, post
* its done event with status of "ISC_R_CANCELED".
* o Reset any state needed.
*/
}
}
}
how &= ~ISC_SOCKCANCEL_RECV;
}
}
}
how &= ~ISC_SOCKCANCEL_SEND;
}
}
}
how &= ~ISC_SOCKCANCEL_ACCEPT;
}
}
}
}
/*
* make sure that the socket's not closed
*/
return (ISC_R_CONNREFUSED);
}
return (type);
}
/*
* make sure that the socket's not closed
*/
return (ISC_FALSE);
}
return (val);
}
void
#if defined(IPV6_V6ONLY)
#else
#endif
#ifdef IPV6_V6ONLY
}
#endif
}
void
#if !defined(IP_TOS) && !defined(IPV6_TCLASS)
#else
if (dscp < 0)
return;
dscp <<= 2;
dscp &= 0xff;
#endif
#ifdef IP_TOS
}
#endif
#ifdef IPV6_TCLASS
}
#endif
}
void
}
{
return (ISC_R_NOTIMPLEMENTED);
}
void
/*
* Name 'socket'.
*/
}
const char *
}
void *
}
int
}
void
}
void
}
void *arg)
{
}
#ifdef HAVE_LIBXML2
static const char *
if (type == isc_sockettype_udp)
return ("udp");
else if (type == isc_sockettype_tcp)
return ("tcp");
else if (type == isc_sockettype_unix)
return ("unix");
else if (type == isc_sockettype_fdwatch)
return ("fdwatch");
else
return ("not-initialized");
}
int
{
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
int xmlrc;
#ifndef ISC_PLATFORM_USETHREADS
#endif
ISC_XMLCHAR "name"));
}
ISC_XMLCHAR "references"));
sock->references));
sizeof(peerbuf));
ISC_XMLCHAR "peer-address",
}
ISC_XMLCHAR "local-address",
}
if (sock->pending_recv)
ISC_XMLCHAR "state",
ISC_XMLCHAR "pending-receive"));
if (sock->pending_send)
ISC_XMLCHAR "state",
ISC_XMLCHAR "pending-send"));
if (sock->pending_accept)
ISC_XMLCHAR "state",
ISC_XMLCHAR "pending_accept"));
ISC_XMLCHAR "state",
ISC_XMLCHAR "listener"));
ISC_XMLCHAR "state",
ISC_XMLCHAR "connected"));
if (sock->pending_connect)
ISC_XMLCHAR "state",
ISC_XMLCHAR "connecting"));
ISC_XMLCHAR "state",
ISC_XMLCHAR "bound"));
}
return (xmlrc);
}
#endif /* HAVE_LIBXML2 */
/*
* Replace ../socket_api.c
*/
isc__socket_register(void) {
return (ISC_R_SUCCESS);
}
{
if (result == ISC_R_SUCCESS)
return (result);
}