rpcb_prot.c revision 296373
1226586Sdim/*	$NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $	*/
2226586Sdim
3353358Sdim/*-
4353358Sdim * Copyright (c) 2009, Sun Microsystems, Inc.
5353358Sdim * All rights reserved.
6226586Sdim *
7226586Sdim * Redistribution and use in source and binary forms, with or without
8226586Sdim * modification, are permitted provided that the following conditions are met:
9243830Sdim * - Redistributions of source code must retain the above copyright notice,
10226586Sdim *   this list of conditions and the following disclaimer.
11226586Sdim * - Redistributions in binary form must reproduce the above copyright notice,
12226586Sdim *   this list of conditions and the following disclaimer in the documentation
13280031Sdim *   and/or other materials provided with the distribution.
14280031Sdim * - Neither the name of Sun Microsystems, Inc. nor the names of its
15226586Sdim *   contributors may be used to endorse or promote products derived
16226586Sdim *   from this software without specific prior written permission.
17226586Sdim *
18243830Sdim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19226586Sdim * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21249423Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22226586Sdim * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23226586Sdim * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24226586Sdim * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25276479Sdim * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26309124Sdim * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27226586Sdim * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28226586Sdim * POSSIBILITY OF SUCH DAMAGE.
29226586Sdim */
30226586Sdim/*
31226586Sdim * Copyright (c) 1986-1991 by Sun Microsystems Inc.
32226586Sdim */
33226586Sdim
34226586Sdim/* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
35226586Sdim
36226586Sdim#if defined(LIBC_SCCS) && !defined(lint)
37341825Sdimstatic char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
38239462Sdim#endif
39239462Sdim#include <sys/cdefs.h>
40226586Sdim__FBSDID("$FreeBSD: releng/10.3/lib/libc/rpc/rpcb_prot.c 294300 2016-01-19 01:30:22Z ngie $");
41280031Sdim
42280031Sdim/*
43280031Sdim * rpcb_prot.c
44280031Sdim * XDR routines for the rpcbinder version 3.
45226586Sdim *
46226586Sdim * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
47226586Sdim */
48226586Sdim
49226586Sdim#include "namespace.h"
50226586Sdim#include <rpc/rpc.h>
51226586Sdim#include <rpc/types.h>
52226586Sdim#include <rpc/xdr.h>
53226586Sdim#include <rpc/rpcb_prot.h>
54226586Sdim#include "un-namespace.h"
55226586Sdim
56226586Sdimbool_t
57226586Sdimxdr_rpcb(xdrs, objp)
58226586Sdim	XDR *xdrs;
59226586Sdim	RPCB *objp;
60226586Sdim{
61226586Sdim	if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
62226586Sdim		return (FALSE);
63226586Sdim	}
64226586Sdim	if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
65226586Sdim		return (FALSE);
66226586Sdim	}
67226586Sdim	if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
68226586Sdim		return (FALSE);
69226586Sdim	}
70226586Sdim	if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
71226586Sdim		return (FALSE);
72226586Sdim	}
73226586Sdim	if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
74226586Sdim		return (FALSE);
75226586Sdim	}
76226586Sdim	return (TRUE);
77226586Sdim}
78288943Sdim
79226586Sdim/*
80226586Sdim * rpcblist_ptr implements a linked list.  The RPCL definition from
81226586Sdim * rpcb_prot.x is:
82226586Sdim *
83226586Sdim * struct rpcblist {
84226586Sdim * 	rpcb		rpcb_map;
85226586Sdim *	struct rpcblist *rpcb_next;
86226586Sdim * };
87226586Sdim * typedef rpcblist *rpcblist_ptr;
88226586Sdim *
89226586Sdim * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
90226586Sdim * there's any data behind the pointer, followed by the data (if any exists).
91226586Sdim * The boolean can be interpreted as ``more data follows me''; if FALSE then
92234353Sdim * nothing follows the boolean; if TRUE then the boolean is followed by an
93226586Sdim * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
94226586Sdim * rpcblist *").
95226586Sdim *
96226586Sdim * This could be implemented via the xdr_pointer type, though this would
97226586Sdim * result in one recursive call per element in the list.  Rather than do that
98226586Sdim * we can ``unwind'' the recursion into a while loop and use xdr_reference to
99226586Sdim * serialize the rpcb elements.
100314564Sdim */
101226586Sdim
102226586Sdimbool_t
103226586Sdimxdr_rpcblist_ptr(xdrs, rp)
104314564Sdim	XDR *xdrs;
105226586Sdim	rpcblist_ptr *rp;
106226586Sdim{
107344779Sdim	/*
108344779Sdim	 * more_elements is pre-computed in case the direction is
109226586Sdim	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
110243830Sdim	 * xdr_bool when the direction is XDR_DECODE.
111243830Sdim	 */
112243830Sdim	bool_t more_elements;
113314564Sdim	int freeing = (xdrs->x_op == XDR_FREE);
114341825Sdim	rpcblist_ptr next;
115341825Sdim	rpcblist_ptr next_copy;
116243830Sdim
117243830Sdim	next = NULL;
118226586Sdim	for (;;) {
119226586Sdim		more_elements = (bool_t)(*rp != NULL);
120226586Sdim		if (! xdr_bool(xdrs, &more_elements)) {
121226586Sdim			return (FALSE);
122226586Sdim		}
123226586Sdim		if (! more_elements) {
124226586Sdim			return (TRUE);  /* we are done */
125226586Sdim		}
126226586Sdim		/*
127314564Sdim		 * the unfortunate side effect of non-recursion is that in
128226586Sdim		 * the case of freeing we must remember the next object
129226586Sdim		 * before we free the current object ...
130226586Sdim		 */
131226586Sdim		if (freeing && *rp)
132226586Sdim			next = (*rp)->rpcb_next;
133226586Sdim		if (! xdr_reference(xdrs, (caddr_t *)rp,
134226586Sdim		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
135226586Sdim			return (FALSE);
136226586Sdim		}
137226586Sdim		if (freeing) {
138226586Sdim			next_copy = next;
139226586Sdim			rp = &next_copy;
140226586Sdim			/*
141226586Sdim			 * Note that in the subsequent iteration, next_copy
142226586Sdim			 * gets nulled out by the xdr_reference
143226586Sdim			 * but next itself survives.
144226586Sdim			 */
145226586Sdim		} else if (*rp) {
146226586Sdim			rp = &((*rp)->rpcb_next);
147226586Sdim		}
148226586Sdim	}
149226586Sdim	/*NOTREACHED*/
150226586Sdim}
151226586Sdim
152226586Sdim/*
153226586Sdim * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
154226586Sdim * functionality to xdr_rpcblist_ptr().
155226586Sdim */
156226586Sdimbool_t
157226586Sdimxdr_rpcblist(xdrs, rp)
158226586Sdim	XDR *xdrs;
159226586Sdim	RPCBLIST **rp;
160226586Sdim{
161226586Sdim	bool_t	dummy;
162226586Sdim
163226586Sdim	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
164226586Sdim	return (dummy);
165226586Sdim}
166226586Sdim
167226586Sdim
168226586Sdimbool_t
169226586Sdimxdr_rpcb_entry(xdrs, objp)
170226586Sdim	XDR *xdrs;
171226586Sdim	rpcb_entry *objp;
172226586Sdim{
173226586Sdim	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
174226586Sdim		return (FALSE);
175226586Sdim	}
176226586Sdim	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
177249423Sdim		return (FALSE);
178249423Sdim	}
179249423Sdim	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
180249423Sdim		return (FALSE);
181249423Sdim	}
182341825Sdim	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
183341825Sdim		return (FALSE);
184226586Sdim	}
185249423Sdim	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
186249423Sdim		return (FALSE);
187249423Sdim	}
188249423Sdim	return (TRUE);
189249423Sdim}
190341825Sdim
191226586Sdimbool_t
192226586Sdimxdr_rpcb_entry_list_ptr(xdrs, rp)
193341825Sdim	XDR *xdrs;
194341825Sdim	rpcb_entry_list_ptr *rp;
195341825Sdim{
196296417Sdim	/*
197296417Sdim	 * more_elements is pre-computed in case the direction is
198296417Sdim	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
199296417Sdim	 * xdr_bool when the direction is XDR_DECODE.
200296417Sdim	 */
201296417Sdim	bool_t more_elements;
202296417Sdim	int freeing = (xdrs->x_op == XDR_FREE);
203341825Sdim	rpcb_entry_list_ptr next;
204341825Sdim	rpcb_entry_list_ptr next_copy;
205341825Sdim
206341825Sdim	next = NULL;
207296417Sdim	for (;;) {
208296417Sdim		more_elements = (bool_t)(*rp != NULL);
209296417Sdim		if (! xdr_bool(xdrs, &more_elements)) {
210296417Sdim			return (FALSE);
211296417Sdim		}
212296417Sdim		if (! more_elements) {
213341825Sdim			return (TRUE);  /* we are done */
214314564Sdim		}
215314564Sdim		/*
216296417Sdim		 * the unfortunate side effect of non-recursion is that in
217341825Sdim		 * the case of freeing we must remember the next object
218341825Sdim		 * before we free the current object ...
219341825Sdim		 */
220341825Sdim		if (freeing && *rp)
221341825Sdim			next = (*rp)->rpcb_entry_next;
222249423Sdim		if (! xdr_reference(xdrs, (caddr_t *)rp,
223249423Sdim		    (u_int)sizeof (rpcb_entry_list),
224314564Sdim				    (xdrproc_t)xdr_rpcb_entry)) {
225341825Sdim			return (FALSE);
226341825Sdim		}
227341825Sdim		if (freeing) {
228226586Sdim			next_copy = next;
229226586Sdim			rp = &next_copy;
230226586Sdim			/*
231226586Sdim			 * Note that in the subsequent iteration, next_copy
232226586Sdim			 * gets nulled out by the xdr_reference
233226586Sdim			 * but next itself survives.
234226586Sdim			 */
235226586Sdim		} else if (*rp) {
236226586Sdim			rp = &((*rp)->rpcb_entry_next);
237341825Sdim		}
238341825Sdim	}
239341825Sdim	/*NOTREACHED*/
240226586Sdim}
241341825Sdim
242341825Sdim/*
243341825Sdim * XDR remote call arguments
244226586Sdim * written for XDR_ENCODE direction only
245341825Sdim */
246341825Sdimbool_t
247226586Sdimxdr_rpcb_rmtcallargs(xdrs, p)
248341825Sdim	XDR *xdrs;
249341825Sdim	struct rpcb_rmtcallargs *p;
250341825Sdim{
251341825Sdim	struct r_rpcb_rmtcallargs *objp =
252341825Sdim	    (struct r_rpcb_rmtcallargs *)(void *)p;
253341825Sdim	u_int lenposition, argposition, position;
254341825Sdim	int32_t *buf;
255341825Sdim
256226586Sdim	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
257341825Sdim	if (buf == NULL) {
258341825Sdim		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
259341825Sdim			return (FALSE);
260341825Sdim		}
261226586Sdim		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
262341825Sdim			return (FALSE);
263341825Sdim		}
264341825Sdim		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
265249423Sdim			return (FALSE);
266249423Sdim		}
267249423Sdim	} else {
268249423Sdim		IXDR_PUT_U_INT32(buf, objp->prog);
269249423Sdim		IXDR_PUT_U_INT32(buf, objp->vers);
270249423Sdim		IXDR_PUT_U_INT32(buf, objp->proc);
271249423Sdim	}
272249423Sdim
273249423Sdim	/*
274249423Sdim	 * All the jugglery for just getting the size of the arguments
275249423Sdim	 */
276249423Sdim	lenposition = XDR_GETPOS(xdrs);
277249423Sdim	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
278249423Sdim		return (FALSE);
279249423Sdim	}
280314564Sdim	argposition = XDR_GETPOS(xdrs);
281261991Sdim	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
282341825Sdim		return (FALSE);
283249423Sdim	}
284249423Sdim	position = XDR_GETPOS(xdrs);
285276479Sdim	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
286276479Sdim	XDR_SETPOS(xdrs, lenposition);
287276479Sdim	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
288226586Sdim		return (FALSE);
289341825Sdim	}
290249423Sdim	XDR_SETPOS(xdrs, position);
291249423Sdim	return (TRUE);
292276479Sdim}
293276479Sdim
294276479Sdim/*
295249423Sdim * XDR remote call results
296226586Sdim * written for XDR_DECODE direction only
297226586Sdim */
298341825Sdimbool_t
299341825Sdimxdr_rpcb_rmtcallres(xdrs, p)
300226586Sdim	XDR *xdrs;
301341825Sdim	struct rpcb_rmtcallres *p;
302341825Sdim{
303341825Sdim	bool_t dummy;
304341825Sdim	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
305341825Sdim
306341825Sdim	if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
307341825Sdim		return (FALSE);
308226586Sdim	}
309226586Sdim	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
310226586Sdim		return (FALSE);
311314564Sdim	}
312226586Sdim	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
313226586Sdim	return (dummy);
314226586Sdim}
315226586Sdim
316226586Sdimbool_t
317226586Sdimxdr_netbuf(xdrs, objp)
318226586Sdim	XDR *xdrs;
319226586Sdim	struct netbuf *objp;
320239462Sdim{
321239462Sdim	bool_t dummy;
322239462Sdim	void **pp;
323226586Sdim
324226586Sdim	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
325226586Sdim		return (FALSE);
326226586Sdim	}
327243830Sdim	pp = &objp->buf;
328314564Sdim	dummy = xdr_bytes(xdrs, (char **) pp,
329234353Sdim			(u_int *)&(objp->len), objp->maxlen);
330226586Sdim	return (dummy);
331341825Sdim}
332234353Sdim