r8270: Export some symbols, making the code more readable.
[sfrench/samba-autobuild/.git] / source4 / build / pidl / Parse / Pidl / Samba / COM / Stub.pm
1 ###################################################
2 # DCOM stub boilerplate generator
3 # Copyright jelmer@samba.org 2004-2005
4 # Copyright tridge@samba.org 2003
5 # Copyright metze@samba.org 2004
6 # released under the GNU GPL
7
8 package Parse::Pidl::Samba::COM::Stub;
9
10 use Parse::Pidl::Util qw(has_property);
11 use strict;
12
13 my($res);
14
15 sub pidl($)
16 {
17         $res .= shift;
18 }
19
20 #####################################################
21 # generate the switch statement for function dispatch
22 sub gen_dispatch_switch($)
23 {
24         my $data = shift;
25
26         my $count = 0;
27         foreach my $d (@{$data}) {
28                 next if ($d->{TYPE} ne "FUNCTION");
29
30                 pidl "\tcase $count: {\n";
31                 if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") {
32                         pidl "\t\tNTSTATUS result;\n";
33                 }
34                 pidl "\t\tstruct $d->{NAME} *r2 = r;\n";
35                 pidl "\t\tif (DEBUGLEVEL > 10) {\n";
36                 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_IN, r2);\n";
37                 pidl "\t\t}\n";
38                 if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") {
39                         pidl "\t\tresult = vtable->$d->{NAME}(iface, mem_ctx, r2);\n";
40                 } else {
41                         pidl "\t\tvtable->$d->{NAME}(iface, mem_ctx, r2);\n";
42                 }
43                 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
44                 pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} will reply async\\n\"));\n";
45                 pidl "\t\t}\n";
46                 pidl "\t\tbreak;\n\t}\n";
47                 $count++; 
48         }
49 }
50
51 #####################################################
52 # generate the switch statement for function reply
53 sub gen_reply_switch($)
54 {
55         my $data = shift;
56
57         my $count = 0;
58         foreach my $d (@{$data}) {
59                 next if ($d->{TYPE} ne "FUNCTION");
60
61                 pidl "\tcase $count: {\n";
62                 pidl "\t\tstruct $d->{NAME} *r2 = r;\n";
63                 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
64                 pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} replied async\\n\"));\n";
65                 pidl "\t\t}\n";
66                 pidl "\t\tif (DEBUGLEVEL > 10 && dce_call->fault_code == 0) {\n";
67                 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n";
68                 pidl "\t\t}\n";
69                 pidl "\t\tif (dce_call->fault_code != 0) {\n";
70                 pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $d->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n";
71                 pidl "\t\t}\n";
72                 pidl "\t\tbreak;\n\t}\n";
73                 $count++; 
74         }
75 }
76
77 #####################################################################
78 # produce boilerplate code for a interface
79 sub Boilerplate_Iface($)
80 {
81         my($interface) = shift;
82         my($data) = $interface->{DATA};
83         my $name = $interface->{NAME};
84         my $uname = uc $name;
85         my $uuid = Parse::Pidl::Util::make_str($interface->{PROPERTIES}->{uuid});
86         my $if_version = $interface->{PROPERTIES}->{version};
87
88         pidl "
89 static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
90 {
91 #ifdef DCESRV_INTERFACE_$uname\_BIND
92         return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface);
93 #else
94         return NT_STATUS_OK;
95 #endif
96 }
97
98 static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
99 {
100 #ifdef DCESRV_INTERFACE_$uname\_UNBIND
101         DCESRV_INTERFACE_$uname\_UNBIND(context, iface);
102 #else
103         return;
104 #endif
105 }
106
107 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
108 {
109         NTSTATUS status;
110         uint16_t opnum = dce_call->pkt.u.request.opnum;
111
112         dce_call->fault_code = 0;
113
114         if (opnum >= dcerpc_table_$name.num_calls) {
115                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
116                 return NT_STATUS_NET_WRITE_FAULT;
117         }
118
119         *r = talloc_size(mem_ctx, dcerpc_table_$name.calls[opnum].struct_size);
120         NT_STATUS_HAVE_NO_MEMORY(*r);
121
122         /* unravel the NDR for the packet */
123         status = dcerpc_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);
124         if (!NT_STATUS_IS_OK(status)) {
125                 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
126                                   &dce_call->pkt.u.request.stub_and_verifier);
127                 dce_call->fault_code = DCERPC_FAULT_NDR;
128                 return NT_STATUS_NET_WRITE_FAULT;
129         }
130
131         return NT_STATUS_OK;
132 }
133
134 static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
135 {
136         uint16_t opnum = dce_call->pkt.u.request.opnum;
137         struct GUID ipid = dce_call->pkt.u.request.object.object;
138         struct dcom_interface_p *iface = dcom_get_local_iface_p(&ipid);
139         const struct dcom_$name\_vtable *vtable = iface->vtable;
140
141         switch (opnum) {
142 ";
143         gen_dispatch_switch($data);
144
145 pidl "
146         default:
147                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
148                 break;
149         }
150
151         if (dce_call->fault_code != 0) {
152                 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
153                                   &dce_call->pkt.u.request.stub_and_verifier);
154                 return NT_STATUS_NET_WRITE_FAULT;
155         }
156
157         return NT_STATUS_OK;
158 }
159
160 static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
161 {
162         uint16_t opnum = dce_call->pkt.u.request.opnum;
163
164         switch (opnum) {
165 ";
166         gen_reply_switch($data);
167
168 pidl "
169         default:
170                 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
171                 break;
172         }
173
174         if (dce_call->fault_code != 0) {
175                 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
176                                   &dce_call->pkt.u.request.stub_and_verifier);
177                 return NT_STATUS_NET_WRITE_FAULT;
178         }
179
180         return NT_STATUS_OK;
181 }
182
183 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
184 {
185         NTSTATUS status;
186         uint16_t opnum = dce_call->pkt.u.request.opnum;
187
188         status = dcerpc_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
189         if (!NT_STATUS_IS_OK(status)) {
190                 dce_call->fault_code = DCERPC_FAULT_NDR;
191                 return NT_STATUS_NET_WRITE_FAULT;
192         }
193
194         return NT_STATUS_OK;
195 }
196
197 static const struct dcesrv_interface $name\_interface = {
198         .name           = \"$name\",
199         .uuid           = $uuid,
200         .if_version     = $if_version,
201         .bind           = $name\__op_bind,
202         .unbind         = $name\__op_unbind,
203         .ndr_pull       = $name\__op_ndr_pull,
204         .dispatch       = $name\__op_dispatch,
205         .reply          = $name\__op_reply,
206         .ndr_push       = $name\__op_ndr_push
207 };
208
209 ";
210 }
211
212 #####################################################################
213 # produce boilerplate code for an endpoint server
214 sub Boilerplate_Ep_Server($)
215 {
216         my($interface) = shift;
217         my $name = $interface->{NAME};
218         my $uname = uc $name;
219
220         pidl "
221 static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
222 {
223         int i;
224
225         for (i=0;i<dcerpc_table_$name.endpoints->count;i++) {
226                 NTSTATUS ret;
227                 const char *name = dcerpc_table_$name.endpoints->names[i];
228
229                 ret = dcesrv_interface_register(dce_ctx, name, &$name\_interface, NULL);
230                 if (!NT_STATUS_IS_OK(ret)) {
231                         DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
232                         return ret;
233                 }
234         }
235
236         return NT_STATUS_OK;
237 }
238
239 static BOOL $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
240 {
241         if (dcerpc_table_$name.if_version == if_version &&
242                 strcmp(dcerpc_table_$name.uuid, uuid)==0) {
243                 memcpy(iface,&dcerpc_table_$name, sizeof(*iface));
244                 return True;
245         }
246
247         return False;
248 }
249
250 static BOOL $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
251 {
252         if (strcmp(dcerpc_table_$name.name, name)==0) {
253                 memcpy(iface,&dcerpc_table_$name, sizeof(*iface));
254                 return True;
255         }
256
257         return False;   
258 }
259         
260 NTSTATUS dcerpc_server_$name\_init(void)
261 {
262         NTSTATUS ret;
263         struct dcesrv_endpoint_server ep_server;
264
265         /* fill in our name */
266         ep_server.name = \"$name\";
267
268         /* fill in all the operations */
269         ep_server.init_server = $name\__op_init_server;
270
271         ep_server.interface_by_uuid = $name\__op_interface_by_uuid;
272         ep_server.interface_by_name = $name\__op_interface_by_name;
273
274         /* register ourselves with the DCERPC subsystem. */
275         ret = dcerpc_register_ep_server(&ep_server);
276
277         if (!NT_STATUS_IS_OK(ret)) {
278                 DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
279                 return ret;
280         }
281
282         return ret;
283 }
284
285 ";
286 }
287
288 #####################################################################
289 # dcom interface stub from a parsed IDL structure 
290 sub ParseInterface($)
291 {
292         my($interface) = shift;
293         
294         return "" if has_property($interface, "local");
295         
296         my($data) = $interface->{DATA};
297         my $count = 0;
298
299         $res = "";
300
301         if (!defined $interface->{PROPERTIES}->{uuid}) {
302                 return $res;
303         }
304
305         if (!defined $interface->{PROPERTIES}->{version}) {
306                 $interface->{PROPERTIES}->{version} = "0.0";
307         }
308
309         foreach my $d (@{$data}) {
310                 if ($d->{TYPE} eq "FUNCTION") { $count++; }
311         }
312
313         if ($count == 0) {
314                 return $res;
315         }
316
317         $res = "/* dcom interface stub generated by pidl */\n\n";
318         Boilerplate_Iface($interface);
319         Boilerplate_Ep_Server($interface);
320
321         return $res;
322 }
323
324 1;