Home | History | Annotate | Line # | Download | only in rpc
svc_vc.c revision 1.12.10.1
      1 /*	$NetBSD: svc_vc.c,v 1.12.10.1 2006/10/24 08:30:15 ghen Exp $	*/
      2 
      3 /*
      4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      5  * unrestricted use provided that this legend is included on all tape
      6  * media and as a part of the software program in whole or part.  Users
      7  * may copy or modify Sun RPC without charge, but are not authorized
      8  * to license or distribute it to anyone else except as part of a product or
      9  * program developed by the user.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #if defined(LIBC_SCCS) && !defined(lint)
     34 #if 0
     35 static char *sccsid = "@(#)svc_tcp.c 1.21 87/08/11 Copyr 1984 Sun Micro";
     36 static char *sccsid = "@(#)svc_tcp.c	2.2 88/08/01 4.0 RPCSRC";
     37 #else
     38 __RCSID("$NetBSD: svc_vc.c,v 1.12.10.1 2006/10/24 08:30:15 ghen Exp $");
     39 #endif
     40 #endif
     41 
     42 /*
     43  * svc_vc.c, Server side for Connection Oriented based RPC.
     44  *
     45  * Actually implements two flavors of transporter -
     46  * a tcp rendezvouser (a listner and connection establisher)
     47  * and a record/tcp stream.
     48  */
     49 
     50 #include "namespace.h"
     51 #include "reentrant.h"
     52 #include <sys/types.h>
     53 #include <sys/param.h>
     54 #include <sys/poll.h>
     55 #include <sys/socket.h>
     56 #include <sys/un.h>
     57 #include <sys/time.h>
     58 #include <netinet/in.h>
     59 #include <netinet/tcp.h>
     60 
     61 #include <assert.h>
     62 #include <err.h>
     63 #include <errno.h>
     64 #include <fcntl.h>
     65 #include <stdio.h>
     66 #include <stdlib.h>
     67 #include <string.h>
     68 #include <unistd.h>
     69 
     70 #include <rpc/rpc.h>
     71 
     72 #include "rpc_internal.h"
     73 
     74 #ifdef __weak_alias
     75 __weak_alias(svc_fd_create,_svc_fd_create)
     76 __weak_alias(svc_vc_create,_svc_vc_create)
     77 #endif
     78 
     79 #ifdef _REENTRANT
     80 extern rwlock_t svc_fd_lock;
     81 #endif
     82 
     83 static SVCXPRT *makefd_xprt __P((int, u_int, u_int));
     84 static bool_t rendezvous_request __P((SVCXPRT *, struct rpc_msg *));
     85 static enum xprt_stat rendezvous_stat __P((SVCXPRT *));
     86 static void svc_vc_destroy __P((SVCXPRT *));
     87 static void __svc_vc_dodestroy __P((SVCXPRT *));
     88 static int read_vc __P((caddr_t, caddr_t, int));
     89 static int write_vc __P((caddr_t, caddr_t, int));
     90 static enum xprt_stat svc_vc_stat __P((SVCXPRT *));
     91 static bool_t svc_vc_recv __P((SVCXPRT *, struct rpc_msg *));
     92 static bool_t svc_vc_getargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     93 static bool_t svc_vc_freeargs __P((SVCXPRT *, xdrproc_t, caddr_t));
     94 static bool_t svc_vc_reply __P((SVCXPRT *, struct rpc_msg *));
     95 static void svc_vc_rendezvous_ops __P((SVCXPRT *));
     96 static void svc_vc_ops __P((SVCXPRT *));
     97 static bool_t svc_vc_control __P((SVCXPRT *xprt, const u_int rq, void *in));
     98 static bool_t svc_vc_rendezvous_control __P((SVCXPRT *xprt, const u_int rq,
     99 					     void *in));
    100 
    101 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
    102 	u_int sendsize;
    103 	u_int recvsize;
    104 	int maxrec;
    105 };
    106 
    107 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
    108 	enum xprt_stat strm_stat;
    109 	u_int32_t x_id;
    110 	XDR xdrs;
    111 	char verf_body[MAX_AUTH_BYTES];
    112 	u_int sendsize;
    113 	u_int recvsize;
    114 	int maxrec;
    115 	bool_t nonblock;
    116 	struct timeval last_recv_time;
    117 };
    118 
    119 /*
    120  * Usage:
    121  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
    122  *
    123  * Creates, registers, and returns a (rpc) tcp based transporter.
    124  * Once *xprt is initialized, it is registered as a transporter
    125  * see (svc.h, xprt_register).  This routine returns
    126  * a NULL if a problem occurred.
    127  *
    128  * The filedescriptor passed in is expected to refer to a bound, but
    129  * not yet connected socket.
    130  *
    131  * Since streams do buffered io similar to stdio, the caller can specify
    132  * how big the send and receive buffers are via the second and third parms;
    133  * 0 => use the system default.
    134  */
    135 SVCXPRT *
    136 svc_vc_create(fd, sendsize, recvsize)
    137 	int fd;
    138 	u_int sendsize;
    139 	u_int recvsize;
    140 {
    141 	SVCXPRT *xprt;
    142 	struct cf_rendezvous *r = NULL;
    143 	struct __rpc_sockinfo si;
    144 	struct sockaddr_storage sslocal;
    145 	socklen_t slen;
    146 	int one = 1;
    147 
    148 	r = mem_alloc(sizeof(*r));
    149 	if (r == NULL) {
    150 		warnx("svc_vc_create: out of memory");
    151 		goto cleanup_svc_vc_create;
    152 	}
    153 	if (!__rpc_fd2sockinfo(fd, &si))
    154 		return NULL;
    155 	r->sendsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)sendsize);
    156 	r->recvsize = __rpc_get_t_size(si.si_af, si.si_proto, (int)recvsize);
    157 	r->maxrec = __svc_maxrec;
    158 	xprt = mem_alloc(sizeof(SVCXPRT));
    159 	if (xprt == NULL) {
    160 		warnx("svc_vc_create: out of memory");
    161 		goto cleanup_svc_vc_create;
    162 	}
    163 	xprt->xp_tp = NULL;
    164 	xprt->xp_p1 = (caddr_t)(void *)r;
    165 	xprt->xp_p2 = NULL;
    166 	xprt->xp_p3 = NULL;
    167 	xprt->xp_verf = _null_auth;
    168 	svc_vc_rendezvous_ops(xprt);
    169 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
    170 	xprt->xp_fd = fd;
    171 
    172 	slen = sizeof (struct sockaddr_storage);
    173 	if (getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
    174 		warnx("svc_vc_create: could not retrieve local addr");
    175 		goto cleanup_svc_vc_create;
    176 	}
    177 
    178 	/*
    179 	 * We want to be able to check credentials on local sockets.
    180 	 */
    181 	if (sslocal.ss_family == AF_LOCAL)
    182 		if (setsockopt(fd, 0, LOCAL_CREDS, &one, sizeof one) < 0)
    183 			goto cleanup_svc_vc_create;
    184 
    185 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
    186 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
    187 	if (xprt->xp_ltaddr.buf == NULL) {
    188 		warnx("svc_vc_create: no mem for local addr");
    189 		goto cleanup_svc_vc_create;
    190 	}
    191 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
    192 
    193 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
    194 	xprt_register(xprt);
    195 	return (xprt);
    196 cleanup_svc_vc_create:
    197 	if (r != NULL)
    198 		mem_free(r, sizeof(*r));
    199 	return (NULL);
    200 }
    201 
    202 /*
    203  * Like svtcp_create(), except the routine takes any *open* UNIX file
    204  * descriptor as its first input.
    205  */
    206 SVCXPRT *
    207 svc_fd_create(fd, sendsize, recvsize)
    208 	int fd;
    209 	u_int sendsize;
    210 	u_int recvsize;
    211 {
    212 	struct sockaddr_storage ss;
    213 	socklen_t slen;
    214 	SVCXPRT *ret;
    215 
    216 	_DIAGASSERT(fd != -1);
    217 
    218 	ret = makefd_xprt(fd, sendsize, recvsize);
    219 	if (ret == NULL)
    220 		return NULL;
    221 
    222 	slen = sizeof (struct sockaddr_storage);
    223 	if (getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    224 		warnx("svc_fd_create: could not retrieve local addr");
    225 		goto freedata;
    226 	}
    227 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
    228 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
    229 	if (ret->xp_ltaddr.buf == NULL) {
    230 		warnx("svc_fd_create: no mem for local addr");
    231 		goto freedata;
    232 	}
    233 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
    234 
    235 	slen = sizeof (struct sockaddr_storage);
    236 	if (getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
    237 		warnx("svc_fd_create: could not retrieve remote addr");
    238 		goto freedata;
    239 	}
    240 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
    241 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
    242 	if (ret->xp_rtaddr.buf == NULL) {
    243 		warnx("svc_fd_create: no mem for local addr");
    244 		goto freedata;
    245 	}
    246 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
    247 #ifdef PORTMAP
    248 	if (ss.ss_family == AF_INET) {
    249 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
    250 		ret->xp_addrlen = sizeof (struct sockaddr_in);
    251 	}
    252 #endif
    253 
    254 	return ret;
    255 
    256 freedata:
    257 	if (ret->xp_ltaddr.buf != NULL)
    258 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
    259 
    260 	return NULL;
    261 }
    262 
    263 static SVCXPRT *
    264 makefd_xprt(fd, sendsize, recvsize)
    265 	int fd;
    266 	u_int sendsize;
    267 	u_int recvsize;
    268 {
    269 	SVCXPRT *xprt;
    270 	struct cf_conn *cd;
    271 	const char *netid;
    272 	struct __rpc_sockinfo si;
    273 
    274 	_DIAGASSERT(fd != -1);
    275 
    276 	xprt = mem_alloc(sizeof(SVCXPRT));
    277 	if (xprt == NULL) {
    278 		warnx("svc_vc: makefd_xprt: out of memory");
    279 		goto done;
    280 	}
    281 	memset(xprt, 0, sizeof *xprt);
    282 	cd = mem_alloc(sizeof(struct cf_conn));
    283 	if (cd == NULL) {
    284 		warnx("svc_tcp: makefd_xprt: out of memory");
    285 		mem_free(xprt, sizeof(SVCXPRT));
    286 		xprt = NULL;
    287 		goto done;
    288 	}
    289 	cd->strm_stat = XPRT_IDLE;
    290 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
    291 	    (caddr_t)(void *)xprt, read_vc, write_vc);
    292 	xprt->xp_p1 = (caddr_t)(void *)cd;
    293 	xprt->xp_verf.oa_base = cd->verf_body;
    294 	svc_vc_ops(xprt);  /* truely deals with calls */
    295 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
    296 	xprt->xp_fd = fd;
    297 	if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
    298 		xprt->xp_netid = strdup(netid);
    299 
    300 	xprt_register(xprt);
    301 done:
    302 	return (xprt);
    303 }
    304 
    305 /*ARGSUSED*/
    306 static bool_t
    307 rendezvous_request(xprt, msg)
    308 	SVCXPRT *xprt;
    309 	struct rpc_msg *msg;
    310 {
    311 	int sock, flags;
    312 	struct cf_rendezvous *r;
    313 	struct cf_conn *cd;
    314 	struct sockaddr_storage addr;
    315 	socklen_t len;
    316 	struct __rpc_sockinfo si;
    317 	SVCXPRT *newxprt;
    318 	fd_set cleanfds;
    319 
    320 	_DIAGASSERT(xprt != NULL);
    321 	_DIAGASSERT(msg != NULL);
    322 
    323 	r = (struct cf_rendezvous *)xprt->xp_p1;
    324 again:
    325 	len = sizeof addr;
    326 	if ((sock = accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
    327 	    &len)) < 0) {
    328 		if (errno == EINTR)
    329 			goto again;
    330 		/*
    331 		 * Clean out the most idle file descriptor when we're
    332 		 * running out.
    333 		 */
    334 		if (errno == EMFILE || errno == ENFILE) {
    335 			cleanfds = svc_fdset;
    336 			if (__svc_clean_idle(&cleanfds, 0, FALSE))
    337 				goto again;
    338 		}
    339 		return (FALSE);
    340 	}
    341 	/*
    342 	 * make a new transporter (re-uses xprt)
    343 	 */
    344 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
    345 	newxprt->xp_rtaddr.buf = mem_alloc(len);
    346 	if (newxprt->xp_rtaddr.buf == NULL)
    347 		return (FALSE);
    348 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
    349 	newxprt->xp_rtaddr.len = len;
    350 #ifdef PORTMAP
    351 	if (addr.ss_family == AF_INET) {
    352 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
    353 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
    354 	}
    355 #endif
    356 	if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
    357 		len = 1;
    358 		/* XXX fvdl - is this useful? */
    359 		setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
    360 	}
    361 
    362 	cd = (struct cf_conn *)newxprt->xp_p1;
    363 
    364 	cd->recvsize = r->recvsize;
    365 	cd->sendsize = r->sendsize;
    366 	cd->maxrec = r->maxrec;
    367 
    368 	if (cd->maxrec != 0) {
    369 		flags = fcntl(sock, F_GETFL, 0);
    370 		if (flags  == -1)
    371 			return (FALSE);
    372 		if (fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
    373 			return (FALSE);
    374 		if (cd->recvsize > cd->maxrec)
    375 			cd->recvsize = cd->maxrec;
    376 		cd->nonblock = TRUE;
    377 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
    378 	} else
    379 		cd->nonblock = FALSE;
    380 
    381 	gettimeofday(&cd->last_recv_time, NULL);
    382 
    383 	return (FALSE); /* there is never an rpc msg to be processed */
    384 }
    385 
    386 /*ARGSUSED*/
    387 static enum xprt_stat
    388 rendezvous_stat(xprt)
    389 	SVCXPRT *xprt;
    390 {
    391 
    392 	return (XPRT_IDLE);
    393 }
    394 
    395 static void
    396 svc_vc_destroy(xprt)
    397 	SVCXPRT *xprt;
    398 {
    399 	_DIAGASSERT(xprt != NULL);
    400 
    401 	xprt_unregister(xprt);
    402 	__svc_vc_dodestroy(xprt);
    403 }
    404 
    405 static void
    406 __svc_vc_dodestroy(xprt)
    407 	SVCXPRT *xprt;
    408 {
    409 	struct cf_conn *cd;
    410 	struct cf_rendezvous *r;
    411 
    412 	cd = (struct cf_conn *)xprt->xp_p1;
    413 
    414 	if (xprt->xp_fd != RPC_ANYFD)
    415 		(void)close(xprt->xp_fd);
    416 	if (xprt->xp_port != 0) {
    417 		/* a rendezvouser socket */
    418 		r = (struct cf_rendezvous *)xprt->xp_p1;
    419 		mem_free(r, sizeof (struct cf_rendezvous));
    420 		xprt->xp_port = 0;
    421 	} else {
    422 		/* an actual connection socket */
    423 		XDR_DESTROY(&(cd->xdrs));
    424 		mem_free(cd, sizeof(struct cf_conn));
    425 	}
    426 	if (xprt->xp_rtaddr.buf)
    427 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
    428 	if (xprt->xp_ltaddr.buf)
    429 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
    430 	if (xprt->xp_tp)
    431 		free(xprt->xp_tp);
    432 	if (xprt->xp_netid)
    433 		free(xprt->xp_netid);
    434 	mem_free(xprt, sizeof(SVCXPRT));
    435 }
    436 
    437 /*ARGSUSED*/
    438 static bool_t
    439 svc_vc_control(xprt, rq, in)
    440 	SVCXPRT *xprt;
    441 	const u_int rq;
    442 	void *in;
    443 {
    444 	return (FALSE);
    445 }
    446 
    447 /*ARGSUSED*/
    448 static bool_t
    449 svc_vc_rendezvous_control(xprt, rq, in)
    450 	SVCXPRT *xprt;
    451 	const u_int rq;
    452 	void *in;
    453 {
    454 	struct cf_rendezvous *cfp;
    455 
    456 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
    457 	if (cfp == NULL)
    458 		return (FALSE);
    459 	switch (rq) {
    460 		case SVCGET_CONNMAXREC:
    461 			*(int *)in = cfp->maxrec;
    462 			break;
    463 		case SVCSET_CONNMAXREC:
    464 			cfp->maxrec = *(int *)in;
    465 			break;
    466 		default:
    467 			return (FALSE);
    468 	}
    469 	return (TRUE);
    470 }
    471 
    472 /*
    473  * reads data from the tcp connection.
    474  * any error is fatal and the connection is closed.
    475  * (And a read of zero bytes is a half closed stream => error.)
    476  * All read operations timeout after 35 seconds.  A timeout is
    477  * fatal for the connection.
    478  */
    479 static int
    480 read_vc(xprtp, buf, len)
    481 	caddr_t xprtp;
    482 	caddr_t buf;
    483 	int len;
    484 {
    485 	SVCXPRT *xprt;
    486 	int sock;
    487 	int milliseconds = 35 * 1000;
    488 	struct pollfd pollfd;
    489 	struct sockaddr *sa;
    490 	struct msghdr msg;
    491 	struct cmsghdr *cmp;
    492 	void *crmsg = NULL;
    493 	struct sockcred *sc;
    494 	socklen_t crmsgsize;
    495 	struct cf_conn *cfp;
    496 
    497 	xprt = (SVCXPRT *)(void *)xprtp;
    498 	_DIAGASSERT(xprt != NULL);
    499 
    500 	sock = xprt->xp_fd;
    501 
    502 	sa = (struct sockaddr *)xprt->xp_rtaddr.buf;
    503 	if (sa->sa_family == AF_LOCAL && xprt->xp_p2 == NULL) {
    504 		memset(&msg, 0, sizeof msg);
    505 		crmsgsize = CMSG_SPACE(SOCKCREDSIZE(NGROUPS));
    506 		crmsg = malloc(crmsgsize);
    507 		if (crmsg == NULL)
    508 			goto fatal_err;
    509 		memset(crmsg, 0, crmsgsize);
    510 
    511 		msg.msg_control = crmsg;
    512 		msg.msg_controllen = crmsgsize;
    513 
    514 		if (recvmsg(sock, &msg, 0) < 0)
    515 			goto fatal_err;
    516 
    517 		if (msg.msg_controllen == 0 ||
    518 		    (msg.msg_flags & MSG_CTRUNC) != 0)
    519 			goto fatal_err;
    520 
    521 		cmp = CMSG_FIRSTHDR(&msg);
    522 		if (cmp->cmsg_level != SOL_SOCKET ||
    523 		    cmp->cmsg_type != SCM_CREDS)
    524 			goto fatal_err;
    525 
    526 		sc = (struct sockcred *)(void *)CMSG_DATA(cmp);
    527 
    528 		xprt->xp_p2 = mem_alloc(SOCKCREDSIZE(sc->sc_ngroups));
    529 		if (xprt->xp_p2 == NULL)
    530 			goto fatal_err;
    531 
    532 		memcpy(xprt->xp_p2, sc, SOCKCREDSIZE(sc->sc_ngroups));
    533 		free(crmsg);
    534 		crmsg = NULL;
    535 	}
    536 
    537 	cfp = (struct cf_conn *)xprt->xp_p1;
    538 
    539 	if (cfp->nonblock) {
    540 		len = read(sock, buf, (size_t)len);
    541 		if (len < 0) {
    542 			if (errno == EAGAIN)
    543 				len = 0;
    544 			else
    545 				goto fatal_err;
    546 		}
    547 		if (len != 0)
    548 			gettimeofday(&cfp->last_recv_time, NULL);
    549 		return len;
    550 	}
    551 
    552 	do {
    553 		pollfd.fd = sock;
    554 		pollfd.events = POLLIN;
    555 		switch (poll(&pollfd, 1, milliseconds)) {
    556 		case -1:
    557 			if (errno == EINTR) {
    558 				continue;
    559 			}
    560 			/*FALLTHROUGH*/
    561 		case 0:
    562 			goto fatal_err;
    563 
    564 		default:
    565 			break;
    566 		}
    567 	} while ((pollfd.revents & POLLIN) == 0);
    568 
    569 	if ((len = read(sock, buf, (size_t)len)) > 0) {
    570 		gettimeofday(&cfp->last_recv_time, NULL);
    571 		return (len);
    572 	}
    573 
    574 fatal_err:
    575 	if (crmsg != NULL)
    576 		free(crmsg);
    577 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
    578 	return (-1);
    579 }
    580 
    581 /*
    582  * writes data to the tcp connection.
    583  * Any error is fatal and the connection is closed.
    584  */
    585 static int
    586 write_vc(xprtp, buf, len)
    587 	caddr_t xprtp;
    588 	caddr_t buf;
    589 	int len;
    590 {
    591 	SVCXPRT *xprt;
    592 	int i, cnt;
    593 	struct cf_conn *cd;
    594 	struct timeval tv0, tv1;
    595 
    596 	xprt = (SVCXPRT *)(void *)xprtp;
    597 	_DIAGASSERT(xprt != NULL);
    598 
    599 	cd = (struct cf_conn *)xprt->xp_p1;
    600 
    601 	if (cd->nonblock)
    602 		gettimeofday(&tv0, NULL);
    603 
    604 	for (cnt = len; cnt > 0; cnt -= i, buf += i) {
    605 		if ((i = write(xprt->xp_fd, buf, (size_t)cnt)) < 0) {
    606 			if (errno != EAGAIN || !cd->nonblock) {
    607 				cd->strm_stat = XPRT_DIED;
    608 				return (-1);
    609 			}
    610 			if (cd->nonblock && i != cnt) {
    611 				/*
    612 				 * For non-blocking connections, do not
    613 				 * take more than 2 seconds writing the
    614 				 * data out.
    615 				 *
    616 				 * XXX 2 is an arbitrary amount.
    617 				 */
    618 				gettimeofday(&tv1, NULL);
    619 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
    620 					cd->strm_stat = XPRT_DIED;
    621 					return (-1);
    622 				}
    623 			}
    624 		}
    625 	}
    626 	return (len);
    627 }
    628 
    629 static enum xprt_stat
    630 svc_vc_stat(xprt)
    631 	SVCXPRT *xprt;
    632 {
    633 	struct cf_conn *cd;
    634 
    635 	_DIAGASSERT(xprt != NULL);
    636 
    637 	cd = (struct cf_conn *)(xprt->xp_p1);
    638 
    639 	if (cd->strm_stat == XPRT_DIED)
    640 		return (XPRT_DIED);
    641 	if (! xdrrec_eof(&(cd->xdrs)))
    642 		return (XPRT_MOREREQS);
    643 	return (XPRT_IDLE);
    644 }
    645 
    646 static bool_t
    647 svc_vc_recv(xprt, msg)
    648 	SVCXPRT *xprt;
    649 	struct rpc_msg *msg;
    650 {
    651 	struct cf_conn *cd;
    652 	XDR *xdrs;
    653 
    654 	_DIAGASSERT(xprt != NULL);
    655 	_DIAGASSERT(msg != NULL);
    656 
    657 	cd = (struct cf_conn *)(xprt->xp_p1);
    658 	xdrs = &(cd->xdrs);
    659 
    660 	if (cd->nonblock) {
    661 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
    662 			return FALSE;
    663 	}
    664 
    665 	xdrs->x_op = XDR_DECODE;
    666 	(void)xdrrec_skiprecord(xdrs);
    667 
    668 	if (xdr_callmsg(xdrs, msg)) {
    669 		cd->x_id = msg->rm_xid;
    670 		return (TRUE);
    671 	}
    672 	cd->strm_stat = XPRT_DIED;
    673 	return (FALSE);
    674 }
    675 
    676 static bool_t
    677 svc_vc_getargs(xprt, xdr_args, args_ptr)
    678 	SVCXPRT *xprt;
    679 	xdrproc_t xdr_args;
    680 	caddr_t args_ptr;
    681 {
    682 
    683 	_DIAGASSERT(xprt != NULL);
    684 	/* args_ptr may be NULL */
    685 
    686 	return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
    687 	    args_ptr));
    688 }
    689 
    690 static bool_t
    691 svc_vc_freeargs(xprt, xdr_args, args_ptr)
    692 	SVCXPRT *xprt;
    693 	xdrproc_t xdr_args;
    694 	caddr_t args_ptr;
    695 {
    696 	XDR *xdrs;
    697 
    698 	_DIAGASSERT(xprt != NULL);
    699 	/* args_ptr may be NULL */
    700 
    701 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
    702 
    703 	xdrs->x_op = XDR_FREE;
    704 	return ((*xdr_args)(xdrs, args_ptr));
    705 }
    706 
    707 static bool_t
    708 svc_vc_reply(xprt, msg)
    709 	SVCXPRT *xprt;
    710 	struct rpc_msg *msg;
    711 {
    712 	struct cf_conn *cd;
    713 	XDR *xdrs;
    714 	bool_t rstat;
    715 
    716 	_DIAGASSERT(xprt != NULL);
    717 	_DIAGASSERT(msg != NULL);
    718 
    719 	cd = (struct cf_conn *)(xprt->xp_p1);
    720 	xdrs = &(cd->xdrs);
    721 
    722 	xdrs->x_op = XDR_ENCODE;
    723 	msg->rm_xid = cd->x_id;
    724 	rstat = xdr_replymsg(xdrs, msg);
    725 	(void)xdrrec_endofrecord(xdrs, TRUE);
    726 	return (rstat);
    727 }
    728 
    729 static void
    730 svc_vc_ops(xprt)
    731 	SVCXPRT *xprt;
    732 {
    733 	static struct xp_ops ops;
    734 	static struct xp_ops2 ops2;
    735 #ifdef _REENTRANT
    736 	extern mutex_t ops_lock;
    737 #endif
    738 
    739 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
    740 
    741 	mutex_lock(&ops_lock);
    742 	if (ops.xp_recv == NULL) {
    743 		ops.xp_recv = svc_vc_recv;
    744 		ops.xp_stat = svc_vc_stat;
    745 		ops.xp_getargs = svc_vc_getargs;
    746 		ops.xp_reply = svc_vc_reply;
    747 		ops.xp_freeargs = svc_vc_freeargs;
    748 		ops.xp_destroy = svc_vc_destroy;
    749 		ops2.xp_control = svc_vc_control;
    750 	}
    751 	xprt->xp_ops = &ops;
    752 	xprt->xp_ops2 = &ops2;
    753 	mutex_unlock(&ops_lock);
    754 }
    755 
    756 static void
    757 svc_vc_rendezvous_ops(xprt)
    758 	SVCXPRT *xprt;
    759 {
    760 	static struct xp_ops ops;
    761 	static struct xp_ops2 ops2;
    762 #ifdef _REENTRANT
    763 	extern mutex_t ops_lock;
    764 #endif
    765 
    766 	mutex_lock(&ops_lock);
    767 	if (ops.xp_recv == NULL) {
    768 		ops.xp_recv = rendezvous_request;
    769 		ops.xp_stat = rendezvous_stat;
    770 		ops.xp_getargs =
    771 		    (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort;
    772 		ops.xp_reply =
    773 		    (bool_t (*) __P((SVCXPRT *, struct rpc_msg *)))abort;
    774 		ops.xp_freeargs =
    775 		    (bool_t (*) __P((SVCXPRT *, xdrproc_t, caddr_t)))abort,
    776 		ops.xp_destroy = svc_vc_destroy;
    777 		ops2.xp_control = svc_vc_rendezvous_control;
    778 	}
    779 	xprt->xp_ops = &ops;
    780 	xprt->xp_ops2 = &ops2;
    781 	mutex_unlock(&ops_lock);
    782 }
    783 
    784 /*
    785  * Destroy xprts that have not have had any activity in 'timeout' seconds.
    786  * If 'cleanblock' is true, blocking connections (the default) are also
    787  * cleaned. If timeout is 0, the least active connection is picked.
    788  */
    789 bool_t
    790 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
    791 {
    792 	int i, ncleaned;
    793 	SVCXPRT *xprt, *least_active;
    794 	struct timeval tv, tdiff, tmax;
    795 	struct cf_conn *cd;
    796 
    797 	gettimeofday(&tv, NULL);
    798 	tmax.tv_sec = tmax.tv_usec = 0;
    799 	least_active = NULL;
    800 	rwlock_wrlock(&svc_fd_lock);
    801 	for (i = ncleaned = 0; i <= svc_maxfd; i++) {
    802 		if (FD_ISSET(i, fds)) {
    803 			xprt = __svc_xports[i];
    804 			if (xprt == NULL || xprt->xp_ops == NULL ||
    805 			    xprt->xp_ops->xp_recv != svc_vc_recv)
    806 				continue;
    807 			cd = (struct cf_conn *)xprt->xp_p1;
    808 			if (!cleanblock && !cd->nonblock)
    809 				continue;
    810 			if (timeout == 0) {
    811 				timersub(&tv, &cd->last_recv_time, &tdiff);
    812 				if (timercmp(&tdiff, &tmax, >)) {
    813 					tmax = tdiff;
    814 					least_active = xprt;
    815 				}
    816 				continue;
    817 			}
    818 			if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
    819 				__xprt_unregister_unlocked(xprt);
    820 				__svc_vc_dodestroy(xprt);
    821 				ncleaned++;
    822 			}
    823 		}
    824 	}
    825 	if (timeout == 0 && least_active != NULL) {
    826 		__xprt_unregister_unlocked(least_active);
    827 		__svc_vc_dodestroy(least_active);
    828 		ncleaned++;
    829 	}
    830 	rwlock_unlock(&svc_fd_lock);
    831 	return ncleaned > 0 ? TRUE : FALSE;
    832 }
    833