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