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