summaryrefslogtreecommitdiff
path: root/pidl/lib/Parse/Pidl/Samba4/NDR/Client.pm
blob: 55bc2fc75c0487dd64914cbddf12b8b2b7ff09c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
###################################################
# client calls generator
# Copyright tridge@samba.org 2003
# Copyright jelmer@samba.org 2005-2006
# released under the GNU GPL

package Parse::Pidl::Samba4::NDR::Client;

use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(Parse);

use Parse::Pidl::Samba4 qw(choose_header is_intree);
use Parse::Pidl::Util qw(has_property);

use vars qw($VERSION);
$VERSION = '0.01';

use strict;

sub indent($) { my ($self) = @_; $self->{tabs}.="\t"; }
sub deindent($) { my ($self) = @_; $self->{tabs} = substr($self->{tabs}, 1); }
sub pidl($$) { my ($self,$txt) = @_; $self->{res} .= $txt ? "$self->{tabs}$txt\n" : "\n"; }
sub pidl_hdr($$) { my ($self, $txt) = @_; $self->{res_hdr} .= "$txt\n"; }
sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); }

sub genpad($)
{
	my ($s) = @_;
	my $nt = int((length($s)+1)/8);
	my $lt = ($nt*8)-1;
	my $ns = (length($s)-$lt);
	return "\t"x($nt)." "x($ns);
}

sub new($)
{
	my ($class) = shift;
	my $self = { res => "", res_hdr => "", tabs => "" };
	bless($self, $class);
}

sub ParseFunction_r_State($$$$)
{
	my ($self, $if, $fn, $name) = @_;
	my $uname = uc $name;

	$self->pidl("struct dcerpc_$name\_r_state {");
	$self->indent;
	$self->pidl("TALLOC_CTX *out_mem_ctx;");
	$self->deindent;
	$self->pidl("};");
	$self->pidl("");
	$self->pidl("static void dcerpc_$name\_r_done(struct tevent_req *subreq);");
	$self->pidl("");
}

sub ParseFunction_r_Send($$$$)
{
	my ($self, $if, $fn, $name) = @_;
	my $uname = uc $name;

	my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n";
	$proto   .= "\tstruct tevent_context *ev,\n",
	$proto   .= "\tstruct dcerpc_binding_handle *h,\n",
	$proto   .= "\tstruct $name *r)";

	$self->fn_declare($proto);

	$self->pidl("{");
	$self->indent;

	$self->pidl("struct tevent_req *req;");
	$self->pidl("struct dcerpc_$name\_r_state *state;");
	$self->pidl("struct tevent_req *subreq;");
	$self->pidl("");

	$self->pidl("req = tevent_req_create(mem_ctx, &state,");
	$self->pidl("\t\t\tstruct dcerpc_$name\_r_state);");
	$self->pidl("if (req == NULL) {");
	$self->indent;
	$self->pidl("return NULL;");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("");

	my $out_params = 0;
	foreach (@{$fn->{ELEMENTS}}) {
		if (grep(/out/, @{$_->{DIRECTION}})) {
			$out_params++;
		}
	}

	my $submem;
	if ($out_params > 0) {
		$self->pidl("state->out_mem_ctx = talloc_new(state);");
		$self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {");
		$self->indent;
		$self->pidl("return tevent_req_post(req, ev);");
		$self->deindent;
		$self->pidl("}");
		$self->pidl("");
		$submem = "state->out_mem_ctx";
	} else {
		$self->pidl("state->out_mem_ctx = NULL;");
		$submem = "state";
	}

	$self->pidl("subreq = dcerpc_binding_handle_call_send(state, ev, h,");
	$self->pidl("\t\tNULL, &ndr_table_$if->{NAME},");
	$self->pidl("\t\tNDR_$uname, $submem, r);");
	$self->pidl("if (tevent_req_nomem(subreq, req)) {");
	$self->indent;
	$self->pidl("return tevent_req_post(req, ev);");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("tevent_req_set_callback(subreq, dcerpc_$name\_r_done, req);");
	$self->pidl("");

	$self->pidl("return req;");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("");
}

sub ParseFunction_r_Done($$$$)
{
	my ($self, $if, $fn, $name) = @_;
	my $uname = uc $name;

	my $proto = "static void dcerpc_$name\_r_done(struct tevent_req *subreq)";

	$self->pidl("$proto");
	$self->pidl("{");
	$self->indent;

	$self->pidl("struct tevent_req *req =");
	$self->pidl("\ttevent_req_callback_data(subreq,");
	$self->pidl("\tstruct tevent_req);");
	$self->pidl("NTSTATUS status;");
	$self->pidl("");

	$self->pidl("status = dcerpc_binding_handle_call_recv(subreq);");
	$self->pidl("if (!NT_STATUS_IS_OK(status)) {");
	$self->indent;
	$self->pidl("tevent_req_nterror(req, status);");
	$self->pidl("return;");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("");

	$self->pidl("tevent_req_done(req);");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("");
}

sub ParseFunction_r_Recv($$$$)
{
	my ($if, $fn, $name) = @_;
	my ($self, $if, $fn, $name) = @_;
	my $uname = uc $name;

	my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)";

	$self->fn_declare($proto);

	$self->pidl("{");
	$self->indent;

	$self->pidl("struct dcerpc_$name\_r_state *state =");
	$self->pidl("\ttevent_req_data(req,");
	$self->pidl("\tstruct dcerpc_$name\_r_state);");
	$self->pidl("NTSTATUS status;");
	$self->pidl("");

	$self->pidl("if (tevent_req_is_nterror(req, &status)) {");
	$self->indent;
	$self->pidl("tevent_req_received(req);");
	$self->pidl("return status;");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("");

	$self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);");
	$self->pidl("");

	$self->pidl("tevent_req_received(req);");
	$self->pidl("return NT_STATUS_OK;");
	$self->deindent;
	$self->pidl("}");
	$self->pidl("");
}

sub ParseFunction_r_Sync($$$$)
{
	my ($if, $fn, $name) = @_;
	my ($self, $if, $fn, $name) = @_;
	my $uname = uc $name;

	my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)";

	$self->fn_declare($proto);

	$self->pidl("{");
	$self->indent;
	$self->pidl("NTSTATUS status;");
	$self->pidl("");

	$self->pidl("status = dcerpc_binding_handle_call(h,");
	$self->pidl("\t\tNULL, &ndr_table_$if->{NAME},");
	$self->pidl("\t\tNDR_$uname, mem_ctx, r);");
	$self->pidl("");
	$self->pidl("return status;");

	$self->deindent;
	$self->pidl("}");
	$self->pidl("");
}

#####################################################################
# parse a function
sub ParseFunction($$$)
{
	my ($self, $if, $fn) = @_;

	$self->ParseFunction_r_State($if, $fn, $fn->{NAME});
	$self->ParseFunction_r_Send($if, $fn, $fn->{NAME});
	$self->ParseFunction_r_Done($if, $fn, $fn->{NAME});
	$self->ParseFunction_r_Recv($if, $fn, $fn->{NAME});
	$self->ParseFunction_r_Sync($if, $fn, $fn->{NAME});

	$self->pidl_hdr("");
}

my %done;

#####################################################################
# parse the interface definitions
sub ParseInterface($$)
{
	my ($self, $if) = @_;

	$self->pidl_hdr("#ifndef _HEADER_RPC_$if->{NAME}");
	$self->pidl_hdr("#define _HEADER_RPC_$if->{NAME}");
	$self->pidl_hdr("");

	if (defined $if->{PROPERTIES}->{uuid}) {
		$self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$if->{NAME};");
		$self->pidl_hdr("");
	}

	$self->pidl("/* $if->{NAME} - client functions generated by pidl */");
	$self->pidl("");

	foreach my $fn (@{$if->{FUNCTIONS}}) {
		next if defined($done{$fn->{NAME}});
		next if has_property($fn, "noopnum");
		next if has_property($fn, "todo");
		$self->ParseFunction($if, $fn);
		$done{$fn->{NAME}} = 1;
	}

	$self->pidl_hdr("#endif /* _HEADER_RPC_$if->{NAME} */");
}

sub Parse($$$$$$)
{
	my($self,$ndr,$header,$ndr_header,$client_header) = @_;

	$self->pidl("/* client functions auto-generated by pidl */");
	$self->pidl("");
	if (is_intree()) {
		$self->pidl("#include \"includes.h\"");
	} else {
		$self->pidl("#ifndef _GNU_SOURCE");
		$self->pidl("#define _GNU_SOURCE");
		$self->pidl("#endif");
		$self->pidl("#include <stdio.h>");
		$self->pidl("#include <stdbool.h>");
		$self->pidl("#include <stdlib.h>");
		$self->pidl("#include <stdint.h>");
		$self->pidl("#include <stdarg.h>");
		$self->pidl("#include <core/ntstatus.h>");
	}
	$self->pidl("#include <tevent.h>");
	$self->pidl(choose_header("lib/util/tevent_ntstatus.h", "util/tevent_ntstatus.h")."");
	$self->pidl("#include \"$ndr_header\"");
	$self->pidl("#include \"$client_header\"");
	$self->pidl("");

	$self->pidl_hdr(choose_header("librpc/rpc/dcerpc.h", "dcerpc.h")."");
	$self->pidl_hdr("#include \"$header\"");

	foreach my $x (@{$ndr}) {
		($x->{TYPE} eq "INTERFACE") && $self->ParseInterface($x);
	}

	return ($self->{res},$self->{res_hdr});
}

1;