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 qw(error);
21 use Parse::Pidl::Typelist qw(getType);
22 use Parse::Pidl::Util qw(has_property ParseExpr property_matches make_str);
23 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
24 use Parse::Pidl::Dump qw(DumpTypedef DumpFunction);
25 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
28 use vars qw($VERSION);
34 my %return_types = ();
35 my %dissector_used = ();
37 my $conformance = undef;
39 my %ptrtype_mappings = (
40 "unique" => "NDR_POINTER_UNIQUE",
41 "ref" => "NDR_POINTER_REF",
42 "ptr" => "NDR_POINTER_PTR"
49 foreach (@{$conformance->{strip_prefixes}}) {
56 # Convert a IDL structure field name (e.g access_mask) to a prettier
57 # string like 'Access Mask'.
63 $field =~ s/_/ /g; # Replace underscores with spaces
64 $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
80 die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
87 return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
96 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
97 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
106 $tabs = substr($tabs, 0, -1);
113 foreach (split /\n/, $idl) {
114 pidl_code "/* IDL: $_ */";
120 #####################################################################
121 # parse the interface definitions
125 Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
126 Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPES}});
127 Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
132 my ($e,$name,$ifname) = @_;
133 my $valsstring = "$ifname\_$name\_vals";
134 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
136 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
138 foreach (@{$e->{ELEMENTS}}) {
139 if (/([^=]*)=(.*)/) {
140 pidl_hdr "#define $1 ($2)";
144 pidl_hdr "extern const value_string $valsstring\[];";
145 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param);";
147 pidl_def "const value_string ".$valsstring."[] = {";
148 foreach (@{$e->{ELEMENTS}}) {
149 next unless (/([^=]*)=(.*)/);
150 pidl_def "\t{ $1, \"$1\" },";
153 pidl_def "{ 0, NULL }";
156 pidl_fn_start $dissectorname;
158 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param)";
161 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
162 pidl_code "return offset;";
165 pidl_fn_end $dissectorname;
167 my $enum_size = $e->{BASE_TYPE};
168 $enum_size =~ s/uint//g;
169 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
174 my ($e,$name,$ifname) = @_;
175 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
177 register_ett("ett_$ifname\_$name");
179 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
181 pidl_fn_start $dissectorname;
183 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
186 pidl_code "proto_item *item = NULL;";
187 pidl_code "proto_tree *tree = NULL;";
190 pidl_code "g$e->{BASE_TYPE} flags;";
191 if ($e->{ALIGN} > 1) {
192 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
197 pidl_code "if (parent_tree) {";
199 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
200 pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
204 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
206 pidl_code "proto_item_append_text(item, \": \");\n";
207 pidl_code "if (!flags)";
208 pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
210 foreach (@{$e->{ELEMENTS}}) {
211 next unless (/([^ ]*) (.*)/);
212 my ($en,$ev) = ($1,$2);
213 my $hf_bitname = "hf_$ifname\_$name\_$en";
214 my $filtername = "$ifname\.$name\.$en";
216 $hf_used{$hf_bitname} = 1;
218 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
220 pidl_def "static const true_false_string $name\_$en\_tfs = {";
221 if (defined($conformance->{tfs}->{$hf_bitname})) {
222 pidl_def " $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
223 pidl_def " $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
224 $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
226 pidl_def " \"$en is SET\",";
227 pidl_def " \"$en is NOT SET\",";
231 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
232 pidl_code "if (flags&$ev){";
233 pidl_code "\tproto_item_append_text(item, \"$en\");";
234 pidl_code "\tif (flags & (~$ev))";
235 pidl_code "\t\tproto_item_append_text(item, \", \");";
237 pidl_code "flags&=(~$ev);";
241 pidl_code "if (flags) {";
242 pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
244 pidl_code "return offset;";
247 pidl_fn_end $dissectorname;
249 my $size = $e->{BASE_TYPE};
251 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
254 sub ElementLevel($$$$$$)
256 my ($e,$l,$hf,$myname,$pn,$ifname) = @_;
260 if (defined($conformance->{dissectorparams}->{$myname})) {
261 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
264 if ($l->{TYPE} eq "POINTER") {
266 if ($l->{LEVEL} eq "TOP") {
268 } elsif ($l->{LEVEL} eq "EMBEDDED") {
271 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);";
272 } elsif ($l->{TYPE} eq "ARRAY") {
273 if ($l->{IS_INLINE}) {
274 error($e->{ORIGINAL}, "Inline arrays not supported");
275 } elsif ($l->{IS_FIXED}) {
277 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
278 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
281 $type .= "c" if ($l->{IS_CONFORMANT});
282 $type .= "v" if ($l->{IS_VARYING});
284 unless ($l->{IS_ZERO_TERMINATED}) {
285 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
287 my $nl = GetNextLevel($e,$l);
288 pidl_code "char *data;";
290 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
291 pidl_code "proto_item_append_text(tree, \": %s\", data);";
294 } elsif ($l->{TYPE} eq "DATA") {
295 if ($l->{DATA_TYPE} eq "string") {
296 my $bs = 2; # Byte size defaults to that of UCS2
299 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
301 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
302 pidl_code "char *data;\n";
303 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
304 pidl_code "proto_item_append_text(tree, \": %s\", data);";
305 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
306 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
308 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
313 if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
314 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};
315 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
316 } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
317 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
318 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
320 } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
321 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
322 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
324 pidl_code "offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);";
329 $call =~ s/\@HF\@/$hf/g;
330 $call =~ s/\@PARAM\@/$param/g;
333 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
334 my $num_bits = ($l->{HEADER_SIZE}*8);
335 pidl_code "guint$num_bits size;";
336 pidl_code "int start_offset = offset;";
337 pidl_code "tvbuff_t *subtvb;";
338 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
339 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
341 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
342 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
344 die("Unknown type `$_->{TYPE}'");
350 my ($e,$pn,$ifname) = @_;
352 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
354 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
356 my $type = find_type($e->{TYPE});
358 if (not defined($type)) {
362 VALSSTRING => "NULL",
363 FT_TYPE => "FT_NONE",
364 BASE_TYPE => "BASE_HEX"
368 if (ContainsString($e)) {
371 VALSSTRING => "NULL",
372 FT_TYPE => "FT_STRING",
373 BASE_TYPE => "BASE_DEC"
377 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}, "");
380 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
381 if (defined($conformance->{noemit}->{$eltname})) {
387 foreach (@{$e->{LEVELS}}) {
388 next if ($_->{TYPE} eq "SWITCH");
389 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
390 pidl_fn_start "$dissectorname$add";
391 pidl_code "static int";
392 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
396 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
399 pidl_code "return offset;";
402 pidl_fn_end "$dissectorname$add";
404 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
412 my ($fn,$ifname) = @_;
416 foreach (@{$fn->{ELEMENTS}}) {
417 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
420 my $fn_name = $_->{NAME};
421 $fn_name =~ s/^${ifname}_//;
423 PrintIdl DumpFunction($fn->{ORIGINAL});
424 pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
425 pidl_code "static int";
426 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_)";
429 if ( not defined($fn->{RETURN_TYPE})) {
430 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
432 pidl_code "guint32 status;\n";
433 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
434 if ($type->{DATA}->{TYPE} eq "ENUM") {
435 pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
436 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
437 pidl_code "g$fn->{RETURN_TYPE} status;\n";
439 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
442 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
445 foreach (@{$fn->{ELEMENTS}}) {
446 if (grep(/out/,@{$_->{DIRECTION}})) {
447 pidl_code "$dissectornames{$_->{NAME}}";
448 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
453 if (not defined($fn->{RETURN_TYPE})) {
454 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
455 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
456 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
457 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
458 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
459 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
460 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &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, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
464 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
465 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
466 if ($type->{DATA}->{TYPE} eq "ENUM") {
467 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
468 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
470 pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
471 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
472 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";
473 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
474 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
475 pidl_code "offset = dissect_ndr_$fn->{RETURN_TYPE}(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: %d\", status);\n";
478 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
483 pidl_code "return offset;";
486 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
488 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
489 pidl_code "static int";
490 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_)";
493 foreach (@{$fn->{ELEMENTS}}) {
494 if (grep(/in/,@{$_->{DIRECTION}})) {
495 pidl_code "$dissectornames{$_->{NAME}}";
496 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
501 pidl_code "return offset;";
504 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
509 my ($e,$name,$ifname) = @_;
510 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
512 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
514 register_ett("ett_$ifname\_$name");
517 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
519 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
521 pidl_fn_start $dissectorname;
523 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
526 pidl_code "proto_item *item = NULL;";
527 pidl_code "proto_tree *tree = NULL;";
528 pidl_code "int old_offset;";
531 if ($e->{ALIGN} > 1) {
532 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
536 pidl_code "old_offset = offset;";
538 pidl_code "if (parent_tree) {";
540 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
541 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
547 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
548 pidl_code "return offset;";
551 pidl_fn_end $dissectorname;
553 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
558 my ($e,$name,$ifname) = @_;
560 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
562 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
564 register_ett("ett_$ifname\_$name");
567 foreach (@{$e->{ELEMENTS}}) {
568 $res.="\n\t\t$_->{CASE}:\n";
569 if ($_->{TYPE} ne "EMPTY") {
570 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
572 $res.="\t\tbreak;\n";
577 my $switch_dt = getType($e->{SWITCH_TYPE});
578 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
579 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
580 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
581 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
582 $switch_type = "g$e->{SWITCH_TYPE}";
583 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
586 pidl_fn_start $dissectorname;
587 pidl_code "static int";
588 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
591 pidl_code "proto_item *item = NULL;";
592 pidl_code "proto_tree *tree = NULL;";
593 pidl_code "int old_offset;";
594 pidl_code "$switch_type level;";
597 if ($e->{ALIGN} > 1) {
598 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
603 pidl_code "old_offset = offset;";
604 pidl_code "if (parent_tree) {";
606 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
607 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
613 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
615 pidl_code "switch(level) {$res\t}";
616 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
617 pidl_code "return offset;";
620 pidl_fn_end $dissectorname;
622 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
627 my ($const,$ifname) = @_;
629 if (!defined($const->{ARRAY_LEN}[0])) {
630 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
632 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
638 my ($e,$ifname) = @_;
640 PrintIdl DumpTypedef($e->{ORIGINAL});
647 }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
650 sub RegisterInterface($)
654 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
655 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
659 $res{code}.=DumpHfList()."\n";
660 $res{code}.="\n".DumpEttList()."\n";
662 if (defined($x->{UUID})) {
663 # These can be changed to non-pidl_code names if the old dissectors
664 # in epan/dissctors are deleted.
666 my $name = uc($x->{NAME}) . " (pidl)";
667 my $short_name = uc($x->{NAME});
668 my $filter_name = $x->{NAME};
670 if (has_property($x, "helpstring")) {
671 $name = $x->{PROPERTIES}->{helpstring};
674 if (defined($conformance->{protocols}->{$x->{NAME}})) {
675 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
676 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
677 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
680 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
682 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
683 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
685 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
686 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
687 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
692 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
695 sub RegisterInterfaceHandoff($)
699 if (defined($x->{UUID})) {
700 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
701 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
704 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
705 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
706 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
709 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
711 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
718 foreach (@includes) {
719 pidl_hdr "#include \"$_\"\n";
727 next if($_ eq "security");
730 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
734 sub ProcessInterface($)
738 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
740 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
741 pidl_hdr "#ifndef $define";
742 pidl_hdr "#define $define";
745 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
746 register_ett("ett_dcerpc_$x->{NAME}");
747 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
749 if (defined($x->{UUID})) {
750 my $if_uuid = $x->{UUID};
752 pidl_def "/* Version information */\n\n";
754 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
755 pidl_def "\t0x" . substr($if_uuid, 1, 8)
756 . ", 0x" . substr($if_uuid, 10, 4)
757 . ", 0x" . substr($if_uuid, 15, 4) . ",";
758 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
759 . ", 0x" . substr($if_uuid, 22, 2)
760 . ", 0x" . substr($if_uuid, 25, 2)
761 . ", 0x" . substr($if_uuid, 27, 2)
762 . ", 0x" . substr($if_uuid, 29, 2)
763 . ", 0x" . substr($if_uuid, 31, 2)
764 . ", 0x" . substr($if_uuid, 33, 2)
765 . ", 0x" . substr($if_uuid, 35, 2) . " }";
768 my $maj = $x->{VERSION};
769 $maj =~ s/\.(.*)$//g;
770 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
774 $return_types{$x->{NAME}} = {};
778 pidl_code "\n".DumpFunctionTable($x);
780 foreach (keys %{$return_types{$x->{NAME}}}) {
781 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
782 my $dt = find_type($type);
783 $dt or die("Unable to find information about return type `$type'");
784 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
785 $hf_used{"hf_$x->{NAME}_$_"} = 1;
788 RegisterInterface($x);
789 RegisterInterfaceHandoff($x);
791 pidl_hdr "#endif /* $define */";
798 return $conformance->{types}->{$n};
801 sub register_type($$$$$$$)
803 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
805 $conformance->{types}->{$type} = {
807 DISSECTOR_NAME => $call,
816 # Loads the default types
819 my $cnf_file = shift;
826 ReadConformance($cnf_file, $conformance) or print STDERR "warning: No conformance file `$cnf_file'\n";
828 foreach my $bytes (qw(1 2 4 8)) {
829 my $bits = $bytes * 8;
830 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
831 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
834 register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
835 register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
836 register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
837 register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
838 register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
839 register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
840 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);
841 register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
842 register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
843 register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
844 register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
845 register_type("SID", "
846 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
848 di->hf_index = \@HF\@;
850 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
851 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
852 register_type("WERROR",
853 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
854 register_type("NTSTATUS",
855 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
859 #####################################################################
860 # Generate Wireshark parser and header code
863 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
864 Initialize($cnf_file);
866 return (undef, undef) if defined($conformance->{noemit_dissector});
870 %res = (code=>"",def=>"",hdr=>"");
875 This filter was automatically generated
876 from $idl_file and $cnf_file.
878 Pidl is a perl based IDL compiler for DCE/RPC idl files.
879 It is maintained by the Samba team, not the Wireshark team.
880 Instructions on how to download and install Pidl can be
881 found at http://wiki.wireshark.org/Pidl
888 $res{headers} = "\n";
889 $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
890 $res{headers} .= "#include \"config.h\"\n";
891 $res{headers} .= "#endif\n\n";
892 $res{headers} .= "#include <glib.h>\n";
893 $res{headers} .= "#include <string.h>\n";
894 $res{headers} .= "#include <epan/packet.h>\n\n";
896 $res{headers} .= "#include \"packet-dcerpc.h\"\n";
897 $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
898 $res{headers} .= "#include \"packet-windows-common.h\"\n";
900 my $h_basename = basename($h_filename);
902 $res{headers} .= "#include \"$h_basename\"\n";
905 # Wireshark protocol registration
908 ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
909 ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
910 ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
913 $res{ett} = DumpEttDeclaration();
914 $res{hf} = DumpHfDeclaration();
916 my $parser = $notice;
917 $parser.= $res{headers};
921 $parser.=$conformance->{override};
924 my $header = "/* autogenerated by pidl */\n\n";
927 CheckUsed($conformance);
929 return ($parser,$header);
932 ###############################################################################
934 ###############################################################################
945 my $res = "\tstatic gint *ett[] = {\n";
947 $res .= "\t\t&$_,\n";
953 sub DumpEttDeclaration()
955 my $res = "\n/* Ett declarations */\n";
957 $res .= "static gint $_ = -1;\n";
963 ###############################################################################
965 ###############################################################################
967 sub register_hf_field($$$$$$$$)
969 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
971 if (defined ($conformance->{hf_renames}->{$index})) {
972 $conformance->{hf_renames}->{$index}->{USED} = 1;
973 return $conformance->{hf_renames}->{$index}->{NEWNAME};
976 $conformance->{header_fields}->{$index} = {
979 FILTER => $filter_name,
981 BASE_TYPE => $base_type,
982 VALSSTRING => $valsstring,
987 if ((not defined($blurb) or $blurb eq "") and
988 defined($conformance->{fielddescription}->{$index})) {
989 $conformance->{header_fields}->{$index}->{BLURB} =
990 $conformance->{fielddescription}->{$index}->{DESCRIPTION};
991 $conformance->{fielddescription}->{$index}->{USED} = 1;
997 sub DumpHfDeclaration()
1001 $res = "\n/* Header field declarations */\n";
1003 foreach (keys %{$conformance->{header_fields}})
1005 $res .= "static gint $_ = -1;\n";
1013 my $res = "\tstatic hf_register_info hf[] = {\n";
1015 foreach (values %{$conformance->{header_fields}})
1017 $res .= "\t{ &$_->{INDEX},
1018 { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
1022 return $res."\t};\n";
1026 ###############################################################################
1028 ###############################################################################
1030 sub DumpFunctionTable($)
1034 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1035 foreach (@{$if->{FUNCTIONS}}) {
1036 my $fn_name = $_->{NAME};
1037 $fn_name =~ s/^$if->{NAME}_//;
1038 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1039 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1042 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1049 my $conformance = shift;
1050 foreach (values %{$conformance->{header_fields}}) {
1051 if (not defined($hf_used{$_->{INDEX}})) {
1052 warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1056 foreach (values %{$conformance->{hf_renames}}) {
1057 if (not $_->{USED}) {
1058 warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1062 foreach (values %{$conformance->{dissectorparams}}) {
1063 if (not $_->{USED}) {
1064 warning($_->{POS}, "dissector param never used");
1068 foreach (values %{$conformance->{imports}}) {
1069 if (not $_->{USED}) {
1070 warning($_->{POS}, "import never used");
1074 foreach (values %{$conformance->{types}}) {
1075 if (not $_->{USED} and defined($_->{POS})) {
1076 warning($_->{POS}, "type never used");
1080 foreach (values %{$conformance->{fielddescription}}) {
1081 if (not $_->{USED}) {
1082 warning($_->{POS}, "description never used");
1086 foreach (values %{$conformance->{tfs}}) {
1087 if (not $_->{USED}) {
1088 warning($_->{POS}, "True/False description never used");