xref: /freebsd/lib/libc/rpc/svc_vc.c (revision 74bf4e164ba5851606a27d4feff27717452583e5)
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 *sccsid = "@(#)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 "un-namespace.h"
72 
73 extern rwlock_t svc_fd_lock;
74 
75 static SVCXPRT *makefd_xprt(int, u_int, u_int);
76 static bool_t rendezvous_request(SVCXPRT *, struct rpc_msg *);
77 static enum xprt_stat rendezvous_stat(SVCXPRT *);
78 static void svc_vc_destroy(SVCXPRT *);
79 static void __svc_vc_dodestroy (SVCXPRT *);
80 static int read_vc(void *, void *, int);
81 static int write_vc(void *, void *, int);
82 static enum xprt_stat svc_vc_stat(SVCXPRT *);
83 static bool_t svc_vc_recv(SVCXPRT *, struct rpc_msg *);
84 static bool_t svc_vc_getargs(SVCXPRT *, xdrproc_t, void *);
85 static bool_t svc_vc_freeargs(SVCXPRT *, xdrproc_t, void *);
86 static bool_t svc_vc_reply(SVCXPRT *, struct rpc_msg *);
87 static void svc_vc_rendezvous_ops(SVCXPRT *);
88 static void svc_vc_ops(SVCXPRT *);
89 static bool_t svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in);
90 static bool_t svc_vc_rendezvous_control (SVCXPRT *xprt, const u_int rq,
91 				   	     void *in);
92 
93 struct cf_rendezvous { /* kept in xprt->xp_p1 for rendezvouser */
94 	u_int sendsize;
95 	u_int recvsize;
96 	int maxrec;
97 };
98 
99 struct cf_conn {  /* kept in xprt->xp_p1 for actual connection */
100 	enum xprt_stat strm_stat;
101 	u_int32_t x_id;
102 	XDR xdrs;
103 	char verf_body[MAX_AUTH_BYTES];
104 	u_int sendsize;
105 	u_int recvsize;
106 	int maxrec;
107 	bool_t nonblock;
108 	struct timeval last_recv_time;
109 };
110 
111 /*
112  * Usage:
113  *	xprt = svc_vc_create(sock, send_buf_size, recv_buf_size);
114  *
115  * Creates, registers, and returns a (rpc) tcp based transporter.
116  * Once *xprt is initialized, it is registered as a transporter
117  * see (svc.h, xprt_register).  This routine returns
118  * a NULL if a problem occurred.
119  *
120  * The filedescriptor passed in is expected to refer to a bound, but
121  * not yet connected socket.
122  *
123  * Since streams do buffered io similar to stdio, the caller can specify
124  * how big the send and receive buffers are via the second and third parms;
125  * 0 => use the system default.
126  */
127 SVCXPRT *
128 svc_vc_create(fd, sendsize, recvsize)
129 	int fd;
130 	u_int sendsize;
131 	u_int recvsize;
132 {
133 	SVCXPRT *xprt;
134 	struct cf_rendezvous *r = NULL;
135 	struct __rpc_sockinfo si;
136 	struct sockaddr_storage sslocal;
137 	socklen_t slen;
138 
139 	r = mem_alloc(sizeof(*r));
140 	if (r == NULL) {
141 		warnx("svc_vc_create: out of memory");
142 		goto cleanup_svc_vc_create;
143 	}
144 	if (!__rpc_fd2sockinfo(fd, &si))
145 		return NULL;
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 (r != NULL)
182 		mem_free(r, sizeof(*r));
183 	return (NULL);
184 }
185 
186 /*
187  * Like svtcp_create(), except the routine takes any *open* UNIX file
188  * descriptor as its first input.
189  */
190 SVCXPRT *
191 svc_fd_create(fd, sendsize, recvsize)
192 	int fd;
193 	u_int sendsize;
194 	u_int recvsize;
195 {
196 	struct sockaddr_storage ss;
197 	socklen_t slen;
198 	SVCXPRT *ret;
199 
200 	assert(fd != -1);
201 
202 	ret = makefd_xprt(fd, sendsize, recvsize);
203 	if (ret == NULL)
204 		return NULL;
205 
206 	slen = sizeof (struct sockaddr_storage);
207 	if (_getsockname(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
208 		warnx("svc_fd_create: could not retrieve local addr");
209 		goto freedata;
210 	}
211 	ret->xp_ltaddr.maxlen = ret->xp_ltaddr.len = ss.ss_len;
212 	ret->xp_ltaddr.buf = mem_alloc((size_t)ss.ss_len);
213 	if (ret->xp_ltaddr.buf == NULL) {
214 		warnx("svc_fd_create: no mem for local addr");
215 		goto freedata;
216 	}
217 	memcpy(ret->xp_ltaddr.buf, &ss, (size_t)ss.ss_len);
218 
219 	slen = sizeof (struct sockaddr_storage);
220 	if (_getpeername(fd, (struct sockaddr *)(void *)&ss, &slen) < 0) {
221 		warnx("svc_fd_create: could not retrieve remote addr");
222 		goto freedata;
223 	}
224 	ret->xp_rtaddr.maxlen = ret->xp_rtaddr.len = ss.ss_len;
225 	ret->xp_rtaddr.buf = mem_alloc((size_t)ss.ss_len);
226 	if (ret->xp_rtaddr.buf == NULL) {
227 		warnx("svc_fd_create: no mem for local addr");
228 		goto freedata;
229 	}
230 	memcpy(ret->xp_rtaddr.buf, &ss, (size_t)ss.ss_len);
231 #ifdef PORTMAP
232 	if (ss.ss_family == AF_INET || ss.ss_family == AF_LOCAL) {
233 		ret->xp_raddr = *(struct sockaddr_in *)ret->xp_rtaddr.buf;
234 		ret->xp_addrlen = sizeof (struct sockaddr_in);
235 	}
236 #endif				/* PORTMAP */
237 
238 	return ret;
239 
240 freedata:
241 	if (ret->xp_ltaddr.buf != NULL)
242 		mem_free(ret->xp_ltaddr.buf, rep->xp_ltaddr.maxlen);
243 
244 	return NULL;
245 }
246 
247 static SVCXPRT *
248 makefd_xprt(fd, sendsize, recvsize)
249 	int fd;
250 	u_int sendsize;
251 	u_int recvsize;
252 {
253 	SVCXPRT *xprt;
254 	struct cf_conn *cd;
255 	const char *netid;
256 	struct __rpc_sockinfo si;
257 
258 	assert(fd != -1);
259 
260 	xprt = mem_alloc(sizeof(SVCXPRT));
261 	if (xprt == NULL) {
262 		warnx("svc_vc: makefd_xprt: out of memory");
263 		goto done;
264 	}
265 	memset(xprt, 0, sizeof *xprt);
266 	cd = mem_alloc(sizeof(struct cf_conn));
267 	if (cd == NULL) {
268 		warnx("svc_tcp: makefd_xprt: out of memory");
269 		mem_free(xprt, sizeof(SVCXPRT));
270 		xprt = NULL;
271 		goto done;
272 	}
273 	cd->strm_stat = XPRT_IDLE;
274 	xdrrec_create(&(cd->xdrs), sendsize, recvsize,
275 	    xprt, read_vc, write_vc);
276 	xprt->xp_p1 = cd;
277 	xprt->xp_verf.oa_base = cd->verf_body;
278 	svc_vc_ops(xprt);  /* truely deals with calls */
279 	xprt->xp_port = 0;  /* this is a connection, not a rendezvouser */
280 	xprt->xp_fd = fd;
281         if (__rpc_fd2sockinfo(fd, &si) && __rpc_sockinfo2netid(&si, &netid))
282 		xprt->xp_netid = strdup(netid);
283 
284 	xprt_register(xprt);
285 done:
286 	return (xprt);
287 }
288 
289 /*ARGSUSED*/
290 static bool_t
291 rendezvous_request(xprt, msg)
292 	SVCXPRT *xprt;
293 	struct rpc_msg *msg;
294 {
295 	int sock, flags;
296 	struct cf_rendezvous *r;
297 	struct cf_conn *cd;
298 	struct sockaddr_storage addr;
299 	socklen_t len;
300 	struct __rpc_sockinfo si;
301 	SVCXPRT *newxprt;
302 	fd_set cleanfds;
303 
304 	assert(xprt != NULL);
305 	assert(msg != NULL);
306 
307 	r = (struct cf_rendezvous *)xprt->xp_p1;
308 again:
309 	len = sizeof addr;
310 	if ((sock = _accept(xprt->xp_fd, (struct sockaddr *)(void *)&addr,
311 	    &len)) < 0) {
312 		if (errno == EINTR)
313 			goto again;
314 		/*
315 		 * Clean out the most idle file descriptor when we're
316 		 * running out.
317 		 */
318 		if (errno == EMFILE || errno == ENFILE) {
319 			cleanfds = svc_fdset;
320 			__svc_clean_idle(&cleanfds, 0, FALSE);
321 			goto again;
322 		}
323 		return (FALSE);
324 	}
325 	/*
326 	 * make a new transporter (re-uses xprt)
327 	 */
328 	newxprt = makefd_xprt(sock, r->sendsize, r->recvsize);
329 	newxprt->xp_rtaddr.buf = mem_alloc(len);
330 	if (newxprt->xp_rtaddr.buf == NULL)
331 		return (FALSE);
332 	memcpy(newxprt->xp_rtaddr.buf, &addr, len);
333 	newxprt->xp_rtaddr.len = len;
334 #ifdef PORTMAP
335 	if (addr.ss_family == AF_INET || addr.ss_family == AF_LOCAL) {
336 		newxprt->xp_raddr = *(struct sockaddr_in *)newxprt->xp_rtaddr.buf;
337 		newxprt->xp_addrlen = sizeof (struct sockaddr_in);
338 	}
339 #endif				/* PORTMAP */
340 	if (__rpc_fd2sockinfo(sock, &si) && si.si_proto == IPPROTO_TCP) {
341 		len = 1;
342 		/* XXX fvdl - is this useful? */
343 		_setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &len, sizeof (len));
344 	}
345 
346 	cd = (struct cf_conn *)newxprt->xp_p1;
347 
348 	cd->recvsize = r->recvsize;
349 	cd->sendsize = r->sendsize;
350 	cd->maxrec = r->maxrec;
351 
352 	if (cd->maxrec != 0) {
353 		flags = _fcntl(sock, F_GETFL, 0);
354 		if (flags  == -1)
355 			return (FALSE);
356 		if (_fcntl(sock, F_SETFL, flags | O_NONBLOCK) == -1)
357 			return (FALSE);
358 		if (cd->recvsize > cd->maxrec)
359 			cd->recvsize = cd->maxrec;
360 		cd->nonblock = TRUE;
361 		__xdrrec_setnonblock(&cd->xdrs, cd->maxrec);
362 	} else
363 		cd->nonblock = FALSE;
364 
365 	gettimeofday(&cd->last_recv_time, NULL);
366 
367 	return (FALSE); /* there is never an rpc msg to be processed */
368 }
369 
370 /*ARGSUSED*/
371 static enum xprt_stat
372 rendezvous_stat(xprt)
373 	SVCXPRT *xprt;
374 {
375 
376 	return (XPRT_IDLE);
377 }
378 
379 static void
380 svc_vc_destroy(xprt)
381 	SVCXPRT *xprt;
382 {
383 	assert(xprt != NULL);
384 
385 	xprt_unregister(xprt);
386 	__svc_vc_dodestroy(xprt);
387 }
388 
389 static void
390 __svc_vc_dodestroy(xprt)
391 	SVCXPRT *xprt;
392 {
393 	struct cf_conn *cd;
394 	struct cf_rendezvous *r;
395 
396 	cd = (struct cf_conn *)xprt->xp_p1;
397 
398 	if (xprt->xp_fd != RPC_ANYFD)
399 		(void)_close(xprt->xp_fd);
400 	if (xprt->xp_port != 0) {
401 		/* a rendezvouser socket */
402 		r = (struct cf_rendezvous *)xprt->xp_p1;
403 		mem_free(r, sizeof (struct cf_rendezvous));
404 		xprt->xp_port = 0;
405 	} else {
406 		/* an actual connection socket */
407 		XDR_DESTROY(&(cd->xdrs));
408 		mem_free(cd, sizeof(struct cf_conn));
409 	}
410 	if (xprt->xp_rtaddr.buf)
411 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
412 	if (xprt->xp_ltaddr.buf)
413 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
414 	if (xprt->xp_tp)
415 		free(xprt->xp_tp);
416 	if (xprt->xp_netid)
417 		free(xprt->xp_netid);
418 	mem_free(xprt, sizeof(SVCXPRT));
419 }
420 
421 /*ARGSUSED*/
422 static bool_t
423 svc_vc_control(xprt, rq, in)
424 	SVCXPRT *xprt;
425 	const u_int rq;
426 	void *in;
427 {
428 	return (FALSE);
429 }
430 
431 static bool_t
432 svc_vc_rendezvous_control(xprt, rq, in)
433 	SVCXPRT *xprt;
434 	const u_int rq;
435 	void *in;
436 {
437 	struct cf_rendezvous *cfp;
438 
439 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
440 	if (cfp == NULL)
441 		return (FALSE);
442 	switch (rq) {
443 		case SVCGET_CONNMAXREC:
444 			*(int *)in = cfp->maxrec;
445 			break;
446 		case SVCSET_CONNMAXREC:
447 			cfp->maxrec = *(int *)in;
448 			break;
449 		default:
450 			return (FALSE);
451 	}
452 	return (TRUE);
453 }
454 
455 /*
456  * reads data from the tcp or uip connection.
457  * any error is fatal and the connection is closed.
458  * (And a read of zero bytes is a half closed stream => error.)
459  * All read operations timeout after 35 seconds.  A timeout is
460  * fatal for the connection.
461  */
462 static int
463 read_vc(xprtp, buf, len)
464 	void *xprtp;
465 	void *buf;
466 	int len;
467 {
468 	SVCXPRT *xprt;
469 	int sock;
470 	int milliseconds = 35 * 1000;
471 	struct pollfd pollfd;
472 	struct cf_conn *cfp;
473 
474 	xprt = (SVCXPRT *)xprtp;
475 	assert(xprt != NULL);
476 
477 	sock = xprt->xp_fd;
478 
479 	cfp = (struct cf_conn *)xprt->xp_p1;
480 
481 	if (cfp->nonblock) {
482 		len = _read(sock, buf, (size_t)len);
483 		if (len < 0) {
484 			if (errno == EAGAIN)
485 				len = 0;
486 			else
487 				goto fatal_err;
488 		}
489 		if (len != 0)
490 			gettimeofday(&cfp->last_recv_time, NULL);
491 		return len;
492 	}
493 
494 	do {
495 		pollfd.fd = sock;
496 		pollfd.events = POLLIN;
497 		pollfd.revents = 0;
498 		switch (_poll(&pollfd, 1, milliseconds)) {
499 		case -1:
500 			if (errno == EINTR)
501 				continue;
502 			/*FALLTHROUGH*/
503 		case 0:
504 			goto fatal_err;
505 
506 		default:
507 			break;
508 		}
509 	} while ((pollfd.revents & POLLIN) == 0);
510 
511 	if ((len = _read(sock, buf, (size_t)len)) > 0) {
512 		gettimeofday(&cfp->last_recv_time, NULL);
513 		return (len);
514 	}
515 
516 fatal_err:
517 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
518 	return (-1);
519 }
520 
521 /*
522  * writes data to the tcp connection.
523  * Any error is fatal and the connection is closed.
524  */
525 static int
526 write_vc(xprtp, buf, len)
527 	void *xprtp;
528 	void *buf;
529 	int len;
530 {
531 	SVCXPRT *xprt;
532 	int i, cnt;
533 	struct cf_conn *cd;
534 	struct timeval tv0, tv1;
535 
536 	xprt = (SVCXPRT *)xprtp;
537 	assert(xprt != NULL);
538 
539 	cd = (struct cf_conn *)xprt->xp_p1;
540 
541 	if (cd->nonblock)
542 		gettimeofday(&tv0, NULL);
543 
544 	for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
545 		i = _write(xprt->xp_fd, buf, (size_t)cnt);
546 		if (i  < 0) {
547 			if (errno != EAGAIN || !cd->nonblock) {
548 				cd->strm_stat = XPRT_DIED;
549 				return (-1);
550 			}
551 			if (cd->nonblock && i != cnt) {
552 				/*
553 				 * For non-blocking connections, do not
554 				 * take more than 2 seconds writing the
555 				 * data out.
556 				 *
557 				 * XXX 2 is an arbitrary amount.
558 				 */
559 				gettimeofday(&tv1, NULL);
560 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
561 					cd->strm_stat = XPRT_DIED;
562 					return (-1);
563 				}
564 			}
565 		}
566 	}
567 
568 	return (len);
569 }
570 
571 static enum xprt_stat
572 svc_vc_stat(xprt)
573 	SVCXPRT *xprt;
574 {
575 	struct cf_conn *cd;
576 
577 	assert(xprt != NULL);
578 
579 	cd = (struct cf_conn *)(xprt->xp_p1);
580 
581 	if (cd->strm_stat == XPRT_DIED)
582 		return (XPRT_DIED);
583 	if (! xdrrec_eof(&(cd->xdrs)))
584 		return (XPRT_MOREREQS);
585 	return (XPRT_IDLE);
586 }
587 
588 static bool_t
589 svc_vc_recv(xprt, msg)
590 	SVCXPRT *xprt;
591 	struct rpc_msg *msg;
592 {
593 	struct cf_conn *cd;
594 	XDR *xdrs;
595 
596 	assert(xprt != NULL);
597 	assert(msg != NULL);
598 
599 	cd = (struct cf_conn *)(xprt->xp_p1);
600 	xdrs = &(cd->xdrs);
601 
602 	if (cd->nonblock) {
603 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
604 			return FALSE;
605 	}
606 
607 	xdrs->x_op = XDR_DECODE;
608 	(void)xdrrec_skiprecord(xdrs);
609 	if (xdr_callmsg(xdrs, msg)) {
610 		cd->x_id = msg->rm_xid;
611 		return (TRUE);
612 	}
613 	cd->strm_stat = XPRT_DIED;
614 	return (FALSE);
615 }
616 
617 static bool_t
618 svc_vc_getargs(xprt, xdr_args, args_ptr)
619 	SVCXPRT *xprt;
620 	xdrproc_t xdr_args;
621 	void *args_ptr;
622 {
623 
624 	assert(xprt != NULL);
625 	/* args_ptr may be NULL */
626 	return ((*xdr_args)(&(((struct cf_conn *)(xprt->xp_p1))->xdrs),
627 	    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 
656 	assert(xprt != NULL);
657 	assert(msg != NULL);
658 
659 	cd = (struct cf_conn *)(xprt->xp_p1);
660 	xdrs = &(cd->xdrs);
661 
662 	xdrs->x_op = XDR_ENCODE;
663 	msg->rm_xid = cd->x_id;
664 	rstat = xdr_replymsg(xdrs, msg);
665 	(void)xdrrec_endofrecord(xdrs, TRUE);
666 	return (rstat);
667 }
668 
669 static void
670 svc_vc_ops(xprt)
671 	SVCXPRT *xprt;
672 {
673 	static struct xp_ops ops;
674 	static struct xp_ops2 ops2;
675 	extern mutex_t ops_lock;
676 
677 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
678 
679 	mutex_lock(&ops_lock);
680 	if (ops.xp_recv == NULL) {
681 		ops.xp_recv = svc_vc_recv;
682 		ops.xp_stat = svc_vc_stat;
683 		ops.xp_getargs = svc_vc_getargs;
684 		ops.xp_reply = svc_vc_reply;
685 		ops.xp_freeargs = svc_vc_freeargs;
686 		ops.xp_destroy = svc_vc_destroy;
687 		ops2.xp_control = svc_vc_control;
688 	}
689 	xprt->xp_ops = &ops;
690 	xprt->xp_ops2 = &ops2;
691 	mutex_unlock(&ops_lock);
692 }
693 
694 static void
695 svc_vc_rendezvous_ops(xprt)
696 	SVCXPRT *xprt;
697 {
698 	static struct xp_ops ops;
699 	static struct xp_ops2 ops2;
700 	extern mutex_t ops_lock;
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