Debian Codegraph @

pidl/lib/Parse/Pidl/Wireshark/NDR.pm

raw
1 ##################################################
2 # Wireshark NDR parser generator for IDL structures
3 # Copyright [email protected] 2000-2003
4 # Copyright [email protected] 2001,2005
5 # Copyright [email protected] 2004-2007
6 # Portions based on idl2eth.c by Ronnie Sahlberg
7 # released under the GNU GPL
8
9 =pod
10
11 =head1 NAME
12
13 Parse::Pidl::Wireshark::NDR - Parser generator for Wireshark
14
15 =cut
16
17 package Parse::Pidl::Wireshark::NDR;
18
19 use Exporter;
20 @ISA = qw(Exporter);
21 @EXPORT_OK = qw(field2name %res PrintIdl StripPrefixes RegisterInterfaceHandoff register_hf_field CheckUsed ProcessImport ProcessInclude find_type DumpEttList DumpEttDeclaration DumpHfList DumpHfDeclaration DumpFunctionTable register_type register_ett);
22
23 use strict;
24 use warnings;
25 use Parse::Pidl qw(error warning);
26 use Parse::Pidl::Typelist qw(getType mapScalarType);
27 use Parse::Pidl::Util qw(has_property property_matches make_str);
28 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
29 use Parse::Pidl::Dump qw(DumpType DumpFunction);
30 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
31 use File::Basename;
32
33 use vars qw($VERSION);
34 $VERSION = '0.01';
35
36 my %return_types = ();
37 my %dissector_used = ();
38
39 my %ptrtype_mappings = (
40 "unique" => "NDR_POINTER_UNIQUE",
41 "ref" => "NDR_POINTER_REF",
42 "ptr" => "NDR_POINTER_PTR"
43 );
44
45 sub StripPrefixes($$)
46 {
47 my ($s, $prefixes) = @_;
48
49 foreach (@$prefixes) {
50 $s =~ s/^$_\_//g;
51 }
52
53 return $s;
54 }
55
56 # Convert a IDL structure field name (e.g access_mask) to a prettier
57 # string like 'Access Mask'.
58
59 sub field2name($)
60 {
61 my($field) = shift;
62
63 $field =~ s/^(_)*//g; # Remove any starting underscores
64 $field =~ s/_/ /g; # Replace underscores with spaces
65 $field =~ s/(\w+)/\u$1/g; # Capitalise each word
66
67 return $field;
68 }
69
70 sub new($)
71 {
72 my ($class) = @_;
73 my $self = {res => {hdr => "", def => "", code => ""}, tabs => "", cur_fn => undef,
74 hf_used => {}, ett => [], conformance => undef
75
76 };
77 bless($self, $class);
78 }
79
80 sub pidl_fn_start($$)
81 {
82 my ($self, $fn) = @_;
83 $self->{cur_fn} = $fn;
84 }
85 sub pidl_fn_end($$)
86 {
87 my ($self, $fn) = @_;
88 die("Inconsistent state: $fn != $self->{cur_fn}") if ($fn ne $self->{cur_fn});
89 $self->{cur_fn} = undef;
90 }
91
92 sub pidl_code($$)
93 {
94 my ($self, $d) = @_;
95 return if (defined($self->{cur_fn}) and defined($self->{conformance}->{manual}->{$self->{cur_fn}}));
96
97 if ($d) {
98 $self->{res}->{code} .= $self->{tabs};
99 $self->{res}->{code} .= $d;
100 }
101 $self->{res}->{code} .="\n";
102 }
103
104 sub pidl_hdr($$) { my ($self,$x) = @_; $self->{res}->{hdr} .= "$x\n"; }
105 sub pidl_def($$) { my ($self,$x) = @_; $self->{res}->{def} .= "$x\n"; }
106
107 sub indent($)
108 {
109 my ($self) = @_;
110 $self->{tabs} .= "\t";
111 }
112
113 sub deindent($)
114 {
115 my ($self) = @_;
116 $self->{tabs} = substr($self->{tabs}, 0, -1);
117 }
118
119 sub PrintIdl($$)
120 {
121 my ($self, $idl) = @_;
122
123 foreach (split /\n/, $idl) {
124 $self->pidl_code("/* IDL: $_ */");
125 }
126
127 $self->pidl_code("");
128 }
129
130 #####################################################################
131 # parse the interface definitions
132 sub Interface($$)
133 {
134 my($self, $interface) = @_;
135 $self->Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
136 $self->Type($_, $_->{NAME}, $interface->{NAME}) foreach (@{$interface->{TYPES}});
137 $self->Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
138 }
139
140 sub Enum($$$$)
141 {
142 my ($self, $e,$name,$ifname) = @_;
143 my $valsstring = "$ifname\_$name\_vals";
144 my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
145
146 return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
147
148 foreach (@{$e->{ELEMENTS}}) {
149 if (/([^=]*)=(.*)/) {
150 $self->pidl_hdr("#define $1 ($2)");
151 }
152 }
153
154 $self->pidl_hdr("extern const value_string $valsstring\[];");
155 $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_);");
156
157 $self->pidl_def("const value_string ".$valsstring."[] = {");
158 foreach (@{$e->{ELEMENTS}}) {
159 next unless (/([^=]*)=(.*)/);
160 $self->pidl_def("\t{ $1, \"$1\" },");
161 }
162
163 $self->pidl_def("{ 0, NULL }");
164 $self->pidl_def("};");
165
166 $self->pidl_fn_start($dissectorname);
167 $self->pidl_code("int");
168 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_)");
169 $self->pidl_code("{");
170 $self->indent;
171 $self->pidl_code("g$e->{BASE_TYPE} parameter=0;");
172 $self->pidl_code("if (param) {");
173 $self->indent;
174 $self->pidl_code("parameter = *param;");
175 $self->deindent;
176 $self->pidl_code("}");
177 $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, di, drep, hf_index, &parameter);");
178 $self->pidl_code("if (param) {");
179 $self->indent;
180 $self->pidl_code("*param = parameter;");
181 $self->deindent;
182 $self->pidl_code("}");
183 $self->pidl_code("return offset;");
184 $self->deindent;
185 $self->pidl_code("}\n");
186 $self->pidl_fn_end($dissectorname);
187
188 my $enum_size = $e->{BASE_TYPE};
189 $enum_size =~ s/uint//g;
190 $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
191 }
192
193 sub Pipe($$$$)
194 {
195 my ($self,$e,$name,$ifname) = @_;
196 error($e->{ORIGINAL}, "Pipe not yet supported");
197 return;
198 }
199
200 sub Bitmap($$$$)
201 {
202 my ($self,$e,$name,$ifname) = @_;
203 my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
204 my $element_count = 0;
205 my $total_ev = 0;
206
207 $self->register_ett("ett_$ifname\_$name");
208
209 $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_);");
210
211 $self->pidl_fn_start($dissectorname);
212 $self->pidl_code("int");
213 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_)");
214 $self->pidl_code("{");
215 $self->indent;
216 foreach (@{$e->{ELEMENTS}}) {
217 next unless (/([^ ]*) (.*)/);
218 $element_count++;
219 }
220 if ($element_count > 0) {
221 $self->pidl_code("proto_item *item;");
222 $self->pidl_code("static int * const $ifname\_$name\_fields[] = {");
223 $self->indent;
224 foreach (@{$e->{ELEMENTS}}) {
225 next unless (/([^ ]*) (.*)/);
226 my ($en,$ev) = ($1,$2);
227 my $hf_bitname = "hf_$ifname\_$name\_$1";
228
229 $ev =~ s/[()\s]//g;
230 if (hex($ev) != 0) {
231 $total_ev += hex($ev);
232 $self->pidl_code("&$hf_bitname,");
233 }
234 }
235 $self->pidl_code("NULL");
236 $self->deindent;
237 $self->pidl_code("};");
238 }
239
240 $self->pidl_code("g$e->{BASE_TYPE} flags;");
241 if ($e->{ALIGN} > 1) {
242 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
243 }
244
245 $self->pidl_code("");
246
247 if ($element_count > 0) {
248 $self->pidl_code("item = proto_tree_add_bitmask_with_flags(parent_tree, tvb, offset, hf_index,");
249 $self->pidl_code("\t\t\tett_$ifname\_$name, $ifname\_$name\_fields, DREP_ENC_INTEGER(drep), BMT_NO_FALSE);");
250 $self->pidl_code("");
251
252 $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, parent_tree, di, drep, -1, &flags);");
253 $self->pidl_code("");
254
255 $self->pidl_code("if (!flags)");
256 $self->pidl_code("\tproto_item_append_text(item, \": (No values set)\");\n");
257 } else {
258 $self->pidl_code("proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, DREP_ENC_INTEGER(drep));");
259 $self->pidl_code("");
260
261 $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, parent_tree, di, drep, -1, &flags);");
262 $self->pidl_code("");
263 }
264
265 foreach (@{$e->{ELEMENTS}}) {
266 next unless (/([^ ]*) (.*)/);
267 my ($en,$ev) = ($1,$2);
268 my $hf_bitname = "hf_$ifname\_$name\_$en";
269 my $filtername = "$ifname\.$name\.$en";
270
271 $self->{hf_used}->{$hf_bitname} = 1;
272
273 $ev =~ s/[()\s]//g;
274 if (hex($ev) != 0) {
275 $self->register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", "( $ev )", "");
276
277 $self->pidl_def("static const true_false_string $name\_$en\_tfs = {");
278 if (defined($self->{conformance}->{tfs}->{$hf_bitname})) {
279 $self->pidl_def(" $self->{conformance}->{tfs}->{$hf_bitname}->{TRUE_STRING},");
280 $self->pidl_def(" $self->{conformance}->{tfs}->{$hf_bitname}->{FALSE_STRING},");
281 $self->{conformance}->{tfs}->{$hf_bitname}->{USED} = 1;
282 } else {
283 $self->pidl_def(" \"$en is SET\",");
284 $self->pidl_def(" \"$en is NOT SET\",");
285 }
286 $self->pidl_def("};");
287 }
288 }
289
290 if ($element_count > 0) {
291 my $total_ev_hex = sprintf("0x%08x", $total_ev);
292 $self->pidl_code("if (flags & (~$total_ev_hex)) {");
293 $self->pidl_code("\tflags &= (~$total_ev_hex);");
294 $self->pidl_code("\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);");
295 $self->pidl_code("}\n");
296 }
297 $self->pidl_code("return offset;");
298 $self->deindent;
299 $self->pidl_code("}\n");
300 $self->pidl_fn_end($dissectorname);
301
302 my $size = $e->{BASE_TYPE};
303 $size =~ s/uint//g;
304 $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
305 }
306
307 sub ElementLevel($$$$$$$$)
308 {
309 my ($self,$e,$l,$hf,$myname,$pn,$ifname,$param) = @_;
310
311 if (defined($self->{conformance}->{dissectorparams}->{$myname})) {
312 $param = $self->{conformance}->{dissectorparams}->{$myname}->{PARAM};
313 }
314
315 if ($l->{TYPE} eq "POINTER") {
316 my $type;
317 if ($l->{LEVEL} eq "TOP") {
318 $type = "toplevel";
319 } elsif ($l->{LEVEL} eq "EMBEDDED") {
320 $type = "embedded";
321 }
322 $self->pidl_code("offset = dissect_ndr_$type\_pointer(tvb, offset, pinfo, tree, di, drep, $myname\_, $ptrtype_mappings{$l->{POINTER_TYPE}}, \"Pointer to ".field2name(StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes})) . " ($e->{TYPE})\",$hf);");
323 } elsif ($l->{TYPE} eq "ARRAY") {
324 if ($l->{IS_INLINE}) {
325 error($e->{ORIGINAL}, "Inline arrays not supported automatically. Use conformance file MANUAL directives for `$myname' and `$l->{SIZE_IS}' to implement them.");
326 } elsif ($l->{IS_FIXED}) {
327 $self->pidl_code("int i;");
328 $self->pidl_code("for (i = 0; i < $l->{SIZE_IS}; i++)");
329 $self->pidl_code("\toffset = $myname\_(tvb, offset, pinfo, tree, di, drep);");
330 } else {
331 my $type = "";
332 $type .= "c" if ($l->{IS_CONFORMANT});
333 $type .= "v" if ($l->{IS_VARYING});
334
335 unless ($l->{IS_ZERO_TERMINATED}) {
336 $self->pidl_code("offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, di, drep, $myname\_);");
337 } else {
338 my $nl = GetNextLevel($e,$l);
339 my $nl_ctype = mapScalarType($nl->{DATA_TYPE});
340 $self->pidl_code("char *data;");
341 $self->pidl_code("");
342 $self->pidl_code("offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, di, drep, sizeof($nl_ctype), $hf, false, &data);");
343 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
344 }
345 }
346 } elsif ($l->{TYPE} eq "DATA") {
347 if ($l->{DATA_TYPE} eq "string") {
348 my $bs = 2; # Byte size defaults to that of UCS2
349
350
351 ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
352
353 if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
354 $self->pidl_code("char *data;\n");
355 $self->pidl_code("offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, di, drep, $bs, $hf, false, &data);");
356 $self->pidl_code("proto_item_append_text(tree, \": %s\", data);");
357 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
358 $self->pidl_code("offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, di, drep, $bs, $hf, false, NULL);");
359 } elsif (property_matches($e, "flag", ".*STR_NULLTERM.*")) {
360 if ($bs == 2) {
361 $self->pidl_code("offset = dissect_null_term_wstring(tvb, offset, pinfo, tree, drep, $hf , 0);")
362 } else {
363 $self->pidl_code("offset = dissect_null_term_string(tvb, offset, pinfo, tree, drep, $hf , 0);")
364 }
365 } else {
366 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
367 }
368 } elsif ($l->{DATA_TYPE} eq "DATA_BLOB") {
369 my $remain = 0;
370 $remain = 1 if (property_matches($e->{ORIGINAL}, "flag", ".*LIBNDR_FLAG_REMAINING.*"));
371 $self->pidl_code("offset = dissect_ndr_datablob(tvb, offset, pinfo, tree, di, drep, $hf, $remain);");
372 } else {
373 my $call;
374
375 if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) {
376 $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};
377 $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
378 } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
379 $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
380 $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
381 } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) {
382 $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
383 $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
384 } else {
385 my $t;
386 if (ref($l->{DATA_TYPE}) eq "HASH" ) {
387 $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
388 } else {
389 $t = $l->{DATA_TYPE};
390 }
391
392 if ($param ne 0) {
393 $self->{conformance}->{dissectorparams}->{$myname}->{USED} = 1;
394 }
395 $self->pidl_code("offset = $ifname\_dissect_struct_" . $t . "(tvb,offset,pinfo,tree,di,drep,$hf,$param);");
396
397 return;
398 }
399
400 $call =~ s/\@HF\@/$hf/g;
401 my $replacements = ($call =~ s/\@PARAM\@/$param/g);
402 if ($param ne 0 and $replacements ne 0) {
403 $self->{conformance}->{dissectorparams}->{$myname}->{USED} = 1;
404 }
405 $self->pidl_code($call);
406 }
407 } elsif ($_->{TYPE} eq "SUBCONTEXT") {
408 my $varswitch;
409 if (has_property($e, "switch_is")) {
410 $varswitch = $e->{PROPERTIES}->{switch_is};
411 }
412 my $num_bits = ($l->{HEADER_SIZE}*8);
413 my $hf2 = $self->register_hf_field($hf."_", "Subcontext length", "$ifname.$pn.$e->{NAME}.subcontext", "FT_UINT$num_bits", "BASE_HEX", "NULL", 0, "");
414 $num_bits = 3264 if ($num_bits == 32);
415 $self->{hf_used}->{$hf2} = 1;
416 $self->pidl_code("guint$num_bits size;");
417 $self->pidl_code("int conformant = di->conformant_run;");
418 $self->pidl_code("tvbuff_t *subtvb;");
419 $self->pidl_code("");
420 # We need to be able to dissect the length of the context in every case
421 # and conformant run skips the dissections of scalars ...
422 $self->pidl_code("if (!conformant) {");
423 $self->indent;
424 $self->pidl_code("guint32 saved_flags = di->call_data->flags;");
425 $self->pidl_code("offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, di, drep, $hf2, &size);");
426 # This is a subcontext, there is normally no such thing as
427 # 64 bit NDR is subcontext so we clear the flag so that we can
428 # continue to dissect handmarshalled stuff with pidl
429 $self->pidl_code("di->call_data->flags &= ~DCERPC_IS_NDR64;");
430
431 $self->pidl_code("subtvb = tvb_new_subset_length_caplen(tvb, offset, (const int)size, -1);");
432 if ($param ne 0) {
433 $self->{conformance}->{dissectorparams}->{$myname}->{USED} = 1;
434 $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep, $param);");
435 } else {
436 $self->pidl_code("$myname\_(subtvb, 0, pinfo, tree, di, drep);");
437 }
438 $self->pidl_code("offset += (int)size;");
439 $self->pidl_code("di->call_data->flags = saved_flags;");
440 $self->deindent;
441 $self->pidl_code("}");
442 } elsif ($_->{TYPE} eq "PIPE") {
443 error($e->{ORIGINAL}, "Type PIPE not yet supported");
444 } else {
445 die("Unknown type `$_->{TYPE}'");
446 }
447 }
448
449 sub SwitchType($$;$)
450 {
451 my ($e, $type, $nodiscriminant) = @_;
452
453 my $switch_type = undef;
454 if (not defined($type)) {
455 $switch_type = $nodiscriminant;
456 } else {
457 my $switch_dt = getType($type);
458 if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
459 $switch_type = Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
460 } elsif ($switch_dt->{DATA}->{TYPE} eq "BITMAP") {
461 $switch_type = Parse::Pidl::Typelist::bitmap_type_fn($switch_dt->{DATA});
462 } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
463 if (defined $e->{SWITCH_TYPE}) {
464 $switch_type = "$e->{SWITCH_TYPE}";
465 } else {
466 $switch_type = "$switch_dt->{DATA}->{NAME}";
467 }
468 }
469 }
470
471 return $switch_type
472 }
473
474 sub Element($$$$$$)
475 {
476 my ($self,$e,$pn,$ifname,$isoruseswitch,%switchvars) = @_;
477
478 my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn, $self->{conformance}->{strip_prefixes})."\_".StripPrefixes($e->{NAME}, $self->{conformance}->{strip_prefixes});
479
480 my ($call_code, $moreparam);
481 my $param = 0;
482 if (defined $isoruseswitch and @$isoruseswitch) {
483 my $type = $isoruseswitch->[0];
484 my $name = $isoruseswitch->[1];
485
486 my $switch_dt = getType($type);
487 my $switch_raw_type = SwitchType($e, $type, "uint32");
488 if (not defined($switch_raw_type)) {
489 die("Unknown type[$type]\n");
490 }
491 my $switch_type = "g${switch_raw_type}";
492
493 if ($name ne "") {
494 $moreparam = ", $switch_type *".$name;
495 } else {
496 $moreparam = "";
497 }
498 if ((not has_property($e, "switch_is")) && ($switchvars{$name}) &&
499 #not a "native" type
500 (!($type =~ /^uint(8|16|1632|32|3264|64)/))) {
501 $param = $name;
502 } elsif ( $switch_dt->{DATA}->{TYPE} eq "ENUM") {
503 $param = $name;
504 } elsif ($name ne "") {
505 $param = "*".$name;
506 }
507
508 if ($name ne "") {
509 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, &$name);";
510 } else {
511 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep);";
512 }
513 } else {
514 $moreparam = "";
515 $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep);";
516 }
517
518
519 my $type = $self->find_type($e->{TYPE});
520
521 if (not defined($type)) {
522 # default settings
523 $type = {
524 MASK => 0,
525 VALSSTRING => "NULL",
526 FT_TYPE => "FT_NONE",
527 BASE_TYPE => "BASE_NONE"
528 };
529 }
530
531 if (ContainsString($e)) {
532 $type = {
533 MASK => 0,
534 VALSSTRING => "NULL",
535 FT_TYPE => "FT_STRING",
536 BASE_TYPE => "BASE_NONE"
537 };
538 }
539 if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) {
540 my $align_flag = $e->{PROPERTIES}->{flag};
541 if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) {
542 $call_code = "ALIGN_TO_$1_BYTES; ".$call_code;
543 }
544 }
545
546 my $hf = $self->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}, "");
547 $self->{hf_used}->{$hf} = 1;
548
549 my $eltname = StripPrefixes($pn, $self->{conformance}->{strip_prefixes}) . ".$e->{NAME}";
550 if (defined($self->{conformance}->{noemit}->{$eltname})) {
551 return $call_code;
552 }
553
554 my $add = "";
555
556 my $oldparam = undef;
557 foreach (@{$e->{LEVELS}}) {
558 if (defined $_->{SWITCH_IS}) {
559 $oldparam = $param;
560 if (($param ne "0") && (!($param =~ /\*/))) {
561 $param = "*$param";
562 }
563 }
564 next if ($_->{TYPE} eq "SWITCH");
565 next if (defined($self->{conformance}->{noemit}->{"$dissectorname$add"}));
566 $self->pidl_def("static int $dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_$moreparam);");
567 if (not defined($self->{conformance}->{manual}->{"$dissectorname$add"})) {
568 $self->pidl_fn_start("$dissectorname$add");
569 $self->pidl_code("static int");
570 $self->pidl_code("$dissectorname$add(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_$moreparam)");
571 $self->pidl_code("{");
572 $self->indent;
573
574 $self->ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname,$param);
575 if (defined $oldparam) {
576 $param = $oldparam;
577 }
578
579 $self->pidl_code("");
580 $self->pidl_code("return offset;");
581 $self->deindent;
582 $self->pidl_code("}\n");
583 $self->pidl_fn_end("$dissectorname$add");
584 }
585 $add.="_";
586 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
587 }
588
589 return $call_code;
590 }
591
592 sub Function($$$)
593 {
594 my ($self, $fn,$ifname) = @_;
595
596 my %dissectornames;
597
598 foreach (@{$fn->{ELEMENTS}}) {
599 $dissectornames{$_->{NAME}} = $self->Element($_, $fn->{NAME}, $ifname, undef, ()) if not defined($dissectornames{$_->{NAME}});
600 }
601
602 my $fn_name = $_->{NAME};
603 $fn_name =~ s/^${ifname}_//;
604
605 $self->PrintIdl(DumpFunction($fn->{ORIGINAL}));
606 $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_response");
607 $self->pidl_code("static int");
608 $self->pidl_code("$ifname\_dissect\_${fn_name}_response(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_)");
609 $self->pidl_code("{");
610 $self->indent;
611 if ( not defined($fn->{RETURN_TYPE})) {
612 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR" or $fn->{RETURN_TYPE} eq "HRESULT")
613 {
614 $self->pidl_code("guint32 status;\n");
615 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
616 if ($type->{DATA}->{TYPE} eq "ENUM") {
617 $self->pidl_code("g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n");
618 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
619 $self->pidl_code("g$fn->{RETURN_TYPE} status;\n");
620 } else {
621 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
622 }
623 } else {
624 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
625 }
626
627 $self->pidl_code("di->dcerpc_procedure_name=\"${fn_name}\";");
628 foreach (@{$fn->{ELEMENTS}}) {
629 if (grep(/out/,@{$_->{DIRECTION}})) {
630 $self->pidl_code("$dissectornames{$_->{NAME}}");
631 $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, di, drep);");
632 $self->pidl_code("");
633 }
634 }
635
636 if (not defined($fn->{RETURN_TYPE})) {
637 } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
638 $self->pidl_code("offset = dissect_ntstatus(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_status, &status);\n");
639 $self->pidl_code("if (status != 0)");
640 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n");
641 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
642 } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
643 $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_werror, &status);\n");
644 $self->pidl_code("if (status != 0)");
645 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, WERR_errors, \"Unknown DOS error 0x%08x\"));\n");
646
647 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
648 } elsif ($fn->{RETURN_TYPE} eq "HRESULT") {
649 $self->pidl_code("offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_hresult, &status);\n");
650 $self->pidl_code("if (status != 0)");
651 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, HRES_errors, \"Unknown HRES error 0x%08x\"));\n");
652 $return_types{$ifname}->{"hresult"} = ["HRESULT", "HRES Windows Error"];
653 } elsif (my $type = getType($fn->{RETURN_TYPE})) {
654 if ($type->{DATA}->{TYPE} eq "ENUM") {
655 my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
656 my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
657
658 $self->pidl_code("offset = $return_dissect(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
659 $self->pidl_code("if (status != 0)");
660 $self->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");
661 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
662 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
663 $self->pidl_code("offset = dissect_ndr_$fn->{RETURN_TYPE}(tvb, offset, pinfo, tree, di, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);");
664 $self->pidl_code("if (status != 0)");
665 $self->pidl_code("\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Status: %d\", status);\n");
666 $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
667 }
668 }
669
670 $self->pidl_code("return offset;");
671 $self->deindent;
672 $self->pidl_code("}\n");
673 $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_response");
674
675 $self->pidl_fn_start("$ifname\_dissect\_$fn_name\_request");
676 $self->pidl_code("static int");
677 $self->pidl_code("$ifname\_dissect\_${fn_name}_request(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_)");
678 $self->pidl_code("{");
679 $self->indent;
680 $self->pidl_code("di->dcerpc_procedure_name=\"${fn_name}\";");
681 foreach (@{$fn->{ELEMENTS}}) {
682 if (grep(/in/,@{$_->{DIRECTION}})) {
683 $self->pidl_code("$dissectornames{$_->{NAME}}");
684 $self->pidl_code("offset = dissect_deferred_pointers(pinfo, tvb, offset, di, drep);");
685 }
686
687 }
688
689 $self->pidl_code("return offset;");
690 $self->deindent;
691 $self->pidl_code("}\n");
692 $self->pidl_fn_end("$ifname\_dissect\_$fn_name\_request");
693 }
694
695 sub Struct($$$$)
696 {
697 my ($self,$e,$name,$ifname) = @_;
698 my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
699
700 return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
701
702 $self->register_ett("ett_$ifname\_$name");
703
704 my $res = "";
705 my $varswitchs = {};
706 # will contain the switch var declaration;
707 my $vars = [];
708 my %switch_hash;
709 foreach (@{$e->{ELEMENTS}}) {
710 if (has_property($_, "switch_is")) {
711 $varswitchs->{$_->{PROPERTIES}->{switch_is}} = [];
712 $switch_hash{ $_->{PROPERTIES}->{switch_is}} = $_->{PROPERTIES}->{switch_is};
713 }
714 }
715 foreach (@{$e->{ELEMENTS}}) {
716 my $switch_info = undef;
717
718 my $v = $_->{NAME};
719 if (scalar(grep {/^$v$/} keys(%$varswitchs)) == 1) {
720 # This element is one of the switch attribute
721 my $switch_raw_type = SwitchType($e, $_->{TYPE}, "uint32");
722 if (not defined($switch_raw_type)) {
723 die("Unknown type[$_->{TYPE}]\n");
724 }
725 my $switch_type = "g${switch_raw_type}";
726
727 if ($switch_type ne "") {
728 push @$vars, "$switch_type $v = 0;";
729 }
730 $switch_info = [ $_->{TYPE}, $v ];
731 $varswitchs->{$v} = $switch_info;
732 }
733
734 if (has_property($_, "switch_is")) {
735 my $varswitch = $_->{PROPERTIES}->{switch_is};
736 $switch_info = $varswitchs->{$varswitch};
737 if (not @$switch_info) {
738 warning($_->{ORIGINAL}, "`$v' switch_is discriminant `$varswitch' not found. (Only single identifiers are supported, not expressions as in MIDL.)");
739 }
740 }
741
742 $res.="\t".$self->Element($_, $name, $ifname, $switch_info, %switch_hash)."\n\n";
743 }
744
745 my $doalign = undef;
746 if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
747 $doalign = 1;
748 } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
749 $doalign = 0;
750 }
751
752 $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_);");
753
754 $self->pidl_fn_start($dissectorname);
755 $self->pidl_code("int");
756 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_)");
757 $self->pidl_code("{");
758 $self->indent;
759 $self->pidl_code($_) foreach (@$vars);
760 $self->pidl_code("proto_item *item = NULL;");
761 if($res) {
762 $self->pidl_code("proto_tree *tree = NULL;");
763 }
764 if (defined($doalign) and $doalign == 0) {
765 $self->pidl_code("bool oldalign = di->no_align;");
766 }
767 $self->pidl_code("int old_offset;");
768 $self->pidl_code("");
769
770 if (defined($doalign)) {
771 if ($doalign == 1) {
772 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
773 }
774 if ($doalign == 0) {
775 $self->pidl_code("di->no_align = true;");
776 }
777 $self->pidl_code("");
778 }
779
780 $self->pidl_code("old_offset = offset;");
781 $self->pidl_code("");
782 $self->pidl_code("if (parent_tree) {");
783 $self->indent;
784 $self->pidl_code("item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, ENC_NA);");
785 if($res) {
786 $self->pidl_code("tree = proto_item_add_subtree(item, ett_$ifname\_$name);");
787 }
788 $self->deindent;
789 $self->pidl_code("}");
790 $self->pidl_code("");
791
792 $self->deindent;
793 $self->pidl_code("$res");
794 $self->indent;
795
796 $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
797 if (defined($doalign) and $doalign == 1) {
798 $self->pidl_code("");
799 $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {");
800 $self->indent;
801 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
802 $self->deindent;
803 $self->pidl_code("}");
804 }
805 if (defined($doalign) and $doalign == 0) {
806 $self->pidl_code("");
807 $self->pidl_code("di->no_align = oldalign;");
808 }
809 $self->pidl_code("");
810 $self->pidl_code("return offset;");
811 $self->deindent;
812 $self->pidl_code("}\n");
813 $self->pidl_fn_end($dissectorname);
814
815 $self->register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,di,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
816 }
817
818 sub Union($$$$)
819 {
820 my ($self,$e,$name,$ifname) = @_;
821
822 my $dissectorname = "$ifname\_dissect_".StripPrefixes($name, $self->{conformance}->{strip_prefixes});
823
824 return if (defined($self->{conformance}->{noemit}->{StripPrefixes($name, $self->{conformance}->{strip_prefixes})}));
825
826 $self->register_ett("ett_$ifname\_$name");
827
828 my $res = "";
829 foreach (@{$e->{ELEMENTS}}) {
830 $res.="\n\t\t$_->{CASE}:\n";
831 if ($_->{TYPE} ne "EMPTY") {
832 $res.="\t\t\t".$self->Element($_, $name, $ifname, undef, ())."\n";
833 }
834 $res.="\t\tbreak;\n";
835 }
836
837 my $switch_type = undef;
838 my $switch_dissect = undef;
839 my $switch_raw_type = SwitchType($e, $e->{SWITCH_TYPE});
840 if (defined($switch_raw_type)) {
841 $switch_type = "g${switch_raw_type}";
842 $switch_dissect = "dissect_ndr_${switch_raw_type}";
843 }
844
845 $self->pidl_fn_start($dissectorname);
846 $self->pidl_code("static int");
847 $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, dcerpc_info* di _U_, uint8_t *drep _U_, int hf_index _U_, uint32_t param _U_)");
848 $self->pidl_code("{");
849 $self->indent;
850 $self->pidl_code("proto_item *item = NULL;");
851 $self->pidl_code("proto_tree *tree = NULL;");
852 $self->pidl_code("int old_offset;");
853 if (!defined $switch_type) {
854 $self->pidl_code("guint32 level = param;");
855 } else {
856 $self->pidl_code("$switch_type level;");
857 }
858 $self->pidl_code("");
859
860 $self->pidl_code("old_offset = offset;");
861 $self->pidl_code("if (parent_tree) {");
862 $self->indent;
863 $self->pidl_code("tree = proto_tree_add_subtree(parent_tree, tvb, offset, -1, ett_$ifname\_$name, &item, \"$name\");");
864 $self->deindent;
865 $self->pidl_code("}");
866
867 $self->pidl_code("");
868
869 if (defined $switch_type) {
870 $self->pidl_code("offset = $switch_dissect(tvb, offset, pinfo, tree, di, drep, hf_index, &level);");
871
872 if ($e->{ALIGN} > 1) {
873 $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
874 $self->pidl_code("");
875 }
876 }
877
878
879 $self->pidl_code("switch(level) {$res\t}");
880 $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
881 $self->pidl_code("");
882
883 $self->pidl_code("return offset;");
884 $self->deindent;
885 $self->pidl_code("}");
886 $self->pidl_fn_end($dissectorname);
887
888 $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
889 }
890
891 sub Const($$$)
892 {
893 my ($self,$const,$ifname) = @_;
894
895 if (!defined($const->{ARRAY_LEN}[0])) {
896 $self->pidl_hdr("#define $const->{NAME}\t( $const->{VALUE} )\n");
897 } else {
898 $self->pidl_hdr("#define $const->{NAME}\t $const->{VALUE}\n");
899 }
900 }
901
902 sub Typedef($$$$)
903 {
904 my ($self,$e,$name,$ifname) = @_;
905
906 $self->Type($e->{DATA}, $name, $ifname);
907 }
908
909 sub Type($$$$)
910 {
911 my ($self, $e, $name, $ifname) = @_;
912
913 $self->PrintIdl(DumpType($e->{ORIGINAL}));
914 {
915 ENUM => \&Enum,
916 STRUCT => \&Struct,
917 UNION => \&Union,
918 BITMAP => \&Bitmap,
919 TYPEDEF => \&Typedef,
920 PIPE => \&Pipe
921 }->{$e->{TYPE}}->($self, $e, $name, $ifname);
922 }
923
924 sub RegisterInterface($$)
925 {
926 my ($self, $x) = @_;
927
928 $self->pidl_fn_start("proto_register_dcerpc_$x->{NAME}");
929 $self->pidl_code("void proto_register_dcerpc_$x->{NAME}(void)");
930 $self->pidl_code("{");
931 $self->indent;
932
933 $self->{res}->{headers} .= "void proto_register_dcerpc_$x->{NAME}(void);\n";
934
935 $self->{res}->{code}.=$self->DumpHfList()."\n";
936 $self->{res}->{code}.="\n".DumpEttList($self->{ett})."\n";
937
938 if (defined($x->{UUID})) {
939 # These can be changed to non-pidl_code names if the old
940 # dissectors in epan/dissectors are deleted.
941
942 my $name = uc($x->{NAME}) . " (pidl)";
943 my $short_name = uc($x->{NAME});
944 my $filter_name = $x->{NAME};
945
946 if (has_property($x, "helpstring")) {
947 $name = $x->{PROPERTIES}->{helpstring};
948 }
949
950 if (defined($self->{conformance}->{protocols}->{$x->{NAME}})) {
951 $short_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{SHORTNAME};
952 $name = $self->{conformance}->{protocols}->{$x->{NAME}}->{LONGNAME};
953 $filter_name = $self->{conformance}->{protocols}->{$x->{NAME}}->{FILTERNAME};
954 }
955
956 $self->pidl_code("proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");");
957
958 $self->pidl_code("proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));");
959 $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
960 } else {
961 $self->pidl_code("proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");");
962 $self->pidl_code("proto_register_field_array(proto_dcerpc, hf, array_length(hf));");
963 $self->pidl_code("proto_register_subtree_array(ett, array_length(ett));");
964 }
965
966 $self->deindent;
967 $self->pidl_code("}\n");
968 $self->pidl_fn_end("proto_register_dcerpc_$x->{NAME}");
969 }
970
971 sub RegisterInterfaceHandoff($$)
972 {
973 my ($self,$x) = @_;
974
975 if (defined($x->{UUID})) {
976 $self->pidl_fn_start("proto_reg_handoff_dcerpc_$x->{NAME}");
977 $self->pidl_code("void proto_reg_handoff_dcerpc_$x->{NAME}(void)");
978 $self->pidl_code("{");
979 $self->indent;
980 $self->pidl_code("dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},");
981 $self->pidl_code("\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},");
982 $self->pidl_code("\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);");
983 $self->deindent;
984 $self->pidl_code("}");
985 $self->pidl_fn_end("proto_reg_handoff_dcerpc_$x->{NAME}");
986
987 $self->{res}->{headers} .= "void proto_reg_handoff_dcerpc_$x->{NAME}(void);\n";
988
989 $self->{hf_used}->{"hf_$x->{NAME}_opnum"} = 1;
990 }
991 }
992
993 sub ProcessInclude
994 {
995 my $self = shift;
996 my @includes = @_;
997 foreach (@includes) {
998 $self->pidl_hdr("#include \"$_\"");
999 }
1000 $self->pidl_hdr("");
1001 }
1002
1003 sub ProcessImport
1004 {
1005 my $self = shift;
1006 my @imports = @_;
1007 foreach (@imports) {
1008 next if($_ eq "security");
1009 s/^\"//;
1010 s/\.idl"?$//;
1011 $self->pidl_hdr("#include \"packet-dcerpc-$_\.h\"");
1012 }
1013 $self->pidl_hdr("");
1014 }
1015
1016 sub ProcessInterface($$)
1017 {
1018 my ($self, $x) = @_;
1019
1020 push(@{$self->{conformance}->{strip_prefixes}}, $x->{NAME});
1021
1022 my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
1023 $self->pidl_hdr("#ifndef $define");
1024 $self->pidl_hdr("#define $define");
1025 $self->pidl_hdr("");
1026
1027 $self->pidl_def("static int proto_dcerpc_$x->{NAME};");
1028 $self->register_ett("ett_dcerpc_$x->{NAME}");
1029 $self->register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
1030
1031 if (defined($x->{UUID})) {
1032 my $if_uuid = $x->{UUID};
1033
1034 $self->pidl_def("/* Version information */\n\n");
1035
1036 $self->pidl_def("static e_guid_t uuid_dcerpc_$x->{NAME} = {");
1037 $self->pidl_def("\t0x" . substr($if_uuid, 1, 8)
1038 . ", 0x" . substr($if_uuid, 10, 4)
1039 . ", 0x" . substr($if_uuid, 15, 4) . ",");
1040 $self->pidl_def("\t{ 0x" . substr($if_uuid, 20, 2)
1041 . ", 0x" . substr($if_uuid, 22, 2)
1042 . ", 0x" . substr($if_uuid, 25, 2)
1043 . ", 0x" . substr($if_uuid, 27, 2)
1044 . ", 0x" . substr($if_uuid, 29, 2)
1045 . ", 0x" . substr($if_uuid, 31, 2)
1046 . ", 0x" . substr($if_uuid, 33, 2)
1047 . ", 0x" . substr($if_uuid, 35, 2) . " }");
1048 $self->pidl_def("};");
1049
1050 my $maj = 0x0000FFFF & $x->{VERSION};
1051 $maj =~ s/\.(.*)$//g;
1052 $self->pidl_def("static uint16_t ver_dcerpc_$x->{NAME} = $maj;");
1053 $self->pidl_def("");
1054 }
1055
1056 $return_types{$x->{NAME}} = {};
1057
1058 $self->Interface($x);
1059 $self->pidl_code("\n".DumpFunctionTable($x));
1060
1061 foreach (sort(keys %{$return_types{$x->{NAME}}})) {
1062 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
1063 my $dt = $self->find_type($type);
1064 $dt or die("Unable to find information about return type `$type'");
1065 $self->register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
1066 $self->{hf_used}->{"hf_$x->{NAME}_$_"} = 1;
1067 }
1068
1069 $self->RegisterInterface($x);
1070 $self->RegisterInterfaceHandoff($x);
1071
1072 if (exists ($self->{conformance}->{header})) {
1073 $self->pidl_hdr($self->{conformance}->{header});
1074 }
1075
1076 $self->pidl_hdr("#endif /* $define */");
1077 }
1078
1079 sub find_type($$)
1080 {
1081 my ($self, $n) = @_;
1082
1083 return $self->{conformance}->{types}->{$n};
1084 }
1085
1086 sub register_type($$$$$$$$)
1087 {
1088 my ($self, $type,$call,$ft,$base,$mask,$vals,$length) = @_;
1089
1090 return if (defined($self->{conformance}->{types}->{$type}));
1091
1092 $self->{conformance}->{types}->{$type} = {
1093 NAME => $type,
1094 DISSECTOR_NAME => $call,
1095 FT_TYPE => $ft,
1096 BASE_TYPE => $base,
1097 MASK => $mask,
1098 VALSSTRING => $vals,
1099 ALIGNMENT => $length
1100 };
1101 }
1102
1103 # Loads the default types
1104 sub Initialize($$)
1105 {
1106 my ($self, $cnf_file) = @_;
1107
1108 $self->{conformance} = {
1109 imports => {},
1110 header_fields=> {}
1111 };
1112
1113 ReadConformance($cnf_file, $self->{conformance}) or print STDERR "warning: No conformance file `$cnf_file'\n";
1114
1115 foreach my $bytes (qw(1 2 4 8)) {
1116 my $bits = $bytes * 8;
1117 $self->register_type("uint$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
1118 $self->register_type("int$bits", "offset = PIDL_dissect_uint$bits(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
1119 }
1120
1121 $self->register_type("uint3264", "offset = dissect_ndr_uint3264(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT32", "BASE_DEC", 0, "NULL", 8);
1122 $self->register_type("hyper", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 8);
1123 $self->register_type("int64", "offset = dissect_ndr_uint64(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_INT64", "BASE_DEC", 0, "NULL", 8);
1124 $self->register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
1125 $self->register_type("bool8", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1126 $self->register_type("char", "offset = PIDL_dissect_uint8(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
1127 $self->register_type("long", "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
1128 $self->register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
1129 $self->register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, di, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
1130 $self->register_type("policy_handle", "offset = PIDL_dissect_policy_hnd(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_BYTES", "BASE_NONE", 0, "NULL", 4);
1131 $self->register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1132 $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1133 $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, di, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1134 $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, di, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
1135 $self->register_type("dom_sid28",
1136 "offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, di, drep, \@HF\@);", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
1137 $self->register_type("SID",
1138 "offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, di, drep, param, \@HF\@);","FT_STRING", "BASE_NONE", 0, "NULL", 4);
1139 $self->register_type("WERROR",
1140 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
1141 $self->register_type("NTSTATUS",
1142 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
1143 $self->register_type("HRESULT",
1144 "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, di, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(HRES_errors)", 4);
1145 $self->register_type("ipv6address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 16, ENC_NA); offset += 16;", "FT_IPv6", "BASE_NONE", 0, "NULL", 16);
1146 $self->register_type("ipv4address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 4, ENC_BIG_ENDIAN); offset += 4;", "FT_IPv4", "BASE_NONE", 0, "NULL", 4);
1147
1148 }
1149
1150 #####################################################################
1151 # Generate Wireshark parser and header code
1152 sub Parse($$$$$)
1153 {
1154 my($self,$ndr,$idl_file,$h_filename,$cnf_file) = @_;
1155
1156 $self->Initialize($cnf_file);
1157
1158 return (undef, undef) if defined($self->{conformance}->{noemit_dissector});
1159
1160 my $notice =
1161 "/* DO NOT EDIT
1162 This file was automatically generated by Pidl
1163 from $idl_file and $cnf_file.
1164
1165 Pidl is a perl based IDL compiler for DCE/RPC idl files.
1166 It is maintained by the Samba team, not the Wireshark team.
1167 Instructions on how to download and install Pidl can be
1168 found at https://wiki.wireshark.org/Pidl
1169 */
1170
1171 ";
1172
1173 $self->{res}->{headers} = "\n";
1174 $self->{res}->{headers} .= "#include \"config.h\"\n";
1175
1176 $self->{res}->{headers} .= "#include <glib.h>\n";
1177 $self->{res}->{headers} .= "#include <string.h>\n";
1178 $self->{res}->{headers} .= "#include <epan/packet.h>\n\n";
1179
1180 $self->{res}->{headers} .= "#include \"packet-dcerpc.h\"\n";
1181 $self->{res}->{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
1182 $self->{res}->{headers} .= "#include \"packet-windows-common.h\"\n";
1183
1184 my $h_basename = basename($h_filename);
1185
1186 $self->{res}->{headers} .= "#include \"$h_basename\"\n";
1187 $self->pidl_code("");
1188
1189 if (defined($self->{conformance}->{ett})) {
1190 register_ett($self,$_) foreach(@{$self->{conformance}->{ett}})
1191 }
1192
1193 # Wireshark protocol registration
1194
1195 foreach (@$ndr) {
1196 $self->ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
1197 $self->ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
1198 $self->ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
1199 }
1200
1201 $self->{res}->{ett} = DumpEttDeclaration($self->{ett});
1202 $self->{res}->{hf} = $self->DumpHfDeclaration();
1203
1204 my $parser = $notice;
1205 $parser.= $self->{res}->{headers};
1206 $parser.=$self->{res}->{ett};
1207 $parser.=$self->{res}->{hf};
1208 $parser.=$self->{res}->{def};
1209 if (exists ($self->{conformance}->{override})) {
1210 $parser.=$self->{conformance}->{override};
1211 }
1212 $parser.=$self->{res}->{code};
1213
1214 my $header = $notice;
1215 $header.=$self->{res}->{hdr};
1216
1217 $self->CheckUsed($self->{conformance});
1218
1219 return ($parser,$header);
1220 }
1221
1222 ###############################################################################
1223 # ETT
1224 ###############################################################################
1225
1226 sub register_ett($$)
1227 {
1228 my ($self, $name) = @_;
1229
1230 push (@{$self->{ett}}, $name);
1231 }
1232
1233 sub DumpEttList
1234 {
1235 my ($ett) = @_;
1236 my $res = "\tstatic int *ett[] = {\n";
1237 foreach (@$ett) {
1238 $res .= "\t\t&$_,\n";
1239 }
1240
1241 return "$res\t};\n";
1242 }
1243
1244 sub DumpEttDeclaration
1245 {
1246 my ($ett) = @_;
1247 my $res = "\n/* Ett declarations */\n";
1248 foreach (@$ett) {
1249 $res .= "static int $_;\n";
1250 }
1251
1252 return "$res\n";
1253 }
1254
1255 ###############################################################################
1256 # HF
1257 ###############################################################################
1258
1259 sub register_hf_field($$$$$$$$$)
1260 {
1261 my ($self,$index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
1262
1263 if (defined ($self->{conformance}->{hf_renames}->{$index})) {
1264 $self->{conformance}->{hf_renames}->{$index}->{USED} = 1;
1265 return $self->{conformance}->{hf_renames}->{$index}->{NEWNAME};
1266 }
1267
1268 $self->{conformance}->{header_fields}->{$index} = {
1269 INDEX => $index,
1270 NAME => $name,
1271 FILTER => $filter_name,
1272 FT_TYPE => $ft_type,
1273 BASE_TYPE => $base_type,
1274 VALSSTRING => $valsstring,
1275 MASK => $mask,
1276 BLURB => $blurb
1277 };
1278
1279 if ((not defined($blurb) or $blurb eq "") and
1280 defined($self->{conformance}->{fielddescription}->{$index})) {
1281 $self->{conformance}->{header_fields}->{$index}->{BLURB} =
1282 $self->{conformance}->{fielddescription}->{$index}->{DESCRIPTION};
1283 $self->{conformance}->{fielddescription}->{$index}->{USED} = 1;
1284 }
1285
1286 return $index;
1287 }
1288
1289 sub change_hf_field_type($$$$)
1290 {
1291 my ($self,$index,$ft_type,$base_type) = @_;
1292 if (defined ($self->{conformance}->{hf_renames}->{$index})) {
1293 print "Field $index has been renamed to ".$self->{conformance}->{hf_renames}->{$index}->{NEWNAME}." you can't change it's type";
1294 return 0;
1295 }
1296
1297 if (!defined ($self->{conformance}->{header_fields}->{$index})) {
1298 print "Field $index doesn't exists";
1299 return 0;
1300 }
1301 $self->{conformance}->{header_fields}->{$index}->{FT_TYPE} = $ft_type;
1302 $self->{conformance}->{header_fields}->{$index}->{BASE_TYPE} = $base_type;
1303 return 1;
1304 }
1305
1306 sub DumpHfDeclaration($)
1307 {
1308 my ($self) = @_;
1309 my $res = "";
1310
1311 $res = "\n/* Header field declarations */\n";
1312
1313 foreach (sort(keys %{$self->{conformance}->{header_fields}}))
1314 {
1315 $res .= "static int $_;\n";
1316 }
1317
1318 return "$res\n";
1319 }
1320
1321 sub make_str_or_null($)
1322 {
1323 my $str = shift;
1324 if (substr($str, 0, 1) eq "\"") {
1325 $str = substr($str, 1, length($str)-2);
1326 }
1327 $str =~ s/^\s*//;
1328 $str =~ s/\s*$//;
1329 if ($str eq "") {
1330 return "NULL";
1331 }
1332 return make_str($str);
1333 }
1334
1335 sub DumpHfList($)
1336 {
1337 my ($self) = @_;
1338 my $res = "\tstatic hf_register_info hf[] = {\n";
1339
1340 foreach (sort {$a->{INDEX} cmp $b->{INDEX}} values %{$self->{conformance}->{header_fields}})
1341 {
1342 $res .= "\t{ &$_->{INDEX},\n".
1343 "\t { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str_or_null($_->{BLURB}).", HFILL }},\n";
1344 }
1345
1346 return $res."\t};\n";
1347 }
1348
1349
1350 ###############################################################################
1351 # Function table
1352 ###############################################################################
1353
1354 sub DumpFunctionTable($)
1355 {
1356 my $if = shift;
1357
1358 my $res = "static const dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1359 foreach (@{$if->{FUNCTIONS}}) {
1360 my $fn_name = $_->{NAME};
1361 $fn_name =~ s/^$if->{NAME}_//;
1362 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1363 $res.= "\t $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1364 }
1365
1366 $res .= "\t{ 0, NULL, NULL, NULL }\n";
1367
1368 return "$res};\n";
1369 }
1370
1371 sub CheckUsed($$)
1372 {
1373 my ($self, $conformance) = @_;
1374
1375 # Check if some word appears inside a CODE block
1376 # This has false positives if a word appears within a comment.
1377 # We could use Regexp::Common to remove comments before searching.
1378 local *CheckCode = sub {
1379 my $name = shift;
1380 return (exists ($conformance->{override}) and ($conformance->{override} =~ /\b$name\b/));
1381 };
1382
1383 foreach (values %{$conformance->{header_fields}}) {
1384 if (not defined($self->{hf_used}->{$_->{INDEX}})) {
1385 if (not CheckCode($_->{INDEX})) {
1386 warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1387 }
1388 }
1389 }
1390
1391 foreach (values %{$conformance->{hf_renames}}) {
1392 if (not $_->{USED}) {
1393 warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1394 }
1395 }
1396
1397 foreach (values %{$conformance->{dissectorparams}}) {
1398 if (not $_->{USED}) {
1399 warning($_->{POS}, "dissector param never used");
1400 }
1401 }
1402
1403 foreach (values %{$conformance->{imports}}) {
1404 if (not $_->{USED}) {
1405 warning($_->{POS}, "import never used");
1406 }
1407 }
1408
1409 foreach (values %{$conformance->{types}}) {
1410 if (not $_->{USED} and defined($_->{POS})) {
1411 warning($_->{POS}, "type never used");
1412 }
1413 }
1414
1415 foreach (values %{$conformance->{fielddescription}}) {
1416 if (not $_->{USED}) {
1417 warning($_->{POS}, "description never used");
1418 }
1419 }
1420
1421 foreach (values %{$conformance->{tfs}}) {
1422 if (not $_->{USED}) {
1423 warning($_->{POS}, "True/False description never used");
1424 }
1425 }
1426 }
1427
1428 1;