1 ###################################################
2 # Samba3 server generator for IDL structures
3 # on top of Samba4 style NDR functions
4 # Copyright jelmer@samba.org 2005-2006
5 # released under the GNU GPL
7 package Parse::Pidl::Samba3::ServerNDR;
11 @EXPORT_OK = qw(DeclLevel);
14 use Parse::Pidl qw(warning error fatal);
15 use Parse::Pidl::Typelist qw(mapTypeName scalar_is_reference);
16 use Parse::Pidl::Util qw(ParseExpr has_property is_constant);
17 use Parse::Pidl::NDR qw(GetNextLevel ContainsPipe);
18 use Parse::Pidl::Samba4 qw(ElementStars DeclLong);
19 use Parse::Pidl::Samba4::Header qw(GenerateFunctionOutEnv);
21 use vars qw($VERSION);
27 sub pidl_reset() { $res=""; $res_hdr="", $tabs=""; }
28 sub pidl_return() { my $s = $res; my $h = $res_hdr; pidl_reset(); return ($s, $h) }
29 sub indent() { $tabs.="\t"; }
30 sub deindent() { $tabs = substr($tabs, 1); }
31 sub pidl($) { my ($txt) = @_; $res .= $txt?$tabs.(shift)."\n":"\n"; }
32 sub pidl_hdr($) { $res_hdr .= (shift)."\n"; }
33 sub fn_declare($) { my ($n) = @_; pidl $n; pidl_hdr "$n;"; }
40 if (has_property($e, "charset")) {
43 $res .= mapTypeName($e->{TYPE});
46 my $stars = ElementStars($e, $l);
48 $res .= " ".$stars unless ($stars eq "");
53 sub AllocOutVar($$$$$$$)
55 my ($e, $mem_ctx, $name, $env, $check, $cleanup, $return) = @_;
57 my $l = $e->{LEVELS}[0];
59 # we skip pointer to arrays
60 if ($l->{TYPE} eq "POINTER") {
61 my $nl = GetNextLevel($e, $l);
62 $l = $nl if ($nl->{TYPE} eq "ARRAY");
65 # we don't support multi-dimentional arrays yet
66 ($l->{TYPE} eq "ARRAY") {
67 my $nl = GetNextLevel($e, $l);
68 if ($nl->{TYPE} eq "ARRAY") {
69 fatal($e->{ORIGINAL},"multi-dimentional [out] arrays are not supported!");
72 # neither pointer nor array, no need to alloc something.
76 if ($l->{TYPE} eq "ARRAY") {
77 unless(defined($l->{SIZE_IS})) {
78 error($e->{ORIGINAL}, "No size known for array `$e->{NAME}'");
79 pidl "#error No size known for array `$e->{NAME}'";
81 my $size = ParseExpr($l->{SIZE_IS}, $env, $e);
82 pidl "$name = talloc_zero_array($mem_ctx, " . DeclLevel($e, 1) . ", $size);";
85 pidl "$name = talloc_zero($mem_ctx, " . DeclLevel($e, 1) . ");";
88 pidl "if (" . $check->($name) . ") {";
90 pidl $cleanup->($name) if defined($cleanup);
91 pidl $return->($name) if defined($return);
97 sub CallWithStruct($$$$$$)
99 my ($pipes_struct, $mem_ctx, $fn, $check, $cleanup, $return) = @_;
100 my $env = GenerateFunctionOutEnv($fn);
102 foreach (@{$fn->{ELEMENTS}}) {
103 if (grep(/out/, @{$_->{DIRECTION}})) { $hasout = 1; }
106 pidl "NDR_ZERO_STRUCT(r->out);" if ($hasout);
108 foreach (@{$fn->{ELEMENTS}}) {
109 my @dir = @{$_->{DIRECTION}};
110 if (grep(/in/, @dir) and grep(/out/, @dir)) {
111 pidl "r->out.$_->{NAME} = r->in.$_->{NAME};";
115 foreach (@{$fn->{ELEMENTS}}) {
116 next if ContainsPipe($_, $_->{LEVELS}[0]);
117 my @dir = @{$_->{DIRECTION}};
118 if (grep(/in/, @dir) and grep(/out/, @dir)) {
120 } elsif (grep(/out/, @dir) and not
121 has_property($_, "represent_as")) {
122 AllocOutVar($_, $mem_ctx, "r->out.$_->{NAME}", $env,
123 $check, $cleanup, $return);
127 my $proto = "_$fn->{NAME}(struct pipes_struct *p, struct $fn->{NAME} *r)";
128 my $ret = "_$fn->{NAME}($pipes_struct, r)";
130 if ($fn->{RETURN_TYPE}) {
131 $ret = "r->out.result = $ret";
132 $proto = mapTypeName($fn->{RETURN_TYPE})." $proto";
134 $proto = "void $proto";
141 sub ParseFunction($$)
145 my $op = "NDR_".uc($fn->{NAME});
147 pidl "static bool api_$fn->{NAME}(struct pipes_struct *p)";
150 pidl "const struct ndr_interface_call *call;";
151 pidl "struct ndr_pull *pull;";
152 pidl "struct ndr_push *push;";
153 pidl "enum ndr_err_code ndr_err;";
154 pidl "struct $fn->{NAME} *r;";
156 pidl "call = &ndr_table_$if->{NAME}.calls[$op];";
158 pidl "r = talloc(talloc_tos(), struct $fn->{NAME});";
159 pidl "if (r == NULL) {";
160 pidl "\treturn false;";
163 pidl "pull = ndr_pull_init_blob(&p->in_data.data, r);";
164 pidl "if (pull == NULL) {";
165 pidl "\ttalloc_free(r);";
166 pidl "\treturn false;";
169 pidl "pull->flags |= LIBNDR_FLAG_REF_ALLOC;";
170 pidl "if (p->endian) {";
171 pidl "\tpull->flags |= LIBNDR_FLAG_BIGENDIAN;";
173 pidl "ndr_err = call->ndr_pull(pull, NDR_IN, r);";
174 pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
175 pidl "\ttalloc_free(r);";
176 pidl "\treturn false;";
179 pidl "if (DEBUGLEVEL >= 10) {";
180 pidl "\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_IN, r);";
184 CallWithStruct("p", "r", $fn,
187 return "${name} == NULL";
191 return "talloc_free(r);";
195 return "return false;";
200 pidl "if (p->fault_state) {";
201 pidl "\ttalloc_free(r);";
202 pidl "\t/* Return true here, srv_pipe_hnd.c will take care */";
203 pidl "\treturn true;";
206 pidl "if (DEBUGLEVEL >= 10) {";
207 pidl "\tNDR_PRINT_FUNCTION_DEBUG($fn->{NAME}, NDR_OUT | NDR_SET_VALUES, r);";
210 pidl "push = ndr_push_init_ctx(r);";
211 pidl "if (push == NULL) {";
212 pidl "\ttalloc_free(r);";
213 pidl "\treturn false;";
217 pidl " * carry over the pointer count to the reply in case we are";
218 pidl " * using full pointer. See NDR specification for full pointers";
220 pidl "push->ptr_count = pull->ptr_count;";
222 pidl "ndr_err = call->ndr_push(push, NDR_OUT, r);";
223 pidl "if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {";
224 pidl "\ttalloc_free(r);";
225 pidl "\treturn false;";
228 pidl "p->out_data.rdata = ndr_push_blob(push);";
229 pidl "talloc_steal(p->mem_ctx, p->out_data.rdata.data);";
231 pidl "talloc_free(r);";
239 sub ParseInterface($)
243 my $uif = uc($if->{NAME});
245 pidl_hdr "#ifndef __SRV_$uif\__";
246 pidl_hdr "#define __SRV_$uif\__";
248 foreach (@{$if->{FUNCTIONS}}) {
249 next if ($_->{PROPERTIES}{noopnum});
250 ParseFunction($if, $_);
255 pidl "static const struct api_struct api_$if->{NAME}_cmds[] = ";
259 foreach (@{$if->{FUNCTIONS}}) {
260 next if ($_->{PROPERTIES}{noopnum});
261 pidl "{\"" . uc($_->{NAME}) . "\", NDR_" . uc($_->{NAME}) . ", api_$_->{NAME}},";
269 pidl_hdr "const struct api_struct *$if->{NAME}_get_pipe_fns(int *n_fns);";
270 pidl "const struct api_struct *$if->{NAME}_get_pipe_fns(int *n_fns)";
273 pidl "*n_fns = sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct);";
274 pidl "return api_$if->{NAME}_cmds;";
279 if (not has_property($if, "no_srv_register")) {
280 pidl_hdr "struct rpc_srv_callbacks;";
281 pidl_hdr "NTSTATUS rpc_$if->{NAME}_init(const struct rpc_srv_callbacks *rpc_srv_cb);";
282 pidl "NTSTATUS rpc_$if->{NAME}_init(const struct rpc_srv_callbacks *rpc_srv_cb)";
284 pidl "\treturn rpc_srv_register(SMB_RPC_INTERFACE_VERSION, \"$if->{NAME}\", \"$if->{NAME}\", \&ndr_table_$if->{NAME}, api_$if->{NAME}_cmds, sizeof(api_$if->{NAME}_cmds) / sizeof(struct api_struct), rpc_srv_cb);";
289 pidl_hdr "NTSTATUS rpc_$if->{NAME}_shutdown(void);";
290 pidl "NTSTATUS rpc_$if->{NAME}_shutdown(void)";
292 pidl "\treturn rpc_srv_unregister(\&ndr_table_$if->{NAME});";
295 pidl_hdr "#endif /* __SRV_$uif\__ */";
300 my($ndr,$header,$ndr_header) = @_;
305 pidl " * Unix SMB/CIFS implementation.";
306 pidl " * server auto-generated by pidl. DO NOT MODIFY!";
309 pidl "#include \"includes.h\"";
310 pidl "#include \"ntdomain.h\"";
311 pidl "#include \"$header\"";
312 pidl_hdr "#include \"$ndr_header\"";
316 ParseInterface($_) if ($_->{TYPE} eq "INTERFACE");
319 return pidl_return();