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