Home | History | Annotate | Line # | Download | only in rpc
      1 /*	$NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2010, Oracle America, Inc.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are
      8  * met:
      9  *
     10  *     * Redistributions of source code must retain the above copyright
     11  *       notice, this list of conditions and the following disclaimer.
     12  *     * Redistributions in binary form must reproduce the above
     13  *       copyright notice, this list of conditions and the following
     14  *       disclaimer in the documentation and/or other materials
     15  *       provided with the distribution.
     16  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  *       contributors may be used to endorse or promote products derived
     18  *       from this software without specific prior written permission.
     19  *
     20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #if defined(LIBC_SCCS) && !defined(lint)
     36 #if 0
     37 static char *sccsid = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
     38 static char *sccsid = "@(#)pmap_prot2.c	2.1 88/07/29 4.0 RPCSRC";
     39 #else
     40 __RCSID("$NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $");
     41 #endif
     42 #endif
     43 
     44 /*
     45  * pmap_prot2.c
     46  * Protocol for the local binder service, or pmap.
     47  *
     48  * Copyright (C) 1984, Sun Microsystems, Inc.
     49  */
     50 
     51 #include "namespace.h"
     52 
     53 #include <assert.h>
     54 
     55 #include <rpc/types.h>
     56 #include <rpc/xdr.h>
     57 #include <rpc/pmap_prot.h>
     58 
     59 #ifdef __weak_alias
     60 __weak_alias(xdr_pmaplist,_xdr_pmaplist)
     61 #endif
     62 
     63 /*
     64  * What is going on with linked lists? (!)
     65  * First recall the link list declaration from pmap_prot.h:
     66  *
     67  * struct pmaplist {
     68  *	struct pmap pml_map;
     69  *	struct pmaplist *pml_map;
     70  * };
     71  *
     72  * Compare that declaration with a corresponding xdr declaration that
     73  * is (a) pointer-less, and (b) recursive:
     74  *
     75  * typedef union switch (bool_t) {
     76  *
     77  *	case TRUE: struct {
     78  *		struct pmap;
     79  * 		pmaplist_t foo;
     80  *	};
     81  *
     82  *	case FALSE: struct {};
     83  * } pmaplist_t;
     84  *
     85  * Notice that the xdr declaration has no nxt pointer while
     86  * the C declaration has no bool_t variable.  The bool_t can be
     87  * interpreted as ``more data follows me''; if FALSE then nothing
     88  * follows this bool_t; if TRUE then the bool_t is followed by
     89  * an actual struct pmap, and then (recursively) by the
     90  * xdr union, pamplist_t.
     91  *
     92  * This could be implemented via the xdr_union primitive, though this
     93  * would cause a one recursive call per element in the list.  Rather than do
     94  * that we can ``unwind'' the recursion
     95  * into a while loop and do the union arms in-place.
     96  *
     97  * The head of the list is what the C programmer wishes to past around
     98  * the net, yet is the data that the pointer points to which is interesting;
     99  * this sounds like a job for xdr_reference!
    100  */
    101 bool_t
    102 xdr_pmaplist(XDR *xdrs, struct pmaplist **rp)
    103 {
    104 	/*
    105 	 * more_elements is pre-computed in case the direction is
    106 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
    107 	 * xdr_bool when the direction is XDR_DECODE.
    108 	 */
    109 	bool_t more_elements;
    110 	int freeing;
    111 	struct pmaplist **next	= NULL; /* pacify gcc */
    112 
    113 	_DIAGASSERT(xdrs != NULL);
    114 	_DIAGASSERT(rp != NULL);
    115 
    116 	freeing = (xdrs->x_op == XDR_FREE);
    117 
    118 	for (;;) {
    119 		more_elements = (bool_t)(*rp != NULL);
    120 		if (! xdr_bool(xdrs, &more_elements))
    121 			return (FALSE);
    122 		if (! more_elements)
    123 			return (TRUE);  /* we are done */
    124 		/*
    125 		 * the unfortunate side effect of non-recursion is that in
    126 		 * the case of freeing we must remember the next object
    127 		 * before we free the current object ...
    128 		 */
    129 		if (freeing)
    130 			next = &((*rp)->pml_next);
    131 		if (! xdr_reference(xdrs, (caddr_t *)rp,
    132 		    (u_int)sizeof(struct pmaplist), (xdrproc_t)xdr_pmap))
    133 			return (FALSE);
    134 		rp = (freeing) ? next : &((*rp)->pml_next);
    135 	}
    136 }
    137 
    138 
    139 /*
    140  * xdr_pmaplist_ptr() is specified to take a PMAPLIST *, but is identical in
    141  * functionality to xdr_pmaplist().
    142  */
    143 bool_t
    144 xdr_pmaplist_ptr(XDR *xdrs, struct pmaplist *rp)
    145 {
    146 
    147 	_DIAGASSERT(xdrs != NULL);
    148 	_DIAGASSERT(rp != NULL);
    149 
    150 	return xdr_pmaplist(xdrs, (struct pmaplist **)(void *)rp);
    151 }
    152