Back to index

glibc  2.9
xdr.h
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 
00030 /*
00031  * xdr.h, External Data Representation Serialization Routines.
00032  *
00033  * Copyright (C) 1984, Sun Microsystems, Inc.
00034  */
00035 
00036 #ifndef _RPC_XDR_H
00037 #define _RPC_XDR_H 1
00038 
00039 #include <features.h>
00040 #include <sys/types.h>
00041 #include <rpc/types.h>
00042 
00043 /* We need FILE.  */
00044 #include <stdio.h>
00045 
00046 __BEGIN_DECLS
00047 
00048 /*
00049  * XDR provides a conventional way for converting between C data
00050  * types and an external bit-string representation.  Library supplied
00051  * routines provide for the conversion on built-in C data types.  These
00052  * routines and utility routines defined here are used to help implement
00053  * a type encode/decode routine for each user-defined type.
00054  *
00055  * Each data type provides a single procedure which takes two arguments:
00056  *
00057  *      bool_t
00058  *      xdrproc(xdrs, argresp)
00059  *              XDR *xdrs;
00060  *              <type> *argresp;
00061  *
00062  * xdrs is an instance of a XDR handle, to which or from which the data
00063  * type is to be converted.  argresp is a pointer to the structure to be
00064  * converted.  The XDR handle contains an operation field which indicates
00065  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
00066  *
00067  * XDR_DECODE may allocate space if the pointer argresp is null.  This
00068  * data can be freed with the XDR_FREE operation.
00069  *
00070  * We write only one procedure per data type to make it easy
00071  * to keep the encode and decode procedures for a data type consistent.
00072  * In many cases the same code performs all operations on a user defined type,
00073  * because all the hard work is done in the component type routines.
00074  * decode as a series of calls on the nested data types.
00075  */
00076 
00077 /*
00078  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
00079  * stream.  XDR_DECODE causes the type to be extracted from the stream.
00080  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
00081  * request.
00082  */
00083 enum xdr_op {
00084   XDR_ENCODE = 0,
00085   XDR_DECODE = 1,
00086   XDR_FREE = 2
00087 };
00088 
00089 /*
00090  * This is the number of bytes per unit of external data.
00091  */
00092 #define BYTES_PER_XDR_UNIT  (4)
00093 /*
00094  * This only works if the above is a power of 2.  But it's defined to be
00095  * 4 by the appropriate RFCs.  So it will work.  And it's normally quicker
00096  * than the old routine.
00097  */
00098 #if 1
00099 #define RNDUP(x)  (((x) + BYTES_PER_XDR_UNIT - 1) & ~(BYTES_PER_XDR_UNIT - 1))
00100 #else /* this is the old routine */
00101 #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
00102                   * BYTES_PER_XDR_UNIT)
00103 #endif
00104 
00105 /*
00106  * The XDR handle.
00107  * Contains operation which is being applied to the stream,
00108  * an operations vector for the particular implementation (e.g. see xdr_mem.c),
00109  * and two private fields for the use of the particular implementation.
00110  */
00111 typedef struct XDR XDR;
00112 struct XDR
00113   {
00114     enum xdr_op x_op;              /* operation; fast additional param */
00115     struct xdr_ops
00116       {
00117        bool_t (*x_getlong) (XDR *__xdrs, long *__lp);
00118        /* get a long from underlying stream */
00119        bool_t (*x_putlong) (XDR *__xdrs, __const long *__lp);
00120        /* put a long to " */
00121        bool_t (*x_getbytes) (XDR *__xdrs, caddr_t __addr, u_int __len);
00122        /* get some bytes from " */
00123        bool_t (*x_putbytes) (XDR *__xdrs, __const char *__addr, u_int __len);
00124        /* put some bytes to " */
00125        u_int (*x_getpostn) (__const XDR *__xdrs);
00126        /* returns bytes off from beginning */
00127        bool_t (*x_setpostn) (XDR *__xdrs, u_int __pos);
00128        /* lets you reposition the stream */
00129        int32_t *(*x_inline) (XDR *__xdrs, u_int __len);
00130        /* buf quick ptr to buffered data */
00131        void (*x_destroy) (XDR *__xdrs);
00132        /* free privates of this xdr_stream */
00133        bool_t (*x_getint32) (XDR *__xdrs, int32_t *__ip);
00134        /* get a int from underlying stream */
00135        bool_t (*x_putint32) (XDR *__xdrs, __const int32_t *__ip);
00136        /* put a int to " */
00137       }
00138      *x_ops;
00139     caddr_t x_public;              /* users' data */
00140     caddr_t x_private;             /* pointer to private data */
00141     caddr_t x_base;         /* private used for position info */
00142     u_int x_handy;          /* extra private word */
00143   };
00144 
00145 /*
00146  * A xdrproc_t exists for each data type which is to be encoded or decoded.
00147  *
00148  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
00149  * The opaque pointer generally points to a structure of the data type
00150  * to be decoded.  If this pointer is 0, then the type routines should
00151  * allocate dynamic storage of the appropriate size and return it.
00152  * bool_t       (*xdrproc_t)(XDR *, caddr_t *);
00153  */
00154 typedef bool_t (*xdrproc_t) (XDR *, void *,...);
00155 
00156 
00157 /*
00158  * Operations defined on a XDR handle
00159  *
00160  * XDR          *xdrs;
00161  * int32_t      *int32p;
00162  * long         *longp;
00163  * caddr_t       addr;
00164  * u_int         len;
00165  * u_int         pos;
00166  */
00167 #define XDR_GETINT32(xdrs, int32p)                      \
00168         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
00169 #define xdr_getint32(xdrs, int32p)                      \
00170         (*(xdrs)->x_ops->x_getint32)(xdrs, int32p)
00171 
00172 #define XDR_PUTINT32(xdrs, int32p)                      \
00173         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
00174 #define xdr_putint32(xdrs, int32p)                      \
00175         (*(xdrs)->x_ops->x_putint32)(xdrs, int32p)
00176 
00177 #define XDR_GETLONG(xdrs, longp)                 \
00178        (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
00179 #define xdr_getlong(xdrs, longp)                 \
00180        (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
00181 
00182 #define XDR_PUTLONG(xdrs, longp)                 \
00183        (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
00184 #define xdr_putlong(xdrs, longp)                 \
00185        (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
00186 
00187 #define XDR_GETBYTES(xdrs, addr, len)                   \
00188        (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
00189 #define xdr_getbytes(xdrs, addr, len)                   \
00190        (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
00191 
00192 #define XDR_PUTBYTES(xdrs, addr, len)                   \
00193        (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
00194 #define xdr_putbytes(xdrs, addr, len)                   \
00195        (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
00196 
00197 #define XDR_GETPOS(xdrs)                         \
00198        (*(xdrs)->x_ops->x_getpostn)(xdrs)
00199 #define xdr_getpos(xdrs)                         \
00200        (*(xdrs)->x_ops->x_getpostn)(xdrs)
00201 
00202 #define XDR_SETPOS(xdrs, pos)                           \
00203        (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
00204 #define xdr_setpos(xdrs, pos)                           \
00205        (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
00206 
00207 #define       XDR_INLINE(xdrs, len)                            \
00208        (*(xdrs)->x_ops->x_inline)(xdrs, len)
00209 #define       xdr_inline(xdrs, len)                            \
00210        (*(xdrs)->x_ops->x_inline)(xdrs, len)
00211 
00212 #define       XDR_DESTROY(xdrs)                                \
00213        do {                                             \
00214               if ((xdrs)->x_ops->x_destroy)                    \
00215                      (*(xdrs)->x_ops->x_destroy)(xdrs); \
00216        } while (0)
00217 #define       xdr_destroy(xdrs)                                \
00218        do {                                             \
00219               if ((xdrs)->x_ops->x_destroy)                    \
00220                      (*(xdrs)->x_ops->x_destroy)(xdrs); \
00221        } while (0)
00222 
00223 /*
00224  * Support struct for discriminated unions.
00225  * You create an array of xdrdiscrim structures, terminated with
00226  * a entry with a null procedure pointer.  The xdr_union routine gets
00227  * the discriminant value and then searches the array of structures
00228  * for a matching value.  If a match is found the associated xdr routine
00229  * is called to handle that part of the union.  If there is
00230  * no match, then a default routine may be called.
00231  * If there is no match and no default routine it is an error.
00232  */
00233 #define NULL_xdrproc_t ((xdrproc_t)0)
00234 struct xdr_discrim
00235 {
00236   int value;
00237   xdrproc_t proc;
00238 };
00239 
00240 /*
00241  * Inline routines for fast encode/decode of primitive data types.
00242  * Caveat emptor: these use single memory cycles to get the
00243  * data from the underlying buffer, and will fail to operate
00244  * properly if the data is not aligned.  The standard way to use these
00245  * is to say:
00246  *      if ((buf = XDR_INLINE(xdrs, count)) == NULL)
00247  *              return (FALSE);
00248  *      <<< macro calls >>>
00249  * where ``count'' is the number of bytes of data occupied
00250  * by the primitive data types.
00251  *
00252  * N.B. and frozen for all time: each data type here uses 4 bytes
00253  * of external representation.
00254  */
00255 
00256 #define IXDR_GET_INT32(buf)           ((int32_t)ntohl((uint32_t)*(buf)++))
00257 #define IXDR_PUT_INT32(buf, v)        (*(buf)++ = (int32_t)htonl((uint32_t)(v)))
00258 #define IXDR_GET_U_INT32(buf)         ((uint32_t)IXDR_GET_INT32(buf))
00259 #define IXDR_PUT_U_INT32(buf, v)      IXDR_PUT_INT32(buf, (int32_t)(v))
00260 
00261 /* WARNING: The IXDR_*_LONG defines are removed by Sun for new platforms
00262  * and shouldn't be used any longer. Code which use this defines or longs
00263  * in the RPC code will not work on 64bit Solaris platforms !
00264  */
00265 #define IXDR_GET_LONG(buf) ((long)IXDR_GET_U_INT32(buf))
00266 #define IXDR_PUT_LONG(buf, v) ((long)IXDR_PUT_INT32(buf, (long)(v)))
00267 #define IXDR_GET_U_LONG(buf)             ((u_long)IXDR_GET_LONG(buf))
00268 #define IXDR_PUT_U_LONG(buf, v)          IXDR_PUT_LONG(buf, (long)(v))
00269 
00270 
00271 #define IXDR_GET_BOOL(buf)            ((bool_t)IXDR_GET_LONG(buf))
00272 #define IXDR_GET_ENUM(buf, t)         ((t)IXDR_GET_LONG(buf))
00273 #define IXDR_GET_SHORT(buf)           ((short)IXDR_GET_LONG(buf))
00274 #define IXDR_GET_U_SHORT(buf)         ((u_short)IXDR_GET_LONG(buf))
00275 
00276 #define IXDR_PUT_BOOL(buf, v)         IXDR_PUT_LONG(buf, (long)(v))
00277 #define IXDR_PUT_ENUM(buf, v)         IXDR_PUT_LONG(buf, (long)(v))
00278 #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG(buf, (long)(v))
00279 #define IXDR_PUT_U_SHORT(buf, v)      IXDR_PUT_LONG(buf, (long)(v))
00280 
00281 /*
00282  * These are the "generic" xdr routines.
00283  * None of these can have const applied because it's not possible to
00284  * know whether the call is a read or a write to the passed parameter
00285  * also, the XDR structure is always updated by some of these calls.
00286  */
00287 extern bool_t xdr_void (void) __THROW;
00288 extern bool_t xdr_short (XDR *__xdrs, short *__sp) __THROW;
00289 extern bool_t xdr_u_short (XDR *__xdrs, u_short *__usp) __THROW;
00290 extern bool_t xdr_int (XDR *__xdrs, int *__ip) __THROW;
00291 extern bool_t xdr_u_int (XDR *__xdrs, u_int *__up) __THROW;
00292 extern bool_t xdr_long (XDR *__xdrs, long *__lp) __THROW;
00293 extern bool_t xdr_u_long (XDR *__xdrs, u_long *__ulp) __THROW;
00294 extern bool_t xdr_hyper (XDR *__xdrs, quad_t *__llp) __THROW;
00295 extern bool_t xdr_u_hyper (XDR *__xdrs, u_quad_t *__ullp) __THROW;
00296 extern bool_t xdr_longlong_t (XDR *__xdrs, quad_t *__llp) __THROW;
00297 extern bool_t xdr_u_longlong_t (XDR *__xdrs, u_quad_t *__ullp) __THROW;
00298 extern bool_t xdr_int8_t (XDR *__xdrs, int8_t *__ip) __THROW;
00299 extern bool_t xdr_uint8_t (XDR *__xdrs, uint8_t *__up) __THROW;
00300 extern bool_t xdr_int16_t (XDR *__xdrs, int16_t *__ip) __THROW;
00301 extern bool_t xdr_uint16_t (XDR *__xdrs, uint16_t *__up) __THROW;
00302 extern bool_t xdr_int32_t (XDR *__xdrs, int32_t *__ip) __THROW;
00303 extern bool_t xdr_uint32_t (XDR *__xdrs, uint32_t *__up) __THROW;
00304 extern bool_t xdr_int64_t (XDR *__xdrs, int64_t *__ip) __THROW;
00305 extern bool_t xdr_uint64_t (XDR *__xdrs, uint64_t *__up) __THROW;
00306 extern bool_t xdr_quad_t (XDR *__xdrs, quad_t *__ip) __THROW;
00307 extern bool_t xdr_u_quad_t (XDR *__xdrs, u_quad_t *__up) __THROW;
00308 extern bool_t xdr_bool (XDR *__xdrs, bool_t *__bp) __THROW;
00309 extern bool_t xdr_enum (XDR *__xdrs, enum_t *__ep) __THROW;
00310 extern bool_t xdr_array (XDR * _xdrs, caddr_t *__addrp, u_int *__sizep,
00311                       u_int __maxsize, u_int __elsize, xdrproc_t __elproc)
00312      __THROW;
00313 extern bool_t xdr_bytes (XDR *__xdrs, char **__cpp, u_int *__sizep,
00314                       u_int __maxsize) __THROW;
00315 extern bool_t xdr_opaque (XDR *__xdrs, caddr_t __cp, u_int __cnt) __THROW;
00316 extern bool_t xdr_string (XDR *__xdrs, char **__cpp, u_int __maxsize) __THROW;
00317 extern bool_t xdr_union (XDR *__xdrs, enum_t *__dscmp, char *__unp,
00318                       __const struct xdr_discrim *__choices,
00319                       xdrproc_t dfault) __THROW;
00320 extern bool_t xdr_char (XDR *__xdrs, char *__cp) __THROW;
00321 extern bool_t xdr_u_char (XDR *__xdrs, u_char *__cp) __THROW;
00322 extern bool_t xdr_vector (XDR *__xdrs, char *__basep, u_int __nelem,
00323                        u_int __elemsize, xdrproc_t __xdr_elem) __THROW;
00324 extern bool_t xdr_float (XDR *__xdrs, float *__fp) __THROW;
00325 extern bool_t xdr_double (XDR *__xdrs, double *__dp) __THROW;
00326 extern bool_t xdr_reference (XDR *__xdrs, caddr_t *__xpp, u_int __size,
00327                           xdrproc_t __proc) __THROW;
00328 extern bool_t xdr_pointer (XDR *__xdrs, char **__objpp,
00329                         u_int __obj_size, xdrproc_t __xdr_obj) __THROW;
00330 extern bool_t xdr_wrapstring (XDR *__xdrs, char **__cpp) __THROW;
00331 extern u_long xdr_sizeof (xdrproc_t, void *) __THROW;
00332 
00333 /*
00334  * Common opaque bytes objects used by many rpc protocols;
00335  * declared here due to commonality.
00336  */
00337 #define MAX_NETOBJ_SZ 1024
00338 struct netobj
00339 {
00340   u_int n_len;
00341   char *n_bytes;
00342 };
00343 typedef struct netobj netobj;
00344 extern bool_t xdr_netobj (XDR *__xdrs, struct netobj *__np) __THROW;
00345 
00346 /*
00347  * These are the public routines for the various implementations of
00348  * xdr streams.
00349  */
00350 
00351 /* XDR using memory buffers */
00352 extern void xdrmem_create (XDR *__xdrs, __const caddr_t __addr,
00353                         u_int __size, enum xdr_op __xop) __THROW;
00354 
00355 /* XDR using stdio library */
00356 extern void xdrstdio_create (XDR *__xdrs, FILE *__file, enum xdr_op __xop)
00357      __THROW;
00358 
00359 /* XDR pseudo records for tcp */
00360 extern void xdrrec_create (XDR *__xdrs, u_int __sendsize,
00361                         u_int __recvsize, caddr_t __tcp_handle,
00362                         int (*__readit) (char *, char *, int),
00363                         int (*__writeit) (char *, char *, int)) __THROW;
00364 
00365 /* make end of xdr record */
00366 extern bool_t xdrrec_endofrecord (XDR *__xdrs, bool_t __sendnow) __THROW;
00367 
00368 /* move to beginning of next record */
00369 extern bool_t xdrrec_skiprecord (XDR *__xdrs) __THROW;
00370 
00371 /* true if no more input */
00372 extern bool_t xdrrec_eof (XDR *__xdrs) __THROW;
00373 
00374 /* free memory buffers for xdr */
00375 extern void xdr_free (xdrproc_t __proc, char *__objp) __THROW;
00376 
00377 __END_DECLS
00378 
00379 #endif /* rpc/xdr.h */