4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 /* ev_connects.c - implement asynch connect/accept for the eventlib
21 * vix 16sep96 [initial]
24 #if !defined(LINT) && !defined(CODECENTER)
25 static const char rcsid
[] = "Id: ev_connects.c,v 1.8 2006/03/09 23:57:56 marka Exp";
30 #include "port_before.h"
31 #include "fd_setsize.h"
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/ioctl.h>
39 #include <isc/eventlib.h>
40 #include <isc/assertions.h>
41 #include "eventlib_p.h"
43 #include "port_after.h"
47 #define GETXXXNAME(f, s, sa, len) ( \
48 (f((s), (&sa), (&len)) >= 0) ? 0 : \
49 (errno != EAFNOSUPPORT && errno != EOPNOTSUPP) ? -1 : ( \
50 memset(&(sa), 0, sizeof (sa)), \
51 (len) = sizeof (sa), \
52 (sa).sa_family = AF_UNIX, \
59 static void listener(evContext ctx
, void *uap
, int fd
, int evmask
);
60 static void connector(evContext ctx
, void *uap
, int fd
, int evmask
);
65 evListen(evContext opaqueCtx
, int fd
, int maxconn
,
66 evConnFunc func
, void *uap
, evConnID
*id
)
68 evContext_p
*ctx
= opaqueCtx
.opaque
;
73 new->flags
= EV_CONN_LISTEN
;
74 OKFREE(mode
= fcntl(fd
, F_GETFL
, NULL
), new); /*%< side effect: validate fd. */
76 * Remember the nonblocking status. We assume that either evSelectFD
77 * has not been done to this fd, or that if it has then the caller
78 * will evCancelConn before they evDeselectFD. If our assumptions
79 * are not met, then we might restore the old nonblocking status
82 if ((mode
& PORT_NONBLOCK
) == 0) {
83 #ifdef USE_FIONBIO_IOCTL
85 OKFREE(ioctl(fd
, FIONBIO
, (char *)&on
), new);
87 OKFREE(fcntl(fd
, F_SETFL
, mode
| PORT_NONBLOCK
), new);
89 new->flags
|= EV_CONN_BLOCK
;
91 OKFREE(listen(fd
, maxconn
), new);
92 if (evSelectFD(opaqueCtx
, fd
, EV_READ
, listener
, new, &new->file
) < 0){
99 new->flags
|= EV_CONN_SELECTED
;
103 if (ctx
->conns
!= NULL
)
104 ctx
->conns
->prev
= new;
106 new->next
= ctx
->conns
;
114 evConnect(evContext opaqueCtx
, int fd
, const void *ra
, int ralen
,
115 evConnFunc func
, void *uap
, evConnID
*id
)
117 evContext_p
*ctx
= opaqueCtx
.opaque
;
122 /* Do the select() first to get the socket into nonblocking mode. */
123 if (evSelectFD(opaqueCtx
, fd
, EV_MASK_ALL
,
124 connector
, new, &new->file
) < 0) {
131 new->flags
|= EV_CONN_SELECTED
;
132 if (connect(fd
, ra
, ralen
) < 0 &&
133 errno
!= EWOULDBLOCK
&&
135 errno
!= EINPROGRESS
) {
138 (void) evDeselectFD(opaqueCtx
, new->file
);
143 /* No error, or EWOULDBLOCK. select() tells when it's ready. */
147 if (ctx
->conns
!= NULL
)
148 ctx
->conns
->prev
= new;
150 new->next
= ctx
->conns
;
158 evCancelConn(evContext opaqueCtx
, evConnID id
) {
159 evContext_p
*ctx
= opaqueCtx
.opaque
;
160 evConn
*this = id
.opaque
;
161 evAccept
*acc
, *nxtacc
;
164 if ((this->flags
& EV_CONN_SELECTED
) != 0)
165 (void) evDeselectFD(opaqueCtx
, this->file
);
166 if ((this->flags
& EV_CONN_BLOCK
) != 0) {
167 mode
= fcntl(this->fd
, F_GETFL
, NULL
);
172 #ifdef USE_FIONBIO_IOCTL
174 OK(ioctl(this->fd
, FIONBIO
, (char *)&off
));
176 OK(fcntl(this->fd
, F_SETFL
, mode
& ~PORT_NONBLOCK
));
181 /* Unlink from ctx->conns. */
182 if (this->prev
!= NULL
)
183 this->prev
->next
= this->next
;
185 ctx
->conns
= this->next
;
186 if (this->next
!= NULL
)
187 this->next
->prev
= this->prev
;
190 * Remove `this' from the ctx->accepts list (zero or more times).
192 for (acc
= HEAD(ctx
->accepts
), nxtacc
= NULL
;
196 nxtacc
= NEXT(acc
, link
);
197 if (acc
->conn
== this) {
198 UNLINK(ctx
->accepts
, acc
, link
);
204 /* Wrap up and get out. */
209 int evHold(evContext opaqueCtx
, evConnID id
) {
210 evConn
*this = id
.opaque
;
212 if ((this->flags
& EV_CONN_LISTEN
) == 0) {
216 if ((this->flags
& EV_CONN_SELECTED
) == 0)
218 this->flags
&= ~EV_CONN_SELECTED
;
219 return (evDeselectFD(opaqueCtx
, this->file
));
222 int evUnhold(evContext opaqueCtx
, evConnID id
) {
223 evConn
*this = id
.opaque
;
226 if ((this->flags
& EV_CONN_LISTEN
) == 0) {
230 if ((this->flags
& EV_CONN_SELECTED
) != 0)
232 ret
= evSelectFD(opaqueCtx
, this->fd
, EV_READ
, listener
, this,
235 this->flags
|= EV_CONN_SELECTED
;
240 evTryAccept(evContext opaqueCtx
, evConnID id
, int *sys_errno
) {
241 evContext_p
*ctx
= opaqueCtx
.opaque
;
242 evConn
*conn
= id
.opaque
;
245 if ((conn
->flags
& EV_CONN_LISTEN
) == 0) {
251 new->ralen
= sizeof new->ra
;
252 new->fd
= accept(conn
->fd
, &new->ra
.sa
, &new->ralen
);
253 if (new->fd
> ctx
->highestFD
) {
256 new->ioErrno
= ENOTSOCK
;
259 new->lalen
= sizeof new->la
;
260 if (GETXXXNAME(getsockname
, new->fd
, new->la
.sa
, new->lalen
) < 0) {
261 new->ioErrno
= errno
;
262 (void) close(new->fd
);
267 new->ioErrno
= errno
;
268 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
) {
273 INIT_LINK(new, link
);
274 APPEND(ctx
->accepts
, new, link
);
275 *sys_errno
= new->ioErrno
;
282 listener(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
283 evContext_p
*ctx
= opaqueCtx
.opaque
;
287 struct sockaddr_in in
;
288 #ifndef NO_SOCKADDR_UN
289 struct sockaddr_un un
;
293 ISC_SOCKLEN_T lalen
= 0, ralen
;
295 REQUIRE((evmask
& EV_READ
) != 0);
297 new = accept(fd
, &ra
.sa
, &ralen
);
298 if (new > ctx
->highestFD
) {
305 if (GETXXXNAME(getsockname
, new, la
.sa
, lalen
) < 0) {
312 } else if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
)
314 (*conn
->func
)(opaqueCtx
, conn
->uap
, new, &la
.sa
, lalen
, &ra
.sa
, ralen
);
318 connector(evContext opaqueCtx
, void *uap
, int fd
, int evmask
) {
322 struct sockaddr_in in
;
323 #ifndef NO_SOCKADDR_UN
324 struct sockaddr_un un
;
327 ISC_SOCKLEN_T lalen
, ralen
;
328 #ifndef NETREAD_BROKEN
332 evConnFunc conn_func
;
334 int socket_errno
= 0;
335 ISC_SOCKLEN_T optlen
;
341 conn_uap
= conn
->uap
;
342 conn_func
= conn
->func
;
345 optlen
= sizeof socket_errno
;
347 getsockopt(conn
->fd
, SOL_SOCKET
, SO_ERROR
, (char *)&socket_errno
,
349 socket_errno
= errno
;
351 errno
= socket_errno
;
353 if (evCancelConn(opaqueCtx
, id
) < 0 ||
355 #ifdef NETREAD_BROKEN
358 read(fd
, buf
, 0) < 0 ||
360 GETXXXNAME(getsockname
, fd
, la
.sa
, lalen
) < 0 ||
361 GETXXXNAME(getpeername
, fd
, ra
.sa
, ralen
) < 0) {
364 (void) close(fd
); /*%< XXX closing caller's fd */
368 (*conn_func
)(opaqueCtx
, conn_uap
, fd
, &la
.sa
, lalen
, &ra
.sa
, ralen
);