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);
13 use vars qw($VERSION);
23 #####################################################
24 # generate the switch statement for function dispatch
25 sub gen_dispatch_switch($)
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";
41 if ($d->{RETURN_TYPE} && $d->{RETURN_TYPE} ne "void") {
42 pidl "\t\tresult = vtable->$d->{NAME}(iface, mem_ctx, r2);\n";
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";
49 pidl "\t\tbreak;\n\t}\n";
54 #####################################################
55 # generate the switch statement for function reply
56 sub gen_reply_switch($)
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";
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";
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";
75 pidl "\t\tbreak;\n\t}\n";
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};
88 my $uuid = Parse::Pidl::Util::make_str($interface->{PROPERTIES}->{uuid});
89 my $if_version = $interface->{PROPERTIES}->{version};
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);
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);
110 static NTSTATUS $name\__op_ndr_pull(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_pull *pull, void **r)
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;
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;
146 gen_dispatch_switch($data);
150 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
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;
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;
169 gen_reply_switch($data);
173 dce_call->fault_code = DCERPC_FAULT_OP_RNG_ERROR;
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;
186 static NTSTATUS $name\__op_ndr_push(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct ndr_push *push, const void *r)
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;
200 static const struct dcesrv_interface $name\_interface = {
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;
224 static NTSTATUS $name\__op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
228 for (i=0;i<dcerpc_table_$name.endpoints->count;i++) {
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));
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));
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));
263 NTSTATUS dcerpc_server_$name\_init(void)
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\"));
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};
304 if (!defined $interface->{PROPERTIES}->{uuid}) {
308 if (!defined $interface->{PROPERTIES}->{version}) {
309 $interface->{PROPERTIES}->{version} = "0.0";
312 foreach my $d (@{$data}) {
313 if ($d->{TYPE} eq "FUNCTION") { $count++; }
320 $res = "/* dcom interface stub generated by pidl */\n\n";
321 Boilerplate_Iface($interface);
322 Boilerplate_Ep_Server($interface);