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