xref: /illumos-gate/usr/src/lib/libnsl/rpc/xdr_refer.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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  * Copyright 1991 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
27 /*
28  * Portions of this source code were derived from Berkeley
29  * 4.3 BSD under license from the Regents of the University of
30  * California.
31  */
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 /*
36  * xdr_refer.c, Generic XDR routines impelmentation.
37  *
38  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
39  * "pointers".  See xdr.h for more info on the interface to xdr.
40  */
41 #include <sys/types.h>
42 #include <rpc/trace.h>
43 #ifdef KERNEL
44 #include <sys/param.h>
45 #else
46 #include <syslog.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #endif
50 #include <rpc/types.h>
51 #include <rpc/xdr.h>
52 #include <memory.h>
53 
54 #define	LASTUNSIGNED	((u_int)0-1)
55 char mem_err_msg_ref[] = "xdr_reference: out of memory";
56 
57 /*
58  * XDR an indirect pointer
59  * xdr_reference is for recursively translating a structure that is
60  * referenced by a pointer inside the structure that is currently being
61  * translated.  pp references a pointer to storage. If *pp is null
62  * the  necessary storage is allocated.
63  * size is the sizeof the referneced structure.
64  * proc is the routine to handle the referenced structure.
65  */
66 bool_t
67 xdr_reference(XDR *xdrs, caddr_t *pp, u_int size, xdrproc_t proc)
68 {
69 	register caddr_t loc = *pp;
70 	register bool_t stat;
71 
72 	trace2(TR_xdr_reference, 0, size);
73 	if (loc == NULL)
74 		switch (xdrs->x_op) {
75 		case XDR_FREE:
76 			trace1(TR_xdr_reference, 1);
77 			return (TRUE);
78 
79 		case XDR_DECODE:
80 			*pp = loc = (caddr_t) mem_alloc(size);
81 #ifndef KERNEL
82 			if (loc == NULL) {
83 				(void) syslog(LOG_ERR, mem_err_msg_ref);
84 
85 				trace1(TR_xdr_reference, 1);
86 				return (FALSE);
87 			}
88 			(void) memset(loc, 0, (int)size);
89 #else
90 			(void) memset(loc, 0, size);
91 #endif
92 			break;
93 	}
94 
95 	stat = (*proc)(xdrs, loc, LASTUNSIGNED);
96 
97 	if (xdrs->x_op == XDR_FREE) {
98 		mem_free(loc, size);
99 		*pp = NULL;
100 	}
101 	trace1(TR_xdr_reference, 1);
102 	return (stat);
103 }
104 
105 
106 #ifndef KERNEL
107 /*
108  * xdr_pointer():
109  *
110  * XDR a pointer to a possibly recursive data structure. This
111  * differs with xdr_reference in that it can serialize/deserialiaze
112  * trees correctly.
113  *
114  *  What's sent is actually a union:
115  *
116  *  union object_pointer switch (boolean b) {
117  *  case TRUE: object_data data;
118  *  case FALSE: void nothing;
119  *  }
120  *
121  * > objpp: Pointer to the pointer to the object.
122  * > obj_size: size of the object.
123  * > xdr_obj: routine to XDR an object.
124  *
125  */
126 bool_t
127 xdr_pointer(XDR *xdrs, char **objpp, u_int obj_size, xdrproc_t xdr_obj)
128 {
129 	bool_t more_data;
130 	bool_t dummy;
131 
132 	trace2(TR_xdr_pointer, 0, obj_size);
133 	more_data = (*objpp != NULL);
134 	if (! xdr_bool(xdrs, &more_data)) {
135 		trace1(TR_xdr_pointer, 1);
136 		return (FALSE);
137 	}
138 	if (! more_data) {
139 		*objpp = NULL;
140 		trace1(TR_xdr_pointer, 1);
141 		return (TRUE);
142 	}
143 	dummy = xdr_reference(xdrs, objpp, obj_size, xdr_obj);
144 	trace1(TR_xdr_pointer, 1);
145 	return (dummy);
146 }
147 #endif /* ! KERNEL */
148