Blame src/lib/rpc/pmap_prot2.c

Packit fd8b60
/* @(#)pmap_prot2.c	2.1 88/07/29 4.0 RPCSRC */
Packit fd8b60
/*
Packit fd8b60
 * Copyright (c) 2010, Oracle America, Inc.
Packit fd8b60
 *
Packit fd8b60
 * All rights reserved.
Packit fd8b60
 *
Packit fd8b60
 * Redistribution and use in source and binary forms, with or without
Packit fd8b60
 * modification, are permitted provided that the following conditions are met:
Packit fd8b60
 *
Packit fd8b60
 *     * Redistributions of source code must retain the above copyright
Packit fd8b60
 *       notice, this list of conditions and the following disclaimer.
Packit fd8b60
 *
Packit fd8b60
 *     * Redistributions in binary form must reproduce the above copyright
Packit fd8b60
 *       notice, this list of conditions and the following disclaimer in
Packit fd8b60
 *       the documentation and/or other materials provided with the
Packit fd8b60
 *       distribution.
Packit fd8b60
 *
Packit fd8b60
 *     * Neither the name of the "Oracle America, Inc." nor the names of
Packit fd8b60
 *       its contributors may be used to endorse or promote products
Packit fd8b60
 *       derived from this software without specific prior written permission.
Packit fd8b60
 *
Packit fd8b60
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
Packit fd8b60
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Packit fd8b60
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
Packit fd8b60
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit fd8b60
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit fd8b60
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
Packit fd8b60
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit fd8b60
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit fd8b60
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit fd8b60
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
Packit fd8b60
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit fd8b60
 */
Packit fd8b60
#if !defined(lint) && defined(SCCSIDS)
Packit fd8b60
static char sccsid[] = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
Packit fd8b60
#endif
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * pmap_prot2.c
Packit fd8b60
 * Protocol for the local binder service, or pmap.
Packit fd8b60
 */
Packit fd8b60
Packit fd8b60
#include <gssrpc/types.h>
Packit fd8b60
#include <gssrpc/xdr.h>
Packit fd8b60
#include <gssrpc/pmap_prot.h>
Packit fd8b60
Packit fd8b60
Packit fd8b60
/*
Packit fd8b60
 * What is going on with linked lists? (!)
Packit fd8b60
 * First recall the link list declaration from pmap_prot.h:
Packit fd8b60
 *
Packit fd8b60
 * struct pmaplist {
Packit fd8b60
 *	struct pmap pml_map;
Packit fd8b60
 *	struct pmaplist *pml_map;
Packit fd8b60
 * };
Packit fd8b60
 *
Packit fd8b60
 * Compare that declaration with a corresponding xdr declaration that
Packit fd8b60
 * is (a) pointer-less, and (b) recursive:
Packit fd8b60
 *
Packit fd8b60
 * typedef union switch (bool_t) {
Packit fd8b60
 *
Packit fd8b60
 *	case TRUE: struct {
Packit fd8b60
 *		struct pmap;
Packit fd8b60
 * 		pmaplist_t foo;
Packit fd8b60
 *	};
Packit fd8b60
 *
Packit fd8b60
 *	case FALSE: struct {};
Packit fd8b60
 * } pmaplist_t;
Packit fd8b60
 *
Packit fd8b60
 * Notice that the xdr declaration has no nxt pointer while
Packit fd8b60
 * the C declaration has no bool_t variable.  The bool_t can be
Packit fd8b60
 * interpreted as ``more data follows me''; if FALSE then nothing
Packit fd8b60
 * follows this bool_t; if TRUE then the bool_t is followed by
Packit fd8b60
 * an actual struct pmap, and then (recursively) by the
Packit fd8b60
 * xdr union, pamplist_t.
Packit fd8b60
 *
Packit fd8b60
 * This could be implemented via the xdr_union primitive, though this
Packit fd8b60
 * would cause a one recursive call per element in the list.  Rather than do
Packit fd8b60
 * that we can ``unwind'' the recursion
Packit fd8b60
 * into a while loop and do the union arms in-place.
Packit fd8b60
 *
Packit fd8b60
 * The head of the list is what the C programmer wishes to past around
Packit fd8b60
 * the net, yet is the data that the pointer points to which is interesting;
Packit fd8b60
 * this sounds like a job for xdr_reference!
Packit fd8b60
 */
Packit fd8b60
bool_t
Packit fd8b60
xdr_pmaplist(XDR *xdrs, struct pmaplist **rp)
Packit fd8b60
{
Packit fd8b60
	/*
Packit fd8b60
	 * more_elements is pre-computed in case the direction is
Packit fd8b60
	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
Packit fd8b60
	 * xdr_bool when the direction is XDR_DECODE.
Packit fd8b60
	 */
Packit fd8b60
	bool_t more_elements;
Packit fd8b60
	int freeing = (xdrs->x_op == XDR_FREE);
Packit fd8b60
	struct pmaplist **next = NULL;
Packit fd8b60
Packit fd8b60
	while (TRUE) {
Packit fd8b60
		more_elements = (bool_t)(*rp != NULL);
Packit fd8b60
		if (! xdr_bool(xdrs, &more_elements))
Packit fd8b60
			return (FALSE);
Packit fd8b60
		if (! more_elements)
Packit fd8b60
			return (TRUE);  /* we are done */
Packit fd8b60
		/*
Packit fd8b60
		 * the unfortunate side effect of non-recursion is that in
Packit fd8b60
		 * the case of freeing we must remember the next object
Packit fd8b60
		 * before we free the current object ...
Packit fd8b60
		 */
Packit fd8b60
		if (freeing)
Packit fd8b60
			next = &((*rp)->pml_next);
Packit fd8b60
		if (! xdr_reference(xdrs, (caddr_t *)rp,
Packit fd8b60
		    (u_int)sizeof(struct pmaplist), xdr_pmap))
Packit fd8b60
			return (FALSE);
Packit fd8b60
		rp = (freeing) ? next : &((*rp)->pml_next);
Packit fd8b60
	}
Packit fd8b60
}