| 1 |
################################################### |
| 2 |
# client calls generator |
| 3 |
# Copyright [email protected] 2003 |
| 4 |
# Copyright [email protected] 2005-2006 |
| 5 |
# released under the GNU GPL |
| 6 |
|
| 7 |
package Parse::Pidl::Samba4::NDR::Client; |
| 8 |
use parent Parse::Pidl::Base; |
| 9 |
|
| 10 |
use Parse::Pidl qw(fatal warning error); |
| 11 |
use Parse::Pidl::Util qw(has_property ParseExpr genpad); |
| 12 |
use Parse::Pidl::NDR qw(ContainsPipe); |
| 13 |
use Parse::Pidl::Typelist qw(mapTypeName); |
| 14 |
use Parse::Pidl::Samba4 qw(choose_header is_intree DeclLong); |
| 15 |
use Parse::Pidl::Samba4::Header qw(GenerateFunctionInEnv GenerateFunctionOutEnv); |
| 16 |
|
| 17 |
|
| 18 |
use vars qw($VERSION); |
| 19 |
$VERSION = '0.01'; |
| 20 |
|
| 21 |
use strict; |
| 22 |
use warnings; |
| 23 |
|
| 24 |
sub fn_declare($$) { my ($self,$n) = @_; $self->pidl($n); $self->pidl_hdr("$n;"); } |
| 25 |
sub new($) |
| 26 |
{ |
| 27 |
my ($class) = shift; |
| 28 |
my $self = { res => "", res_hdr => "", tabs => "" }; |
| 29 |
bless($self, $class); |
| 30 |
} |
| 31 |
|
| 32 |
sub ParseFunctionHasPipes($$) |
| 33 |
{ |
| 34 |
my ($self, $fn) = @_; |
| 35 |
|
| 36 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 37 |
return 1 if ContainsPipe($e, $e->{LEVELS}[0]); |
| 38 |
} |
| 39 |
|
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
sub ParseFunction_r_State($$$$) |
| 44 |
{ |
| 45 |
my ($self, $if, $fn, $name) = @_; |
| 46 |
my $uname = uc $name; |
| 47 |
|
| 48 |
$self->pidl("struct dcerpc_$name\_r_state {"); |
| 49 |
$self->indent; |
| 50 |
$self->pidl("TALLOC_CTX *out_mem_ctx;"); |
| 51 |
$self->deindent; |
| 52 |
$self->pidl("};"); |
| 53 |
$self->pidl(""); |
| 54 |
$self->pidl("static void dcerpc_$name\_r_done(struct tevent_req *subreq);"); |
| 55 |
$self->pidl(""); |
| 56 |
} |
| 57 |
|
| 58 |
sub ParseFunction_r_Send($$$$) |
| 59 |
{ |
| 60 |
my ($self, $if, $fn, $name) = @_; |
| 61 |
my $uname = uc $name; |
| 62 |
|
| 63 |
my $proto = "struct tevent_req *dcerpc_$name\_r_send(TALLOC_CTX *mem_ctx,\n"; |
| 64 |
$proto .= "\tstruct tevent_context *ev,\n", |
| 65 |
$proto .= "\tstruct dcerpc_binding_handle *h,\n", |
| 66 |
$proto .= "\tstruct $name *r)"; |
| 67 |
|
| 68 |
$self->fn_declare($proto); |
| 69 |
|
| 70 |
$self->pidl("{"); |
| 71 |
$self->indent; |
| 72 |
|
| 73 |
$self->pidl("struct tevent_req *req;"); |
| 74 |
$self->pidl("struct dcerpc_$name\_r_state *state;"); |
| 75 |
$self->pidl("struct tevent_req *subreq;"); |
| 76 |
$self->pidl(""); |
| 77 |
|
| 78 |
$self->pidl("req = tevent_req_create(mem_ctx, &state,"); |
| 79 |
$self->pidl("\t\t\tstruct dcerpc_$name\_r_state);"); |
| 80 |
$self->pidl("if (req == NULL) {"); |
| 81 |
$self->indent; |
| 82 |
$self->pidl("return NULL;"); |
| 83 |
$self->deindent; |
| 84 |
$self->pidl("}"); |
| 85 |
$self->pidl(""); |
| 86 |
|
| 87 |
my $out_params = 0; |
| 88 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 89 |
next unless grep(/out/, @{$e->{DIRECTION}}); |
| 90 |
next if ContainsPipe($e, $e->{LEVELS}[0]); |
| 91 |
$out_params++; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
my $submem; |
| 96 |
if ($out_params > 0) { |
| 97 |
$self->pidl("state->out_mem_ctx = talloc_new(state);"); |
| 98 |
$self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {"); |
| 99 |
$self->indent; |
| 100 |
$self->pidl("return tevent_req_post(req, ev);"); |
| 101 |
$self->deindent; |
| 102 |
$self->pidl("}"); |
| 103 |
$submem = "state->out_mem_ctx"; |
| 104 |
} else { |
| 105 |
$self->pidl("state->out_mem_ctx = NULL;"); |
| 106 |
$submem = "state"; |
| 107 |
} |
| 108 |
$self->pidl(""); |
| 109 |
|
| 110 |
$self->pidl("subreq = dcerpc_binding_handle_call_send(state, ev, h,"); |
| 111 |
$self->pidl("\t\tNULL, &ndr_table_$if->{NAME},"); |
| 112 |
$self->pidl("\t\tNDR_$uname, $submem, r);"); |
| 113 |
$self->pidl("if (tevent_req_nomem(subreq, req)) {"); |
| 114 |
$self->indent; |
| 115 |
$self->pidl("return tevent_req_post(req, ev);"); |
| 116 |
$self->deindent; |
| 117 |
$self->pidl("}"); |
| 118 |
$self->pidl("tevent_req_set_callback(subreq, dcerpc_$name\_r_done, req);"); |
| 119 |
$self->pidl(""); |
| 120 |
|
| 121 |
$self->pidl("return req;"); |
| 122 |
$self->deindent; |
| 123 |
$self->pidl("}"); |
| 124 |
$self->pidl(""); |
| 125 |
} |
| 126 |
|
| 127 |
sub ParseFunction_r_Done($$$$) |
| 128 |
{ |
| 129 |
my ($self, $if, $fn, $name) = @_; |
| 130 |
my $uname = uc $name; |
| 131 |
|
| 132 |
my $proto = "static void dcerpc_$name\_r_done(struct tevent_req *subreq)"; |
| 133 |
|
| 134 |
$self->pidl("$proto"); |
| 135 |
$self->pidl("{"); |
| 136 |
$self->indent; |
| 137 |
|
| 138 |
$self->pidl("struct tevent_req *req ="); |
| 139 |
$self->pidl("\ttevent_req_callback_data(subreq,"); |
| 140 |
$self->pidl("\tstruct tevent_req);"); |
| 141 |
$self->pidl("NTSTATUS status;"); |
| 142 |
$self->pidl(""); |
| 143 |
|
| 144 |
$self->pidl("status = dcerpc_binding_handle_call_recv(subreq);"); |
| 145 |
$self->pidl("TALLOC_FREE(subreq);"); |
| 146 |
$self->pidl("if (tevent_req_nterror(req, status)) {"); |
| 147 |
$self->indent; |
| 148 |
$self->pidl("return;"); |
| 149 |
$self->deindent; |
| 150 |
$self->pidl("}"); |
| 151 |
$self->pidl(""); |
| 152 |
|
| 153 |
$self->pidl("tevent_req_done(req);"); |
| 154 |
$self->deindent; |
| 155 |
$self->pidl("}"); |
| 156 |
$self->pidl(""); |
| 157 |
} |
| 158 |
|
| 159 |
sub ParseFunction_r_Recv($$$$) |
| 160 |
{ |
| 161 |
my ($self, $if, $fn, $name) = @_; |
| 162 |
my $uname = uc $name; |
| 163 |
|
| 164 |
my $proto = "NTSTATUS dcerpc_$name\_r_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx)"; |
| 165 |
|
| 166 |
$self->fn_declare($proto); |
| 167 |
|
| 168 |
$self->pidl("{"); |
| 169 |
$self->indent; |
| 170 |
|
| 171 |
$self->pidl("struct dcerpc_$name\_r_state *state ="); |
| 172 |
$self->pidl("\ttevent_req_data(req,"); |
| 173 |
$self->pidl("\tstruct dcerpc_$name\_r_state);"); |
| 174 |
$self->pidl("NTSTATUS status;"); |
| 175 |
$self->pidl(""); |
| 176 |
|
| 177 |
$self->pidl("if (tevent_req_is_nterror(req, &status)) {"); |
| 178 |
$self->indent; |
| 179 |
$self->pidl("tevent_req_received(req);"); |
| 180 |
$self->pidl("return status;"); |
| 181 |
$self->deindent; |
| 182 |
$self->pidl("}"); |
| 183 |
$self->pidl(""); |
| 184 |
|
| 185 |
$self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);"); |
| 186 |
$self->pidl(""); |
| 187 |
|
| 188 |
$self->pidl("tevent_req_received(req);"); |
| 189 |
$self->pidl("return NT_STATUS_OK;"); |
| 190 |
$self->deindent; |
| 191 |
$self->pidl("}"); |
| 192 |
$self->pidl(""); |
| 193 |
} |
| 194 |
|
| 195 |
sub ParseFunction_r_Sync($$$$) |
| 196 |
{ |
| 197 |
my ($self, $if, $fn, $name) = @_; |
| 198 |
my $uname = uc $name; |
| 199 |
|
| 200 |
if ($self->ParseFunctionHasPipes($fn)) { |
| 201 |
$self->pidl_both("/*"); |
| 202 |
$self->pidl_both(" * The following function is skipped because"); |
| 203 |
$self->pidl_both(" * it uses pipes:"); |
| 204 |
$self->pidl_both(" *"); |
| 205 |
$self->pidl_both(" * dcerpc_$name\_r()"); |
| 206 |
$self->pidl_both(" */"); |
| 207 |
$self->pidl_both(""); |
| 208 |
return; |
| 209 |
} |
| 210 |
|
| 211 |
my $proto = "NTSTATUS dcerpc_$name\_r(struct dcerpc_binding_handle *h, TALLOC_CTX *mem_ctx, struct $name *r)"; |
| 212 |
|
| 213 |
$self->fn_declare($proto); |
| 214 |
|
| 215 |
$self->pidl("{"); |
| 216 |
$self->indent; |
| 217 |
$self->pidl("NTSTATUS status;"); |
| 218 |
$self->pidl(""); |
| 219 |
|
| 220 |
$self->pidl("status = dcerpc_binding_handle_call(h,"); |
| 221 |
$self->pidl("\t\tNULL, &ndr_table_$if->{NAME},"); |
| 222 |
$self->pidl("\t\tNDR_$uname, mem_ctx, r);"); |
| 223 |
$self->pidl(""); |
| 224 |
$self->pidl("return status;"); |
| 225 |
|
| 226 |
$self->deindent; |
| 227 |
$self->pidl("}"); |
| 228 |
$self->pidl(""); |
| 229 |
} |
| 230 |
|
| 231 |
sub ElementDirection($) |
| 232 |
{ |
| 233 |
my ($e) = @_; |
| 234 |
|
| 235 |
return "[in,out]" if (has_property($e, "in") and has_property($e, "out")); |
| 236 |
return "[in]" if (has_property($e, "in")); |
| 237 |
return "[out]" if (has_property($e, "out")); |
| 238 |
return "[in,out]"; |
| 239 |
} |
| 240 |
|
| 241 |
sub HeaderProperties($$) |
| 242 |
{ |
| 243 |
my($props,$ignores) = @_; |
| 244 |
my $ret = ""; |
| 245 |
|
| 246 |
foreach my $d (sort(keys %{$props})) { |
| 247 |
next if (grep(/^$d$/, @$ignores)); |
| 248 |
if($props->{$d} ne "1") { |
| 249 |
$ret.= "$d($props->{$d}),"; |
| 250 |
} else { |
| 251 |
$ret.="$d,"; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
if ($ret) { |
| 256 |
return "[" . substr($ret, 0, -1) . "]"; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
sub ParseCopyArgument($$$$$) |
| 261 |
{ |
| 262 |
my ($self, $fn, $e, $r, $i) = @_; |
| 263 |
my $l = $e->{LEVELS}[0]; |
| 264 |
|
| 265 |
if ($l->{TYPE} eq "ARRAY" and $l->{IS_FIXED} == 1) { |
| 266 |
$self->pidl("memcpy(${r}$e->{NAME}, ${i}$e->{NAME}, sizeof(${r}$e->{NAME}));"); |
| 267 |
} else { |
| 268 |
$self->pidl("${r}$e->{NAME} = ${i}$e->{NAME};"); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
sub ParseInvalidResponse($$) |
| 273 |
{ |
| 274 |
my ($self, $type) = @_; |
| 275 |
|
| 276 |
if ($type eq "sync") { |
| 277 |
$self->pidl("return NT_STATUS_INVALID_NETWORK_RESPONSE;"); |
| 278 |
} elsif ($type eq "async") { |
| 279 |
$self->pidl("tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);"); |
| 280 |
$self->pidl("return;"); |
| 281 |
} else { |
| 282 |
die("ParseInvalidResponse($type)"); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
sub ParseOutputArgument($$$$$$) |
| 287 |
{ |
| 288 |
my ($self, $fn, $e, $r, $o, $invalid_response_type) = @_; |
| 289 |
my $level = 0; |
| 290 |
|
| 291 |
if ($e->{LEVELS}[0]->{TYPE} ne "POINTER" and $e->{LEVELS}[0]->{TYPE} ne "ARRAY") { |
| 292 |
fatal($e->{ORIGINAL}, "[out] argument is not a pointer or array"); |
| 293 |
return; |
| 294 |
} |
| 295 |
|
| 296 |
if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { |
| 297 |
$level = 1; |
| 298 |
if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") { |
| 299 |
$self->pidl("if ($o$e->{NAME} && ${r}out.$e->{NAME}) {"); |
| 300 |
$self->indent; |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
if ($e->{LEVELS}[$level]->{TYPE} eq "ARRAY") { |
| 305 |
# This is a call to GenerateFunctionInEnv intentionally. |
| 306 |
# Since the data is being copied into a user-provided data |
| 307 |
# structure, the user should be able to know the size beforehand |
| 308 |
# to allocate a structure of the right size. |
| 309 |
my $in_env = GenerateFunctionInEnv($fn, $r); |
| 310 |
my $out_env = GenerateFunctionOutEnv($fn, $r); |
| 311 |
my $l = $e->{LEVELS}[$level]; |
| 312 |
|
| 313 |
my $in_var = undef; |
| 314 |
if (grep(/in/, @{$e->{DIRECTION}})) { |
| 315 |
$in_var = ParseExpr($e->{NAME}, $in_env, $e->{ORIGINAL}); |
| 316 |
} |
| 317 |
my $out_var = ParseExpr($e->{NAME}, $out_env, $e->{ORIGINAL}); |
| 318 |
|
| 319 |
my $in_size_is = undef; |
| 320 |
my $out_size_is = undef; |
| 321 |
my $out_length_is = undef; |
| 322 |
|
| 323 |
my $avail_len = undef; |
| 324 |
my $needed_len = undef; |
| 325 |
|
| 326 |
$self->pidl("{"); |
| 327 |
$self->indent; |
| 328 |
my $copy_len_var = "_copy_len_$e->{NAME}"; |
| 329 |
$self->pidl("size_t $copy_len_var;"); |
| 330 |
|
| 331 |
if (not defined($l->{SIZE_IS})) { |
| 332 |
if (not $l->{IS_ZERO_TERMINATED}) { |
| 333 |
fatal($e->{ORIGINAL}, "no size known for [out] array `$e->{NAME}'"); |
| 334 |
} |
| 335 |
if (has_property($e, "charset")) { |
| 336 |
$avail_len = "ndr_charset_length($in_var, CH_UNIX)"; |
| 337 |
$needed_len = "ndr_charset_length($out_var, CH_UNIX)"; |
| 338 |
} else { |
| 339 |
$avail_len = "ndr_string_length($in_var, sizeof(*$in_var))"; |
| 340 |
$needed_len = "ndr_string_length($out_var, sizeof(*$out_var))"; |
| 341 |
} |
| 342 |
$in_size_is = ""; |
| 343 |
$out_size_is = ""; |
| 344 |
$out_length_is = ""; |
| 345 |
} else { |
| 346 |
$in_size_is = ParseExpr($l->{SIZE_IS}, $in_env, $e->{ORIGINAL}); |
| 347 |
$out_size_is = ParseExpr($l->{SIZE_IS}, $out_env, $e->{ORIGINAL}); |
| 348 |
$out_length_is = $out_size_is; |
| 349 |
if (defined($l->{LENGTH_IS})) { |
| 350 |
$out_length_is = ParseExpr($l->{LENGTH_IS}, $out_env, $e->{ORIGINAL}); |
| 351 |
} |
| 352 |
if (has_property($e, "charset")) { |
| 353 |
if (defined($in_var)) { |
| 354 |
$avail_len = "ndr_charset_length($in_var, CH_UNIX)"; |
| 355 |
} else { |
| 356 |
$avail_len = $out_length_is; |
| 357 |
} |
| 358 |
$needed_len = "ndr_charset_length($out_var, CH_UNIX)"; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
if ($out_size_is ne $in_size_is) { |
| 363 |
$self->pidl("if (($out_size_is) > ($in_size_is)) {"); |
| 364 |
$self->indent; |
| 365 |
$self->ParseInvalidResponse($invalid_response_type); |
| 366 |
$self->deindent; |
| 367 |
$self->pidl("}"); |
| 368 |
} |
| 369 |
if ($out_length_is ne $out_size_is) { |
| 370 |
$self->pidl("if (($out_length_is) > ($out_size_is)) {"); |
| 371 |
$self->indent; |
| 372 |
$self->ParseInvalidResponse($invalid_response_type); |
| 373 |
$self->deindent; |
| 374 |
$self->pidl("}"); |
| 375 |
} |
| 376 |
if (defined($needed_len)) { |
| 377 |
$self->pidl("$copy_len_var = $needed_len;"); |
| 378 |
$self->pidl("if ($copy_len_var > $avail_len) {"); |
| 379 |
$self->indent; |
| 380 |
$self->ParseInvalidResponse($invalid_response_type); |
| 381 |
$self->deindent; |
| 382 |
$self->pidl("}"); |
| 383 |
} else { |
| 384 |
$self->pidl("$copy_len_var = $out_length_is;"); |
| 385 |
} |
| 386 |
|
| 387 |
my $dest_ptr = "$o$e->{NAME}"; |
| 388 |
my $elem_size = "sizeof(*$dest_ptr)"; |
| 389 |
$self->pidl("if ($dest_ptr != $out_var) {"); |
| 390 |
$self->indent; |
| 391 |
if (has_property($e, "charset")) { |
| 392 |
$dest_ptr = "discard_const_p(uint8_t *, $dest_ptr)"; |
| 393 |
} |
| 394 |
$self->pidl("memcpy($dest_ptr, $out_var, $copy_len_var * $elem_size);"); |
| 395 |
$self->deindent; |
| 396 |
$self->pidl("}"); |
| 397 |
|
| 398 |
$self->deindent; |
| 399 |
$self->pidl("}"); |
| 400 |
} else { |
| 401 |
$self->pidl("*$o$e->{NAME} = *${r}out.$e->{NAME};"); |
| 402 |
} |
| 403 |
|
| 404 |
if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { |
| 405 |
if ($e->{LEVELS}[0]->{POINTER_TYPE} ne "ref") { |
| 406 |
$self->deindent; |
| 407 |
$self->pidl("}"); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
sub ParseFunction_State($$$$) |
| 413 |
{ |
| 414 |
my ($self, $if, $fn, $name) = @_; |
| 415 |
|
| 416 |
my $state_str = "struct dcerpc_$name\_state"; |
| 417 |
my $done_fn = "dcerpc_$name\_done"; |
| 418 |
|
| 419 |
$self->pidl("$state_str {"); |
| 420 |
$self->indent; |
| 421 |
$self->pidl("struct $name orig;"); |
| 422 |
$self->pidl("struct $name tmp;"); |
| 423 |
$self->pidl("TALLOC_CTX *out_mem_ctx;"); |
| 424 |
$self->deindent; |
| 425 |
$self->pidl("};"); |
| 426 |
$self->pidl(""); |
| 427 |
$self->pidl("static void $done_fn(struct tevent_req *subreq);"); |
| 428 |
$self->pidl(""); |
| 429 |
} |
| 430 |
|
| 431 |
sub ParseFunction_Send($$$$) |
| 432 |
{ |
| 433 |
my ($self, $if, $fn, $name) = @_; |
| 434 |
|
| 435 |
my $fn_args = ""; |
| 436 |
my $state_str = "struct dcerpc_$name\_state"; |
| 437 |
my $done_fn = "dcerpc_$name\_done"; |
| 438 |
my $out_mem_ctx = "dcerpc_$name\_out_memory"; |
| 439 |
my $fn_str = "struct tevent_req *dcerpc_$name\_send"; |
| 440 |
my $pad = genpad($fn_str); |
| 441 |
|
| 442 |
$fn_args .= "TALLOC_CTX *mem_ctx"; |
| 443 |
$fn_args .= ",\n" . $pad . "struct tevent_context *ev"; |
| 444 |
$fn_args .= ",\n" . $pad . "struct dcerpc_binding_handle *h"; |
| 445 |
|
| 446 |
foreach (@{$fn->{ELEMENTS}}) { |
| 447 |
my $dir = ElementDirection($_); |
| 448 |
my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); |
| 449 |
$fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; |
| 450 |
} |
| 451 |
|
| 452 |
$self->fn_declare("$fn_str($fn_args)"); |
| 453 |
$self->pidl("{"); |
| 454 |
$self->indent; |
| 455 |
$self->pidl("struct tevent_req *req;"); |
| 456 |
$self->pidl("$state_str *state;"); |
| 457 |
$self->pidl("struct tevent_req *subreq;"); |
| 458 |
$self->pidl(""); |
| 459 |
$self->pidl("req = tevent_req_create(mem_ctx, &state,"); |
| 460 |
$self->pidl("\t\t\t$state_str);"); |
| 461 |
$self->pidl("if (req == NULL) {"); |
| 462 |
$self->indent; |
| 463 |
$self->pidl("return NULL;"); |
| 464 |
$self->deindent; |
| 465 |
$self->pidl("}"); |
| 466 |
$self->pidl("state->out_mem_ctx = NULL;"); |
| 467 |
$self->pidl(""); |
| 468 |
|
| 469 |
$self->pidl("/* In parameters */"); |
| 470 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 471 |
next unless (grep(/in/, @{$e->{DIRECTION}})); |
| 472 |
|
| 473 |
$self->ParseCopyArgument($fn, $e, "state->orig.in.", "_"); |
| 474 |
} |
| 475 |
$self->pidl(""); |
| 476 |
|
| 477 |
my $out_params = 0; |
| 478 |
$self->pidl("/* Out parameters */"); |
| 479 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 480 |
next unless grep(/out/, @{$e->{DIRECTION}}); |
| 481 |
|
| 482 |
$self->ParseCopyArgument($fn, $e, "state->orig.out.", "_"); |
| 483 |
|
| 484 |
next if ContainsPipe($e, $e->{LEVELS}[0]); |
| 485 |
|
| 486 |
$out_params++; |
| 487 |
} |
| 488 |
$self->pidl(""); |
| 489 |
|
| 490 |
if (defined($fn->{RETURN_TYPE})) { |
| 491 |
$self->pidl("/* Result */"); |
| 492 |
$self->pidl("NDR_ZERO_STRUCT(state->orig.out.result);"); |
| 493 |
$self->pidl(""); |
| 494 |
} |
| 495 |
|
| 496 |
if ($out_params > 0) { |
| 497 |
$self->pidl("state->out_mem_ctx = talloc_named_const(state, 0,"); |
| 498 |
$self->pidl("\t\t \"$out_mem_ctx\");"); |
| 499 |
$self->pidl("if (tevent_req_nomem(state->out_mem_ctx, req)) {"); |
| 500 |
$self->indent; |
| 501 |
$self->pidl("return tevent_req_post(req, ev);"); |
| 502 |
$self->deindent; |
| 503 |
$self->pidl("}"); |
| 504 |
$self->pidl(""); |
| 505 |
} |
| 506 |
|
| 507 |
$self->pidl("/* make a temporary copy, that we pass to the dispatch function */"); |
| 508 |
$self->pidl("state->tmp = state->orig;"); |
| 509 |
$self->pidl(""); |
| 510 |
|
| 511 |
$self->pidl("subreq = dcerpc_$name\_r_send(state, ev, h, &state->tmp);"); |
| 512 |
$self->pidl("if (tevent_req_nomem(subreq, req)) {"); |
| 513 |
$self->indent; |
| 514 |
$self->pidl("return tevent_req_post(req, ev);"); |
| 515 |
$self->deindent; |
| 516 |
$self->pidl("}"); |
| 517 |
$self->pidl("tevent_req_set_callback(subreq, $done_fn, req);"); |
| 518 |
$self->pidl("return req;"); |
| 519 |
$self->deindent; |
| 520 |
$self->pidl("}"); |
| 521 |
$self->pidl(""); |
| 522 |
} |
| 523 |
|
| 524 |
sub ParseFunction_Done($$$$) |
| 525 |
{ |
| 526 |
my ($self, $if, $fn, $name) = @_; |
| 527 |
|
| 528 |
my $state_str = "struct dcerpc_$name\_state"; |
| 529 |
my $done_fn = "dcerpc_$name\_done"; |
| 530 |
|
| 531 |
$self->pidl("static void $done_fn(struct tevent_req *subreq)"); |
| 532 |
$self->pidl("{"); |
| 533 |
$self->indent; |
| 534 |
$self->pidl("struct tevent_req *req = tevent_req_callback_data("); |
| 535 |
$self->pidl("\tsubreq, struct tevent_req);"); |
| 536 |
$self->pidl("$state_str *state = tevent_req_data("); |
| 537 |
$self->pidl("\treq, $state_str);"); |
| 538 |
$self->pidl("NTSTATUS status;"); |
| 539 |
$self->pidl("TALLOC_CTX *mem_ctx;"); |
| 540 |
$self->pidl(""); |
| 541 |
|
| 542 |
$self->pidl("if (state->out_mem_ctx) {"); |
| 543 |
$self->indent; |
| 544 |
$self->pidl("mem_ctx = state->out_mem_ctx;"); |
| 545 |
$self->deindent; |
| 546 |
$self->pidl("} else {"); |
| 547 |
$self->indent; |
| 548 |
$self->pidl("mem_ctx = state;"); |
| 549 |
$self->deindent; |
| 550 |
$self->pidl("}"); |
| 551 |
$self->pidl(""); |
| 552 |
|
| 553 |
$self->pidl("status = dcerpc_$name\_r_recv(subreq, mem_ctx);"); |
| 554 |
$self->pidl("TALLOC_FREE(subreq);"); |
| 555 |
$self->pidl("if (tevent_req_nterror(req, status)) {"); |
| 556 |
$self->indent; |
| 557 |
$self->pidl("return;"); |
| 558 |
$self->deindent; |
| 559 |
$self->pidl("}"); |
| 560 |
$self->pidl(""); |
| 561 |
|
| 562 |
$self->pidl("/* Copy out parameters */"); |
| 563 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 564 |
next if ContainsPipe($e, $e->{LEVELS}[0]); |
| 565 |
next unless (grep(/out/, @{$e->{DIRECTION}})); |
| 566 |
|
| 567 |
$self->ParseOutputArgument($fn, $e, |
| 568 |
"state->tmp.", |
| 569 |
"state->orig.out.", |
| 570 |
"async"); |
| 571 |
} |
| 572 |
$self->pidl(""); |
| 573 |
|
| 574 |
if (defined($fn->{RETURN_TYPE})) { |
| 575 |
$self->pidl("/* Copy result */"); |
| 576 |
$self->pidl("state->orig.out.result = state->tmp.out.result;"); |
| 577 |
$self->pidl(""); |
| 578 |
} |
| 579 |
|
| 580 |
$self->pidl("/* Reset temporary structure */"); |
| 581 |
$self->pidl("NDR_ZERO_STRUCT(state->tmp);"); |
| 582 |
$self->pidl(""); |
| 583 |
|
| 584 |
$self->pidl("tevent_req_done(req);"); |
| 585 |
$self->deindent; |
| 586 |
$self->pidl("}"); |
| 587 |
$self->pidl(""); |
| 588 |
} |
| 589 |
|
| 590 |
sub ParseFunction_Recv($$$$) |
| 591 |
{ |
| 592 |
my ($self, $if, $fn, $name) = @_; |
| 593 |
|
| 594 |
my $fn_args = ""; |
| 595 |
my $state_str = "struct dcerpc_$name\_state"; |
| 596 |
my $fn_str = "NTSTATUS dcerpc_$name\_recv"; |
| 597 |
my $pad = genpad($fn_str); |
| 598 |
|
| 599 |
$fn_args .= "struct tevent_req *req,\n" . $pad . "TALLOC_CTX *mem_ctx"; |
| 600 |
|
| 601 |
if (defined($fn->{RETURN_TYPE})) { |
| 602 |
$fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result"; |
| 603 |
} |
| 604 |
|
| 605 |
$self->fn_declare("$fn_str($fn_args)"); |
| 606 |
$self->pidl("{"); |
| 607 |
$self->indent; |
| 608 |
$self->pidl("$state_str *state = tevent_req_data("); |
| 609 |
$self->pidl("\treq, $state_str);"); |
| 610 |
$self->pidl("NTSTATUS status;"); |
| 611 |
$self->pidl(""); |
| 612 |
$self->pidl("if (tevent_req_is_nterror(req, &status)) {"); |
| 613 |
$self->indent; |
| 614 |
$self->pidl("tevent_req_received(req);"); |
| 615 |
$self->pidl("return status;"); |
| 616 |
$self->deindent; |
| 617 |
$self->pidl("}"); |
| 618 |
$self->pidl(""); |
| 619 |
|
| 620 |
$self->pidl("/* Steal possible out parameters to the callers context */"); |
| 621 |
$self->pidl("talloc_steal(mem_ctx, state->out_mem_ctx);"); |
| 622 |
$self->pidl(""); |
| 623 |
|
| 624 |
if (defined($fn->{RETURN_TYPE})) { |
| 625 |
$self->pidl("/* Return result */"); |
| 626 |
$self->pidl("*result = state->orig.out.result;"); |
| 627 |
$self->pidl(""); |
| 628 |
} |
| 629 |
|
| 630 |
$self->pidl("tevent_req_received(req);"); |
| 631 |
$self->pidl("return NT_STATUS_OK;"); |
| 632 |
$self->deindent; |
| 633 |
$self->pidl("}"); |
| 634 |
$self->pidl(""); |
| 635 |
} |
| 636 |
|
| 637 |
sub ParseFunction_Sync($$$$) |
| 638 |
{ |
| 639 |
my ($self, $if, $fn, $name) = @_; |
| 640 |
|
| 641 |
if ($self->ParseFunctionHasPipes($fn)) { |
| 642 |
$self->pidl_both("/*"); |
| 643 |
$self->pidl_both(" * The following function is skipped because"); |
| 644 |
$self->pidl_both(" * it uses pipes:"); |
| 645 |
$self->pidl_both(" *"); |
| 646 |
$self->pidl_both(" * dcerpc_$name()"); |
| 647 |
$self->pidl_both(" */"); |
| 648 |
$self->pidl_both(""); |
| 649 |
return; |
| 650 |
} |
| 651 |
|
| 652 |
my $uname = uc $name; |
| 653 |
my $fn_args = ""; |
| 654 |
my $fn_str = "NTSTATUS dcerpc_$name"; |
| 655 |
my $pad = genpad($fn_str); |
| 656 |
|
| 657 |
$fn_args .= "struct dcerpc_binding_handle *h,\n" . $pad . "TALLOC_CTX *mem_ctx"; |
| 658 |
|
| 659 |
foreach (@{$fn->{ELEMENTS}}) { |
| 660 |
my $dir = ElementDirection($_); |
| 661 |
my $prop = HeaderProperties($_->{PROPERTIES}, ["in", "out"]); |
| 662 |
$fn_args .= ",\n" . $pad . DeclLong($_, "_") . " /* $dir $prop */"; |
| 663 |
} |
| 664 |
|
| 665 |
if (defined($fn->{RETURN_TYPE})) { |
| 666 |
$fn_args .= ",\n" . $pad . mapTypeName($fn->{RETURN_TYPE}). " *result"; |
| 667 |
} |
| 668 |
|
| 669 |
$self->fn_declare("$fn_str($fn_args)"); |
| 670 |
$self->pidl("{"); |
| 671 |
$self->indent; |
| 672 |
$self->pidl("struct $name r;"); |
| 673 |
$self->pidl("NTSTATUS status;"); |
| 674 |
$self->pidl(""); |
| 675 |
|
| 676 |
$self->pidl("/* In parameters */"); |
| 677 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 678 |
next unless (grep(/in/, @{$e->{DIRECTION}})); |
| 679 |
|
| 680 |
$self->ParseCopyArgument($fn, $e, "r.in.", "_"); |
| 681 |
} |
| 682 |
$self->pidl(""); |
| 683 |
|
| 684 |
$self->pidl("/* Out parameters */"); |
| 685 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 686 |
next unless grep(/out/, @{$e->{DIRECTION}}); |
| 687 |
|
| 688 |
$self->ParseCopyArgument($fn, $e, "r.out.", "_"); |
| 689 |
} |
| 690 |
$self->pidl(""); |
| 691 |
|
| 692 |
if (defined($fn->{RETURN_TYPE})) { |
| 693 |
$self->pidl("/* Result */"); |
| 694 |
$self->pidl("NDR_ZERO_STRUCT(r.out.result);"); |
| 695 |
$self->pidl(""); |
| 696 |
} |
| 697 |
|
| 698 |
$self->pidl("status = dcerpc_$name\_r(h, mem_ctx, &r);"); |
| 699 |
$self->pidl("if (!NT_STATUS_IS_OK(status)) {"); |
| 700 |
$self->indent; |
| 701 |
$self->pidl("return status;"); |
| 702 |
$self->deindent; |
| 703 |
$self->pidl("}"); |
| 704 |
$self->pidl(""); |
| 705 |
|
| 706 |
$self->pidl("/* Return variables */"); |
| 707 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 708 |
next if ContainsPipe($e, $e->{LEVELS}[0]); |
| 709 |
next unless (grep(/out/, @{$e->{DIRECTION}})); |
| 710 |
|
| 711 |
$self->ParseOutputArgument($fn, $e, "r.", "_", "sync"); |
| 712 |
} |
| 713 |
$self->pidl(""); |
| 714 |
|
| 715 |
$self->pidl("/* Return result */"); |
| 716 |
if ($fn->{RETURN_TYPE}) { |
| 717 |
$self->pidl("*result = r.out.result;"); |
| 718 |
} |
| 719 |
$self->pidl(""); |
| 720 |
|
| 721 |
$self->pidl("return NT_STATUS_OK;"); |
| 722 |
|
| 723 |
$self->deindent; |
| 724 |
$self->pidl("}"); |
| 725 |
$self->pidl(""); |
| 726 |
} |
| 727 |
|
| 728 |
##################################################################### |
| 729 |
# parse a function |
| 730 |
sub ParseFunction($$$) |
| 731 |
{ |
| 732 |
my ($self, $if, $fn) = @_; |
| 733 |
|
| 734 |
if ($self->ParseFunctionHasPipes($fn)) { |
| 735 |
$self->pidl_both("/*"); |
| 736 |
$self->pidl_both(" * The following function is skipped because"); |
| 737 |
$self->pidl_both(" * it uses pipes:"); |
| 738 |
$self->pidl_both(" *"); |
| 739 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_r_send()"); |
| 740 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_r_recv()"); |
| 741 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_r()"); |
| 742 |
$self->pidl_both(" *"); |
| 743 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_send()"); |
| 744 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_recv()"); |
| 745 |
$self->pidl_both(" * dcerpc_$fn->{NAME}()"); |
| 746 |
$self->pidl_both(" */"); |
| 747 |
$self->pidl_both(""); |
| 748 |
warning($fn->{ORIGINAL}, "$fn->{NAME}: dcerpc client does not support pipe yet"); |
| 749 |
return; |
| 750 |
} |
| 751 |
|
| 752 |
$self->ParseFunction_r_State($if, $fn, $fn->{NAME}); |
| 753 |
$self->ParseFunction_r_Send($if, $fn, $fn->{NAME}); |
| 754 |
$self->ParseFunction_r_Done($if, $fn, $fn->{NAME}); |
| 755 |
$self->ParseFunction_r_Recv($if, $fn, $fn->{NAME}); |
| 756 |
$self->ParseFunction_r_Sync($if, $fn, $fn->{NAME}); |
| 757 |
|
| 758 |
foreach my $e (@{$fn->{ELEMENTS}}) { |
| 759 |
next unless (grep(/out/, @{$e->{DIRECTION}})); |
| 760 |
|
| 761 |
my $reason = "is not a pointer or array"; |
| 762 |
|
| 763 |
# TODO: make this fatal at NDR level |
| 764 |
if ($e->{LEVELS}[0]->{TYPE} eq "POINTER") { |
| 765 |
if ($e->{LEVELS}[1]->{TYPE} eq "DATA" and |
| 766 |
Parse::Pidl::Typelist::is_string_type($e->{LEVELS}[1]->{DATA_TYPE})) { |
| 767 |
$reason = "is a pointer to a string type"; |
| 768 |
} elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and |
| 769 |
$e->{LEVELS}[1]->{IS_ZERO_TERMINATED}) { |
| 770 |
next; |
| 771 |
} elsif ($e->{LEVELS}[1]->{TYPE} eq "ARRAY" and |
| 772 |
not defined($e->{LEVELS}[1]->{SIZE_IS})) { |
| 773 |
$reason = "is a pointer to an unsized array"; |
| 774 |
} else { |
| 775 |
next; |
| 776 |
} |
| 777 |
} |
| 778 |
if ($e->{LEVELS}[0]->{TYPE} eq "ARRAY") { |
| 779 |
if (not defined($e->{LEVELS}[0]->{SIZE_IS})) { |
| 780 |
$reason = "is an unsized array"; |
| 781 |
} else { |
| 782 |
next; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
$self->pidl_both("/*"); |
| 787 |
$self->pidl_both(" * The following functions are skipped because"); |
| 788 |
$self->pidl_both(" * an [out] argument $e->{NAME} $reason:"); |
| 789 |
$self->pidl_both(" *"); |
| 790 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_send()"); |
| 791 |
$self->pidl_both(" * dcerpc_$fn->{NAME}_recv()"); |
| 792 |
$self->pidl_both(" * dcerpc_$fn->{NAME}()"); |
| 793 |
$self->pidl_both(" */"); |
| 794 |
$self->pidl_both(""); |
| 795 |
|
| 796 |
error($e->{ORIGINAL}, "$fn->{NAME}: [out] argument '$e->{NAME}' $reason, skip client functions"); |
| 797 |
return; |
| 798 |
} |
| 799 |
|
| 800 |
$self->ParseFunction_State($if, $fn, $fn->{NAME}); |
| 801 |
$self->ParseFunction_Send($if, $fn, $fn->{NAME}); |
| 802 |
$self->ParseFunction_Done($if, $fn, $fn->{NAME}); |
| 803 |
$self->ParseFunction_Recv($if, $fn, $fn->{NAME}); |
| 804 |
$self->ParseFunction_Sync($if, $fn, $fn->{NAME}); |
| 805 |
|
| 806 |
$self->pidl_hdr(""); |
| 807 |
} |
| 808 |
|
| 809 |
my %done; |
| 810 |
|
| 811 |
##################################################################### |
| 812 |
# parse the interface definitions |
| 813 |
sub ParseInterface($$) |
| 814 |
{ |
| 815 |
my ($self, $if) = @_; |
| 816 |
my $ifu = uc($if->{NAME}); |
| 817 |
|
| 818 |
$self->pidl_hdr("#ifndef _HEADER_RPC_$if->{NAME}"); |
| 819 |
$self->pidl_hdr("#define _HEADER_RPC_$if->{NAME}"); |
| 820 |
$self->pidl_hdr(""); |
| 821 |
|
| 822 |
if (defined $if->{PROPERTIES}->{uuid}) { |
| 823 |
$self->pidl_hdr("extern const struct ndr_interface_table ndr_table_$if->{NAME};"); |
| 824 |
$self->pidl_hdr(""); |
| 825 |
} |
| 826 |
|
| 827 |
$self->pidl("/* $if->{NAME} - client functions generated by pidl */"); |
| 828 |
$self->pidl(""); |
| 829 |
|
| 830 |
foreach my $fn (@{$if->{FUNCTIONS}}) { |
| 831 |
next if defined($done{$fn->{NAME}}); |
| 832 |
next if has_property($fn, "noopnum"); |
| 833 |
next if has_property($fn, "todo"); |
| 834 |
$self->ParseFunction($if, $fn); |
| 835 |
$done{$fn->{NAME}} = 1; |
| 836 |
} |
| 837 |
|
| 838 |
$self->pidl_hdr("#endif /* _HEADER_RPC_$if->{NAME} */"); |
| 839 |
} |
| 840 |
|
| 841 |
sub Parse($$$$$$) |
| 842 |
{ |
| 843 |
my($self,$ndr,$header,$ndr_header,$client_header) = @_; |
| 844 |
|
| 845 |
$self->pidl("/* client functions auto-generated by pidl */"); |
| 846 |
$self->pidl(""); |
| 847 |
if (is_intree()) { |
| 848 |
$self->pidl("#include \"includes.h\""); |
| 849 |
} else { |
| 850 |
$self->pidl("#ifndef _GNU_SOURCE"); |
| 851 |
$self->pidl("#define _GNU_SOURCE"); |
| 852 |
$self->pidl("#endif"); |
| 853 |
$self->pidl("#include <stdio.h>"); |
| 854 |
$self->pidl("#include <stdbool.h>"); |
| 855 |
$self->pidl("#include <stdlib.h>"); |
| 856 |
$self->pidl("#include <stdint.h>"); |
| 857 |
$self->pidl("#include <stdarg.h>"); |
| 858 |
$self->pidl("#include <string.h>"); |
| 859 |
$self->pidl("#include <core/ntstatus.h>"); |
| 860 |
} |
| 861 |
$self->pidl("#include <tevent.h>"); |
| 862 |
$self->pidl(choose_header("lib/util/tevent_ntstatus.h", "util/tevent_ntstatus.h").""); |
| 863 |
$self->pidl("#include \"$ndr_header\""); |
| 864 |
$self->pidl("#include \"$client_header\""); |
| 865 |
$self->pidl(""); |
| 866 |
|
| 867 |
$self->pidl_hdr(choose_header("librpc/rpc/dcerpc.h", "dcerpc.h").""); |
| 868 |
$self->pidl_hdr("#include \"$header\""); |
| 869 |
|
| 870 |
foreach my $x (@{$ndr}) { |
| 871 |
($x->{TYPE} eq "INTERFACE") && $self->ParseInterface($x); |
| 872 |
} |
| 873 |
|
| 874 |
return ($self->{res},$self->{res_hdr}); |
| 875 |
} |
| 876 |
|
| 877 |
1; |