socket.c revision 850b5e80930907e4747347201dc41e4d04e036f8
/*
* Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1998-2003 Internet Software Consortium.
*
* 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$ */
/*! \file */
#include <config.h>
#if defined(HAVE_LINUX_NETLINK_H) && defined(HAVE_LINUX_RTNETLINK_H)
#include <linux/rtnetlink.h>
#endif
#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 <isc/strerror.h>
#ifdef ISC_PLATFORM_HAVESYSUNH
#endif
#ifdef ISC_PLATFORM_HAVEKQUEUE
#endif
#ifdef ISC_PLATFORM_HAVEEPOLL
#endif
#ifdef ISC_PLATFORM_HAVEDEVPOLL
#if defined(HAVE_SYS_DEVPOLL_H)
#elif defined(HAVE_DEVPOLL_H)
#include <devpoll.h>
#endif
#endif
#include "errno2result.h"
/* See task.c about the following definition: */
#ifdef ISC_PLATFORM_USETHREADS
#define USE_WATCHER_THREAD
#else
#define USE_SHARED_MANAGER
#endif /* ISC_PLATFORM_USETHREADS */
#ifndef USE_WATCHER_THREAD
#include "socket_p.h"
#include "../task_p.h"
#endif /* USE_WATCHER_THREAD */
#if defined(SO_BSDCOMPAT) && defined(__linux__)
#endif
/*%
* Choose the most preferable multiplex method.
*/
#ifdef ISC_PLATFORM_HAVEKQUEUE
#define USE_KQUEUE
#elif defined (ISC_PLATFORM_HAVEEPOLL)
#define USE_EPOLL
#elif defined (ISC_PLATFORM_HAVEDEVPOLL)
#define USE_DEVPOLL
typedef struct {
unsigned int want_read : 1,
want_write : 1;
} pollinfo_t;
#else
#define USE_SELECT
#endif /* ISC_PLATFORM_HAVEKQUEUE */
#ifndef USE_WATCHER_THREAD
struct isc_socketwait {
int nevents;
};
#elif defined (USE_SELECT)
struct isc_socketwait {
int nfds;
int maxfd;
};
#endif /* USE_KQUEUE */
#endif /* !USE_WATCHER_THREAD */
/*
* Set by the -T dscp option on the command line. If set to a value
* other than -1, we check to make sure DSCP values match it, and
* assert if not.
*/
int isc_dscp_check_value = -1;
/*%
* Maximum number of allowable open sockets. This is also the maximum
* allowable socket file descriptor.
*
* Care should be taken before modifying this value for select():
* The API standard doesn't ensure select() accept more than (the system default
* of) FD_SETSIZE descriptors, and the default size should in fact be fine in
* the vast majority of cases. This constant should therefore be increased only
* when absolutely necessary and possible, i.e., the server is exhausting all
* available file descriptors (up to FD_SETSIZE) and the select() function
* and FD_xxx macros support larger values than FD_SETSIZE (which may not
* always by true, but we keep using some of them to ensure as much
* portability as possible). Note also that overall server performance
* may be rather worsened with a larger value of this constant due to
* inherent scalability problems of select().
*
* As a special note, this value shouldn't have to be touched if
* this is a build for an authoritative only DNS server.
*/
#ifndef ISC_SOCKET_MAXSOCKETS
#define ISC_SOCKET_MAXSOCKETS 4096
#elif defined(USE_SELECT)
#define ISC_SOCKET_MAXSOCKETS FD_SETSIZE
#endif /* USE_KQUEUE... */
#endif /* ISC_SOCKET_MAXSOCKETS */
#ifdef USE_SELECT
/*%
* Mac OS X needs a special definition to support larger values in select().
* We always define this because a larger value can be specified run-time.
*/
#ifdef __APPLE__
#define _DARWIN_UNLIMITED_SELECT
#endif /* __APPLE__ */
#endif /* USE_SELECT */
#ifdef ISC_SOCKET_USE_POLLWATCH
/*%
* bug: DP_POLL ioctl could keep sleeping even if socket I/O is possible for
* some of the specified FD. The idea is based on the observation that it's
* likely for a busy server to keep receiving packets. It specifically works
* as follows: the socket watcher is first initialized with the state of
* "poll_idle". While it's in the idle state it keeps sleeping until a socket
* event occurs. When it wakes up for a socket I/O event, it moves to the
* poll_active state, and sets the poll timeout to a short period
* (ISC_SOCKET_POLLWATCH_TIMEOUT msec). If timeout occurs in this state, the
* watcher goes to the poll_checking state with the same timeout period.
* In this state, the watcher tries to detect whether this is a break
* during intermittent events or the kernel bug is triggered. If the next
* polling reports an event within the short period, the previous timeout is
* likely to be a kernel bug, and so the watcher goes back to the active state.
* Otherwise, it moves to the idle state again.
*
* It's not clear whether this is a thread-related bug, but since we've only
* seen this with threads, this workaround is used only when enabling threads.
*/
#ifndef ISC_SOCKET_POLLWATCH_TIMEOUT
#define ISC_SOCKET_POLLWATCH_TIMEOUT 10
#endif /* ISC_SOCKET_POLLWATCH_TIMEOUT */
#endif /* ISC_SOCKET_USE_POLLWATCH */
/*%
* Size of per-FD lock buckets.
*/
#ifdef ISC_PLATFORM_USETHREADS
#define FDLOCK_COUNT 1024
#else
#define FDLOCK_COUNT 1
#endif /* ISC_PLATFORM_USETHREADS */
/*%
* Maximum number of events communicated with the kernel. There should normally
* be no need for having a large number.
*/
#ifndef ISC_SOCKET_MAXEVENTS
#define ISC_SOCKET_MAXEVENTS 64
#endif
#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) == 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_HAVEIN6PKTINFO
#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
/*%
* The size to raise the receive buffer to (from BIND 8).
*/
/*%
* The number of times a send operation is repeated if the result is EINTR.
*/
#define NRETRIES 10
typedef struct isc__socket isc__socket_t;
typedef struct isc__socketmgr isc__socketmgr_t;
struct isc__socket {
/* Not locked. */
const isc_statscounter_t *statsindex;
/* Locked by socket lock. */
unsigned int references;
int fd;
int pf;
char name[16];
void * tag;
/*
* 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,
dupped : 1,
#ifdef ISC_NET_RECVOVERFLOW
unsigned char overflow; /* used for MSG_TRUNC fake */
#endif
char *recvcmsgbuf;
char *sendcmsgbuf;
void *fdwatcharg;
int fdwatchflags;
int dscp;
};
struct isc__socketmgr {
/* Not locked. */
#ifdef USE_KQUEUE
int kqueue_fd;
int nevents;
#endif /* USE_KQUEUE */
#ifdef USE_EPOLL
int epoll_fd;
int nevents;
struct epoll_event *events;
#endif /* USE_EPOLL */
#ifdef USE_DEVPOLL
int devpoll_fd;
int nevents;
#endif /* USE_DEVPOLL */
#ifdef USE_SELECT
int fd_bufsize;
#endif /* USE_SELECT */
unsigned int maxsocks;
#ifdef ISC_PLATFORM_USETHREADS
int pipe_fds[2];
#endif
/* Locked by fdlock. */
isc__socket_t **fds;
int *fdstate;
#ifdef USE_DEVPOLL
#endif
/* Locked by manager lock. */
#ifdef USE_SELECT
int maxfd;
#endif /* USE_SELECT */
int reserved; /* unlocked */
#ifdef USE_WATCHER_THREAD
#else /* USE_WATCHER_THREAD */
unsigned int refs;
#endif /* USE_WATCHER_THREAD */
int maxudp;
};
#ifdef USE_SHARED_MANAGER
#endif /* USE_SHARED_MANAGER */
#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 **);
#ifdef USE_WATCHER_THREAD
#endif
/*%
* The following are intended for internal use (indicated by "isc__"
* prefix) but are not declared as static, allowing direct access from
* unit tests etc.
*/
isc_socket_t **socketp);
void
void
unsigned int flags);
const void *arg);
void
unsigned int options);
const void *arg);
void
void
void
int
unsigned int maxsocks);
void
void
void
const char *
void *
#ifdef HAVE_LIBXML2
void
#endif
#ifdef HAVE_JSON
#endif
static struct {
/*%
* The following are defined just for avoiding unused static functions.
*/
} socketmethods = {
{
},
(void *)isc__socket_recvv, (void *)isc__socket_send,
(void *)isc__socket_sendv, (void *)isc__socket_sendto2,
(void *)isc__socket_cleanunix, (void *)isc__socket_permunix,
(void *)isc__socket_filter, (void *)isc__socket_listen,
(void *)isc__socket_accept, (void *)isc__socket_getpeername,
(void *)isc__socket_isbound
};
static isc_socketmgrmethods_t socketmgrmethods = {
};
#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)
/*%
* Shortcut index arrays to get access to statistics counters.
*/
enum {
STATID_OPEN = 0,
STATID_OPENFAIL = 1,
STATID_CLOSE = 2,
STATID_BINDFAIL = 3,
STATID_CONNECTFAIL = 4,
STATID_CONNECT = 5,
STATID_ACCEPTFAIL = 6,
STATID_ACCEPT = 7,
STATID_SENDFAIL = 8,
STATID_RECVFAIL = 9,
STATID_ACTIVE = 10
};
static const isc_statscounter_t udp4statsindex[] = {
-1,
-1,
};
static const isc_statscounter_t udp6statsindex[] = {
-1,
-1,
};
static const isc_statscounter_t tcp4statsindex[] = {
};
static const isc_statscounter_t tcp6statsindex[] = {
};
static const isc_statscounter_t unixstatsindex[] = {
};
static const isc_statscounter_t fdwatchstatsindex[] = {
-1,
-1,
-1,
-1,
-1
};
static const isc_statscounter_t rawstatsindex[] = {
-1,
-1,
-1,
-1,
-1,
-1,
};
defined(USE_WATCHER_THREAD)
static void
static void
const char *fmt, ...)
{
char msgbuf[2048];
return;
}
#endif
static void
static void
const char *fmt, ...)
{
char msgbuf[2048];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
return;
} else {
}
}
#if defined(_AIX) && defined(ISC_NET_BSD44MSGHDR) && \
defined(USE_CMSG) && defined(IPV6_RECVPKTINFO)
/*
* AIX has a kernel bug where IPV6_RECVPKTINFO gets cleared by
* setting IPV6_V6ONLY.
*/
static void
{
char strbuf[ISC_STRERRORSIZE];
int on = 1;
return;
"setsockopt(%d, IPV6_RECVPKTINFO) "
"failed"),
strbuf);
}
}
#else
#define FIX_IPV6_RECVPKTINFO(sock) (void)0
#endif
/*%
* Increment socket-related statistics counters.
*/
static inline void
}
/*%
* Decrement socket-related statistics counters.
*/
static inline void
}
static inline isc_result_t
#ifdef USE_KQUEUE
if (msg == SELECT_POKE_READ)
else
return (result);
struct epoll_event event;
if (msg == SELECT_POKE_READ)
else
}
return (result);
#elif defined(USE_DEVPOLL)
if (msg == SELECT_POKE_READ)
else
else {
if (msg == SELECT_POKE_READ)
else
}
return (result);
#elif defined(USE_SELECT)
if (msg == SELECT_POKE_READ)
if (msg == SELECT_POKE_WRITE)
return (result);
#endif
}
static inline isc_result_t
#ifdef USE_KQUEUE
if (msg == SELECT_POKE_READ)
else
return (result);
struct epoll_event event;
if (msg == SELECT_POKE_READ)
else
char strbuf[ISC_STRERRORSIZE];
}
return (result);
#elif defined(USE_DEVPOLL)
/*
* only provides a way of canceling per FD, we may need to re-poll the
* socket for the other operation.
*/
if (msg == SELECT_POKE_READ &&
}
if (msg == SELECT_POKE_WRITE &&
}
else {
if (msg == SELECT_POKE_READ)
else
}
return (result);
#elif defined(USE_SELECT)
if (msg == SELECT_POKE_READ)
else if (msg == SELECT_POKE_WRITE)
return (result);
#endif
}
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.
*/
if (msg == SELECT_POKE_CLOSE) {
/* No one should be updating fdstate, so no need to lock it */
return;
}
/*
* We accept (and ignore) any error from unwatch_fd() as we are
* closing the socket, hoping it doesn't leave dangling state in
* the kernel.
* Note that unwatch_fd() must be called after releasing the
* fdlock; otherwise it could cause deadlock due to a lock order
* reversal.
*/
return;
}
return;
}
/*
* Set requested bit.
*/
if (result != ISC_R_SUCCESS) {
/*
* XXXJT: what should we do? Ignoring the failure of watching
* a socket will make the application dysfunctional, but there
* seems to be no reasonable recovery process.
*/
"failed to start watching FD (%d): %s",
}
}
#ifdef USE_WATCHER_THREAD
/*
* 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];
char strbuf[ISC_STRERRORSIZE];
do {
#ifdef ENOSR
/*
* Treat ENOSR as EAGAIN but loop slowly as it is
* unlikely to clear fast.
*/
sleep(1);
}
#endif
if (cc < 0) {
"write() failed "
"during watcher poke: %s"),
strbuf);
}
}
/*
* Read a message on the internal fd.
*/
static void
int buf[2];
int cc;
char strbuf[ISC_STRERRORSIZE];
if (cc < 0) {
if (SOFT_ERROR(errno))
return;
"read() failed "
"during watcher poke: %s"),
strbuf);
return;
}
}
#else /* USE_WATCHER_THREAD */
/*
* Update the state of the socketmgr when something changes.
*/
static void
if (msg == SELECT_POKE_SHUTDOWN)
return;
else if (fd >= 0)
return;
}
#endif /* USE_WATCHER_THREAD */
/*
* Make a fd non-blocking.
*/
static isc_result_t
make_nonblock(int fd) {
int ret;
int flags;
char strbuf[ISC_STRERRORSIZE];
#ifdef USE_FIONBIO_IOCTL
int on = 1;
#else
flags |= PORT_NONBLOCK;
#endif
if (ret == -1) {
#ifdef USE_FIONBIO_IOCTL
"ioctl(%d, FIONBIO, &on): %s", fd,
#else
#endif
strbuf);
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
#ifdef USE_CMSG
/*
* Not all OSes support advanced CMSG macros: CMSG_LEN and CMSG_SPACE.
* In order to ensure as much portability as possible, we provide wrapper
* functions of these macros.
* Note that cmsg_space() could run slow on OSes that do not have
* CMSG_SPACE.
*/
static inline ISC_SOCKADDR_LEN_T
#ifdef CMSG_LEN
#else
/*
* Cast NULL so that any pointer arithmetic performed by CMSG_DATA
* is correct.
*/
#endif
}
static inline ISC_SOCKADDR_LEN_T
#ifdef CMSG_SPACE
return (CMSG_SPACE(len));
#else
/*
* XXX: The buffer length is an ad-hoc value, but should be enough
* in a practical sense.
*/
else
return (0);
#endif
}
#endif /* USE_CMSG */
/*
* Process control messages received on a socket.
*/
static void
#ifdef USE_CMSG
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
struct in6_pktinfo *pktinfop;
#endif
#ifdef SO_TIMESTAMP
void *timevalp;
#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.
*/
#ifdef ISC_NET_BSD44MSGHDR
#ifdef MSG_TRUNC
#endif
#ifdef MSG_CTRUNC
#endif
#ifndef USE_CMSG
return;
#else
return;
#ifdef SO_TIMESTAMP
#endif
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
#endif
"processing cmsg %p", cmsgp);
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
sizeof(struct in6_pktinfo));
"interface received on ifindex %u",
goto next;
}
#endif
#ifdef SO_TIMESTAMP
goto next;
}
#endif
#ifdef IPV6_TCLASS
goto next;
}
#endif
#ifdef IP_TOS
#ifdef IP_RECVTOS
#endif
)) {
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;
#ifdef ISC_NET_BSD44MSGHDR
#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 == 0U);
#ifdef ISC_NET_BSD44MSGHDR
msg->msg_controllen = 0;
#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
#if defined(IPV6_USE_MIN_MTU)
#endif
struct in6_pktinfo *pktinfop;
"sendto pktinfo data, ifindex %u",
#if defined(IPV6_USE_MIN_MTU)
/*
* Set IPV6_USE_MIN_MTU as a per packet option as FreeBSD
* ignores setsockopt(IPV6_USE_MIN_MTU) when IPV6_PKTINFO
* is used.
*/
#endif
}
if (isc_dscp_check_value != -1) {
}
{
#ifdef IP_TOS
((isc_net_probedscp() & ISC_NET_DSCPPKTV4) != 0))
{
(void *)&dscp, sizeof(int)) < 0)
{
char strbuf[ISC_STRERRORSIZE];
"setsockopt(%d, IP_TOS, %.02x)"
" %s: %s",
"failed"),
strbuf);
}
}
#endif
#if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
((isc_net_probedscp() & ISC_NET_DSCPPKTV6) != 0))
{
(void *)&dscp, sizeof(int)) < 0) {
char strbuf[ISC_STRERRORSIZE];
"setsockopt(%d, IPV6_TCLASS, "
"%.02x) %s: %s",
"failed"),
strbuf);
}
}
#endif
}
#endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
#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 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;
#ifdef BROKEN_RECVMSG
#ifdef ISC_PLATFORM_HAVESYSUNH
#endif
} else {
}
#else
#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 void
}
static isc_socketevent_t *
const void *arg)
{
sizeof(*ev));
return (NULL);
ev->n = 0;
ev->attributes = 0;
return (ev);
}
#if defined(ISC_SOCKET_DEBUG)
static void
unsigned int i;
(long) msg->msg_namelen);
(long) msg->msg_iovlen);
for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
printf("\t\t%d\tbase %p, len %ld\n", i,
#ifdef ISC_NET_BSD44MSGHDR
(long) msg->msg_controllen);
#endif
}
#endif
#define DOIO_SUCCESS 0 /* i/o ok, event sent */
static int
int cc;
int recv_errno;
char strbuf[ISC_STRERRORSIZE];
#if defined(ISC_SOCKET_DEBUG)
#endif
recv_errno = errno;
#if defined(ISC_SOCKET_DEBUG)
#endif
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); \
}
/* HPUX 11.11 can return EADDRNOTAVAIL. */
/* Should never get this one but it was seen. */
#ifdef ENOPROTOOPT
#endif
/*
* errors.
*/
#ifdef EPROTO
#endif
return (DOIO_HARD);
}
/*
* On TCP and UNIX sockets, zero length reads indicate EOF,
* while on UDP sockets, zero length reads are perfectly valid,
* although strange.
*/
case isc_sockettype_tcp:
case isc_sockettype_unix:
if (cc == 0)
return (DOIO_EOF);
break;
case isc_sockettype_udp:
case isc_sockettype_raw:
break;
case isc_sockettype_fdwatch:
default:
INSIST(0);
}
"dropping source port zero packet");
}
return (DOIO_SOFT);
}
/*
* Simulate a firewall blocking UDP responses bigger than
* 'maxudp' bytes.
*/
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
/*
* 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 == 0U);
}
}
/*
* 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];
int attempts = 0;
int send_errno;
char strbuf[ISC_STRERRORSIZE];
cc = write_count;
else
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.
*/
return (DOIO_HARD);
}
if (cc == 0) {
"doio_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
/*
* No one has this socket open, so the watcher doesn't have to be
* poked, and the socket doesn't have to be locked.
*/
if (type == isc_sockettype_fdwatch)
else
if (type == isc_sockettype_fdwatch) {
/*
* The caller may close the socket once this function returns,
* and `fd' may be reassigned for a new socket. So we do
* unwatch_fd() here, rather than defer it via select_poke().
* Note: this may complicate data protection among threads and
* may reduce performance due to additional locks. One way to
* solve this would be to dup() the watched descriptor, but we
* take a simpler approach at this moment.
*/
} else
}
/*
* update manager->maxfd here (XXX: this should be implemented more
* efficiently)
*/
#ifdef USE_SELECT
int i;
for (i = fd - 1; i >= 0; i--) {
break;
}
}
#ifdef ISC_PLATFORM_USETHREADS
#endif
}
#endif /* USE_SELECT */
}
static void
int fd;
ISC_MSG_DESTROYING, "destroying");
}
#ifdef USE_WATCHER_THREAD
#endif /* USE_WATCHER_THREAD */
/* can't unlock manager as its memory context is still used */
}
static isc_result_t
{
return (ISC_R_NOMEMORY);
sock->references = 0;
/*
* Set up cmsg buffers.
*/
cmsgbuflen = 0;
#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
#endif
#if defined(USE_CMSG) && defined(SO_TIMESTAMP)
#endif
cmsgbuflen += cmsg_space(sizeof(int));
#endif
if (sock->recvcmsgbuflen != 0U) {
goto error;
}
}
cmsgbuflen = 0;
#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
#if defined(IPV6_USE_MIN_MTU)
/*
* Provide space for working around FreeBSD's broken IPV6_USE_MIN_MTU
* support.
*/
cmsgbuflen += cmsg_space(sizeof(int));
#endif
#endif
cmsgbuflen += cmsg_space(sizeof(int));
#endif
if (sock->sendcmsgbuflen != 0U) {
goto error;
}
}
/*
* 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.
*/
if (result != ISC_R_SUCCESS) {
goto error;
}
/*
* Initialize readable and writable events.
*/
return (ISC_R_SUCCESS);
return (result);
}
/*
* 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 SO_BSDCOMPAT
/*
* This really should not be necessary to do. Having to workout
* which kernel version we are on at run time so that we don't cause
* the kernel to issue a warning about us using a deprecated socket option.
* Such warnings should *never* be on by default in production kernels.
*
* We can't do this a build time because executables are moved between
* machines and hence kernels.
*
* We can't just not set SO_BSDCOMAT because some kernels require it.
*/
static void
clear_bsdcompat(void) {
#ifdef __linux__
char *endp;
long int major;
long int minor;
/* Paranoia in parsing can be increased, but we trust uname(). */
if (*endp == '.') {
}
}
#endif /* __linux __ */
}
#endif
static void
#if !defined(IPV6_USE_MIN_MTU) && !defined(IPV6_MTU)
#endif
#ifdef IPV6_USE_MIN_MTU
/* use minimum MTU */
int on = 1;
}
#endif
#if defined(IPV6_MTU)
/*
* Use minimum MTU on IPv6 sockets.
*/
int mtu = 1280;
}
#endif
}
static isc_result_t
{
char strbuf[ISC_STRERRORSIZE];
const char *err = "socket";
int tries = 0;
int on = 1;
#endif
#if defined(SO_RCVBUF)
int size;
#endif
if (dup_socket == NULL) {
case isc_sockettype_udp:
break;
case isc_sockettype_tcp:
break;
case isc_sockettype_unix:
break;
case isc_sockettype_raw:
/*
* PF_ROUTE is a alias for PF_NETLINK on linux.
*/
#if defined(PF_ROUTE)
#ifdef NETLINK_ROUTE
#else
#endif
#ifdef NETLINK_ROUTE
struct sockaddr_nl sa;
int n;
/*
* Do an implicit bind.
*/
sizeof(sa));
if (n < 0) {
}
#endif
}
}
#endif
break;
case isc_sockettype_fdwatch:
/*
* We should not be called for isc_sockettype_fdwatch
* sockets.
*/
INSIST(0);
break;
}
} else {
}
goto again;
#ifdef F_DUPFD
/*
* Leave a space for stdio and TCP to work in.
*/
err = "isc_socket_create: fcntl";
}
#endif
"socket: file descriptor exceeds limit (%d/%u)",
return (ISC_R_NORESOURCES);
}
switch (errno) {
case EMFILE:
case ENFILE:
/* fallthrough */
case ENOBUFS:
return (ISC_R_NORESOURCES);
case EPROTONOSUPPORT:
case EPFNOSUPPORT:
case EAFNOSUPPORT:
/*
* Linux 2.2 (and maybe others) return EINVAL instead of
* EAFNOSUPPORT.
*/
case EINVAL:
return (ISC_R_FAMILYNOSUPPORT);
default:
"%s() %s: %s", err,
"failed"),
strbuf);
return (ISC_R_UNEXPECTED);
}
}
if (dup_socket != NULL)
goto setup_done;
if (result != ISC_R_SUCCESS) {
return (result);
}
#ifdef SO_BSDCOMPAT
clear_bsdcompat) == ISC_R_SUCCESS);
"setsockopt(%d, SO_BSDCOMPAT) %s: %s",
ISC_MSG_FAILED, "failed"),
strbuf);
/* Press on... */
}
#endif
#ifdef SO_NOSIGPIPE
"setsockopt(%d, SO_NOSIGPIPE) %s: %s",
ISC_MSG_FAILED, "failed"),
strbuf);
/* Press on... */
}
#endif
/*
* Use minimum mtu if possible.
*/
#if defined(USE_CMSG)
#if defined(SO_TIMESTAMP)
&& errno != ENOPROTOOPT) {
"setsockopt(%d, SO_TIMESTAMP) %s: %s",
"failed"),
strbuf);
/* Press on... */
}
#endif /* SO_TIMESTAMP */
#if defined(ISC_PLATFORM_HAVEIPV6)
/*
* Warn explicitly because this anomaly can be hidden
* in usual operation (and unexpectedly appear later).
*/
"No buffer available to receive "
"IPv6 destination");
}
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
#ifdef IPV6_RECVPKTINFO
/* RFC 3542 */
"setsockopt(%d, IPV6_RECVPKTINFO) "
"failed"),
strbuf);
}
#else
/* RFC 2292 */
"setsockopt(%d, IPV6_PKTINFO) %s: %s",
"failed"),
strbuf);
}
#endif /* IPV6_RECVPKTINFO */
#endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
#if defined(IPV6_MTU_DISCOVER) && defined(IPV6_PMTUDISC_DONT)
/*
*/
int action = IPV6_PMTUDISC_DONT;
sizeof(action));
}
#endif
#endif /* ISC_PLATFORM_HAVEIPV6 */
#endif /* defined(USE_CMSG) */
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
/*
*/
int action = IP_PMTUDISC_DONT;
}
#endif
#if defined(IP_DONTFRAG)
/*
*/
int off = 0;
}
#endif
#if defined(SO_RCVBUF)
size < RCVBUFSIZE) {
size = RCVBUFSIZE;
"setsockopt(%d, SO_RCVBUF, %d) %s: %s",
"failed"),
strbuf);
}
}
#endif
}
#ifdef IPV6_RECVTCLASS
"setsockopt(%d, IPV6_RECVTCLASS) "
ISC_MSG_FAILED, "failed"),
strbuf);
}
#endif
#ifdef IP_RECVTOS
"setsockopt(%d, IP_RECVTOS) "
ISC_MSG_FAILED, "failed"),
strbuf);
}
#endif
#endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
}
return (ISC_R_SUCCESS);
}
/*
* Create a 'type' socket or duplicate an existing socket, managed
* by 'manager'. Events will be posted to 'task' and when dispatched
* 'action' will be called with 'arg' as the arg value. The new
* socket is returned in 'socketp'.
*/
static isc_result_t
{
int lockid;
if (result != ISC_R_SUCCESS)
return (result);
case isc_sockettype_udp:
sock->statsindex =
break;
case isc_sockettype_tcp:
sock->statsindex =
break;
case isc_sockettype_unix:
break;
case isc_sockettype_raw:
break;
default:
INSIST(0);
}
if (result != ISC_R_SUCCESS) {
free_socket(&sock);
return (result);
}
/*
* Note we don't have to lock the socket like we normally would because
* there are no external references to it yet.
*/
#ifdef USE_DEVPOLL
#endif
#ifdef USE_SELECT
#endif
return (ISC_R_SUCCESS);
}
/*%
* 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'.
*/
{
}
/*%
* Duplicate an existing socket. The new socket is returned
* in 'socketp'.
*/
sock0));
}
/*
* We don't need to retain the lock hereafter, since no one else has
* this socket.
*/
if (result != ISC_R_SUCCESS)
if (result == ISC_R_SUCCESS) {
#ifdef USE_DEVPOLL
#endif
#ifdef USE_SELECT
#endif
}
return (result);
}
/*
* 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'.
*/
{
int lockid;
if (result != ISC_R_SUCCESS)
return (result);
/*
* Note we don't have to lock the socket like we normally would because
* there are no external references to it yet.
*/
#ifdef USE_SELECT
#endif
if (flags & ISC_SOCKFDWATCH_READ)
if (flags & ISC_SOCKFDWATCH_WRITE)
ISC_MSG_CREATED, "fdwatch-created");
return (ISC_R_SUCCESS);
}
/*
* Indicate to the manager that it should watch the socket again.
* This can be used to restart watching if the previous event handler
* didn't indicate there was more data to be processed. Primarily
* it is for writing but could be used for reading if desired
*/
{
/*
* We check both flags first to allow us to get the lock
* once but only if we need it.
*/
if (((flags & ISC_SOCKFDWATCH_READ) != 0) &&
!sock->pending_recv)
if (((flags & ISC_SOCKFDWATCH_WRITE) != 0) &&
!sock->pending_send)
}
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)
}
int fd;
sock->connecting = 0;
return (ISC_R_SUCCESS);
}
/*
* 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;
"dispatch_recv: event %p -> task %p",
} else {
}
sock->references++;
else
}
static void
return;
"dispatch_send: event %p -> task %p",
} else {
}
sock->references++;
else
}
/*
* 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;
char strbuf[ISC_STRERRORSIZE];
const char *err = "accept";
"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(). ECONNABORTED has been
* reported for Solaris 8. The rest are thrown in not because
* we have seen them but because they are ignored by other
* daemons such as BIND 8 and Apache.
*/
(void *)&addrlen);
#ifdef F_DUPFD
/*
* Leave a space for stdio to work in.
*/
}
#endif
if (fd < 0) {
if (SOFT_ERROR(errno))
goto soft_error;
switch (errno) {
case ENFILE:
case EMFILE:
"%s: too many open file descriptors",
err);
goto soft_error;
case ENOBUFS:
case ENOMEM:
case ECONNRESET:
case ECONNABORTED:
case EHOSTUNREACH:
case EHOSTDOWN:
case ENETUNREACH:
case ENETDOWN:
case ECONNREFUSED:
#ifdef EPROTO
case EPROTO:
#endif
#ifdef ENONET
case ENONET:
#endif
goto soft_error;
default:
break;
}
"internal_accept: %s() %s: %s", err,
"failed"),
strbuf);
fd = -1;
} else {
if (addrlen == 0U) {
"internal_accept(): "
"accept() failed to return "
"remote address");
goto soft_error;
{
"internal_accept(): "
"accept() returned peer address "
"family %u (expected %u)",
goto soft_error;
"accept: "
"file descriptor exceeds limit (%d/%u)",
goto soft_error;
}
}
if (fd != -1) {
}
/*
* Pull off the done event.
*/
/*
* Poke watcher if there are more pending accepts.
*/
if (fd != -1) {
if (result != ISC_R_SUCCESS) {
fd = -1;
}
}
/*
* -1 means the new socket didn't happen.
*/
if (fd != -1) {
/*
* Use minimum mtu if possible.
*/
/*
* Ensure DSCP settings are inherited across accept.
*/
/*
* Save away the remote address
*/
#ifdef USE_SELECT
#endif
"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 more_data;
/*
* Find out what socket this is and lock it.
*/
sock->pending_send = 0;
if (sock->references == 0) {
return;
}
if (more_data)
}
static void
int more_data;
/*
* Find out what socket this is and lock it.
*/
sock->pending_recv = 0;
if (sock->references == 0) {
return;
}
if (more_data)
}
/*
* and unlocking twice if both reads and writes are possible.
*/
static void
{
/*
* If the socket is going to be closed, don't do more I/O.
*/
return;
}
if (readable) {
goto check_write;
}
else
}
}
if (writeable) {
goto unlock_fd;
}
if (!unlock_sock) {
}
if (sock->connecting)
else
}
}
if (unlock_sock)
if (unwatch_read)
if (unwatch_write)
}
#ifdef USE_KQUEUE
static isc_boolean_t
int i;
#ifdef USE_WATCHER_THREAD
#endif
/*
* This is not an error, but something unexpected. If this
* happens, it may indicate the need for increasing
* ISC_SOCKET_MAXEVENTS.
*/
"maximum number of FD events (%d) received",
nevents);
}
for (i = 0; i < nevents; i++) {
#ifdef USE_WATCHER_THREAD
continue;
}
#endif
}
#ifdef USE_WATCHER_THREAD
if (have_ctlevent)
#endif
return (done);
}
static isc_boolean_t
{
int i;
#ifdef USE_WATCHER_THREAD
#endif
"maximum number of FD events (%d) received",
nevents);
}
for (i = 0; i < nevents; i++) {
#ifdef USE_WATCHER_THREAD
continue;
}
#endif
/*
* condition, so we need to try both anyway. This is a
* bit inefficient, but should be okay for such rare
* events. Note also that the read or write attempt
* won't block because we use non-blocking sockets.
*/
}
}
#ifdef USE_WATCHER_THREAD
if (have_ctlevent)
#endif
return (done);
}
#elif defined(USE_DEVPOLL)
static isc_boolean_t
int i;
#ifdef USE_WATCHER_THREAD
#endif
"maximum number of FD events (%d) received",
nevents);
}
for (i = 0; i < nevents; i++) {
#ifdef USE_WATCHER_THREAD
continue;
}
#endif
}
#ifdef USE_WATCHER_THREAD
if (have_ctlevent)
#endif
return (done);
}
#elif defined(USE_SELECT)
static void
{
int i;
for (i = 0; i < maxfd; i++) {
#ifdef USE_WATCHER_THREAD
continue;
#endif /* USE_WATCHER_THREAD */
}
}
#endif
#ifdef USE_WATCHER_THREAD
static isc_boolean_t
for (;;) {
"watcher got message %d "
/*
* 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)
return (ISC_TRUE);
/*
* 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.
*/
}
return (ISC_FALSE);
}
/*
* 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 cc;
#ifdef USE_KQUEUE
const char *fnname = "kevent()";
const char *fnname = "epoll_wait()";
#elif defined(USE_DEVPOLL)
const char *fnname = "ioctl(DP_POLL)";
#elif defined (USE_SELECT)
const char *fnname = "select()";
int maxfd;
int ctlfd;
#endif
char strbuf[ISC_STRERRORSIZE];
#ifdef ISC_SOCKET_USE_POLLWATCH
#endif
#if defined (USE_SELECT)
/*
* Get the control fd here. This will never change.
*/
#endif
while (!done) {
do {
#ifdef USE_KQUEUE
#elif defined(USE_DEVPOLL)
#ifndef ISC_SOCKET_USE_POLLWATCH
#else
else
#endif /* ISC_SOCKET_USE_POLLWATCH */
#elif defined(USE_SELECT)
#endif /* USE_KQUEUE */
"%s %s: %s", fnname,
"failed"), strbuf);
}
#if defined(USE_DEVPOLL) && defined(ISC_SOCKET_USE_POLLWATCH)
if (cc == 0) {
if (pollstate == poll_active)
else if (pollstate == poll_checking)
} else if (cc > 0) {
if (pollstate == poll_checking) {
/*
* XXX: We'd like to use a more
* verbose log level as it's actually an
* unexpected event, but the kernel bug
* reportedly happens pretty frequently
* (and it can also be a false positive)
* so it would be just too noisy.
*/
ISC_LOG_DEBUG(1),
"unexpected POLL timeout");
}
}
#endif
} while (cc < 0);
#elif defined(USE_SELECT)
/*
* Process reads on internal, control fd.
*/
#endif
}
ISC_MSG_EXITING, "watcher exiting"));
return ((isc_threadresult_t)0);
}
#endif /* USE_WATCHER_THREAD */
void
}
void
}
/*
* Create a new socket manager.
*/
static isc_result_t
char strbuf[ISC_STRERRORSIZE];
#endif
#ifdef USE_KQUEUE
return (ISC_R_NOMEMORY);
"kqueue %s: %s",
ISC_MSG_FAILED, "failed"),
strbuf);
return (result);
}
#ifdef USE_WATCHER_THREAD
if (result != ISC_R_SUCCESS) {
return (result);
}
#endif /* USE_WATCHER_THREAD */
return (ISC_R_NOMEMORY);
"epoll_create %s: %s",
ISC_MSG_FAILED, "failed"),
strbuf);
return (result);
}
#ifdef USE_WATCHER_THREAD
if (result != ISC_R_SUCCESS) {
return (result);
}
#endif /* USE_WATCHER_THREAD */
#elif defined(USE_DEVPOLL)
/*
* so we should be careful about redefining ISC_SOCKET_MAXEVENTS.
*/
return (ISC_R_NOMEMORY);
/*
* Note: fdpollinfo should be able to support all possible FDs, so
* it must have maxsocks entries (not nevents).
*/
return (ISC_R_NOMEMORY);
}
ISC_MSG_FAILED, "failed"),
strbuf);
return (result);
}
#ifdef USE_WATCHER_THREAD
if (result != ISC_R_SUCCESS) {
return (result);
}
#endif /* USE_WATCHER_THREAD */
#elif defined(USE_SELECT)
/*
* Note: this code should also cover the case of MAXSOCKETS <=
* FD_SETSIZE, but we separate the cases to avoid possible portability
* issues regarding howmany() and the actual representation of fd_set.
*/
sizeof(fd_mask);
#else
#endif
}
}
}
}
return (ISC_R_NOMEMORY);
}
#ifdef USE_WATCHER_THREAD
#else /* USE_WATCHER_THREAD */
#endif /* USE_WATCHER_THREAD */
#endif /* USE_KQUEUE */
return (ISC_R_SUCCESS);
}
static void
#ifdef USE_WATCHER_THREAD
if (result != ISC_R_SUCCESS) {
"epoll_ctl(DEL) %s",
ISC_MSG_FAILED, "failed"));
}
#endif /* USE_WATCHER_THREAD */
#ifdef USE_KQUEUE
#elif defined(USE_DEVPOLL)
#elif defined(USE_SELECT)
#endif /* USE_KQUEUE */
}
}
unsigned int maxsocks)
{
int i;
#ifdef USE_WATCHER_THREAD
char strbuf[ISC_STRERRORSIZE];
#endif
#ifdef USE_SHARED_MANAGER
/* Don't allow maxsocks to be updated */
return (ISC_R_EXISTS);
return (ISC_R_SUCCESS);
}
#endif /* USE_SHARED_MANAGER */
if (maxsocks == 0)
return (ISC_R_NOMEMORY);
/* zero-clear so that necessary cleanup on failure will be easy */
goto free_manager;
}
goto free_manager;
}
if (result != ISC_R_SUCCESS)
goto free_manager;
goto cleanup_lock;
}
for (i = 0; i < FDLOCK_COUNT; i++) {
if (result != ISC_R_SUCCESS) {
while (--i >= 0)
FDLOCK_COUNT * sizeof(isc_mutex_t));
goto cleanup_lock;
}
}
#ifdef USE_WATCHER_THREAD
"isc_condition_init() %s",
ISC_MSG_FAILED, "failed"));
goto cleanup_lock;
}
/*
* Create the special fds that will be used to wake up the
*/
"pipe() %s: %s",
ISC_MSG_FAILED, "failed"),
strbuf);
goto cleanup_condition;
}
#if 0
#endif
#endif /* USE_WATCHER_THREAD */
#ifdef USE_SHARED_MANAGER
#endif /* USE_SHARED_MANAGER */
/*
* Set up initial state for the select loop
*/
if (result != ISC_R_SUCCESS)
goto cleanup;
#ifdef USE_WATCHER_THREAD
/*
*/
"isc_thread_create() %s",
ISC_MSG_FAILED, "failed"));
goto cleanup;
}
#endif /* USE_WATCHER_THREAD */
#ifdef USE_SHARED_MANAGER
#endif /* USE_SHARED_MANAGER */
return (ISC_R_SUCCESS);
#ifdef USE_WATCHER_THREAD
#endif /* USE_WATCHER_THREAD */
#ifdef USE_WATCHER_THREAD
#endif /* USE_WATCHER_THREAD */
for (i = 0; i < FDLOCK_COUNT; i++)
}
FDLOCK_COUNT * sizeof(isc_mutex_t));
}
}
}
return (result);
}
return (ISC_R_SUCCESS);
}
void
}
void
int i;
/*
* Destroy a socket manager.
*/
#ifdef USE_SHARED_MANAGER
return;
}
#endif /* USE_SHARED_MANAGER */
/*
* Wait for all sockets to be destroyed.
*/
#ifdef USE_WATCHER_THREAD
"sockets exist"));
#else /* USE_WATCHER_THREAD */
#endif /* USE_WATCHER_THREAD */
}
/*
* half of the pipe, which will send EOF to the read half.
* This is currently a no-op in the non-threaded case.
*/
#ifdef USE_WATCHER_THREAD
/*
* Wait for thread to exit.
*/
"isc_thread_join() %s",
ISC_MSG_FAILED, "failed"));
#endif /* USE_WATCHER_THREAD */
/*
* Clean up.
*/
#ifdef USE_WATCHER_THREAD
#endif /* USE_WATCHER_THREAD */
(void)close(i);
for (i = 0; i < FDLOCK_COUNT; i++)
FDLOCK_COUNT * sizeof(isc_mutex_t));
}
#ifdef USE_SHARED_MANAGER
#endif
}
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 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.
*/
!sock->pending_send)
"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, 0));
}
{
pktinfo, 0));
}
unsigned int flags)
{
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;
}
void
#ifdef ISC_PLATFORM_HAVESYSUNH
int s;
char strbuf[ISC_STRERRORSIZE];
return;
#ifndef S_ISSOCK
#endif
#endif
#ifndef S_ISFIFO
#endif
#endif
#endif
#ifndef S_ISFIFO
#endif
#ifndef S_ISSOCK
#endif
if (active) {
"isc_socket_cleanunix: stat(%s): %s",
return;
}
"isc_socket_cleanunix: %s: not a socket",
return;
}
"isc_socket_cleanunix: unlink(%s): %s",
}
return;
}
if (s < 0) {
"isc_socket_cleanunix: socket(%s): %s",
return;
}
switch (errno) {
case ENOENT: /* We exited cleanly last time */
break;
default:
"isc_socket_cleanunix: stat(%s): %s",
break;
}
goto cleanup;
}
"isc_socket_cleanunix: %s: not a socket",
goto cleanup;
}
switch (errno) {
case ECONNREFUSED:
case ECONNRESET:
"isc_socket_cleanunix: "
"unlink(%s): %s",
strbuf);
}
break;
default:
"isc_socket_cleanunix: connect(%s): %s",
break;
}
}
close(s);
#else
#endif
}
{
#ifdef ISC_PLATFORM_HAVESYSUNH
char strbuf[ISC_STRERRORSIZE];
#ifdef NEED_SECURE_DIRECTORY
char *slash;
#endif
#ifdef NEED_SECURE_DIRECTORY
*slash = '\0';
else
} else
#endif
"isc_socket_permunix: chmod(%s, %d): %s",
}
"isc_socket_permunix: chown(%s, %d, %d): %s",
strbuf);
}
return (result);
#else
return (ISC_R_NOTIMPLEMENTED);
#endif
}
unsigned int options) {
char strbuf[ISC_STRERRORSIZE];
int on = 1;
return (ISC_R_FAMILYMISMATCH);
}
/*
* Only set SO_REUSEADDR when we want a specific port.
*/
#ifdef AF_UNIX
goto bind_socket;
#endif
if ((options & ISC_SOCKET_REUSEADDRESS) != 0 &&
sizeof(on)) < 0) {
ISC_MSG_FAILED, "failed"));
/* Press on... */
}
#ifdef AF_UNIX
#endif
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:
strbuf);
return (ISC_R_UNEXPECTED);
}
}
return (ISC_R_SUCCESS);
}
/*
* Enable this only for specific OS versions, and only when they have repaired
* their problems with it. Until then, this is is broken and needs to be
* diabled by default. See RT22589 for details.
*/
#if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
char strbuf[ISC_STRERRORSIZE];
struct accept_filter_arg afa;
#else
#endif
#if defined(SO_ACCEPTFILTER) && defined(ENABLE_ACCEPTFILTER)
ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
strbuf);
return (ISC_R_FAILURE);
}
return (ISC_R_SUCCESS);
#else
return (ISC_R_NOTIMPLEMENTED);
#endif
}
/*
* 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);
}
return (ISC_R_SUCCESS);
}
/*
* This should try to do aggressive 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 (result != ISC_R_SUCCESS) {
return (result);
}
/*
* Attach to socket and to task.
*/
if (isc_task_exiting(ntask)) {
free_socket(&nsock);
return (ISC_R_SHUTTINGDOWN);
}
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;
char strbuf[ISC_STRERRORSIZE];
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
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) {
/*
* HP-UX "fails" to connect a UDP socket and sets errno to
* EINPROGRESS if it's non-blocking. We'd rather regard this as
* a success and let the user detect it if it's really an error
* at the time of sending a packet on the socket.
*/
cc = 0;
goto success;
}
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;
char strbuf[ISC_STRERRORSIZE];
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
/*
* 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:
sizeof(peerbuf));
"internal_connect: connect(%s) %s",
}
} else {
}
}
} else {
}
return (result);
}
char strbuf[ISC_STRERRORSIZE];
goto out;
}
strbuf);
goto out;
}
out:
return (result);
}
/*
* Run through the list of events on this socket, and cancel the ones
* queued for task "task" of type "how". "how" is a bitmask.
*/
void
/*
* Quick exit if there is nothing to do. Don't even bother locking
* in this case.
*/
if (how == 0)
return;
/*
* 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_PTR(&dev));
}
}
}
/*
* Connecting is not a list.
*/
sock->connecting = 0;
ISC_EVENT_PTR(&dev));
}
}
}
}
return (val);
}
void
#if defined(IPV6_V6ONLY)
#else
#endif
#ifdef IPV6_V6ONLY
(void *)&onoff, sizeof(int)) < 0) {
char strbuf[ISC_STRERRORSIZE];
"setsockopt(%d, IPV6_V6ONLY) "
"failed"),
strbuf);
}
}
#endif
}
static void
#if defined(IP_TOS) || defined(IPV6_TCLASS)
#endif
#ifdef IP_TOS
char strbuf[ISC_STRERRORSIZE];
"setsockopt(%d, IP_TOS, %.02x) "
"failed"),
strbuf);
}
}
#endif
#ifdef IPV6_TCLASS
char strbuf[ISC_STRERRORSIZE];
"setsockopt(%d, IPV6_TCLASS, %.02x) "
"failed"),
strbuf);
}
}
#endif
}
void
#if !defined(IP_TOS) && !defined(IPV6_TCLASS)
#else
if (dscp < 0)
return;
if (isc_dscp_check_value != -1)
#endif
#ifdef notyet
#endif
}
const void *arg)
{
}
#ifndef USE_WATCHER_THREAD
/*
* In our assumed scenario, we can simply use a single static object.
* XXX: this is not true if the application uses multiple threads with
* 'multi-context' mode. Fixing this is a future TODO item.
*/
static isc_socketwait_t swait_private;
int
{
int n;
#ifdef USE_KQUEUE
#endif
#ifdef USE_EPOLL
int timeout;
#endif
#ifdef USE_DEVPOLL
#endif
#ifdef USE_SHARED_MANAGER
#endif
return (0);
#ifdef USE_KQUEUE
} else
tsp);
n = swait_private.nevents;
else
timeout = -1;
n = swait_private.nevents;
#elif defined(USE_DEVPOLL)
} else
n = swait_private.nevents;
#elif defined(USE_SELECT)
#endif
*swaitp = &swait_private;
return (n);
}
#ifdef USE_SHARED_MANAGER
#endif
return (ISC_R_NOTFOUND);
return (ISC_R_SUCCESS);
#elif defined(USE_SELECT)
return (ISC_R_SUCCESS);
#endif
}
#endif /* USE_WATCHER_THREAD */
void
/*
* Name 'socket'.
*/
}
const char *
}
void *
}
return (isc_socket_register(isc__socketmgr_create));
}
int
}
#if defined(HAVE_LIBXML2) || defined(HAVE_JSON)
static const char *
{
if (type == isc_sockettype_udp)
return ("udp");
else if (type == isc_sockettype_tcp)
return ("tcp");
else if (type == isc_sockettype_unix)
return ("unix");
else if (type == isc_sockettype_fdwatch)
return ("fdwatch");
else
return ("not-initialized");
}
#endif
#ifdef HAVE_LIBXML2
int
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
int xmlrc;
#ifdef USE_SHARED_MANAGER
#endif /* USE_SHARED_MANAGER */
ISC_XMLCHAR "name"));
}
ISC_XMLCHAR "references"));
sock->references));
sizeof(peerbuf));
ISC_XMLCHAR "peer-address",
}
ISC_XMLCHAR "local-address",
}
if (sock->pending_recv)
ISC_XMLCHAR "state",
ISC_XMLCHAR "pending-receive"));
if (sock->pending_send)
ISC_XMLCHAR "state",
ISC_XMLCHAR "pending-send"));
if (sock->pending_accept)
ISC_XMLCHAR "state",
ISC_XMLCHAR "pending_accept"));
ISC_XMLCHAR "state",
ISC_XMLCHAR "listener"));
ISC_XMLCHAR "state",
ISC_XMLCHAR "connected"));
if (sock->connecting)
ISC_XMLCHAR "state",
ISC_XMLCHAR "connecting"));
ISC_XMLCHAR "state",
ISC_XMLCHAR "bound"));
}
return (xmlrc);
}
#endif /* HAVE_LIBXML2 */
#ifdef HAVE_JSON
#define CHECKMEM(m) do { \
if (m == NULL) { \
result = ISC_R_NOMEMORY;\
goto error;\
} \
} while(0)
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
#ifdef USE_SHARED_MANAGER
#endif /* USE_SHARED_MANAGER */
char buf[255];
}
sizeof(peerbuf));
}
}
if (sock->pending_recv) {
}
if (sock->pending_send) {
}
if (sock->pending_accept) {
}
}
}
if (sock->connecting) {
}
}
}
return (result);
}
#endif /* HAVE_JSON */
#include "../socket_api.c"