xref: /titanic_44/usr/src/lib/libnsl/rpc/rpcb_prot.c (revision fe0e7ec4d916b05b52d8c7cc8a3e6a1b28e77b6f)
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 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
29 /*
30  * Portions of this source code were derived from Berkeley
31  * 4.3 BSD under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 /*
38  * rpcb_prot.c
39  * XDR routines for the rpcbinder version 3.
40  */
41 
42 #include <rpc/rpc.h>
43 #include <rpc/types.h>
44 #include <rpc/xdr.h>
45 #include <rpc/rpcb_prot.h>
46 
47 
48 bool_t
49 xdr_rpcb(XDR *xdrs, RPCB *objp)
50 {
51 	if (!xdr_u_int(xdrs, (uint_t *)&objp->r_prog))
52 		return (FALSE);
53 	if (!xdr_u_int(xdrs, (uint_t *)&objp->r_vers))
54 		return (FALSE);
55 	if (!xdr_string(xdrs, &objp->r_netid, ~0))
56 		return (FALSE);
57 	if (!xdr_string(xdrs, &objp->r_addr, ~0))
58 		return (FALSE);
59 	return (xdr_string(xdrs, &objp->r_owner, ~0));
60 }
61 
62 /*
63  * rpcblist_ptr implements a linked list.  The RPCL definition from
64  * rpcb_prot.x is:
65  *
66  * struct rpcblist {
67  * 	rpcb		rpcb_map;
68  *	struct rpcblist *rpcb_next;
69  * };
70  * typedef rpcblist *rpcblist_ptr;
71  *
72  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
73  * there's any data behind the pointer, followed by the data (if any exists).
74  * The boolean can be interpreted as ``more data follows me''; if FALSE then
75  * nothing follows the boolean; if TRUE then the boolean is followed by an
76  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
77  * rpcblist *").
78  *
79  * This could be implemented via the xdr_pointer type, though this would
80  * result in one recursive call per element in the list.  Rather than do that
81  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
82  * serialize the rpcb elements.
83  */
84 
85 bool_t
86 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
87 {
88 	/*
89 	 * more_elements is pre-computed in case the direction is
90 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
91 	 * xdr_bool when the direction is XDR_DECODE.
92 	 */
93 	bool_t more_elements;
94 	int freeing = (xdrs->x_op == XDR_FREE);
95 	rpcblist_ptr next;
96 	rpcblist_ptr next_copy;
97 
98 	for (;;) {
99 		more_elements = (bool_t)(*rp != NULL);
100 		if (!xdr_bool(xdrs, &more_elements))
101 			return (FALSE);
102 		if (!more_elements)
103 			return (TRUE);  /* we are done */
104 		/*
105 		 * the unfortunate side effect of non-recursion is that in
106 		 * the case of freeing we must remember the next object
107 		 * before we free the current object ...
108 		 */
109 		if (freeing)
110 			next = (*rp)->rpcb_next;
111 		if (!xdr_reference(xdrs, (caddr_t *)rp,
112 		    (uint_t)sizeof (rpcblist), (xdrproc_t)xdr_rpcb))
113 			return (FALSE);
114 		if (freeing) {
115 			next_copy = next;
116 			rp = &next_copy;
117 			/*
118 			 * Note that in the subsequent iteration, next_copy
119 			 * gets nulled out by the xdr_reference
120 			 * but next itself survives.
121 			 */
122 		} else {
123 			rp = &((*rp)->rpcb_next);
124 		}
125 	}
126 	/*NOTREACHED*/
127 }
128 
129 /*
130  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
131  * functionality to xdr_rpcblist_ptr().
132  */
133 bool_t
134 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
135 {
136 	return (xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp));
137 }
138 
139 
140 bool_t
141 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
142 {
143 	if (!xdr_string(xdrs, &objp->r_maddr, ~0))
144 		return (FALSE);
145 	if (!xdr_string(xdrs, &objp->r_nc_netid, ~0))
146 		return (FALSE);
147 	if (!xdr_u_int(xdrs, &objp->r_nc_semantics))
148 		return (FALSE);
149 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, ~0))
150 		return (FALSE);
151 	return (xdr_string(xdrs, &objp->r_nc_proto, ~0));
152 }
153 
154 bool_t
155 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
156 {
157 	/*
158 	 * more_elements is pre-computed in case the direction is
159 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
160 	 * xdr_bool when the direction is XDR_DECODE.
161 	 */
162 	bool_t more_elements;
163 	int freeing = (xdrs->x_op == XDR_FREE);
164 	rpcb_entry_list_ptr next;
165 	rpcb_entry_list_ptr next_copy;
166 
167 	for (;;) {
168 		more_elements = (bool_t)(*rp != NULL);
169 		if (!xdr_bool(xdrs, &more_elements))
170 			return (FALSE);
171 		if (!more_elements)
172 			return (TRUE);  /* we are done */
173 		/*
174 		 * the unfortunate side effect of non-recursion is that in
175 		 * the case of freeing we must remember the next object
176 		 * before we free the current object ...
177 		 */
178 		if (freeing)
179 			next = (*rp)->rpcb_entry_next;
180 		if (!xdr_reference(xdrs, (caddr_t *)rp,
181 		    (uint_t)sizeof (rpcb_entry_list),
182 				    (xdrproc_t)xdr_rpcb_entry))
183 			return (FALSE);
184 		if (freeing) {
185 			next_copy = next;
186 			rp = &next_copy;
187 			/*
188 			 * Note that in the subsequent iteration, next_copy
189 			 * gets nulled out by the xdr_reference
190 			 * but next itself survives.
191 			 */
192 		} else {
193 			rp = &((*rp)->rpcb_entry_next);
194 		}
195 	}
196 	/*NOTREACHED*/
197 }
198 
199 /*
200  * XDR remote call arguments
201  * written for XDR_ENCODE direction only
202  */
203 bool_t
204 xdr_rpcb_rmtcallargs(XDR *xdrs, struct r_rpcb_rmtcallargs *objp)
205 {
206 	uint_t lenposition, argposition, position;
207 	rpc_inline_t *buf;
208 
209 	buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT);
210 	if (buf == NULL) {
211 		if (!xdr_u_int(xdrs, (uint_t *)&objp->prog))
212 			return (FALSE);
213 		if (!xdr_u_int(xdrs, (uint_t *)&objp->vers))
214 			return (FALSE);
215 		if (!xdr_u_int(xdrs, (uint_t *)&objp->proc))
216 			return (FALSE);
217 	} else {
218 		IXDR_PUT_U_INT32(buf, objp->prog);
219 		IXDR_PUT_U_INT32(buf, objp->vers);
220 		IXDR_PUT_U_INT32(buf, objp->proc);
221 	}
222 
223 	/*
224 	 * All the jugglery for just getting the size of the arguments
225 	 */
226 	lenposition = XDR_GETPOS(xdrs);
227 	if (!xdr_u_int(xdrs, &(objp->args.args_len)))
228 		return (FALSE);
229 	argposition = XDR_GETPOS(xdrs);
230 	if (!(*objp->xdr_args)(xdrs, objp->args.args_val))
231 		return (FALSE);
232 	position = XDR_GETPOS(xdrs);
233 	objp->args.args_len = (uint_t)position - (uint_t)argposition;
234 	XDR_SETPOS(xdrs, lenposition);
235 	if (!xdr_u_int(xdrs, &(objp->args.args_len)))
236 		return (FALSE);
237 	XDR_SETPOS(xdrs, position);
238 	return (TRUE);
239 }
240 
241 /*
242  * XDR remote call results
243  * written for XDR_DECODE direction only
244  */
245 bool_t
246 xdr_rpcb_rmtcallres(XDR *xdrs, struct r_rpcb_rmtcallres *objp)
247 {
248 	if (!xdr_string(xdrs, &objp->addr, ~0))
249 		return (FALSE);
250 	if (!xdr_u_int(xdrs, &objp->results.results_len))
251 		return (FALSE);
252 	return ((*(objp->xdr_res))(xdrs, objp->results.results_val));
253 }
254 
255 bool_t
256 xdr_netbuf(XDR *xdrs, struct netbuf *objp)
257 {
258 	if (!xdr_u_int(xdrs, (uint_t *)&objp->maxlen))
259 		return (FALSE);
260 	return (xdr_bytes(xdrs, (char **)&(objp->buf),
261 			(uint_t *)&(objp->len), objp->maxlen));
262 }
263