xref: /dragonfly/lib/libc/rpc/rpcb_prot.c (revision 684cb317)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)rpcb_prot.c	1.13	94/04/24 SMI; 1.9 89/04/21 Copyr 1984 Sun Micro
29  * $NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $
30  * $FreeBSD: src/lib/libc/rpc/rpcb_prot.c,v 1.7 2007/11/20 01:51:20 jb Exp $
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 /*
37  * rpcb_prot.c
38  * XDR routines for the rpcbinder version 3.
39  *
40  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
41  */
42 
43 #include "namespace.h"
44 #include <rpc/rpc.h>
45 #include <rpc/types.h>
46 #include <rpc/xdr.h>
47 #include <rpc/rpcb_prot.h>
48 #include "un-namespace.h"
49 
50 bool_t
51 xdr_rpcb(XDR *xdrs, RPCB *objp)
52 {
53 	if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
54 		return (FALSE);
55 	}
56 	if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
57 		return (FALSE);
58 	}
59 	if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
60 		return (FALSE);
61 	}
62 	if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
63 		return (FALSE);
64 	}
65 	if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
66 		return (FALSE);
67 	}
68 	return (TRUE);
69 }
70 
71 /*
72  * rpcblist_ptr implements a linked list.  The RPCL definition from
73  * rpcb_prot.x is:
74  *
75  * struct rpcblist {
76  * 	rpcb		rpcb_map;
77  *	struct rpcblist *rpcb_next;
78  * };
79  * typedef rpcblist *rpcblist_ptr;
80  *
81  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
82  * there's any data behind the pointer, followed by the data (if any exists).
83  * The boolean can be interpreted as ``more data follows me''; if FALSE then
84  * nothing follows the boolean; if TRUE then the boolean is followed by an
85  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
86  * rpcblist *").
87  *
88  * This could be implemented via the xdr_pointer type, though this would
89  * result in one recursive call per element in the list.  Rather than do that
90  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
91  * serialize the rpcb elements.
92  */
93 
94 bool_t
95 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
96 {
97 	/*
98 	 * more_elements is pre-computed in case the direction is
99 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
100 	 * xdr_bool when the direction is XDR_DECODE.
101 	 */
102 	bool_t more_elements;
103 	int freeing = (xdrs->x_op == XDR_FREE);
104 	rpcblist_ptr next;
105 	rpcblist_ptr next_copy;
106 
107 	next = NULL;
108 	for (;;) {
109 		more_elements = (bool_t)(*rp != NULL);
110 		if (! xdr_bool(xdrs, &more_elements)) {
111 			return (FALSE);
112 		}
113 		if (! more_elements) {
114 			return (TRUE);  /* we are done */
115 		}
116 		/*
117 		 * the unfortunate side effect of non-recursion is that in
118 		 * the case of freeing we must remember the next object
119 		 * before we free the current object ...
120 		 */
121 		if (freeing && *rp)
122 			next = (*rp)->rpcb_next;
123 		if (! xdr_reference(xdrs, (caddr_t *)rp,
124 		    (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
125 			return (FALSE);
126 		}
127 		if (freeing) {
128 			next_copy = next;
129 			rp = &next_copy;
130 			/*
131 			 * Note that in the subsequent iteration, next_copy
132 			 * gets nulled out by the xdr_reference
133 			 * but next itself survives.
134 			 */
135 		} else if (*rp) {
136 			rp = &((*rp)->rpcb_next);
137 		}
138 	}
139 	/*NOTREACHED*/
140 }
141 
142 /*
143  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
144  * functionality to xdr_rpcblist_ptr().
145  */
146 bool_t
147 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
148 {
149 	bool_t	dummy;
150 
151 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
152 	return (dummy);
153 }
154 
155 
156 bool_t
157 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
158 {
159 	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
160 		return (FALSE);
161 	}
162 	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
163 		return (FALSE);
164 	}
165 	if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
166 		return (FALSE);
167 	}
168 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
169 		return (FALSE);
170 	}
171 	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
172 		return (FALSE);
173 	}
174 	return (TRUE);
175 }
176 
177 bool_t
178 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
179 {
180 	/*
181 	 * more_elements is pre-computed in case the direction is
182 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
183 	 * xdr_bool when the direction is XDR_DECODE.
184 	 */
185 	bool_t more_elements;
186 	int freeing = (xdrs->x_op == XDR_FREE);
187 	rpcb_entry_list_ptr next;
188 	rpcb_entry_list_ptr next_copy;
189 
190 	next = NULL;
191 	for (;;) {
192 		more_elements = (bool_t)(*rp != NULL);
193 		if (! xdr_bool(xdrs, &more_elements)) {
194 			return (FALSE);
195 		}
196 		if (! more_elements) {
197 			return (TRUE);  /* we are done */
198 		}
199 		/*
200 		 * the unfortunate side effect of non-recursion is that in
201 		 * the case of freeing we must remember the next object
202 		 * before we free the current object ...
203 		 */
204 		if (freeing)
205 			next = (*rp)->rpcb_entry_next;
206 		if (! xdr_reference(xdrs, (caddr_t *)rp,
207 		    (u_int)sizeof (rpcb_entry_list),
208 				    (xdrproc_t)xdr_rpcb_entry)) {
209 			return (FALSE);
210 		}
211 		if (freeing && *rp) {
212 			next_copy = next;
213 			rp = &next_copy;
214 			/*
215 			 * Note that in the subsequent iteration, next_copy
216 			 * gets nulled out by the xdr_reference
217 			 * but next itself survives.
218 			 */
219 		} else if (*rp) {
220 			rp = &((*rp)->rpcb_entry_next);
221 		}
222 	}
223 	/*NOTREACHED*/
224 }
225 
226 /*
227  * XDR remote call arguments
228  * written for XDR_ENCODE direction only
229  */
230 bool_t
231 xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
232 {
233 	struct r_rpcb_rmtcallargs *objp =
234 	    (struct r_rpcb_rmtcallargs *)(void *)p;
235 	u_int lenposition, argposition, position;
236 	int32_t *buf;
237 
238 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
239 	if (buf == NULL) {
240 		if (!xdr_u_int32_t(xdrs, &objp->prog)) {
241 			return (FALSE);
242 		}
243 		if (!xdr_u_int32_t(xdrs, &objp->vers)) {
244 			return (FALSE);
245 		}
246 		if (!xdr_u_int32_t(xdrs, &objp->proc)) {
247 			return (FALSE);
248 		}
249 	} else {
250 		IXDR_PUT_U_INT32(buf, objp->prog);
251 		IXDR_PUT_U_INT32(buf, objp->vers);
252 		IXDR_PUT_U_INT32(buf, objp->proc);
253 	}
254 
255 	/*
256 	 * All the jugglery for just getting the size of the arguments
257 	 */
258 	lenposition = XDR_GETPOS(xdrs);
259 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
260 		return (FALSE);
261 	}
262 	argposition = XDR_GETPOS(xdrs);
263 	if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
264 		return (FALSE);
265 	}
266 	position = XDR_GETPOS(xdrs);
267 	objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
268 	XDR_SETPOS(xdrs, lenposition);
269 	if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
270 		return (FALSE);
271 	}
272 	XDR_SETPOS(xdrs, position);
273 	return (TRUE);
274 }
275 
276 /*
277  * XDR remote call results
278  * written for XDR_DECODE direction only
279  */
280 bool_t
281 xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
282 {
283 	bool_t dummy;
284 	struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
285 
286 	if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
287 		return (FALSE);
288 	}
289 	if (!xdr_u_int(xdrs, &objp->results.results_len)) {
290 		return (FALSE);
291 	}
292 	dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
293 	return (dummy);
294 }
295 
296 bool_t
297 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
298 {
299 	bool_t dummy;
300 	void **pp;
301 
302 	if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
303 		return (FALSE);
304 	}
305 	pp = &objp->buf;
306 	dummy = xdr_bytes(xdrs, (char **) pp,
307 			(u_int *)&(objp->len), objp->maxlen);
308 	return (dummy);
309 }
310