1 ##################################################
2 # Samba4 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-2005
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
;
20 use Parse
::Pidl
::Typelist
qw(getType);
21 use Parse
::Pidl
::Util
qw(has_property ParseExpr property_matches make_str);
22 use Parse
::Pidl
::NDR
qw(ContainsString GetNextLevel);
23 use Parse
::Pidl
::Dump
qw(DumpTypedef DumpFunction);
24 use Parse
::Pidl
::Wireshark
::Conformance
qw(ReadConformance);
27 use vars
qw($VERSION);
33 print "$e->{FILE}:$e->{LINE}: $t\n";
39 my %return_types = ();
40 my %dissector_used = ();
42 my $conformance = undef;
44 my %ptrtype_mappings = (
45 "unique" => "NDR_POINTER_UNIQUE",
46 "ref" => "NDR_POINTER_REF",
47 "ptr" => "NDR_POINTER_PTR"
54 foreach (@{$conformance->{strip_prefixes}}) {
61 # Convert a IDL structure field name (e.g access_mask) to a prettier
62 # string like 'Access Mask'.
68 $field =~ s/_/ /g; # Replace underscores with spaces
69 $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
85 die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
92 return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
101 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
102 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
111 $tabs = substr($tabs, 0, -1);
118 foreach (split /\n/, $idl) {
119 pidl_code "/* IDL: $_ */";
125 #####################################################################
126 # parse the interface definitions
130 Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
131 Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPES}});
132 Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
137 my ($e,$name,$ifname) = @_;
138 my $valsstring = "$ifname\_$name\_vals";
139 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
141 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
143 foreach (@{$e->{ELEMENTS}}) {
144 if (/([^=]*)=(.*)/) {
145 pidl_hdr "#define $1 ($2)";
149 pidl_hdr "extern const value_string $valsstring\[];";
150 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param);";
152 pidl_def "const value_string ".$valsstring."[] = {";
153 foreach (@{$e->{ELEMENTS}}) {
154 next unless (/([^=]*)=(.*)/);
155 pidl_def "\t{ $1, \"$1\" },";
158 pidl_def "{ 0, NULL }";
161 pidl_fn_start $dissectorname;
163 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param)";
166 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
167 pidl_code "return offset;";
170 pidl_fn_end $dissectorname;
172 my $enum_size = $e->{BASE_TYPE};
173 $enum_size =~ s/uint//g;
174 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
179 my ($e,$name,$ifname) = @_;
180 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
182 register_ett("ett_$ifname\_$name");
184 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
186 pidl_fn_start $dissectorname;
188 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
191 pidl_code "proto_item *item = NULL;";
192 pidl_code "proto_tree *tree = NULL;";
195 pidl_code "g$e->{BASE_TYPE} flags;";
196 if ($e->{ALIGN} > 1) {
197 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
202 pidl_code "if (parent_tree) {";
204 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
205 pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
209 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
211 pidl_code "proto_item_append_text(item, \": \");\n";
212 pidl_code "if (!flags)";
213 pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
215 foreach (@{$e->{ELEMENTS}}) {
216 next unless (/([^ ]*) (.*)/);
217 my ($en,$ev) = ($1,$2);
218 my $hf_bitname = "hf_$ifname\_$name\_$en";
219 my $filtername = "$ifname\.$name\.$en";
221 $hf_used{$hf_bitname} = 1;
223 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
225 pidl_def "static const true_false_string $name\_$en\_tfs = {";
226 if (defined($conformance->{tfs}->{$hf_bitname})) {
227 pidl_def " $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
228 pidl_def " $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
229 $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
231 pidl_def " \"$en is SET\",";
232 pidl_def " \"$en is NOT SET\",";
236 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
237 pidl_code "if (flags&$ev){";
238 pidl_code "\tproto_item_append_text(item, \"$en\");";
239 pidl_code "\tif (flags & (~$ev))";
240 pidl_code "\t\tproto_item_append_text(item, \", \");";
242 pidl_code "flags&=(~$ev);";
246 pidl_code "if (flags) {";
247 pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
249 pidl_code "return offset;";
252 pidl_fn_end $dissectorname;
254 my $size = $e->{BASE_TYPE};
256 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
259 sub ElementLevel($$$$$)
261 my ($e,$l,$hf,$myname,$pn) = @_;
265 if (defined($conformance->{dissectorparams}->{$myname})) {
266 $conformance->{dissectorparams}->{$myname}->{PARAM} = 1;
267 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
270 if ($l->{TYPE} eq "POINTER") {
272 if ($l->{LEVEL} eq "TOP") {
274 } elsif ($l->{LEVEL} eq "EMBEDDED") {
277 pidl_code "offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME})) . " ($e->{TYPE})\",$hf);";
278 } elsif ($l->{TYPE} eq "ARRAY") {
279 if ($l->{IS_INLINE}) {
280 error($e->{ORIGINAL}, "Inline arrays not supported");
281 } elsif ($l->{IS_FIXED}) {
283 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
284 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
287 $type .= "c" if ($l->{IS_CONFORMANT});
288 $type .= "v" if ($l->{IS_VARYING});
290 unless ($l->{IS_ZERO_TERMINATED}) {
291 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
293 my $nl = GetNextLevel($e,$l);
294 pidl_code "char *data;";
296 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
297 pidl_code "proto_item_append_text(tree, \": %s\", data);";
300 } elsif ($l->{TYPE} eq "DATA") {
301 if ($l->{DATA_TYPE} eq "string") {
302 my $bs = 2; # Byte size defaults to that of UCS2
305 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
307 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
308 pidl_code "char *data;\n";
309 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
310 pidl_code "proto_item_append_text(tree, \": %s\", data);";
311 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
312 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
314 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
319 if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
320 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};
321 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
322 } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
323 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
324 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
326 } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
327 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
328 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
330 if ($l->{DATA_TYPE} =~ /^([a-z]+)\_(.*)$/)
332 pidl_code "offset = $1_dissect_struct_$2(tvb,offset,pinfo,tree,drep,$hf,$param);";
338 $call =~ s/\@HF\@/$hf/g;
339 $call =~ s/\@PARAM\@/$param/g;
342 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
343 my $num_bits = ($l->{HEADER_SIZE}*8);
344 pidl_code "guint$num_bits size;";
345 pidl_code "int start_offset = offset;";
346 pidl_code "tvbuff_t *subtvb;";
347 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
348 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
350 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
351 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
353 die("Unknown type `$_->{TYPE}'");
359 my ($e,$pn,$ifname) = @_;
361 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
363 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
365 my $type = find_type($e->{TYPE});
367 if (not defined($type)) {
371 VALSSTRING => "NULL",
372 FT_TYPE => "FT_NONE",
373 BASE_TYPE => "BASE_HEX"
377 if (ContainsString($e)) {
380 VALSSTRING => "NULL",
381 FT_TYPE => "FT_STRING",
382 BASE_TYPE => "BASE_DEC"
386 my $hf = 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}, "");
389 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
390 if (defined($conformance->{noemit}->{$eltname})) {
396 foreach (@{$e->{LEVELS}}) {
397 next if ($_->{TYPE} eq "SWITCH");
398 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
399 pidl_fn_start "$dissectorname$add";
400 pidl_code "static int";
401 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
405 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn);
408 pidl_code "return offset;";
411 pidl_fn_end "$dissectorname$add";
413 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
421 my ($fn,$ifname) = @_;
425 foreach (@{$fn->{ELEMENTS}}) {
426 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
429 my $fn_name = $_->{NAME};
430 $fn_name =~ s/^${ifname}_//;
432 PrintIdl DumpFunction($fn->{ORIGINAL});
433 pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
434 pidl_code "static int";
435 pidl_code "$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)";
438 if ( not defined($fn->{RETURN_TYPE})) {
439 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
441 pidl_code "guint32 status;\n";
442 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
443 if ($type->{DATA}->{TYPE} eq "ENUM") {
444 pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
446 print "$fn->{FILE}:$fn->{LINE}: error: return type `$fn->{RETURN_TYPE}' not yet supported\n";
449 print "$fn->{FILE}:$fn->{LINE}: error: unknown return type `$fn->{RETURN_TYPE}'\n";
452 foreach (@{$fn->{ELEMENTS}}) {
453 if (grep(/out/,@{$_->{DIRECTION}})) {
454 pidl_code "$dissectornames{$_->{NAME}}";
455 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
460 if (not defined($fn->{RETURN_TYPE})) {
461 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
462 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
463 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
464 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
465 $return_types{$ifname}->{"status"} = ["NTSTATUS", "Windows Error"];
466 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
467 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
468 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
469 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
471 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
472 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
473 if ($type->{DATA}->{TYPE} eq "ENUM") {
474 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
475 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
477 pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
478 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
479 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";
480 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
485 pidl_code "return offset;";
488 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
490 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
491 pidl_code "static int";
492 pidl_code "$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)";
495 foreach (@{$fn->{ELEMENTS}}) {
496 if (grep(/in/,@{$_->{DIRECTION}})) {
497 pidl_code "$dissectornames{$_->{NAME}}";
498 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
503 pidl_code "return offset;";
506 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
511 my ($e,$name,$ifname) = @_;
512 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
514 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
516 register_ett("ett_$ifname\_$name");
519 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
521 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
523 pidl_fn_start $dissectorname;
525 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
528 pidl_code "proto_item *item = NULL;";
529 pidl_code "proto_tree *tree = NULL;";
530 pidl_code "int old_offset;";
533 if ($e->{ALIGN} > 1) {
534 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
538 pidl_code "old_offset = offset;";
540 pidl_code "if (parent_tree) {";
542 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
543 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
549 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
550 pidl_code "return offset;";
553 pidl_fn_end $dissectorname;
555 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
560 my ($e,$name,$ifname) = @_;
562 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
564 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
566 register_ett("ett_$ifname\_$name");
569 foreach (@{$e->{ELEMENTS}}) {
570 $res.="\n\t\t$_->{CASE}:\n";
571 if ($_->{TYPE} ne "EMPTY") {
572 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
574 $res.="\t\tbreak;\n";
579 my $switch_dt = getType($e->{SWITCH_TYPE});
580 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
581 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
582 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
583 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
584 $switch_type = "g$e->{SWITCH_TYPE}";
585 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
588 pidl_fn_start $dissectorname;
589 pidl_code "static int";
590 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
593 pidl_code "proto_item *item = NULL;";
594 pidl_code "proto_tree *tree = NULL;";
595 pidl_code "int old_offset;";
596 pidl_code "$switch_type level;";
599 if ($e->{ALIGN} > 1) {
600 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
605 pidl_code "old_offset = offset;";
606 pidl_code "if (parent_tree) {";
608 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
609 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
615 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
617 pidl_code "switch(level) {$res\t}";
618 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
619 pidl_code "return offset;";
622 pidl_fn_end $dissectorname;
624 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
629 my ($const,$ifname) = @_;
631 if (!defined($const->{ARRAY_LEN}[0])) {
632 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
634 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
640 my ($e,$ifname) = @_;
642 PrintIdl DumpTypedef($e->{ORIGINAL});
649 }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
652 sub RegisterInterface($)
656 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
657 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
661 $res{code}.=DumpHfList()."\n";
662 $res{code}.="\n".DumpEttList()."\n";
664 if (defined($x->{UUID})) {
665 # These can be changed to non-pidl_code names if the old dissectors
666 # in epan/dissctors are deleted.
668 my $name = uc($x->{NAME}) . " (pidl)";
669 my $short_name = uc($x->{NAME});
670 my $filter_name = $x->{NAME};
672 if (has_property($x, "helpstring")) {
673 $name = $x->{PROPERTIES}->{helpstring};
676 if (defined($conformance->{protocols}->{$x->{NAME}})) {
677 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
678 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
679 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
682 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
684 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
685 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
687 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
688 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
689 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
694 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
697 sub RegisterInterfaceHandoff($)
701 if (defined($x->{UUID})) {
702 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
703 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
706 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
707 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
708 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
711 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
713 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
717 sub ProcessInterface($)
721 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
723 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
724 pidl_hdr "#ifndef $define";
725 pidl_hdr "#define $define";
728 if (defined $x->{PROPERTIES}->{depends}) {
729 foreach (split / /, $x->{PROPERTIES}->{depends}) {
730 next if($_ eq "security");
731 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
735 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
736 register_ett("ett_dcerpc_$x->{NAME}");
737 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
739 if (defined($x->{UUID})) {
740 my $if_uuid = $x->{UUID};
742 pidl_def "/* Version information */\n\n";
744 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
745 pidl_def "\t0x" . substr($if_uuid, 1, 8)
746 . ", 0x" . substr($if_uuid, 10, 4)
747 . ", 0x" . substr($if_uuid, 15, 4) . ",";
748 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
749 . ", 0x" . substr($if_uuid, 22, 2)
750 . ", 0x" . substr($if_uuid, 25, 2)
751 . ", 0x" . substr($if_uuid, 27, 2)
752 . ", 0x" . substr($if_uuid, 29, 2)
753 . ", 0x" . substr($if_uuid, 31, 2)
754 . ", 0x" . substr($if_uuid, 33, 2)
755 . ", 0x" . substr($if_uuid, 35, 2) . " }";
758 my $maj = $x->{VERSION};
759 $maj =~ s/\.(.*)$//g;
760 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
764 $return_types{$x->{NAME}} = {};
768 pidl_code "\n".DumpFunctionTable($x);
770 foreach (keys %{$return_types{$x->{NAME}}}) {
771 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
772 my $dt = find_type($type);
773 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
774 $hf_used{"hf_$x->{NAME}_$_"} = 1;
777 RegisterInterface($x);
778 RegisterInterfaceHandoff($x);
780 pidl_hdr "#endif /* $define */";
787 return $conformance->{types}->{$n};
790 sub register_type($$$$$$$)
792 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
794 $conformance->{types}->{$type} = {
796 DISSECTOR_NAME => $call,
805 # Loads the default types
808 my $cnf_file = shift;
815 ReadConformance($cnf_file, $conformance) or print "Warning: No conformance file `$cnf_file'\n";
817 foreach my $bytes (qw(1 2 4 8)) {
818 my $bits = $bytes * 8;
819 register_type
("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
820 register_type
("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
823 register_type
("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
824 register_type
("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
825 register_type
("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
826 register_type
("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
827 register_type
("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
828 register_type
("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
829 register_type
("policy_handle", "offset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, NULL, NULL, \@PARAM\@&0x01, \@PARAM\@&0x02);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
830 register_type
("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
831 register_type
("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
832 register_type
("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
833 register_type
("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
834 register_type
("SID", "
835 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
837 di->hf_index = \@HF\@;
839 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
840 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
841 register_type
("WERROR",
842 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
846 #####################################################################
847 # Generate Wireshark parser and header code
850 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
851 Initialize
($cnf_file);
853 return (undef, undef) if defined($conformance->{noemit_dissector
});
857 %res = (code
=>"",def
=>"",hdr
=>"");
862 This filter was automatically generated
863 from $idl_file and $cnf_file.
865 Pidl is a perl based IDL compiler for DCE/RPC idl files.
866 It is maintained by the Samba team, not the Wireshark team.
867 Instructions on how to download and install Pidl can be
868 found at http://wiki.wireshark.org/Pidl
875 $res{headers
} = "\n";
876 $res{headers
} .= "#ifdef HAVE_CONFIG_H\n";
877 $res{headers
} .= "#include \"config.h\"\n";
878 $res{headers
} .= "#endif\n\n";
879 $res{headers
} .= "#include <glib.h>\n";
880 $res{headers
} .= "#include <string.h>\n";
881 $res{headers
} .= "#include <epan/packet.h>\n\n";
883 $res{headers
} .= "#include \"packet-dcerpc.h\"\n";
884 $res{headers
} .= "#include \"packet-dcerpc-nt.h\"\n";
885 $res{headers
} .= "#include \"packet-windows-common.h\"\n";
887 my $h_basename = basename
($h_filename);
889 $res{headers
} .= "#include \"$h_basename\"\n";
892 # Wireshark protocol registration
894 ProcessInterface
($_) foreach (@
$ndr);
896 $res{ett
} = DumpEttDeclaration
();
897 $res{hf
} = DumpHfDeclaration
();
899 my $parser = $notice;
900 $parser.= $res{headers
};
904 $parser.=$conformance->{override
};
907 my $header = "/* autogenerated by pidl */\n\n";
910 CheckUsed
($conformance);
912 return ($parser,$header);
915 ###############################################################################
917 ###############################################################################
928 my $res = "\tstatic gint *ett[] = {\n";
930 $res .= "\t\t&$_,\n";
936 sub DumpEttDeclaration
()
938 my $res = "\n/* Ett declarations */\n";
940 $res .= "static gint $_ = -1;\n";
946 ###############################################################################
948 ###############################################################################
950 sub register_hf_field
($$$$$$$$)
952 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
954 if (defined ($conformance->{hf_renames
}->{$index})) {
955 $conformance->{hf_renames
}->{$index}->{USED
} = 1;
956 return $conformance->{hf_renames
}->{$index}->{NEWNAME
};
959 $conformance->{header_fields
}->{$index} = {
962 FILTER
=> $filter_name,
964 BASE_TYPE
=> $base_type,
965 VALSSTRING
=> $valsstring,
970 if ((not defined($blurb) or $blurb eq "") and
971 defined($conformance->{fielddescription
}->{$index})) {
972 $conformance->{header_fields
}->{$index}->{BLURB
} =
973 $conformance->{fielddescription
}->{$index}->{DESCRIPTION
};
974 $conformance->{fielddescription
}->{$index}->{USED
} = 1;
980 sub DumpHfDeclaration
()
984 $res = "\n/* Header field declarations */\n";
986 foreach (keys %{$conformance->{header_fields
}})
988 $res .= "static gint $_ = -1;\n";
996 my $res = "\tstatic hf_register_info hf[] = {\n";
998 foreach (values %{$conformance->{header_fields
}})
1000 $res .= "\t{ &$_->{INDEX},
1001 { ".make_str
($_->{NAME
}).", ".make_str
($_->{FILTER
}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str
($_->{BLURB
}).", HFILL }},
1005 return $res."\t};\n";
1009 ###############################################################################
1011 ###############################################################################
1013 sub DumpFunctionTable
($)
1017 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1018 foreach (@
{$if->{FUNCTIONS
}}) {
1019 my $fn_name = $_->{NAME
};
1020 $fn_name =~ s/^$if->{NAME}_//;
1021 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1022 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1025 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1032 my $conformance = shift;
1033 foreach (values %{$conformance->{header_fields
}}) {
1034 if (not defined($hf_used{$_->{INDEX
}})) {
1035 print "$_->{POS}: warning: hf field `$_->{INDEX}' not used\n";
1039 foreach (values %{$conformance->{hf_renames
}}) {
1040 if (not $_->{USED
}) {
1041 print "$_->{POS}: warning: hf field `$_->{OLDNAME}' not used\n";
1045 foreach (values %{$conformance->{dissectorparams
}}) {
1046 if (not $_->{USED
}) {
1047 print "$_->{POS}: warning: dissector param never used\n";
1051 foreach (values %{$conformance->{imports
}}) {
1052 if (not $_->{USED
}) {
1053 print "$_->{POS}: warning: import never used\n";
1057 foreach (values %{$conformance->{types
}}) {
1058 if (not $_->{USED
} and defined($_->{POS
})) {
1059 print "$_->{POS}: warning: type never used\n";
1063 foreach (values %{$conformance->{fielddescription
}}) {
1064 if (not $_->{USED
}) {
1065 print "$_->{POS}: warning: description never used\n";
1069 foreach (values %{$conformance->{tfs
}}) {
1070 if (not $_->{USED
}) {
1071 print "$_->{POS}: warning: True/False description never used\n";