pidl/NDR/Parser: split off ParseArrayPullGetSize() and ParseArrayPullGetLength()
[Samba.git] / pidl / lib / Parse / Pidl / Samba4 / COM / Stub.pm
blob150acbfde9b3e50b7230b55e962597fd349bc6f0
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
8 package Parse::Pidl::Samba4::COM::Stub;
10 use Parse::Pidl::Util qw(has_property);
11 use strict;
13 use vars qw($VERSION);
14 $VERSION = '0.01';
16 my($res);
18 sub pidl($)
20 $res .= shift;
23 #####################################################
24 # generate the switch statement for function dispatch
25 sub gen_dispatch_switch($)
27 my $data = shift;
29 my $count = 0;
30 foreach my $d (@{$data}) {
31 next if ($d->{TYPE} ne "FUNCTION");
33 pidl "\tcase $count: {\n";
34 if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") {
35 pidl "\t\tNTSTATUS result;\n";
37 pidl "\t\tstruct $d->{NAME} *r2 = r;\n";
38 pidl "\t\tif (DEBUGLEVEL > 10) {\n";
39 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_IN, r2);\n";
40 pidl "\t\t}\n";
41 if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") {
42 pidl "\t\tresult = vtable->$d->{NAME}(iface, mem_ctx, r2);\n";
43 } else {
44 pidl "\t\tvtable->$d->{NAME}(iface, mem_ctx, r2);\n";
46 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
47 pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} will reply async\\n\"));\n";
48 pidl "\t\t}\n";
49 pidl "\t\tbreak;\n\t}\n";
50 $count++;
54 #####################################################
55 # generate the switch statement for function reply
56 sub gen_reply_switch($)
58 my $data = shift;
60 my $count = 0;
61 foreach my $d (@{$data}) {
62 next if ($d->{TYPE} ne "FUNCTION");
64 pidl "\tcase $count: {\n";
65 pidl "\t\tstruct $d->{NAME} *r2 = r;\n";
66 pidl "\t\tif (dce_call->state_flags & DCESRV_CALL_STATE_FLAG_ASYNC) {\n";
67 pidl "\t\t\tDEBUG(5,(\"function $d->{NAME} replied async\\n\"));\n";
68 pidl "\t\t}\n";
69 pidl "\t\tif (DEBUGLEVEL > 10 && dce_call->fault_code == 0) {\n";
70 pidl "\t\t\tNDR_PRINT_FUNCTION_DEBUG($d->{NAME}, NDR_OUT | NDR_SET_VALUES, r2);\n";
71 pidl "\t\t}\n";
72 pidl "\t\tif (dce_call->fault_code != 0) {\n";
73 pidl "\t\t\tDEBUG(2,(\"dcerpc_fault %s in $d->{NAME}\\n\", dcerpc_errstr(mem_ctx, dce_call->fault_code)));\n";
74 pidl "\t\t}\n";
75 pidl "\t\tbreak;\n\t}\n";
76 $count++;
80 #####################################################################
81 # produce boilerplate code for a interface
82 sub Boilerplate_Iface($)
84 my($interface) = shift;
85 my($data) = $interface->{DATA};
86 my $name = $interface->{NAME};
87 my $uname = uc $name;
88 my $uuid = Parse::Pidl::Util::make_str($interface->{PROPERTIES}->{uuid});
89 my $if_version = $interface->{PROPERTIES}->{version};
91 pidl "
92 static NTSTATUS $name\__op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
94 #ifdef DCESRV_INTERFACE_$uname\_BIND
95 return DCESRV_INTERFACE_$uname\_BIND(dce_call,iface);
96 #else
97 return NT_STATUS_OK;
98 #endif
101 static void $name\__op_unbind(struct dcesrv_connection_context *context, const struct dcesrv_interface *iface)
103 #ifdef DCESRV_INTERFACE_$uname\_UNBIND
104 DCESRV_INTERFACE_$uname\_UNBIND(context, iface);
105 #else
106 return;
107 #endif
110 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
112 NTSTATUS status;
113 uint16_t opnum = dce_call->pkt.u.request.opnum;
115 dce_call->fault_code = 0;
117 if (opnum >= dcerpc_table_$name.num_calls) {
118 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
119 return NT_STATUS_NET_WRITE_FAULT;
122 *r = talloc_size(mem_ctx, dcerpc_table_$name.calls[opnum].struct_size);
123 NT_STATUS_HAVE_NO_MEMORY(*r);
125 /* unravel the NDR for the packet */
126 status = dcerpc_table_$name.calls[opnum].ndr_pull(pull, NDR_IN, *r);
127 if (!NT_STATUS_IS_OK(status)) {
128 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
129 &dce_call->pkt.u.request.stub_and_verifier);
130 dce_call->fault_code = DCERPC_FAULT_NDR;
131 return NT_STATUS_NET_WRITE_FAULT;
134 return NT_STATUS_OK;
137 static NTSTATUS $name\__op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
139 uint16_t opnum = dce_call->pkt.u.request.opnum;
140 struct GUID ipid = dce_call->pkt.u.request.object.object;
141 struct dcom_interface_p *iface = dcom_get_local_iface_p(&ipid);
142 const struct dcom_$name\_vtable *vtable = iface->vtable;
144 switch (opnum) {
146 gen_dispatch_switch($data);
148 pidl "
149 default:
150 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
151 break;
154 if (dce_call->fault_code != 0) {
155 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
156 &dce_call->pkt.u.request.stub_and_verifier);
157 return NT_STATUS_NET_WRITE_FAULT;
160 return NT_STATUS_OK;
163 static NTSTATUS $name\__op_reply(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
165 uint16_t opnum = dce_call->pkt.u.request.opnum;
167 switch (opnum) {
169 gen_reply_switch($data);
171 pidl "
172 default:
173 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
174 break;
177 if (dce_call->fault_code != 0) {
178 dcerpc_log_packet(&dcerpc_table_$name, opnum, NDR_IN,
179 &dce_call->pkt.u.request.stub_and_verifier);
180 return NT_STATUS_NET_WRITE_FAULT;
183 return NT_STATUS_OK;
186 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
188 NTSTATUS status;
189 uint16_t opnum = dce_call->pkt.u.request.opnum;
191 status = dcerpc_table_$name.calls[opnum].ndr_push(push, NDR_OUT, r);
192 if (!NT_STATUS_IS_OK(status)) {
193 dce_call->fault_code = DCERPC_FAULT_NDR;
194 return NT_STATUS_NET_WRITE_FAULT;
197 return NT_STATUS_OK;
200 static const struct dcesrv_interface $name\_interface = {
201 .name = \"$name\",
202 .uuid = $uuid,
203 .if_version = $if_version,
204 .bind = $name\__op_bind,
205 .unbind = $name\__op_unbind,
206 .ndr_pull = $name\__op_ndr_pull,
207 .dispatch = $name\__op_dispatch,
208 .reply = $name\__op_reply,
209 .ndr_push = $name\__op_ndr_push
215 #####################################################################
216 # produce boilerplate code for an endpoint server
217 sub Boilerplate_Ep_Server($)
219 my($interface) = shift;
220 my $name = $interface->{NAME};
221 my $uname = uc $name;
223 pidl "
224 static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
226 int i;
228 for (i=0;i<dcerpc_table_$name.endpoints->count;i++) {
229 NTSTATUS ret;
230 const char *name = dcerpc_table_$name.endpoints->names[i];
232 ret = dcesrv_interface_register(dce_ctx, name, &$name\_interface, NULL);
233 if (!NT_STATUS_IS_OK(ret)) {
234 DEBUG(1,(\"$name\_op_init_server: failed to register endpoint \'%s\'\\n\",name));
235 return ret;
239 return NT_STATUS_OK;
242 static BOOL $name\__op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
244 if (dcerpc_table_$name.if_version == if_version &&
245 strcmp(dcerpc_table_$name.uuid, uuid)==0) {
246 memcpy(iface,&dcerpc_table_$name, sizeof(*iface));
247 return True;
250 return False;
253 static BOOL $name\__op_interface_by_name(struct dcesrv_interface *iface, const char *name)
255 if (strcmp(dcerpc_table_$name.name, name)==0) {
256 memcpy(iface,&dcerpc_table_$name, sizeof(*iface));
257 return True;
260 return False;
263 NTSTATUS dcerpc_server_$name\_init(void)
265 NTSTATUS ret;
266 struct dcesrv_endpoint_server ep_server;
268 /* fill in our name */
269 ep_server.name = \"$name\";
271 /* fill in all the operations */
272 ep_server.init_server = $name\__op_init_server;
274 ep_server.interface_by_uuid = $name\__op_interface_by_uuid;
275 ep_server.interface_by_name = $name\__op_interface_by_name;
277 /* register ourselves with the DCERPC subsystem. */
278 ret = dcerpc_register_ep_server(&ep_server);
280 if (!NT_STATUS_IS_OK(ret)) {
281 DEBUG(0,(\"Failed to register \'$name\' endpoint server!\\n\"));
282 return ret;
285 return ret;
291 #####################################################################
292 # dcom interface stub from a parsed IDL structure
293 sub ParseInterface($)
295 my($interface) = shift;
297 return "" if has_property($interface, "local");
299 my($data) = $interface->{DATA};
300 my $count = 0;
302 $res = "";
304 if (!defined $interface->{PROPERTIES}->{uuid}) {
305 return $res;
308 if (!defined $interface->{PROPERTIES}->{version}) {
309 $interface->{PROPERTIES}->{version} = "0.0";
312 foreach my $d (@{$data}) {
313 if ($d->{TYPE} eq "FUNCTION") { $count++; }
316 if ($count == 0) {
317 return $res;
320 $res = "/* dcom interface stub generated by pidl */\n\n";
321 Boilerplate_Iface($interface);
322 Boilerplate_Ep_Server($interface);
324 return $res;