1998-12-12 20:48:14 +00:00
|
|
|
/*
|
2008-01-10 23:47:01 +00:00
|
|
|
* Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
|
2004-03-05 05:14:21 +00:00
|
|
|
* Copyright (C) 1998-2003 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1998-12-12 20:48:14 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* 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.
|
1998-12-12 20:48:14 +00:00
|
|
|
*/
|
1998-12-12 19:25:20 +00:00
|
|
|
|
2008-07-18 02:43:31 +00:00
|
|
|
/* $Id: socket.c,v 1.288 2008/07/18 02:43:31 jinmei Exp $ */
|
2005-04-27 04:57:32 +00:00
|
|
|
|
|
|
|
/*! \file */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
1998-12-12 19:25:20 +00:00
|
|
|
#include <config.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-04-13 08:10:18 +00:00
|
|
|
#include <sys/param.h>
|
1999-08-25 23:50:48 +00:00
|
|
|
#include <sys/types.h>
|
1999-12-04 01:27:44 +00:00
|
|
|
#include <sys/socket.h>
|
2005-02-23 01:09:23 +00:00
|
|
|
#include <sys/stat.h>
|
1999-12-04 01:27:44 +00:00
|
|
|
#include <sys/time.h>
|
1999-08-25 23:50:48 +00:00
|
|
|
#include <sys/uio.h>
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <errno.h>
|
2001-04-24 18:40:07 +00:00
|
|
|
#include <fcntl.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
1999-09-02 02:10:44 +00:00
|
|
|
#include <isc/buffer.h>
|
2000-04-28 03:53:48 +00:00
|
|
|
#include <isc/bufferlist.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/condition.h>
|
2001-08-08 22:54:55 +00:00
|
|
|
#include <isc/formatcheck.h>
|
1999-09-02 02:10:44 +00:00
|
|
|
#include <isc/list.h>
|
2000-05-03 21:09:49 +00:00
|
|
|
#include <isc/log.h>
|
2000-04-28 03:53:48 +00:00
|
|
|
#include <isc/mem.h>
|
2000-12-06 00:30:32 +00:00
|
|
|
#include <isc/msgs.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/mutex.h>
|
1999-08-31 20:57:46 +00:00
|
|
|
#include <isc/net.h>
|
2007-02-26 01:37:37 +00:00
|
|
|
#include <isc/once.h>
|
2000-08-29 23:58:17 +00:00
|
|
|
#include <isc/platform.h>
|
2000-05-03 22:04:49 +00:00
|
|
|
#include <isc/print.h>
|
1999-09-02 02:10:44 +00:00
|
|
|
#include <isc/region.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <isc/socket.h>
|
2001-08-31 05:57:58 +00:00
|
|
|
#include <isc/strerror.h>
|
2000-04-28 03:53:48 +00:00
|
|
|
#include <isc/task.h>
|
2000-12-26 21:45:08 +00:00
|
|
|
#include <isc/thread.h>
|
1999-12-16 22:24:22 +00:00
|
|
|
#include <isc/util.h>
|
2007-02-13 02:49:08 +00:00
|
|
|
#include <isc/xml.h>
|
1998-12-13 02:04:22 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
#ifdef ISC_PLATFORM_HAVEKQUEUE
|
|
|
|
#include <sys/event.h>
|
|
|
|
#endif
|
|
|
|
#ifdef ISC_PLATFORM_HAVEEPOLL
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#endif
|
|
|
|
#ifdef ISC_PLATFORM_HAVEDEVPOLL
|
|
|
|
#include <sys/devpoll.h>
|
|
|
|
#endif
|
|
|
|
|
2001-05-14 23:58:33 +00:00
|
|
|
#include "errno2result.h"
|
|
|
|
|
2000-12-26 21:45:08 +00:00
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
2000-08-29 23:58:17 +00:00
|
|
|
#include "socket_p.h"
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2007-02-26 01:37:37 +00:00
|
|
|
#if defined(SO_BSDCOMPAT) && defined(__linux__)
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*%
|
2008-06-23 23:47:11 +00:00
|
|
|
* Choose the most preferable multiplex method.
|
2008-06-23 19:41:20 +00:00
|
|
|
*/
|
|
|
|
#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 ISC_PLATFORM_USETHREADS
|
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
|
|
|
struct isc_socketwait {
|
|
|
|
int nevents;
|
|
|
|
};
|
|
|
|
#elif defined (USE_SELECT)
|
|
|
|
struct isc_socketwait {
|
|
|
|
fd_set readset;
|
|
|
|
fd_set writeset;
|
|
|
|
int nfds;
|
|
|
|
int maxfd;
|
|
|
|
};
|
|
|
|
#endif /* USE_KQUEUE */
|
|
|
|
#endif /* !ISC_PLATFORM_USETHREADS */
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Maximum number of allowable open sockets. This is also the maximum
|
|
|
|
* allowable socket file descriptor. This definition is meaningless with
|
|
|
|
* USE_SELECT due to the API limitation of select(2).
|
|
|
|
*/
|
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
|
|
|
#ifndef ISC_SOCKET_MAXSOCKETS
|
|
|
|
#define ISC_SOCKET_MAXSOCKETS 4096
|
|
|
|
#endif
|
|
|
|
#endif /* USE_KQUEUE || USE_EPOLL || USE_DEVPOLL */
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Size of per-FD lock buckets.
|
|
|
|
*/
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
#define FDLOCK_COUNT 1024
|
|
|
|
#define FDLOCK_ID(fd) ((fd) % FDLOCK_COUNT)
|
|
|
|
#else
|
|
|
|
#define FDLOCK_COUNT 1
|
|
|
|
#define FDLOCK_ID(fd) 0
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
|
|
|
|
/*%
|
|
|
|
* Maximum number of events communicated with the kernel. There should normally
|
|
|
|
* be no need for having a large number.
|
|
|
|
*/
|
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
|
|
|
#ifndef ISC_SOCKET_MAXEVENTS
|
|
|
|
#define ISC_SOCKET_MAXEVENTS 64
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-02-11 06:38:12 +00:00
|
|
|
* Some systems define the socket length argument as an int, some as size_t,
|
1999-08-28 05:34:41 +00:00
|
|
|
* some as socklen_t. This is here so it can be easily changed if needed.
|
1999-02-11 06:38:12 +00:00
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
#ifndef ISC_SOCKADDR_LEN_T
|
1999-08-28 05:34:41 +00:00
|
|
|
#define ISC_SOCKADDR_LEN_T unsigned int
|
1999-03-29 23:56:06 +00:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-02-11 06:38:12 +00:00
|
|
|
* Define what the possible "soft" errors can be. These are non-fatal returns
|
|
|
|
* of various network related functions, like recv() and so on.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
|
|
|
* 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.
|
1999-02-11 06:38:12 +00:00
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
#define SOFT_ERROR(e) ((e) == EAGAIN || \
|
|
|
|
(e) == EWOULDBLOCK || \
|
|
|
|
(e) == EINTR || \
|
|
|
|
(e) == 0)
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-05-03 21:09:49 +00:00
|
|
|
#define DLVL(x) ISC_LOGCATEGORY_GENERAL, ISC_LOGMODULE_SOCKET, ISC_LOG_DEBUG(x)
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!<
|
2000-05-03 21:09:49 +00:00
|
|
|
* DLVL(90) -- Function entry/exit and other tracing.
|
|
|
|
* DLVL(70) -- Socket "correctness" -- including returning of events, etc.
|
|
|
|
* DLVL(60) -- Socket data send/receive
|
|
|
|
* DLVL(50) -- Event tracing, including receiving/sending completion events.
|
|
|
|
* DLVL(20) -- Socket creation/destruction.
|
|
|
|
*/
|
2000-07-13 01:13:53 +00:00
|
|
|
#define TRACE_LEVEL 90
|
|
|
|
#define CORRECTNESS_LEVEL 70
|
|
|
|
#define IOEVENT_LEVEL 60
|
|
|
|
#define EVENT_LEVEL 50
|
|
|
|
#define CREATION_LEVEL 20
|
|
|
|
|
|
|
|
#define TRACE DLVL(TRACE_LEVEL)
|
|
|
|
#define CORRECTNESS DLVL(CORRECTNESS_LEVEL)
|
|
|
|
#define IOEVENT DLVL(IOEVENT_LEVEL)
|
|
|
|
#define EVENT DLVL(EVENT_LEVEL)
|
|
|
|
#define CREATION DLVL(CREATION_LEVEL)
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
typedef isc_event_t intev_t;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define SOCKET_MAGIC ISC_MAGIC('I', 'O', 'i', 'o')
|
|
|
|
#define VALID_SOCKET(t) ISC_MAGIC_VALID(t, SOCKET_MAGIC)
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*!
|
1999-12-04 01:27:44 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2006-05-19 02:47:18 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
|
1999-12-04 01:27:44 +00:00
|
|
|
#ifndef USE_CMSG
|
|
|
|
#define USE_CMSG 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
1999-12-15 20:47:38 +00:00
|
|
|
* NetBSD and FreeBSD can timestamp packets. XXXMLG Should we have
|
1999-12-04 01:27:44 +00:00
|
|
|
* 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
|
|
|
|
|
2005-07-12 00:41:55 +00:00
|
|
|
/*%
|
|
|
|
* The size to raise the recieve buffer to (from BIND 8).
|
|
|
|
*/
|
|
|
|
#define RCVBUFSIZE (32*1024)
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2001-03-06 01:23:03 +00:00
|
|
|
* The number of times a send operation is repeated if the result is EINTR.
|
|
|
|
*/
|
|
|
|
#define NRETRIES 10
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
struct isc_socket {
|
|
|
|
/* Not locked. */
|
2000-05-10 21:17:49 +00:00
|
|
|
unsigned int magic;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
isc_sockettype_t type;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/* Locked by socket lock. */
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LINK(isc_socket_t) link;
|
2000-05-10 21:17:49 +00:00
|
|
|
unsigned int references;
|
|
|
|
int fd;
|
2000-08-10 23:11:12 +00:00
|
|
|
int pf;
|
2007-02-13 02:49:08 +00:00
|
|
|
char name[16];
|
|
|
|
void * tag;
|
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
ISC_LIST(isc_socketevent_t) send_list;
|
1999-07-28 01:07:21 +00:00
|
|
|
ISC_LIST(isc_socketevent_t) recv_list;
|
1999-06-10 00:45:30 +00:00
|
|
|
ISC_LIST(isc_socket_newconnev_t) accept_list;
|
1999-07-28 01:07:21 +00:00
|
|
|
isc_socket_connev_t *connect_ev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-05-10 21:17:49 +00:00
|
|
|
intev_t readable_ev;
|
|
|
|
intev_t writable_ev;
|
1999-06-10 00:45:30 +00:00
|
|
|
|
2007-01-03 20:57:07 +00:00
|
|
|
isc_sockaddr_t peer_address; /* remote address */
|
1999-07-28 06:50:10 +00:00
|
|
|
|
2000-05-10 21:17:49 +00:00
|
|
|
unsigned int pending_recv : 1,
|
|
|
|
pending_send : 1,
|
|
|
|
pending_accept : 1,
|
|
|
|
listener : 1, /* listener socket */
|
|
|
|
connected : 1,
|
|
|
|
connecting : 1, /* connect pending */
|
|
|
|
bound : 1; /* bound to local addr */
|
1999-08-28 04:26:49 +00:00
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
#ifdef ISC_NET_RECVOVERFLOW
|
2000-05-10 21:17:49 +00:00
|
|
|
unsigned char overflow; /* used for MSG_TRUNC fake */
|
1999-09-13 22:00:28 +00:00
|
|
|
#endif
|
2004-01-29 04:39:19 +00:00
|
|
|
|
|
|
|
char *recvcmsgbuf;
|
|
|
|
ISC_SOCKADDR_LEN_T recvcmsgbuflen;
|
|
|
|
char *sendcmsgbuf;
|
|
|
|
ISC_SOCKADDR_LEN_T sendcmsgbuflen;
|
2005-12-06 16:54:49 +00:00
|
|
|
|
|
|
|
void *fdwatcharg;
|
|
|
|
isc_sockfdwatch_t fdwatchcb;
|
|
|
|
int fdwatchflags;
|
|
|
|
isc_task_t *fdwatchtask;
|
1998-11-03 00:54:47 +00:00
|
|
|
};
|
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define SOCKET_MANAGER_MAGIC ISC_MAGIC('I', 'O', 'm', 'g')
|
|
|
|
#define VALID_MANAGER(m) ISC_MAGIC_VALID(m, SOCKET_MANAGER_MAGIC)
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
struct isc_socketmgr {
|
|
|
|
/* Not locked. */
|
2000-05-10 21:17:49 +00:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_mutex_t lock;
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_mutex_t *fdlock;
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
int kqueue_fd;
|
|
|
|
int nevents;
|
|
|
|
struct kevent *events;
|
|
|
|
#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;
|
|
|
|
struct pollfd *events;
|
|
|
|
#endif /* USE_DEVPOLL */
|
|
|
|
unsigned int maxsocks;
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
int pipe_fds[2];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Locked by fdlock. */
|
|
|
|
isc_socket_t **fds;
|
|
|
|
int *fdstate;
|
|
|
|
#ifdef USE_DEVPOLL
|
|
|
|
pollinfo_t *fdpollinfo;
|
|
|
|
#endif
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/* Locked by manager lock. */
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST(isc_socket_t) socklist;
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_SELECT
|
2000-05-10 21:17:49 +00:00
|
|
|
fd_set read_fds;
|
|
|
|
fd_set write_fds;
|
|
|
|
int maxfd;
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif /* USE_SELECT */
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
isc_thread_t watcher;
|
|
|
|
isc_condition_t shutdown_ok;
|
2000-09-28 21:31:08 +00:00
|
|
|
#else /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
unsigned int refs;
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-03 00:54:47 +00:00
|
|
|
};
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
|
|
|
static isc_socketmgr_t *socketmgr = NULL;
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2005-12-06 18:11:54 +00:00
|
|
|
#define CLOSED 0 /* this one must be zero */
|
|
|
|
#define MANAGED 1
|
|
|
|
#define CLOSE_PENDING 2
|
|
|
|
#define MANAGER_CLOSE_PENDING 3
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
/*
|
|
|
|
* send() and recv() iovec counts
|
|
|
|
*/
|
|
|
|
#define MAXSCATTERGATHER_SEND (ISC_SOCKET_MAXSCATTERGATHER)
|
|
|
|
#ifdef ISC_NET_RECVOVERFLOW
|
|
|
|
# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER + 1)
|
|
|
|
#else
|
|
|
|
# define MAXSCATTERGATHER_RECV (ISC_SOCKET_MAXSCATTERGATHER)
|
|
|
|
#endif
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
static void send_recvdone_event(isc_socket_t *, isc_socketevent_t **);
|
|
|
|
static void send_senddone_event(isc_socket_t *, isc_socketevent_t **);
|
1998-12-13 23:45:21 +00:00
|
|
|
static void free_socket(isc_socket_t **);
|
|
|
|
static isc_result_t allocate_socket(isc_socketmgr_t *, isc_sockettype_t,
|
|
|
|
isc_socket_t **);
|
|
|
|
static void destroy(isc_socket_t **);
|
1998-12-16 02:05:38 +00:00
|
|
|
static void internal_accept(isc_task_t *, isc_event_t *);
|
|
|
|
static void internal_connect(isc_task_t *, isc_event_t *);
|
|
|
|
static void internal_recv(isc_task_t *, isc_event_t *);
|
|
|
|
static void internal_send(isc_task_t *, isc_event_t *);
|
2005-12-06 16:54:49 +00:00
|
|
|
static void internal_fdwatch_write(isc_task_t *, isc_event_t *);
|
|
|
|
static void internal_fdwatch_read(isc_task_t *, isc_event_t *);
|
1999-08-28 04:26:49 +00:00
|
|
|
static void process_cmsg(isc_socket_t *, struct msghdr *, isc_socketevent_t *);
|
1999-09-03 01:18:45 +00:00
|
|
|
static void build_msghdr_send(isc_socket_t *, isc_socketevent_t *,
|
2004-01-29 04:39:19 +00:00
|
|
|
struct msghdr *, struct iovec *, size_t *);
|
1999-09-03 01:18:45 +00:00
|
|
|
static void build_msghdr_recv(isc_socket_t *, isc_socketevent_t *,
|
2004-01-29 04:39:19 +00:00
|
|
|
struct msghdr *, struct iovec *, size_t *);
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
static isc_boolean_t process_ctlfd(isc_socketmgr_t *manager);
|
|
|
|
#endif
|
1998-12-04 11:21:11 +00:00
|
|
|
|
|
|
|
#define SELECT_POKE_SHUTDOWN (-1)
|
|
|
|
#define SELECT_POKE_NOTHING (-2)
|
2001-01-25 22:25:10 +00:00
|
|
|
#define SELECT_POKE_READ (-3)
|
2005-04-27 04:57:32 +00:00
|
|
|
#define SELECT_POKE_ACCEPT (-3) /*%< Same as _READ */
|
2001-01-25 22:25:10 +00:00
|
|
|
#define SELECT_POKE_WRITE (-4)
|
2005-04-27 04:57:32 +00:00
|
|
|
#define SELECT_POKE_CONNECT (-4) /*%< Same as _WRITE */
|
2001-01-25 22:25:10 +00:00
|
|
|
#define SELECT_POKE_CLOSE (-5)
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-01-28 01:31:21 +00:00
|
|
|
#define SOCK_DEAD(s) ((s)->references == 0)
|
|
|
|
|
2001-08-08 22:54:55 +00:00
|
|
|
static void
|
|
|
|
manager_log(isc_socketmgr_t *sockmgr,
|
|
|
|
isc_logcategory_t *category, isc_logmodule_t *module, int level,
|
|
|
|
const char *fmt, ...) ISC_FORMAT_PRINTF(5, 6);
|
2000-05-03 21:09:49 +00:00
|
|
|
static void
|
|
|
|
manager_log(isc_socketmgr_t *sockmgr,
|
|
|
|
isc_logcategory_t *category, isc_logmodule_t *module, int level,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char msgbuf[2048];
|
|
|
|
va_list ap;
|
|
|
|
|
2000-07-13 01:38:40 +00:00
|
|
|
if (! isc_log_wouldlog(isc_lctx, level))
|
|
|
|
return;
|
|
|
|
|
2000-05-03 21:09:49 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
isc_log_write(isc_lctx, category, module, level,
|
|
|
|
"sockmgr %p: %s", sockmgr, msgbuf);
|
|
|
|
}
|
|
|
|
|
2001-08-08 22:54:55 +00:00
|
|
|
static void
|
|
|
|
socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
|
|
|
|
isc_logcategory_t *category, isc_logmodule_t *module, int level,
|
|
|
|
isc_msgcat_t *msgcat, int msgset, int message,
|
|
|
|
const char *fmt, ...) ISC_FORMAT_PRINTF(9, 10);
|
2000-05-03 21:09:49 +00:00
|
|
|
static void
|
|
|
|
socket_log(isc_socket_t *sock, isc_sockaddr_t *address,
|
|
|
|
isc_logcategory_t *category, isc_logmodule_t *module, int level,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat_t *msgcat, int msgset, int message,
|
2000-05-03 21:09:49 +00:00
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
char msgbuf[2048];
|
2005-03-15 01:41:28 +00:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
2000-05-03 21:09:49 +00:00
|
|
|
va_list ap;
|
|
|
|
|
2000-07-13 00:25:38 +00:00
|
|
|
if (! isc_log_wouldlog(isc_lctx, level))
|
|
|
|
return;
|
|
|
|
|
2000-05-03 21:09:49 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (address == NULL) {
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_log_iwrite(isc_lctx, category, module, level,
|
|
|
|
msgcat, msgset, message,
|
|
|
|
"socket %p: %s", sock, msgbuf);
|
2000-05-03 21:09:49 +00:00
|
|
|
} else {
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_sockaddr_format(address, peerbuf, sizeof(peerbuf));
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_log_iwrite(isc_lctx, category, module, level,
|
|
|
|
msgcat, msgset, message,
|
|
|
|
"socket %p %s: %s", sock, peerbuf, msgbuf);
|
2000-05-03 21:09:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
static inline isc_result_t
|
|
|
|
watch_fd(isc_socketmgr_t *manager, int fd, int msg) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
struct kevent evchange;
|
|
|
|
|
|
|
|
memset(&evchange, 0, sizeof(evchange));
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
evchange.filter = EVFILT_READ;
|
|
|
|
else
|
|
|
|
evchange.filter = EVFILT_WRITE;
|
|
|
|
evchange.flags = EV_ADD;
|
|
|
|
evchange.ident = fd;
|
|
|
|
if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
struct epoll_event event;
|
|
|
|
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
else
|
|
|
|
event.events = EPOLLOUT;
|
|
|
|
event.data.fd = fd;
|
|
|
|
if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_ADD, fd, &event) == -1 &&
|
|
|
|
errno != EEXIST) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
struct pollfd pfd;
|
|
|
|
int lockid = FDLOCK_ID(fd);
|
|
|
|
|
|
|
|
memset(&pfd, 0, sizeof(pfd));
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
pfd.events = POLLIN;
|
|
|
|
else
|
|
|
|
pfd.events = POLLOUT;
|
|
|
|
pfd.fd = fd;
|
|
|
|
pfd.revents = 0;
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
|
|
|
if (write(manager->devpoll_fd, &pfd, sizeof(pfd)) == -1)
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
else {
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
manager->fdpollinfo[fd].want_read = 1;
|
|
|
|
else
|
|
|
|
manager->fdpollinfo[fd].want_write = 1;
|
|
|
|
}
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
FD_SET(fd, &manager->read_fds);
|
|
|
|
if (msg == SELECT_POKE_WRITE)
|
|
|
|
FD_SET(fd, &manager->write_fds);
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
|
|
|
unwatch_fd(isc_socketmgr_t *manager, int fd, int msg) {
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
struct kevent evchange;
|
|
|
|
|
|
|
|
memset(&evchange, 0, sizeof(evchange));
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
evchange.filter = EVFILT_READ;
|
|
|
|
else
|
|
|
|
evchange.filter = EVFILT_WRITE;
|
|
|
|
evchange.flags = EV_DELETE;
|
|
|
|
evchange.ident = fd;
|
|
|
|
if (kevent(manager->kqueue_fd, &evchange, 1, NULL, 0, NULL) != 0)
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
struct epoll_event event;
|
|
|
|
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
event.events = EPOLLIN;
|
|
|
|
else
|
|
|
|
event.events = EPOLLOUT;
|
|
|
|
event.data.fd = fd;
|
|
|
|
if (epoll_ctl(manager->epoll_fd, EPOLL_CTL_DEL, fd, &event) == -1 &&
|
|
|
|
errno != ENOENT) {
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"epoll_ctl(DEL), %d: %s", fd, strbuf);
|
|
|
|
result = ISC_R_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
struct pollfd pfds[2];
|
|
|
|
size_t writelen = sizeof(pfds[0]);
|
|
|
|
int lockid = FDLOCK_ID(fd);
|
|
|
|
|
|
|
|
memset(pfds, 0, sizeof(pfds));
|
|
|
|
pfds[0].events = POLLREMOVE;
|
|
|
|
pfds[0].fd = fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Canceling read or write polling via /dev/poll is tricky. Since it
|
|
|
|
* only provides a way of canceling per FD, we may need to re-poll the
|
|
|
|
* socket for the other operation.
|
|
|
|
*/
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
|
|
|
if (msg == SELECT_POKE_READ &&
|
|
|
|
manager->fdpollinfo[fd].want_write == 1) {
|
|
|
|
pfds[1].events = POLLOUT;
|
|
|
|
pfds[1].fd = fd;
|
|
|
|
writelen += sizeof(pfds[1]);
|
|
|
|
}
|
|
|
|
if (msg == SELECT_POKE_WRITE &&
|
|
|
|
manager->fdpollinfo[fd].want_read == 1) {
|
|
|
|
pfds[1].events = POLLIN;
|
|
|
|
pfds[1].fd = fd;
|
|
|
|
writelen += sizeof(pfds[1]);
|
|
|
|
}
|
|
|
|
|
2008-06-23 23:47:11 +00:00
|
|
|
if (write(manager->devpoll_fd, pfds, writelen) == -1)
|
2008-06-23 19:41:20 +00:00
|
|
|
result = isc__errno2result(errno);
|
|
|
|
else {
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
manager->fdpollinfo[fd].want_read = 0;
|
|
|
|
else
|
|
|
|
manager->fdpollinfo[fd].want_write = 0;
|
|
|
|
}
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
if (msg == SELECT_POKE_READ)
|
|
|
|
FD_CLR(fd, &manager->read_fds);
|
|
|
|
else if (msg == SELECT_POKE_WRITE)
|
|
|
|
FD_CLR(fd, &manager->write_fds);
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
static void
|
2001-01-25 22:25:10 +00:00
|
|
|
wakeup_socket(isc_socketmgr_t *manager, int fd, int msg) {
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_result_t result;
|
|
|
|
isc_boolean_t needclose;
|
|
|
|
int lockid = FDLOCK_ID(fd);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
|
|
|
/*
|
2001-01-25 22:25:10 +00:00
|
|
|
* 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.
|
2000-08-29 23:58:17 +00:00
|
|
|
*/
|
2001-04-26 23:45:55 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
INSIST(fd >= 0 && fd < (int)manager->maxsocks);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
LOCK(&manager->fdlock[lockid]);
|
2005-12-06 18:11:54 +00:00
|
|
|
if (manager->fdstate[fd] == CLOSE_PENDING
|
|
|
|
|| manager->fdstate[fd] == MANAGER_CLOSE_PENDING) {
|
2008-06-23 19:41:20 +00:00
|
|
|
needclose = ISC_TF(manager->fdstate[fd] == CLOSE_PENDING);
|
2005-12-07 04:21:27 +00:00
|
|
|
manager->fdstate[fd] = CLOSED;
|
2008-06-23 19:41:20 +00:00
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_READ);
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
|
|
|
|
if (needclose)
|
|
|
|
(void)close(fd);
|
2000-08-29 23:58:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
if (manager->fdstate[fd] != MANAGED) {
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
2000-08-29 23:58:17 +00:00
|
|
|
return;
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
|
|
|
/*
|
2001-12-19 05:51:34 +00:00
|
|
|
* Set requested bit.
|
2000-08-29 23:58:17 +00:00
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
result = watch_fd(manager, fd, msg);
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"failed to start watching FD (%d): %s",
|
|
|
|
fd, isc_result_totext(result));
|
|
|
|
}
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
1998-12-04 11:21:11 +00:00
|
|
|
* Poke the select loop when there is something for us to do.
|
2001-01-25 22:25:10 +00:00
|
|
|
* The write is required (by POSIX) to complete. That is, we
|
|
|
|
* will not get partial writes.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
|
|
|
static void
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(isc_socketmgr_t *mgr, int fd, int msg) {
|
1998-11-06 01:45:35 +00:00
|
|
|
int cc;
|
2001-01-25 22:25:10 +00:00
|
|
|
int buf[2];
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2001-01-25 22:25:10 +00:00
|
|
|
|
|
|
|
buf[0] = fd;
|
|
|
|
buf[1] = msg;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
do {
|
2001-01-25 22:25:10 +00:00
|
|
|
cc = write(mgr->pipe_fds[1], buf, sizeof(buf));
|
2002-01-22 02:38:34 +00:00
|
|
|
#ifdef ENOSR
|
|
|
|
/*
|
|
|
|
* Treat ENOSR as EAGAIN but loop slowly as it is
|
|
|
|
* unlikely to clear fast.
|
|
|
|
*/
|
|
|
|
if (cc < 0 && errno == ENOSR) {
|
|
|
|
sleep(1);
|
|
|
|
errno = EAGAIN;
|
|
|
|
}
|
|
|
|
#endif
|
1999-02-11 06:38:12 +00:00
|
|
|
} while (cc < 0 && SOFT_ERROR(errno));
|
2005-05-19 02:42:42 +00:00
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
if (cc < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1998-12-04 11:21:11 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_WRITEFAILED,
|
|
|
|
"write() failed "
|
|
|
|
"during watcher poke: %s"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
|
|
|
}
|
1999-02-11 06:38:12 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
INSIST(cc == sizeof(buf));
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Read a message on the internal fd.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2001-01-25 22:25:10 +00:00
|
|
|
static void
|
|
|
|
select_readmsg(isc_socketmgr_t *mgr, int *fd, int *msg) {
|
|
|
|
int buf[2];
|
1998-11-06 01:45:35 +00:00
|
|
|
int cc;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
cc = read(mgr->pipe_fds[0], buf, sizeof(buf));
|
1998-11-06 01:45:35 +00:00
|
|
|
if (cc < 0) {
|
2001-01-25 22:25:10 +00:00
|
|
|
*msg = SELECT_POKE_NOTHING;
|
2005-11-03 22:59:53 +00:00
|
|
|
*fd = -1; /* Silence compiler. */
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
2001-01-25 22:25:10 +00:00
|
|
|
return;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1998-12-04 11:21:11 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_READFAILED,
|
|
|
|
"read() failed "
|
|
|
|
"during watcher poke: %s"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
return;
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
INSIST(cc == sizeof(buf));
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
*fd = buf[0];
|
|
|
|
*msg = buf[1];
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#else /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
/*
|
|
|
|
* Update the state of the socketmgr when something changes.
|
|
|
|
*/
|
|
|
|
static void
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(isc_socketmgr_t *manager, int fd, int msg) {
|
2000-08-29 23:58:17 +00:00
|
|
|
if (msg == SELECT_POKE_SHUTDOWN)
|
|
|
|
return;
|
2001-01-25 22:25:10 +00:00
|
|
|
else if (fd >= 0)
|
|
|
|
wakeup_socket(manager, fd, msg);
|
2000-08-29 23:58:17 +00:00
|
|
|
return;
|
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Make a fd non-blocking.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
static isc_result_t
|
2000-05-14 22:35:45 +00:00
|
|
|
make_nonblock(int fd) {
|
1998-11-06 01:45:35 +00:00
|
|
|
int ret;
|
|
|
|
int flags;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2005-07-08 04:30:22 +00:00
|
|
|
#ifdef USE_FIONBIO_IOCTL
|
|
|
|
int on = 1;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2005-07-08 04:30:22 +00:00
|
|
|
ret = ioctl(fd, FIONBIO, (char *)&on);
|
|
|
|
#else
|
1998-11-06 01:45:35 +00:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
2005-07-08 04:30:22 +00:00
|
|
|
flags |= PORT_NONBLOCK;
|
1998-11-06 01:45:35 +00:00
|
|
|
ret = fcntl(fd, F_SETFL, flags);
|
2005-07-08 04:30:22 +00:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
if (ret == -1) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1998-11-06 01:45:35 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2005-07-08 04:30:22 +00:00
|
|
|
#ifdef USE_FIONBIO_IOCTL
|
|
|
|
"ioctl(%d, FIONBIO, &on): %s", fd,
|
|
|
|
#else
|
|
|
|
"fcntl(%d, F_SETFL, %d): %s", fd, flags,
|
|
|
|
#endif
|
|
|
|
strbuf);
|
1998-12-04 11:21:11 +00:00
|
|
|
|
1998-12-01 21:39:00 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-12-01 21:39:00 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
#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
|
|
|
|
cmsg_len(ISC_SOCKADDR_LEN_T len) {
|
|
|
|
#ifdef CMSG_LEN
|
|
|
|
return (CMSG_LEN(len));
|
|
|
|
#else
|
|
|
|
ISC_SOCKADDR_LEN_T hdrlen;
|
|
|
|
|
2005-03-30 05:37:08 +00:00
|
|
|
/*
|
|
|
|
* Cast NULL so that any pointer arithmetic performed by CMSG_DATA
|
|
|
|
* is correct.
|
|
|
|
*/
|
|
|
|
hdrlen = (ISC_SOCKADDR_LEN_T)CMSG_DATA(((struct cmsghdr *)NULL));
|
2004-01-29 04:39:19 +00:00
|
|
|
return (hdrlen + len);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline ISC_SOCKADDR_LEN_T
|
|
|
|
cmsg_space(ISC_SOCKADDR_LEN_T len) {
|
|
|
|
#ifdef CMSG_SPACE
|
|
|
|
return (CMSG_SPACE(len));
|
|
|
|
#else
|
|
|
|
struct msghdr msg;
|
|
|
|
struct cmsghdr *cmsgp;
|
|
|
|
/*
|
|
|
|
* XXX: The buffer length is an ad-hoc value, but should be enough
|
|
|
|
* in a practical sense.
|
|
|
|
*/
|
|
|
|
char dummybuf[sizeof(struct cmsghdr) + 1024];
|
|
|
|
|
|
|
|
memset(&msg, 0, sizeof(msg));
|
|
|
|
msg.msg_control = dummybuf;
|
|
|
|
msg.msg_controllen = sizeof(dummybuf);
|
|
|
|
|
|
|
|
cmsgp = (struct cmsghdr *)dummybuf;
|
|
|
|
cmsgp->cmsg_len = cmsg_len(len);
|
|
|
|
|
|
|
|
cmsgp = CMSG_NXTHDR(&msg, cmsgp);
|
|
|
|
if (cmsgp != NULL)
|
2004-03-18 02:58:08 +00:00
|
|
|
return ((char *)cmsgp - (char *)msg.msg_control);
|
2004-01-29 04:39:19 +00:00
|
|
|
else
|
|
|
|
return (0);
|
2008-01-18 23:46:58 +00:00
|
|
|
#endif
|
2004-01-29 04:39:19 +00:00
|
|
|
}
|
|
|
|
#endif /* USE_CMSG */
|
|
|
|
|
1999-08-28 04:26:49 +00:00
|
|
|
/*
|
|
|
|
* Process control messages received on a socket.
|
|
|
|
*/
|
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
process_cmsg(isc_socket_t *sock, struct msghdr *msg, isc_socketevent_t *dev) {
|
1999-12-04 01:27:44 +00:00
|
|
|
#ifdef USE_CMSG
|
1999-12-09 00:19:31 +00:00
|
|
|
struct cmsghdr *cmsgp;
|
2004-11-18 21:31:47 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
|
1999-12-04 01:27:44 +00:00
|
|
|
struct in6_pktinfo *pktinfop;
|
|
|
|
#endif
|
|
|
|
#ifdef SO_TIMESTAMP
|
|
|
|
struct timeval *timevalp;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2000-05-14 22:35:45 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-04-17 19:22:44 +00:00
|
|
|
UNUSED(sock);
|
2000-05-14 22:35:45 +00:00
|
|
|
UNUSED(msg);
|
|
|
|
UNUSED(dev);
|
|
|
|
|
2001-10-31 01:24:58 +00:00
|
|
|
#ifdef ISC_NET_BSD44MSGHDR
|
1999-08-31 20:57:46 +00:00
|
|
|
|
1999-08-28 19:30:23 +00:00
|
|
|
#ifdef MSG_TRUNC
|
1999-08-28 04:26:49 +00:00
|
|
|
if ((msg->msg_flags & MSG_TRUNC) == MSG_TRUNC)
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
|
1999-08-28 19:30:23 +00:00
|
|
|
#endif
|
1999-08-28 04:26:49 +00:00
|
|
|
|
1999-08-28 19:30:23 +00:00
|
|
|
#ifdef MSG_CTRUNC
|
1999-08-28 04:26:49 +00:00
|
|
|
if ((msg->msg_flags & MSG_CTRUNC) == MSG_CTRUNC)
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_CTRUNC;
|
1999-08-28 19:30:23 +00:00
|
|
|
#endif
|
1999-08-28 04:26:49 +00:00
|
|
|
|
1999-12-04 01:27:44 +00:00
|
|
|
#ifndef USE_CMSG
|
|
|
|
return;
|
|
|
|
#else
|
2004-03-18 02:58:08 +00:00
|
|
|
if (msg->msg_controllen == 0U || msg->msg_control == NULL)
|
1999-08-28 04:26:49 +00:00
|
|
|
return;
|
1999-12-04 01:27:44 +00:00
|
|
|
|
|
|
|
#ifdef SO_TIMESTAMP
|
|
|
|
timevalp = NULL;
|
|
|
|
#endif
|
2004-11-18 21:31:47 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
|
1999-12-04 01:27:44 +00:00
|
|
|
pktinfop = NULL;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
cmsgp = CMSG_FIRSTHDR(msg);
|
|
|
|
while (cmsgp != NULL) {
|
2000-12-06 00:30:32 +00:00
|
|
|
socket_log(sock, NULL, TRACE,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PROCESSCMSG,
|
|
|
|
"processing cmsg %p", cmsgp);
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2004-11-18 21:31:47 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
|
1999-12-09 00:19:31 +00:00
|
|
|
if (cmsgp->cmsg_level == IPPROTO_IPV6
|
|
|
|
&& cmsgp->cmsg_type == IPV6_PKTINFO) {
|
2000-05-23 02:20:23 +00:00
|
|
|
|
1999-12-04 01:27:44 +00:00
|
|
|
pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
|
2000-03-14 03:30:52 +00:00
|
|
|
memcpy(&dev->pktinfo, pktinfop,
|
|
|
|
sizeof(struct in6_pktinfo));
|
1999-12-04 01:27:44 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
|
2000-05-31 23:03:01 +00:00
|
|
|
socket_log(sock, NULL, TRACE,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_IFRECEIVED,
|
|
|
|
"interface received on ifindex %u",
|
2000-05-24 05:10:00 +00:00
|
|
|
dev->pktinfo.ipi6_ifindex);
|
2001-10-31 01:24:58 +00:00
|
|
|
if (IN6_IS_ADDR_MULTICAST(&pktinfop->ipi6_addr))
|
2008-01-18 23:46:58 +00:00
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_MULTICAST;
|
1999-12-04 01:27:44 +00:00
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SO_TIMESTAMP
|
1999-12-09 00:19:31 +00:00
|
|
|
if (cmsgp->cmsg_level == SOL_SOCKET
|
|
|
|
&& cmsgp->cmsg_type == SCM_TIMESTAMP) {
|
1999-12-04 01:27:44 +00:00
|
|
|
timevalp = (struct timeval *)CMSG_DATA(cmsgp);
|
|
|
|
dev->timestamp.seconds = timevalp->tv_sec;
|
|
|
|
dev->timestamp.nanoseconds = timevalp->tv_usec * 1000;
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_TIMESTAMP;
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
next:
|
|
|
|
cmsgp = CMSG_NXTHDR(msg, cmsgp);
|
|
|
|
}
|
|
|
|
#endif /* USE_CMSG */
|
|
|
|
|
1999-08-31 20:57:46 +00:00
|
|
|
#endif /* ISC_NET_BSD44MSGHDR */
|
1999-08-28 04:26:49 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 02:10:44 +00:00
|
|
|
/*
|
2001-01-25 22:25:10 +00:00
|
|
|
* 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).
|
1999-09-02 02:10:44 +00:00
|
|
|
*
|
|
|
|
* 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.
|
1999-09-02 23:34:09 +00:00
|
|
|
*
|
|
|
|
* If write_countp != NULL, *write_countp will hold the number of bytes
|
|
|
|
* this transaction can send.
|
1999-09-02 02:10:44 +00:00
|
|
|
*/
|
1999-09-03 01:18:45 +00:00
|
|
|
static void
|
1999-09-02 20:06:55 +00:00
|
|
|
build_msghdr_send(isc_socket_t *sock, isc_socketevent_t *dev,
|
2004-01-29 04:39:19 +00:00
|
|
|
struct msghdr *msg, struct iovec *iov, size_t *write_countp)
|
1999-09-02 02:10:44 +00:00
|
|
|
{
|
|
|
|
unsigned int iovcount;
|
|
|
|
isc_buffer_t *buffer;
|
|
|
|
isc_region_t used;
|
1999-09-02 20:06:55 +00:00
|
|
|
size_t write_count;
|
|
|
|
size_t skip_count;
|
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(msg, 0, sizeof(*msg));
|
1999-09-02 20:06:55 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (!sock->connected) {
|
1999-09-02 20:06:55 +00:00
|
|
|
msg->msg_name = (void *)&dev->address.type.sa;
|
|
|
|
msg->msg_namelen = dev->address.length;
|
1999-09-02 23:34:09 +00:00
|
|
|
} else {
|
|
|
|
msg->msg_name = NULL;
|
|
|
|
msg->msg_namelen = 0;
|
1999-09-02 20:06:55 +00:00
|
|
|
}
|
1999-09-02 02:10:44 +00:00
|
|
|
|
|
|
|
buffer = ISC_LIST_HEAD(dev->bufferlist);
|
1999-09-02 20:06:55 +00:00
|
|
|
write_count = 0;
|
1999-09-02 23:34:09 +00:00
|
|
|
iovcount = 0;
|
1999-09-02 02:10:44 +00:00
|
|
|
|
1999-09-02 18:56:11 +00:00
|
|
|
/*
|
1999-09-02 20:06:55 +00:00
|
|
|
* Single buffer I/O? Skip what we've done so far in this region.
|
1999-09-02 18:56:11 +00:00
|
|
|
*/
|
|
|
|
if (buffer == NULL) {
|
1999-09-02 20:06:55 +00:00
|
|
|
write_count = dev->region.length - dev->n;
|
|
|
|
iov[0].iov_base = (void *)(dev->region.base + dev->n);
|
|
|
|
iov[0].iov_len = write_count;
|
1999-09-02 23:34:09 +00:00
|
|
|
iovcount = 1;
|
1999-09-02 02:10:44 +00:00
|
|
|
|
1999-09-02 20:06:55 +00:00
|
|
|
goto config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multibuffer I/O.
|
|
|
|
* Skip the data in the buffer list that we have already written.
|
|
|
|
*/
|
1999-09-02 23:34:09 +00:00
|
|
|
skip_count = dev->n;
|
1999-09-02 20:06:55 +00:00
|
|
|
while (buffer != NULL) {
|
1999-09-10 21:13:39 +00:00
|
|
|
REQUIRE(ISC_BUFFER_VALID(buffer));
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
if (skip_count < isc_buffer_usedlength(buffer))
|
1999-09-02 20:06:55 +00:00
|
|
|
break;
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
skip_count -= isc_buffer_usedlength(buffer);
|
1999-09-02 20:06:55 +00:00
|
|
|
buffer = ISC_LIST_NEXT(buffer, link);
|
1999-09-02 18:56:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
while (buffer != NULL) {
|
2000-06-20 17:24:47 +00:00
|
|
|
INSIST(iovcount < MAXSCATTERGATHER_SEND);
|
1999-09-02 02:10:44 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_usedregion(buffer, &used);
|
1999-09-02 20:06:55 +00:00
|
|
|
|
1999-09-02 02:10:44 +00:00
|
|
|
if (used.length > 0) {
|
1999-09-02 20:06:55 +00:00
|
|
|
iov[iovcount].iov_base = (void *)(used.base
|
|
|
|
+ skip_count);
|
|
|
|
iov[iovcount].iov_len = used.length - skip_count;
|
|
|
|
write_count += (used.length - skip_count);
|
|
|
|
skip_count = 0;
|
1999-09-02 02:10:44 +00:00
|
|
|
iovcount++;
|
|
|
|
}
|
|
|
|
buffer = ISC_LIST_NEXT(buffer, link);
|
|
|
|
}
|
|
|
|
|
2003-07-25 00:01:16 +00:00
|
|
|
INSIST(skip_count == 0U);
|
1999-09-02 23:34:09 +00:00
|
|
|
|
|
|
|
config:
|
1999-09-02 02:10:44 +00:00
|
|
|
msg->msg_iov = iov;
|
|
|
|
msg->msg_iovlen = iovcount;
|
|
|
|
|
1999-09-02 20:06:55 +00:00
|
|
|
#ifdef ISC_NET_BSD44MSGHDR
|
|
|
|
msg->msg_control = NULL;
|
|
|
|
msg->msg_controllen = 0;
|
|
|
|
msg->msg_flags = 0;
|
2004-11-18 21:31:47 +00:00
|
|
|
#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
|
1999-12-15 20:47:38 +00:00
|
|
|
if ((sock->type == isc_sockettype_udp)
|
|
|
|
&& ((dev->attributes & ISC_SOCKEVENTATTR_PKTINFO) != 0)) {
|
|
|
|
struct cmsghdr *cmsgp;
|
2000-05-23 02:20:23 +00:00
|
|
|
struct in6_pktinfo *pktinfop;
|
|
|
|
|
2000-05-31 23:03:01 +00:00
|
|
|
socket_log(sock, NULL, TRACE,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_SENDTODATA,
|
|
|
|
"sendto pktinfo data, ifindex %u",
|
2000-05-24 05:10:00 +00:00
|
|
|
dev->pktinfo.ipi6_ifindex);
|
2000-05-23 02:20:23 +00:00
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
msg->msg_controllen = cmsg_space(sizeof(struct in6_pktinfo));
|
|
|
|
INSIST(msg->msg_controllen <= sock->sendcmsgbuflen);
|
|
|
|
msg->msg_control = (void *)sock->sendcmsgbuf;
|
1999-12-15 20:47:38 +00:00
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
cmsgp = (struct cmsghdr *)sock->sendcmsgbuf;
|
1999-12-15 20:47:38 +00:00
|
|
|
cmsgp->cmsg_level = IPPROTO_IPV6;
|
|
|
|
cmsgp->cmsg_type = IPV6_PKTINFO;
|
2004-01-29 04:39:19 +00:00
|
|
|
cmsgp->cmsg_len = cmsg_len(sizeof(struct in6_pktinfo));
|
1999-12-15 20:47:38 +00:00
|
|
|
pktinfop = (struct in6_pktinfo *)CMSG_DATA(cmsgp);
|
2000-03-14 03:30:52 +00:00
|
|
|
memcpy(pktinfop, &dev->pktinfo, sizeof(struct in6_pktinfo));
|
1999-12-15 20:47:38 +00:00
|
|
|
}
|
2001-08-16 07:27:48 +00:00
|
|
|
#endif /* USE_CMSG && ISC_PLATFORM_HAVEIPV6 */
|
1999-12-15 20:47:38 +00:00
|
|
|
#else /* ISC_NET_BSD44MSGHDR */
|
1999-09-02 20:06:55 +00:00
|
|
|
msg->msg_accrights = NULL;
|
|
|
|
msg->msg_accrightslen = 0;
|
1999-12-15 20:47:38 +00:00
|
|
|
#endif /* ISC_NET_BSD44MSGHDR */
|
1999-09-02 20:06:55 +00:00
|
|
|
|
|
|
|
if (write_countp != NULL)
|
|
|
|
*write_countp = write_count;
|
1999-09-02 02:10:44 +00:00
|
|
|
}
|
|
|
|
|
1999-09-02 23:34:09 +00:00
|
|
|
/*
|
2001-01-25 22:25:10 +00:00
|
|
|
* 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).
|
1999-09-02 23:34:09 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
1999-09-03 01:18:45 +00:00
|
|
|
static void
|
1999-09-02 23:34:09 +00:00
|
|
|
build_msghdr_recv(isc_socket_t *sock, isc_socketevent_t *dev,
|
2004-01-29 04:39:19 +00:00
|
|
|
struct msghdr *msg, struct iovec *iov, size_t *read_countp)
|
1999-09-02 23:34:09 +00:00
|
|
|
{
|
|
|
|
unsigned int iovcount;
|
|
|
|
isc_buffer_t *buffer;
|
|
|
|
isc_region_t available;
|
|
|
|
size_t read_count;
|
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
memset(msg, 0, sizeof(struct msghdr));
|
1999-09-02 23:34:09 +00:00
|
|
|
|
|
|
|
if (sock->type == isc_sockettype_udp) {
|
|
|
|
memset(&dev->address, 0, sizeof(dev->address));
|
2006-02-02 23:07:53 +00:00
|
|
|
#ifdef BROKEN_RECVMSG
|
|
|
|
if (sock->pf == AF_INET) {
|
|
|
|
msg->msg_name = (void *)&dev->address.type.sin;
|
|
|
|
msg->msg_namelen = sizeof(dev->address.type.sin6);
|
|
|
|
} else if (sock->pf == AF_INET6) {
|
|
|
|
msg->msg_name = (void *)&dev->address.type.sin6;
|
|
|
|
msg->msg_namelen = sizeof(dev->address.type.sin6);
|
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
} else if (sock->pf == AF_UNIX) {
|
|
|
|
msg->msg_name = (void *)&dev->address.type.sunix;
|
|
|
|
msg->msg_namelen = sizeof(dev->address.type.sunix);
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
msg->msg_name = (void *)&dev->address.type.sa;
|
|
|
|
msg->msg_namelen = sizeof(dev->address.type);
|
|
|
|
}
|
|
|
|
#else
|
1999-09-02 23:34:09 +00:00
|
|
|
msg->msg_name = (void *)&dev->address.type.sa;
|
1999-11-01 22:51:19 +00:00
|
|
|
msg->msg_namelen = sizeof(dev->address.type);
|
2006-02-02 23:07:53 +00:00
|
|
|
#endif
|
1999-09-13 22:00:28 +00:00
|
|
|
#ifdef ISC_NET_RECVOVERFLOW
|
|
|
|
/* If needed, steal one iovec for overflow detection. */
|
|
|
|
maxiov--;
|
|
|
|
#endif
|
|
|
|
} else { /* TCP */
|
1999-09-02 23:34:09 +00:00
|
|
|
msg->msg_name = NULL;
|
|
|
|
msg->msg_namelen = 0;
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = sock->peer_address;
|
1999-09-02 23:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer = ISC_LIST_HEAD(dev->bufferlist);
|
|
|
|
read_count = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Single buffer I/O? Skip what we've done so far in this region.
|
|
|
|
*/
|
|
|
|
if (buffer == NULL) {
|
|
|
|
read_count = dev->region.length - dev->n;
|
|
|
|
iov[0].iov_base = (void *)(dev->region.base + dev->n);
|
|
|
|
iov[0].iov_len = read_count;
|
1999-09-13 22:00:28 +00:00
|
|
|
iovcount = 1;
|
1999-09-02 23:34:09 +00:00
|
|
|
|
|
|
|
goto config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Multibuffer I/O.
|
|
|
|
* Skip empty buffers.
|
|
|
|
*/
|
|
|
|
while (buffer != NULL) {
|
1999-09-10 21:13:39 +00:00
|
|
|
REQUIRE(ISC_BUFFER_VALID(buffer));
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
if (isc_buffer_availablelength(buffer) != 0)
|
1999-09-02 23:34:09 +00:00
|
|
|
break;
|
|
|
|
buffer = ISC_LIST_NEXT(buffer, link);
|
|
|
|
}
|
|
|
|
|
|
|
|
iovcount = 0;
|
|
|
|
while (buffer != NULL) {
|
2000-06-20 17:24:47 +00:00
|
|
|
INSIST(iovcount < MAXSCATTERGATHER_RECV);
|
1999-09-02 23:34:09 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(buffer, &available);
|
1999-09-02 23:34:09 +00:00
|
|
|
|
|
|
|
if (available.length > 0) {
|
|
|
|
iov[iovcount].iov_base = (void *)(available.base);
|
|
|
|
iov[iovcount].iov_len = available.length;
|
|
|
|
read_count += available.length;
|
|
|
|
iovcount++;
|
|
|
|
}
|
|
|
|
buffer = ISC_LIST_NEXT(buffer, link);
|
|
|
|
}
|
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
config:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
if (sock->type == isc_sockettype_udp) {
|
|
|
|
iov[iovcount].iov_base = (void *)(&sock->overflow);
|
|
|
|
iov[iovcount].iov_len = 1;
|
|
|
|
iovcount++;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-09-02 23:34:09 +00:00
|
|
|
msg->msg_iov = iov;
|
|
|
|
msg->msg_iovlen = iovcount;
|
|
|
|
|
|
|
|
#ifdef ISC_NET_BSD44MSGHDR
|
|
|
|
msg->msg_control = NULL;
|
|
|
|
msg->msg_controllen = 0;
|
1999-12-15 20:47:38 +00:00
|
|
|
msg->msg_flags = 0;
|
|
|
|
#if defined(USE_CMSG)
|
1999-12-04 01:27:44 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
2004-01-29 04:39:19 +00:00
|
|
|
msg->msg_control = sock->recvcmsgbuf;
|
|
|
|
msg->msg_controllen = sock->recvcmsgbuflen;
|
1999-12-04 01:27:44 +00:00
|
|
|
}
|
1999-12-15 20:47:38 +00:00
|
|
|
#endif /* USE_CMSG */
|
|
|
|
#else /* ISC_NET_BSD44MSGHDR */
|
1999-09-02 23:34:09 +00:00
|
|
|
msg->msg_accrights = NULL;
|
|
|
|
msg->msg_accrightslen = 0;
|
1999-12-15 20:47:38 +00:00
|
|
|
#endif /* ISC_NET_BSD44MSGHDR */
|
1999-09-02 23:34:09 +00:00
|
|
|
|
|
|
|
if (read_countp != NULL)
|
|
|
|
*read_countp = read_count;
|
|
|
|
}
|
|
|
|
|
1999-09-02 20:06:55 +00:00
|
|
|
static void
|
|
|
|
set_dev_address(isc_sockaddr_t *address, isc_socket_t *sock,
|
|
|
|
isc_socketevent_t *dev)
|
|
|
|
{
|
|
|
|
if (sock->type == isc_sockettype_udp) {
|
|
|
|
if (address != NULL)
|
|
|
|
dev->address = *address;
|
|
|
|
else
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = sock->peer_address;
|
1999-09-02 20:06:55 +00:00
|
|
|
} else if (sock->type == isc_sockettype_tcp) {
|
|
|
|
INSIST(address == NULL);
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = sock->peer_address;
|
1999-09-02 20:06:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-06 00:53:36 +00:00
|
|
|
static void
|
|
|
|
destroy_socketevent(isc_event_t *event) {
|
|
|
|
isc_socketevent_t *ev = (isc_socketevent_t *)event;
|
|
|
|
|
|
|
|
INSIST(ISC_LIST_EMPTY(ev->bufferlist));
|
|
|
|
|
|
|
|
(ev->destroy)(event);
|
|
|
|
}
|
|
|
|
|
1999-09-02 18:56:11 +00:00
|
|
|
static isc_socketevent_t *
|
|
|
|
allocate_socketevent(isc_socket_t *sock, isc_eventtype_t eventtype,
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_taskaction_t action, const void *arg)
|
1999-09-02 18:56:11 +00:00
|
|
|
{
|
|
|
|
isc_socketevent_t *ev;
|
|
|
|
|
|
|
|
ev = (isc_socketevent_t *)isc_event_allocate(sock->manager->mctx,
|
|
|
|
sock, eventtype,
|
|
|
|
action, arg,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*ev));
|
1999-09-02 18:56:11 +00:00
|
|
|
|
|
|
|
if (ev == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
ev->result = ISC_R_UNEXPECTED;
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LINK_INIT(ev, ev_link);
|
1999-09-02 18:56:11 +00:00
|
|
|
ISC_LIST_INIT(ev->bufferlist);
|
|
|
|
ev->region.base = NULL;
|
1999-09-02 23:34:09 +00:00
|
|
|
ev->n = 0;
|
|
|
|
ev->offset = 0;
|
2000-04-17 19:22:44 +00:00
|
|
|
ev->attributes = 0;
|
2006-06-06 00:53:36 +00:00
|
|
|
ev->destroy = ev->ev_destroy;
|
|
|
|
ev->ev_destroy = destroy_socketevent;
|
1999-09-02 18:56:11 +00:00
|
|
|
|
|
|
|
return (ev);
|
|
|
|
}
|
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
dump_msg(struct msghdr *msg) {
|
1999-09-13 22:00:28 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
printf("MSGHDR %p\n", msg);
|
|
|
|
printf("\tname %p, namelen %d\n", msg->msg_name, msg->msg_namelen);
|
|
|
|
printf("\tiov %p, iovlen %d\n", msg->msg_iov, msg->msg_iovlen);
|
2001-11-27 00:56:32 +00:00
|
|
|
for (i = 0; i < (unsigned int)msg->msg_iovlen; i++)
|
1999-09-13 22:00:28 +00:00
|
|
|
printf("\t\t%d\tbase %p, len %d\n", i,
|
|
|
|
msg->msg_iov[i].iov_base,
|
|
|
|
msg->msg_iov[i].iov_len);
|
1999-12-04 01:27:44 +00:00
|
|
|
#ifdef ISC_NET_BSD44MSGHDR
|
|
|
|
printf("\tcontrol %p, controllen %d\n", msg->msg_control,
|
|
|
|
msg->msg_controllen);
|
|
|
|
#endif
|
1999-09-13 22:00:28 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-09-03 01:18:45 +00:00
|
|
|
#define DOIO_SUCCESS 0 /* i/o ok, event sent */
|
|
|
|
#define DOIO_SOFT 1 /* i/o ok, soft error, no event sent */
|
|
|
|
#define DOIO_HARD 2 /* i/o error, event sent */
|
|
|
|
#define DOIO_EOF 3 /* EOF, no event sent */
|
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
static int
|
2000-05-14 22:35:45 +00:00
|
|
|
doio_recv(isc_socket_t *sock, isc_socketevent_t *dev) {
|
1999-09-03 00:22:28 +00:00
|
|
|
int cc;
|
1999-09-13 22:00:28 +00:00
|
|
|
struct iovec iov[MAXSCATTERGATHER_RECV];
|
1999-09-03 00:22:28 +00:00
|
|
|
size_t read_count;
|
1999-09-03 16:38:08 +00:00
|
|
|
size_t actual_count;
|
1999-09-03 00:22:28 +00:00
|
|
|
struct msghdr msghdr;
|
1999-09-03 16:38:08 +00:00
|
|
|
isc_buffer_t *buffer;
|
2001-05-14 18:20:03 +00:00
|
|
|
int recv_errno;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
build_msghdr_recv(sock, dev, &msghdr, iov, &read_count);
|
1999-09-13 22:00:28 +00:00
|
|
|
|
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
|
|
|
dump_msg(&msghdr);
|
|
|
|
#endif
|
1999-09-03 00:22:28 +00:00
|
|
|
|
|
|
|
cc = recvmsg(sock->fd, &msghdr, 0);
|
2001-05-14 18:20:03 +00:00
|
|
|
recv_errno = errno;
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2006-02-02 23:07:53 +00:00
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
|
|
|
dump_msg(&msghdr);
|
|
|
|
#endif
|
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
if (cc < 0) {
|
2001-05-14 18:20:03 +00:00
|
|
|
if (SOFT_ERROR(recv_errno))
|
1999-09-03 00:22:28 +00:00
|
|
|
return (DOIO_SOFT);
|
|
|
|
|
2001-10-22 20:57:41 +00:00
|
|
|
if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(recv_errno, strbuf, sizeof(strbuf));
|
2000-07-13 01:13:53 +00:00
|
|
|
socket_log(sock, NULL, IOEVENT,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET,
|
2008-01-18 23:46:58 +00:00
|
|
|
ISC_MSG_DOIORECV,
|
2000-12-19 20:35:37 +00:00
|
|
|
"doio_recv: recvmsg(%d) %d bytes, err %d/%s",
|
2001-08-31 05:57:58 +00:00
|
|
|
sock->fd, cc, recv_errno, strbuf);
|
2001-10-22 20:57:41 +00:00
|
|
|
}
|
1999-12-06 23:07:05 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
2001-05-14 18:20:03 +00:00
|
|
|
if (recv_errno == _system) { \
|
1999-09-03 00:22:28 +00:00
|
|
|
if (sock->connected) { \
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = _isc; \
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_HARD); \
|
1999-09-03 00:22:28 +00:00
|
|
|
} \
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_SOFT); \
|
1999-09-03 00:22:28 +00:00
|
|
|
}
|
2000-04-14 15:38:28 +00:00
|
|
|
#define ALWAYS_HARD(_system, _isc) \
|
2001-05-14 18:20:03 +00:00
|
|
|
if (recv_errno == _system) { \
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = _isc; \
|
2000-04-14 15:38:28 +00:00
|
|
|
return (DOIO_HARD); \
|
|
|
|
}
|
1999-09-03 00:22:28 +00:00
|
|
|
|
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
2000-09-06 22:55:33 +00:00
|
|
|
SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
2001-09-18 22:54:02 +00:00
|
|
|
SOFT_OR_HARD(EHOSTDOWN, ISC_R_HOSTDOWN);
|
2002-03-15 04:59:03 +00:00
|
|
|
/* HPUX 11.11 can return EADDRNOTAVAIL. */
|
|
|
|
SOFT_OR_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
|
2000-04-14 15:38:28 +00:00
|
|
|
ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
|
2008-07-18 02:43:31 +00:00
|
|
|
/*
|
|
|
|
* HPUX returns EPROTO and EINVAL on receiving some ICMP/ICMPv6
|
|
|
|
* errors.
|
|
|
|
*/
|
|
|
|
SOFT_OR_HARD(EPROTO, ISC_R_HOSTUNREACH);
|
|
|
|
SOFT_OR_HARD(EINVAL, ISC_R_HOSTUNREACH);
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2000-04-14 15:38:28 +00:00
|
|
|
#undef SOFT_OR_HARD
|
|
|
|
#undef ALWAYS_HARD
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2001-05-14 23:58:33 +00:00
|
|
|
dev->result = isc__errno2result(recv_errno);
|
2001-02-12 21:43:17 +00:00
|
|
|
return (DOIO_HARD);
|
1999-09-03 00:22:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* On TCP, zero length reads indicate EOF, while on
|
|
|
|
* UDP, zero length reads are perfectly valid, although
|
|
|
|
* strange.
|
|
|
|
*/
|
2000-09-07 01:59:57 +00:00
|
|
|
if ((sock->type == isc_sockettype_tcp) && (cc == 0))
|
1999-09-03 00:22:28 +00:00
|
|
|
return (DOIO_EOF);
|
|
|
|
|
2002-04-02 04:36:52 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
1999-12-04 01:27:44 +00:00
|
|
|
dev->address.length = msghdr.msg_namelen;
|
2002-04-02 04:36:52 +00:00
|
|
|
if (isc_sockaddr_getport(&dev->address) == 0) {
|
|
|
|
if (isc_log_wouldlog(isc_lctx, IOEVENT_LEVEL)) {
|
|
|
|
socket_log(sock, &dev->address, IOEVENT,
|
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET,
|
2008-01-18 23:46:58 +00:00
|
|
|
ISC_MSG_ZEROPORT,
|
2002-04-02 04:36:52 +00:00
|
|
|
"dropping source port zero packet");
|
|
|
|
}
|
|
|
|
return (DOIO_SOFT);
|
|
|
|
}
|
|
|
|
}
|
1999-12-04 01:27:44 +00:00
|
|
|
|
2000-12-06 00:30:32 +00:00
|
|
|
socket_log(sock, &dev->address, IOEVENT,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_PKTRECV,
|
|
|
|
"packet received correctly");
|
2000-05-03 21:09:49 +00:00
|
|
|
|
1999-09-13 22:00:28 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
if ((sock->type == isc_sockettype_udp) && ((size_t)cc > read_count)) {
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_TRUNC;
|
|
|
|
cc--;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
/*
|
|
|
|
* If there are control messages attached, run through them and pull
|
|
|
|
* out the interesting bits.
|
|
|
|
*/
|
1999-12-04 01:27:44 +00:00
|
|
|
if (sock->type == isc_sockettype_udp)
|
|
|
|
process_cmsg(sock, &msghdr, dev);
|
1999-09-03 00:22:28 +00:00
|
|
|
|
1999-09-03 16:38:08 +00:00
|
|
|
/*
|
|
|
|
* update the buffers (if any) and the i/o count
|
|
|
|
*/
|
|
|
|
dev->n += cc;
|
|
|
|
actual_count = cc;
|
|
|
|
buffer = ISC_LIST_HEAD(dev->bufferlist);
|
2003-07-25 00:01:16 +00:00
|
|
|
while (buffer != NULL && actual_count > 0U) {
|
1999-09-10 21:13:39 +00:00
|
|
|
REQUIRE(ISC_BUFFER_VALID(buffer));
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
if (isc_buffer_availablelength(buffer) <= actual_count) {
|
|
|
|
actual_count -= isc_buffer_availablelength(buffer);
|
1999-09-10 21:13:39 +00:00
|
|
|
isc_buffer_add(buffer,
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availablelength(buffer));
|
1999-09-03 16:38:08 +00:00
|
|
|
} else {
|
|
|
|
isc_buffer_add(buffer, actual_count);
|
|
|
|
actual_count = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer = ISC_LIST_NEXT(buffer, link);
|
|
|
|
if (buffer == NULL) {
|
2003-07-25 00:01:16 +00:00
|
|
|
INSIST(actual_count == 0U);
|
1999-09-03 16:38:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
/*
|
|
|
|
* If we read less than we expected, update counters,
|
|
|
|
* and let the upper layer poke the descriptor.
|
|
|
|
*/
|
|
|
|
if (((size_t)cc != read_count) && (dev->n < dev->minimum))
|
|
|
|
return (DOIO_SOFT);
|
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Full reads are posted, or partials if partials are ok.
|
1999-09-03 00:22:28 +00:00
|
|
|
*/
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-06-26 22:59:22 +00:00
|
|
|
/*
|
|
|
|
* Returns:
|
2001-02-12 21:43:17 +00:00
|
|
|
* DOIO_SUCCESS The operation succeeded. dev->result contains
|
|
|
|
* ISC_R_SUCCESS.
|
2000-06-26 22:59:22 +00:00
|
|
|
*
|
|
|
|
* DOIO_HARD A hard or unexpected I/O error was encountered.
|
2001-02-12 21:43:17 +00:00
|
|
|
* dev->result contains the appropriate error.
|
2000-06-26 22:59:22 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
1999-09-03 01:18:45 +00:00
|
|
|
static int
|
2000-05-14 22:35:45 +00:00
|
|
|
doio_send(isc_socket_t *sock, isc_socketevent_t *dev) {
|
1999-09-03 01:18:45 +00:00
|
|
|
int cc;
|
1999-09-13 22:00:28 +00:00
|
|
|
struct iovec iov[MAXSCATTERGATHER_SEND];
|
1999-09-03 01:18:45 +00:00
|
|
|
size_t write_count;
|
|
|
|
struct msghdr msghdr;
|
2000-08-31 13:38:55 +00:00
|
|
|
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
|
2001-03-06 01:23:03 +00:00
|
|
|
int attempts = 0;
|
2001-06-07 00:21:53 +00:00
|
|
|
int send_errno;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
build_msghdr_send(sock, dev, &msghdr, iov, &write_count);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
resend:
|
1999-09-03 01:18:45 +00:00
|
|
|
cc = sendmsg(sock->fd, &msghdr, 0);
|
2001-06-07 00:21:53 +00:00
|
|
|
send_errno = errno;
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Check for error or block condition.
|
1999-09-03 01:18:45 +00:00
|
|
|
*/
|
|
|
|
if (cc < 0) {
|
2001-06-07 00:21:53 +00:00
|
|
|
if (send_errno == EINTR && ++attempts < NRETRIES)
|
2001-03-06 01:23:03 +00:00
|
|
|
goto resend;
|
|
|
|
|
2001-06-07 00:21:53 +00:00
|
|
|
if (SOFT_ERROR(send_errno))
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_SOFT);
|
|
|
|
|
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
2001-06-07 00:21:53 +00:00
|
|
|
if (send_errno == _system) { \
|
1999-09-03 01:18:45 +00:00
|
|
|
if (sock->connected) { \
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = _isc; \
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_HARD); \
|
|
|
|
} \
|
|
|
|
return (DOIO_SOFT); \
|
|
|
|
}
|
2000-04-14 15:38:28 +00:00
|
|
|
#define ALWAYS_HARD(_system, _isc) \
|
2001-06-07 00:21:53 +00:00
|
|
|
if (send_errno == _system) { \
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = _isc; \
|
2000-04-14 15:38:28 +00:00
|
|
|
return (DOIO_HARD); \
|
|
|
|
}
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-09-21 21:30:27 +00:00
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
2001-02-24 23:51:09 +00:00
|
|
|
ALWAYS_HARD(EACCES, ISC_R_NOPERM);
|
|
|
|
ALWAYS_HARD(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
|
2000-09-21 21:30:27 +00:00
|
|
|
ALWAYS_HARD(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
|
2000-06-26 20:16:01 +00:00
|
|
|
ALWAYS_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
2000-11-17 21:04:12 +00:00
|
|
|
#ifdef EHOSTDOWN
|
|
|
|
ALWAYS_HARD(EHOSTDOWN, ISC_R_HOSTUNREACH);
|
|
|
|
#endif
|
2000-09-21 21:30:27 +00:00
|
|
|
ALWAYS_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
2000-04-14 15:38:28 +00:00
|
|
|
ALWAYS_HARD(ENOBUFS, ISC_R_NORESOURCES);
|
2000-08-10 21:47:46 +00:00
|
|
|
ALWAYS_HARD(EPERM, ISC_R_HOSTUNREACH);
|
2000-09-07 01:59:57 +00:00
|
|
|
ALWAYS_HARD(EPIPE, ISC_R_NOTCONNECTED);
|
2002-06-26 00:12:54 +00:00
|
|
|
ALWAYS_HARD(ECONNRESET, ISC_R_CONNECTIONRESET);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-04-14 15:38:28 +00:00
|
|
|
#undef SOFT_OR_HARD
|
|
|
|
#undef ALWAYS_HARD
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2000-08-31 13:38:55 +00:00
|
|
|
isc_sockaddr_format(&dev->address, addrbuf, sizeof(addrbuf));
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(send_errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "internal_send: %s: %s",
|
|
|
|
addrbuf, strbuf);
|
2001-06-07 00:21:53 +00:00
|
|
|
dev->result = isc__errno2result(send_errno);
|
1999-09-03 01:18:45 +00:00
|
|
|
return (DOIO_HARD);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cc == 0)
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"internal_send: send() %s 0",
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_RETURNED, "returned"));
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* If we write less than we expected, update counters, poke.
|
1999-09-03 01:18:45 +00:00
|
|
|
*/
|
|
|
|
dev->n += cc;
|
|
|
|
if ((size_t)cc != write_count)
|
|
|
|
return (DOIO_SOFT);
|
1999-09-03 00:22:28 +00:00
|
|
|
|
1999-09-03 01:18:45 +00:00
|
|
|
/*
|
|
|
|
* Exactly what we wanted to write. We're done with this
|
|
|
|
* entry. Post its completion event.
|
|
|
|
*/
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
1999-09-03 00:22:28 +00:00
|
|
|
return (DOIO_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Kill.
|
|
|
|
*
|
1999-07-28 01:07:21 +00:00
|
|
|
* Caller must ensure that the socket is not locked and no external
|
|
|
|
* references exist.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
static void
|
|
|
|
closesocket(isc_socketmgr_t *manager, isc_sockettype_t type, int fd) {
|
|
|
|
int lockid = FDLOCK_ID(fd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* No one has this socket open, so the watcher doesn't have to be
|
|
|
|
* poked, and the socket doesn't have to be locked.
|
|
|
|
*/
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
|
|
|
manager->fds[fd] = NULL;
|
|
|
|
if (type == isc_sockettype_fdwatch)
|
|
|
|
manager->fdstate[fd] = MANAGER_CLOSE_PENDING;
|
|
|
|
else
|
|
|
|
manager->fdstate[fd] = CLOSE_PENDING;
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
select_poke(manager, fd, SELECT_POKE_CLOSE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* update manager->maxfd here (XXX: this should be implemented more
|
|
|
|
* efficiently)
|
|
|
|
*/
|
|
|
|
#ifdef USE_SELECT
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
if (manager->maxfd == fd) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
manager->maxfd = 0;
|
|
|
|
for (i = fd - 1; i >= 0; i--) {
|
|
|
|
lockid = FDLOCK_ID(i);
|
|
|
|
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
|
|
|
if (manager->fdstate[i] == MANAGED) {
|
|
|
|
manager->maxfd = i;
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
}
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
if (manager->maxfd < manager->pipe_fds[0])
|
|
|
|
manager->maxfd = manager->pipe_fds[0];
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
#endif /* USE_SELECT */
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
destroy(isc_socket_t **sockp) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int fd;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock = *sockp;
|
|
|
|
isc_socketmgr_t *manager = sock->manager;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-12-19 20:35:37 +00:00
|
|
|
socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_DESTROYING, "destroying");
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->accept_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->recv_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->send_list));
|
|
|
|
INSIST(sock->connect_ev == NULL);
|
2008-06-23 19:41:20 +00:00
|
|
|
REQUIRE(sock->fd == -1 || sock->fd < (int)manager->maxsocks);
|
|
|
|
|
|
|
|
if (sock->fd >= 0) {
|
|
|
|
fd = sock->fd;
|
|
|
|
sock->fd = -1;
|
|
|
|
closesocket(manager, sock->type, fd);
|
|
|
|
}
|
1999-07-28 01:07:21 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST_UNLINK(manager->socklist, sock, link);
|
2000-05-03 21:09:49 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
2000-06-02 01:27:27 +00:00
|
|
|
if (ISC_LIST_EMPTY(manager->socklist))
|
1999-05-17 22:31:26 +00:00
|
|
|
SIGNAL(&manager->shutdown_ok);
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
free_socket(sockp);
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
allocate_socket(isc_socketmgr_t *manager, isc_sockettype_t type,
|
|
|
|
isc_socket_t **socketp)
|
1998-11-10 01:56:44 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock;
|
2005-07-12 01:00:20 +00:00
|
|
|
isc_result_t result;
|
2004-01-29 04:39:19 +00:00
|
|
|
ISC_SOCKADDR_LEN_T cmsgbuflen;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
sock = isc_mem_get(manager->mctx, sizeof(*sock));
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
if (sock == NULL)
|
1998-12-01 17:58:34 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
sock->magic = 0;
|
1998-11-26 00:29:12 +00:00
|
|
|
sock->references = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
sock->manager = manager;
|
|
|
|
sock->type = type;
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->fd = -1;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LINK_INIT(sock, link);
|
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
sock->recvcmsgbuf = NULL;
|
|
|
|
sock->sendcmsgbuf = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up cmsg buffers
|
|
|
|
*/
|
|
|
|
cmsgbuflen = 0;
|
2004-11-18 21:31:47 +00:00
|
|
|
#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
|
2004-01-29 04:39:19 +00:00
|
|
|
cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
|
|
|
|
#endif
|
|
|
|
#if defined(USE_CMSG) && defined(SO_TIMESTAMP)
|
|
|
|
cmsgbuflen += cmsg_space(sizeof(struct timeval));
|
|
|
|
#endif
|
|
|
|
sock->recvcmsgbuflen = cmsgbuflen;
|
2005-08-25 03:26:54 +00:00
|
|
|
if (sock->recvcmsgbuflen != 0U) {
|
2004-01-29 04:39:19 +00:00
|
|
|
sock->recvcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
|
|
|
|
if (sock->recvcmsgbuf == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsgbuflen = 0;
|
2004-11-18 21:31:47 +00:00
|
|
|
#if defined(USE_CMSG) && defined(ISC_PLATFORM_HAVEIN6PKTINFO)
|
2004-01-29 04:39:19 +00:00
|
|
|
cmsgbuflen = cmsg_space(sizeof(struct in6_pktinfo));
|
|
|
|
#endif
|
|
|
|
sock->sendcmsgbuflen = cmsgbuflen;
|
2005-08-25 03:26:54 +00:00
|
|
|
if (sock->sendcmsgbuflen != 0U) {
|
2004-01-29 04:39:19 +00:00
|
|
|
sock->sendcmsgbuf = isc_mem_get(manager->mctx, cmsgbuflen);
|
|
|
|
if (sock->sendcmsgbuf == NULL)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* set up list of readers and writers to be initially empty
|
|
|
|
*/
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_INIT(sock->recv_list);
|
|
|
|
ISC_LIST_INIT(sock->send_list);
|
1999-07-28 06:50:10 +00:00
|
|
|
ISC_LIST_INIT(sock->accept_list);
|
1998-12-18 04:03:11 +00:00
|
|
|
sock->connect_ev = NULL;
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->pending_recv = 0;
|
|
|
|
sock->pending_send = 0;
|
|
|
|
sock->pending_accept = 0;
|
|
|
|
sock->listener = 0;
|
|
|
|
sock->connected = 0;
|
|
|
|
sock->connecting = 0;
|
2000-05-10 21:17:49 +00:00
|
|
|
sock->bound = 0;
|
1999-06-10 00:45:30 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* initialize the lock
|
|
|
|
*/
|
2005-07-12 01:00:20 +00:00
|
|
|
result = isc_mutex_init(&sock->lock);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
1998-11-10 01:56:44 +00:00
|
|
|
sock->magic = 0;
|
2001-01-25 22:25:10 +00:00
|
|
|
goto error;
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
1999-07-28 01:07:21 +00:00
|
|
|
* Initialize readable and writable events
|
1999-06-10 00:45:30 +00:00
|
|
|
*/
|
|
|
|
ISC_EVENT_INIT(&sock->readable_ev, sizeof(intev_t),
|
1999-07-10 01:05:54 +00:00
|
|
|
ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTR,
|
1999-06-10 00:45:30 +00:00
|
|
|
NULL, sock, sock, NULL, NULL);
|
1999-07-28 01:07:21 +00:00
|
|
|
ISC_EVENT_INIT(&sock->writable_ev, sizeof(intev_t),
|
1999-07-28 21:30:37 +00:00
|
|
|
ISC_EVENTATTR_NOPURGE, NULL, ISC_SOCKEVENT_INTW,
|
1999-07-28 01:07:21 +00:00
|
|
|
NULL, sock, sock, NULL, NULL);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
sock->magic = SOCKET_MAGIC;
|
1998-11-10 01:56:44 +00:00
|
|
|
*socketp = sock;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
2004-01-26 23:33:32 +00:00
|
|
|
error:
|
2004-01-29 04:39:19 +00:00
|
|
|
if (sock->recvcmsgbuf != NULL)
|
|
|
|
isc_mem_put(manager->mctx, sock->recvcmsgbuf,
|
|
|
|
sock->recvcmsgbuflen);
|
|
|
|
if (sock->sendcmsgbuf != NULL)
|
|
|
|
isc_mem_put(manager->mctx, sock->sendcmsgbuf,
|
|
|
|
sock->sendcmsgbuflen);
|
2004-01-26 23:33:32 +00:00
|
|
|
isc_mem_put(manager->mctx, sock, sizeof(*sock));
|
1999-06-10 00:45:30 +00:00
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-05-14 22:35:45 +00:00
|
|
|
free_socket(isc_socket_t **socketp) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock = *socketp;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(sock->references == 0);
|
|
|
|
INSIST(VALID_SOCKET(sock));
|
|
|
|
INSIST(!sock->connecting);
|
|
|
|
INSIST(!sock->pending_recv);
|
|
|
|
INSIST(!sock->pending_send);
|
|
|
|
INSIST(!sock->pending_accept);
|
2000-06-02 01:27:27 +00:00
|
|
|
INSIST(ISC_LIST_EMPTY(sock->recv_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->send_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->accept_list));
|
2000-06-02 20:09:01 +00:00
|
|
|
INSIST(!ISC_LINK_LINKED(sock, link));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2004-01-29 04:39:19 +00:00
|
|
|
if (sock->recvcmsgbuf != NULL)
|
|
|
|
isc_mem_put(sock->manager->mctx, sock->recvcmsgbuf,
|
|
|
|
sock->recvcmsgbuflen);
|
|
|
|
if (sock->sendcmsgbuf != NULL)
|
|
|
|
isc_mem_put(sock->manager->mctx, sock->sendcmsgbuf,
|
|
|
|
sock->sendcmsgbuflen);
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
sock->magic = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2000-08-26 01:31:56 +00:00
|
|
|
DESTROYLOCK(&sock->lock);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(sock->manager->mctx, sock, sizeof(*sock));
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
*socketp = NULL;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2007-02-26 01:37:37 +00:00
|
|
|
#ifdef SO_BSDCOMPAT
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
2007-02-26 01:37:37 +00:00
|
|
|
* 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 isc_once_t bsdcompat_once = ISC_ONCE_INIT;
|
|
|
|
isc_boolean_t bsdcompat = ISC_TRUE;
|
|
|
|
|
|
|
|
static void
|
|
|
|
clear_bsdcompat(void) {
|
|
|
|
#ifdef __linux__
|
|
|
|
struct utsname buf;
|
|
|
|
char *endp;
|
|
|
|
long int major;
|
|
|
|
long int minor;
|
|
|
|
|
|
|
|
uname(&buf); /* Can only fail if buf is bad in Linux. */
|
|
|
|
|
|
|
|
/* Paranoia in parsing can be increased, but we trust uname(). */
|
|
|
|
major = strtol(buf.release, &endp, 10);
|
|
|
|
if (*endp == '.') {
|
|
|
|
minor = strtol(endp+1, &endp, 10);
|
|
|
|
if ((major > 2) || ((major == 2) && (minor >= 4))) {
|
|
|
|
bsdcompat = ISC_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* __linux __ */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
static isc_result_t
|
|
|
|
opensocket(isc_socketmgr_t *manager, isc_socket_t *sock) {
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
const char *err = "socket";
|
|
|
|
int tries = 0;
|
2000-06-26 18:20:25 +00:00
|
|
|
#if defined(USE_CMSG) || defined(SO_BSDCOMPAT)
|
1999-12-09 00:19:31 +00:00
|
|
|
int on = 1;
|
2005-07-12 00:41:55 +00:00
|
|
|
#endif
|
|
|
|
#if defined(SO_RCVBUF)
|
|
|
|
ISC_SOCKADDR_LEN_T optlen;
|
|
|
|
int size;
|
1999-12-09 00:19:31 +00:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2007-05-21 01:55:10 +00:00
|
|
|
again:
|
2008-06-23 19:41:20 +00:00
|
|
|
switch (sock->type) {
|
1999-07-13 01:49:33 +00:00
|
|
|
case isc_sockettype_udp:
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->fd = socket(sock->pf, SOCK_DGRAM, IPPROTO_UDP);
|
1998-11-03 00:54:47 +00:00
|
|
|
break;
|
1999-07-13 01:49:33 +00:00
|
|
|
case isc_sockettype_tcp:
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->fd = socket(sock->pf, SOCK_STREAM, IPPROTO_TCP);
|
1998-11-03 00:54:47 +00:00
|
|
|
break;
|
2005-02-23 01:09:23 +00:00
|
|
|
case isc_sockettype_unix:
|
2008-06-23 19:41:20 +00:00
|
|
|
sock->fd = socket(sock->pf, SOCK_STREAM, 0);
|
2005-02-23 01:09:23 +00:00
|
|
|
break;
|
2005-12-06 16:54:49 +00:00
|
|
|
case isc_sockettype_fdwatch:
|
2008-06-23 19:41:20 +00:00
|
|
|
INSIST(sock->type != isc_sockettype_fdwatch);
|
2005-12-06 16:54:49 +00:00
|
|
|
break;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
2008-01-27 01:58:19 +00:00
|
|
|
if (sock->fd == -1 && errno == EINTR && tries++ < 42)
|
2007-05-21 01:55:10 +00:00
|
|
|
goto again;
|
2001-04-26 23:45:55 +00:00
|
|
|
|
2003-02-26 04:00:20 +00:00
|
|
|
#ifdef F_DUPFD
|
2005-05-19 02:42:42 +00:00
|
|
|
/*
|
|
|
|
* Leave a space for stdio to work in.
|
|
|
|
*/
|
|
|
|
if (sock->fd >= 0 && sock->fd < 20) {
|
|
|
|
int new, tmp;
|
|
|
|
new = fcntl(sock->fd, F_DUPFD, 20);
|
|
|
|
tmp = errno;
|
|
|
|
(void)close(sock->fd);
|
|
|
|
errno = tmp;
|
|
|
|
sock->fd = new;
|
|
|
|
err = "isc_socket_create: fcntl";
|
|
|
|
}
|
2003-02-26 04:00:20 +00:00
|
|
|
#endif
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->fd >= (int)manager->maxsocks) {
|
2001-04-26 23:45:55 +00:00
|
|
|
(void)close(sock->fd);
|
|
|
|
isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
2005-05-19 02:42:42 +00:00
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
2001-07-09 21:06:30 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_TOOMANYFDS,
|
2001-04-26 23:45:55 +00:00
|
|
|
"%s: too many open file descriptors", "socket");
|
|
|
|
return (ISC_R_NORESOURCES);
|
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
if (sock->fd < 0) {
|
|
|
|
switch (errno) {
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
|
|
|
case ENOBUFS:
|
|
|
|
return (ISC_R_NORESOURCES);
|
2000-08-15 01:43:38 +00:00
|
|
|
|
|
|
|
case EPROTONOSUPPORT:
|
|
|
|
case EPFNOSUPPORT:
|
|
|
|
case EAFNOSUPPORT:
|
2000-12-06 01:53:38 +00:00
|
|
|
/*
|
|
|
|
* Linux 2.2 (and maybe others) return EINVAL instead of
|
|
|
|
* EAFNOSUPPORT.
|
|
|
|
*/
|
|
|
|
case EINVAL:
|
2000-08-15 01:43:38 +00:00
|
|
|
return (ISC_R_FAMILYNOSUPPORT);
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
default:
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1998-11-03 00:54:47 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2005-05-19 02:42:42 +00:00
|
|
|
"%s() %s: %s", err,
|
2000-12-06 00:30:32 +00:00
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
1998-11-03 00:54:47 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
if (make_nonblock(sock->fd) != ISC_R_SUCCESS) {
|
2001-11-09 00:37:10 +00:00
|
|
|
(void)close(sock->fd);
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2000-06-26 17:48:26 +00:00
|
|
|
#ifdef SO_BSDCOMPAT
|
2007-02-26 01:37:37 +00:00
|
|
|
RUNTIME_CHECK(isc_once_do(&bsdcompat_once,
|
|
|
|
clear_bsdcompat) == ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->type != isc_sockettype_unix && bsdcompat &&
|
2005-02-23 01:09:23 +00:00
|
|
|
setsockopt(sock->fd, SOL_SOCKET, SO_BSDCOMPAT,
|
2001-11-27 01:56:32 +00:00
|
|
|
(void *)&on, sizeof(on)) < 0) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2000-06-26 17:48:26 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"setsockopt(%d, SO_BSDCOMPAT) %s: %s",
|
|
|
|
sock->fd,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
2000-06-26 17:48:26 +00:00
|
|
|
/* Press on... */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-12-14 03:52:40 +00:00
|
|
|
#ifdef SO_NOSIGPIPE
|
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_NOSIGPIPE,
|
|
|
|
(void *)&on, sizeof(on)) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, SO_NOSIGPIPE) %s: %s",
|
|
|
|
sock->fd,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"),
|
|
|
|
strbuf);
|
|
|
|
/* Press on... */
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-07-12 00:41:55 +00:00
|
|
|
#if defined(USE_CMSG) || defined(SO_RCVBUF)
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
1999-12-09 00:19:31 +00:00
|
|
|
|
2005-07-12 00:41:55 +00:00
|
|
|
#if defined(USE_CMSG)
|
1999-12-09 00:19:31 +00:00
|
|
|
#if defined(SO_TIMESTAMP)
|
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_TIMESTAMP,
|
2001-11-27 01:56:32 +00:00
|
|
|
(void *)&on, sizeof(on)) < 0
|
2000-10-12 00:35:49 +00:00
|
|
|
&& errno != ENOPROTOOPT) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1999-12-09 00:19:31 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"setsockopt(%d, SO_TIMESTAMP) %s: %s",
|
2008-01-18 23:46:58 +00:00
|
|
|
sock->fd,
|
2000-12-06 00:30:32 +00:00
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
1999-12-09 00:19:31 +00:00
|
|
|
/* Press on... */
|
|
|
|
}
|
|
|
|
#endif /* SO_TIMESTAMP */
|
|
|
|
|
|
|
|
#if defined(ISC_PLATFORM_HAVEIPV6)
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->pf == AF_INET6 && sock->recvcmsgbuflen == 0U) {
|
2004-01-29 04:39:19 +00:00
|
|
|
/*
|
|
|
|
* Warn explicitly because this anomaly can be hidden
|
|
|
|
* in usual operation (and unexpectedly appear later).
|
|
|
|
*/
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"No buffer available to receive "
|
|
|
|
"IPv6 destination");
|
|
|
|
}
|
2004-11-18 21:31:47 +00:00
|
|
|
#ifdef ISC_PLATFORM_HAVEIN6PKTINFO
|
2000-08-10 00:05:45 +00:00
|
|
|
#ifdef IPV6_RECVPKTINFO
|
2007-04-02 02:03:55 +00:00
|
|
|
/* RFC 3542 */
|
2008-06-23 19:41:20 +00:00
|
|
|
if ((sock->pf == AF_INET6)
|
2000-08-10 00:05:45 +00:00
|
|
|
&& (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_RECVPKTINFO,
|
2001-11-27 01:56:32 +00:00
|
|
|
(void *)&on, sizeof(on)) < 0)) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
2000-08-10 00:05:45 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, IPV6_RECVPKTINFO) "
|
2000-12-06 00:30:32 +00:00
|
|
|
"%s: %s", sock->fd,
|
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
2000-08-10 00:05:45 +00:00
|
|
|
}
|
|
|
|
#else
|
2007-04-02 02:03:55 +00:00
|
|
|
/* RFC 2292 */
|
2008-06-23 19:41:20 +00:00
|
|
|
if ((sock->pf == AF_INET6)
|
1999-12-09 00:19:31 +00:00
|
|
|
&& (setsockopt(sock->fd, IPPROTO_IPV6, IPV6_PKTINFO,
|
2001-11-27 01:56:32 +00:00
|
|
|
(void *)&on, sizeof(on)) < 0)) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1999-12-09 00:19:31 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"setsockopt(%d, IPV6_PKTINFO) %s: %s",
|
|
|
|
sock->fd,
|
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
1999-12-09 00:19:31 +00:00
|
|
|
}
|
2000-08-10 00:05:45 +00:00
|
|
|
#endif /* IPV6_RECVPKTINFO */
|
2004-11-18 21:31:47 +00:00
|
|
|
#endif /* ISC_PLATFORM_HAVEIN6PKTINFO */
|
2007-04-02 02:03:55 +00:00
|
|
|
#ifdef IPV6_USE_MIN_MTU /* RFC 3542, not too common yet*/
|
2000-06-15 18:25:49 +00:00
|
|
|
/* use minimum MTU */
|
2008-06-23 19:41:20 +00:00
|
|
|
if (sock->pf == AF_INET6) {
|
2000-06-15 18:25:49 +00:00
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IPV6,
|
|
|
|
IPV6_USE_MIN_MTU,
|
2001-11-27 01:56:32 +00:00
|
|
|
(void *)&on, sizeof(on));
|
2000-06-15 18:25:49 +00:00
|
|
|
}
|
|
|
|
#endif
|
1999-12-09 00:19:31 +00:00
|
|
|
#endif /* ISC_PLATFORM_HAVEIPV6 */
|
2005-07-12 00:41:55 +00:00
|
|
|
#endif /* defined(USE_CMSG) */
|
|
|
|
|
|
|
|
#if defined(SO_RCVBUF)
|
|
|
|
optlen = sizeof(size);
|
|
|
|
if (getsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
|
|
|
|
(void *)&size, &optlen) >= 0 &&
|
|
|
|
size < RCVBUFSIZE) {
|
|
|
|
size = RCVBUFSIZE;
|
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_RCVBUF,
|
|
|
|
(void *)&size, sizeof(size)) == -1) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d, SO_RCVBUF, %d) %s: %s",
|
|
|
|
sock->fd, size,
|
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"),
|
|
|
|
strbuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-28 04:26:49 +00:00
|
|
|
}
|
2005-07-12 00:41:55 +00:00
|
|
|
#endif /* defined(USE_CMSG) || defined(SO_RCVBUF) */
|
1999-08-28 04:26:49 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
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'.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_create(isc_socketmgr_t *manager, int pf, isc_sockettype_t type,
|
|
|
|
isc_socket_t **socketp)
|
|
|
|
{
|
|
|
|
isc_socket_t *sock = NULL;
|
|
|
|
isc_result_t result;
|
|
|
|
int lockid;
|
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
|
|
|
result = allocate_socket(manager, type, &sock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
sock->pf = pf;
|
|
|
|
result = opensocket(manager, sock);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
free_socket(&sock);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2007-02-13 02:49:08 +00:00
|
|
|
memset(sock->name, 0, sizeof(sock->name));
|
|
|
|
sock->tag = NULL;
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->references = 1;
|
|
|
|
*socketp = sock;
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Note we don't have to lock the socket like we normally would because
|
|
|
|
* there are no external references to it yet.
|
|
|
|
*/
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
lockid = FDLOCK_ID(sock->fd);
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
1998-11-06 01:45:35 +00:00
|
|
|
manager->fds[sock->fd] = sock;
|
1998-12-05 00:28:13 +00:00
|
|
|
manager->fdstate[sock->fd] = MANAGED;
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_DEVPOLL
|
|
|
|
INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
|
|
|
|
sock->manager->fdpollinfo[sock->fd].want_write == 0);
|
|
|
|
#endif
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST_APPEND(manager->socklist, sock, link);
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_SELECT
|
1998-11-11 01:44:08 +00:00
|
|
|
if (manager->maxfd < sock->fd)
|
|
|
|
manager->maxfd = sock->fd;
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
2000-12-19 20:35:37 +00:00
|
|
|
socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_CREATED, "created");
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_socket_open(isc_socket_t *sock) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
REQUIRE(sock->references == 1);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
/*
|
|
|
|
* We don't need to retain the lock hereafter, since no one else has
|
|
|
|
* this socket.
|
|
|
|
*/
|
|
|
|
REQUIRE(sock->fd == -1);
|
|
|
|
|
|
|
|
result = opensocket(sock->manager, sock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
sock->fd = -1;
|
|
|
|
|
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
int lockid = FDLOCK_ID(sock->fd);
|
|
|
|
|
|
|
|
LOCK(&sock->manager->fdlock[lockid]);
|
|
|
|
sock->manager->fds[sock->fd] = sock;
|
|
|
|
sock->manager->fdstate[sock->fd] = MANAGED;
|
|
|
|
#ifdef USE_DEVPOLL
|
|
|
|
INSIST(sock->manager->fdpollinfo[sock->fd].want_read == 0 &&
|
|
|
|
sock->manager->fdpollinfo[sock->fd].want_write == 0);
|
|
|
|
#endif
|
|
|
|
UNLOCK(&sock->manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
#ifdef USE_SELECT
|
|
|
|
LOCK(&sock->manager->lock);
|
|
|
|
if (sock->manager->maxfd < sock->fd)
|
|
|
|
sock->manager->maxfd = sock->fd;
|
|
|
|
UNLOCK(&sock->manager->lock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2005-12-06 16:54:49 +00:00
|
|
|
/*
|
|
|
|
* Create a new 'type' socket managed by 'manager'. Events
|
|
|
|
* will be posted to 'task' and when dispatched 'action' will be
|
|
|
|
* called with 'arg' as the arg value. The new socket is returned
|
|
|
|
* in 'socketp'.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_fdwatchcreate(isc_socketmgr_t *manager, int fd, int flags,
|
|
|
|
isc_sockfdwatch_t callback, void *cbarg,
|
|
|
|
isc_task_t *task, isc_socket_t **socketp)
|
|
|
|
{
|
|
|
|
isc_socket_t *sock = NULL;
|
|
|
|
isc_result_t result;
|
2008-06-23 19:41:20 +00:00
|
|
|
int lockid;
|
2005-12-06 16:54:49 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
|
|
|
result = allocate_socket(manager, isc_sockettype_fdwatch, &sock);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
|
|
|
sock->fd = fd;
|
|
|
|
sock->fdwatcharg = cbarg;
|
|
|
|
sock->fdwatchcb = callback;
|
|
|
|
sock->fdwatchflags = flags;
|
|
|
|
sock->fdwatchtask = task;
|
|
|
|
|
|
|
|
sock->references = 1;
|
|
|
|
*socketp = sock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note we don't have to lock the socket like we normally would because
|
|
|
|
* there are no external references to it yet.
|
|
|
|
*/
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
lockid = FDLOCK_ID(sock->fd);
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
2005-12-06 16:54:49 +00:00
|
|
|
manager->fds[sock->fd] = sock;
|
|
|
|
manager->fdstate[sock->fd] = MANAGED;
|
2008-06-23 19:41:20 +00:00
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
2005-12-06 16:54:49 +00:00
|
|
|
ISC_LIST_APPEND(manager->socklist, sock, link);
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_SELECT
|
2005-12-06 16:54:49 +00:00
|
|
|
if (manager->maxfd < sock->fd)
|
|
|
|
manager->maxfd = sock->fd;
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
2005-12-06 16:54:49 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
if (flags & ISC_SOCKFDWATCH_READ)
|
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
|
|
|
|
if (flags & ISC_SOCKFDWATCH_WRITE)
|
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
|
|
|
|
|
|
|
|
socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_CREATED, "fdwatch-created");
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Attach to a socket. Caller must explicitly detach when it is done.
|
|
|
|
*/
|
|
|
|
void
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp) {
|
1998-11-03 00:54:47 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
sock->references++;
|
|
|
|
UNLOCK(&sock->lock);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
*socketp = sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dereference a socket. If this is the last reference to it, clean things
|
|
|
|
* up by destroying the socket.
|
|
|
|
*/
|
2000-08-01 01:33:37 +00:00
|
|
|
void
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_detach(isc_socket_t **socketp) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock;
|
1998-11-11 00:43:14 +00:00
|
|
|
isc_boolean_t kill_socket = ISC_FALSE;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
REQUIRE(socketp != NULL);
|
|
|
|
sock = *socketp;
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
REQUIRE(sock->references > 0);
|
|
|
|
sock->references--;
|
|
|
|
if (sock->references == 0)
|
1998-11-11 00:43:14 +00:00
|
|
|
kill_socket = ISC_TRUE;
|
1998-11-03 00:54:47 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
if (kill_socket)
|
|
|
|
destroy(&sock);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
*socketp = NULL;
|
|
|
|
}
|
|
|
|
|
2008-07-03 00:13:25 +00:00
|
|
|
isc_result_t
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_socket_close(isc_socket_t *sock) {
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
REQUIRE(sock->references == 1);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
/*
|
|
|
|
* We don't need to retain the lock hereafter, since no one else has
|
|
|
|
* this socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(sock->fd >= 0 && sock->fd < (int)sock->manager->maxsocks);
|
|
|
|
|
|
|
|
INSIST(!sock->connecting);
|
|
|
|
INSIST(!sock->pending_recv);
|
|
|
|
INSIST(!sock->pending_send);
|
|
|
|
INSIST(!sock->pending_accept);
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->recv_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->send_list));
|
|
|
|
INSIST(ISC_LIST_EMPTY(sock->accept_list));
|
|
|
|
INSIST(sock->connect_ev == NULL);
|
|
|
|
|
|
|
|
fd = sock->fd;
|
|
|
|
sock->fd = -1;
|
|
|
|
sock->listener = 0;
|
|
|
|
sock->connected = 0;
|
|
|
|
sock->connecting = 0;
|
|
|
|
sock->bound = 0;
|
|
|
|
isc_sockaddr_any(&sock->peer_address);
|
|
|
|
|
|
|
|
closesocket(sock->manager, sock->type, fd);
|
2008-07-03 00:13:25 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2000-05-14 22:35:45 +00:00
|
|
|
dispatch_recv(isc_socket_t *sock) {
|
1999-07-28 01:07:21 +00:00
|
|
|
intev_t *iev;
|
|
|
|
isc_socketevent_t *ev;
|
2005-12-06 16:54:49 +00:00
|
|
|
isc_task_t *sender;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#if 0
|
|
|
|
/*
|
|
|
|
* XXXJT: this assertion seems to strong, but leave it here for
|
|
|
|
* reference.
|
|
|
|
*/
|
2000-02-02 19:00:01 +00:00
|
|
|
INSIST(!sock->pending_recv);
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
|
|
|
if (sock->pending_recv != 0)
|
|
|
|
return;
|
2000-02-02 19:00:01 +00:00
|
|
|
|
2005-12-06 16:54:49 +00:00
|
|
|
if (sock->type != isc_sockettype_fdwatch) {
|
|
|
|
ev = ISC_LIST_HEAD(sock->recv_list);
|
2005-12-07 03:55:06 +00:00
|
|
|
if (ev == NULL)
|
2005-12-06 16:54:49 +00:00
|
|
|
return;
|
|
|
|
socket_log(sock, NULL, EVENT, NULL, 0, 0,
|
|
|
|
"dispatch_recv: event %p -> task %p",
|
|
|
|
ev, ev->ev_sender);
|
|
|
|
sender = ev->ev_sender;
|
|
|
|
} else {
|
|
|
|
sender = sock->fdwatchtask;
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->pending_recv = 1;
|
2000-02-02 19:00:01 +00:00
|
|
|
iev = &sock->readable_ev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
sock->references++;
|
2000-04-17 19:22:44 +00:00
|
|
|
iev->ev_sender = sock;
|
2005-12-06 16:54:49 +00:00
|
|
|
if (sock->type == isc_sockettype_fdwatch)
|
|
|
|
iev->ev_action = internal_fdwatch_read;
|
|
|
|
else
|
|
|
|
iev->ev_action = internal_recv;
|
2000-04-17 19:22:44 +00:00
|
|
|
iev->ev_arg = sock;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2005-12-06 16:54:49 +00:00
|
|
|
isc_task_send(sender, (isc_event_t **)&iev);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
dispatch_send(isc_socket_t *sock) {
|
1999-07-28 06:50:10 +00:00
|
|
|
intev_t *iev;
|
|
|
|
isc_socketevent_t *ev;
|
2005-12-07 03:55:06 +00:00
|
|
|
isc_task_t *sender;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2000-02-02 19:00:01 +00:00
|
|
|
INSIST(!sock->pending_send);
|
|
|
|
|
2005-12-07 03:55:06 +00:00
|
|
|
if (sock->type != isc_sockettype_fdwatch) {
|
|
|
|
ev = ISC_LIST_HEAD(sock->send_list);
|
|
|
|
if (ev == NULL)
|
|
|
|
return;
|
|
|
|
socket_log(sock, NULL, EVENT, NULL, 0, 0,
|
|
|
|
"dispatch_send: event %p -> task %p",
|
|
|
|
ev, ev->ev_sender);
|
|
|
|
sender = ev->ev_sender;
|
|
|
|
} else {
|
|
|
|
sender = sock->fdwatchtask;
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->pending_send = 1;
|
2000-02-02 19:00:01 +00:00
|
|
|
iev = &sock->writable_ev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
sock->references++;
|
2000-04-17 19:22:44 +00:00
|
|
|
iev->ev_sender = sock;
|
2005-12-06 16:54:49 +00:00
|
|
|
if (sock->type == isc_sockettype_fdwatch)
|
|
|
|
iev->ev_action = internal_fdwatch_write;
|
|
|
|
else
|
|
|
|
iev->ev_action = internal_send;
|
2000-04-17 19:22:44 +00:00
|
|
|
iev->ev_arg = sock;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2005-12-07 03:55:06 +00:00
|
|
|
isc_task_send(sender, (isc_event_t **)&iev);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Dispatch an internal accept event.
|
|
|
|
*/
|
1998-11-10 11:37:54 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
dispatch_accept(isc_socket_t *sock) {
|
1999-06-10 00:45:30 +00:00
|
|
|
intev_t *iev;
|
|
|
|
isc_socket_newconnev_t *ev;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2000-02-02 19:00:01 +00:00
|
|
|
INSIST(!sock->pending_accept);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Are there any done events left, or were they all canceled
|
|
|
|
* before the manager got the socket lock?
|
|
|
|
*/
|
1999-06-10 00:45:30 +00:00
|
|
|
ev = ISC_LIST_HEAD(sock->accept_list);
|
2000-02-02 19:00:01 +00:00
|
|
|
if (ev == NULL)
|
|
|
|
return;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
sock->pending_accept = 1;
|
2000-02-02 19:00:01 +00:00
|
|
|
iev = &sock->readable_ev;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
sock->references++; /* keep socket around for this internal event */
|
2000-04-17 19:22:44 +00:00
|
|
|
iev->ev_sender = sock;
|
|
|
|
iev->ev_action = internal_accept;
|
|
|
|
iev->ev_arg = sock;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
dispatch_connect(isc_socket_t *sock) {
|
1999-07-28 01:07:21 +00:00
|
|
|
intev_t *iev;
|
|
|
|
isc_socket_connev_t *ev;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
iev = &sock->writable_ev;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
ev = sock->connect_ev;
|
2000-02-02 19:00:01 +00:00
|
|
|
INSIST(ev != NULL); /* XXX */
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(sock->connecting);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
sock->references++; /* keep socket around for this internal event */
|
2000-04-17 19:22:44 +00:00
|
|
|
iev->ev_sender = sock;
|
|
|
|
iev->ev_action = internal_connect;
|
|
|
|
iev->ev_arg = sock;
|
1999-07-28 01:07:21 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
isc_task_send(ev->ev_sender, (isc_event_t **)&iev);
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
1999-07-28 06:50:10 +00:00
|
|
|
* 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.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
2001-01-25 22:25:10 +00:00
|
|
|
* Caller must have the socket locked if the event is attached to the socket.
|
1998-11-07 02:31:04 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
static void
|
2001-02-12 21:43:17 +00:00
|
|
|
send_recvdone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
|
1999-09-03 00:22:28 +00:00
|
|
|
isc_task_t *task;
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
task = (*dev)->ev_sender;
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
(*dev)->ev_sender = sock;
|
1999-09-03 00:22:28 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
if (ISC_LINK_LINKED(*dev, ev_link))
|
|
|
|
ISC_LIST_DEQUEUE(sock->recv_list, *dev, ev_link);
|
1999-09-03 00:22:28 +00:00
|
|
|
|
|
|
|
if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
|
|
|
|
== ISC_SOCKEVENTATTR_ATTACHED)
|
1999-09-23 21:31:03 +00:00
|
|
|
isc_task_sendanddetach(&task, (isc_event_t **)dev);
|
1999-07-28 01:07:21 +00:00
|
|
|
else
|
1999-09-23 21:31:03 +00:00
|
|
|
isc_task_send(task, (isc_event_t **)dev);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1999-03-29 23:56:06 +00:00
|
|
|
|
|
|
|
/*
|
1999-03-30 06:22:28 +00:00
|
|
|
* See comments for send_recvdone_event() above.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
2001-01-25 22:25:10 +00:00
|
|
|
* Caller must have the socket locked if the event is attached to the socket.
|
1999-03-29 23:56:06 +00:00
|
|
|
*/
|
1998-11-11 02:05:36 +00:00
|
|
|
static void
|
2001-02-12 21:43:17 +00:00
|
|
|
send_senddone_event(isc_socket_t *sock, isc_socketevent_t **dev) {
|
1999-09-03 01:18:45 +00:00
|
|
|
isc_task_t *task;
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
INSIST(dev != NULL && *dev != NULL);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
task = (*dev)->ev_sender;
|
|
|
|
(*dev)->ev_sender = sock;
|
1999-09-03 01:18:45 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
if (ISC_LINK_LINKED(*dev, ev_link))
|
|
|
|
ISC_LIST_DEQUEUE(sock->send_list, *dev, ev_link);
|
1999-09-03 01:18:45 +00:00
|
|
|
|
|
|
|
if (((*dev)->attributes & ISC_SOCKEVENTATTR_ATTACHED)
|
|
|
|
== ISC_SOCKEVENTATTR_ATTACHED)
|
1999-09-23 21:31:03 +00:00
|
|
|
isc_task_sendanddetach(&task, (isc_event_t **)dev);
|
1999-07-28 06:50:10 +00:00
|
|
|
else
|
1999-09-23 21:31:03 +00:00
|
|
|
isc_task_send(task, (isc_event_t **)dev);
|
1998-11-11 02:05:36 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* 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
|
1999-06-10 00:45:30 +00:00
|
|
|
* 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.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
internal_accept(isc_task_t *me, isc_event_t *ev) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_socket_newconnev_t *dev;
|
1999-07-28 01:07:21 +00:00
|
|
|
isc_task_t *task;
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T addrlen;
|
1998-11-10 01:56:44 +00:00
|
|
|
int fd;
|
1998-12-10 16:14:05 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2005-05-19 02:42:42 +00:00
|
|
|
const char *err = "accept";
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
UNUSED(me);
|
1999-07-28 01:07:21 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
sock = ev->ev_sender;
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
LOCK(&sock->lock);
|
2000-12-06 00:30:32 +00:00
|
|
|
socket_log(sock, NULL, TRACE,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTLOCK,
|
|
|
|
"internal_accept called, locked socket");
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
manager = sock->manager;
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_MANAGER(manager));
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
INSIST(sock->listener);
|
1999-07-28 01:07:21 +00:00
|
|
|
INSIST(sock->pending_accept == 1);
|
|
|
|
sock->pending_accept = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
INSIST(sock->references > 0);
|
|
|
|
sock->references--; /* the internal event is done with this socket */
|
|
|
|
if (sock->references == 0) {
|
1998-11-10 11:37:54 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1999-06-10 00:45:30 +00:00
|
|
|
destroy(&sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the first item off the accept list.
|
|
|
|
* If it is empty, unlock the socket and return.
|
|
|
|
*/
|
|
|
|
dev = ISC_LIST_HEAD(sock->accept_list);
|
|
|
|
if (dev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* Try to accept the new connection. If the accept fails with
|
1998-12-04 11:21:11 +00:00
|
|
|
* EAGAIN or EINTR, simply poke the watcher to watch this socket
|
2001-07-15 22:50:24 +00:00
|
|
|
* again. Also ignore ECONNRESET, which has been reported to
|
|
|
|
* be spuriously returned on Linux 2.2.19 although it is not
|
2001-11-08 20:24:25 +00:00
|
|
|
* 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
|
|
|
|
* deamons such as BIND 8 and Apache.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
2001-11-08 20:24:25 +00:00
|
|
|
|
2007-01-03 20:57:07 +00:00
|
|
|
addrlen = sizeof(dev->newsocket->peer_address.type);
|
2008-01-22 01:06:33 +00:00
|
|
|
memset(&dev->newsocket->peer_address.type, 0, addrlen);
|
2007-01-03 20:57:07 +00:00
|
|
|
fd = accept(sock->fd, &dev->newsocket->peer_address.type.sa,
|
2000-05-14 22:35:45 +00:00
|
|
|
(void *)&addrlen);
|
2003-02-26 04:00:20 +00:00
|
|
|
|
|
|
|
#ifdef F_DUPFD
|
2005-05-19 02:42:42 +00:00
|
|
|
/*
|
|
|
|
* Leave a space for stdio to work in.
|
|
|
|
*/
|
|
|
|
if (fd >= 0 && fd < 20) {
|
|
|
|
int new, tmp;
|
|
|
|
new = fcntl(fd, F_DUPFD, 20);
|
|
|
|
tmp = errno;
|
|
|
|
(void)close(fd);
|
|
|
|
errno = tmp;
|
|
|
|
fd = new;
|
|
|
|
err = "fcntl";
|
|
|
|
}
|
2003-02-26 04:00:20 +00:00
|
|
|
#endif
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
if (fd < 0) {
|
2001-11-08 20:24:25 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
2001-04-10 21:48:27 +00:00
|
|
|
goto soft_error;
|
2001-11-08 20:24:25 +00:00
|
|
|
switch (errno) {
|
2002-04-03 05:41:20 +00:00
|
|
|
case ENOBUFS:
|
|
|
|
case ENFILE:
|
|
|
|
case ENOMEM:
|
2001-11-08 20:24:25 +00:00
|
|
|
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;
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
2001-11-08 20:24:25 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2005-05-19 02:42:42 +00:00
|
|
|
"internal_accept: %s() %s: %s", err,
|
2001-11-08 20:24:25 +00:00
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"),
|
|
|
|
strbuf);
|
|
|
|
fd = -1;
|
|
|
|
result = ISC_R_UNEXPECTED;
|
2001-01-23 21:07:12 +00:00
|
|
|
} else {
|
2005-08-25 03:26:54 +00:00
|
|
|
if (addrlen == 0U) {
|
2001-02-07 23:40:28 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_accept(): "
|
|
|
|
"accept() failed to return "
|
|
|
|
"remote address");
|
2001-02-08 00:04:11 +00:00
|
|
|
|
2001-02-07 23:40:28 +00:00
|
|
|
(void)close(fd);
|
2001-04-10 21:48:27 +00:00
|
|
|
goto soft_error;
|
2007-01-03 20:57:07 +00:00
|
|
|
} else if (dev->newsocket->peer_address.type.sa.sa_family !=
|
2001-02-08 00:04:11 +00:00
|
|
|
sock->pf)
|
|
|
|
{
|
2001-02-06 23:04:02 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_accept(): "
|
|
|
|
"accept() returned peer address "
|
2008-01-18 23:46:58 +00:00
|
|
|
"family %u (expected %u)",
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->newsocket->peer_address.
|
2001-02-06 23:04:02 +00:00
|
|
|
type.sa.sa_family,
|
|
|
|
sock->pf);
|
|
|
|
(void)close(fd);
|
2001-04-10 21:48:27 +00:00
|
|
|
goto soft_error;
|
2008-06-23 19:41:20 +00:00
|
|
|
} else if (fd >= (int)manager->maxsocks) {
|
2001-04-26 23:45:55 +00:00
|
|
|
isc_log_iwrite(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_TOOMANYFDS,
|
|
|
|
"%s: too many open file descriptors",
|
|
|
|
"accept");
|
|
|
|
(void)close(fd);
|
|
|
|
goto soft_error;
|
2001-02-06 23:04:02 +00:00
|
|
|
}
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2001-04-10 21:38:33 +00:00
|
|
|
if (fd != -1) {
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->newsocket->peer_address.length = addrlen;
|
2001-04-10 21:38:33 +00:00
|
|
|
dev->newsocket->pf = sock->pf;
|
2001-02-08 00:04:11 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Pull off the done event.
|
|
|
|
*/
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_UNLINK(sock->accept_list, dev, ev_link);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Poke watcher if there are more pending accepts.
|
|
|
|
*/
|
2000-06-02 01:27:27 +00:00
|
|
|
if (!ISC_LIST_EMPTY(sock->accept_list))
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
if (fd != -1 && (make_nonblock(fd) != ISC_R_SUCCESS)) {
|
2001-11-30 01:59:49 +00:00
|
|
|
(void)close(fd);
|
1998-12-10 16:14:05 +00:00
|
|
|
fd = -1;
|
|
|
|
result = ISC_R_UNEXPECTED;
|
1998-12-05 01:44:38 +00:00
|
|
|
}
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
/*
|
1998-12-10 16:14:05 +00:00
|
|
|
* -1 means the new socket didn't happen.
|
1998-11-10 11:37:54 +00:00
|
|
|
*/
|
1998-12-10 16:14:05 +00:00
|
|
|
if (fd != -1) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int lockid = FDLOCK_ID(fd);
|
|
|
|
|
|
|
|
LOCK(&manager->fdlock[lockid]);
|
|
|
|
manager->fds[fd] = dev->newsocket;
|
|
|
|
manager->fdstate[fd] = MANAGED;
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
2001-04-26 23:45:55 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
ISC_LIST_APPEND(manager->socklist, dev->newsocket, link);
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
dev->newsocket->fd = fd;
|
2000-05-11 06:35:20 +00:00
|
|
|
dev->newsocket->bound = 1;
|
|
|
|
dev->newsocket->connected = 1;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
/*
|
|
|
|
* Save away the remote address
|
|
|
|
*/
|
2007-01-03 20:57:07 +00:00
|
|
|
dev->address = dev->newsocket->peer_address;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_SELECT
|
1998-12-10 16:14:05 +00:00
|
|
|
if (manager->maxfd < fd)
|
|
|
|
manager->maxfd = fd;
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
1998-12-10 16:14:05 +00:00
|
|
|
|
2007-01-03 20:57:07 +00:00
|
|
|
socket_log(sock, &dev->newsocket->peer_address, CREATION,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_ACCEPTEDCXN,
|
|
|
|
"accepted connection, new socket %p",
|
2000-05-03 21:09:49 +00:00
|
|
|
dev->newsocket);
|
2000-06-02 20:09:01 +00:00
|
|
|
|
2001-04-26 23:45:55 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
} else {
|
|
|
|
dev->newsocket->references--;
|
|
|
|
free_socket(&dev->newsocket);
|
|
|
|
}
|
2008-01-18 23:46:58 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Fill in the done event details and send it off.
|
|
|
|
*/
|
|
|
|
dev->result = result;
|
2000-04-17 19:22:44 +00:00
|
|
|
task = dev->ev_sender;
|
|
|
|
dev->ev_sender = sock;
|
1999-06-10 00:45:30 +00:00
|
|
|
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
|
2001-04-10 21:48:27 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
soft_error:
|
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_ACCEPT);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return;
|
1998-11-07 02:31:04 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
internal_recv(isc_task_t *me, isc_event_t *ev) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socket_t *sock;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
|
1999-07-28 21:30:37 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
sock = ev->ev_sender;
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
LOCK(&sock->lock);
|
2000-05-03 21:09:49 +00:00
|
|
|
socket_log(sock, NULL, IOEVENT,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
|
2001-08-12 00:12:14 +00:00
|
|
|
"internal_recv: task %p got event %p", me, ev);
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
INSIST(sock->pending_recv == 1);
|
|
|
|
sock->pending_recv = 0;
|
|
|
|
|
|
|
|
INSIST(sock->references > 0);
|
|
|
|
sock->references--; /* the internal event is done with this socket */
|
|
|
|
if (sock->references == 0) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
destroy(&sock);
|
|
|
|
return;
|
|
|
|
}
|
1998-11-07 02:31:04 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* Try to do as much I/O as possible on this socket. There are no
|
2001-01-26 23:17:26 +00:00
|
|
|
* limits here, currently.
|
1998-11-06 01:45:35 +00:00
|
|
|
*/
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
while (dev != NULL) {
|
1999-09-03 00:22:28 +00:00
|
|
|
switch (doio_recv(sock, dev)) {
|
|
|
|
case DOIO_SOFT:
|
|
|
|
goto poke;
|
1998-12-18 01:48:43 +00:00
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
case DOIO_EOF:
|
1998-12-18 01:48:43 +00:00
|
|
|
/*
|
1999-09-03 00:22:28 +00:00
|
|
|
* read of 0 means the remote end was closed.
|
|
|
|
* Run through the event queue and dispatch all
|
2001-01-26 23:17:26 +00:00
|
|
|
* the events with an EOF result code.
|
1998-12-18 01:48:43 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
do {
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_EOF;
|
|
|
|
send_recvdone_event(sock, &dev);
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
} while (dev != NULL);
|
1998-11-06 01:45:35 +00:00
|
|
|
goto poke;
|
|
|
|
|
1999-09-03 00:22:28 +00:00
|
|
|
case DOIO_SUCCESS:
|
1999-09-03 01:18:45 +00:00
|
|
|
case DOIO_HARD:
|
2001-02-12 21:43:17 +00:00
|
|
|
send_recvdone_event(sock, &dev);
|
1999-09-03 00:22:28 +00:00
|
|
|
break;
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
poke:
|
2000-06-02 01:27:27 +00:00
|
|
|
if (!ISC_LIST_EMPTY(sock->recv_list))
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
internal_send(isc_task_t *me, isc_event_t *ev) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socket_t *sock;
|
1999-07-28 06:50:10 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
|
1999-07-28 21:30:37 +00:00
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
/*
|
|
|
|
* Find out what socket this is and lock it.
|
|
|
|
*/
|
2000-04-17 19:22:44 +00:00
|
|
|
sock = (isc_socket_t *)ev->ev_sender;
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
1999-07-28 06:50:10 +00:00
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
LOCK(&sock->lock);
|
2000-05-03 21:09:49 +00:00
|
|
|
socket_log(sock, NULL, IOEVENT,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
|
2001-08-12 00:12:14 +00:00
|
|
|
"internal_send: task %p got event %p", me, ev);
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
INSIST(sock->pending_send == 1);
|
|
|
|
sock->pending_send = 0;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
INSIST(sock->references > 0);
|
|
|
|
sock->references--; /* the internal event is done with this socket */
|
|
|
|
if (sock->references == 0) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
destroy(&sock);
|
|
|
|
return;
|
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to do as much I/O as possible on this socket. There are no
|
2001-01-26 23:17:26 +00:00
|
|
|
* limits here, currently.
|
1998-11-11 00:43:14 +00:00
|
|
|
*/
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->send_list);
|
|
|
|
while (dev != NULL) {
|
1999-09-03 01:18:45 +00:00
|
|
|
switch (doio_send(sock, dev)) {
|
|
|
|
case DOIO_SOFT:
|
1998-11-11 00:43:14 +00:00
|
|
|
goto poke;
|
|
|
|
|
1999-09-03 01:18:45 +00:00
|
|
|
case DOIO_HARD:
|
|
|
|
case DOIO_SUCCESS:
|
2001-02-12 21:43:17 +00:00
|
|
|
send_senddone_event(sock, &dev);
|
1999-09-03 01:18:45 +00:00
|
|
|
break;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->send_list);
|
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
poke:
|
2000-06-02 01:27:27 +00:00
|
|
|
if (!ISC_LIST_EMPTY(sock->send_list))
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
|
2005-12-06 16:54:49 +00:00
|
|
|
static void
|
|
|
|
internal_fdwatch_write(isc_task_t *me, isc_event_t *ev) {
|
|
|
|
isc_socket_t *sock;
|
|
|
|
int more_data;
|
|
|
|
|
|
|
|
INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out what socket this is and lock it.
|
|
|
|
*/
|
|
|
|
sock = (isc_socket_t *)ev->ev_sender;
|
|
|
|
INSIST(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
socket_log(sock, NULL, IOEVENT,
|
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALSEND,
|
|
|
|
"internal_fdwatch_write: task %p got event %p", me, ev);
|
|
|
|
|
|
|
|
INSIST(sock->pending_send == 1);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
more_data = (sock->fdwatchcb)(me, sock, sock->fdwatcharg);
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
sock->pending_send = 0;
|
|
|
|
|
|
|
|
INSIST(sock->references > 0);
|
|
|
|
sock->references--; /* the internal event is done with this socket */
|
|
|
|
if (sock->references == 0) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
destroy(&sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (more_data)
|
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_WRITE);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
internal_fdwatch_read(isc_task_t *me, isc_event_t *ev) {
|
|
|
|
isc_socket_t *sock;
|
|
|
|
int more_data;
|
|
|
|
|
|
|
|
INSIST(ev->ev_type == ISC_SOCKEVENT_INTR);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out what socket this is and lock it.
|
|
|
|
*/
|
|
|
|
sock = (isc_socket_t *)ev->ev_sender;
|
|
|
|
INSIST(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
socket_log(sock, NULL, IOEVENT,
|
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_INTERNALRECV,
|
|
|
|
"internal_fdwatch_read: task %p got event %p", me, ev);
|
|
|
|
|
|
|
|
INSIST(sock->pending_recv == 1);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
more_data = (sock->fdwatchcb)(me, sock, sock->fdwatcharg);
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
sock->pending_recv = 0;
|
|
|
|
|
|
|
|
INSIST(sock->references > 0);
|
|
|
|
sock->references--; /* the internal event is done with this socket */
|
|
|
|
if (sock->references == 0) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
destroy(&sock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (more_data)
|
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/*
|
|
|
|
* Process read/writes on each fd here. Avoid locking
|
|
|
|
* and unlocking twice if both reads and writes are possible.
|
|
|
|
*/
|
2000-08-29 23:58:17 +00:00
|
|
|
static void
|
2008-06-23 19:41:20 +00:00
|
|
|
process_fd(isc_socketmgr_t *manager, int fd, isc_boolean_t readable,
|
|
|
|
isc_boolean_t writeable)
|
2000-08-29 23:58:17 +00:00
|
|
|
{
|
|
|
|
isc_socket_t *sock;
|
|
|
|
isc_boolean_t unlock_sock;
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_boolean_t needclose;
|
|
|
|
int lockid = FDLOCK_ID(fd);
|
2001-04-26 23:45:55 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
/*
|
2008-06-23 19:41:20 +00:00
|
|
|
* If we need to close the socket, do it now.
|
2000-08-29 23:58:17 +00:00
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
LOCK(&manager->fdlock[lockid]);
|
|
|
|
if (manager->fdstate[fd] == CLOSE_PENDING
|
|
|
|
|| manager->fdstate[fd] == MANAGER_CLOSE_PENDING) {
|
|
|
|
needclose = ISC_TF(manager->fdstate[fd] == CLOSE_PENDING);
|
|
|
|
manager->fdstate[fd] = CLOSED;
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_READ);
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
|
|
|
|
if (needclose)
|
|
|
|
(void)close(fd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = manager->fds[fd];
|
|
|
|
UNLOCK(&manager->fdlock[lockid]);
|
|
|
|
unlock_sock = ISC_FALSE;
|
|
|
|
if (readable) {
|
|
|
|
if (sock == NULL) {
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_READ);
|
|
|
|
goto check_write;
|
|
|
|
}
|
|
|
|
unlock_sock = ISC_TRUE;
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
if (!SOCK_DEAD(sock)) {
|
|
|
|
if (sock->listener)
|
|
|
|
dispatch_accept(sock);
|
|
|
|
else
|
|
|
|
dispatch_recv(sock);
|
|
|
|
}
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_READ);
|
|
|
|
}
|
|
|
|
check_write:
|
|
|
|
if (writeable) {
|
|
|
|
if (sock == NULL) {
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!unlock_sock) {
|
|
|
|
unlock_sock = ISC_TRUE;
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
if (!SOCK_DEAD(sock)) {
|
|
|
|
if (sock->connecting)
|
|
|
|
dispatch_connect(sock);
|
|
|
|
else
|
|
|
|
dispatch_send(sock);
|
|
|
|
}
|
|
|
|
(void)unwatch_fd(manager, fd, SELECT_POKE_WRITE);
|
|
|
|
}
|
|
|
|
if (unlock_sock)
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
static isc_boolean_t
|
|
|
|
process_fds(isc_socketmgr_t *manager, struct kevent *events, int nevents) {
|
|
|
|
int i;
|
|
|
|
isc_boolean_t readable, writable;
|
|
|
|
isc_boolean_t done = ISC_FALSE;
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (nevents == manager->nevents) {
|
2005-12-06 16:54:49 +00:00
|
|
|
/*
|
2008-06-23 19:41:20 +00:00
|
|
|
* This is not an error, but something unexpected. If this
|
|
|
|
* happens, it may indicate the need for increasing
|
|
|
|
* ISC_SOCKET_MAXEVENTS.
|
2005-12-06 16:54:49 +00:00
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
manager_log(manager, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
|
|
|
|
"maximum number of FD events (%d) received",
|
|
|
|
nevents);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
|
|
|
REQUIRE(events[i].ident < manager->maxsocks);
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
if (events[i].ident == (uintptr_t)manager->pipe_fds[0]) {
|
|
|
|
done = process_ctlfd(manager);
|
2000-08-29 23:58:17 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
|
|
|
readable = ISC_TF(events[i].filter == EVFILT_READ);
|
|
|
|
writable = ISC_TF(events[i].filter == EVFILT_WRITE);
|
|
|
|
process_fd(manager, events[i].ident, readable, writable);
|
|
|
|
}
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
return (done);
|
|
|
|
}
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
static isc_boolean_t
|
|
|
|
process_fds(isc_socketmgr_t *manager, struct epoll_event *events, int nevents) {
|
|
|
|
int i;
|
|
|
|
isc_boolean_t done = ISC_FALSE;
|
|
|
|
|
|
|
|
if (nevents == manager->nevents) {
|
|
|
|
manager_log(manager, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
|
|
|
|
"maximum number of FD events (%d) received",
|
|
|
|
nevents);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
|
|
|
REQUIRE(events[i].data.fd < (int)manager->maxsocks);
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
if (events[i].data.fd == manager->pipe_fds[0]) {
|
|
|
|
done = process_ctlfd(manager);
|
|
|
|
continue;
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
|
|
|
if ((events[i].events & EPOLLERR) != 0 ||
|
|
|
|
(events[i].events & EPOLLHUP) != 0) {
|
|
|
|
/*
|
|
|
|
* epoll does not set IN/OUT bits on an erroneous
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
events[i].events |= (EPOLLIN | EPOLLOUT);
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
process_fd(manager, events[i].data.fd,
|
|
|
|
(events[i].events & EPOLLIN) != 0,
|
|
|
|
(events[i].events & EPOLLOUT) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (done);
|
|
|
|
}
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
static isc_boolean_t
|
|
|
|
process_fds(isc_socketmgr_t *manager, struct pollfd *events, int nevents) {
|
|
|
|
int i;
|
|
|
|
isc_boolean_t done = ISC_FALSE;
|
|
|
|
|
|
|
|
if (nevents == manager->nevents) {
|
|
|
|
manager_log(manager, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_INFO,
|
|
|
|
"maximum number of FD events (%d) received",
|
|
|
|
nevents);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nevents; i++) {
|
|
|
|
REQUIRE(events[i].fd < (int)manager->maxsocks);
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
if (events[i].fd == manager->pipe_fds[0]) {
|
|
|
|
done = process_ctlfd(manager);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
process_fd(manager, events[i].fd,
|
|
|
|
(events[i].events & POLLIN) != 0,
|
|
|
|
(events[i].events & POLLOUT) != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (done);
|
|
|
|
}
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
static void
|
|
|
|
process_fds(isc_socketmgr_t *manager, int maxfd,
|
|
|
|
fd_set *readfds, fd_set *writefds)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
REQUIRE(maxfd <= (int)manager->maxsocks);
|
|
|
|
|
|
|
|
for (i = 0; i < maxfd; i++) {
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
if (i == manager->pipe_fds[0] || i == manager->pipe_fds[1])
|
|
|
|
continue;
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
process_fd(manager, i, FD_ISSET(i, readfds),
|
|
|
|
FD_ISSET(i, writefds));
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
2000-08-29 23:58:17 +00:00
|
|
|
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
2008-06-23 19:41:20 +00:00
|
|
|
static isc_boolean_t
|
|
|
|
process_ctlfd(isc_socketmgr_t *manager) {
|
|
|
|
int msg, fd;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
select_readmsg(manager, &fd, &msg);
|
|
|
|
|
|
|
|
manager_log(manager, IOEVENT,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_WATCHERMSG,
|
|
|
|
"watcher got message %d "
|
|
|
|
"for socket %d"), msg, fd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nothing to read?
|
|
|
|
*/
|
|
|
|
if (msg == SELECT_POKE_NOTHING)
|
|
|
|
return (ISC_FALSE);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
wakeup_socket(manager, fd, msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
1998-11-06 01:45:35 +00:00
|
|
|
* This is the thread that will loop forever, always in a select or poll
|
|
|
|
* call.
|
|
|
|
*
|
1998-11-03 00:54:47 +00:00
|
|
|
* 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
|
2000-05-14 22:35:45 +00:00
|
|
|
watcher(void *uap) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager = uap;
|
1998-11-03 00:54:47 +00:00
|
|
|
isc_boolean_t done;
|
|
|
|
int ctlfd;
|
|
|
|
int cc;
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
const char *fnname = "kevent()";
|
|
|
|
#elif defined (USE_EPOLL)
|
|
|
|
const char *fnname = "epoll_wait()";
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
const char *fnname = "ioctl(DP_POLL)";
|
|
|
|
struct dvpoll dvp;
|
|
|
|
#elif defined (USE_SELECT)
|
|
|
|
const char *fnname = "select()";
|
1998-11-03 00:54:47 +00:00
|
|
|
fd_set readfds;
|
|
|
|
fd_set writefds;
|
1998-11-11 01:44:08 +00:00
|
|
|
int maxfd;
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the control fd here. This will never change.
|
|
|
|
*/
|
|
|
|
ctlfd = manager->pipe_fds[0];
|
|
|
|
done = ISC_FALSE;
|
|
|
|
while (!done) {
|
1998-11-11 00:43:14 +00:00
|
|
|
do {
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
cc = kevent(manager->kqueue_fd, NULL, 0,
|
|
|
|
manager->events, manager->nevents, NULL);
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
cc = epoll_wait(manager->epoll_fd, manager->events,
|
|
|
|
manager->nevents, -1);
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
dvp.dp_fds = manager->events;
|
|
|
|
dvp.dp_nfds = manager->nevents;
|
|
|
|
dvp.dp_timeout = -1;
|
|
|
|
cc = ioctl(manager->devpoll_fd, DP_POLL, &dvp);
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
LOCK(&manager->lock);
|
1998-11-11 00:43:14 +00:00
|
|
|
readfds = manager->read_fds;
|
|
|
|
writefds = manager->write_fds;
|
1998-11-11 01:44:08 +00:00
|
|
|
maxfd = manager->maxfd + 1;
|
1998-11-11 00:43:14 +00:00
|
|
|
UNLOCK(&manager->lock);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
cc = select(maxfd, &readfds, &writefds, NULL, NULL);
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif /* USE_KQUEUE */
|
1998-11-11 00:43:14 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (cc < 0 && !SOFT_ERROR(errno)) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
|
|
"%s %s: %s", fnname,
|
|
|
|
isc_msgcat_get(isc_msgcat,
|
|
|
|
ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED,
|
|
|
|
"failed"), strbuf);
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
} while (cc < 0);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#if defined(USE_KQUEUE) || defined (USE_EPOLL) || defined (USE_DEVPOLL)
|
|
|
|
done = process_fds(manager, manager->events, cc);
|
|
|
|
#elif defined(USE_SELECT)
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Process reads on internal, control fd.
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
if (FD_ISSET(ctlfd, &readfds))
|
|
|
|
done = process_ctlfd(manager);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
process_fds(manager, maxfd, &readfds, &writefds);
|
2008-06-23 19:41:20 +00:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2000-12-06 00:30:32 +00:00
|
|
|
manager_log(manager, TRACE,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_EXITING, "watcher exiting"));
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
return ((isc_threadresult_t)0);
|
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new socket manager.
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
setup_watcher(isc_mem_t *mctx, isc_socketmgr_t *manager) {
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
manager->nevents = ISC_SOCKET_MAXEVENTS;
|
|
|
|
manager->events = isc_mem_get(mctx, sizeof(struct kevent) *
|
|
|
|
manager->nevents);
|
|
|
|
if (manager->events == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
manager->kqueue_fd = kqueue();
|
|
|
|
if (manager->kqueue_fd == -1) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct kevent) * manager->nevents);
|
|
|
|
return (result);
|
|
|
|
}
|
2008-06-23 23:47:11 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
close(manager->kqueue_fd);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct kevent) * manager->nevents);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
manager->nevents = ISC_SOCKET_MAXEVENTS;
|
|
|
|
manager->events = isc_mem_get(mctx, sizeof(struct epoll_event) *
|
|
|
|
manager->nevents);
|
|
|
|
if (manager->events == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
manager->epoll_fd = epoll_create(manager->nevents);
|
|
|
|
if (manager->epoll_fd == -1) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct epoll_event) * manager->nevents);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
close(manager->epoll_fd);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct epoll_event) * manager->nevents);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
/*
|
|
|
|
* XXXJT: /dev/poll seems to reject large numbers of events,
|
|
|
|
* so we should be careful about redefining ISC_SOCKET_MAXEVENTS.
|
|
|
|
*/
|
|
|
|
manager->nevents = ISC_SOCKET_MAXEVENTS;
|
|
|
|
manager->events = isc_mem_get(mctx, sizeof(struct pollfd) *
|
|
|
|
manager->nevents);
|
|
|
|
if (manager->events == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
/*
|
|
|
|
* Note: fdpollinfo should be able to support all possible FDs, so
|
|
|
|
* it must have maxsocks entries (not nevents).
|
|
|
|
*/
|
|
|
|
manager->fdpollinfo = isc_mem_get(mctx, sizeof(pollinfo_t) *
|
|
|
|
manager->maxsocks);
|
|
|
|
if (manager->fdpollinfo == NULL) {
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(pollinfo_t) * manager->maxsocks);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
memset(manager->fdpollinfo, 0, sizeof(pollinfo_t) * manager->maxsocks);
|
|
|
|
manager->devpoll_fd = open("/dev/poll", O_RDWR);
|
|
|
|
if (manager->devpoll_fd == -1) {
|
|
|
|
result = isc__errno2result(errno);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct pollfd) * manager->nevents);
|
|
|
|
isc_mem_put(mctx, manager->fdpollinfo,
|
|
|
|
sizeof(pollinfo_t) * manager->maxsocks);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
result = watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
close(manager->devpoll_fd);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct pollfd) * manager->nevents);
|
|
|
|
isc_mem_put(mctx, manager->fdpollinfo,
|
|
|
|
sizeof(pollinfo_t) * manager->maxsocks);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
UNUSED(mctx);
|
|
|
|
UNUSED(result);
|
|
|
|
|
|
|
|
FD_ZERO(&manager->read_fds);
|
|
|
|
FD_ZERO(&manager->write_fds);
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
(void)watch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
manager->maxfd = manager->pipe_fds[0];
|
|
|
|
#else /* ISC_PLATFORM_USETHREADS */
|
|
|
|
manager->maxfd = 0;
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
#endif /* USE_KQUEUE */
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cleanup_watcher(isc_mem_t *mctx, isc_socketmgr_t *manager) {
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
result = unwatch_fd(manager, manager->pipe_fds[0], SELECT_POKE_READ);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"epoll_ctl(DEL) %s",
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"));
|
|
|
|
}
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
close(manager->kqueue_fd);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct kevent) * manager->nevents);
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
close(manager->epoll_fd);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct epoll_event) * manager->nevents);
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
close(manager->devpoll_fd);
|
|
|
|
isc_mem_put(mctx, manager->events,
|
|
|
|
sizeof(struct pollfd) * manager->nevents);
|
|
|
|
isc_mem_put(mctx, manager->fdpollinfo,
|
|
|
|
sizeof(pollinfo_t) * manager->maxsocks);
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
UNUSED(mctx);
|
|
|
|
UNUSED(manager);
|
|
|
|
#endif /* USE_KQUEUE */
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
isc_result_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp) {
|
2008-06-23 19:41:20 +00:00
|
|
|
int i;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager;
|
2001-08-31 05:57:58 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
#endif
|
2005-07-12 01:00:20 +00:00
|
|
|
isc_result_t result;
|
2008-06-24 01:58:16 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
REQUIRE(managerp != NULL && *managerp == NULL);
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
|
|
|
if (socketmgr != NULL) {
|
|
|
|
socketmgr->refs++;
|
|
|
|
*managerp = socketmgr;
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
manager = isc_mem_get(mctx, sizeof(*manager));
|
1998-11-03 00:54:47 +00:00
|
|
|
if (manager == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
/* zero-clear so that necessary cleanup on failure will be easy */
|
|
|
|
memset(manager, 0, sizeof(*manager));
|
|
|
|
|
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
|
|
|
manager->maxsocks = ISC_SOCKET_MAXSOCKETS;
|
|
|
|
#elif defined (USE_SELECT)
|
|
|
|
manager->maxsocks = FD_SETSIZE;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
manager->fds = isc_mem_get(mctx,
|
|
|
|
manager->maxsocks * sizeof(isc_socket_t *));
|
|
|
|
if (manager->fds == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto free_manager;
|
|
|
|
}
|
|
|
|
manager->fdstate = isc_mem_get(mctx, manager->maxsocks * sizeof(int));
|
|
|
|
if (manager->fds == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto free_manager;
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
manager->magic = SOCKET_MANAGER_MAGIC;
|
2000-06-21 00:01:52 +00:00
|
|
|
manager->mctx = NULL;
|
2008-06-23 19:41:20 +00:00
|
|
|
memset(manager->fds, 0, manager->maxsocks * sizeof(isc_socket_t *));
|
2000-06-02 01:27:27 +00:00
|
|
|
ISC_LIST_INIT(manager->socklist);
|
2005-07-12 01:00:20 +00:00
|
|
|
result = isc_mutex_init(&manager->lock);
|
2008-06-23 19:41:20 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto free_manager;
|
|
|
|
manager->fdlock = isc_mem_get(mctx, FDLOCK_COUNT * sizeof(isc_mutex_t));
|
|
|
|
if (manager->fdlock == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
goto cleanup_lock;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
2008-06-23 19:41:20 +00:00
|
|
|
for (i = 0; i < FDLOCK_COUNT; i++) {
|
|
|
|
result = isc_mutex_init(&manager->fdlock[i]);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
while (--i >= 0)
|
|
|
|
DESTROYLOCK(&manager->fdlock[i]);
|
|
|
|
isc_mem_put(mctx, manager->fdlock,
|
|
|
|
FDLOCK_COUNT * sizeof(isc_mutex_t));
|
|
|
|
manager->fdlock = NULL;
|
|
|
|
goto cleanup_lock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
1999-05-17 22:31:26 +00:00
|
|
|
if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"isc_condition_init() %s",
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"));
|
2008-06-23 19:41:20 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
|
|
|
goto cleanup_lock;
|
1999-05-17 22:31:26 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Create the special fds that will be used to wake up the
|
|
|
|
* select/poll loop when something internal needs to be done.
|
|
|
|
*/
|
|
|
|
if (pipe(manager->pipe_fds) != 0) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1998-11-03 00:54:47 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"pipe() %s: %s",
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"),
|
2001-08-31 05:57:58 +00:00
|
|
|
strbuf);
|
2008-06-23 19:41:20 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
|
|
|
goto cleanup_condition;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
1999-01-06 20:02:52 +00:00
|
|
|
RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
|
2000-03-15 19:48:11 +00:00
|
|
|
#if 0
|
1999-01-06 20:02:52 +00:00
|
|
|
RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
|
2000-03-15 19:48:11 +00:00
|
|
|
#endif
|
2000-09-28 21:31:08 +00:00
|
|
|
#else /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
manager->refs = 1;
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Set up initial state for the select loop
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
result = setup_watcher(mctx, manager);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup;
|
|
|
|
memset(manager->fdstate, 0, manager->maxsocks * sizeof(int));
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Start up the select/poll thread.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (isc_thread_create(watcher, manager, &manager->watcher) !=
|
1998-11-03 00:54:47 +00:00
|
|
|
ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"isc_thread_create() %s",
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"));
|
2008-06-23 19:41:20 +00:00
|
|
|
cleanup_watcher(mctx, manager);
|
|
|
|
result = ISC_R_UNEXPECTED;
|
|
|
|
goto cleanup;
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
2000-06-21 00:01:52 +00:00
|
|
|
isc_mem_attach(mctx, &manager->mctx);
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
|
|
|
socketmgr = manager;
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-03 00:54:47 +00:00
|
|
|
*managerp = manager;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
(void)close(manager->pipe_fds[0]);
|
|
|
|
(void)close(manager->pipe_fds[1]);
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
|
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
|
|
|
cleanup_condition:
|
|
|
|
(void)isc_condition_destroy(&manager->shutdown_ok);
|
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
|
|
|
|
|
|
|
|
|
|
|
cleanup_lock:
|
|
|
|
if (manager->fdlock != NULL) {
|
|
|
|
for (i = 0; i < FDLOCK_COUNT; i++)
|
|
|
|
DESTROYLOCK(&manager->fdlock[i]);
|
|
|
|
}
|
|
|
|
DESTROYLOCK(&manager->lock);
|
|
|
|
|
|
|
|
free_manager:
|
|
|
|
if (manager->fdlock != NULL) {
|
|
|
|
isc_mem_put(mctx, manager->fdlock,
|
|
|
|
FDLOCK_COUNT * sizeof(isc_mutex_t));
|
|
|
|
}
|
|
|
|
if (manager->fdstate != NULL) {
|
|
|
|
isc_mem_put(mctx, manager->fdstate,
|
|
|
|
manager->maxsocks * sizeof(int));
|
|
|
|
}
|
|
|
|
if (manager->fds != NULL) {
|
|
|
|
isc_mem_put(mctx, manager->fds,
|
|
|
|
manager->maxsocks * sizeof(isc_socket_t *));
|
|
|
|
}
|
|
|
|
isc_mem_put(mctx, manager, sizeof(*manager));
|
|
|
|
|
|
|
|
return (result);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
2008-07-11 23:05:46 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_socketmgr_getmaxsockets(isc_socketmgr_t *manager, unsigned int *nsockp) {
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(nsockp != NULL);
|
|
|
|
|
|
|
|
*nsockp = manager->maxsocks;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
void
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socketmgr_destroy(isc_socketmgr_t **managerp) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager;
|
1998-12-05 00:28:13 +00:00
|
|
|
int i;
|
2000-06-21 00:01:52 +00:00
|
|
|
isc_mem_t *mctx;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a socket manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL);
|
|
|
|
manager = *managerp;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
|
|
|
if (manager->refs > 1) {
|
|
|
|
manager->refs--;
|
|
|
|
*managerp = NULL;
|
|
|
|
return;
|
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
LOCK(&manager->lock);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
1998-12-18 04:03:11 +00:00
|
|
|
/*
|
1999-05-17 22:31:26 +00:00
|
|
|
* Wait for all sockets to be destroyed.
|
1998-12-18 04:03:11 +00:00
|
|
|
*/
|
2000-06-02 01:27:27 +00:00
|
|
|
while (!ISC_LIST_EMPTY(manager->socklist)) {
|
2000-12-06 00:30:32 +00:00
|
|
|
manager_log(manager, CREATION,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_SOCKETSREMAIN,
|
|
|
|
"sockets exist"));
|
1999-05-17 22:31:26 +00:00
|
|
|
WAIT(&manager->shutdown_ok, &manager->lock);
|
1998-12-18 04:03:11 +00:00
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#else /* ISC_PLATFORM_USETHREADS */
|
2000-08-29 23:58:17 +00:00
|
|
|
/*
|
|
|
|
* Hope all sockets have been destroyed.
|
|
|
|
*/
|
|
|
|
if (!ISC_LIST_EMPTY(manager->socklist)) {
|
2000-12-06 00:30:32 +00:00
|
|
|
manager_log(manager, CREATION,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_SOCKETSREMAIN,
|
|
|
|
"sockets exist"));
|
2000-08-29 23:58:17 +00:00
|
|
|
INSIST(0);
|
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Here, poke our select/poll thread. Do this by closing the write
|
|
|
|
* half of the pipe, which will send EOF to the read half.
|
2000-08-29 23:58:17 +00:00
|
|
|
* This is currently a no-op in the non-threaded case.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(manager, 0, SELECT_POKE_SHUTDOWN);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Wait for thread to exit.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (isc_thread_join(manager->watcher, NULL) != ISC_R_SUCCESS)
|
1998-11-03 00:54:47 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-12-06 00:30:32 +00:00
|
|
|
"isc_thread_join() %s",
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"));
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
2008-06-23 19:41:20 +00:00
|
|
|
cleanup_watcher(manager->mctx, manager);
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifdef ISC_PLATFORM_USETHREADS
|
2001-11-30 01:59:49 +00:00
|
|
|
(void)close(manager->pipe_fds[0]);
|
|
|
|
(void)close(manager->pipe_fds[1]);
|
2000-08-29 23:58:17 +00:00
|
|
|
(void)isc_condition_destroy(&manager->shutdown_ok);
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
for (i = 0; i < (int)manager->maxsocks; i++)
|
|
|
|
if (manager->fdstate[i] == CLOSE_PENDING) /* no need to lock */
|
2001-11-30 01:59:49 +00:00
|
|
|
(void)close(i);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
isc_mem_put(manager->mctx, manager->fds,
|
|
|
|
manager->maxsocks * sizeof(isc_socket_t *));
|
|
|
|
isc_mem_put(manager->mctx, manager->fdstate,
|
|
|
|
manager->maxsocks * sizeof(int));
|
|
|
|
|
|
|
|
if (manager->fdlock != NULL) {
|
|
|
|
for (i = 0; i < FDLOCK_COUNT; i++)
|
|
|
|
DESTROYLOCK(&manager->fdlock[i]);
|
|
|
|
isc_mem_put(manager->mctx, manager->fdlock,
|
|
|
|
FDLOCK_COUNT * sizeof(isc_mutex_t));
|
|
|
|
}
|
2000-08-26 01:31:56 +00:00
|
|
|
DESTROYLOCK(&manager->lock);
|
1998-11-03 00:54:47 +00:00
|
|
|
manager->magic = 0;
|
2000-06-21 00:01:52 +00:00
|
|
|
mctx= manager->mctx;
|
2001-11-27 01:56:32 +00:00
|
|
|
isc_mem_put(mctx, manager, sizeof(*manager));
|
2000-06-21 00:01:52 +00:00
|
|
|
|
|
|
|
isc_mem_detach(&mctx);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
static isc_result_t
|
|
|
|
socket_recv(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
2001-01-25 22:25:10 +00:00
|
|
|
int io_state;
|
|
|
|
isc_boolean_t have_lock = ISC_FALSE;
|
2001-01-27 06:41:36 +00:00
|
|
|
isc_task_t *ntask = NULL;
|
2001-02-12 21:43:17 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = task;
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
if (sock->type == isc_sockettype_udp) {
|
|
|
|
io_state = doio_recv(sock, dev);
|
|
|
|
} else {
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
have_lock = ISC_TRUE;
|
|
|
|
|
|
|
|
if (ISC_LIST_EMPTY(sock->recv_list))
|
|
|
|
io_state = doio_recv(sock, dev);
|
|
|
|
else
|
|
|
|
io_state = DOIO_SOFT;
|
|
|
|
}
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
switch (io_state) {
|
1999-09-03 16:38:08 +00:00
|
|
|
case DOIO_SOFT:
|
2001-01-25 22:25:10 +00:00
|
|
|
/*
|
|
|
|
* We couldn't read all or part of the request right now, so
|
|
|
|
* queue it.
|
|
|
|
*
|
|
|
|
* Attach to socket and to task
|
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
|
|
|
|
|
|
|
|
if (!have_lock) {
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
have_lock = ISC_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enqueue the request. If the socket was previously not being
|
|
|
|
* watched, poke the watcher to start paying attention to it.
|
|
|
|
*/
|
|
|
|
if (ISC_LIST_EMPTY(sock->recv_list))
|
|
|
|
select_poke(sock->manager, sock->fd, SELECT_POKE_READ);
|
|
|
|
ISC_LIST_ENQUEUE(sock->recv_list, dev, ev_link);
|
|
|
|
|
|
|
|
socket_log(sock, NULL, EVENT, NULL, 0, 0,
|
2001-01-27 06:41:36 +00:00
|
|
|
"socket_recv: event %p -> task %p",
|
2001-01-25 22:25:10 +00:00
|
|
|
dev, ntask);
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
|
|
|
|
result = ISC_R_INPROGRESS;
|
2001-01-25 22:25:10 +00:00
|
|
|
break;
|
1999-09-03 16:38:08 +00:00
|
|
|
|
|
|
|
case DOIO_EOF:
|
2001-02-12 21:43:17 +00:00
|
|
|
dev->result = ISC_R_EOF;
|
|
|
|
/* fallthrough */
|
1999-09-03 16:38:08 +00:00
|
|
|
|
|
|
|
case DOIO_HARD:
|
|
|
|
case DOIO_SUCCESS:
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
|
|
|
|
send_recvdone_event(sock, &dev);
|
2001-01-25 22:25:10 +00:00
|
|
|
break;
|
1999-09-03 16:38:08 +00:00
|
|
|
}
|
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
if (have_lock)
|
|
|
|
UNLOCK(&sock->lock);
|
1999-09-03 16:38:08 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (result);
|
1999-09-02 02:10:44 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
isc_result_t
|
2001-01-27 06:41:36 +00:00
|
|
|
isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
|
|
|
|
unsigned int minimum, isc_task_t *task,
|
|
|
|
isc_taskaction_t action, const void *arg)
|
1998-11-06 01:45:35 +00:00
|
|
|
{
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_socketevent_t *dev;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager;
|
2001-01-27 06:41:36 +00:00
|
|
|
unsigned int iocount;
|
|
|
|
isc_buffer_t *buffer;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
2001-01-27 06:41:36 +00:00
|
|
|
REQUIRE(buflist != NULL);
|
|
|
|
REQUIRE(!ISC_LIST_EMPTY(*buflist));
|
1999-07-28 23:04:33 +00:00
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
manager = sock->manager;
|
1999-07-28 01:07:21 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
2001-01-27 06:41:36 +00:00
|
|
|
iocount = isc_bufferlist_availablecount(buflist);
|
|
|
|
REQUIRE(iocount > 0);
|
|
|
|
|
2000-05-10 21:17:49 +00:00
|
|
|
INSIST(sock->bound);
|
|
|
|
|
1999-09-02 18:56:11 +00:00
|
|
|
dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
|
1999-07-28 01:07:21 +00:00
|
|
|
if (dev == NULL) {
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-01-27 06:41:36 +00:00
|
|
|
* UDP sockets are always partial read
|
1998-11-06 01:45:35 +00:00
|
|
|
*/
|
1999-07-13 01:49:33 +00:00
|
|
|
if (sock->type == isc_sockettype_udp)
|
1999-07-28 21:30:37 +00:00
|
|
|
dev->minimum = 1;
|
|
|
|
else {
|
|
|
|
if (minimum == 0)
|
2001-01-27 06:41:36 +00:00
|
|
|
dev->minimum = iocount;
|
1999-07-28 21:30:37 +00:00
|
|
|
else
|
|
|
|
dev->minimum = minimum;
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
2001-01-27 06:41:36 +00:00
|
|
|
/*
|
|
|
|
* Move each buffer from the passed in list to our internal one.
|
|
|
|
*/
|
|
|
|
buffer = ISC_LIST_HEAD(*buflist);
|
|
|
|
while (buffer != NULL) {
|
|
|
|
ISC_LIST_DEQUEUE(*buflist, buffer, link);
|
|
|
|
ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
|
|
|
|
buffer = ISC_LIST_HEAD(*buflist);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (socket_recv(sock, dev, task, 0));
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
isc_result_t
|
2001-01-27 06:41:36 +00:00
|
|
|
isc_socket_recv(isc_socket_t *sock, isc_region_t *region, unsigned int minimum,
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg)
|
1998-11-11 00:43:14 +00:00
|
|
|
{
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_socketevent_t *dev;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1998-12-18 02:28:27 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1999-07-28 23:04:33 +00:00
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
2000-05-10 21:17:49 +00:00
|
|
|
INSIST(sock->bound);
|
|
|
|
|
2001-01-27 06:41:36 +00:00
|
|
|
dev = allocate_socketevent(sock, ISC_SOCKEVENT_RECVDONE, action, arg);
|
2001-02-12 21:43:17 +00:00
|
|
|
if (dev == NULL)
|
1999-09-06 04:44:40 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
2001-02-12 21:43:17 +00:00
|
|
|
|
|
|
|
return (isc_socket_recv2(sock, region, minimum, task, dev, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
unsigned int minimum, isc_task_t *task,
|
|
|
|
isc_socketevent_t *event, unsigned int flags)
|
|
|
|
{
|
|
|
|
event->ev_sender = sock;
|
|
|
|
event->result = ISC_R_UNEXPECTED;
|
|
|
|
ISC_LIST_INIT(event->bufferlist);
|
|
|
|
event->region = *region;
|
|
|
|
event->n = 0;
|
|
|
|
event->offset = 0;
|
|
|
|
event->attributes = 0;
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2001-01-27 06:41:36 +00:00
|
|
|
/*
|
|
|
|
* UDP sockets are always partial read.
|
|
|
|
*/
|
|
|
|
if (sock->type == isc_sockettype_udp)
|
2001-02-12 21:43:17 +00:00
|
|
|
event->minimum = 1;
|
2001-01-27 06:41:36 +00:00
|
|
|
else {
|
|
|
|
if (minimum == 0)
|
2001-02-12 21:43:17 +00:00
|
|
|
event->minimum = region->length;
|
2001-01-27 06:41:36 +00:00
|
|
|
else
|
2001-02-12 21:43:17 +00:00
|
|
|
event->minimum = minimum;
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (socket_recv(sock, event, task, flags));
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
static isc_result_t
|
2001-01-27 06:41:36 +00:00
|
|
|
socket_send(isc_socket_t *sock, isc_socketevent_t *dev, isc_task_t *task,
|
2001-02-12 21:43:17 +00:00
|
|
|
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
|
|
|
|
unsigned int flags)
|
2001-01-27 06:41:36 +00:00
|
|
|
{
|
|
|
|
int io_state;
|
|
|
|
isc_boolean_t have_lock = ISC_FALSE;
|
|
|
|
isc_task_t *ntask = NULL;
|
2001-02-12 21:43:17 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2001-01-27 06:41:36 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = task;
|
1999-09-06 04:44:40 +00:00
|
|
|
|
|
|
|
set_dev_address(address, sock, dev);
|
1999-12-15 20:47:38 +00:00
|
|
|
if (pktinfo != NULL) {
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_PKTINFO;
|
|
|
|
dev->pktinfo = *pktinfo;
|
2002-04-03 06:44:11 +00:00
|
|
|
|
2005-11-30 03:33:49 +00:00
|
|
|
if (!isc_sockaddr_issitelocal(&dev->address) &&
|
|
|
|
!isc_sockaddr_islinklocal(&dev->address)) {
|
2002-04-03 06:44:11 +00:00
|
|
|
socket_log(sock, NULL, TRACE, isc_msgcat,
|
|
|
|
ISC_MSGSET_SOCKET, ISC_MSG_PKTINFOPROVIDED,
|
|
|
|
"pktinfo structure provided, ifindex %u "
|
|
|
|
"(set to 0)", pktinfo->ipi6_ifindex);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the pktinfo index to 0 here, to let the
|
|
|
|
* kernel decide what interface it should send on.
|
|
|
|
*/
|
|
|
|
dev->pktinfo.ipi6_ifindex = 0;
|
|
|
|
}
|
1999-12-15 20:47:38 +00:00
|
|
|
}
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2001-01-27 06:41:36 +00:00
|
|
|
if (sock->type == isc_sockettype_udp)
|
2001-01-25 22:25:10 +00:00
|
|
|
io_state = doio_send(sock, dev);
|
2001-01-27 06:41:36 +00:00
|
|
|
else {
|
2001-01-25 22:25:10 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
have_lock = ISC_TRUE;
|
|
|
|
|
|
|
|
if (ISC_LIST_EMPTY(sock->send_list))
|
|
|
|
io_state = doio_send(sock, dev);
|
|
|
|
else
|
|
|
|
io_state = DOIO_SOFT;
|
|
|
|
}
|
1999-09-06 04:44:40 +00:00
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
switch (io_state) {
|
1999-09-06 04:44:40 +00:00
|
|
|
case DOIO_SOFT:
|
2001-01-25 22:25:10 +00:00
|
|
|
/*
|
|
|
|
* We couldn't send all or part of the request right now, so
|
2001-03-06 01:23:03 +00:00
|
|
|
* queue it unless ISC_SOCKFLAG_NORETRY is set.
|
2001-01-25 22:25:10 +00:00
|
|
|
*/
|
2001-03-06 01:23:03 +00:00
|
|
|
if ((flags & ISC_SOCKFLAG_NORETRY) == 0) {
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
dev->attributes |= ISC_SOCKEVENTATTR_ATTACHED;
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
if (!have_lock) {
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
have_lock = ISC_TRUE;
|
|
|
|
}
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
/*
|
|
|
|
* Enqueue the request. If the socket was previously
|
|
|
|
* not being watched, poke the watcher to start
|
|
|
|
* paying attention to it.
|
|
|
|
*/
|
|
|
|
if (ISC_LIST_EMPTY(sock->send_list))
|
|
|
|
select_poke(sock->manager, sock->fd,
|
|
|
|
SELECT_POKE_WRITE);
|
|
|
|
ISC_LIST_ENQUEUE(sock->send_list, dev, ev_link);
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
socket_log(sock, NULL, EVENT, NULL, 0, 0,
|
|
|
|
"socket_send: event %p -> task %p",
|
|
|
|
dev, ntask);
|
2001-01-25 22:25:10 +00:00
|
|
|
|
2001-03-06 01:23:03 +00:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) != 0)
|
|
|
|
result = ISC_R_INPROGRESS;
|
|
|
|
break;
|
|
|
|
}
|
1999-09-06 04:44:40 +00:00
|
|
|
|
|
|
|
case DOIO_HARD:
|
|
|
|
case DOIO_SUCCESS:
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((flags & ISC_SOCKFLAG_IMMEDIATE) == 0)
|
|
|
|
send_senddone_event(sock, &dev);
|
2001-01-25 22:25:10 +00:00
|
|
|
break;
|
1999-09-06 04:44:40 +00:00
|
|
|
}
|
|
|
|
|
2001-01-25 22:25:10 +00:00
|
|
|
if (have_lock)
|
|
|
|
UNLOCK(&sock->lock);
|
2001-02-12 21:43:17 +00:00
|
|
|
|
|
|
|
return (result);
|
2001-01-27 06:41:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_send(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* REQUIRE() checking is performed in isc_socket_sendto().
|
|
|
|
*/
|
|
|
|
return (isc_socket_sendto(sock, region, task, action, arg, NULL,
|
|
|
|
NULL));
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg,
|
|
|
|
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
|
|
|
|
{
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(region != NULL);
|
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
INSIST(sock->bound);
|
|
|
|
|
|
|
|
dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
|
|
|
|
if (dev == NULL) {
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->region = *region;
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (socket_send(sock, dev, task, address, pktinfo, 0));
|
1999-09-06 04:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg)
|
1999-09-06 04:44:40 +00:00
|
|
|
{
|
1999-12-04 01:27:44 +00:00
|
|
|
return (isc_socket_sendtov(sock, buflist, task, action, arg, NULL,
|
|
|
|
NULL));
|
1999-09-06 04:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg,
|
1999-12-04 01:27:44 +00:00
|
|
|
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo)
|
1999-09-06 04:44:40 +00:00
|
|
|
{
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
unsigned int iocount;
|
|
|
|
isc_buffer_t *buffer;
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(buflist != NULL);
|
|
|
|
REQUIRE(!ISC_LIST_EMPTY(*buflist));
|
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
manager = sock->manager;
|
1999-09-06 04:44:40 +00:00
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
iocount = isc_bufferlist_usedcount(buflist);
|
|
|
|
REQUIRE(iocount > 0);
|
|
|
|
|
1999-09-02 18:56:11 +00:00
|
|
|
dev = allocate_socketevent(sock, ISC_SOCKEVENT_SENDDONE, action, arg);
|
1999-07-28 06:50:10 +00:00
|
|
|
if (dev == NULL) {
|
1998-11-11 00:43:14 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
1999-09-06 04:44:40 +00:00
|
|
|
/*
|
|
|
|
* Move each buffer from the passed in list to our internal one.
|
|
|
|
*/
|
|
|
|
buffer = ISC_LIST_HEAD(*buflist);
|
|
|
|
while (buffer != NULL) {
|
|
|
|
ISC_LIST_DEQUEUE(*buflist, buffer, link);
|
|
|
|
ISC_LIST_ENQUEUE(dev->bufferlist, buffer, link);
|
|
|
|
buffer = ISC_LIST_HEAD(*buflist);
|
|
|
|
}
|
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
return (socket_send(sock, dev, task, address, pktinfo, 0));
|
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
isc_task_t *task,
|
|
|
|
isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
|
|
|
|
isc_socketevent_t *event, unsigned int flags)
|
|
|
|
{
|
2001-03-06 01:23:03 +00:00
|
|
|
REQUIRE((flags & ~(ISC_SOCKFLAG_IMMEDIATE|ISC_SOCKFLAG_NORETRY)) == 0);
|
|
|
|
if ((flags & ISC_SOCKFLAG_NORETRY) != 0)
|
|
|
|
REQUIRE(sock->type == isc_sockettype_udp);
|
2001-02-12 21:43:17 +00:00
|
|
|
event->ev_sender = sock;
|
|
|
|
event->result = ISC_R_UNEXPECTED;
|
|
|
|
ISC_LIST_INIT(event->bufferlist);
|
|
|
|
event->region = *region;
|
|
|
|
event->n = 0;
|
|
|
|
event->offset = 0;
|
|
|
|
event->attributes = 0;
|
|
|
|
|
|
|
|
return (socket_send(sock, event, task, address, pktinfo, flags));
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
2005-02-23 01:09:23 +00:00
|
|
|
void
|
|
|
|
isc_socket_cleanunix(isc_sockaddr_t *sockaddr, isc_boolean_t active) {
|
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
int s;
|
|
|
|
struct stat sb;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
|
|
|
|
if (sockaddr->type.sa.sa_family != AF_UNIX)
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifndef S_ISSOCK
|
|
|
|
#if defined(S_IFMT) && defined(S_IFSOCK)
|
|
|
|
#define S_ISSOCK(mode) ((mode & S_IFMT)==S_IFSOCK)
|
|
|
|
#elif defined(_S_IFMT) && defined(S_IFSOCK)
|
|
|
|
#define S_ISSOCK(mode) ((mode & _S_IFMT)==S_IFSOCK)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISFIFO
|
|
|
|
#if defined(S_IFMT) && defined(S_IFIFO)
|
|
|
|
#define S_ISFIFO(mode) ((mode & S_IFMT)==S_IFIFO)
|
|
|
|
#elif defined(_S_IFMT) && defined(S_IFIFO)
|
|
|
|
#define S_ISFIFO(mode) ((mode & _S_IFMT)==S_IFIFO)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(S_ISFIFO) && !defined(S_ISSOCK)
|
|
|
|
#error You need to define S_ISFIFO and S_ISSOCK as appropriate for your platform. See <sys/stat.h>.
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISFIFO
|
|
|
|
#define S_ISFIFO(mode) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef S_ISSOCK
|
|
|
|
#define S_ISSOCK(mode) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (active) {
|
2005-03-16 23:39:06 +00:00
|
|
|
if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
|
2005-02-23 01:09:23 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_cleanunix: stat(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_cleanunix: %s: not a socket",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path);
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2005-03-16 23:39:06 +00:00
|
|
|
if (unlink(sockaddr->type.sunix.sun_path) < 0) {
|
2005-02-23 01:09:23 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_cleanunix: unlink(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (s < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: socket(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-03-16 23:39:06 +00:00
|
|
|
if (stat(sockaddr->type.sunix.sun_path, &sb) < 0) {
|
2005-02-23 01:09:23 +00:00
|
|
|
switch (errno) {
|
|
|
|
case ENOENT: /* We exited cleanly last time */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: stat(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(S_ISSOCK(sb.st_mode) || S_ISFIFO(sb.st_mode))) {
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: %s: not a socket",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path);
|
2005-02-23 01:09:23 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2005-03-16 23:39:06 +00:00
|
|
|
if (connect(s, (struct sockaddr *)&sockaddr->type.sunix,
|
|
|
|
sizeof(sockaddr->type.sunix)) < 0) {
|
2005-02-23 01:09:23 +00:00
|
|
|
switch (errno) {
|
|
|
|
case ECONNREFUSED:
|
|
|
|
case ECONNRESET:
|
2005-03-16 23:39:06 +00:00
|
|
|
if (unlink(sockaddr->type.sunix.sun_path) < 0) {
|
2005-02-23 01:09:23 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET,
|
|
|
|
ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: "
|
|
|
|
"unlink(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path,
|
2005-02-23 01:09:23 +00:00
|
|
|
strbuf);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_WARNING,
|
|
|
|
"isc_socket_cleanunix: connect(%s): %s",
|
2005-03-16 23:39:06 +00:00
|
|
|
sockaddr->type.sunix.sun_path, strbuf);
|
2005-02-23 01:09:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cleanup:
|
|
|
|
close(s);
|
|
|
|
#else
|
|
|
|
UNUSED(sockaddr);
|
|
|
|
UNUSED(active);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2005-10-17 03:47:10 +00:00
|
|
|
isc_result_t
|
2005-02-23 01:09:23 +00:00
|
|
|
isc_socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
|
|
|
|
isc_uint32_t owner, isc_uint32_t group)
|
|
|
|
{
|
|
|
|
#ifdef ISC_PLATFORM_HAVESYSUNH
|
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2005-03-16 23:39:06 +00:00
|
|
|
char path[sizeof(sockaddr->type.sunix.sun_path)];
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef NEED_SECURE_DIRECTORY
|
|
|
|
char *slash;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
REQUIRE(sockaddr->type.sa.sa_family == AF_UNIX);
|
2005-03-16 23:39:06 +00:00
|
|
|
INSIST(strlen(sockaddr->type.sunix.sun_path) < sizeof(path));
|
|
|
|
strcpy(path, sockaddr->type.sunix.sun_path);
|
2005-02-23 01:09:23 +00:00
|
|
|
|
|
|
|
#ifdef NEED_SECURE_DIRECTORY
|
|
|
|
slash = strrchr(path, '/');
|
|
|
|
if (slash != NULL) {
|
|
|
|
if (slash != path)
|
|
|
|
*slash = '\0';
|
|
|
|
else
|
|
|
|
strcpy(path, "/");
|
|
|
|
} else
|
|
|
|
strcpy(path, ".");
|
|
|
|
#endif
|
2008-01-18 23:46:58 +00:00
|
|
|
|
2005-02-23 01:09:23 +00:00
|
|
|
if (chmod(path, perm) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_permunix: chmod(%s, %d): %s",
|
|
|
|
path, perm, strbuf);
|
|
|
|
result = ISC_R_FAILURE;
|
|
|
|
}
|
|
|
|
if (chown(path, owner, group) < 0) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
isc_log_write(isc_lctx, ISC_LOGCATEGORY_GENERAL,
|
|
|
|
ISC_LOGMODULE_SOCKET, ISC_LOG_ERROR,
|
|
|
|
"isc_socket_permunix: chown(%s, %d, %d): %s",
|
|
|
|
path, owner, group,
|
|
|
|
strbuf);
|
|
|
|
result = ISC_R_FAILURE;
|
|
|
|
}
|
|
|
|
return (result);
|
|
|
|
#else
|
|
|
|
UNUSED(sockaddr);
|
|
|
|
UNUSED(perm);
|
|
|
|
UNUSED(owner);
|
|
|
|
UNUSED(group);
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
isc_result_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr) {
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-12-05 01:44:38 +00:00
|
|
|
int on = 1;
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2000-05-10 21:17:49 +00:00
|
|
|
INSIST(!sock->bound);
|
|
|
|
|
2000-08-10 23:11:12 +00:00
|
|
|
if (sock->pf != sockaddr->type.sa.sa_family) {
|
|
|
|
UNLOCK(&sock->lock);
|
2000-08-15 01:43:38 +00:00
|
|
|
return (ISC_R_FAMILYMISMATCH);
|
2000-08-10 23:11:12 +00:00
|
|
|
}
|
2004-06-30 23:35:17 +00:00
|
|
|
/*
|
|
|
|
* Only set SO_REUSEADDR when we want a specific port.
|
|
|
|
*/
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef AF_UNIX
|
|
|
|
if (sock->pf == AF_UNIX)
|
|
|
|
goto bind_socket;
|
|
|
|
#endif
|
2004-06-30 23:35:17 +00:00
|
|
|
if (isc_sockaddr_getport(sockaddr) != (in_port_t)0 &&
|
|
|
|
setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR, (void *)&on,
|
2004-07-01 04:53:14 +00:00
|
|
|
sizeof(on)) < 0) {
|
2000-12-06 00:30:32 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"setsockopt(%d) %s", sock->fd,
|
|
|
|
isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
|
|
|
|
ISC_MSG_FAILED, "failed"));
|
2000-02-02 02:34:30 +00:00
|
|
|
/* Press on... */
|
1998-12-05 01:44:38 +00:00
|
|
|
}
|
2005-02-23 01:09:23 +00:00
|
|
|
#ifdef AF_UNIX
|
|
|
|
bind_socket:
|
|
|
|
#endif
|
1999-07-07 02:05:08 +00:00
|
|
|
if (bind(sock->fd, &sockaddr->type.sa, sockaddr->length) < 0) {
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
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:
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "bind: %s",
|
|
|
|
strbuf);
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-06 00:30:32 +00:00
|
|
|
socket_log(sock, sockaddr, TRACE,
|
2000-12-19 20:35:37 +00:00
|
|
|
isc_msgcat, ISC_MSGSET_SOCKET, ISC_MSG_BOUND, "bound");
|
2000-05-10 21:17:49 +00:00
|
|
|
sock->bound = 1;
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2002-05-27 00:40:23 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_socket_filter(isc_socket_t *sock, const char *filter) {
|
|
|
|
#ifdef SO_ACCEPTFILTER
|
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
struct accept_filter_arg afa;
|
|
|
|
#else
|
|
|
|
UNUSED(sock);
|
|
|
|
UNUSED(filter);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
#ifdef SO_ACCEPTFILTER
|
|
|
|
bzero(&afa, sizeof(afa));
|
|
|
|
strncpy(afa.af_name, filter, sizeof(afa.af_name));
|
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_ACCEPTFILTER,
|
|
|
|
&afa, sizeof(afa)) == -1) {
|
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
socket_log(sock, NULL, CREATION, isc_msgcat, ISC_MSGSET_SOCKET,
|
|
|
|
ISC_MSG_FILTER, "setsockopt(SO_ACCEPTFILTER): %s",
|
|
|
|
strbuf);
|
|
|
|
return (ISC_R_FAILURE);
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
#else
|
|
|
|
return (ISC_R_NOTIMPLEMENTED);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
2000-05-10 21:17:49 +00:00
|
|
|
* Set up to listen on a given socket. We do this by creating an internal
|
1998-11-06 01:45:35 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_listen(isc_socket_t *sock, unsigned int backlog) {
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
REQUIRE(!sock->listener);
|
2000-05-10 21:17:49 +00:00
|
|
|
REQUIRE(sock->bound);
|
2005-02-23 01:09:23 +00:00
|
|
|
REQUIRE(sock->type == isc_sockettype_tcp ||
|
|
|
|
sock->type == isc_sockettype_unix);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
if (backlog == 0)
|
|
|
|
backlog = SOMAXCONN;
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
if (listen(sock->fd, (int)backlog) < 0) {
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s", strbuf);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->listener = 1;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-10 11:37:54 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
2008-06-25 00:09:50 +00:00
|
|
|
* This should try to do aggressive accept() XXXMLG
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
1998-11-10 11:37:54 +00:00
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_accept(isc_socket_t *sock,
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg)
|
1998-11-10 11:37:54 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_newconnev_t *dev;
|
|
|
|
isc_socketmgr_t *manager;
|
1999-07-28 01:07:21 +00:00
|
|
|
isc_task_t *ntask = NULL;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *nsock;
|
2005-07-12 01:00:20 +00:00
|
|
|
isc_result_t result;
|
2000-08-29 23:58:17 +00:00
|
|
|
isc_boolean_t do_poke = ISC_FALSE;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
REQUIRE(sock->listener);
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
/*
|
|
|
|
* Sender field is overloaded here with the task we will be sending
|
|
|
|
* this event to. Just before the actual event is delivered the
|
2000-04-17 19:22:44 +00:00
|
|
|
* actual ev_sender will be touched up to be the socket.
|
1999-06-10 00:45:30 +00:00
|
|
|
*/
|
1998-12-13 23:45:21 +00:00
|
|
|
dev = (isc_socket_newconnev_t *)
|
1999-06-10 00:45:30 +00:00
|
|
|
isc_event_allocate(manager->mctx, task, ISC_SOCKEVENT_NEWCONN,
|
2001-11-27 01:56:32 +00:00
|
|
|
action, arg, sizeof(*dev));
|
1998-11-10 11:37:54 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LINK_INIT(dev, ev_link);
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
result = allocate_socket(manager, sock->type, &nsock);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_event_free(ISC_EVENT_PTR(&dev));
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Attach to socket and to task.
|
1998-11-07 02:31:04 +00:00
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
1998-11-26 00:29:12 +00:00
|
|
|
nsock->references++;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = ntask;
|
1998-11-10 11:37:54 +00:00
|
|
|
dev->newsocket = nsock;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Poke watcher here. We still have the socket locked, so there
|
1998-11-10 01:56:44 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
2000-06-02 01:27:27 +00:00
|
|
|
if (ISC_LIST_EMPTY(sock->accept_list))
|
2000-08-29 23:58:17 +00:00
|
|
|
do_poke = ISC_TRUE;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->accept_list, dev, ev_link);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
if (do_poke)
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(manager, sock->fd, SELECT_POKE_ACCEPT);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
isc_result_t
|
1999-07-07 02:05:08 +00:00
|
|
|
isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr,
|
2000-06-01 17:20:56 +00:00
|
|
|
isc_task_t *task, isc_taskaction_t action, const void *arg)
|
1998-11-26 00:10:33 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_connev_t *dev;
|
|
|
|
isc_task_t *ntask = NULL;
|
|
|
|
isc_socketmgr_t *manager;
|
1998-11-26 00:10:33 +00:00
|
|
|
int cc;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1999-07-28 23:04:33 +00:00
|
|
|
REQUIRE(addr != NULL);
|
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(action != NULL);
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
1998-12-01 17:58:34 +00:00
|
|
|
REQUIRE(addr != NULL);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2000-06-15 18:25:49 +00:00
|
|
|
if (isc_sockaddr_ismulticast(addr))
|
|
|
|
return (ISC_R_MULTICAST);
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
REQUIRE(!sock->connecting);
|
|
|
|
|
1999-07-28 23:04:33 +00:00
|
|
|
dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx, sock,
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_SOCKEVENT_CONNECT,
|
1999-07-28 23:04:33 +00:00
|
|
|
action, arg,
|
2001-11-27 01:56:32 +00:00
|
|
|
sizeof(*dev));
|
1998-11-26 00:10:33 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LINK_INIT(dev, ev_link);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to do the connect right away, as there can be only one
|
|
|
|
* outstanding, and it might happen to complete.
|
|
|
|
*/
|
2007-01-03 20:57:07 +00:00
|
|
|
sock->peer_address = *addr;
|
1999-07-07 02:05:08 +00:00
|
|
|
cc = connect(sock->fd, &addr->type.sa, addr->length);
|
1998-11-26 00:10:33 +00:00
|
|
|
if (cc < 0) {
|
2008-06-25 22:56:33 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (sock->type == isc_sockettype_udp && errno == EINPROGRESS) {
|
|
|
|
cc = 0;
|
|
|
|
goto success;
|
|
|
|
}
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno) || errno == EINPROGRESS)
|
1998-11-26 00:10:33 +00:00
|
|
|
goto queue;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-05-21 07:16:13 +00:00
|
|
|
switch (errno) {
|
2000-09-21 21:30:27 +00:00
|
|
|
#define ERROR_MATCH(a, b) case a: dev->result = b; goto err_exit;
|
|
|
|
ERROR_MATCH(EACCES, ISC_R_NOPERM);
|
|
|
|
ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
|
|
|
|
ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
|
|
|
|
ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
|
|
|
|
ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
2000-11-17 21:04:12 +00:00
|
|
|
#ifdef EHOSTDOWN
|
|
|
|
ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
|
|
|
|
#endif
|
2000-09-21 21:30:27 +00:00
|
|
|
ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
|
|
|
|
ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
|
|
|
|
ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
|
2005-03-15 01:41:28 +00:00
|
|
|
ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
|
2000-09-21 21:30:27 +00:00
|
|
|
#undef ERROR_MATCH
|
1999-05-21 07:16:13 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connected = 0;
|
1998-12-18 01:48:43 +00:00
|
|
|
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "%d/%s", errno, strbuf);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_event_free(ISC_EVENT_PTR(&dev));
|
1998-11-26 00:10:33 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
|
1999-05-21 07:16:13 +00:00
|
|
|
err_exit:
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connected = 0;
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_task_send(task, ISC_EVENT_PTR(&dev));
|
1999-05-21 07:16:13 +00:00
|
|
|
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1999-05-21 07:16:13 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-12-18 01:48:43 +00:00
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* If connect completed, fire off the done event.
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
2008-06-25 22:56:33 +00:00
|
|
|
success:
|
1998-11-26 00:10:33 +00:00
|
|
|
if (cc == 0) {
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connected = 1;
|
2000-05-10 21:17:49 +00:00
|
|
|
sock->bound = 1;
|
1998-11-26 00:10:33 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_task_send(task, ISC_EVENT_PTR(&dev));
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 07:02:56 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-26 00:10:33 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
queue:
|
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Attach to task.
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connecting = 1;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = ntask;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
/*
|
2000-08-31 17:10:58 +00:00
|
|
|
* Poke watcher here. We still have the socket locked, so there
|
1998-11-26 00:10:33 +00:00
|
|
|
* 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 (sock->connect_ev == NULL)
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(manager, sock->fd, SELECT_POKE_CONNECT);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
sock->connect_ev = dev;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called when a socket with a pending connect() finishes.
|
|
|
|
*/
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
2000-05-14 22:35:45 +00:00
|
|
|
internal_connect(isc_task_t *me, isc_event_t *ev) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock;
|
|
|
|
isc_socket_connev_t *dev;
|
1999-07-28 01:07:21 +00:00
|
|
|
isc_task_t *task;
|
1998-11-26 00:10:33 +00:00
|
|
|
int cc;
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T optlen;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
2005-03-15 01:41:28 +00:00
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
UNUSED(me);
|
|
|
|
INSIST(ev->ev_type == ISC_SOCKEVENT_INTW);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
sock = ev->ev_sender;
|
1999-07-28 23:04:33 +00:00
|
|
|
INSIST(VALID_SOCKET(sock));
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
/*
|
1999-07-28 01:07:21 +00:00
|
|
|
* When the internal event was sent the reference count was bumped
|
|
|
|
* to keep the socket around for us. Decrement the count here.
|
1998-11-26 00:10:33 +00:00
|
|
|
*/
|
1999-07-28 01:07:21 +00:00
|
|
|
INSIST(sock->references > 0);
|
|
|
|
sock->references--;
|
|
|
|
if (sock->references == 0) {
|
1998-11-26 00:10:33 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1999-07-28 01:07:21 +00:00
|
|
|
destroy(&sock);
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
/*
|
|
|
|
* Has this event been canceled?
|
|
|
|
*/
|
|
|
|
dev = sock->connect_ev;
|
|
|
|
if (dev == NULL) {
|
2000-01-28 01:31:21 +00:00
|
|
|
INSIST(!sock->connecting);
|
1999-07-28 01:07:21 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return;
|
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
2000-01-28 01:31:21 +00:00
|
|
|
INSIST(sock->connecting);
|
|
|
|
sock->connecting = 0;
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* Get any possible error status here.
|
|
|
|
*/
|
|
|
|
optlen = sizeof(cc);
|
|
|
|
if (getsockopt(sock->fd, SOL_SOCKET, SO_ERROR,
|
1999-08-31 20:57:46 +00:00
|
|
|
(void *)&cc, (void *)&optlen) < 0)
|
1998-11-26 00:10:33 +00:00
|
|
|
cc = errno;
|
|
|
|
else
|
|
|
|
errno = cc;
|
|
|
|
|
|
|
|
if (errno != 0) {
|
|
|
|
/*
|
|
|
|
* If the error is EAGAIN, just re-select on this
|
|
|
|
* fd and pretend nothing strange happened.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno) || errno == EINPROGRESS) {
|
1999-06-10 00:57:33 +00:00
|
|
|
sock->connecting = 1;
|
2001-01-25 22:25:10 +00:00
|
|
|
select_poke(sock->manager, sock->fd,
|
|
|
|
SELECT_POKE_CONNECT);
|
1998-11-26 00:10:33 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Translate other errors into ISC_R_* flavors.
|
|
|
|
*/
|
|
|
|
switch (errno) {
|
2000-09-21 21:40:29 +00:00
|
|
|
#define ERROR_MATCH(a, b) case a: dev->result = b; break;
|
2000-09-21 21:30:27 +00:00
|
|
|
ERROR_MATCH(EACCES, ISC_R_NOPERM);
|
|
|
|
ERROR_MATCH(EADDRNOTAVAIL, ISC_R_ADDRNOTAVAIL);
|
|
|
|
ERROR_MATCH(EAFNOSUPPORT, ISC_R_ADDRNOTAVAIL);
|
|
|
|
ERROR_MATCH(ECONNREFUSED, ISC_R_CONNREFUSED);
|
|
|
|
ERROR_MATCH(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
2000-11-17 21:04:12 +00:00
|
|
|
#ifdef EHOSTDOWN
|
|
|
|
ERROR_MATCH(EHOSTDOWN, ISC_R_HOSTUNREACH);
|
|
|
|
#endif
|
2000-09-21 21:30:27 +00:00
|
|
|
ERROR_MATCH(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
ERROR_MATCH(ENOBUFS, ISC_R_NORESOURCES);
|
|
|
|
ERROR_MATCH(EPERM, ISC_R_HOSTUNREACH);
|
|
|
|
ERROR_MATCH(EPIPE, ISC_R_NOTCONNECTED);
|
|
|
|
ERROR_MATCH(ETIMEDOUT, ISC_R_TIMEDOUT);
|
2005-05-19 02:42:42 +00:00
|
|
|
ERROR_MATCH(ECONNRESET, ISC_R_CONNECTIONRESET);
|
2000-09-21 21:30:27 +00:00
|
|
|
#undef ERROR_MATCH
|
1998-11-26 00:10:33 +00:00
|
|
|
default:
|
|
|
|
dev->result = ISC_R_UNEXPECTED;
|
2007-01-03 20:57:07 +00:00
|
|
|
isc_sockaddr_format(&sock->peer_address, peerbuf,
|
2005-03-15 01:41:28 +00:00
|
|
|
sizeof(peerbuf));
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
1998-11-26 00:10:33 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2005-03-15 01:41:28 +00:00
|
|
|
"internal_connect: connect(%s) %s",
|
|
|
|
peerbuf, strbuf);
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
2000-07-10 19:40:06 +00:00
|
|
|
} else {
|
1999-07-07 02:05:08 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
2000-07-10 19:40:06 +00:00
|
|
|
sock->connected = 1;
|
|
|
|
sock->bound = 1;
|
|
|
|
}
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
sock->connect_ev = NULL;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
task = dev->ev_sender;
|
|
|
|
dev->ev_sender = sock;
|
2004-04-15 01:58:25 +00:00
|
|
|
isc_task_sendanddetach(&task, ISC_EVENT_PTR(&dev));
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
1998-12-01 23:59:39 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp) {
|
2005-07-12 01:00:20 +00:00
|
|
|
isc_result_t result;
|
2000-05-11 05:43:53 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(addressp != NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2000-05-11 05:43:53 +00:00
|
|
|
if (sock->connected) {
|
2007-01-03 20:57:07 +00:00
|
|
|
*addressp = sock->peer_address;
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-05-11 05:43:53 +00:00
|
|
|
} else {
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_NOTCONNECTED;
|
2000-05-11 05:43:53 +00:00
|
|
|
}
|
1998-12-01 23:59:39 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-12-01 23:59:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp) {
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T len;
|
2005-07-12 01:00:20 +00:00
|
|
|
isc_result_t result;
|
2001-08-31 05:57:58 +00:00
|
|
|
char strbuf[ISC_STRERRORSIZE];
|
1998-12-01 23:59:39 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(addressp != NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
2000-05-11 05:43:53 +00:00
|
|
|
if (!sock->bound) {
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_NOTBOUND;
|
2000-05-11 05:43:53 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_SUCCESS;
|
2000-05-11 05:43:53 +00:00
|
|
|
|
2001-11-27 01:56:32 +00:00
|
|
|
len = sizeof(addressp->type);
|
1999-08-31 20:57:46 +00:00
|
|
|
if (getsockname(sock->fd, &addressp->type.sa, (void *)&len) < 0) {
|
2001-08-31 05:57:58 +00:00
|
|
|
isc__strerror(errno, strbuf, sizeof(strbuf));
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "getsockname: %s",
|
|
|
|
strbuf);
|
2005-07-12 01:00:20 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
2000-05-11 05:43:53 +00:00
|
|
|
goto out;
|
1998-12-01 23:59:39 +00:00
|
|
|
}
|
1999-07-07 02:05:08 +00:00
|
|
|
addressp->length = (unsigned int)len;
|
1998-12-01 23:59:39 +00:00
|
|
|
|
2000-05-11 05:43:53 +00:00
|
|
|
out:
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
2000-05-11 05:43:53 +00:00
|
|
|
|
2005-07-12 01:00:20 +00:00
|
|
|
return (result);
|
1998-12-01 23:59:39 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_cancel(isc_socket_t *sock, isc_task_t *task, unsigned int how) {
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Quick exit if there is nothing to do. Don't even bother locking
|
|
|
|
* in this case.
|
|
|
|
*/
|
|
|
|
if (how == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All of these do the same thing, more or less.
|
|
|
|
* Each will:
|
1998-12-10 16:14:05 +00:00
|
|
|
* o If the internal event is marked as "posted" try to
|
1998-12-05 00:28:13 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
1999-01-27 04:17:05 +00:00
|
|
|
if (((how & ISC_SOCKCANCEL_RECV) == ISC_SOCKCANCEL_RECV)
|
2000-06-02 01:27:27 +00:00
|
|
|
&& !ISC_LIST_EMPTY(sock->recv_list)) {
|
1999-01-27 04:17:05 +00:00
|
|
|
isc_socketevent_t *dev;
|
1999-07-28 01:07:21 +00:00
|
|
|
isc_socketevent_t *next;
|
1999-01-27 04:17:05 +00:00
|
|
|
isc_task_t *current_task;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->recv_list);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
while (dev != NULL) {
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
send_recvdone_event(sock, &dev);
|
|
|
|
}
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = next;
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
|
2000-06-02 01:27:27 +00:00
|
|
|
&& !ISC_LIST_EMPTY(sock->send_list)) {
|
1999-01-27 04:17:05 +00:00
|
|
|
isc_socketevent_t *dev;
|
1999-07-28 06:50:10 +00:00
|
|
|
isc_socketevent_t *next;
|
1999-01-27 04:17:05 +00:00
|
|
|
isc_task_t *current_task;
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->send_list);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-07-28 06:50:10 +00:00
|
|
|
while (dev != NULL) {
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
2001-02-12 21:43:17 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
send_senddone_event(sock, &dev);
|
|
|
|
}
|
1999-07-28 06:50:10 +00:00
|
|
|
dev = next;
|
1998-12-18 04:03:11 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
|
2000-06-02 01:27:27 +00:00
|
|
|
&& !ISC_LIST_EMPTY(sock->accept_list)) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_newconnev_t *dev;
|
1999-06-10 00:45:30 +00:00
|
|
|
isc_socket_newconnev_t *next;
|
1999-01-27 04:17:05 +00:00
|
|
|
isc_task_t *current_task;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
dev = ISC_LIST_HEAD(sock->accept_list);
|
|
|
|
while (dev != NULL) {
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
|
|
|
next = ISC_LIST_NEXT(dev, ev_link);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
1998-12-05 00:28:13 +00:00
|
|
|
|
2000-04-17 19:22:44 +00:00
|
|
|
ISC_LIST_UNLINK(sock->accept_list, dev,
|
|
|
|
ev_link);
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
dev->newsocket->references--;
|
|
|
|
free_socket(&dev->newsocket);
|
1999-06-10 00:45:30 +00:00
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = sock;
|
1999-09-23 21:31:03 +00:00
|
|
|
isc_task_sendanddetach(¤t_task,
|
2005-05-19 02:42:42 +00:00
|
|
|
ISC_EVENT_PTR(&dev));
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1999-06-10 00:45:30 +00:00
|
|
|
dev = next;
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-05-17 22:31:26 +00:00
|
|
|
/*
|
|
|
|
* Connecting is not a list.
|
|
|
|
*/
|
1999-01-27 04:17:05 +00:00
|
|
|
if (((how & ISC_SOCKCANCEL_CONNECT) == ISC_SOCKCANCEL_CONNECT)
|
|
|
|
&& sock->connect_ev != NULL) {
|
|
|
|
isc_socket_connev_t *dev;
|
|
|
|
isc_task_t *current_task;
|
|
|
|
|
2000-01-28 01:31:21 +00:00
|
|
|
INSIST(sock->connecting);
|
|
|
|
sock->connecting = 0;
|
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
dev = sock->connect_ev;
|
2000-04-17 19:22:44 +00:00
|
|
|
current_task = dev->ev_sender;
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1999-07-28 01:07:21 +00:00
|
|
|
if ((task == NULL) || (task == current_task)) {
|
1999-05-17 22:54:17 +00:00
|
|
|
sock->connect_ev = NULL;
|
1999-01-27 04:17:05 +00:00
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
2000-04-17 19:22:44 +00:00
|
|
|
dev->ev_sender = sock;
|
1999-09-23 21:31:03 +00:00
|
|
|
isc_task_sendanddetach(¤t_task,
|
2004-04-15 01:58:25 +00:00
|
|
|
ISC_EVENT_PTR(&dev));
|
1999-01-27 04:17:05 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-06-25 01:36:52 +00:00
|
|
|
isc_sockettype_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_gettype(isc_socket_t *sock) {
|
1999-06-25 01:36:52 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
return (sock->type);
|
|
|
|
}
|
2000-05-11 07:39:17 +00:00
|
|
|
|
|
|
|
isc_boolean_t
|
2000-05-14 22:35:45 +00:00
|
|
|
isc_socket_isbound(isc_socket_t *sock) {
|
2000-05-11 07:39:17 +00:00
|
|
|
isc_boolean_t val;
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
val = ((sock->bound) ? ISC_TRUE : ISC_FALSE);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (val);
|
|
|
|
}
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2001-11-29 07:31:25 +00:00
|
|
|
void
|
|
|
|
isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes) {
|
|
|
|
#if defined(IPV6_V6ONLY)
|
|
|
|
int onoff = yes ? 1 : 0;
|
|
|
|
#else
|
|
|
|
UNUSED(yes);
|
|
|
|
UNUSED(sock);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
#ifdef IPV6_V6ONLY
|
|
|
|
if (sock->pf == AF_INET6) {
|
|
|
|
(void)setsockopt(sock->fd, IPPROTO_IPV6, IPV6_V6ONLY,
|
|
|
|
(void *)&onoff, sizeof(onoff));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
2008-06-23 19:41:20 +00:00
|
|
|
/* In our assumed scenario, we can simply use a single static object. */
|
|
|
|
static isc_socketwait_t swait_private;
|
|
|
|
|
|
|
|
int
|
|
|
|
isc__socketmgr_waitevents(struct timeval *tvp, isc_socketwait_t **swaitp) {
|
|
|
|
int n;
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
struct timespec ts, *tsp;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_EPOLL
|
|
|
|
int timeout;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_DEVPOLL
|
|
|
|
struct dvpoll dvp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
REQUIRE(swaitp != NULL && *swaitp == NULL);
|
|
|
|
|
2000-08-29 23:58:17 +00:00
|
|
|
if (socketmgr == NULL)
|
2008-06-23 19:41:20 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
#ifdef USE_KQUEUE
|
|
|
|
if (tvp != NULL) {
|
|
|
|
ts.tv_sec = tvp->tv_sec;
|
|
|
|
ts.tv_nsec = tvp->tv_usec * 1000;
|
|
|
|
tsp = &ts;
|
|
|
|
} else
|
|
|
|
tsp = NULL;
|
|
|
|
swait_private.nevents = kevent(socketmgr->kqueue_fd, NULL, 0,
|
|
|
|
socketmgr->events, socketmgr->nevents,
|
|
|
|
tsp);
|
|
|
|
n = swait_private.nevents;
|
|
|
|
#elif defined(USE_EPOLL)
|
|
|
|
if (tvp != NULL)
|
|
|
|
timeout = tvp->tv_sec * 1000 + (tvp->tv_usec + 999) / 1000;
|
|
|
|
else
|
|
|
|
timeout = -1;
|
|
|
|
swait_private.nevents = epoll_wait(socketmgr->epoll_fd,
|
|
|
|
socketmgr->events,
|
|
|
|
socketmgr->nevents, timeout);
|
|
|
|
n = swait_private.nevents;
|
|
|
|
#elif defined(USE_DEVPOLL)
|
|
|
|
dvp.dp_fds = socketmgr->events;
|
|
|
|
dvp.dp_nfds = socketmgr->nevents;
|
|
|
|
if (tvp != NULL) {
|
|
|
|
dvp.dp_timeout = tvp->tv_sec * 1000 +
|
|
|
|
(tvp->tv_usec + 999) / 1000;
|
|
|
|
} else
|
|
|
|
dvp.dp_timeout = -1;
|
|
|
|
swait_private.nevents = ioctl(socketmgr->devpoll_fd, DP_POLL, &dvp);
|
|
|
|
n = swait_private.nevents;
|
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
swait_private.readset = socketmgr->read_fds;
|
|
|
|
swait_private.writeset = socketmgr->write_fds;
|
|
|
|
swait_private.maxfd = socketmgr->maxfd + 1;
|
|
|
|
|
|
|
|
n = select(swait_private.maxfd, &swait_private.readset,
|
|
|
|
&swait_private.writeset, NULL, tvp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
*swaitp = &swait_private;
|
|
|
|
return (n);
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2008-06-23 19:41:20 +00:00
|
|
|
isc__socketmgr_dispatch(isc_socketwait_t *swait) {
|
|
|
|
REQUIRE(swait == &swait_private);
|
2000-08-29 23:58:17 +00:00
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
if (socketmgr == NULL)
|
2000-08-29 23:58:17 +00:00
|
|
|
return (ISC_R_NOTFOUND);
|
|
|
|
|
2008-06-23 19:41:20 +00:00
|
|
|
#if defined(USE_KQUEUE) || defined(USE_EPOLL) || defined(USE_DEVPOLL)
|
|
|
|
(void)process_fds(socketmgr, socketmgr->events, swait->nevents);
|
2000-08-29 23:58:17 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2008-06-23 19:41:20 +00:00
|
|
|
#elif defined(USE_SELECT)
|
|
|
|
process_fds(socketmgr, swait->maxfd, &swait->readset, &swait->writeset);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
#endif
|
2000-08-29 23:58:17 +00:00
|
|
|
}
|
2000-09-28 21:31:08 +00:00
|
|
|
#endif /* ISC_PLATFORM_USETHREADS */
|
2007-02-13 02:49:08 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
isc_socket_setname(isc_socket_t *socket, const char *name, void *tag) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Name 'socket'.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(socket));
|
|
|
|
|
|
|
|
LOCK(&socket->lock);
|
|
|
|
memset(socket->name, 0, sizeof(socket->name));
|
|
|
|
strncpy(socket->name, name, sizeof(socket->name) - 1);
|
|
|
|
socket->tag = tag;
|
|
|
|
UNLOCK(&socket->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
isc_socket_getname(isc_socket_t *socket) {
|
|
|
|
return (socket->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
isc_socket_gettag(isc_socket_t *socket) {
|
|
|
|
return (socket->tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
|
|
|
|
static const char *
|
2007-07-27 14:28:04 +00:00
|
|
|
_socktype(isc_sockettype_t type)
|
2007-02-13 02:49:08 +00:00
|
|
|
{
|
2007-07-27 14:28:04 +00:00
|
|
|
if (type == isc_sockettype_udp)
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("udp");
|
2007-07-27 14:28:04 +00:00
|
|
|
else if (type == isc_sockettype_tcp)
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("tcp");
|
2007-07-27 14:28:04 +00:00
|
|
|
else if (type == isc_sockettype_unix)
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("unix");
|
2007-07-27 14:28:04 +00:00
|
|
|
else if (type == isc_sockettype_fdwatch)
|
2007-02-13 02:49:08 +00:00
|
|
|
return ("fdwatch");
|
|
|
|
else
|
|
|
|
return ("not-initialized");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
isc_socketmgr_renderxml(isc_socketmgr_t *mgr, xmlTextWriterPtr writer)
|
|
|
|
{
|
|
|
|
isc_socket_t *sock;
|
|
|
|
char peerbuf[ISC_SOCKADDR_FORMATSIZE];
|
|
|
|
isc_sockaddr_t addr;
|
|
|
|
ISC_SOCKADDR_LEN_T len;
|
|
|
|
|
|
|
|
LOCK(&mgr->lock);
|
|
|
|
|
|
|
|
#ifndef ISC_PLATFORM_USETHREADS
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
|
|
|
|
xmlTextWriterWriteFormatString(writer, "%d", mgr->refs);
|
|
|
|
xmlTextWriterEndElement(writer);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "sockets");
|
|
|
|
sock = ISC_LIST_HEAD(mgr->socklist);
|
|
|
|
while (sock != NULL) {
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "socket");
|
|
|
|
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "id");
|
|
|
|
xmlTextWriterWriteFormatString(writer, "%p", sock);
|
|
|
|
xmlTextWriterEndElement(writer);
|
|
|
|
|
|
|
|
if (sock->name[0] != 0) {
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "name");
|
|
|
|
xmlTextWriterWriteFormatString(writer, "%s",
|
|
|
|
sock->name);
|
|
|
|
xmlTextWriterEndElement(writer); /* name */
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "references");
|
|
|
|
xmlTextWriterWriteFormatString(writer, "%d", sock->references);
|
|
|
|
xmlTextWriterEndElement(writer);
|
|
|
|
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "type",
|
|
|
|
ISC_XMLCHAR _socktype(sock->type));
|
|
|
|
|
|
|
|
if (sock->connected) {
|
|
|
|
isc_sockaddr_format(&sock->peer_address, peerbuf,
|
|
|
|
sizeof(peerbuf));
|
|
|
|
xmlTextWriterWriteElement(writer,
|
|
|
|
ISC_XMLCHAR "peer-address",
|
|
|
|
ISC_XMLCHAR peerbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname(sock->fd, &addr.type.sa, (void *)&len) == 0) {
|
|
|
|
isc_sockaddr_format(&addr, peerbuf, sizeof(peerbuf));
|
|
|
|
xmlTextWriterWriteElement(writer,
|
|
|
|
ISC_XMLCHAR "local-address",
|
|
|
|
ISC_XMLCHAR peerbuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlTextWriterStartElement(writer, ISC_XMLCHAR "states");
|
|
|
|
if (sock->pending_recv)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "pending-receive");
|
|
|
|
if (sock->pending_send)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "pending-send");
|
|
|
|
if (sock->pending_accept)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "pending_accept");
|
|
|
|
if (sock->listener)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "listener");
|
|
|
|
if (sock->connected)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "connected");
|
|
|
|
if (sock->connecting)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "connecting");
|
|
|
|
if (sock->bound)
|
|
|
|
xmlTextWriterWriteElement(writer, ISC_XMLCHAR "state",
|
|
|
|
ISC_XMLCHAR "bound");
|
|
|
|
|
|
|
|
xmlTextWriterEndElement(writer); /* states */
|
|
|
|
|
|
|
|
xmlTextWriterEndElement(writer); /* socket */
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
sock = ISC_LIST_NEXT(sock, link);
|
|
|
|
}
|
|
|
|
xmlTextWriterEndElement(writer); /* sockets */
|
|
|
|
|
|
|
|
UNLOCK(&mgr->lock);
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBXML2 */
|