rx-race-condition-cleanup-by-adding-busy-status-20010605
[openafs.git] / src / rx / xdr.h
1 /*
2  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3  * unrestricted use provided that this legend is included on all tape
4  * media and as a part of the software program in whole or part.  Users
5  * may copy or modify Sun RPC without charge, but are not authorized
6  * to license or distribute it to anyone else except as part of a product or
7  * program developed by the user.
8  * 
9  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12  * 
13  * Sun RPC is provided with no support and without any obligation on the
14  * part of Sun Microsystems, Inc. to assist in its use, correction,
15  * modification or enhancement.
16  * 
17  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19  * OR ANY PART THEREOF.
20  * 
21  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22  * or profits or other special, indirect and consequential damages, even if
23  * Sun has been advised of the possibility of such damages.
24  * 
25  * Sun Microsystems, Inc.
26  * 2550 Garcia Avenue
27  * Mountain View, California  94043
28  */
29
30 /* This file has the contents of Sun's orginal types.h file added. */
31
32 /*      @(#)types.h 1.1 86/02/03 SMI      */
33
34 /*
35  * Rpc additions to <sys/types.h>
36  */
37
38 #ifndef __XDR_INCLUDE__
39 #define __XDR_INCLUDE__
40 #include <afs/param.h>
41 #ifdef AFS_NT40_ENV
42 #ifndef _MFC_VER
43 #include <winsock2.h>
44 #endif /* _MFC_VER */
45 #endif
46 #define bool_t  int
47 #define enum_t  int
48 #ifndef FALSE
49 #define FALSE   0
50 #endif /* !FALSE */
51 #ifndef TRUE
52 #define TRUE    1
53 #endif /* !TRUE */
54 #define __dontcare__    -1
55
56 #ifndef mem_alloc
57 #define mem_alloc(bsize)        malloc(bsize)
58 #endif
59
60 #ifndef mem_free
61 #define mem_free(ptr, bsize)    free(ptr)
62 #endif
63
64 #ifdef  KERNEL
65 void *afs_osi_Alloc();
66 #define osi_alloc               afs_osi_Alloc
67 #define osi_free                afs_osi_Free
68 #endif
69 #ifndef major           /* ouch! */
70 #include <sys/types.h>
71 #endif
72
73 /*      @(#)xdr.h 1.1 86/02/03 SMI      */
74
75 /*
76  * xdr.h, External Data Representation Serialization Routines.
77  *
78  * Copyright (C) 1984, Sun Microsystems, Inc.
79  */
80
81 /*
82  * XDR provides a conventional way for converting between C data
83  * types and an external bit-string representation.  Library supplied
84  * routines provide for the conversion on built-in C data types.  These
85  * routines and utility routines defined here are used to help implement
86  * a type encode/decode routine for each user-defined type.
87  *
88  * Each data type provides a single procedure which takes two arguments:
89  *
90  *      bool_t
91  *      xdrproc(xdrs, argresp)
92  *              XDR *xdrs;
93  *              <type> *argresp;
94  *
95  * xdrs is an instance of a XDR handle, to which or from which the data
96  * type is to be converted.  argresp is a pointer to the structure to be
97  * converted.  The XDR handle contains an operation field which indicates
98  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
99  *
100  * XDR_DECODE may allocate space if the pointer argresp is null.  This
101  * data can be freed with the XDR_FREE operation.
102  *
103  * We write only one procedure per data type to make it easy
104  * to keep the encode and decode procedures for a data type consistent.
105  * In many cases the same code performs all operations on a user defined type,
106  * because all the hard work is done in the component type routines.
107  * decode as a series of calls on the nested data types.
108  */
109
110 /*
111  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
112  * stream.  XDR_DECODE causes the type to be extracted from the stream.
113  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
114  * request.
115  */
116 enum xdr_op {
117         XDR_ENCODE=0,
118         XDR_DECODE=1,
119         XDR_FREE=2
120 };
121
122 /*
123  * This is the number of bytes per unit of external data.
124  */
125 #define BYTES_PER_XDR_UNIT      (4)
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  * bool_t       (*xdrproc_t)(XDR *, caddr_t *);
135  */
136 typedef bool_t (*xdrproc_t)();
137
138 /*
139  * The XDR handle.
140  * Contains operation which is being applied to the stream,
141  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
142  * and two private fields for the use of the particular impelementation.
143  */
144 typedef struct {
145         enum xdr_op     x_op;           /* operation; fast additional param */
146         struct xdr_ops {
147 #if defined(AFS_SGI61_ENV) && defined(KERNEL) && (_MIPS_SZLONG != _MIPS_SZINT)
148 /* NOTE: SGI 6.1 adds two routines to the xdr_ops if the size of a long is
149  * 64 bits. I've only done this for the kernel, since other changes may
150  * be necessary if we make a 64 bit user version of AFS.
151  */
152                 bool_t  (*x_getint64)(); /* get 32 bits into a long */
153                 bool_t  (*x_putint64)(); /* send 32 bits of a long */
154 #endif /* AFS_SGI61_ENV */
155 #if !(defined(KERNEL) && defined(AFS_SUN57_ENV))
156                 bool_t  (*x_getint32)();        /* get an afs_int32 from underlying stream */
157                 bool_t  (*x_putint32)();        /* put an afs_int32 to " */
158 #endif
159                 bool_t  (*x_getbytes)();/* get some bytes from " */
160                 bool_t  (*x_putbytes)();/* put some bytes to " */
161                 u_int   (*x_getpostn)();/* returns bytes off from beginning */
162                 bool_t  (*x_setpostn)();/* lets you reposition the stream */
163                 afs_int32 *     (*x_inline)();  /* buf quick ptr to buffered data */
164                 void    (*x_destroy)(); /* free privates of this xdr_stream */
165 #if defined(KERNEL) && defined(AFS_SUN57_ENV)
166                 bool_t  (*x_control)();
167                 bool_t  (*x_getint32)();
168                 bool_t  (*x_putint32)();
169 #endif
170         } *x_ops;
171         caddr_t         x_public;       /* users' data */
172         caddr_t         x_private;      /* pointer to private data */
173         caddr_t         x_base;         /* private used for position info */
174         int             x_handy;        /* extra private word */
175 } XDR;
176
177 /*
178  * Operations defined on a XDR handle
179  *
180  * XDR          *xdrs;
181  * afs_int32            *int32p;
182  * caddr_t       addr;
183  * u_int         len;
184  * u_int         pos;
185  */
186 #if defined(AFS_SGI61_ENV) && defined(KERNEL) && (_MIPS_SZLONG != _MIPS_SZINT)
187 #define XDR_GETINT64(xdrs, int64p)                      \
188         (*(xdrs)->x_ops->x_getint64)(xdrs, int64p)
189 #define xdr_getint64(xdrs, int64p)                      \
190         (*(xdrs)->x_ops->x_getint64)(xdrs, int64p)
191
192 #define XDR_PUTINT64(xdrs, int64p)                      \
193         (*(xdrs)->x_ops->x_putint64)(xdrs, int64p)
194 #define xdr_putint64(xdrs, int64p)                      \
195         (*(xdrs)->x_ops->x_putint64)(xdrs, int64p)
196 #endif /* defined(AFS_SGI61_ENV) && KERNEL && (_MIPS_SZLONG != _MIPS_SZINT) */
197
198 #define XDR_GETINT32(xdrs, int32p)                      \
199         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
200 #define xdr_getint32(xdrs, int32p)                      \
201         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
202
203 #define XDR_PUTINT32(xdrs, int32p)                      \
204         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
205 #define xdr_putint32(xdrs, int32p)                      \
206         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
207
208 #define XDR_GETBYTES(xdrs, addr, len)                   \
209         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
210 #define xdr_getbytes(xdrs, addr, len)                   \
211         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
212
213 #define XDR_PUTBYTES(xdrs, addr, len)                   \
214         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
215 #define xdr_putbytes(xdrs, addr, len)                   \
216         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
217
218 #define XDR_GETPOS(xdrs)                                \
219         (*(xdrs)->x_ops->x_getpostn)(xdrs)
220 #define xdr_getpos(xdrs)                                \
221         (*(xdrs)->x_ops->x_getpostn)(xdrs)
222
223 #define XDR_SETPOS(xdrs, pos)                           \
224         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
225 #define xdr_setpos(xdrs, pos)                           \
226         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
227
228 #define XDR_INLINE(xdrs, len)                           \
229         (*(xdrs)->x_ops->x_inline)(xdrs, len)
230 #define xdr_inline(xdrs, len)                           \
231         (*(xdrs)->x_ops->x_inline)(xdrs, len)
232
233 #define XDR_DESTROY(xdrs)                               \
234         if ((xdrs)->x_ops->x_destroy)                   \
235                 (*(xdrs)->x_ops->x_destroy)(xdrs)
236 #define xdr_destroy(xdrs)                               \
237         if ((xdrs)->x_ops->x_destroy)                   \
238                 (*(xdrs)->x_ops->x_destroy)(xdrs)
239
240 /*
241  * Support struct for discriminated unions.
242  * You create an array of xdrdiscrim structures, terminated with
243  * a entry with a null procedure pointer.  The xdr_union routine gets
244  * the discriminant value and then searches the array of structures
245  * for a matching value.  If a match is found the associated xdr routine
246  * is called to handle that part of the union.  If there is
247  * no match, then a default routine may be called.
248  * If there is no match and no default routine it is an error.
249  */
250 #define NULL_xdrproc_t ((xdrproc_t)0)
251 struct xdr_discrim {
252         int     value;
253         xdrproc_t proc;
254 };
255
256 /*
257  * In-line routines for fast encode/decode of primitve data types.
258  * Caveat emptor: these use single memory cycles to get the
259  * data from the underlying buffer, and will fail to operate
260  * properly if the data is not aligned.  The standard way to use these
261  * is to say:
262  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
263  *              return (FALSE);
264  *      <<< macro calls >>>
265  * where ``count'' is the number of bytes of data occupied
266  * by the primitive data types.
267  *
268  * N.B. and frozen for all time: each data type here uses 4 bytes
269  * of external representation.
270  */
271 #define IXDR_GET_INT32(buf)             ntohl(*buf++)
272 #define IXDR_PUT_INT32(buf, v)          (*buf++ = htonl(v))
273
274 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_INT32(buf))
275 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_INT32(buf))
276 #define IXDR_GET_U_INT32(buf)           ((afs_uint32)IXDR_GET_INT32(buf))
277 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_INT32(buf))
278 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_INT32(buf))
279
280 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_INT32((buf), ((afs_int32)(v)))
281 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_INT32((buf), ((afs_int32)(v)))
282 #define IXDR_PUT_U_INT32(buf, v)        IXDR_PUT_INT32((buf), ((afs_int32)(v)))
283 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_INT32((buf), ((afs_int32)(v)))
284 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_INT32((buf), ((afs_int32)(v)))
285
286 /*
287  * These are the "generic" xdr routines.
288  */
289 extern bool_t   xdr_void();
290 extern bool_t   xdr_int();
291 extern bool_t   xdr_u_int();
292 extern bool_t   xdr_short();
293 extern bool_t   xdr_u_short();
294 extern bool_t   xdr_long();
295 extern bool_t   xdr_char();
296 extern bool_t   xdr_u_char();
297 extern bool_t   xdr_bool();
298 extern bool_t   xdr_enum();
299 extern bool_t   xdr_array();
300 extern bool_t   xdr_bytes();
301 extern bool_t   xdr_opaque();
302 extern bool_t   xdr_string();
303 extern bool_t   xdr_union();
304 extern bool_t   xdr_float();
305 extern bool_t   xdr_double();
306 extern bool_t   xdr_reference();
307 extern bool_t   xdr_wrapstring();
308 extern bool_t   xdr_vector();
309 extern bool_t   xdr_int64();
310 extern bool_t   xdr_uint64();
311
312 /*
313  * These are the public routines for the various implementations of
314  * xdr streams.
315  */
316 extern void   xdrmem_create();          /* XDR using memory buffers */
317 extern void   xdrrx_create();           /* XDR using rx */
318 extern void   xdrstdio_create();        /* XDR using stdio library */
319 extern void   xdrrec_create();          /* XDR pseudo records for tcp */
320 extern bool_t xdrrec_endofrecord();     /* make end of xdr record */
321 extern bool_t xdrrec_skiprecord();      /* move to begining of next record */
322 extern bool_t xdrrec_eof();             /* true iff no more input */
323
324 /*
325  * If you change the definitions of xdr_afs_int32 and xdr_afs_uint32, be sure
326  * to change them in BOTH rx/xdr.h and rxgen/rpc_main.c.  Also, config/stds.h
327  * has the defines for afs_int32 which should match below.
328  */
329
330 #ifndef xdr_afs_int32
331 #define xdr_afs_int32 xdr_int
332 #endif
333 #ifndef xdr_afs_uint32
334 #define xdr_afs_uint32 xdr_u_int
335 #endif
336 #ifndef xdr_afs_int64
337 #define xdr_afs_int64 xdr_int64
338 #endif
339 #ifndef xdr_afs_uint64
340 #define xdr_afs_uint64 xdr_uint64
341 #endif
342
343 #endif /* __XDR_INCLUDE__ */