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