r21531: Use pidl-specific utility functions. Patch from Ronnie Sahlberg
[Samba/ekacnet.git] / source / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
blob72d353aa14f72ac6058d98a91b175f73e648f232
1 ##################################################
2 # Samba4 NDR parser generator for IDL structures
3 # Copyright tridge@samba.org 2000-2003
4 # Copyright tpot@samba.org 2001,2005
5 # Copyright jelmer@samba.org 2004-2007
6 # Portions based on idl2eth.c by Ronnie Sahlberg
7 # released under the GNU GPL
9 =pod
11 =head1 NAME
13 Parse::Pidl::Wireshark::NDR - Parser generator for Wireshark
15 =cut
17 package Parse::Pidl::Wireshark::NDR;
19 use Exporter;
20 @ISA = qw(Exporter);
21 @EXPORT_OK = qw(field2name @ett %res PrintIdl);
23 use strict;
24 use Parse::Pidl qw(error warning);
25 use Parse::Pidl::Typelist qw(getType);
26 use Parse::Pidl::Util qw(has_property property_matches make_str);
27 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
28 use Parse::Pidl::Dump qw(DumpType DumpFunction);
29 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
30 use File::Basename;
32 use vars qw($VERSION);
33 $VERSION = '0.01';
35 our @ett;
37 my %hf_used = ();
38 my %return_types = ();
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 our %res = ();
74 my $tabs = "";
75 my $cur_fn = undef;
76 sub pidl_fn_start($)
78 my $fn = shift;
79 $cur_fn = $fn;
81 sub pidl_fn_end($)
83 my $fn = shift;
84 die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
85 $cur_fn = undef;
88 sub pidl_code($)
90 my $d = shift;
91 return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
93 if ($d) {
94 $res{code} .= $tabs;
95 $res{code} .= $d;
97 $res{code} .="\n";
100 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
101 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
103 sub indent()
105 $tabs .= "\t";
108 sub deindent()
110 $tabs = substr($tabs, 0, -1);
113 sub PrintIdl($)
115 my ($idl) = @_;
117 foreach (split /\n/, $idl) {
118 pidl_code "/* IDL: $_ */";
121 pidl_code "";
124 #####################################################################
125 # parse the interface definitions
126 sub Interface($)
128 my($interface) = @_;
129 Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
130 Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}});
131 Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
134 sub Enum($$$)
136 my ($e,$name,$ifname) = @_;
137 my $valsstring = "$ifname\_$name\_vals";
138 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
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)";
163 pidl_code "{";
164 indent;
165 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
166 pidl_code "return offset;";
167 deindent;
168 pidl_code "}\n";
169 pidl_fn_end $dissectorname;
171 my $enum_size = $e->{BASE_TYPE};
172 $enum_size =~ s/uint//g;
173 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
176 sub Bitmap($$$)
178 my ($e,$name,$ifname) = @_;
179 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
181 register_ett("ett_$ifname\_$name");
183 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
185 pidl_fn_start $dissectorname;
186 pidl_code "int";
187 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
188 pidl_code "{";
189 indent;
190 pidl_code "proto_item *item = NULL;";
191 pidl_code "proto_tree *tree = NULL;";
192 pidl_code "";
194 pidl_code "g$e->{BASE_TYPE} flags;";
195 if ($e->{ALIGN} > 1) {
196 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
199 pidl_code "";
201 pidl_code "if (parent_tree) {";
202 indent;
203 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
204 pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
205 deindent;
206 pidl_code "}\n";
208 pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
210 pidl_code "proto_item_append_text(item, \": \");\n";
211 pidl_code "if (!flags)";
212 pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
214 foreach (@{$e->{ELEMENTS}}) {
215 next unless (/([^ ]*) (.*)/);
216 my ($en,$ev) = ($1,$2);
217 my $hf_bitname = "hf_$ifname\_$name\_$en";
218 my $filtername = "$ifname\.$name\.$en";
220 $hf_used{$hf_bitname} = 1;
222 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
224 pidl_def "static const true_false_string $name\_$en\_tfs = {";
225 if (defined($conformance->{tfs}->{$hf_bitname})) {
226 pidl_def " $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
227 pidl_def " $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
228 $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
229 } else {
230 pidl_def " \"$en is SET\",";
231 pidl_def " \"$en is NOT SET\",";
233 pidl_def "};";
235 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
236 pidl_code "if (flags&$ev){";
237 pidl_code "\tproto_item_append_text(item, \"$en\");";
238 pidl_code "\tif (flags & (~$ev))";
239 pidl_code "\t\tproto_item_append_text(item, \", \");";
240 pidl_code "}";
241 pidl_code "flags&=(~$ev);";
242 pidl_code "";
245 pidl_code "if (flags) {";
246 pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
247 pidl_code "}\n";
248 pidl_code "return offset;";
249 deindent;
250 pidl_code "}\n";
251 pidl_fn_end $dissectorname;
253 my $size = $e->{BASE_TYPE};
254 $size =~ s/uint//g;
255 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
258 sub ElementLevel($$$$$$)
260 my ($e,$l,$hf,$myname,$pn,$ifname) = @_;
262 my $param = 0;
264 if (defined($conformance->{dissectorparams}->{$myname})) {
265 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
268 if ($l->{TYPE} eq "POINTER") {
269 my $type;
270 if ($l->{LEVEL} eq "TOP") {
271 $type = "toplevel";
272 } elsif ($l->{LEVEL} eq "EMBEDDED") {
273 $type = "embedded";
275 pidl_code "offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME})) . " ($e->{TYPE})\",$hf);";
276 } elsif ($l->{TYPE} eq "ARRAY") {
277 if ($l->{IS_INLINE}) {
278 error($e->{ORIGINAL}, "Inline arrays not supported");
279 } elsif ($l->{IS_FIXED}) {
280 pidl_code "int i;";
281 pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
282 pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
283 } else {
284 my $type = "";
285 $type .= "c" if ($l->{IS_CONFORMANT});
286 $type .= "v" if ($l->{IS_VARYING});
288 unless ($l->{IS_ZERO_TERMINATED}) {
289 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
290 } else {
291 my $nl = GetNextLevel($e,$l);
292 pidl_code "char *data;";
293 pidl_code "";
294 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
295 pidl_code "proto_item_append_text(tree, \": %s\", data);";
298 } elsif ($l->{TYPE} eq "DATA") {
299 if ($l->{DATA_TYPE} eq "string") {
300 my $bs = 2; # Byte size defaults to that of UCS2
303 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
305 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
306 pidl_code "char *data;\n";
307 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
308 pidl_code "proto_item_append_text(tree, \": %s\", data);";
309 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
310 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
311 } else {
312 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
314 } else {
315 my $call;
317 if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
318 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};
319 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
320 } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
321 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
322 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
324 } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
325 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
326 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
327 } else {
328 pidl_code "offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);";
330 return;
333 $call =~ s/\@HF\@/$hf/g;
334 $call =~ s/\@PARAM\@/$param/g;
335 pidl_code "$call";
337 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
338 my $num_bits = ($l->{HEADER_SIZE}*8);
339 pidl_code "guint$num_bits size;";
340 pidl_code "int start_offset = offset;";
341 pidl_code "tvbuff_t *subtvb;";
342 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
343 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
345 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
346 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
347 } else {
348 die("Unknown type `$_->{TYPE}'");
352 sub Element($$$)
354 my ($e,$pn,$ifname) = @_;
356 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
358 my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
360 my $type = find_type($e->{TYPE});
362 if (not defined($type)) {
363 # default settings
364 $type = {
365 MASK => 0,
366 VALSSTRING => "NULL",
367 FT_TYPE => "FT_NONE",
368 BASE_TYPE => "BASE_HEX"
372 if (ContainsString($e)) {
373 $type = {
374 MASK => 0,
375 VALSSTRING => "NULL",
376 FT_TYPE => "FT_STRING",
377 BASE_TYPE => "BASE_DEC"
381 my $hf = register_hf_field("hf_$ifname\_$pn\_$e->{NAME}", field2name($e->{NAME}), "$ifname.$pn.$e->{NAME}", $type->{FT_TYPE}, $type->{BASE_TYPE}, $type->{VALSSTRING}, $type->{MASK}, "");
382 $hf_used{$hf} = 1;
384 my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
385 if (defined($conformance->{noemit}->{$eltname})) {
386 return $call_code;
389 my $add = "";
391 foreach (@{$e->{LEVELS}}) {
392 next if ($_->{TYPE} eq "SWITCH");
393 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
394 pidl_fn_start "$dissectorname$add";
395 pidl_code "static int";
396 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
397 pidl_code "{";
398 indent;
400 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
402 pidl_code "";
403 pidl_code "return offset;";
404 deindent;
405 pidl_code "}\n";
406 pidl_fn_end "$dissectorname$add";
407 $add.="_";
408 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
411 return $call_code;
414 sub Function($$$)
416 my ($fn,$ifname) = @_;
418 my %dissectornames;
420 foreach (@{$fn->{ELEMENTS}}) {
421 $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
424 my $fn_name = $_->{NAME};
425 $fn_name =~ s/^${ifname}_//;
427 PrintIdl DumpFunction($fn->{ORIGINAL});
428 pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
429 pidl_code "static int";
430 pidl_code "$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo, proto_tree *tree _U_, guint8 *drep _U_)";
431 pidl_code "{";
432 indent;
433 if ( not defined($fn->{RETURN_TYPE})) {
434 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR")
436 pidl_code "guint32 status;\n";
437 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
438 if ($type->{DATA}->{TYPE} eq "ENUM") {
439 pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
440 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
441 pidl_code "g$fn->{RETURN_TYPE} status;\n";
442 } else {
443 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
445 } else {
446 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
449 pidl_code "pinfo->dcerpc_procedure_name=\"${fn_name}\";";
450 foreach (@{$fn->{ELEMENTS}}) {
451 if (grep(/out/,@{$_->{DIRECTION}})) {
452 pidl_code "$dissectornames{$_->{NAME}}";
453 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
454 pidl_code "";
458 if (not defined($fn->{RETURN_TYPE})) {
459 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
460 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
461 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
462 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
463 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
464 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
465 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &status);\n";
466 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
467 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
469 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
470 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
471 if ($type->{DATA}->{TYPE} eq "ENUM") {
472 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
473 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
475 pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
476 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
477 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %s\", val_to_str(status, $ifname\_$fn->{RETURN_TYPE}\_vals, \"Unknown " . $fn->{RETURN_TYPE} . " error 0x%08x\"));\n";
478 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
479 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
480 pidl_code "offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
481 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
482 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n";
483 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
488 pidl_code "return offset;";
489 deindent;
490 pidl_code "}\n";
491 pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
493 pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
494 pidl_code "static int";
495 pidl_code "$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo, proto_tree *tree _U_, guint8 *drep _U_)";
496 pidl_code "{";
497 indent;
498 pidl_code "pinfo->dcerpc_procedure_name=\"${fn_name}\";";
499 foreach (@{$fn->{ELEMENTS}}) {
500 if (grep(/in/,@{$_->{DIRECTION}})) {
501 pidl_code "$dissectornames{$_->{NAME}}";
502 pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
507 pidl_code "return offset;";
508 deindent;
509 pidl_code "}\n";
510 pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
513 sub Struct($$$)
515 my ($e,$name,$ifname) = @_;
516 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
518 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
520 register_ett("ett_$ifname\_$name");
522 my $res = "";
523 ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
525 pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
527 pidl_fn_start $dissectorname;
528 pidl_code "int";
529 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
530 pidl_code "{";
531 indent;
532 pidl_code "proto_item *item = NULL;";
533 pidl_code "proto_tree *tree = NULL;";
534 pidl_code "int old_offset;";
535 pidl_code "";
537 if ($e->{ALIGN} > 1) {
538 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
540 pidl_code "";
542 pidl_code "old_offset = offset;";
543 pidl_code "";
544 pidl_code "if (parent_tree) {";
545 indent;
546 pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
547 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
548 deindent;
549 pidl_code "}";
551 pidl_code "\n$res";
553 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
554 pidl_code "return offset;";
555 deindent;
556 pidl_code "}\n";
557 pidl_fn_end $dissectorname;
559 register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
562 sub Union($$$)
564 my ($e,$name,$ifname) = @_;
566 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
568 return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
570 register_ett("ett_$ifname\_$name");
572 my $res = "";
573 foreach (@{$e->{ELEMENTS}}) {
574 $res.="\n\t\t$_->{CASE}:\n";
575 if ($_->{TYPE} ne "EMPTY") {
576 $res.="\t\t\t".Element($_, $name, $ifname)."\n";
578 $res.="\t\tbreak;\n";
581 my $switch_type;
582 my $switch_dissect;
583 my $switch_dt = getType($e->{SWITCH_TYPE});
584 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
585 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
586 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
587 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
588 $switch_type = "g$e->{SWITCH_TYPE}";
589 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
592 pidl_fn_start $dissectorname;
593 pidl_code "static int";
594 pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
595 pidl_code "{";
596 indent;
597 pidl_code "proto_item *item = NULL;";
598 pidl_code "proto_tree *tree = NULL;";
599 pidl_code "int old_offset;";
600 pidl_code "$switch_type level;";
601 pidl_code "";
603 if ($e->{ALIGN} > 1) {
604 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
607 pidl_code "";
609 pidl_code "old_offset = offset;";
610 pidl_code "if (parent_tree) {";
611 indent;
612 pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
613 pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
614 deindent;
615 pidl_code "}";
617 pidl_code "";
619 pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
621 pidl_code "switch(level) {$res\t}";
622 pidl_code "proto_item_set_len(item, offset-old_offset);\n";
623 pidl_code "return offset;";
624 deindent;
625 pidl_code "}";
626 pidl_fn_end $dissectorname;
628 register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
631 sub Const($$)
633 my ($const,$ifname) = @_;
635 if (!defined($const->{ARRAY_LEN}[0])) {
636 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
637 } else {
638 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
642 sub Typedef($$$)
644 my ($e,$name,$ifname) = @_;
646 Type($e->{DATA}, $name, $ifname);
649 sub Type($$$)
651 my ($e, $name, $ifname) = @_;
653 PrintIdl DumpType($e->{ORIGINAL});
656 ENUM => \&Enum,
657 STRUCT => \&Struct,
658 UNION => \&Union,
659 BITMAP => \&Bitmap,
660 TYPEDEF => \&Typedef
661 }->{$e->{TYPE}}->($e, $name, $ifname);
664 sub RegisterInterface($)
666 my ($x) = @_;
668 pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
669 pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
670 pidl_code "{";
671 indent;
673 $res{code}.=DumpHfList()."\n";
674 $res{code}.="\n".DumpEttList()."\n";
676 if (defined($x->{UUID})) {
677 # These can be changed to non-pidl_code names if the old dissectors
678 # in epan/dissctors are deleted.
680 my $name = uc($x->{NAME}) . " (pidl)";
681 my $short_name = uc($x->{NAME});
682 my $filter_name = $x->{NAME};
684 if (has_property($x, "helpstring")) {
685 $name = $x->{PROPERTIES}->{helpstring};
688 if (defined($conformance->{protocols}->{$x->{NAME}})) {
689 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
690 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
691 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
694 pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
696 pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
697 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
698 } else {
699 pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
700 pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
701 pidl_code "proto_register_subtree_array(ett, array_length(ett));";
704 deindent;
705 pidl_code "}\n";
706 pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
709 sub RegisterInterfaceHandoff($)
711 my $x = shift;
713 if (defined($x->{UUID})) {
714 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
715 pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
716 pidl_code "{";
717 indent;
718 pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
719 pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
720 pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
721 deindent;
722 pidl_code "}";
723 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
725 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
729 sub ProcessInclude
731 my @includes = @_;
732 foreach (@includes) {
733 pidl_hdr "#include \"$_\"\n";
737 sub ProcessImport
739 my @imports = @_;
740 foreach (@imports) {
741 next if($_ eq "security");
742 s/\.idl\"$//;
743 s/^\"//;
744 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
748 sub ProcessInterface($)
750 my ($x) = @_;
752 push(@{$conformance->{strip_prefixes}}, $x->{NAME});
754 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
755 pidl_hdr "#ifndef $define";
756 pidl_hdr "#define $define";
757 pidl_hdr "";
759 pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
760 register_ett("ett_dcerpc_$x->{NAME}");
761 register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
763 if (defined($x->{UUID})) {
764 my $if_uuid = $x->{UUID};
766 pidl_def "/* Version information */\n\n";
768 pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
769 pidl_def "\t0x" . substr($if_uuid, 1, 8)
770 . ", 0x" . substr($if_uuid, 10, 4)
771 . ", 0x" . substr($if_uuid, 15, 4) . ",";
772 pidl_def "\t{ 0x" . substr($if_uuid, 20, 2)
773 . ", 0x" . substr($if_uuid, 22, 2)
774 . ", 0x" . substr($if_uuid, 25, 2)
775 . ", 0x" . substr($if_uuid, 27, 2)
776 . ", 0x" . substr($if_uuid, 29, 2)
777 . ", 0x" . substr($if_uuid, 31, 2)
778 . ", 0x" . substr($if_uuid, 33, 2)
779 . ", 0x" . substr($if_uuid, 35, 2) . " }";
780 pidl_def "};";
782 my $maj = $x->{VERSION};
783 $maj =~ s/\.(.*)$//g;
784 pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
785 pidl_def "";
788 $return_types{$x->{NAME}} = {};
790 Interface($x);
792 pidl_code "\n".DumpFunctionTable($x);
794 foreach (keys %{$return_types{$x->{NAME}}}) {
795 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
796 my $dt = find_type($type);
797 $dt or die("Unable to find information about return type `$type'");
798 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
799 $hf_used{"hf_$x->{NAME}_$_"} = 1;
802 RegisterInterface($x);
803 RegisterInterfaceHandoff($x);
805 pidl_hdr "#endif /* $define */";
808 sub find_type($)
810 my $n = shift;
812 return $conformance->{types}->{$n};
815 sub register_type($$$$$$$)
817 my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
819 $conformance->{types}->{$type} = {
820 NAME => $type,
821 DISSECTOR_NAME => $call,
822 FT_TYPE => $ft,
823 BASE_TYPE => $base,
824 MASK => $mask,
825 VALSSTRING => $vals,
826 ALIGNMENT => $length
830 # Loads the default types
831 sub Initialize($)
833 my $cnf_file = shift;
835 $conformance = {
836 imports => {},
837 header_fields=> {}
840 ReadConformance($cnf_file, $conformance) or print STDERR "warning: No conformance file `$cnf_file'\n";
842 foreach my $bytes (qw(1 2 4 8)) {
843 my $bits = $bytes * 8;
844 register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
845 register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
848 register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
849 register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
850 register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
851 register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
852 register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
853 register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
854 register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
855 register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
856 register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
857 register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
858 register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
859 register_type("SID", "
860 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
862 di->hf_index = \@HF\@;
864 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
865 ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
866 register_type("WERROR",
867 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
868 register_type("NTSTATUS",
869 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
873 #####################################################################
874 # Generate Wireshark parser and header code
875 sub Parse($$$$)
877 my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
878 Initialize($cnf_file);
880 return (undef, undef) if defined($conformance->{noemit_dissector});
882 $tabs = "";
884 %res = (code=>"",def=>"",hdr=>"");
885 @ett = ();
887 my $notice =
888 "/* DO NOT EDIT
889 This filter was automatically generated
890 from $idl_file and $cnf_file.
892 Pidl is a perl based IDL compiler for DCE/RPC idl files.
893 It is maintained by the Samba team, not the Wireshark team.
894 Instructions on how to download and install Pidl can be
895 found at http://wiki.wireshark.org/Pidl
900 pidl_hdr $notice;
902 $res{headers} = "\n";
903 $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
904 $res{headers} .= "#include \"config.h\"\n";
905 $res{headers} .= "#endif\n\n";
906 $res{headers} .= "#include <glib.h>\n";
907 $res{headers} .= "#include <string.h>\n";
908 $res{headers} .= "#include <epan/packet.h>\n\n";
910 $res{headers} .= "#include \"packet-dcerpc.h\"\n";
911 $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
912 $res{headers} .= "#include \"packet-windows-common.h\"\n";
914 my $h_basename = basename($h_filename);
916 $res{headers} .= "#include \"$h_basename\"\n";
917 pidl_code "";
919 # Wireshark protocol registration
921 foreach (@$ndr) {
922 ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
923 ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
924 ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
927 $res{ett} = DumpEttDeclaration();
928 $res{hf} = DumpHfDeclaration();
930 my $parser = $notice;
931 $parser.= $res{headers};
932 $parser.=$res{ett};
933 $parser.=$res{hf};
934 $parser.=$res{def};
935 if (exists ($conformance->{override})) {
936 $parser.=$conformance->{override};
938 $parser.=$res{code};
940 my $header = "/* autogenerated by pidl */\n\n";
941 $header.=$res{hdr};
943 CheckUsed($conformance);
945 return ($parser,$header);
948 ###############################################################################
949 # ETT
950 ###############################################################################
952 sub register_ett($)
954 my $name = shift;
956 push (@ett, $name);
959 sub DumpEttList()
961 my $res = "\tstatic gint *ett[] = {\n";
962 foreach (@ett) {
963 $res .= "\t\t&$_,\n";
966 return "$res\t};\n";
969 sub DumpEttDeclaration()
971 my $res = "\n/* Ett declarations */\n";
972 foreach (@ett) {
973 $res .= "static gint $_ = -1;\n";
976 return "$res\n";
979 ###############################################################################
980 # HF
981 ###############################################################################
983 sub register_hf_field($$$$$$$$)
985 my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
987 if (defined ($conformance->{hf_renames}->{$index})) {
988 $conformance->{hf_renames}->{$index}->{USED} = 1;
989 return $conformance->{hf_renames}->{$index}->{NEWNAME};
992 $conformance->{header_fields}->{$index} = {
993 INDEX => $index,
994 NAME => $name,
995 FILTER => $filter_name,
996 FT_TYPE => $ft_type,
997 BASE_TYPE => $base_type,
998 VALSSTRING => $valsstring,
999 MASK => $mask,
1000 BLURB => $blurb
1003 if ((not defined($blurb) or $blurb eq "") and
1004 defined($conformance->{fielddescription}->{$index})) {
1005 $conformance->{header_fields}->{$index}->{BLURB} =
1006 $conformance->{fielddescription}->{$index}->{DESCRIPTION};
1007 $conformance->{fielddescription}->{$index}->{USED} = 1;
1010 return $index;
1013 sub DumpHfDeclaration()
1015 my $res = "";
1017 $res = "\n/* Header field declarations */\n";
1019 foreach (keys %{$conformance->{header_fields}})
1021 $res .= "static gint $_ = -1;\n";
1024 return "$res\n";
1027 sub DumpHfList()
1029 my $res = "\tstatic hf_register_info hf[] = {\n";
1031 foreach (values %{$conformance->{header_fields}})
1033 $res .= "\t{ &$_->{INDEX},
1034 { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
1038 return $res."\t};\n";
1042 ###############################################################################
1043 # Function table
1044 ###############################################################################
1046 sub DumpFunctionTable($)
1048 my $if = shift;
1050 my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1051 foreach (@{$if->{FUNCTIONS}}) {
1052 my $fn_name = $_->{NAME};
1053 $fn_name =~ s/^$if->{NAME}_//;
1054 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1055 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1058 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1060 return "$res};\n";
1063 sub CheckUsed($)
1065 my $conformance = shift;
1066 foreach (values %{$conformance->{header_fields}}) {
1067 if (not defined($hf_used{$_->{INDEX}})) {
1068 warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1072 foreach (values %{$conformance->{hf_renames}}) {
1073 if (not $_->{USED}) {
1074 warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1078 foreach (values %{$conformance->{dissectorparams}}) {
1079 if (not $_->{USED}) {
1080 warning($_->{POS}, "dissector param never used");
1084 foreach (values %{$conformance->{imports}}) {
1085 if (not $_->{USED}) {
1086 warning($_->{POS}, "import never used");
1090 foreach (values %{$conformance->{types}}) {
1091 if (not $_->{USED} and defined($_->{POS})) {
1092 warning($_->{POS}, "type never used");
1096 foreach (values %{$conformance->{fielddescription}}) {
1097 if (not $_->{USED}) {
1098 warning($_->{POS}, "description never used");
1102 foreach (values %{$conformance->{tfs}}) {
1103 if (not $_->{USED}) {
1104 warning($_->{POS}, "True/False description never used");