xref: /netbsd-src/lib/libc/rpc/pmap_prot2.c (revision 1f2744e6e4915c9da2a3f980279398c4cf7d5e6d)
1 /*	$NetBSD: pmap_prot2.c,v 1.2 1995/02/25 03:01:50 cgd Exp $	*/
2 
3 /*
4  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5  * unrestricted use provided that this legend is included on all tape
6  * media and as a part of the software program in whole or part.  Users
7  * may copy or modify Sun RPC without charge, but are not authorized
8  * to license or distribute it to anyone else except as part of a product or
9  * program developed by the user.
10  *
11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14  *
15  * Sun RPC is provided with no support and without any obligation on the
16  * part of Sun Microsystems, Inc. to assist in its use, correction,
17  * modification or enhancement.
18  *
19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21  * OR ANY PART THEREOF.
22  *
23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24  * or profits or other special, indirect and consequential damages, even if
25  * Sun has been advised of the possibility of such damages.
26  *
27  * Sun Microsystems, Inc.
28  * 2550 Garcia Avenue
29  * Mountain View, California  94043
30  */
31 
32 #if defined(LIBC_SCCS) && !defined(lint)
33 /*static char *sccsid = "from: @(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";*/
34 /*static char *sccsid = "from: @(#)pmap_prot2.c	2.1 88/07/29 4.0 RPCSRC";*/
35 static char *rcsid = "$NetBSD: pmap_prot2.c,v 1.2 1995/02/25 03:01:50 cgd Exp $";
36 #endif
37 
38 /*
39  * pmap_prot2.c
40  * Protocol for the local binder service, or pmap.
41  *
42  * Copyright (C) 1984, Sun Microsystems, Inc.
43  */
44 
45 #include <rpc/types.h>
46 #include <rpc/xdr.h>
47 #include <rpc/pmap_prot.h>
48 
49 
50 /*
51  * What is going on with linked lists? (!)
52  * First recall the link list declaration from pmap_prot.h:
53  *
54  * struct pmaplist {
55  *	struct pmap pml_map;
56  *	struct pmaplist *pml_map;
57  * };
58  *
59  * Compare that declaration with a corresponding xdr declaration that
60  * is (a) pointer-less, and (b) recursive:
61  *
62  * typedef union switch (bool_t) {
63  *
64  *	case TRUE: struct {
65  *		struct pmap;
66  * 		pmaplist_t foo;
67  *	};
68  *
69  *	case FALSE: struct {};
70  * } pmaplist_t;
71  *
72  * Notice that the xdr declaration has no nxt pointer while
73  * the C declaration has no bool_t variable.  The bool_t can be
74  * interpreted as ``more data follows me''; if FALSE then nothing
75  * follows this bool_t; if TRUE then the bool_t is followed by
76  * an actual struct pmap, and then (recursively) by the
77  * xdr union, pamplist_t.
78  *
79  * This could be implemented via the xdr_union primitive, though this
80  * would cause a one recursive call per element in the list.  Rather than do
81  * that we can ``unwind'' the recursion
82  * into a while loop and do the union arms in-place.
83  *
84  * The head of the list is what the C programmer wishes to past around
85  * the net, yet is the data that the pointer points to which is interesting;
86  * this sounds like a job for xdr_reference!
87  */
88 bool_t
89 xdr_pmaplist(xdrs, rp)
90 	register XDR *xdrs;
91 	register struct pmaplist **rp;
92 {
93 	/*
94 	 * more_elements is pre-computed in case the direction is
95 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
96 	 * xdr_bool when the direction is XDR_DECODE.
97 	 */
98 	bool_t more_elements;
99 	register int freeing = (xdrs->x_op == XDR_FREE);
100 	register struct pmaplist **next;
101 
102 	while (TRUE) {
103 		more_elements = (bool_t)(*rp != NULL);
104 		if (! xdr_bool(xdrs, &more_elements))
105 			return (FALSE);
106 		if (! more_elements)
107 			return (TRUE);  /* we are done */
108 		/*
109 		 * the unfortunate side effect of non-recursion is that in
110 		 * the case of freeing we must remember the next object
111 		 * before we free the current object ...
112 		 */
113 		if (freeing)
114 			next = &((*rp)->pml_next);
115 		if (! xdr_reference(xdrs, (caddr_t *)rp,
116 		    (u_int)sizeof(struct pmaplist), xdr_pmap))
117 			return (FALSE);
118 		rp = (freeing) ? next : &((*rp)->pml_next);
119 	}
120 }
121