1 /*
2  * Copyright 1997-2002 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 1995-1999 by Internet Software Consortium
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
14  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
16  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
17  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
18  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
19  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20  * SOFTWARE.
21  */
22 
23 #pragma ident	"%Z%%M%	%I%	%E% SMI"
24 
25 /* ev_connects.c - implement asynch connect/accept for the eventlib
26  * vix 16sep96 [initial]
27  */
28 
29 #if !defined(LINT) && !defined(CODECENTER)
30 static const char rcsid[] = "$Id: ev_connects.c,v 8.32 2001/07/03 13:26:35 marka Exp $";
31 #endif
32 
33 /* Import. */
34 
35 #include "port_before.h"
36 #include "fd_setsize.h"
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/ioctl.h>
41 
42 #include <unistd.h>
43 
44 #include <isc/eventlib.h>
45 #include <isc/assertions.h>
46 #include "eventlib_p.h"
47 
48 #include "port_after.h"
49 
50 /* Macros. */
51 
52 #define GETXXXNAME(f, s, sa, len) ( \
53 	(f((s), (&sa), (&len)) >= 0) ? 0 : \
54 		(errno != EAFNOSUPPORT && errno != EOPNOTSUPP) ? -1 : ( \
55 			memset(&(sa), 0, sizeof (sa)), \
56 			(len) = sizeof (sa), \
57 			(sa).sa_family = AF_UNIX, \
58 			0 \
59 		) \
60 	)
61 
62 /* Forward. */
63 
64 static void	listener(evContext ctx, void *uap, int fd, int evmask);
65 static void	connector(evContext ctx, void *uap, int fd, int evmask);
66 
67 /* Public. */
68 
69 int
70 evListen(evContext opaqueCtx, int fd, int maxconn,
71 	 evConnFunc func, void *uap, evConnID *id)
72 {
73 	evContext_p *ctx = opaqueCtx.opaque;
74 	evConn *new;
75 	int mode;
76 
77 	OKNEW(new);
78 	new->flags = EV_CONN_LISTEN;
79 	OK(mode = fcntl(fd, F_GETFL, NULL));	/* side effect: validate fd. */
80 	/*
81 	 * Remember the nonblocking status.  We assume that either evSelectFD
82 	 * has not been done to this fd, or that if it has then the caller
83 	 * will evCancelConn before they evDeselectFD.  If our assumptions
84 	 * are not met, then we might restore the old nonblocking status
85 	 * incorrectly.
86 	 */
87 	if ((mode & PORT_NONBLOCK) == 0) {
88 #ifdef USE_FIONBIO_IOCTL
89 		int on = 1;
90 		OK(ioctl(fd, FIONBIO, (char *)&on));
91 #else
92 		OK(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK));
93 #endif
94 		new->flags |= EV_CONN_BLOCK;
95 	}
96 	OK(listen(fd, maxconn));
97 	if (evSelectFD(opaqueCtx, fd, EV_READ, listener, new, &new->file) < 0){
98 		int save = errno;
99 
100 		FREE(new);
101 		errno = save;
102 		return (-1);
103 	}
104 	new->flags |= EV_CONN_SELECTED;
105 	new->func = func;
106 	new->uap = uap;
107 	new->fd = fd;
108 	if (ctx->conns != NULL)
109 		ctx->conns->prev = new;
110 	new->prev = NULL;
111 	new->next = ctx->conns;
112 	ctx->conns = new;
113 	if (id)
114 		id->opaque = new;
115 	return (0);
116 }
117 
118 int
119 evConnect(evContext opaqueCtx, int fd, const void *ra, int ralen,
120 	  evConnFunc func, void *uap, evConnID *id)
121 {
122 	evContext_p *ctx = opaqueCtx.opaque;
123 	evConn *new;
124 
125 	OKNEW(new);
126 	new->flags = 0;
127 	/* Do the select() first to get the socket into nonblocking mode. */
128 	if (evSelectFD(opaqueCtx, fd, EV_MASK_ALL,
129 		       connector, new, &new->file) < 0) {
130 		int save = errno;
131 
132 		FREE(new);
133 		errno = save;
134 		return (-1);
135 	}
136 	new->flags |= EV_CONN_SELECTED;
137 	if (connect(fd, ra, ralen) < 0 &&
138 	    errno != EWOULDBLOCK &&
139 	    errno != EAGAIN &&
140 	    errno != EINPROGRESS) {
141 		int save = errno;
142 
143 		(void) evDeselectFD(opaqueCtx, new->file);
144 		FREE(new);
145 		errno = save;
146 		return (-1);
147 	}
148 	/* No error, or EWOULDBLOCK.  select() tells when it's ready. */
149 	new->func = func;
150 	new->uap = uap;
151 	new->fd = fd;
152 	if (ctx->conns != NULL)
153 		ctx->conns->prev = new;
154 	new->prev = NULL;
155 	new->next = ctx->conns;
156 	ctx->conns = new;
157 	if (id)
158 		id->opaque = new;
159 	return (0);
160 }
161 
162 int
163 evCancelConn(evContext opaqueCtx, evConnID id) {
164 	evContext_p *ctx = opaqueCtx.opaque;
165 	evConn *this = id.opaque;
166 	evAccept *acc, *nxtacc;
167 	int mode;
168 
169 	if ((this->flags & EV_CONN_SELECTED) != 0)
170 		(void) evDeselectFD(opaqueCtx, this->file);
171 	if ((this->flags & EV_CONN_BLOCK) != 0) {
172 		mode = fcntl(this->fd, F_GETFL, NULL);
173 		if (mode == -1) {
174 			if (errno != EBADF)
175 				return (-1);
176 		} else {
177 #ifdef USE_FIONBIO_IOCTL
178 			int on = 1;
179 			OK(ioctl(this->fd, FIONBIO, (char *)&on));
180 #else
181 			OK(fcntl(this->fd, F_SETFL, mode | PORT_NONBLOCK));
182 #endif
183 		}
184 	}
185 
186 	/* Unlink from ctx->conns. */
187 	if (this->prev != NULL)
188 		this->prev->next = this->next;
189 	else
190 		ctx->conns = this->next;
191 	if (this->next != NULL)
192 		this->next->prev = this->prev;
193 
194 	/*
195 	 * Remove `this' from the ctx->accepts list (zero or more times).
196 	 */
197 	for (acc = HEAD(ctx->accepts), nxtacc = NULL;
198 	     acc != NULL;
199 	     acc = nxtacc)
200 	{
201 		nxtacc = NEXT(acc, link);
202 		if (acc->conn == this) {
203 			UNLINK(ctx->accepts, acc, link);
204 			close(acc->fd);
205 			FREE(acc);
206 		}
207 	}
208 
209 	/* Wrap up and get out. */
210 	FREE(this);
211 	return (0);
212 }
213 
214 int evHold(evContext opaqueCtx, evConnID id) {
215 	evConn *this = id.opaque;
216 
217 	if ((this->flags & EV_CONN_LISTEN) == 0) {
218 		errno = EINVAL;
219 		return (-1);
220 	}
221 	if ((this->flags & EV_CONN_SELECTED) == 0)
222 		return (0);
223 	this->flags &= ~EV_CONN_SELECTED;
224 	return (evDeselectFD(opaqueCtx, this->file));
225 }
226 
227 int evUnhold(evContext opaqueCtx, evConnID id) {
228 	evConn *this = id.opaque;
229 	int ret;
230 
231 	if ((this->flags & EV_CONN_LISTEN) == 0) {
232 		errno = EINVAL;
233 		return (-1);
234 	}
235 	if ((this->flags & EV_CONN_SELECTED) != 0)
236 		return (0);
237 	ret = evSelectFD(opaqueCtx, this->fd, EV_READ, listener, this,
238 			 &this->file);
239 	if (ret == 0)
240 		this->flags |= EV_CONN_SELECTED;
241 	return (ret);
242 }
243 
244 int
245 evTryAccept(evContext opaqueCtx, evConnID id, int *sys_errno) {
246 	evContext_p *ctx = opaqueCtx.opaque;
247 	evConn *conn = id.opaque;
248 	evAccept *new;
249 
250 	if ((conn->flags & EV_CONN_LISTEN) == 0) {
251 		errno = EINVAL;
252 		return (-1);
253 	}
254 	OKNEW(new);
255 	new->conn = conn;
256 	new->ralen = sizeof new->ra;
257 	new->fd = accept(conn->fd, &new->ra.sa, &new->ralen);
258 	if (new->fd > ctx->highestFD) {
259 		close(new->fd);
260 		new->fd = -1;
261 		new->ioErrno = ENOTSOCK;
262 	}
263 	if (new->fd >= 0) {
264 		new->lalen = sizeof new->la;
265 		if (GETXXXNAME(getsockname, new->fd, new->la.sa, new->lalen) < 0) {
266 			new->ioErrno = errno;
267 			(void) close(new->fd);
268 			new->fd = -1;
269 		} else
270 			new->ioErrno = 0;
271 	} else {
272 		new->ioErrno = errno;
273 		if (errno == EAGAIN || errno == EWOULDBLOCK) {
274 			FREE(new);
275 			return (-1);
276 		}
277 	}
278 	INIT_LINK(new, link);
279 	APPEND(ctx->accepts, new, link);
280 	*sys_errno = new->ioErrno;
281 	return (0);
282 }
283 
284 /* Private. */
285 
286 static void
287 listener(evContext opaqueCtx, void *uap, int fd, int evmask) {
288 	evContext_p *ctx = opaqueCtx.opaque;
289 	evConn *conn = uap;
290 	union {
291 		struct sockaddr    sa;
292 		struct sockaddr_in in;
293 #ifndef NO_SOCKADDR_UN
294 		struct sockaddr_un un;
295 #endif
296 	} la, ra;
297 	int new;
298 	ISC_SOCKLEN_T lalen = 0, ralen;
299 
300 	REQUIRE((evmask & EV_READ) != 0);
301 	ralen = sizeof ra;
302 	new = accept(fd, &ra.sa, &ralen);
303 	if (new > ctx->highestFD) {
304 		close(new);
305 		new = -1;
306 		errno = ENOTSOCK;
307 	}
308 	if (new >= 0) {
309 		lalen = sizeof la;
310 		if (GETXXXNAME(getsockname, new, la.sa, lalen) < 0) {
311 			int save = errno;
312 
313 			(void) close(new);
314 			errno = save;
315 			new = -1;
316 		}
317 	} else if (errno == EAGAIN || errno == EWOULDBLOCK)
318 		return;
319 	(*conn->func)(opaqueCtx, conn->uap, new, &la.sa, lalen, &ra.sa, ralen);
320 }
321 
322 static void
323 connector(evContext opaqueCtx, void *uap, int fd, int evmask) {
324 	evConn *conn = uap;
325 	union {
326 		struct sockaddr    sa;
327 		struct sockaddr_in in;
328 #ifndef NO_SOCKADDR_UN
329 		struct sockaddr_un un;
330 #endif
331 	} la, ra;
332 	ISC_SOCKLEN_T lalen, ralen;
333 	char buf[1];
334 	void *conn_uap;
335 	evConnFunc conn_func;
336 	evConnID id;
337 	int socket_errno = 0;
338 	ISC_SOCKLEN_T optlen;
339 
340 	UNUSED(evmask);
341 
342 	lalen = sizeof la;
343 	ralen = sizeof ra;
344 	conn_uap = conn->uap;
345 	conn_func = conn->func;
346 	id.opaque = conn;
347 #ifdef SO_ERROR
348 	optlen = sizeof socket_errno;
349 	if (fd < 0 &&
350 	    getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (char *)&socket_errno,
351 		       &optlen) < 0)
352 		socket_errno = errno;
353 	else
354 		errno = socket_errno;
355 #endif
356 	if (evCancelConn(opaqueCtx, id) < 0 ||
357 	    socket_errno ||
358 #ifdef NETREAD_BROKEN
359 	    0 ||
360 #else
361 	    read(fd, buf, 0) < 0 ||
362 #endif
363 	    GETXXXNAME(getsockname, fd, la.sa, lalen) < 0 ||
364 	    GETXXXNAME(getpeername, fd, ra.sa, ralen) < 0) {
365 		int save = errno;
366 
367 		(void) close(fd);	/* XXX closing caller's fd */
368 		errno = save;
369 		fd = -1;
370 	}
371 	(*conn_func)(opaqueCtx, conn_uap, fd, &la.sa, lalen, &ra.sa, ralen);
372 }
373