xref: /netbsd-src/lib/libc/rpc/rpcb_prot.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: rpcb_prot.c,v 1.6 2005/06/01 05:46:35 lukem 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.6 2005/06/01 05:46:35 lukem 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 	next = NULL;
143 
144 	for (;;) {
145 		more_elements = (bool_t)(*rp != NULL);
146 		if (! xdr_bool(xdrs, &more_elements)) {
147 			return (FALSE);
148 		}
149 		if (! more_elements) {
150 			return (TRUE);  /* we are done */
151 		}
152 		/*
153 		 * the unfortunate side effect of non-recursion is that in
154 		 * the case of freeing we must remember the next object
155 		 * before we free the current object ...
156 		 */
157 		if (freeing)
158 			next = (*rp)->rpcb_next;
159 		if (! xdr_reference(xdrs, (caddr_t *)rp,
160 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
161 			return (FALSE);
162 		}
163 		if (freeing) {
164 			next_copy = next;
165 			rp = &next_copy;
166 			/*
167 			 * Note that in the subsequent iteration, next_copy
168 			 * gets nulled out by the xdr_reference
169 			 * but next itself survives.
170 			 */
171 		} else {
172 			rp = &((*rp)->rpcb_next);
173 		}
174 	}
175 	/*NOTREACHED*/
176 }
177 
178 /*
179  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
180  * functionality to xdr_rpcblist_ptr().
181  */
182 bool_t
183 xdr_rpcblist(xdrs, rp)
184 	XDR *xdrs;
185 	RPCBLIST **rp;
186 {
187 	bool_t	dummy;
188 
189 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
190 	return (dummy);
191 }
192 
193 
194 bool_t
195 xdr_rpcb_entry(xdrs, objp)
196 	XDR *xdrs;
197 	rpcb_entry *objp;
198 {
199 
200 	_DIAGASSERT(objp != NULL);
201 
202 	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
203 		return (FALSE);
204 	}
205 	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
206 		return (FALSE);
207 	}
208 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
209 		return (FALSE);
210 	}
211 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
212 		return (FALSE);
213 	}
214 	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
215 		return (FALSE);
216 	}
217 	return (TRUE);
218 }
219 
220 bool_t
221 xdr_rpcb_entry_list_ptr(xdrs, rp)
222 	XDR *xdrs;
223 	rpcb_entry_list_ptr *rp;
224 {
225 	/*
226 	 * more_elements is pre-computed in case the direction is
227 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
228 	 * xdr_bool when the direction is XDR_DECODE.
229 	 */
230 	bool_t more_elements;
231 	int freeing;
232 	rpcb_entry_list_ptr next;
233 	rpcb_entry_list_ptr next_copy;
234 
235 	_DIAGASSERT(xdrs != NULL);
236 	/* XXX: rp is allowed to be NULL ??? */
237 
238 	freeing = (xdrs->x_op == XDR_FREE);
239 	next = NULL;
240 
241 	for (;;) {
242 		more_elements = (bool_t)(*rp != NULL);
243 		if (! xdr_bool(xdrs, &more_elements)) {
244 			return (FALSE);
245 		}
246 		if (! more_elements) {
247 			return (TRUE);  /* we are done */
248 		}
249 		/*
250 		 * the unfortunate side effect of non-recursion is that in
251 		 * the case of freeing we must remember the next object
252 		 * before we free the current object ...
253 		 */
254 		if (freeing)
255 			next = (*rp)->rpcb_entry_next;
256 		if (! xdr_reference(xdrs, (caddr_t *)rp,
257 		    (u_int)sizeof (rpcb_entry_list),
258 				    (xdrproc_t)xdr_rpcb_entry)) {
259 			return (FALSE);
260 		}
261 		if (freeing) {
262 			next_copy = next;
263 			rp = &next_copy;
264 			/*
265 			 * Note that in the subsequent iteration, next_copy
266 			 * gets nulled out by the xdr_reference
267 			 * but next itself survives.
268 			 */
269 		} else {
270 			rp = &((*rp)->rpcb_entry_next);
271 		}
272 	}
273 	/*NOTREACHED*/
274 }
275 
276 /*
277  * XDR remote call arguments
278  * written for XDR_ENCODE direction only
279  */
280 bool_t
281 xdr_rpcb_rmtcallargs(xdrs, p)
282 	XDR *xdrs;
283 	struct rpcb_rmtcallargs *p;
284 {
285 	struct r_rpcb_rmtcallargs *objp =
286 	    (struct r_rpcb_rmtcallargs *)(void *)p;
287 	u_int lenposition, argposition, position;
288 	int32_t *buf;
289 
290 	_DIAGASSERT(p != NULL);
291 
292 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
293 	if (buf == NULL) {
294 		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
295 			return (FALSE);
296 		}
297 		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
298 			return (FALSE);
299 		}
300 		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
301 			return (FALSE);
302 		}
303 	} else {
304 		IXDR_PUT_U_INT32(buf, objp->prog);
305 		IXDR_PUT_U_INT32(buf, objp->vers);
306 		IXDR_PUT_U_INT32(buf, objp->proc);
307 	}
308 
309 	/*
310 	 * All the jugglery for just getting the size of the arguments
311 	 */
312 	lenposition = XDR_GETPOS(xdrs);
313 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
314 		return (FALSE);
315 	}
316 	argposition = XDR_GETPOS(xdrs);
317 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
318 		return (FALSE);
319 	}
320 	position = XDR_GETPOS(xdrs);
321 	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
322 	XDR_SETPOS(xdrs, lenposition);
323 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
324 		return (FALSE);
325 	}
326 	XDR_SETPOS(xdrs, position);
327 	return (TRUE);
328 }
329 
330 /*
331  * XDR remote call results
332  * written for XDR_DECODE direction only
333  */
334 bool_t
335 xdr_rpcb_rmtcallres(xdrs, p)
336 	XDR *xdrs;
337 	struct rpcb_rmtcallres *p;
338 {
339 	bool_t dummy;
340 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
341 
342 	_DIAGASSERT(p != NULL);
343 
344 	if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
345 		return (FALSE);
346 	}
347 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
348 		return (FALSE);
349 	}
350 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
351 	return (dummy);
352 }
353 
354 bool_t
355 xdr_netbuf(xdrs, objp)
356 	XDR *xdrs;
357 	struct netbuf *objp;
358 {
359 	bool_t dummy;
360 
361 	_DIAGASSERT(objp != NULL);
362 
363 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
364 		return (FALSE);
365 	}
366 	dummy = xdr_bytes(xdrs, (char **)&(objp->buf),
367 			(u_int *)&(objp->len), objp->maxlen);
368 	return (dummy);
369 }
370