02fd1884a240e88c44e060ee968528ded8410025
[ira/wip.git] / source / pidl / lib / Parse / Pidl / Samba3 / ServerNDR.pm
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
6
7 package Parse::Pidl::Samba3::ServerNDR;
8
9 use strict;
10 use Parse::Pidl::Typelist qw(hasType getType mapType scalar_is_reference);
11 use Parse::Pidl::Util qw(has_property ParseExpr is_constant);
12 use Parse::Pidl::NDR qw(GetPrevLevel GetNextLevel ContainsDeferred);
13 use Parse::Pidl::Samba4 qw(DeclLong);
14
15 use vars qw($VERSION);
16 $VERSION = '0.01';
17
18 my $res;
19 my $res_hdr;
20 my $tabs = "";
21 sub indent() { $tabs.="\t"; }
22 sub deindent() { $tabs = substr($tabs, 1); }
23 sub pidl($) { $res .= $tabs.(shift)."\n"; }
24 sub pidl_hdr($) { $res_hdr .= (shift)."\n"; }
25 sub fatal($$) { my ($e,$s) = @_; die("$e->{ORIGINAL}->{FILE}:$e->{ORIGINAL}->{LINE}: $s\n"); }
26 sub warning($$) { my ($e,$s) = @_; warn("$e->{ORIGINAL}->{FILE}:$e->{ORIGINAL}->{LINE}: $s\n"); }
27 sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; }
28
29 sub AllocOutVar($$$$)
30 {
31         my ($e, $mem_ctx, $name, $env) = @_;
32
33         my $l = $e->{LEVELS}[0];
34
35         if ($l->{TYPE} eq "POINTER") {
36                 $l = GetNextLevel($e, $l);
37         }
38
39         if ($l->{TYPE} eq "ARRAY") {
40                 my $size = ParseExpr($l->{SIZE_IS}, $env);
41                 pidl "$name = talloc_zero_size($mem_ctx, sizeof(*$name) * $size);";
42         } else {
43                 pidl "$name = talloc_zero_size($mem_ctx, sizeof(*$name));";
44         }
45
46         pidl "if ($name == NULL) {";
47         pidl "\ttalloc_free(mem_ctx);";
48         pidl "\treturn False;";
49         pidl "}";
50         pidl "";
51 }
52
53 sub ParseFunction($$)
54 {
55         my ($if,$fn) = @_;
56
57         pidl "static BOOL api_$fn->{NAME}(pipes_struct *p)";
58         pidl "{";
59         indent;
60         pidl "struct ndr_pull *pull;";
61         pidl "struct ndr_push *push;";
62         pidl "NTSTATUS status;";
63         pidl "DATA_BLOB blob;";
64         pidl "struct $fn->{NAME} r;";
65         pidl "TALLOC_CTX *mem_ctx = talloc_init(\"api_$fn->{NAME}\");";
66         pidl "";
67         pidl "if (!prs_data_blob(&p->in_data.data, &blob, mem_ctx)) {";
68         pidl "\ttalloc_free(mem_ctx);";
69         pidl "\treturn False;";
70         pidl "}";
71         pidl "";
72         pidl "pull = ndr_pull_init_blob(&blob, mem_ctx);";
73         pidl "if (pull == NULL)";
74         pidl "\treturn False;";
75         pidl "";
76         pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
77         pidl "status = ndr_pull_$fn->{NAME}(pull, NDR_IN, &r);";
78         pidl "if (NT_STATUS_IS_ERR(status)) {";
79         pidl "\ttalloc_free(mem_ctx);";
80         pidl "\treturn False;";
81         pidl "}";
82         pidl "";
83         pidl "if (DEBUGLEVEL >= 10)";
84         pidl "\tNDR_PRINT_IN_DEBUG($fn->{NAME}, &r);";
85         pidl "";
86
87         my %env = ();
88         my $hasout = 0;
89         foreach (@{$fn->{ELEMENTS}}) {
90                 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
91                 next unless (grep (/in/, @{$_->{DIRECTION}}));
92                 $env{$_->{NAME}} = "r.in.$_->{NAME}";
93         }
94
95         pidl "ZERO_STRUCT(r.out);" if ($hasout);
96
97         my $proto = "_$fn->{NAME}(pipes_struct *p";
98         my $ret = "_$fn->{NAME}(p";
99         foreach (@{$fn->{ELEMENTS}}) {
100                 my @dir = @{$_->{DIRECTION}};
101                 if (grep(/in/, @dir) and grep(/out/, @dir)) {
102                         pidl "r.out.$_->{NAME} = r.in.$_->{NAME};";
103                 } elsif (grep(/out/, @dir)) {
104                         AllocOutVar($_, "mem_ctx", "r.out.$_->{NAME}", \%env);
105                 }
106                 if (grep(/in/, @dir)) { $ret .= ", r.in.$_->{NAME}"; }
107                 else { $ret .= ", r.out.$_->{NAME}"; }
108
109                 $proto .= ", " . DeclLong($_);
110         }
111         $ret .= ")";
112         $proto .= ");";
113
114         if ($fn->{RETURN_TYPE}) {
115                 $ret = "r.out.result = $ret";
116                 $proto = "$fn->{RETURN_TYPE} $proto";
117         } else {
118                 $proto = "void $proto";
119         }
120
121         pidl_hdr "$proto";
122         pidl "$ret;";
123
124         pidl "";
125         pidl "if (DEBUGLEVEL >= 10)";
126         pidl "\tNDR_PRINT_OUT_DEBUG($fn->{NAME}, &r);";
127         pidl "";
128         pidl "push = ndr_push_init_ctx(mem_ctx);";
129         pidl "if (push == NULL) {";
130         pidl "\ttalloc_free(mem_ctx);";
131         pidl "\treturn False;";
132         pidl "}";
133         pidl "";
134         pidl "status = ndr_push_$fn->{NAME}(push, NDR_OUT, &r);";
135         pidl "if (NT_STATUS_IS_ERR(status)) {";
136         pidl "\ttalloc_free(mem_ctx);";
137         pidl "\treturn False;";
138         pidl "}";
139         pidl "";
140         pidl "blob = ndr_push_blob(push);";
141         pidl "if (!prs_copy_data_in(&p->out_data.rdata, blob.data, (uint32)blob.length)) {";
142         pidl "\ttalloc_free(mem_ctx);";
143         pidl "\treturn False;";
144         pidl "}";
145         pidl "";
146         pidl "talloc_free(mem_ctx);";
147         pidl "";
148         pidl "return True;";
149         deindent;
150         pidl "}";
151         pidl "";
152 }
153
154 sub ParseInterface($)
155 {
156         my $if = shift;
157
158         my $uif = uc($if->{NAME});
159
160         pidl_hdr "#ifndef __SRV_$uif\__";
161         pidl_hdr "#define __SRV_$uif\__";
162         ParseFunction($if, $_) foreach (@{$if->{FUNCTIONS}});
163
164         pidl "";
165         pidl "/* Tables */";
166         pidl "static struct api_struct api_$if->{NAME}_cmds[] = ";
167         pidl "{";
168         indent;
169
170         foreach (@{$if->{FUNCTIONS}}) {
171                 pidl "{\"" . uc($_->{NAME}) . "\", DCERPC_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
172         }
173
174         deindent;
175         pidl "};";
176
177         pidl "";
178
179         pidl_hdr "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns);";
180         pidl "void $if->{NAME}_get_pipe_fns(struct api_struct **fns, int *n_fns)";
181         pidl "{";
182         indent;
183         pidl "*fns = api_$if->{NAME}_cmds;";
184         pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
185         deindent;
186         pidl "}";
187         pidl "";
188
189         pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(void);";
190         pidl "NTSTATUS rpc_$if->{NAME}_init(void)";
191         pidl "{";
192         pidl "\treturn rpc_pipe_register_commands(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct));";
193         pidl "}";
194
195         pidl_hdr "#endif /* __SRV_$uif\__ */";
196 }
197
198 sub Parse($$$)
199 {
200         my($ndr,$header,$ndr_header) = @_;
201
202         $res = "";
203         $res_hdr = "";
204
205         pidl "/*";
206         pidl " * Unix SMB/CIFS implementation.";
207         pidl " * server auto-generated by pidl. DO NOT MODIFY!";
208         pidl " */";
209         pidl "";
210         pidl "#include \"includes.h\"";
211         pidl "#include \"$header\"";
212         pidl_hdr "#include \"$ndr_header\"";
213         pidl "";
214         
215         foreach (@$ndr) {
216                 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
217         }
218
219         return ($res, $res_hdr);
220 }
221
222 1;