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-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 @ett %res PrintIdl);
24 use Parse::Pidl qw(error warning);
25 use Parse
::Pidl
::Typelist
qw(getType);
26 use Parse
::Pidl
::Util
qw(has_property property_matches make_str);
27 use Parse
::Pidl
::NDR
qw(ContainsString GetNextLevel);
28 use Parse
::Pidl
::Dump
qw(DumpType DumpFunction);
29 use Parse
::Pidl
::Wireshark
::Conformance
qw(ReadConformance);
32 use vars
qw($VERSION);
38 my %return_types = ();
39 my %dissector_used = ();
41 my $conformance = undef;
43 my %ptrtype_mappings = (
44 "unique" => "NDR_POINTER_UNIQUE",
45 "ref" => "NDR_POINTER_REF",
46 "ptr" => "NDR_POINTER_PTR"
53 foreach (@{$conformance->{strip_prefixes}}) {
60 # Convert a IDL structure field name (e.g access_mask) to a prettier
61 # string like 'Access Mask'.
67 $field =~ s/_/ /g; # Replace underscores with spaces
68 $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
84 die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
91 return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
100 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
101 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
110 $tabs = substr($tabs, 0, -1);
117 foreach (split /\n/, $idl) {
118 pidl_code "/* IDL: $_ */";
124 #####################################################################
125 # parse the interface definitions
129 Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
130 Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}});
131 Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
136 my ($e,$name,$ifname) = @_;
137 my $valsstring = "$ifname\_$name\_vals";
138 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
140 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
142 foreach (@{$e->{ELEMENTS}}) {
143 if (/([^=]*)=(.*)/) {
144 pidl_hdr "#define $1 ($2)";
148 pidl_hdr "extern const value_string $valsstring\[];";
149 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param);";
151 pidl_def "const value_string ".$valsstring."[] = {";
152 foreach (@{$e->{ELEMENTS}}) {
153 next unless (/([^=]*)=(.*)/);
154 pidl_def "\t{ $1, \"$1\" },";
157 pidl_def "{ 0, NULL }";
160 pidl_fn_start $dissectorname;
162 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param)";
165 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
166 pidl_code "return offset;";
169 pidl_fn_end $dissectorname;
171 my $enum_size = $e->{BASE_TYPE};
172 $enum_size =~ s/uint//g;
173 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
178 my ($e,$name,$ifname) = @_;
179 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
181 register_ett("ett_$ifname\_$name");
183 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
185 pidl_fn_start $dissectorname;
187 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
190 pidl_code "proto_item *item = NULL;";
191 pidl_code "proto_tree *tree = NULL;";
194 pidl_code "g$e->{BASE_TYPE} flags;";
195 if ($e->{ALIGN} > 1) {
196 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
201 pidl_code "if (parent_tree) {";
203 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
204 pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
208 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
210 pidl_code "proto_item_append_text(item, \": \");\n";
211 pidl_code "if (!flags)";
212 pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
214 foreach (@{$e->{ELEMENTS}}) {
215 next unless (/([^ ]*) (.*)/);
216 my ($en,$ev) = ($1,$2);
217 my $hf_bitname = "hf_$ifname\_$name\_$en";
218 my $filtername = "$ifname\.$name\.$en";
220 $hf_used{$hf_bitname} = 1;
222 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
224 pidl_def "static const true_false_string $name\_$en\_tfs = {";
225 if (defined($conformance->{tfs}->{$hf_bitname})) {
226 pidl_def " $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
227 pidl_def " $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
228 $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
230 pidl_def " \"$en is SET\",";
231 pidl_def " \"$en is NOT SET\",";
235 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
236 pidl_code "if (flags&$ev){";
237 pidl_code "\tproto_item_append_text(item, \"$en\");";
238 pidl_code "\tif (flags & (~$ev))";
239 pidl_code "\t\tproto_item_append_text(item, \", \");";
241 pidl_code "flags&=(~$ev);";
245 pidl_code "if (flags) {";
246 pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
248 pidl_code "return offset;";
251 pidl_fn_end $dissectorname;
253 my $size = $e->{BASE_TYPE};
255 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
258 sub ElementLevel($$$$$$)
260 my ($e,$l,$hf,$myname,$pn,$ifname) = @_;
264 if (defined($conformance->{dissectorparams}->{$myname})) {
265 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
268 if ($l->{TYPE} eq "POINTER") {
270 if ($l->{LEVEL} eq "TOP") {
272 } elsif ($l->{LEVEL} eq "EMBEDDED") {
275 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);";
276 } elsif ($l->{TYPE} eq "ARRAY") {
277 if ($l->{IS_INLINE}) {
278 error($e->{ORIGINAL}, "Inline arrays not supported");
279 } elsif ($l->{IS_FIXED}) {
281 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
282 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
285 $type .= "c" if ($l->{IS_CONFORMANT});
286 $type .= "v" if ($l->{IS_VARYING});
288 unless ($l->{IS_ZERO_TERMINATED}) {
289 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
291 my $nl = GetNextLevel($e,$l);
292 pidl_code "char *data;";
294 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
295 pidl_code "proto_item_append_text(tree, \": %s\", data);";
298 } elsif ($l->{TYPE} eq "DATA") {
299 if ($l->{DATA_TYPE} eq "string") {
300 my $bs = 2; # Byte size defaults to that of UCS2
303 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
305 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
306 pidl_code "char *data;\n";
307 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
308 pidl_code "proto_item_append_text(tree, \": %s\", data);";
309 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
310 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
312 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
317 if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
318 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};
319 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
320 } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
321 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
322 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
324 } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
325 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
326 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
328 pidl_code "offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);";
333 $call =~ s/\@HF\@/$hf/g;
334 $call =~ s/\@PARAM\@/$param/g;
337 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
338 my $num_bits = ($l->{HEADER_SIZE}*8);
339 pidl_code "guint$num_bits size;";
340 pidl_code "int start_offset = offset;";
341 pidl_code "tvbuff_t *subtvb;";
342 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
343 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
345 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
346 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
348 die("Unknown type `$_->{TYPE}'");
354 my ($e,$pn,$ifname) = @_;
356 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
358 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
360 my $type = find_type($e->{TYPE});
362 if (not defined($type)) {
366 VALSSTRING => "NULL",
367 FT_TYPE => "FT_NONE",
368 BASE_TYPE => "BASE_HEX"
372 if (ContainsString($e)) {
375 VALSSTRING => "NULL",
376 FT_TYPE => "FT_STRING",
377 BASE_TYPE => "BASE_DEC"
381 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}, "");
384 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
385 if (defined($conformance->{noemit}->{$eltname})) {
391 foreach (@{$e->{LEVELS}}) {
392 next if ($_->{TYPE} eq "SWITCH");
393 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
394 pidl_fn_start "$dissectorname$add";
395 pidl_code "static int";
396 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
400 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
403 pidl_code "return offset;";
406 pidl_fn_end "$dissectorname$add";
408 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
416 my ($fn,$ifname) = @_;
420 foreach (@{$fn->{ELEMENTS}}) {
421 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
424 my $fn_name = $_->{NAME};
425 $fn_name =~ s/^${ifname}_//;
427 PrintIdl DumpFunction($fn->{ORIGINAL});
428 pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
429 pidl_code "static int";
430 pidl_code "$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo, proto_tree *tree _U_, guint8 *drep _U_)";
433 if ( not defined($fn->{RETURN_TYPE})) {
434 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
436 pidl_code "guint32 status;\n";
437 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
438 if ($type->{DATA}->{TYPE} eq "ENUM") {
439 pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
440 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
441 pidl_code "g$fn->{RETURN_TYPE} status;\n";
443 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
446 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
449 pidl_code "pinfo->dcerpc_procedure_name=\"${fn_name}\";";
450 foreach (@{$fn->{ELEMENTS}}) {
451 if (grep(/out/,@{$_->{DIRECTION}})) {
452 pidl_code "$dissectornames{$_->{NAME}}";
453 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
458 if (not defined($fn->{RETURN_TYPE})) {
459 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
460 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
461 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
462 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
463 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
464 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
465 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
466 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
467 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
469 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
470 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
471 if ($type->{DATA}->{TYPE} eq "ENUM") {
472 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
473 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
475 pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
476 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
477 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";
478 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
479 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
480 pidl_code "offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
481 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
482 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n";
483 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
488 pidl_code "return offset;";
491 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
493 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
494 pidl_code "static int";
495 pidl_code "$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo, proto_tree *tree _U_, guint8 *drep _U_)";
498 pidl_code "pinfo->dcerpc_procedure_name=\"${fn_name}\";";
499 foreach (@{$fn->{ELEMENTS}}) {
500 if (grep(/in/,@{$_->{DIRECTION}})) {
501 pidl_code "$dissectornames{$_->{NAME}}";
502 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
507 pidl_code "return offset;";
510 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
515 my ($e,$name,$ifname) = @_;
516 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
518 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
520 register_ett("ett_$ifname\_$name");
523 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
525 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
527 pidl_fn_start $dissectorname;
529 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
532 pidl_code "proto_item *item = NULL;";
533 pidl_code "proto_tree *tree = NULL;";
534 pidl_code "int old_offset;";
537 if ($e->{ALIGN} > 1) {
538 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
542 pidl_code "old_offset = offset;";
544 pidl_code "if (parent_tree) {";
546 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
547 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
553 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
554 pidl_code "return offset;";
557 pidl_fn_end $dissectorname;
559 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
564 my ($e,$name,$ifname) = @_;
566 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
568 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
570 register_ett("ett_$ifname\_$name");
573 foreach (@{$e->{ELEMENTS}}) {
574 $res.="\n\t\t$_->{CASE}:\n";
575 if ($_->{TYPE} ne "EMPTY") {
576 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
578 $res.="\t\tbreak;\n";
583 my $switch_dt = getType($e->{SWITCH_TYPE});
584 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
585 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
586 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
587 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
588 $switch_type = "g$e->{SWITCH_TYPE}";
589 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
592 pidl_fn_start $dissectorname;
593 pidl_code "static int";
594 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
597 pidl_code "proto_item *item = NULL;";
598 pidl_code "proto_tree *tree = NULL;";
599 pidl_code "int old_offset;";
600 pidl_code "$switch_type level;";
603 if ($e->{ALIGN} > 1) {
604 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
609 pidl_code "old_offset = offset;";
610 pidl_code "if (parent_tree) {";
612 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
613 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
619 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
621 pidl_code "switch(level) {$res\t}";
622 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
623 pidl_code "return offset;";
626 pidl_fn_end $dissectorname;
628 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
633 my ($const,$ifname) = @_;
635 if (!defined($const->{ARRAY_LEN}[0])) {
636 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
638 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
644 my ($e,$name,$ifname) = @_;
646 Type($e->{DATA}, $name, $ifname);
651 my ($e, $name, $ifname) = @_;
653 PrintIdl DumpType($e->{ORIGINAL});
661 }->{$e->{TYPE}}->($e, $name, $ifname);
664 sub RegisterInterface($)
668 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
669 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
673 $res{code}.=DumpHfList()."\n";
674 $res{code}.="\n".DumpEttList()."\n";
676 if (defined($x->{UUID})) {
677 # These can be changed to non-pidl_code names if the old dissectors
678 # in epan/dissctors are deleted.
680 my $name = uc($x->{NAME}) . " (pidl)";
681 my $short_name = uc($x->{NAME});
682 my $filter_name = $x->{NAME};
684 if (has_property($x, "helpstring")) {
685 $name = $x->{PROPERTIES}->{helpstring};
688 if (defined($conformance->{protocols}->{$x->{NAME}})) {
689 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
690 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
691 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
694 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
696 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
697 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
699 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
700 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
701 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
706 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
709 sub RegisterInterfaceHandoff($)
713 if (defined($x->{UUID})) {
714 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
715 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
718 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
719 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
720 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
723 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
725 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
732 foreach (@includes) {
733 pidl_hdr "#include \"$_\"\n";
741 next if($_ eq "security");
744 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
748 sub ProcessInterface($)
752 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
754 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
755 pidl_hdr "#ifndef $define";
756 pidl_hdr "#define $define";
759 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
760 register_ett("ett_dcerpc_$x->{NAME}");
761 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
763 if (defined($x->{UUID})) {
764 my $if_uuid = $x->{UUID};
766 pidl_def "/* Version information */\n\n";
768 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
769 pidl_def "\t0x" . substr($if_uuid, 1, 8)
770 . ", 0x" . substr($if_uuid, 10, 4)
771 . ", 0x" . substr($if_uuid, 15, 4) . ",";
772 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
773 . ", 0x" . substr($if_uuid, 22, 2)
774 . ", 0x" . substr($if_uuid, 25, 2)
775 . ", 0x" . substr($if_uuid, 27, 2)
776 . ", 0x" . substr($if_uuid, 29, 2)
777 . ", 0x" . substr($if_uuid, 31, 2)
778 . ", 0x" . substr($if_uuid, 33, 2)
779 . ", 0x" . substr($if_uuid, 35, 2) . " }";
782 my $maj = $x->{VERSION};
783 $maj =~ s/\.(.*)$//g;
784 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
788 $return_types{$x->{NAME}} = {};
792 pidl_code "\n".DumpFunctionTable($x);
794 foreach (keys %{$return_types{$x->{NAME}}}) {
795 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
796 my $dt = find_type($type);
797 $dt or die("Unable to find information about return type `$type'");
798 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
799 $hf_used{"hf_$x->{NAME}_$_"} = 1;
802 RegisterInterface($x);
803 RegisterInterfaceHandoff($x);
805 pidl_hdr "#endif /* $define */";
812 return $conformance->{types}->{$n};
815 sub register_type($$$$$$$)
817 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
819 $conformance->{types}->{$type} = {
821 DISSECTOR_NAME => $call,
830 # Loads the default types
833 my $cnf_file = shift;
840 ReadConformance($cnf_file, $conformance) or print STDERR "warning: No conformance file `$cnf_file'\n";
842 foreach my $bytes (qw(1 2 4 8)) {
843 my $bits = $bytes * 8;
844 register_type
("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
845 register_type
("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
848 register_type
("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
849 register_type
("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
850 register_type
("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
851 register_type
("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
852 register_type
("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
853 register_type
("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
854 register_type
("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
855 register_type
("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
856 register_type
("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
857 register_type
("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
858 register_type
("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
859 register_type
("SID", "
860 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
862 di->hf_index = \@HF\@;
864 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
865 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
866 register_type
("WERROR",
867 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
868 register_type
("NTSTATUS",
869 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
873 #####################################################################
874 # Generate Wireshark parser and header code
877 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
878 Initialize
($cnf_file);
880 return (undef, undef) if defined($conformance->{noemit_dissector
});
884 %res = (code
=>"",def
=>"",hdr
=>"");
889 This filter was automatically generated
890 from $idl_file and $cnf_file.
892 Pidl is a perl based IDL compiler for DCE/RPC idl files.
893 It is maintained by the Samba team, not the Wireshark team.
894 Instructions on how to download and install Pidl can be
895 found at http://wiki.wireshark.org/Pidl
902 $res{headers
} = "\n";
903 $res{headers
} .= "#ifdef HAVE_CONFIG_H\n";
904 $res{headers
} .= "#include \"config.h\"\n";
905 $res{headers
} .= "#endif\n\n";
906 $res{headers
} .= "#include <glib.h>\n";
907 $res{headers
} .= "#include <string.h>\n";
908 $res{headers
} .= "#include <epan/packet.h>\n\n";
910 $res{headers
} .= "#include \"packet-dcerpc.h\"\n";
911 $res{headers
} .= "#include \"packet-dcerpc-nt.h\"\n";
912 $res{headers
} .= "#include \"packet-windows-common.h\"\n";
914 my $h_basename = basename
($h_filename);
916 $res{headers
} .= "#include \"$h_basename\"\n";
919 # Wireshark protocol registration
922 ProcessInterface
($_) if ($_->{TYPE
} eq "INTERFACE");
923 ProcessImport
(@
{$_->{PATHS
}}) if ($_->{TYPE
} eq "IMPORT");
924 ProcessInclude
(@
{$_->{PATHS
}}) if ($_->{TYPE
} eq "INCLUDE");
927 $res{ett
} = DumpEttDeclaration
();
928 $res{hf
} = DumpHfDeclaration
();
930 my $parser = $notice;
931 $parser.= $res{headers
};
935 if (exists ($conformance->{override
})) {
936 $parser.=$conformance->{override
};
940 my $header = "/* autogenerated by pidl */\n\n";
943 CheckUsed
($conformance);
945 return ($parser,$header);
948 ###############################################################################
950 ###############################################################################
961 my $res = "\tstatic gint *ett[] = {\n";
963 $res .= "\t\t&$_,\n";
969 sub DumpEttDeclaration
()
971 my $res = "\n/* Ett declarations */\n";
973 $res .= "static gint $_ = -1;\n";
979 ###############################################################################
981 ###############################################################################
983 sub register_hf_field
($$$$$$$$)
985 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
987 if (defined ($conformance->{hf_renames
}->{$index})) {
988 $conformance->{hf_renames
}->{$index}->{USED
} = 1;
989 return $conformance->{hf_renames
}->{$index}->{NEWNAME
};
992 $conformance->{header_fields
}->{$index} = {
995 FILTER
=> $filter_name,
997 BASE_TYPE
=> $base_type,
998 VALSSTRING
=> $valsstring,
1003 if ((not defined($blurb) or $blurb eq "") and
1004 defined($conformance->{fielddescription
}->{$index})) {
1005 $conformance->{header_fields
}->{$index}->{BLURB
} =
1006 $conformance->{fielddescription
}->{$index}->{DESCRIPTION
};
1007 $conformance->{fielddescription
}->{$index}->{USED
} = 1;
1013 sub DumpHfDeclaration
()
1017 $res = "\n/* Header field declarations */\n";
1019 foreach (keys %{$conformance->{header_fields
}})
1021 $res .= "static gint $_ = -1;\n";
1029 my $res = "\tstatic hf_register_info hf[] = {\n";
1031 foreach (values %{$conformance->{header_fields
}})
1033 $res .= "\t{ &$_->{INDEX},
1034 { ".make_str
($_->{NAME
}).", ".make_str
($_->{FILTER
}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str
($_->{BLURB
}).", HFILL }},
1038 return $res."\t};\n";
1042 ###############################################################################
1044 ###############################################################################
1046 sub DumpFunctionTable
($)
1050 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1051 foreach (@
{$if->{FUNCTIONS
}}) {
1052 my $fn_name = $_->{NAME
};
1053 $fn_name =~ s/^$if->{NAME}_//;
1054 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1055 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1058 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1065 my $conformance = shift;
1066 foreach (values %{$conformance->{header_fields
}}) {
1067 if (not defined($hf_used{$_->{INDEX
}})) {
1068 warning
($_->{POS
}, "hf field `$_->{INDEX}' not used");
1072 foreach (values %{$conformance->{hf_renames
}}) {
1073 if (not $_->{USED
}) {
1074 warning
($_->{POS
}, "hf field `$_->{OLDNAME}' not used");
1078 foreach (values %{$conformance->{dissectorparams
}}) {
1079 if (not $_->{USED
}) {
1080 warning
($_->{POS
}, "dissector param never used");
1084 foreach (values %{$conformance->{imports
}}) {
1085 if (not $_->{USED
}) {
1086 warning
($_->{POS
}, "import never used");
1090 foreach (values %{$conformance->{types
}}) {
1091 if (not $_->{USED
} and defined($_->{POS
})) {
1092 warning
($_->{POS
}, "type never used");
1096 foreach (values %{$conformance->{fielddescription
}}) {
1097 if (not $_->{USED
}) {
1098 warning
($_->{POS
}, "description never used");
1102 foreach (values %{$conformance->{tfs
}}) {
1103 if (not $_->{USED
}) {
1104 warning
($_->{POS
}, "True/False description never used");