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