xref: /illumos-gate/usr/src/lib/libnsl/rpc/rpcb_prot.c (revision 03270635d68df6a0392fb8f4b7c04acad764648b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30 /*
31  * Portions of this source code were derived from Berkeley
32  * 4.3 BSD under license from the Regents of the University of
33  * California.
34  */
35 
36 /*
37  * XDR routines for the rpcbinder version 3.
38  */
39 
40 #include "mt.h"
41 #include <rpc/rpc.h>
42 #include <rpc/types.h>
43 #include <rpc/xdr.h>
44 #include <rpc/rpcb_prot.h>
45 
46 
47 bool_t
48 xdr_rpcb(XDR *xdrs, RPCB *objp)
49 {
50 	if (!xdr_u_int(xdrs, (uint_t *)&objp->r_prog))
51 		return (FALSE);
52 	if (!xdr_u_int(xdrs, (uint_t *)&objp->r_vers))
53 		return (FALSE);
54 	if (!xdr_string(xdrs, &objp->r_netid, ~0))
55 		return (FALSE);
56 	if (!xdr_string(xdrs, &objp->r_addr, ~0))
57 		return (FALSE);
58 	return (xdr_string(xdrs, &objp->r_owner, ~0));
59 }
60 
61 /*
62  * rpcblist_ptr implements a linked list.  The RPCL definition from
63  * rpcb_prot.x is:
64  *
65  * struct rpcblist {
66  * 	rpcb		rpcb_map;
67  *	struct rpcblist *rpcb_next;
68  * };
69  * typedef rpcblist *rpcblist_ptr;
70  *
71  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
72  * there's any data behind the pointer, followed by the data (if any exists).
73  * The boolean can be interpreted as ``more data follows me''; if FALSE then
74  * nothing follows the boolean; if TRUE then the boolean is followed by an
75  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
76  * rpcblist *").
77  *
78  * This could be implemented via the xdr_pointer type, though this would
79  * result in one recursive call per element in the list.  Rather than do that
80  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
81  * serialize the rpcb elements.
82  */
83 
84 bool_t
85 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
86 {
87 	/*
88 	 * more_elements is pre-computed in case the direction is
89 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
90 	 * xdr_bool when the direction is XDR_DECODE.
91 	 */
92 	bool_t more_elements;
93 	int freeing = (xdrs->x_op == XDR_FREE);
94 	rpcblist_ptr next;
95 	rpcblist_ptr next_copy;
96 
97 	for (;;) {
98 		more_elements = (bool_t)(*rp != NULL);
99 		if (!xdr_bool(xdrs, &more_elements))
100 			return (FALSE);
101 		if (!more_elements)
102 			return (TRUE);  /* we are done */
103 		/*
104 		 * the unfortunate side effect of non-recursion is that in
105 		 * the case of freeing we must remember the next object
106 		 * before we free the current object ...
107 		 */
108 		if (freeing)
109 			next = (*rp)->rpcb_next;
110 		if (!xdr_reference(xdrs, (caddr_t *)rp,
111 		    (uint_t)sizeof (rpcblist), (xdrproc_t)xdr_rpcb))
112 			return (FALSE);
113 		if (freeing) {
114 			next_copy = next;
115 			rp = &next_copy;
116 			/*
117 			 * Note that in the subsequent iteration, next_copy
118 			 * gets nulled out by the xdr_reference
119 			 * but next itself survives.
120 			 */
121 		} else {
122 			rp = &((*rp)->rpcb_next);
123 		}
124 	}
125 	/*NOTREACHED*/
126 }
127 
128 /*
129  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
130  * functionality to xdr_rpcblist_ptr().
131  */
132 bool_t
133 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
134 {
135 	return (xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp));
136 }
137 
138 
139 bool_t
140 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
141 {
142 	if (!xdr_string(xdrs, &objp->r_maddr, ~0))
143 		return (FALSE);
144 	if (!xdr_string(xdrs, &objp->r_nc_netid, ~0))
145 		return (FALSE);
146 	if (!xdr_u_int(xdrs, &objp->r_nc_semantics))
147 		return (FALSE);
148 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, ~0))
149 		return (FALSE);
150 	return (xdr_string(xdrs, &objp->r_nc_proto, ~0));
151 }
152 
153 bool_t
154 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
155 {
156 	/*
157 	 * more_elements is pre-computed in case the direction is
158 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
159 	 * xdr_bool when the direction is XDR_DECODE.
160 	 */
161 	bool_t more_elements;
162 	int freeing = (xdrs->x_op == XDR_FREE);
163 	rpcb_entry_list_ptr next;
164 	rpcb_entry_list_ptr next_copy;
165 
166 	for (;;) {
167 		more_elements = (bool_t)(*rp != NULL);
168 		if (!xdr_bool(xdrs, &more_elements))
169 			return (FALSE);
170 		if (!more_elements)
171 			return (TRUE);  /* we are done */
172 		/*
173 		 * the unfortunate side effect of non-recursion is that in
174 		 * the case of freeing we must remember the next object
175 		 * before we free the current object ...
176 		 */
177 		if (freeing)
178 			next = (*rp)->rpcb_entry_next;
179 		if (!xdr_reference(xdrs, (caddr_t *)rp,
180 		    (uint_t)sizeof (rpcb_entry_list),
181 		    (xdrproc_t)xdr_rpcb_entry))
182 			return (FALSE);
183 		if (freeing) {
184 			next_copy = next;
185 			rp = &next_copy;
186 			/*
187 			 * Note that in the subsequent iteration, next_copy
188 			 * gets nulled out by the xdr_reference
189 			 * but next itself survives.
190 			 */
191 		} else {
192 			rp = &((*rp)->rpcb_entry_next);
193 		}
194 	}
195 	/*NOTREACHED*/
196 }
197 
198 /*
199  * XDR remote call arguments
200  * written for XDR_ENCODE direction only
201  */
202 bool_t
203 xdr_rpcb_rmtcallargs(XDR *xdrs, struct r_rpcb_rmtcallargs *objp)
204 {
205 	uint_t lenposition, argposition, position;
206 	rpc_inline_t *buf;
207 
208 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
209 	if (buf == NULL) {
210 		if (!xdr_u_int(xdrs, (uint_t *)&objp->prog))
211 			return (FALSE);
212 		if (!xdr_u_int(xdrs, (uint_t *)&objp->vers))
213 			return (FALSE);
214 		if (!xdr_u_int(xdrs, (uint_t *)&objp->proc))
215 			return (FALSE);
216 	} else {
217 		IXDR_PUT_U_INT32(buf, objp->prog);
218 		IXDR_PUT_U_INT32(buf, objp->vers);
219 		IXDR_PUT_U_INT32(buf, objp->proc);
220 	}
221 
222 	/*
223 	 * All the jugglery for just getting the size of the arguments
224 	 */
225 	lenposition = XDR_GETPOS(xdrs);
226 	if (!xdr_u_int(xdrs, &(objp->args.args_len)))
227 		return (FALSE);
228 	argposition = XDR_GETPOS(xdrs);
229 	if (!(*objp->xdr_args)(xdrs, objp->args.args_val))
230 		return (FALSE);
231 	position = XDR_GETPOS(xdrs);
232 	objp->args.args_len = (uint_t)position - (uint_t)argposition;
233 	XDR_SETPOS(xdrs, lenposition);
234 	if (!xdr_u_int(xdrs, &(objp->args.args_len)))
235 		return (FALSE);
236 	XDR_SETPOS(xdrs, position);
237 	return (TRUE);
238 }
239 
240 /*
241  * XDR remote call results
242  * written for XDR_DECODE direction only
243  */
244 bool_t
245 xdr_rpcb_rmtcallres(XDR *xdrs, struct r_rpcb_rmtcallres *objp)
246 {
247 	if (!xdr_string(xdrs, &objp->addr, ~0))
248 		return (FALSE);
249 	if (!xdr_u_int(xdrs, &objp->results.results_len))
250 		return (FALSE);
251 	return ((*(objp->xdr_res))(xdrs, objp->results.results_val));
252 }
253 
254 bool_t
255 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
256 {
257 	bool_t res;
258 
259 	/*
260 	 * Save the passed in maxlen value and buf pointer.  We might
261 	 * need them later.
262 	 */
263 	uint_t maxlen_save = objp->maxlen;
264 	void *buf_save = objp->buf;
265 
266 	if (!xdr_u_int(xdrs, &objp->maxlen))
267 		return (FALSE);
268 
269 	/*
270 	 * We need to free maxlen, not len, so do it explicitly now.
271 	 */
272 	if (xdrs->x_op == XDR_FREE)
273 		return (xdr_bytes(xdrs, &objp->buf, &objp->maxlen,
274 		    objp->maxlen));
275 
276 	/*
277 	 * If we're decoding and the caller has already allocated a
278 	 * buffer restore the maxlen value since the decoded value
279 	 * doesn't apply to the caller's buffer.  xdr_bytes() will
280 	 * return an error if the buffer isn't big enough.
281 	 */
282 	if (xdrs->x_op == XDR_DECODE && objp->buf != NULL)
283 		objp->maxlen = maxlen_save;
284 
285 	res = xdr_bytes(xdrs, &objp->buf, &objp->len, objp->maxlen);
286 
287 	/*
288 	 * If we are decoding and the buffer was allocated in the
289 	 * xdr_bytes() function we need to set maxlen properly to
290 	 * follow the netbuf semantics.
291 	 */
292 	if (xdrs->x_op == XDR_DECODE && objp->buf != buf_save)
293 		objp->maxlen = objp->len;
294 
295 	return (res);
296 }
297