Merge branches 'master' and 'suser_to_priv'
[dragonfly.git] / lib / libc / rpc / rpcb_prot.c
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  *
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  *
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  *
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  *
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  *
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  *
29  * @(#)rpcb_prot.c      1.13    94/04/24 SMI; 1.9 89/04/21 Copyr 1984 Sun Micro
30  * $NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $
31  * $FreeBSD: src/lib/libc/rpc/rpcb_prot.c,v 1.7 2007/11/20 01:51:20 jb Exp $
32  * $DragonFly$
33  */
34 /*
35  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
36  */
37
38 /*
39  * rpcb_prot.c
40  * XDR routines for the rpcbinder version 3.
41  *
42  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
43  */
44
45 #include "namespace.h"
46 #include <rpc/rpc.h>
47 #include <rpc/types.h>
48 #include <rpc/xdr.h>
49 #include <rpc/rpcb_prot.h>
50 #include "un-namespace.h"
51
52 bool_t
53 xdr_rpcb(XDR *xdrs, RPCB *objp)
54 {
55         if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
56                 return (FALSE);
57         }
58         if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
59                 return (FALSE);
60         }
61         if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
62                 return (FALSE);
63         }
64         if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
65                 return (FALSE);
66         }
67         if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
68                 return (FALSE);
69         }
70         return (TRUE);
71 }
72
73 /*
74  * rpcblist_ptr implements a linked list.  The RPCL definition from
75  * rpcb_prot.x is:
76  *
77  * struct rpcblist {
78  *      rpcb            rpcb_map;
79  *      struct rpcblist *rpcb_next;
80  * };
81  * typedef rpcblist *rpcblist_ptr;
82  *
83  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
84  * there's any data behind the pointer, followed by the data (if any exists).
85  * The boolean can be interpreted as ``more data follows me''; if FALSE then
86  * nothing follows the boolean; if TRUE then the boolean is followed by an
87  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
88  * rpcblist *").
89  *
90  * This could be implemented via the xdr_pointer type, though this would
91  * result in one recursive call per element in the list.  Rather than do that
92  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
93  * serialize the rpcb elements.
94  */
95
96 bool_t
97 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
98 {
99         /*
100          * more_elements is pre-computed in case the direction is
101          * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
102          * xdr_bool when the direction is XDR_DECODE.
103          */
104         bool_t more_elements;
105         int freeing = (xdrs->x_op == XDR_FREE);
106         rpcblist_ptr next;
107         rpcblist_ptr next_copy;
108
109         next = NULL;
110         for (;;) {
111                 more_elements = (bool_t)(*rp != NULL);
112                 if (! xdr_bool(xdrs, &more_elements)) {
113                         return (FALSE);
114                 }
115                 if (! more_elements) {
116                         return (TRUE);  /* we are done */
117                 }
118                 /*
119                  * the unfortunate side effect of non-recursion is that in
120                  * the case of freeing we must remember the next object
121                  * before we free the current object ...
122                  */
123                 if (freeing && *rp)
124                         next = (*rp)->rpcb_next;
125                 if (! xdr_reference(xdrs, (caddr_t *)rp,
126                     (u_int)sizeof (rpcblist), (xdrproc_t)xdr_rpcb)) {
127                         return (FALSE);
128                 }
129                 if (freeing) {
130                         next_copy = next;
131                         rp = &next_copy;
132                         /*
133                          * Note that in the subsequent iteration, next_copy
134                          * gets nulled out by the xdr_reference
135                          * but next itself survives.
136                          */
137                 } else if (*rp) {
138                         rp = &((*rp)->rpcb_next);
139                 }
140         }
141         /*NOTREACHED*/
142 }
143
144 /*
145  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
146  * functionality to xdr_rpcblist_ptr().
147  */
148 bool_t
149 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
150 {
151         bool_t  dummy;
152
153         dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
154         return (dummy);
155 }
156
157
158 bool_t
159 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
160 {
161         if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
162                 return (FALSE);
163         }
164         if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
165                 return (FALSE);
166         }
167         if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
168                 return (FALSE);
169         }
170         if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
171                 return (FALSE);
172         }
173         if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
174                 return (FALSE);
175         }
176         return (TRUE);
177 }
178
179 bool_t
180 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
181 {
182         /*
183          * more_elements is pre-computed in case the direction is
184          * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
185          * xdr_bool when the direction is XDR_DECODE.
186          */
187         bool_t more_elements;
188         int freeing = (xdrs->x_op == XDR_FREE);
189         rpcb_entry_list_ptr next;
190         rpcb_entry_list_ptr next_copy;
191
192         next = NULL;
193         for (;;) {
194                 more_elements = (bool_t)(*rp != NULL);
195                 if (! xdr_bool(xdrs, &more_elements)) {
196                         return (FALSE);
197                 }
198                 if (! more_elements) {
199                         return (TRUE);  /* we are done */
200                 }
201                 /*
202                  * the unfortunate side effect of non-recursion is that in
203                  * the case of freeing we must remember the next object
204                  * before we free the current object ...
205                  */
206                 if (freeing)
207                         next = (*rp)->rpcb_entry_next;
208                 if (! xdr_reference(xdrs, (caddr_t *)rp,
209                     (u_int)sizeof (rpcb_entry_list),
210                                     (xdrproc_t)xdr_rpcb_entry)) {
211                         return (FALSE);
212                 }
213                 if (freeing && *rp) {
214                         next_copy = next;
215                         rp = &next_copy;
216                         /*
217                          * Note that in the subsequent iteration, next_copy
218                          * gets nulled out by the xdr_reference
219                          * but next itself survives.
220                          */
221                 } else if (*rp) {
222                         rp = &((*rp)->rpcb_entry_next);
223                 }
224         }
225         /*NOTREACHED*/
226 }
227
228 /*
229  * XDR remote call arguments
230  * written for XDR_ENCODE direction only
231  */
232 bool_t
233 xdr_rpcb_rmtcallargs(XDR *xdrs, struct rpcb_rmtcallargs *p)
234 {
235         struct r_rpcb_rmtcallargs *objp =
236             (struct r_rpcb_rmtcallargs *)(void *)p;
237         u_int lenposition, argposition, position;
238         int32_t *buf;
239
240         buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
241         if (buf == NULL) {
242                 if (!xdr_u_int32_t(xdrs, &objp->prog)) {
243                         return (FALSE);
244                 }
245                 if (!xdr_u_int32_t(xdrs, &objp->vers)) {
246                         return (FALSE);
247                 }
248                 if (!xdr_u_int32_t(xdrs, &objp->proc)) {
249                         return (FALSE);
250                 }
251         } else {
252                 IXDR_PUT_U_INT32(buf, objp->prog);
253                 IXDR_PUT_U_INT32(buf, objp->vers);
254                 IXDR_PUT_U_INT32(buf, objp->proc);
255         }
256
257         /*
258          * All the jugglery for just getting the size of the arguments
259          */
260         lenposition = XDR_GETPOS(xdrs);
261         if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
262                 return (FALSE);
263         }
264         argposition = XDR_GETPOS(xdrs);
265         if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
266                 return (FALSE);
267         }
268         position = XDR_GETPOS(xdrs);
269         objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
270         XDR_SETPOS(xdrs, lenposition);
271         if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
272                 return (FALSE);
273         }
274         XDR_SETPOS(xdrs, position);
275         return (TRUE);
276 }
277
278 /*
279  * XDR remote call results
280  * written for XDR_DECODE direction only
281  */
282 bool_t
283 xdr_rpcb_rmtcallres(XDR *xdrs, struct rpcb_rmtcallres *p)
284 {
285         bool_t dummy;
286         struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
287
288         if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
289                 return (FALSE);
290         }
291         if (!xdr_u_int(xdrs, &objp->results.results_len)) {
292                 return (FALSE);
293         }
294         dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
295         return (dummy);
296 }
297
298 bool_t
299 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
300 {
301         bool_t dummy;
302         void **pp;
303
304         if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
305                 return (FALSE);
306         }
307         pp = &objp->buf;
308         dummy = xdr_bytes(xdrs, (char **) pp,
309                         (u_int *)&(objp->len), objp->maxlen);
310         return (dummy);
311 }