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