pidl-wireshark: SWITCH_TYPE is not always defined, SwitchType() will try to find...
[amitay/samba.git] / pidl / lib / Parse / Pidl / Wireshark / NDR.pm
index 5cfcf5cdc8d0da6d33d0a642ef729a5f7512f2dd..89cbf84cd90dfdb0c261b5555bb6e93f47c2fec4 100644 (file)
@@ -150,7 +150,7 @@ sub Enum($$$$)
        }
        
        $self->pidl_hdr("extern const value_string $valsstring\[];");
-       $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 *param _U_);");
+       $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_);");
 
        $self->pidl_def("const value_string ".$valsstring."[] = {");
        foreach (@{$e->{ELEMENTS}}) {
@@ -163,19 +163,19 @@ sub Enum($$$$)
 
        $self->pidl_fn_start($dissectorname);
        $self->pidl_code("int");
-       $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 *param _U_)");
+       $self->pidl_code("$dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *tree _U_, guint8 *drep _U_, int hf_index _U_, g$e->{BASE_TYPE} *param _U_)");
        $self->pidl_code("{");
        $self->indent;
        $self->pidl_code("g$e->{BASE_TYPE} parameter=0;");
-       $self->pidl_code("if(param){");
+       $self->pidl_code("if (param) {");
        $self->indent;
-       $self->pidl_code("parameter=(g$e->{BASE_TYPE})*param;");
+       $self->pidl_code("parameter = *param;");
        $self->deindent;
        $self->pidl_code("}");
        $self->pidl_code("offset = dissect_ndr_$e->{BASE_TYPE}(tvb, offset, pinfo, tree, drep, hf_index, &parameter);");
-       $self->pidl_code("if(param){");
+       $self->pidl_code("if (param) {");
        $self->indent;
-       $self->pidl_code("*param=(guint32)parameter;");
+       $self->pidl_code("*param = parameter;");
        $self->deindent;
        $self->pidl_code("}");
        $self->pidl_code("return offset;");
@@ -188,6 +188,13 @@ sub Enum($$$$)
        $self->register_type($name, "offset = $dissectorname(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);", "FT_UINT$enum_size", "BASE_DEC", "0", "VALS($valsstring)", $enum_size / 8);
 }
 
+sub Pipe($$$$)
+{
+       my ($self,$e,$name,$ifname) = @_;
+       error($e->{ORIGINAL}, "Pipe not yet supported");
+       return;
+}
+
 sub Bitmap($$$$)
 {
        my ($self,$e,$name,$ifname) = @_;
@@ -338,17 +345,25 @@ sub ElementLevel($$$$$$$$)
                        my $call;
 
                        if ($self->{conformance}->{imports}->{$l->{DATA_TYPE}}) {
-                               $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};     
+                               $call = $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{DATA};
                                $self->{conformance}->{imports}->{$l->{DATA_TYPE}}->{USED} = 1;
-                       } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
-                               $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
+
+                       } elsif (defined($self->{conformance}->{imports}->{"$pn.$e->{NAME}"})) {
+                               $call = $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{DATA};
                                $self->{conformance}->{imports}->{"$pn.$e->{NAME}"}->{USED} = 1;
-                           
+
                        } elsif (defined($self->{conformance}->{types}->{$l->{DATA_TYPE}})) {
                                $call= $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{DISSECTOR_NAME};
                                $self->{conformance}->{types}->{$l->{DATA_TYPE}}->{USED} = 1;
                        } else {
-                               $self->pidl_code("offset = $ifname\_dissect_struct_" . $l->{DATA_TYPE} . "(tvb,offset,pinfo,tree,drep,$hf,$param);");
+                               my $t;
+                               if (ref($l->{DATA_TYPE}) eq "HASH" ) {
+                                       $t = "$l->{DATA_TYPE}->{TYPE}_$l->{DATA_TYPE}->{NAME}";
+                               } else {
+                                       $t = $l->{DATA_TYPE};
+                               }
+
+                               $self->pidl_code("offset = $ifname\_dissect_struct_" . $t . "(tvb,offset,pinfo,tree,drep,$hf,$param);");
 
                                return;
                        }
@@ -392,11 +407,36 @@ sub ElementLevel($$$$$$$$)
                $self->pidl_code("di->call_data->flags = saved_flags;");
                $self->deindent;
                $self->pidl_code("}");
+       } elsif ($_->{TYPE} eq "PIPE") {
+               error($e->{ORIGINAL}, "Type PIPE not yet supported");
        } else {
                die("Unknown type `$_->{TYPE}'");
        }
 }
 
+sub SwitchType($$;$)
+{
+       my ($e, $type, $nodiscriminant) = @_;
+
+       my $switch_dt =  getType($type);
+       my $switch_type = undef;
+       if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
+               $switch_type = Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
+       } elsif ($switch_dt->{DATA}->{TYPE} eq "BITMAP") {
+               $switch_type = Parse::Pidl::Typelist::bitmap_type_fn($switch_dt->{DATA});
+       } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
+               if (defined $e->{SWITCH_TYPE}) {
+                       $switch_type = "$e->{SWITCH_TYPE}";
+               } else {
+                       $switch_type = "$switch_dt->{DATA}->{NAME}";
+               }
+       } elsif (not defined $e->{SWITCH_TYPE}) {
+               $switch_type = $nodiscriminant;
+       }
+
+       return $switch_type
+}
+
 sub Element($$$$$)
 {
        my ($self,$e,$pn,$ifname,$isoruseswitch) = @_;
@@ -409,13 +449,12 @@ sub Element($$$$$)
                my $type = $isoruseswitch->[0];
                my $name = $isoruseswitch->[1];
 
-               my $switch_dt =  getType($type);
-               my $switch_type;
-               if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
-                       $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
-               } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
-                       $switch_type = "g$e->{SWITCH_TYPE}";
+               my $switch_raw_type = SwitchType($e, $type, "uint32");
+               if (not defined($switch_raw_type)) {
+                       die("Unknown type[$type]\n");
                }
+               my $switch_type = "g${switch_raw_type}";
+
                $moreparam = ", $switch_type *".$name;
                $param = $name;
                $call_code = "offset = $dissectorname(tvb, offset, pinfo, tree, drep, &$name);";
@@ -445,6 +484,12 @@ sub Element($$$$$)
                        BASE_TYPE => "BASE_NONE"
                };
        }
+       if (property_matches($e, "flag", ".*LIBNDR_FLAG_ALIGN.*")) {
+               my $align_flag = $e->{PROPERTIES}->{flag};
+               if ($align_flag =~ m/LIBNDR_FLAG_ALIGN(\d+)/) {
+                       $call_code = "ALIGN_TO_$1_BYTES; ".$call_code;
+               }
+       }
 
        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}, "");
        $self->{hf_used}->{$hf} = 1;
@@ -607,15 +652,13 @@ sub Struct($$$$)
                my $switch_info = undef;
 
                my $v = $_->{NAME};
-               if (scalar(grep {/$v/} keys(%$varswitchs)) == 1) {
+               if (scalar(grep {/^$v$/} keys(%$varswitchs)) == 1) {
                        # This element is one of the switch attribute
-                       my $switch_dt =  getType($_->{TYPE});
-                       my $switch_type;
-                       if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
-                               $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
-                       } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
-                               $switch_type = "g$e->{SWITCH_TYPE}";
+                       my $switch_raw_type = SwitchType($e, $_->{TYPE}, "uint32");
+                       if (not defined($switch_raw_type)) {
+                               die("Unknown type[$_->{TYPE}]\n");
                        }
+                       my $switch_type = "g${switch_raw_type}";
 
                        push @$vars, "$switch_type $v;";
                        $switch_info = [ $_->{TYPE}, $v ];
@@ -630,6 +673,13 @@ sub Struct($$$$)
                $res.="\t".$self->Element($_, $name, $ifname, $switch_info)."\n\n";
        }
 
+       my $doalign = undef;
+       if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
+               $doalign = 1;
+       } elsif (property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
+               $doalign = 0;
+       }
+
        $self->pidl_hdr("int $dissectorname(tvbuff_t *tvb _U_, int offset _U_, packet_info *pinfo _U_, proto_tree *parent_tree _U_, guint8 *drep _U_, int hf_index _U_, guint32 param _U_);");
 
        $self->pidl_fn_start($dissectorname);
@@ -640,16 +690,24 @@ sub Struct($$$$)
        $self->pidl_code($_) foreach (@$vars);
        $self->pidl_code("proto_item *item = NULL;");
        $self->pidl_code("proto_tree *tree = NULL;");
-       if ($e->{ALIGN} > 1) {
+       if (defined($doalign)) {
                $self->pidl_code("dcerpc_info *di = (dcerpc_info *)pinfo->private_data;");
+               if ($doalign == 0) {
+                       $self->pidl_code("gboolean oldalign = di->no_align;");
+               }
        }
        $self->pidl_code("int old_offset;");
        $self->pidl_code("");
 
-       if ($e->{ALIGN} > 1 and not property_matches($e, "flag", ".*LIBNDR_FLAG_NOALIGN.*")) {
-               $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
+       if (defined($doalign)) {
+               if ($doalign == 1) {
+                       $self->pidl_code("ALIGN_TO_$e->{ALIGN}_BYTES;");
+               }
+               if ($doalign == 0) {
+                       $self->pidl_code("di->no_align = TRUE;");
+               }
+               $self->pidl_code("");
        }
-       $self->pidl_code("");
 
        $self->pidl_code("old_offset = offset;");
        $self->pidl_code("");
@@ -663,7 +721,7 @@ sub Struct($$$$)
        $self->pidl_code("\n$res");
 
        $self->pidl_code("proto_item_set_len(item, offset-old_offset);\n");
-       if ($e->{ALIGN} > 1) {
+       if (defined($doalign) and $doalign == 1) {
                $self->pidl_code("");
                $self->pidl_code("if (di->call_data->flags & DCERPC_IS_NDR64) {");
                $self->indent;
@@ -671,6 +729,10 @@ sub Struct($$$$)
                $self->deindent;
                $self->pidl_code("}");
        }
+       if (defined($doalign) and $doalign == 0) {
+               $self->pidl_code("");
+               $self->pidl_code("di->no_align = oldalign;");
+       }
        $self->pidl_code("");
        $self->pidl_code("return offset;");
        $self->deindent;
@@ -699,15 +761,12 @@ sub Union($$$$)
                $res.="\t\tbreak;\n";
        }
 
-       my $switch_type;
-       my $switch_dissect;
-       my $switch_dt = getType($e->{SWITCH_TYPE});
-       if ($switch_dt->{DATA}->{TYPE} eq "ENUM") {
-               $switch_type = "g".Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
-               $switch_dissect = "dissect_ndr_" .Parse::Pidl::Typelist::enum_type_fn($switch_dt->{DATA});
-       } elsif ($switch_dt->{DATA}->{TYPE} eq "SCALAR") {
-               $switch_type = "g$e->{SWITCH_TYPE}";
-               $switch_dissect = "dissect_ndr_$e->{SWITCH_TYPE}";
+       my $switch_type = undef;
+       my $switch_dissect = undef;
+       my $switch_raw_type = SwitchType($e, $e->{SWITCH_TYPE});
+       if (defined($switch_raw_type)) {
+               $switch_type = "g${switch_raw_type}";
+               $switch_dissect = "dissect_ndr_${switch_raw_type}";
        }
 
        $self->pidl_fn_start($dissectorname);
@@ -780,13 +839,13 @@ sub Type($$$$)
        my ($self, $e, $name, $ifname) = @_;
 
        $self->PrintIdl(DumpType($e->{ORIGINAL}));
-
        {
                ENUM => \&Enum,
                STRUCT => \&Struct,
                UNION => \&Union,
                BITMAP => \&Bitmap,
-               TYPEDEF => \&Typedef
+               TYPEDEF => \&Typedef,
+               PIPE    => \&Pipe
        }->{$e->{TYPE}}->($self, $e, $name, $ifname);
 }
 
@@ -992,17 +1051,25 @@ sub Initialize($$)
        $self->register_type("NTTIME_hyper", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
        $self->register_type("time_t", "offset = dissect_ndr_time_t(tvb, offset, pinfo,tree, drep, \@HF\@, NULL);","FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
        $self->register_type("NTTIME_1sec", "offset = dissect_ndr_nt_NTTIME(tvb, offset, pinfo, tree, drep, \@HF\@);", "FT_ABSOLUTE_TIME", "ABSOLUTE_TIME_LOCAL", 0, "NULL", 4);
-       $self->register_type("SID", "
+       $self->register_type("dom_sid28", "{
+               dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
+               di->hf_index = \@HF\@;
+
+               offset = dissect_ndr_nt_SID28(tvb, offset, pinfo, tree, drep);
+       }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
+       $self->register_type("SID", "{
                dcerpc_info *di = (dcerpc_info *)pinfo->private_data;
 
                di->hf_index = \@HF\@;
 
                offset = dissect_ndr_nt_SID_with_options(tvb, offset, pinfo, tree, drep, param);
-       ","FT_STRING", "BASE_NONE", 0, "NULL", 4);
+       }", "FT_STRING", "BASE_NONE", 0, "NULL", 4);
        $self->register_type("WERROR", 
                "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(WERR_errors)", 4);
        $self->register_type("NTSTATUS", 
                "offset = PIDL_dissect_uint32(tvb, offset, pinfo, tree, drep, \@HF\@, \@PARAM\@);","FT_UINT32", "BASE_DEC", 0, "VALS(NT_errors)", 4);
+       $self->register_type("ipv6address", "proto_tree_add_item(tree, \@HF\@, tvb, offset, 16, ENC_NA); offset += 16;", "FT_IPv6", "BASE_NONE", 0, "NULL", 16);
+       $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);
 
 }
 
@@ -1020,13 +1087,11 @@ sub Parse($$$$$)
 "/* DO NOT EDIT
        This filter was automatically generated
        from $idl_file and $cnf_file.
-       
+
        Pidl is a perl based IDL compiler for DCE/RPC idl files.
        It is maintained by the Samba team, not the Wireshark team.
        Instructions on how to download and install Pidl can be
        found at http://wiki.wireshark.org/Pidl
-
-       \$Id\$
 */
 
 ";
@@ -1156,6 +1221,23 @@ sub register_hf_field($$$$$$$$$)
        return $index;
 }
 
+sub change_hf_field_type($$$$)
+{
+       my ($self,$index,$ft_type,$base_type) = @_;
+       if (defined ($self->{conformance}->{hf_renames}->{$index})) {
+               print "Field $index has been renamed to ".$self->{conformance}->{hf_renames}->{$index}->{NEWNAME}." you can't change it's type";
+               return 0;
+       }
+
+       if (!defined ($self->{conformance}->{header_fields}->{$index})) {
+               print "Field $index doesn't exists";
+               return 0;
+       }
+       $self->{conformance}->{header_fields}->{$index}->{FT_TYPE} = $ft_type;
+       $self->{conformance}->{header_fields}->{$index}->{BASE_TYPE} = $base_type;
+       return 1;
+}
+
 sub DumpHfDeclaration($)
 {
        my ($self) = @_;