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