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
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.
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]
22 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26 /* All Rights Reserved */
28 * Portions of this source code were derived from Berkeley
29 * 4.3 BSD under license from the Regents of the University of
34 * xdr.h, External Data Representation Serialization Routines.
41 #pragma ident "%Z%%M% %I% %E% SMI"
43 #include <sys/byteorder.h> /* For all ntoh* and hton*() kind of macros */
44 #include <rpc/types.h> /* For all ntoh* and hton*() kind of macros */
46 #include <stdio.h> /* defines FILE *, used in ANSI C function prototypes */
54 * XDR provides a conventional way for converting between C data
55 * types and an external bit-string representation. Library supplied
56 * routines provide for the conversion on built-in C data types. These
57 * routines and utility routines defined here are used to help implement
58 * a type encode/decode routine for each user-defined type.
60 * Each data type provides a single procedure which takes two arguments:
63 * xdrproc(xdrs, argresp)
67 * xdrs is an instance of a XDR handle, to which or from which the data
68 * type is to be converted. argresp is a pointer to the structure to be
69 * converted. The XDR handle contains an operation field which indicates
70 * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
72 * XDR_DECODE may allocate space if the pointer argresp is null. This
73 * data can be freed with the XDR_FREE operation.
75 * We write only one procedure per data type to make it easy
76 * to keep the encode and decode procedures for a data type consistent.
77 * In many cases the same code performs all operations on a user defined type,
78 * because all the hard work is done in the component type routines.
79 * decode as a series of calls on the nested data types.
83 * Xdr operations. XDR_ENCODE causes the type to be encoded into the
84 * stream. XDR_DECODE causes the type to be extracted from the stream.
85 * XDR_FREE can be used to release the space allocated by an XDR_DECODE
95 * This is the number of bytes per unit of external data.
97 #define BYTES_PER_XDR_UNIT (4)
98 #define RNDUP(x) ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
103 * Contains operation which is being applied to the stream,
104 * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
105 * and two private fields for the use of the particular impelementation.
107 * PSARC 2003/523 Contract Private Interface
109 * Changes must be reviewed by Solaris File Sharing
110 * Changes must be communicated to contract-2003-523@sun.com
113 enum xdr_op x_op; /* operation; fast additional param */
114 struct xdr_ops *x_ops;
115 caddr_t x_public; /* users' data */
116 caddr_t x_private; /* pointer to private data */
117 caddr_t x_base; /* private used for position info */
118 int x_handy; /* extra private word */
122 * PSARC 2003/523 Contract Private Interface
124 * Changes must be reviewed by Solaris File Sharing
125 * Changes must be communicated to contract-2003-523@sun.com
129 #if !defined(_KERNEL)
130 bool_t (*x_getlong)(struct XDR *, long *);
131 /* get a long from underlying stream */
132 bool_t (*x_putlong)(struct XDR *, long *);
133 /* put a long to " */
135 bool_t (*x_getbytes)(struct XDR *, caddr_t, int);
136 /* get some bytes from " */
137 bool_t (*x_putbytes)(struct XDR *, caddr_t, int);
138 /* put some bytes to " */
139 uint_t (*x_getpostn)(struct XDR *);
140 /* returns bytes off from beginning */
141 bool_t (*x_setpostn)(struct XDR *, uint_t);
142 /* lets you reposition the stream */
143 rpc_inline_t *(*x_inline)(struct XDR *, int);
144 /* buf quick ptr to buffered data */
145 void (*x_destroy)(struct XDR *);
146 /* free privates of this xdr_stream */
147 bool_t (*x_control)(struct XDR *, int, void *);
148 #if defined(_LP64) || defined(_KERNEL)
149 bool_t (*x_getint32)(struct XDR *, int32_t *);
150 /* get a int from underlying stream */
151 bool_t (*x_putint32)(struct XDR *, int32_t *);
152 /* put an int to " */
153 #endif /* _LP64 || _KERNEL */
155 #if !defined(_KERNEL)
156 bool_t (*x_getlong)(); /* get a long from underlying stream */
157 bool_t (*x_putlong)(); /* put a long to " */
159 bool_t (*x_getbytes)(); /* get some bytes from " */
160 bool_t (*x_putbytes)(); /* put some bytes to " */
161 uint_t (*x_getpostn)(); /* returns bytes off from beginning */
162 bool_t (*x_setpostn)(); /* lets you reposition the stream */
163 rpc_inline_t *(*x_inline)();
164 /* buf quick ptr to buffered data */
165 void (*x_destroy)(); /* free privates of this xdr_stream */
166 bool_t (*x_control)();
167 #if defined(_LP64) || defined(_KERNEL)
168 bool_t (*x_getint32)();
169 bool_t (*x_putint32)();
170 #endif /* _LP64 || defined(_KERNEL) */
175 * Operations defined on a XDR handle
183 #if !defined(_KERNEL)
184 #define XDR_GETLONG(xdrs, longp) \
185 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
186 #define xdr_getlong(xdrs, longp) \
187 (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
189 #define XDR_PUTLONG(xdrs, longp) \
190 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
191 #define xdr_putlong(xdrs, longp) \
192 (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
196 #if !defined(_LP64) && !defined(_KERNEL)
199 * For binary compatability on ILP32 we do not change the shape
200 * of the XDR structure and the GET/PUTINT32 functions just use
201 * the get/putlong vectors which operate on identically-sized
205 #define XDR_GETINT32(xdrs, int32p) \
206 (*(xdrs)->x_ops->x_getlong)(xdrs, (long *)int32p)
207 #define xdr_getint32(xdrs, int32p) \
208 (*(xdrs)->x_ops->x_getlong)(xdrs, (long *)int32p)
210 #define XDR_PUTINT32(xdrs, int32p) \
211 (*(xdrs)->x_ops->x_putlong)(xdrs, (long *)int32p)
212 #define xdr_putint32(xdrs, int32p) \
213 (*(xdrs)->x_ops->x_putlong)(xdrs, (long *)int32p)
215 #else /* !_LP64 && !_KERNEL */
217 #define XDR_GETINT32(xdrs, int32p) \
218 (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
219 #define xdr_getint32(xdrs, int32p) \
220 (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
222 #define XDR_PUTINT32(xdrs, int32p) \
223 (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
224 #define xdr_putint32(xdrs, int32p) \
225 (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
227 #endif /* !_LP64 && !_KERNEL */
229 #define XDR_GETBYTES(xdrs, addr, len) \
230 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
231 #define xdr_getbytes(xdrs, addr, len) \
232 (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
234 #define XDR_PUTBYTES(xdrs, addr, len) \
235 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
236 #define xdr_putbytes(xdrs, addr, len) \
237 (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
239 #define XDR_GETPOS(xdrs) \
240 (*(xdrs)->x_ops->x_getpostn)(xdrs)
241 #define xdr_getpos(xdrs) \
242 (*(xdrs)->x_ops->x_getpostn)(xdrs)
244 #define XDR_SETPOS(xdrs, pos) \
245 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
246 #define xdr_setpos(xdrs, pos) \
247 (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
249 #define XDR_INLINE(xdrs, len) \
250 (*(xdrs)->x_ops->x_inline)(xdrs, len)
251 #define xdr_inline(xdrs, len) \
252 (*(xdrs)->x_ops->x_inline)(xdrs, len)
254 #define XDR_DESTROY(xdrs) \
255 (*(xdrs)->x_ops->x_destroy)(xdrs)
256 #define xdr_destroy(xdrs) \
257 (*(xdrs)->x_ops->x_destroy)(xdrs)
259 #define XDR_CONTROL(xdrs, req, op) \
260 (*(xdrs)->x_ops->x_control)(xdrs, req, op)
261 #define xdr_control(xdrs, req, op) \
262 (*(xdrs)->x_ops->x_control)(xdrs, req, op)
265 * Support struct for discriminated unions.
266 * You create an array of xdrdiscrim structures, terminated with
267 * a entry with a null procedure pointer. The xdr_union routine gets
268 * the discriminant value and then searches the array of structures
269 * for a matching value. If a match is found the associated xdr routine
270 * is called to handle that part of the union. If there is
271 * no match, then a default routine may be called.
272 * If there is no match and no default routine it is an error.
277 * A xdrproc_t exists for each data type which is to be encoded or decoded.
279 * The second argument to the xdrproc_t is a pointer to an opaque pointer.
280 * The opaque pointer generally points to a structure of the data type
281 * to be decoded. If this pointer is 0, then the type routines should
282 * allocate dynamic storage of the appropriate size and return it.
283 * bool_t (*xdrproc_t)(XDR *, void *);
286 typedef bool_t (*xdrproc_t)(XDR *, void *);
289 typedef bool_t (*xdrproc_t)(); /* For Backward compatibility */
291 typedef bool_t (*xdrproc_t)();
295 #define NULL_xdrproc_t ((xdrproc_t)0)
297 #if defined(_LP64) || defined(_I32LPx)
298 #define xdr_rpcvers(xdrs, versp) xdr_u_int(xdrs, versp)
299 #define xdr_rpcprog(xdrs, progp) xdr_u_int(xdrs, progp)
300 #define xdr_rpcproc(xdrs, procp) xdr_u_int(xdrs, procp)
301 #define xdr_rpcprot(xdrs, protp) xdr_u_int(xdrs, protp)
302 #define xdr_rpcport(xdrs, portp) xdr_u_int(xdrs, portp)
304 #define xdr_rpcvers(xdrs, versp) xdr_u_long(xdrs, versp)
305 #define xdr_rpcprog(xdrs, progp) xdr_u_long(xdrs, progp)
306 #define xdr_rpcproc(xdrs, procp) xdr_u_long(xdrs, procp)
307 #define xdr_rpcprot(xdrs, protp) xdr_u_long(xdrs, protp)
308 #define xdr_rpcport(xdrs, portp) xdr_u_long(xdrs, portp)
317 * In-line routines for fast encode/decode of primitve data types.
318 * Caveat emptor: these use single memory cycles to get the
319 * data from the underlying buffer, and will fail to operate
320 * properly if the data is not aligned. The standard way to use these
322 * if ((buf = XDR_INLINE(xdrs, count)) == NULL)
324 * <<< macro calls >>>
325 * where ``count'' is the number of bytes of data occupied
326 * by the primitive data types.
328 * N.B. and frozen for all time: each data type here uses 4 bytes
329 * of external representation.
332 #define IXDR_GET_INT32(buf) ((int32_t)ntohl((uint32_t)*(buf)++))
333 #define IXDR_PUT_INT32(buf, v) (*(buf)++ = (int32_t)htonl((uint32_t)v))
334 #define IXDR_GET_U_INT32(buf) ((uint32_t)IXDR_GET_INT32(buf))
335 #define IXDR_PUT_U_INT32(buf, v) IXDR_PUT_INT32((buf), ((int32_t)(v)))
337 #if !defined(_KERNEL) && !defined(_LP64)
339 #define IXDR_GET_LONG(buf) ((long)ntohl((ulong_t)*(buf)++))
340 #define IXDR_PUT_LONG(buf, v) (*(buf)++ = (long)htonl((ulong_t)v))
341 #define IXDR_GET_U_LONG(buf) ((ulong_t)IXDR_GET_LONG(buf))
342 #define IXDR_PUT_U_LONG(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
344 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_LONG(buf))
345 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_LONG(buf))
346 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_LONG(buf))
347 #define IXDR_GET_U_SHORT(buf) ((ushort_t)IXDR_GET_LONG(buf))
349 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
350 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
351 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
352 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_LONG((buf), ((long)(v)))
356 #define IXDR_GET_BOOL(buf) ((bool_t)IXDR_GET_INT32(buf))
357 #define IXDR_GET_ENUM(buf, t) ((t)IXDR_GET_INT32(buf))
358 #define IXDR_GET_SHORT(buf) ((short)IXDR_GET_INT32(buf))
359 #define IXDR_GET_U_SHORT(buf) ((ushort_t)IXDR_GET_INT32(buf))
361 #define IXDR_PUT_BOOL(buf, v) IXDR_PUT_INT32((buf), ((int)(v)))
362 #define IXDR_PUT_ENUM(buf, v) IXDR_PUT_INT32((buf), ((int)(v)))
363 #define IXDR_PUT_SHORT(buf, v) IXDR_PUT_INT32((buf), ((int)(v)))
364 #define IXDR_PUT_U_SHORT(buf, v) IXDR_PUT_INT32((buf), ((int)(v)))
368 #if BYTE_ORDER == _BIG_ENDIAN
369 #define IXDR_GET_HYPER(buf, v) { \
370 *((int32_t *)(&v)) = ntohl(*(uint32_t *)buf++); \
371 *((int32_t *)(((char *)&v) + BYTES_PER_XDR_UNIT)) \
372 = ntohl(*(uint32_t *)buf++); \
374 #define IXDR_PUT_HYPER(buf, v) { \
375 *(buf)++ = (int32_t)htonl(*(uint32_t *) \
378 (int32_t)htonl(*(uint32_t *)(((char *)&v) \
379 + BYTES_PER_XDR_UNIT)); \
383 #define IXDR_GET_HYPER(buf, v) { \
384 *((int32_t *)(((char *)&v) + \
385 BYTES_PER_XDR_UNIT)) \
386 = ntohl(*(uint32_t *)buf++); \
387 *((int32_t *)(&v)) = \
388 ntohl(*(uint32_t *)buf++); \
391 #define IXDR_PUT_HYPER(buf, v) { \
393 (int32_t)htonl(*(uint32_t *)(((char *)&v) + \
394 BYTES_PER_XDR_UNIT)); \
396 (int32_t)htonl(*(uint32_t *)((char *)&v)); \
399 #define IXDR_GET_U_HYPER(buf, v) IXDR_GET_HYPER(buf, v)
400 #define IXDR_PUT_U_HYPER(buf, v) IXDR_PUT_HYPER(buf, v)
404 * These are the "generic" xdr routines.
407 extern bool_t xdr_void(void);
408 extern bool_t xdr_int(XDR *, int *);
409 extern bool_t xdr_u_int(XDR *, uint_t *);
410 extern bool_t xdr_long(XDR *, long *);
411 extern bool_t xdr_u_long(XDR *, ulong_t *);
412 extern bool_t xdr_short(XDR *, short *);
413 extern bool_t xdr_u_short(XDR *, ushort_t *);
414 extern bool_t xdr_bool(XDR *, bool_t *);
415 extern bool_t xdr_enum(XDR *, enum_t *);
416 extern bool_t xdr_array(XDR *, caddr_t *, uint_t *, const uint_t,
417 const uint_t, const xdrproc_t);
418 extern bool_t xdr_bytes(XDR *, char **, uint_t *, const uint_t);
419 extern bool_t xdr_opaque(XDR *, caddr_t, const uint_t);
420 extern bool_t xdr_string(XDR *, char **, const uint_t);
421 extern bool_t xdr_union(XDR *, enum_t *, char *,
422 const struct xdr_discrim *, const xdrproc_t);
423 extern unsigned int xdr_sizeof(xdrproc_t, void *);
425 extern bool_t xdr_hyper(XDR *, longlong_t *);
426 extern bool_t xdr_longlong_t(XDR *, longlong_t *);
427 extern bool_t xdr_u_hyper(XDR *, u_longlong_t *);
428 extern bool_t xdr_u_longlong_t(XDR *, u_longlong_t *);
430 extern bool_t xdr_char(XDR *, char *);
431 extern bool_t xdr_wrapstring(XDR *, char **);
432 extern bool_t xdr_reference(XDR *, caddr_t *, uint_t, const xdrproc_t);
433 extern bool_t xdr_pointer(XDR *, char **, uint_t, const xdrproc_t);
434 extern void xdr_free(xdrproc_t, char *);
435 extern bool_t xdr_time_t(XDR *, time_t *);
437 extern bool_t xdr_int8_t(XDR *, int8_t *);
438 extern bool_t xdr_uint8_t(XDR *, uint8_t *);
439 extern bool_t xdr_int16_t(XDR *, int16_t *);
440 extern bool_t xdr_uint16_t(XDR *, uint16_t *);
441 extern bool_t xdr_int32_t(XDR *, int32_t *);
442 extern bool_t xdr_uint32_t(XDR *, uint32_t *);
443 #if defined(_INT64_TYPE)
444 extern bool_t xdr_int64_t(XDR *, int64_t *);
445 extern bool_t xdr_uint64_t(XDR *, uint64_t *);
449 extern bool_t xdr_u_char(XDR *, uchar_t *);
450 extern bool_t xdr_vector(XDR *, char *, const uint_t, const uint_t, const
452 extern bool_t xdr_float(XDR *, float *);
453 extern bool_t xdr_double(XDR *, double *);
454 extern bool_t xdr_quadruple(XDR *, long double *);
455 #endif /* !_KERNEL */
457 extern bool_t xdr_void();
458 extern bool_t xdr_int();
459 extern bool_t xdr_u_int();
460 extern bool_t xdr_long();
461 extern bool_t xdr_u_long();
462 extern bool_t xdr_short();
463 extern bool_t xdr_u_short();
464 extern bool_t xdr_bool();
465 extern bool_t xdr_enum();
466 extern bool_t xdr_array();
467 extern bool_t xdr_bytes();
468 extern bool_t xdr_opaque();
469 extern bool_t xdr_string();
470 extern bool_t xdr_union();
472 extern bool_t xdr_hyper();
473 extern bool_t xdr_longlong_t();
474 extern bool_t xdr_u_hyper();
475 extern bool_t xdr_u_longlong_t();
476 extern bool_t xdr_char();
477 extern bool_t xdr_reference();
478 extern bool_t xdr_pointer();
479 extern void xdr_free();
480 extern bool_t xdr_wrapstring();
481 extern bool_t xdr_time_t();
483 extern bool_t xdr_int8_t();
484 extern bool_t xdr_uint8_t();
485 extern bool_t xdr_int16_t();
486 extern bool_t xdr_uint16_t();
487 extern bool_t xdr_int32_t();
488 extern bool_t xdr_uint32_t();
489 #if defined(_INT64_TYPE)
490 extern bool_t xdr_int64_t();
491 extern bool_t xdr_uint64_t();
495 extern bool_t xdr_u_char();
496 extern bool_t xdr_vector();
497 extern bool_t xdr_float();
498 extern bool_t xdr_double();
499 extern bool_t xdr_quadruple();
500 #endif /* !_KERNEL */
504 * Common opaque bytes objects used by many rpc protocols;
505 * declared here due to commonality.
507 #define MAX_NETOBJ_SZ 1024
512 typedef struct netobj netobj;
515 extern bool_t xdr_netobj(XDR *, netobj *);
517 extern bool_t xdr_netobj();
521 * These are XDR control operators
524 #define XDR_GET_BYTES_AVAIL 1
526 struct xdr_bytesrec {
527 bool_t xc_is_last_record;
531 typedef struct xdr_bytesrec xdr_bytesrec;
534 * These are the request arguments to XDR_CONTROL.
536 * XDR_PEEK - returns the contents of the next XDR unit on the XDR stream.
537 * XDR_SKIPBYTES - skips the next N bytes in the XDR stream.
538 * XDR_RDMAGET - for xdr implementation over RDMA, gets private flags from
539 * the XDR stream being moved over RDMA
540 * XDR_RDMANOCHUNK - for xdr implementaion over RDMA, sets private flags in
541 * the XDR stream moving over RDMA.
545 #define XDR_SKIPBYTES 3
546 #define XDR_RDMAGET 4
547 #define XDR_RDMASET 5
551 * These are the public routines for the various implementations of
556 extern void xdrmem_create(XDR *, const caddr_t, const uint_t, const enum
558 /* XDR using memory buffers */
559 extern void xdrrec_create(XDR *, const uint_t, const uint_t, const caddr_t,
560 int (*) (void *, caddr_t, int), int (*) (void *, caddr_t, int));
561 /* XDR pseudo records for tcp */
562 extern bool_t xdrrec_endofrecord(XDR *, bool_t);
563 /* make end of xdr record */
564 extern bool_t xdrrec_skiprecord(XDR *);
565 /* move to beginning of next record */
566 extern bool_t xdrrec_eof(XDR *);
567 extern uint_t xdrrec_readbytes(XDR *, caddr_t, uint_t);
568 /* true if no more input */
570 extern void xdrmem_create();
571 extern void xdrstdio_create();
572 extern void xdrrec_create();
573 extern bool_t xdrrec_endofrecord();
574 extern bool_t xdrrec_skiprecord();
575 extern bool_t xdrrec_eof();
576 extern uint_t xdrrec_readbytes();
580 extern void xdrmem_create(XDR *, caddr_t, uint_t, enum xdr_op);
582 extern struct xdr_ops xdrmblk_ops;
585 extern bool_t xdr_callmsg(XDR *, struct rpc_msg *);
586 extern bool_t xdr_replymsg_body(XDR *, struct rpc_msg *);
587 extern bool_t xdr_replymsg_hdr(XDR *, struct rpc_msg *);
589 #include <sys/malloc.h>
592 #define mem_alloc(size) malloc((size), M_TEMP, M_WAITOK | M_ZERO)
596 #define mem_free(ptr, size) free((ptr), M_TEMP)
599 #endif /* !_KERNEL */
605 #endif /* !_RPC_XDR_H */