xdr.h revision 261057
1179187Sjb/*	$NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt Exp $	*/
2179187Sjb
3179187Sjb/*-
4179187Sjb * Copyright (c) 2009, Sun Microsystems, Inc.
5179187Sjb * All rights reserved.
6179187Sjb *
7179187Sjb * Redistribution and use in source and binary forms, with or without
8179187Sjb * modification, are permitted provided that the following conditions are met:
9179187Sjb * - Redistributions of source code must retain the above copyright notice,
10179187Sjb *   this list of conditions and the following disclaimer.
11179187Sjb * - Redistributions in binary form must reproduce the above copyright notice,
12179187Sjb *   this list of conditions and the following disclaimer in the documentation
13179187Sjb *   and/or other materials provided with the distribution.
14179187Sjb * - Neither the name of Sun Microsystems, Inc. nor the names of its
15179187Sjb *   contributors may be used to endorse or promote products derived
16179187Sjb *   from this software without specific prior written permission.
17179187Sjb *
18179187Sjb * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19179187Sjb * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20179187Sjb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21179187Sjb * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22179187Sjb * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23179187Sjb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24179187Sjb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25179187Sjb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26179187Sjb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27179187Sjb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28179187Sjb * POSSIBILITY OF SUCH DAMAGE.
29179187Sjb *
30179187Sjb *	from: @(#)xdr.h 1.19 87/04/22 SMI
31179187Sjb *	from: @(#)xdr.h	2.2 88/07/29 4.0 RPCSRC
32179187Sjb * $FreeBSD: stable/9/sys/rpc/xdr.h 261057 2014-01-23 00:28:17Z mav $
33179187Sjb */
34179187Sjb
35179187Sjb/*
36179187Sjb * xdr.h, External Data Representation Serialization Routines.
37179187Sjb *
38179187Sjb * Copyright (C) 1984, Sun Microsystems, Inc.
39179187Sjb */
40179187Sjb
41179187Sjb#ifndef _KRPC_XDR_H
42179187Sjb#define _KRPC_XDR_H
43179187Sjb#include <sys/cdefs.h>
44179187Sjb
45179187Sjb/*
46179187Sjb * XDR provides a conventional way for converting between C data
47179187Sjb * types and an external bit-string representation.  Library supplied
48179187Sjb * routines provide for the conversion on built-in C data types.  These
49179187Sjb * routines and utility routines defined here are used to help implement
50179187Sjb * a type encode/decode routine for each user-defined type.
51179187Sjb *
52179187Sjb * Each data type provides a single procedure which takes two arguments:
53179187Sjb *
54179187Sjb *	bool_t
55179187Sjb *	xdrproc(xdrs, argresp)
56179187Sjb *		XDR *xdrs;
57179187Sjb *		<type> *argresp;
58248641Savg *
59179187Sjb * xdrs is an instance of a XDR handle, to which or from which the data
60179187Sjb * type is to be converted.  argresp is a pointer to the structure to be
61179187Sjb * converted.  The XDR handle contains an operation field which indicates
62179187Sjb * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
63179187Sjb *
64179187Sjb * XDR_DECODE may allocate space if the pointer argresp is null.  This
65179187Sjb * data can be freed with the XDR_FREE operation.
66179187Sjb *
67179187Sjb * We write only one procedure per data type to make it easy
68179187Sjb * to keep the encode and decode procedures for a data type consistent.
69179187Sjb * In many cases the same code performs all operations on a user defined type,
70179187Sjb * because all the hard work is done in the component type routines.
71179187Sjb * decode as a series of calls on the nested data types.
72179187Sjb */
73179187Sjb
74179187Sjb/*
75179187Sjb * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
76179187Sjb * stream.  XDR_DECODE causes the type to be extracted from the stream.
77179187Sjb * XDR_FREE can be used to release the space allocated by an XDR_DECODE
78179187Sjb * request.
79179187Sjb */
80179187Sjbenum xdr_op {
81179187Sjb	XDR_ENCODE=0,
82179187Sjb	XDR_DECODE=1,
83179187Sjb	XDR_FREE=2
84179187Sjb};
85179187Sjb
86179187Sjb/*
87179187Sjb * This is the number of bytes per unit of external data.
88179187Sjb */
89179187Sjb#define BYTES_PER_XDR_UNIT	(4)
90179187Sjb#define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
91179187Sjb		    * BYTES_PER_XDR_UNIT)
92179187Sjb
93179187Sjb/*
94179187Sjb * The XDR handle.
95179187Sjb * Contains operation which is being applied to the stream,
96179187Sjb * an operations vector for the particular implementation (e.g. see xdr_mem.c),
97179187Sjb * and two private fields for the use of the particular implementation.
98179187Sjb */
99179187Sjbtypedef struct XDR {
100179187Sjb	enum xdr_op	x_op;		/* operation; fast additional param */
101179187Sjb	const struct xdr_ops {
102179187Sjb		/* get a long from underlying stream */
103179187Sjb		bool_t	(*x_getlong)(struct XDR *, long *);
104179187Sjb		/* put a long to " */
105179187Sjb		bool_t	(*x_putlong)(struct XDR *, const long *);
106179187Sjb		/* get some bytes from " */
107179187Sjb		bool_t	(*x_getbytes)(struct XDR *, char *, u_int);
108179187Sjb		/* put some bytes to " */
109179187Sjb		bool_t	(*x_putbytes)(struct XDR *, const char *, u_int);
110179187Sjb		/* returns bytes off from beginning */
111179187Sjb		u_int	(*x_getpostn)(struct XDR *);
112179187Sjb		/* lets you reposition the stream */
113179187Sjb		bool_t  (*x_setpostn)(struct XDR *, u_int);
114179187Sjb		/* buf quick ptr to buffered data */
115179187Sjb		int32_t *(*x_inline)(struct XDR *, u_int);
116179187Sjb		/* free privates of this xdr_stream */
117179187Sjb		void	(*x_destroy)(struct XDR *);
118179187Sjb		bool_t	(*x_control)(struct XDR *, int, void *);
119179187Sjb	} *x_ops;
120179187Sjb	char *	 	x_public;	/* users' data */
121179187Sjb	void *		x_private;	/* pointer to private data */
122179187Sjb	char * 		x_base;		/* private used for position info */
123179187Sjb	u_int		x_handy;	/* extra private word */
124179187Sjb} XDR;
125179187Sjb
126179187Sjb/*
127179187Sjb * A xdrproc_t exists for each data type which is to be encoded or decoded.
128179187Sjb *
129179187Sjb * The second argument to the xdrproc_t is a pointer to an opaque pointer.
130179187Sjb * The opaque pointer generally points to a structure of the data type
131179187Sjb * to be decoded.  If this pointer is 0, then the type routines should
132179187Sjb * allocate dynamic storage of the appropriate size and return it.
133179187Sjb */
134179187Sjb#ifdef _KERNEL
135179187Sjbtypedef	bool_t (*xdrproc_t)(XDR *, void *, ...);
136179187Sjb#else
137179187Sjb/*
138179187Sjb * XXX can't actually prototype it, because some take three args!!!
139179187Sjb */
140179187Sjbtypedef	bool_t (*xdrproc_t)(XDR *, ...);
141179187Sjb#endif
142179187Sjb
143179187Sjb/*
144179187Sjb * Operations defined on a XDR handle
145179187Sjb *
146179187Sjb * XDR		*xdrs;
147179187Sjb * long		*longp;
148179187Sjb * char *	 addr;
149179187Sjb * u_int	 len;
150179187Sjb * u_int	 pos;
151179187Sjb */
152179187Sjb#define XDR_GETLONG(xdrs, longp)			\
153179187Sjb	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
154179187Sjb#define xdr_getlong(xdrs, longp)			\
155179187Sjb	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
156179187Sjb
157179187Sjb#define XDR_PUTLONG(xdrs, longp)			\
158179187Sjb	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
159179187Sjb#define xdr_putlong(xdrs, longp)			\
160179187Sjb	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
161179187Sjb
162179187Sjbstatic __inline int
163179187Sjbxdr_getint32(XDR *xdrs, int32_t *ip)
164179187Sjb{
165179187Sjb	long l;
166179187Sjb
167179187Sjb	if (!xdr_getlong(xdrs, &l))
168179187Sjb		return (FALSE);
169179187Sjb	*ip = (int32_t)l;
170179187Sjb	return (TRUE);
171179187Sjb}
172179187Sjb
173179187Sjbstatic __inline int
174179187Sjbxdr_putint32(XDR *xdrs, int32_t *ip)
175179187Sjb{
176179187Sjb	long l;
177179187Sjb
178179187Sjb	l = (long)*ip;
179179187Sjb	return xdr_putlong(xdrs, &l);
180179187Sjb}
181179187Sjb
182179187Sjb#define XDR_GETINT32(xdrs, int32p)	xdr_getint32(xdrs, int32p)
183179187Sjb#define XDR_PUTINT32(xdrs, int32p)	xdr_putint32(xdrs, int32p)
184179187Sjb
185179187Sjb#define XDR_GETBYTES(xdrs, addr, len)			\
186179187Sjb	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
187179187Sjb#define xdr_getbytes(xdrs, addr, len)			\
188179187Sjb	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
189179187Sjb
190#define XDR_PUTBYTES(xdrs, addr, len)			\
191	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
192#define xdr_putbytes(xdrs, addr, len)			\
193	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
194
195#define XDR_GETPOS(xdrs)				\
196	(*(xdrs)->x_ops->x_getpostn)(xdrs)
197#define xdr_getpos(xdrs)				\
198	(*(xdrs)->x_ops->x_getpostn)(xdrs)
199
200#define XDR_SETPOS(xdrs, pos)				\
201	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
202#define xdr_setpos(xdrs, pos)				\
203	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
204
205#define	XDR_INLINE(xdrs, len)				\
206	(*(xdrs)->x_ops->x_inline)(xdrs, len)
207#define	xdr_inline(xdrs, len)				\
208	(*(xdrs)->x_ops->x_inline)(xdrs, len)
209
210#define	XDR_DESTROY(xdrs)				\
211	if ((xdrs)->x_ops->x_destroy) 			\
212		(*(xdrs)->x_ops->x_destroy)(xdrs)
213#define	xdr_destroy(xdrs)				\
214	if ((xdrs)->x_ops->x_destroy) 			\
215		(*(xdrs)->x_ops->x_destroy)(xdrs)
216
217#define XDR_CONTROL(xdrs, req, op)			\
218	(((xdrs)->x_ops->x_control == NULL) ? (FALSE) :	\
219		(*(xdrs)->x_ops->x_control)(xdrs, req, op))
220#define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
221
222/*
223 * Solaris strips the '_t' from these types -- not sure why.
224 * But, let's be compatible.
225 */
226#define xdr_rpcvers(xdrs, versp) xdr_uint32_t(xdrs, versp)
227#define xdr_rpcprog(xdrs, progp) xdr_uint32_t(xdrs, progp)
228#define xdr_rpcproc(xdrs, procp) xdr_uint32_t(xdrs, procp)
229#define xdr_rpcprot(xdrs, protp) xdr_uint32_t(xdrs, protp)
230#define xdr_rpcport(xdrs, portp) xdr_uint32_t(xdrs, portp)
231
232/*
233 * Support struct for discriminated unions.
234 * You create an array of xdrdiscrim structures, terminated with
235 * an entry with a null procedure pointer.  The xdr_union routine gets
236 * the discriminant value and then searches the array of structures
237 * for a matching value.  If a match is found the associated xdr routine
238 * is called to handle that part of the union.  If there is
239 * no match, then a default routine may be called.
240 * If there is no match and no default routine it is an error.
241 */
242#define NULL_xdrproc_t ((xdrproc_t)0)
243struct xdr_discrim {
244	int	value;
245	xdrproc_t proc;
246};
247
248/*
249 * In-line routines for fast encode/decode of primitive data types.
250 * Caveat emptor: these use single memory cycles to get the
251 * data from the underlying buffer, and will fail to operate
252 * properly if the data is not aligned.  The standard way to use these
253 * is to say:
254 *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
255 *		return (FALSE);
256 *	<<< macro calls >>>
257 * where ``count'' is the number of bytes of data occupied
258 * by the primitive data types.
259 *
260 * N.B. and frozen for all time: each data type here uses 4 bytes
261 * of external representation.
262 */
263#define IXDR_GET_INT32(buf)		((int32_t)__ntohl((uint32_t)*(buf)++))
264#define IXDR_PUT_INT32(buf, v)		(*(buf)++ =(int32_t)__htonl((uint32_t)v))
265#define IXDR_GET_U_INT32(buf)		((uint32_t)IXDR_GET_INT32(buf))
266#define IXDR_PUT_U_INT32(buf, v)	IXDR_PUT_INT32((buf), ((int32_t)(v)))
267
268#define IXDR_GET_UINT32(buf)		((uint32_t)IXDR_GET_INT32(buf))
269#define IXDR_PUT_UINT32(buf, v)		IXDR_PUT_INT32((buf), ((int32_t)(v)))
270
271#define IXDR_GET_LONG(buf)		((long)__ntohl((uint32_t)*(buf)++))
272#define IXDR_PUT_LONG(buf, v)		(*(buf)++ =(int32_t)__htonl((uint32_t)v))
273
274#define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
275#define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf))
276#define IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_LONG(buf))
277#define IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf))
278#define IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_LONG(buf))
279
280#define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), (v))
281#define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), (v))
282#define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), (v))
283#define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), (v))
284#define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), (v))
285
286/*
287 * These are the "generic" xdr routines.
288 */
289__BEGIN_DECLS
290extern bool_t	xdr_void(void);
291extern bool_t	xdr_int(XDR *, int *);
292extern bool_t	xdr_u_int(XDR *, u_int *);
293extern bool_t	xdr_long(XDR *, long *);
294extern bool_t	xdr_u_long(XDR *, u_long *);
295extern bool_t	xdr_short(XDR *, short *);
296extern bool_t	xdr_u_short(XDR *, u_short *);
297extern bool_t	xdr_int16_t(XDR *, int16_t *);
298extern bool_t	xdr_uint16_t(XDR *, uint16_t *);
299extern bool_t	xdr_int32_t(XDR *, int32_t *);
300extern bool_t	xdr_uint32_t(XDR *, uint32_t *);
301extern bool_t	xdr_int64_t(XDR *, int64_t *);
302extern bool_t	xdr_uint64_t(XDR *, uint64_t *);
303extern bool_t	xdr_bool(XDR *, bool_t *);
304extern bool_t	xdr_enum(XDR *, enum_t *);
305extern bool_t	xdr_array(XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
306extern bool_t	xdr_bytes(XDR *, char **, u_int *, u_int);
307extern bool_t	xdr_opaque(XDR *, char *, u_int);
308extern bool_t	xdr_string(XDR *, char **, u_int);
309extern bool_t	xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
310extern bool_t	xdr_char(XDR *, char *);
311extern bool_t	xdr_u_char(XDR *, u_char *);
312extern bool_t	xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
313extern bool_t	xdr_float(XDR *, float *);
314extern bool_t	xdr_double(XDR *, double *);
315extern bool_t	xdr_quadruple(XDR *, long double *);
316extern bool_t	xdr_reference(XDR *, char **, u_int, xdrproc_t);
317extern bool_t	xdr_pointer(XDR *, char **, u_int, xdrproc_t);
318extern bool_t	xdr_wrapstring(XDR *, char **);
319extern void	xdr_free(xdrproc_t, void *);
320extern bool_t	xdr_hyper(XDR *, quad_t *);
321extern bool_t	xdr_u_hyper(XDR *, u_quad_t *);
322extern bool_t	xdr_longlong_t(XDR *, quad_t *);
323extern bool_t	xdr_u_longlong_t(XDR *, u_quad_t *);
324extern unsigned long xdr_sizeof(xdrproc_t func, void *data);
325__END_DECLS
326
327/*
328 * Common opaque bytes objects used by many rpc protocols;
329 * declared here due to commonality.
330 */
331#define MAX_NETOBJ_SZ 1024
332struct netobj {
333	u_int	n_len;
334	char	*n_bytes;
335};
336typedef struct netobj netobj;
337extern bool_t   xdr_netobj(XDR *, struct netobj *);
338
339/*
340 * These are XDR control operators
341 */
342
343#define	XDR_GET_BYTES_AVAIL 	1
344#define	XDR_PEEK		2
345#define	XDR_SKIPBYTES		3
346
347struct xdr_bytesrec {
348	bool_t xc_is_last_record;
349	size_t xc_num_avail;
350};
351
352typedef struct xdr_bytesrec xdr_bytesrec;
353
354
355/*
356 * These are the public routines for the various implementations of
357 * xdr streams.
358 */
359__BEGIN_DECLS
360/* XDR using memory buffers */
361extern void   xdrmem_create(XDR *, char *, u_int, enum xdr_op);
362
363/* XDR using mbufs */
364struct mbuf;
365extern void   xdrmbuf_create(XDR *, struct mbuf *, enum xdr_op);
366extern void   xdrmbuf_append(XDR *, struct mbuf *);
367extern struct mbuf * xdrmbuf_getall(XDR *);
368
369/* XDR pseudo records for tcp */
370extern void   xdrrec_create(XDR *, u_int, u_int, void *,
371			    int (*)(void *, void *, int),
372			    int (*)(void *, void *, int));
373
374/* make end of xdr record */
375extern bool_t xdrrec_endofrecord(XDR *, int);
376
377/* move to beginning of next record */
378extern bool_t xdrrec_skiprecord(XDR *);
379
380/* true if no more input */
381extern bool_t xdrrec_eof(XDR *);
382extern u_int xdrrec_readbytes(XDR *, caddr_t, u_int);
383__END_DECLS
384
385#endif /* !_KRPC_XDR_H */
386