r16824: Rename Ethereal -> Wireshark (patch from Joerg Mayer)
[Samba/aatanasov.git] / source / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
blob872a1492746700d6d3775541cb8985b79aa718b4
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 %dissector_used = ();
41 my $conformance = undef;
43 my %ptrtype_mappings = (
44 "unique" => "NDR_POINTER_UNIQUE",
45 "ref" => "NDR_POINTER_REF",
46 "ptr" => "NDR_POINTER_PTR"
49 sub StripPrefixes($)
51 my ($s) = @_;
53 foreach (@{$conformance->{strip_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 my %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 = shift;
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 Typedef($_,$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);
140 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
142 foreach (@{$e->{ELEMENTS}}) {
143 if (/([^=]*)=(.*)/) {
144 pidl_hdr "#define $1 ($2)";
148 pidl_hdr "extern const value_string $valsstring\[];";
149 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
151 pidl_def "const value_string ".$valsstring."[] = {";
152 foreach (@{$e->{ELEMENTS}}) {
153 next unless (/([^=]*)=(.*)/);
154 pidl_def "\t{ $1, \"$1\" },";
157 pidl_def "{ 0, NULL }";
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 _U_)";
163 pidl_code "{";
164 indent;
165 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, NULL);";
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);
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) = @_;
262 my $param = 0;
264 if (defined($conformance->{dissectorparams}->{$myname})) {
265 $conformance->{dissectorparams}->{$myname}->{PARAM} = 1;
266 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
269 if ($l->{TYPE} eq "POINTER") {
270 my $type;
271 if ($l->{LEVEL} eq "TOP") {
272 $type = "toplevel";
273 } elsif ($l->{LEVEL} eq "EMBEDDED") {
274 $type = "embedded";
276 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);";
277 } elsif ($l->{TYPE} eq "ARRAY") {
278 if ($l->{IS_INLINE}) {
279 error($e->{ORIGINAL}, "Inline arrays not supported");
280 } elsif ($l->{IS_FIXED}) {
281 pidl_code "int i;";
282 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
283 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
284 } else {
285 my $type = "";
286 $type .= "c" if ($l->{IS_CONFORMANT});
287 $type .= "v" if ($l->{IS_VARYING});
289 unless ($l->{IS_ZERO_TERMINATED}) {
290 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
291 } else {
292 my $nl = GetNextLevel($e,$l);
293 pidl_code "char *data;";
294 pidl_code "";
295 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
296 pidl_code "proto_item_append_text(tree, \": %s\", data);";
299 } elsif ($l->{TYPE} eq "DATA") {
300 if ($l->{DATA_TYPE} eq "string") {
301 my $bs = 2; # Byte size defaults to that of UCS2
304 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
306 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
307 pidl_code "char *data;\n";
308 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
309 pidl_code "proto_item_append_text(tree, \": %s\", data);";
310 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
311 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
312 } else {
313 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
315 } else {
316 my $call;
318 if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
319 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};
320 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
321 } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
322 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
323 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
325 } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
326 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
327 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
328 } else {
329 if ($l->{DATA_TYPE} =~ /^([a-z]+)\_(.*)$/)
331 pidl_code "offset = $1_dissect_struct_$2(tvb,offset,pinfo,tree,drep,$hf,$param);";
334 return;
337 $call =~ s/\@HF\@/$hf/g;
338 $call =~ s/\@PARAM\@/$param/g;
339 pidl_code "$call";
341 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
342 my $num_bits = ($l->{HEADER_SIZE}*8);
343 pidl_code "guint$num_bits size;";
344 pidl_code "int start_offset = offset;";
345 pidl_code "tvbuff_t *subtvb;";
346 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
347 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
349 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
350 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
351 } else {
352 die("Unknown type `$_->{TYPE}'");
356 sub Element($$$)
358 my ($e,$pn,$ifname) = @_;
360 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
362 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
364 my $type = find_type($e->{TYPE});
366 if (not defined($type)) {
367 # default settings
368 $type = {
369 MASK => 0,
370 VALSSTRING => "NULL",
371 FT_TYPE => "FT_NONE",
372 BASE_TYPE => "BASE_HEX"
376 if (ContainsString($e)) {
377 $type = {
378 MASK => 0,
379 VALSSTRING => "NULL",
380 FT_TYPE => "FT_STRING",
381 BASE_TYPE => "BASE_DEC"
385 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}, "");
386 $hf_used{$hf} = 1;
388 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
389 if (defined($conformance->{noemit}->{$eltname})) {
390 return $call_code;
393 my $add = "";
395 foreach (@{$e->{LEVELS}}) {
396 next if ($_->{TYPE} eq "SWITCH");
397 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
398 pidl_fn_start "$dissectorname$add";
399 pidl_code "static int";
400 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
401 pidl_code "{";
402 indent;
404 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn);
406 pidl_code "";
407 pidl_code "return offset;";
408 deindent;
409 pidl_code "}\n";
410 pidl_fn_end "$dissectorname$add";
411 $add.="_";
412 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
415 return $call_code;
418 sub Function($$$)
420 my ($fn,$ifname) = @_;
422 my %dissectornames;
424 foreach (@{$fn->{ELEMENTS}}) {
425 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
428 my $fn_name = $_->{NAME};
429 $fn_name =~ s/^${ifname}_//;
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 pidl_code "guint32 status;\n";
438 foreach (@{$fn->{ELEMENTS}}) {
439 if (grep(/out/,@{$_->{DIRECTION}})) {
440 pidl_code "$dissectornames{$_->{NAME}}";
441 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
442 pidl_code "";
446 if (not defined($fn->{RETURN_TYPE})) {
447 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
448 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
449 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
450 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
451 $hf_used{"hf\_$ifname\_status"} = 1;
452 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
453 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
454 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
455 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, DOS_errors, \"Unknown DOS error 0x%08x\"));\n";
457 $hf_used{"hf\_$ifname\_werror"} = 1;
458 } else {
459 print "$fn->{FILE}:$fn->{LINE}: error: return type `$fn->{RETURN_TYPE}' not yet supported\n";
463 pidl_code "return offset;";
464 deindent;
465 pidl_code "}\n";
466 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
468 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
469 pidl_code "static int";
470 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_)";
471 pidl_code "{";
472 indent;
473 foreach (@{$fn->{ELEMENTS}}) {
474 if (grep(/in/,@{$_->{DIRECTION}})) {
475 pidl_code "$dissectornames{$_->{NAME}}";
476 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
481 pidl_code "return offset;";
482 deindent;
483 pidl_code "}\n";
484 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
487 sub Struct($$$)
489 my ($e,$name,$ifname) = @_;
490 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
492 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
494 register_ett("ett_$ifname\_$name");
496 my $res = "";
497 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
499 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
501 pidl_fn_start $dissectorname;
502 pidl_code "int";
503 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
504 pidl_code "{";
505 indent;
506 pidl_code "proto_item *item = NULL;";
507 pidl_code "proto_tree *tree = NULL;";
508 pidl_code "int old_offset;";
509 pidl_code "";
511 if ($e->{ALIGN} > 1) {
512 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
514 pidl_code "";
516 pidl_code "old_offset = offset;";
517 pidl_code "";
518 pidl_code "if (parent_tree) {";
519 indent;
520 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
521 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
522 deindent;
523 pidl_code "}";
525 pidl_code "\n$res";
527 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
528 pidl_code "return offset;";
529 deindent;
530 pidl_code "}\n";
531 pidl_fn_end $dissectorname;
533 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
536 sub Union($$$)
538 my ($e,$name,$ifname) = @_;
540 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
542 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
544 register_ett("ett_$ifname\_$name");
546 my $res = "";
547 foreach (@{$e->{ELEMENTS}}) {
548 $res.="\n\t\t$_->{CASE}:\n";
549 if ($_->{TYPE} ne "EMPTY") {
550 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
552 $res.="\t\tbreak;\n";
555 my $switch_type;
556 my $switch_dissect;
557 my $switch_dt = getType($e->{SWITCH_TYPE});
558 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
559 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt);
560 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt);
561 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
562 $switch_type = "g$e->{SWITCH_TYPE}";
563 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
566 pidl_fn_start $dissectorname;
567 pidl_code "static int";
568 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
569 pidl_code "{";
570 indent;
571 pidl_code "proto_item *item = NULL;";
572 pidl_code "proto_tree *tree = NULL;";
573 pidl_code "int old_offset;";
574 pidl_code "$switch_type level;";
575 pidl_code "";
577 if ($e->{ALIGN} > 1) {
578 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
581 pidl_code "";
583 pidl_code "old_offset = offset;";
584 pidl_code "if (parent_tree) {";
585 indent;
586 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
587 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
588 deindent;
589 pidl_code "}";
591 pidl_code "";
593 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
595 pidl_code "switch(level) {$res\t}";
596 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
597 pidl_code "return offset;";
598 deindent;
599 pidl_code "}";
600 pidl_fn_end $dissectorname;
602 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
605 sub Const($$)
607 my ($const,$ifname) = @_;
609 if (!defined($const->{ARRAY_LEN}[0])) {
610 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
611 } else {
612 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
616 sub Typedef($$)
618 my ($e,$ifname) = @_;
620 PrintIdl DumpTypedef($e->{ORIGINAL});
623 ENUM => \&Enum,
624 STRUCT => \&Struct,
625 UNION => \&Union,
626 BITMAP => \&Bitmap
627 }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
630 sub RegisterInterface($)
632 my ($x) = @_;
634 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
635 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
636 pidl_code "{";
637 indent;
639 $res{code}.=DumpHfList()."\n";
640 $res{code}.="\n".DumpEttList()."\n";
642 if (defined($x->{UUID})) {
643 # These can be changed to non-pidl_code names if the old dissectors
644 # in epan/dissctors are deleted.
646 my $name = uc($x->{NAME}) . " (pidl)";
647 my $short_name = uc($x->{NAME});
648 my $filter_name = $x->{NAME};
650 if (has_property($x, "helpstring")) {
651 $name = $x->{PROPERTIES}->{helpstring};
654 if (defined($conformance->{protocols}->{$x->{NAME}})) {
655 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
656 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
657 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
660 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
662 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
663 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
664 } else {
665 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
666 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
667 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
670 deindent;
671 pidl_code "}\n";
672 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
675 sub RegisterInterfaceHandoff($)
677 my $x = shift;
679 if (defined($x->{UUID})) {
680 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
681 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
682 pidl_code "{";
683 indent;
684 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
685 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
686 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
687 deindent;
688 pidl_code "}";
689 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
691 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
695 sub ProcessInterface($)
697 my ($x) = @_;
699 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
701 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
702 pidl_hdr "#ifndef $define";
703 pidl_hdr "#define $define";
704 pidl_hdr "";
706 if (defined $x->{PROPERTIES}->{depends}) {
707 foreach (split / /, $x->{PROPERTIES}->{depends}) {
708 next if($_ eq "security");
709 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
713 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
714 register_ett("ett_dcerpc_$x->{NAME}");
715 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
717 if (defined($x->{UUID})) {
718 my $if_uuid = $x->{UUID};
720 pidl_def "/* Version information */\n\n";
722 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
723 pidl_def "\t0x" . substr($if_uuid, 1, 8)
724 . ", 0x" . substr($if_uuid, 10, 4)
725 . ", 0x" . substr($if_uuid, 15, 4) . ",";
726 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
727 . ", 0x" . substr($if_uuid, 22, 2)
728 . ", 0x" . substr($if_uuid, 25, 2)
729 . ", 0x" . substr($if_uuid, 27, 2)
730 . ", 0x" . substr($if_uuid, 29, 2)
731 . ", 0x" . substr($if_uuid, 31, 2)
732 . ", 0x" . substr($if_uuid, 33, 2)
733 . ", 0x" . substr($if_uuid, 35, 2) . " }";
734 pidl_def "};";
736 my $maj = $x->{VERSION};
737 $maj =~ s/\.(.*)$//g;
738 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
739 pidl_def "";
742 Interface($x);
744 pidl_code "\n".DumpFunctionTable($x);
746 # Only register these two return types if they were actually used
747 if (defined($hf_used{"hf_$x->{NAME}_status"})) {
748 register_hf_field("hf_$x->{NAME}_status", "Status", "$x->{NAME}.status", "FT_UINT32", "BASE_HEX", "VALS(NT_errors)", 0, "");
751 if (defined($hf_used{"hf_$x->{NAME}_werror"})) {
752 register_hf_field("hf_$x->{NAME}_werror", "Windows Error", "$x->{NAME}.werror", "FT_UINT32", "BASE_HEX", "VALS(DOS_errors)", 0, "");
755 RegisterInterface($x);
756 RegisterInterfaceHandoff($x);
758 pidl_hdr "#endif /* $define */";
761 sub find_type($)
763 my $n = shift;
765 return $conformance->{types}->{$n};
768 sub register_type($$$$$$$)
770 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
772 $conformance->{types}->{$type} = {
773 NAME => $type,
774 DISSECTOR_NAME => $call,
775 FT_TYPE => $ft,
776 BASE_TYPE => $base,
777 MASK => $mask,
778 VALSSTRING => $vals,
779 ALIGNMENT => $length
783 # Loads the default types
784 sub Initialize($)
786 my $cnf_file = shift;
788 $conformance = {
789 imports => {},
790 header_fields=> {}
793 ReadConformance($cnf_file, $conformance) or print "Warning: No conformance file `$cnf_file'\n";
795 foreach my $bytes (qw(1 2 4 8)) {
796 my $bits = $bytes * 8;
797 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
798 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
801 register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
802 register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
803 register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
804 register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
805 register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
806 register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
807 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);
808 register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
809 register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
810 register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
811 register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
812 register_type("SID", "
813 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
815 di->hf_index = \@HF\@;
817 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
818 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
819 register_type("WERROR",
820 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
824 #####################################################################
825 # Generate Wireshark parser and header code
826 sub Parse($$$$)
828 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
829 Initialize($cnf_file);
831 return (undef, undef) if defined($conformance->{noemit_dissector});
833 $tabs = "";
835 %res = (code=>"",def=>"",hdr=>"");
836 @ett = ();
838 my $notice =
839 "/* DO NOT EDIT
840 This filter was automatically generated
841 from $idl_file and $cnf_file.
843 Pidl is a perl based IDL compiler for DCE/RPC idl files.
844 It is maintained by the Samba team, not the Wireshark team.
845 Instructions on how to download and install Pidl can be
846 found at http://wiki.wireshark.org/Pidl
851 pidl_hdr $notice;
853 $res{headers} = "\n";
854 $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
855 $res{headers} .= "#include \"config.h\"\n";
856 $res{headers} .= "#endif\n\n";
857 $res{headers} .= "#include <glib.h>\n";
858 $res{headers} .= "#include <string.h>\n";
859 $res{headers} .= "#include <epan/packet.h>\n\n";
861 $res{headers} .= "#include \"packet-dcerpc.h\"\n";
862 $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
863 $res{headers} .= "#include \"packet-windows-common.h\"\n";
865 my $h_basename = basename($h_filename);
867 $res{headers} .= "#include \"$h_basename\"\n";
868 pidl_code "";
870 # Wireshark protocol registration
872 ProcessInterface($_) foreach (@$ndr);
874 $res{ett} = DumpEttDeclaration();
875 $res{hf} = DumpHfDeclaration();
877 my $parser = $notice;
878 $parser.= $res{headers};
879 $parser.=$res{ett};
880 $parser.=$res{hf};
881 $parser.=$res{def};
882 $parser.=$conformance->{override};
883 $parser.=$res{code};
885 my $header = "/* autogenerated by pidl */\n\n";
886 $header.=$res{hdr};
888 CheckUsed($conformance);
890 return ($parser,$header);
893 ###############################################################################
894 # ETT
895 ###############################################################################
897 sub register_ett($)
899 my $name = shift;
901 push (@ett, $name);
904 sub DumpEttList()
906 my $res = "\tstatic gint *ett[] = {\n";
907 foreach (@ett) {
908 $res .= "\t\t&$_,\n";
911 return "$res\t};\n";
914 sub DumpEttDeclaration()
916 my $res = "\n/* Ett declarations */\n";
917 foreach (@ett) {
918 $res .= "static gint $_ = -1;\n";
921 return "$res\n";
924 ###############################################################################
925 # HF
926 ###############################################################################
928 sub register_hf_field($$$$$$$$)
930 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
932 if (defined ($conformance->{hf_renames}->{$index})) {
933 $conformance->{hf_renames}->{$index}->{USED} = 1;
934 return $conformance->{hf_renames}->{$index}->{NEWNAME};
937 $conformance->{header_fields}->{$index} = {
938 INDEX => $index,
939 NAME => $name,
940 FILTER => $filter_name,
941 FT_TYPE => $ft_type,
942 BASE_TYPE => $base_type,
943 VALSSTRING => $valsstring,
944 MASK => $mask,
945 BLURB => $blurb
948 if ((not defined($blurb) or $blurb eq "") and
949 defined($conformance->{fielddescription}->{$index})) {
950 $conformance->{header_fields}->{$index}->{BLURB} =
951 $conformance->{fielddescription}->{$index}->{DESCRIPTION};
952 $conformance->{fielddescription}->{$index}->{USED} = 1;
955 return $index;
958 sub DumpHfDeclaration()
960 my $res = "";
962 $res = "\n/* Header field declarations */\n";
964 foreach (keys %{$conformance->{header_fields}})
966 $res .= "static gint $_ = -1;\n";
969 return "$res\n";
972 sub DumpHfList()
974 my $res = "\tstatic hf_register_info hf[] = {\n";
976 foreach (values %{$conformance->{header_fields}})
978 $res .= "\t{ &$_->{INDEX},
979 { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
983 return $res."\t};\n";
987 ###############################################################################
988 # Function table
989 ###############################################################################
991 sub DumpFunctionTable($)
993 my $if = shift;
995 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
996 foreach (@{$if->{FUNCTIONS}}) {
997 my $fn_name = $_->{NAME};
998 $fn_name =~ s/^$if->{NAME}_//;
999 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1000 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1003 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1005 return "$res};\n";
1008 sub CheckUsed($)
1010 my $conformance = shift;
1011 foreach (values %{$conformance->{header_fields}}) {
1012 if (not defined($hf_used{$_->{INDEX}})) {
1013 print "$_->{POS}: warning: hf field `$_->{INDEX}' not used\n";
1017 foreach (values %{$conformance->{hf_renames}}) {
1018 if (not $_->{USED}) {
1019 print "$_->{POS}: warning: hf field `$_->{OLDNAME}' not used\n";
1023 foreach (values %{$conformance->{dissectorparams}}) {
1024 if (not $_->{USED}) {
1025 print "$_->{POS}: warning: dissector param never used\n";
1029 foreach (values %{$conformance->{imports}}) {
1030 if (not $_->{USED}) {
1031 print "$_->{POS}: warning: import never used\n";
1035 foreach (values %{$conformance->{types}}) {
1036 if (not $_->{USED} and defined($_->{POS})) {
1037 print "$_->{POS}: warning: type never used\n";
1041 foreach (values %{$conformance->{fielddescription}}) {
1042 if (not $_->{USED}) {
1043 print "$_->{POS}: warning: description never used\n";
1047 foreach (values %{$conformance->{tfs}}) {
1048 if (not $_->{USED}) {
1049 print "$_->{POS}: warning: True/False description never used\n";