xref: /onnv-gate/usr/src/lib/libnsl/rpc/pmap_prot.c (revision 1219:f89f56c2d9ac)
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
21132Srobinson  */
22132Srobinson 
23132Srobinson /*
24*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
27*1219Sraf 
280Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
330Sstevel@tonic-gate  * California.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate /*
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 
43*1219Sraf #include "mt.h"
440Sstevel@tonic-gate #include <rpc/types.h>
450Sstevel@tonic-gate #include <rpc/xdr.h>
460Sstevel@tonic-gate #include <rpc/pmap_prot.h>
470Sstevel@tonic-gate #include <rpc/pmap_rmt.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate bool_t
xdr_pmap(XDR * xdrs,struct pmap * objp)50132Srobinson xdr_pmap(XDR *xdrs, struct pmap *objp)
510Sstevel@tonic-gate {
52132Srobinson 	rpc_inline_t *buf;
530Sstevel@tonic-gate 
54132Srobinson 	switch (xdrs->x_op) {
55132Srobinson 	case XDR_ENCODE:
560Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 4 * BYTES_PER_XDR_UNIT);
570Sstevel@tonic-gate 		if (buf == NULL) {
58132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_prog))
590Sstevel@tonic-gate 				return (FALSE);
60132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_vers))
610Sstevel@tonic-gate 				return (FALSE);
62132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_prot))
630Sstevel@tonic-gate 				return (FALSE);
64132Srobinson 			if (!XDR_PUTINT32(xdrs, (int32_t *)&objp->pm_port))
650Sstevel@tonic-gate 				return (FALSE);
660Sstevel@tonic-gate 		} else {
670Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_prog);
680Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_vers);
690Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_prot);
700Sstevel@tonic-gate 			IXDR_PUT_U_INT32(buf, objp->pm_port);
710Sstevel@tonic-gate 		}
720Sstevel@tonic-gate 		return (TRUE);
73132Srobinson 	case XDR_DECODE:
740Sstevel@tonic-gate 		buf = XDR_INLINE(xdrs, 4 * BYTES_PER_XDR_UNIT);
750Sstevel@tonic-gate 		if (buf == NULL) {
76132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_prog))
770Sstevel@tonic-gate 				return (FALSE);
78132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_vers))
790Sstevel@tonic-gate 				return (FALSE);
80132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_prot))
810Sstevel@tonic-gate 				return (FALSE);
82132Srobinson 			if (!XDR_GETINT32(xdrs, (int32_t *)&objp->pm_port))
830Sstevel@tonic-gate 				return (FALSE);
840Sstevel@tonic-gate 		} else {
850Sstevel@tonic-gate 			objp->pm_prog = IXDR_GET_U_INT32(buf);
860Sstevel@tonic-gate 			objp->pm_vers = IXDR_GET_U_INT32(buf);
870Sstevel@tonic-gate 			objp->pm_prot = IXDR_GET_U_INT32(buf);
880Sstevel@tonic-gate 			objp->pm_port = IXDR_GET_U_INT32(buf);
890Sstevel@tonic-gate 		}
90132Srobinson 		return (TRUE);
91132Srobinson 	case XDR_FREE:
920Sstevel@tonic-gate 		return (TRUE);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 	return (FALSE);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * pmaplist_ptr implements a linked list.  The RPCL definition from
990Sstevel@tonic-gate  * pmap_prot.x is:
1000Sstevel@tonic-gate  *
1010Sstevel@tonic-gate  * struct pm__list {
1020Sstevel@tonic-gate  * 	pmap		pml_map;
1030Sstevel@tonic-gate  *	struct pm__list *pml_next;
1040Sstevel@tonic-gate  * };
1050Sstevel@tonic-gate  * typedef pm__list *pmaplist_ptr;
1060Sstevel@tonic-gate  *
1070Sstevel@tonic-gate  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
1080Sstevel@tonic-gate  * there's any data behind the pointer, followed by the data (if any exists).
1090Sstevel@tonic-gate  * The boolean can be interpreted as ``more data follows me''; if FALSE then
1100Sstevel@tonic-gate  * nothing follows the boolean; if TRUE then the boolean is followed by an
1110Sstevel@tonic-gate  * actual struct pmap, and another pmaplist_ptr (declared in RPCL as "struct
1120Sstevel@tonic-gate  * pmaplist *").
1130Sstevel@tonic-gate  *
1140Sstevel@tonic-gate  * This could be implemented via the xdr_pointer type, though this would
1150Sstevel@tonic-gate  * result in one recursive call per element in the list.  Rather than do that
1160Sstevel@tonic-gate  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
1170Sstevel@tonic-gate  * serialize the pmap elements.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate bool_t
xdr_pmaplist_ptr(XDR * xdrs,pmaplist_ptr * rp)120132Srobinson xdr_pmaplist_ptr(XDR *xdrs, pmaplist_ptr *rp)
1210Sstevel@tonic-gate {
1220Sstevel@tonic-gate 	/*
1230Sstevel@tonic-gate 	 * more_elements is pre-computed in case the direction is
1240Sstevel@tonic-gate 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
1250Sstevel@tonic-gate 	 * xdr_bool when the direction is XDR_DECODE.
1260Sstevel@tonic-gate 	 */
1270Sstevel@tonic-gate 	bool_t more_elements;
128132Srobinson 	int freeing = (xdrs->x_op == XDR_FREE);
1290Sstevel@tonic-gate 	pmaplist_ptr next;
1300Sstevel@tonic-gate 	pmaplist_ptr next_copy;
1310Sstevel@tonic-gate 
132132Srobinson 	for (;;) {
1330Sstevel@tonic-gate 		more_elements = (bool_t)(*rp != NULL);
134132Srobinson 		if (!xdr_bool(xdrs, &more_elements))
1350Sstevel@tonic-gate 			return (FALSE);
136132Srobinson 		if (!more_elements)
1370Sstevel@tonic-gate 			return (TRUE);  /* we are done */
1380Sstevel@tonic-gate 		/*
1390Sstevel@tonic-gate 		 * the unfortunate side effect of non-recursion is that in
1400Sstevel@tonic-gate 		 * the case of freeing we must remember the next object
1410Sstevel@tonic-gate 		 * before we free the current object ...
1420Sstevel@tonic-gate 		 */
1430Sstevel@tonic-gate 		if (freeing)
1440Sstevel@tonic-gate 			next = (*rp)->pml_next;
145132Srobinson 		if (!xdr_reference(xdrs, (caddr_t *)rp,
146132Srobinson 		    (uint_t)sizeof (struct pmaplist), (xdrproc_t)xdr_pmap))
1470Sstevel@tonic-gate 			return (FALSE);
1480Sstevel@tonic-gate 		if (freeing) {
1490Sstevel@tonic-gate 			next_copy = next;
1500Sstevel@tonic-gate 			rp = &next_copy;
1510Sstevel@tonic-gate 			/*
1520Sstevel@tonic-gate 			 * Note that in the subsequent iteration, next_copy
1530Sstevel@tonic-gate 			 * gets nulled out by the xdr_reference
1540Sstevel@tonic-gate 			 * but next itself survives.
1550Sstevel@tonic-gate 			 */
1560Sstevel@tonic-gate 		} else {
1570Sstevel@tonic-gate 			rp = &((*rp)->pml_next);
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 	/*NOTREACHED*/
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate /*
1640Sstevel@tonic-gate  * xdr_pmaplist() is specified to take a PMAPLIST **, but is identical in
1650Sstevel@tonic-gate  * functionality to xdr_pmaplist_ptr().
1660Sstevel@tonic-gate  */
1670Sstevel@tonic-gate bool_t
xdr_pmaplist(XDR * xdrs,PMAPLIST ** rp)168132Srobinson xdr_pmaplist(XDR *xdrs, PMAPLIST **rp)
1690Sstevel@tonic-gate {
170132Srobinson 	return (xdr_pmaplist_ptr(xdrs, (pmaplist_ptr *)rp));
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate /*
1750Sstevel@tonic-gate  * XDR remote call arguments
1760Sstevel@tonic-gate  * written for XDR_ENCODE direction only
1770Sstevel@tonic-gate  */
1780Sstevel@tonic-gate bool_t
xdr_rmtcallargs(XDR * xdrs,struct p_rmtcallargs * cap)179132Srobinson xdr_rmtcallargs(XDR *xdrs, struct p_rmtcallargs *cap)
1800Sstevel@tonic-gate {
181132Srobinson 	uint_t lenposition, argposition, position;
182132Srobinson 	rpc_inline_t *buf;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
1850Sstevel@tonic-gate 	if (buf == NULL) {
186132Srobinson 		if (!xdr_u_int(xdrs, (uint_t *)&(cap->prog)) ||
187132Srobinson 		    !xdr_u_int(xdrs, (uint_t *)&(cap->vers)) ||
188132Srobinson 		    !xdr_u_int(xdrs, (uint_t *)&(cap->proc)))
1890Sstevel@tonic-gate 			return (FALSE);
1900Sstevel@tonic-gate 	} else {
1910Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, cap->prog);
1920Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, cap->vers);
1930Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, cap->proc);
1940Sstevel@tonic-gate 	}
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * All the jugglery for just getting the size of the arguments
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	lenposition = XDR_GETPOS(xdrs);
200132Srobinson 	if (!xdr_u_int(xdrs, &(cap->args.args_len)))
2010Sstevel@tonic-gate 		return (FALSE);
2020Sstevel@tonic-gate 	argposition = XDR_GETPOS(xdrs);
203132Srobinson 	if (!(*cap->xdr_args)(xdrs, cap->args.args_val))
2040Sstevel@tonic-gate 		return (FALSE);
2050Sstevel@tonic-gate 	position = XDR_GETPOS(xdrs);
2060Sstevel@tonic-gate 	cap->args.args_len = position - argposition;
2070Sstevel@tonic-gate 	XDR_SETPOS(xdrs, lenposition);
208132Srobinson 	if (!xdr_u_int(xdrs, &(cap->args.args_len)))
2090Sstevel@tonic-gate 		return (FALSE);
2100Sstevel@tonic-gate 	XDR_SETPOS(xdrs, position);
2110Sstevel@tonic-gate 	return (TRUE);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate /*
2150Sstevel@tonic-gate  * XDR remote call results
2160Sstevel@tonic-gate  * written for XDR_DECODE direction only
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate bool_t
xdr_rmtcallres(XDR * xdrs,struct p_rmtcallres * crp)219132Srobinson xdr_rmtcallres(XDR *xdrs, struct p_rmtcallres *crp)
2200Sstevel@tonic-gate {
221132Srobinson 	if (xdr_u_int(xdrs, (uint_t *)&crp->port) &&
222132Srobinson 	    xdr_u_int(xdrs, &crp->res.res_len))
223132Srobinson 		return ((*(crp->xdr_res))(xdrs, crp->res.res_val));
2240Sstevel@tonic-gate 	return (FALSE);
2250Sstevel@tonic-gate }
226