1177633Sdfr/*	$NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $	*/
2177633Sdfr
3261046Smav/*-
4261046Smav * Copyright (c) 2009, Sun Microsystems, Inc.
5261046Smav * All rights reserved.
6261046Smav *
7261046Smav * Redistribution and use in source and binary forms, with or without
8261046Smav * modification, are permitted provided that the following conditions are met:
9261046Smav * - Redistributions of source code must retain the above copyright notice,
10261046Smav *   this list of conditions and the following disclaimer.
11261046Smav * - Redistributions in binary form must reproduce the above copyright notice,
12261046Smav *   this list of conditions and the following disclaimer in the documentation
13261046Smav *   and/or other materials provided with the distribution.
14261046Smav * - Neither the name of Sun Microsystems, Inc. nor the names of its
15261046Smav *   contributors may be used to endorse or promote products derived
16261046Smav *   from this software without specific prior written permission.
17177633Sdfr *
18261046Smav * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19261046Smav * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20261046Smav * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21261046Smav * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22261046Smav * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23261046Smav * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24261046Smav * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25261046Smav * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26261046Smav * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27261046Smav * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28261046Smav * POSSIBILITY OF SUCH DAMAGE.
29177633Sdfr */
30177633Sdfr/*
31177633Sdfr * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32177633Sdfr */
33177633Sdfr
34177633Sdfr/* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
35177633Sdfr
36177633Sdfr#if defined(LIBC_SCCS) && !defined(lint)
37177633Sdfrstatic char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
38177633Sdfr#endif
39177633Sdfr#include <sys/cdefs.h>
40177633Sdfr__FBSDID("$FreeBSD$");
41177633Sdfr
42177633Sdfr/*
43177633Sdfr * rpcb_prot.c
44177633Sdfr * XDR routines for the rpcbinder version 3.
45177633Sdfr *
46177633Sdfr * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
47177633Sdfr */
48177633Sdfr
49177633Sdfr#include <sys/param.h>
50177633Sdfr#include <sys/systm.h>
51177633Sdfr#include <sys/kernel.h>
52177633Sdfr#include <sys/malloc.h>
53177633Sdfr
54177633Sdfr#include <rpc/rpc.h>
55177633Sdfr#include <rpc/rpcb_prot.h>
56177633Sdfr
57177633Sdfrbool_t
58182154Sdfrxdr_portmap(XDR *xdrs, struct portmap *regs)
59177633Sdfr{
60177633Sdfr
61177633Sdfr	if (xdr_u_long(xdrs, &regs->pm_prog) &&
62177633Sdfr		xdr_u_long(xdrs, &regs->pm_vers) &&
63177633Sdfr		xdr_u_long(xdrs, &regs->pm_prot))
64177633Sdfr		return (xdr_u_long(xdrs, &regs->pm_port));
65177633Sdfr	return (FALSE);
66177633Sdfr}
67177633Sdfr
68177633Sdfrbool_t
69177633Sdfrxdr_rpcb(XDR *xdrs, RPCB *objp)
70177633Sdfr{
71177633Sdfr	if (!xdr_uint32_t(xdrs, &objp->r_prog)) {
72177633Sdfr		return (FALSE);
73177633Sdfr	}
74177633Sdfr	if (!xdr_uint32_t(xdrs, &objp->r_vers)) {
75177633Sdfr		return (FALSE);
76177633Sdfr	}
77177633Sdfr	if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
78177633Sdfr		return (FALSE);
79177633Sdfr	}
80177633Sdfr	if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
81177633Sdfr		return (FALSE);
82177633Sdfr	}
83177633Sdfr	if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
84177633Sdfr		return (FALSE);
85177633Sdfr	}
86177633Sdfr	return (TRUE);
87177633Sdfr}
88177633Sdfr
89177633Sdfr/*
90177633Sdfr * rpcblist_ptr implements a linked list.  The RPCL definition from
91177633Sdfr * rpcb_prot.x is:
92177633Sdfr *
93177633Sdfr * struct rpcblist {
94177633Sdfr * 	rpcb		rpcb_map;
95177633Sdfr *	struct rpcblist *rpcb_next;
96177633Sdfr * };
97177633Sdfr * typedef rpcblist *rpcblist_ptr;
98177633Sdfr *
99177633Sdfr * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
100177633Sdfr * there's any data behind the pointer, followed by the data (if any exists).
101177633Sdfr * The boolean can be interpreted as ``more data follows me''; if FALSE then
102177633Sdfr * nothing follows the boolean; if TRUE then the boolean is followed by an
103177633Sdfr * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
104177633Sdfr * rpcblist *").
105177633Sdfr *
106177633Sdfr * This could be implemented via the xdr_pointer type, though this would
107177633Sdfr * result in one recursive call per element in the list.  Rather than do that
108177633Sdfr * we can ``unwind'' the recursion into a while loop and use xdr_reference to
109177633Sdfr * serialize the rpcb elements.
110177633Sdfr */
111177633Sdfr
112177633Sdfrbool_t
113177633Sdfrxdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
114177633Sdfr{
115177633Sdfr	/*
116177633Sdfr	 * more_elements is pre-computed in case the direction is
117177633Sdfr	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
118177633Sdfr	 * xdr_bool when the direction is XDR_DECODE.
119177633Sdfr	 */
120177633Sdfr	bool_t more_elements;
121177633Sdfr	int freeing = (xdrs->x_op == XDR_FREE);
122177633Sdfr	rpcblist_ptr next;
123177633Sdfr	rpcblist_ptr next_copy;
124177633Sdfr
125177633Sdfr	next = NULL;
126177633Sdfr	for (;;) {
127177633Sdfr		more_elements = (bool_t)(*rp != NULL);
128177633Sdfr		if (! xdr_bool(xdrs, &more_elements)) {
129177633Sdfr			return (FALSE);
130177633Sdfr		}
131177633Sdfr		if (! more_elements) {
132177633Sdfr			return (TRUE);  /* we are done */
133177633Sdfr		}
134177633Sdfr		/*
135177633Sdfr		 * the unfortunate side effect of non-recursion is that in
136177633Sdfr		 * the case of freeing we must remember the next object
137177633Sdfr		 * before we free the current object ...
138177633Sdfr		 */
139177633Sdfr		if (freeing && *rp)
140177633Sdfr			next = (*rp)->rpcb_next;
141177633Sdfr		if (! xdr_reference(xdrs, (caddr_t *)rp,
142177633Sdfr		    (u_int)sizeof (RPCBLIST), (xdrproc_t)xdr_rpcb)) {
143177633Sdfr			return (FALSE);
144177633Sdfr		}
145177633Sdfr		if (freeing) {
146177633Sdfr			next_copy = next;
147177633Sdfr			rp = &next_copy;
148177633Sdfr			/*
149177633Sdfr			 * Note that in the subsequent iteration, next_copy
150177633Sdfr			 * gets nulled out by the xdr_reference
151177633Sdfr			 * but next itself survives.
152177633Sdfr			 */
153177633Sdfr		} else if (*rp) {
154177633Sdfr			rp = &((*rp)->rpcb_next);
155177633Sdfr		}
156177633Sdfr	}
157177633Sdfr	/*NOTREACHED*/
158177633Sdfr}
159177633Sdfr
160177633Sdfr#if 0
161177633Sdfr/*
162177633Sdfr * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
163177633Sdfr * functionality to xdr_rpcblist_ptr().
164177633Sdfr */
165177633Sdfrbool_t
166177633Sdfrxdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
167177633Sdfr{
168177633Sdfr	bool_t	dummy;
169177633Sdfr
170177633Sdfr	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
171177633Sdfr	return (dummy);
172177633Sdfr}
173177633Sdfr#endif
174177633Sdfr
175177633Sdfrbool_t
176177633Sdfrxdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
177177633Sdfr{
178177633Sdfr	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
179177633Sdfr		return (FALSE);
180177633Sdfr	}
181177633Sdfr	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
182177633Sdfr		return (FALSE);
183177633Sdfr	}
184177633Sdfr	if (!xdr_uint32_t(xdrs, &objp->r_nc_semantics)) {
185177633Sdfr		return (FALSE);
186177633Sdfr	}
187177633Sdfr	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
188177633Sdfr		return (FALSE);
189177633Sdfr	}
190177633Sdfr	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
191177633Sdfr		return (FALSE);
192177633Sdfr	}
193177633Sdfr	return (TRUE);
194177633Sdfr}
195177633Sdfr
196177633Sdfrbool_t
197177633Sdfrxdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
198177633Sdfr{
199177633Sdfr	/*
200177633Sdfr	 * more_elements is pre-computed in case the direction is
201177633Sdfr	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
202177633Sdfr	 * xdr_bool when the direction is XDR_DECODE.
203177633Sdfr	 */
204177633Sdfr	bool_t more_elements;
205177633Sdfr	int freeing = (xdrs->x_op == XDR_FREE);
206177633Sdfr	rpcb_entry_list_ptr next;
207177633Sdfr	rpcb_entry_list_ptr next_copy;
208177633Sdfr
209177633Sdfr	next = NULL;
210177633Sdfr	for (;;) {
211177633Sdfr		more_elements = (bool_t)(*rp != NULL);
212177633Sdfr		if (! xdr_bool(xdrs, &more_elements)) {
213177633Sdfr			return (FALSE);
214177633Sdfr		}
215177633Sdfr		if (! more_elements) {
216177633Sdfr			return (TRUE);  /* we are done */
217177633Sdfr		}
218177633Sdfr		/*
219177633Sdfr		 * the unfortunate side effect of non-recursion is that in
220177633Sdfr		 * the case of freeing we must remember the next object
221177633Sdfr		 * before we free the current object ...
222177633Sdfr		 */
223177633Sdfr		if (freeing)
224177633Sdfr			next = (*rp)->rpcb_entry_next;
225177633Sdfr		if (! xdr_reference(xdrs, (caddr_t *)rp,
226177633Sdfr		    (u_int)sizeof (rpcb_entry_list),
227177633Sdfr				    (xdrproc_t)xdr_rpcb_entry)) {
228177633Sdfr			return (FALSE);
229177633Sdfr		}
230177633Sdfr		if (freeing && *rp) {
231177633Sdfr			next_copy = next;
232177633Sdfr			rp = &next_copy;
233177633Sdfr			/*
234177633Sdfr			 * Note that in the subsequent iteration, next_copy
235177633Sdfr			 * gets nulled out by the xdr_reference
236177633Sdfr			 * but next itself survives.
237177633Sdfr			 */
238177633Sdfr		} else if (*rp) {
239177633Sdfr			rp = &((*rp)->rpcb_entry_next);
240177633Sdfr		}
241177633Sdfr	}
242177633Sdfr	/*NOTREACHED*/
243177633Sdfr}
244