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