socket.c revision 15a44745412679c30a6d022733925af70a38b715
/*
* Copyright (C) 1998-2000 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.152 2000/07/27 09:52:52 tale Exp $ */
#include <config.h>
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <isc/bufferlist.h>
#include <isc/condition.h>
/*
* 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
struct isc_socket {
/* Not locked. */
unsigned int magic;
/* Locked by socket lock. */
unsigned int references;
int fd;
/*
* 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
#ifdef USE_CMSG
unsigned char *cmsg;
unsigned int cmsglen;
#endif
};
#define VALID_MANAGER(m) ((m) != NULL && \
(m)->magic == SOCKET_MANAGER_MAGIC)
struct isc_socketmgr {
/* Not locked. */
unsigned int magic;
/* Locked by manager lock. */
int fdstate[FD_SETSIZE];
int maxfd;
int pipe_fds[2];
};
#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 SOCK_DEAD(s) ((s)->references == 0)
static void
const char *fmt, ...)
{
char msgbuf[2048];
return;
}
static void
const char *fmt, ...)
{
char msgbuf[2048];
char peerbuf[256];
return;
} else {
}
}
/*
* Poke the select loop when there is something for us to do.
* We assume that if a write completes here, it will be inserted into the
* queue fully. That is, we will not get partial writes.
*/
static void
int cc;
do {
if (cc < 0)
"write() failed during watcher poke: %s",
}
/*
* read a message on the internal fd.
*/
static int
int msg;
int cc;
if (cc < 0) {
if (SOFT_ERROR(errno))
return (SELECT_POKE_NOTHING);
"read() failed during watcher poke: %s",
return (SELECT_POKE_NOTHING);
}
return (msg);
}
/*
* 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
#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. Return
* 0 on success, non-zero on failure. This is the SEND constructor, which
* will used 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);
#ifdef ISC_NET_BSD44MSGHDR
msg->msg_controllen = 0;
#if defined(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. Return
* 0 on success, non-zero on failure. 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;
#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;
#if defined(ISC_SOCKET_DEBUG)
#endif
if (cc < 0) {
if (SOFT_ERROR(errno))
return (DOIO_SOFT);
"doio_recv: recvmsg(%d) %d bytes, err %d/%s",
return (DOIO_HARD); \
} \
return (DOIO_SOFT); \
}
return (DOIO_HARD); \
}
return (DOIO_SUCCESS);
}
/*
* On TCP, zero length reads indicate EOF, while on
* UDP, zero length reads are perfectly valid, although
* strange.
*/
return (DOIO_EOF);
}
/*
* 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. The senddone event
* was sent.
*
* DOIO_HARD A hard or unexpected I/O error was encountered.
* The senddone event was sent.
*
* 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;
/*
* check for error or block condition
*/
if (cc < 0) {
if (SOFT_ERROR(errno))
return (DOIO_SOFT);
return (DOIO_HARD); \
} \
return (DOIO_SOFT); \
}
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.
*/
"internal_send: %s",
return (DOIO_HARD);
}
if (cc == 0)
"internal_send: send() returned 0");
/*
* 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
/*
* No one has this socket open, so the watcher doesn't have to be
* poked, and the socket doesn't have to be locked.
*/
/*
* XXX should reset manager->maxfd here
*/
}
static isc_result_t
{
return (ISC_R_NOMEMORY);
#if USE_CMSG /* Let's hope the OSs are sane, and pad correctly XXXMLG */
#ifdef ISC_PLATFORM_HAVEIPV6
#endif
#ifdef SO_TIMESTAMP
#endif
goto err1;
}
#endif
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() failed");
goto err2;
}
/*
* Initialize readable and writable events
*/
return (ISC_R_SUCCESS);
err2: /* cmsg allocated */
#ifdef USE_CMSG
err1: /* socket allocated */
#endif
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
#ifdef USE_CMSG
#endif
}
/*
* Create a new 'type' socket managed by 'manager'. The sockets
* parameters are specified by 'expires' and 'interval'. 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;
}
free_socket(&sock);
switch (errno) {
case EMFILE:
case ENFILE:
case ENOBUFS:
return (ISC_R_NORESOURCES);
default:
"socket() failed: %s",
return (ISC_R_UNEXPECTED);
}
}
free_socket(&sock);
return (ISC_R_UNEXPECTED);
}
#ifdef SO_BSDCOMPAT
"setsockopt(%d, SO_BSDCOMPAT) failed",
/* Press on... */
}
#endif
#if defined(USE_CMSG)
if (type == isc_sockettype_udp) {
#if defined(SO_TIMESTAMP)
"setsockopt(%d, SO_TIMESTAMP) failed",
/* Press on... */
}
#endif /* SO_TIMESTAMP */
#if defined(ISC_PLATFORM_HAVEIPV6)
"setsockopt(%d, IPV6_PKTINFO) failed: %s",
}
#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)
}
/*
* 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.
*/
static void
{
else
}
/*
* See comments for send_recvdone_event() above.
*
* Caller must have the socket locked.
*/
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;
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.
*/
(void *)&addrlen);
if (fd < 0) {
if (SOFT_ERROR(errno)) {
return;
}
/*
* If some other error, ignore it as well and hope
* for the best, but log it.
*/
"accept() returned %d/%s", errno,
fd = -1;
"internal_accept: accept() failed: %s",
}
/*
* Pull off the done event.
*/
/*
* Poke watcher if there are more pending accepts.
*/
fd = -1;
"internal_accept: make_nonblock() failed: %s",
}
/*
* -1 means the new socket didn't happen.
*/
if (fd != -1) {
/*
* Save away the remote address
*/
"accepted connection, new socket %p",
}
/*
* Fill in the done event details and send it off.
*/
}
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. If some sort of quantum read count is
* desired before giving up control, make certain to process markers
* regardless of quantum.
*/
/*
* If this is a marker event, post its completion and
* continue the loop.
*/
goto next;
}
goto next;
}
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. This will
* set the EOF flag in markers as well, but
* that's really ok.
*/
do {
goto poke;
case DOIO_SUCCESS:
case DOIO_HARD:
break;
}
next:
}
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. If some sort of quantum write count is
* desired before giving up control, make certain to process markers
* regardless of quantum.
*/
/*
* If this is a marker event, post its completion and
* continue the loop.
*/
goto next;
}
goto next;
}
case DOIO_SOFT:
goto poke;
case DOIO_HARD:
case DOIO_SUCCESS:
break;
}
next:
}
poke:
}
/*
* 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 msg;
int i;
int maxfd;
/*
* Get the control fd here. This will never change.
*/
while (!done) {
do {
#ifdef ISC_SOCKET_DEBUG
for (i = 0 ; i < FD_SETSIZE ; i++) {
int printit;
printit = 0;
printit = 1;
}
printit = 1;
}
}
#endif
if (cc < 0) {
if (!SOFT_ERROR(errno))
"select failed: %s",
}
} 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.
*/
if (msg >= 0) {
continue;
}
continue;
/*
* 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.
*/
ev2 = (isc_event_t *)
|| sock->pending_recv
|| sock->pending_accept) {
} else {
}
|| sock->pending_send)
&& !sock->connecting) {
} else {
}
}
}
}
/*
* and unlocking twice if both reads and writes are possible.
*/
for (i = 0 ; i < maxfd ; i++) {
continue;
close(i);
continue;
}
goto check_write;
}
else
}
}
continue;
}
if (!unlock_sock) {
}
if (sock->connecting)
else
}
}
if (unlock_sock)
}
}
return ((isc_threadresult_t)0);
}
/*
* Create a new socket manager.
*/
return (ISC_R_NOMEMORY);
"isc_mutex_init() failed");
return (ISC_R_UNEXPECTED);
}
"isc_condition_init() failed");
return (ISC_R_UNEXPECTED);
}
/*
* Create the special fds that will be used to wake up the
*/
"pipe() failed: %s",
return (ISC_R_UNEXPECTED);
}
#if 0
#endif
/*
* Set up initial state for the select loop
*/
/*
*/
"isc_thread_create() failed");
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
void
int i;
/*
* Destroy a socket manager.
*/
/*
* Wait for all sockets to be destroyed.
*/
}
/*
* half of the pipe, which will send EOF to the read half.
*/
/*
* Wait for thread to exit.
*/
"isc_thread_join() failed");
/*
* Clean up.
*/
for (i = 0 ; i < FD_SETSIZE ; i++)
close(i);
}
{
unsigned int iocount;
return (ISC_R_NOMEMORY);
}
/***
*** From here down, only ISC_R_SUCCESS can be returned. Any further
*** error information will result in the done event being posted
*** to the task rather than this function failing.
***/
/*
* UDP sockets are always partial read
*/
else {
if (minimum == 0)
else
}
/*
* Move each buffer from the passed in list to our internal one.
*/
}
/*
* If the read queue is empty, try to do the I/O right now.
*/
if (!was_empty)
goto queue;
return (ISC_R_SUCCESS);
}
case DOIO_SOFT:
goto queue;
case DOIO_EOF:
return (ISC_R_SUCCESS);
case DOIO_HARD:
case DOIO_SUCCESS:
return (ISC_R_SUCCESS);
}
/*
* We couldn't read all or part of the request right now, so queue
* it.
*
* Attach to socket and to task
*/
/*
* Enqueue the request. If the socket was previously not being
* watched, poke the watcher to start paying attention to it.
*/
if (was_empty)
return (ISC_R_SUCCESS);
}
{
return (ISC_R_NOMEMORY);
}
/*
* UDP sockets are always partial read
*/
else {
if (minimum == 0)
else
}
dev->n = 0;
/*
* If the read queue is empty, try to do the I/O right now.
*/
if (!was_empty)
goto queue;
return (ISC_R_SUCCESS);
}
case DOIO_SOFT:
goto queue;
case DOIO_EOF:
return (ISC_R_SUCCESS);
case DOIO_HARD:
case DOIO_SUCCESS:
return (ISC_R_SUCCESS);
}
/*
* We couldn't read all or part of the request right now, so queue
* it.
*
* Attach to socket and to task
*/
/*
* Enqueue the request. If the socket was previously not being
* watched, poke the watcher to start paying attention to it.
*/
if (was_empty)
return (ISC_R_SUCCESS);
}
{
/*
* REQUIRE() checking performed in isc_socket_sendto()
*/
NULL));
}
{
return (ISC_R_NOMEMORY);
}
"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.
*/
}
/*
* If the write queue is empty, try to do the I/O right now.
*/
if (!was_empty)
goto queue;
return (ISC_R_SUCCESS);
}
case DOIO_SOFT:
goto queue;
case DOIO_HARD:
case DOIO_SUCCESS:
return (ISC_R_SUCCESS);
}
/*
* We couldn't send all or part of the request right now, so queue
* it.
*/
/*
* Enqueue the request. If the socket was previously not being
* watched, poke the watcher to start paying attention to it.
*/
if (was_empty)
return (ISC_R_SUCCESS);
}
{
NULL));
}
{
unsigned int iocount;
return (ISC_R_NOMEMORY);
}
/***
*** From here down, only ISC_R_SUCCESS can be returned. Any further
*** error information will result in the done event being posted
*** to the task rather than this function failing.
***/
}
/*
* Move each buffer from the passed in list to our internal one.
*/
}
/*
* If the read queue is empty, try to do the I/O right now.
*/
if (!was_empty)
goto queue;
return (ISC_R_SUCCESS);
}
case DOIO_SOFT:
goto queue;
case DOIO_HARD:
case DOIO_SUCCESS:
return (ISC_R_SUCCESS);
}
/*
* We couldn't send all or part of the request right now, so queue
* it.
*/
/*
* Enqueue the request. If the socket was previously not being
* watched, poke the watcher to start paying attention to it.
*/
if (was_empty)
return (ISC_R_SUCCESS);
}
int on = 1;
sizeof on) < 0) {
/* 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.
*/
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) {
case ECONNREFUSED:
goto err_exit;
case ENETUNREACH:
goto err_exit;
}
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 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) {
case ETIMEDOUT:
break;
case ECONNREFUSED:
break;
case ENETUNREACH:
break;
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);
}
}
/*
* Need to guess if we need to poke or not... XXX
*/
}
{
return (ISC_R_NOMEMORY);
}
/*
* If the queue is empty, simply return the last error we got on
* this socket as the result code, and send off the done event.
*/
return (ISC_R_SUCCESS);
}
/*
* Bad luck. The queue wasn't empty. Insert this in the proper
* place.
*/
return (ISC_R_SUCCESS);
}
{
return (ISC_R_NOMEMORY);
}
/*
* If the queue is empty, simply return the last error we got on
* this socket as the result code, and send off the done event.
*/
return (ISC_R_SUCCESS);
}
/*
* Bad luck. The queue wasn't empty. Insert this in the proper
* place.
*/
return (ISC_R_SUCCESS);
}
}
return (val);
}