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