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