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