rpcb_prot.c revision 256281
155682Smarkm/*	$NetBSD: rpcb_prot.c,v 1.3 2000/07/14 08:40:42 fvdl Exp $	*/
2233294Sstas
3233294Sstas/*
4233294Sstas * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
555682Smarkm * unrestricted use provided that this legend is included on all tape
6233294Sstas * media and as a part of the software program in whole or part.  Users
7233294Sstas * may copy or modify Sun RPC without charge, but are not authorized
8233294Sstas * to license or distribute it to anyone else except as part of a product or
955682Smarkm * program developed by the user.
10233294Sstas *
11233294Sstas * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1255682Smarkm * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13233294Sstas * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14233294Sstas *
15233294Sstas * Sun RPC is provided with no support and without any obligation on the
1655682Smarkm * part of Sun Microsystems, Inc. to assist in its use, correction,
1755682Smarkm * modification or enhancement.
1855682Smarkm *
1955682Smarkm * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
2055682Smarkm * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
2155682Smarkm * OR ANY PART THEREOF.
2255682Smarkm *
2355682Smarkm * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2455682Smarkm * or profits or other special, indirect and consequential damages, even if
2555682Smarkm * Sun has been advised of the possibility of such damages.
2655682Smarkm *
2755682Smarkm * Sun Microsystems, Inc.
2855682Smarkm * 2550 Garcia Avenue
2955682Smarkm * Mountain View, California  94043
3055682Smarkm */
3155682Smarkm/*
3255682Smarkm * Copyright (c) 1986-1991 by Sun Microsystems Inc.
3355682Smarkm */
34233294Sstas
35178825Sdfr/* #ident	"@(#)rpcb_prot.c	1.13	94/04/24 SMI" */
36178825Sdfr
37178825Sdfr#if defined(LIBC_SCCS) && !defined(lint)
3855682Smarkmstatic char sccsid[] = "@(#)rpcb_prot.c 1.9 89/04/21 Copyr 1984 Sun Micro";
3955682Smarkm#endif
4055682Smarkm#include <sys/cdefs.h>
4155682Smarkm__FBSDID("$FreeBSD: stable/10/sys/rpc/rpcb_prot.c 182154 2008-08-25 09:36:17Z dfr $");
4255682Smarkm
4355682Smarkm/*
4455682Smarkm * rpcb_prot.c
4555682Smarkm * XDR routines for the rpcbinder version 3.
4655682Smarkm *
4755682Smarkm * Copyright (C) 1984, 1988, Sun Microsystems, Inc.
4855682Smarkm */
4955682Smarkm
5055682Smarkm#include <sys/param.h>
5155682Smarkm#include <sys/systm.h>
5255682Smarkm#include <sys/kernel.h>
5355682Smarkm#include <sys/malloc.h>
5455682Smarkm
5555682Smarkm#include <rpc/rpc.h>
5655682Smarkm#include <rpc/rpcb_prot.h>
5755682Smarkm
5855682Smarkmbool_t
5955682Smarkmxdr_portmap(XDR *xdrs, struct portmap *regs)
6055682Smarkm{
6155682Smarkm
6255682Smarkm	if (xdr_u_long(xdrs, &regs->pm_prog) &&
6355682Smarkm		xdr_u_long(xdrs, &regs->pm_vers) &&
6455682Smarkm		xdr_u_long(xdrs, &regs->pm_prot))
6555682Smarkm		return (xdr_u_long(xdrs, &regs->pm_port));
6655682Smarkm	return (FALSE);
6755682Smarkm}
6855682Smarkm
6955682Smarkmbool_t
7055682Smarkmxdr_rpcb(XDR *xdrs, RPCB *objp)
7155682Smarkm{
7255682Smarkm	if (!xdr_uint32_t(xdrs, &objp->r_prog)) {
7355682Smarkm		return (FALSE);
7455682Smarkm	}
7555682Smarkm	if (!xdr_uint32_t(xdrs, &objp->r_vers)) {
7655682Smarkm		return (FALSE);
7755682Smarkm	}
7855682Smarkm	if (!xdr_string(xdrs, &objp->r_netid, (u_int)~0)) {
7955682Smarkm		return (FALSE);
8055682Smarkm	}
8155682Smarkm	if (!xdr_string(xdrs, &objp->r_addr, (u_int)~0)) {
8255682Smarkm		return (FALSE);
8355682Smarkm	}
8455682Smarkm	if (!xdr_string(xdrs, &objp->r_owner, (u_int)~0)) {
8555682Smarkm		return (FALSE);
8655682Smarkm	}
8755682Smarkm	return (TRUE);
8855682Smarkm}
8955682Smarkm
9055682Smarkm/*
9155682Smarkm * rpcblist_ptr implements a linked list.  The RPCL definition from
9255682Smarkm * rpcb_prot.x is:
9355682Smarkm *
9455682Smarkm * struct rpcblist {
9555682Smarkm * 	rpcb		rpcb_map;
9655682Smarkm *	struct rpcblist *rpcb_next;
9755682Smarkm * };
9855682Smarkm * typedef rpcblist *rpcblist_ptr;
9955682Smarkm *
10055682Smarkm * Recall that "pointers" in XDR are encoded as a boolean, indicating whether
10155682Smarkm * there's any data behind the pointer, followed by the data (if any exists).
10255682Smarkm * The boolean can be interpreted as ``more data follows me''; if FALSE then
10355682Smarkm * nothing follows the boolean; if TRUE then the boolean is followed by an
10455682Smarkm * actual struct rpcb, and another rpcblist_ptr (declared in RPCL as "struct
10555682Smarkm * rpcblist *").
10655682Smarkm *
10755682Smarkm * This could be implemented via the xdr_pointer type, though this would
10855682Smarkm * result in one recursive call per element in the list.  Rather than do that
10955682Smarkm * we can ``unwind'' the recursion into a while loop and use xdr_reference to
11055682Smarkm * serialize the rpcb elements.
11155682Smarkm */
11255682Smarkm
11355682Smarkmbool_t
11455682Smarkmxdr_rpcblist_ptr(XDR *xdrs, rpcblist_ptr *rp)
11555682Smarkm{
11655682Smarkm	/*
11755682Smarkm	 * more_elements is pre-computed in case the direction is
11855682Smarkm	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
11955682Smarkm	 * xdr_bool when the direction is XDR_DECODE.
12055682Smarkm	 */
12155682Smarkm	bool_t more_elements;
12255682Smarkm	int freeing = (xdrs->x_op == XDR_FREE);
12355682Smarkm	rpcblist_ptr next;
12455682Smarkm	rpcblist_ptr next_copy;
12555682Smarkm
12655682Smarkm	next = NULL;
12755682Smarkm	for (;;) {
12855682Smarkm		more_elements = (bool_t)(*rp != NULL);
12955682Smarkm		if (! xdr_bool(xdrs, &more_elements)) {
13055682Smarkm			return (FALSE);
13155682Smarkm		}
13255682Smarkm		if (! more_elements) {
13355682Smarkm			return (TRUE);  /* we are done */
13455682Smarkm		}
13555682Smarkm		/*
13655682Smarkm		 * the unfortunate side effect of non-recursion is that in
13755682Smarkm		 * the case of freeing we must remember the next object
13855682Smarkm		 * before we free the current object ...
13955682Smarkm		 */
14055682Smarkm		if (freeing && *rp)
14155682Smarkm			next = (*rp)->rpcb_next;
14255682Smarkm		if (! xdr_reference(xdrs, (caddr_t *)rp,
14355682Smarkm		    (u_int)sizeof (RPCBLIST), (xdrproc_t)xdr_rpcb)) {
14455682Smarkm			return (FALSE);
14555682Smarkm		}
14655682Smarkm		if (freeing) {
14755682Smarkm			next_copy = next;
14855682Smarkm			rp = &next_copy;
14955682Smarkm			/*
15055682Smarkm			 * Note that in the subsequent iteration, next_copy
15155682Smarkm			 * gets nulled out by the xdr_reference
15255682Smarkm			 * but next itself survives.
15355682Smarkm			 */
154233294Sstas		} else if (*rp) {
15555682Smarkm			rp = &((*rp)->rpcb_next);
15655682Smarkm		}
15755682Smarkm	}
15855682Smarkm	/*NOTREACHED*/
15955682Smarkm}
16055682Smarkm
16155682Smarkm#if 0
16255682Smarkm/*
16355682Smarkm * xdr_rpcblist() is specified to take a RPCBLIST **, but is identical in
16455682Smarkm * functionality to xdr_rpcblist_ptr().
16555682Smarkm */
16655682Smarkmbool_t
16755682Smarkmxdr_rpcblist(XDR *xdrs, RPCBLIST **rp)
16855682Smarkm{
169233294Sstas	bool_t	dummy;
17055682Smarkm
17155682Smarkm	dummy = xdr_rpcblist_ptr(xdrs, (rpcblist_ptr *)rp);
17255682Smarkm	return (dummy);
17355682Smarkm}
17490926Snectar#endif
17555682Smarkm
17655682Smarkmbool_t
17755682Smarkmxdr_rpcb_entry(XDR *xdrs, rpcb_entry *objp)
17855682Smarkm{
17955682Smarkm	if (!xdr_string(xdrs, &objp->r_maddr, (u_int)~0)) {
18055682Smarkm		return (FALSE);
18155682Smarkm	}
18255682Smarkm	if (!xdr_string(xdrs, &objp->r_nc_netid, (u_int)~0)) {
18355682Smarkm		return (FALSE);
18455682Smarkm	}
18555682Smarkm	if (!xdr_uint32_t(xdrs, &objp->r_nc_semantics)) {
18655682Smarkm		return (FALSE);
18755682Smarkm	}
18855682Smarkm	if (!xdr_string(xdrs, &objp->r_nc_protofmly, (u_int)~0)) {
18955682Smarkm		return (FALSE);
19055682Smarkm	}
19155682Smarkm	if (!xdr_string(xdrs, &objp->r_nc_proto, (u_int)~0)) {
19255682Smarkm		return (FALSE);
19355682Smarkm	}
19455682Smarkm	return (TRUE);
19555682Smarkm}
19655682Smarkm
19755682Smarkmbool_t
19855682Smarkmxdr_rpcb_entry_list_ptr(XDR *xdrs, rpcb_entry_list_ptr *rp)
19955682Smarkm{
20055682Smarkm	/*
20155682Smarkm	 * more_elements is pre-computed in case the direction is
20255682Smarkm	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
20355682Smarkm	 * xdr_bool when the direction is XDR_DECODE.
20455682Smarkm	 */
20555682Smarkm	bool_t more_elements;
20655682Smarkm	int freeing = (xdrs->x_op == XDR_FREE);
20755682Smarkm	rpcb_entry_list_ptr next;
20855682Smarkm	rpcb_entry_list_ptr next_copy;
20955682Smarkm
21055682Smarkm	next = NULL;
21155682Smarkm	for (;;) {
21255682Smarkm		more_elements = (bool_t)(*rp != NULL);
21355682Smarkm		if (! xdr_bool(xdrs, &more_elements)) {
21455682Smarkm			return (FALSE);
21555682Smarkm		}
21655682Smarkm		if (! more_elements) {
21755682Smarkm			return (TRUE);  /* we are done */
21855682Smarkm		}
21955682Smarkm		/*
22055682Smarkm		 * the unfortunate side effect of non-recursion is that in
22155682Smarkm		 * the case of freeing we must remember the next object
22255682Smarkm		 * before we free the current object ...
22355682Smarkm		 */
22455682Smarkm		if (freeing)
22555682Smarkm			next = (*rp)->rpcb_entry_next;
22655682Smarkm		if (! xdr_reference(xdrs, (caddr_t *)rp,
22755682Smarkm		    (u_int)sizeof (rpcb_entry_list),
22855682Smarkm				    (xdrproc_t)xdr_rpcb_entry)) {
22955682Smarkm			return (FALSE);
23055682Smarkm		}
23155682Smarkm		if (freeing && *rp) {
23255682Smarkm			next_copy = next;
23355682Smarkm			rp = &next_copy;
23455682Smarkm			/*
23555682Smarkm			 * Note that in the subsequent iteration, next_copy
23655682Smarkm			 * gets nulled out by the xdr_reference
23755682Smarkm			 * but next itself survives.
23855682Smarkm			 */
23955682Smarkm		} else if (*rp) {
24055682Smarkm			rp = &((*rp)->rpcb_entry_next);
24155682Smarkm		}
24255682Smarkm	}
24355682Smarkm	/*NOTREACHED*/
24455682Smarkm}
24555682Smarkm