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