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