xref: /freebsd/lib/libc/rpc/svc_vc.c (revision faf139cc5dd3396181c11922bc6685c0c59b7b24)
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, sslocal;
285 	socklen_t len, slen;
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 	slen = sizeof(struct sockaddr_storage);
351 	if(_getsockname(sock, (struct sockaddr *)(void *)&sslocal, &slen) < 0) {
352 		warnx("svc_vc_create: could not retrieve local addr");
353 		newxprt->xp_ltaddr.maxlen = newxprt->xp_ltaddr.len = 0;
354 	} else {
355 		newxprt->xp_ltaddr.maxlen = newxprt->xp_ltaddr.len = sslocal.ss_len;
356 		newxprt->xp_ltaddr.buf = mem_alloc((size_t)sslocal.ss_len);
357 		if (newxprt->xp_ltaddr.buf == NULL) {
358 			warnx("svc_vc_create: no mem for local addr");
359 			newxprt->xp_ltaddr.maxlen = newxprt->xp_ltaddr.len = 0;
360 		} else {
361 			memcpy(newxprt->xp_ltaddr.buf, &sslocal, (size_t)sslocal.ss_len);
362 		}
363 	}
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(SVCXPRT *xprt)
373 {
374 
375 	return (XPRT_IDLE);
376 }
377 
378 static void
379 svc_vc_destroy(SVCXPRT *xprt)
380 {
381 	assert(xprt != NULL);
382 
383 	xprt_unregister(xprt);
384 	__svc_vc_dodestroy(xprt);
385 }
386 
387 static void
388 __svc_vc_dodestroy(SVCXPRT *xprt)
389 {
390 	struct cf_conn *cd;
391 	struct cf_rendezvous *r;
392 
393 	cd = (struct cf_conn *)xprt->xp_p1;
394 
395 	if (xprt->xp_fd != RPC_ANYFD)
396 		(void)_close(xprt->xp_fd);
397 	if (xprt->xp_port != 0) {
398 		/* a rendezvouser socket */
399 		r = (struct cf_rendezvous *)xprt->xp_p1;
400 		mem_free(r, sizeof (struct cf_rendezvous));
401 		xprt->xp_port = 0;
402 	} else {
403 		/* an actual connection socket */
404 		XDR_DESTROY(&(cd->xdrs));
405 		mem_free(cd, sizeof(struct cf_conn));
406 	}
407 	if (xprt->xp_rtaddr.buf)
408 		mem_free(xprt->xp_rtaddr.buf, xprt->xp_rtaddr.maxlen);
409 	if (xprt->xp_ltaddr.buf)
410 		mem_free(xprt->xp_ltaddr.buf, xprt->xp_ltaddr.maxlen);
411 	free(xprt->xp_tp);
412 	free(xprt->xp_netid);
413 	svc_xprt_free(xprt);
414 }
415 
416 /*ARGSUSED*/
417 static bool_t
418 svc_vc_control(SVCXPRT *xprt, const u_int rq, void *in)
419 {
420 	return (FALSE);
421 }
422 
423 static bool_t
424 svc_vc_rendezvous_control(SVCXPRT *xprt, const u_int rq, void *in)
425 {
426 	struct cf_rendezvous *cfp;
427 
428 	cfp = (struct cf_rendezvous *)xprt->xp_p1;
429 	if (cfp == NULL)
430 		return (FALSE);
431 	switch (rq) {
432 		case SVCGET_CONNMAXREC:
433 			*(int *)in = cfp->maxrec;
434 			break;
435 		case SVCSET_CONNMAXREC:
436 			cfp->maxrec = *(int *)in;
437 			break;
438 		default:
439 			return (FALSE);
440 	}
441 	return (TRUE);
442 }
443 
444 /*
445  * reads data from the tcp or uip connection.
446  * any error is fatal and the connection is closed.
447  * (And a read of zero bytes is a half closed stream => error.)
448  * All read operations timeout after 35 seconds.  A timeout is
449  * fatal for the connection.
450  */
451 static int
452 read_vc(void *xprtp, void *buf, int len)
453 {
454 	SVCXPRT *xprt;
455 	int sock;
456 	int milliseconds = 35 * 1000;
457 	struct pollfd pollfd;
458 	struct cf_conn *cfp;
459 
460 	xprt = (SVCXPRT *)xprtp;
461 	assert(xprt != NULL);
462 
463 	sock = xprt->xp_fd;
464 
465 	cfp = (struct cf_conn *)xprt->xp_p1;
466 
467 	if (cfp->nonblock) {
468 		len = _read(sock, buf, (size_t)len);
469 		if (len < 0) {
470 			if (errno == EAGAIN)
471 				len = 0;
472 			else
473 				goto fatal_err;
474 		}
475 		if (len != 0)
476 			gettimeofday(&cfp->last_recv_time, NULL);
477 		return len;
478 	}
479 
480 	do {
481 		pollfd.fd = sock;
482 		pollfd.events = POLLIN;
483 		pollfd.revents = 0;
484 		switch (_poll(&pollfd, 1, milliseconds)) {
485 		case -1:
486 			if (errno == EINTR)
487 				continue;
488 			/*FALLTHROUGH*/
489 		case 0:
490 			goto fatal_err;
491 
492 		default:
493 			break;
494 		}
495 	} while ((pollfd.revents & POLLIN) == 0);
496 
497 	if ((len = _read(sock, buf, (size_t)len)) > 0) {
498 		gettimeofday(&cfp->last_recv_time, NULL);
499 		return (len);
500 	}
501 
502 fatal_err:
503 	((struct cf_conn *)(xprt->xp_p1))->strm_stat = XPRT_DIED;
504 	return (-1);
505 }
506 
507 /*
508  * writes data to the tcp connection.
509  * Any error is fatal and the connection is closed.
510  */
511 static int
512 write_vc(void *xprtp, void *buf, int len)
513 {
514 	SVCXPRT *xprt;
515 	int i, cnt;
516 	struct cf_conn *cd;
517 	struct timeval tv0, tv1;
518 
519 	xprt = (SVCXPRT *)xprtp;
520 	assert(xprt != NULL);
521 
522 	cd = (struct cf_conn *)xprt->xp_p1;
523 
524 	if (cd->nonblock)
525 		gettimeofday(&tv0, NULL);
526 
527 	for (cnt = len; cnt > 0; cnt -= i, buf = (char *)buf + i) {
528 		i = _write(xprt->xp_fd, buf, (size_t)cnt);
529 		if (i  < 0) {
530 			if (errno != EAGAIN || !cd->nonblock) {
531 				cd->strm_stat = XPRT_DIED;
532 				return (-1);
533 			}
534 			if (cd->nonblock) {
535 				/*
536 				 * For non-blocking connections, do not
537 				 * take more than 2 seconds writing the
538 				 * data out.
539 				 *
540 				 * XXX 2 is an arbitrary amount.
541 				 */
542 				gettimeofday(&tv1, NULL);
543 				if (tv1.tv_sec - tv0.tv_sec >= 2) {
544 					cd->strm_stat = XPRT_DIED;
545 					return (-1);
546 				}
547 			}
548 			i = 0;
549 		}
550 	}
551 
552 	return (len);
553 }
554 
555 static enum xprt_stat
556 svc_vc_stat(SVCXPRT *xprt)
557 {
558 	struct cf_conn *cd;
559 
560 	assert(xprt != NULL);
561 
562 	cd = (struct cf_conn *)(xprt->xp_p1);
563 
564 	if (cd->strm_stat == XPRT_DIED)
565 		return (XPRT_DIED);
566 	if (! xdrrec_eof(&(cd->xdrs)))
567 		return (XPRT_MOREREQS);
568 	return (XPRT_IDLE);
569 }
570 
571 static bool_t
572 svc_vc_recv(SVCXPRT *xprt, struct rpc_msg *msg)
573 {
574 	struct cf_conn *cd;
575 	XDR *xdrs;
576 
577 	assert(xprt != NULL);
578 	assert(msg != NULL);
579 
580 	cd = (struct cf_conn *)(xprt->xp_p1);
581 	xdrs = &(cd->xdrs);
582 
583 	if (cd->nonblock) {
584 		if (!__xdrrec_getrec(xdrs, &cd->strm_stat, TRUE))
585 			return FALSE;
586 	} else {
587 		(void)xdrrec_skiprecord(xdrs);
588 	}
589 
590 	xdrs->x_op = XDR_DECODE;
591 	if (xdr_callmsg(xdrs, msg)) {
592 		cd->x_id = msg->rm_xid;
593 		return (TRUE);
594 	}
595 	cd->strm_stat = XPRT_DIED;
596 	return (FALSE);
597 }
598 
599 static bool_t
600 svc_vc_getargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
601 {
602 	struct cf_conn *cd;
603 
604 	assert(xprt != NULL);
605 	cd = (struct cf_conn *)(xprt->xp_p1);
606 	return (SVCAUTH_UNWRAP(&SVC_AUTH(xprt),
607 		&cd->xdrs, xdr_args, args_ptr));
608 }
609 
610 static bool_t
611 svc_vc_freeargs(SVCXPRT *xprt, xdrproc_t xdr_args, void *args_ptr)
612 {
613 	XDR *xdrs;
614 
615 	assert(xprt != NULL);
616 	/* args_ptr may be NULL */
617 
618 	xdrs = &(((struct cf_conn *)(xprt->xp_p1))->xdrs);
619 
620 	xdrs->x_op = XDR_FREE;
621 	return ((*xdr_args)(xdrs, args_ptr));
622 }
623 
624 static bool_t
625 svc_vc_reply(SVCXPRT *xprt, struct rpc_msg *msg)
626 {
627 	struct cf_conn *cd;
628 	XDR *xdrs;
629 	bool_t rstat;
630 	xdrproc_t xdr_proc;
631 	caddr_t xdr_where;
632 	u_int pos;
633 
634 	assert(xprt != NULL);
635 	assert(msg != NULL);
636 
637 	cd = (struct cf_conn *)(xprt->xp_p1);
638 	xdrs = &(cd->xdrs);
639 
640 	xdrs->x_op = XDR_ENCODE;
641 	msg->rm_xid = cd->x_id;
642 	rstat = TRUE;
643 	if (msg->rm_reply.rp_stat == MSG_ACCEPTED &&
644 	    msg->rm_reply.rp_acpt.ar_stat == SUCCESS) {
645 		xdr_proc = msg->acpted_rply.ar_results.proc;
646 		xdr_where = msg->acpted_rply.ar_results.where;
647 		msg->acpted_rply.ar_results.proc = (xdrproc_t) xdr_void;
648 		msg->acpted_rply.ar_results.where = NULL;
649 
650 		pos = XDR_GETPOS(xdrs);
651 		if (!xdr_replymsg(xdrs, msg) ||
652 		    !SVCAUTH_WRAP(&SVC_AUTH(xprt), xdrs, xdr_proc, xdr_where)) {
653 			XDR_SETPOS(xdrs, pos);
654 			rstat = FALSE;
655 		}
656 	} else {
657 		rstat = xdr_replymsg(xdrs, msg);
658 	}
659 
660 	if (rstat)
661 		(void)xdrrec_endofrecord(xdrs, TRUE);
662 
663 	return (rstat);
664 }
665 
666 static void
667 svc_vc_ops(SVCXPRT *xprt)
668 {
669 	static struct xp_ops ops;
670 	static struct xp_ops2 ops2;
671 
672 /* VARIABLES PROTECTED BY ops_lock: ops, ops2 */
673 
674 	mutex_lock(&ops_lock);
675 	if (ops.xp_recv == NULL) {
676 		ops.xp_recv = svc_vc_recv;
677 		ops.xp_stat = svc_vc_stat;
678 		ops.xp_getargs = svc_vc_getargs;
679 		ops.xp_reply = svc_vc_reply;
680 		ops.xp_freeargs = svc_vc_freeargs;
681 		ops.xp_destroy = svc_vc_destroy;
682 		ops2.xp_control = svc_vc_control;
683 	}
684 	xprt->xp_ops = &ops;
685 	xprt->xp_ops2 = &ops2;
686 	mutex_unlock(&ops_lock);
687 }
688 
689 static void
690 svc_vc_rendezvous_ops(SVCXPRT *xprt)
691 {
692 	static struct xp_ops ops;
693 	static struct xp_ops2 ops2;
694 
695 	mutex_lock(&ops_lock);
696 	if (ops.xp_recv == NULL) {
697 		ops.xp_recv = rendezvous_request;
698 		ops.xp_stat = rendezvous_stat;
699 		ops.xp_getargs =
700 		    (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort;
701 		ops.xp_reply =
702 		    (bool_t (*)(SVCXPRT *, struct rpc_msg *))abort;
703 		ops.xp_freeargs =
704 		    (bool_t (*)(SVCXPRT *, xdrproc_t, void *))abort,
705 		ops.xp_destroy = svc_vc_destroy;
706 		ops2.xp_control = svc_vc_rendezvous_control;
707 	}
708 	xprt->xp_ops = &ops;
709 	xprt->xp_ops2 = &ops2;
710 	mutex_unlock(&ops_lock);
711 }
712 
713 /*
714  * Get the effective UID of the sending process. Used by rpcbind, keyserv
715  * and rpc.yppasswdd on AF_LOCAL.
716  */
717 int
718 __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid) {
719 	int sock, ret;
720 	gid_t egid;
721 	uid_t euid;
722 	struct sockaddr *sa;
723 
724 	sock = transp->xp_fd;
725 	sa = (struct sockaddr *)transp->xp_rtaddr.buf;
726 	if (sa->sa_family == AF_LOCAL) {
727 		ret = getpeereid(sock, &euid, &egid);
728 		if (ret == 0)
729 			*uid = euid;
730 		return (ret);
731 	} else
732 		return (-1);
733 }
734 
735 /*
736  * Destroy xprts that have not have had any activity in 'timeout' seconds.
737  * If 'cleanblock' is true, blocking connections (the default) are also
738  * cleaned. If timeout is 0, the least active connection is picked.
739  */
740 bool_t
741 __svc_clean_idle(fd_set *fds, int timeout, bool_t cleanblock)
742 {
743 	int i, ncleaned;
744 	SVCXPRT *xprt, *least_active;
745 	struct timeval tv, tdiff, tmax;
746 	struct cf_conn *cd;
747 
748 	gettimeofday(&tv, NULL);
749 	tmax.tv_sec = tmax.tv_usec = 0;
750 	least_active = NULL;
751 	rwlock_wrlock(&svc_fd_lock);
752 	for (i = ncleaned = 0; i <= svc_maxfd; i++) {
753 		if (FD_ISSET(i, fds)) {
754 			xprt = __svc_xports[i];
755 			if (xprt == NULL || xprt->xp_ops == NULL ||
756 			    xprt->xp_ops->xp_recv != svc_vc_recv)
757 				continue;
758 			cd = (struct cf_conn *)xprt->xp_p1;
759 			if (!cleanblock && !cd->nonblock)
760 				continue;
761 			if (timeout == 0) {
762 				timersub(&tv, &cd->last_recv_time, &tdiff);
763 				if (timercmp(&tdiff, &tmax, >)) {
764 					tmax = tdiff;
765 					least_active = xprt;
766 				}
767 				continue;
768 			}
769 			if (tv.tv_sec - cd->last_recv_time.tv_sec > timeout) {
770 				__xprt_unregister_unlocked(xprt);
771 				__svc_vc_dodestroy(xprt);
772 				ncleaned++;
773 			}
774 		}
775 	}
776 	if (timeout == 0 && least_active != NULL) {
777 		__xprt_unregister_unlocked(least_active);
778 		__svc_vc_dodestroy(least_active);
779 		ncleaned++;
780 	}
781 	rwlock_unlock(&svc_fd_lock);
782 	return ncleaned > 0 ? TRUE : FALSE;
783 }
784