samba-3.5.8 for ARM
[tomato.git] / release / src-rt-6.x.4708 / router / samba-3.5.8 / pidl / lib / Parse / Pidl / Samba3 / ServerNDR.pm
blob5599de9d790904692ee2b215859f8957f603e584
1 ###################################################
2 # Samba3 server generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
7 package Parse::Pidl::Samba3::ServerNDR;
9 use Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw(DeclLevel);
13 use strict;
14 use Parse::Pidl qw(warning error fatal);
15 use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference);
16 use Parse::Pidl::Util qw(ParseExpr has_property is_constant);
17 use Parse::Pidl::NDR qw(GetNextLevel);
18 use Parse::Pidl::Samba4 qw(ElementStars DeclLong);
19 use Parse::Pidl::Samba4::Header qw(GenerateFunctionOutEnv);
21 use vars qw($VERSION);
22 $VERSION = '0.01';
24 my $res;
25 my $res_hdr;
26 my $tabs = "";
27 sub indent() { $tabs.="\t"; }
28 sub deindent() { $tabs = substr($tabs, 1); }
29 sub pidl($) { my ($txt) = @_; $res .= $txt?$tabs.(shift)."\n":"\n"; }
30 sub pidl_hdr($) { $res_hdr .= (shift)."\n"; }
31 sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; }
33 sub DeclLevel($$)
35 my ($e, $l) = @_;
36 my $res = "";
38 if (has_property($e, "charset")) {
39 $res .= "const char";
40 } else {
41 $res .= mapTypeName($e->{TYPE});
44 my $stars = ElementStars($e, $l);
46 $res .= " ".$stars unless ($stars eq "");
48 return $res;
51 sub AllocOutVar($$$$$)
53 my ($e, $mem_ctx, $name, $env, $fail) = @_;
55 my $l = $e->{LEVELS}[0];
57 # we skip pointer to arrays
58 if ($l->{TYPE} eq "POINTER") {
59 my $nl = GetNextLevel($e, $l);
60 $l = $nl if ($nl->{TYPE} eq "ARRAY");
61 } elsif
63 # we don't support multi-dimentional arrays yet
64 ($l->{TYPE} eq "ARRAY") {
65 my $nl = GetNextLevel($e, $l);
66 if ($nl->{TYPE} eq "ARRAY") {
67 fatal($e->{ORIGINAL},"multi-dimentional [out] arrays are not supported!");
69 } else {
70 # neither pointer nor array, no need to alloc something.
71 return;
74 if ($l->{TYPE} eq "ARRAY") {
75 unless(defined($l->{SIZE_IS})) {
76 error($e->{ORIGINAL}, "No size known for array `$e->{NAME}'");
77 pidl "#error No size known for array `$e->{NAME}'";
78 } else {
79 my $size = ParseExpr($l->{SIZE_IS}, $env, $e);
80 pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);";
82 } else {
83 pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");";
86 pidl "if ($name == NULL) {";
87 $fail->();
88 pidl "}";
89 pidl "";
92 sub CallWithStruct($$$$)
94 my ($pipes_struct, $mem_ctx, $fn, $fail) = @_;
95 my $env = GenerateFunctionOutEnv($fn);
96 my $hasout = 0;
97 foreach (@{$fn->{ELEMENTS}}) {
98 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
101 pidl "ZERO_STRUCT(r->out);" if ($hasout);
103 my $proto = "_$fn->{NAME}(pipes_struct *p, struct $fn->{NAME} *r";
104 my $ret = "_$fn->{NAME}($pipes_struct, r";
105 foreach (@{$fn->{ELEMENTS}}) {
106 my @dir = @{$_->{DIRECTION}};
107 if (grep(/in/, @dir) and grep(/out/, @dir)) {
108 pidl "r->out.$_->{NAME} = r->in.$_->{NAME};";
112 foreach (@{$fn->{ELEMENTS}}) {
113 my @dir = @{$_->{DIRECTION}};
114 if (grep(/in/, @dir) and grep(/out/, @dir)) {
115 # noop
116 } elsif (grep(/out/, @dir) and not
117 has_property($_, "represent_as")) {
118 AllocOutVar($_, $mem_ctx, "r->out.$_->{NAME}", $env, $fail);
121 $ret .= ")";
122 $proto .= ");";
124 if ($fn->{RETURN_TYPE}) {
125 $ret = "r->out.result = $ret";
126 $proto = "$fn->{RETURN_TYPE} $proto";
127 } else {
128 $proto = "void $proto";
131 pidl_hdr "$proto";
132 pidl "$ret;";
135 sub ParseFunction($$)
137 my ($if,$fn) = @_;
139 my $op = "NDR_".uc($fn->{NAME});
141 pidl "static bool api_$fn->{NAME}(pipes_struct *p)";
142 pidl "{";
143 indent;
144 pidl "const struct ndr_interface_call *call;";
145 pidl "struct ndr_pull *pull;";
146 pidl "struct ndr_push *push;";
147 pidl "enum ndr_err_code ndr_err;";
148 pidl "DATA_BLOB blob;";
149 pidl "struct $fn->{NAME} *r;";
150 pidl "";
151 pidl "call = &ndr_table_$if->{NAME}.calls[$op];";
152 pidl "";
153 pidl "r = talloc(talloc_tos(), struct $fn->{NAME});";
154 pidl "if (r == NULL) {";
155 pidl "\treturn false;";
156 pidl "}";
157 pidl "";
158 pidl "if (!prs_data_blob(&p->in_data.data, &blob, r)) {";
159 pidl "\ttalloc_free(r);";
160 pidl "\treturn false;";
161 pidl "}";
162 pidl "";
163 pidl "pull = ndr_pull_init_blob(&blob, r, NULL);";
164 pidl "if (pull == NULL) {";
165 pidl "\ttalloc_free(r);";
166 pidl "\treturn false;";
167 pidl "}";
168 pidl "";
169 pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
170 pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);";
171 pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
172 pidl "\ttalloc_free(r);";
173 pidl "\treturn false;";
174 pidl "}";
175 pidl "";
176 pidl "if (DEBUGLEVEL >= 10) {";
177 pidl "\tNDR_PRINT_IN_DEBUG($fn->{NAME}, r);";
178 pidl "}";
179 pidl "";
181 CallWithStruct("p", "r", $fn,
182 sub {
183 pidl "\ttalloc_free(r);";
184 pidl "\treturn false;";
188 pidl "";
189 pidl "if (p->rng_fault_state) {";
190 pidl "\ttalloc_free(r);";
191 pidl "\t/* Return true here, srv_pipe_hnd.c will take care */";
192 pidl "\treturn true;";
193 pidl "}";
194 pidl "";
195 pidl "if (DEBUGLEVEL >= 10) {";
196 pidl "\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, r);";
197 pidl "}";
198 pidl "";
199 pidl "push = ndr_push_init_ctx(r, NULL);";
200 pidl "if (push == NULL) {";
201 pidl "\ttalloc_free(r);";
202 pidl "\treturn false;";
203 pidl "}";
204 pidl "";
205 pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);";
206 pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
207 pidl "\ttalloc_free(r);";
208 pidl "\treturn false;";
209 pidl "}";
210 pidl "";
211 pidl "blob = ndr_push_blob(push);";
212 pidl "if (!prs_copy_data_in(&p->out_data.rdata, (const char *)blob.data, (uint32_t)blob.length)) {";
213 pidl "\ttalloc_free(r);";
214 pidl "\treturn false;";
215 pidl "}";
216 pidl "";
217 pidl "talloc_free(r);";
218 pidl "";
219 pidl "return true;";
220 deindent;
221 pidl "}";
222 pidl "";
225 sub ParseDispatchFunction($)
227 my ($if) = @_;
229 pidl_hdr "NTSTATUS rpc_$if->{NAME}_dispatch(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, const struct ndr_interface_table *table, uint32_t opnum, void *r);";
230 pidl "NTSTATUS rpc_$if->{NAME}_dispatch(struct rpc_pipe_client *cli, TALLOC_CTX *mem_ctx, const struct ndr_interface_table *table, uint32_t opnum, void *_r)";
231 pidl "{";
232 indent;
233 pidl "if (cli->pipes_struct == NULL) {";
234 pidl "\treturn NT_STATUS_INVALID_PARAMETER;";
235 pidl "}";
236 pidl "";
237 pidl "switch (opnum)";
238 pidl "{";
239 indent;
240 foreach my $fn (@{$if->{FUNCTIONS}}) {
241 next if ($fn->{PROPERTIES}{noopnum});
242 my $op = "NDR_".uc($fn->{NAME});
243 pidl "case $op: {";
244 indent;
245 pidl "struct $fn->{NAME} *r = (struct $fn->{NAME} *)_r;";
246 CallWithStruct("cli->pipes_struct", "mem_ctx", $fn,
247 sub { pidl "return NT_STATUS_NO_MEMORY;"; });
248 pidl "return NT_STATUS_OK;";
249 deindent;
250 pidl "}";
251 pidl "";
254 pidl "default:";
255 pidl "\treturn NT_STATUS_NOT_IMPLEMENTED;";
256 deindent;
257 pidl "}";
258 deindent;
259 pidl "}";
261 pidl "";
264 sub ParseInterface($)
266 my $if = shift;
268 my $uif = uc($if->{NAME});
270 pidl_hdr "#ifndef __SRV_$uif\__";
271 pidl_hdr "#define __SRV_$uif\__";
273 foreach (@{$if->{FUNCTIONS}}) {
274 next if ($_->{PROPERTIES}{noopnum});
275 ParseFunction($if, $_);
278 pidl "";
279 pidl "/* Tables */";
280 pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
281 pidl "{";
282 indent;
284 foreach (@{$if->{FUNCTIONS}}) {
285 next if ($_->{PROPERTIES}{noopnum});
286 pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
289 deindent;
290 pidl "};";
292 pidl "";
294 pidl_hdr "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns);";
295 pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
296 pidl "{";
297 indent;
298 pidl "*fns = api_$if->{NAME}_cmds;";
299 pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
300 deindent;
301 pidl "}";
302 pidl "";
304 ParseDispatchFunction($if);
306 pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(void);";
307 pidl "NTSTATUS rpc_$if->{NAME}_init(void)";
308 pidl "{";
309 pidl "\treturn rpc_srv_register(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", \&ndr_table_$if->{NAME}, api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct));";
310 pidl "}";
312 pidl_hdr "#endif /* __SRV_$uif\__ */";
315 sub Parse($$$)
317 my($ndr,$header,$ndr_header) = @_;
319 $res = "";
320 $res_hdr = "";
322 pidl "/*";
323 pidl " * Unix SMB/CIFS implementation.";
324 pidl " * server auto-generated by pidl. DO NOT MODIFY!";
325 pidl " */";
326 pidl "";
327 pidl "#include \"includes.h\"";
328 pidl "#include \"$header\"";
329 pidl_hdr "#include \"$ndr_header\"";
330 pidl "";
332 foreach (@$ndr) {
333 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
336 return ($res, $res_hdr);