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