Bring in a transport-independent RPC (TI-RPC).
[dragonfly.git] / sys / netproto / atm / spans / spans_kxdr.h
1 /*      $NetBSD: types.h,v 1.13 2000/06/13 01:02:44 thorpej Exp $       */
2 /*      $NetBSD: xdr.h,v 1.19 2000/07/17 05:00:45 matt 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  * 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  *      from: @(#)types.h 1.18 87/07/24 SMI
30  *      from: @(#)types.h       2.3 88/08/15 4.0 RPCSRC
31  *      from: @(#)xdr.h 1.19 87/04/22 SMI
32  *      from: @(#)xdr.h 2.2 88/07/29 4.0 RPCSRC
33  * $FreeBSD: src/sys/netatm/spans/spans_kxdr.h,v 1.3 2005/01/07 01:45:38 imp Exp $
34  */
35
36 #ifndef _SPANS_KXDR_H
37 #define _SPANS_KXDR_H
38
39 /*
40  * Rpc additions to <sys/types.h>
41  */
42 #include <sys/types.h>
43
44 typedef int32_t bool_t;
45 typedef int32_t enum_t;
46
47 typedef u_int32_t rpcprog_t;
48 typedef u_int32_t rpcvers_t;
49 typedef u_int32_t rpcproc_t;
50 typedef u_int32_t rpcprot_t;
51 typedef u_int32_t rpcport_t;
52 typedef   int32_t rpc_inline_t;
53
54 #define __dontcare__    -1
55
56 #ifndef FALSE
57 #       define FALSE    (0)
58 #endif
59 #ifndef TRUE
60 #       define TRUE     (1)
61 #endif
62 #ifndef NULL
63 #       define NULL     0
64 #endif
65
66 /*
67  * xdr.h, External Data Representation Serialization Routines.
68  *
69  * Copyright (C) 1984, Sun Microsystems, Inc.
70  */
71
72 /*
73  * XDR provides a conventional way for converting between C data
74  * types and an external bit-string representation.  Library supplied
75  * routines provide for the conversion on built-in C data types.  These
76  * routines and utility routines defined here are used to help implement
77  * a type encode/decode routine for each user-defined type.
78  *
79  * Each data type provides a single procedure which takes two arguments:
80  *
81  *      bool_t
82  *      xdrproc(xdrs, argresp)
83  *              XDR *xdrs;
84  *              <type> *argresp;
85  *
86  * xdrs is an instance of a XDR handle, to which or from which the data
87  * type is to be converted.  argresp is a pointer to the structure to be
88  * converted.  The XDR handle contains an operation field which indicates
89  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
90  *
91  * XDR_DECODE may allocate space if the pointer argresp is null.  This
92  * data can be freed with the XDR_FREE operation.
93  *
94  * We write only one procedure per data type to make it easy
95  * to keep the encode and decode procedures for a data type consistent.
96  * In many cases the same code performs all operations on a user defined type,
97  * because all the hard work is done in the component type routines.
98  * decode as a series of calls on the nested data types.
99  */
100
101 /*
102  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
103  * stream.  XDR_DECODE causes the type to be extracted from the stream.
104  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
105  * request.
106  */
107 enum xdr_op {
108         XDR_ENCODE=0,
109         XDR_DECODE=1,
110         XDR_FREE=2
111 };
112
113 /*
114  * This is the number of bytes per unit of external data.
115  */
116 #define BYTES_PER_XDR_UNIT      (4)
117 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
118                     * BYTES_PER_XDR_UNIT)
119
120 /*
121  * The XDR handle.
122  * Contains operation which is being applied to the stream,
123  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
124  * and two private fields for the use of the particular implementation.
125  */
126 typedef struct __rpc_xdr {
127         enum xdr_op     x_op;           /* operation; fast additional param */
128         const struct xdr_ops {
129                 /* get a long from underlying stream */
130                 bool_t  (*x_getlong)(struct __rpc_xdr *, long *);
131                 /* put a long to " */
132                 bool_t  (*x_putlong)(struct __rpc_xdr *, const long *);
133                 /* get some bytes from " */
134                 bool_t  (*x_getbytes)(struct __rpc_xdr *, char *, u_int);
135                 /* put some bytes to " */
136                 bool_t  (*x_putbytes)(struct __rpc_xdr *, const char *, u_int);
137                 /* returns bytes off from beginning */
138                 u_int   (*x_getpostn)(struct __rpc_xdr *);
139                 /* lets you reposition the stream */
140                 bool_t  (*x_setpostn)(struct __rpc_xdr *, u_int);
141                 /* buf quick ptr to buffered data */
142                 int32_t *(*x_inline)(struct __rpc_xdr *, u_int);
143                 /* free privates of this xdr_stream */
144                 void    (*x_destroy)(struct __rpc_xdr *);
145                 bool_t  (*x_control)(struct __rpc_xdr *, int, void *);
146         } *x_ops;
147         char *          x_public;       /* users' data */
148         void *          x_private;      /* pointer to private data */
149         char *          x_base;         /* private used for position info */
150         int             x_handy;        /* extra private word */
151 } XDR;
152
153 /*
154  * A xdrproc_t exists for each data type which is to be encoded or decoded.
155  *
156  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
157  * The opaque pointer generally points to a structure of the data type
158  * to be decoded.  If this pointer is 0, then the type routines should
159  * allocate dynamic storage of the appropriate size and return it.
160  */
161 typedef bool_t (*xdrproc_t)(XDR *, void *, u_int);
162
163 /*
164  * Operations defined on a XDR handle
165  *
166  * XDR          *xdrs;
167  * long         *longp;
168  * char *        addr;
169  * u_int         len;
170  * u_int         pos;
171  */
172 #define XDR_GETLONG(xdrs, longp)                        \
173         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
174 #define xdr_getlong(xdrs, longp)                        \
175         (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
176
177 #define XDR_PUTLONG(xdrs, longp)                        \
178         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
179 #define xdr_putlong(xdrs, longp)                        \
180         (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
181
182 static __inline int
183 xdr_getint32(XDR *xdrs, int32_t *ip)
184 {
185         long l;
186
187         if (!xdr_getlong(xdrs, &l))
188                 return (FALSE);
189         *ip = (int32_t)l;
190         return (TRUE);
191 }
192
193 static __inline int
194 xdr_putint32(XDR *xdrs, int32_t *ip)
195 {
196         long l;
197
198         l = (long)*ip;
199         return xdr_putlong(xdrs, &l);
200 }
201
202 #define XDR_GETINT32(xdrs, int32p)      xdr_getint32(xdrs, int32p)
203 #define XDR_PUTINT32(xdrs, int32p)      xdr_putint32(xdrs, int32p)
204
205 #define XDR_GETBYTES(xdrs, addr, len)                   \
206         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
207 #define xdr_getbytes(xdrs, addr, len)                   \
208         (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
209
210 #define XDR_PUTBYTES(xdrs, addr, len)                   \
211         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
212 #define xdr_putbytes(xdrs, addr, len)                   \
213         (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
214
215 #define XDR_GETPOS(xdrs)                                \
216         (*(xdrs)->x_ops->x_getpostn)(xdrs)
217 #define xdr_getpos(xdrs)                                \
218         (*(xdrs)->x_ops->x_getpostn)(xdrs)
219
220 #define XDR_SETPOS(xdrs, pos)                           \
221         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
222 #define xdr_setpos(xdrs, pos)                           \
223         (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
224
225 #define XDR_INLINE(xdrs, len)                           \
226         (*(xdrs)->x_ops->x_inline)(xdrs, len)
227 #define xdr_inline(xdrs, len)                           \
228         (*(xdrs)->x_ops->x_inline)(xdrs, len)
229
230 #define XDR_DESTROY(xdrs)                               \
231         if ((xdrs)->x_ops->x_destroy)                   \
232                 (*(xdrs)->x_ops->x_destroy)(xdrs)
233 #define xdr_destroy(xdrs)                               \
234         if ((xdrs)->x_ops->x_destroy)                   \
235                 (*(xdrs)->x_ops->x_destroy)(xdrs)
236
237 #define XDR_CONTROL(xdrs, req, op)                      \
238         if ((xdrs)->x_ops->x_control)                   \
239                 (*(xdrs)->x_ops->x_control)(xdrs, req, op)
240 #define xdr_control(xdrs, req, op) XDR_CONTROL(xdrs, req, op)
241
242 /*
243  * Solaris strips the '_t' from these types -- not sure why.
244  * But, let's be compatible.
245  */
246 #define xdr_rpcvers(xdrs, versp) xdr_u_int32(xdrs, versp)
247 #define xdr_rpcprog(xdrs, progp) xdr_u_int32(xdrs, progp)
248 #define xdr_rpcproc(xdrs, procp) xdr_u_int32(xdrs, procp)
249 #define xdr_rpcprot(xdrs, protp) xdr_u_int32(xdrs, protp)
250 #define xdr_rpcport(xdrs, portp) xdr_u_int32(xdrs, portp)
251
252 /*
253  * Support struct for discriminated unions.
254  * You create an array of xdrdiscrim structures, terminated with
255  * an entry with a null procedure pointer.  The xdr_union routine gets
256  * the discriminant value and then searches the array of structures
257  * for a matching value.  If a match is found the associated xdr routine
258  * is called to handle that part of the union.  If there is
259  * no match, then a default routine may be called.
260  * If there is no match and no default routine it is an error.
261  */
262 #define NULL_xdrproc_t ((xdrproc_t)0)
263 struct xdr_discrim {
264         int     value;
265         xdrproc_t proc;
266 };
267
268 /*
269  * In-line routines for fast encode/decode of primitive data types.
270  * Caveat emptor: these use single memory cycles to get the
271  * data from the underlying buffer, and will fail to operate
272  * properly if the data is not aligned.  The standard way to use these
273  * is to say:
274  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
275  *              return (FALSE);
276  *      <<< macro calls >>>
277  * where ``count'' is the number of bytes of data occupied
278  * by the primitive data types.
279  *
280  * N.B. and frozen for all time: each data type here uses 4 bytes
281  * of external representation.
282  */
283 #define IXDR_GET_INT32(buf)             ((int32_t)__ntohl((u_int32_t)*(buf)++))
284 #define IXDR_PUT_INT32(buf, v)          (*(buf)++ =(int32_t)__htonl((u_int32_t)v))
285 #define IXDR_GET_U_INT32(buf)           ((u_int32_t)IXDR_GET_INT32(buf))
286 #define IXDR_PUT_U_INT32(buf, v)        IXDR_PUT_INT32((buf), ((int32_t)(v)))
287
288 #define IXDR_GET_LONG(buf)              ((long)__ntohl((u_int32_t)*(buf)++))
289 #define IXDR_PUT_LONG(buf, v)           (*(buf)++ =(int32_t)__htonl((u_int32_t)v))
290
291 #define IXDR_GET_BOOL(buf)              ((bool_t)IXDR_GET_LONG(buf))
292 #define IXDR_GET_ENUM(buf, t)           ((t)IXDR_GET_LONG(buf))
293 #define IXDR_GET_U_LONG(buf)            ((u_long)IXDR_GET_LONG(buf))
294 #define IXDR_GET_SHORT(buf)             ((short)IXDR_GET_LONG(buf))
295 #define IXDR_GET_U_SHORT(buf)           ((u_short)IXDR_GET_LONG(buf))
296
297 #define IXDR_PUT_BOOL(buf, v)           IXDR_PUT_LONG((buf), (v))
298 #define IXDR_PUT_ENUM(buf, v)           IXDR_PUT_LONG((buf), (v))
299 #define IXDR_PUT_U_LONG(buf, v)         IXDR_PUT_LONG((buf), (v))
300 #define IXDR_PUT_SHORT(buf, v)          IXDR_PUT_LONG((buf), (v))
301 #define IXDR_PUT_U_SHORT(buf, v)        IXDR_PUT_LONG((buf), (v))
302
303 /*
304  * These are the "generic" xdr routines.
305  */
306 __BEGIN_DECLS
307 extern bool_t   xdr_void(void);
308 extern bool_t   xdr_int(XDR *, int *);
309 extern bool_t   xdr_u_int(XDR *, u_int *);
310 extern bool_t   xdr_long(XDR *, long *);
311 extern bool_t   xdr_u_long(XDR *, u_long *);
312 extern bool_t   xdr_short(XDR *, short *);
313 extern bool_t   xdr_u_short(XDR *, u_short *);
314 extern bool_t   xdr_int16_t(XDR *, int16_t *);
315 extern bool_t   xdr_u_int16_t(XDR *, u_int16_t *);
316 extern bool_t   xdr_int32_t(XDR *, int32_t *);
317 extern bool_t   xdr_u_int32_t(XDR *, u_int32_t *);
318 extern bool_t   xdr_int64_t(XDR *, int64_t *);
319 extern bool_t   xdr_u_int64_t(XDR *, u_int64_t *);
320 extern bool_t   xdr_bool(XDR *, bool_t *);
321 extern bool_t   xdr_enum(XDR *, enum_t *);
322 extern bool_t   xdr_array(XDR *, char **, u_int *, u_int, u_int, xdrproc_t);
323 extern bool_t   xdr_bytes(XDR *, char **, u_int *, u_int);
324 extern bool_t   xdr_opaque(XDR *, char *, u_int);
325 extern bool_t   xdr_string(XDR *, char **, u_int);
326 extern bool_t   xdr_union(XDR *, enum_t *, char *, const struct xdr_discrim *, xdrproc_t);
327 extern bool_t   xdr_char(XDR *, char *);
328 extern bool_t   xdr_u_char(XDR *, u_char *);
329 extern bool_t   xdr_vector(XDR *, char *, u_int, u_int, xdrproc_t);
330 extern bool_t   xdr_float(XDR *, float *);
331 extern bool_t   xdr_double(XDR *, double *);
332 extern bool_t   xdr_quadruple(XDR *, long double *);
333 extern bool_t   xdr_reference(XDR *, char **, u_int, xdrproc_t);
334 extern bool_t   xdr_pointer(XDR *, char **, u_int, xdrproc_t);
335 extern bool_t   xdr_wrapstring(XDR *, char **);
336 extern void     xdr_free(xdrproc_t, void *);
337 extern bool_t   xdr_hyper(XDR *, quad_t *);
338 extern bool_t   xdr_u_hyper(XDR *, u_quad_t *);
339 extern bool_t   xdr_longlong_t(XDR *, quad_t *);
340 extern bool_t   xdr_u_longlong_t(XDR *, u_quad_t *);
341 __END_DECLS
342
343 /*
344  * Common opaque bytes objects used by many rpc protocols;
345  * declared here due to commonality.
346  */
347 #define MAX_NETOBJ_SZ 1024
348 struct netobj {
349         u_int   n_len;
350         char    *n_bytes;
351 };
352 typedef struct netobj netobj;
353 extern bool_t   xdr_netobj(XDR *, struct netobj *);
354
355 /*
356  * These are the public routines for the various implementations of
357  * xdr streams.
358  */
359 __BEGIN_DECLS
360 /* XDR using memory buffers */
361 extern void   xdrmem_create(XDR *, char *, u_int, enum xdr_op);
362
363 /* XDR pseudo records for tcp */
364 extern void   xdrrec_create(XDR *, u_int, u_int, void *,
365                             int (*)(void *, void *, int),
366                             int (*)(void *, void *, int));
367
368 /* make end of xdr record */
369 extern bool_t xdrrec_endofrecord(XDR *, int);
370
371 /* move to beginning of next record */
372 extern bool_t xdrrec_skiprecord(XDR *);
373
374 /* true if no more input */
375 extern bool_t xdrrec_eof(XDR *);
376 extern u_int xdrrec_readbytes(XDR *, caddr_t, u_int);
377 __END_DECLS
378
379 #endif /* !_SPANS_KXDR_H */