socket.c revision 76c8294c81fb48b1da6e1fc5b83322a4cedb8e58
/*
* Copyright (C) 1998-2001 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 INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM 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.205 2001/08/08 22:54:52 gson Exp $ */
#include <config.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <isc/bufferlist.h>
#include <isc/condition.h>
#include <isc/formatcheck.h>
#include <isc/platform.h>
#include "errno2result.h"
#ifndef ISC_PLATFORM_USETHREADS
#include "socket_p.h"
#endif /* ISC_PLATFORM_USETHREADS */
/*
* 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) == EAGAIN || \
(e) == EWOULDBLOCK || \
(e) == EINTR || \
(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?
*/
#ifdef SO_TIMESTAMP
#ifndef USE_CMSG
#define USE_CMSG 1
#endif
#endif
/*
* Check to see if we have even basic support for cracking messages from
*/
#endif
/*
* Determine the size of the control data buffer.
*/
#if USE_CMSG
#ifdef ISC_PLATFORM_HAVEIPV6
#else
#define CMSG_BUF_V6_SIZE 0
#endif
#ifdef SO_TIMESTAMP
#else
#define CMSG_BUF_TS_SIZE 0
#endif
#endif /* USE_CMSG */
/*
* 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;
/* Locked by socket lock. */
unsigned int references;
int fd;
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_recv : 1,
pending_send : 1,
pending_accept : 1,
connected : 1,
#ifdef ISC_NET_RECVOVERFLOW
unsigned char overflow; /* used for MSG_TRUNC fake */
#endif
};
struct isc_socketmgr {
/* Not locked. */
unsigned int magic;
/* Locked by manager lock. */
int fdstate[FD_SETSIZE];
int maxfd;
#ifdef ISC_PLATFORM_USETHREADS
int pipe_fds[2];
#else /* ISC_PLATFORM_USETHREADS */
unsigned int refs;
#endif /* ISC_PLATFORM_USETHREADS */
};
#ifndef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
#define CLOSED 0 /* this one must be zero */
#define MANAGED 1
#define CLOSE_PENDING 2
/*
* send() and recv() iovec counts
*/
#ifdef ISC_NET_RECVOVERFLOW
#else
# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
#endif
static void free_socket(isc_socket_t **);
isc_socket_t **);
static void destroy(isc_socket_t **);
#define SELECT_POKE_SHUTDOWN (-1)
#define SELECT_POKE_NOTHING (-2)
#define SELECT_POKE_READ (-3)
#define SELECT_POKE_WRITE (-4)
#define SELECT_POKE_CLOSE (-5)
#define SOCK_DEAD(s) ((s)->references == 0)
static void
static void
const char *fmt, ...)
{
char msgbuf[2048];
return;
}
static void
static void
const char *fmt, ...)
{
char msgbuf[2048];
char peerbuf[256];
return;
} else {
}
}
static void
/*
* This is a wakeup on a socket. If the socket is not in the
* process of being closed, start watching it for either reads
* or writes.
*/
return;
}
return;
/*
* If there are no events, or there is an event but we
* have already queued up the internal event on a task's
* queue, clear the bit. Otherwise, set it.
*/
if (msg == SELECT_POKE_READ)
if (msg == SELECT_POKE_WRITE)
}
#ifdef ISC_PLATFORM_USETHREADS
/*
* Poke the select loop when there is something for us to do.
* The write is required (by POSIX) to complete. That is, we
* will not get partial writes.
*/
static void
int cc;
int buf[2];
do {
if (cc < 0)
"write() failed "
"during watcher poke: %s"),
}
/*
* Read a message on the internal fd.
*/
static void
int buf[2];
int cc;
if (cc < 0) {
if (SOFT_ERROR(errno))
return;
"read() failed "
"during watcher poke: %s"),
return;
}
}
#else /* ISC_PLATFORM_USETHREADS */
/*
* Update the state of the socketmgr when something changes.
*/
static void
if (msg == SELECT_POKE_SHUTDOWN)
return;
else if (fd >= 0)
return;
}
#endif /* ISC_PLATFORM_USETHREADS */
/*
* Make a fd non-blocking.
*/
static isc_result_t
make_nonblock(int fd) {
int ret;
int flags;
flags |= O_NONBLOCK;
if (ret == -1) {
"fcntl(%d, F_SETFL, %d): %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
/*
* Process control messages received on a socket.
*/
static void
#ifdef USE_CMSG
#ifdef ISC_PLATFORM_HAVEIPV6
struct in6_pktinfo *pktinfop;
#endif
#ifdef SO_TIMESTAMP
#endif
#endif
/*
* sock is used only when ISC_NET_BSD44MSGHDR and USE_CMSG are defined.
* msg and dev are used only when ISC_NET_BSD44MSGHDR is defined.
* They are all here, outside of the CPP tests, because it is
* more consistent with the usual ISC coding style.
*/
#ifndef ISC_NET_BSD44MSGHDR
return;
#else /* defined ISC_NET_BSD44MSGHDR */
#ifdef MSG_TRUNC
#endif
#ifdef MSG_CTRUNC
#endif
/*
* Check for multicast.
*/
#ifndef USE_CMSG
return;
#else
return;
#ifdef SO_TIMESTAMP
#endif
#ifdef ISC_PLATFORM_HAVEIPV6
#endif
"processing cmsg %p", cmsgp);
#ifdef ISC_PLATFORM_HAVEIPV6
sizeof(struct in6_pktinfo));
"interface received on ifindex %u",
goto next;
}
#endif
#ifdef SO_TIMESTAMP
goto next;
}
#endif
next:
}
#endif /* USE_CMSG */
#endif /* ISC_NET_BSD44MSGHDR */
}
/*
* 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;
#ifndef USE_CMSG
#endif
} 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);
#ifdef ISC_NET_BSD44MSGHDR
msg->msg_controllen = 0;
#ifdef USE_CMSG
struct in6_pktinfo *pktinfop;
"sendto pktinfo data, ifindex %u",
}
#endif /* USE_CMSG */
#else /* ISC_NET_BSD44MSGHDR */
msg->msg_accrightslen = 0;
#endif /* ISC_NET_BSD44MSGHDR */
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 avialable 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;
#ifndef USE_CMSG
#endif
#ifdef ISC_NET_RECVOVERFLOW
/* If needed, steal one iovec for overflow detection. */
maxiov--;
#endif
} 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;
goto config;
}
/*
* 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.
*/
#ifdef ISC_NET_RECVOVERFLOW
iovcount++;
}
#endif
#ifdef ISC_NET_BSD44MSGHDR
msg->msg_controllen = 0;
#if defined(USE_CMSG)
}
#endif /* USE_CMSG */
#else /* ISC_NET_BSD44MSGHDR */
msg->msg_accrightslen = 0;
#endif /* ISC_NET_BSD44MSGHDR */
if (read_countp != NULL)
}
static void
{
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,
#ifdef ISC_NET_BSD44MSGHDR
#endif
}
#endif
#define DOIO_SUCCESS 0 /* i/o ok, event sent */
static int
int cc;
int recv_errno;
#if USE_CMSG
char cmsg[CMSG_BUF_SIZE];
#else
#endif
#if defined(ISC_SOCKET_DEBUG)
#endif
recv_errno = errno;
if (cc < 0) {
if (SOFT_ERROR(recv_errno))
return (DOIO_SOFT);
"doio_recv: recvmsg(%d) %d bytes, err %d/%s",
if (recv_errno == _system) { \
return (DOIO_HARD); \
} \
return (DOIO_SOFT); \
}
if (recv_errno == _system) { \
return (DOIO_HARD); \
}
return (DOIO_HARD);
}
/*
* On TCP, zero length reads indicate EOF, while on
* UDP, zero length reads are perfectly valid, although
* strange.
*/
return (DOIO_EOF);
"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
/*
* If there are control messages attached, run through them and pull
* out the interesting bits.
*/
/*
* 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 poke the descriptor.
*/
return (DOIO_SOFT);
/*
* Full reads are posted, or partials if partials are ok.
*/
return (DOIO_SUCCESS);
}
/*
* 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 cc;
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
#if USE_CMSG
char cmsg[CMSG_BUF_SIZE];
#else
#endif
int attempts = 0;
int send_errno;
send_errno = errno;
/*
* Check for error or block condition.
*/
if (cc < 0) {
goto resend;
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); \
}
#ifdef EHOSTDOWN
#endif
/*
* 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.
*/
"internal_send: %s: %s",
return (DOIO_HARD);
}
if (cc == 0)
"internal_send: send() %s 0",
ISC_MSG_RETURNED, "returned"));
/*
* 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);
}
/*
* Kill.
*
* Caller must ensure that the socket is not locked and no external
* references exist.
*/
static void
ISC_MSG_DESTROYING, "destroying");
/*
* No one has this socket open, so the watcher doesn't have to be
* poked, and the socket doesn't have to be locked.
*/
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
/*
* XXX should reset manager->maxfd here
*/
}
static isc_result_t
{
return (ISC_R_NOMEMORY);
sock->references = 0;
/*
* set up list of readers and writers to be initially empty
*/
sock->pending_recv = 0;
sock->pending_send = 0;
sock->pending_accept = 0;
sock->connecting = 0;
/*
* initialize the lock
*/
"isc_mutex_init() %s",
ISC_MSG_FAILED, "failed"));
goto error;
}
/*
* Initialize readable and writable events
*/
return (ISC_R_SUCCESS);
error: /* socket allocated */
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 -1 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'.
*/
{
#if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
int on = 1;
#endif
if (ret != ISC_R_SUCCESS)
return (ret);
switch (type) {
case isc_sockettype_udp:
break;
case isc_sockettype_tcp:
break;
}
"%s: too many open file descriptors", "socket");
free_socket(&sock);
return (ISC_R_NORESOURCES);
}
free_socket(&sock);
switch (errno) {
case EMFILE:
case ENFILE:
case ENOBUFS:
return (ISC_R_NORESOURCES);
case EPROTONOSUPPORT:
case EPFNOSUPPORT:
case EAFNOSUPPORT:
#ifdef LINUX
/*
* Linux 2.2 (and maybe others) return EINVAL instead of
* EAFNOSUPPORT.
*/
case EINVAL:
#endif
return (ISC_R_FAMILYNOSUPPORT);
default:
"socket() %s: %s",
"failed"),
return (ISC_R_UNEXPECTED);
}
}
free_socket(&sock);
return (ISC_R_UNEXPECTED);
}
#ifdef SO_BSDCOMPAT
"setsockopt(%d, SO_BSDCOMPAT) %s: %s",
ISC_MSG_FAILED, "failed"),
/* Press on... */
}
#endif
#if defined(USE_CMSG)
if (type == isc_sockettype_udp) {
#if defined(SO_TIMESTAMP)
&& errno != ENOPROTOOPT) {
"setsockopt(%d, SO_TIMESTAMP) %s: %s",
"failed"),
/* Press on... */
}
#endif /* SO_TIMESTAMP */
#if defined(ISC_PLATFORM_HAVEIPV6)
#ifdef IPV6_RECVPKTINFO
/* 2292bis */
"setsockopt(%d, IPV6_RECVPKTINFO) "
"failed"),
}
#else
/* 2292 */
"setsockopt(%d, IPV6_PKTINFO) %s: %s",
"failed"),
}
#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.
*/
ISC_MSG_CREATED, "created");
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)
}
/*
* I/O is possible on a given socket. Schedule an event to this task that
* will call an internal function to do the I/O. This will charge the
* task with the I/O operation and let our select loop handler get back
* to doing something real as fast as possible.
*
* The socket and manager must be locked before calling this function.
*/
static void
return;
sock->references++;
}
static void
return;
sock->references++;
}
/*
* Dispatch an internal accept event.
*/
static void
/*
* Are there any done events left, or were they all canceled
* before the manager got the socket lock?
*/
return;
}
static void
}
/*
* 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
int fd;
"internal_accept called, locked socket");
sock->pending_accept = 0;
if (sock->references == 0) {
return;
}
/*
* Get the first item off the accept list.
* If it is empty, unlock the socket and return.
*/
return;
}
/*
* Try to accept the new connection. If the accept fails with
* EAGAIN or EINTR, simply poke the watcher to watch this socket
* again. Also ignore ECONNRESET, which has been reported to
* be spuriously returned on Linux 2.2.19 although it is not
* a documented error for accept().
*/
(void *)&addrlen);
if (fd < 0) {
goto soft_error;
} else {
"internal_accept: accept() %s: %s",
"failed"),
fd = -1;
}
} else {
if (addrlen == 0) {
"internal_accept(): "
"accept() failed to return "
"remote address");
goto soft_error;
{
"internal_accept(): "
"accept() returned peer address "
"family %u (expected %u)",
goto soft_error;
} else if (fd >= FD_SETSIZE) {
"%s: too many open file descriptors",
"accept");
goto soft_error;
}
}
if (fd != -1) {
}
/*
* Pull off the done event.
*/
/*
* Poke watcher if there are more pending accepts.
*/
fd = -1;
}
/*
* -1 means the new socket didn't happen.
*/
if (fd != -1) {
/*
* Save away the remote address
*/
"accepted connection, new socket %p",
} else {
}
/*
* Fill in the done event details and send it off.
*/
return;
return;
}
static void
sock->pending_recv = 0;
if (sock->references == 0) {
return;
}
/*
* Try to do as much I/O as possible on this socket. There are no
* limits here, currently.
*/
case DOIO_SOFT:
goto poke;
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.
*/
do {
goto poke;
case DOIO_SUCCESS:
case DOIO_HARD:
break;
}
}
poke:
}
static void
/*
* Find out what socket this is and lock it.
*/
sock->pending_send = 0;
if (sock->references == 0) {
return;
}
/*
* Try to do as much I/O as possible on this socket. There are no
* limits here, currently.
*/
case DOIO_SOFT:
goto poke;
case DOIO_HARD:
case DOIO_SUCCESS:
break;
}
}
poke:
}
static void
{
int i;
/*
* and unlocking twice if both reads and writes are possible.
*/
for (i = 0 ; i < maxfd ; i++) {
#ifdef ISC_PLATFORM_USETHREADS
continue;
#endif /* ISC_PLATFORM_USETHREADS */
close(i);
continue;
}
goto check_write;
}
else
}
}
continue;
}
if (!unlock_sock) {
}
if (sock->connecting)
else
}
}
if (unlock_sock)
}
}
#ifdef ISC_PLATFORM_USETHREADS
/*
* This is the thread that will loop forever, always in a select or poll
* call.
*
* When select returns something to do, track down what thread gets to do
* this I/O and post the event to it.
*/
static isc_threadresult_t
int ctlfd;
int cc;
int maxfd;
/*
* Get the control fd here. This will never change.
*/
while (!done) {
do {
if (cc < 0) {
if (!SOFT_ERROR(errno))
"select() %s: %s",
"failed"),
}
} while (cc < 0);
/*
* Process reads on internal, control fd.
*/
for (;;) {
"watcher got message %d"),
msg);
/*
* Nothing to read?
*/
if (msg == SELECT_POKE_NOTHING)
break;
/*
* Handle shutdown message. We really should
* jump out of this loop right away, but
* it doesn't matter if we have to do a little
* more work first.
*/
if (msg == SELECT_POKE_SHUTDOWN) {
break;
}
/*
* This is a wakeup on a socket. Look
* at the event queue for both read and write,
* and decide if we need to watch on it now
* or not.
*/
}
}
}
ISC_MSG_EXITING, "watcher exiting"));
return ((isc_threadresult_t)0);
}
#endif /* ISC_PLATFORM_USETHREADS */
/*
* Create a new socket manager.
*/
#ifndef ISC_PLATFORM_USETHREADS
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_NOMEMORY);
"isc_mutex_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
#ifdef ISC_PLATFORM_USETHREADS
"isc_condition_init() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
/*
* Create the special fds that will be used to wake up the
*/
"pipe() %s: %s",
ISC_MSG_FAILED, "failed"),
return (ISC_R_UNEXPECTED);
}
#if 0
#endif
#else /* ISC_PLATFORM_USETHREADS */
#endif /* ISC_PLATFORM_USETHREADS */
/*
* Set up initial state for the select loop
*/
#ifdef ISC_PLATFORM_USETHREADS
#else /* ISC_PLATFORM_USETHREADS */
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
/*
*/
"isc_thread_create() %s",
ISC_MSG_FAILED, "failed"));
return (ISC_R_UNEXPECTED);
}
#endif /* ISC_PLATFORM_USETHREADS */
#ifndef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
return (ISC_R_SUCCESS);
}
void
int i;
/*
* Destroy a socket manager.
*/
#ifndef ISC_PLATFORM_USETHREADS
return;
}
#endif /* ISC_PLATFORM_USETHREADS */
#ifdef ISC_PLATFORM_USETHREADS
/*
* Wait for all sockets to be destroyed.
*/
"sockets exist"));
}
#else /* ISC_PLATFORM_USETHREADS */
/*
* Hope all sockets have been destroyed.
*/
"sockets exist"));
INSIST(0);
}
#endif /* ISC_PLATFORM_USETHREADS */
/*
* half of the pipe, which will send EOF to the read half.
* This is currently a no-op in the non-threaded case.
*/
#ifdef ISC_PLATFORM_USETHREADS
/*
* Wait for thread to exit.
*/
"isc_thread_join() %s",
ISC_MSG_FAILED, "failed"));
#endif /* ISC_PLATFORM_USETHREADS */
/*
* Clean up.
*/
#ifdef ISC_PLATFORM_USETHREADS
#endif /* ISC_PLATFORM_USETHREADS */
for (i = 0 ; i < FD_SETSIZE ; i++)
close(i);
}
static isc_result_t
unsigned int flags)
{
int io_state;
} else {
else
}
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
*/
if (!have_lock) {
}
/*
* Enqueue the request. If the socket was previously not being
* watched, poke the watcher to start paying attention to it.
*/
"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;
}
if (have_lock)
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;
"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.
*/
}
else {
else
}
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 ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
if (!have_lock) {
}
/*
* Enqueue the request. If the socket was previously
* not being watched, poke the watcher to start
* paying attention to it.
*/
"socket_send: event %p -> task %p",
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
break;
}
case DOIO_HARD:
case DOIO_SUCCESS:
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
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 on = 1;
return (ISC_R_FAMILYMISMATCH);
}
sizeof on) < 0) {
ISC_MSG_FAILED, "failed"));
/* Press on... */
}
switch (errno) {
case EACCES:
return (ISC_R_NOPERM);
case EADDRNOTAVAIL:
return (ISC_R_ADDRNOTAVAIL);
case EADDRINUSE:
return (ISC_R_ADDRINUSE);
case EINVAL:
return (ISC_R_BOUND);
default:
return (ISC_R_UNEXPECTED);
}
}
return (ISC_R_SUCCESS);
}
/*
* 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.
*/
if (backlog == 0)
return (ISC_R_UNEXPECTED);
}
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++;
/*
* Poke watcher here. We still have the socket locked, so there
* is no race condition. We will keep the lock for such a short
* bit of time waking it up now or later won't matter all that much.
*/
if (do_poke)
return (ISC_R_SUCCESS);
}
{
int cc;
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) {
goto queue;
switch (errno) {
#ifdef EHOSTDOWN
#endif
}
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.
*/
/*
* Poke watcher here. We still have the socket locked, so there
* is no race condition. We will keep the lock for such a short
* bit of time waking it up now or later won't matter all that much.
*/
return (ISC_R_SUCCESS);
}
/*
* Called when a socket with a pending connect() finishes.
*/
static void
int cc;
/*
* 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;
/*
* Get any possible error status here.
*/
else
if (errno != 0) {
/*
* If the error is EAGAIN, just re-select on this
* fd and pretend nothing strange happened.
*/
return;
}
/*
* Translate other errors into ISC_R_* flavors.
*/
switch (errno) {
#ifdef EHOSTDOWN
#endif
default:
"internal_connect: connect() %s",
}
} else {
}
}
ret = ISC_R_SUCCESS;
} else {
}
return (ret);
}
goto out;
}
ret = ISC_R_SUCCESS;
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);
}
}
}
/*
* Connecting is not a list.
*/
sock->connecting = 0;
(isc_event_t **)&dev);
}
}
}
}
return (val);
}
#ifndef ISC_PLATFORM_USETHREADS
void
*maxfd = 0;
else {
}
}
return (ISC_R_NOTFOUND);
return (ISC_R_SUCCESS);
}
#endif /* ISC_PLATFORM_USETHREADS */