xref: /dragonfly/include/rpc/xdr.h (revision f401eab7)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  *	from: @(#)xdr.h 1.19 87/04/22 SMI
29  *	from: @(#)xdr.h	2.2 88/07/29 4.0 RPCSRC
30  * $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt Exp $
31  * $FreeBSD: src/include/rpc/xdr.h,v 1.23 2003/03/07 13:19:40 nectar Exp $
32  */
33 
34 /*
35  * xdr.h, External Data Representation Serialization Routines.
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  */
39 
40 #ifndef _RPC_XDR_H
41 #define _RPC_XDR_H
42 #include <sys/cdefs.h>
43 
44 /*
45  * XDR provides a conventional way for converting between C data
46  * types and an external bit-string representation.  Library supplied
47  * routines provide for the conversion on built-in C data types.  These
48  * routines and utility routines defined here are used to help implement
49  * a type encode/decode routine for each user-defined type.
50  *
51  * Each data type provides a single procedure which takes two arguments:
52  *
53  *	bool_t
54  *	xdrproc(xdrs, argresp)
55  *		XDR *xdrs;
56  *		<type> *argresp;
57  *
58  * xdrs is an instance of a XDR handle, to which or from which the data
59  * type is to be converted.  argresp is a pointer to the structure to be
60  * converted.  The XDR handle contains an operation field which indicates
61  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
62  *
63  * XDR_DECODE may allocate space if the pointer argresp is null.  This
64  * data can be freed with the XDR_FREE operation.
65  *
66  * We write only one procedure per data type to make it easy
67  * to keep the encode and decode procedures for a data type consistent.
68  * In many cases the same code performs all operations on a user defined type,
69  * because all the hard work is done in the component type routines.
70  * decode as a series of calls on the nested data types.
71  */
72 
73 /*
74  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
75  * stream.  XDR_DECODE causes the type to be extracted from the stream.
76  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
77  * request.
78  */
79 enum xdr_op {
80 	XDR_ENCODE=0,
81 	XDR_DECODE=1,
82 	XDR_FREE=2
83 };
84 
85 /*
86  * This is the number of bytes per unit of external data.
87  */
88 #define BYTES_PER_XDR_UNIT	(4)
89 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
90 		    * BYTES_PER_XDR_UNIT)
91 
92 /*
93  * The XDR handle.
94  * Contains operation which is being applied to the stream,
95  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
96  * and two private fields for the use of the particular implementation.
97  */
98 typedef struct __rpc_xdr {
99 	enum xdr_op	x_op;		/* operation; fast additional param */
100 	const struct xdr_ops {
101 		/* get a long from underlying stream */
102 		bool_t	(*x_getlong)(struct __rpc_xdr *, long *);
103 		/* put a long to " */
104 		bool_t	(*x_putlong)(struct __rpc_xdr *, const long *);
105 		/* get some bytes from " */
106 		bool_t	(*x_getbytes)(struct __rpc_xdr *, char *, u_int);
107 		/* put some bytes to " */
108 		bool_t	(*x_putbytes)(struct __rpc_xdr *, const char *, u_int);
109 		/* returns bytes off from beginning */
110 		u_int	(*x_getpostn)(struct __rpc_xdr *);
111 		/* lets you reposition the stream */
112 		bool_t  (*x_setpostn)(struct __rpc_xdr *, u_int);
113 		/* buf quick ptr to buffered data */
114 		int32_t *(*x_inline)(struct __rpc_xdr *, u_int);
115 		/* free privates of this xdr_stream */
116 		void	(*x_destroy)(struct __rpc_xdr *);
117 		bool_t	(*x_control)(struct __rpc_xdr *, int, void *);
118 	} *x_ops;
119 	char *	 	x_public;	/* users' data */
120 	void *		x_private;	/* pointer to private data */
121 	char * 		x_base;		/* private used for position info */
122 	u_int		x_handy;	/* extra private word */
123 } XDR;
124 
125 /*
126  * A xdrproc_t exists for each data type which is to be encoded or decoded.
127  *
128  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
129  * The opaque pointer generally points to a structure of the data type
130  * to be decoded.  If this pointer is 0, then the type routines should
131  * allocate dynamic storage of the appropriate size and return it.
132  */
133 #ifdef _KERNEL
134 typedef	bool_t (*xdrproc_t)(XDR *, void *, u_int);
135 #else
136 /*
137  * XXX can't actually prototype it, because some take three args!!!
138  */
139 typedef	bool_t (*xdrproc_t)(XDR *, ...);
140 #endif
141 
142 /*
143  * Operations defined on a XDR handle
144  *
145  * XDR		*xdrs;
146  * long		*longp;
147  * char *	 addr;
148  * u_int	 len;
149  * u_int	 pos;
150  */
151 #define XDR_GETLONG(xdrs, longp)			\
152 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
153 #define xdr_getlong(xdrs, longp)			\
154 	(*(xdrs)->x_ops->x_getlong)(xdrs, longp)
155 
156 #define XDR_PUTLONG(xdrs, longp)			\
157 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
158 #define xdr_putlong(xdrs, longp)			\
159 	(*(xdrs)->x_ops->x_putlong)(xdrs, longp)
160 
161 static __inline int
xdr_getint32(XDR * xdrs,int32_t * ip)162 xdr_getint32(XDR *xdrs, int32_t *ip)
163 {
164 	long l;
165 
166 	if (!xdr_getlong(xdrs, &l))
167 		return (FALSE);
168 	*ip = (int32_t)l;
169 	return (TRUE);
170 }
171 
172 static __inline int
xdr_putint32(XDR * xdrs,int32_t * ip)173 xdr_putint32(XDR *xdrs, int32_t *ip)
174 {
175 	long l;
176 
177 	l = (long)*ip;
178 	return xdr_putlong(xdrs, &l);
179 }
180 
181 #define XDR_GETINT32(xdrs, int32p)	xdr_getint32(xdrs, int32p)
182 #define XDR_PUTINT32(xdrs, int32p)	xdr_putint32(xdrs, int32p)
183 
184 #define XDR_GETBYTES(xdrs, addr, len)			\
185 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
186 #define xdr_getbytes(xdrs, addr, len)			\
187 	(*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
188 
189 #define XDR_PUTBYTES(xdrs, addr, len)			\
190 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
191 #define xdr_putbytes(xdrs, addr, len)			\
192 	(*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
193 
194 #define XDR_GETPOS(xdrs)				\
195 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
196 #define xdr_getpos(xdrs)				\
197 	(*(xdrs)->x_ops->x_getpostn)(xdrs)
198 
199 #define XDR_SETPOS(xdrs, pos)				\
200 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
201 #define xdr_setpos(xdrs, pos)				\
202 	(*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
203 
204 #define	XDR_INLINE(xdrs, len)				\
205 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
206 #define	xdr_inline(xdrs, len)				\
207 	(*(xdrs)->x_ops->x_inline)(xdrs, len)
208 
209 #define	XDR_DESTROY(xdrs)				\
210 	if ((xdrs)->x_ops->x_destroy) 			\
211 		(*(xdrs)->x_ops->x_destroy)(xdrs)
212 #define	xdr_destroy(xdrs)				\
213 	if ((xdrs)->x_ops->x_destroy) 			\
214 		(*(xdrs)->x_ops->x_destroy)(xdrs)
215 
216 #define XDR_CONTROL(xdrs, req, op)			\
217 	if ((xdrs)->x_ops->x_control)			\
218 		(*(xdrs)->x_ops->x_control)(xdrs, req, op)
219 #define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
220 
221 #define xdr_rpcvers(xdrs, versp) xdr_u_int32_t(xdrs, versp)
222 #define xdr_rpcprog(xdrs, progp) xdr_u_int32_t(xdrs, progp)
223 #define xdr_rpcproc(xdrs, procp) xdr_u_int32_t(xdrs, procp)
224 #define xdr_rpcprot(xdrs, protp) xdr_u_int32_t(xdrs, protp)
225 #define xdr_rpcport(xdrs, portp) xdr_u_int32_t(xdrs, portp)
226 
227 /*
228  * Support struct for discriminated unions.
229  * You create an array of xdrdiscrim structures, terminated with
230  * an entry with a null procedure pointer.  The xdr_union routine gets
231  * the discriminant value and then searches the array of structures
232  * for a matching value.  If a match is found the associated xdr routine
233  * is called to handle that part of the union.  If there is
234  * no match, then a default routine may be called.
235  * If there is no match and no default routine it is an error.
236  */
237 #define NULL_xdrproc_t ((xdrproc_t)0)
238 struct xdr_discrim {
239 	int	value;
240 	xdrproc_t proc;
241 };
242 
243 /*
244  * In-line routines for fast encode/decode of primitive data types.
245  * Caveat emptor: these use single memory cycles to get the
246  * data from the underlying buffer, and will fail to operate
247  * properly if the data is not aligned.  The standard way to use these
248  * is to say:
249  *	if ((buf = XDR_INLINE(xdrs, count)) == NULL)
250  *		return (FALSE);
251  *	<<< macro calls >>>
252  * where ``count'' is the number of bytes of data occupied
253  * by the primitive data types.
254  *
255  * N.B. and frozen for all time: each data type here uses 4 bytes
256  * of external representation.
257  */
258 #define IXDR_GET_INT32(buf)		((int32_t)__ntohl((uint32_t)*(buf)++))
259 #define IXDR_PUT_INT32(buf, v)		(*(buf)++ =(int32_t)__htonl((uint32_t)v))
260 #define IXDR_GET_U_INT32(buf)		((uint32_t)IXDR_GET_INT32(buf))
261 #define IXDR_PUT_U_INT32(buf, v)	IXDR_PUT_INT32((buf), ((int32_t)(v)))
262 
263 #define IXDR_GET_LONG(buf)		((long)__ntohl((uint32_t)*(buf)++))
264 #define IXDR_PUT_LONG(buf, v)		(*(buf)++ =(int32_t)__htonl((uint32_t)v))
265 
266 #define IXDR_GET_BOOL(buf)		((bool_t)IXDR_GET_LONG(buf))
267 #define IXDR_GET_ENUM(buf, t)		((t)IXDR_GET_LONG(buf))
268 #define IXDR_GET_U_LONG(buf)		((u_long)IXDR_GET_LONG(buf))
269 #define IXDR_GET_SHORT(buf)		((short)IXDR_GET_LONG(buf))
270 #define IXDR_GET_U_SHORT(buf)		((u_short)IXDR_GET_LONG(buf))
271 
272 #define IXDR_PUT_BOOL(buf, v)		IXDR_PUT_LONG((buf), (v))
273 #define IXDR_PUT_ENUM(buf, v)		IXDR_PUT_LONG((buf), (v))
274 #define IXDR_PUT_U_LONG(buf, v)		IXDR_PUT_LONG((buf), (v))
275 #define IXDR_PUT_SHORT(buf, v)		IXDR_PUT_LONG((buf), (v))
276 #define IXDR_PUT_U_SHORT(buf, v)	IXDR_PUT_LONG((buf), (v))
277 
278 /*
279  * These are the "generic" xdr routines.
280  */
281 __BEGIN_DECLS
282 extern bool_t	xdr_void(void);
283 extern bool_t	xdr_int(XDR *, int *);
284 extern bool_t	xdr_u_int(XDR *, u_int *);
285 extern bool_t	xdr_long(XDR *, long *);
286 extern bool_t	xdr_u_long(XDR *, u_long *);
287 extern bool_t	xdr_short(XDR *, short *);
288 extern bool_t	xdr_u_short(XDR *, u_short *);
289 extern bool_t	xdr_int16_t(XDR *, int16_t *);
290 extern bool_t	xdr_u_int16_t(XDR *, uint16_t *);
291 extern bool_t	xdr_uint16_t(XDR *, uint16_t *);
292 extern bool_t	xdr_int32_t(XDR *, int32_t *);
293 extern bool_t	xdr_u_int32_t(XDR *, uint32_t *);
294 extern bool_t	xdr_uint32_t(XDR *, uint32_t *);
295 extern bool_t	xdr_int64_t(XDR *, int64_t *);
296 extern bool_t	xdr_u_int64_t(XDR *, uint64_t *);
297 extern bool_t	xdr_uint64_t(XDR *, uint64_t *);
298 extern bool_t	xdr_bool(XDR *, bool_t *);
299 extern bool_t	xdr_enum(XDR *, enum_t *);
300 extern bool_t	xdr_array(XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
301 extern bool_t	xdr_bytes(XDR *, char **, u_int *, u_int);
302 extern bool_t	xdr_opaque(XDR *, char *, u_int);
303 extern bool_t	xdr_string(XDR *, char **, u_int);
304 extern bool_t	xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
305 extern bool_t	xdr_char(XDR *, char *);
306 extern bool_t	xdr_u_char(XDR *, u_char *);
307 extern bool_t	xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
308 extern bool_t	xdr_float(XDR *, float *);
309 extern bool_t	xdr_double(XDR *, double *);
310 extern bool_t	xdr_quadruple(XDR *, long double *);
311 extern bool_t	xdr_reference(XDR *, char **, u_int, xdrproc_t);
312 extern bool_t	xdr_pointer(XDR *, char **, u_int, xdrproc_t);
313 extern bool_t	xdr_wrapstring(XDR *, char **);
314 extern void	xdr_free(xdrproc_t, void *);
315 extern bool_t	xdr_hyper(XDR *, quad_t *);
316 extern bool_t	xdr_u_hyper(XDR *, u_quad_t *);
317 extern bool_t	xdr_longlong_t(XDR *, quad_t *);
318 extern bool_t	xdr_u_longlong_t(XDR *, u_quad_t *);
319 extern unsigned long	xdr_sizeof(xdrproc_t, void *);
320 __END_DECLS
321 
322 /*
323  * Common opaque bytes objects used by many rpc protocols;
324  * declared here due to commonality.
325  */
326 #define MAX_NETOBJ_SZ 1024
327 struct netobj {
328 	u_int	n_len;
329 	char	*n_bytes;
330 };
331 typedef struct netobj netobj;
332 extern bool_t   xdr_netobj(XDR *, struct netobj *);
333 
334 /*
335  * These are the public routines for the various implementations of
336  * xdr streams.
337  */
338 __BEGIN_DECLS
339 /* XDR using memory buffers */
340 extern void   xdrmem_create(XDR *, char *, u_int, enum xdr_op);
341 
342 /* XDR using stdio library */
343 #ifdef _STDIO_H_
344 extern void   xdrstdio_create(XDR *, FILE *, enum xdr_op);
345 #endif
346 
347 /* XDR pseudo records for tcp */
348 extern void   xdrrec_create(XDR *, u_int, u_int, void *,
349 			    int (*)(void *, void *, int),
350 			    int (*)(void *, void *, int));
351 
352 /* make end of xdr record */
353 extern bool_t xdrrec_endofrecord(XDR *, int);
354 
355 /* move to beginning of next record */
356 extern bool_t xdrrec_skiprecord(XDR *);
357 
358 /* true if no more input */
359 extern bool_t xdrrec_eof(XDR *);
360 extern u_int xdrrec_readbytes(XDR *, caddr_t, u_int);
361 __END_DECLS
362 
363 #endif /* !_RPC_XDR_H */
364