8849 libresolv2: remove rcsid
[unleashed.git] / usr / src / lib / libresolv2 / common / isc / ev_connects.c
blob821eb8ebff8b31a650775bdf0cfa2b2a09aaab2a
1 /*
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>
29 #include <unistd.h>
31 #include <isc/eventlib.h>
32 #include <isc/assertions.h>
33 #include "eventlib_p.h"
35 #include "port_after.h"
37 /* Macros. */
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, \
45 0 \
46 ) \
49 /* Forward. */
51 static void listener(evContext ctx, void *uap, int fd, int evmask);
52 static void connector(evContext ctx, void *uap, int fd, int evmask);
54 /* Public. */
56 int
57 evListen(evContext opaqueCtx, int fd, int maxconn,
58 evConnFunc func, void *uap, evConnID *id)
60 evContext_p *ctx = opaqueCtx.opaque;
61 evConn *new;
62 int mode;
64 OKNEW(new);
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
72 * incorrectly.
74 if ((mode & PORT_NONBLOCK) == 0) {
75 #ifdef USE_FIONBIO_IOCTL
76 int on = 1;
77 OKFREE(ioctl(fd, FIONBIO, (char *)&on), new);
78 #else
79 OKFREE(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK), new);
80 #endif
81 new->flags |= EV_CONN_BLOCK;
83 OKFREE(listen(fd, maxconn), new);
84 if (evSelectFD(opaqueCtx, fd, EV_READ, listener, new, &new->file) < 0){
85 int save = errno;
87 FREE(new);
88 errno = save;
89 return (-1);
91 new->flags |= EV_CONN_SELECTED;
92 new->func = func;
93 new->uap = uap;
94 new->fd = fd;
95 if (ctx->conns != NULL)
96 ctx->conns->prev = new;
97 new->prev = NULL;
98 new->next = ctx->conns;
99 ctx->conns = new;
100 if (id)
101 id->opaque = new;
102 return (0);
106 evConnect(evContext opaqueCtx, int fd, const void *ra, int ralen,
107 evConnFunc func, void *uap, evConnID *id)
109 evContext_p *ctx = opaqueCtx.opaque;
110 evConn *new;
112 OKNEW(new);
113 new->flags = 0;
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) {
117 int save = errno;
119 FREE(new);
120 errno = save;
121 return (-1);
123 new->flags |= EV_CONN_SELECTED;
124 if (connect(fd, ra, ralen) < 0 &&
125 errno != EWOULDBLOCK &&
126 errno != EAGAIN &&
127 errno != EINPROGRESS) {
128 int save = errno;
130 (void) evDeselectFD(opaqueCtx, new->file);
131 FREE(new);
132 errno = save;
133 return (-1);
135 /* No error, or EWOULDBLOCK. select() tells when it's ready. */
136 new->func = func;
137 new->uap = uap;
138 new->fd = fd;
139 if (ctx->conns != NULL)
140 ctx->conns->prev = new;
141 new->prev = NULL;
142 new->next = ctx->conns;
143 ctx->conns = new;
144 if (id)
145 id->opaque = new;
146 return (0);
150 evCancelConn(evContext opaqueCtx, evConnID id) {
151 evContext_p *ctx = opaqueCtx.opaque;
152 evConn *this = id.opaque;
153 evAccept *acc, *nxtacc;
154 int mode;
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);
160 if (mode == -1) {
161 if (errno != EBADF)
162 return (-1);
163 } else {
164 #ifdef USE_FIONBIO_IOCTL
165 int off = 0;
166 OK(ioctl(this->fd, FIONBIO, (char *)&off));
167 #else
168 OK(fcntl(this->fd, F_SETFL, mode & ~PORT_NONBLOCK));
169 #endif
173 /* Unlink from ctx->conns. */
174 if (this->prev != NULL)
175 this->prev->next = this->next;
176 else
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;
185 acc != NULL;
186 acc = nxtacc)
188 nxtacc = NEXT(acc, link);
189 if (acc->conn == this) {
190 UNLINK(ctx->accepts, acc, link);
191 close(acc->fd);
192 FREE(acc);
196 /* Wrap up and get out. */
197 FREE(this);
198 return (0);
201 int evHold(evContext opaqueCtx, evConnID id) {
202 evConn *this = id.opaque;
204 if ((this->flags & EV_CONN_LISTEN) == 0) {
205 errno = EINVAL;
206 return (-1);
208 if ((this->flags & EV_CONN_SELECTED) == 0)
209 return (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;
216 int ret;
218 if ((this->flags & EV_CONN_LISTEN) == 0) {
219 errno = EINVAL;
220 return (-1);
222 if ((this->flags & EV_CONN_SELECTED) != 0)
223 return (0);
224 ret = evSelectFD(opaqueCtx, this->fd, EV_READ, listener, this,
225 &this->file);
226 if (ret == 0)
227 this->flags |= EV_CONN_SELECTED;
228 return (ret);
232 evTryAccept(evContext opaqueCtx, evConnID id, int *sys_errno) {
233 evContext_p *ctx = opaqueCtx.opaque;
234 evConn *conn = id.opaque;
235 evAccept *new;
237 if ((conn->flags & EV_CONN_LISTEN) == 0) {
238 errno = EINVAL;
239 return (-1);
241 OKNEW(new);
242 new->conn = conn;
243 new->ralen = sizeof new->ra;
244 new->fd = accept(conn->fd, &new->ra.sa, &new->ralen);
245 if (new->fd > ctx->highestFD) {
246 close(new->fd);
247 new->fd = -1;
248 new->ioErrno = ENOTSOCK;
250 if (new->fd >= 0) {
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);
255 new->fd = -1;
256 } else
257 new->ioErrno = 0;
258 } else {
259 new->ioErrno = errno;
260 if (errno == EAGAIN || errno == EWOULDBLOCK) {
261 FREE(new);
262 return (-1);
265 INIT_LINK(new, link);
266 APPEND(ctx->accepts, new, link);
267 *sys_errno = new->ioErrno;
268 return (0);
271 /* Private. */
273 static void
274 listener(evContext opaqueCtx, void *uap, int fd, int evmask) {
275 evContext_p *ctx = opaqueCtx.opaque;
276 evConn *conn = uap;
277 union {
278 struct sockaddr sa;
279 struct sockaddr_in in;
280 #ifndef NO_SOCKADDR_UN
281 struct sockaddr_un un;
282 #endif
283 } la, ra;
284 int new;
285 ISC_SOCKLEN_T lalen = 0, ralen;
287 REQUIRE((evmask & EV_READ) != 0);
288 ralen = sizeof ra;
289 new = accept(fd, &ra.sa, &ralen);
290 if (new > ctx->highestFD) {
291 close(new);
292 new = -1;
293 errno = ENOTSOCK;
295 if (new >= 0) {
296 lalen = sizeof la;
297 if (GETXXXNAME(getsockname, new, la.sa, lalen) < 0) {
298 int save = errno;
300 (void) close(new);
301 errno = save;
302 new = -1;
304 } else if (errno == EAGAIN || errno == EWOULDBLOCK)
305 return;
306 (*conn->func)(opaqueCtx, conn->uap, new, &la.sa, lalen, &ra.sa, ralen);
309 static void
310 connector(evContext opaqueCtx, void *uap, int fd, int evmask) {
311 evConn *conn = uap;
312 union {
313 struct sockaddr sa;
314 struct sockaddr_in in;
315 #ifndef NO_SOCKADDR_UN
316 struct sockaddr_un un;
317 #endif
318 } la, ra;
319 ISC_SOCKLEN_T lalen, ralen;
320 #ifndef NETREAD_BROKEN
321 char buf[1];
322 #endif
323 void *conn_uap;
324 evConnFunc conn_func;
325 evConnID id;
326 int socket_errno = 0;
327 ISC_SOCKLEN_T optlen;
329 UNUSED(evmask);
331 lalen = sizeof la;
332 ralen = sizeof ra;
333 conn_uap = conn->uap;
334 conn_func = conn->func;
335 id.opaque = conn;
336 #ifdef SO_ERROR
337 optlen = sizeof socket_errno;
338 if (fd < 0 &&
339 getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (char *)&socket_errno,
340 &optlen) < 0)
341 socket_errno = errno;
342 else
343 errno = socket_errno;
344 #endif
345 if (evCancelConn(opaqueCtx, id) < 0 ||
346 socket_errno ||
347 #ifdef NETREAD_BROKEN
348 0 ||
349 #else
350 read(fd, buf, 0) < 0 ||
351 #endif
352 GETXXXNAME(getsockname, fd, la.sa, lalen) < 0 ||
353 GETXXXNAME(getpeername, fd, ra.sa, ralen) < 0) {
354 int save = errno;
356 (void) close(fd); /*%< XXX closing caller's fd */
357 errno = save;
358 fd = -1;
360 (*conn_func)(opaqueCtx, conn_uap, fd, &la.sa, lalen, &ra.sa, ralen);
363 /*! \file */