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