xref: /netbsd-src/lib/libc/rpc/rpcb_prot.c (revision 21e37cc72a480a47828990a439cde7ac9ffaf0c6)
1 /*	$NetBSD: rpcb_prot.c,v 1.5 2003/09/09 03:56:40 itojun Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 /*
32  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
33  */
34 
35 /* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
36 
37 #include <sys/cdefs.h>
38 #if defined(LIBC_SCCS) && !defined(lint)
39 #if 0
40 static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
41 #else
42 __RCSID("$NetBSD: rpcb_prot.c,v 1.5 2003/09/09 03:56:40 itojun Exp $");
43 #endif
44 #endif
45 
46 /*
47  * rpcb_prot.c
48  * XDR routines for the rpcbinder version 3.
49  *
50  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
51  */
52 
53 #include "namespace.h"
54 
55 #include <rpc/rpc.h>
56 #include <rpc/types.h>
57 #include <rpc/xdr.h>
58 #include <rpc/rpcb_prot.h>
59 
60 #include <assert.h>
61 
62 #ifdef __weak_alias
63 __weak_alias(xdr_rpcb,_xdr_rpcb)
64 __weak_alias(xdr_rpcblist_ptr,_xdr_rpcblist_ptr)
65 __weak_alias(xdr_rpcblist,_xdr_rpcblist)
66 __weak_alias(xdr_rpcb_entry,_xdr_rpcb_entry)
67 __weak_alias(xdr_rpcb_entry_list_ptr,_xdr_rpcb_entry_list_ptr)
68 __weak_alias(xdr_rpcb_rmtcallargs,_xdr_rpcb_rmtcallargs)
69 __weak_alias(xdr_rpcb_rmtcallres,_xdr_rpcb_rmtcallres)
70 __weak_alias(xdr_netbuf,_xdr_netbuf)
71 #endif
72 
73 
74 bool_t
75 xdr_rpcb(xdrs, objp)
76 	XDR *xdrs;
77 	RPCB *objp;
78 {
79 
80 	_DIAGASSERT(objp != NULL);
81 
82 	if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
83 		return (FALSE);
84 	}
85 	if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
86 		return (FALSE);
87 	}
88 	if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
89 		return (FALSE);
90 	}
91 	if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
92 		return (FALSE);
93 	}
94 	if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
95 		return (FALSE);
96 	}
97 	return (TRUE);
98 }
99 
100 /*
101  * rpcblist_ptr implements a linked list.  The RPCL definition from
102  * rpcb_prot.x is:
103  *
104  * struct rpcblist {
105  * 	rpcb		rpcb_map;
106  *	struct rpcblist *rpcb_next;
107  * };
108  * typedef rpcblist *rpcblist_ptr;
109  *
110  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
111  * there's any data behind the pointer, followed by the data (if any exists).
112  * The boolean can be interpreted as ``more data follows me''; if FALSE then
113  * nothing follows the boolean; if TRUE then the boolean is followed by an
114  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
115  * rpcblist *").
116  *
117  * This could be implemented via the xdr_pointer type, though this would
118  * result in one recursive call per element in the list.  Rather than do that
119  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
120  * serialize the rpcb elements.
121  */
122 
123 bool_t
124 xdr_rpcblist_ptr(xdrs, rp)
125 	XDR *xdrs;
126 	rpcblist_ptr *rp;
127 {
128 	/*
129 	 * more_elements is pre-computed in case the direction is
130 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
131 	 * xdr_bool when the direction is XDR_DECODE.
132 	 */
133 	bool_t more_elements;
134 	int freeing;
135 	rpcblist_ptr next;
136 	rpcblist_ptr next_copy;
137 
138 	_DIAGASSERT(xdrs != NULL);
139 	/* XXX: rp may be NULL ??? */
140 
141 	freeing = (xdrs->x_op == XDR_FREE);
142 
143 	for (;;) {
144 		more_elements = (bool_t)(*rp != NULL);
145 		if (! xdr_bool(xdrs, &more_elements)) {
146 			return (FALSE);
147 		}
148 		if (! more_elements) {
149 			return (TRUE);  /* we are done */
150 		}
151 		/*
152 		 * the unfortunate side effect of non-recursion is that in
153 		 * the case of freeing we must remember the next object
154 		 * before we free the current object ...
155 		 */
156 		if (freeing)
157 			next = (*rp)->rpcb_next;
158 		if (! xdr_reference(xdrs, (caddr_t *)rp,
159 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
160 			return (FALSE);
161 		}
162 		if (freeing) {
163 			next_copy = next;
164 			rp = &next_copy;
165 			/*
166 			 * Note that in the subsequent iteration, next_copy
167 			 * gets nulled out by the xdr_reference
168 			 * but next itself survives.
169 			 */
170 		} else {
171 			rp = &((*rp)->rpcb_next);
172 		}
173 	}
174 	/*NOTREACHED*/
175 }
176 
177 /*
178  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
179  * functionality to xdr_rpcblist_ptr().
180  */
181 bool_t
182 xdr_rpcblist(xdrs, rp)
183 	XDR *xdrs;
184 	RPCBLIST **rp;
185 {
186 	bool_t	dummy;
187 
188 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
189 	return (dummy);
190 }
191 
192 
193 bool_t
194 xdr_rpcb_entry(xdrs, objp)
195 	XDR *xdrs;
196 	rpcb_entry *objp;
197 {
198 
199 	_DIAGASSERT(objp != NULL);
200 
201 	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
202 		return (FALSE);
203 	}
204 	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
205 		return (FALSE);
206 	}
207 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
208 		return (FALSE);
209 	}
210 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
211 		return (FALSE);
212 	}
213 	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
214 		return (FALSE);
215 	}
216 	return (TRUE);
217 }
218 
219 bool_t
220 xdr_rpcb_entry_list_ptr(xdrs, rp)
221 	XDR *xdrs;
222 	rpcb_entry_list_ptr *rp;
223 {
224 	/*
225 	 * more_elements is pre-computed in case the direction is
226 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
227 	 * xdr_bool when the direction is XDR_DECODE.
228 	 */
229 	bool_t more_elements;
230 	int freeing;
231 	rpcb_entry_list_ptr next;
232 	rpcb_entry_list_ptr next_copy;
233 
234 	_DIAGASSERT(xdrs != NULL);
235 	/* XXX: rp is allowed to be NULL ??? */
236 
237 	freeing = (xdrs->x_op == XDR_FREE);
238 
239 	for (;;) {
240 		more_elements = (bool_t)(*rp != NULL);
241 		if (! xdr_bool(xdrs, &more_elements)) {
242 			return (FALSE);
243 		}
244 		if (! more_elements) {
245 			return (TRUE);  /* we are done */
246 		}
247 		/*
248 		 * the unfortunate side effect of non-recursion is that in
249 		 * the case of freeing we must remember the next object
250 		 * before we free the current object ...
251 		 */
252 		if (freeing)
253 			next = (*rp)->rpcb_entry_next;
254 		if (! xdr_reference(xdrs, (caddr_t *)rp,
255 		    (u_int)sizeof (rpcb_entry_list),
256 				    (xdrproc_t)xdr_rpcb_entry)) {
257 			return (FALSE);
258 		}
259 		if (freeing) {
260 			next_copy = next;
261 			rp = &next_copy;
262 			/*
263 			 * Note that in the subsequent iteration, next_copy
264 			 * gets nulled out by the xdr_reference
265 			 * but next itself survives.
266 			 */
267 		} else {
268 			rp = &((*rp)->rpcb_entry_next);
269 		}
270 	}
271 	/*NOTREACHED*/
272 }
273 
274 /*
275  * XDR remote call arguments
276  * written for XDR_ENCODE direction only
277  */
278 bool_t
279 xdr_rpcb_rmtcallargs(xdrs, p)
280 	XDR *xdrs;
281 	struct rpcb_rmtcallargs *p;
282 {
283 	struct r_rpcb_rmtcallargs *objp =
284 	    (struct r_rpcb_rmtcallargs *)(void *)p;
285 	u_int lenposition, argposition, position;
286 	int32_t *buf;
287 
288 	_DIAGASSERT(p != NULL);
289 
290 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
291 	if (buf == NULL) {
292 		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
293 			return (FALSE);
294 		}
295 		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
296 			return (FALSE);
297 		}
298 		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
299 			return (FALSE);
300 		}
301 	} else {
302 		IXDR_PUT_U_INT32(buf, objp->prog);
303 		IXDR_PUT_U_INT32(buf, objp->vers);
304 		IXDR_PUT_U_INT32(buf, objp->proc);
305 	}
306 
307 	/*
308 	 * All the jugglery for just getting the size of the arguments
309 	 */
310 	lenposition = XDR_GETPOS(xdrs);
311 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
312 		return (FALSE);
313 	}
314 	argposition = XDR_GETPOS(xdrs);
315 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
316 		return (FALSE);
317 	}
318 	position = XDR_GETPOS(xdrs);
319 	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
320 	XDR_SETPOS(xdrs, lenposition);
321 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
322 		return (FALSE);
323 	}
324 	XDR_SETPOS(xdrs, position);
325 	return (TRUE);
326 }
327 
328 /*
329  * XDR remote call results
330  * written for XDR_DECODE direction only
331  */
332 bool_t
333 xdr_rpcb_rmtcallres(xdrs, p)
334 	XDR *xdrs;
335 	struct rpcb_rmtcallres *p;
336 {
337 	bool_t dummy;
338 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
339 
340 	_DIAGASSERT(p != NULL);
341 
342 	if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
343 		return (FALSE);
344 	}
345 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
346 		return (FALSE);
347 	}
348 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
349 	return (dummy);
350 }
351 
352 bool_t
353 xdr_netbuf(xdrs, objp)
354 	XDR *xdrs;
355 	struct netbuf *objp;
356 {
357 	bool_t dummy;
358 
359 	_DIAGASSERT(objp != NULL);
360 
361 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
362 		return (FALSE);
363 	}
364 	dummy = xdr_bytes(xdrs, (char **)&(objp->buf),
365 			(u_int *)&(objp->len), objp->maxlen);
366 	return (dummy);
367 }
368