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 #include "port_before.h"
23 #include "fd_setsize.h"
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/ioctl.h>
31 #include <isc/eventlib.h>
32 #include <isc/assertions.h>
33 #include "eventlib_p.h"
35 #include "port_after.h"
39 #define GETXXXNAME(f, s, sa, len) ( \
40 (f((s), (&sa), (&len)) >= 0) ? 0 : \
41 (errno != EAFNOSUPPORT && errno != EOPNOTSUPP) ? -1 : ( \
42 memset(&(sa), 0, sizeof (sa)), \
43 (len) = sizeof (sa), \
44 (sa).sa_family = AF_UNIX, \
51 static void listener(evContext ctx
, void *uap
, int fd
, int evmask
);
52 static void connector(evContext ctx
, void *uap
, int fd
, int evmask
);
57 evListen(evContext opaqueCtx
, int fd
, int maxconn
,
58 evConnFunc func
, void *uap
, evConnID
*id
)
60 evContext_p
*ctx
= opaqueCtx
.opaque
;
65 new->flags
= EV_CONN_LISTEN
;
66 OKFREE(mode
= fcntl(fd
, F_GETFL
, NULL
), new); /*%< side effect: validate fd. */
68 * Remember the nonblocking status. We assume that either evSelectFD
69 * has not been done to this fd, or that if it has then the caller
70 * will evCancelConn before they evDeselectFD. If our assumptions
71 * are not met, then we might restore the old nonblocking status
74 if ((mode
& PORT_NONBLOCK
) == 0) {
75 #ifdef USE_FIONBIO_IOCTL
77 OKFREE(ioctl(fd
, FIONBIO
, (char *)&on
), new);
79 OKFREE(fcntl(fd
, F_SETFL
, mode
| PORT_NONBLOCK
), new);
81 new->flags
|= EV_CONN_BLOCK
;
83 OKFREE(listen(fd
, maxconn
), new);
84 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, listener
, new, &new->file
) < 0){
91 new->flags
|= EV_CONN_SELECTED
;
95 if (ctx
->conns
!= NULL
)
96 ctx
->conns
->prev
= new;
98 new->next
= ctx
->conns
;
106 evConnect(evContext opaqueCtx
, int fd
, const void *ra
, int ralen
,
107 evConnFunc func
, void *uap
, evConnID
*id
)
109 evContext_p
*ctx
= opaqueCtx
.opaque
;
114 /* Do the select() first to get the socket into nonblocking mode. */
115 if (evSelectFD(opaqueCtx
, fd
, EV_MASK_ALL
,
116 connector
, new, &new->file
) < 0) {
123 new->flags
|= EV_CONN_SELECTED
;
124 if (connect(fd
, ra
, ralen
) < 0 &&
125 errno
!= EWOULDBLOCK
&&
127 errno
!= EINPROGRESS
) {
130 (void) evDeselectFD(opaqueCtx
, new->file
);
135 /* No error, or EWOULDBLOCK. select() tells when it's ready. */
139 if (ctx
->conns
!= NULL
)
140 ctx
->conns
->prev
= new;
142 new->next
= ctx
->conns
;
150 evCancelConn(evContext opaqueCtx
, evConnID id
) {
151 evContext_p
*ctx
= opaqueCtx
.opaque
;
152 evConn
*this = id
.opaque
;
153 evAccept
*acc
, *nxtacc
;
156 if ((this->flags
& EV_CONN_SELECTED
) != 0)
157 (void) evDeselectFD(opaqueCtx
, this->file
);
158 if ((this->flags
& EV_CONN_BLOCK
) != 0) {
159 mode
= fcntl(this->fd
, F_GETFL
, NULL
);
164 #ifdef USE_FIONBIO_IOCTL
166 OK(ioctl(this->fd
, FIONBIO
, (char *)&off
));
168 OK(fcntl(this->fd
, F_SETFL
, mode
& ~PORT_NONBLOCK
));
173 /* Unlink from ctx->conns. */
174 if (this->prev
!= NULL
)
175 this->prev
->next
= this->next
;
177 ctx
->conns
= this->next
;
178 if (this->next
!= NULL
)
179 this->next
->prev
= this->prev
;
182 * Remove `this' from the ctx->accepts list (zero or more times).
184 for (acc
= HEAD(ctx
->accepts
), nxtacc
= NULL
;
188 nxtacc
= NEXT(acc
, link
);
189 if (acc
->conn
== this) {
190 UNLINK(ctx
->accepts
, acc
, link
);
196 /* Wrap up and get out. */
201 int evHold(evContext opaqueCtx
, evConnID id
) {
202 evConn
*this = id
.opaque
;
204 if ((this->flags
& EV_CONN_LISTEN
) == 0) {
208 if ((this->flags
& EV_CONN_SELECTED
) == 0)
210 this->flags
&= ~EV_CONN_SELECTED
;
211 return (evDeselectFD(opaqueCtx
, this->file
));
214 int evUnhold(evContext opaqueCtx
, evConnID id
) {
215 evConn
*this = id
.opaque
;
218 if ((this->flags
& EV_CONN_LISTEN
) == 0) {
222 if ((this->flags
& EV_CONN_SELECTED
) != 0)
224 ret
= evSelectFD(opaqueCtx
, this->fd
, EV_READ
, listener
, this,
227 this->flags
|= EV_CONN_SELECTED
;
232 evTryAccept(evContext opaqueCtx
, evConnID id
, int *sys_errno
) {
233 evContext_p
*ctx
= opaqueCtx
.opaque
;
234 evConn
*conn
= id
.opaque
;
237 if ((conn
->flags
& EV_CONN_LISTEN
) == 0) {
243 new->ralen
= sizeof new->ra
;
244 new->fd
= accept(conn
->fd
, &new->ra
.sa
, &new->ralen
);
245 if (new->fd
> ctx
->highestFD
) {
248 new->ioErrno
= ENOTSOCK
;
251 new->lalen
= sizeof new->la
;
252 if (GETXXXNAME(getsockname
, new->fd
, new->la
.sa
, new->lalen
) < 0) {
253 new->ioErrno
= errno
;
254 (void) close(new->fd
);
259 new->ioErrno
= errno
;
260 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
265 INIT_LINK(new, link
);
266 APPEND(ctx
->accepts
, new, link
);
267 *sys_errno
= new->ioErrno
;
274 listener(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
275 evContext_p
*ctx
= opaqueCtx
.opaque
;
279 struct sockaddr_in in
;
280 #ifndef NO_SOCKADDR_UN
281 struct sockaddr_un un
;
285 ISC_SOCKLEN_T lalen
= 0, ralen
;
287 REQUIRE((evmask
& EV_READ
) != 0);
289 new = accept(fd
, &ra
.sa
, &ralen
);
290 if (new > ctx
->highestFD
) {
297 if (GETXXXNAME(getsockname
, new, la
.sa
, lalen
) < 0) {
304 } else if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)
306 (*conn
->func
)(opaqueCtx
, conn
->uap
, new, &la
.sa
, lalen
, &ra
.sa
, ralen
);
310 connector(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
314 struct sockaddr_in in
;
315 #ifndef NO_SOCKADDR_UN
316 struct sockaddr_un un
;
319 ISC_SOCKLEN_T lalen
, ralen
;
320 #ifndef NETREAD_BROKEN
324 evConnFunc conn_func
;
326 int socket_errno
= 0;
327 ISC_SOCKLEN_T optlen
;
333 conn_uap
= conn
->uap
;
334 conn_func
= conn
->func
;
337 optlen
= sizeof socket_errno
;
339 getsockopt(conn
->fd
, SOL_SOCKET
, SO_ERROR
, (char *)&socket_errno
,
341 socket_errno
= errno
;
343 errno
= socket_errno
;
345 if (evCancelConn(opaqueCtx
, id
) < 0 ||
347 #ifdef NETREAD_BROKEN
350 read(fd
, buf
, 0) < 0 ||
352 GETXXXNAME(getsockname
, fd
, la
.sa
, lalen
) < 0 ||
353 GETXXXNAME(getpeername
, fd
, ra
.sa
, ralen
) < 0) {
356 (void) close(fd
); /*%< XXX closing caller's fd */
360 (*conn_func
)(opaqueCtx
, conn_uap
, fd
, &la
.sa
, lalen
, &ra
.sa
, ralen
);