[NETAPI32]
[reactos.git] / reactos / dll / 3rdparty / libtirpc / 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 <wintirpc.h>
41 #include <rpc/rpc.h>
42 #include <rpc/types.h>
43 #include <rpc/xdr.h>
44 #include <rpc/rpcb_prot.h>
45
46 bool_t
47 xdr_rpcb(xdrs, objp)
48 XDR *xdrs;
49 RPCB *objp;
50 {
51 if (!xdr_u_int32_t(xdrs, &objp->r_prog)) {
52 return (FALSE);
53 }
54 if (!xdr_u_int32_t(xdrs, &objp->r_vers)) {
55 return (FALSE);
56 }
57 if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
58 return (FALSE);
59 }
60 if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
61 return (FALSE);
62 }
63 if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
64 return (FALSE);
65 }
66 return (TRUE);
67 }
68
69 /*
70 * rpcblist_ptr implements a linked list. The RPCL definition from
71 * rpcb_prot.x is:
72 *
73 * struct rpcblist {
74 * rpcb rpcb_map;
75 * struct rpcblist *rpcb_next;
76 * };
77 * typedef rpcblist *rpcblist_ptr;
78 *
79 * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
80 * there's any data behind the pointer, followed by the data (if any exists).
81 * The boolean can be interpreted as ``more data follows me''; if FALSE then
82 * nothing follows the boolean; if TRUE then the boolean is followed by an
83 * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
84 * rpcblist *").
85 *
86 * This could be implemented via the xdr_pointer type, though this would
87 * result in one recursive call per element in the list. Rather than do that
88 * we can ``unwind'' the recursion into a while loop and use xdr_reference to
89 * serialize the rpcb elements.
90 */
91
92 bool_t
93 xdr_rpcblist_ptr(xdrs, rp)
94 XDR *xdrs;
95 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)
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 {
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(xdrs, rp)
148 XDR *xdrs;
149 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(xdrs, objp)
160 XDR *xdrs;
161 rpcb_entry *objp;
162 {
163 if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
164 return (FALSE);
165 }
166 if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
167 return (FALSE);
168 }
169 if (!xdr_u_int32_t(xdrs, &objp->r_nc_semantics)) {
170 return (FALSE);
171 }
172 if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
173 return (FALSE);
174 }
175 if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
176 return (FALSE);
177 }
178 return (TRUE);
179 }
180
181 bool_t
182 xdr_rpcb_entry_list_ptr(xdrs, rp)
183 XDR *xdrs;
184 rpcb_entry_list_ptr *rp;
185 {
186 /*
187 * more_elements is pre-computed in case the direction is
188 * XDR_ENCODE or XDR_FREE. more_elements is overwritten by
189 * xdr_bool when the direction is XDR_DECODE.
190 */
191 bool_t more_elements;
192 int freeing = (xdrs->x_op == XDR_FREE);
193 rpcb_entry_list_ptr next;
194 rpcb_entry_list_ptr next_copy;
195
196 next = NULL;
197 for (;;) {
198 more_elements = (bool_t)(*rp != NULL);
199 if (! xdr_bool(xdrs, &more_elements)) {
200 return (FALSE);
201 }
202 if (! more_elements) {
203 return (TRUE); /* we are done */
204 }
205 /*
206 * the unfortunate side effect of non-recursion is that in
207 * the case of freeing we must remember the next object
208 * before we free the current object ...
209 */
210 if (freeing)
211 next = (*rp)->rpcb_entry_next;
212 if (! xdr_reference(xdrs, (caddr_t *)rp,
213 (u_int)sizeof (rpcb_entry_list),
214 (xdrproc_t)xdr_rpcb_entry)) {
215 return (FALSE);
216 }
217 if (freeing) {
218 next_copy = next;
219 rp = &next_copy;
220 /*
221 * Note that in the subsequent iteration, next_copy
222 * gets nulled out by the xdr_reference
223 * but next itself survives.
224 */
225 } else {
226 rp = &((*rp)->rpcb_entry_next);
227 }
228 }
229 /*NOTREACHED*/
230 }
231
232 /*
233 * XDR remote call arguments
234 * written for XDR_ENCODE direction only
235 */
236 bool_t
237 xdr_rpcb_rmtcallargs(xdrs, p)
238 XDR *xdrs;
239 struct rpcb_rmtcallargs *p;
240 {
241 struct r_rpcb_rmtcallargs *objp =
242 (struct r_rpcb_rmtcallargs *)(void *)p;
243 u_int lenposition, argposition, position;
244 int32_t *buf;
245
246 buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
247 if (buf == NULL) {
248 if (!xdr_u_int32_t(xdrs, &objp->prog)) {
249 return (FALSE);
250 }
251 if (!xdr_u_int32_t(xdrs, &objp->vers)) {
252 return (FALSE);
253 }
254 if (!xdr_u_int32_t(xdrs, &objp->proc)) {
255 return (FALSE);
256 }
257 } else {
258 IXDR_PUT_U_INT32(buf, objp->prog);
259 IXDR_PUT_U_INT32(buf, objp->vers);
260 IXDR_PUT_U_INT32(buf, objp->proc);
261 }
262
263 /*
264 * All the jugglery for just getting the size of the arguments
265 */
266 lenposition = XDR_GETPOS(xdrs);
267 if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
268 return (FALSE);
269 }
270 argposition = XDR_GETPOS(xdrs);
271 if (! (*objp->xdr_args)(xdrs, objp->args.args_val)) {
272 return (FALSE);
273 }
274 position = XDR_GETPOS(xdrs);
275 objp->args.args_len = (u_int)((u_long)position - (u_long)argposition);
276 XDR_SETPOS(xdrs, lenposition);
277 if (! xdr_u_int(xdrs, &(objp->args.args_len))) {
278 return (FALSE);
279 }
280 XDR_SETPOS(xdrs, position);
281 return (TRUE);
282 }
283
284 /*
285 * XDR remote call results
286 * written for XDR_DECODE direction only
287 */
288 bool_t
289 xdr_rpcb_rmtcallres(xdrs, p)
290 XDR *xdrs;
291 struct rpcb_rmtcallres *p;
292 {
293 bool_t dummy;
294 struct r_rpcb_rmtcallres *objp = (struct r_rpcb_rmtcallres *)(void *)p;
295
296 if (!xdr_string(xdrs, &objp->addr, (u_int)~0)) {
297 return (FALSE);
298 }
299 if (!xdr_u_int(xdrs, &objp->results.results_len)) {
300 return (FALSE);
301 }
302 dummy = (*(objp->xdr_res))(xdrs, objp->results.results_val);
303 return (dummy);
304 }
305
306 bool_t
307 xdr_netbuf(xdrs, objp)
308 XDR *xdrs;
309 struct netbuf *objp;
310 {
311 bool_t dummy;
312
313 if (!xdr_u_int32_t(xdrs, (u_int32_t *) &objp->maxlen)) {
314 return (FALSE);
315 }
316 dummy = xdr_bytes(xdrs, (char **)&(objp->buf),
317 (u_int *)&(objp->len), objp->maxlen);
318 return (dummy);
319 }