2 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (c) 1995-1999 by Internet Software Consortium
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 /* ev_connects.c - implement asynch connect/accept for the eventlib
19 * vix 16sep96 [initial]
22 #if !defined(LINT) && !defined(CODECENTER)
23 static const char rcsid[] = "$Id: ev_connects.c,v 1.4.2.1 2004/03/09 09:17:35 marka Exp $";
28 #include "port_before.h"
29 #include "fd_setsize.h"
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/ioctl.h>
37 #include <isc/eventlib.h>
38 #include <isc/assertions.h>
39 #include "eventlib_p.h"
41 #include "port_after.h"
45 #define GETXXXNAME(f, s, sa, len) ( \
46 (f((s), (&sa), (&len)) >= 0) ? 0 : \
47 (errno != EAFNOSUPPORT && errno != EOPNOTSUPP) ? -1 : ( \
48 memset(&(sa), 0, sizeof (sa)), \
49 (len) = sizeof (sa), \
50 (sa).sa_family = AF_UNIX, \
57 static void listener(evContext ctx, void *uap, int fd, int evmask);
58 static void connector(evContext ctx, void *uap, int fd, int evmask);
63 evListen(evContext opaqueCtx, int fd, int maxconn,
64 evConnFunc func, void *uap, evConnID *id)
66 evContext_p *ctx = opaqueCtx.opaque;
71 new->flags = EV_CONN_LISTEN;
72 OK(mode = fcntl(fd, F_GETFL, NULL)); /* side effect: validate fd. */
74 * Remember the nonblocking status. We assume that either evSelectFD
75 * has not been done to this fd, or that if it has then the caller
76 * will evCancelConn before they evDeselectFD. If our assumptions
77 * are not met, then we might restore the old nonblocking status
80 if ((mode & PORT_NONBLOCK) == 0) {
81 #ifdef USE_FIONBIO_IOCTL
83 OK(ioctl(fd, FIONBIO, (char *)&on));
85 OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
87 new->flags |= EV_CONN_BLOCK;
89 OK(listen(fd, maxconn));
90 if (evSelectFD(opaqueCtx, fd, EV_READ, listener, new, &new->file) < 0){
97 new->flags |= EV_CONN_SELECTED;
101 if (ctx->conns != NULL)
102 ctx->conns->prev = new;
104 new->next = ctx->conns;
112 evConnect(evContext opaqueCtx, int fd, const void *ra, int ralen,
113 evConnFunc func, void *uap, evConnID *id)
115 evContext_p *ctx = opaqueCtx.opaque;
120 /* Do the select() first to get the socket into nonblocking mode. */
121 if (evSelectFD(opaqueCtx, fd, EV_MASK_ALL,
122 connector, new, &new->file) < 0) {
129 new->flags |= EV_CONN_SELECTED;
130 if (connect(fd, ra, ralen) < 0 &&
131 errno != EWOULDBLOCK &&
133 errno != EINPROGRESS) {
136 (void) evDeselectFD(opaqueCtx, new->file);
141 /* No error, or EWOULDBLOCK. select() tells when it's ready. */
145 if (ctx->conns != NULL)
146 ctx->conns->prev = new;
148 new->next = ctx->conns;
156 evCancelConn(evContext opaqueCtx, evConnID id) {
157 evContext_p *ctx = opaqueCtx.opaque;
158 evConn *this = id.opaque;
159 evAccept *acc, *nxtacc;
162 if ((this->flags & EV_CONN_SELECTED) != 0)
163 (void) evDeselectFD(opaqueCtx, this->file);
164 if ((this->flags & EV_CONN_BLOCK) != 0) {
165 mode = fcntl(this->fd, F_GETFL, NULL);
170 #ifdef USE_FIONBIO_IOCTL
172 OK(ioctl(this->fd, FIONBIO, (char *)&on));
174 OK(fcntl(this->fd, F_SETFL, mode | PORT_NONBLOCK));
179 /* Unlink from ctx->conns. */
180 if (this->prev != NULL)
181 this->prev->next = this->next;
183 ctx->conns = this->next;
184 if (this->next != NULL)
185 this->next->prev = this->prev;
188 * Remove `this' from the ctx->accepts list (zero or more times).
190 for (acc = HEAD(ctx->accepts), nxtacc = NULL;
194 nxtacc = NEXT(acc, link);
195 if (acc->conn == this) {
196 UNLINK(ctx->accepts, acc, link);
202 /* Wrap up and get out. */
207 int evHold(evContext opaqueCtx, evConnID id) {
208 evConn *this = id.opaque;
210 if ((this->flags & EV_CONN_LISTEN) == 0) {
214 if ((this->flags & EV_CONN_SELECTED) == 0)
216 this->flags &= ~EV_CONN_SELECTED;
217 return (evDeselectFD(opaqueCtx, this->file));
220 int evUnhold(evContext opaqueCtx, evConnID id) {
221 evConn *this = id.opaque;
224 if ((this->flags & EV_CONN_LISTEN) == 0) {
228 if ((this->flags & EV_CONN_SELECTED) != 0)
230 ret = evSelectFD(opaqueCtx, this->fd, EV_READ, listener, this,
233 this->flags |= EV_CONN_SELECTED;
238 evTryAccept(evContext opaqueCtx, evConnID id, int *sys_errno) {
239 evContext_p *ctx = opaqueCtx.opaque;
240 evConn *conn = id.opaque;
243 if ((conn->flags & EV_CONN_LISTEN) == 0) {
249 new->ralen = sizeof new->ra;
250 new->fd = accept(conn->fd, &new->ra.sa, &new->ralen);
251 if (new->fd > ctx->highestFD) {
254 new->ioErrno = ENOTSOCK;
257 new->lalen = sizeof new->la;
258 if (GETXXXNAME(getsockname, new->fd, new->la.sa, new->lalen) < 0) {
259 new->ioErrno = errno;
260 (void) close(new->fd);
265 new->ioErrno = errno;
266 if (errno == EAGAIN || errno == EWOULDBLOCK) {
271 INIT_LINK(new, link);
272 APPEND(ctx->accepts, new, link);
273 *sys_errno = new->ioErrno;
280 listener(evContext opaqueCtx, void *uap, int fd, int evmask) {
281 evContext_p *ctx = opaqueCtx.opaque;
285 struct sockaddr_in in;
286 #ifndef NO_SOCKADDR_UN
287 struct sockaddr_un un;
291 ISC_SOCKLEN_T lalen = 0, ralen;
293 REQUIRE((evmask & EV_READ) != 0);
295 new = accept(fd, &ra.sa, &ralen);
296 if (new > ctx->highestFD) {
303 if (GETXXXNAME(getsockname, new, la.sa, lalen) < 0) {
310 } else if (errno == EAGAIN || errno == EWOULDBLOCK)
312 (*conn->func)(opaqueCtx, conn->uap, new, &la.sa, lalen, &ra.sa, ralen);
316 connector(evContext opaqueCtx, void *uap, int fd, int evmask) {
320 struct sockaddr_in in;
321 #ifndef NO_SOCKADDR_UN
322 struct sockaddr_un un;
325 ISC_SOCKLEN_T lalen, ralen;
326 #ifndef NETREAD_BROKEN
330 evConnFunc conn_func;
332 int socket_errno = 0;
333 ISC_SOCKLEN_T optlen;
339 conn_uap = conn->uap;
340 conn_func = conn->func;
343 optlen = sizeof socket_errno;
345 getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (char *)&socket_errno,
347 socket_errno = errno;
349 errno = socket_errno;
351 if (evCancelConn(opaqueCtx, id) < 0 ||
353 #ifdef NETREAD_BROKEN
356 read(fd, buf, 0) < 0 ||
358 GETXXXNAME(getsockname, fd, la.sa, lalen) < 0 ||
359 GETXXXNAME(getpeername, fd, ra.sa, ralen) < 0) {
362 (void) close(fd); /* XXX closing caller's fd */
366 (*conn_func)(opaqueCtx, conn_uap, fd, &la.sa, lalen, &ra.sa, ralen);