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