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