xref: /illumos-gate/usr/src/lib/libnsl/rpc/xdr_refer.c (revision 22cc57556161a28b2141976ff578db2558def3e1)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2161961e0fSrobinson  */
2261961e0fSrobinson 
2361961e0fSrobinson /*
24e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
27e8031f0aSraf 
287c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
297c478bd9Sstevel@tonic-gate /* All Rights Reserved */
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
327c478bd9Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
337c478bd9Sstevel@tonic-gate  * California.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
37e8031f0aSraf  * Generic XDR routines impelmentation.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
407c478bd9Sstevel@tonic-gate  * "pointers".  See xdr.h for more info on the interface to xdr.
417c478bd9Sstevel@tonic-gate  */
42e8031f0aSraf #include "mt.h"
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <syslog.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <rpc/types.h>
487c478bd9Sstevel@tonic-gate #include <rpc/xdr.h>
497c478bd9Sstevel@tonic-gate #include <memory.h>
507c478bd9Sstevel@tonic-gate 
5161961e0fSrobinson #define	LASTUNSIGNED	((uint_t)0-1)
527c478bd9Sstevel@tonic-gate char mem_err_msg_ref[] = "xdr_reference: out of memory";
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * XDR an indirect pointer
567c478bd9Sstevel@tonic-gate  * xdr_reference is for recursively translating a structure that is
577c478bd9Sstevel@tonic-gate  * referenced by a pointer inside the structure that is currently being
587c478bd9Sstevel@tonic-gate  * translated.  pp references a pointer to storage. If *pp is null
597c478bd9Sstevel@tonic-gate  * the  necessary storage is allocated.
607c478bd9Sstevel@tonic-gate  * size is the sizeof the referneced structure.
617c478bd9Sstevel@tonic-gate  * proc is the routine to handle the referenced structure.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate bool_t
xdr_reference(XDR * xdrs,caddr_t * pp,uint_t size,const xdrproc_t proc)6461961e0fSrobinson xdr_reference(XDR *xdrs, caddr_t *pp, uint_t size, const xdrproc_t proc)
657c478bd9Sstevel@tonic-gate {
6661961e0fSrobinson 	caddr_t loc = *pp;
6761961e0fSrobinson 	bool_t stat;
687c478bd9Sstevel@tonic-gate 
69*22cc5755SMarcel Telka 	/* Make sure x_op contains a valid value */
70*22cc5755SMarcel Telka 	if (xdrs->x_op != XDR_ENCODE &&
71*22cc5755SMarcel Telka 	    xdrs->x_op != XDR_DECODE &&
72*22cc5755SMarcel Telka 	    xdrs->x_op != XDR_FREE)
73*22cc5755SMarcel Telka 		return (FALSE);
74*22cc5755SMarcel Telka 
757c478bd9Sstevel@tonic-gate 	if (loc == NULL)
767c478bd9Sstevel@tonic-gate 		switch (xdrs->x_op) {
777c478bd9Sstevel@tonic-gate 		case XDR_FREE:
787c478bd9Sstevel@tonic-gate 			return (TRUE);
797c478bd9Sstevel@tonic-gate 		case XDR_DECODE:
8061961e0fSrobinson 			*pp = loc = malloc(size);
817c478bd9Sstevel@tonic-gate 			if (loc == NULL) {
827c478bd9Sstevel@tonic-gate 				(void) syslog(LOG_ERR, mem_err_msg_ref);
837c478bd9Sstevel@tonic-gate 				return (FALSE);
847c478bd9Sstevel@tonic-gate 			}
857c478bd9Sstevel@tonic-gate 			(void) memset(loc, 0, (int)size);
867c478bd9Sstevel@tonic-gate 			break;
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	stat = (*proc)(xdrs, loc, LASTUNSIGNED);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if (xdrs->x_op == XDR_FREE) {
9261961e0fSrobinson 		free(loc);
937c478bd9Sstevel@tonic-gate 		*pp = NULL;
947c478bd9Sstevel@tonic-gate 	}
957c478bd9Sstevel@tonic-gate 	return (stat);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate /*
1007c478bd9Sstevel@tonic-gate  * xdr_pointer():
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * XDR a pointer to a possibly recursive data structure. This
1037c478bd9Sstevel@tonic-gate  * differs with xdr_reference in that it can serialize/deserialiaze
1047c478bd9Sstevel@tonic-gate  * trees correctly.
1057c478bd9Sstevel@tonic-gate  *
1067c478bd9Sstevel@tonic-gate  *  What's sent is actually a union:
1077c478bd9Sstevel@tonic-gate  *
1087c478bd9Sstevel@tonic-gate  *  union object_pointer switch (boolean b) {
1097c478bd9Sstevel@tonic-gate  *  case TRUE: object_data data;
1107c478bd9Sstevel@tonic-gate  *  case FALSE: void nothing;
1117c478bd9Sstevel@tonic-gate  *  }
1127c478bd9Sstevel@tonic-gate  *
1137c478bd9Sstevel@tonic-gate  * > objpp: Pointer to the pointer to the object.
1147c478bd9Sstevel@tonic-gate  * > obj_size: size of the object.
1157c478bd9Sstevel@tonic-gate  * > xdr_obj: routine to XDR an object.
1167c478bd9Sstevel@tonic-gate  *
1177c478bd9Sstevel@tonic-gate  */
1187c478bd9Sstevel@tonic-gate bool_t
xdr_pointer(XDR * xdrs,char ** objpp,uint_t obj_size,const xdrproc_t xdr_obj)11961961e0fSrobinson xdr_pointer(XDR *xdrs, char **objpp, uint_t obj_size, const xdrproc_t xdr_obj)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	bool_t more_data;
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	more_data = (*objpp != NULL);
12461961e0fSrobinson 	if (!xdr_bool(xdrs, &more_data))
1257c478bd9Sstevel@tonic-gate 		return (FALSE);
1267c478bd9Sstevel@tonic-gate 	if (!more_data) {
1277c478bd9Sstevel@tonic-gate 		*objpp = NULL;
1287c478bd9Sstevel@tonic-gate 		return (TRUE);
1297c478bd9Sstevel@tonic-gate 	}
13061961e0fSrobinson 	return (xdr_reference(xdrs, objpp, obj_size, xdr_obj));
1317c478bd9Sstevel@tonic-gate }
132