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