1998-12-12 20:48:14 +00:00
|
|
|
/*
|
1999-01-06 20:02:52 +00:00
|
|
|
* Copyright (C) 1998, 1999 Internet Software Consortium.
|
1998-12-12 20:48:14 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
1998-12-12 19:25:20 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
1998-11-06 01:45:35 +00:00
|
|
|
#include <fcntl.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
1998-12-04 20:00:26 +00:00
|
|
|
#include <isc/error.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/mutex.h>
|
|
|
|
#include <isc/condition.h>
|
|
|
|
#include <isc/socket.h>
|
1999-04-29 04:49:52 +00:00
|
|
|
#include <isc/list.h>
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1999-01-15 02:51:47 +00:00
|
|
|
#include "util.h"
|
1998-12-13 02:04:22 +00:00
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
/*
|
|
|
|
* Some systems define the socket length argument as an int, some as size_t,
|
|
|
|
* some as socklen_t. This is here, so it can be easily changed if needed.
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
#ifndef ISC_SOCKADDR_LEN_T
|
1999-02-06 08:48:08 +00:00
|
|
|
#define ISC_SOCKADDR_LEN_T int
|
1999-03-29 23:56:06 +00:00
|
|
|
#endif
|
1999-02-06 08:48:08 +00:00
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
/*
|
|
|
|
* As above, one system (solaris) wants the pointers passed into recv() and
|
|
|
|
* the other network functions to be char *. All the others seem to use
|
|
|
|
* void *. Cast everything to char * for now.
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
#ifndef ISC_SOCKDATA_CAST
|
1999-02-11 06:38:12 +00:00
|
|
|
#define ISC_SOCKDATA_CAST(x) ((char *)(x))
|
1999-03-29 23:56:06 +00:00
|
|
|
#endif
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
/*
|
|
|
|
* If we cannot send to this task, the application is broken.
|
|
|
|
*/
|
1998-12-10 16:14:05 +00:00
|
|
|
#define ISC_TASK_SEND(a, b) do { \
|
1999-01-06 20:02:52 +00:00
|
|
|
RUNTIME_CHECK(isc_task_send(a, b) == ISC_R_SUCCESS); \
|
1999-03-29 23:56:06 +00:00
|
|
|
} while (0)
|
1998-12-10 16:14:05 +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
|
|
|
|
1999-01-19 06:33:18 +00:00
|
|
|
#if 0
|
1998-12-10 16:14:05 +00:00
|
|
|
#define ISC_SOCKET_DEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(ISC_SOCKET_DEBUG)
|
1998-11-26 00:29:12 +00:00
|
|
|
#define TRACE_WATCHER 0x0001
|
|
|
|
#define TRACE_LISTEN 0x0002
|
|
|
|
#define TRACE_CONNECT 0x0004
|
|
|
|
#define TRACE_RECV 0x0008
|
|
|
|
#define TRACE_SEND 0x0010
|
|
|
|
#define TRACE_MANAGER 0x0020
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
int trace_level = TRACE_WATCHER | TRACE_MANAGER;
|
1999-05-17 22:31:26 +00:00
|
|
|
#define XTRACE(l, a) do { if ((l) & trace_level) printf a; } while (0)
|
1999-03-29 23:56:06 +00:00
|
|
|
#define XENTER(l, a) do { \
|
|
|
|
if ((l) & trace_level) \
|
|
|
|
printf("ENTER %s\n", (a)); \
|
|
|
|
} while (0)
|
|
|
|
#define XEXIT(l, a) do { \
|
|
|
|
if ((l) & trace_level) \
|
|
|
|
printf("EXIT %s\n", (a)); \
|
|
|
|
} while (0)
|
1998-11-03 00:54:47 +00:00
|
|
|
#else
|
1998-12-01 21:39:00 +00:00
|
|
|
#define XTRACE(l, a)
|
|
|
|
#define XENTER(l, a)
|
|
|
|
#define XEXIT(l, a)
|
1998-11-03 00:54:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1999-02-11 06:38:12 +00:00
|
|
|
* Internal event used to send readable/writable events to our internal
|
1998-11-06 01:45:35 +00:00
|
|
|
* functions.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
typedef struct rwintev {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_t common; /* Sender is the socket. */
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_task_t *task; /* task to send these to */
|
|
|
|
isc_socketevent_t *done_ev; /* the done event to post */
|
1998-11-06 01:45:35 +00:00
|
|
|
isc_boolean_t partial; /* partial i/o ok */
|
1998-11-10 01:56:44 +00:00
|
|
|
isc_boolean_t canceled; /* I/O was canceled */
|
1998-12-05 00:28:13 +00:00
|
|
|
isc_boolean_t posted; /* event posted to task */
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK(struct rwintev) link; /* next event */
|
1998-12-13 23:45:21 +00:00
|
|
|
} rwintev_t;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
|
|
|
typedef struct ncintev {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_t common; /* Sender is the socket */
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_task_t *task; /* task to send these to */
|
|
|
|
isc_socket_newconnev_t *done_ev; /* the done event */
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_boolean_t canceled; /* accept was canceled */
|
1998-12-05 00:28:13 +00:00
|
|
|
isc_boolean_t posted; /* event posted to task */
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK(struct ncintev) link; /* next event */
|
1998-12-13 23:45:21 +00:00
|
|
|
} ncintev_t;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
|
|
|
typedef struct cnintev {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_t common; /* Sender is the socket */
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_task_t *task; /* task to send these to */
|
|
|
|
isc_socket_connev_t *done_ev; /* the done event */
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_boolean_t canceled; /* connect was canceled */
|
1998-12-05 00:28:13 +00:00
|
|
|
isc_boolean_t posted; /* event posted to task */
|
1998-12-13 23:45:21 +00:00
|
|
|
} cnintev_t;
|
1998-12-04 11:21:11 +00:00
|
|
|
|
|
|
|
#define SOCKET_MAGIC 0x494f696fU /* IOio */
|
|
|
|
#define VALID_SOCKET(t) ((t) != NULL && (t)->magic == SOCKET_MAGIC)
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
struct isc_socket {
|
|
|
|
/* Not locked. */
|
|
|
|
unsigned int magic;
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_socketmgr_t *manager;
|
1998-11-03 00:54:47 +00:00
|
|
|
isc_mutex_t lock;
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_sockettype_t type;
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/* Locked by socket lock. */
|
|
|
|
unsigned int references;
|
|
|
|
int fd;
|
1998-12-10 16:14:05 +00:00
|
|
|
isc_result_t recv_result;
|
|
|
|
isc_result_t send_result;
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST(rwintev_t) recv_list;
|
|
|
|
ISC_LIST(rwintev_t) send_list;
|
|
|
|
ISC_LIST(ncintev_t) accept_list;
|
1999-03-29 23:56:06 +00:00
|
|
|
cnintev_t *connect_ev;
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_boolean_t pending_recv;
|
|
|
|
isc_boolean_t pending_send;
|
1998-12-05 00:28:13 +00:00
|
|
|
isc_boolean_t pending_accept;
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_boolean_t listener; /* is a listener socket */
|
1998-12-18 01:48:43 +00:00
|
|
|
isc_boolean_t connected;
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_boolean_t connecting; /* connect pending */
|
1999-03-29 23:56:06 +00:00
|
|
|
rwintev_t *riev; /* allocated recv intev */
|
|
|
|
rwintev_t *wiev; /* allocated send intev */
|
|
|
|
cnintev_t *ciev; /* allocated accept intev */
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_sockaddr_t address; /* remote address */
|
1998-12-04 11:21:11 +00:00
|
|
|
int addrlength; /* remote addrlen */
|
1998-11-03 00:54:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define SOCKET_MANAGER_MAGIC 0x494f6d67U /* IOmg */
|
|
|
|
#define VALID_MANAGER(m) ((m) != NULL && \
|
|
|
|
(m)->magic == SOCKET_MANAGER_MAGIC)
|
|
|
|
struct isc_socketmgr {
|
|
|
|
/* Not locked. */
|
|
|
|
unsigned int magic;
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_mem_t *mctx;
|
1998-11-03 00:54:47 +00:00
|
|
|
isc_mutex_t lock;
|
|
|
|
/* Locked by manager lock. */
|
1998-11-06 01:45:35 +00:00
|
|
|
unsigned int nsockets; /* sockets managed */
|
1998-12-04 11:21:11 +00:00
|
|
|
isc_thread_t watcher;
|
1999-05-17 22:31:26 +00:00
|
|
|
isc_condition_t shutdown_ok;
|
1998-11-06 01:45:35 +00:00
|
|
|
fd_set read_fds;
|
1998-11-03 00:54:47 +00:00
|
|
|
fd_set write_fds;
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_socket_t *fds[FD_SETSIZE];
|
1998-12-05 00:28:13 +00:00
|
|
|
int fdstate[FD_SETSIZE];
|
1998-11-11 01:44:08 +00:00
|
|
|
int maxfd;
|
1998-11-06 01:45:35 +00:00
|
|
|
int pipe_fds[2];
|
1998-11-03 00:54:47 +00:00
|
|
|
};
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
#define CLOSED 0 /* this one must be zero */
|
|
|
|
#define MANAGED 1
|
|
|
|
#define CLOSE_PENDING 2
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
static void send_recvdone_event(isc_socket_t *, isc_task_t **, rwintev_t **,
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t **, isc_result_t);
|
1999-03-29 23:56:06 +00:00
|
|
|
static void send_senddone_event(isc_socket_t *, isc_task_t **, rwintev_t **,
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t **, isc_result_t);
|
|
|
|
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 *);
|
1998-12-04 11:21:11 +00:00
|
|
|
|
|
|
|
#define SELECT_POKE_SHUTDOWN (-1)
|
|
|
|
#define SELECT_POKE_NOTHING (-2)
|
|
|
|
#define SELECT_POKE_RESCAN (-3) /* XXX implement */
|
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.
|
|
|
|
* We assume that if a write completes here, it will be inserted into the
|
|
|
|
* queue fully. That is, we will not get partial writes.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
select_poke(isc_socketmgr_t *mgr, int msg)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-11-06 01:45:35 +00:00
|
|
|
int cc;
|
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
do {
|
|
|
|
cc = write(mgr->pipe_fds[1], &msg, sizeof(int));
|
|
|
|
} while (cc < 0 && SOFT_ERROR(errno));
|
|
|
|
|
|
|
|
if (cc < 0)
|
1998-12-04 11:21:11 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
|
|
"write() failed during watcher poke: %s",
|
|
|
|
strerror(errno));
|
1999-02-11 06:38:12 +00:00
|
|
|
|
|
|
|
INSIST(cc == sizeof(int));
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* read a message on the internal fd.
|
|
|
|
*/
|
1998-11-10 01:56:44 +00:00
|
|
|
static int
|
1998-12-13 23:45:21 +00:00
|
|
|
select_readmsg(isc_socketmgr_t *mgr)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-11-10 01:56:44 +00:00
|
|
|
int msg;
|
1998-11-06 01:45:35 +00:00
|
|
|
int cc;
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
cc = read(mgr->pipe_fds[0], &msg, sizeof(int));
|
1998-11-06 01:45:35 +00:00
|
|
|
if (cc < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
1998-11-26 00:10:33 +00:00
|
|
|
return (SELECT_POKE_NOTHING);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-12-04 11:21:11 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
|
|
"read() failed during watcher poke: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
return (SELECT_POKE_NOTHING);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
return (msg);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-06 01:45:35 +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
|
|
|
|
make_nonblock(int fd)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-11-06 01:45:35 +00:00
|
|
|
int ret;
|
|
|
|
int flags;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
flags = fcntl(fd, F_GETFL, 0);
|
|
|
|
flags |= O_NONBLOCK;
|
|
|
|
ret = fcntl(fd, F_SETFL, flags);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
if (ret == -1) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"fcntl(%d, F_SETFL, %d): %s",
|
|
|
|
fd, flags, strerror(errno));
|
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
|
|
|
}
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
#ifdef ISC_SOCKET_DEBUG
|
1998-12-05 00:28:13 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
socket_dump(isc_socket_t *sock)
|
1998-12-05 00:28:13 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t * rwiev;
|
|
|
|
ncintev_t * aiev;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
printf("--------\nDump of socket %p\n", sock);
|
|
|
|
printf("fd: %d, references %u\n", sock->fd, sock->references);
|
|
|
|
|
|
|
|
printf("recv queue:\n");
|
1999-04-29 04:49:52 +00:00
|
|
|
rwiev = ISC_LIST_HEAD(sock->recv_list);
|
1998-12-05 00:28:13 +00:00
|
|
|
while (rwiev != NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
printf("\tintev %p, done_ev %p, task %p, "
|
1999-05-17 22:31:26 +00:00
|
|
|
"canceled %d, posted %d\n",
|
1998-12-05 00:28:13 +00:00
|
|
|
rwiev, rwiev->done_ev, rwiev->task, rwiev->canceled,
|
|
|
|
rwiev->posted);
|
1999-04-29 04:49:52 +00:00
|
|
|
rwiev = ISC_LIST_NEXT(rwiev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("send queue:\n");
|
1999-04-29 04:49:52 +00:00
|
|
|
rwiev = ISC_LIST_HEAD(sock->send_list);
|
1998-12-05 00:28:13 +00:00
|
|
|
while (rwiev != NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
printf("\tintev %p, done_ev %p, task %p, "
|
1999-05-17 22:31:26 +00:00
|
|
|
"canceled %d, posted %d\n",
|
1998-12-05 00:28:13 +00:00
|
|
|
rwiev, rwiev->done_ev, rwiev->task, rwiev->canceled,
|
|
|
|
rwiev->posted);
|
1999-04-29 04:49:52 +00:00
|
|
|
rwiev = ISC_LIST_NEXT(rwiev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("accept queue:\n");
|
1999-04-29 04:49:52 +00:00
|
|
|
aiev = ISC_LIST_HEAD(sock->accept_list);
|
1998-12-05 00:28:13 +00:00
|
|
|
while (aiev != NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
printf("\tintev %p, done_ev %p, task %p, "
|
|
|
|
"canceled %d, posted %d\n",
|
1998-12-05 00:28:13 +00:00
|
|
|
aiev, aiev->done_ev, aiev->task, aiev->canceled,
|
|
|
|
aiev->posted);
|
1999-04-29 04:49:52 +00:00
|
|
|
aiev = ISC_LIST_NEXT(aiev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("--------\n");
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
#endif
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Kill.
|
|
|
|
*
|
|
|
|
* Caller must ensure locking.
|
|
|
|
*/
|
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
destroy(isc_socket_t **sockp)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
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
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_MANAGER,
|
|
|
|
("destroy sockp = %p, sock = %p\n", sockp, sock));
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1998-12-18 04:03:11 +00:00
|
|
|
if (sock->riev)
|
|
|
|
isc_event_free((isc_event_t **)&sock->riev);
|
|
|
|
if (sock->wiev)
|
|
|
|
isc_event_free((isc_event_t **)&sock->wiev);
|
|
|
|
if (sock->ciev)
|
|
|
|
isc_event_free((isc_event_t **)&sock->ciev);
|
|
|
|
if (sock->connect_ev)
|
|
|
|
isc_event_free((isc_event_t **)&sock->connect_ev);
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
/*
|
1998-11-06 01:45:35 +00:00
|
|
|
* Noone has this socket open, so the watcher doesn't have to be
|
1998-11-10 01:56:44 +00:00
|
|
|
* poked, and the socket doesn't have to be locked.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
manager->fds[sock->fd] = NULL;
|
1998-12-05 00:28:13 +00:00
|
|
|
manager->fdstate[sock->fd] = CLOSE_PENDING;
|
|
|
|
select_poke(sock->manager, sock->fd);
|
1998-11-06 01:45:35 +00:00
|
|
|
manager->nsockets--;
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_MANAGER, ("nsockets == %d\n", manager->nsockets));
|
1999-05-17 22:31:26 +00:00
|
|
|
if (manager->nsockets == 0)
|
|
|
|
SIGNAL(&manager->shutdown_ok);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-11 01:44:08 +00:00
|
|
|
/*
|
|
|
|
* XXX should reset manager->maxfd here
|
|
|
|
*/
|
|
|
|
|
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;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
sock = isc_mem_get(manager->mctx, sizeof *sock);
|
|
|
|
|
|
|
|
if (sock == NULL)
|
1998-12-01 17:58:34 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
sock->magic = SOCKET_MAGIC;
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
ISC_LIST_INIT(sock->accept_list);
|
1998-12-18 04:03:11 +00:00
|
|
|
sock->connect_ev = NULL;
|
1998-12-04 11:21:11 +00:00
|
|
|
sock->pending_recv = ISC_FALSE;
|
|
|
|
sock->pending_send = ISC_FALSE;
|
1998-12-05 00:28:13 +00:00
|
|
|
sock->pending_accept = ISC_FALSE;
|
1998-11-26 00:10:33 +00:00
|
|
|
sock->listener = ISC_FALSE;
|
1998-12-18 01:48:43 +00:00
|
|
|
sock->connected = ISC_FALSE;
|
1998-12-18 04:03:11 +00:00
|
|
|
sock->connecting = ISC_FALSE;
|
|
|
|
sock->riev = NULL;
|
|
|
|
sock->wiev = NULL;
|
|
|
|
sock->ciev = NULL;
|
|
|
|
|
|
|
|
sock->addrlength = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->recv_result = ISC_R_SUCCESS;
|
|
|
|
sock->send_result = ISC_R_SUCCESS;
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* initialize the lock
|
|
|
|
*/
|
|
|
|
if (isc_mutex_init(&sock->lock) != ISC_R_SUCCESS) {
|
|
|
|
sock->magic = 0;
|
|
|
|
isc_mem_put(manager->mctx, sock, sizeof *sock);
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_mutex_init() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
*socketp = sock;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
1998-12-13 23:45:21 +00:00
|
|
|
free_socket(isc_socket_t **socketp)
|
1998-11-10 01:56:44 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock = *socketp;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-05-17 22:31:26 +00:00
|
|
|
#ifdef ISC_SOCKET_DEBUG
|
|
|
|
socket_dump(sock);
|
|
|
|
#endif
|
1998-11-11 00:43:14 +00:00
|
|
|
REQUIRE(sock->references == 0);
|
1998-11-10 01:56:44 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
1998-11-26 00:29:12 +00:00
|
|
|
REQUIRE(!sock->connecting);
|
1998-12-04 11:21:11 +00:00
|
|
|
REQUIRE(!sock->pending_recv);
|
|
|
|
REQUIRE(!sock->pending_send);
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(!sock->pending_accept);
|
1998-12-04 11:21:11 +00:00
|
|
|
REQUIRE(EMPTY(sock->recv_list));
|
|
|
|
REQUIRE(EMPTY(sock->send_list));
|
|
|
|
REQUIRE(EMPTY(sock->accept_list));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
sock->magic = 0;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
(void)isc_mutex_destroy(&sock->lock);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new 'type' socket managed by 'manager'. The sockets
|
|
|
|
* parameters are specified by 'expires' and 'interval'. Events
|
|
|
|
* will be posted to 'task' and when dispatched 'action' will be
|
|
|
|
* called with 'arg' as the arg value. The new socket is returned
|
|
|
|
* in 'socketp'.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_create(isc_socketmgr_t *manager, isc_sockettype_t type,
|
|
|
|
isc_socket_t **socketp)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock = NULL;
|
1998-11-10 01:56:44 +00:00
|
|
|
isc_result_t ret;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
REQUIRE(socketp != NULL && *socketp == NULL);
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XENTER(TRACE_MANAGER, "isc_socket_create");
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
ret = allocate_socket(manager, type, &sock);
|
|
|
|
if (ret != ISC_R_SUCCESS)
|
|
|
|
return (ret);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case isc_socket_udp:
|
|
|
|
sock->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
break;
|
|
|
|
case isc_socket_tcp:
|
|
|
|
sock->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (sock->fd < 0) {
|
1998-11-10 01:56:44 +00:00
|
|
|
free_socket(&sock);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
switch (errno) {
|
|
|
|
case EMFILE:
|
|
|
|
case ENFILE:
|
|
|
|
case ENOBUFS:
|
|
|
|
return (ISC_R_NORESOURCES);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-03 00:54:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"socket() failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-03 00:54:47 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
if (make_nonblock(sock->fd) != ISC_R_SUCCESS) {
|
1998-11-10 01:56:44 +00:00
|
|
|
free_socket(&sock);
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->references = 1;
|
|
|
|
*socketp = sock;
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
LOCK(&manager->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note we don't have to lock the socket like we normally would because
|
|
|
|
* there are no external references to it yet.
|
|
|
|
*/
|
|
|
|
|
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;
|
1998-11-06 01:45:35 +00:00
|
|
|
manager->nsockets++;
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_MANAGER, ("nsockets == %d\n", manager->nsockets));
|
1998-11-11 01:44:08 +00:00
|
|
|
if (manager->maxfd < sock->fd)
|
|
|
|
manager->maxfd = sock->fd;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XEXIT(TRACE_MANAGER, "isc_socket_create");
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Attach to a socket. Caller must explicitly detach when it is done.
|
|
|
|
*/
|
|
|
|
void
|
1998-12-13 23:45:21 +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);
|
|
|
|
|
|
|
|
*socketp = sock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Dereference a socket. If this is the last reference to it, clean things
|
|
|
|
* up by destroying the socket.
|
|
|
|
*/
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_detach(isc_socket_t **socketp)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
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));
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XENTER(TRACE_MANAGER, "isc_socket_detach");
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
if (kill_socket)
|
|
|
|
destroy(&sock);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XEXIT(TRACE_MANAGER, "isc_socket_detach");
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
*socketp = NULL;
|
|
|
|
}
|
|
|
|
|
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
|
1998-12-13 23:45:21 +00:00
|
|
|
dispatch_read(isc_socket_t *sock)
|
1998-11-06 01:45:35 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t *iev;
|
|
|
|
isc_event_t *ev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->recv_list);
|
1998-12-13 23:45:21 +00:00
|
|
|
ev = (isc_event_t *)iev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
REQUIRE(!sock->pending_recv);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-12-04 11:21:11 +00:00
|
|
|
sock->pending_recv = ISC_TRUE;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER, ("dispatch_read: posted event %p to task %p\n",
|
|
|
|
ev, iev->task));
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->posted = ISC_TRUE;
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
ISC_TASK_SEND(iev->task, &ev);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
dispatch_write(isc_socket_t *sock)
|
1998-11-06 01:45:35 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t *iev;
|
|
|
|
isc_event_t *ev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->send_list);
|
1998-12-13 23:45:21 +00:00
|
|
|
ev = (isc_event_t *)iev;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
REQUIRE(!sock->pending_send);
|
1998-12-04 11:21:11 +00:00
|
|
|
sock->pending_send = ISC_TRUE;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->posted = ISC_TRUE;
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
ISC_TASK_SEND(iev->task, &ev);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
dispatch_listen(isc_socket_t *sock)
|
1998-11-10 11:37:54 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
ncintev_t *iev;
|
|
|
|
isc_event_t *ev;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->accept_list);
|
1998-12-13 23:45:21 +00:00
|
|
|
ev = (isc_event_t *)iev;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
REQUIRE(!sock->pending_accept);
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
sock->pending_accept = ISC_TRUE;
|
|
|
|
|
|
|
|
iev->posted = ISC_TRUE;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
ISC_TASK_SEND(iev->task, &ev);
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
dispatch_connect(isc_socket_t *sock)
|
1998-11-26 00:10:33 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
cnintev_t *iev;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
REQUIRE(sock->connecting);
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
iev = sock->connect_ev;
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->posted = ISC_TRUE;
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_TASK_SEND(iev->task, (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-03-30 06:22:28 +00:00
|
|
|
* Entry: socket is a valid, locked socket.
|
|
|
|
* *task is a valid task.
|
|
|
|
* if iev != NULL, *iev must be valid, and the first item on the
|
|
|
|
* socket's receive queue.
|
|
|
|
* *dev is a valid done event.
|
|
|
|
* resultcode is the code to be set within the done event.
|
1999-03-29 23:56:06 +00:00
|
|
|
*
|
1999-03-30 06:22:28 +00:00
|
|
|
* Exit: task is detached iff iev != NULL
|
1999-03-29 23:56:06 +00:00
|
|
|
* iev is freed if != NULL
|
|
|
|
* done event is sent to the task.
|
|
|
|
*
|
1998-11-07 02:31:04 +00:00
|
|
|
* Caller must have the socket locked.
|
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
static void
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(isc_socket_t *sock, isc_task_t **task,
|
|
|
|
rwintev_t **iev,
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t **dev, isc_result_t resultcode)
|
1998-11-06 01:45:35 +00:00
|
|
|
{
|
1998-11-07 02:31:04 +00:00
|
|
|
REQUIRE(dev != NULL);
|
|
|
|
REQUIRE(*dev != NULL);
|
1999-03-29 23:56:06 +00:00
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(*task != NULL);
|
|
|
|
|
|
|
|
if (iev != NULL) {
|
|
|
|
REQUIRE(!EMPTY(sock->recv_list));
|
|
|
|
REQUIRE(*iev != NULL);
|
1999-03-30 06:18:45 +00:00
|
|
|
REQUIRE((*iev)->task == *task);
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->recv_list, *iev, link);
|
1999-03-29 23:56:06 +00:00
|
|
|
}
|
1998-11-07 02:31:04 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
(*dev)->result = resultcode;
|
1999-03-29 23:56:06 +00:00
|
|
|
ISC_TASK_SEND(*task, (isc_event_t **)dev);
|
1999-03-30 06:18:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* task might be an element in iev, so it cannot be used after iev
|
|
|
|
* is freed!
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
if (iev != NULL) {
|
|
|
|
isc_task_detach(task);
|
1999-03-30 06:18:45 +00:00
|
|
|
(*iev)->done_ev = NULL;
|
|
|
|
isc_event_free((isc_event_t **)iev);
|
1999-03-29 23:56:06 +00:00
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* Caller must have the socket locked.
|
|
|
|
*/
|
1998-11-11 02:05:36 +00:00
|
|
|
static void
|
1999-03-29 23:56:06 +00:00
|
|
|
send_senddone_event(isc_socket_t *sock, isc_task_t **task,
|
|
|
|
rwintev_t **iev,
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t **dev, isc_result_t resultcode)
|
1998-11-11 02:05:36 +00:00
|
|
|
{
|
|
|
|
REQUIRE(dev != NULL);
|
|
|
|
REQUIRE(*dev != NULL);
|
1999-03-29 23:56:06 +00:00
|
|
|
REQUIRE(task != NULL);
|
|
|
|
REQUIRE(*task != NULL);
|
|
|
|
|
|
|
|
if (iev != NULL) {
|
|
|
|
REQUIRE(*iev != NULL);
|
|
|
|
REQUIRE(!EMPTY(sock->send_list));
|
1999-03-30 06:18:45 +00:00
|
|
|
REQUIRE((*iev)->task == *task);
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->send_list, *iev, link);
|
1999-03-29 23:56:06 +00:00
|
|
|
}
|
1998-11-11 02:05:36 +00:00
|
|
|
|
|
|
|
(*dev)->result = resultcode;
|
1999-03-29 23:56:06 +00:00
|
|
|
ISC_TASK_SEND(*task, (isc_event_t **)dev);
|
1999-03-30 06:18:45 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* task might be an element in iev, so it cannot be used after iev
|
|
|
|
* is freed!
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
if (iev != NULL) {
|
|
|
|
isc_task_detach(task);
|
1999-03-30 06:18:45 +00:00
|
|
|
(*iev)->done_ev = NULL;
|
|
|
|
isc_event_free((isc_event_t **)iev);
|
1999-03-29 23:56:06 +00:00
|
|
|
}
|
1998-11-11 02:05:36 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
send_ncdone_event(ncintev_t **iev,
|
|
|
|
isc_socket_newconnev_t **dev, isc_result_t resultcode)
|
1998-11-10 11:37:54 +00:00
|
|
|
{
|
|
|
|
REQUIRE(iev != NULL);
|
|
|
|
REQUIRE(*iev != NULL);
|
|
|
|
REQUIRE(dev != NULL);
|
|
|
|
REQUIRE(*dev != NULL);
|
|
|
|
|
|
|
|
(*dev)->result = resultcode;
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_TASK_SEND((*iev)->task, (isc_event_t **)dev);
|
1998-12-18 04:03:11 +00:00
|
|
|
isc_task_detach(&(*iev)->task);
|
1998-12-05 00:28:13 +00:00
|
|
|
(*iev)->done_ev = NULL;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)iev);
|
1998-11-10 11:37:54 +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
|
|
|
|
* socket is referenced twice (one for the task which is receiving this
|
|
|
|
* message, and once for the message itself) so the task does not need to
|
|
|
|
* attach to the socket again. The task is not attached at all.
|
|
|
|
*/
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
internal_accept(isc_task_t *task, isc_event_t *ev)
|
1998-11-07 02:31:04 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_socket_newconnev_t *dev;
|
|
|
|
ncintev_t *iev;
|
1998-11-10 01:56:44 +00:00
|
|
|
struct sockaddr addr;
|
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;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
sock = ev->sender;
|
1998-12-10 16:14:05 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (ncintev_t *)ev;
|
1998-12-10 16:14:05 +00:00
|
|
|
manager = sock->manager;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
1998-11-10 01:56:44 +00:00
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_LISTEN,
|
|
|
|
("internal_accept called, locked parent sock %p\n", sock));
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(sock->pending_accept);
|
1998-11-10 01:56:44 +00:00
|
|
|
REQUIRE(sock->listener);
|
1998-12-04 11:21:11 +00:00
|
|
|
REQUIRE(!EMPTY(sock->accept_list));
|
1998-11-10 01:56:44 +00:00
|
|
|
REQUIRE(iev->task == task);
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
sock->pending_accept = ISC_FALSE;
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
/*
|
|
|
|
* Has this event been canceled?
|
|
|
|
*/
|
|
|
|
if (iev->canceled) {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->accept_list, iev, link);
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)iev);
|
1998-12-04 11:21:11 +00:00
|
|
|
if (!EMPTY(sock->accept_list))
|
1998-11-10 11:37:54 +00:00
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
|
|
|
|
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
|
1998-11-10 01:56:44 +00:00
|
|
|
* again.
|
|
|
|
*/
|
1998-11-15 11:48:21 +00:00
|
|
|
addrlen = sizeof(addr);
|
1998-11-10 01:56:44 +00:00
|
|
|
fd = accept(sock->fd, &addr, &addrlen);
|
|
|
|
if (fd < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno)) {
|
1998-11-10 01:56:44 +00:00
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
UNLOCK(&sock->lock);
|
1998-12-16 02:05:38 +00:00
|
|
|
return;
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If some other error, ignore it as well and hope
|
1998-12-18 01:48:43 +00:00
|
|
|
* for the best, but log it.
|
1998-11-10 01:56:44 +00:00
|
|
|
*/
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_LISTEN, ("internal_accept: accept returned %s\n",
|
|
|
|
strerror(errno)));
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
fd = -1;
|
1999-03-29 23:56:06 +00:00
|
|
|
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_accept: accept() failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
if (fd != -1 && (make_nonblock(fd) != ISC_R_SUCCESS)) {
|
1998-12-05 01:44:38 +00:00
|
|
|
close(fd);
|
1998-12-10 16:14:05 +00:00
|
|
|
fd = -1;
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_accept: make_nonblock() failed: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
result = ISC_R_UNEXPECTED;
|
1998-12-05 01:44:38 +00:00
|
|
|
}
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->accept_list, iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
if (!EMPTY(sock->accept_list))
|
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
|
1998-12-01 21:39:00 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* The accept succeeded. Pull off the done event and set the
|
|
|
|
* fd and other information in the socket descriptor here. These
|
|
|
|
* were preallocated for us.
|
|
|
|
*/
|
1998-12-05 00:28:13 +00:00
|
|
|
dev = iev->done_ev;
|
|
|
|
iev->done_ev = NULL;
|
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) {
|
|
|
|
dev->newsocket->fd = fd;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
/*
|
|
|
|
* Save away the remote address
|
|
|
|
*/
|
|
|
|
dev->newsocket->addrlength = addrlen;
|
|
|
|
memcpy(&dev->newsocket->address, &addr, addrlen);
|
|
|
|
dev->addrlength = addrlen;
|
|
|
|
memcpy(&dev->address, &addr, addrlen);
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
manager->fds[fd] = dev->newsocket;
|
|
|
|
manager->fdstate[fd] = MANAGED;
|
|
|
|
if (manager->maxfd < fd)
|
|
|
|
manager->maxfd = fd;
|
|
|
|
manager->nsockets++;
|
1999-05-21 07:16:13 +00:00
|
|
|
XTRACE(TRACE_MANAGER, ("nsockets == %d\n", manager->nsockets));
|
1998-12-10 16:14:05 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
|
|
|
|
XTRACE(TRACE_LISTEN, ("internal_accept: newsock %p, fd %d\n",
|
|
|
|
dev->newsocket, fd));
|
|
|
|
}
|
1998-12-01 21:39:00 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
send_ncdone_event(&iev, &dev, result);
|
1998-11-07 02:31:04 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
internal_recv(isc_task_t *task, isc_event_t *ev)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t *iev;
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socket_t *sock;
|
1998-11-06 01:45:35 +00:00
|
|
|
int cc;
|
|
|
|
size_t read_count;
|
1998-11-15 11:48:21 +00:00
|
|
|
struct sockaddr addr;
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T addrlen;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out what socket this is and lock it.
|
|
|
|
*/
|
1998-12-13 23:45:21 +00:00
|
|
|
sock = (isc_socket_t *)ev->sender;
|
1998-11-06 01:45:35 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
1998-12-04 11:21:11 +00:00
|
|
|
INSIST(sock->pending_recv == ISC_TRUE);
|
|
|
|
sock->pending_recv = ISC_FALSE;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_RECV,
|
1998-12-04 11:21:11 +00:00
|
|
|
("internal_recv: sock %p, fd %d\n", sock, sock->fd));
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
|
|
|
* Pull the first entry off the list, and look at it. If it is
|
|
|
|
* NULL, or not ours, something bad happened.
|
|
|
|
*/
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->recv_list);
|
1998-11-07 02:31:04 +00:00
|
|
|
INSIST(iev != NULL);
|
|
|
|
INSIST(iev->task == task);
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* Try to do as much I/O as possible on this socket. There are no
|
|
|
|
* limits here, currently. If some sort of quantum read count is
|
|
|
|
* desired before giving up control, make certain to process markers
|
|
|
|
* regardless of quantum.
|
|
|
|
*/
|
|
|
|
do {
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->recv_list);
|
1998-11-06 01:45:35 +00:00
|
|
|
dev = iev->done_ev;
|
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
1998-11-10 11:37:54 +00:00
|
|
|
* check for canceled I/O
|
1998-11-07 02:31:04 +00:00
|
|
|
*/
|
1998-11-10 11:37:54 +00:00
|
|
|
if (iev->canceled) {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->recv_list, iev, link);
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-07 02:31:04 +00:00
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* If this is a marker event, post its completion and
|
|
|
|
* continue the loop.
|
|
|
|
*/
|
|
|
|
if (dev->common.type == ISC_SOCKEVENT_RECVMARK) {
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task, &iev, &dev,
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->recv_result);
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
|
|
|
* It must be a read request. Try to satisfy it as best
|
|
|
|
* we can.
|
|
|
|
*/
|
1998-11-06 01:45:35 +00:00
|
|
|
read_count = dev->region.length - dev->n;
|
1998-12-01 21:39:00 +00:00
|
|
|
if (sock->type == isc_socket_udp) {
|
1998-11-15 11:48:21 +00:00
|
|
|
addrlen = sizeof(addr);
|
1999-02-11 06:38:12 +00:00
|
|
|
cc = recvfrom(sock->fd,
|
|
|
|
ISC_SOCKDATA_CAST(dev->region.base
|
|
|
|
+ dev->n),
|
1998-11-15 11:48:21 +00:00
|
|
|
read_count, 0,
|
|
|
|
(struct sockaddr *)&addr,
|
|
|
|
&addrlen);
|
1999-01-19 06:33:18 +00:00
|
|
|
if (cc >= 0) {
|
|
|
|
memcpy(&dev->address, &addr, addrlen);
|
|
|
|
dev->addrlength = addrlen;
|
|
|
|
}
|
1998-12-01 21:39:00 +00:00
|
|
|
} else {
|
1999-02-11 06:38:12 +00:00
|
|
|
cc = recv(sock->fd,
|
|
|
|
ISC_SOCKDATA_CAST(dev->region.base + dev->n),
|
1998-12-01 21:39:00 +00:00
|
|
|
read_count, 0);
|
1999-01-19 06:33:18 +00:00
|
|
|
if (cc >= 0) {
|
|
|
|
memcpy(&dev->address, &sock->address,
|
|
|
|
(size_t)sock->addrlength);
|
|
|
|
dev->addrlength = sock->addrlength;
|
|
|
|
}
|
1998-11-15 11:48:21 +00:00
|
|
|
}
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_RECV,
|
1998-12-04 11:21:11 +00:00
|
|
|
("internal_recv: read(%d) %d\n", sock->fd, cc));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* check for error or block condition
|
|
|
|
*/
|
|
|
|
if (cc < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
1998-11-06 01:45:35 +00:00
|
|
|
goto poke;
|
1998-12-18 01:48:43 +00:00
|
|
|
|
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
|
|
|
if (errno == _system) { \
|
|
|
|
if (sock->connected) { \
|
|
|
|
if (sock->type == isc_socket_tcp) \
|
|
|
|
sock->recv_result = _isc; \
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task, &iev, \
|
|
|
|
&dev, _isc); \
|
1998-12-18 01:48:43 +00:00
|
|
|
} \
|
|
|
|
goto next; \
|
|
|
|
}
|
|
|
|
|
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
|
|
|
SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
|
|
|
#undef SOFT_OR_HARD
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This might not be a permanent error.
|
|
|
|
*/
|
|
|
|
if (errno == ENOBUFS) {
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task,
|
|
|
|
&iev, &dev,
|
1998-12-18 01:48:43 +00:00
|
|
|
ISC_R_NORESOURCES);
|
|
|
|
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-12-10 16:14:05 +00:00
|
|
|
"internal read: %s", strerror(errno));
|
|
|
|
|
1998-12-18 04:03:11 +00:00
|
|
|
sock->recv_result = ISC_R_UNEXPECTED;
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task, &iev, &dev,
|
1998-12-18 04:03:11 +00:00
|
|
|
ISC_R_UNEXPECTED);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-12-18 01:48:43 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* read of 0 means the remote end was closed. Run through
|
|
|
|
* the event queue and dispatch all the events with an EOF
|
1998-11-07 02:31:04 +00:00
|
|
|
* result code. This will set the EOF flag in markers as
|
|
|
|
* well, but that's really ok.
|
1998-11-06 01:45:35 +00:00
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
if ((sock->type == isc_socket_tcp) && (cc == 0)) {
|
1998-11-06 01:45:35 +00:00
|
|
|
do {
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task,
|
|
|
|
&iev, &dev,
|
1998-11-11 02:05:36 +00:00
|
|
|
ISC_R_EOF);
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->recv_list);
|
1998-11-06 01:45:35 +00:00
|
|
|
} while (iev != NULL);
|
|
|
|
|
|
|
|
goto poke;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if we read less than we expected, update counters,
|
|
|
|
* poke.
|
|
|
|
*/
|
|
|
|
if ((size_t)cc < read_count) {
|
|
|
|
dev->n += cc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If partial reads are allowed, we return whatever
|
|
|
|
* was read with a success result, and continue
|
|
|
|
* the loop.
|
|
|
|
*/
|
|
|
|
if (iev->partial) {
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task,
|
|
|
|
&iev, &dev,
|
1998-11-11 02:05:36 +00:00
|
|
|
ISC_R_SUCCESS);
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Partials not ok. Exit the loop and notify the
|
|
|
|
* watcher to wait for more reads
|
|
|
|
*/
|
|
|
|
goto poke;
|
|
|
|
}
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
1998-11-06 01:45:35 +00:00
|
|
|
* Exactly what we wanted to read. We're done with this
|
|
|
|
* entry. Post its completion event.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
1998-11-10 11:37:54 +00:00
|
|
|
if ((size_t)cc == read_count) {
|
|
|
|
dev->n += read_count;
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task, &iev, &dev,
|
|
|
|
ISC_R_SUCCESS);
|
1998-11-10 11:37:54 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
next:
|
1998-12-18 22:02:41 +00:00
|
|
|
; /* some compilers need this here... */
|
1998-12-04 11:21:11 +00:00
|
|
|
} while (!EMPTY(sock->recv_list));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
poke:
|
1998-12-04 11:21:11 +00:00
|
|
|
if (!EMPTY(sock->recv_list))
|
1998-11-06 01:45:35 +00:00
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
1998-12-16 02:05:38 +00:00
|
|
|
static void
|
1998-12-13 23:45:21 +00:00
|
|
|
internal_send(isc_task_t *task, isc_event_t *ev)
|
1998-11-11 00:43:14 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t *iev;
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_socket_t *sock;
|
1998-11-11 00:43:14 +00:00
|
|
|
int cc;
|
|
|
|
size_t write_count;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out what socket this is and lock it.
|
|
|
|
*/
|
1998-12-13 23:45:21 +00:00
|
|
|
sock = (isc_socket_t *)ev->sender;
|
1998-11-11 00:43:14 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
1998-12-04 11:21:11 +00:00
|
|
|
INSIST(sock->pending_send == ISC_TRUE);
|
|
|
|
sock->pending_send = ISC_FALSE;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_SEND,
|
1998-12-04 11:21:11 +00:00
|
|
|
("internal_send: sock %p, fd %d\n", sock, sock->fd));
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Pull the first entry off the list, and look at it. If it is
|
|
|
|
* NULL, or not ours, something bad happened.
|
|
|
|
*/
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->send_list);
|
1998-11-11 00:43:14 +00:00
|
|
|
INSIST(iev != NULL);
|
|
|
|
INSIST(iev->task == task);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to do as much I/O as possible on this socket. There are no
|
|
|
|
* limits here, currently. If some sort of quantum write count is
|
|
|
|
* desired before giving up control, make certain to process markers
|
|
|
|
* regardless of quantum.
|
|
|
|
*/
|
|
|
|
do {
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->send_list);
|
1998-11-11 00:43:14 +00:00
|
|
|
dev = iev->done_ev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for canceled I/O
|
|
|
|
*/
|
|
|
|
if (iev->canceled) {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->send_list, iev, link);
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this is a marker event, post its completion and
|
|
|
|
* continue the loop.
|
|
|
|
*/
|
|
|
|
if (dev->common.type == ISC_SOCKEVENT_SENDMARK) {
|
1999-03-29 23:56:06 +00:00
|
|
|
send_senddone_event(sock, &iev->task, &iev, &dev,
|
1998-12-10 16:14:05 +00:00
|
|
|
sock->send_result);
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* It must be a write request. Try to satisfy it as best
|
|
|
|
* we can.
|
|
|
|
*/
|
|
|
|
write_count = dev->region.length - dev->n;
|
|
|
|
if (sock->type == isc_socket_udp)
|
1999-02-11 06:38:12 +00:00
|
|
|
cc = sendto(sock->fd,
|
|
|
|
ISC_SOCKDATA_CAST(dev->region.base
|
|
|
|
+ dev->n),
|
1998-11-11 00:43:14 +00:00
|
|
|
write_count, 0,
|
|
|
|
(struct sockaddr *)&dev->address,
|
1998-12-11 20:47:15 +00:00
|
|
|
(int)dev->addrlength);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
else
|
1999-02-11 06:38:12 +00:00
|
|
|
cc = send(sock->fd,
|
|
|
|
ISC_SOCKDATA_CAST(dev->region.base + dev->n),
|
1998-11-11 00:43:14 +00:00
|
|
|
write_count, 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* check for error or block condition
|
|
|
|
*/
|
|
|
|
if (cc < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
1998-11-11 00:43:14 +00:00
|
|
|
goto poke;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
|
|
|
if (errno == _system) { \
|
|
|
|
if (sock->connected) { \
|
|
|
|
if (sock->type == isc_socket_tcp) \
|
1999-03-29 23:56:06 +00:00
|
|
|
sock->send_result = _isc; \
|
|
|
|
send_senddone_event(sock, &iev->task, &iev, \
|
|
|
|
&dev, _isc); \
|
1998-12-18 01:48:43 +00:00
|
|
|
} \
|
|
|
|
goto next; \
|
|
|
|
}
|
|
|
|
|
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
|
|
|
SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
|
|
|
#undef SOFT_OR_HARD
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This might not be a permanent error.
|
|
|
|
*/
|
|
|
|
if (errno == ENOBUFS) {
|
1999-03-29 23:56:06 +00:00
|
|
|
send_senddone_event(sock, &iev->task, &iev,
|
|
|
|
&dev, ISC_R_NORESOURCES);
|
1998-12-18 01:48:43 +00:00
|
|
|
|
|
|
|
goto next;
|
|
|
|
}
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
/*
|
1999-03-29 23:56:06 +00:00
|
|
|
* The other error types depend on whether or not the
|
1998-12-10 16:14:05 +00:00
|
|
|
* 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.
|
|
|
|
*/
|
1998-11-11 00:43:14 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-12-04 11:21:11 +00:00
|
|
|
"internal_send: %s",
|
1998-11-11 00:43:14 +00:00
|
|
|
strerror(errno));
|
1998-12-18 01:48:43 +00:00
|
|
|
sock->send_result = ISC_R_UNEXPECTED;
|
1999-03-29 23:56:06 +00:00
|
|
|
send_senddone_event(sock, &iev->task, &iev, &dev,
|
1998-12-18 01:48:43 +00:00
|
|
|
ISC_R_UNEXPECTED);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
if (cc == 0)
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_send: send() returned 0");
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
/*
|
|
|
|
* if we write less than we expected, update counters,
|
|
|
|
* poke.
|
|
|
|
*/
|
|
|
|
if ((size_t)cc < write_count) {
|
|
|
|
dev->n += cc;
|
|
|
|
|
|
|
|
goto poke;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-11 02:05:36 +00:00
|
|
|
* Exactly what we wanted to write. We're done with this
|
1998-11-11 00:43:14 +00:00
|
|
|
* entry. Post its completion event.
|
|
|
|
*/
|
|
|
|
if ((size_t)cc == write_count) {
|
|
|
|
dev->n += write_count;
|
1999-03-29 23:56:06 +00:00
|
|
|
send_senddone_event(sock, &iev->task,
|
|
|
|
&iev, &dev, ISC_R_SUCCESS);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
goto next;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
next:
|
1998-12-18 22:02:41 +00:00
|
|
|
; /* some compilers need this here... */
|
1998-12-04 11:21:11 +00:00
|
|
|
} while (!EMPTY(sock->send_list));
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
poke:
|
1998-12-04 11:21:11 +00:00
|
|
|
if (!EMPTY(sock->send_list))
|
1998-11-11 00:43:14 +00:00
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
|
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
|
1998-11-06 01:45:35 +00:00
|
|
|
watcher(void *uap)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager = uap;
|
|
|
|
isc_socket_t *sock;
|
1998-11-03 00:54:47 +00:00
|
|
|
isc_boolean_t done;
|
|
|
|
int ctlfd;
|
|
|
|
int cc;
|
|
|
|
fd_set readfds;
|
|
|
|
fd_set writefds;
|
1998-11-10 01:56:44 +00:00
|
|
|
int msg;
|
1998-11-06 01:45:35 +00:00
|
|
|
isc_boolean_t unlock_sock;
|
|
|
|
int i;
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t *iev;
|
|
|
|
ncintev_t *nciev;
|
1998-11-11 01:44:08 +00:00
|
|
|
int maxfd;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the control fd here. This will never change.
|
|
|
|
*/
|
|
|
|
LOCK(&manager->lock);
|
|
|
|
ctlfd = manager->pipe_fds[0];
|
|
|
|
|
|
|
|
done = ISC_FALSE;
|
|
|
|
while (!done) {
|
1998-11-11 00:43:14 +00:00
|
|
|
do {
|
|
|
|
readfds = manager->read_fds;
|
|
|
|
writefds = manager->write_fds;
|
1998-11-11 01:44:08 +00:00
|
|
|
maxfd = manager->maxfd + 1;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
1998-12-10 16:14:05 +00:00
|
|
|
#ifdef ISC_SOCKET_DEBUG
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_WATCHER, ("select maxfd %d\n", maxfd));
|
|
|
|
for (i = 0 ; i < FD_SETSIZE ; i++) {
|
|
|
|
int printit;
|
|
|
|
|
|
|
|
printit = 0;
|
|
|
|
|
|
|
|
if (FD_ISSET(i, &readfds)) {
|
|
|
|
printf("watcher: select r on %d\n", i);
|
|
|
|
printit = 1;
|
|
|
|
}
|
|
|
|
if (FD_ISSET(i, &writefds)) {
|
|
|
|
printf("watcher: select w on %d\n", i);
|
|
|
|
printit = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (printit && manager->fds[i] != NULL)
|
|
|
|
socket_dump(manager->fds[i]);
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
#endif
|
1998-12-05 00:28:13 +00:00
|
|
|
|
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);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("select(%d, ...) == %d, errno %d\n",
|
1998-11-11 01:44:08 +00:00
|
|
|
maxfd, cc, errno));
|
1998-11-06 01:45:35 +00:00
|
|
|
if (cc < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (!SOFT_ERROR(errno))
|
1998-12-05 00:28:13 +00:00
|
|
|
FATAL_ERROR(__FILE__, __LINE__,
|
|
|
|
"select failed: %s",
|
|
|
|
strerror(errno));
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
1998-11-06 01:45:35 +00:00
|
|
|
} while (cc < 0);
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process reads on internal, control fd.
|
|
|
|
*/
|
|
|
|
if (FD_ISSET(ctlfd, &readfds)) {
|
1998-11-06 01:45:35 +00:00
|
|
|
while (1) {
|
|
|
|
msg = select_readmsg(manager);
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watcher got message %d\n", msg));
|
1998-11-10 01:56:44 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* Nothing to read?
|
|
|
|
*/
|
|
|
|
if (msg == SELECT_POKE_NOTHING)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle shutdown message. We really should
|
|
|
|
* jump out of this loop right away, but
|
|
|
|
* it doesn't matter if we have to do a little
|
|
|
|
* more work first.
|
|
|
|
*/
|
1998-12-05 00:28:13 +00:00
|
|
|
if (msg == SELECT_POKE_SHUTDOWN) {
|
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watcher got SHUTDOWN\n"));
|
1998-11-06 01:45:35 +00:00
|
|
|
done = ISC_TRUE;
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
/*
|
|
|
|
* This is a wakeup on a socket. Look
|
|
|
|
* at the event queue for both read and write,
|
|
|
|
* and decide if we need to watch on it now
|
|
|
|
* or not.
|
|
|
|
*/
|
|
|
|
if (msg >= 0) {
|
|
|
|
INSIST(msg < FD_SETSIZE);
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
if (manager->fdstate[msg] ==
|
|
|
|
CLOSE_PENDING) {
|
1998-12-05 00:28:13 +00:00
|
|
|
manager->fdstate[msg] = CLOSED;
|
|
|
|
FD_CLR(msg,
|
|
|
|
&manager->read_fds);
|
|
|
|
FD_CLR(msg,
|
|
|
|
&manager->write_fds);
|
|
|
|
|
|
|
|
close(msg);
|
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("Watcher closed %d\n",
|
|
|
|
msg));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (manager->fdstate[msg] != MANAGED)
|
|
|
|
continue;
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
sock = manager->fds[msg];
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
LOCK(&sock->lock);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watcher locked socket %p\n",
|
1998-11-10 01:56:44 +00:00
|
|
|
sock));
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If there are no events, or there
|
|
|
|
* is an event but we have already
|
|
|
|
* queued up the internal event on a
|
|
|
|
* task's queue, clear the bit.
|
|
|
|
* Otherwise, set it.
|
|
|
|
*/
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
nciev = ISC_LIST_HEAD(sock->accept_list);
|
1998-11-10 11:37:54 +00:00
|
|
|
if ((iev == NULL && nciev == NULL)
|
1998-12-05 00:28:13 +00:00
|
|
|
|| sock->pending_recv
|
|
|
|
|| sock->pending_accept) {
|
1998-11-06 01:45:35 +00:00
|
|
|
FD_CLR(sock->fd,
|
|
|
|
&manager->read_fds);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watch cleared r\n"));
|
1998-11-10 01:56:44 +00:00
|
|
|
} else {
|
1998-11-06 01:45:35 +00:00
|
|
|
FD_SET(sock->fd,
|
|
|
|
&manager->read_fds);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watch set r\n"));
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->send_list);
|
1998-11-26 00:10:33 +00:00
|
|
|
if ((iev == NULL
|
1998-12-04 11:21:11 +00:00
|
|
|
|| sock->pending_send)
|
1998-11-26 00:10:33 +00:00
|
|
|
&& !sock->connecting) {
|
1998-11-06 01:45:35 +00:00
|
|
|
FD_CLR(sock->fd,
|
|
|
|
&manager->write_fds);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watch cleared w\n"));
|
1998-11-10 01:56:44 +00:00
|
|
|
} else {
|
1998-11-06 01:45:35 +00:00
|
|
|
FD_SET(sock->fd,
|
|
|
|
&manager->write_fds);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watch set w\n"));
|
1998-11-10 01:56:44 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-06 01:45:35 +00:00
|
|
|
* Process read/writes on other fds here. Avoid locking
|
|
|
|
* and unlocking twice if both reads and writes are possible.
|
1998-11-03 00:54:47 +00:00
|
|
|
*/
|
1998-11-11 01:44:08 +00:00
|
|
|
for (i = 0 ; i < maxfd ; i++) {
|
1998-12-05 00:28:13 +00:00
|
|
|
if (i == manager->pipe_fds[0]
|
|
|
|
|| i == manager->pipe_fds[1])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (manager->fdstate[i] == CLOSE_PENDING) {
|
|
|
|
manager->fdstate[i] = CLOSED;
|
|
|
|
FD_CLR(i, &manager->read_fds);
|
|
|
|
FD_CLR(i, &manager->write_fds);
|
|
|
|
|
|
|
|
close(i);
|
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("Watcher closed %d\n", i));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock = manager->fds[i];
|
|
|
|
unlock_sock = ISC_FALSE;
|
|
|
|
if (FD_ISSET(i, &readfds)) {
|
|
|
|
if (sock == NULL) {
|
1998-11-06 01:45:35 +00:00
|
|
|
FD_CLR(i, &manager->read_fds);
|
1998-12-05 00:28:13 +00:00
|
|
|
goto check_write;
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watcher r on %d, sock %p\n",
|
|
|
|
i, manager->fds[i]));
|
|
|
|
unlock_sock = ISC_TRUE;
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
if (sock->listener)
|
|
|
|
dispatch_listen(sock);
|
|
|
|
else
|
|
|
|
dispatch_read(sock);
|
|
|
|
FD_CLR(i, &manager->read_fds);
|
|
|
|
}
|
|
|
|
check_write:
|
|
|
|
if (FD_ISSET(i, &writefds)) {
|
|
|
|
if (sock == NULL) {
|
1998-11-06 01:45:35 +00:00
|
|
|
FD_CLR(i, &manager->write_fds);
|
1998-12-05 00:28:13 +00:00
|
|
|
continue;
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_WATCHER,
|
|
|
|
("watcher w on %d, sock %p\n",
|
|
|
|
i, manager->fds[i]));
|
|
|
|
if (!unlock_sock) {
|
|
|
|
unlock_sock = ISC_TRUE;
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
}
|
|
|
|
if (sock->connecting)
|
|
|
|
dispatch_connect(sock);
|
|
|
|
else
|
|
|
|
dispatch_write(sock);
|
|
|
|
FD_CLR(i, &manager->write_fds);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
if (unlock_sock)
|
|
|
|
UNLOCK(&sock->lock);
|
1998-11-06 01:45:35 +00:00
|
|
|
}
|
1998-11-03 00:54:47 +00:00
|
|
|
}
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_WATCHER, ("Watcher exiting\n"));
|
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
UNLOCK(&manager->lock);
|
|
|
|
return ((isc_threadresult_t)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new socket manager.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
1998-12-18 19:14:37 +00:00
|
|
|
isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
REQUIRE(managerp != NULL && *managerp == NULL);
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XENTER(TRACE_MANAGER, "isc_socketmgr_create");
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
manager = isc_mem_get(mctx, sizeof *manager);
|
|
|
|
if (manager == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
manager->magic = SOCKET_MANAGER_MAGIC;
|
|
|
|
manager->mctx = mctx;
|
1998-11-06 01:45:35 +00:00
|
|
|
memset(manager->fds, 0, sizeof(manager->fds));
|
|
|
|
manager->nsockets = 0;
|
1998-11-03 00:54:47 +00:00
|
|
|
if (isc_mutex_init(&manager->lock) != ISC_R_SUCCESS) {
|
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_mutex_init() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
1999-05-17 22:31:26 +00:00
|
|
|
if (isc_condition_init(&manager->shutdown_ok) != ISC_R_SUCCESS) {
|
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_condition_init() failed");
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"pipe() failed: %s",
|
1998-11-26 00:10:33 +00:00
|
|
|
strerror(errno));
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
1999-01-06 20:02:52 +00:00
|
|
|
RUNTIME_CHECK(make_nonblock(manager->pipe_fds[0]) == ISC_R_SUCCESS);
|
|
|
|
RUNTIME_CHECK(make_nonblock(manager->pipe_fds[1]) == ISC_R_SUCCESS);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-03 00:54:47 +00:00
|
|
|
/*
|
|
|
|
* Set up initial state for the select loop
|
|
|
|
*/
|
|
|
|
FD_ZERO(&manager->read_fds);
|
|
|
|
FD_ZERO(&manager->write_fds);
|
|
|
|
FD_SET(manager->pipe_fds[0], &manager->read_fds);
|
1998-11-11 01:44:08 +00:00
|
|
|
manager->maxfd = manager->pipe_fds[0];
|
1998-12-05 00:28:13 +00:00
|
|
|
memset(manager->fdstate, 0, sizeof(manager->fdstate));
|
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) {
|
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
|
|
|
isc_mem_put(mctx, manager, sizeof *manager);
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_thread_create() failed");
|
1998-12-10 16:14:05 +00:00
|
|
|
close(manager->pipe_fds[0]);
|
|
|
|
close(manager->pipe_fds[1]);
|
1998-11-03 00:54:47 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
*managerp = manager;
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XEXIT(TRACE_MANAGER, "isc_socketmgr_create (normal)");
|
1998-11-03 00:54:47 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_destroy(isc_socketmgr_t **managerp)
|
1998-11-03 00:54:47 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketmgr_t *manager;
|
1998-12-05 00:28:13 +00:00
|
|
|
int i;
|
1998-11-03 00:54:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Destroy a socket manager.
|
|
|
|
*/
|
|
|
|
|
|
|
|
REQUIRE(managerp != NULL);
|
|
|
|
manager = *managerp;
|
|
|
|
REQUIRE(VALID_MANAGER(manager));
|
|
|
|
|
|
|
|
LOCK(&manager->lock);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
XTRACE(TRACE_MANAGER, ("nsockets == %d\n", manager->nsockets));
|
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
|
|
|
*/
|
|
|
|
while (manager->nsockets != 0) {
|
|
|
|
XTRACE(TRACE_MANAGER, ("nsockets == %d\n", manager->nsockets));
|
1999-05-17 22:31:26 +00:00
|
|
|
WAIT(&manager->shutdown_ok, &manager->lock);
|
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.
|
|
|
|
*/
|
|
|
|
select_poke(manager, SELECT_POKE_SHUTDOWN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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__,
|
|
|
|
"isc_thread_join() failed");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clean up.
|
|
|
|
*/
|
|
|
|
close(manager->pipe_fds[0]);
|
|
|
|
close(manager->pipe_fds[1]);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
for (i = 0 ; i < FD_SETSIZE ; i++)
|
|
|
|
if (manager->fdstate[i] == CLOSE_PENDING)
|
|
|
|
close(i);
|
|
|
|
|
1999-05-17 22:31:26 +00:00
|
|
|
(void)isc_condition_destroy(&manager->shutdown_ok);
|
1998-11-03 00:54:47 +00:00
|
|
|
(void)isc_mutex_destroy(&manager->lock);
|
|
|
|
manager->magic = 0;
|
|
|
|
isc_mem_put(manager->mctx, manager, sizeof *manager);
|
|
|
|
|
|
|
|
*managerp = NULL;
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_recv(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
isc_boolean_t partial, isc_task_t *task,
|
1998-11-06 01:45:35 +00:00
|
|
|
isc_taskaction_t action, void *arg)
|
|
|
|
{
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_socketevent_t *dev;
|
1998-12-13 23:45:21 +00:00
|
|
|
rwintev_t *iev;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_task_t *ntask = NULL;
|
1998-11-11 00:43:14 +00:00
|
|
|
int cc;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
manager = sock->manager;
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
dev = (isc_socketevent_t *)isc_event_allocate(manager->mctx, sock,
|
|
|
|
ISC_SOCKEVENT_RECVDONE,
|
|
|
|
action, arg,
|
|
|
|
sizeof(*dev));
|
|
|
|
if (dev == NULL)
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
if (sock->riev == NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (rwintev_t *)isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_INTRECV,
|
|
|
|
internal_recv,
|
|
|
|
sock,
|
|
|
|
sizeof(*iev));
|
1998-11-06 01:45:35 +00:00
|
|
|
if (iev == NULL) {
|
|
|
|
/* no special free routine yet */
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_event_free((isc_event_t **)&dev);
|
1998-12-05 01:44:38 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-06 01:45:35 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK_INIT(iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->posted = ISC_FALSE;
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
sock->riev = iev;
|
|
|
|
iev = NULL; /* just in case */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-12-18 01:48:43 +00:00
|
|
|
* UDP sockets are always partial read
|
1998-11-06 01:45:35 +00:00
|
|
|
*/
|
1998-12-18 01:48:43 +00:00
|
|
|
if (sock->type == isc_socket_udp)
|
|
|
|
partial = ISC_TRUE;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->region = *region;
|
|
|
|
dev->n = 0;
|
|
|
|
dev->result = ISC_R_SUCCESS;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the read queue is empty, try to do the I/O right now.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (EMPTY(sock->recv_list)) {
|
1998-11-11 00:43:14 +00:00
|
|
|
if (sock->type == isc_socket_udp) {
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T addrlen;
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->addrlength = sizeof(isc_sockaddr_t);
|
|
|
|
addrlen = (ISC_SOCKADDR_LEN_T)dev->addrlength;
|
1999-02-11 06:38:12 +00:00
|
|
|
cc = recvfrom(sock->fd,
|
1999-03-29 23:56:06 +00:00
|
|
|
ISC_SOCKDATA_CAST(dev->region.base),
|
|
|
|
dev->region.length, 0,
|
|
|
|
(struct sockaddr *)&dev->address,
|
1999-02-06 08:48:08 +00:00
|
|
|
&addrlen);
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->addrlength = (unsigned int)addrlen;
|
1998-11-11 00:43:14 +00:00
|
|
|
} else {
|
1999-03-29 23:56:06 +00:00
|
|
|
/*
|
|
|
|
* recv() is used on TCP sockets, since some OSs
|
|
|
|
* don't like recvfrom() being called on TCP sockets.
|
|
|
|
* Failures range from function failure returns to
|
|
|
|
* the addresses not being filled in properly.
|
|
|
|
*/
|
|
|
|
cc = recv(sock->fd,
|
|
|
|
ISC_SOCKDATA_CAST(dev->region.base),
|
|
|
|
dev->region.length, 0);
|
|
|
|
dev->address = sock->address;
|
|
|
|
dev->addrlength = sock->addrlength;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cc < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
1998-11-11 00:43:14 +00:00
|
|
|
goto queue;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
|
|
|
if (errno == _system) { \
|
|
|
|
if (sock->connected) { \
|
|
|
|
if (sock->type == isc_socket_tcp) \
|
|
|
|
sock->recv_result = _isc; \
|
|
|
|
send_recvdone_event(sock, &task, NULL, &dev, _isc); \
|
|
|
|
} \
|
|
|
|
select_poke(sock->manager, sock->fd); \
|
|
|
|
goto out; \
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
|
|
|
SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
|
|
|
#undef SOFT_OR_HARD
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
/*
|
|
|
|
* This might not be a permanent error.
|
|
|
|
*/
|
|
|
|
if (errno == ENOBUFS) {
|
|
|
|
send_recvdone_event(sock, &task, NULL, &dev,
|
|
|
|
ISC_R_NORESOURCES);
|
|
|
|
|
|
|
|
goto queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sock->recv_result = ISC_R_UNEXPECTED;
|
|
|
|
send_recvdone_event(sock, &task, NULL, &dev,
|
|
|
|
ISC_R_UNEXPECTED);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
/*
|
|
|
|
* On TCP, zero length reads indicate EOF, while on
|
|
|
|
* UDP, zero length reads are perfectly valid, although
|
|
|
|
* strange.
|
|
|
|
*/
|
|
|
|
if ((sock->type == isc_socket_tcp) && (cc == 0)) {
|
|
|
|
sock->recv_result = ISC_R_EOF;
|
|
|
|
send_recvdone_event(sock, &task, NULL,
|
|
|
|
&dev, ISC_R_EOF);
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->n = cc;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Partial reads need to be queued
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
if ((size_t)cc != dev->region.length && !partial)
|
1998-11-11 00:43:14 +00:00
|
|
|
goto queue;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
/*
|
|
|
|
* full reads are posted, or partials if partials are ok.
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &task, NULL, &dev, ISC_R_SUCCESS);
|
1998-11-06 01:45:35 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We couldn't read all or part of the request right now, so queue
|
|
|
|
* it.
|
|
|
|
*/
|
1998-11-11 00:43:14 +00:00
|
|
|
queue:
|
1998-11-06 01:45:35 +00:00
|
|
|
iev = sock->riev;
|
|
|
|
sock->riev = NULL;
|
|
|
|
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
iev->done_ev = dev;
|
1998-11-06 01:45:35 +00:00
|
|
|
iev->task = ntask;
|
|
|
|
iev->partial = partial;
|
1998-12-18 04:03:11 +00:00
|
|
|
iev->canceled = ISC_FALSE;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
|
|
|
* Enqueue the request. If the socket was previously not being
|
|
|
|
* watched, poke the watcher to start paying attention to it.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (EMPTY(sock->recv_list)) {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->recv_list, iev, link);
|
1998-11-07 02:31:04 +00:00
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
} else {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->recv_list, iev, link);
|
1998-11-07 02:31:04 +00:00
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_RECV,
|
|
|
|
("isc_socket_recv: posted ievent %p, dev %p, task %p\n",
|
1998-11-10 11:37:54 +00:00
|
|
|
iev, iev->done_ev, task));
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
out:
|
1998-11-06 01:45:35 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_send(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg)
|
1998-11-11 00:43:14 +00:00
|
|
|
{
|
1998-12-18 02:28:27 +00:00
|
|
|
return (isc_socket_sendto(sock, region, task, action, arg, NULL, 0));
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg,
|
1999-02-06 08:48:08 +00:00
|
|
|
isc_sockaddr_t *address, unsigned int addrlen)
|
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
|
|
|
rwintev_t *iev;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_task_t *ntask = NULL;
|
1998-11-11 00:43:14 +00:00
|
|
|
int cc;
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T addrlength = (ISC_SOCKADDR_LEN_T)addrlen;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1998-12-18 02:28:27 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
1998-11-11 00:43:14 +00:00
|
|
|
manager = sock->manager;
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
dev = (isc_socketevent_t *)isc_event_allocate(manager->mctx, sock,
|
|
|
|
ISC_SOCKEVENT_SENDDONE,
|
|
|
|
action, arg,
|
|
|
|
sizeof(*dev));
|
|
|
|
if (dev == NULL)
|
1998-11-11 00:43:14 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
if (sock->wiev == NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (rwintev_t *)isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_INTSEND,
|
|
|
|
internal_send,
|
|
|
|
sock,
|
|
|
|
sizeof(*iev));
|
1998-11-11 00:43:14 +00:00
|
|
|
if (iev == NULL) {
|
|
|
|
/* no special free routine yet */
|
1999-03-29 23:56:06 +00:00
|
|
|
isc_event_free((isc_event_t **)&dev);
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-11 00:43:14 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK_INIT(iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->posted = ISC_FALSE;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
sock->wiev = iev;
|
|
|
|
iev = NULL; /* just in case */
|
|
|
|
}
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->region = *region;
|
|
|
|
dev->n = 0;
|
|
|
|
dev->result = ISC_R_SUCCESS;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the write queue is empty, try to do the I/O right now.
|
|
|
|
*/
|
|
|
|
if (sock->type == isc_socket_udp) {
|
|
|
|
INSIST(addrlength > 0 || sock->addrlength > 0);
|
|
|
|
if (addrlength > 0) {
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->address = *address;
|
|
|
|
dev->addrlength = addrlength;
|
1998-11-11 00:43:14 +00:00
|
|
|
} else if (sock->addrlength > 0) {
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->address = sock->address;
|
|
|
|
dev->addrlength = sock->addrlength;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
} else if (sock->type == isc_socket_tcp) {
|
|
|
|
INSIST(address == NULL);
|
|
|
|
INSIST(addrlength == 0);
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->address = sock->address;
|
|
|
|
dev->addrlength = sock->addrlength;
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-04 11:21:11 +00:00
|
|
|
if (EMPTY(sock->send_list)) {
|
1998-11-11 00:43:14 +00:00
|
|
|
if (sock->type == isc_socket_udp)
|
1999-02-11 06:38:12 +00:00
|
|
|
cc = sendto(sock->fd,
|
1999-03-29 23:56:06 +00:00
|
|
|
ISC_SOCKDATA_CAST(dev->region.base),
|
|
|
|
dev->region.length, 0,
|
|
|
|
(struct sockaddr *)&dev->address,
|
|
|
|
(int)dev->addrlength);
|
1998-11-11 00:43:14 +00:00
|
|
|
else if (sock->type == isc_socket_tcp)
|
1999-03-29 23:56:06 +00:00
|
|
|
cc = send(sock->fd,
|
|
|
|
ISC_SOCKDATA_CAST(dev->region.base),
|
|
|
|
dev->region.length, 0);
|
1998-11-26 00:10:33 +00:00
|
|
|
else {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
1998-12-13 23:45:21 +00:00
|
|
|
"isc_socket_send: "
|
|
|
|
"unknown socket type");
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-26 00:10:33 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
if (cc < 0) {
|
1998-12-04 11:21:11 +00:00
|
|
|
if (SOFT_ERROR(errno))
|
1998-11-11 00:43:14 +00:00
|
|
|
goto queue;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
#define SOFT_OR_HARD(_system, _isc) \
|
|
|
|
if (errno == _system) { \
|
|
|
|
if (sock->connected) { \
|
|
|
|
if (sock->type == isc_socket_tcp) \
|
|
|
|
sock->send_result = _isc; \
|
|
|
|
send_senddone_event(sock, &task, NULL, &dev, _isc); \
|
|
|
|
} \
|
|
|
|
select_poke(sock->manager, sock->fd); \
|
|
|
|
goto out; \
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
SOFT_OR_HARD(ECONNREFUSED, ISC_R_CONNREFUSED);
|
|
|
|
SOFT_OR_HARD(ENETUNREACH, ISC_R_NETUNREACH);
|
|
|
|
SOFT_OR_HARD(EHOSTUNREACH, ISC_R_HOSTUNREACH);
|
|
|
|
#undef SOFT_OR_HARD
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
/*
|
|
|
|
* This might not be a permanent error.
|
|
|
|
*/
|
|
|
|
if (errno == ENOBUFS) {
|
|
|
|
send_senddone_event(sock, &task, NULL, &dev,
|
|
|
|
ISC_R_NORESOURCES);
|
|
|
|
|
|
|
|
goto out;
|
|
|
|
}
|
1998-11-11 00:43:14 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
sock->send_result = ISC_R_UNEXPECTED;
|
|
|
|
send_senddone_event(sock, &task, NULL, &dev,
|
|
|
|
ISC_R_UNEXPECTED);
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
dev->n = cc;
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Partial writes need to be queued
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
if ((size_t)cc != dev->region.length)
|
1998-11-11 00:43:14 +00:00
|
|
|
goto queue;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* full writes are posted.
|
|
|
|
*/
|
1999-03-29 23:56:06 +00:00
|
|
|
send_senddone_event(sock, &task, NULL, &dev, ISC_R_SUCCESS);
|
1998-11-11 00:43:14 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We couldn't send all or part of the request right now, so queue
|
|
|
|
* it.
|
|
|
|
*/
|
|
|
|
queue:
|
|
|
|
iev = sock->wiev;
|
|
|
|
sock->wiev = NULL;
|
|
|
|
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
iev->done_ev = dev;
|
1998-11-11 00:43:14 +00:00
|
|
|
iev->task = ntask;
|
|
|
|
iev->partial = ISC_FALSE; /* doesn't matter */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Enqueue the request. If the socket was previously not being
|
|
|
|
* watched, poke the watcher to start paying attention to it.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (EMPTY(sock->send_list)) {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->send_list, iev, link);
|
1998-11-11 00:43:14 +00:00
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
} else {
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->send_list, iev, link);
|
1998-11-11 00:43:14 +00:00
|
|
|
}
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_SEND,
|
|
|
|
("isc_socket_send: posted ievent %p, dev %p, task %p\n",
|
1998-11-11 00:43:14 +00:00
|
|
|
iev, iev->done_ev, task));
|
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
out:
|
1998-11-11 00:43:14 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *sockaddr,
|
1998-11-06 01:45:35 +00:00
|
|
|
int addrlen)
|
|
|
|
{
|
1998-12-05 01:44:38 +00:00
|
|
|
int on = 1;
|
|
|
|
|
1998-11-06 01:45:35 +00:00
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
1998-12-05 01:44:38 +00:00
|
|
|
if (setsockopt(sock->fd, SOL_SOCKET, SO_REUSEADDR,
|
1999-02-11 06:38:12 +00:00
|
|
|
ISC_SOCKDATA_CAST(&on), sizeof on) < 0) {
|
1998-12-05 01:44:38 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "setsockopt(%d) failed",
|
|
|
|
sock->fd);
|
|
|
|
/* Press on... */
|
|
|
|
}
|
1998-11-06 01:45:35 +00:00
|
|
|
if (bind(sock->fd, (struct sockaddr *)sockaddr, addrlen) < 0) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
switch (errno) {
|
|
|
|
case EACCES:
|
|
|
|
return (ISC_R_NOPERM);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-06 01:45:35 +00:00
|
|
|
break;
|
|
|
|
case EADDRNOTAVAIL:
|
|
|
|
return (ISC_R_ADDRNOTAVAIL);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-06 01:45:35 +00:00
|
|
|
break;
|
|
|
|
case EADDRINUSE:
|
|
|
|
return (ISC_R_ADDRINUSE);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-06 01:45:35 +00:00
|
|
|
break;
|
|
|
|
case EINVAL:
|
|
|
|
return (ISC_R_BOUND);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-06 01:45:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"bind: %s", strerror(errno));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1998-11-06 01:45:35 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set up to listen on a given socket. We do this by creating an internal
|
|
|
|
* event that will be dispatched when the socket has read activity. The
|
|
|
|
* watcher will send the internal event to the task when there is a new
|
|
|
|
* connection.
|
|
|
|
*
|
|
|
|
* Unlike in read, we don't preallocate a done event here. Every time there
|
|
|
|
* is a new connection we'll have to allocate a new one anyway, so we might
|
|
|
|
* as well keep things simple rather than having to track them.
|
|
|
|
*/
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_listen(isc_socket_t *sock, unsigned int backlog)
|
1998-11-06 01:45:35 +00:00
|
|
|
{
|
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);
|
|
|
|
REQUIRE(sock->type == isc_socket_tcp);
|
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);
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__, "listen: %s",
|
|
|
|
strerror(errno));
|
|
|
|
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
1998-11-10 11:37:54 +00:00
|
|
|
sock->listener = ISC_TRUE;
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* This should try to do agressive accept()
|
|
|
|
*/
|
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,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg)
|
1998-11-10 11:37:54 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
ncintev_t *iev;
|
|
|
|
isc_socket_newconnev_t *dev;
|
|
|
|
isc_task_t *ntask = NULL;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_socket_t *nsock;
|
1998-11-10 11:37:54 +00:00
|
|
|
isc_result_t ret;
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XENTER(TRACE_LISTEN, "isc_socket_accept");
|
1998-12-01 17:58:34 +00:00
|
|
|
|
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);
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (ncintev_t *)isc_event_allocate(manager->mctx, sock,
|
|
|
|
ISC_SOCKEVENT_INTACCEPT,
|
|
|
|
internal_accept, sock,
|
|
|
|
sizeof(*iev));
|
1998-11-10 11:37:54 +00:00
|
|
|
if (iev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->posted = ISC_FALSE;
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
dev = (isc_socket_newconnev_t *)
|
|
|
|
isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_NEWCONN,
|
|
|
|
action,
|
|
|
|
arg,
|
|
|
|
sizeof (*dev));
|
1998-11-10 11:37:54 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-11-10 11:37:54 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ret = allocate_socket(manager, sock->type, &nsock);
|
|
|
|
if (ret != ISC_R_SUCCESS) {
|
|
|
|
UNLOCK(&sock->lock);
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&iev);
|
|
|
|
isc_event_free((isc_event_t **)&dev);
|
1998-11-10 11:37:54 +00:00
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK_INIT(iev, link);
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-11-07 02:31:04 +00:00
|
|
|
/*
|
|
|
|
* Attach to socket and to task
|
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
1998-11-26 00:29:12 +00:00
|
|
|
nsock->references++;
|
1998-11-10 11:37:54 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
sock->listener = ISC_TRUE;
|
1998-11-07 02:31:04 +00:00
|
|
|
|
|
|
|
iev->task = ntask;
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->done_ev = dev;
|
1998-11-10 11:37:54 +00:00
|
|
|
iev->canceled = ISC_FALSE;
|
|
|
|
dev->newsocket = nsock;
|
1998-11-06 01:45:35 +00:00
|
|
|
|
1998-11-10 01:56:44 +00:00
|
|
|
/*
|
|
|
|
* poke watcher here. We still have the socket locked, so there
|
|
|
|
* is no race condition. We will keep the lock for such a short
|
|
|
|
* bit of time waking it up now or later won't matter all that much.
|
|
|
|
*/
|
1998-12-04 11:21:11 +00:00
|
|
|
if (EMPTY(sock->accept_list))
|
1998-11-10 01:56:44 +00:00
|
|
|
select_poke(manager, sock->fd);
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->accept_list, iev, link);
|
1998-11-10 01:56:44 +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
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addr, int addrlen,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, 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;
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XENTER(TRACE_CONNECT, "isc_socket_connect");
|
1998-11-26 00:10:33 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
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
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
REQUIRE(!sock->connecting);
|
|
|
|
|
|
|
|
if (sock->ciev == NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
sock->ciev = (cnintev_t *)
|
|
|
|
isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_INTCONN,
|
|
|
|
internal_connect,
|
|
|
|
sock,
|
|
|
|
sizeof(*(sock->ciev)));
|
1998-11-26 00:10:33 +00:00
|
|
|
if (sock->ciev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
sock->ciev->posted = ISC_FALSE;
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
dev = (isc_socket_connev_t *)isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_CONNECT,
|
|
|
|
action,
|
|
|
|
arg,
|
|
|
|
sizeof (*dev));
|
1998-11-26 00:10:33 +00:00
|
|
|
if (dev == NULL) {
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to do the connect right away, as there can be only one
|
|
|
|
* outstanding, and it might happen to complete.
|
|
|
|
*/
|
|
|
|
sock->address = *addr;
|
|
|
|
sock->addrlength = addrlen;
|
|
|
|
cc = connect(sock->fd, (struct sockaddr *)addr, addrlen);
|
|
|
|
if (cc < 0) {
|
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) {
|
|
|
|
case ECONNREFUSED:
|
|
|
|
dev->result = ISC_R_CONNREFUSED;
|
|
|
|
goto err_exit;
|
|
|
|
case ENETUNREACH:
|
|
|
|
dev->result = ISC_R_NETUNREACH;
|
|
|
|
goto err_exit;
|
|
|
|
}
|
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
sock->connected = ISC_FALSE;
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"%s", strerror(errno));
|
1998-12-10 16:14:05 +00:00
|
|
|
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-11-26 00:10:33 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
|
1999-05-21 07:16:13 +00:00
|
|
|
err_exit:
|
|
|
|
sock->connected = ISC_FALSE;
|
|
|
|
ISC_TASK_SEND(task, (isc_event_t **)&dev);
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
1998-12-18 01:48:43 +00:00
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* If connect completed, fire off the done event
|
|
|
|
*/
|
|
|
|
if (cc == 0) {
|
1998-12-18 01:48:43 +00:00
|
|
|
sock->connected = ISC_TRUE;
|
1998-11-26 00:10:33 +00:00
|
|
|
dev->result = ISC_R_SUCCESS;
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_TASK_SEND(task, (isc_event_t **)&dev);
|
1998-11-26 00:10:33 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
queue:
|
|
|
|
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_CONNECT, ("queueing connect internal event\n"));
|
1998-11-26 00:10:33 +00:00
|
|
|
/*
|
|
|
|
* Attach to to task
|
|
|
|
*/
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
|
|
|
sock->connecting = ISC_TRUE;
|
|
|
|
|
|
|
|
sock->ciev->task = ntask;
|
1998-12-05 00:28:13 +00:00
|
|
|
sock->ciev->done_ev = dev;
|
1998-11-26 00:10:33 +00:00
|
|
|
sock->ciev->canceled = ISC_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* poke watcher here. We still have the socket locked, so there
|
|
|
|
* is no race condition. We will keep the lock for such a short
|
|
|
|
* bit of time waking it up now or later won't matter all that much.
|
|
|
|
*/
|
|
|
|
if (sock->connect_ev == NULL)
|
|
|
|
select_poke(manager, sock->fd);
|
|
|
|
|
|
|
|
sock->connect_ev = sock->ciev;
|
|
|
|
sock->ciev = NULL;
|
|
|
|
|
|
|
|
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
|
1998-12-13 23:45:21 +00:00
|
|
|
internal_connect(isc_task_t *task, isc_event_t *ev)
|
1998-11-26 00:10:33 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_t *sock;
|
|
|
|
isc_socket_connev_t *dev;
|
|
|
|
cnintev_t *iev;
|
1998-11-26 00:10:33 +00:00
|
|
|
int cc;
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T optlen;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
sock = ev->sender;
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (cnintev_t *)ev;
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
1998-11-26 00:29:12 +00:00
|
|
|
XTRACE(TRACE_CONNECT,
|
|
|
|
("internal_connect called, locked parent sock %p\n", sock));
|
1998-11-26 00:10:33 +00:00
|
|
|
|
|
|
|
REQUIRE(sock->connecting);
|
1998-12-13 23:45:21 +00:00
|
|
|
REQUIRE(sock->connect_ev == (cnintev_t *)ev);
|
1998-11-26 00:10:33 +00:00
|
|
|
REQUIRE(iev->task == task);
|
|
|
|
|
1998-12-18 04:03:11 +00:00
|
|
|
sock->connect_ev = NULL;
|
|
|
|
|
1998-11-26 00:10:33 +00:00
|
|
|
sock->connecting = ISC_FALSE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Has this event been canceled?
|
|
|
|
*/
|
|
|
|
if (iev->canceled) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)(sock->connect_ev));
|
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
|
|
|
}
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
dev = iev->done_ev;
|
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,
|
|
|
|
(char *)&cc, &optlen) < 0)
|
|
|
|
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) {
|
1998-11-26 00:10:33 +00:00
|
|
|
sock->connecting = ISC_TRUE;
|
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
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) {
|
|
|
|
case ETIMEDOUT:
|
|
|
|
dev->result = ISC_R_TIMEDOUT;
|
|
|
|
break;
|
|
|
|
case ECONNREFUSED:
|
|
|
|
dev->result = ISC_R_CONNREFUSED;
|
|
|
|
break;
|
|
|
|
case ENETUNREACH:
|
|
|
|
dev->result = ISC_R_NETUNREACH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
dev->result = ISC_R_UNEXPECTED;
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"internal_connect: connect() %s",
|
|
|
|
strerror(errno));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_TASK_SEND(iev->task, (isc_event_t **)&dev);
|
1998-12-18 04:03:11 +00:00
|
|
|
isc_task_detach(&iev->task);
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->done_ev = NULL;
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-11-26 00:10:33 +00:00
|
|
|
}
|
1998-12-01 23:59:39 +00:00
|
|
|
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp,
|
1998-12-01 23:59:39 +00:00
|
|
|
int *lengthp)
|
|
|
|
{
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(addressp != NULL);
|
|
|
|
REQUIRE(lengthp != NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
if (*lengthp < sock->addrlength) {
|
|
|
|
UNLOCK(&sock->lock);
|
1998-12-30 20:18:09 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1998-12-11 20:47:15 +00:00
|
|
|
memcpy(addressp, &sock->address, (size_t)sock->addrlength);
|
1998-12-01 23:59:39 +00:00
|
|
|
*lengthp = sock->addrlength;
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-12-01 23:59:39 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp,
|
1998-12-01 23:59:39 +00:00
|
|
|
int *lengthp)
|
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_sockaddr_t addr;
|
1999-02-06 08:48:08 +00:00
|
|
|
ISC_SOCKADDR_LEN_T len;
|
1998-12-01 23:59:39 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
REQUIRE(VALID_SOCKET(sock));
|
|
|
|
REQUIRE(addressp != NULL);
|
|
|
|
REQUIRE(lengthp != NULL);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
1998-12-01 23:59:39 +00:00
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname(sock->fd, (struct sockaddr *)&addr, &len) < 0) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"getsockname: %s", strerror(errno));
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-12-01 23:59:39 +00:00
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
1999-02-06 08:48:08 +00:00
|
|
|
if ((unsigned int)*lengthp < (unsigned int)len) {
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-12-30 20:18:09 +00:00
|
|
|
return (ISC_R_NOSPACE);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
1998-12-01 23:59:39 +00:00
|
|
|
|
1999-02-06 08:48:08 +00:00
|
|
|
memcpy(addressp, &sock->address, (size_t)len);
|
|
|
|
*lengthp = (unsigned int)len;
|
1998-12-01 23:59:39 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
1998-12-01 23:59:39 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
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
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_cancel(isc_socket_t *sock, isc_task_t *task,
|
1998-12-05 00:28:13 +00:00
|
|
|
unsigned int how)
|
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_boolean_t poke_needed;
|
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;
|
|
|
|
|
|
|
|
poke_needed = ISC_FALSE;
|
|
|
|
|
|
|
|
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)
|
|
|
|
&& !EMPTY(sock->recv_list)) {
|
|
|
|
rwintev_t *iev;
|
|
|
|
rwintev_t *next;
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_task_t *current_task;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->recv_list);
|
|
|
|
next = ISC_LIST_NEXT(iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
if ((task == NULL || task == iev->task) && !iev->canceled) {
|
|
|
|
dev = iev->done_ev;
|
|
|
|
current_task = iev->task;
|
|
|
|
|
1999-05-17 22:54:17 +00:00
|
|
|
/*
|
|
|
|
* DEQUEUE here, and if the item cannot be removed
|
|
|
|
* from the task's queue, PREPEND it to the front
|
|
|
|
* of the list again, so the normal cleanup thing
|
|
|
|
* will occur.
|
|
|
|
*/
|
|
|
|
ISC_LIST_DEQUEUE(sock->recv_list, iev, link);
|
1999-01-27 04:17:05 +00:00
|
|
|
if (iev->posted) {
|
1999-05-17 22:31:26 +00:00
|
|
|
if (isc_task_purge(current_task, sock,
|
1999-01-27 04:17:05 +00:00
|
|
|
ISC_SOCKEVENT_INTRECV)
|
|
|
|
== 0) {
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->canceled = ISC_TRUE;
|
|
|
|
iev->done_ev = NULL;
|
1999-05-17 22:56:13 +00:00
|
|
|
ISC_LIST_PREPEND(sock->recv_list,
|
1999-05-17 22:54:17 +00:00
|
|
|
iev, link);
|
1999-01-27 04:17:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
1999-01-27 04:17:05 +00:00
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
ISC_TASK_SEND(current_task, (isc_event_t **)&dev);
|
|
|
|
isc_task_detach(¤t_task);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
iev = next;
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
/*
|
|
|
|
* run through the event queue, posting done events with the
|
|
|
|
* canceled result, and freeing the internal event.
|
|
|
|
*/
|
|
|
|
while (iev != NULL) {
|
1999-04-29 04:49:52 +00:00
|
|
|
next = ISC_LIST_NEXT(iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
if (task == NULL || task == iev->task)
|
1999-03-29 23:56:06 +00:00
|
|
|
send_recvdone_event(sock, &iev->task, &iev,
|
1998-12-05 00:28:13 +00:00
|
|
|
&iev->done_ev,
|
|
|
|
ISC_R_CANCELED);
|
|
|
|
|
|
|
|
iev = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
if (((how & ISC_SOCKCANCEL_SEND) == ISC_SOCKCANCEL_SEND)
|
|
|
|
&& !EMPTY(sock->send_list)) {
|
|
|
|
rwintev_t *iev;
|
|
|
|
rwintev_t *next;
|
|
|
|
isc_socketevent_t *dev;
|
|
|
|
isc_task_t *current_task;
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->send_list);
|
|
|
|
next = ISC_LIST_NEXT(iev, link);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
if ((task == NULL || task == iev->task) && !iev->canceled) {
|
|
|
|
dev = iev->done_ev;
|
|
|
|
current_task = iev->task;
|
|
|
|
|
1999-05-17 22:54:17 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->send_list, iev, link);
|
1999-01-27 04:17:05 +00:00
|
|
|
if (iev->posted) {
|
|
|
|
if (isc_task_purge(current_task, sock,
|
|
|
|
ISC_SOCKEVENT_INTSEND)
|
|
|
|
== 0) {
|
1998-12-18 04:03:11 +00:00
|
|
|
iev->canceled = ISC_TRUE;
|
|
|
|
iev->done_ev = NULL;
|
1999-05-17 22:56:13 +00:00
|
|
|
ISC_LIST_PREPEND(sock->send_list,
|
1999-05-17 22:54:17 +00:00
|
|
|
iev, link);
|
1999-01-27 04:17:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-12-18 04:03:11 +00:00
|
|
|
}
|
1999-01-27 04:17:05 +00:00
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
ISC_TASK_SEND(current_task, (isc_event_t **)&dev);
|
|
|
|
isc_task_detach(¤t_task);
|
1998-12-18 04:03:11 +00:00
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
iev = next;
|
|
|
|
|
1998-12-18 04:03:11 +00:00
|
|
|
while (iev != NULL) {
|
1999-04-29 04:49:52 +00:00
|
|
|
next = ISC_LIST_NEXT(iev, link);
|
1998-12-18 04:03:11 +00:00
|
|
|
|
1999-03-29 23:56:06 +00:00
|
|
|
if ((task == NULL) || (task == iev->task))
|
|
|
|
send_senddone_event(sock, &iev->task, &iev,
|
1998-12-18 04:03:11 +00:00
|
|
|
&iev->done_ev,
|
|
|
|
ISC_R_CANCELED);
|
|
|
|
|
|
|
|
iev = next;
|
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
if (((how & ISC_SOCKCANCEL_ACCEPT) == ISC_SOCKCANCEL_ACCEPT)
|
|
|
|
&& !EMPTY(sock->accept_list)) {
|
1998-12-13 23:45:21 +00:00
|
|
|
ncintev_t * iev;
|
|
|
|
ncintev_t * next;
|
|
|
|
isc_socket_newconnev_t *dev;
|
1999-01-27 04:17:05 +00:00
|
|
|
isc_task_t *current_task;
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
iev = ISC_LIST_HEAD(sock->accept_list);
|
|
|
|
next = ISC_LIST_NEXT(iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
1999-05-13 10:46:12 +00:00
|
|
|
if ((task == NULL || task == iev->task) && !iev->canceled) {
|
1999-01-27 04:17:05 +00:00
|
|
|
dev = iev->done_ev;
|
|
|
|
current_task = iev->task;
|
|
|
|
|
1999-05-17 22:54:17 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->accept_list, iev, link);
|
1999-01-27 04:17:05 +00:00
|
|
|
if (iev->posted) {
|
1999-05-17 22:31:26 +00:00
|
|
|
if (isc_task_purge(current_task, sock,
|
1999-01-27 04:17:05 +00:00
|
|
|
ISC_SOCKEVENT_INTACCEPT)
|
|
|
|
== 0) {
|
1998-12-05 00:28:13 +00:00
|
|
|
iev->canceled = ISC_TRUE;
|
|
|
|
iev->done_ev = NULL;
|
1999-05-17 22:56:13 +00:00
|
|
|
ISC_LIST_PREPEND(sock->accept_list,
|
1999-05-17 22:54:17 +00:00
|
|
|
iev, link);
|
1999-01-27 04:17:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isc_event_free((isc_event_t **)&iev);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
1999-01-27 04:17:05 +00:00
|
|
|
|
|
|
|
dev->newsocket->references--;
|
|
|
|
free_socket(&dev->newsocket);
|
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
ISC_TASK_SEND(current_task, (isc_event_t **)&dev);
|
|
|
|
isc_task_detach(¤t_task);
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
1999-01-27 04:17:05 +00:00
|
|
|
iev = next;
|
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
while (iev != NULL) {
|
1999-04-29 04:49:52 +00:00
|
|
|
next = ISC_LIST_NEXT(iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
|
|
|
|
if (task == NULL || task == iev->task) {
|
|
|
|
dev = iev->done_ev;
|
|
|
|
iev->done_ev = NULL;
|
1999-01-27 04:17:05 +00:00
|
|
|
|
1998-12-05 00:28:13 +00:00
|
|
|
dev->newsocket->references--;
|
|
|
|
free_socket(&dev->newsocket);
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_DEQUEUE(sock->accept_list, iev, link);
|
1998-12-05 00:28:13 +00:00
|
|
|
send_ncdone_event(&iev, &dev, ISC_R_CANCELED);
|
|
|
|
}
|
|
|
|
|
|
|
|
iev = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
cnintev_t * iev;
|
|
|
|
isc_socket_connev_t *dev;
|
|
|
|
isc_task_t *current_task;
|
|
|
|
|
|
|
|
iev = sock->connect_ev;
|
|
|
|
dev = iev->done_ev;
|
|
|
|
current_task = iev->task;
|
|
|
|
|
|
|
|
if ((task == NULL || task == iev->task) && !iev->canceled) {
|
1999-05-17 22:54:17 +00:00
|
|
|
sock->connect_ev = NULL;
|
1999-01-27 04:17:05 +00:00
|
|
|
if (iev->posted) {
|
1999-05-17 22:31:26 +00:00
|
|
|
if (isc_task_purge(current_task, sock,
|
1999-01-27 04:17:05 +00:00
|
|
|
ISC_SOCKEVENT_INTCONN)
|
|
|
|
== 0) {
|
|
|
|
iev->canceled = ISC_TRUE;
|
|
|
|
iev->done_ev = NULL;
|
1999-05-17 22:54:17 +00:00
|
|
|
sock->connect_ev = iev;
|
1999-01-27 04:17:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isc_event_free((isc_event_t **)&iev);
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->result = ISC_R_CANCELED;
|
|
|
|
ISC_TASK_SEND(current_task, (isc_event_t **)&dev);
|
|
|
|
isc_task_detach(¤t_task);
|
|
|
|
}
|
1998-12-05 00:28:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to guess if we need to poke or not... XXX
|
|
|
|
*/
|
|
|
|
select_poke(sock->manager, sock->fd);
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
}
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_recvmark(isc_socket_t *sock,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg)
|
1998-12-10 16:14:05 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t *dev;
|
|
|
|
rwintev_t *iev;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_task_t *ntask = NULL;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
manager = sock->manager;
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
dev = (isc_socketevent_t *)isc_event_allocate(manager->mctx, sock,
|
|
|
|
ISC_SOCKEVENT_RECVMARK,
|
|
|
|
action, arg,
|
|
|
|
sizeof(*dev));
|
1998-12-10 16:14:05 +00:00
|
|
|
if (dev == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the queue is empty, simply return the last error we got on
|
|
|
|
* this socket as the result code, and send off the done event.
|
|
|
|
*/
|
|
|
|
if (EMPTY(sock->recv_list)) {
|
|
|
|
dev->result = sock->recv_result;
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_TASK_SEND(task, (isc_event_t **)&dev);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bad luck. The queue wasn't empty. Insert this in the proper
|
|
|
|
* place.
|
|
|
|
*/
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (rwintev_t *)isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_INTRECV,
|
|
|
|
internal_recv,
|
|
|
|
sock,
|
|
|
|
sizeof(*iev));
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
if (iev == NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&dev);
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-12-10 16:14:05 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK_INIT(iev, link);
|
1998-12-10 16:14:05 +00:00
|
|
|
iev->posted = ISC_FALSE;
|
|
|
|
|
|
|
|
dev->result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
|
|
|
iev->done_ev = dev;
|
|
|
|
iev->task = ntask;
|
|
|
|
iev->partial = ISC_FALSE; /* doesn't matter */
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->send_list, iev, link);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
XTRACE(TRACE_RECV,
|
|
|
|
("isc_socket_recvmark: posted ievent %p, dev %p, task %p\n",
|
|
|
|
iev, dev, task));
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socket_sendmark(isc_socket_t *sock,
|
|
|
|
isc_task_t *task, isc_taskaction_t action, void *arg)
|
1998-12-10 16:14:05 +00:00
|
|
|
{
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_socketevent_t *dev;
|
|
|
|
rwintev_t *iev;
|
|
|
|
isc_socketmgr_t *manager;
|
|
|
|
isc_task_t *ntask = NULL;
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
manager = sock->manager;
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
dev = (isc_socketevent_t *)isc_event_allocate(manager->mctx, sock,
|
|
|
|
ISC_SOCKEVENT_SENDMARK,
|
|
|
|
action, arg,
|
|
|
|
sizeof(*dev));
|
1998-12-10 16:14:05 +00:00
|
|
|
if (dev == NULL)
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
|
|
|
|
LOCK(&sock->lock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the queue is empty, simply return the last error we got on
|
|
|
|
* this socket as the result code, and send off the done event.
|
|
|
|
*/
|
|
|
|
if (EMPTY(sock->send_list)) {
|
|
|
|
dev->result = sock->send_result;
|
|
|
|
|
1998-12-13 23:45:21 +00:00
|
|
|
ISC_TASK_SEND(task, (isc_event_t **)&dev);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bad luck. The queue wasn't empty. Insert this in the proper
|
|
|
|
* place.
|
|
|
|
*/
|
1998-12-13 23:45:21 +00:00
|
|
|
iev = (rwintev_t *)isc_event_allocate(manager->mctx,
|
|
|
|
sock,
|
|
|
|
ISC_SOCKEVENT_INTSEND,
|
|
|
|
internal_send,
|
|
|
|
sock,
|
|
|
|
sizeof(*iev));
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
if (iev == NULL) {
|
1998-12-13 23:45:21 +00:00
|
|
|
isc_event_free((isc_event_t **)&dev);
|
1998-12-18 01:48:43 +00:00
|
|
|
UNLOCK(&sock->lock);
|
1998-12-10 16:14:05 +00:00
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
}
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LINK_INIT(iev, link);
|
1998-12-10 16:14:05 +00:00
|
|
|
iev->posted = ISC_FALSE;
|
|
|
|
|
|
|
|
dev->result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
isc_task_attach(task, &ntask);
|
|
|
|
|
|
|
|
iev->done_ev = dev;
|
|
|
|
iev->task = ntask;
|
|
|
|
iev->partial = ISC_FALSE; /* doesn't matter */
|
|
|
|
|
1999-04-29 04:49:52 +00:00
|
|
|
ISC_LIST_ENQUEUE(sock->send_list, iev, link);
|
1998-12-10 16:14:05 +00:00
|
|
|
|
|
|
|
XTRACE(TRACE_SEND,
|
|
|
|
("isc_socket_sendmark: posted ievent %p, dev %p, task %p\n",
|
|
|
|
iev, dev, task));
|
|
|
|
|
|
|
|
UNLOCK(&sock->lock);
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|