r14402: Generate seperate headers for RPC client functions.
[samba.git] / source4 / librpc / ndr / libndr.h
1 /* 
2    Unix SMB/CIFS implementation.
3    rpc interface definitions
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it 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 program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #ifndef __LIBNDR_H__
22 #define __LIBNDR_H__
23
24 /*
25   this provides definitions for the libcli/rpc/ MSRPC library
26 */
27
28
29 /*
30   this is used by the token store/retrieve code
31 */
32 struct ndr_token_list {
33         struct ndr_token_list *next, *prev;
34         const void *key;
35         uint32_t value;
36 };
37
38 /* this is the base structure passed to routines that 
39    parse MSRPC formatted data 
40
41    note that in Samba4 we use separate routines and structures for
42    MSRPC marshalling and unmarshalling. Also note that these routines
43    are being kept deliberately very simple, and are not tied to a
44    particular transport
45 */
46 struct ndr_pull {
47         uint32_t flags; /* LIBNDR_FLAG_* */
48         uint8_t *data;
49         uint32_t data_size;
50         uint32_t offset;
51
52         uint32_t relative_base_offset;
53         struct ndr_token_list *relative_base_list;
54
55         struct ndr_token_list *relative_list;
56         struct ndr_token_list *array_size_list;
57         struct ndr_token_list *array_length_list;
58         struct ndr_token_list *switch_list;
59
60         TALLOC_CTX *current_mem_ctx;
61
62         /* this is used to ensure we generate unique reference IDs
63            between request and reply */
64         uint32_t ptr_count;
65 };
66
67 struct ndr_pull_save {
68         uint32_t data_size;
69         uint32_t offset;
70         struct ndr_pull_save *next;
71 };
72
73 /* structure passed to functions that generate NDR formatted data */
74 struct ndr_push {
75         uint32_t flags; /* LIBNDR_FLAG_* */
76         uint8_t *data;
77         uint32_t alloc_size;
78         uint32_t offset;
79
80         uint32_t relative_base_offset;
81         struct ndr_token_list *relative_base_list;
82
83         struct ndr_token_list *switch_list;
84         struct ndr_token_list *relative_list;
85         struct ndr_token_list *nbt_string_list;
86
87         /* this is used to ensure we generate unique reference IDs */
88         uint32_t ptr_count;
89 };
90
91 struct ndr_push_save {
92         uint32_t offset;
93         struct ndr_push_save *next;
94 };
95
96
97 /* structure passed to functions that print IDL structures */
98 struct ndr_print {
99         uint32_t flags; /* LIBNDR_FLAG_* */
100         uint32_t depth;
101         struct ndr_token_list *switch_list;
102         void (*print)(struct ndr_print *, const char *, ...);
103         void *private;
104 };
105
106 #define LIBNDR_FLAG_BIGENDIAN  (1<<0)
107 #define LIBNDR_FLAG_NOALIGN    (1<<1)
108
109 #define LIBNDR_FLAG_STR_ASCII           (1<<2)
110 #define LIBNDR_FLAG_STR_LEN4            (1<<3)
111 #define LIBNDR_FLAG_STR_SIZE4           (1<<4)
112 #define LIBNDR_FLAG_STR_NOTERM          (1<<5)
113 #define LIBNDR_FLAG_STR_NULLTERM        (1<<6)
114 #define LIBNDR_FLAG_STR_SIZE2           (1<<7)
115 #define LIBNDR_FLAG_STR_BYTESIZE        (1<<8)
116 #define LIBNDR_FLAG_STR_FIXLEN32        (1<<9)
117 #define LIBNDR_FLAG_STR_CONFORMANT      (1<<10)
118 #define LIBNDR_FLAG_STR_CHARLEN         (1<<11)
119 #define LIBNDR_FLAG_STR_UTF8            (1<<12)
120 #define LIBNDR_FLAG_STR_FIXLEN15        (1<<13)
121 #define LIBNDR_STRING_FLAGS             (0x7FFC)
122
123
124 #define LIBNDR_FLAG_REF_ALLOC    (1<<20)
125 #define LIBNDR_FLAG_REMAINING    (1<<21)
126 #define LIBNDR_FLAG_ALIGN2       (1<<22)
127 #define LIBNDR_FLAG_ALIGN4       (1<<23)
128 #define LIBNDR_FLAG_ALIGN8       (1<<24)
129
130 #define LIBNDR_ALIGN_FLAGS (LIBNDR_FLAG_ALIGN2|LIBNDR_FLAG_ALIGN4|LIBNDR_FLAG_ALIGN8)
131
132 #define LIBNDR_PRINT_ARRAY_HEX   (1<<25)
133 #define LIBNDR_PRINT_SET_VALUES  (1<<26)
134
135 /* used to force a section of IDL to be little-endian */
136 #define LIBNDR_FLAG_LITTLE_ENDIAN (1<<27)
137
138 /* used to check if alignment padding is zero */
139 #define LIBNDR_FLAG_PAD_CHECK     (1<<28)
140
141 /* set if an object uuid will be present */
142 #define LIBNDR_FLAG_OBJECT_PRESENT    (1<<30)
143
144 /* set to avoid recursion in ndr_size_*() calculation */
145 #define LIBNDR_FLAG_NO_NDR_SIZE         (1<<31)
146
147 /* useful macro for debugging */
148 #define NDR_PRINT_DEBUG(type, p) ndr_print_debug((ndr_print_fn_t)ndr_print_ ##type, #p, p)
149 #define NDR_PRINT_UNION_DEBUG(type, level, p) ndr_print_union_debug((ndr_print_fn_t)ndr_print_ ##type, #p, level, p)
150 #define NDR_PRINT_FUNCTION_DEBUG(type, flags, p) ndr_print_function_debug((ndr_print_function_t)ndr_print_ ##type, #type, flags, p)
151 #define NDR_PRINT_BOTH_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_BOTH, p)
152 #define NDR_PRINT_OUT_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_OUT, p)
153 #define NDR_PRINT_IN_DEBUG(type, p) NDR_PRINT_FUNCTION_DEBUG(type, NDR_IN | NDR_SET_VALUES, p)
154
155 #define NDR_BE(ndr) (((ndr)->flags & (LIBNDR_FLAG_BIGENDIAN|LIBNDR_FLAG_LITTLE_ENDIAN)) == LIBNDR_FLAG_BIGENDIAN)
156
157 enum ndr_err_code {
158         NDR_ERR_ARRAY_SIZE,
159         NDR_ERR_BAD_SWITCH,
160         NDR_ERR_OFFSET,
161         NDR_ERR_RELATIVE,
162         NDR_ERR_CHARCNV,
163         NDR_ERR_LENGTH,
164         NDR_ERR_SUBCONTEXT,
165         NDR_ERR_COMPRESSION,
166         NDR_ERR_STRING,
167         NDR_ERR_VALIDATE,
168         NDR_ERR_BUFSIZE,
169         NDR_ERR_ALLOC,
170         NDR_ERR_RANGE,
171         NDR_ERR_TOKEN,
172         NDR_ERR_IPV4ADDRESS
173 };
174
175 enum ndr_compression_alg {
176         NDR_COMPRESSION_MSZIP   = 2,
177         NDR_COMPRESSION_XPRESS  = 3
178 };
179
180 /*
181   flags passed to control parse flow
182 */
183 #define NDR_SCALARS 1
184 #define NDR_BUFFERS 2
185
186 /*
187   flags passed to ndr_print_*()
188 */
189 #define NDR_IN 1
190 #define NDR_OUT 2
191 #define NDR_BOTH 3
192 #define NDR_SET_VALUES 4
193
194 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
195         if ((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size) { \
196                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull bytes %u", (unsigned)n); \
197         } \
198 } while(0)
199
200 #define NDR_ALIGN(ndr, n) ndr_align_size(ndr->offset, n)
201
202 #define NDR_ROUND(size, n) (((size)+((n)-1)) & ~((n)-1))
203
204 #define NDR_PULL_ALIGN(ndr, n) do { \
205         if (!(ndr->flags & LIBNDR_FLAG_NOALIGN)) { \
206                 if (ndr->flags & LIBNDR_FLAG_PAD_CHECK) { \
207                         ndr_check_padding(ndr, n); \
208                 } \
209                 ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
210         } \
211         if (ndr->offset > ndr->data_size) { \
212                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, "Pull align %u", (unsigned)n); \
213         } \
214 } while(0)
215
216 #define NDR_PUSH_NEED_BYTES(ndr, n) NDR_CHECK(ndr_push_expand(ndr, ndr->offset+(n)))
217
218 #define NDR_PUSH_ALIGN(ndr, n) do { \
219         if (!(ndr->flags & LIBNDR_FLAG_NOALIGN)) { \
220                 uint32_t _pad = ((ndr->offset + (n-1)) & ~(n-1)) - ndr->offset; \
221                 while (_pad--) NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 0)); \
222         } \
223 } while(0)
224
225 /* these are used to make the error checking on each element in libndr
226    less tedious, hopefully making the code more readable */
227 #define NDR_CHECK(call) do { NTSTATUS _status; \
228                              _status = call; \
229                              if (!NT_STATUS_IS_OK(_status)) \
230                                 return _status; \
231                         } while (0)
232
233 #define NDR_PULL_GET_MEM_CTX(ndr) (ndr->current_mem_ctx)
234
235 #define NDR_PULL_SET_MEM_CTX(ndr, mem_ctx, flgs) do {\
236         if ( !(flgs) || (ndr->flags & flgs) ) {\
237                 if (!(mem_ctx)) {\
238                         return ndr_pull_error(ndr, NDR_ERR_ALLOC, "NDR_PULL_SET_MEM_CTX(NULL): %s\n", __location__); \
239                 }\
240                 ndr->current_mem_ctx = discard_const(mem_ctx);\
241         }\
242 } while(0)
243
244 #define _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr) do {\
245         if (!ndr->current_mem_ctx) {\
246                 ndr->current_mem_ctx = talloc_new(ndr);\
247                 if (!ndr->current_mem_ctx) {\
248                         return ndr_pull_error(ndr, NDR_ERR_ALLOC, "_NDR_PULL_FIX_CURRENT_MEM_CTX() failed: %s\n", __location__); \
249                 }\
250         }\
251 } while(0)
252
253 #define NDR_PULL_ALLOC_SIZE(ndr, s, size) do { \
254         _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr);\
255         (s) = talloc_size(ndr->current_mem_ctx, size); \
256         if (!(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Alloc %u failed: %s\n",(unsigned)size, __location__); \
257 } while (0)
258
259 #define NDR_PULL_ALLOC(ndr, s) NDR_PULL_ALLOC_SIZE(ndr, s, sizeof(*(s)))
260
261 #define NDR_PULL_ALLOC_N_SIZE(ndr, s, n, elsize) do { \
262         _NDR_PULL_FIX_CURRENT_MEM_CTX(ndr);\
263         (s) = talloc_array_size(ndr->current_mem_ctx, elsize, n); \
264         if (!(s)) return ndr_pull_error(ndr, NDR_ERR_ALLOC, "Alloc %u * %u failed: %s\n", (unsigned)n, (unsigned)elsize, __location__); \
265 } while (0)
266
267 #define NDR_PULL_ALLOC_N(ndr, s, n) NDR_PULL_ALLOC_N_SIZE(ndr, s, n, sizeof(*(s)))
268
269
270 #define NDR_PUSH_ALLOC_SIZE(ndr, s, size) do { \
271        (s) = talloc_size(ndr, size); \
272        if (!(s)) return ndr_push_error(ndr, NDR_ERR_ALLOC, "push alloc %u failed: %s\n", (unsigned)size, __location__); \
273 } while (0)
274
275 #define NDR_PUSH_ALLOC(ndr, s) NDR_PUSH_ALLOC_SIZE(ndr, s, sizeof(*(s)))
276
277 /* these are used when generic fn pointers are needed for ndr push/pull fns */
278 typedef NTSTATUS (*ndr_push_flags_fn_t)(struct ndr_push *, int ndr_flags, const void *);
279 typedef NTSTATUS (*ndr_pull_flags_fn_t)(struct ndr_pull *, int ndr_flags, void *);
280 typedef void (*ndr_print_fn_t)(struct ndr_print *, const char *, const void *);
281 typedef void (*ndr_print_function_t)(struct ndr_print *, const char *, int, const void *);
282
283 extern const struct dcerpc_syntax_id ndr_transfer_syntax;
284 extern const struct dcerpc_syntax_id ndr64_transfer_syntax;
285
286 #include "librpc/gen_ndr/misc.h"
287 #include "librpc/ndr/libndr_proto.h"
288
289 #endif /* __LIBNDR_H__ */