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