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