r21568: More tests.
[Samba.git] / source / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
blob3c38801faea9c545bede2f6158d4a97469aed914
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
9 =pod
11 =head1 NAME
13 Parse::Pidl::Wireshark::NDR - Parser generator for Wireshark
15 =cut
17 package Parse::Pidl::Wireshark::NDR;
19 use Exporter;
20 @ISA = qw(Exporter);
21 @EXPORT_OK = qw(field2name @ett %res PrintIdl StripPrefixes %hf_used RegisterInterfaceHandoff $conformance register_hf_field CheckUsed ProcessImport ProcessInclude find_type DumpEttList DumpEttDeclaration DumpHfList DumpHfDeclaration DumpFunctionTable register_type register_ett);
23 use strict;
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);
30 use File::Basename;
32 use vars qw($VERSION);
33 $VERSION = '0.01';
35 our @ett;
37 our %hf_used = ();
38 my %return_types = ();
39 my %dissector_used = ();
41 our $conformance = undef;
43 my %ptrtype_mappings = (
44 "unique" => "NDR_POINTER_UNIQUE",
45 "ref" => "NDR_POINTER_REF",
46 "ptr" => "NDR_POINTER_PTR"
49 sub StripPrefixes($$)
51 my ($s, $prefixes) = @_;
53 foreach (@$prefixes) {
54 $s =~ s/^$_\_//g;
57 return $s;
60 # Convert a IDL structure field name (e.g access_mask) to a prettier
61 # string like 'Access Mask'.
63 sub field2name($)
65 my($field) = shift;
67 $field =~ s/_/ /g; # Replace underscores with spaces
68 $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
70 return $field;
73 our %res = ();
74 my $tabs = "";
75 my $cur_fn = undef;
76 sub pidl_fn_start($)
78 my $fn = shift;
79 $cur_fn = $fn;
81 sub pidl_fn_end($)
83 my $fn = shift;
84 die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
85 $cur_fn = undef;
88 sub pidl_code($)
90 my $d = shift;
91 return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
93 if ($d) {
94 $res{code} .= $tabs;
95 $res{code} .= $d;
97 $res{code} .="\n";
100 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
101 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
103 sub indent()
105 $tabs .= "\t";
108 sub deindent()
110 $tabs = substr($tabs, 0, -1);
113 sub PrintIdl($)
115 my ($idl) = @_;
117 foreach (split /\n/, $idl) {
118 pidl_code "/* IDL: $_ */";
121 pidl_code "";
124 #####################################################################
125 # parse the interface definitions
126 sub Interface($)
128 my($interface) = @_;
129 Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
130 Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}});
131 Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
134 sub Enum($$$)
136 my ($e,$name,$ifname) = @_;
137 my $valsstring = "$ifname\_$name\_vals";
138 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name, $conformance->{strip_prefixes});
140 return if (defined($conformance->{noemit}->{StripPrefixes($name, $conformance->{strip_prefixes})}));
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 }";
158 pidl_def "};";
160 pidl_fn_start $dissectorname;
161 pidl_code "int";
162 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param)";
163 pidl_code "{";
164 indent;
165 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
166 pidl_code "return offset;";
167 deindent;
168 pidl_code "}\n";
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);
176 sub Bitmap($$$)
178 my ($e,$name,$ifname) = @_;
179 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name, $conformance->{strip_prefixes});
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;
186 pidl_code "int";
187 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
188 pidl_code "{";
189 indent;
190 pidl_code "proto_item *item = NULL;";
191 pidl_code "proto_tree *tree = NULL;";
192 pidl_code "";
194 pidl_code "g$e->{BASE_TYPE} flags;";
195 if ($e->{ALIGN} > 1) {
196 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
199 pidl_code "";
201 pidl_code "if (parent_tree) {";
202 indent;
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);";
205 deindent;
206 pidl_code "}\n";
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;
229 } else {
230 pidl_def " \"$en is SET\",";
231 pidl_def " \"$en is NOT SET\",";
233 pidl_def "};";
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, \", \");";
240 pidl_code "}";
241 pidl_code "flags&=(~$ev);";
242 pidl_code "";
245 pidl_code "if (flags) {";
246 pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
247 pidl_code "}\n";
248 pidl_code "return offset;";
249 deindent;
250 pidl_code "}\n";
251 pidl_fn_end $dissectorname;
253 my $size = $e->{BASE_TYPE};
254 $size =~ s/uint//g;
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) = @_;
262 my $param = 0;
264 if (defined($conformance->{dissectorparams}->{$myname})) {
265 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
268 if ($l->{TYPE} eq "POINTER") {
269 my $type;
270 if ($l->{LEVEL} eq "TOP") {
271 $type = "toplevel";
272 } elsif ($l->{LEVEL} eq "EMBEDDED") {
273 $type = "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}, $conformance->{strip_prefixes})) . " ($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}) {
280 pidl_code "int i;";
281 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
282 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
283 } else {
284 my $type = "";
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\_);";
290 } else {
291 my $nl = GetNextLevel($e,$l);
292 pidl_code "char *data;";
293 pidl_code "";
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);";
311 } else {
312 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
314 } else {
315 my $call;
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;
327 } else {
328 pidl_code "offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);";
330 return;
333 $call =~ s/\@HF\@/$hf/g;
334 $call =~ s/\@PARAM\@/$param/g;
335 pidl_code "$call";
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);";
347 } else {
348 die("Unknown type `$_->{TYPE}'");
352 sub Element($$$)
354 my ($e,$pn,$ifname) = @_;
356 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $conformance->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $conformance->{strip_prefixes});
358 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
360 my $type = find_type($e->{TYPE});
362 if (not defined($type)) {
363 # default settings
364 $type = {
365 MASK => 0,
366 VALSSTRING => "NULL",
367 FT_TYPE => "FT_NONE",
368 BASE_TYPE => "BASE_HEX"
372 if (ContainsString($e)) {
373 $type = {
374 MASK => 0,
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}, "");
382 $hf_used{$hf} = 1;
384 my $eltname = StripPrefixes($pn, $conformance->{strip_prefixes}) . ".$e->{NAME}";
385 if (defined($conformance->{noemit}->{$eltname})) {
386 return $call_code;
389 my $add = "";
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)";
397 pidl_code "{";
398 indent;
400 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
402 pidl_code "";
403 pidl_code "return offset;";
404 deindent;
405 pidl_code "}\n";
406 pidl_fn_end "$dissectorname$add";
407 $add.="_";
408 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
411 return $call_code;
414 sub Function($$$)
416 my ($fn,$ifname) = @_;
418 my %dissectornames;
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_)";
431 pidl_code "{";
432 indent;
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";
442 } else {
443 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
445 } else {
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);";
454 pidl_code "";
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;";
489 deindent;
490 pidl_code "}\n";
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_)";
496 pidl_code "{";
497 indent;
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;";
508 deindent;
509 pidl_code "}\n";
510 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
513 sub Struct($$$)
515 my ($e,$name,$ifname) = @_;
516 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name, $conformance->{strip_prefixes});
518 return if (defined($conformance->{noemit}->{StripPrefixes($name, $conformance->{strip_prefixes})}));
520 register_ett("ett_$ifname\_$name");
522 my $res = "";
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;
528 pidl_code "int";
529 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
530 pidl_code "{";
531 indent;
532 pidl_code "proto_item *item = NULL;";
533 pidl_code "proto_tree *tree = NULL;";
534 pidl_code "int old_offset;";
535 pidl_code "";
537 if ($e->{ALIGN} > 1) {
538 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
540 pidl_code "";
542 pidl_code "old_offset = offset;";
543 pidl_code "";
544 pidl_code "if (parent_tree) {";
545 indent;
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);";
548 deindent;
549 pidl_code "}";
551 pidl_code "\n$res";
553 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
554 pidl_code "return offset;";
555 deindent;
556 pidl_code "}\n";
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);
562 sub Union($$$)
564 my ($e,$name,$ifname) = @_;
566 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $conformance->{strip_prefixes});
568 return if (defined($conformance->{noemit}->{StripPrefixes($name, $conformance->{strip_prefixes})}));
570 register_ett("ett_$ifname\_$name");
572 my $res = "";
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";
581 my $switch_type;
582 my $switch_dissect;
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_)";
595 pidl_code "{";
596 indent;
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;";
601 pidl_code "";
603 if ($e->{ALIGN} > 1) {
604 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
607 pidl_code "";
609 pidl_code "old_offset = offset;";
610 pidl_code "if (parent_tree) {";
611 indent;
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);";
614 deindent;
615 pidl_code "}";
617 pidl_code "";
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;";
624 deindent;
625 pidl_code "}";
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);
631 sub Const($$)
633 my ($const,$ifname) = @_;
635 if (!defined($const->{ARRAY_LEN}[0])) {
636 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
637 } else {
638 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
642 sub Typedef($$$)
644 my ($e,$name,$ifname) = @_;
646 Type($e->{DATA}, $name, $ifname);
649 sub Type($$$)
651 my ($e, $name, $ifname) = @_;
653 PrintIdl DumpType($e->{ORIGINAL});
656 ENUM => \&Enum,
657 STRUCT => \&Struct,
658 UNION => \&Union,
659 BITMAP => \&Bitmap,
660 TYPEDEF => \&Typedef
661 }->{$e->{TYPE}}->($e, $name, $ifname);
664 sub RegisterInterface($)
666 my ($x) = @_;
668 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
669 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
670 pidl_code "{";
671 indent;
673 $res{code}.=DumpHfList()."\n";
674 $res{code}.="\n".DumpEttList(@ett)."\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));";
698 } else {
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));";
704 deindent;
705 pidl_code "}\n";
706 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
709 sub RegisterInterfaceHandoff($)
711 my $x = shift;
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)";
716 pidl_code "{";
717 indent;
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);";
721 deindent;
722 pidl_code "}";
723 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
725 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
729 sub ProcessInclude
731 my @includes = @_;
732 foreach (@includes) {
733 pidl_hdr "#include \"$_\"";
735 pidl_hdr "";
738 sub ProcessImport
740 my @imports = @_;
741 foreach (@imports) {
742 next if($_ eq "security");
743 s/\.idl\"$//;
744 s/^\"//;
745 pidl_hdr "#include \"packet-dcerpc-$_\.h\"";
747 pidl_hdr "";
750 sub ProcessInterface($)
752 my ($x) = @_;
754 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
756 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
757 pidl_hdr "#ifndef $define";
758 pidl_hdr "#define $define";
759 pidl_hdr "";
761 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
762 register_ett("ett_dcerpc_$x->{NAME}");
763 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
765 if (defined($x->{UUID})) {
766 my $if_uuid = $x->{UUID};
768 pidl_def "/* Version information */\n\n";
770 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
771 pidl_def "\t0x" . substr($if_uuid, 1, 8)
772 . ", 0x" . substr($if_uuid, 10, 4)
773 . ", 0x" . substr($if_uuid, 15, 4) . ",";
774 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
775 . ", 0x" . substr($if_uuid, 22, 2)
776 . ", 0x" . substr($if_uuid, 25, 2)
777 . ", 0x" . substr($if_uuid, 27, 2)
778 . ", 0x" . substr($if_uuid, 29, 2)
779 . ", 0x" . substr($if_uuid, 31, 2)
780 . ", 0x" . substr($if_uuid, 33, 2)
781 . ", 0x" . substr($if_uuid, 35, 2) . " }";
782 pidl_def "};";
784 my $maj = $x->{VERSION};
785 $maj =~ s/\.(.*)$//g;
786 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
787 pidl_def "";
790 $return_types{$x->{NAME}} = {};
792 Interface($x);
794 pidl_code "\n".DumpFunctionTable($x);
796 foreach (keys %{$return_types{$x->{NAME}}}) {
797 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
798 my $dt = find_type($type);
799 $dt or die("Unable to find information about return type `$type'");
800 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
801 $hf_used{"hf_$x->{NAME}_$_"} = 1;
804 RegisterInterface($x);
805 RegisterInterfaceHandoff($x);
807 pidl_hdr "#endif /* $define */";
810 sub find_type($)
812 my $n = shift;
814 return $conformance->{types}->{$n};
817 sub register_type($$$$$$$)
819 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
821 $conformance->{types}->{$type} = {
822 NAME => $type,
823 DISSECTOR_NAME => $call,
824 FT_TYPE => $ft,
825 BASE_TYPE => $base,
826 MASK => $mask,
827 VALSSTRING => $vals,
828 ALIGNMENT => $length
832 # Loads the default types
833 sub Initialize($)
835 my $cnf_file = shift;
837 $conformance = {
838 imports => {},
839 header_fields=> {}
842 ReadConformance($cnf_file, $conformance) or print STDERR "warning: No conformance file `$cnf_file'\n";
844 foreach my $bytes (qw(1 2 4 8)) {
845 my $bits = $bytes * 8;
846 register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
847 register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
850 register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
851 register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
852 register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
853 register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
854 register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
855 register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
856 register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
857 register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
858 register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
859 register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
860 register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
861 register_type("SID", "
862 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
864 di->hf_index = \@HF\@;
866 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
867 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
868 register_type("WERROR",
869 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
870 register_type("NTSTATUS",
871 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
875 #####################################################################
876 # Generate Wireshark parser and header code
877 sub Parse($$$$)
879 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
880 Initialize($cnf_file);
882 return (undef, undef) if defined($conformance->{noemit_dissector});
884 $tabs = "";
886 %res = (code=>"",def=>"",hdr=>"");
887 @ett = ();
889 my $notice =
890 "/* DO NOT EDIT
891 This filter was automatically generated
892 from $idl_file and $cnf_file.
894 Pidl is a perl based IDL compiler for DCE/RPC idl files.
895 It is maintained by the Samba team, not the Wireshark team.
896 Instructions on how to download and install Pidl can be
897 found at http://wiki.wireshark.org/Pidl
902 pidl_hdr $notice;
904 $res{headers} = "\n";
905 $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
906 $res{headers} .= "#include \"config.h\"\n";
907 $res{headers} .= "#endif\n\n";
908 $res{headers} .= "#include <glib.h>\n";
909 $res{headers} .= "#include <string.h>\n";
910 $res{headers} .= "#include <epan/packet.h>\n\n";
912 $res{headers} .= "#include \"packet-dcerpc.h\"\n";
913 $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
914 $res{headers} .= "#include \"packet-windows-common.h\"\n";
916 my $h_basename = basename($h_filename);
918 $res{headers} .= "#include \"$h_basename\"\n";
919 pidl_code "";
921 # Wireshark protocol registration
923 foreach (@$ndr) {
924 ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
925 ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
926 ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
929 $res{ett} = DumpEttDeclaration(@ett);
930 $res{hf} = DumpHfDeclaration();
932 my $parser = $notice;
933 $parser.= $res{headers};
934 $parser.=$res{ett};
935 $parser.=$res{hf};
936 $parser.=$res{def};
937 if (exists ($conformance->{override})) {
938 $parser.=$conformance->{override};
940 $parser.=$res{code};
942 my $header = "/* autogenerated by pidl */\n\n";
943 $header.=$res{hdr};
945 CheckUsed($conformance);
947 return ($parser,$header);
950 ###############################################################################
951 # ETT
952 ###############################################################################
954 sub register_ett($)
956 my $name = shift;
958 push (@ett, $name);
961 sub DumpEttList
963 my @ett = @_;
964 my $res = "\tstatic gint *ett[] = {\n";
965 foreach (@ett) {
966 $res .= "\t\t&$_,\n";
969 return "$res\t};\n";
972 sub DumpEttDeclaration
974 my @ett = @_;
975 my $res = "\n/* Ett declarations */\n";
976 foreach (@ett) {
977 $res .= "static gint $_ = -1;\n";
980 return "$res\n";
983 ###############################################################################
984 # HF
985 ###############################################################################
987 sub register_hf_field($$$$$$$$)
989 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
991 if (defined ($conformance->{hf_renames}->{$index})) {
992 $conformance->{hf_renames}->{$index}->{USED} = 1;
993 return $conformance->{hf_renames}->{$index}->{NEWNAME};
996 $conformance->{header_fields}->{$index} = {
997 INDEX => $index,
998 NAME => $name,
999 FILTER => $filter_name,
1000 FT_TYPE => $ft_type,
1001 BASE_TYPE => $base_type,
1002 VALSSTRING => $valsstring,
1003 MASK => $mask,
1004 BLURB => $blurb
1007 if ((not defined($blurb) or $blurb eq "") and
1008 defined($conformance->{fielddescription}->{$index})) {
1009 $conformance->{header_fields}->{$index}->{BLURB} =
1010 $conformance->{fielddescription}->{$index}->{DESCRIPTION};
1011 $conformance->{fielddescription}->{$index}->{USED} = 1;
1014 return $index;
1017 sub DumpHfDeclaration()
1019 my $res = "";
1021 $res = "\n/* Header field declarations */\n";
1023 foreach (keys %{$conformance->{header_fields}})
1025 $res .= "static gint $_ = -1;\n";
1028 return "$res\n";
1031 sub DumpHfList()
1033 my $res = "\tstatic hf_register_info hf[] = {\n";
1035 foreach (values %{$conformance->{header_fields}})
1037 $res .= "\t{ &$_->{INDEX},
1038 { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
1042 return $res."\t};\n";
1046 ###############################################################################
1047 # Function table
1048 ###############################################################################
1050 sub DumpFunctionTable($)
1052 my $if = shift;
1054 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1055 foreach (@{$if->{FUNCTIONS}}) {
1056 my $fn_name = $_->{NAME};
1057 $fn_name =~ s/^$if->{NAME}_//;
1058 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1059 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1062 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1064 return "$res};\n";
1067 sub CheckUsed($)
1069 my $conformance = shift;
1070 foreach (values %{$conformance->{header_fields}}) {
1071 if (not defined($hf_used{$_->{INDEX}})) {
1072 warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1076 foreach (values %{$conformance->{hf_renames}}) {
1077 if (not $_->{USED}) {
1078 warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1082 foreach (values %{$conformance->{dissectorparams}}) {
1083 if (not $_->{USED}) {
1084 warning($_->{POS}, "dissector param never used");
1088 foreach (values %{$conformance->{imports}}) {
1089 if (not $_->{USED}) {
1090 warning($_->{POS}, "import never used");
1094 foreach (values %{$conformance->{types}}) {
1095 if (not $_->{USED} and defined($_->{POS})) {
1096 warning($_->{POS}, "type never used");
1100 foreach (values %{$conformance->{fielddescription}}) {
1101 if (not $_->{USED}) {
1102 warning($_->{POS}, "description never used");
1106 foreach (values %{$conformance->{tfs}}) {
1107 if (not $_->{USED}) {
1108 warning($_->{POS}, "True/False description never used");