1 ##################################################
2 # Wireshark NDR parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001,2005
5 # Copyright jelmer@samba.org 2004-2007
6 # Portions based on idl2eth.c by Ronnie Sahlberg
7 # released under the GNU GPL
13 Parse::Pidl::Wireshark::NDR - Parser generator for Wireshark
17 package Parse
::Pidl
::Wireshark
::NDR
;
21 @EXPORT_OK = qw(field2name %res PrintIdl StripPrefixes RegisterInterfaceHandoff register_hf_field CheckUsed ProcessImport ProcessInclude find_type DumpEttList DumpEttDeclaration DumpHfList DumpHfDeclaration DumpFunctionTable register_type register_ett);
25 use Parse::Pidl qw(error warning);
26 use Parse
::Pidl
::Typelist
qw(getType mapScalarType);
27 use Parse
::Pidl
::Util
qw(has_property property_matches make_str);
28 use Parse
::Pidl
::NDR
qw(ContainsString GetNextLevel GetPrevLevel);
29 use Parse
::Pidl
::Dump
qw(DumpType DumpFunction);
30 use Parse
::Pidl
::Wireshark
::Conformance
qw(ReadConformance);
33 use vars
qw($VERSION);
36 my %return_types = ();
37 my %dissector_used = ();
39 my %ptrtype_mappings = (
40 "unique" => "NDR_POINTER_UNIQUE",
41 "ref" => "NDR_POINTER_REF",
42 "ptr" => "NDR_POINTER_PTR",
43 "full" => "NDR_POINTER_PTR"
46 my %variable_scalars = (
47 "int1632" => "int32_t",
48 "uint1632" => "uint32_t",
49 "int3264" => "int64_t",
50 "uint3264" => "uint64_t",
53 # map from an IDL type to a C header type, using the on-the-wire length.
54 # Produces different results than mapScalarType in Parse::Pidl::Typelist
55 # for the types that have different wire lengths in NDR and NDR64 (i.e.,
56 # includes the padding for uint1632 and uint3264, unlike that function.)
57 sub mapWireScalarType($)
61 return $variable_scalars{$name} if defined($variable_scalars{$name});
63 return mapScalarType($name);
68 my ($s, $prefixes) = @_;
70 foreach (@$prefixes) {
77 sub ContainsWithinEmbeddedPointer($$)
83 while ($l = GetPrevLevel($e,$l))
86 next if ($l->{TYPE} eq "SWITCH");
87 return 0 if ($l->{TYPE} ne "POINTER");
88 return $ret if ($l->{LEVEL} eq "EMBEDDED");
94 # Convert a IDL structure field name (e.g access_mask) to a prettier
95 # string like 'Access Mask'.
101 $field =~ s/^(_)*//g; # Remove any starting underscores
102 $field =~ s/_/ /g; # Replace underscores with spaces
103 $field =~ s/(\w+)/\u$1/g; # Capitalise each word
111 my $self = {res => {hdr => "", def => "", code => ""}, tabs => "", cur_fn => undef,
112 hf_used => {}, ett => [], conformance => undef
115 bless($self, $class);
118 sub pidl_fn_start($$)
120 my ($self, $fn) = @_;
121 $self->{cur_fn} = $fn;
125 my ($self, $fn) = @_;
126 die("Inconsistent state: $fn != $self->{cur_fn}") if ($fn ne $self->{cur_fn});
127 $self->{cur_fn} = undef;
133 return if (defined($self->{cur_fn}) and defined($self->{conformance}->{manual}->{$self->{cur_fn}}));
136 $self->{res}->{code} .= $self->{tabs};
137 $self->{res}->{code} .= $d;
139 $self->{res}->{code} .="\n";
142 sub pidl_hdr($$) { my ($self,$x) = @_; $self->{res}->{hdr} .= "$x\n"; }
143 sub pidl_def($$) { my ($self,$x) = @_; $self->{res}->{def} .= "$x\n"; }
148 $self->{tabs} .= "\t";
154 $self->{tabs} = substr($self->{tabs}, 0, -1);
159 my ($self, $idl) = @_;
161 foreach (split /\n/, $idl) {
162 $self->pidl_code("/* IDL: $_ */");
165 $self->pidl_code("");
168 #####################################################################
169 # parse the interface definitions
172 my($self, $interface) = @_;
173 $self->Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
174 $self->Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}});
175 $self->Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
180 my ($self, $e,$name,$ifname) = @_;
181 my $valsstring = "$ifname\_$name\_vals";
182 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
184 return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
186 foreach (@{$e->{ELEMENTS}}) {
187 if (/([^=]*)=(.*)/) {
188 $self->pidl_hdr("#define $1 ($2)");
192 $self->pidl_hdr("extern const value_string $valsstring\[];");
193 $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, " . mapWireScalarType($e->{BASE_TYPE}) ." *param _U_);");
195 $self->pidl_def("const value_string ".$valsstring."[] = {");
196 foreach (@{$e->{ELEMENTS}}) {
197 next unless (/([^=]*)=(.*)/);
198 $self->pidl_def("\t{ $1, \"$1\" },");
201 $self->pidl_def("{ 0, NULL }");
202 $self->pidl_def("};");
204 $self->pidl_fn_start($dissectorname);
205 $self->pidl_code("int");
206 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, " . mapWireScalarType($e->{BASE_TYPE}) . " *param _U_)");
207 $self->pidl_code("{");
209 $self->pidl_code(mapWireScalarType($e->{BASE_TYPE}) . " parameter=0;");
210 $self->pidl_code("if (param) {");
212 $self->pidl_code("parameter = *param;");
214 $self->pidl_code("}");
215 $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, di, drep, hf_index, ¶meter);");
216 $self->pidl_code("if (param) {");
218 $self->pidl_code("*param = parameter;");
220 $self->pidl_code("}");
221 $self->pidl_code("return offset;");
223 $self->pidl_code("}\n");
224 $self->pidl_fn_end($dissectorname);
226 my $enum_size = $e->{BASE_TYPE};
227 $enum_size =~ s/uint//g;
228 my $ws_base = "BASE_DEC";
229 $ws_base = "BASE_HEX" if (property_matches($e, "flag", ".*LIBNDR_PRINT_ARRAY_HEX.*"));
230 $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", $ws_base, "0", "VALS($valsstring)", $enum_size / 8);
235 my ($self,$e,$name,$ifname) = @_;
236 error($e->{ORIGINAL}, "Pipe not yet supported");
242 my ($self,$e,$name,$ifname) = @_;
243 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
244 my $element_count = 0;
247 $self->register_ett("ett_$ifname\_$name");
249 $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_);");
251 $self->pidl_fn_start($dissectorname);
252 $self->pidl_code("int");
253 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_)");
254 $self->pidl_code("{");
256 foreach (@{$e->{ELEMENTS}}) {
257 next unless (/([^ ]*) (.*)/);
260 if ($element_count > 0) {
261 $self->pidl_code("proto_item *item;");
262 $self->pidl_code("static int * const $ifname\_$name\_fields[] = {");
264 foreach (@{$e->{ELEMENTS}}) {
265 next unless (/([^ ]*) (.*)/);
266 my ($en,$ev) = ($1,$2);
267 my $hf_bitname = "hf_$ifname\_$name\_$1";
271 $total_ev += hex($ev);
272 $self->pidl_code("&$hf_bitname,");
275 $self->pidl_code("NULL");
277 $self->pidl_code("};");
280 $self->pidl_code(mapWireScalarType($e->{BASE_TYPE}) . " flags;");
281 if ($e->{ALIGN} > 1) {
282 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
285 $self->pidl_code("");
287 if ($element_count > 0) {
288 $self->pidl_code("item = proto_tree_add_bitmask_with_flags(parent_tree, tvb, offset, hf_index,");
289 $self->pidl_code("\t\t\tett_$ifname\_$name, $ifname\_$name\_fields, DREP_ENC_INTEGER(drep), BMT_NO_FALSE);");
290 $self->pidl_code("");
292 $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, parent_tree, di, drep, -1, &flags);");
293 $self->pidl_code("");
295 $self->pidl_code("if (!flags)");
296 $self->pidl_code("\tproto_item_append_text(item, \": (No values set)\");\n");
298 $self->pidl_code("proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, DREP_ENC_INTEGER(drep));");
299 $self->pidl_code("");
301 $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, parent_tree, di, drep, -1, &flags);");
302 $self->pidl_code("");
305 foreach (@{$e->{ELEMENTS}}) {
306 next unless (/([^ ]*) (.*)/);
307 my ($en,$ev) = ($1,$2);
308 my $hf_bitname = "hf_$ifname\_$name\_$en";
309 my $filtername = "$ifname\.$name\.$en";
311 $self->{hf_used}->{$hf_bitname} = 1;
315 $self->register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", "( $ev )", "");
317 $self->pidl_def("static const true_false_string $name\_$en\_tfs = {");
318 if (defined($self->{conformance}->{tfs}->{$hf_bitname})) {
319 $self->pidl_def(" $self->{conformance}->{tfs}->{$hf_bitname}->{TRUE_STRING},");
320 $self->pidl_def(" $self->{conformance}->{tfs}->{$hf_bitname}->{FALSE_STRING},");
321 $self->{conformance}->{tfs}->{$hf_bitname}->{USED} = 1;
323 $self->pidl_def(" \"$en is SET\",");
324 $self->pidl_def(" \"$en is NOT SET\",");
326 $self->pidl_def("};");
330 # Note that if bitmask is full, some compilers would complain about flags &= (~0xffffffff)
331 if ($element_count > 0 && $element_count < 32) {
332 my $total_ev_hex = sprintf("0x%08x", $total_ev);
333 $self->pidl_code("if (flags & (~$total_ev_hex)) {");
334 $self->pidl_code("\tflags &= (~$total_ev_hex);");
335 $self->pidl_code("\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);");
336 $self->pidl_code("}\n");
338 $self->pidl_code("return offset;");
340 $self->pidl_code("}\n");
341 $self->pidl_fn_end($dissectorname);
343 my $size = $e->{BASE_TYPE};
345 $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
348 sub ElementLevel($$$$$$$$)
350 my ($self,$e,$l,$hf,$myname,$pn,$ifname,$param) = @_;
352 if (defined($self->{conformance}->{dissectorparams}->{$myname})) {
353 $param = $self->{conformance}->{dissectorparams}->{$myname}->{PARAM};
356 if ($l->{TYPE} eq "POINTER") {
359 if ($l->{LEVEL} eq "TOP") {
361 } elsif ($l->{LEVEL} eq "EMBEDDED") {
363 $nl = GetNextLevel($e,$l);
366 if (defined($nl) and $nl->{TYPE} eq "SWITCH") {
367 $self->pidl_code("uint64_t saved_switch_level = di->switch_level;");
368 $self->pidl_code("");
369 $self->pidl_code("di->switch_level = $param;");
370 $self->pidl_code("");
373 $self->pidl_code("offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, di, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes})) . " ($e->{TYPE})\",$hf);");
374 if (defined($nl) and $nl->{TYPE} eq "SWITCH") {
375 $self->pidl_code("");
376 $self->pidl_code("di->switch_level = saved_switch_level;");
378 } elsif ($l->{TYPE} eq "ARRAY") {
379 if ($l->{IS_INLINE}) {
380 #print Parse::Pidl::Util::MyDumper($e);
381 #print Parse::Pidl::Util::MyDumper($l);
382 print Parse::Pidl::Util::MyDumper($param);
383 $self->pidl_code("/* TODO $myname IS_INLINE */");
384 error($e->{ORIGINAL}, "Inline arrays not supported");
385 } elsif ($l->{IS_FIXED}) {
386 $self->pidl_code("int i;");
387 $self->pidl_code("for (i = 0; i < $l->{SIZE_IS}; i++)");
388 $self->pidl_code("\toffset = $myname\_(tvb, offset, pinfo, tree, di, drep);");
390 if ($l->{IS_ZERO_TERMINATED}) {
391 $self->pidl_code("char *data = NULL;");
394 if (!$l->{IS_SURROUNDING}) {
395 $self->pidl_code("struct ndr_generic_array nga = { .is_conformant = false, };");
398 if (!$l->{IS_SURROUNDING} and $l->{IS_CONFORMANT}) {
399 $self->pidl_code("");
400 $self->pidl_code("offset = dissect_ndr_conformant_array_hdr(tvb, offset, pinfo, tree, di, drep, $ngavar);");
402 if ($l->{IS_VARYING}) {
403 $self->pidl_code("");
404 $self->pidl_code("offset = dissect_ndr_varying_array_hdr(tvb, offset, pinfo, tree, di, drep, $ngavar);");
407 unless ($l->{IS_ZERO_TERMINATED}) {
408 $self->pidl_code("");
409 $self->pidl_code("offset = dissect_ndr_generic_array_bytes(tvb, offset, pinfo, tree, di, drep, $ngavar, $myname\_);");
411 my $nl = GetNextLevel($e,$l);
412 my $nl_ctype = mapScalarType($nl->{DATA_TYPE});
413 $self->pidl_code("offset = dissect_ndr_generic_array_string(tvb, offset, pinfo, tree, di, drep, sizeof($nl_ctype), $hf, false, $ngavar, &data);");
414 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
417 } elsif ($l->{TYPE} eq "DATA") {
418 if ($l->{DATA_TYPE} eq "string") {
419 my $bs = 2; # Byte size defaults to that of UCS2
422 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
424 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
425 $self->pidl_code("char *data = NULL;");
426 $self->pidl_code("struct ndr_generic_array nga = { .is_conformant = false, };");
427 $self->pidl_code("offset = dissect_ndr_conformant_array_hdr(tvb, offset, pinfo, tree, di, drep, &nga);");
428 $self->pidl_code("offset = dissect_ndr_varying_array_hdr(tvb, offset, pinfo, tree, di, drep, &nga);");
429 $self->pidl_code("offset = dissect_ndr_generic_array_string(tvb, offset, pinfo, tree, di, drep, $bs, $hf, false, &nga, &data);");
430 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
431 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
432 $self->pidl_code("char *data = NULL;");
433 $self->pidl_code("struct ndr_generic_array nga = { .is_conformant = false, };");
434 $self->pidl_code("offset = dissect_ndr_conformant_array_hdr(tvb, offset, pinfo, tree, di, drep, &nga);");
435 $self->pidl_code("offset = dissect_ndr_generic_array_string(tvb, offset, pinfo, tree, di, drep, $bs, $hf, false, &nga, &data);");
436 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
437 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
438 $self->pidl_code("char *data = NULL;");
439 $self->pidl_code("struct ndr_generic_array nga = { .is_conformant = false, };");
440 $self->pidl_code("offset = dissect_ndr_varying_array_hdr(tvb, offset, pinfo, tree, di, drep, &nga);");
441 $self->pidl_code("offset = dissect_ndr_generic_array_string(tvb, offset, pinfo, tree, di, drep, $bs, $hf, false, &nga, &data);");
442 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
443 } elsif (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
445 $self->pidl_code("offset = dissect_null_term_wstring(tvb, offset, pinfo, tree, drep, $hf , 0);")
447 $self->pidl_code("offset = dissect_null_term_string(tvb, offset, pinfo, tree, drep, $hf , 0);")
450 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
452 } elsif ($l->{DATA_TYPE} eq "DATA_BLOB") {
454 $remain = 1 if (property_matches($e->{ORIGINAL}, "flag", ".*LIBNDR_FLAG_REMAINING.*"));
455 $self->pidl_code("offset = dissect_ndr_datablob(tvb, offset, pinfo, tree, di, drep, $hf, $remain);");
459 my $within = ContainsWithinEmbeddedPointer($e, $l);
460 my $pl = GetPrevLevel($e,$l);
461 if ($param ne "0" and $within != 0 and defined($pl) and $pl->{TYPE} eq "SWITCH") {
462 $param = "di->switch_level";
465 if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) {
466 $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};
467 $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
468 } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
469 $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
470 $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
471 } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) {
472 $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
473 $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
476 if (ref($l->{DATA_TYPE}) eq "HASH" ) {
477 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
479 $t = $l->{DATA_TYPE};
482 $self->pidl_code("offset = $ifname\_dissect_struct_" . $t . "(tvb,offset,pinfo,tree,di,drep,$hf,$param);");
487 $call =~ s/\@HF\@/$hf/g;
488 $call =~ s/\@PARAM\@/$param/g;
489 $self->pidl_code($call);
491 } elsif ($_->{TYPE} eq "SUBCONTEXT" and $l->{HEADER_SIZE} == 0xFFFFFC01) {
493 if (has_property($e, "switch_is")) {
494 $varswitch = $e->{PROPERTIES}->{switch_is};
497 my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$_->{NAME}subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, "");
498 $num_bits = 3264 if ($num_bits == 32);
499 $self->{hf_used}->{$hf2} = 1;
500 $self->pidl_code("uint64_t size;");
501 $self->pidl_code("int conformant = di->conformant_run;");
502 $self->pidl_code("tvbuff_t *subtvb;");
503 $self->pidl_code("");
504 # We need to be able to dissect the length of the context in every case
505 # and conformant run skips the dissections of scalars ...
506 $self->pidl_code("if (!conformant) {");
508 $self->pidl_code("uint32_t saved_flags = di->call_data->flags;");
509 $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, di, drep, $hf2, &size);");
510 # This is a subcontext, there is normally no such thing as
511 # 64 bit NDR is subcontext so we clear the flag so that we can
512 # continue to dissect handmarshalled stuff with pidl
513 $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;");
515 $self->pidl_code("subtvb = tvb_new_subset_length_caplen(tvb, offset, (int)size, (int)size);");
517 $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep, $param);");
519 $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep);");
521 $self->pidl_code("offset += (int)size;");
522 $self->pidl_code("di->call_data->flags = saved_flags;");
524 $self->pidl_code("}");
525 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
527 if (has_property($e, "switch_is")) {
528 $varswitch = $e->{PROPERTIES}->{switch_is};
530 my $num_bits = ($l->{HEADER_SIZE}*8);
531 if ($num_bits == 0) {
534 my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$_->{NAME}subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, "");
535 $num_bits = 3264 if ($num_bits == 32);
536 $self->{hf_used}->{$hf2} = 1;
537 $self->pidl_code("uint64_t size;");
538 $self->pidl_code("int conformant = di->conformant_run;");
539 $self->pidl_code("tvbuff_t *subtvb;");
540 $self->pidl_code("");
541 # We need to be able to dissect the length of the context in every case
542 # and conformant run skips the dissections of scalars ...
543 $self->pidl_code("if (!conformant) {");
545 $self->pidl_code("uint32_t saved_flags = di->call_data->flags;");
546 $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, di, drep, $hf2, &size);");
547 # This is a subcontext, there is normally no such thing as
548 # 64 bit NDR is subcontext so we clear the flag so that we can
549 # continue to dissect handmarshalled stuff with pidl
550 $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;");
552 $self->pidl_code("subtvb = tvb_new_subset_length_caplen(tvb, offset, (int)size, (int)size);");
554 $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep, $param);");
556 $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep);");
558 $self->pidl_code("offset += (int)size;");
559 $self->pidl_code("di->call_data->flags = saved_flags;");
561 $self->pidl_code("}");
562 } elsif ($_->{TYPE} eq "PIPE") {
563 error($e->{ORIGINAL}, "Type PIPE not yet supported");
565 die("Unknown type `$_->{TYPE}'");
571 my ($e, $type, $nodiscriminant) = @_;
573 my $switch_dt = getType($type);
574 my $switch_type = undef;
575 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
576 $switch_type = Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
577 } elsif ($switch_dt->{DATA}->{TYPE} eq "BITMAP") {
578 $switch_type = Parse::Pidl::Typelist::bitmap_type_fn($switch_dt->{DATA});
579 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
580 if (defined $e->{SWITCH_TYPE}) {
581 $switch_type = "$e->{SWITCH_TYPE}";
583 $switch_type = "$switch_dt->{DATA}->{NAME}";
585 } elsif (not defined $e->{SWITCH_TYPE}) {
586 $switch_type = $nodiscriminant;
594 my ($self,$e,$pn,$ifname,$isoruseswitch,%switchvars) = @_;
596 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes});
598 my ($call_code, $moreparam);
599 my $l = @{$e->{LEVELS}}[0];
601 my $moreparam_once = 0;
602 if (defined $isoruseswitch) {
603 my $type = $isoruseswitch->[0];
604 my $name = $isoruseswitch->[1];
606 my $switch_dt = getType($type);
607 my $switch_raw_type = SwitchType($e, $type, "uint32");
608 if (not defined($switch_raw_type)) {
609 die("Unknown type[$type]\n");
611 my $switch_type = mapWireScalarType(${switch_raw_type});
614 if (has_property($e, "switch_is")) {
615 $moreparam = ", $switch_type ".$name;
617 $moreparam = ", $switch_type *".$name;
622 if (($e->{PROPERTIES}->{switch_is} eq "") && ($switchvars{$name}) &&
624 (!($type =~ /^uint(8|16|1632|32|3264|64)/))) {
626 } elsif ( $switch_dt->{DATA}->{TYPE} eq "ENUM") {
628 } elsif ($name ne "") {
629 if (has_property($e, "switch_is")) {
637 if (has_property($e, "switch_is")) {
638 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, $name);";
640 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, &$name);";
643 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep);";
645 } elsif ($l->{IS_SURROUNDING} and $l->{IS_CONFORMANT}) {
646 my $cfvarname = "nga_$e->{NAME}";
647 $moreparam = ", struct ndr_generic_array *nga";
649 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, &$cfvarname);";
652 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep);";
656 my $type = $self->find_type($e->{TYPE});
658 if (not defined($type)) {
662 VALSSTRING => "NULL",
663 FT_TYPE => "FT_NONE",
664 BASE_TYPE => "BASE_NONE"
668 if (ContainsString($e)) {
671 VALSSTRING => "NULL",
672 FT_TYPE => "FT_STRING",
673 BASE_TYPE => "BASE_NONE"
676 if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) {
677 my $align_flag = $e->{PROPERTIES}->{flag};
678 if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) {
679 $call_code = "ALIGN_TO_$1_BYTES; ".$call_code;
683 my $hf = $self->register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, "");
684 $self->{hf_used}->{$hf} = 1;
686 my $eltname = StripPrefixes($pn, $self->{conformance}->{strip_prefixes}) . ".$e->{NAME}";
687 if (defined($self->{conformance}->{noemit}->{$eltname})) {
693 my $oldparam = undef;
694 foreach (@{$e->{LEVELS}}) {
695 if (defined $_->{SWITCH_IS}) {
697 #if (($param ne "0") && (!($param =~ /\*/))) {
698 # $param = "*$param";
701 next if ($_->{TYPE} eq "SWITCH");
702 next if (defined($self->{conformance}->{noemit}->{"$dissectorname$add"}));
703 my $within = ContainsWithinEmbeddedPointer($e, $_);
704 my $saved_moreparam = $moreparam;
708 $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_$moreparam);");
709 $self->pidl_fn_start("$dissectorname$add");
710 $self->pidl_code("static int");
711 $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_$moreparam)");
712 $self->pidl_code("{");
715 $moreparam = $saved_moreparam;
717 $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname,$param);
718 if (defined $oldparam) {
722 $self->pidl_code("");
723 $self->pidl_code("return offset;");
725 $self->pidl_code("}\n");
726 $self->pidl_fn_end("$dissectorname$add");
728 $moreparam="" if $moreparam_once;
729 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
737 my ($self, $fn,$ifname) = @_;
741 foreach (@{$fn->{ELEMENTS}}) {
742 $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname, undef, ()) if not defined($dissectornames{$_->{NAME}});
745 my $fn_name = $_->{NAME};
746 $fn_name =~ s/^${ifname}_//;
748 $self->PrintIdl(DumpFunction($fn->{ORIGINAL}));
749 $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_response");
750 $self->pidl_code("static int");
751 $self->pidl_code("$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_)");
752 $self->pidl_code("{");
754 if ( not defined($fn->{RETURN_TYPE})) {
755 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "HRESULT")
757 $self->pidl_code("uint32_t status;\n");
758 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
759 if ($type->{DATA}->{TYPE} eq "ENUM") {
760 $self->pidl_code(Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . "_t status;\n");
761 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
762 $self->pidl_code(mapWireScalarType($fn->{RETURN_TYPE}) . " status;\n");
764 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
767 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
770 $self->pidl_code("di->dcerpc_procedure_name=\"${fn_name}\";");
771 foreach (@{$fn->{ELEMENTS}}) {
772 if (grep(/out/,@{$_->{DIRECTION}})) {
773 $self->pidl_code("$dissectornames{$_->{NAME}}");
774 $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, di, drep);");
775 $self->pidl_code("");
779 if (not defined($fn->{RETURN_TYPE})) {
780 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
781 $self->pidl_code("offset = dissect_ntstatus(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_status, &status);\n");
782 $self->pidl_code("if (status != 0)");
783 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str_ext(status, &NT_errors_ext, \"Unknown NT status 0x%08x\"));\n");
784 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
785 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
786 $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_werror, &status);\n");
787 $self->pidl_code("if (status != 0)");
788 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str_ext(status, &WERR_errors_ext, \"Unknown DOS error 0x%08x\"));\n");
790 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
791 } elsif ($fn->{RETURN_TYPE} eq "HRESULT") {
792 $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_hresult, &status);\n");
793 $self->pidl_code("if (status != 0)");
794 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str_ext(status, &HRES_errors_ext, \"Unknown HRES error 0x%08x\"));\n");
795 $return_types{$ifname}->{"hresult"} = ["HRESULT", "HRES Windows Error"];
796 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
797 if ($type->{DATA}->{TYPE} eq "ENUM") {
798 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
800 $self->pidl_code("offset = $return_dissect(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
801 $self->pidl_code("if (status != 0)");
802 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %s\", val_to_str(status, $ifname\_$fn->{RETURN_TYPE}\_vals, \"Unknown " . $fn->{RETURN_TYPE} . " error 0x%08x\"));\n");
803 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
804 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
805 $self->pidl_code("offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
806 $self->pidl_code("if (status != 0)");
807 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n");
808 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
812 $self->pidl_code("return offset;");
814 $self->pidl_code("}\n");
815 $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_response");
817 $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_request");
818 $self->pidl_code("static int");
819 $self->pidl_code("$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_)");
820 $self->pidl_code("{");
822 $self->pidl_code("di->dcerpc_procedure_name=\"${fn_name}\";");
823 foreach (@{$fn->{ELEMENTS}}) {
824 if (grep(/in/,@{$_->{DIRECTION}})) {
825 $self->pidl_code("$dissectornames{$_->{NAME}}");
826 $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, di, drep);");
831 $self->pidl_code("return offset;");
833 $self->pidl_code("}\n");
834 $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_request");
839 my ($self,$e,$name,$ifname) = @_;
840 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
842 return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
844 $self->register_ett("ett_$ifname\_$name");
848 # will contain the switch var declaration;
851 foreach (@{$e->{ELEMENTS}}) {
852 my $paramvar = undef;
854 if (has_property($_, "switch_is")) {
855 $paramvar = $_->{PROPERTIES}->{switch_is};
858 if ($_->{LEVELS}[0]{IS_INLINE}) {
859 $paramvar = $_->{LEVELS}[0]{SIZE_IS};
862 if (defined($paramvar)) {
863 $paramsrefs->{$paramvar} = [];
864 $paramshash{$paramvar} = $paramvar;
867 # 'IS_CONFORMANT' => 0,
868 # 'IS_DEFERRED' => 0,
871 # 'IS_SURROUNDING' => 0,
874 # 'IS_ZERO_TERMINATED' => 0,
875 # 'LENGTH_IS' => 'name_len',
876 # 'LEVEL_INDEX' => 0,
877 # 'SIZE_IS' => 'name_len',
882 foreach (@{$e->{ELEMENTS}}) {
883 my $paraminfo = undef;
886 if (scalar(grep {/^$v$/} keys(%$paramsrefs)) == 1) {
887 # This element is one of the switch attribute
888 my $switch_raw_type = SwitchType($e, $_->{TYPE}, "uint32");
889 if (not defined($switch_raw_type)) {
890 die("Unknown type[$_->{TYPE}]\n");
892 my $switch_type = mapWireScalarType(${switch_raw_type});
894 if ($switch_type ne "") {
895 push @$vars, "$switch_type $v = 0;";
897 $paraminfo = [ $_->{TYPE}, $v ];
898 $paramsrefs->{$v} = $paraminfo;
901 my $paramvar = undef;
902 if (has_property($_, "switch_is")) {
903 $paramvar = $_->{PROPERTIES}->{switch_is};
906 if ($_->{LEVELS}[0]{IS_INLINE}) {
907 $paramvar = $_->{LEVELS}[0]{SIZE_IS};
910 if (defined($paramvar)) {
911 $paraminfo = $paramsrefs->{$paramvar};
914 $res.="\t".$self->Element($_, $name, $ifname, $paraminfo, %paramshash)."\n\n";
918 if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
920 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
924 my $cfdissectorname = undef;
925 my $cfvarname = undef;
926 if (defined $e->{SURROUNDING_ELEMENT}) {
927 my $se = $e->{SURROUNDING_ELEMENT};
929 $cfdissectorname = "$ifname\_dissect\_conformant\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($se->{NAME}, $self->{conformance}->{strip_prefixes});
930 $cfvarname = "nga_$se->{NAME}";
931 push @$vars, "struct ndr_generic_array $cfvarname = { .is_conformant = false, };";
932 unless (defined($self->{conformance}->{noemit}->{"$cfdissectorname"})) {
934 $self->pidl_def("static int $cfdissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, struct ndr_generic_array *nga);");
935 $self->pidl_fn_start("$dissectorname");
936 $self->pidl_code("static int");
937 $self->pidl_code("$cfdissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, struct ndr_generic_array *nga)");
938 $self->pidl_code("{");
941 $self->pidl_code("offset = dissect_ndr_conformant_array_hdr(tvb, offset, pinfo, tree, di, drep, nga);");
943 $self->pidl_code("");
944 $self->pidl_code("return offset;");
946 $self->pidl_code("}\n");
950 $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_);");
952 $self->pidl_fn_start($dissectorname);
953 $self->pidl_code("int");
954 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_)");
955 $self->pidl_code("{");
957 $self->pidl_code($_) foreach (@$vars);
958 $self->pidl_code("proto_item *item = NULL;");
960 $self->pidl_code("proto_tree *tree = NULL;");
962 if (defined($doalign) and $doalign == 0) {
963 $self->pidl_code("bool oldalign = di->no_align;");
965 $self->pidl_code("int old_offset;");
966 $self->pidl_code("");
968 if (defined $cfdissectorname) {
969 $self->pidl_code("offset = $cfdissectorname(tvb, offset, pinfo, parent_tree, di, drep, &$cfvarname);");
970 $self->pidl_code("");
973 if (defined($doalign)) {
975 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
978 $self->pidl_code("di->no_align = true;");
980 $self->pidl_code("");
983 $self->pidl_code("old_offset = offset;");
984 $self->pidl_code("");
985 $self->pidl_code("if (parent_tree) {");
987 $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);");
989 $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
992 $self->pidl_code("}");
993 $self->pidl_code("");
996 $self->pidl_code("$res");
999 $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
1000 if (defined($doalign) and $doalign == 1 and not defined($e->{SURROUNDING_ELEMENT})) {
1001 $self->pidl_code("");
1002 $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {");
1004 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
1006 $self->pidl_code("}");
1008 if (defined($doalign) and $doalign == 0) {
1009 $self->pidl_code("");
1010 $self->pidl_code("di->no_align = oldalign;");
1012 $self->pidl_code("");
1013 $self->pidl_code("return offset;");
1015 $self->pidl_code("}\n");
1016 $self->pidl_fn_end($dissectorname);
1018 $self->register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,di,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
1023 my ($self,$e,$name,$ifname) = @_;
1025 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
1027 return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
1029 $self->register_ett("ett_$ifname\_$name");
1032 foreach (@{$e->{ELEMENTS}}) {
1033 $res.="\n\t\t$_->{CASE}:\n";
1034 if ($_->{TYPE} ne "EMPTY") {
1035 $res.="\t\t\t".$self->Element($_, $name, $ifname, undef, ())."\n";
1037 $res.="\t\tbreak;\n";
1040 my $switch_type = undef;
1041 my $switch_dissect = undef;
1042 my $switch_raw_type = SwitchType($e, $e->{SWITCH_TYPE});
1043 if (defined($switch_raw_type)) {
1044 $switch_type = mapWireScalarType(${switch_raw_type});
1045 $switch_dissect = "dissect_ndr_${switch_raw_type}";
1048 $self->pidl_fn_start($dissectorname);
1049 $self->pidl_code("static int");
1050 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_)");
1051 $self->pidl_code("{");
1053 $self->pidl_code("proto_item *item = NULL;");
1054 $self->pidl_code("proto_tree *tree = NULL;");
1055 $self->pidl_code("int old_offset;");
1056 if (!defined $switch_type) {
1057 $self->pidl_code("uint32_t level = param;");
1059 $self->pidl_code("$switch_type level;");
1061 $self->pidl_code("");
1063 $self->pidl_code("old_offset = offset;");
1064 $self->pidl_code("if (parent_tree) {");
1066 $self->pidl_code("tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_$ifname\_$name, &item, \"$name\");");
1068 $self->pidl_code("}");
1070 $self->pidl_code("");
1072 my $alignValue = $e->{ALIGN};
1073 if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) {
1074 my $align_flag = $e->{PROPERTIES}->{flag};
1075 if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) {
1080 if (defined $switch_type) {
1081 if ($alignValue > 1) {
1082 $self->pidl_code("UNION_ALIGN_TO_".$alignValue."_BYTES;");
1085 $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, di, drep, hf_index, &level);");
1088 if ($alignValue > 1) {
1089 if ($e->{IS_MS_UNION}) {
1090 $self->pidl_code("/* ms_union is always aligned to the largest union arm*/");
1091 $self->pidl_code("ALIGN_TO_".$alignValue."_BYTES;");
1093 $self->pidl_code("UNION_ALIGN_TO_".$alignValue."_BYTES;");
1095 $self->pidl_code("");
1098 $self->pidl_code("switch(level) {$res\t}");
1099 $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
1100 $self->pidl_code("");
1102 $self->pidl_code("return offset;");
1104 $self->pidl_code("}");
1105 $self->pidl_fn_end($dissectorname);
1107 $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
1112 my ($self,$const,$ifname) = @_;
1114 if (!defined($const->{ARRAY_LEN}[0])) {
1115 $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n");
1117 $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n");
1123 my ($self,$e,$name,$ifname) = @_;
1125 $self->Type($e->{DATA}, $name, $ifname);
1130 my ($self, $e, $name, $ifname) = @_;
1132 $self->PrintIdl(DumpType($e->{ORIGINAL}));
1138 TYPEDEF => \&Typedef,
1140 }->{$e->{TYPE}}->($self, $e, $name, $ifname);
1143 sub RegisterInterface($$)
1145 my ($self, $x) = @_;
1147 $self->pidl_fn_start("proto_register_dcerpc_$x->{NAME}");
1148 $self->pidl_code("void proto_register_dcerpc_$x->{NAME}(void)");
1149 $self->pidl_code("{");
1152 $self->{res}->{headers} .= "void proto_register_dcerpc_$x->{NAME}(void);\n";
1154 $self->{res}->{code}.=$self->DumpHfList()."\n";
1155 $self->{res}->{code}.="\n".DumpEttList($self->{ett})."\n";
1157 if (defined($x->{UUID})) {
1158 # These can be changed to non-pidl_code names if the old
1159 # dissectors in epan/dissectors are deleted.
1161 my $name = uc($x->{NAME}) . " (pidl)";
1162 my $short_name = uc($x->{NAME});
1163 my $filter_name = $x->{NAME};
1165 if (has_property($x, "helpstring")) {
1166 $name = $x->{PROPERTIES}->{helpstring};
1169 if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) {
1170 $short_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{SHORTNAME};
1171 $name = $self->{conformance}->{protocols}->{$x->{NAME}}->{LONGNAME};
1172 $filter_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{FILTERNAME};
1175 $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");");
1177 $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));");
1178 $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
1180 $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");");
1181 $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));");
1182 $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
1186 $self->pidl_code("}\n");
1187 $self->pidl_fn_end("proto_register_dcerpc_$x->{NAME}");
1190 sub RegisterInterfaceHandoff($$)
1194 if (defined($x->{UUID})) {
1195 $self->pidl_fn_start("proto_reg_handoff_dcerpc_$x->{NAME}");
1196 $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)");
1197 $self->pidl_code("{");
1199 $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},");
1200 $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},");
1201 $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);");
1203 $self->pidl_code("}");
1204 $self->pidl_fn_end("proto_reg_handoff_dcerpc_$x->{NAME}");
1206 $self->{res}->{headers} .= "void proto_reg_handoff_dcerpc_$x->{NAME}(void);\n";
1208 $self->{hf_used}->{"hf_$x->{NAME}_opnum"} = 1;
1216 foreach (@includes) {
1217 $self->pidl_hdr("#include \"$_\"");
1219 $self->pidl_hdr("");
1226 foreach (@imports) {
1227 next if($_ eq "security");
1231 $self->pidl_hdr("#include \"packet-dcerpc-$_\.h\"");
1233 $self->pidl_hdr("");
1236 sub ProcessInterface($$)
1238 my ($self, $x) = @_;
1240 push(@{$self->{conformance}->{strip_prefixes}}, $x->{NAME});
1242 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
1243 $self->pidl_hdr("#ifndef $define");
1244 $self->pidl_hdr("#define $define");
1245 $self->pidl_hdr("");
1247 $self->pidl_def("static int proto_dcerpc_$x->{NAME};");
1248 $self->register_ett("ett_dcerpc_$x->{NAME}");
1249 $self->register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
1251 if (defined($x->{UUID})) {
1252 my $if_uuid = $x->{UUID};
1254 $self->pidl_def("/* Version information */\n\n");
1256 $self->pidl_def("static e_guid_t uuid_dcerpc_$x->{NAME} = {");
1257 $self->pidl_def("\t0x" . substr($if_uuid, 1, 8)
1258 . ", 0x" . substr($if_uuid, 10, 4)
1259 . ", 0x" . substr($if_uuid, 15, 4) . ",");
1260 $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2)
1261 . ", 0x" . substr($if_uuid, 22, 2)
1262 . ", 0x" . substr($if_uuid, 25, 2)
1263 . ", 0x" . substr($if_uuid, 27, 2)
1264 . ", 0x" . substr($if_uuid, 29, 2)
1265 . ", 0x" . substr($if_uuid, 31, 2)
1266 . ", 0x" . substr($if_uuid, 33, 2)
1267 . ", 0x" . substr($if_uuid, 35, 2) . " }");
1268 $self->pidl_def("};");
1270 my $maj = 0x0000FFFF & $x->{VERSION};
1271 $maj =~ s/\.(.*)$//g;
1272 $self->pidl_def("static uint16_t ver_dcerpc_$x->{NAME} = $maj;");
1273 $self->pidl_def("");
1276 $return_types{$x->{NAME}} = {};
1278 $self->Interface($x);
1279 $self->pidl_code("\n".DumpFunctionTable($x));
1281 foreach (sort(keys %{$return_types{$x->{NAME}}})) {
1282 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
1283 my $dt = $self->find_type($type);
1284 $dt or die("Unable to find information about return type `$type'");
1285 $self->register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, $dt->{BASE_TYPE}, $dt->{VALSSTRING}, 0, "");
1286 $self->{hf_used}->{"hf_$x->{NAME}_$_"} = 1;
1289 $self->RegisterInterface($x);
1290 $self->RegisterInterfaceHandoff($x);
1292 if (exists ($self->{conformance}->{header})) {
1293 $self->pidl_hdr($self->{conformance}->{header});
1296 $self->pidl_hdr("#endif /* $define */");
1301 my ($self, $n) = @_;
1303 return $self->{conformance}->{types}->{$n};
1306 sub register_type($$$$$$$$)
1308 my ($self, $type,$call,$ft,$base,$mask,$vals,$length) = @_;
1310 return if (defined($self->{conformance}->{types}->{$type}));
1312 $self->{conformance}->{types}->{$type} = {
1314 DISSECTOR_NAME => $call,
1318 VALSSTRING => $vals,
1319 ALIGNMENT => $length
1323 # Loads the default types
1326 my ($self, $cnf_file) = @_;
1328 $self->{conformance} = {
1333 ReadConformance($cnf_file, $self->{conformance}) or print STDERR "warning: No conformance file `$cnf_file'\n";
1335 foreach my $bytes (qw(1 2 4 8)) {
1336 my $bits = $bytes * 8;
1337 $self->register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
1338 $self->register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
1341 $self->register_type("uint3264", "offset = dissect_ndr_uint3264(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT32", "BASE_DEC", 0, "NULL", 8);
1342 $self->register_type("hyper", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 8);
1343 $self->register_type("int64", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_INT64", "BASE_DEC", 0, "NULL", 8);
1344 $self->register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
1345 $self->register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1346 $self->register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1347 $self->register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
1348 $self->register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
1349 $self->register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
1350 $self->register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
1351 $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1352 $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME_hyper(tvb, offset, pinfo, tree, di, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1353 $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, di, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1354 $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME_1sec(tvb, offset, pinfo, tree, di, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1355 $self->register_type("dom_sid28",
1356 "offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, di, drep, \@HF\@);", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
1357 $self->register_type("SID",
1358 "offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, di, drep, param, \@HF\@);","FT_STRING", "BASE_NONE", 0, "NULL", 4);
1359 $self->register_type("WERROR",
1360 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_HEX|BASE_EXT_STRING", 0, "&WERR_errors_ext", 4);
1361 $self->register_type("NTSTATUS",
1362 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_HEX|BASE_EXT_STRING", 0, "&NT_errors_ext", 4);
1363 $self->register_type("HRESULT",
1364 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_HEX|BASE_EXT_STRING", 0, "&HRES_errors_ext", 4);
1365 $self->register_type("ipv6address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 16, ENC_NA); offset += 16;", "FT_IPv6", "BASE_NONE", 0, "NULL", 16);
1366 $self->register_type("ipv4address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 4, ENC_BIG_ENDIAN); offset += 4;", "FT_IPv4", "BASE_NONE", 0, "NULL", 4);
1370 #####################################################################
1371 # Generate Wireshark parser and header code
1374 my($self,$ndr,$idl_file,$h_filename,$cnf_file) = @_;
1376 $self->Initialize($cnf_file);
1378 return (undef, undef) if defined($self->{conformance
}->{noemit_dissector
});
1382 This file was automatically generated by Pidl
1383 from $idl_file and $cnf_file.
1385 Pidl is a perl based IDL compiler for DCE/RPC idl files.
1386 It is maintained by the Samba team, not the Wireshark team.
1387 Instructions on how to download and install Pidl can be
1388 found at https://wiki.wireshark.org/Pidl
1393 $self->{res
}->{headers
} = "\n";
1394 $self->{res
}->{headers
} .= "#include \"config.h\"\n";
1396 $self->{res
}->{headers
} .= "#include <string.h>\n";
1397 $self->{res
}->{headers
} .= "#include <wsutil/array.h>\n";
1398 $self->{res
}->{headers
} .= "#include <epan/packet.h>\n";
1399 $self->{res
}->{headers
} .= "#include <epan/tfs.h>\n\n";
1401 $self->{res
}->{headers
} .= "#include \"packet-dcerpc.h\"\n";
1402 $self->{res
}->{headers
} .= "#include \"packet-dcerpc-nt.h\"\n";
1403 $self->{res
}->{headers
} .= "#include \"packet-windows-common.h\"\n";
1405 my $h_basename = basename
($h_filename);
1407 $self->{res
}->{headers
} .= "#include \"$h_basename\"\n";
1408 $self->pidl_code("");
1410 if (defined($self->{conformance
}->{ett
})) {
1411 register_ett
($self,$_) foreach(@
{$self->{conformance
}->{ett
}})
1414 # Wireshark protocol registration
1417 $self->ProcessInterface($_) if ($_->{TYPE
} eq "INTERFACE");
1418 $self->ProcessImport(@
{$_->{PATHS
}}) if ($_->{TYPE
} eq "IMPORT");
1419 $self->ProcessInclude(@
{$_->{PATHS
}}) if ($_->{TYPE
} eq "INCLUDE");
1422 $self->{res
}->{ett
} = DumpEttDeclaration
($self->{ett
});
1423 $self->{res
}->{hf
} = $self->DumpHfDeclaration();
1425 my $parser = $notice;
1426 $parser.= $self->{res
}->{headers
};
1427 $parser.=$self->{res
}->{ett
};
1428 $parser.=$self->{res
}->{hf
};
1429 $parser.=$self->{res
}->{def
};
1430 if (exists ($self->{conformance
}->{override
})) {
1431 $parser.=$self->{conformance
}->{override
};
1433 $parser.=$self->{res
}->{code
};
1435 my $header = $notice;
1436 $header.=$self->{res
}->{hdr
};
1438 $self->CheckUsed($self->{conformance
});
1440 return ($parser,$header);
1443 ###############################################################################
1445 ###############################################################################
1447 sub register_ett
($$)
1449 my ($self, $name) = @_;
1451 push (@
{$self->{ett
}}, $name);
1457 my $res = "\tstatic int *ett[] = {\n";
1459 $res .= "\t\t&$_,\n";
1462 return "$res\t};\n";
1465 sub DumpEttDeclaration
1468 my $res = "\n/* Ett declarations */\n";
1470 $res .= "static int $_;\n";
1476 ###############################################################################
1478 ###############################################################################
1480 sub register_hf_field
($$$$$$$$$)
1482 my ($self,$index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
1484 if (defined ($self->{conformance
}->{hf_renames
}->{$index})) {
1485 $self->{conformance
}->{hf_renames
}->{$index}->{USED
} = 1;
1486 return $self->{conformance
}->{hf_renames
}->{$index}->{NEWNAME
};
1489 $self->{conformance
}->{header_fields
}->{$index} = {
1492 FILTER
=> $filter_name,
1493 FT_TYPE
=> $ft_type,
1494 BASE_TYPE
=> $base_type,
1495 VALSSTRING
=> $valsstring,
1500 if ((not defined($blurb) or $blurb eq "") and
1501 defined($self->{conformance
}->{fielddescription
}->{$index})) {
1502 $self->{conformance
}->{header_fields
}->{$index}->{BLURB
} =
1503 $self->{conformance
}->{fielddescription
}->{$index}->{DESCRIPTION
};
1504 $self->{conformance
}->{fielddescription
}->{$index}->{USED
} = 1;
1510 sub change_hf_field_type
($$$$)
1512 my ($self,$index,$ft_type,$base_type) = @_;
1513 if (defined ($self->{conformance
}->{hf_renames
}->{$index})) {
1514 print "Field $index has been renamed to ".$self->{conformance
}->{hf_renames
}->{$index}->{NEWNAME
}." you can't change it's type";
1518 if (!defined ($self->{conformance
}->{header_fields
}->{$index})) {
1519 print "Field $index doesn't exists";
1522 $self->{conformance
}->{header_fields
}->{$index}->{FT_TYPE
} = $ft_type;
1523 $self->{conformance
}->{header_fields
}->{$index}->{BASE_TYPE
} = $base_type;
1527 sub DumpHfDeclaration
($)
1532 $res = "\n/* Header field declarations */\n";
1534 foreach (sort(keys %{$self->{conformance
}->{header_fields
}}))
1536 $res .= "static int $_;\n";
1542 sub make_str_or_null
($)
1545 if (substr($str, 0, 1) eq "\"") {
1546 $str = substr($str, 1, length($str)-2);
1553 return make_str
($str);
1559 my $res = "\tstatic hf_register_info hf[] = {\n";
1561 foreach (sort {$a->{INDEX
} cmp $b->{INDEX
}} values %{$self->{conformance
}->{header_fields
}})
1563 $res .= "\t{ &$_->{INDEX},\n".
1564 "\t { ".make_str
($_->{NAME
}).", ".make_str
($_->{FILTER
}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str_or_null
($_->{BLURB
}).", HFILL }},\n";
1567 return $res."\t};\n";
1571 ###############################################################################
1573 ###############################################################################
1575 sub DumpFunctionTable
($)
1579 my $res = "static const dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1580 foreach (@
{$if->{FUNCTIONS
}}) {
1581 my $fn_name = $_->{NAME
};
1582 $fn_name =~ s/^$if->{NAME}_//;
1583 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1584 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1587 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1594 my ($self, $conformance) = @_;
1595 foreach (values %{$conformance->{header_fields
}}) {
1596 if (not defined($self->{hf_used
}->{$_->{INDEX
}})) {
1597 warning
($_->{POS
}, "hf field `$_->{INDEX}' not used");
1601 foreach (values %{$conformance->{hf_renames
}}) {
1602 if (not $_->{USED
}) {
1603 warning
($_->{POS
}, "hf field `$_->{OLDNAME}' not used");
1607 foreach (values %{$conformance->{dissectorparams
}}) {
1608 if (not $_->{USED
}) {
1609 warning
($_->{POS
}, "dissector param never used");
1613 foreach (values %{$conformance->{imports
}}) {
1614 if (not $_->{USED
}) {
1615 warning
($_->{POS
}, "import never used");
1619 foreach (values %{$conformance->{types
}}) {
1620 if (not $_->{USED
} and defined($_->{POS
})) {
1621 warning
($_->{POS
}, "type never used");
1625 foreach (values %{$conformance->{fielddescription
}}) {
1626 if (not $_->{USED
}) {
1627 warning
($_->{POS
}, "description never used");
1631 foreach (values %{$conformance->{tfs
}}) {
1632 if (not $_->{USED
}) {
1633 warning
($_->{POS
}, "True/False description never used");