]> git.proxmox.com Git - mirror_zfs.git/blame - include/rpc/xdr.h
Add XDR implementation
[mirror_zfs.git] / include / rpc / xdr.h
CommitLineData
f48b6193
RC
1/*
2 * This file is part of the SPL: Solaris Porting Layer.
3 *
4 * Copyright (c) 2008 Sun Microsystems, Inc.
5 *
6 * This is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
dbb484ec
BB
21#ifndef _SPL_RPC_XDR_H
22#define _SPL_RPC_XDR_H
23
f48b6193
RC
24#include <sys/types.h>
25#include <rpc/types.h>
26
27/*
28 * XDR enums and types.
29 */
30enum xdr_op {
31 XDR_ENCODE,
32 XDR_DECODE
33};
34
35struct xdr_ops;
36
37typedef struct {
38 struct xdr_ops *x_ops; /* Also used to let caller know if
39 xdrmem_create() succeeds (sigh..) */
40 caddr_t x_addr; /* Current buffer addr */
41 caddr_t x_addr_end; /* End of the buffer */
42 enum xdr_op x_op; /* Stream direction */
43} XDR;
44
45typedef bool_t (*xdrproc_t)(XDR *xdrs, void *ptr);
46
47struct xdr_ops {
48 bool_t (*xdr_control)(XDR *, int, void *);
49
50 bool_t (*xdr_char)(XDR *, char *);
51 bool_t (*xdr_u_short)(XDR *, unsigned short *);
52 bool_t (*xdr_u_int)(XDR *, unsigned *);
53 bool_t (*xdr_u_longlong_t)(XDR *, u_longlong_t *);
54
55 bool_t (*xdr_opaque)(XDR *, caddr_t, const uint_t);
56 bool_t (*xdr_string)(XDR *, char **, const uint_t);
57 bool_t (*xdr_array)(XDR *, caddr_t *, uint_t *, const uint_t,
58 const uint_t, const xdrproc_t);
59};
60
61/*
62 * XDR control operator.
63 */
64#define XDR_GET_BYTES_AVAIL 1
65
66struct xdr_bytesrec {
67 bool_t xc_is_last_record;
68 size_t xc_num_avail;
69};
70
71typedef struct xdr_bytesrec xdr_bytesrec;
72
73/*
74 * XDR functions.
75 */
76void xdrmem_create(XDR *xdrs, const caddr_t addr, const uint_t size,
77 const enum xdr_op op);
78#define xdr_destroy(xdrs) ((void) 0) /* Currently not needed. If needed later,
79 we'll add it to struct xdr_ops */
80
81#define xdr_control(xdrs, req, info) (xdrs)->x_ops->xdr_control((xdrs), \
82 (req), (info))
83
84/*
85 * For precaution, the following are defined as static inlines instead of macros
86 * to get some amount of type safety.
87 *
88 * Also, macros wouldn't work in the case where typecasting is done, because it
89 * must be possible to reference the functions' addresses by these names.
90 */
91static inline bool_t xdr_char(XDR *xdrs, char *cp)
92{
93 return xdrs->x_ops->xdr_char(xdrs, cp);
94}
95
96static inline bool_t xdr_u_short(XDR *xdrs, unsigned short *usp)
97{
98 return xdrs->x_ops->xdr_u_short(xdrs, usp);
99}
100
101static inline bool_t xdr_short(XDR *xdrs, short *sp)
102{
103 BUILD_BUG_ON(sizeof(short) != 2);
104 return xdrs->x_ops->xdr_u_short(xdrs, (unsigned short *) sp);
105}
106
107static inline bool_t xdr_u_int(XDR *xdrs, unsigned *up)
108{
109 return xdrs->x_ops->xdr_u_int(xdrs, up);
110}
111
112static inline bool_t xdr_int(XDR *xdrs, int *ip)
113{
114 BUILD_BUG_ON(sizeof(int) != 4);
115 return xdrs->x_ops->xdr_u_int(xdrs, (unsigned *) ip);
116}
117
118static inline bool_t xdr_u_longlong_t(XDR *xdrs, u_longlong_t *ullp)
119{
120 return xdrs->x_ops->xdr_u_longlong_t(xdrs, ullp);
121}
122
123static inline bool_t xdr_longlong_t(XDR *xdrs, longlong_t *llp)
124{
125 BUILD_BUG_ON(sizeof(longlong_t) != 8);
126 return xdrs->x_ops->xdr_u_longlong_t(xdrs, (u_longlong_t *) llp);
127}
128
129/*
130 * Fixed-length opaque data.
131 */
132static inline bool_t xdr_opaque(XDR *xdrs, caddr_t cp, const uint_t cnt)
133{
134 return xdrs->x_ops->xdr_opaque(xdrs, cp, cnt);
135}
136
137/*
138 * Variable-length string.
139 * The *sp buffer must have (maxsize + 1) bytes.
140 */
141static inline bool_t xdr_string(XDR *xdrs, char **sp, const uint_t maxsize)
142{
143 return xdrs->x_ops->xdr_string(xdrs, sp, maxsize);
144}
145
146/*
147 * Variable-length arrays.
148 */
149static inline bool_t xdr_array(XDR *xdrs, caddr_t *arrp, uint_t *sizep,
150 const uint_t maxsize, const uint_t elsize, const xdrproc_t elproc)
151{
152 return xdrs->x_ops->xdr_array(xdrs, arrp, sizep, maxsize, elsize,
153 elproc);
154}
dbb484ec
BB
155
156#endif /* SPL_RPC_XDR_H */