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