pidl: add support for 'pipe' at the NDR layer
[kai/samba.git] / pidl / lib / Parse / Pidl / Samba4 / NDR / Client.pm
1 ###################################################
2 # client calls generator
3 # Copyright tridge@samba.org 2003
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
6
7 package Parse::Pidl::Samba4::NDR::Client;
8
9 use Exporter;
10 @ISA = qw(Exporter);
11 @EXPORT_OK = qw(Parse);
12
13 use Parse::Pidl qw(fatal warning error);
14 use Parse::Pidl::Util qw(has_property ParseExpr);
15 use Parse::Pidl::NDR qw(ContainsPipe);
16 use Parse::Pidl::Typelist qw(mapTypeName);
17 use Parse::Pidl::Samba4 qw(choose_header is_intree DeclLong);
18 use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv);
19
20 use vars qw($VERSION);
21 $VERSION = '0.01';
22
23 use strict;
24
25 sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
26 sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
27 sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; }
28 sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; }
29 sub pidl_both($$) { my ($self, $txt) = @_; $self->{hdr} .= "$txt\n"; $self->{res_hdr} .= "$txt\n"; }
30 sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }
31
32 sub genpad($)
33 {
34         my ($s) = @_;
35         my $nt = int((length($s)+1)/8);
36         my $lt = ($nt*8)-1;
37         my $ns = (length($s)-$lt);
38         return "\t"x($nt)." "x($ns);
39 }
40
41 sub new($)
42 {
43         my ($class) = shift;
44         my $self = { res => "", res_hdr => "", tabs => "" };
45         bless($self, $class);
46 }
47
48 sub ParseFunctionHasPipes($$)
49 {
50         my ($self, $fn) = @_;
51
52         foreach my $e (@{$fn->{ELEMENTS}}) {
53                 return 1 if ContainsPipe($e, $e->{LEVELS}[0]);
54         }
55
56         return 0;
57 }
58
59 sub ParseFunction_r_State($$$$)
60 {
61         my ($self, $if, $fn, $name) = @_;
62         my $uname = uc $name;
63
64         $self->pidl("struct dcerpc_$name\_r_state {");
65         $self->indent;
66         $self->pidl("TALLOC_CTX *out_mem_ctx;");
67         $self->deindent;
68         $self->pidl("};");
69         $self->pidl("");
70         $self->pidl("static void dcerpc_$name\_r_done(struct tevent_req *subreq);");
71         $self->pidl("");
72 }
73
74 sub ParseFunction_r_Send($$$$)
75 {
76         my ($self, $if, $fn, $name) = @_;
77         my $uname = uc $name;
78
79         my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n";
80         $proto   .= "\tstruct tevent_context *ev,\n",
81         $proto   .= "\tstruct dcerpc_binding_handle *h,\n",
82         $proto   .= "\tstruct $name *r)";
83
84         $self->fn_declare($proto);
85
86         $self->pidl("{");
87         $self->indent;
88
89         $self->pidl("struct tevent_req *req;");
90         $self->pidl("struct dcerpc_$name\_r_state *state;");
91         $self->pidl("struct tevent_req *subreq;");
92         $self->pidl("");
93
94         $self->pidl("req = tevent_req_create(mem_ctx, &state,");
95         $self->pidl("\t\t\tstruct dcerpc_$name\_r_state);");
96         $self->pidl("if (req == NULL) {");
97         $self->indent;
98         $self->pidl("return NULL;");
99         $self->deindent;
100         $self->pidl("}");
101         $self->pidl("");
102
103         my $out_params = 0;
104         foreach my $e (@{$fn->{ELEMENTS}}) {
105                 next unless grep(/out/, @{$e->{DIRECTION}});
106                 next if ContainsPipe($e, $e->{LEVELS}[0]);
107                 $out_params++;
108
109         }
110
111         my $submem;
112         if ($out_params > 0) {
113                 $self->pidl("state->out_mem_ctx = talloc_new(state);");
114                 $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
115                 $self->indent;
116                 $self->pidl("return tevent_req_post(req, ev);");
117                 $self->deindent;
118                 $self->pidl("}");
119                 $submem = "state->out_mem_ctx";
120         } else {
121                 $self->pidl("state->out_mem_ctx = NULL;");
122                 $submem = "state";
123         }
124         $self->pidl("");
125
126         $self->pidl("subreq = dcerpc_binding_handle_call_send(state, ev, h,");
127         $self->pidl("\t\tNULL, &ndr_table_$if->{NAME},");
128         $self->pidl("\t\tNDR_$uname, $submem, r);");
129         $self->pidl("if (tevent_req_nomem(subreq, req)) {");
130         $self->indent;
131         $self->pidl("return tevent_req_post(req, ev);");
132         $self->deindent;
133         $self->pidl("}");
134         $self->pidl("tevent_req_set_callback(subreq, dcerpc_$name\_r_done, req);");
135         $self->pidl("");
136
137         $self->pidl("return req;");
138         $self->deindent;
139         $self->pidl("}");
140         $self->pidl("");
141 }
142
143 sub ParseFunction_r_Done($$$$)
144 {
145         my ($self, $if, $fn, $name) = @_;
146         my $uname = uc $name;
147
148         my $proto = "static void dcerpc_$name\_r_done(struct tevent_req *subreq)";
149
150         $self->pidl("$proto");
151         $self->pidl("{");
152         $self->indent;
153
154         $self->pidl("struct tevent_req *req =");
155         $self->pidl("\ttevent_req_callback_data(subreq,");
156         $self->pidl("\tstruct tevent_req);");
157         $self->pidl("NTSTATUS status;");
158         $self->pidl("");
159
160         $self->pidl("status = dcerpc_binding_handle_call_recv(subreq);");
161         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
162         $self->indent;
163         $self->pidl("tevent_req_nterror(req, status);");
164         $self->pidl("return;");
165         $self->deindent;
166         $self->pidl("}");
167         $self->pidl("");
168
169         $self->pidl("tevent_req_done(req);");
170         $self->deindent;
171         $self->pidl("}");
172         $self->pidl("");
173 }
174
175 sub ParseFunction_r_Recv($$$$)
176 {
177         my ($self, $if, $fn, $name) = @_;
178         my $uname = uc $name;
179
180         my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)";
181
182         $self->fn_declare($proto);
183
184         $self->pidl("{");
185         $self->indent;
186
187         $self->pidl("struct dcerpc_$name\_r_state *state =");
188         $self->pidl("\ttevent_req_data(req,");
189         $self->pidl("\tstruct dcerpc_$name\_r_state);");
190         $self->pidl("NTSTATUS status;");
191         $self->pidl("");
192
193         $self->pidl("if (tevent_req_is_nterror(req, &status)) {");
194         $self->indent;
195         $self->pidl("tevent_req_received(req);");
196         $self->pidl("return status;");
197         $self->deindent;
198         $self->pidl("}");
199         $self->pidl("");
200
201         $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
202         $self->pidl("");
203
204         $self->pidl("tevent_req_received(req);");
205         $self->pidl("return NT_STATUS_OK;");
206         $self->deindent;
207         $self->pidl("}");
208         $self->pidl("");
209 }
210
211 sub ParseFunction_r_Sync($$$$)
212 {
213         my ($self, $if, $fn, $name) = @_;
214         my $uname = uc $name;
215
216         if ($self->ParseFunctionHasPipes($fn)) {
217                 $self->pidl_both("/*");
218                 $self->pidl_both(" * The following function is skipped because");
219                 $self->pidl_both(" * it uses pipes:");
220                 $self->pidl_both(" *");
221                 $self->pidl_both(" * dcerpc_$name\_r()");
222                 $self->pidl_both(" */");
223                 $self->pidl_both("");
224                 return;
225         }
226
227         my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)";
228
229         $self->fn_declare($proto);
230
231         $self->pidl("{");
232         $self->indent;
233         $self->pidl("NTSTATUS status;");
234         $self->pidl("");
235
236         $self->pidl("status = dcerpc_binding_handle_call(h,");
237         $self->pidl("\t\tNULL, &ndr_table_$if->{NAME},");
238         $self->pidl("\t\tNDR_$uname, mem_ctx, r);");
239         $self->pidl("");
240         $self->pidl("return status;");
241
242         $self->deindent;
243         $self->pidl("}");
244         $self->pidl("");
245 }
246
247 sub ElementDirection($)
248 {
249         my ($e) = @_;
250
251         return "[in,out]" if (has_property($e, "in") and has_property($e, "out"));
252         return "[in]" if (has_property($e, "in"));
253         return "[out]" if (has_property($e, "out"));
254         return "[in,out]";
255 }
256
257 sub HeaderProperties($$)
258 {
259         my($props,$ignores) = @_;
260         my $ret = "";
261
262         foreach my $d (keys %{$props}) {
263                 next if (grep(/^$d$/, @$ignores));
264                 if($props->{$d} ne "1") {
265                         $ret.= "$d($props->{$d}),";
266                 } else {
267                         $ret.="$d,";
268                 }
269         }
270
271         if ($ret) {
272                 return "[" . substr($ret, 0, -1) . "]";
273         }
274 }
275
276 sub ParseCopyArgument($$$$$)
277 {
278         my ($self, $fn, $e, $r, $i) = @_;
279         my $l = $e->{LEVELS}[0];
280
281         if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED} == 1) {
282                 $self->pidl("memcpy(${r}$e->{NAME}, ${i}$e->{NAME}, sizeof(${r}$e->{NAME}));");
283         } else {
284                 $self->pidl("${r}$e->{NAME} = ${i}$e->{NAME};");
285         }
286 }
287
288 sub ParseInvalidResponse($$)
289 {
290         my ($self, $type) = @_;
291
292         if ($type eq "sync") {
293                 $self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;");
294         } elsif ($type eq "async") {
295                 $self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);");
296                 $self->pidl("return;");
297         } else {
298                 die("ParseInvalidResponse($type)");
299         }
300 }
301
302 sub ParseOutputArgument($$$$$$)
303 {
304         my ($self, $fn, $e, $r, $o, $invalid_response_type) = @_;
305         my $level = 0;
306
307         if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY") {
308                 fatal($e->{ORIGINAL}, "[out] argument is not a pointer or array");
309                 return;
310         }
311
312         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
313                 $level = 1;
314                 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
315                         $self->pidl("if ($o$e->{NAME} && ${r}out.$e->{NAME}) {");
316                         $self->indent;
317                 }
318         }
319
320         if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
321                 # This is a call to GenerateFunctionInEnv intentionally.
322                 # Since the data is being copied into a user-provided data
323                 # structure, the user should be able to know the size beforehand
324                 # to allocate a structure of the right size.
325                 my $in_env = GenerateFunctionInEnv($fn, $r);
326                 my $out_env = GenerateFunctionOutEnv($fn, $r);
327                 my $l = $e->{LEVELS}[$level];
328
329                 my $in_var = undef;
330                 if (grep(/in/, @{$e->{DIRECTION}})) {
331                         $in_var = ParseExpr($e->{NAME}, $in_env, $e->{ORIGINAL});
332                 }
333                 my $out_var = ParseExpr($e->{NAME}, $out_env, $e->{ORIGINAL});
334
335                 my $in_size_is = undef;
336                 my $out_size_is = undef;
337                 my $out_length_is = undef;
338
339                 my $avail_len = undef;
340                 my $needed_len = undef;
341
342                 $self->pidl("{");
343                 $self->indent;
344                 my $copy_len_var = "_copy_len_$e->{NAME}";
345                 $self->pidl("size_t $copy_len_var;");
346
347                 if (not defined($l->{SIZE_IS})) {
348                         if (not $l->{IS_ZERO_TERMINATED}) {
349                                 fatal($e->{ORIGINAL}, "no size known for [out] array `$e->{NAME}'");
350                         }
351                         if (has_property($e, "charset")) {
352                                 $avail_len = "ndr_charset_length($in_var, CH_UNIX)";
353                                 $needed_len = "ndr_charset_length($out_var, CH_UNIX)";
354                         } else {
355                                 $avail_len = "ndr_string_length($in_var, sizeof(*$in_var))";
356                                 $needed_len = "ndr_string_length($out_var, sizeof(*$out_var))";
357                         }
358                         $in_size_is = "";
359                         $out_size_is = "";
360                         $out_length_is = "";
361                 } else {
362                         $in_size_is = ParseExpr($l->{SIZE_IS}, $in_env, $e->{ORIGINAL});
363                         $out_size_is = ParseExpr($l->{SIZE_IS}, $out_env, $e->{ORIGINAL});
364                         $out_length_is = $out_size_is;
365                         if (defined($l->{LENGTH_IS})) {
366                                 $out_length_is = ParseExpr($l->{LENGTH_IS}, $out_env, $e->{ORIGINAL});
367                         }
368                         if (has_property($e, "charset")) {
369                                 if (defined($in_var)) {
370                                         $avail_len = "ndr_charset_length($in_var, CH_UNIX)";
371                                 } else {
372                                         $avail_len = $out_length_is;
373                                 }
374                                 $needed_len = "ndr_charset_length($out_var, CH_UNIX)";
375                         }
376                 }
377
378                 if ($out_size_is ne $in_size_is) {
379                         $self->pidl("if (($out_size_is) > ($in_size_is)) {");
380                         $self->indent;
381                         $self->ParseInvalidResponse($invalid_response_type);
382                         $self->deindent;
383                         $self->pidl("}");
384                 }
385                 if ($out_length_is ne $out_size_is) {
386                         $self->pidl("if (($out_length_is) > ($out_size_is)) {");
387                         $self->indent;
388                         $self->ParseInvalidResponse($invalid_response_type);
389                         $self->deindent;
390                         $self->pidl("}");
391                 }
392                 if (defined($needed_len)) {
393                         $self->pidl("$copy_len_var = $needed_len;");
394                         $self->pidl("if ($copy_len_var > $avail_len) {");
395                         $self->indent;
396                         $self->ParseInvalidResponse($invalid_response_type);
397                         $self->deindent;
398                         $self->pidl("}");
399                 } else {
400                         $self->pidl("$copy_len_var = $out_length_is;");
401                 }
402
403                 if (has_property($e, "charset")) {
404                         $self->pidl("memcpy(discard_const_p(uint8_t *, $o$e->{NAME}), $out_var, $copy_len_var * sizeof(*$o$e->{NAME}));");
405                 } else {
406                         $self->pidl("memcpy($o$e->{NAME}, $out_var, $copy_len_var * sizeof(*$o$e->{NAME}));");
407                 }
408
409                 $self->deindent;
410                 $self->pidl("}");
411         } else {
412                 $self->pidl("*$o$e->{NAME} = *${r}out.$e->{NAME};");
413         }
414
415         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
416                 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
417                         $self->deindent;
418                         $self->pidl("}");
419                 }
420         }
421 }
422
423 sub ParseFunction_State($$$$)
424 {
425         my ($self, $if, $fn, $name) = @_;
426
427         my $state_str = "struct dcerpc_$name\_state";
428         my $done_fn = "dcerpc_$name\_done";
429
430         $self->pidl("$state_str {");
431         $self->indent;
432         $self->pidl("struct $name orig;");
433         $self->pidl("struct $name tmp;");
434         $self->pidl("TALLOC_CTX *out_mem_ctx;");
435         $self->deindent;
436         $self->pidl("};");
437         $self->pidl("");
438         $self->pidl("static void $done_fn(struct tevent_req *subreq);");
439         $self->pidl("");
440 }
441
442 sub ParseFunction_Send($$$$)
443 {
444         my ($self, $if, $fn, $name) = @_;
445
446         my $fn_args = "";
447         my $state_str = "struct dcerpc_$name\_state";
448         my $done_fn = "dcerpc_$name\_done";
449         my $out_mem_ctx = "dcerpc_$name\_out_memory";
450         my $fn_str = "struct tevent_req *dcerpc_$name\_send";
451         my $pad = genpad($fn_str);
452
453         $fn_args .= "TALLOC_CTX *mem_ctx";
454         $fn_args .= ",\n" . $pad . "struct tevent_context *ev";
455         $fn_args .= ",\n" . $pad . "struct dcerpc_binding_handle *h";
456
457         foreach (@{$fn->{ELEMENTS}}) {
458                 my $dir = ElementDirection($_);
459                 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
460                 $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */";
461         }
462
463         $self->fn_declare("$fn_str($fn_args)");
464         $self->pidl("{");
465         $self->indent;
466         $self->pidl("struct tevent_req *req;");
467         $self->pidl("$state_str *state;");
468         $self->pidl("struct tevent_req *subreq;");
469         $self->pidl("");
470         $self->pidl("req = tevent_req_create(mem_ctx, &state,");
471         $self->pidl("\t\t\t$state_str);");
472         $self->pidl("if (req == NULL) {");
473         $self->indent;
474         $self->pidl("return NULL;");
475         $self->deindent;
476         $self->pidl("}");
477         $self->pidl("state->out_mem_ctx = NULL;");
478         $self->pidl("");
479
480         $self->pidl("/* In parameters */");
481         foreach my $e (@{$fn->{ELEMENTS}}) {
482                 next unless (grep(/in/, @{$e->{DIRECTION}}));
483
484                 $self->ParseCopyArgument($fn, $e, "state->orig.in.", "_");
485         }
486         $self->pidl("");
487
488         my $out_params = 0;
489         $self->pidl("/* Out parameters */");
490         foreach my $e (@{$fn->{ELEMENTS}}) {
491                 next unless grep(/out/, @{$e->{DIRECTION}});
492
493                 $self->ParseCopyArgument($fn, $e, "state->orig.out.", "_");
494
495                 next if ContainsPipe($e, $e->{LEVELS}[0]);
496
497                 $out_params++;
498         }
499         $self->pidl("");
500
501         if (defined($fn->{RETURN_TYPE})) {
502                 $self->pidl("/* Result */");
503                 $self->pidl("ZERO_STRUCT(state->orig.out.result);");
504                 $self->pidl("");
505         }
506
507         if ($out_params > 0) {
508                 $self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,");
509                 $self->pidl("\t\t     \"$out_mem_ctx\");");
510                 $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
511                 $self->indent;
512                 $self->pidl("return tevent_req_post(req, ev);");
513                 $self->deindent;
514                 $self->pidl("}");
515                 $self->pidl("");
516         }
517
518         $self->pidl("/* make a temporary copy, that we pass to the dispatch function */");
519         $self->pidl("state->tmp = state->orig;");
520         $self->pidl("");
521
522         $self->pidl("subreq = dcerpc_$name\_r_send(state, ev, h, &state->tmp);");
523         $self->pidl("if (tevent_req_nomem(subreq, req)) {");
524         $self->indent;
525         $self->pidl("return tevent_req_post(req, ev);");
526         $self->deindent;
527         $self->pidl("}");
528         $self->pidl("tevent_req_set_callback(subreq, $done_fn, req);");
529         $self->pidl("return req;");
530         $self->deindent;
531         $self->pidl("}");
532         $self->pidl("");
533 }
534
535 sub ParseFunction_Done($$$$)
536 {
537         my ($self, $if, $fn, $name) = @_;
538
539         my $state_str = "struct dcerpc_$name\_state";
540         my $done_fn = "dcerpc_$name\_done";
541
542         $self->pidl("static void $done_fn(struct tevent_req *subreq)");
543         $self->pidl("{");
544         $self->indent;
545         $self->pidl("struct tevent_req *req = tevent_req_callback_data(");
546         $self->pidl("\tsubreq, struct tevent_req);");
547         $self->pidl("$state_str *state = tevent_req_data(");
548         $self->pidl("\treq, $state_str);");
549         $self->pidl("NTSTATUS status;");
550         $self->pidl("TALLOC_CTX *mem_ctx;");
551         $self->pidl("");
552
553         $self->pidl("if (state->out_mem_ctx) {");
554         $self->indent;
555         $self->pidl("mem_ctx = state->out_mem_ctx;");
556         $self->deindent;
557         $self->pidl("} else {");
558         $self->indent;
559         $self->pidl("mem_ctx = state;");
560         $self->deindent;
561         $self->pidl("}");
562         $self->pidl("");
563
564         $self->pidl("status = dcerpc_$name\_r_recv(subreq, mem_ctx);");
565         $self->pidl("TALLOC_FREE(subreq);");
566         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
567         $self->indent;
568         $self->pidl("tevent_req_nterror(req, status);");
569         $self->pidl("return;");
570         $self->deindent;
571         $self->pidl("}");
572         $self->pidl("");
573
574         $self->pidl("/* Copy out parameters */");
575         foreach my $e (@{$fn->{ELEMENTS}}) {
576                 next if ContainsPipe($e, $e->{LEVELS}[0]);
577                 next unless (grep(/out/, @{$e->{DIRECTION}}));
578
579                 $self->ParseOutputArgument($fn, $e,
580                                            "state->tmp.",
581                                            "state->orig.out.",
582                                            "async");
583         }
584         $self->pidl("");
585
586         if (defined($fn->{RETURN_TYPE})) {
587                 $self->pidl("/* Copy result */");
588                 $self->pidl("state->orig.out.result = state->tmp.out.result;");
589                 $self->pidl("");
590         }
591
592         $self->pidl("/* Reset temporary structure */");
593         $self->pidl("ZERO_STRUCT(state->tmp);");
594         $self->pidl("");
595
596         $self->pidl("tevent_req_done(req);");
597         $self->deindent;
598         $self->pidl("}");
599         $self->pidl("");
600 }
601
602 sub ParseFunction_Recv($$$$)
603 {
604         my ($self, $if, $fn, $name) = @_;
605
606         my $fn_args = "";
607         my $state_str = "struct dcerpc_$name\_state";
608         my $fn_str = "NTSTATUS dcerpc_$name\_recv";
609         my $pad = genpad($fn_str);
610
611         $fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx";
612
613         if (defined($fn->{RETURN_TYPE})) {
614                 $fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result";
615         }
616
617         $self->fn_declare("$fn_str($fn_args)");
618         $self->pidl("{");
619         $self->indent;
620         $self->pidl("$state_str *state = tevent_req_data(");
621         $self->pidl("\treq, $state_str);");
622         $self->pidl("NTSTATUS status;");
623         $self->pidl("");
624         $self->pidl("if (tevent_req_is_nterror(req, &status)) {");
625         $self->indent;
626         $self->pidl("tevent_req_received(req);");
627         $self->pidl("return status;");
628         $self->deindent;
629         $self->pidl("}");
630         $self->pidl("");
631
632         $self->pidl("/* Steal possible out parameters to the callers context */");
633         $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
634         $self->pidl("");
635
636         if (defined($fn->{RETURN_TYPE})) {
637                 $self->pidl("/* Return result */");
638                 $self->pidl("*result = state->orig.out.result;");
639                 $self->pidl("");
640         }
641
642         $self->pidl("tevent_req_received(req);");
643         $self->pidl("return NT_STATUS_OK;");
644         $self->deindent;
645         $self->pidl("}");
646         $self->pidl("");
647 }
648
649 sub ParseFunction_Sync($$$$)
650 {
651         my ($self, $if, $fn, $name) = @_;
652
653         if ($self->ParseFunctionHasPipes($fn)) {
654                 $self->pidl_both("/*");
655                 $self->pidl_both(" * The following function is skipped because");
656                 $self->pidl_both(" * it uses pipes:");
657                 $self->pidl_both(" *");
658                 $self->pidl_both(" * dcerpc_$name()");
659                 $self->pidl_both(" */");
660                 $self->pidl_both("");
661                 return;
662         }
663
664         my $uname = uc $name;
665         my $fn_args = "";
666         my $fn_str = "NTSTATUS dcerpc_$name";
667         my $pad = genpad($fn_str);
668
669         $fn_args .= "struct dcerpc_binding_handle *h,\n" . $pad . "TALLOC_CTX *mem_ctx";
670
671         foreach (@{$fn->{ELEMENTS}}) {
672                 my $dir = ElementDirection($_);
673                 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
674                 $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */";
675         }
676
677         if (defined($fn->{RETURN_TYPE})) {
678                 $fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result";
679         }
680
681         $self->fn_declare("$fn_str($fn_args)");
682         $self->pidl("{");
683         $self->indent;
684         $self->pidl("struct $name r;");
685         $self->pidl("NTSTATUS status;");
686         $self->pidl("");
687
688         $self->pidl("/* In parameters */");
689         foreach my $e (@{$fn->{ELEMENTS}}) {
690                 next unless (grep(/in/, @{$e->{DIRECTION}}));
691
692                 $self->ParseCopyArgument($fn, $e, "r.in.", "_");
693         }
694         $self->pidl("");
695
696         $self->pidl("status = dcerpc_$name\_r(h, mem_ctx, &r);");
697         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
698         $self->indent;
699         $self->pidl("return status;");
700         $self->deindent;
701         $self->pidl("}");
702         $self->pidl("");
703
704         $self->pidl("/* Return variables */");
705         foreach my $e (@{$fn->{ELEMENTS}}) {
706                 next if ContainsPipe($e, $e->{LEVELS}[0]);
707                 next unless (grep(/out/, @{$e->{DIRECTION}}));
708
709                 $self->ParseOutputArgument($fn, $e, "r.", "_", "sync");
710         }
711         $self->pidl("");
712
713         $self->pidl("/* Return result */");
714         if ($fn->{RETURN_TYPE}) {
715                 $self->pidl("*result = r.out.result;");
716         }
717         $self->pidl("");
718
719         $self->pidl("return NT_STATUS_OK;");
720
721         $self->deindent;
722         $self->pidl("}");
723         $self->pidl("");
724 }
725
726 #####################################################################
727 # parse a function
728 sub ParseFunction($$$)
729 {
730         my ($self, $if, $fn) = @_;
731
732         if ($self->ParseFunctionHasPipes($fn)) {
733                 $self->pidl_both("/*");
734                 $self->pidl_both(" * The following function is skipped because");
735                 $self->pidl_both(" * it uses pipes:");
736                 $self->pidl_both(" *");
737                 $self->pidl_both(" * dcerpc_$fn->{NAME}_r_send()");
738                 $self->pidl_both(" * dcerpc_$fn->{NAME}_r_recv()");
739                 $self->pidl_both(" * dcerpc_$fn->{NAME}_r()");
740                 $self->pidl_both(" *");
741                 $self->pidl_both(" * dcerpc_$fn->{NAME}_send()");
742                 $self->pidl_both(" * dcerpc_$fn->{NAME}_recv()");
743                 $self->pidl_both(" * dcerpc_$fn->{NAME}()");
744                 $self->pidl_both(" */");
745                 $self->pidl_both("");
746                 warning($fn, "$fn->{NAME}: dcerpc client does not support pipe yet");
747                 return;
748         }
749
750         $self->ParseFunction_r_State($if, $fn, $fn->{NAME});
751         $self->ParseFunction_r_Send($if, $fn, $fn->{NAME});
752         $self->ParseFunction_r_Done($if, $fn, $fn->{NAME});
753         $self->ParseFunction_r_Recv($if, $fn, $fn->{NAME});
754         $self->ParseFunction_r_Sync($if, $fn, $fn->{NAME});
755
756         foreach my $e (@{$fn->{ELEMENTS}}) {
757                 next unless (grep(/out/, @{$e->{DIRECTION}}));
758
759                 my $reason = "is not a pointer or array";
760
761                 # TODO: make this fatal at NDR level
762                 if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
763                         if ($e->{LEVELS}[1]->{TYPE} eq "DATA" and
764                             $e->{LEVELS}[1]->{DATA_TYPE} eq "string") {
765                                 $reason = "is a pointer to type 'string'";
766                         } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and
767                                  $e->{LEVELS}[1]->{IS_ZERO_TERMINATED}) {
768                                 next;
769                         } elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and
770                                  not defined($e->{LEVELS}[1]->{SIZE_IS})) {
771                                 $reason = "is a pointer to an unsized array";
772                         } else {
773                                 next;
774                         }
775                 }
776                 if ($e->{LEVELS}[0]->{TYPE} eq "ARRAY") {
777                         if (not defined($e->{LEVELS}[0]->{SIZE_IS})) {
778                                 $reason = "is an unsized array";
779                         } else {
780                                 next;
781                         }
782                 }
783
784                 $self->pidl_both("/*");
785                 $self->pidl_both(" * The following functions are skipped because");
786                 $self->pidl_both(" * an [out] argument $e->{NAME} $reason:");
787                 $self->pidl_both(" *");
788                 $self->pidl_both(" * dcerpc_$fn->{NAME}_send()");
789                 $self->pidl_both(" * dcerpc_$fn->{NAME}_recv()");
790                 $self->pidl_both(" * dcerpc_$fn->{NAME}()");
791                 $self->pidl_both(" */");
792                 $self->pidl_both("");
793
794                 error($e->{ORIGINAL}, "$fn->{NAME}: [out] argument '$e->{NAME}' $reason, skip client functions");
795                 return;
796         }
797
798         $self->ParseFunction_State($if, $fn, $fn->{NAME});
799         $self->ParseFunction_Send($if, $fn, $fn->{NAME});
800         $self->ParseFunction_Done($if, $fn, $fn->{NAME});
801         $self->ParseFunction_Recv($if, $fn, $fn->{NAME});
802         $self->ParseFunction_Sync($if, $fn, $fn->{NAME});
803
804         $self->pidl_hdr("");
805 }
806
807 my %done;
808
809 #####################################################################
810 # parse the interface definitions
811 sub ParseInterface($$)
812 {
813         my ($self, $if) = @_;
814         my $ifu = uc($if->{NAME});
815
816         $self->pidl_hdr("#ifndef _HEADER_RPC_$if->{NAME}");
817         $self->pidl_hdr("#define _HEADER_RPC_$if->{NAME}");
818         $self->pidl_hdr("");
819
820         if (defined $if->{PROPERTIES}->{uuid}) {
821                 $self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$if->{NAME};");
822                 $self->pidl_hdr("");
823         }
824
825         $self->pidl("/* $if->{NAME} - client functions generated by pidl */");
826         $self->pidl("");
827
828         foreach my $fn (@{$if->{FUNCTIONS}}) {
829                 next if defined($done{$fn->{NAME}});
830                 next if has_property($fn, "noopnum");
831                 next if has_property($fn, "todo");
832                 $self->ParseFunction($if, $fn);
833                 $done{$fn->{NAME}} = 1;
834         }
835
836         $self->pidl_hdr("#endif /* _HEADER_RPC_$if->{NAME} */");
837 }
838
839 sub Parse($$$$$$)
840 {
841         my($self,$ndr,$header,$ndr_header,$client_header) = @_;
842
843         $self->pidl("/* client functions auto-generated by pidl */");
844         $self->pidl("");
845         if (is_intree()) {
846                 $self->pidl("#include \"includes.h\"");
847         } else {
848                 $self->pidl("#ifndef _GNU_SOURCE");
849                 $self->pidl("#define _GNU_SOURCE");
850                 $self->pidl("#endif");
851                 $self->pidl("#include <stdio.h>");
852                 $self->pidl("#include <stdbool.h>");
853                 $self->pidl("#include <stdlib.h>");
854                 $self->pidl("#include <stdint.h>");
855                 $self->pidl("#include <stdarg.h>");
856                 $self->pidl("#include <string.h>");
857                 $self->pidl("#include <core/ntstatus.h>");
858         }
859         $self->pidl("#include <tevent.h>");
860         $self->pidl(choose_header("lib/util/tevent_ntstatus.h", "util/tevent_ntstatus.h")."");
861         $self->pidl("#include \"$ndr_header\"");
862         $self->pidl("#include \"$client_header\"");
863         $self->pidl("");
864
865         $self->pidl_hdr(choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."");
866         $self->pidl_hdr("#include \"$header\"");
867
868         foreach my $x (@{$ndr}) {
869                 ($x->{TYPE} eq "INTERFACE") && $self->ParseInterface($x);
870         }
871
872         return ($self->{res},$self->{res_hdr});
873 }
874
875 1;