Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

308 lines
11 KiB

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