xref: /freebsd/lib/libc/rpc/rpcb_prot.c (revision 4f52dfbb)
1 /*	$NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
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.
19  *
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.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 /* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
37 
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
40 #endif
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 /*
45  * rpcb_prot.c
46  * XDR routines for the rpcbinder version 3.
47  *
48  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
49  */
50 
51 #include "namespace.h"
52 #include <rpc/rpc.h>
53 #include <rpc/types.h>
54 #include <rpc/xdr.h>
55 #include <rpc/rpcb_prot.h>
56 #include <rpc/rpc_com.h>
57 #include "un-namespace.h"
58 
59 bool_t
60 xdr_rpcb(XDR *xdrs, RPCB *objp)
61 {
62 	if (!xdr_rpcprog(xdrs, &objp->r_prog)) {
63 		return (FALSE);
64 	}
65 	if (!xdr_rpcvers(xdrs, &objp->r_vers)) {
66 		return (FALSE);
67 	}
68 	if (!xdr_string(xdrs, &objp->r_netid, RPC_MAXDATASIZE)) {
69 		return (FALSE);
70 	}
71 	if (!xdr_string(xdrs, &objp->r_addr, RPC_MAXDATASIZE)) {
72 		return (FALSE);
73 	}
74 	if (!xdr_string(xdrs, &objp->r_owner, RPC_MAXDATASIZE)) {
75 		return (FALSE);
76 	}
77 	return (TRUE);
78 }
79 
80 /*
81  * rpcblist_ptr implements a linked list.  The RPCL definition from
82  * rpcb_prot.x is:
83  *
84  * struct rpcblist {
85  * 	rpcb		rpcb_map;
86  *	struct rpcblist *rpcb_next;
87  * };
88  * typedef rpcblist *rpcblist_ptr;
89  *
90  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
91  * there's any data behind the pointer, followed by the data (if any exists).
92  * The boolean can be interpreted as ``more data follows me''; if FALSE then
93  * nothing follows the boolean; if TRUE then the boolean is followed by an
94  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
95  * rpcblist *").
96  *
97  * This could be implemented via the xdr_pointer type, though this would
98  * result in one recursive call per element in the list.  Rather than do that
99  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
100  * serialize the rpcb elements.
101  */
102 
103 bool_t
104 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
105 {
106 	/*
107 	 * more_elements is pre-computed in case the direction is
108 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
109 	 * xdr_bool when the direction is XDR_DECODE.
110 	 */
111 	bool_t more_elements;
112 	int freeing = (xdrs->x_op == XDR_FREE);
113 	rpcblist_ptr next;
114 	rpcblist_ptr next_copy;
115 
116 	next = NULL;
117 	for (;;) {
118 		more_elements = (bool_t)(*rp != NULL);
119 		if (! xdr_bool(xdrs, &more_elements)) {
120 			return (FALSE);
121 		}
122 		if (! more_elements) {
123 			return (TRUE);  /* we are done */
124 		}
125 		/*
126 		 * the unfortunate side effect of non-recursion is that in
127 		 * the case of freeing we must remember the next object
128 		 * before we free the current object ...
129 		 */
130 		if (freeing && *rp)
131 			next = (*rp)->rpcb_next;
132 		if (! xdr_reference(xdrs, (caddr_t *)rp,
133 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
134 			return (FALSE);
135 		}
136 		if (freeing) {
137 			next_copy = next;
138 			rp = &next_copy;
139 			/*
140 			 * Note that in the subsequent iteration, next_copy
141 			 * gets nulled out by the xdr_reference
142 			 * but next itself survives.
143 			 */
144 		} else if (*rp) {
145 			rp = &((*rp)->rpcb_next);
146 		}
147 	}
148 	/*NOTREACHED*/
149 }
150 
151 /*
152  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
153  * functionality to xdr_rpcblist_ptr().
154  */
155 bool_t
156 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
157 {
158 	bool_t	dummy;
159 
160 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
161 	return (dummy);
162 }
163 
164 
165 bool_t
166 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
167 {
168 	if (!xdr_string(xdrs, &objp->r_maddr, RPC_MAXDATASIZE)) {
169 		return (FALSE);
170 	}
171 	if (!xdr_string(xdrs, &objp->r_nc_netid, RPC_MAXDATASIZE)) {
172 		return (FALSE);
173 	}
174 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
175 		return (FALSE);
176 	}
177 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, RPC_MAXDATASIZE)) {
178 		return (FALSE);
179 	}
180 	if (!xdr_string(xdrs, &objp->r_nc_proto, RPC_MAXDATASIZE)) {
181 		return (FALSE);
182 	}
183 	return (TRUE);
184 }
185 
186 bool_t
187 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
188 {
189 	/*
190 	 * more_elements is pre-computed in case the direction is
191 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
192 	 * xdr_bool when the direction is XDR_DECODE.
193 	 */
194 	bool_t more_elements;
195 	int freeing = (xdrs->x_op == XDR_FREE);
196 	rpcb_entry_list_ptr next;
197 	rpcb_entry_list_ptr next_copy;
198 
199 	next = NULL;
200 	for (;;) {
201 		more_elements = (bool_t)(*rp != NULL);
202 		if (! xdr_bool(xdrs, &more_elements)) {
203 			return (FALSE);
204 		}
205 		if (! more_elements) {
206 			return (TRUE);  /* we are done */
207 		}
208 		/*
209 		 * the unfortunate side effect of non-recursion is that in
210 		 * the case of freeing we must remember the next object
211 		 * before we free the current object ...
212 		 */
213 		if (freeing && *rp)
214 			next = (*rp)->rpcb_entry_next;
215 		if (! xdr_reference(xdrs, (caddr_t *)rp,
216 		    (u_int)sizeof (rpcb_entry_list),
217 				    (xdrproc_t)xdr_rpcb_entry)) {
218 			return (FALSE);
219 		}
220 		if (freeing) {
221 			next_copy = next;
222 			rp = &next_copy;
223 			/*
224 			 * Note that in the subsequent iteration, next_copy
225 			 * gets nulled out by the xdr_reference
226 			 * but next itself survives.
227 			 */
228 		} else if (*rp) {
229 			rp = &((*rp)->rpcb_entry_next);
230 		}
231 	}
232 	/*NOTREACHED*/
233 }
234 
235 /*
236  * XDR remote call arguments
237  * written for XDR_ENCODE direction only
238  */
239 bool_t
240 xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
241 {
242 	struct r_rpcb_rmtcallargs *objp =
243 	    (struct r_rpcb_rmtcallargs *)(void *)p;
244 	u_int lenposition, argposition, position;
245 	int32_t *buf;
246 
247 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
248 	if (buf == NULL) {
249 		if (!xdr_rpcprog(xdrs, &objp->prog)) {
250 			return (FALSE);
251 		}
252 		if (!xdr_rpcvers(xdrs, &objp->vers)) {
253 			return (FALSE);
254 		}
255 		if (!xdr_rpcproc(xdrs, &objp->proc)) {
256 			return (FALSE);
257 		}
258 	} else {
259 		IXDR_PUT_U_INT32(buf, objp->prog);
260 		IXDR_PUT_U_INT32(buf, objp->vers);
261 		IXDR_PUT_U_INT32(buf, objp->proc);
262 	}
263 
264 	/*
265 	 * All the jugglery for just getting the size of the arguments
266 	 */
267 	lenposition = XDR_GETPOS(xdrs);
268 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
269 		return (FALSE);
270 	}
271 	argposition = XDR_GETPOS(xdrs);
272 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
273 		return (FALSE);
274 	}
275 	position = XDR_GETPOS(xdrs);
276 	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
277 	XDR_SETPOS(xdrs, lenposition);
278 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
279 		return (FALSE);
280 	}
281 	XDR_SETPOS(xdrs, position);
282 	return (TRUE);
283 }
284 
285 /*
286  * XDR remote call results
287  * written for XDR_DECODE direction only
288  */
289 bool_t
290 xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
291 {
292 	bool_t dummy;
293 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
294 
295 	if (!xdr_string(xdrs, &objp->addr, RPC_MAXDATASIZE)) {
296 		return (FALSE);
297 	}
298 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
299 		return (FALSE);
300 	}
301 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
302 	return (dummy);
303 }
304 
305 bool_t
306 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
307 {
308 	bool_t dummy;
309 	void **pp;
310 
311 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
312 		return (FALSE);
313 	}
314 	pp = &objp->buf;
315 	dummy = xdr_bytes(xdrs, (char **) pp,
316 			(u_int *)&(objp->len), objp->maxlen);
317 	return (dummy);
318 }
319