9526d76a3729008579b661f14f457fc515d8ac2c
[samba.git] / source4 / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
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
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 strict;
20 use Parse::Pidl qw(error);
21 use Parse::Pidl::Typelist qw(getType);
22 use Parse::Pidl::Util qw(has_property ParseExpr property_matches make_str);
23 use Parse::Pidl::NDR qw(ContainsString GetNextLevel);
24 use Parse::Pidl::Dump qw(DumpTypedef DumpFunction);
25 use Parse::Pidl::Wireshark::Conformance qw(ReadConformance);
26 use File::Basename;     
27
28 use vars qw($VERSION);
29 $VERSION = '0.01';
30
31 my @ett;
32
33 my %hf_used = ();
34 my %return_types = ();
35 my %dissector_used = ();
36
37 my $conformance = undef;
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) = @_;
48
49         foreach (@{$conformance->{strip_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;          # Replace underscores with spaces
64     $field =~ s/(\w+)/\u\L$1/g; # Capitalise each word
65     
66     return $field;
67 }
68
69 my %res = ();
70 my $tabs = "";
71 my $cur_fn = undef;
72 sub pidl_fn_start($)
73 {
74         my $fn = shift;
75         $cur_fn = $fn;
76 }
77 sub pidl_fn_end($)
78 {
79         my $fn = shift;
80         die("Inconsistent state: $fn != $cur_fn") if ($fn ne $cur_fn);
81         $cur_fn = undef;
82 }
83
84 sub pidl_code($)
85 {
86         my $d = shift;
87         return if (defined($cur_fn) and defined($conformance->{manual}->{$cur_fn}));
88  
89         if ($d) {
90                 $res{code} .= $tabs;
91                 $res{code} .= $d;
92         }
93         $res{code} .="\n";
94 }
95
96 sub pidl_hdr($) { my $x = shift; $res{hdr} .= "$x\n"; }
97 sub pidl_def($) { my $x = shift; $res{def} .= "$x\n"; }
98
99 sub indent()
100 {
101         $tabs .= "\t";
102 }
103
104 sub deindent()
105 {
106         $tabs = substr($tabs, 0, -1);
107 }
108
109 sub PrintIdl($)
110 {
111         my $idl = shift;
112
113         foreach (split /\n/, $idl) {
114                 pidl_code "/* IDL: $_ */";
115         }
116
117         pidl_code "";
118 }
119
120 #####################################################################
121 # parse the interface definitions
122 sub Interface($)
123 {
124         my($interface) = @_;
125         Const($_,$interface->{NAME}) foreach (@{$interface->{CONSTS}});
126         Typedef($_,$interface->{NAME}) foreach (@{$interface->{TYPES}});
127         Function($_,$interface->{NAME}) foreach (@{$interface->{FUNCTIONS}});
128 }
129
130 sub Enum($$$)
131 {
132         my ($e,$name,$ifname) = @_;
133         my $valsstring = "$ifname\_$name\_vals";
134         my $dissectorname = "$ifname\_dissect\_enum\_".StripPrefixes($name);
135
136         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
137
138         foreach (@{$e->{ELEMENTS}}) {
139                 if (/([^=]*)=(.*)/) {
140                         pidl_hdr "#define $1 ($2)";
141                 }
142         }
143         
144         pidl_hdr "extern const value_string $valsstring\[];";
145         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param);";
146
147         pidl_def "const value_string ".$valsstring."[] = {";
148         foreach (@{$e->{ELEMENTS}}) {
149                 next unless (/([^=]*)=(.*)/);
150                 pidl_def "\t{ $1, \"$1\" },";
151         }
152
153         pidl_def "{ 0, NULL }";
154         pidl_def "};";
155
156         pidl_fn_start $dissectorname;
157         pidl_code "int";
158         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 *param)";
159         pidl_code "{";
160         indent;
161         pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, param);";
162         pidl_code "return offset;";
163         deindent;
164         pidl_code "}\n";
165         pidl_fn_end $dissectorname;
166
167         my $enum_size = $e->{BASE_TYPE};
168         $enum_size =~ s/uint//g;
169         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
170 }
171
172 sub Bitmap($$$)
173 {
174         my ($e,$name,$ifname) = @_;
175         my $dissectorname = "$ifname\_dissect\_bitmap\_".StripPrefixes($name);
176
177         register_ett("ett_$ifname\_$name");
178
179         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep, int hf_index, guint32 param);";
180
181         pidl_fn_start $dissectorname;
182         pidl_code "int";
183         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
184         pidl_code "{";
185         indent;
186         pidl_code "proto_item *item = NULL;";
187         pidl_code "proto_tree *tree = NULL;";
188         pidl_code "";
189                 
190         pidl_code "g$e->{BASE_TYPE} flags;";
191         if ($e->{ALIGN} > 1) {
192                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
193         }
194
195         pidl_code "";
196
197         pidl_code "if (parent_tree) {";
198         indent;
199         pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, $e->{ALIGN}, TRUE);";
200         pidl_code "tree = proto_item_add_subtree(item,ett_$ifname\_$name);";
201         deindent;
202         pidl_code "}\n";
203
204         pidl_code "offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, NULL, drep, -1, &flags);";
205
206         pidl_code "proto_item_append_text(item, \": \");\n";
207         pidl_code "if (!flags)";
208         pidl_code "\tproto_item_append_text(item, \"(No values set)\");\n";
209
210         foreach (@{$e->{ELEMENTS}}) {
211                 next unless (/([^ ]*) (.*)/);
212                 my ($en,$ev) = ($1,$2);
213                 my $hf_bitname = "hf_$ifname\_$name\_$en";
214                 my $filtername = "$ifname\.$name\.$en";
215
216                 $hf_used{$hf_bitname} = 1;
217                 
218                 register_hf_field($hf_bitname, field2name($en), $filtername, "FT_BOOLEAN", $e->{ALIGN} * 8, "TFS(&$name\_$en\_tfs)", $ev, "");
219
220                 pidl_def "static const true_false_string $name\_$en\_tfs = {";
221                 if (defined($conformance->{tfs}->{$hf_bitname})) {
222                         pidl_def "   $conformance->{tfs}->{$hf_bitname}->{TRUE_STRING},";
223                         pidl_def "   $conformance->{tfs}->{$hf_bitname}->{FALSE_STRING},";
224                         $conformance->{tfs}->{$hf_bitname}->{USED} = 1;
225                 } else {
226                         pidl_def "   \"$en is SET\",";
227                         pidl_def "   \"$en is NOT SET\",";
228                 }
229                 pidl_def "};";
230                 
231                 pidl_code "proto_tree_add_boolean(tree, $hf_bitname, tvb, offset-$e->{ALIGN}, $e->{ALIGN}, flags);";
232                 pidl_code "if (flags&$ev){";
233                 pidl_code "\tproto_item_append_text(item, \"$en\");";
234                 pidl_code "\tif (flags & (~$ev))";
235                 pidl_code "\t\tproto_item_append_text(item, \", \");";
236                 pidl_code "}";
237                 pidl_code "flags&=(~$ev);";
238                 pidl_code "";
239         }
240
241         pidl_code "if (flags) {";
242         pidl_code "\tproto_item_append_text(item, \"Unknown bitmap value 0x%x\", flags);";
243         pidl_code "}\n";
244         pidl_code "return offset;";
245         deindent;
246         pidl_code "}\n";
247         pidl_fn_end $dissectorname;
248
249         my $size = $e->{BASE_TYPE};
250         $size =~ s/uint//g;
251         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$size", "BASE_HEX", "0", "NULL", $size/8);
252 }
253
254 sub ElementLevel($$$$$$)
255 {
256         my ($e,$l,$hf,$myname,$pn,$ifname) = @_;
257
258         my $param = 0;
259
260         if (defined($conformance->{dissectorparams}->{$myname})) {
261                 $param = $conformance->{dissectorparams}->{$myname}->{PARAM};
262         }
263
264         if ($l->{TYPE} eq "POINTER") {
265                 my $type;
266                 if ($l->{LEVEL} eq "TOP") {
267                         $type = "toplevel";
268                 } elsif ($l->{LEVEL} eq "EMBEDDED") {
269                         $type = "embedded";
270                 }
271                 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);";
272         } elsif ($l->{TYPE} eq "ARRAY") {
273                 if ($l->{IS_INLINE}) {
274                         error($e->{ORIGINAL}, "Inline arrays not supported");
275                 } elsif ($l->{IS_FIXED}) {
276                         pidl_code "int i;";
277                         pidl_code "for (i = 0; i < $l->{SIZE_IS}; i++)";
278                         pidl_code "\toffset = $myname\_(tvb, offset, pinfo, tree, drep);";
279                 } else {
280                         my $type = "";
281                         $type .= "c" if ($l->{IS_CONFORMANT});
282                         $type .= "v" if ($l->{IS_VARYING});
283
284                         unless ($l->{IS_ZERO_TERMINATED}) {
285                                 pidl_code "offset = dissect_ndr_u" . $type . "array(tvb, offset, pinfo, tree, drep, $myname\_);";
286                         } else {
287                                 my $nl = GetNextLevel($e,$l);
288                                 pidl_code "char *data;";
289                                 pidl_code "";
290                                 pidl_code "offset = dissect_ndr_$type" . "string(tvb, offset, pinfo, tree, drep, sizeof(g$nl->{DATA_TYPE}), $hf, FALSE, &data);";
291                                 pidl_code "proto_item_append_text(tree, \": %s\", data);";
292                         }
293                 }
294         } elsif ($l->{TYPE} eq "DATA") {
295                 if ($l->{DATA_TYPE} eq "string") {
296                         my $bs = 2; # Byte size defaults to that of UCS2
297
298
299                         ($bs = 1) if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_ASCII.*"));
300                         
301                         if (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*") and property_matches($e, "flag", ".*LIBNDR_FLAG_STR_LEN4.*")) {
302                                 pidl_code "char *data;\n";
303                                 pidl_code "offset = dissect_ndr_cvstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, &data);";
304                                 pidl_code "proto_item_append_text(tree, \": %s\", data);";
305                         } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_STR_SIZE4.*")) {
306                                 pidl_code "offset = dissect_ndr_vstring(tvb, offset, pinfo, tree, drep, $bs, $hf, FALSE, NULL);";
307                         } else {
308                                 warn("Unable to handle string with flags $e->{PROPERTIES}->{flag}");
309                         }
310                 } else {
311                         my $call;
312
313                         if ($conformance->{imports}->{$l->{DATA_TYPE}}) {
314                                 $call = $conformance->{imports}->{$l->{DATA_TYPE}}->{DATA};     
315                                 $conformance->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
316                         } elsif (defined($conformance->{imports}->{"$pn.$e->{NAME}"})) {
317                                 $call = $conformance->{imports}->{"$pn.$e->{NAME}"}->{DATA};
318                                 $conformance->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
319                             
320                         } elsif (defined($conformance->{types}->{$l->{DATA_TYPE}})) {
321                                 $call= $conformance->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
322                                 $conformance->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
323                         } else {
324                                 pidl_code "offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);";
325
326                                 return;
327                         }
328
329                         $call =~ s/\@HF\@/$hf/g;
330                         $call =~ s/\@PARAM\@/$param/g;
331                         pidl_code "$call";
332                 }
333         } elsif ($_->{TYPE} eq "SUBCONTEXT") {
334                 my $num_bits = ($l->{HEADER_SIZE}*8);
335                 pidl_code "guint$num_bits size;";
336                 pidl_code "int start_offset = offset;";
337                 pidl_code "tvbuff_t *subtvb;";
338                 pidl_code "offset = dissect_ndr_uint$num_bits(tvb, offset, pinfo, tree, drep, $hf, &size);";
339                 pidl_code "proto_tree_add_text(tree, tvb, start_offset, offset - start_offset + size, \"Subcontext size\");";
340
341                 pidl_code "subtvb = tvb_new_subset(tvb, offset, size, -1);";
342                 pidl_code "$myname\_(subtvb, 0, pinfo, tree, drep);";
343         } else {
344                 die("Unknown type `$_->{TYPE}'");
345         }
346 }
347
348 sub Element($$$)
349 {
350         my ($e,$pn,$ifname) = @_;
351
352         my $dissectorname = "$ifname\_dissect\_element\_".StripPrefixes($pn)."\_".StripPrefixes($e->{NAME});
353
354         my $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep);";
355
356         my $type = find_type($e->{TYPE});
357
358         if (not defined($type)) {
359                 # default settings
360                 $type = {
361                         MASK => 0,
362                         VALSSTRING => "NULL",
363                         FT_TYPE => "FT_NONE",
364                         BASE_TYPE => "BASE_HEX"
365                 };
366         }
367
368         if (ContainsString($e)) {
369                 $type = {
370                         MASK => 0,
371                         VALSSTRING => "NULL",
372                         FT_TYPE => "FT_STRING",
373                         BASE_TYPE => "BASE_DEC"
374                 };
375         }
376
377         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}, "");
378         $hf_used{$hf} = 1;
379
380         my $eltname = StripPrefixes($pn) . ".$e->{NAME}";
381         if (defined($conformance->{noemit}->{$eltname})) {
382                 return $call_code;
383         }
384
385         my $add = "";
386
387         foreach (@{$e->{LEVELS}}) {
388                 next if ($_->{TYPE} eq "SWITCH");
389                 pidl_def "static int $dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep);";
390                 pidl_fn_start "$dissectorname$add";
391                 pidl_code "static int";
392                 pidl_code "$dissectorname$add(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *tree, guint8 *drep)";
393                 pidl_code "{";
394                 indent;
395
396                 ElementLevel($e,$_,$hf,$dissectorname.$add,$pn,$ifname);
397
398                 pidl_code "";
399                 pidl_code "return offset;";
400                 deindent;
401                 pidl_code "}\n";
402                 pidl_fn_end "$dissectorname$add";
403                 $add.="_";
404                 last if ($_->{TYPE} eq "ARRAY" and $_->{IS_ZERO_TERMINATED});
405         }
406
407         return $call_code;
408 }
409
410 sub Function($$$)
411 {
412         my ($fn,$ifname) = @_;
413
414         my %dissectornames;
415
416         foreach (@{$fn->{ELEMENTS}}) {
417             $dissectornames{$_->{NAME}} = Element($_, $fn->{NAME}, $ifname) if not defined($dissectornames{$_->{NAME}});
418         }
419         
420         my $fn_name = $_->{NAME};
421         $fn_name =~ s/^${ifname}_//;
422
423         PrintIdl DumpFunction($fn->{ORIGINAL});
424         pidl_fn_start "$ifname\_dissect\_$fn_name\_response";
425         pidl_code "static int";
426         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_)";
427         pidl_code "{";
428         indent;
429         if ( not defined($fn->{RETURN_TYPE})) {
430         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS" or $fn->{RETURN_TYPE} eq "WERROR") 
431         {
432                 pidl_code "guint32 status;\n";
433         } elsif (my $type = getType($fn->{RETURN_TYPE})) {
434                 if ($type->{DATA}->{TYPE} eq "ENUM") {
435                         pidl_code "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA}) . " status;\n";
436                 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
437                         pidl_code "g$fn->{RETURN_TYPE} status;\n";
438                 } else {
439                 error($fn, "return type `$fn->{RETURN_TYPE}' not yet supported");
440                 }
441         } else {
442                 error($fn, "unknown return type `$fn->{RETURN_TYPE}'");
443         }
444
445         foreach (@{$fn->{ELEMENTS}}) {
446                 if (grep(/out/,@{$_->{DIRECTION}})) {
447                         pidl_code "$dissectornames{$_->{NAME}}";
448                         pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
449                         pidl_code "";
450                 }
451         }
452
453         if (not defined($fn->{RETURN_TYPE})) {
454         } elsif ($fn->{RETURN_TYPE} eq "NTSTATUS") {
455                 pidl_code "offset = dissect_ntstatus(tvb, offset, pinfo, tree, drep, hf\_$ifname\_status, &status);\n";
456                 pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
457                 pidl_code "\tcol_append_fstr(pinfo->cinfo, COL_INFO, \", Error: %s\", val_to_str(status, NT_errors, \"Unknown NT status 0x%08x\"));\n";
458                 $return_types{$ifname}->{"status"} = ["NTSTATUS", "NT Error"];
459         } elsif ($fn->{RETURN_TYPE} eq "WERROR") {
460                 pidl_code "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, hf\_$ifname\_werror, &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, WERR_errors, \"Unknown DOS error 0x%08x\"));\n";
463                 
464                 $return_types{$ifname}->{"werror"} = ["WERROR", "Windows Error"];
465         } elsif (my $type = getType($fn->{RETURN_TYPE})) {
466                 if ($type->{DATA}->{TYPE} eq "ENUM") {
467                         my $return_type = "g".Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
468                         my $return_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($type->{DATA});
469
470                         pidl_code "offset = $return_dissect(tvb, offset, pinfo, tree, drep, hf\_$ifname\_$fn->{RETURN_TYPE}_status, &status);";
471                         pidl_code "if (status != 0 && check_col(pinfo->cinfo, COL_INFO))";
472                         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";
473                         $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
474                 } elsif ($type->{DATA}->{TYPE} eq "SCALAR") {
475                         pidl_code "offset = dissect_ndr_$fn->{RETURN_TYPE}(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: %d\", status);\n";
478                         $return_types{$ifname}->{$fn->{RETURN_TYPE}."_status"} = [$fn->{RETURN_TYPE}, $fn->{RETURN_TYPE}];
479                 }
480         }
481                 
482
483         pidl_code "return offset;";
484         deindent;
485         pidl_code "}\n";
486         pidl_fn_end "$ifname\_dissect\_$fn_name\_response";
487
488         pidl_fn_start "$ifname\_dissect\_$fn_name\_request";
489         pidl_code "static int";
490         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_)";
491         pidl_code "{";
492         indent;
493         foreach (@{$fn->{ELEMENTS}}) {
494                 if (grep(/in/,@{$_->{DIRECTION}})) {
495                         pidl_code "$dissectornames{$_->{NAME}}";
496                         pidl_code "offset = dissect_deferred_pointers(pinfo, tvb, offset, drep);";
497                 }
498
499         }
500
501         pidl_code "return offset;";
502         deindent;
503         pidl_code "}\n";
504         pidl_fn_end "$ifname\_dissect\_$fn_name\_request";
505 }
506
507 sub Struct($$$)
508 {
509         my ($e,$name,$ifname) = @_;
510         my $dissectorname = "$ifname\_dissect\_struct\_".StripPrefixes($name);
511
512         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
513
514         register_ett("ett_$ifname\_$name");
515
516         my $res = "";
517         ($res.="\t".Element($_, $name, $ifname)."\n\n") foreach (@{$e->{ELEMENTS}});
518
519         pidl_hdr "int $dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_);";
520
521         pidl_fn_start $dissectorname;
522         pidl_code "int";
523         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
524         pidl_code "{";
525         indent;
526         pidl_code "proto_item *item = NULL;";
527         pidl_code "proto_tree *tree = NULL;";
528         pidl_code "int old_offset;";
529         pidl_code "";
530
531         if ($e->{ALIGN} > 1) {
532                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
533         }
534         pidl_code "";
535
536         pidl_code "old_offset = offset;";
537         pidl_code "";
538         pidl_code "if (parent_tree) {";
539         indent;
540         pidl_code "item = proto_tree_add_item(parent_tree, hf_index, tvb, offset, -1, TRUE);";
541         pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
542         deindent;
543         pidl_code "}";
544
545         pidl_code "\n$res";
546
547         pidl_code "proto_item_set_len(item, offset-old_offset);\n";
548         pidl_code "return offset;";
549         deindent;
550         pidl_code "}\n";
551         pidl_fn_end $dissectorname;
552
553         register_type($name, "offset = $dissectorname(tvb,offset,pinfo,tree,drep,\@HF\@,\@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
554 }
555
556 sub Union($$$)
557 {
558         my ($e,$name,$ifname) = @_;
559
560         my $dissectorname = "$ifname\_dissect_".StripPrefixes($name);
561
562         return if (defined($conformance->{noemit}->{StripPrefixes($name)}));
563         
564         register_ett("ett_$ifname\_$name");
565
566         my $res = "";
567         foreach (@{$e->{ELEMENTS}}) {
568                 $res.="\n\t\t$_->{CASE}:\n";
569                 if ($_->{TYPE} ne "EMPTY") {
570                         $res.="\t\t\t".Element($_, $name, $ifname)."\n";
571                 }
572                 $res.="\t\tbreak;\n";
573         }
574
575         my $switch_type;
576         my $switch_dissect;
577         my $switch_dt = getType($e->{SWITCH_TYPE});
578         if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
579                 $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
580                 $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
581         } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
582                 $switch_type = "g$e->{SWITCH_TYPE}";
583                 $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
584         }
585
586         pidl_fn_start $dissectorname;
587         pidl_code "static int";
588         pidl_code "$dissectorname(tvbuff_t *tvb, int offset, packet_info *pinfo, proto_tree *parent_tree, guint8 *drep, int hf_index, guint32 param _U_)";
589         pidl_code "{";
590         indent;
591         pidl_code "proto_item *item = NULL;";
592         pidl_code "proto_tree *tree = NULL;";
593         pidl_code "int old_offset;";
594         pidl_code "$switch_type level;";
595         pidl_code "";
596
597         if ($e->{ALIGN} > 1) {
598                 pidl_code "ALIGN_TO_$e->{ALIGN}_BYTES;";
599         }
600
601         pidl_code "";
602
603         pidl_code "old_offset = offset;";
604         pidl_code "if (parent_tree) {";
605         indent;
606         pidl_code "item = proto_tree_add_text(parent_tree, tvb, offset, -1, \"$name\");";
607         pidl_code "tree = proto_item_add_subtree(item, ett_$ifname\_$name);";
608         deindent;
609         pidl_code "}";
610
611         pidl_code "";
612
613         pidl_code "offset = $switch_dissect(tvb, offset, pinfo, tree, drep, hf_index, &level);";
614
615         pidl_code "switch(level) {$res\t}";
616         pidl_code "proto_item_set_len(item, offset-old_offset);\n";
617         pidl_code "return offset;";
618         deindent;
619         pidl_code "}";
620         pidl_fn_end $dissectorname;
621
622         register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_NONE", "BASE_NONE", 0, "NULL", 0);
623 }
624
625 sub Const($$)
626 {
627         my ($const,$ifname) = @_;
628         
629         if (!defined($const->{ARRAY_LEN}[0])) {
630                 pidl_hdr "#define $const->{NAME}\t( $const->{VALUE} )\n";
631         } else {
632                 pidl_hdr "#define $const->{NAME}\t $const->{VALUE}\n";
633         }
634 }
635
636 sub Typedef($$)
637 {
638         my ($e,$ifname) = @_;
639
640         PrintIdl DumpTypedef($e->{ORIGINAL});
641
642         {
643                 ENUM => \&Enum,
644                 STRUCT => \&Struct,
645                 UNION => \&Union,
646                 BITMAP => \&Bitmap
647         }->{$e->{DATA}->{TYPE}}->($e->{DATA}, $e->{NAME}, $ifname);
648 }
649
650 sub RegisterInterface($)
651 {
652         my ($x) = @_;
653
654         pidl_fn_start "proto_register_dcerpc_$x->{NAME}";
655         pidl_code "void proto_register_dcerpc_$x->{NAME}(void)";
656         pidl_code "{";
657         indent;
658
659         $res{code}.=DumpHfList()."\n";
660         $res{code}.="\n".DumpEttList()."\n";
661         
662         if (defined($x->{UUID})) {
663             # These can be changed to non-pidl_code names if the old dissectors
664             # in epan/dissctors are deleted.
665     
666             my $name = uc($x->{NAME}) . " (pidl)";
667             my $short_name = uc($x->{NAME});
668             my $filter_name = $x->{NAME};
669
670             if (has_property($x, "helpstring")) {
671                 $name = $x->{PROPERTIES}->{helpstring};
672             }
673
674             if (defined($conformance->{protocols}->{$x->{NAME}})) {
675                 $short_name = $conformance->{protocols}->{$x->{NAME}}->{SHORTNAME};
676                 $name = $conformance->{protocols}->{$x->{NAME}}->{LONGNAME};
677                 $filter_name = $conformance->{protocols}->{$x->{NAME}}->{FILTERNAME};
678             }
679
680             pidl_code "proto_dcerpc_$x->{NAME} = proto_register_protocol(".make_str($name).", ".make_str($short_name).", ".make_str($filter_name).");";
681             
682             pidl_code "proto_register_field_array(proto_dcerpc_$x->{NAME}, hf, array_length (hf));";
683             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
684         } else {
685             pidl_code "proto_dcerpc = proto_get_id_by_filter_name(\"dcerpc\");";
686             pidl_code "proto_register_field_array(proto_dcerpc, hf, array_length(hf));";
687             pidl_code "proto_register_subtree_array(ett, array_length(ett));";
688         }
689             
690         deindent;
691         pidl_code "}\n";
692         pidl_fn_end "proto_register_dcerpc_$x->{NAME}";
693 }
694
695 sub RegisterInterfaceHandoff($)
696 {
697         my $x = shift;
698
699         if (defined($x->{UUID})) {
700                 pidl_fn_start "proto_reg_handoff_dcerpc_$x->{NAME}";
701             pidl_code "void proto_reg_handoff_dcerpc_$x->{NAME}(void)";
702             pidl_code "{";
703             indent;
704             pidl_code "dcerpc_init_uuid(proto_dcerpc_$x->{NAME}, ett_dcerpc_$x->{NAME},";
705             pidl_code "\t&uuid_dcerpc_$x->{NAME}, ver_dcerpc_$x->{NAME},";
706             pidl_code "\t$x->{NAME}_dissectors, hf_$x->{NAME}_opnum);";
707             deindent;
708             pidl_code "}";
709                 pidl_fn_end "proto_reg_handoff_dcerpc_$x->{NAME}";
710
711                 $hf_used{"hf_$x->{NAME}_opnum"} = 1;
712         }
713 }
714
715 sub ProcessInclude
716 {
717         my @includes = @_;
718         foreach (@includes) {
719                 pidl_hdr "#include \"$_\"\n";
720         }
721 }
722
723 sub ProcessImport
724 {
725         my @imports = @_;
726         foreach (@imports) {
727                 next if($_ eq "security");
728                 s/\.idl\"$//;
729                 s/^\"//;
730                 pidl_hdr "#include \"packet-dcerpc-$_\.h\"\n";
731         }
732 }
733
734 sub ProcessInterface($)
735 {
736         my ($x) = @_;
737
738         push(@{$conformance->{strip_prefixes}}, $x->{NAME});
739
740         my $define = "__PACKET_DCERPC_" . uc($_->{NAME}) . "_H";
741         pidl_hdr "#ifndef $define";
742         pidl_hdr "#define $define";
743         pidl_hdr "";
744
745         pidl_def "static gint proto_dcerpc_$x->{NAME} = -1;";
746         register_ett("ett_dcerpc_$x->{NAME}");
747         register_hf_field("hf_$x->{NAME}_opnum", "Operation", "$x->{NAME}.opnum", "FT_UINT16", "BASE_DEC", "NULL", 0, "");
748
749         if (defined($x->{UUID})) {
750                 my $if_uuid = $x->{UUID};
751
752             pidl_def "/* Version information */\n\n";
753             
754             pidl_def "static e_uuid_t uuid_dcerpc_$x->{NAME} = {";
755             pidl_def "\t0x" . substr($if_uuid, 1, 8) 
756                 . ", 0x" . substr($if_uuid, 10, 4)
757             . ", 0x" . substr($if_uuid, 15, 4) . ",";
758             pidl_def "\t{ 0x" . substr($if_uuid, 20, 2) 
759                 . ", 0x" . substr($if_uuid, 22, 2)
760             . ", 0x" . substr($if_uuid, 25, 2)
761             . ", 0x" . substr($if_uuid, 27, 2)
762             . ", 0x" . substr($if_uuid, 29, 2)
763             . ", 0x" . substr($if_uuid, 31, 2)
764             . ", 0x" . substr($if_uuid, 33, 2)
765             . ", 0x" . substr($if_uuid, 35, 2) . " }";
766             pidl_def "};";
767         
768             my $maj = $x->{VERSION};
769             $maj =~ s/\.(.*)$//g;
770             pidl_def "static guint16 ver_dcerpc_$x->{NAME} = $maj;";
771             pidl_def "";
772         }
773
774         $return_types{$x->{NAME}} = {};
775
776         Interface($x);
777
778         pidl_code "\n".DumpFunctionTable($x);
779
780         foreach (keys %{$return_types{$x->{NAME}}}) {
781                 my ($type, $desc) = @{$return_types{$x->{NAME}}->{$_}};
782                 my $dt = find_type($type);
783                 $dt or die("Unable to find information about return type `$type'");
784                 register_hf_field("hf_$x->{NAME}_$_", $desc, "$x->{NAME}.$_", $dt->{FT_TYPE}, "BASE_HEX", $dt->{VALSSTRING}, 0, "");
785                 $hf_used{"hf_$x->{NAME}_$_"} = 1;
786         }
787
788         RegisterInterface($x);
789         RegisterInterfaceHandoff($x);
790
791         pidl_hdr "#endif /* $define */";
792 }
793
794 sub find_type($)
795 {
796         my $n = shift;
797
798         return $conformance->{types}->{$n};
799 }
800
801 sub register_type($$$$$$$)
802 {
803         my ($type,$call,$ft,$base,$mask,$vals,$length) = @_;
804
805         $conformance->{types}->{$type} = {
806                 NAME => $type,
807                 DISSECTOR_NAME => $call,
808                 FT_TYPE => $ft,
809                 BASE_TYPE => $base,
810                 MASK => $mask,
811                 VALSSTRING => $vals,
812                 ALIGNMENT => $length
813         };
814 }
815
816 # Loads the default types
817 sub Initialize($)
818 {
819         my $cnf_file = shift;
820
821         $conformance = {
822                 imports => {},
823                 header_fields=> {} 
824         };
825
826         ReadConformance($cnf_file, $conformance) or print STDERR "warning: No conformance file `$cnf_file'\n";
827         
828         foreach my $bytes (qw(1 2 4 8)) {
829                 my $bits = $bytes * 8;
830                 register_type("uint$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@,NULL);", "FT_UINT$bits", "BASE_DEC", 0, "NULL", $bytes);
831                 register_type("int$bits", "offset = dissect_ndr_uint$bits(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_INT$bits", "BASE_DEC", 0, "NULL", $bytes);
832         }
833                 
834         register_type("udlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);", "FT_UINT64", "BASE_DEC", 0, "NULL", 4);
835         register_type("bool8", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
836         register_type("char", "offset = dissect_ndr_uint8(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT8", "BASE_DEC", 0, "NULL", 1);
837         register_type("long", "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT32", "BASE_DEC", 0, "NULL", 4);
838         register_type("dlong", "offset = dissect_ndr_duint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_INT64", "BASE_DEC", 0, "NULL", 8);
839         register_type("GUID", "offset = dissect_ndr_uuid_t(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_GUID", "BASE_NONE", 0, "NULL", 4);
840         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);
841         register_type("NTTIME", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
842         register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
843         register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "BASE_DEC", 0, "NULL", 4);
844         register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "BASE_NONE", 0, "NULL", 4);
845         register_type("SID", "
846                 dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
847
848                 di->hf_index = \@HF\@;
849
850                 offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
851         ","FT_STRING", "BASE_DEC", 0, "NULL", 4);
852         register_type("WERROR", 
853                 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
854         register_type("NTSTATUS", 
855                 "offset = dissect_ndr_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, NULL);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
856
857 }
858
859 #####################################################################
860 # Generate Wireshark parser and header code
861 sub Parse($$$$)
862 {
863         my($ndr,$idl_file,$h_filename,$cnf_file) = @_;
864         Initialize($cnf_file);
865
866         return (undef, undef) if defined($conformance->{noemit_dissector});
867
868         $tabs = "";
869
870         %res = (code=>"",def=>"",hdr=>"");
871         @ett = ();
872
873         my $notice = 
874 "/* DO NOT EDIT
875         This filter was automatically generated
876         from $idl_file and $cnf_file.
877         
878         Pidl is a perl based IDL compiler for DCE/RPC idl files. 
879         It is maintained by the Samba team, not the Wireshark team.
880         Instructions on how to download and install Pidl can be 
881         found at http://wiki.wireshark.org/Pidl
882 */
883
884 ";
885
886         pidl_hdr $notice;
887
888         $res{headers} = "\n";
889         $res{headers} .= "#ifdef HAVE_CONFIG_H\n";
890         $res{headers} .= "#include \"config.h\"\n";
891         $res{headers} .= "#endif\n\n";
892         $res{headers} .= "#include <glib.h>\n";
893         $res{headers} .= "#include <string.h>\n";
894         $res{headers} .= "#include <epan/packet.h>\n\n";
895
896         $res{headers} .= "#include \"packet-dcerpc.h\"\n";
897         $res{headers} .= "#include \"packet-dcerpc-nt.h\"\n";
898         $res{headers} .= "#include \"packet-windows-common.h\"\n";
899
900         my $h_basename = basename($h_filename);
901
902         $res{headers} .= "#include \"$h_basename\"\n";
903         pidl_code "";
904
905         # Wireshark protocol registration
906
907         foreach (@$ndr) {
908                 ProcessInterface($_) if ($_->{TYPE} eq "INTERFACE");
909                 ProcessImport(@{$_->{PATHS}}) if ($_->{TYPE} eq "IMPORT");
910                 ProcessInclude(@{$_->{PATHS}}) if ($_->{TYPE} eq "INCLUDE");
911         }
912
913         $res{ett} = DumpEttDeclaration();
914         $res{hf} = DumpHfDeclaration();
915
916         my $parser = $notice;
917         $parser.= $res{headers};
918         $parser.=$res{ett};
919         $parser.=$res{hf};
920         $parser.=$res{def};
921         $parser.=$conformance->{override};
922         $parser.=$res{code};
923
924         my $header = "/* autogenerated by pidl */\n\n";
925         $header.=$res{hdr};
926
927         CheckUsed($conformance);
928     
929         return ($parser,$header);
930 }
931
932 ###############################################################################
933 # ETT
934 ###############################################################################
935
936 sub register_ett($)
937 {
938         my $name = shift;
939
940         push (@ett, $name);     
941 }
942
943 sub DumpEttList()
944 {
945         my $res = "\tstatic gint *ett[] = {\n";
946         foreach (@ett) {
947                 $res .= "\t\t&$_,\n";
948         }
949
950         return "$res\t};\n";
951 }
952
953 sub DumpEttDeclaration()
954 {
955         my $res = "\n/* Ett declarations */\n";
956         foreach (@ett) {
957                 $res .= "static gint $_ = -1;\n";
958         }
959
960         return "$res\n";
961 }
962
963 ###############################################################################
964 # HF
965 ###############################################################################
966
967 sub register_hf_field($$$$$$$$) 
968 {
969         my ($index,$name,$filter_name,$ft_type,$base_type,$valsstring,$mask,$blurb) = @_;
970
971         if (defined ($conformance->{hf_renames}->{$index})) {
972                 $conformance->{hf_renames}->{$index}->{USED} = 1;
973                 return $conformance->{hf_renames}->{$index}->{NEWNAME};
974         }
975
976         $conformance->{header_fields}->{$index} = {
977                 INDEX => $index,
978                 NAME => $name,
979                 FILTER => $filter_name,
980                 FT_TYPE => $ft_type,
981                 BASE_TYPE => $base_type,
982                 VALSSTRING => $valsstring,
983                 MASK => $mask,
984                 BLURB => $blurb
985         };
986
987         if ((not defined($blurb) or $blurb eq "") and 
988                         defined($conformance->{fielddescription}->{$index})) {
989                 $conformance->{header_fields}->{$index}->{BLURB} = 
990                         $conformance->{fielddescription}->{$index}->{DESCRIPTION};
991                 $conformance->{fielddescription}->{$index}->{USED} = 1;
992         }
993
994         return $index;
995 }
996
997 sub DumpHfDeclaration()
998 {
999         my $res = "";
1000
1001         $res = "\n/* Header field declarations */\n";
1002
1003         foreach (keys %{$conformance->{header_fields}}) 
1004         {
1005                 $res .= "static gint $_ = -1;\n";
1006         }
1007
1008         return "$res\n";
1009 }
1010
1011 sub DumpHfList()
1012 {
1013         my $res = "\tstatic hf_register_info hf[] = {\n";
1014
1015         foreach (values %{$conformance->{header_fields}}) 
1016         {
1017                 $res .= "\t{ &$_->{INDEX}, 
1018           { ".make_str($_->{NAME}).", ".make_str($_->{FILTER}).", $_->{FT_TYPE}, $_->{BASE_TYPE}, $_->{VALSSTRING}, $_->{MASK}, ".make_str($_->{BLURB}).", HFILL }},
1019 ";
1020         }
1021
1022         return $res."\t};\n";
1023 }
1024
1025
1026 ###############################################################################
1027 # Function table
1028 ###############################################################################
1029
1030 sub DumpFunctionTable($)
1031 {
1032         my $if = shift;
1033
1034         my $res = "static dcerpc_sub_dissector $if->{NAME}\_dissectors[] = {\n";
1035         foreach (@{$if->{FUNCTIONS}}) {
1036                 my $fn_name = $_->{NAME};
1037                 $fn_name =~ s/^$if->{NAME}_//;
1038                 $res.= "\t{ $_->{OPNUM}, \"$fn_name\",\n";
1039                 $res.= "\t   $if->{NAME}_dissect_${fn_name}_request, $if->{NAME}_dissect_${fn_name}_response},\n";
1040         }
1041
1042         $res .= "\t{ 0, NULL, NULL, NULL }\n";
1043
1044         return "$res};\n";
1045 }
1046
1047 sub CheckUsed($)
1048 {
1049         my $conformance = shift;
1050         foreach (values %{$conformance->{header_fields}}) {
1051                 if (not defined($hf_used{$_->{INDEX}})) {
1052                         warning($_->{POS}, "hf field `$_->{INDEX}' not used");
1053                 }
1054         }
1055
1056         foreach (values %{$conformance->{hf_renames}}) {
1057                 if (not $_->{USED}) {
1058                         warning($_->{POS}, "hf field `$_->{OLDNAME}' not used");
1059                 }
1060         }
1061
1062         foreach (values %{$conformance->{dissectorparams}}) {
1063                 if (not $_->{USED}) {
1064                         warning($_->{POS}, "dissector param never used");
1065                 }
1066         }
1067
1068         foreach (values %{$conformance->{imports}}) {
1069                 if (not $_->{USED}) {
1070                         warning($_->{POS}, "import never used");
1071                 }
1072         }
1073
1074         foreach (values %{$conformance->{types}}) {
1075                 if (not $_->{USED} and defined($_->{POS})) {
1076                         warning($_->{POS}, "type never used");
1077                 }
1078         }
1079
1080         foreach (values %{$conformance->{fielddescription}}) {
1081                 if (not $_->{USED}) {
1082                         warning($_->{POS}, "description never used");
1083                 }
1084         }
1085
1086         foreach (values %{$conformance->{tfs}}) {
1087                 if (not $_->{USED}) {
1088                         warning($_->{POS}, "True/False description never used");
1089                 }
1090         }
1091 }
1092
1093 1;