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