1 /* $NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $ */
4 * SPDX-License-Identifier: BSD-3-Clause
6 * Copyright (c) 2009, Sun Microsystems, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36 #include <sys/cdefs.h>
39 * XDR routines for the rpcbinder version 3.
41 * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/malloc.h>
50 #include <rpc/rpc_com.h>
51 #include <rpc/rpcb_prot.h>
54 xdr_portmap(XDR *xdrs, struct portmap *regs)
57 if (xdr_u_long(xdrs, ®s->pm_prog) &&
58 xdr_u_long(xdrs, ®s->pm_vers) &&
59 xdr_u_long(xdrs, ®s->pm_prot))
60 return (xdr_u_long(xdrs, ®s->pm_port));
65 xdr_rpcb(XDR *xdrs, RPCB *objp)
67 if (!xdr_uint32_t(xdrs, &objp->r_prog)) {
70 if (!xdr_uint32_t(xdrs, &objp->r_vers)) {
73 if (!xdr_string(xdrs, &objp->r_netid, RPC_MAXDATASIZE)) {
76 if (!xdr_string(xdrs, &objp->r_addr, RPC_MAXDATASIZE)) {
79 if (!xdr_string(xdrs, &objp->r_owner, RPC_MAXDATASIZE)) {
86 * rpcblist_ptr implements a linked list. The RPCL definition from
91 * struct rpcblist *rpcb_next;
93 * typedef rpcblist *rpcblist_ptr;
95 * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
96 * there's any data behind the pointer, followed by the data (if any exists).
97 * The boolean can be interpreted as ``more data follows me''; if FALSE then
98 * nothing follows the boolean; if TRUE then the boolean is followed by an
99 * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
102 * This could be implemented via the xdr_pointer type, though this would
103 * result in one recursive call per element in the list. Rather than do that
104 * we can ``unwind'' the recursion into a while loop and use xdr_reference to
105 * serialize the rpcb elements.
109 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
112 * more_elements is pre-computed in case the direction is
113 * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
114 * xdr_bool when the direction is XDR_DECODE.
116 bool_t more_elements;
117 int freeing = (xdrs->x_op == XDR_FREE);
119 rpcblist_ptr next_copy;
123 more_elements = (bool_t)(*rp != NULL);
124 if (! xdr_bool(xdrs, &more_elements)) {
127 if (! more_elements) {
128 return (TRUE); /* we are done */
131 * the unfortunate side effect of non-recursion is that in
132 * the case of freeing we must remember the next object
133 * before we free the current object ...
136 next = (*rp)->rpcb_next;
137 if (! xdr_reference(xdrs, (caddr_t *)rp,
138 (u_int)sizeof (RPCBLIST), (xdrproc_t)xdr_rpcb)) {
145 * Note that in the subsequent iteration, next_copy
146 * gets nulled out by the xdr_reference
147 * but next itself survives.
150 rp = &((*rp)->rpcb_next);
158 * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
159 * functionality to xdr_rpcblist_ptr().
162 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
166 dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
172 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
174 if (!xdr_string(xdrs, &objp->r_maddr, RPC_MAXDATASIZE)) {
177 if (!xdr_string(xdrs, &objp->r_nc_netid, RPC_MAXDATASIZE)) {
180 if (!xdr_uint32_t(xdrs, &objp->r_nc_semantics)) {
183 if (!xdr_string(xdrs, &objp->r_nc_protofmly, RPC_MAXDATASIZE)) {
186 if (!xdr_string(xdrs, &objp->r_nc_proto, RPC_MAXDATASIZE)) {
193 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
196 * more_elements is pre-computed in case the direction is
197 * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
198 * xdr_bool when the direction is XDR_DECODE.
200 bool_t more_elements;
201 int freeing = (xdrs->x_op == XDR_FREE);
202 rpcb_entry_list_ptr next;
203 rpcb_entry_list_ptr next_copy;
207 more_elements = (bool_t)(*rp != NULL);
208 if (! xdr_bool(xdrs, &more_elements)) {
211 if (! more_elements) {
212 return (TRUE); /* we are done */
215 * the unfortunate side effect of non-recursion is that in
216 * the case of freeing we must remember the next object
217 * before we free the current object ...
220 next = (*rp)->rpcb_entry_next;
221 if (! xdr_reference(xdrs, (caddr_t *)rp,
222 (u_int)sizeof (rpcb_entry_list),
223 (xdrproc_t)xdr_rpcb_entry)) {
226 if (freeing && *rp) {
230 * Note that in the subsequent iteration, next_copy
231 * gets nulled out by the xdr_reference
232 * but next itself survives.
235 rp = &((*rp)->rpcb_entry_next);