xref: /dflybsd-src/lib/libc/rpc/rpcb_prot.c (revision ba96d07f7c72db2df2f4855fd9cef54db7111960)
1*ba96d07fShrs /*-
2*ba96d07fShrs  * Copyright (c) 2009, Sun Microsystems, Inc.
3*ba96d07fShrs  * All rights reserved.
4ce0e08e2SPeter Avalos  *
5*ba96d07fShrs  * Redistribution and use in source and binary forms, with or without
6*ba96d07fShrs  * modification, are permitted provided that the following conditions are met:
7*ba96d07fShrs  * - Redistributions of source code must retain the above copyright notice,
8*ba96d07fShrs  *   this list of conditions and the following disclaimer.
9*ba96d07fShrs  * - Redistributions in binary form must reproduce the above copyright notice,
10*ba96d07fShrs  *   this list of conditions and the following disclaimer in the documentation
11*ba96d07fShrs  *   and/or other materials provided with the distribution.
12*ba96d07fShrs  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*ba96d07fShrs  *   contributors may be used to endorse or promote products derived
14*ba96d07fShrs  *   from this software without specific prior written permission.
15ce0e08e2SPeter Avalos  *
16*ba96d07fShrs  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*ba96d07fShrs  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*ba96d07fShrs  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*ba96d07fShrs  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*ba96d07fShrs  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*ba96d07fShrs  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*ba96d07fShrs  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*ba96d07fShrs  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*ba96d07fShrs  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*ba96d07fShrs  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*ba96d07fShrs  * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos  *
28ce0e08e2SPeter Avalos  * @(#)rpcb_prot.c	1.13	94/04/24 SMI; 1.9 89/04/21 Copyr 1984 Sun Micro
29ce0e08e2SPeter Avalos  * $NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $
30ce0e08e2SPeter Avalos  * $FreeBSD: src/lib/libc/rpc/rpcb_prot.c,v 1.7 2007/11/20 01:51:20 jb Exp $
31ce0e08e2SPeter Avalos  */
32ce0e08e2SPeter Avalos /*
33ce0e08e2SPeter Avalos  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34ce0e08e2SPeter Avalos  */
35ce0e08e2SPeter Avalos 
36ce0e08e2SPeter Avalos /*
37ce0e08e2SPeter Avalos  * rpcb_prot.c
38ce0e08e2SPeter Avalos  * XDR routines for the rpcbinder version 3.
39ce0e08e2SPeter Avalos  *
40ce0e08e2SPeter Avalos  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
41ce0e08e2SPeter Avalos  */
42ce0e08e2SPeter Avalos 
43ce0e08e2SPeter Avalos #include "namespace.h"
44ce0e08e2SPeter Avalos #include <rpc/rpc.h>
45ce0e08e2SPeter Avalos #include <rpc/types.h>
46ce0e08e2SPeter Avalos #include <rpc/xdr.h>
47ce0e08e2SPeter Avalos #include <rpc/rpcb_prot.h>
48ce0e08e2SPeter Avalos #include "un-namespace.h"
49ce0e08e2SPeter Avalos 
50ce0e08e2SPeter Avalos bool_t
51ce0e08e2SPeter Avalos xdr_rpcb(XDR *xdrs, RPCB *objp)
52ce0e08e2SPeter Avalos {
53ce0e08e2SPeter Avalos 	if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
54ce0e08e2SPeter Avalos 		return (FALSE);
55ce0e08e2SPeter Avalos 	}
56ce0e08e2SPeter Avalos 	if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
57ce0e08e2SPeter Avalos 		return (FALSE);
58ce0e08e2SPeter Avalos 	}
59ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
60ce0e08e2SPeter Avalos 		return (FALSE);
61ce0e08e2SPeter Avalos 	}
62ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
63ce0e08e2SPeter Avalos 		return (FALSE);
64ce0e08e2SPeter Avalos 	}
65ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
66ce0e08e2SPeter Avalos 		return (FALSE);
67ce0e08e2SPeter Avalos 	}
68ce0e08e2SPeter Avalos 	return (TRUE);
69ce0e08e2SPeter Avalos }
70ce0e08e2SPeter Avalos 
71ce0e08e2SPeter Avalos /*
72ce0e08e2SPeter Avalos  * rpcblist_ptr implements a linked list.  The RPCL definition from
73ce0e08e2SPeter Avalos  * rpcb_prot.x is:
74ce0e08e2SPeter Avalos  *
75ce0e08e2SPeter Avalos  * struct rpcblist {
76ce0e08e2SPeter Avalos  * 	rpcb		rpcb_map;
77ce0e08e2SPeter Avalos  *	struct rpcblist *rpcb_next;
78ce0e08e2SPeter Avalos  * };
79ce0e08e2SPeter Avalos  * typedef rpcblist *rpcblist_ptr;
80ce0e08e2SPeter Avalos  *
81ce0e08e2SPeter Avalos  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
82ce0e08e2SPeter Avalos  * there's any data behind the pointer, followed by the data (if any exists).
83ce0e08e2SPeter Avalos  * The boolean can be interpreted as ``more data follows me''; if FALSE then
84ce0e08e2SPeter Avalos  * nothing follows the boolean; if TRUE then the boolean is followed by an
85ce0e08e2SPeter Avalos  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
86ce0e08e2SPeter Avalos  * rpcblist *").
87ce0e08e2SPeter Avalos  *
88ce0e08e2SPeter Avalos  * This could be implemented via the xdr_pointer type, though this would
89ce0e08e2SPeter Avalos  * result in one recursive call per element in the list.  Rather than do that
90ce0e08e2SPeter Avalos  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
91ce0e08e2SPeter Avalos  * serialize the rpcb elements.
92ce0e08e2SPeter Avalos  */
93ce0e08e2SPeter Avalos 
94ce0e08e2SPeter Avalos bool_t
95ce0e08e2SPeter Avalos xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
96ce0e08e2SPeter Avalos {
97ce0e08e2SPeter Avalos 	/*
98ce0e08e2SPeter Avalos 	 * more_elements is pre-computed in case the direction is
99ce0e08e2SPeter Avalos 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
100ce0e08e2SPeter Avalos 	 * xdr_bool when the direction is XDR_DECODE.
101ce0e08e2SPeter Avalos 	 */
102ce0e08e2SPeter Avalos 	bool_t more_elements;
103ce0e08e2SPeter Avalos 	int freeing = (xdrs->x_op == XDR_FREE);
104ce0e08e2SPeter Avalos 	rpcblist_ptr next;
105ce0e08e2SPeter Avalos 	rpcblist_ptr next_copy;
106ce0e08e2SPeter Avalos 
107ce0e08e2SPeter Avalos 	next = NULL;
108ce0e08e2SPeter Avalos 	for (;;) {
109ce0e08e2SPeter Avalos 		more_elements = (bool_t)(*rp != NULL);
110ce0e08e2SPeter Avalos 		if (! xdr_bool(xdrs, &more_elements)) {
111ce0e08e2SPeter Avalos 			return (FALSE);
112ce0e08e2SPeter Avalos 		}
113ce0e08e2SPeter Avalos 		if (! more_elements) {
114ce0e08e2SPeter Avalos 			return (TRUE);  /* we are done */
115ce0e08e2SPeter Avalos 		}
116ce0e08e2SPeter Avalos 		/*
117ce0e08e2SPeter Avalos 		 * the unfortunate side effect of non-recursion is that in
118ce0e08e2SPeter Avalos 		 * the case of freeing we must remember the next object
119ce0e08e2SPeter Avalos 		 * before we free the current object ...
120ce0e08e2SPeter Avalos 		 */
121ce0e08e2SPeter Avalos 		if (freeing && *rp)
122ce0e08e2SPeter Avalos 			next = (*rp)->rpcb_next;
123ce0e08e2SPeter Avalos 		if (! xdr_reference(xdrs, (caddr_t *)rp,
124ce0e08e2SPeter Avalos 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
125ce0e08e2SPeter Avalos 			return (FALSE);
126ce0e08e2SPeter Avalos 		}
127ce0e08e2SPeter Avalos 		if (freeing) {
128ce0e08e2SPeter Avalos 			next_copy = next;
129ce0e08e2SPeter Avalos 			rp = &next_copy;
130ce0e08e2SPeter Avalos 			/*
131ce0e08e2SPeter Avalos 			 * Note that in the subsequent iteration, next_copy
132ce0e08e2SPeter Avalos 			 * gets nulled out by the xdr_reference
133ce0e08e2SPeter Avalos 			 * but next itself survives.
134ce0e08e2SPeter Avalos 			 */
135ce0e08e2SPeter Avalos 		} else if (*rp) {
136ce0e08e2SPeter Avalos 			rp = &((*rp)->rpcb_next);
137ce0e08e2SPeter Avalos 		}
138ce0e08e2SPeter Avalos 	}
139ce0e08e2SPeter Avalos 	/*NOTREACHED*/
140ce0e08e2SPeter Avalos }
141ce0e08e2SPeter Avalos 
142ce0e08e2SPeter Avalos /*
143ce0e08e2SPeter Avalos  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
144ce0e08e2SPeter Avalos  * functionality to xdr_rpcblist_ptr().
145ce0e08e2SPeter Avalos  */
146ce0e08e2SPeter Avalos bool_t
147ce0e08e2SPeter Avalos xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
148ce0e08e2SPeter Avalos {
149ce0e08e2SPeter Avalos 	bool_t	dummy;
150ce0e08e2SPeter Avalos 
151ce0e08e2SPeter Avalos 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
152ce0e08e2SPeter Avalos 	return (dummy);
153ce0e08e2SPeter Avalos }
154ce0e08e2SPeter Avalos 
155ce0e08e2SPeter Avalos 
156ce0e08e2SPeter Avalos bool_t
157ce0e08e2SPeter Avalos xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
158ce0e08e2SPeter Avalos {
159ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
160ce0e08e2SPeter Avalos 		return (FALSE);
161ce0e08e2SPeter Avalos 	}
162ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
163ce0e08e2SPeter Avalos 		return (FALSE);
164ce0e08e2SPeter Avalos 	}
165ce0e08e2SPeter Avalos 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
166ce0e08e2SPeter Avalos 		return (FALSE);
167ce0e08e2SPeter Avalos 	}
168ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
169ce0e08e2SPeter Avalos 		return (FALSE);
170ce0e08e2SPeter Avalos 	}
171ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
172ce0e08e2SPeter Avalos 		return (FALSE);
173ce0e08e2SPeter Avalos 	}
174ce0e08e2SPeter Avalos 	return (TRUE);
175ce0e08e2SPeter Avalos }
176ce0e08e2SPeter Avalos 
177ce0e08e2SPeter Avalos bool_t
178ce0e08e2SPeter Avalos xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
179ce0e08e2SPeter Avalos {
180ce0e08e2SPeter Avalos 	/*
181ce0e08e2SPeter Avalos 	 * more_elements is pre-computed in case the direction is
182ce0e08e2SPeter Avalos 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
183ce0e08e2SPeter Avalos 	 * xdr_bool when the direction is XDR_DECODE.
184ce0e08e2SPeter Avalos 	 */
185ce0e08e2SPeter Avalos 	bool_t more_elements;
186ce0e08e2SPeter Avalos 	int freeing = (xdrs->x_op == XDR_FREE);
187ce0e08e2SPeter Avalos 	rpcb_entry_list_ptr next;
188ce0e08e2SPeter Avalos 	rpcb_entry_list_ptr next_copy;
189ce0e08e2SPeter Avalos 
190ce0e08e2SPeter Avalos 	next = NULL;
191ce0e08e2SPeter Avalos 	for (;;) {
192ce0e08e2SPeter Avalos 		more_elements = (bool_t)(*rp != NULL);
193ce0e08e2SPeter Avalos 		if (! xdr_bool(xdrs, &more_elements)) {
194ce0e08e2SPeter Avalos 			return (FALSE);
195ce0e08e2SPeter Avalos 		}
196ce0e08e2SPeter Avalos 		if (! more_elements) {
197ce0e08e2SPeter Avalos 			return (TRUE);  /* we are done */
198ce0e08e2SPeter Avalos 		}
199ce0e08e2SPeter Avalos 		/*
200ce0e08e2SPeter Avalos 		 * the unfortunate side effect of non-recursion is that in
201ce0e08e2SPeter Avalos 		 * the case of freeing we must remember the next object
202ce0e08e2SPeter Avalos 		 * before we free the current object ...
203ce0e08e2SPeter Avalos 		 */
204ce0e08e2SPeter Avalos 		if (freeing)
205ce0e08e2SPeter Avalos 			next = (*rp)->rpcb_entry_next;
206ce0e08e2SPeter Avalos 		if (! xdr_reference(xdrs, (caddr_t *)rp,
207ce0e08e2SPeter Avalos 		    (u_int)sizeof (rpcb_entry_list),
208ce0e08e2SPeter Avalos 				    (xdrproc_t)xdr_rpcb_entry)) {
209ce0e08e2SPeter Avalos 			return (FALSE);
210ce0e08e2SPeter Avalos 		}
211ce0e08e2SPeter Avalos 		if (freeing && *rp) {
212ce0e08e2SPeter Avalos 			next_copy = next;
213ce0e08e2SPeter Avalos 			rp = &next_copy;
214ce0e08e2SPeter Avalos 			/*
215ce0e08e2SPeter Avalos 			 * Note that in the subsequent iteration, next_copy
216ce0e08e2SPeter Avalos 			 * gets nulled out by the xdr_reference
217ce0e08e2SPeter Avalos 			 * but next itself survives.
218ce0e08e2SPeter Avalos 			 */
219ce0e08e2SPeter Avalos 		} else if (*rp) {
220ce0e08e2SPeter Avalos 			rp = &((*rp)->rpcb_entry_next);
221ce0e08e2SPeter Avalos 		}
222ce0e08e2SPeter Avalos 	}
223ce0e08e2SPeter Avalos 	/*NOTREACHED*/
224ce0e08e2SPeter Avalos }
225ce0e08e2SPeter Avalos 
226ce0e08e2SPeter Avalos /*
227ce0e08e2SPeter Avalos  * XDR remote call arguments
228ce0e08e2SPeter Avalos  * written for XDR_ENCODE direction only
229ce0e08e2SPeter Avalos  */
230ce0e08e2SPeter Avalos bool_t
231ce0e08e2SPeter Avalos xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
232ce0e08e2SPeter Avalos {
233ce0e08e2SPeter Avalos 	struct r_rpcb_rmtcallargs *objp =
234ce0e08e2SPeter Avalos 	    (struct r_rpcb_rmtcallargs *)(void *)p;
235ce0e08e2SPeter Avalos 	u_int lenposition, argposition, position;
236ce0e08e2SPeter Avalos 	int32_t *buf;
237ce0e08e2SPeter Avalos 
238ce0e08e2SPeter Avalos 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
239ce0e08e2SPeter Avalos 	if (buf == NULL) {
240ce0e08e2SPeter Avalos 		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
241ce0e08e2SPeter Avalos 			return (FALSE);
242ce0e08e2SPeter Avalos 		}
243ce0e08e2SPeter Avalos 		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
244ce0e08e2SPeter Avalos 			return (FALSE);
245ce0e08e2SPeter Avalos 		}
246ce0e08e2SPeter Avalos 		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
247ce0e08e2SPeter Avalos 			return (FALSE);
248ce0e08e2SPeter Avalos 		}
249ce0e08e2SPeter Avalos 	} else {
250ce0e08e2SPeter Avalos 		IXDR_PUT_U_INT32(buf, objp->prog);
251ce0e08e2SPeter Avalos 		IXDR_PUT_U_INT32(buf, objp->vers);
252ce0e08e2SPeter Avalos 		IXDR_PUT_U_INT32(buf, objp->proc);
253ce0e08e2SPeter Avalos 	}
254ce0e08e2SPeter Avalos 
255ce0e08e2SPeter Avalos 	/*
256ce0e08e2SPeter Avalos 	 * All the jugglery for just getting the size of the arguments
257ce0e08e2SPeter Avalos 	 */
258ce0e08e2SPeter Avalos 	lenposition = XDR_GETPOS(xdrs);
259ce0e08e2SPeter Avalos 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
260ce0e08e2SPeter Avalos 		return (FALSE);
261ce0e08e2SPeter Avalos 	}
262ce0e08e2SPeter Avalos 	argposition = XDR_GETPOS(xdrs);
263ce0e08e2SPeter Avalos 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
264ce0e08e2SPeter Avalos 		return (FALSE);
265ce0e08e2SPeter Avalos 	}
266ce0e08e2SPeter Avalos 	position = XDR_GETPOS(xdrs);
267ce0e08e2SPeter Avalos 	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
268ce0e08e2SPeter Avalos 	XDR_SETPOS(xdrs, lenposition);
269ce0e08e2SPeter Avalos 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
270ce0e08e2SPeter Avalos 		return (FALSE);
271ce0e08e2SPeter Avalos 	}
272ce0e08e2SPeter Avalos 	XDR_SETPOS(xdrs, position);
273ce0e08e2SPeter Avalos 	return (TRUE);
274ce0e08e2SPeter Avalos }
275ce0e08e2SPeter Avalos 
276ce0e08e2SPeter Avalos /*
277ce0e08e2SPeter Avalos  * XDR remote call results
278ce0e08e2SPeter Avalos  * written for XDR_DECODE direction only
279ce0e08e2SPeter Avalos  */
280ce0e08e2SPeter Avalos bool_t
281ce0e08e2SPeter Avalos xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
282ce0e08e2SPeter Avalos {
283ce0e08e2SPeter Avalos 	bool_t dummy;
284ce0e08e2SPeter Avalos 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
285ce0e08e2SPeter Avalos 
286ce0e08e2SPeter Avalos 	if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
287ce0e08e2SPeter Avalos 		return (FALSE);
288ce0e08e2SPeter Avalos 	}
289ce0e08e2SPeter Avalos 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
290ce0e08e2SPeter Avalos 		return (FALSE);
291ce0e08e2SPeter Avalos 	}
292ce0e08e2SPeter Avalos 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
293ce0e08e2SPeter Avalos 	return (dummy);
294ce0e08e2SPeter Avalos }
295ce0e08e2SPeter Avalos 
296ce0e08e2SPeter Avalos bool_t
297ce0e08e2SPeter Avalos xdr_netbuf(XDR *xdrs, struct netbuf *objp)
298ce0e08e2SPeter Avalos {
299ce0e08e2SPeter Avalos 	bool_t dummy;
300ce0e08e2SPeter Avalos 	void **pp;
301ce0e08e2SPeter Avalos 
302ce0e08e2SPeter Avalos 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
303ce0e08e2SPeter Avalos 		return (FALSE);
304ce0e08e2SPeter Avalos 	}
305ce0e08e2SPeter Avalos 	pp = &objp->buf;
306ce0e08e2SPeter Avalos 	dummy = xdr_bytes(xdrs, (char **) pp,
307ce0e08e2SPeter Avalos 			(u_int *)&(objp->len), objp->maxlen);
308ce0e08e2SPeter Avalos 	return (dummy);
309ce0e08e2SPeter Avalos }
310