r19557: Fix lookup table for WERROR.
[Samba/ekacnet.git] / source / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
blobe211a896ad979aeeed11b311b5f954ac61eaa240
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
9 =pod
11 =head1 NAME
13 Parse::Pidl::Wireshark::NDR - Parser generator for Wireshark
15 =cut
17 package Parse::Pidl::Wireshark::NDR;
19 use strict;
20 use Parse::Pidl::Typelist qw(getType);
21 use Parse::Pidl::Util qw(has_property ParseExpr property_matches make_str);
22 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
23 use Parse::Pidl::Dump qw(DumpTypedef DumpFunction);
24 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
25 use File::Basename;
27 use vars qw($VERSION);
28 $VERSION = '0.01';
30 sub error($$)
32 my ($e,$t) = @_;
33 print "$e->{FILE}:$e->{LINE}: $t\n";
36 my @ett;
38 my %hf_used = ();
39 my %return_types = ();
40 my %dissector_used = ();
42 my $conformance = undef;
44 my %ptrtype_mappings = (
45 "unique" => "NDR_POINTER_UNIQUE",
46 "ref" => "NDR_POINTER_REF",
47 "ptr" => "NDR_POINTER_PTR"
50 sub StripPrefixes($)
52 my ($s) = @_;
54 foreach (@{$conformance->{strip_prefixes}}) {
55 $s =~ s/^$_\_//g;
58 return $s;
61 # Convert a IDL structure field name (e.g access_mask) to a prettier
62 # string like 'Access Mask'.
64 sub field2name($)
66 my($field) = shift;
68 $field =~ s/_/ /g; # Replace underscores with spaces
69 $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
71 return $field;
74 my %res = ();
75 my $tabs = "";
76 my $cur_fn = undef;
77 sub pidl_fn_start($)
79 my $fn = shift;
80 $cur_fn = $fn;
82 sub pidl_fn_end($)
84 my $fn = shift;
85 die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
86 $cur_fn = undef;
89 sub pidl_code($)
91 my $d = shift;
92 return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
94 if ($d) {
95 $res{code} .= $tabs;
96 $res{code} .= $d;
98 $res{code} .="\n";
101 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
102 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
104 sub indent()
106 $tabs .= "\t";
109 sub deindent()
111 $tabs = substr($tabs, 0, -1);
114 sub PrintIdl($)
116 my $idl = shift;
118 foreach (split /\n/, $idl) {
119 pidl_code "/* IDL: $_ */";
122 pidl_code "";
125 #####################################################################
126 # parse the interface definitions
127 sub Interface($)
129 my($interface) = @_;
130 Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
131 Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPES}});
132 Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
135 sub Enum($$$)
137 my ($e,$name,$ifname) = @_;
138 my $valsstring = "$ifname\_$name\_vals";
139 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
141 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
143 foreach (@{$e->{ELEMENTS}}) {
144 if (/([^=]*)=(.*)/) {
145 pidl_hdr "#define $1 ($2)";
149 pidl_hdr "extern const value_string $valsstring\[];";
150 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param);";
152 pidl_def "const value_string ".$valsstring."[] = {";
153 foreach (@{$e->{ELEMENTS}}) {
154 next unless (/([^=]*)=(.*)/);
155 pidl_def "\t{ $1, \"$1\" },";
158 pidl_def "{ 0, NULL }";
159 pidl_def "};";
161 pidl_fn_start $dissectorname;
162 pidl_code "int";
163 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param)";
164 pidl_code "{";
165 indent;
166 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
167 pidl_code "return offset;";
168 deindent;
169 pidl_code "}\n";
170 pidl_fn_end $dissectorname;
172 my $enum_size = $e->{BASE_TYPE};
173 $enum_size =~ s/uint//g;
174 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
177 sub Bitmap($$$)
179 my ($e,$name,$ifname) = @_;
180 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
182 register_ett("ett_$ifname\_$name");
184 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
186 pidl_fn_start $dissectorname;
187 pidl_code "int";
188 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
189 pidl_code "{";
190 indent;
191 pidl_code "proto_item *item = NULL;";
192 pidl_code "proto_tree *tree = NULL;";
193 pidl_code "";
195 pidl_code "g$e->{BASE_TYPE} flags;";
196 if ($e->{ALIGN} > 1) {
197 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
200 pidl_code "";
202 pidl_code "if (parent_tree) {";
203 indent;
204 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
205 pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
206 deindent;
207 pidl_code "}\n";
209 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
211 pidl_code "proto_item_append_text(item, \": \");\n";
212 pidl_code "if (!flags)";
213 pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
215 foreach (@{$e->{ELEMENTS}}) {
216 next unless (/([^ ]*) (.*)/);
217 my ($en,$ev) = ($1,$2);
218 my $hf_bitname = "hf_$ifname\_$name\_$en";
219 my $filtername = "$ifname\.$name\.$en";
221 $hf_used{$hf_bitname} = 1;
223 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
225 pidl_def "static const true_false_string $name\_$en\_tfs = {";
226 if (defined($conformance->{tfs}->{$hf_bitname})) {
227 pidl_def " $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
228 pidl_def " $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
229 $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
230 } else {
231 pidl_def " \"$en is SET\",";
232 pidl_def " \"$en is NOT SET\",";
234 pidl_def "};";
236 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
237 pidl_code "if (flags&$ev){";
238 pidl_code "\tproto_item_append_text(item, \"$en\");";
239 pidl_code "\tif (flags & (~$ev))";
240 pidl_code "\t\tproto_item_append_text(item, \", \");";
241 pidl_code "}";
242 pidl_code "flags&=(~$ev);";
243 pidl_code "";
246 pidl_code "if (flags) {";
247 pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
248 pidl_code "}\n";
249 pidl_code "return offset;";
250 deindent;
251 pidl_code "}\n";
252 pidl_fn_end $dissectorname;
254 my $size = $e->{BASE_TYPE};
255 $size =~ s/uint//g;
256 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
259 sub ElementLevel($$$$$)
261 my ($e,$l,$hf,$myname,$pn) = @_;
263 my $param = 0;
265 if (defined($conformance->{dissectorparams}->{$myname})) {
266 $conformance->{dissectorparams}->{$myname}->{PARAM} = 1;
267 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
270 if ($l->{TYPE} eq "POINTER") {
271 my $type;
272 if ($l->{LEVEL} eq "TOP") {
273 $type = "toplevel";
274 } elsif ($l->{LEVEL} eq "EMBEDDED") {
275 $type = "embedded";
277 pidl_code "offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME})) . " ($e->{TYPE})\",$hf);";
278 } elsif ($l->{TYPE} eq "ARRAY") {
279 if ($l->{IS_INLINE}) {
280 error($e->{ORIGINAL}, "Inline arrays not supported");
281 } elsif ($l->{IS_FIXED}) {
282 pidl_code "int i;";
283 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
284 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
285 } else {
286 my $type = "";
287 $type .= "c" if ($l->{IS_CONFORMANT});
288 $type .= "v" if ($l->{IS_VARYING});
290 unless ($l->{IS_ZERO_TERMINATED}) {
291 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
292 } else {
293 my $nl = GetNextLevel($e,$l);
294 pidl_code "char *data;";
295 pidl_code "";
296 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
297 pidl_code "proto_item_append_text(tree, \": %s\", data);";
300 } elsif ($l->{TYPE} eq "DATA") {
301 if ($l->{DATA_TYPE} eq "string") {
302 my $bs = 2; # Byte size defaults to that of UCS2
305 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
307 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
308 pidl_code "char *data;\n";
309 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
310 pidl_code "proto_item_append_text(tree, \": %s\", data);";
311 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
312 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
313 } else {
314 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
316 } else {
317 my $call;
319 if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
320 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};
321 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
322 } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
323 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
324 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
326 } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
327 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
328 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
329 } else {
330 if ($l->{DATA_TYPE} =~ /^([a-z]+)\_(.*)$/)
332 pidl_code "offset = $1_dissect_struct_$2(tvb,offset,pinfo,tree,drep,$hf,$param);";
335 return;
338 $call =~ s/\@HF\@/$hf/g;
339 $call =~ s/\@PARAM\@/$param/g;
340 pidl_code "$call";
342 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
343 my $num_bits = ($l->{HEADER_SIZE}*8);
344 pidl_code "guint$num_bits size;";
345 pidl_code "int start_offset = offset;";
346 pidl_code "tvbuff_t *subtvb;";
347 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
348 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
350 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
351 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
352 } else {
353 die("Unknown type `$_->{TYPE}'");
357 sub Element($$$)
359 my ($e,$pn,$ifname) = @_;
361 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
363 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
365 my $type = find_type($e->{TYPE});
367 if (not defined($type)) {
368 # default settings
369 $type = {
370 MASK => 0,
371 VALSSTRING => "NULL",
372 FT_TYPE => "FT_NONE",
373 BASE_TYPE => "BASE_HEX"
377 if (ContainsString($e)) {
378 $type = {
379 MASK => 0,
380 VALSSTRING => "NULL",
381 FT_TYPE => "FT_STRING",
382 BASE_TYPE => "BASE_DEC"
386 my $hf = register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, "");
387 $hf_used{$hf} = 1;
389 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
390 if (defined($conformance->{noemit}->{$eltname})) {
391 return $call_code;
394 my $add = "";
396 foreach (@{$e->{LEVELS}}) {
397 next if ($_->{TYPE} eq "SWITCH");
398 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
399 pidl_fn_start "$dissectorname$add";
400 pidl_code "static int";
401 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
402 pidl_code "{";
403 indent;
405 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn);
407 pidl_code "";
408 pidl_code "return offset;";
409 deindent;
410 pidl_code "}\n";
411 pidl_fn_end "$dissectorname$add";
412 $add.="_";
413 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
416 return $call_code;
419 sub Function($$$)
421 my ($fn,$ifname) = @_;
423 my %dissectornames;
425 foreach (@{$fn->{ELEMENTS}}) {
426 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
429 my $fn_name = $_->{NAME};
430 $fn_name =~ s/^${ifname}_//;
432 PrintIdl DumpFunction($fn->{ORIGINAL});
433 pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
434 pidl_code "static int";
435 pidl_code "$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)";
436 pidl_code "{";
437 indent;
438 if ( not defined($fn->{RETURN_TYPE})) {
439 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
441 pidl_code "guint32 status;\n";
442 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
443 if ($type->{DATA}->{TYPE} eq "ENUM") {
444 pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
445 } else {
446 print "$fn->{FILE}:$fn->{LINE}: error: return type `$fn->{RETURN_TYPE}' not yet supported\n";
448 } else {
449 print "$fn->{FILE}:$fn->{LINE}: error: unknown return type `$fn->{RETURN_TYPE}'\n";
452 foreach (@{$fn->{ELEMENTS}}) {
453 if (grep(/out/,@{$_->{DIRECTION}})) {
454 pidl_code "$dissectornames{$_->{NAME}}";
455 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
456 pidl_code "";
460 if (not defined($fn->{RETURN_TYPE})) {
461 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
462 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
463 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
464 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
465 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
466 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
467 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
468 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
469 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
471 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
472 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
473 if ($type->{DATA}->{TYPE} eq "ENUM") {
474 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
475 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
477 pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
478 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
479 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %s\", val_to_str(status, $ifname\_$fn->{RETURN_TYPE}\_vals, \"Unknown " . $fn->{RETURN_TYPE} . " error 0x%08x\"));\n";
480 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
485 pidl_code "return offset;";
486 deindent;
487 pidl_code "}\n";
488 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
490 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
491 pidl_code "static int";
492 pidl_code "$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_)";
493 pidl_code "{";
494 indent;
495 foreach (@{$fn->{ELEMENTS}}) {
496 if (grep(/in/,@{$_->{DIRECTION}})) {
497 pidl_code "$dissectornames{$_->{NAME}}";
498 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
503 pidl_code "return offset;";
504 deindent;
505 pidl_code "}\n";
506 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
509 sub Struct($$$)
511 my ($e,$name,$ifname) = @_;
512 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
514 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
516 register_ett("ett_$ifname\_$name");
518 my $res = "";
519 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
521 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
523 pidl_fn_start $dissectorname;
524 pidl_code "int";
525 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 "{";
527 indent;
528 pidl_code "proto_item *item = NULL;";
529 pidl_code "proto_tree *tree = NULL;";
530 pidl_code "int old_offset;";
531 pidl_code "";
533 if ($e->{ALIGN} > 1) {
534 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
536 pidl_code "";
538 pidl_code "old_offset = offset;";
539 pidl_code "";
540 pidl_code "if (parent_tree) {";
541 indent;
542 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
543 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
544 deindent;
545 pidl_code "}";
547 pidl_code "\n$res";
549 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
550 pidl_code "return offset;";
551 deindent;
552 pidl_code "}\n";
553 pidl_fn_end $dissectorname;
555 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
558 sub Union($$$)
560 my ($e,$name,$ifname) = @_;
562 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
564 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
566 register_ett("ett_$ifname\_$name");
568 my $res = "";
569 foreach (@{$e->{ELEMENTS}}) {
570 $res.="\n\t\t$_->{CASE}:\n";
571 if ($_->{TYPE} ne "EMPTY") {
572 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
574 $res.="\t\tbreak;\n";
577 my $switch_type;
578 my $switch_dissect;
579 my $switch_dt = getType($e->{SWITCH_TYPE});
580 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
581 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
582 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
583 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
584 $switch_type = "g$e->{SWITCH_TYPE}";
585 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
588 pidl_fn_start $dissectorname;
589 pidl_code "static int";
590 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
591 pidl_code "{";
592 indent;
593 pidl_code "proto_item *item = NULL;";
594 pidl_code "proto_tree *tree = NULL;";
595 pidl_code "int old_offset;";
596 pidl_code "$switch_type level;";
597 pidl_code "";
599 if ($e->{ALIGN} > 1) {
600 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
603 pidl_code "";
605 pidl_code "old_offset = offset;";
606 pidl_code "if (parent_tree) {";
607 indent;
608 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
609 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
610 deindent;
611 pidl_code "}";
613 pidl_code "";
615 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
617 pidl_code "switch(level) {$res\t}";
618 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
619 pidl_code "return offset;";
620 deindent;
621 pidl_code "}";
622 pidl_fn_end $dissectorname;
624 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
627 sub Const($$)
629 my ($const,$ifname) = @_;
631 if (!defined($const->{ARRAY_LEN}[0])) {
632 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
633 } else {
634 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
638 sub Typedef($$)
640 my ($e,$ifname) = @_;
642 PrintIdl DumpTypedef($e->{ORIGINAL});
645 ENUM => \&Enum,
646 STRUCT => \&Struct,
647 UNION => \&Union,
648 BITMAP => \&Bitmap
649 }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
652 sub RegisterInterface($)
654 my ($x) = @_;
656 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
657 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
658 pidl_code "{";
659 indent;
661 $res{code}.=DumpHfList()."\n";
662 $res{code}.="\n".DumpEttList()."\n";
664 if (defined($x->{UUID})) {
665 # These can be changed to non-pidl_code names if the old dissectors
666 # in epan/dissctors are deleted.
668 my $name = uc($x->{NAME}) . " (pidl)";
669 my $short_name = uc($x->{NAME});
670 my $filter_name = $x->{NAME};
672 if (has_property($x, "helpstring")) {
673 $name = $x->{PROPERTIES}->{helpstring};
676 if (defined($conformance->{protocols}->{$x->{NAME}})) {
677 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
678 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
679 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
682 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
684 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
685 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
686 } else {
687 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
688 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
689 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
692 deindent;
693 pidl_code "}\n";
694 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
697 sub RegisterInterfaceHandoff($)
699 my $x = shift;
701 if (defined($x->{UUID})) {
702 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
703 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
704 pidl_code "{";
705 indent;
706 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
707 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
708 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
709 deindent;
710 pidl_code "}";
711 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
713 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
717 sub ProcessInterface($)
719 my ($x) = @_;
721 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
723 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
724 pidl_hdr "#ifndef $define";
725 pidl_hdr "#define $define";
726 pidl_hdr "";
728 if (defined $x->{PROPERTIES}->{depends}) {
729 foreach (split / /, $x->{PROPERTIES}->{depends}) {
730 next if($_ eq "security");
731 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
735 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
736 register_ett("ett_dcerpc_$x->{NAME}");
737 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
739 if (defined($x->{UUID})) {
740 my $if_uuid = $x->{UUID};
742 pidl_def "/* Version information */\n\n";
744 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
745 pidl_def "\t0x" . substr($if_uuid, 1, 8)
746 . ", 0x" . substr($if_uuid, 10, 4)
747 . ", 0x" . substr($if_uuid, 15, 4) . ",";
748 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
749 . ", 0x" . substr($if_uuid, 22, 2)
750 . ", 0x" . substr($if_uuid, 25, 2)
751 . ", 0x" . substr($if_uuid, 27, 2)
752 . ", 0x" . substr($if_uuid, 29, 2)
753 . ", 0x" . substr($if_uuid, 31, 2)
754 . ", 0x" . substr($if_uuid, 33, 2)
755 . ", 0x" . substr($if_uuid, 35, 2) . " }";
756 pidl_def "};";
758 my $maj = $x->{VERSION};
759 $maj =~ s/\.(.*)$//g;
760 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
761 pidl_def "";
764 $return_types{$x->{NAME}} = {};
766 Interface($x);
768 pidl_code "\n".DumpFunctionTable($x);
770 foreach (keys %{$return_types{$x->{NAME}}}) {
771 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
772 my $dt = find_type($type);
773 $dt or die("Unable to find information about return type `$type'");
774 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
775 $hf_used{"hf_$x->{NAME}_$_"} = 1;
778 RegisterInterface($x);
779 RegisterInterfaceHandoff($x);
781 pidl_hdr "#endif /* $define */";
784 sub find_type($)
786 my $n = shift;
788 return $conformance->{types}->{$n};
791 sub register_type($$$$$$$)
793 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
795 $conformance->{types}->{$type} = {
796 NAME => $type,
797 DISSECTOR_NAME => $call,
798 FT_TYPE => $ft,
799 BASE_TYPE => $base,
800 MASK => $mask,
801 VALSSTRING => $vals,
802 ALIGNMENT => $length
806 # Loads the default types
807 sub Initialize($)
809 my $cnf_file = shift;
811 $conformance = {
812 imports => {},
813 header_fields=> {}
816 ReadConformance($cnf_file, $conformance) or print "Warning: No conformance file `$cnf_file'\n";
818 foreach my $bytes (qw(1 2 4 8)) {
819 my $bits = $bytes * 8;
820 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
821 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
824 register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
825 register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
826 register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
827 register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
828 register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
829 register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
830 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);
831 register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
832 register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
833 register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
834 register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
835 register_type("SID", "
836 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
838 di->hf_index = \@HF\@;
840 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
841 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
842 register_type("WERROR",
843 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
844 register_type("NTSTATUS",
845 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
849 #####################################################################
850 # Generate Wireshark parser and header code
851 sub Parse($$$$)
853 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
854 Initialize($cnf_file);
856 return (undef, undef) if defined($conformance->{noemit_dissector});
858 $tabs = "";
860 %res = (code=>"",def=>"",hdr=>"");
861 @ett = ();
863 my $notice =
864 "/* DO NOT EDIT
865 This filter was automatically generated
866 from $idl_file and $cnf_file.
868 Pidl is a perl based IDL compiler for DCE/RPC idl files.
869 It is maintained by the Samba team, not the Wireshark team.
870 Instructions on how to download and install Pidl can be
871 found at http://wiki.wireshark.org/Pidl
876 pidl_hdr $notice;
878 $res{headers} = "\n";
879 $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
880 $res{headers} .= "#include \"config.h\"\n";
881 $res{headers} .= "#endif\n\n";
882 $res{headers} .= "#include <glib.h>\n";
883 $res{headers} .= "#include <string.h>\n";
884 $res{headers} .= "#include <epan/packet.h>\n\n";
886 $res{headers} .= "#include \"packet-dcerpc.h\"\n";
887 $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
888 $res{headers} .= "#include \"packet-windows-common.h\"\n";
890 my $h_basename = basename($h_filename);
892 $res{headers} .= "#include \"$h_basename\"\n";
893 pidl_code "";
895 # Wireshark protocol registration
897 ProcessInterface($_) foreach (@$ndr);
899 $res{ett} = DumpEttDeclaration();
900 $res{hf} = DumpHfDeclaration();
902 my $parser = $notice;
903 $parser.= $res{headers};
904 $parser.=$res{ett};
905 $parser.=$res{hf};
906 $parser.=$res{def};
907 $parser.=$conformance->{override};
908 $parser.=$res{code};
910 my $header = "/* autogenerated by pidl */\n\n";
911 $header.=$res{hdr};
913 CheckUsed($conformance);
915 return ($parser,$header);
918 ###############################################################################
919 # ETT
920 ###############################################################################
922 sub register_ett($)
924 my $name = shift;
926 push (@ett, $name);
929 sub DumpEttList()
931 my $res = "\tstatic gint *ett[] = {\n";
932 foreach (@ett) {
933 $res .= "\t\t&$_,\n";
936 return "$res\t};\n";
939 sub DumpEttDeclaration()
941 my $res = "\n/* Ett declarations */\n";
942 foreach (@ett) {
943 $res .= "static gint $_ = -1;\n";
946 return "$res\n";
949 ###############################################################################
950 # HF
951 ###############################################################################
953 sub register_hf_field($$$$$$$$)
955 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
957 if (defined ($conformance->{hf_renames}->{$index})) {
958 $conformance->{hf_renames}->{$index}->{USED} = 1;
959 return $conformance->{hf_renames}->{$index}->{NEWNAME};
962 $conformance->{header_fields}->{$index} = {
963 INDEX => $index,
964 NAME => $name,
965 FILTER => $filter_name,
966 FT_TYPE => $ft_type,
967 BASE_TYPE => $base_type,
968 VALSSTRING => $valsstring,
969 MASK => $mask,
970 BLURB => $blurb
973 if ((not defined($blurb) or $blurb eq "") and
974 defined($conformance->{fielddescription}->{$index})) {
975 $conformance->{header_fields}->{$index}->{BLURB} =
976 $conformance->{fielddescription}->{$index}->{DESCRIPTION};
977 $conformance->{fielddescription}->{$index}->{USED} = 1;
980 return $index;
983 sub DumpHfDeclaration()
985 my $res = "";
987 $res = "\n/* Header field declarations */\n";
989 foreach (keys %{$conformance->{header_fields}})
991 $res .= "static gint $_ = -1;\n";
994 return "$res\n";
997 sub DumpHfList()
999 my $res = "\tstatic hf_register_info hf[] = {\n";
1001 foreach (values %{$conformance->{header_fields}})
1003 $res .= "\t{ &$_->{INDEX},
1004 { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
1008 return $res."\t};\n";
1012 ###############################################################################
1013 # Function table
1014 ###############################################################################
1016 sub DumpFunctionTable($)
1018 my $if = shift;
1020 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1021 foreach (@{$if->{FUNCTIONS}}) {
1022 my $fn_name = $_->{NAME};
1023 $fn_name =~ s/^$if->{NAME}_//;
1024 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1025 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1028 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1030 return "$res};\n";
1033 sub CheckUsed($)
1035 my $conformance = shift;
1036 foreach (values %{$conformance->{header_fields}}) {
1037 if (not defined($hf_used{$_->{INDEX}})) {
1038 print "$_->{POS}: warning: hf field `$_->{INDEX}' not used\n";
1042 foreach (values %{$conformance->{hf_renames}}) {
1043 if (not $_->{USED}) {
1044 print "$_->{POS}: warning: hf field `$_->{OLDNAME}' not used\n";
1048 foreach (values %{$conformance->{dissectorparams}}) {
1049 if (not $_->{USED}) {
1050 print "$_->{POS}: warning: dissector param never used\n";
1054 foreach (values %{$conformance->{imports}}) {
1055 if (not $_->{USED}) {
1056 print "$_->{POS}: warning: import never used\n";
1060 foreach (values %{$conformance->{types}}) {
1061 if (not $_->{USED} and defined($_->{POS})) {
1062 print "$_->{POS}: warning: type never used\n";
1066 foreach (values %{$conformance->{fielddescription}}) {
1067 if (not $_->{USED}) {
1068 print "$_->{POS}: warning: description never used\n";
1072 foreach (values %{$conformance->{tfs}}) {
1073 if (not $_->{USED}) {
1074 print "$_->{POS}: warning: True/False description never used\n";