10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
21*132Srobinson  */
22*132Srobinson 
23*132Srobinson /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * pmap_prot.c
390Sstevel@tonic-gate  * Protocol for the local binder service, or pmap.
400Sstevel@tonic-gate  * All the pmap xdr routines here.
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include <rpc/types.h>
440Sstevel@tonic-gate #include <rpc/xdr.h>
450Sstevel@tonic-gate #include <rpc/pmap_prot.h>
460Sstevel@tonic-gate #include <rpc/pmap_rmt.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate bool_t
49*132Srobinson xdr_pmap(XDR *xdrs, struct pmap *objp)
500Sstevel@tonic-gate {
51*132Srobinson 	rpc_inline_t *buf;
520Sstevel@tonic-gate 
53*132Srobinson 	switch (xdrs->x_op) {
54*132Srobinson 	case XDR_ENCODE:
550Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 4 * BYTES_PER_XDR_UNIT);
560Sstevel@tonic-gate 		if (buf == NULL) {
57*132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_prog))
580Sstevel@tonic-gate 				return (FALSE);
59*132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_vers))
600Sstevel@tonic-gate 				return (FALSE);
61*132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_prot))
620Sstevel@tonic-gate 				return (FALSE);
63*132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_port))
640Sstevel@tonic-gate 				return (FALSE);
650Sstevel@tonic-gate 		} else {
660Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_prog);
670Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_vers);
680Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_prot);
690Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_port);
700Sstevel@tonic-gate 		}
710Sstevel@tonic-gate 		return (TRUE);
72*132Srobinson 	case XDR_DECODE:
730Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 4 * BYTES_PER_XDR_UNIT);
740Sstevel@tonic-gate 		if (buf == NULL) {
75*132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_prog))
760Sstevel@tonic-gate 				return (FALSE);
77*132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_vers))
780Sstevel@tonic-gate 				return (FALSE);
79*132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_prot))
800Sstevel@tonic-gate 				return (FALSE);
81*132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_port))
820Sstevel@tonic-gate 				return (FALSE);
830Sstevel@tonic-gate 		} else {
840Sstevel@tonic-gate 			objp->pm_prog = IXDR_GET_U_INT32(buf);
850Sstevel@tonic-gate 			objp->pm_vers = IXDR_GET_U_INT32(buf);
860Sstevel@tonic-gate 			objp->pm_prot = IXDR_GET_U_INT32(buf);
870Sstevel@tonic-gate 			objp->pm_port = IXDR_GET_U_INT32(buf);
880Sstevel@tonic-gate 		}
89*132Srobinson 		return (TRUE);
90*132Srobinson 	case XDR_FREE:
910Sstevel@tonic-gate 		return (TRUE);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 	return (FALSE);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * pmaplist_ptr implements a linked list.  The RPCL definition from
980Sstevel@tonic-gate  * pmap_prot.x is:
990Sstevel@tonic-gate  *
1000Sstevel@tonic-gate  * struct pm__list {
1010Sstevel@tonic-gate  * 	pmap		pml_map;
1020Sstevel@tonic-gate  *	struct pm__list *pml_next;
1030Sstevel@tonic-gate  * };
1040Sstevel@tonic-gate  * typedef pm__list *pmaplist_ptr;
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
1070Sstevel@tonic-gate  * there's any data behind the pointer, followed by the data (if any exists).
1080Sstevel@tonic-gate  * The boolean can be interpreted as ``more data follows me''; if FALSE then
1090Sstevel@tonic-gate  * nothing follows the boolean; if TRUE then the boolean is followed by an
1100Sstevel@tonic-gate  * actual struct pmap, and another pmaplist_ptr (declared in RPCL as "struct
1110Sstevel@tonic-gate  * pmaplist *").
1120Sstevel@tonic-gate  *
1130Sstevel@tonic-gate  * This could be implemented via the xdr_pointer type, though this would
1140Sstevel@tonic-gate  * result in one recursive call per element in the list.  Rather than do that
1150Sstevel@tonic-gate  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
1160Sstevel@tonic-gate  * serialize the pmap elements.
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate bool_t
119*132Srobinson xdr_pmaplist_ptr(XDR *xdrs, pmaplist_ptr *rp)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	/*
1220Sstevel@tonic-gate 	 * more_elements is pre-computed in case the direction is
1230Sstevel@tonic-gate 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
1240Sstevel@tonic-gate 	 * xdr_bool when the direction is XDR_DECODE.
1250Sstevel@tonic-gate 	 */
1260Sstevel@tonic-gate 	bool_t more_elements;
127*132Srobinson 	int freeing = (xdrs->x_op == XDR_FREE);
1280Sstevel@tonic-gate 	pmaplist_ptr next;
1290Sstevel@tonic-gate 	pmaplist_ptr next_copy;
1300Sstevel@tonic-gate 
131*132Srobinson 	for (;;) {
1320Sstevel@tonic-gate 		more_elements = (bool_t)(*rp != NULL);
133*132Srobinson 		if (!xdr_bool(xdrs, &more_elements))
1340Sstevel@tonic-gate 			return (FALSE);
135*132Srobinson 		if (!more_elements)
1360Sstevel@tonic-gate 			return (TRUE);  /* we are done */
1370Sstevel@tonic-gate 		/*
1380Sstevel@tonic-gate 		 * the unfortunate side effect of non-recursion is that in
1390Sstevel@tonic-gate 		 * the case of freeing we must remember the next object
1400Sstevel@tonic-gate 		 * before we free the current object ...
1410Sstevel@tonic-gate 		 */
1420Sstevel@tonic-gate 		if (freeing)
1430Sstevel@tonic-gate 			next = (*rp)->pml_next;
144*132Srobinson 		if (!xdr_reference(xdrs, (caddr_t *)rp,
145*132Srobinson 		    (uint_t)sizeof (struct pmaplist), (xdrproc_t)xdr_pmap))
1460Sstevel@tonic-gate 			return (FALSE);
1470Sstevel@tonic-gate 		if (freeing) {
1480Sstevel@tonic-gate 			next_copy = next;
1490Sstevel@tonic-gate 			rp = &next_copy;
1500Sstevel@tonic-gate 			/*
1510Sstevel@tonic-gate 			 * Note that in the subsequent iteration, next_copy
1520Sstevel@tonic-gate 			 * gets nulled out by the xdr_reference
1530Sstevel@tonic-gate 			 * but next itself survives.
1540Sstevel@tonic-gate 			 */
1550Sstevel@tonic-gate 		} else {
1560Sstevel@tonic-gate 			rp = &((*rp)->pml_next);
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 	/*NOTREACHED*/
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate /*
1630Sstevel@tonic-gate  * xdr_pmaplist() is specified to take a PMAPLIST **, but is identical in
1640Sstevel@tonic-gate  * functionality to xdr_pmaplist_ptr().
1650Sstevel@tonic-gate  */
1660Sstevel@tonic-gate bool_t
167*132Srobinson xdr_pmaplist(XDR *xdrs, PMAPLIST **rp)
1680Sstevel@tonic-gate {
169*132Srobinson 	return (xdr_pmaplist_ptr(xdrs, (pmaplist_ptr *)rp));
1700Sstevel@tonic-gate }
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate /*
1740Sstevel@tonic-gate  * XDR remote call arguments
1750Sstevel@tonic-gate  * written for XDR_ENCODE direction only
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate bool_t
178*132Srobinson xdr_rmtcallargs(XDR *xdrs, struct p_rmtcallargs *cap)
1790Sstevel@tonic-gate {
180*132Srobinson 	uint_t lenposition, argposition, position;
181*132Srobinson 	rpc_inline_t *buf;
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
1840Sstevel@tonic-gate 	if (buf == NULL) {
185*132Srobinson 		if (!xdr_u_int(xdrs, (uint_t *)&(cap->prog)) ||
186*132Srobinson 		    !xdr_u_int(xdrs, (uint_t *)&(cap->vers)) ||
187*132Srobinson 		    !xdr_u_int(xdrs, (uint_t *)&(cap->proc)))
1880Sstevel@tonic-gate 			return (FALSE);
1890Sstevel@tonic-gate 	} else {
1900Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, cap->prog);
1910Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, cap->vers);
1920Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, cap->proc);
1930Sstevel@tonic-gate 	}
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	/*
1960Sstevel@tonic-gate 	 * All the jugglery for just getting the size of the arguments
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	lenposition = XDR_GETPOS(xdrs);
199*132Srobinson 	if (!xdr_u_int(xdrs, &(cap->args.args_len)))
2000Sstevel@tonic-gate 		return (FALSE);
2010Sstevel@tonic-gate 	argposition = XDR_GETPOS(xdrs);
202*132Srobinson 	if (!(*cap->xdr_args)(xdrs, cap->args.args_val))
2030Sstevel@tonic-gate 		return (FALSE);
2040Sstevel@tonic-gate 	position = XDR_GETPOS(xdrs);
2050Sstevel@tonic-gate 	cap->args.args_len = position - argposition;
2060Sstevel@tonic-gate 	XDR_SETPOS(xdrs, lenposition);
207*132Srobinson 	if (!xdr_u_int(xdrs, &(cap->args.args_len)))
2080Sstevel@tonic-gate 		return (FALSE);
2090Sstevel@tonic-gate 	XDR_SETPOS(xdrs, position);
2100Sstevel@tonic-gate 	return (TRUE);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2130Sstevel@tonic-gate /*
2140Sstevel@tonic-gate  * XDR remote call results
2150Sstevel@tonic-gate  * written for XDR_DECODE direction only
2160Sstevel@tonic-gate  */
2170Sstevel@tonic-gate bool_t
218*132Srobinson xdr_rmtcallres(XDR *xdrs, struct p_rmtcallres *crp)
2190Sstevel@tonic-gate {
220*132Srobinson 	if (xdr_u_int(xdrs, (uint_t *)&crp->port) &&
221*132Srobinson 	    xdr_u_int(xdrs, &crp->res.res_len))
222*132Srobinson 		return ((*(crp->xdr_res))(xdrs, crp->res.res_val));
2230Sstevel@tonic-gate 	return (FALSE);
2240Sstevel@tonic-gate }
225