xref: /freebsd/sys/rpc/rpcb_prot.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*	$NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Copyright (c) 2009, Sun Microsystems, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  * - Redistributions of source code must retain the above copyright notice,
12  *   this list of conditions and the following disclaimer.
13  * - Redistributions in binary form must reproduce the above copyright notice,
14  *   this list of conditions and the following disclaimer in the documentation
15  *   and/or other materials provided with the distribution.
16  * - Neither the name of Sun Microsystems, Inc. nor the names of its
17  *   contributors may be used to endorse or promote products derived
18  *   from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*
33  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
34  */
35 
36 /* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
37 
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
40 #endif
41 #include <sys/cdefs.h>
42 /*
43  * rpcb_prot.c
44  * XDR routines for the rpcbinder version 3.
45  *
46  * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
47  */
48 
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/kernel.h>
52 #include <sys/malloc.h>
53 
54 #include <rpc/rpc.h>
55 #include <rpc/rpc_com.h>
56 #include <rpc/rpcb_prot.h>
57 
58 bool_t
59 xdr_portmap(XDR *xdrs, struct portmap *regs)
60 {
61 
62 	if (xdr_u_long(xdrs, &regs->pm_prog) &&
63 		xdr_u_long(xdrs, &regs->pm_vers) &&
64 		xdr_u_long(xdrs, &regs->pm_prot))
65 		return (xdr_u_long(xdrs, &regs->pm_port));
66 	return (FALSE);
67 }
68 
69 bool_t
70 xdr_rpcb(XDR *xdrs, RPCB *objp)
71 {
72 	if (!xdr_uint32_t(xdrs, &objp->r_prog)) {
73 		return (FALSE);
74 	}
75 	if (!xdr_uint32_t(xdrs, &objp->r_vers)) {
76 		return (FALSE);
77 	}
78 	if (!xdr_string(xdrs, &objp->r_netid, RPC_MAXDATASIZE)) {
79 		return (FALSE);
80 	}
81 	if (!xdr_string(xdrs, &objp->r_addr, RPC_MAXDATASIZE)) {
82 		return (FALSE);
83 	}
84 	if (!xdr_string(xdrs, &objp->r_owner, RPC_MAXDATASIZE)) {
85 		return (FALSE);
86 	}
87 	return (TRUE);
88 }
89 
90 /*
91  * rpcblist_ptr implements a linked list.  The RPCL definition from
92  * rpcb_prot.x is:
93  *
94  * struct rpcblist {
95  * 	rpcb		rpcb_map;
96  *	struct rpcblist *rpcb_next;
97  * };
98  * typedef rpcblist *rpcblist_ptr;
99  *
100  * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
101  * there's any data behind the pointer, followed by the data (if any exists).
102  * The boolean can be interpreted as ``more data follows me''; if FALSE then
103  * nothing follows the boolean; if TRUE then the boolean is followed by an
104  * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
105  * rpcblist *").
106  *
107  * This could be implemented via the xdr_pointer type, though this would
108  * result in one recursive call per element in the list.  Rather than do that
109  * we can ``unwind'' the recursion into a while loop and use xdr_reference to
110  * serialize the rpcb elements.
111  */
112 
113 bool_t
114 xdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
115 {
116 	/*
117 	 * more_elements is pre-computed in case the direction is
118 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
119 	 * xdr_bool when the direction is XDR_DECODE.
120 	 */
121 	bool_t more_elements;
122 	int freeing = (xdrs->x_op == XDR_FREE);
123 	rpcblist_ptr next;
124 	rpcblist_ptr next_copy;
125 
126 	next = NULL;
127 	for (;;) {
128 		more_elements = (bool_t)(*rp != NULL);
129 		if (! xdr_bool(xdrs, &more_elements)) {
130 			return (FALSE);
131 		}
132 		if (! more_elements) {
133 			return (TRUE);  /* we are done */
134 		}
135 		/*
136 		 * the unfortunate side effect of non-recursion is that in
137 		 * the case of freeing we must remember the next object
138 		 * before we free the current object ...
139 		 */
140 		if (freeing && *rp)
141 			next = (*rp)->rpcb_next;
142 		if (! xdr_reference(xdrs, (caddr_t *)rp,
143 		    (u_int)sizeof (RPCBLIST), (xdrproc_t)xdr_rpcb)) {
144 			return (FALSE);
145 		}
146 		if (freeing) {
147 			next_copy = next;
148 			rp = &next_copy;
149 			/*
150 			 * Note that in the subsequent iteration, next_copy
151 			 * gets nulled out by the xdr_reference
152 			 * but next itself survives.
153 			 */
154 		} else if (*rp) {
155 			rp = &((*rp)->rpcb_next);
156 		}
157 	}
158 	/*NOTREACHED*/
159 }
160 
161 #if 0
162 /*
163  * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
164  * functionality to xdr_rpcblist_ptr().
165  */
166 bool_t
167 xdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
168 {
169 	bool_t	dummy;
170 
171 	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
172 	return (dummy);
173 }
174 #endif
175 
176 bool_t
177 xdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
178 {
179 	if (!xdr_string(xdrs, &objp->r_maddr, RPC_MAXDATASIZE)) {
180 		return (FALSE);
181 	}
182 	if (!xdr_string(xdrs, &objp->r_nc_netid, RPC_MAXDATASIZE)) {
183 		return (FALSE);
184 	}
185 	if (!xdr_uint32_t(xdrs, &objp->r_nc_semantics)) {
186 		return (FALSE);
187 	}
188 	if (!xdr_string(xdrs, &objp->r_nc_protofmly, RPC_MAXDATASIZE)) {
189 		return (FALSE);
190 	}
191 	if (!xdr_string(xdrs, &objp->r_nc_proto, RPC_MAXDATASIZE)) {
192 		return (FALSE);
193 	}
194 	return (TRUE);
195 }
196 
197 bool_t
198 xdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
199 {
200 	/*
201 	 * more_elements is pre-computed in case the direction is
202 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
203 	 * xdr_bool when the direction is XDR_DECODE.
204 	 */
205 	bool_t more_elements;
206 	int freeing = (xdrs->x_op == XDR_FREE);
207 	rpcb_entry_list_ptr next;
208 	rpcb_entry_list_ptr next_copy;
209 
210 	next = NULL;
211 	for (;;) {
212 		more_elements = (bool_t)(*rp != NULL);
213 		if (! xdr_bool(xdrs, &more_elements)) {
214 			return (FALSE);
215 		}
216 		if (! more_elements) {
217 			return (TRUE);  /* we are done */
218 		}
219 		/*
220 		 * the unfortunate side effect of non-recursion is that in
221 		 * the case of freeing we must remember the next object
222 		 * before we free the current object ...
223 		 */
224 		if (freeing)
225 			next = (*rp)->rpcb_entry_next;
226 		if (! xdr_reference(xdrs, (caddr_t *)rp,
227 		    (u_int)sizeof (rpcb_entry_list),
228 				    (xdrproc_t)xdr_rpcb_entry)) {
229 			return (FALSE);
230 		}
231 		if (freeing && *rp) {
232 			next_copy = next;
233 			rp = &next_copy;
234 			/*
235 			 * Note that in the subsequent iteration, next_copy
236 			 * gets nulled out by the xdr_reference
237 			 * but next itself survives.
238 			 */
239 		} else if (*rp) {
240 			rp = &((*rp)->rpcb_entry_next);
241 		}
242 	}
243 	/*NOTREACHED*/
244 }
245