1/*	$NetBSD: ev_connects.c,v 1.1.1.1 2009/04/12 15:33:45 christos Exp $	*/
2
3/*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium
6 *
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.
10 *
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.
18 */
19
20/* ev_connects.c - implement asynch connect/accept for the eventlib
21 * vix 16sep96 [initial]
22 */
23
24#if !defined(LINT) && !defined(CODECENTER)
25static const char rcsid[] = "Id: ev_connects.c,v 1.8 2006/03/09 23:57:56 marka Exp";
26#endif
27
28/* Import. */
29
30#include "port_before.h"
31#include "fd_setsize.h"
32
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <sys/ioctl.h>
36
37#include <unistd.h>
38
39#include <isc/eventlib.h>
40#include <isc/assertions.h>
41#include "eventlib_p.h"
42
43#include "port_after.h"
44
45/* Macros. */
46
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, \
53			0 \
54		) \
55	)
56
57/* Forward. */
58
59static void	listener(evContext ctx, void *uap, int fd, int evmask);
60static void	connector(evContext ctx, void *uap, int fd, int evmask);
61
62/* Public. */
63
64int
65evListen(evContext opaqueCtx, int fd, int maxconn,
66	 evConnFunc func, void *uap, evConnID *id)
67{
68	evContext_p *ctx = opaqueCtx.opaque;
69	evConn *new;
70	int mode;
71
72	OKNEW(new);
73	new->flags = EV_CONN_LISTEN;
74	OKFREE(mode = fcntl(fd, F_GETFL, NULL), new);	/*%< side effect: validate fd. */
75	/*
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
80	 * incorrectly.
81	 */
82	if ((mode & PORT_NONBLOCK) == 0) {
83#ifdef USE_FIONBIO_IOCTL
84		int on = 1;
85		OKFREE(ioctl(fd, FIONBIO, (char *)&on), new);
86#else
87		OKFREE(fcntl(fd, F_SETFL, mode | PORT_NONBLOCK), new);
88#endif
89		new->flags |= EV_CONN_BLOCK;
90	}
91	OKFREE(listen(fd, maxconn), new);
92	if (evSelectFD(opaqueCtx, fd, EV_READ, listener, new, &new->file) < 0){
93		int save = errno;
94
95		FREE(new);
96		errno = save;
97		return (-1);
98	}
99	new->flags |= EV_CONN_SELECTED;
100	new->func = func;
101	new->uap = uap;
102	new->fd = fd;
103	if (ctx->conns != NULL)
104		ctx->conns->prev = new;
105	new->prev = NULL;
106	new->next = ctx->conns;
107	ctx->conns = new;
108	if (id)
109		id->opaque = new;
110	return (0);
111}
112
113int
114evConnect(evContext opaqueCtx, int fd, const void *ra, int ralen,
115	  evConnFunc func, void *uap, evConnID *id)
116{
117	evContext_p *ctx = opaqueCtx.opaque;
118	evConn *new;
119
120	OKNEW(new);
121	new->flags = 0;
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) {
125		int save = errno;
126
127		FREE(new);
128		errno = save;
129		return (-1);
130	}
131	new->flags |= EV_CONN_SELECTED;
132	if (connect(fd, ra, ralen) < 0 &&
133	    errno != EWOULDBLOCK &&
134	    errno != EAGAIN &&
135	    errno != EINPROGRESS) {
136		int save = errno;
137
138		(void) evDeselectFD(opaqueCtx, new->file);
139		FREE(new);
140		errno = save;
141		return (-1);
142	}
143	/* No error, or EWOULDBLOCK.  select() tells when it's ready. */
144	new->func = func;
145	new->uap = uap;
146	new->fd = fd;
147	if (ctx->conns != NULL)
148		ctx->conns->prev = new;
149	new->prev = NULL;
150	new->next = ctx->conns;
151	ctx->conns = new;
152	if (id)
153		id->opaque = new;
154	return (0);
155}
156
157int
158evCancelConn(evContext opaqueCtx, evConnID id) {
159	evContext_p *ctx = opaqueCtx.opaque;
160	evConn *this = id.opaque;
161	evAccept *acc, *nxtacc;
162	int mode;
163
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);
168		if (mode == -1) {
169			if (errno != EBADF)
170				return (-1);
171		} else {
172#ifdef USE_FIONBIO_IOCTL
173			int off = 0;
174			OK(ioctl(this->fd, FIONBIO, (char *)&off));
175#else
176			OK(fcntl(this->fd, F_SETFL, mode & ~PORT_NONBLOCK));
177#endif
178		}
179	}
180
181	/* Unlink from ctx->conns. */
182	if (this->prev != NULL)
183		this->prev->next = this->next;
184	else
185		ctx->conns = this->next;
186	if (this->next != NULL)
187		this->next->prev = this->prev;
188
189	/*
190	 * Remove `this' from the ctx->accepts list (zero or more times).
191	 */
192	for (acc = HEAD(ctx->accepts), nxtacc = NULL;
193	     acc != NULL;
194	     acc = nxtacc)
195	{
196		nxtacc = NEXT(acc, link);
197		if (acc->conn == this) {
198			UNLINK(ctx->accepts, acc, link);
199			close(acc->fd);
200			FREE(acc);
201		}
202	}
203
204	/* Wrap up and get out. */
205	FREE(this);
206	return (0);
207}
208
209int evHold(evContext opaqueCtx, evConnID id) {
210	evConn *this = id.opaque;
211
212	if ((this->flags & EV_CONN_LISTEN) == 0) {
213		errno = EINVAL;
214		return (-1);
215	}
216	if ((this->flags & EV_CONN_SELECTED) == 0)
217		return (0);
218	this->flags &= ~EV_CONN_SELECTED;
219	return (evDeselectFD(opaqueCtx, this->file));
220}
221
222int evUnhold(evContext opaqueCtx, evConnID id) {
223	evConn *this = id.opaque;
224	int ret;
225
226	if ((this->flags & EV_CONN_LISTEN) == 0) {
227		errno = EINVAL;
228		return (-1);
229	}
230	if ((this->flags & EV_CONN_SELECTED) != 0)
231		return (0);
232	ret = evSelectFD(opaqueCtx, this->fd, EV_READ, listener, this,
233			 &this->file);
234	if (ret == 0)
235		this->flags |= EV_CONN_SELECTED;
236	return (ret);
237}
238
239int
240evTryAccept(evContext opaqueCtx, evConnID id, int *sys_errno) {
241	evContext_p *ctx = opaqueCtx.opaque;
242	evConn *conn = id.opaque;
243	evAccept *new;
244
245	if ((conn->flags & EV_CONN_LISTEN) == 0) {
246		errno = EINVAL;
247		return (-1);
248	}
249	OKNEW(new);
250	new->conn = conn;
251	new->ralen = sizeof new->ra;
252	new->fd = accept(conn->fd, &new->ra.sa, &new->ralen);
253	if (new->fd > ctx->highestFD) {
254		close(new->fd);
255		new->fd = -1;
256		new->ioErrno = ENOTSOCK;
257	}
258	if (new->fd >= 0) {
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);
263			new->fd = -1;
264		} else
265			new->ioErrno = 0;
266	} else {
267		new->ioErrno = errno;
268		if (errno == EAGAIN || errno == EWOULDBLOCK) {
269			FREE(new);
270			return (-1);
271		}
272	}
273	INIT_LINK(new, link);
274	APPEND(ctx->accepts, new, link);
275	*sys_errno = new->ioErrno;
276	return (0);
277}
278
279/* Private. */
280
281static void
282listener(evContext opaqueCtx, void *uap, int fd, int evmask) {
283	evContext_p *ctx = opaqueCtx.opaque;
284	evConn *conn = uap;
285	union {
286		struct sockaddr    sa;
287		struct sockaddr_in in;
288#ifndef NO_SOCKADDR_UN
289		struct sockaddr_un un;
290#endif
291	} la, ra;
292	int new;
293	ISC_SOCKLEN_T lalen = 0, ralen;
294
295	REQUIRE((evmask & EV_READ) != 0);
296	ralen = sizeof ra;
297	new = accept(fd, &ra.sa, &ralen);
298	if (new > ctx->highestFD) {
299		close(new);
300		new = -1;
301		errno = ENOTSOCK;
302	}
303	if (new >= 0) {
304		lalen = sizeof la;
305		if (GETXXXNAME(getsockname, new, la.sa, lalen) < 0) {
306			int save = errno;
307
308			(void) close(new);
309			errno = save;
310			new = -1;
311		}
312	} else if (errno == EAGAIN || errno == EWOULDBLOCK)
313		return;
314	(*conn->func)(opaqueCtx, conn->uap, new, &la.sa, lalen, &ra.sa, ralen);
315}
316
317static void
318connector(evContext opaqueCtx, void *uap, int fd, int evmask) {
319	evConn *conn = uap;
320	union {
321		struct sockaddr    sa;
322		struct sockaddr_in in;
323#ifndef NO_SOCKADDR_UN
324		struct sockaddr_un un;
325#endif
326	} la, ra;
327	ISC_SOCKLEN_T lalen, ralen;
328#ifndef NETREAD_BROKEN
329	char buf[1];
330#endif
331	void *conn_uap;
332	evConnFunc conn_func;
333	evConnID id;
334	int socket_errno = 0;
335	ISC_SOCKLEN_T optlen;
336
337	UNUSED(evmask);
338
339	lalen = sizeof la;
340	ralen = sizeof ra;
341	conn_uap = conn->uap;
342	conn_func = conn->func;
343	id.opaque = conn;
344#ifdef SO_ERROR
345	optlen = sizeof socket_errno;
346	if (fd < 0 &&
347	    getsockopt(conn->fd, SOL_SOCKET, SO_ERROR, (char *)&socket_errno,
348		       &optlen) < 0)
349		socket_errno = errno;
350	else
351		errno = socket_errno;
352#endif
353	if (evCancelConn(opaqueCtx, id) < 0 ||
354	    socket_errno ||
355#ifdef NETREAD_BROKEN
356	    0 ||
357#else
358	    read(fd, buf, 0) < 0 ||
359#endif
360	    GETXXXNAME(getsockname, fd, la.sa, lalen) < 0 ||
361	    GETXXXNAME(getpeername, fd, ra.sa, ralen) < 0) {
362		int save = errno;
363
364		(void) close(fd);	/*%< XXX closing caller's fd */
365		errno = save;
366		fd = -1;
367	}
368	(*conn_func)(opaqueCtx, conn_uap, fd, &la.sa, lalen, &ra.sa, ralen);
369}
370
371/*! \file */
372