1*84d9c625SLionel Sambuc /* $NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)pmap_prot2.c 2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * pmap_prot2.c
462fe8fb19SBen Gras * Protocol for the local binder service, or pmap.
472fe8fb19SBen Gras *
482fe8fb19SBen Gras * Copyright (C) 1984, Sun Microsystems, Inc.
492fe8fb19SBen Gras */
502fe8fb19SBen Gras
512fe8fb19SBen Gras #include "namespace.h"
522fe8fb19SBen Gras
532fe8fb19SBen Gras #include <assert.h>
542fe8fb19SBen Gras
552fe8fb19SBen Gras #include <rpc/types.h>
562fe8fb19SBen Gras #include <rpc/xdr.h>
572fe8fb19SBen Gras #include <rpc/pmap_prot.h>
582fe8fb19SBen Gras
592fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(xdr_pmaplist,_xdr_pmaplist)602fe8fb19SBen Gras __weak_alias(xdr_pmaplist,_xdr_pmaplist)
612fe8fb19SBen Gras #endif
622fe8fb19SBen Gras
632fe8fb19SBen Gras /*
642fe8fb19SBen Gras * What is going on with linked lists? (!)
652fe8fb19SBen Gras * First recall the link list declaration from pmap_prot.h:
662fe8fb19SBen Gras *
672fe8fb19SBen Gras * struct pmaplist {
682fe8fb19SBen Gras * struct pmap pml_map;
692fe8fb19SBen Gras * struct pmaplist *pml_map;
702fe8fb19SBen Gras * };
712fe8fb19SBen Gras *
722fe8fb19SBen Gras * Compare that declaration with a corresponding xdr declaration that
732fe8fb19SBen Gras * is (a) pointer-less, and (b) recursive:
742fe8fb19SBen Gras *
752fe8fb19SBen Gras * typedef union switch (bool_t) {
762fe8fb19SBen Gras *
772fe8fb19SBen Gras * case TRUE: struct {
782fe8fb19SBen Gras * struct pmap;
792fe8fb19SBen Gras * pmaplist_t foo;
802fe8fb19SBen Gras * };
812fe8fb19SBen Gras *
822fe8fb19SBen Gras * case FALSE: struct {};
832fe8fb19SBen Gras * } pmaplist_t;
842fe8fb19SBen Gras *
852fe8fb19SBen Gras * Notice that the xdr declaration has no nxt pointer while
862fe8fb19SBen Gras * the C declaration has no bool_t variable. The bool_t can be
872fe8fb19SBen Gras * interpreted as ``more data follows me''; if FALSE then nothing
882fe8fb19SBen Gras * follows this bool_t; if TRUE then the bool_t is followed by
892fe8fb19SBen Gras * an actual struct pmap, and then (recursively) by the
902fe8fb19SBen Gras * xdr union, pamplist_t.
912fe8fb19SBen Gras *
922fe8fb19SBen Gras * This could be implemented via the xdr_union primitive, though this
932fe8fb19SBen Gras * would cause a one recursive call per element in the list. Rather than do
942fe8fb19SBen Gras * that we can ``unwind'' the recursion
952fe8fb19SBen Gras * into a while loop and do the union arms in-place.
962fe8fb19SBen Gras *
972fe8fb19SBen Gras * The head of the list is what the C programmer wishes to past around
982fe8fb19SBen Gras * the net, yet is the data that the pointer points to which is interesting;
992fe8fb19SBen Gras * this sounds like a job for xdr_reference!
1002fe8fb19SBen Gras */
1012fe8fb19SBen Gras bool_t
102f14fb602SLionel Sambuc xdr_pmaplist(XDR *xdrs, struct pmaplist **rp)
1032fe8fb19SBen Gras {
1042fe8fb19SBen Gras /*
1052fe8fb19SBen Gras * more_elements is pre-computed in case the direction is
1062fe8fb19SBen Gras * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
1072fe8fb19SBen Gras * xdr_bool when the direction is XDR_DECODE.
1082fe8fb19SBen Gras */
1092fe8fb19SBen Gras bool_t more_elements;
1102fe8fb19SBen Gras int freeing;
1112fe8fb19SBen Gras struct pmaplist **next = NULL; /* pacify gcc */
1122fe8fb19SBen Gras
1132fe8fb19SBen Gras _DIAGASSERT(xdrs != NULL);
1142fe8fb19SBen Gras _DIAGASSERT(rp != NULL);
1152fe8fb19SBen Gras
1162fe8fb19SBen Gras freeing = (xdrs->x_op == XDR_FREE);
1172fe8fb19SBen Gras
1182fe8fb19SBen Gras for (;;) {
1192fe8fb19SBen Gras more_elements = (bool_t)(*rp != NULL);
1202fe8fb19SBen Gras if (! xdr_bool(xdrs, &more_elements))
1212fe8fb19SBen Gras return (FALSE);
1222fe8fb19SBen Gras if (! more_elements)
1232fe8fb19SBen Gras return (TRUE); /* we are done */
1242fe8fb19SBen Gras /*
1252fe8fb19SBen Gras * the unfortunate side effect of non-recursion is that in
1262fe8fb19SBen Gras * the case of freeing we must remember the next object
1272fe8fb19SBen Gras * before we free the current object ...
1282fe8fb19SBen Gras */
1292fe8fb19SBen Gras if (freeing)
1302fe8fb19SBen Gras next = &((*rp)->pml_next);
1312fe8fb19SBen Gras if (! xdr_reference(xdrs, (caddr_t *)rp,
1322fe8fb19SBen Gras (u_int)sizeof(struct pmaplist), (xdrproc_t)xdr_pmap))
1332fe8fb19SBen Gras return (FALSE);
1342fe8fb19SBen Gras rp = (freeing) ? next : &((*rp)->pml_next);
1352fe8fb19SBen Gras }
1362fe8fb19SBen Gras }
1372fe8fb19SBen Gras
1382fe8fb19SBen Gras
1392fe8fb19SBen Gras /*
1402fe8fb19SBen Gras * xdr_pmaplist_ptr() is specified to take a PMAPLIST *, but is identical in
1412fe8fb19SBen Gras * functionality to xdr_pmaplist().
1422fe8fb19SBen Gras */
1432fe8fb19SBen Gras bool_t
xdr_pmaplist_ptr(XDR * xdrs,struct pmaplist * rp)144f14fb602SLionel Sambuc xdr_pmaplist_ptr(XDR *xdrs, struct pmaplist *rp)
1452fe8fb19SBen Gras {
1462fe8fb19SBen Gras
1472fe8fb19SBen Gras _DIAGASSERT(xdrs != NULL);
1482fe8fb19SBen Gras _DIAGASSERT(rp != NULL);
1492fe8fb19SBen Gras
1502fe8fb19SBen Gras return xdr_pmaplist(xdrs, (struct pmaplist **)(void *)rp);
1512fe8fb19SBen Gras }
152