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