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