xref: /freebsd/lib/libc/rpc/svc_vc.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 = mem_alloc(sizeof(SVCXPRT));
150 	if (xprt == NULL) {
151 		warnx("svc_vc_create: out of memory");
152 		goto cleanup_svc_vc_create;
153 	}
154 	xprt->xp_tp = NULL;
155 	xprt->xp_p1 = r;
156 	xprt->xp_p2 = NULL;
157 	xprt->xp_p3 = NULL;
158 	xprt->xp_verf = _null_auth;
159 	svc_vc_rendezvous_ops(xprt);
160 	xprt->xp_port = (u_short)-1;	/* It is the rendezvouser */
161 	xprt->xp_fd = fd;
162 
163 	slen = sizeof (struct sockaddr_storage);
164 	if (_getsockname(fd, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
165 		warnx("svc_vc_create: could not retrieve local addr");
166 		goto cleanup_svc_vc_create;
167 	}
168 
169 	xprt->xp_ltaddr.maxlen = xprt->xp_ltaddr.len = sslocal.ss_len;
170 	xprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
171 	if (xprt->xp_ltaddr.buf == NULL) {
172 		warnx("svc_vc_create: no mem for local addr");
173 		goto cleanup_svc_vc_create;
174 	}
175 	memcpy(xprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
176 
177 	xprt->xp_rtaddr.maxlen = sizeof (struct sockaddr_storage);
178 	xprt_register(xprt);
179 	return (xprt);
180 cleanup_svc_vc_create:
181 	if (xprt)
182 		mem_free(xprt, sizeof(*xprt));
183 	if (r != NULL)
184 		mem_free(r, sizeof(*r));
185 	return (NULL);
186 }
187 
188 /*
189  * Like svtcp_create(), except the routine takes any *open* UNIX file
190  * descriptor as its first input.
191  */
192 SVCXPRT *
193 svc_fd_create(fd, sendsize, recvsize)
194 	int fd;
195 	u_int sendsize;
196 	u_int recvsize;
197 {
198 	struct sockaddr_storage ss;
199 	socklen_t slen;
200 	SVCXPRT *ret;
201 
202 	assert(fd != -1);
203 
204 	ret = makefd_xprt(fd, sendsize, recvsize);
205 	if (ret == NULL)
206 		return NULL;
207 
208 	slen = sizeof (struct sockaddr_storage);
209 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
210 		warnx("svc_fd_create: could not retrieve local addr");
211 		goto freedata;
212 	}
213 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
214 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
215 	if (ret->xp_ltaddr.buf == NULL) {
216 		warnx("svc_fd_create: no mem for local addr");
217 		goto freedata;
218 	}
219 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
220 
221 	slen = sizeof (struct sockaddr_storage);
222 	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
223 		warnx("svc_fd_create: could not retrieve remote addr");
224 		goto freedata;
225 	}
226 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
227 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
228 	if (ret->xp_rtaddr.buf == NULL) {
229 		warnx("svc_fd_create: no mem for local addr");
230 		goto freedata;
231 	}
232 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
233 #ifdef PORTMAP
234 	if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
235 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
236 		ret->xp_addrlen = sizeof (struct sockaddr_in);
237 	}
238 #endif				/* PORTMAP */
239 
240 	return ret;
241 
242 freedata:
243 	if (ret->xp_ltaddr.buf != NULL)
244 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
245 
246 	return NULL;
247 }
248 
249 static SVCXPRT *
250 makefd_xprt(fd, sendsize, recvsize)
251 	int fd;
252 	u_int sendsize;
253 	u_int recvsize;
254 {
255 	SVCXPRT *xprt;
256 	struct cf_conn *cd;
257 	const char *netid;
258 	struct __rpc_sockinfo si;
259 
260 	assert(fd != -1);
261 
262 	xprt = mem_alloc(sizeof(SVCXPRT));
263 	if (xprt == NULL) {
264 		warnx("svc_vc: makefd_xprt: out of memory");
265 		goto done;
266 	}
267 	memset(xprt, 0, sizeof *xprt);
268 	cd = mem_alloc(sizeof(struct cf_conn));
269 	if (cd == NULL) {
270 		warnx("svc_tcp: makefd_xprt: out of memory");
271 		mem_free(xprt, sizeof(SVCXPRT));
272 		xprt = NULL;
273 		goto done;
274 	}
275 	cd->strm_stat = XPRT_IDLE;
276 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
277 	    xprt, read_vc, write_vc);
278 	xprt->xp_p1 = cd;
279 	xprt->xp_verf.oa_base = cd->verf_body;
280 	svc_vc_ops(xprt);  /* truely deals with calls */
281 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
282 	xprt->xp_fd = fd;
283         if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
284 		xprt->xp_netid = strdup(netid);
285 
286 	xprt_register(xprt);
287 done:
288 	return (xprt);
289 }
290 
291 /*ARGSUSED*/
292 static bool_t
293 rendezvous_request(xprt, msg)
294 	SVCXPRT *xprt;
295 	struct rpc_msg *msg;
296 {
297 	int sock, flags;
298 	struct cf_rendezvous *r;
299 	struct cf_conn *cd;
300 	struct sockaddr_storage addr;
301 	socklen_t len;
302 	struct __rpc_sockinfo si;
303 	SVCXPRT *newxprt;
304 	fd_set cleanfds;
305 
306 	assert(xprt != NULL);
307 	assert(msg != NULL);
308 
309 	r = (struct cf_rendezvous *)xprt->xp_p1;
310 again:
311 	len = sizeof addr;
312 	if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
313 	    &len)) < 0) {
314 		if (errno == EINTR)
315 			goto again;
316 		/*
317 		 * Clean out the most idle file descriptor when we're
318 		 * running out.
319 		 */
320 		if (errno == EMFILE || errno == ENFILE) {
321 			cleanfds = svc_fdset;
322 			__svc_clean_idle(&cleanfds, 0, FALSE);
323 			goto again;
324 		}
325 		return (FALSE);
326 	}
327 	/*
328 	 * make a new transporter (re-uses xprt)
329 	 */
330 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
331 	newxprt->xp_rtaddr.buf = mem_alloc(len);
332 	if (newxprt->xp_rtaddr.buf == NULL)
333 		return (FALSE);
334 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
335 	newxprt->xp_rtaddr.len = len;
336 #ifdef PORTMAP
337 	if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
338 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
339 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
340 	}
341 #endif				/* PORTMAP */
342 	if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
343 		len = 1;
344 		/* XXX fvdl - is this useful? */
345 		_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
346 	}
347 
348 	cd = (struct cf_conn *)newxprt->xp_p1;
349 
350 	cd->recvsize = r->recvsize;
351 	cd->sendsize = r->sendsize;
352 	cd->maxrec = r->maxrec;
353 
354 	if (cd->maxrec != 0) {
355 		flags = _fcntl(sock, F_GETFL, 0);
356 		if (flags  == -1)
357 			return (FALSE);
358 		if (_fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
359 			return (FALSE);
360 		if (cd->recvsize > cd->maxrec)
361 			cd->recvsize = cd->maxrec;
362 		cd->nonblock = TRUE;
363 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
364 	} else
365 		cd->nonblock = FALSE;
366 
367 	gettimeofday(&cd->last_recv_time, NULL);
368 
369 	return (FALSE); /* there is never an rpc msg to be processed */
370 }
371 
372 /*ARGSUSED*/
373 static enum xprt_stat
374 rendezvous_stat(xprt)
375 	SVCXPRT *xprt;
376 {
377 
378 	return (XPRT_IDLE);
379 }
380 
381 static void
382 svc_vc_destroy(xprt)
383 	SVCXPRT *xprt;
384 {
385 	assert(xprt != NULL);
386 
387 	xprt_unregister(xprt);
388 	__svc_vc_dodestroy(xprt);
389 }
390 
391 static void
392 __svc_vc_dodestroy(xprt)
393 	SVCXPRT *xprt;
394 {
395 	struct cf_conn *cd;
396 	struct cf_rendezvous *r;
397 
398 	cd = (struct cf_conn *)xprt->xp_p1;
399 
400 	if (xprt->xp_fd != RPC_ANYFD)
401 		(void)_close(xprt->xp_fd);
402 	if (xprt->xp_port != 0) {
403 		/* a rendezvouser socket */
404 		r = (struct cf_rendezvous *)xprt->xp_p1;
405 		mem_free(r, sizeof (struct cf_rendezvous));
406 		xprt->xp_port = 0;
407 	} else {
408 		/* an actual connection socket */
409 		XDR_DESTROY(&(cd->xdrs));
410 		mem_free(cd, sizeof(struct cf_conn));
411 	}
412 	if (xprt->xp_rtaddr.buf)
413 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
414 	if (xprt->xp_ltaddr.buf)
415 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
416 	if (xprt->xp_tp)
417 		free(xprt->xp_tp);
418 	if (xprt->xp_netid)
419 		free(xprt->xp_netid);
420 	mem_free(xprt, sizeof(SVCXPRT));
421 }
422 
423 /*ARGSUSED*/
424 static bool_t
425 svc_vc_control(xprt, rq, in)
426 	SVCXPRT *xprt;
427 	const u_int rq;
428 	void *in;
429 {
430 	return (FALSE);
431 }
432 
433 static bool_t
434 svc_vc_rendezvous_control(xprt, rq, in)
435 	SVCXPRT *xprt;
436 	const u_int rq;
437 	void *in;
438 {
439 	struct cf_rendezvous *cfp;
440 
441 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
442 	if (cfp == NULL)
443 		return (FALSE);
444 	switch (rq) {
445 		case SVCGET_CONNMAXREC:
446 			*(int *)in = cfp->maxrec;
447 			break;
448 		case SVCSET_CONNMAXREC:
449 			cfp->maxrec = *(int *)in;
450 			break;
451 		default:
452 			return (FALSE);
453 	}
454 	return (TRUE);
455 }
456 
457 /*
458  * reads data from the tcp or uip connection.
459  * any error is fatal and the connection is closed.
460  * (And a read of zero bytes is a half closed stream => error.)
461  * All read operations timeout after 35 seconds.  A timeout is
462  * fatal for the connection.
463  */
464 static int
465 read_vc(xprtp, buf, len)
466 	void *xprtp;
467 	void *buf;
468 	int len;
469 {
470 	SVCXPRT *xprt;
471 	int sock;
472 	int milliseconds = 35 * 1000;
473 	struct pollfd pollfd;
474 	struct cf_conn *cfp;
475 
476 	xprt = (SVCXPRT *)xprtp;
477 	assert(xprt != NULL);
478 
479 	sock = xprt->xp_fd;
480 
481 	cfp = (struct cf_conn *)xprt->xp_p1;
482 
483 	if (cfp->nonblock) {
484 		len = _read(sock, buf, (size_t)len);
485 		if (len < 0) {
486 			if (errno == EAGAIN)
487 				len = 0;
488 			else
489 				goto fatal_err;
490 		}
491 		if (len != 0)
492 			gettimeofday(&cfp->last_recv_time, NULL);
493 		return len;
494 	}
495 
496 	do {
497 		pollfd.fd = sock;
498 		pollfd.events = POLLIN;
499 		pollfd.revents = 0;
500 		switch (_poll(&pollfd, 1, milliseconds)) {
501 		case -1:
502 			if (errno == EINTR)
503 				continue;
504 			/*FALLTHROUGH*/
505 		case 0:
506 			goto fatal_err;
507 
508 		default:
509 			break;
510 		}
511 	} while ((pollfd.revents & POLLIN) == 0);
512 
513 	if ((len = _read(sock, buf, (size_t)len)) > 0) {
514 		gettimeofday(&cfp->last_recv_time, NULL);
515 		return (len);
516 	}
517 
518 fatal_err:
519 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
520 	return (-1);
521 }
522 
523 /*
524  * writes data to the tcp connection.
525  * Any error is fatal and the connection is closed.
526  */
527 static int
528 write_vc(xprtp, buf, len)
529 	void *xprtp;
530 	void *buf;
531 	int len;
532 {
533 	SVCXPRT *xprt;
534 	int i, cnt;
535 	struct cf_conn *cd;
536 	struct timeval tv0, tv1;
537 
538 	xprt = (SVCXPRT *)xprtp;
539 	assert(xprt != NULL);
540 
541 	cd = (struct cf_conn *)xprt->xp_p1;
542 
543 	if (cd->nonblock)
544 		gettimeofday(&tv0, NULL);
545 
546 	for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
547 		i = _write(xprt->xp_fd, buf, (size_t)cnt);
548 		if (i  < 0) {
549 			if (errno != EAGAIN || !cd->nonblock) {
550 				cd->strm_stat = XPRT_DIED;
551 				return (-1);
552 			}
553 			if (cd->nonblock && i != cnt) {
554 				/*
555 				 * For non-blocking connections, do not
556 				 * take more than 2 seconds writing the
557 				 * data out.
558 				 *
559 				 * XXX 2 is an arbitrary amount.
560 				 */
561 				gettimeofday(&tv1, NULL);
562 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
563 					cd->strm_stat = XPRT_DIED;
564 					return (-1);
565 				}
566 			}
567 		}
568 	}
569 
570 	return (len);
571 }
572 
573 static enum xprt_stat
574 svc_vc_stat(xprt)
575 	SVCXPRT *xprt;
576 {
577 	struct cf_conn *cd;
578 
579 	assert(xprt != NULL);
580 
581 	cd = (struct cf_conn *)(xprt->xp_p1);
582 
583 	if (cd->strm_stat == XPRT_DIED)
584 		return (XPRT_DIED);
585 	if (! xdrrec_eof(&(cd->xdrs)))
586 		return (XPRT_MOREREQS);
587 	return (XPRT_IDLE);
588 }
589 
590 static bool_t
591 svc_vc_recv(xprt, msg)
592 	SVCXPRT *xprt;
593 	struct rpc_msg *msg;
594 {
595 	struct cf_conn *cd;
596 	XDR *xdrs;
597 
598 	assert(xprt != NULL);
599 	assert(msg != NULL);
600 
601 	cd = (struct cf_conn *)(xprt->xp_p1);
602 	xdrs = &(cd->xdrs);
603 
604 	if (cd->nonblock) {
605 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
606 			return FALSE;
607 	}
608 
609 	xdrs->x_op = XDR_DECODE;
610 	(void)xdrrec_skiprecord(xdrs);
611 	if (xdr_callmsg(xdrs, msg)) {
612 		cd->x_id = msg->rm_xid;
613 		return (TRUE);
614 	}
615 	cd->strm_stat = XPRT_DIED;
616 	return (FALSE);
617 }
618 
619 static bool_t
620 svc_vc_getargs(xprt, xdr_args, args_ptr)
621 	SVCXPRT *xprt;
622 	xdrproc_t xdr_args;
623 	void *args_ptr;
624 {
625 
626 	assert(xprt != NULL);
627 	/* args_ptr may be NULL */
628 	return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
629 	    args_ptr));
630 }
631 
632 static bool_t
633 svc_vc_freeargs(xprt, xdr_args, args_ptr)
634 	SVCXPRT *xprt;
635 	xdrproc_t xdr_args;
636 	void *args_ptr;
637 {
638 	XDR *xdrs;
639 
640 	assert(xprt != NULL);
641 	/* args_ptr may be NULL */
642 
643 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
644 
645 	xdrs->x_op = XDR_FREE;
646 	return ((*xdr_args)(xdrs, args_ptr));
647 }
648 
649 static bool_t
650 svc_vc_reply(xprt, msg)
651 	SVCXPRT *xprt;
652 	struct rpc_msg *msg;
653 {
654 	struct cf_conn *cd;
655 	XDR *xdrs;
656 	bool_t rstat;
657 
658 	assert(xprt != NULL);
659 	assert(msg != NULL);
660 
661 	cd = (struct cf_conn *)(xprt->xp_p1);
662 	xdrs = &(cd->xdrs);
663 
664 	xdrs->x_op = XDR_ENCODE;
665 	msg->rm_xid = cd->x_id;
666 	rstat = xdr_replymsg(xdrs, msg);
667 	(void)xdrrec_endofrecord(xdrs, TRUE);
668 	return (rstat);
669 }
670 
671 static void
672 svc_vc_ops(xprt)
673 	SVCXPRT *xprt;
674 {
675 	static struct xp_ops ops;
676 	static struct xp_ops2 ops2;
677 
678 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
679 
680 	mutex_lock(&ops_lock);
681 	if (ops.xp_recv == NULL) {
682 		ops.xp_recv = svc_vc_recv;
683 		ops.xp_stat = svc_vc_stat;
684 		ops.xp_getargs = svc_vc_getargs;
685 		ops.xp_reply = svc_vc_reply;
686 		ops.xp_freeargs = svc_vc_freeargs;
687 		ops.xp_destroy = svc_vc_destroy;
688 		ops2.xp_control = svc_vc_control;
689 	}
690 	xprt->xp_ops = &ops;
691 	xprt->xp_ops2 = &ops2;
692 	mutex_unlock(&ops_lock);
693 }
694 
695 static void
696 svc_vc_rendezvous_ops(xprt)
697 	SVCXPRT *xprt;
698 {
699 	static struct xp_ops ops;
700 	static struct xp_ops2 ops2;
701 
702 	mutex_lock(&ops_lock);
703 	if (ops.xp_recv == NULL) {
704 		ops.xp_recv = rendezvous_request;
705 		ops.xp_stat = rendezvous_stat;
706 		ops.xp_getargs =
707 		    (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
708 		ops.xp_reply =
709 		    (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
710 		ops.xp_freeargs =
711 		    (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
712 		ops.xp_destroy = svc_vc_destroy;
713 		ops2.xp_control = svc_vc_rendezvous_control;
714 	}
715 	xprt->xp_ops = &ops;
716 	xprt->xp_ops2 = &ops2;
717 	mutex_unlock(&ops_lock);
718 }
719 
720 /*
721  * Get the effective UID of the sending process. Used by rpcbind, keyserv
722  * and rpc.yppasswdd on AF_LOCAL.
723  */
724 int
725 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
726 	int sock, ret;
727 	gid_t egid;
728 	uid_t euid;
729 	struct sockaddr *sa;
730 
731 	sock = transp->xp_fd;
732 	sa = (struct sockaddr *)transp->xp_rtaddr.buf;
733 	if (sa->sa_family == AF_LOCAL) {
734 		ret = getpeereid(sock, &euid, &egid);
735 		if (ret == 0)
736 			*uid = euid;
737 		return (ret);
738 	} else
739 		return (-1);
740 }
741 
742 /*
743  * Destroy xprts that have not have had any activity in 'timeout' seconds.
744  * If 'cleanblock' is true, blocking connections (the default) are also
745  * cleaned. If timeout is 0, the least active connection is picked.
746  */
747 bool_t
748 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
749 {
750 	int i, ncleaned;
751 	SVCXPRT *xprt, *least_active;
752 	struct timeval tv, tdiff, tmax;
753 	struct cf_conn *cd;
754 
755 	gettimeofday(&tv, NULL);
756 	tmax.tv_sec = tmax.tv_usec = 0;
757 	least_active = NULL;
758 	rwlock_wrlock(&svc_fd_lock);
759 	for (i = ncleaned = 0; i <= svc_maxfd; i++) {
760 		if (FD_ISSET(i, fds)) {
761 			xprt = __svc_xports[i];
762 			if (xprt == NULL || xprt->xp_ops == NULL ||
763 			    xprt->xp_ops->xp_recv != svc_vc_recv)
764 				continue;
765 			cd = (struct cf_conn *)xprt->xp_p1;
766 			if (!cleanblock && !cd->nonblock)
767 				continue;
768 			if (timeout == 0) {
769 				timersub(&tv, &cd->last_recv_time, &tdiff);
770 				if (timercmp(&tdiff, &tmax, >)) {
771 					tmax = tdiff;
772 					least_active = xprt;
773 				}
774 				continue;
775 			}
776 			if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
777 				__xprt_unregister_unlocked(xprt);
778 				__svc_vc_dodestroy(xprt);
779 				ncleaned++;
780 			}
781 		}
782 	}
783 	if (timeout == 0 && least_active != NULL) {
784 		__xprt_unregister_unlocked(least_active);
785 		__svc_vc_dodestroy(least_active);
786 		ncleaned++;
787 	}
788 	rwlock_unlock(&svc_fd_lock);
789 	return ncleaned > 0 ? TRUE : FALSE;
790 }
791