]> git.decadent.org.uk Git - nfs-utils.git/blob - support/rpc/include/rpc/xdr.h
Add gss support from citi @ umich
[nfs-utils.git] / support / rpc / include / rpc / xdr.h
1 /*      $OpenBSD: xdr.h,v 1.2 1997/09/21 10:46:18 niklas Exp $  */
2 /*      $NetBSD: xdr.h,v 1.7 1995/04/29 05:28:06 cgd Exp $      */
3
4 /*
5  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
6  * unrestricted use provided that this legend is included on all tape
7  * media and as a part of the software program in whole or part.  Users
8  * may copy or modify Sun RPC without charge, but are not authorized
9  * to license or distribute it to anyone else except as part of a product or
10  * program developed by the user.
11  *
12  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
13  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
14  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15  *
16  * Sun RPC is provided with no support and without any obligation on the
17  * part of Sun Microsystems, Inc. to assist in its use, correction,
18  * modification or enhancement.
19  *
20  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
21  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
22  * OR ANY PART THEREOF.
23  *
24  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
25  * or profits or other special, indirect and consequential damages, even if
26  * Sun has been advised of the possibility of such damages.
27  *
28  * Sun Microsystems, Inc.
29  * 2550 Garcia Avenue
30  * Mountain View, California  94043
31  *
32  *      from: @(#)xdr.h 1.19 87/04/22 SMI
33  *      @(#)xdr.h       2.2 88/07/29 4.0 RPCSRC
34  */
35
36 /*
37  * xdr.h, External Data Representation Serialization Routines.
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  */
41
42 #ifndef _RPC_XDR_H
43 #define _RPC_XDR_H
44 #include <sys/cdefs.h>
45 #include <stdio.h>
46
47 /*
48  * XDR provides a conventional way for converting between C data
49  * types and an external bit-string representation.  Library supplied
50  * routines provide for the conversion on built-in C data types.  These
51  * routines and utility routines defined here are used to help implement
52  * a type encode/decode routine for each user-defined type.
53  *
54  * Each data type provides a single procedure which takes two arguments:
55  *
56  *      bool_t
57  *      xdrproc(xdrs, argresp)
58  *              XDR *xdrs;
59  *              <type> *argresp;
60  *
61  * xdrs is an instance of a XDR handle, to which or from which the data
62  * type is to be converted.  argresp is a pointer to the structure to be
63  * converted.  The XDR handle contains an operation field which indicates
64  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
65  *
66  * XDR_DECODE may allocate space if the pointer argresp is null.  This
67  * data can be freed with the XDR_FREE operation.
68  *
69  * We write only one procedure per data type to make it easy
70  * to keep the encode and decode procedures for a data type consistent.
71  * In many cases the same code performs all operations on a user defined type,
72  * because all the hard work is done in the component type routines.
73  * decode as a series of calls on the nested data types.
74  */
75
76 /*
77  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
78  * stream.  XDR_DECODE causes the type to be extracted from the stream.
79  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
80  * request.
81  */
82 enum xdr_op {
83         XDR_ENCODE=0,
84         XDR_DECODE=1,
85         XDR_FREE=2
86 };
87
88 /*
89  * This is the number of bytes per unit of external data.
90  */
91 #define BYTES_PER_XDR_UNIT      (4)
92 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
93                     * BYTES_PER_XDR_UNIT)
94
95 /*
96  * The XDR handle.
97  * Contains operation which is being applied to the stream,
98  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
99  * and two private fields for the use of the particular impelementation.
100  */
101 typedef struct __rpc_xdr {
102         enum xdr_op     x_op;           /* operation; fast additional param */
103         struct xdr_ops {
104                 /* get a long from underlying stream */
105                 bool_t  (*x_getlong) __P((struct __rpc_xdr *, long *));
106                 /* put a long to " */
107                 bool_t  (*x_putlong) __P((struct __rpc_xdr *, long *));
108                 /* get some bytes from " */
109                 bool_t  (*x_getbytes) __P((struct __rpc_xdr *, caddr_t, u_int));
110                 /* put some bytes to " */
111                 bool_t  (*x_putbytes) __P((struct __rpc_xdr *, caddr_t, u_int));
112                 /* returns bytes off from beginning */
113                 u_int   (*x_getpostn) __P((struct __rpc_xdr *));
114                 /* lets you reposition the stream */
115                 bool_t  (*x_setpostn) __P((struct __rpc_xdr *, u_int));
116                 /* buf quick ptr to buffered data */
117                 int32_t *(*x_inline) __P((struct __rpc_xdr *, u_int));
118                 /* free privates of this xdr_stream */
119                 void    (*x_destroy) __P((struct __rpc_xdr *));
120         } *x_ops;
121         caddr_t         x_public;       /* users' data */
122         caddr_t         x_private;      /* pointer to private data */
123         caddr_t         x_base;         /* private used for position info */
124         int             x_handy;        /* extra private word */
125 } XDR;
126
127 /*
128  * A xdrproc_t exists for each data type which is to be encoded or decoded.
129  *
130  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
131  * The opaque pointer generally points to a structure of the data type
132  * to be decoded.  If this pointer is 0, then the type routines should
133  * allocate dynamic storage of the appropriate size and return it.
134  *
135  * XXX can't actually prototype it, because some take three args!!!
136  */
137 typedef bool_t (*xdrproc_t) __P((/* XDR *, void *, u_int */));
138
139 /*
140  * Operations defined on a XDR handle
141  *
142  * XDR          *xdrs;
143  * long         *longp;
144  * caddr_t       addr;
145  * u_int         len;
146  * u_int         pos;
147  */
148 #define XDR_GETLONG(xdrs, longp)                        \
149         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
150 #define xdr_getlong(xdrs, longp)                        \
151         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
152
153 #define XDR_PUTLONG(xdrs, longp)                        \
154         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
155 #define xdr_putlong(xdrs, longp)                        \
156         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
157
158 #define XDR_GETBYTES(xdrs, addr, len)                   \
159         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
160 #define xdr_getbytes(xdrs, addr, len)                   \
161         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
162
163 #define XDR_PUTBYTES(xdrs, addr, len)                   \
164         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
165 #define xdr_putbytes(xdrs, addr, len)                   \
166         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
167
168 #define XDR_GETPOS(xdrs)                                \
169         (*(xdrs)->x_ops->x_getpostn)(xdrs)
170 #define xdr_getpos(xdrs)                                \
171         (*(xdrs)->x_ops->x_getpostn)(xdrs)
172
173 #define XDR_SETPOS(xdrs, pos)                           \
174         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
175 #define xdr_setpos(xdrs, pos)                           \
176         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
177
178 #define XDR_INLINE(xdrs, len)                           \
179         (*(xdrs)->x_ops->x_inline)(xdrs, len)
180 #define xdr_inline(xdrs, len)                           \
181         (*(xdrs)->x_ops->x_inline)(xdrs, len)
182
183 #define XDR_DESTROY(xdrs)                               \
184         if ((xdrs)->x_ops->x_destroy)                   \
185                 (*(xdrs)->x_ops->x_destroy)(xdrs)
186 #define xdr_destroy(xdrs)                               \
187         if ((xdrs)->x_ops->x_destroy)                   \
188                 (*(xdrs)->x_ops->x_destroy)(xdrs)
189
190 /*
191  * Support struct for discriminated unions.
192  * You create an array of xdrdiscrim structures, terminated with
193  * a entry with a null procedure pointer.  The xdr_union routine gets
194  * the discriminant value and then searches the array of structures
195  * for a matching value.  If a match is found the associated xdr routine
196  * is called to handle that part of the union.  If there is
197  * no match, then a default routine may be called.
198  * If there is no match and no default routine it is an error.
199  */
200 #define NULL_xdrproc_t ((xdrproc_t)0)
201 struct xdr_discrim {
202         int     value;
203         xdrproc_t proc;
204 };
205
206 /*
207  * In-line routines for fast encode/decode of primitve data types.
208  * Caveat emptor: these use single memory cycles to get the
209  * data from the underlying buffer, and will fail to operate
210  * properly if the data is not aligned.  The standard way to use these
211  * is to say:
212  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
213  *              return (FALSE);
214  *      <<< macro calls >>>
215  * where ``count'' is the number of bytes of data occupied
216  * by the primitive data types.
217  *
218  * N.B. and frozen for all time: each data type here uses 4 bytes
219  * of external representation.
220  */
221 #define IXDR_GET_LONG(buf)              ((long)ntohl((u_long)*(buf)++))
222 #define IXDR_PUT_LONG(buf, v)           (*(buf)++ = (long)htonl((u_long)v))
223
224 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_LONG(buf))
225 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_LONG(buf))
226 #define IXDR_GET_U_LONG(buf)            ((u_long)IXDR_GET_LONG(buf))
227 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_LONG(buf))
228 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_LONG(buf))
229
230 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_LONG((buf), ((long)(v)))
231 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_LONG((buf), ((long)(v)))
232 #define IXDR_PUT_U_LONG(buf, v)         IXDR_PUT_LONG((buf), ((long)(v)))
233 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_LONG((buf), ((long)(v)))
234 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
235
236 /*
237  * These are the "generic" xdr routines.
238  */
239 __BEGIN_DECLS
240 extern bool_t   xdr_void        __P((void));
241 extern bool_t   xdr_int         __P((XDR *, int *));
242 extern bool_t   xdr_u_int       __P((XDR *, u_int *));
243 extern bool_t   xdr_long        __P((XDR *, long *));
244 extern bool_t   xdr_u_long      __P((XDR *, u_long *));
245 extern bool_t   xdr_short       __P((XDR *, short *));
246 extern bool_t   xdr_u_short     __P((XDR *, u_short *));
247 extern bool_t   xdr_int16_t     __P((XDR *, int16_t *));
248 extern bool_t   xdr_u_int16_t   __P((XDR *, u_int16_t *));
249 extern bool_t   xdr_int32_t     __P((XDR *, int32_t *));
250 extern bool_t   xdr_u_int32_t   __P((XDR *, u_int32_t *));
251 extern bool_t   xdr_bool        __P((XDR *, bool_t *));
252 extern bool_t   xdr_enum        __P((XDR *, enum_t *));
253 extern bool_t   xdr_array       __P((XDR *, char **, u_int *, u_int, u_int, xdrproc_t));
254 extern bool_t   xdr_bytes       __P((XDR *, char **, u_int *, u_int));
255 extern bool_t   xdr_opaque      __P((XDR *, caddr_t, u_int));
256 extern bool_t   xdr_string      __P((XDR *, char **, u_int));
257 extern bool_t   xdr_union       __P((XDR *, enum_t *, char *, struct xdr_discrim *, xdrproc_t));
258 extern bool_t   xdr_char        __P((XDR *, char *));
259 extern bool_t   xdr_u_char      __P((XDR *, u_char *));
260 extern bool_t   xdr_vector      __P((XDR *, char *, u_int, u_int, xdrproc_t));
261 extern bool_t   xdr_float       __P((XDR *, float *));
262 extern bool_t   xdr_double      __P((XDR *, double *));
263 extern bool_t   xdr_reference   __P((XDR *, caddr_t *, u_int, xdrproc_t));
264 extern bool_t   xdr_pointer     __P((XDR *, caddr_t *, u_int, xdrproc_t));
265 extern bool_t   xdr_wrapstring  __P((XDR *, char **));
266 extern void     xdr_free        __P((xdrproc_t, char *));
267 __END_DECLS
268
269 /*
270  * Common opaque bytes objects used by many rpc protocols;
271  * declared here due to commonality.
272  */
273
274 #define MAX_NETOBJ_SZ 2048
275 struct netobj {
276         u_int   n_len;
277         char    *n_bytes;
278 };
279 typedef struct netobj netobj;
280 extern bool_t   xdr_netobj __P((XDR *, struct netobj *));
281
282 /*
283  * These are the public routines for the various implementations of
284  * xdr streams.
285  */
286 __BEGIN_DECLS
287 /* XDR using memory buffers */
288 extern void   xdrmem_create     __P((XDR *, char *, u_int, enum xdr_op));
289
290 #ifdef _STDIO_H_
291 /* XDR using stdio library */
292 extern void   xdrstdio_create   __P((XDR *, FILE *, enum xdr_op));
293 #endif
294
295 /* XDR pseudo records for tcp */
296 extern void   xdrrec_create     __P((XDR *, u_int, u_int, char *,
297                                     int (*) __P((caddr_t, caddr_t, int)),
298                                     int (*) __P((caddr_t, caddr_t, int))));
299
300 /* make end of xdr record */
301 extern bool_t xdrrec_endofrecord __P((XDR *, int));
302
303 /* move to beginning of next record */
304 extern bool_t xdrrec_skiprecord __P((XDR *));
305
306 /* true if no more input */
307 extern bool_t xdrrec_eof        __P((XDR *));
308 __END_DECLS
309
310 #endif /* !_RPC_XDR_H */