socket.c revision dafcb997e390efa4423883dafd100c975c4095d6
/*
* Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 2000-2003 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: socket.c,v 1.30 2004/03/05 05:11:59 marka Exp $ */
/* This code has been rewritten to take advantage of Windows Sockets
* I/O Completion Ports and Events. I/O Completion Ports is ONLY
* available on Windows NT, Windows 2000 and Windows XP series of
* the Windows Operating Systems. In CANNOT run on Windows 95, Windows 98
* or the follow-ons to those Systems.
*
* 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 and receives are completed through
* the completion port. Due to an implementation bug in Windows 2000,
* Service Pack 2 must installed on the system for this code to run correctly.
* For details on this problem see Knowledge base article Q263823.
* The code checks for this. 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.
*
* All accepts and connects are accomplished through the WSAEventSelect()
* function and the event_wait loop. Events are added to and deleted from
* each event_wait thread via a common event_update stack owned by the socket
* manager. If the event_wait thread runs out of array space in the events
* array it will look for another event_wait thread to add the event. If it
* fails to find another one it will create a new thread to handle the
* outstanding event.
*
* A future enhancement is to use AcceptEx to take avantage of Overlapped
* I/O which allows for enhanced performance of TCP connections.
* This will also reduce the number of events that are waited on by the
* event_wait threads to just the connect sockets and reduce the number
* additional threads required.
*
* 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 "errno2result.h"
/*
* 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.
*
* For some reason, BSDI (and perhaps others) will sometimes return <0
* from recv() but will have errno==0. This is broken, but we have to
* work around it here.
*/
#define SOFT_ERROR(e) ((e) == WSAEINTR || \
(e) == WSA_IO_PENDING || \
(e) == WSAEWOULDBLOCK || \
(e) == EWOULDBLOCK || \
(e) == EINTR || \
(e) == EAGAIN || \
(e) == 0)
/*
* 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;
/*
* 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
/*
* NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
* a setsockopt() like interface to request timestamps, and if the OS
* doesn't do it for us, call gettimeofday() on every UDP receive?
*/
/*
* We really don't want to try and use these control messages. Win32
* doesn't have this mechanism
*/
/*
* Message header for recvmsg and sendmsg calls.
* Used value-result for recvmsg, value only for sendmsg.
*/
struct msghdr {
void *msg_name; /* optional address */
void *msg_control; /* ancillary data, see below */
int msg_flags; /* flags on received message */
} msghdr;
/*
* The number of times a send operation is repeated if the result is EINTR.
*/
#define NRETRIES 10
struct isc_socket {
/* Not locked. */
unsigned int magic;
long wait_type; /* Events to wait on */
/* Locked by socket lock. */
unsigned int references;
int pf;
/*
* Internal events. Posted when a descriptor is readable or
* writable. These are statically allocated and never freed.
* They will be set to non-purgable before use.
*/
unsigned int pending_close : 1,
pending_accept : 1,
connected : 1,
};
/*
* I/O Completion ports Info structures
*/
static int iocp_total = 0;
typedef struct IoCompletionInfo {
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
/*
* event_change structure to handle adds and deletes from the list of
* events in the Wait
*/
typedef struct event_change event_change_t;
struct event_change {
unsigned int action;
};
/*
* Note: We are using an array here since *WaitForMultiple* wants an array
* WARNING: This value may not be greater than 64 since the
* WSAWaitForMultipleEvents function is limited to 64 events.
*/
#define MAX_EVENTS 64
/*
* List of events being waited on and their associated sockets
*/
typedef struct sock_event_list {
int max_event;
int total_events;
/*
* Thread Event structure for managing the threads handling events
*/
typedef struct events_thread events_thread_t;
struct events_thread {
};
struct isc_socketmgr {
/* Not locked. */
unsigned int magic;
/* Locked by manager lock. */
int event_written;
int maxIOCPThreads;
};
#define CLOSED 0 /* this one must be zero */
#define MANAGED 1
#define CLOSE_PENDING 2
/*
* send() and recv() iovec counts
*/
static void free_socket(isc_socket_t **);
enum {
};
enum {
};
#define SOCK_DEAD(s) ((s)->references == 0)
#if defined(ISC_SOCKET_DEBUG)
/*
* 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
char socktext[256];
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");
}
}
#endif
/* 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);
}
}
}
/*
* 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 miniumum number of structures is 10, there is no maximum
*/
/* 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
*/
}
void
/* Get each of the service threads to exit
*/
}
}
/*
* Add sockets in here and pass the sock data in as part of the information needed
*/
void
}
}
void
int i;
/* Initialize the Event List */
evlist->total_events = 0;
for (i = 0; i < MAX_EVENTS; i++) {
}
}
/*
* Event Thread Initialization
*/
/*
* Start up the event wait thread.
*/
"isc_thread_create() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
/*
* Locate a thread with space for additional events or create one if
* necessary. The manager is locked at this point so the information
* cannot be changed by another thread while we are searching.
*/
void
/*
* We need to find a thread with space to add an event
* If we find it, alert it to process the event change
* list
*/
return;
}
}
/*
* We need to create a new thread as other threads are full.
* If we succeed in creating the thread, alert it to
* process the event change list since it will have space.
* If we are unable to create one, the event will stay on the
* list and the next event_wait thread will try again to add
* the event. It will call here again if it has no space.
*/
}
}
int max_event;
if(max_event >= MAX_EVENTS) {
return (ISC_FALSE);
}
evlist->total_events++;
return (ISC_TRUE);
}
/*
* Note that the eventLock is locked before calling this function
* All Events and associated sockes are closed here
*/
int i;
int iEvent = -1;
/* Make sure this is the right thread from which to delete the event */
return (ISC_FALSE);
/* Find the Event */
iEvent = i;
break;
}
}
/* Actual event start at 1 */
if (iEvent < 1)
return (ISC_FALSE);
}
/* Cleanup */
evlist->total_events--;
return (ISC_TRUE);
}
/*
* Get the event changes off of the list and apply the
* requested changes. The manager lock is taken out at
* the start of this function to prevent other event_wait
* threads processing the same information at the same
* time. The queue may not be empty on exit since other
* threads may be involved in processing the queue.
*
* The deletes are done first in order that there be space
* available for the events being added in the same thread
* in case the event list is almost full. This reduces the
* probability of having to create another thread which would
* increase overhead costs.
*/
/* First the deletes */
/* Delete only if this thread's socket list was updated */
if (del) {
manager->event_written--;
}
}
}
/* Now the adds */
/* Delete only if this thread's socket list was updated */
if (del) {
manager->event_written--;
}
}
}
return (ISC_R_SUCCESS);
}
/*
* Add the event list changes to the queue and notify the
* event loop
*/
static void
unsigned int action) {
sizeof(event_change_t));
/* Alert the Wait List */
else
}
/*
* Note that the socket is already locked before calling this function
*/
int stat;
char strbuf[ISC_STRERRORSIZE];
const char *msg;
hEvent = WSACreateEvent();
if (hEvent == WSA_INVALID_EVENT) {
stat = WSAGetLastError();
ISC_MSG_FAILED, "failed"),
return (ISC_R_UNEXPECTED);
}
stat = WSAGetLastError();
ISC_MSG_FAILED, "failed");
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
/*
* Note that the socket is not locked before calling this function
*/
void
sock->evthread_id = 0;
}
}
/*
* 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 the Wait should not
* be running while closing an event or a socket.
*/
void
else {
}
}
}
/*
* Initialize socket services
*/
BOOL InitSockets() {
int err;
/* Need Winsock 2.0 or better */
if ( err != 0 ) {
/* Tell the user that we could not find a usable Winsock DLL */
return(FALSE);
}
return(TRUE);
}
int
int Result;
int total_sent;
*Error = 0;
(LPOVERLAPPED) lpo,
NULL);
total_sent = (int) BytesSent;
/* Check for errors.*/
if (Result == SOCKET_ERROR) {
*Error = WSAGetLastError();
switch (*Error) {
case NO_ERROR :
case WSA_IO_INCOMPLETE :
case WSA_WAIT_IO_COMPLETION :
case WSA_IO_PENDING :
break;
default :
return (-1);
break;
}
}
return (0);
else
return (total_sent);
}
int
int total_bytes = 0;
int Result;
*Error = 0;
&NumBytes,
&Flags,
(int *)&(messagehdr->msg_namelen),
(LPOVERLAPPED) lpo,
NULL);
total_bytes = (int) NumBytes;
/* Check for errors. */
if (Result == SOCKET_ERROR) {
*Error = WSAGetLastError();
switch (*Error) {
case NO_ERROR :
case WSA_IO_INCOMPLETE :
case WSA_WAIT_IO_COMPLETION :
case WSA_IO_PENDING :
break;
default :
return (-1);
break;
}
}
/* Return the flags received in header */
return (-1);
else
return (total_bytes);
}
static void
const char *fmt, ...) {
char msgbuf[2048];
return;
}
static void
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 WASRecvFrom
* 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_majorversion() < 5)
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.
*
* If write_countp != NULL, *write_countp will hold the number of bytes
* this transaction can send.
*/
static void
unsigned int iovcount;
} else {
msg->msg_namelen = 0;
}
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;
}
+ skip_count);
skip_count = 0;
iovcount++;
}
}
INSIST(skip_count == 0);
if (write_countp != NULL)
}
/*
* Construct an iov array and attach it to the msghdr passed in. This is
* the RECV constructor, which will use the available 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.
*
* If read_countp != NULL, *read_countp will hold the number of bytes
* this transaction can receive.
*/
static void
unsigned int iovcount;
} else { /* TCP */
msg->msg_namelen = 0;
}
read_count = 0;
/*
* Single buffer I/O? Skip what we've done so far in this region.
*/
iovcount = 1;
} else {
/*
* Multibuffer I/O.
* Skip empty buffers.
*/
if (isc_buffer_availablelength(buffer) != 0)
break;
}
iovcount = 0;
iovcount++;
}
}
}
/*
* If needed, set up to receive that one extra byte. Note that
* we know there is at least one iov left, since we stole it
* at the top of this function.
*/
if (read_countp != NULL)
}
static void
isc_socketevent_t *dev) {
else
}
}
static isc_socketevent_t *
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%d\tbase %p, len %d\n", i,
}
#endif
#define DOIO_SUCCESS 0 /* i/o ok, event sent */
static int
if (recv_errno == _system) { \
return (DOIO_HARD); \
} \
return (DOIO_SOFT); \
}
if (recv_errno == _system) { \
return (DOIO_HARD); \
}
if (recv_errno != 0) {
if (SOFT_ERROR(recv_errno))
return (DOIO_SOFT);
return (DOIO_HARD);
}
/*
* On TCP, zero length reads indicate EOF, while on
* UDP, zero length reads are perfectly valid, although
* strange.
*/
return (DOIO_EOF);
"dropping source port zero packet");
}
return (DOIO_SOFT);
}
}
"packet received correctly");
/*
* Overflow bit detection. If we received MORE bytes than we should,
* this indicates an overflow situation. Set the flag in the
* dev entry and adjust how much we read by one.
*/
#ifdef ISC_NET_RECVOVERFLOW
cc--;
}
#endif
/*
* update the buffers (if any) and the i/o count
*/
actual_count = cc;
} else {
actual_count = 0;
break;
}
INSIST(actual_count == 0);
}
}
/*
* If we read less than we expected, update counters,
* and let the upper layer handle it.
*/
return (DOIO_SOFT);
/*
* Full reads are posted, or partials if partials are ok.
*/
return (DOIO_SUCCESS);
}
static int
char strbuf[ISC_STRERRORSIZE];
int status;
struct msghdr messagehdr;
if (!bwait) {
HEAP_ZERO_MEMORY, sizeof(IoCompletionInfo));
} else { /* Wait for recv to complete */
msghdr = &messagehdr;
}
sock->references++;
&(sock->totalBytes));
#if defined(ISC_SOCKET_DEBUG)
#endif
if (*nbytes < 0) {
if (SOFT_ERROR(*recv_errno)) {
goto done;
}
"startio_recv: recvmsg(%d) %d bytes, err %d/%s",
}
sock->references--;
}
goto done;
}
done:
return (status);
}
/*
* 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
int send_errno) {
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
char strbuf[ISC_STRERRORSIZE];
if(send_errno != 0) {
if (SOFT_ERROR(send_errno))
return (DOIO_SOFT);
if (send_errno == _system) { \
return (DOIO_HARD); \
} \
return (DOIO_SOFT); \
}
if (send_errno == _system) { \
return (DOIO_HARD); \
}
/*
* The other error types depend on whether or not the
* socket is UDP or TCP. If it is UDP, some errors
* that we expect to be fatal under TCP are merely
* annoying, and are really soft errors.
*
* However, these soft errors are still returned as
* a status.
*/
return (DOIO_HARD);
}
/*
* 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
char strbuf[ISC_STRERRORSIZE];
int status;
struct msghdr messagehdr;
if (!bwait) {
HEAP_ZERO_MEMORY, sizeof(IoCompletionInfo));
} else { /* Wait for send to complete */
msghdr = &messagehdr;
}
sock->references++;
&(sock->totalBytes));
if (*nbytes < 0) {
if (SOFT_ERROR(*send_errno)) {
goto done;
}
"startio_send: internal_sendmsg(%d) %d bytes, err %d/%s",
}
sock->references--;
}
goto done;
}
done:
return (status);
}
/*
* Kill.
*
* Caller must ensure that the socket is not locked and no external
* references exist.
*/
static void
/*
* No one has this socket open and the socket doesn't have to be
* locked. The socket_close function makes sure that if needed
* the event_wait loop removes any associated event from the list
* of events being waited on.
*/
/*
* XXX should reset manager->maxfd here
*/
}
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_close = 0;
sock->connecting = 0;
sock->evthread_id = 0;
/*
* initialize the lock
*/
"isc_mutex_init() %s",
ISC_MSG_FAILED, "failed"));
goto error;
}
/*
* Initialize readable and writable events
*/
return (ISC_R_SUCCESS);
return (ret);
}
/*
* This event requires that the various lists be empty, that the reference
* count be 1, and that the magic number is valid. The other socket bits,
* like the lock, must be initialized as well. The fd associated must be
* marked as closed, by setting it to INVALID_SOCKET on close, or this
* routine will also close the socket.
*/
static void
}
/*
* 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'.
*/
isc_socket_t **socketp) {
#if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
int on = 1;
#endif
int socket_errno;
char strbuf[ISC_STRERRORSIZE];
if (result != ISC_R_SUCCESS)
return (result);
switch (type) {
case isc_sockettype_udp:
if (result != ISC_R_SUCCESS) {
free_socket(&sock);
return (result);
}
break;
case isc_sockettype_tcp:
break;
}
free_socket(&sock);
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) {
free_socket(&sock);
return (result);
}
#if defined(USE_CMSG)
if (type == isc_sockettype_udp) {
#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 */
#ifdef IPV6_USE_MIN_MTU /*2292bis, not too common yet*/
/* use minimum MTU */
}
#endif
#endif /* ISC_PLATFORM_HAVEIPV6 */
}
#endif /* USE_CMSG */
/*
* 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);
}
/*
* 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--;
if (sock->references == 0)
if (kill_socket)
}
/*
* 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 socket 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.
*
* Caller must have the socket locked if the event is attached to the socket.
*/
static void
}
else
}
/*
* Call accept() on a socket, to get the new file descriptor. The listen
* socket is used as a prototype to create a new isc_socket_t. The new
* socket has one outstanding reference. The task receiving the event
* will be detached from just after the event is delivered.
*
* 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 unlock and return.
*/
static void
char strbuf[ISC_STRERRORSIZE];
"internal_accept called, locked socket");
sock->pending_accept = 0;
if (sock->references == 0) {
return;
}
/*
* Check any possible error status from the event notification here.
* Note that we don't take any action since it was only
* Windows that was notifying about a network event, not the
* application.
* PDMXXX: Should we care about any of the possible event errors
* signalled? The only possible valid errors are:
* WSAENETDOWN, WSAECONNRESET, & WSAECONNABORTED
*/
if (accept_errno != 0) {
switch (accept_errno) {
case WSAENETDOWN:
case WSAECONNRESET:
case WSAECONNABORTED:
break; /* Expected errors */
default:
"internal_accept: from event wait: %s",
strbuf);
break;
}
return;
}
/*
* Get the first item off the accept list.
* If it is empty, unlock the socket and return.
*/
/*
* This should only happen if WSAEventSelect() fails
* below or in isc_socket_cancel().
*/
if (fd != INVALID_SOCKET) {
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
"sock->accept_list empty: "
"dropping TCP request from %s",
addrbuf);
(void)closesocket(fd);
}
return;
}
/*
* Try to accept the new connection. If the accept fails with
* EAGAIN or EINTR, the event wait will be notified again since
* the event will be reset on return to caller.
*/
(void *)&addrlen);
if (fd == INVALID_SOCKET) {
goto soft_error;
} else {
"internal_accept: accept() %s: %s",
"failed"),
strbuf);
fd = INVALID_SOCKET;
}
} else {
if (addrlen == 0) {
"internal_accept(): "
"accept() failed to return "
"remote address");
(void)closesocket(fd);
goto soft_error;
{
"internal_accept(): "
"accept() returned peer address "
"family %u (expected %u)",
(void)closesocket(fd);
goto soft_error;
}
}
if (fd != INVALID_SOCKET) {
}
/*
* Pull off the done event.
*/
/*
* Stop listing for connects.
*/
int stat;
const char *msg;
stat = WSAGetLastError();
ISC_MSG_FAILED, "failed");
}
if (fd != INVALID_SOCKET) {
if (tresult != ISC_R_SUCCESS) {
fd = INVALID_SOCKET;
}
}
/*
* INVALID_SOCKET means the new socket didn't happen.
*/
if (fd != INVALID_SOCKET) {
/*
* The accept socket inherits the listen socket's
* selected events. Remove this socket from all events
* as it is handled by IOCP. (Joe Quanaim, lucent.com)
*/
/* this is an unlikely but non-fatal error */
int stat;
const char *msg;
stat = WSAGetLastError();
ISC_MSG_FAILED, "failed");
}
/*
* Save away the remote address
*/
"accepted connection, new socket %p",
} else {
}
/*
* Fill in the done event details and send it off.
*/
return;
return;
}
/*
* Called when a socket with a pending connect() finishes.
*/
static void
char strbuf[ISC_STRERRORSIZE];
/*
* When the internal event was sent the reference count was bumped
* to keep the socket around for us. Decrement the count here.
*/
sock->references--;
if (sock->references == 0) {
return;
}
/*
* Has this event been canceled?
*/
return;
}
sock->connecting = 0;
/*
* Check possible Windows network event error status here.
*/
if (connect_errno != 0) {
/*
* If the error is EAGAIN, just try again on this
* fd and pretend nothing strange happened.
*/
if (SOFT_ERROR(connect_errno) ||
{
return;
}
/*
* Translate other errors into ISC_R_* flavors.
*/
switch (connect_errno) {
default:
"internal_connect: connect() %s",
strbuf);
}
} else {
}
}
static void
internal_recv(isc_socket_t *sock, isc_socketevent_t *dev, struct msghdr *messagehdr, int nbytes, int recv_errno) {
int io_state;
int cc;
"internal_recv: task got socket event %p", dev);
if (sock->references == 0) {
return;
}
/* If the event is no longer in the list we can just return */
}
goto done;
/*
* Try to do as much I/O as possible on this socket. There are no
* limits here, currently.
*/
case DOIO_SOFT:
cc = 0;
recv_errno = 0;
goto done;
case DOIO_EOF:
/*
* read of 0 means the remote end was closed.
* Run through the event queue and dispatch all
* the events with an EOF result code.
*/
goto done;
case DOIO_SUCCESS:
case DOIO_HARD:
break;
}
done:
}
static void
internal_send(isc_socket_t *sock, isc_socketevent_t *dev, struct msghdr *messagehdr, int nbytes, int send_errno) {
int io_state;
int cc;
/*
* Find out what socket this is and lock it.
*/
"internal_send: task got socket event %p", dev);
if (sock->references == 0) {
return;
}
/* If the event is no longer in the list we can just return */
}
goto done;
/*
* Try to do as much I/O as possible on this socket. There are no
* limits here, currently.
*/
case DOIO_SOFT:
cc = 0;
send_errno = 0;
goto done;
case DOIO_HARD:
case DOIO_SUCCESS:
break;
}
done:
}
/*
* 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.
*/
errval = GetLastError();
"Can't set thread priority: %s"),
strbuf);
}
/*
* Loop forever waiting on I/O Completions and then processing them
*/
while(TRUE) {
&nbytes,
(LPOVERLAPPED *)&lpo,
);
/*
* Received request to exit
*/
break;
}
errstatus = 0;
if(!bSuccess) {
/*
* I/O Failure
* Find out why
*/
}
switch (request) {
case SOCKET_CANCEL:
break;
case SOCKET_RECV:
break;
case SOCKET_SEND:
break;
default:
break; /* Unknown: Just ignore it */
}
}
/*
* Exit Completion Port Thread
*/
ISC_MSG_EXITING, "SocketIoThread exiting"));
return ((isc_threadresult_t)0);
}
/*
* This is the thread that will loop forever, waiting for an event to
* happen.
*
* When the wait returns something to do, find the signaled event
* and issue the request for the given socket
*/
static isc_threadresult_t WINAPI
event_wait(void *uap) {
int cc;
int event_errno;
char strbuf[ISC_STRERRORSIZE];
int iEvent;
int max_event;
int err;
/* We need to know the Id of the thread */
/* See if there's anything waiting to add to the event list */
if (manager->event_written > 0)
do {
event_errno = 0;
FALSE);
if (cc == WSA_WAIT_FAILED) {
if (!SOFT_ERROR(event_errno)) {
sizeof(strbuf));
"WSAWaitForMultipleEvents() %s: %s",
"failed"),
strbuf);
}
}
&& manager->event_written == 0);
break;
/*
* Add or delete events as requested
*/
if (manager->event_written > 0)
/*
* Stopped to add and delete events on the list
*/
if (iEvent == 0)
continue;
continue;
&NetworkEvents) == SOCKET_ERROR) {
err = WSAGetLastError();
"event_wait: WSAEnumNetworkEvents() %s",
strbuf);
/* XXXMPA */
}
if(NetworkEvents.lNetworkEvents == 0 ) {
continue;
}
/*
* Check for FD_CLOSE events first. This takes precedence over
* other possible events as it needs to be handled instead of
* any other event if it happens on the socket.
* The error code found, if any, is fed into the internal_*()
* routines.
*/
} else {
"event_wait: WSAEnumNetworkEvents() "
"unexpected event bit set: %0x",
}
wsock->pending_accept == 0) {
wsock->references++;
}
else {
wsock->references++;
}
}
}
ISC_MSG_EXITING, "event_wait exiting"));
return ((isc_threadresult_t)0);
}
/*
* Create a new socket manager.
*/
return (ISC_R_NOMEMORY);
"isc_mutex_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
"isc_condition_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
/*
* Event Wait Thread Initialization
*/
/*
* Start up the initial event wait thread.
*/
if (result != ISC_R_SUCCESS) {
return (result);
}
manager->event_written = 0;
/* Initialize the event update list */
return (ISC_R_SUCCESS);
}
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.
*/
/*
* Shut down the event wait threads
*/
"isc_thread_join() for event_wait %s",
ISC_MSG_FAILED, "failed"));
}
/*
* Now the I/O Completion Port Worker Threads
*/
for (i = 0; i < manager->maxIOCPThreads; i++) {
!= ISC_R_SUCCESS)
"isc_thread_join() for Completion Port %s",
ISC_MSG_FAILED, "failed"));
}
/*
* Clean up.
*/
}
static isc_result_t
unsigned int flags) {
int io_state;
int cc = 0;
int recv_errno = 0;
switch (io_state) {
case DOIO_SOFT:
/*
* We couldn't read all or part of the request right now, so
* queue it.
*
* Attach to socket and to task
*/
/*
* Enqueue the request.
*/
"socket_recv: event %p -> task %p",
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
break;
case DOIO_EOF:
/* fallthrough */
case DOIO_HARD:
case DOIO_SUCCESS:
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
break;
}
return (result);
}
{
unsigned int iocount;
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 (ISC_R_NOMEMORY);
}
{
event->n = 0;
event->attributes = 0;
/*
* UDP sockets are always partial read.
*/
else {
if (minimum == 0)
else
}
}
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_SOFT:
/*
* We couldn't send all or part of the request right now, so
* queue it unless ISC_SOCKFLAG_NORETRY is set.
*/
if (!have_lock) {
}
/*
* Enqueue the request.
*/
"socket_send: event %p -> task %p",
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
break;
case DOIO_SUCCESS:
break;
}
if (have_lock)
return (result);
}
{
/*
* REQUIRE() checking is performed in isc_socket_sendto().
*/
NULL));
}
{
return (ISC_R_NOMEMORY);
}
}
{
NULL));
}
{
unsigned int iocount;
return (ISC_R_NOMEMORY);
}
/*
* Move each buffer from the passed in list to our internal one.
*/
}
}
{
if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
event->n = 0;
event->attributes = 0;
}
int bind_errno;
char strbuf[ISC_STRERRORSIZE];
int on = 1;
return (ISC_R_FAMILYMISMATCH);
}
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];
if (backlog == 0)
return (ISC_R_UNEXPECTED);
}
/* Add the socket to the list of events to accept */
if (retstat != ISC_R_SUCCESS) {
if (retstat != ISC_R_NOSPACE) {
sizeof(strbuf));
"isc_socket_listen: socket_event_add: %s", strbuf);
}
return (retstat);
}
return (ISC_R_SUCCESS);
}
/*
* This should try to do agressive accept() XXXMLG
*/
{
/*
* 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.
*/
dev = (isc_socket_newconnev_t *)
return (ISC_R_NOMEMORY);
}
if (ret != ISC_R_SUCCESS) {
return (ret);
}
/*
* Attach to socket and to task.
*/
nsock->references++;
/*
* Wait for connects.
*/
char strbuf[ISC_STRERRORSIZE];
int stat;
const char *msg;
stat = WSAGetLastError();
ISC_MSG_FAILED, "failed");
return (ISC_R_UNEXPECTED);
}
/*
* Enqueue the event
*/
return (ISC_R_SUCCESS);
}
{
int cc;
int retstat;
int errval;
char strbuf[ISC_STRERRORSIZE];
if (isc_sockaddr_ismulticast(addr))
return (ISC_R_MULTICAST);
sizeof(*dev));
return (ISC_R_NOMEMORY);
}
/*
* Try to do the connect right away, as there can be only one
* outstanding, and it might happen to complete.
*/
if (cc < 0) {
errval = WSAGetLastError();
goto queue;
switch (errval) {
}
return (ISC_R_UNEXPECTED);
return (ISC_R_SUCCESS);
}
/*
* If connect completed, fire off the done event.
*/
if (cc == 0) {
return (ISC_R_SUCCESS);
}
/*
* Attach to task.
*/
/*
* Enqueue the request.
*/
/* Add the socket to the list of events to connect */
if (retstat != ISC_R_SUCCESS) {
if (retstat != ISC_R_NOSPACE) {
sizeof(strbuf));
"isc_socket_connect: socket_event_add: %s", strbuf);
}
return (retstat);
}
return (ISC_R_SUCCESS);
}
ret = ISC_R_SUCCESS;
} else {
}
return (ret);
}
char strbuf[ISC_STRERRORSIZE];
goto out;
}
ret = ISC_R_SUCCESS;
strbuf);
goto out;
}
out:
return (ret);
}
/*
* 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;
/*
* 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.
*/
}
}
}
}
}
}
ev_link);
(isc_event_t **)&dev);
}
}
char strbuf[ISC_STRERRORSIZE];
int stat;
const char *msg;
stat = WSAGetLastError();
ISC_MSG_FAILED, "failed");
}
}
/*
* Connecting is not a list.
*/
sock->connecting = 0;
(isc_event_t **)&dev);
}
}
}
}
return (val);
}
void
#if defined(IPV6_V6ONLY)
#else
#endif
#ifdef IPV6_V6ONLY
}
#endif
}