cb6c1f252a315df1e26a8c36d245488dc6636963
[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 GenerateFunctionOutEnv);
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 ParseInvalidResponse($$)
75 {
76         my ($self, $type) = @_;
77
78         if ($type eq "sync") {
79                 $self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;");
80         } elsif ($type eq "async") {
81                 $self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);");
82                 $self->pidl("return;");
83         } else {
84                 die("ParseInvalidResponse($type)");
85         }
86 }
87
88 sub ParseOutputArgument($$$;$$$)
89 {
90         my ($self, $fn, $e, $r, $o, $invalid_response_type) = @_;
91         my $level = 0;
92         $r = "r." unless defined($r);
93         $o = "" unless defined($o);
94         $invalid_response_type = "sync" unless defined($invalid_response_type);
95
96         if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY") {
97                 $self->pidl("return NT_STATUS_NOT_SUPPORTED;");
98                 error($e->{ORIGINAL}, "[out] argument is not a pointer or array");
99                 return;
100         }
101
102         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
103                 $level = 1;
104                 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
105                         $self->pidl("if ($o$e->{NAME} && ${r}out.$e->{NAME}) {");
106                         $self->indent;
107                 }
108         }
109
110         if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") {
111                 # This is a call to GenerateFunctionInEnv intentionally. 
112                 # Since the data is being copied into a user-provided data 
113                 # structure, the user should be able to know the size beforehand 
114                 # to allocate a structure of the right size.
115                 my $in_env = GenerateFunctionInEnv($fn, $r);
116                 my $out_env = GenerateFunctionOutEnv($fn, $r);
117                 my $l = $e->{LEVELS}[$level];
118                 unless (defined($l->{SIZE_IS})) {
119                         $self->pidl('#error No size known for [out] array `$e->{NAME}');
120                         error($e->{ORIGINAL}, "no size known for [out] array `$e->{NAME}'");
121                 } else {
122                         my $in_size_is = ParseExpr($l->{SIZE_IS}, $in_env, $e->{ORIGINAL});
123                         my $out_size_is = ParseExpr($l->{SIZE_IS}, $out_env, $e->{ORIGINAL});
124                         my $out_length_is = $out_size_is;
125                         if (defined($l->{LENGTH_IS})) {
126                                 $out_length_is = ParseExpr($l->{LENGTH_IS}, $out_env, $e->{ORIGINAL});
127                         }
128                         if ($out_size_is ne $in_size_is) {
129                                 $self->pidl("if (($out_size_is) > ($in_size_is)) {");
130                                 $self->indent;
131                                 $self->ParseInvalidResponse($invalid_response_type);
132                                 $self->deindent;
133                                 $self->pidl("}");
134                         }
135                         if ($out_length_is ne $out_size_is) {
136                                 $self->pidl("if (($out_length_is) > ($out_size_is)) {");
137                                 $self->indent;
138                                 $self->ParseInvalidResponse($invalid_response_type);
139                                 $self->deindent;
140                                 $self->pidl("}");
141                         }
142                         if (has_property($e, "charset")) {
143                                 $self->pidl("memcpy(discard_const_p(uint8_t *, $o$e->{NAME}), ${r}out.$e->{NAME}, ($out_length_is) * sizeof(*$o$e->{NAME}));");
144                         } else {
145                                 $self->pidl("memcpy($o$e->{NAME}, ${r}out.$e->{NAME}, ($out_length_is) * sizeof(*$o$e->{NAME}));");
146                         }
147                 }
148         } else {
149                 $self->pidl("*$o$e->{NAME} = *${r}out.$e->{NAME};");
150         }
151
152         if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") {
153                 if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") {
154                         $self->deindent;
155                         $self->pidl("}");
156                 }
157         }
158 }
159
160 sub ParseFunctionAsyncState($$$)
161 {
162         my ($self, $if, $fn) = @_;
163
164         my $state_str = "struct rpccli_$fn->{NAME}_state";
165         my $done_fn = "rpccli_$fn->{NAME}_done";
166
167         $self->pidl("$state_str {");
168         $self->indent;
169         $self->pidl("struct $fn->{NAME} orig;");
170         $self->pidl("struct $fn->{NAME} tmp;");
171         $self->pidl("TALLOC_CTX *out_mem_ctx;");
172         $self->pidl("NTSTATUS (*dispatch_recv)(struct tevent_req *req, TALLOC_CTX *mem_ctx);");
173         $self->deindent;
174         $self->pidl("};");
175         $self->pidl("");
176         $self->pidl("static void $done_fn(struct tevent_req *subreq);");
177         $self->pidl("");
178 }
179
180 sub ParseFunctionAsyncSend($$$)
181 {
182         my ($self, $if, $fn) = @_;
183
184         my $fn_args = "";
185         my $uif = uc($if);
186         my $ufn = "NDR_".uc($fn->{NAME});
187         my $state_str = "struct rpccli_$fn->{NAME}_state";
188         my $done_fn = "rpccli_$fn->{NAME}_done";
189         my $out_mem_ctx = "rpccli_$fn->{NAME}_out_memory";
190         my $fn_str = "struct tevent_req *rpccli_$fn->{NAME}_send";
191         my $pad = genpad($fn_str);
192
193         $fn_args .= "TALLOC_CTX *mem_ctx";
194         $fn_args .= ",\n" . $pad . "struct tevent_context *ev";
195         $fn_args .= ",\n" . $pad . "struct rpc_pipe_client *cli";
196
197         foreach (@{$fn->{ELEMENTS}}) {
198                 my $dir = ElementDirection($_);
199                 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
200                 $fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */";
201         }
202
203         $self->fn_declare("$fn_str($fn_args)");
204         $self->pidl("{");
205         $self->indent;
206         $self->pidl("struct tevent_req *req;");
207         $self->pidl("$state_str *state;");
208         $self->pidl("struct tevent_req *subreq;");
209         $self->pidl("");
210         $self->pidl("req = tevent_req_create(mem_ctx, &state,");
211         $self->pidl("\t\t\t$state_str);");
212         $self->pidl("if (req == NULL) {");
213         $self->indent;
214         $self->pidl("return NULL;");
215         $self->deindent;
216         $self->pidl("}");
217         $self->pidl("state->out_mem_ctx = NULL;");
218         $self->pidl("state->dispatch_recv = cli->dispatch_recv;");
219         $self->pidl("");
220
221         $self->pidl("/* In parameters */");
222         foreach (@{$fn->{ELEMENTS}}) {
223                 if (grep(/in/, @{$_->{DIRECTION}})) {
224                         $self->pidl("state->orig.in.$_->{NAME} = _$_->{NAME};");
225                 }
226         }
227         $self->pidl("");
228
229         my $out_params = 0;
230         $self->pidl("/* Out parameters */");
231         foreach (@{$fn->{ELEMENTS}}) {
232                 if (grep(/out/, @{$_->{DIRECTION}})) {
233                         $self->pidl("state->orig.out.$_->{NAME} = _$_->{NAME};");
234                         $out_params++;
235                 }
236         }
237         $self->pidl("");
238
239         if (defined($fn->{RETURN_TYPE})) {
240                 $self->pidl("/* Result */");
241                 $self->pidl("ZERO_STRUCT(state->orig.out.result);");
242                 $self->pidl("");
243         }
244
245         if ($out_params > 0) {
246                 $self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,");
247                 $self->pidl("\t\t     \"$out_mem_ctx\");");
248                 $self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
249                 $self->indent;
250                 $self->pidl("return tevent_req_post(req, ev);");
251                 $self->deindent;
252                 $self->pidl("}");
253                 $self->pidl("");
254         }
255
256         $self->pidl("/* make a temporary copy, that we pass to the dispatch function */");
257         $self->pidl("state->tmp = state->orig;");
258         $self->pidl("");
259
260         $self->pidl("subreq = cli->dispatch_send(state, ev, cli,");
261         $self->pidl("\t\t\t    &ndr_table_$if,");
262         $self->pidl("\t\t\t    $ufn,");
263         $self->pidl("\t\t\t    &state->tmp);");
264         $self->pidl("if (tevent_req_nomem(subreq, req)) {");
265         $self->indent;
266         $self->pidl("return tevent_req_post(req, ev);");
267         $self->deindent;
268         $self->pidl("}");
269         $self->pidl("tevent_req_set_callback(subreq, $done_fn, req);");
270         $self->pidl("return req;");
271         $self->deindent;
272         $self->pidl("}");
273         $self->pidl("");
274 }
275
276 sub ParseFunctionAsyncDone($$$)
277 {
278         my ($self, $if, $fn) = @_;
279
280         my $state_str = "struct rpccli_$fn->{NAME}_state";
281         my $done_fn = "rpccli_$fn->{NAME}_done";
282
283         $self->pidl("static void $done_fn(struct tevent_req *subreq)");
284         $self->pidl("{");
285         $self->indent;
286         $self->pidl("struct tevent_req *req = tevent_req_callback_data(");
287         $self->pidl("\tsubreq, struct tevent_req);");
288         $self->pidl("$state_str *state = tevent_req_data(");
289         $self->pidl("\treq, $state_str);");
290         $self->pidl("NTSTATUS status;");
291         $self->pidl("TALLOC_CTX *mem_ctx;");
292         $self->pidl("");
293
294         $self->pidl("if (state->out_mem_ctx) {");
295         $self->indent;
296         $self->pidl("mem_ctx = state->out_mem_ctx;");
297         $self->deindent;
298         $self->pidl("} else {");
299         $self->indent;
300         $self->pidl("mem_ctx = state;");
301         $self->deindent;
302         $self->pidl("}");
303         $self->pidl("");
304
305         $self->pidl("status = state->dispatch_recv(subreq, mem_ctx);");
306         $self->pidl("TALLOC_FREE(subreq);");
307         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
308         $self->indent;
309         $self->pidl("tevent_req_nterror(req, status);");
310         $self->pidl("return;");
311         $self->deindent;
312         $self->pidl("}");
313         $self->pidl("");
314
315         $self->pidl("/* Copy out parameters */");
316         foreach my $e (@{$fn->{ELEMENTS}}) {
317                 next unless (grep(/out/, @{$e->{DIRECTION}}));
318
319                 $self->ParseOutputArgument($fn, $e,
320                                            "state->tmp.",
321                                            "state->orig.out.",
322                                            "async");
323         }
324         $self->pidl("");
325
326         if (defined($fn->{RETURN_TYPE})) {
327                 $self->pidl("/* Copy result */");
328                 $self->pidl("state->orig.out.result = state->tmp.out.result;");
329                 $self->pidl("");
330         }
331
332         $self->pidl("/* Reset temporary structure */");
333         $self->pidl("ZERO_STRUCT(state->tmp);");
334         $self->pidl("");
335
336         $self->pidl("tevent_req_done(req);");
337         $self->deindent;
338         $self->pidl("}");
339         $self->pidl("");
340 }
341
342 sub ParseFunctionAsyncRecv($$$)
343 {
344         my ($self, $if, $fn) = @_;
345
346         my $fn_args = "";
347         my $state_str = "struct rpccli_$fn->{NAME}_state";
348         my $fn_str = "NTSTATUS rpccli_$fn->{NAME}_recv";
349         my $pad = genpad($fn_str);
350
351         $fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx";
352
353         if (defined($fn->{RETURN_TYPE})) {
354                 $fn_args .= ",\n" . $pad . "$fn->{RETURN_TYPE} *result";
355         }
356
357         $self->fn_declare("$fn_str($fn_args)");
358         $self->pidl("{");
359         $self->indent;
360         $self->pidl("$state_str *state = tevent_req_data(");
361         $self->pidl("\treq, $state_str);");
362         $self->pidl("NTSTATUS status;");
363         $self->pidl("");
364         $self->pidl("if (tevent_req_is_nterror(req, &status)) {");
365         $self->indent;
366         $self->pidl("tevent_req_received(req);");
367         $self->pidl("return status;");
368         $self->deindent;
369         $self->pidl("}");
370         $self->pidl("");
371
372         $self->pidl("/* Steal possbile out parameters to the callers context */");
373         $self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
374         $self->pidl("");
375
376         if (defined($fn->{RETURN_TYPE})) {
377                 $self->pidl("/* Return result */");
378                 $self->pidl("*result = state->orig.out.result;");
379                 $self->pidl("");
380         }
381
382         $self->pidl("tevent_req_received(req);");
383         $self->pidl("return NT_STATUS_OK;");
384         $self->deindent;
385         $self->pidl("}");
386         $self->pidl("");
387 }
388
389 sub ParseFunctionSync($$$)
390 {
391         my ($self, $if, $fn) = @_;
392
393         my $fn_args = "";
394         my $uif = uc($if);
395         my $ufn = "NDR_".uc($fn->{NAME});
396         my $fn_str = "NTSTATUS rpccli_$fn->{NAME}";
397         my $pad = genpad($fn_str);
398
399         $fn_args .= "struct rpc_pipe_client *cli,\n" . $pad . "TALLOC_CTX *mem_ctx";
400
401         foreach (@{$fn->{ELEMENTS}}) {
402                 my $dir = ElementDirection($_);
403                 my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]);
404                 $fn_args .= ",\n" . $pad . DeclLong($_) . " /* $dir $prop */";
405         }
406
407         if (defined($fn->{RETURN_TYPE}) && ($fn->{RETURN_TYPE} eq "WERROR")) {
408                 $fn_args .= ",\n" . $pad . "WERROR *werror";
409         }
410
411         $self->fn_declare("$fn_str($fn_args)");
412         $self->pidl("{");
413         $self->indent;
414         $self->pidl("struct $fn->{NAME} r;");
415         $self->pidl("NTSTATUS status;");
416         $self->pidl("");
417         $self->pidl("/* In parameters */");
418
419         foreach (@{$fn->{ELEMENTS}}) {
420                 if (grep(/in/, @{$_->{DIRECTION}})) {
421                         $self->pidl("r.in.$_->{NAME} = $_->{NAME};");
422                 }
423         }
424
425         $self->pidl("");
426         $self->pidl("status = cli->dispatch(cli,");
427         $self->pidl("\t\t\tmem_ctx,");
428         $self->pidl("\t\t\t&ndr_table_$if,");
429         $self->pidl("\t\t\t$ufn,");
430         $self->pidl("\t\t\t&r);");
431         $self->pidl("");
432
433         $self->pidl("if (!NT_STATUS_IS_OK(status)) {");
434         $self->indent;
435         $self->pidl("return status;");
436         $self->deindent;
437         $self->pidl("}");
438
439         $self->pidl("");
440         $self->pidl("if (NT_STATUS_IS_ERR(status)) {");
441         $self->indent;
442         $self->pidl("return status;");
443         $self->deindent;
444         $self->pidl("}");
445         $self->pidl("");
446         $self->pidl("/* Return variables */");
447         foreach my $e (@{$fn->{ELEMENTS}}) {
448                 next unless (grep(/out/, @{$e->{DIRECTION}}));
449
450                 $self->ParseOutputArgument($fn, $e);
451
452         }
453
454         $self->pidl("");
455         $self->pidl("/* Return result */");
456         if (not $fn->{RETURN_TYPE}) {
457                 $self->pidl("return NT_STATUS_OK;");
458         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
459                 $self->pidl("return r.out.result;");
460         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
461                 $self->pidl("if (werror) {");
462                 $self->indent;
463                 $self->pidl("*werror = r.out.result;");
464                 $self->deindent;
465                 $self->pidl("}");
466                 $self->pidl("");
467                 $self->pidl("return werror_to_ntstatus(r.out.result);");
468         } else {
469                 warning($fn->{ORIGINAL}, "Unable to convert $fn->{RETURN_TYPE} to NTSTATUS");
470                 $self->pidl("return NT_STATUS_OK;");
471         }
472
473         $self->deindent;
474         $self->pidl("}");
475         $self->pidl("");
476 }
477
478 sub ParseFunction($$$)
479 {
480         my ($self, $if, $fn) = @_;
481
482         $self->ParseFunctionAsyncState($if, $fn);
483         $self->ParseFunctionAsyncSend($if, $fn);
484         $self->ParseFunctionAsyncDone($if, $fn);
485         $self->ParseFunctionAsyncRecv($if, $fn);
486
487         $self->ParseFunctionSync($if, $fn);
488 }
489
490 sub ParseInterface($$)
491 {
492         my ($self, $if) = @_;
493
494         my $uif = uc($if->{NAME});
495
496         $self->pidl_hdr("#ifndef __CLI_$uif\__");
497         $self->pidl_hdr("#define __CLI_$uif\__");
498         foreach my $fn (@{$if->{FUNCTIONS}}) {
499                 next if has_property($fn, "noopnum");
500                 next if has_property($fn, "todo");
501                 $self->ParseFunction($if->{NAME}, $fn);
502         }
503         $self->pidl_hdr("#endif /* __CLI_$uif\__ */");
504 }
505
506 sub Parse($$$$)
507 {
508         my($self,$ndr,$header,$ndr_header) = @_;
509
510         $self->pidl("/*");
511         $self->pidl(" * Unix SMB/CIFS implementation.");
512         $self->pidl(" * client auto-generated by pidl. DO NOT MODIFY!");
513         $self->pidl(" */");
514         $self->pidl("");
515         $self->pidl("#include \"includes.h\"");
516         $self->pidl("#include \"$header\"");
517         $self->pidl_hdr("#include \"$ndr_header\"");
518         $self->pidl("");
519         
520         foreach (@$ndr) {
521                 $self->ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
522         }
523
524         return ($self->{res}, $self->{res_hdr});
525 }
526
527 1;