Path: blob/main/crypto/krb5/src/lib/rpc/pmap_prot2.c
39536 views
/* @(#)pmap_prot2.c 2.1 88/07/29 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";35#endif3637/*38* pmap_prot2.c39* Protocol for the local binder service, or pmap.40*/4142#include <gssrpc/types.h>43#include <gssrpc/xdr.h>44#include <gssrpc/pmap_prot.h>454647/*48* What is going on with linked lists? (!)49* First recall the link list declaration from pmap_prot.h:50*51* struct pmaplist {52* struct pmap pml_map;53* struct pmaplist *pml_map;54* };55*56* Compare that declaration with a corresponding xdr declaration that57* is (a) pointer-less, and (b) recursive:58*59* typedef union switch (bool_t) {60*61* case TRUE: struct {62* struct pmap;63* pmaplist_t foo;64* };65*66* case FALSE: struct {};67* } pmaplist_t;68*69* Notice that the xdr declaration has no nxt pointer while70* the C declaration has no bool_t variable. The bool_t can be71* interpreted as ``more data follows me''; if FALSE then nothing72* follows this bool_t; if TRUE then the bool_t is followed by73* an actual struct pmap, and then (recursively) by the74* xdr union, pamplist_t.75*76* This could be implemented via the xdr_union primitive, though this77* would cause a one recursive call per element in the list. Rather than do78* that we can ``unwind'' the recursion79* into a while loop and do the union arms in-place.80*81* The head of the list is what the C programmer wishes to past around82* the net, yet is the data that the pointer points to which is interesting;83* this sounds like a job for xdr_reference!84*/85bool_t86xdr_pmaplist(XDR *xdrs, struct pmaplist **rp)87{88/*89* more_elements is pre-computed in case the direction is90* XDR_ENCODE or XDR_FREE. more_elements is overwritten by91* xdr_bool when the direction is XDR_DECODE.92*/93bool_t more_elements;94int freeing = (xdrs->x_op == XDR_FREE);95struct pmaplist **next = NULL;9697while (TRUE) {98more_elements = (bool_t)(*rp != NULL);99if (! xdr_bool(xdrs, &more_elements))100return (FALSE);101if (! more_elements)102return (TRUE); /* we are done */103/*104* the unfortunate side effect of non-recursion is that in105* the case of freeing we must remember the next object106* before we free the current object ...107*/108if (freeing)109next = &((*rp)->pml_next);110if (! xdr_reference(xdrs, (caddr_t *)rp,111(u_int)sizeof(struct pmaplist),112(xdrproc_t)xdr_pmap))113return (FALSE);114rp = (freeing) ? next : &((*rp)->pml_next);115}116}117118119