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