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