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