| 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 | |
| 29 | /* |
| 30 | * pmap_prot2.c |
| 31 | * Protocol for the local binder service, or pmap. |
| 32 | * |
| 33 | * Copyright (C) 1984, Sun Microsystems, Inc. |
| 34 | */ |
| 35 | |
| 36 | #include <assert.h> |
| 37 | |
| 38 | #include <rpc/types.h> |
| 39 | #include <rpc/xdr.h> |
| 40 | #include <rpc/pmap_prot.h> |
| 41 | |
| 42 | |
| 43 | /* |
| 44 | * What is going on with linked lists? (!) |
| 45 | * First recall the link list declaration from pmap_prot.h: |
| 46 | * |
| 47 | * struct pmaplist { |
| 48 | * struct pmap pml_map; |
| 49 | * struct pmaplist *pml_map; |
| 50 | * }; |
| 51 | * |
| 52 | * Compare that declaration with a corresponding xdr declaration that |
| 53 | * is (a) pointer-less, and (b) recursive: |
| 54 | * |
| 55 | * typedef union switch (bool_t) { |
| 56 | * |
| 57 | * case TRUE: struct { |
| 58 | * struct pmap; |
| 59 | * pmaplist_t foo; |
| 60 | * }; |
| 61 | * |
| 62 | * case FALSE: struct {}; |
| 63 | * } pmaplist_t; |
| 64 | * |
| 65 | * Notice that the xdr declaration has no nxt pointer while |
| 66 | * the C declaration has no bool_t variable. The bool_t can be |
| 67 | * interpreted as ``more data follows me''; if FALSE then nothing |
| 68 | * follows this bool_t; if TRUE then the bool_t is followed by |
| 69 | * an actual struct pmap, and then (recursively) by the |
| 70 | * xdr union, pamplist_t. |
| 71 | * |
| 72 | * This could be implemented via the xdr_union primitive, though this |
| 73 | * would cause a one recursive call per element in the list. Rather than do |
| 74 | * that we can ``unwind'' the recursion |
| 75 | * into a while loop and do the union arms in-place. |
| 76 | * |
| 77 | * The head of the list is what the C programmer wishes to past around |
| 78 | * the net, yet is the data that the pointer points to which is interesting; |
| 79 | * this sounds like a job for xdr_reference! |
| 80 | */ |
| 81 | bool_t |
| 82 | xdr_pmaplist(xdrs, rp) |
| 83 | XDR *xdrs; |
| 84 | struct pmaplist **rp; |
| 85 | { |
| 86 | /* |
| 87 | * more_elements is pre-computed in case the direction is |
| 88 | * XDR_ENCODE or XDR_FREE. more_elements is overwritten by |
| 89 | * xdr_bool when the direction is XDR_DECODE. |
| 90 | */ |
| 91 | bool_t more_elements; |
| 92 | int freeing; |
| 93 | struct pmaplist **next = NULL; /* pacify gcc */ |
| 94 | |
| 95 | assert(xdrs != NULL); |
| 96 | assert(rp != NULL); |
| 97 | |
| 98 | freeing = (xdrs->x_op == XDR_FREE); |
| 99 | |
| 100 | for (;;) { |
| 101 | more_elements = (bool_t)(*rp != NULL); |
| 102 | if (! xdr_bool(xdrs, &more_elements)) |
| 103 | return (FALSE); |
| 104 | if (! more_elements) |
| 105 | return (TRUE); /* we are done */ |
| 106 | /* |
| 107 | * the unfortunate side effect of non-recursion is that in |
| 108 | * the case of freeing we must remember the next object |
| 109 | * before we free the current object ... |
| 110 | */ |
| 111 | if (freeing) |
| 112 | next = &((*rp)->pml_next); |
| 113 | if (! xdr_reference(xdrs, (caddr_t *)rp, |
| 114 | (u_int)sizeof(struct pmaplist), (xdrproc_t)xdr_pmap)) |
| 115 | return (FALSE); |
| 116 | rp = (freeing) ? next : &((*rp)->pml_next); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | |
| 121 | /* |
| 122 | * xdr_pmaplist_ptr() is specified to take a PMAPLIST *, but is identical in |
| 123 | * functionality to xdr_pmaplist(). |
| 124 | */ |
| 125 | bool_t |
| 126 | xdr_pmaplist_ptr(xdrs, rp) |
| 127 | XDR *xdrs; |
| 128 | struct pmaplist *rp; |
| 129 | { |
| 130 | return xdr_pmaplist(xdrs, (struct pmaplist **)(void *)rp); |
| 131 | } |