pidl: fix handling of output arguments in s3 client stubs.
[kamenim/samba.git] / pidl / lib / Parse / Pidl / Samba3 / ClientNDR.pm
1 ###################################################
2 # Samba3 client generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # Copyright gd@samba.org 2008
6 # released under the GNU GPL
7
8 package Parse::Pidl::Samba3::ClientNDR;
9
10 use Exporter;
11 @ISA = qw(Exporter);
12 @EXPORT_OK = qw(ParseFunction $res $res_hdr ParseOutputArgument);
13
14 use strict;
15 use Parse::Pidl qw(fatal warning error);
16 use Parse::Pidl::Util qw(has_property ParseExpr);
17 use Parse::Pidl::Samba4 qw(DeclLong);
18 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv);
19
20 use vars qw($VERSION);
21 $VERSION = '0.01';
22
23 sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
24 sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
25 sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; }
26 sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; } 
27 sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }
28
29 sub genpad($)
30 {
31         my ($s) = @_;
32         my $nt = int((length($s)+1)/8);
33         my $lt = ($nt*8)-1;
34         my $ns = (length($s)-$lt);
35         return "\t"x($nt)." "x($ns);
36 }
37
38 sub new($)
39 {
40         my ($class) = shift;
41         my $self = { res => "", res_hdr => "", tabs => "" };
42         bless($self, $class);
43 }
44
45 sub ElementDirection($)
46 {
47         my ($e) = @_;
48
49         return "[in,out]" if (has_property($e, "in") and has_property($e, "out"));
50         return "[in]" if (has_property($e, "in"));
51         return "[out]" if (has_property($e, "out"));
52         return "[in,out]";
53 }
54
55 sub HeaderProperties($$)
56 {
57         my($props,$ignores) = @_;
58         my $ret = "";
59
60         foreach my $d (keys %{$props}) {
61                 next if (grep(/^$d$/, @$ignores));
62                 if($props->{$d} ne "1") {
63                         $ret.= "$d($props->{$d}),";
64                 } else {
65                         $ret.="$d,";
66                 }
67         }
68
69         if ($ret) {
70                 return "[" . substr($ret, 0, -1) . "]";
71         }
72 }
73
74 sub ParseOutputArgument($$$;$$)
75 {
76         my ($self, $fn, $e, $r, $o) = @_;
77         my $level = 0;
78         $r = "r." unless defined($r);
79         $o = "" unless defined($o);
80
81         if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY") {
82                 $self->pidl("return NT_STATUS_NOT_SUPPORTED;");
83                 error($e->{ORIGINAL}, "[out] argument is not a pointer or array");
84                 return;
85         }
86
87         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
88                 $level = 1;
89                 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
90                         $self->pidl("if ($o$e->{NAME} && ${r}out.$e->{NAME}) {");
91                         $self->indent;
92                 }
93         }
94
95         if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
96                 # This is a call to GenerateFunctionInEnv intentionally. 
97                 # Since the data is being copied into a user-provided data 
98                 # structure, the user should be able to know the size beforehand 
99                 # to allocate a structure of the right size.
100                 my $env = GenerateFunctionInEnv($fn, $r);
101                 my $l = $e->{LEVELS}[$level];
102                 unless (defined($l->{SIZE_IS})) {
103                         error($e->{ORIGINAL}, "no size known for [out] array `$e->{NAME}'");
104                         $self->pidl('#error No size known for [out] array `$e->{NAME}');
105                 } else {
106                         my $size_is = ParseExpr($l->{SIZE_IS}, $env, $e->{ORIGINAL});
107                         if (has_property($e, "charset")) {
108                                 $self->pidl("memcpy(discard_const_p(uint8_t *, $o$e->{NAME}), ${r}out.$e->{NAME}, ($size_is) * sizeof(*$o$e->{NAME}));");
109                         } else {
110                                 $self->pidl("memcpy($o$e->{NAME}, ${r}out.$e->{NAME}, ($size_is) * sizeof(*$o$e->{NAME}));");
111                         }
112                 }
113         } else {
114                 $self->pidl("*$o$e->{NAME} = *${r}out.$e->{NAME};");
115         }
116
117         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
118                 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
119                         $self->deindent;
120                         $self->pidl("}");
121                 }
122         }
123 }
124
125 sub ParseFunctionAsyncState($$$)
126 {
127         my ($self, $if, $fn) = @_;
128
129         my $state_str = "struct rpccli_$fn->{NAME}_state";
130         my $done_fn = "rpccli_$fn->{NAME}_done";
131
132         $self->pidl("$state_str {");
133         $self->indent;
134         $self->pidl("struct $fn->{NAME} orig;");
135         $self->pidl("struct $fn->{NAME} tmp;");
136         $self->pidl("TALLOC_CTX *out_mem_ctx;");
137         $self->pidl("NTSTATUS (*dispatch_recv)(struct tevent_req *req, TALLOC_CTX *mem_ctx);");
138         $self->deindent;
139         $self->pidl("};");
140         $self->pidl("");
141         $self->pidl("static void $done_fn(struct tevent_req *subreq);");
142         $self->pidl("");
143 }
144
145 sub ParseFunctionAsyncSend($$$)
146 {
147         my ($self, $if, $fn) = @_;
148
149         my $fn_args = "";
150         my $uif = uc($if);
151         my $ufn = "NDR_".uc($fn->{NAME});
152         my $state_str = "struct rpccli_$fn->{NAME}_state";
153         my $done_fn = "rpccli_$fn->{NAME}_done";
154         my $out_mem_ctx = "rpccli_$fn->{NAME}_out_memory";
155         my $fn_str = "struct tevent_req *rpccli_$fn->{NAME}_send";
156         my $pad = genpad($fn_str);
157
158         $fn_args .= "TALLOC_CTX *mem_ctx";
159         $fn_args .= ",\n" . $pad . "struct tevent_context *ev";
160         $fn_args .= ",\n" . $pad . "struct rpc_pipe_client *cli";
161
162         foreach (@{$fn->{ELEMENTS}}) {
163                 my $dir = ElementDirection($_);
164                 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
165                 $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */";
166         }
167
168         $self->fn_declare("$fn_str($fn_args)");
169         $self->pidl("{");
170         $self->indent;
171         $self->pidl("struct tevent_req *req;");
172         $self->pidl("$state_str *state;");
173         $self->pidl("struct tevent_req *subreq;");
174         $self->pidl("");
175         $self->pidl("req = tevent_req_create(mem_ctx, &state,");
176         $self->pidl("\t\t\t$state_str);");
177         $self->pidl("if (req == NULL) {");
178         $self->indent;
179         $self->pidl("return NULL;");
180         $self->deindent;
181         $self->pidl("}");
182         $self->pidl("state->out_mem_ctx = NULL;");
183         $self->pidl("state->dispatch_recv = cli->dispatch_recv;");
184         $self->pidl("");
185
186         $self->pidl("/* In parameters */");
187         foreach (@{$fn->{ELEMENTS}}) {
188                 if (grep(/in/, @{$_->{DIRECTION}})) {
189                         $self->pidl("state->orig.in.$_->{NAME} = _$_->{NAME};");
190                 }
191         }
192         $self->pidl("");
193
194         my $out_params = 0;
195         $self->pidl("/* Out parameters */");
196         foreach (@{$fn->{ELEMENTS}}) {
197                 if (grep(/out/, @{$_->{DIRECTION}})) {
198                         $self->pidl("state->orig.out.$_->{NAME} = _$_->{NAME};");
199                         $out_params++;
200                 }
201         }
202         $self->pidl("");
203
204         if (defined($fn->{RETURN_TYPE})) {
205                 $self->pidl("/* Result */");
206                 $self->pidl("ZERO_STRUCT(state->orig.out.result);");
207                 $self->pidl("");
208         }
209
210         if ($out_params > 0) {
211                 $self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,");
212                 $self->pidl("\t\t     \"$out_mem_ctx\");");
213                 $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
214                 $self->indent;
215                 $self->pidl("return tevent_req_post(req, ev);");
216                 $self->deindent;
217                 $self->pidl("}");
218                 $self->pidl("");
219         }
220
221         $self->pidl("/* make a temporary copy, that we pass to the dispatch function */");
222         $self->pidl("state->tmp = state->orig;");
223         $self->pidl("");
224
225         $self->pidl("subreq = cli->dispatch_send(state, ev, cli,");
226         $self->pidl("\t\t\t    &ndr_table_$if,");
227         $self->pidl("\t\t\t    $ufn,");
228         $self->pidl("\t\t\t    &state->tmp);");
229         $self->pidl("if (tevent_req_nomem(subreq, req)) {");
230         $self->indent;
231         $self->pidl("return tevent_req_post(req, ev);");
232         $self->deindent;
233         $self->pidl("}");
234         $self->pidl("tevent_req_set_callback(subreq, $done_fn, req);");
235         $self->pidl("return req;");
236         $self->deindent;
237         $self->pidl("}");
238         $self->pidl("");
239 }
240
241 sub ParseFunctionAsyncDone($$$)
242 {
243         my ($self, $if, $fn) = @_;
244
245         my $state_str = "struct rpccli_$fn->{NAME}_state";
246         my $done_fn = "rpccli_$fn->{NAME}_done";
247
248         $self->pidl("static void $done_fn(struct tevent_req *subreq)");
249         $self->pidl("{");
250         $self->indent;
251         $self->pidl("struct tevent_req *req = tevent_req_callback_data(");
252         $self->pidl("\tsubreq, struct tevent_req);");
253         $self->pidl("$state_str *state = tevent_req_data(");
254         $self->pidl("\treq, $state_str);");
255         $self->pidl("NTSTATUS status;");
256         $self->pidl("TALLOC_CTX *mem_ctx;");
257         $self->pidl("");
258
259         $self->pidl("if (state->out_mem_ctx) {");
260         $self->indent;
261         $self->pidl("mem_ctx = state->out_mem_ctx;");
262         $self->deindent;
263         $self->pidl("} else {");
264         $self->indent;
265         $self->pidl("mem_ctx = state;");
266         $self->deindent;
267         $self->pidl("}");
268         $self->pidl("");
269
270         $self->pidl("status = state->dispatch_recv(subreq, mem_ctx);");
271         $self->pidl("TALLOC_FREE(subreq);");
272         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
273         $self->indent;
274         $self->pidl("tevent_req_nterror(req, status);");
275         $self->pidl("return;");
276         $self->deindent;
277         $self->pidl("}");
278         $self->pidl("");
279
280         $self->pidl("/* Copy out parameters */");
281         foreach my $e (@{$fn->{ELEMENTS}}) {
282                 next unless (grep(/out/, @{$e->{DIRECTION}}));
283
284                 $self->ParseOutputArgument($fn, $e, "state->tmp.", "state->orig.out.");
285         }
286         $self->pidl("");
287
288         if (defined($fn->{RETURN_TYPE})) {
289                 $self->pidl("/* Copy result */");
290                 $self->pidl("state->orig.out.result = state->tmp.out.result;");
291                 $self->pidl("");
292         }
293
294         $self->pidl("/* Reset temporary structure */");
295         $self->pidl("ZERO_STRUCT(state->tmp);");
296         $self->pidl("");
297
298         $self->pidl("tevent_req_done(req);");
299         $self->deindent;
300         $self->pidl("}");
301         $self->pidl("");
302 }
303
304 sub ParseFunctionAsyncRecv($$$)
305 {
306         my ($self, $if, $fn) = @_;
307
308         my $fn_args = "";
309         my $state_str = "struct rpccli_$fn->{NAME}_state";
310         my $fn_str = "NTSTATUS rpccli_$fn->{NAME}_recv";
311         my $pad = genpad($fn_str);
312
313         $fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx";
314
315         if (defined($fn->{RETURN_TYPE})) {
316                 $fn_args .= ",\n" . $pad . "$fn->{RETURN_TYPE} *result";
317         }
318
319         $self->fn_declare("$fn_str($fn_args)");
320         $self->pidl("{");
321         $self->indent;
322         $self->pidl("$state_str *state = tevent_req_data(");
323         $self->pidl("\treq, $state_str);");
324         $self->pidl("NTSTATUS status;");
325         $self->pidl("");
326         $self->pidl("if (tevent_req_is_nterror(req, &status)) {");
327         $self->indent;
328         $self->pidl("tevent_req_received(req);");
329         $self->pidl("return status;");
330         $self->deindent;
331         $self->pidl("}");
332         $self->pidl("");
333
334         $self->pidl("/* Steal possbile out parameters to the callers context */");
335         $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
336         $self->pidl("");
337
338         if (defined($fn->{RETURN_TYPE})) {
339                 $self->pidl("/* Return result */");
340                 $self->pidl("*result = state->orig.out.result;");
341                 $self->pidl("");
342         }
343
344         $self->pidl("tevent_req_received(req);");
345         $self->pidl("return NT_STATUS_OK;");
346         $self->deindent;
347         $self->pidl("}");
348         $self->pidl("");
349 }
350
351 sub ParseFunctionSync($$$)
352 {
353         my ($self, $if, $fn) = @_;
354
355         my $fn_args = "";
356         my $uif = uc($if);
357         my $ufn = "NDR_".uc($fn->{NAME});
358         my $fn_str = "NTSTATUS rpccli_$fn->{NAME}";
359         my $pad = genpad($fn_str);
360
361         $fn_args .= "struct rpc_pipe_client *cli,\n" . $pad . "TALLOC_CTX *mem_ctx";
362
363         foreach (@{$fn->{ELEMENTS}}) {
364                 my $dir = ElementDirection($_);
365                 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
366                 $fn_args .= ",\n" . $pad . DeclLong($_) . " /* $dir $prop */";
367         }
368
369         if (defined($fn->{RETURN_TYPE}) && ($fn->{RETURN_TYPE} eq "WERROR")) {
370                 $fn_args .= ",\n" . $pad . "WERROR *werror";
371         }
372
373         $self->fn_declare("$fn_str($fn_args)");
374         $self->pidl("{");
375         $self->indent;
376         $self->pidl("struct $fn->{NAME} r;");
377         $self->pidl("NTSTATUS status;");
378         $self->pidl("");
379         $self->pidl("/* In parameters */");
380
381         foreach (@{$fn->{ELEMENTS}}) {
382                 if (grep(/in/, @{$_->{DIRECTION}})) {
383                         $self->pidl("r.in.$_->{NAME} = $_->{NAME};");
384                 }
385         }
386
387         $self->pidl("");
388         $self->pidl("status = cli->dispatch(cli,");
389         $self->pidl("\t\t\tmem_ctx,");
390         $self->pidl("\t\t\t&ndr_table_$if,");
391         $self->pidl("\t\t\t$ufn,");
392         $self->pidl("\t\t\t&r);");
393         $self->pidl("");
394
395         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
396         $self->indent;
397         $self->pidl("return status;");
398         $self->deindent;
399         $self->pidl("}");
400
401         $self->pidl("");
402         $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
403         $self->indent;
404         $self->pidl("return status;");
405         $self->deindent;
406         $self->pidl("}");
407         $self->pidl("");
408         $self->pidl("/* Return variables */");
409         foreach my $e (@{$fn->{ELEMENTS}}) {
410                 next unless (grep(/out/, @{$e->{DIRECTION}}));
411
412                 $self->ParseOutputArgument($fn, $e);
413
414         }
415
416         $self->pidl("");
417         $self->pidl("/* Return result */");
418         if (not $fn->{RETURN_TYPE}) {
419                 $self->pidl("return NT_STATUS_OK;");
420         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
421                 $self->pidl("return r.out.result;");
422         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
423                 $self->pidl("if (werror) {");
424                 $self->indent;
425                 $self->pidl("*werror = r.out.result;");
426                 $self->deindent;
427                 $self->pidl("}");
428                 $self->pidl("");
429                 $self->pidl("return werror_to_ntstatus(r.out.result);");
430         } else {
431                 warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS");
432                 $self->pidl("return NT_STATUS_OK;");
433         }
434
435         $self->deindent;
436         $self->pidl("}");
437         $self->pidl("");
438 }
439
440 sub ParseFunction($$$)
441 {
442         my ($self, $if, $fn) = @_;
443
444         $self->ParseFunctionAsyncState($if, $fn);
445         $self->ParseFunctionAsyncSend($if, $fn);
446         $self->ParseFunctionAsyncDone($if, $fn);
447         $self->ParseFunctionAsyncRecv($if, $fn);
448
449         $self->ParseFunctionSync($if, $fn);
450 }
451
452 sub ParseInterface($$)
453 {
454         my ($self, $if) = @_;
455
456         my $uif = uc($if->{NAME});
457
458         $self->pidl_hdr("#ifndef __CLI_$uif\__");
459         $self->pidl_hdr("#define __CLI_$uif\__");
460         foreach (@{$if->{FUNCTIONS}}) {
461                 next if ($_->{PROPERTIES}{noopnum});
462                 $self->ParseFunction($if->{NAME}, $_);
463         }
464         $self->pidl_hdr("#endif /* __CLI_$uif\__ */");
465 }
466
467 sub Parse($$$$)
468 {
469         my($self,$ndr,$header,$ndr_header) = @_;
470
471         $self->pidl("/*");
472         $self->pidl(" * Unix SMB/CIFS implementation.");
473         $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!");
474         $self->pidl(" */");
475         $self->pidl("");
476         $self->pidl("#include \"includes.h\"");
477         $self->pidl("#include \"$header\"");
478         $self->pidl_hdr("#include \"$ndr_header\"");
479         $self->pidl("");
480         
481         foreach (@$ndr) {
482                 $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
483         }
484
485         return ($self->{res}, $self->{res_hdr});
486 }
487
488 1;