xref: /netbsd-src/lib/libc/rpc/pmap_prot2.c (revision 47c0e0c312cd964546204bd0febab9a56fa11df9)
1*47c0e0c3Stron /*	$NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $	*/
29e15c989Scgd 
363d7b677Scgd /*
4*47c0e0c3Stron  * Copyright (c) 2010, Oracle America, Inc.
563d7b677Scgd  *
6*47c0e0c3Stron  * Redistribution and use in source and binary forms, with or without
7*47c0e0c3Stron  * modification, are permitted provided that the following conditions are
8*47c0e0c3Stron  * met:
963d7b677Scgd  *
10*47c0e0c3Stron  *     * Redistributions of source code must retain the above copyright
11*47c0e0c3Stron  *       notice, this list of conditions and the following disclaimer.
12*47c0e0c3Stron  *     * Redistributions in binary form must reproduce the above
13*47c0e0c3Stron  *       copyright notice, this list of conditions and the following
14*47c0e0c3Stron  *       disclaimer in the documentation and/or other materials
15*47c0e0c3Stron  *       provided with the distribution.
16*47c0e0c3Stron  *     * Neither the name of the "Oracle America, Inc." nor the names of its
17*47c0e0c3Stron  *       contributors may be used to endorse or promote products derived
18*47c0e0c3Stron  *       from this software without specific prior written permission.
1963d7b677Scgd  *
20*47c0e0c3Stron  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*47c0e0c3Stron  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*47c0e0c3Stron  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*47c0e0c3Stron  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*47c0e0c3Stron  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*47c0e0c3Stron  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*47c0e0c3Stron  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*47c0e0c3Stron  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*47c0e0c3Stron  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*47c0e0c3Stron  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*47c0e0c3Stron  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*47c0e0c3Stron  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3263d7b677Scgd  */
3363d7b677Scgd 
34c63c52b2Schristos #include <sys/cdefs.h>
3563d7b677Scgd #if defined(LIBC_SCCS) && !defined(lint)
36c63c52b2Schristos #if 0
37c63c52b2Schristos static char *sccsid = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
38c63c52b2Schristos static char *sccsid = "@(#)pmap_prot2.c	2.1 88/07/29 4.0 RPCSRC";
39c63c52b2Schristos #else
40*47c0e0c3Stron __RCSID("$NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $");
41c63c52b2Schristos #endif
4263d7b677Scgd #endif
4363d7b677Scgd 
4463d7b677Scgd /*
4563d7b677Scgd  * pmap_prot2.c
4663d7b677Scgd  * Protocol for the local binder service, or pmap.
4763d7b677Scgd  *
4863d7b677Scgd  * Copyright (C) 1984, Sun Microsystems, Inc.
4963d7b677Scgd  */
5063d7b677Scgd 
5143fa6fe3Sjtc #include "namespace.h"
5246e6c5e8Slukem 
53b48252f3Slukem #include <assert.h>
54b48252f3Slukem 
5563d7b677Scgd #include <rpc/types.h>
5663d7b677Scgd #include <rpc/xdr.h>
5763d7b677Scgd #include <rpc/pmap_prot.h>
5863d7b677Scgd 
5943fa6fe3Sjtc #ifdef __weak_alias
__weak_alias(xdr_pmaplist,_xdr_pmaplist)6060549036Smycroft __weak_alias(xdr_pmaplist,_xdr_pmaplist)
6143fa6fe3Sjtc #endif
6263d7b677Scgd 
6363d7b677Scgd /*
6463d7b677Scgd  * What is going on with linked lists? (!)
6563d7b677Scgd  * First recall the link list declaration from pmap_prot.h:
6663d7b677Scgd  *
6763d7b677Scgd  * struct pmaplist {
6863d7b677Scgd  *	struct pmap pml_map;
6963d7b677Scgd  *	struct pmaplist *pml_map;
7063d7b677Scgd  * };
7163d7b677Scgd  *
7263d7b677Scgd  * Compare that declaration with a corresponding xdr declaration that
7363d7b677Scgd  * is (a) pointer-less, and (b) recursive:
7463d7b677Scgd  *
7563d7b677Scgd  * typedef union switch (bool_t) {
7663d7b677Scgd  *
7763d7b677Scgd  *	case TRUE: struct {
7863d7b677Scgd  *		struct pmap;
7963d7b677Scgd  * 		pmaplist_t foo;
8063d7b677Scgd  *	};
8163d7b677Scgd  *
8263d7b677Scgd  *	case FALSE: struct {};
8363d7b677Scgd  * } pmaplist_t;
8463d7b677Scgd  *
8563d7b677Scgd  * Notice that the xdr declaration has no nxt pointer while
8663d7b677Scgd  * the C declaration has no bool_t variable.  The bool_t can be
8763d7b677Scgd  * interpreted as ``more data follows me''; if FALSE then nothing
8863d7b677Scgd  * follows this bool_t; if TRUE then the bool_t is followed by
8963d7b677Scgd  * an actual struct pmap, and then (recursively) by the
9063d7b677Scgd  * xdr union, pamplist_t.
9163d7b677Scgd  *
9263d7b677Scgd  * This could be implemented via the xdr_union primitive, though this
9363d7b677Scgd  * would cause a one recursive call per element in the list.  Rather than do
9463d7b677Scgd  * that we can ``unwind'' the recursion
9563d7b677Scgd  * into a while loop and do the union arms in-place.
9663d7b677Scgd  *
9763d7b677Scgd  * The head of the list is what the C programmer wishes to past around
9863d7b677Scgd  * the net, yet is the data that the pointer points to which is interesting;
9963d7b677Scgd  * this sounds like a job for xdr_reference!
10063d7b677Scgd  */
10163d7b677Scgd bool_t
102adb74221Smatt xdr_pmaplist(XDR *xdrs, struct pmaplist **rp)
10363d7b677Scgd {
10463d7b677Scgd 	/*
10563d7b677Scgd 	 * more_elements is pre-computed in case the direction is
10663d7b677Scgd 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
10763d7b677Scgd 	 * xdr_bool when the direction is XDR_DECODE.
10863d7b677Scgd 	 */
10963d7b677Scgd 	bool_t more_elements;
110b48252f3Slukem 	int freeing;
11146e6c5e8Slukem 	struct pmaplist **next	= NULL; /* pacify gcc */
11263d7b677Scgd 
113b48252f3Slukem 	_DIAGASSERT(xdrs != NULL);
114b48252f3Slukem 	_DIAGASSERT(rp != NULL);
115b48252f3Slukem 
116b48252f3Slukem 	freeing = (xdrs->x_op == XDR_FREE);
117b48252f3Slukem 
1181325a26dSchristos 	for (;;) {
11963d7b677Scgd 		more_elements = (bool_t)(*rp != NULL);
12063d7b677Scgd 		if (! xdr_bool(xdrs, &more_elements))
12163d7b677Scgd 			return (FALSE);
12263d7b677Scgd 		if (! more_elements)
12363d7b677Scgd 			return (TRUE);  /* we are done */
12463d7b677Scgd 		/*
12563d7b677Scgd 		 * the unfortunate side effect of non-recursion is that in
12663d7b677Scgd 		 * the case of freeing we must remember the next object
12763d7b677Scgd 		 * before we free the current object ...
12863d7b677Scgd 		 */
12963d7b677Scgd 		if (freeing)
13063d7b677Scgd 			next = &((*rp)->pml_next);
13163d7b677Scgd 		if (! xdr_reference(xdrs, (caddr_t *)rp,
132caaf1528Schristos 		    (u_int)sizeof(struct pmaplist), (xdrproc_t)xdr_pmap))
13363d7b677Scgd 			return (FALSE);
13463d7b677Scgd 		rp = (freeing) ? next : &((*rp)->pml_next);
13563d7b677Scgd 	}
13663d7b677Scgd }
1377df0ccbaSfvdl 
1387df0ccbaSfvdl 
1397df0ccbaSfvdl /*
1407df0ccbaSfvdl  * xdr_pmaplist_ptr() is specified to take a PMAPLIST *, but is identical in
1417df0ccbaSfvdl  * functionality to xdr_pmaplist().
1427df0ccbaSfvdl  */
1437df0ccbaSfvdl bool_t
xdr_pmaplist_ptr(XDR * xdrs,struct pmaplist * rp)144adb74221Smatt xdr_pmaplist_ptr(XDR *xdrs, struct pmaplist *rp)
1457df0ccbaSfvdl {
1460e8cfd8fSlukem 
1470e8cfd8fSlukem 	_DIAGASSERT(xdrs != NULL);
1480e8cfd8fSlukem 	_DIAGASSERT(rp != NULL);
1490e8cfd8fSlukem 
150deb154d2Schristos 	return xdr_pmaplist(xdrs, (struct pmaplist **)(void *)rp);
1517df0ccbaSfvdl }
152