1 ###################################################
2 # client calls generator
3 # Copyright tridge@samba.org 2003
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
7 package Parse
::Pidl
::Samba4
::NDR
::Client
;
9 use Parse
::Pidl
::Samba4
qw(choose_header is_intree);
10 use Parse
::Pidl
::Util
qw(has_property);
12 use vars
qw($VERSION);
19 sub ParseFunctionOldSend($$$)
21 my ($interface, $fn, $name) = @_;
24 if (has_property($fn, "todo")) {
28 my $proto = "static struct rpc_request *dcerpc_$name\_send(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, struct $name *r)";
30 $res .= "$proto\n{\n";
33 if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
34 NDR_PRINT_IN_DEBUG($name, r);
37 return dcerpc_ndr_request_send(p, NULL, &ndr_table_$interface->{NAME},
38 NDR_$uname, true, mem_ctx, r);
44 sub ParseFunctionOldRecv($$$)
46 my ($interface, $fn, $name) = @_;
49 if (has_property($fn, "todo")) {
53 my $proto = "static NTSTATUS dcerpc_$name\_recv(struct rpc_request *rreq)";
55 $res .= "$proto\n{\n";
57 $res .= "NTSTATUS status;
58 struct dcerpc_pipe *p = rreq->p;
59 struct $name *r = (struct $name *)rreq->ndr.struct_ptr;
61 status = dcerpc_ndr_request_recv(rreq);
63 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
64 status = dcerpc_fault_to_nt_status(p->last_fault_code);
67 if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
68 NDR_PRINT_OUT_DEBUG($name, r);
77 sub ParseFunction_r_State($$$)
79 my ($interface, $fn, $name) = @_;
82 if (has_property($fn, "todo")) {
86 $res .= "struct dcerpc_$name\_r_state {\n";
87 $res .= "\tTALLOC_CTX *out_mem_ctx;\n";
90 $res .= "static void dcerpc_$name\_r_done(struct rpc_request *subreq);\n";
94 sub ParseFunction_r_Send($$$)
96 my ($interface, $fn, $name) = @_;
99 if (has_property($fn, "todo")) {
103 my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n";
104 $proto .= "\tstruct tevent_context *ev,\n",
105 $proto .= "\tstruct dcerpc_binding_handle *h,\n",
106 $proto .= "\tstruct $name *r)";
108 $res_hdr .= "\n$proto;\n";
110 $res .= "$proto\n{\n";
112 $res .= "\tstruct tevent_req *req;\n";
113 $res .= "\tstruct dcerpc_$name\_r_state *state;\n";
114 $res .= "\tstruct dcerpc_pipe *p =\n";
115 $res .= "\t\ttalloc_get_type_abort(h->private_data,\n";
116 $res .= "\t\tstruct dcerpc_pipe);\n";
117 $res .= "\tstruct rpc_request *subreq;\n";
120 $res .= "\treq = tevent_req_create(mem_ctx, &state,\n";
121 $res .= "\t\t\t\tstruct dcerpc_$name\_r_state);\n";
122 $res .= "\tif (req == NULL) {\n";
123 $res .= "\t\treturn NULL;\n";
128 foreach (@{$fn->{ELEMENTS}}) {
129 if (grep(/out/, @{$_->{DIRECTION}})) {
135 if ($out_params > 0) {
136 $res .= "\tstate->out_mem_ctx = talloc_new(state);\n";
137 $res .= "\tif (tevent_req_nomem(state->out_mem_ctx, req)) {\n";
138 $res .= "\t\treturn tevent_req_post(req, ev);\n";
141 $submem = "state->out_mem_ctx";
143 $res .= "\tstate->out_mem_ctx = NULL;\n";
147 $res .= "\tsubreq = dcerpc_$name\_send(p, $submem, r);\n";
148 $res .= "\tif (tevent_req_nomem(subreq, req)) {\n";
149 $res .= "\t\treturn tevent_req_post(req, ev);\n";
151 $res .= "\tsubreq->async.callback = dcerpc_$name\_r_done;\n";
152 $res .= "\tsubreq->async.private_data = req;\n";
155 $res .= "\treturn req;\n";
160 sub ParseFunction_r_Done($$$)
162 my ($interface, $fn, $name) = @_;
163 my $uname = uc $name;
165 if (has_property($fn, "todo")) {
169 my $proto = "static void dcerpc_$name\_r_done(struct rpc_request *subreq)";
174 $res .= "\tstruct tevent_req *req =\n";
175 $res .= "\t\ttalloc_get_type_abort(subreq->async.private_data,\n";
176 $res .= "\t\tstruct tevent_req);\n";
177 $res .= "\tNTSTATUS status;\n";
180 $res .= "\tstatus = dcerpc_$name\_recv(subreq);\n";
181 $res .= "\tif (!NT_STATUS_IS_OK(status)) {\n";
182 $res .= "\t\ttevent_req_nterror(req, status);\n";
183 $res .= "\t\treturn;\n";
187 $res .= "\ttevent_req_done(req);\n";
192 sub ParseFunction_r_Recv($$$)
194 my ($interface, $fn, $name) = @_;
195 my $uname = uc $name;
197 if (has_property($fn, "todo")) {
201 my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)";
203 $res_hdr .= "\n$proto;\n";
205 $res .= "$proto\n{\n";
207 $res .= "\tstruct dcerpc_$name\_r_state *state =\n";
208 $res .= "\t\ttevent_req_data(req,\n";
209 $res .= "\t\tstruct dcerpc_$name\_r_state);\n";
210 $res .= "\tNTSTATUS status;\n";
213 $res .= "\tif (tevent_req_is_nterror(req, &status)) {\n";
214 $res .= "\t\ttevent_req_received(req);\n";
215 $res .= "\t\treturn status;\n";
219 $res .= "\ttalloc_steal(mem_ctx, state->out_mem_ctx);\n";
222 $res .= "\ttevent_req_received(req);\n";
223 $res .= "\treturn NT_STATUS_OK;\n";
228 sub ParseFunction_r_Sync($$$)
230 my ($interface, $fn, $name) = @_;
231 my $uname = uc $name;
233 if (has_property($fn, "todo")) {
237 my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)";
239 $res_hdr .= "\n$proto;\n";
240 $res .= "$proto\n{\n";
242 $res .= "\tstruct dcerpc_pipe *p =\n";
243 $res .= "\t\ttalloc_get_type_abort(h->private_data,\n";
244 $res .= "\t\tstruct dcerpc_pipe);\n";
250 if (p->conn->flags & DCERPC_DEBUG_PRINT_IN) {
251 NDR_PRINT_IN_DEBUG($name, r);
254 status = dcerpc_ndr_request(p, NULL, &ndr_table_$interface->{NAME},
255 NDR_$uname, mem_ctx, r);
257 if (NT_STATUS_EQUAL(status, NT_STATUS_NET_WRITE_FAULT)) {
258 status = dcerpc_fault_to_nt_status(p->last_fault_code);
261 if (NT_STATUS_IS_OK(status) && (p->conn->flags & DCERPC_DEBUG_PRINT_OUT)) {
262 NDR_PRINT_OUT_DEBUG($name, r);
271 #####################################################################
273 sub ParseFunction($$)
275 my ($interface, $fn) = @_;
277 ParseFunctionOldSend($interface, $fn, $fn->{NAME});
278 ParseFunctionOldRecv($interface, $fn, $fn->{NAME});
279 ParseFunction_r_State($interface, $fn, $fn->{NAME});
280 ParseFunction_r_Send($interface, $fn, $fn->{NAME});
281 ParseFunction_r_Done($interface, $fn, $fn->{NAME});
282 ParseFunction_r_Recv($interface, $fn, $fn->{NAME});
283 ParseFunction_r_Sync($interface, $fn, $fn->{NAME});
288 #####################################################################
289 # parse the interface definitions
290 sub ParseInterface($)
292 my($interface) = shift;
294 $res_hdr .= "#ifndef _HEADER_RPC_$interface->{NAME}\n";
295 $res_hdr .= "#define _HEADER_RPC_$interface->{NAME}\n\n";
297 if (defined $interface->{PROPERTIES}->{uuid}) {
298 $res_hdr .= "extern const struct ndr_interface_table ndr_table_$interface->{NAME};\n";
301 $res .= "/* $interface->{NAME} - client functions generated by pidl */\n\n";
303 foreach my $fn (@{$interface->{FUNCTIONS}}) {
304 next if not defined($fn->{OPNUM});
305 next if defined($done{$fn->{NAME}});
306 ParseFunction($interface, $fn);
307 $done{$fn->{NAME}} = 1;
310 $res_hdr .= "#endif /* _HEADER_RPC_$interface->{NAME} */\n";
317 my($ndr,$header,$ndr_header,$client_header) = @_;
322 $res .= "/* client functions auto-generated by pidl */\n";
325 $res .= "#include \"includes.h\"\n";
327 $res .= "#ifndef _GNU_SOURCE\n";
328 $res .= "#define _GNU_SOURCE\n";
330 $res .= "#include <stdio.h>\n";
331 $res .= "#include <stdbool.h>\n";
332 $res .= "#include <stdlib.h>\n";
333 $res .= "#include <stdint.h>\n";
334 $res .= "#include <stdarg.h>\n";
335 $res .= "#include <core/ntstatus.h>\n";
337 $res .= "#include <tevent.h>\n";
338 $res .= choose_header("lib/util/tevent_ntstatus.h", "util/tevent_ntstatus.h")."\n";
339 $res .= "#include \"$ndr_header\"\n";
340 $res .= "#include \"$client_header\"\n";
343 $res_hdr .= choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."\n";
344 $res_hdr .= "#include \"$header\"\n";
346 foreach my $x (@{$ndr}) {
347 ($x->{TYPE} eq "INTERFACE") && ParseInterface($x);
350 return ($res,$res_hdr);