xref: /illumos-gate/usr/src/lib/libnsl/rpc/rpcb_prot.c (revision cfa354e4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27e8031f0aSraf 
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
327c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
337c478bd9Sstevel@tonic-gate  * California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * XDR routines for the rpcbinder version 3.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
40e8031f0aSraf #include "mt.h"
417c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
427c478bd9Sstevel@tonic-gate #include <rpc/types.h>
437c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
447c478bd9Sstevel@tonic-gate #include <rpc/rpcb_prot.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate bool_t
xdr_rpcb(XDR * xdrs,RPCB * objp)4861961e0fSrobinson xdr_rpcb(XDR *xdrs, RPCB *objp)
497c478bd9Sstevel@tonic-gate {
5061961e0fSrobinson 	if (!xdr_u_int(xdrs, (uint_t *)&objp->r_prog))
517c478bd9Sstevel@tonic-gate 		return (FALSE);
5261961e0fSrobinson 	if (!xdr_u_int(xdrs, (uint_t *)&objp->r_vers))
537c478bd9Sstevel@tonic-gate 		return (FALSE);
5461961e0fSrobinson 	if (!xdr_string(xdrs, &objp->r_netid, ~0))
557c478bd9Sstevel@tonic-gate 		return (FALSE);
5661961e0fSrobinson 	if (!xdr_string(xdrs, &objp->r_addr, ~0))
577c478bd9Sstevel@tonic-gate 		return (FALSE);
5861961e0fSrobinson 	return (xdr_string(xdrs, &objp->r_owner, ~0));
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate  * rpcblist_ptr implements a linked list.  The RPCL definition from
637c478bd9Sstevel@tonic-gate  * rpcb_prot.x is:
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * struct rpcblist {
667c478bd9Sstevel@tonic-gate  * 	rpcb		rpcb_map;
677c478bd9Sstevel@tonic-gate  *	struct rpcblist *rpcb_next;
687c478bd9Sstevel@tonic-gate  * };
697c478bd9Sstevel@tonic-gate  * typedef rpcblist *rpcblist_ptr;
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
727c478bd9Sstevel@tonic-gate  * there's any data behind the pointer, followed by the data (if any exists).
737c478bd9Sstevel@tonic-gate  * The boolean can be interpreted as ``more data follows me''; if FALSE then
747c478bd9Sstevel@tonic-gate  * nothing follows the boolean; if TRUE then the boolean is followed by an
757c478bd9Sstevel@tonic-gate  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
767c478bd9Sstevel@tonic-gate  * rpcblist *").
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * This could be implemented via the xdr_pointer type, though this would
797c478bd9Sstevel@tonic-gate  * result in one recursive call per element in the list.  Rather than do that
807c478bd9Sstevel@tonic-gate  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
817c478bd9Sstevel@tonic-gate  * serialize the rpcb elements.
827c478bd9Sstevel@tonic-gate  */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate bool_t
xdr_rpcblist_ptr(XDR * xdrs,rpcblist_ptr * rp)8561961e0fSrobinson xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate 	/*
887c478bd9Sstevel@tonic-gate 	 * more_elements is pre-computed in case the direction is
897c478bd9Sstevel@tonic-gate 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
907c478bd9Sstevel@tonic-gate 	 * xdr_bool when the direction is XDR_DECODE.
917c478bd9Sstevel@tonic-gate 	 */
927c478bd9Sstevel@tonic-gate 	bool_t more_elements;
9361961e0fSrobinson 	int freeing = (xdrs->x_op == XDR_FREE);
947c478bd9Sstevel@tonic-gate 	rpcblist_ptr next;
957c478bd9Sstevel@tonic-gate 	rpcblist_ptr next_copy;
967c478bd9Sstevel@tonic-gate 
9761961e0fSrobinson 	for (;;) {
987c478bd9Sstevel@tonic-gate 		more_elements = (bool_t)(*rp != NULL);
9961961e0fSrobinson 		if (!xdr_bool(xdrs, &more_elements))
1007c478bd9Sstevel@tonic-gate 			return (FALSE);
10161961e0fSrobinson 		if (!more_elements)
1027c478bd9Sstevel@tonic-gate 			return (TRUE);  /* we are done */
1037c478bd9Sstevel@tonic-gate 		/*
1047c478bd9Sstevel@tonic-gate 		 * the unfortunate side effect of non-recursion is that in
1057c478bd9Sstevel@tonic-gate 		 * the case of freeing we must remember the next object
1067c478bd9Sstevel@tonic-gate 		 * before we free the current object ...
1077c478bd9Sstevel@tonic-gate 		 */
1087c478bd9Sstevel@tonic-gate 		if (freeing)
1097c478bd9Sstevel@tonic-gate 			next = (*rp)->rpcb_next;
11061961e0fSrobinson 		if (!xdr_reference(xdrs, (caddr_t *)rp,
11161961e0fSrobinson 		    (uint_t)sizeof (rpcblist), (xdrproc_t)xdr_rpcb))
1127c478bd9Sstevel@tonic-gate 			return (FALSE);
1137c478bd9Sstevel@tonic-gate 		if (freeing) {
1147c478bd9Sstevel@tonic-gate 			next_copy = next;
1157c478bd9Sstevel@tonic-gate 			rp = &next_copy;
1167c478bd9Sstevel@tonic-gate 			/*
1177c478bd9Sstevel@tonic-gate 			 * Note that in the subsequent iteration, next_copy
1187c478bd9Sstevel@tonic-gate 			 * gets nulled out by the xdr_reference
1197c478bd9Sstevel@tonic-gate 			 * but next itself survives.
1207c478bd9Sstevel@tonic-gate 			 */
1217c478bd9Sstevel@tonic-gate 		} else {
1227c478bd9Sstevel@tonic-gate 			rp = &((*rp)->rpcb_next);
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
1307c478bd9Sstevel@tonic-gate  * functionality to xdr_rpcblist_ptr().
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate bool_t
xdr_rpcblist(XDR * xdrs,RPCBLIST ** rp)13361961e0fSrobinson xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
1347c478bd9Sstevel@tonic-gate {
13561961e0fSrobinson 	return (xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp));
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate bool_t
xdr_rpcb_entry(XDR * xdrs,rpcb_entry * objp)14061961e0fSrobinson xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
1417c478bd9Sstevel@tonic-gate {
14261961e0fSrobinson 	if (!xdr_string(xdrs, &objp->r_maddr, ~0))
1437c478bd9Sstevel@tonic-gate 		return (FALSE);
14461961e0fSrobinson 	if (!xdr_string(xdrs, &objp->r_nc_netid, ~0))
1457c478bd9Sstevel@tonic-gate 		return (FALSE);
14661961e0fSrobinson 	if (!xdr_u_int(xdrs, &objp->r_nc_semantics))
1477c478bd9Sstevel@tonic-gate 		return (FALSE);
14861961e0fSrobinson 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, ~0))
1497c478bd9Sstevel@tonic-gate 		return (FALSE);
15061961e0fSrobinson 	return (xdr_string(xdrs, &objp->r_nc_proto, ~0));
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate bool_t
xdr_rpcb_entry_list_ptr(XDR * xdrs,rpcb_entry_list_ptr * rp)15461961e0fSrobinson xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate 	/*
1577c478bd9Sstevel@tonic-gate 	 * more_elements is pre-computed in case the direction is
1587c478bd9Sstevel@tonic-gate 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
1597c478bd9Sstevel@tonic-gate 	 * xdr_bool when the direction is XDR_DECODE.
1607c478bd9Sstevel@tonic-gate 	 */
1617c478bd9Sstevel@tonic-gate 	bool_t more_elements;
16261961e0fSrobinson 	int freeing = (xdrs->x_op == XDR_FREE);
1637c478bd9Sstevel@tonic-gate 	rpcb_entry_list_ptr next;
1647c478bd9Sstevel@tonic-gate 	rpcb_entry_list_ptr next_copy;
1657c478bd9Sstevel@tonic-gate 
16661961e0fSrobinson 	for (;;) {
1677c478bd9Sstevel@tonic-gate 		more_elements = (bool_t)(*rp != NULL);
16861961e0fSrobinson 		if (!xdr_bool(xdrs, &more_elements))
1697c478bd9Sstevel@tonic-gate 			return (FALSE);
17061961e0fSrobinson 		if (!more_elements)
1717c478bd9Sstevel@tonic-gate 			return (TRUE);  /* we are done */
1727c478bd9Sstevel@tonic-gate 		/*
1737c478bd9Sstevel@tonic-gate 		 * the unfortunate side effect of non-recursion is that in
1747c478bd9Sstevel@tonic-gate 		 * the case of freeing we must remember the next object
1757c478bd9Sstevel@tonic-gate 		 * before we free the current object ...
1767c478bd9Sstevel@tonic-gate 		 */
1777c478bd9Sstevel@tonic-gate 		if (freeing)
1787c478bd9Sstevel@tonic-gate 			next = (*rp)->rpcb_entry_next;
17961961e0fSrobinson 		if (!xdr_reference(xdrs, (caddr_t *)rp,
18061961e0fSrobinson 		    (uint_t)sizeof (rpcb_entry_list),
181*cfa354e4SMarcel Telka 		    (xdrproc_t)xdr_rpcb_entry))
1827c478bd9Sstevel@tonic-gate 			return (FALSE);
1837c478bd9Sstevel@tonic-gate 		if (freeing) {
1847c478bd9Sstevel@tonic-gate 			next_copy = next;
1857c478bd9Sstevel@tonic-gate 			rp = &next_copy;
1867c478bd9Sstevel@tonic-gate 			/*
1877c478bd9Sstevel@tonic-gate 			 * Note that in the subsequent iteration, next_copy
1887c478bd9Sstevel@tonic-gate 			 * gets nulled out by the xdr_reference
1897c478bd9Sstevel@tonic-gate 			 * but next itself survives.
1907c478bd9Sstevel@tonic-gate 			 */
1917c478bd9Sstevel@tonic-gate 		} else {
1927c478bd9Sstevel@tonic-gate 			rp = &((*rp)->rpcb_entry_next);
1937c478bd9Sstevel@tonic-gate 		}
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * XDR remote call arguments
2007c478bd9Sstevel@tonic-gate  * written for XDR_ENCODE direction only
2017c478bd9Sstevel@tonic-gate  */
2027c478bd9Sstevel@tonic-gate bool_t
xdr_rpcb_rmtcallargs(XDR * xdrs,struct r_rpcb_rmtcallargs * objp)20361961e0fSrobinson xdr_rpcb_rmtcallargs(XDR *xdrs, struct r_rpcb_rmtcallargs *objp)
2047c478bd9Sstevel@tonic-gate {
20561961e0fSrobinson 	uint_t lenposition, argposition, position;
20661961e0fSrobinson 	rpc_inline_t *buf;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
2097c478bd9Sstevel@tonic-gate 	if (buf == NULL) {
21061961e0fSrobinson 		if (!xdr_u_int(xdrs, (uint_t *)&objp->prog))
2117c478bd9Sstevel@tonic-gate 			return (FALSE);
21261961e0fSrobinson 		if (!xdr_u_int(xdrs, (uint_t *)&objp->vers))
2137c478bd9Sstevel@tonic-gate 			return (FALSE);
21461961e0fSrobinson 		if (!xdr_u_int(xdrs, (uint_t *)&objp->proc))
2157c478bd9Sstevel@tonic-gate 			return (FALSE);
2167c478bd9Sstevel@tonic-gate 	} else {
2177c478bd9Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, objp->prog);
2187c478bd9Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, objp->vers);
2197c478bd9Sstevel@tonic-gate 		IXDR_PUT_U_INT32(buf, objp->proc);
2207c478bd9Sstevel@tonic-gate 	}
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * All the jugglery for just getting the size of the arguments
2247c478bd9Sstevel@tonic-gate 	 */
2257c478bd9Sstevel@tonic-gate 	lenposition = XDR_GETPOS(xdrs);
22661961e0fSrobinson 	if (!xdr_u_int(xdrs, &(objp->args.args_len)))
2277c478bd9Sstevel@tonic-gate 		return (FALSE);
2287c478bd9Sstevel@tonic-gate 	argposition = XDR_GETPOS(xdrs);
22961961e0fSrobinson 	if (!(*objp->xdr_args)(xdrs, objp->args.args_val))
2307c478bd9Sstevel@tonic-gate 		return (FALSE);
2317c478bd9Sstevel@tonic-gate 	position = XDR_GETPOS(xdrs);
23261961e0fSrobinson 	objp->args.args_len = (uint_t)position - (uint_t)argposition;
2337c478bd9Sstevel@tonic-gate 	XDR_SETPOS(xdrs, lenposition);
23461961e0fSrobinson 	if (!xdr_u_int(xdrs, &(objp->args.args_len)))
2357c478bd9Sstevel@tonic-gate 		return (FALSE);
2367c478bd9Sstevel@tonic-gate 	XDR_SETPOS(xdrs, position);
2377c478bd9Sstevel@tonic-gate 	return (TRUE);
2387c478bd9Sstevel@tonic-gate }
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate /*
2417c478bd9Sstevel@tonic-gate  * XDR remote call results
2427c478bd9Sstevel@tonic-gate  * written for XDR_DECODE direction only
2437c478bd9Sstevel@tonic-gate  */
2447c478bd9Sstevel@tonic-gate bool_t
xdr_rpcb_rmtcallres(XDR * xdrs,struct r_rpcb_rmtcallres * objp)24561961e0fSrobinson xdr_rpcb_rmtcallres(XDR *xdrs, struct r_rpcb_rmtcallres *objp)
2467c478bd9Sstevel@tonic-gate {
24761961e0fSrobinson 	if (!xdr_string(xdrs, &objp->addr, ~0))
2487c478bd9Sstevel@tonic-gate 		return (FALSE);
24961961e0fSrobinson 	if (!xdr_u_int(xdrs, &objp->results.results_len))
2507c478bd9Sstevel@tonic-gate 		return (FALSE);
25161961e0fSrobinson 	return ((*(objp->xdr_res))(xdrs, objp->results.results_val));
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate bool_t
xdr_netbuf(XDR * xdrs,struct netbuf * objp)25561961e0fSrobinson xdr_netbuf(XDR *xdrs, struct netbuf *objp)
2567c478bd9Sstevel@tonic-gate {
257*cfa354e4SMarcel Telka 	bool_t res;
258*cfa354e4SMarcel Telka 
259*cfa354e4SMarcel Telka 	/*
260*cfa354e4SMarcel Telka 	 * Save the passed in maxlen value and buf pointer.  We might
261*cfa354e4SMarcel Telka 	 * need them later.
262*cfa354e4SMarcel Telka 	 */
263*cfa354e4SMarcel Telka 	uint_t maxlen_save = objp->maxlen;
264*cfa354e4SMarcel Telka 	void *buf_save = objp->buf;
265*cfa354e4SMarcel Telka 
266*cfa354e4SMarcel Telka 	if (!xdr_u_int(xdrs, &objp->maxlen))
2677c478bd9Sstevel@tonic-gate 		return (FALSE);
268*cfa354e4SMarcel Telka 
269*cfa354e4SMarcel Telka 	/*
270*cfa354e4SMarcel Telka 	 * We need to free maxlen, not len, so do it explicitly now.
271*cfa354e4SMarcel Telka 	 */
272*cfa354e4SMarcel Telka 	if (xdrs->x_op == XDR_FREE)
273*cfa354e4SMarcel Telka 		return (xdr_bytes(xdrs, &objp->buf, &objp->maxlen,
274*cfa354e4SMarcel Telka 		    objp->maxlen));
275*cfa354e4SMarcel Telka 
276*cfa354e4SMarcel Telka 	/*
277*cfa354e4SMarcel Telka 	 * If we're decoding and the caller has already allocated a
278*cfa354e4SMarcel Telka 	 * buffer restore the maxlen value since the decoded value
279*cfa354e4SMarcel Telka 	 * doesn't apply to the caller's buffer.  xdr_bytes() will
280*cfa354e4SMarcel Telka 	 * return an error if the buffer isn't big enough.
281*cfa354e4SMarcel Telka 	 */
282*cfa354e4SMarcel Telka 	if (xdrs->x_op == XDR_DECODE && objp->buf != NULL)
283*cfa354e4SMarcel Telka 		objp->maxlen = maxlen_save;
284*cfa354e4SMarcel Telka 
285*cfa354e4SMarcel Telka 	res = xdr_bytes(xdrs, &objp->buf, &objp->len, objp->maxlen);
286*cfa354e4SMarcel Telka 
287*cfa354e4SMarcel Telka 	/*
288*cfa354e4SMarcel Telka 	 * If we are decoding and the buffer was allocated in the
289*cfa354e4SMarcel Telka 	 * xdr_bytes() function we need to set maxlen properly to
290*cfa354e4SMarcel Telka 	 * follow the netbuf semantics.
291*cfa354e4SMarcel Telka 	 */
292*cfa354e4SMarcel Telka 	if (xdrs->x_op == XDR_DECODE && objp->buf != buf_save)
293*cfa354e4SMarcel Telka 		objp->maxlen = objp->len;
294*cfa354e4SMarcel Telka 
295*cfa354e4SMarcel Telka 	return (res);
2967c478bd9Sstevel@tonic-gate }
297