r19560: Fix handling of PARAM_VALUE.
[Samba/ekacnet.git] / source / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
blobf72efc753436c900c5eb82a093ec61cd997ec229
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,$ifname) = @_;
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 pidl_code "offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);";
332 return;
335 $call =~ s/\@HF\@/$hf/g;
336 $call =~ s/\@PARAM\@/$param/g;
337 pidl_code "$call";
339 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
340 my $num_bits = ($l->{HEADER_SIZE}*8);
341 pidl_code "guint$num_bits size;";
342 pidl_code "int start_offset = offset;";
343 pidl_code "tvbuff_t *subtvb;";
344 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
345 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
347 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
348 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
349 } else {
350 die("Unknown type `$_->{TYPE}'");
354 sub Element($$$)
356 my ($e,$pn,$ifname) = @_;
358 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
360 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
362 my $type = find_type($e->{TYPE});
364 if (not defined($type)) {
365 # default settings
366 $type = {
367 MASK => 0,
368 VALSSTRING => "NULL",
369 FT_TYPE => "FT_NONE",
370 BASE_TYPE => "BASE_HEX"
374 if (ContainsString($e)) {
375 $type = {
376 MASK => 0,
377 VALSSTRING => "NULL",
378 FT_TYPE => "FT_STRING",
379 BASE_TYPE => "BASE_DEC"
383 my $hf = register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, "");
384 $hf_used{$hf} = 1;
386 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
387 if (defined($conformance->{noemit}->{$eltname})) {
388 return $call_code;
391 my $add = "";
393 foreach (@{$e->{LEVELS}}) {
394 next if ($_->{TYPE} eq "SWITCH");
395 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
396 pidl_fn_start "$dissectorname$add";
397 pidl_code "static int";
398 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
399 pidl_code "{";
400 indent;
402 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
404 pidl_code "";
405 pidl_code "return offset;";
406 deindent;
407 pidl_code "}\n";
408 pidl_fn_end "$dissectorname$add";
409 $add.="_";
410 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
413 return $call_code;
416 sub Function($$$)
418 my ($fn,$ifname) = @_;
420 my %dissectornames;
422 foreach (@{$fn->{ELEMENTS}}) {
423 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
426 my $fn_name = $_->{NAME};
427 $fn_name =~ s/^${ifname}_//;
429 print "$fn_name\n";
431 PrintIdl DumpFunction($fn->{ORIGINAL});
432 pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
433 pidl_code "static int";
434 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_)";
435 pidl_code "{";
436 indent;
437 if ( not defined($fn->{RETURN_TYPE})) {
438 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
440 pidl_code "guint32 status;\n";
441 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
442 if ($type->{DATA}->{TYPE} eq "ENUM") {
443 pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
444 } else {
445 print "$fn->{FILE}:$fn->{LINE}: error: return type `$fn->{RETURN_TYPE}' not yet supported\n";
447 } else {
448 print "$fn->{FILE}:$fn->{LINE}: error: unknown return type `$fn->{RETURN_TYPE}'\n";
451 foreach (@{$fn->{ELEMENTS}}) {
452 if (grep(/out/,@{$_->{DIRECTION}})) {
453 pidl_code "$dissectornames{$_->{NAME}}";
454 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
455 pidl_code "";
459 if (not defined($fn->{RETURN_TYPE})) {
460 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
461 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
462 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
463 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
464 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
465 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
466 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
467 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
468 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
470 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
471 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
472 if ($type->{DATA}->{TYPE} eq "ENUM") {
473 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
474 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
476 pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
477 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
478 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";
479 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
484 pidl_code "return offset;";
485 deindent;
486 pidl_code "}\n";
487 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
489 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
490 pidl_code "static int";
491 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_)";
492 pidl_code "{";
493 indent;
494 foreach (@{$fn->{ELEMENTS}}) {
495 if (grep(/in/,@{$_->{DIRECTION}})) {
496 pidl_code "$dissectornames{$_->{NAME}}";
497 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
502 pidl_code "return offset;";
503 deindent;
504 pidl_code "}\n";
505 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
508 sub Struct($$$)
510 my ($e,$name,$ifname) = @_;
511 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
513 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
515 register_ett("ett_$ifname\_$name");
517 my $res = "";
518 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
520 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
522 pidl_fn_start $dissectorname;
523 pidl_code "int";
524 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
525 pidl_code "{";
526 indent;
527 pidl_code "proto_item *item = NULL;";
528 pidl_code "proto_tree *tree = NULL;";
529 pidl_code "int old_offset;";
530 pidl_code "";
532 if ($e->{ALIGN} > 1) {
533 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
535 pidl_code "";
537 pidl_code "old_offset = offset;";
538 pidl_code "";
539 pidl_code "if (parent_tree) {";
540 indent;
541 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
542 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
543 deindent;
544 pidl_code "}";
546 pidl_code "\n$res";
548 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
549 pidl_code "return offset;";
550 deindent;
551 pidl_code "}\n";
552 pidl_fn_end $dissectorname;
554 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
557 sub Union($$$)
559 my ($e,$name,$ifname) = @_;
561 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
563 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
565 register_ett("ett_$ifname\_$name");
567 my $res = "";
568 foreach (@{$e->{ELEMENTS}}) {
569 $res.="\n\t\t$_->{CASE}:\n";
570 if ($_->{TYPE} ne "EMPTY") {
571 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
573 $res.="\t\tbreak;\n";
576 my $switch_type;
577 my $switch_dissect;
578 my $switch_dt = getType($e->{SWITCH_TYPE});
579 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
580 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
581 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
582 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
583 $switch_type = "g$e->{SWITCH_TYPE}";
584 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
587 pidl_fn_start $dissectorname;
588 pidl_code "static int";
589 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
590 pidl_code "{";
591 indent;
592 pidl_code "proto_item *item = NULL;";
593 pidl_code "proto_tree *tree = NULL;";
594 pidl_code "int old_offset;";
595 pidl_code "$switch_type level;";
596 pidl_code "";
598 if ($e->{ALIGN} > 1) {
599 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
602 pidl_code "";
604 pidl_code "old_offset = offset;";
605 pidl_code "if (parent_tree) {";
606 indent;
607 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
608 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
609 deindent;
610 pidl_code "}";
612 pidl_code "";
614 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
616 pidl_code "switch(level) {$res\t}";
617 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
618 pidl_code "return offset;";
619 deindent;
620 pidl_code "}";
621 pidl_fn_end $dissectorname;
623 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
626 sub Const($$)
628 my ($const,$ifname) = @_;
630 if (!defined($const->{ARRAY_LEN}[0])) {
631 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
632 } else {
633 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
637 sub Typedef($$)
639 my ($e,$ifname) = @_;
641 PrintIdl DumpTypedef($e->{ORIGINAL});
644 ENUM => \&Enum,
645 STRUCT => \&Struct,
646 UNION => \&Union,
647 BITMAP => \&Bitmap
648 }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
651 sub RegisterInterface($)
653 my ($x) = @_;
655 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
656 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
657 pidl_code "{";
658 indent;
660 $res{code}.=DumpHfList()."\n";
661 $res{code}.="\n".DumpEttList()."\n";
663 if (defined($x->{UUID})) {
664 # These can be changed to non-pidl_code names if the old dissectors
665 # in epan/dissctors are deleted.
667 my $name = uc($x->{NAME}) . " (pidl)";
668 my $short_name = uc($x->{NAME});
669 my $filter_name = $x->{NAME};
671 if (has_property($x, "helpstring")) {
672 $name = $x->{PROPERTIES}->{helpstring};
675 if (defined($conformance->{protocols}->{$x->{NAME}})) {
676 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
677 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
678 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
681 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
683 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
684 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
685 } else {
686 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
687 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
688 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
691 deindent;
692 pidl_code "}\n";
693 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
696 sub RegisterInterfaceHandoff($)
698 my $x = shift;
700 if (defined($x->{UUID})) {
701 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
702 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
703 pidl_code "{";
704 indent;
705 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
706 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
707 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
708 deindent;
709 pidl_code "}";
710 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
712 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
716 sub ProcessInterface($)
718 my ($x) = @_;
720 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
722 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
723 pidl_hdr "#ifndef $define";
724 pidl_hdr "#define $define";
725 pidl_hdr "";
727 if (defined $x->{PROPERTIES}->{depends}) {
728 foreach (split / /, $x->{PROPERTIES}->{depends}) {
729 next if($_ eq "security");
730 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
734 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
735 register_ett("ett_dcerpc_$x->{NAME}");
736 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
738 if (defined($x->{UUID})) {
739 my $if_uuid = $x->{UUID};
741 pidl_def "/* Version information */\n\n";
743 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
744 pidl_def "\t0x" . substr($if_uuid, 1, 8)
745 . ", 0x" . substr($if_uuid, 10, 4)
746 . ", 0x" . substr($if_uuid, 15, 4) . ",";
747 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
748 . ", 0x" . substr($if_uuid, 22, 2)
749 . ", 0x" . substr($if_uuid, 25, 2)
750 . ", 0x" . substr($if_uuid, 27, 2)
751 . ", 0x" . substr($if_uuid, 29, 2)
752 . ", 0x" . substr($if_uuid, 31, 2)
753 . ", 0x" . substr($if_uuid, 33, 2)
754 . ", 0x" . substr($if_uuid, 35, 2) . " }";
755 pidl_def "};";
757 my $maj = $x->{VERSION};
758 $maj =~ s/\.(.*)$//g;
759 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
760 pidl_def "";
763 $return_types{$x->{NAME}} = {};
765 Interface($x);
767 pidl_code "\n".DumpFunctionTable($x);
769 foreach (keys %{$return_types{$x->{NAME}}}) {
770 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
771 my $dt = find_type($type);
772 $dt or die("Unable to find information about return type `$type'");
773 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
774 $hf_used{"hf_$x->{NAME}_$_"} = 1;
777 RegisterInterface($x);
778 RegisterInterfaceHandoff($x);
780 pidl_hdr "#endif /* $define */";
783 sub find_type($)
785 my $n = shift;
787 return $conformance->{types}->{$n};
790 sub register_type($$$$$$$)
792 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
794 $conformance->{types}->{$type} = {
795 NAME => $type,
796 DISSECTOR_NAME => $call,
797 FT_TYPE => $ft,
798 BASE_TYPE => $base,
799 MASK => $mask,
800 VALSSTRING => $vals,
801 ALIGNMENT => $length
805 # Loads the default types
806 sub Initialize($)
808 my $cnf_file = shift;
810 $conformance = {
811 imports => {},
812 header_fields=> {}
815 ReadConformance($cnf_file, $conformance) or print "Warning: No conformance file `$cnf_file'\n";
817 foreach my $bytes (qw(1 2 4 8)) {
818 my $bits = $bytes * 8;
819 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
820 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
823 register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
824 register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
825 register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
826 register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
827 register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
828 register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
829 register_type("policy_handle", "offset = dissect_nt_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, NULL, NULL, \@PARAM\@&0x01, \@PARAM\@&0x02);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
830 register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
831 register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
832 register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
833 register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
834 register_type("SID", "
835 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
837 di->hf_index = \@HF\@;
839 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
840 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
841 register_type("WERROR",
842 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
843 register_type("NTSTATUS",
844 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
848 #####################################################################
849 # Generate Wireshark parser and header code
850 sub Parse($$$$)
852 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
853 Initialize($cnf_file);
855 return (undef, undef) if defined($conformance->{noemit_dissector});
857 $tabs = "";
859 %res = (code=>"",def=>"",hdr=>"");
860 @ett = ();
862 my $notice =
863 "/* DO NOT EDIT
864 This filter was automatically generated
865 from $idl_file and $cnf_file.
867 Pidl is a perl based IDL compiler for DCE/RPC idl files.
868 It is maintained by the Samba team, not the Wireshark team.
869 Instructions on how to download and install Pidl can be
870 found at http://wiki.wireshark.org/Pidl
875 pidl_hdr $notice;
877 $res{headers} = "\n";
878 $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
879 $res{headers} .= "#include \"config.h\"\n";
880 $res{headers} .= "#endif\n\n";
881 $res{headers} .= "#include <glib.h>\n";
882 $res{headers} .= "#include <string.h>\n";
883 $res{headers} .= "#include <epan/packet.h>\n\n";
885 $res{headers} .= "#include \"packet-dcerpc.h\"\n";
886 $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
887 $res{headers} .= "#include \"packet-windows-common.h\"\n";
889 my $h_basename = basename($h_filename);
891 $res{headers} .= "#include \"$h_basename\"\n";
892 pidl_code "";
894 # Wireshark protocol registration
896 ProcessInterface($_) foreach (@$ndr);
898 $res{ett} = DumpEttDeclaration();
899 $res{hf} = DumpHfDeclaration();
901 my $parser = $notice;
902 $parser.= $res{headers};
903 $parser.=$res{ett};
904 $parser.=$res{hf};
905 $parser.=$res{def};
906 $parser.=$conformance->{override};
907 $parser.=$res{code};
909 my $header = "/* autogenerated by pidl */\n\n";
910 $header.=$res{hdr};
912 CheckUsed($conformance);
914 return ($parser,$header);
917 ###############################################################################
918 # ETT
919 ###############################################################################
921 sub register_ett($)
923 my $name = shift;
925 push (@ett, $name);
928 sub DumpEttList()
930 my $res = "\tstatic gint *ett[] = {\n";
931 foreach (@ett) {
932 $res .= "\t\t&$_,\n";
935 return "$res\t};\n";
938 sub DumpEttDeclaration()
940 my $res = "\n/* Ett declarations */\n";
941 foreach (@ett) {
942 $res .= "static gint $_ = -1;\n";
945 return "$res\n";
948 ###############################################################################
949 # HF
950 ###############################################################################
952 sub register_hf_field($$$$$$$$)
954 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
956 if (defined ($conformance->{hf_renames}->{$index})) {
957 $conformance->{hf_renames}->{$index}->{USED} = 1;
958 return $conformance->{hf_renames}->{$index}->{NEWNAME};
961 $conformance->{header_fields}->{$index} = {
962 INDEX => $index,
963 NAME => $name,
964 FILTER => $filter_name,
965 FT_TYPE => $ft_type,
966 BASE_TYPE => $base_type,
967 VALSSTRING => $valsstring,
968 MASK => $mask,
969 BLURB => $blurb
972 if ((not defined($blurb) or $blurb eq "") and
973 defined($conformance->{fielddescription}->{$index})) {
974 $conformance->{header_fields}->{$index}->{BLURB} =
975 $conformance->{fielddescription}->{$index}->{DESCRIPTION};
976 $conformance->{fielddescription}->{$index}->{USED} = 1;
979 return $index;
982 sub DumpHfDeclaration()
984 my $res = "";
986 $res = "\n/* Header field declarations */\n";
988 foreach (keys %{$conformance->{header_fields}})
990 $res .= "static gint $_ = -1;\n";
993 return "$res\n";
996 sub DumpHfList()
998 my $res = "\tstatic hf_register_info hf[] = {\n";
1000 foreach (values %{$conformance->{header_fields}})
1002 $res .= "\t{ &$_->{INDEX},
1003 { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
1007 return $res."\t};\n";
1011 ###############################################################################
1012 # Function table
1013 ###############################################################################
1015 sub DumpFunctionTable($)
1017 my $if = shift;
1019 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1020 foreach (@{$if->{FUNCTIONS}}) {
1021 my $fn_name = $_->{NAME};
1022 $fn_name =~ s/^$if->{NAME}_//;
1023 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1024 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1027 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1029 return "$res};\n";
1032 sub CheckUsed($)
1034 my $conformance = shift;
1035 foreach (values %{$conformance->{header_fields}}) {
1036 if (not defined($hf_used{$_->{INDEX}})) {
1037 print "$_->{POS}: warning: hf field `$_->{INDEX}' not used\n";
1041 foreach (values %{$conformance->{hf_renames}}) {
1042 if (not $_->{USED}) {
1043 print "$_->{POS}: warning: hf field `$_->{OLDNAME}' not used\n";
1047 foreach (values %{$conformance->{dissectorparams}}) {
1048 if (not $_->{USED}) {
1049 print "$_->{POS}: warning: dissector param never used\n";
1053 foreach (values %{$conformance->{imports}}) {
1054 if (not $_->{USED}) {
1055 print "$_->{POS}: warning: import never used\n";
1059 foreach (values %{$conformance->{types}}) {
1060 if (not $_->{USED} and defined($_->{POS})) {
1061 print "$_->{POS}: warning: type never used\n";
1065 foreach (values %{$conformance->{fielddescription}}) {
1066 if (not $_->{USED}) {
1067 print "$_->{POS}: warning: description never used\n";
1071 foreach (values %{$conformance->{tfs}}) {
1072 if (not $_->{USED}) {
1073 print "$_->{POS}: warning: True/False description never used\n";