[Spice-devel] [PATCH v6 26/42] dissector: Allows to override default values generated for the fields

Frediano Ziglio fziglio at redhat.com
Thu Aug 13 06:12:05 PDT 2015


ws_type overrides the type (BOOLEAN -> FT_BOOLEAN).
ws_base overrides the base (DEC -> BASE_HEX).
ws_desc overrides the description.
ws allows to specify description and name for wireshark. Name is
important as this can be used in wireshark filters. Having a single
attribute with 2 values allows to quickly specify the main attributes.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 python_modules/dissector.py | 45 ++++++++++++++++++++++++++++++++++-----------
 tests/dissector_test.c      |  2 ++
 tests/out_base1.txt         | 14 +++++++-------
 tests/test.proto            |  8 ++++----
 4 files changed, 47 insertions(+), 22 deletions(-)

diff --git a/python_modules/dissector.py b/python_modules/dissector.py
index 7dd0f3a..4fb451b 100644
--- a/python_modules/dissector.py
+++ b/python_modules/dissector.py
@@ -253,12 +253,10 @@ def get_primitive_ft_type(t):
     return "FT_%sINT%d" % (unsigned, size * 8)
 
 # write a field
-def write_wireshark_field(writer, container, member, t, tree, size, encoding='ENC_LITTLE_ENDIAN', prefix=''):
+def write_wireshark_field(writer, container, member, t, ws, tree, size, encoding='ENC_LITTLE_ENDIAN', prefix=''):
 
     assert(member and container)
 
-    hf_name = member_hf_name(container, member)
-
     # compute proper type
     f_type = 'FT_NONE'
     base = 'BASE_NONE'
@@ -277,8 +275,30 @@ def write_wireshark_field(writer, container, member, t, tree, size, encoding='EN
             assert(t.has_name())
             vals = 'VALS(%s_vs)' % codegen.prefix_underscore_lower(t.name)
 
-    desc = member.name
-    ws_name = 'auto.' + hf_name[3:]
+    # override type
+    if ws.type:
+        f_type = 'FT_%s' % ws.type
+        base = 'BASE_NONE'
+        vals = 'NULL'
+        if f_type == 'FT_BOOLEAN':
+            vals = 'TFS(&tfs_set_notset)'
+
+    # override base
+    if ws.base:
+        base = 'BASE_%s' % ws.base
+
+    # read description
+    desc = ws.desc
+    if desc is None:
+        desc = member.name
+
+    # read name
+    ws_name = ws.name
+    if not ws_name:
+        hf_name = member_hf_name(container, member)
+        ws_name = 'auto.' + hf_name[3:]
+    else:
+        hf_name = 'hf_%s' % ws_name.replace('.', '_')
 
     writer.statement("%sproto_tree_add_item(%s, %s, glb->tvb, offset, %s, %s)" %
             (prefix, tree, hf_name, size, encoding))
@@ -366,7 +386,7 @@ def write_switch(writer, container, switch, dest, scope):
             elif t.is_struct():
                 write_struct(writer, m, t, '-1', dest2, scope)
             elif t.is_primitive():
-                write_member_primitive(writer, container, m, t, dest2, scope)
+                write_member_primitive(writer, container, m, t, WSAttributes(t, m.attributes), dest2, scope)
             elif t.is_array():
                 nelements = read_array_len(writer, m.name, t, dest, block, False)
                 write_array(writer, container, m, nelements, t, dest2, block)
@@ -383,16 +403,18 @@ def write_switch(writer, container, switch, dest, scope):
 def write_array(writer, container, member, nelements, array, dest, scope):
     assert(container and member)
 
+    ws = WSAttributes(array, member.attributes)
+
     element_type = array.element_type
 
     if element_type == ptypes.uint8 or element_type == ptypes.int8:
-        write_wireshark_field(writer, container, member, array, dest.level.tree, nelements, 'ENC_NA')
+        write_wireshark_field(writer, container, member, array, ws, dest.level.tree, nelements, 'ENC_NA')
         writer.increment("offset", nelements)
         return
 
     with writer.index() as index, writer.for_loop(index, nelements) as array_scope:
         if element_type.is_primitive():
-            write_member_primitive(writer, container, member, element_type, dest, scope)
+            write_member_primitive(writer, container, member, element_type, WSAttributes(element_type, array.item_attrs), dest, scope)
         else:
             assert(element_type.is_struct())
             write_struct(writer, member, element_type, index, dest, scope)
@@ -476,12 +498,13 @@ def write_struct(writer, member, t, index, dest, scope):
         write_struct_func(writer, t, func_name, index)
         writer.assign('offset', '%s(glb, %s, offset, %s)' % (func_name, dest.level.tree, index))
 
-def write_member_primitive(writer, container, member, t, dest, scope):
+def write_member_primitive(writer, container, member, t, ws, dest, scope):
     assert(t.is_primitive())
 
     if member.has_attr("bytes_count"):
         raise NotImplementedError("bytes_count not implemented")
-    write_wireshark_field(writer, container, member, t, dest.level.tree, t.get_fixed_nw_size())
+
+    write_wireshark_field(writer, container, member, t, ws, dest.level.tree, t.get_fixed_nw_size())
     if member.has_attr("bytes_count"):
         dest_var = member.attributes["bytes_count"][0]
     else:
@@ -514,7 +537,7 @@ def write_member(writer, container, member, dest, scope):
         if not member.has_attr("nocopy"):
             write_pointer(writer, container, member, t, dest, scope)
     elif t.is_primitive():
-        write_member_primitive(writer, container, member, t, dest, scope)
+        write_member_primitive(writer, container, member, t, WSAttributes(t, member.attributes), dest, scope)
     elif t.is_array():
         nelements = read_array_len(writer, member.name, t, dest, scope, False)
         write_array(writer, container, member, nelements, t, dest, scope)
diff --git a/tests/dissector_test.c b/tests/dissector_test.c
index 79f8592..dcc0134 100644
--- a/tests/dissector_test.c
+++ b/tests/dissector_test.c
@@ -206,6 +206,8 @@ lookup_values(char *label, value_string *vals, guint64 value)
 	return false;
 }
 
+const true_false_string tfs_set_notset = { "Set", "Not set" };
+
 WS_DLL_PUBLIC void
 proto_register_field_array(const int parent, hf_register_info *hf, const int num_records)
 {
diff --git a/tests/out_base1.txt b/tests/out_base1.txt
index 49a7426..7921afd 100644
--- a/tests/out_base1.txt
+++ b/tests/out_base1.txt
@@ -1,10 +1,10 @@
 --- tree
     --- item
-    Text: 130 (0x82)
+    Text: Not set
     Name: u8
     Abbrev: spice2.auto.msg_base_Base1_u8
-    Type: FT_UINT8
-    Base: BASE_DEC
+    Type: FT_BOOLEAN
+    Base: 0
     --- item
     Text: -127 (0xffffffffffffff81)
     Name: i8
@@ -16,17 +16,17 @@
     Name: u16
     Abbrev: spice2.auto.msg_base_Base1_u16
     Type: FT_UINT16
-    Base: BASE_DEC
+    Base: BASE_DEC_HEX
     --- item
     Text: -31743 (0xffffffffffff8401)
-    Name: i16
+    Name: Signed 16
     Abbrev: spice2.auto.msg_base_Base1_i16
     Type: FT_INT16
     Base: BASE_DEC
     --- item
     Text: 2231566849 (0x85030201)
-    Name: u32
-    Abbrev: spice2.auto.msg_base_Base1_u32
+    Name: Unsigned 32
+    Abbrev: spice2.uint.test
     Type: FT_UINT32
     Base: BASE_DEC
     --- item
diff --git a/tests/test.proto b/tests/test.proto
index 2fd930b..b28520c 100644
--- a/tests/test.proto
+++ b/tests/test.proto
@@ -49,11 +49,11 @@ message ArrayStruct {
 channel BaseChannel {
   server:
     message {
-        uint8 u8;
+        uint8 u8 @ws_type(BOOLEAN);
         int8  i8;
-        uint16 u16;
-        int16  i16;
-        uint32 u32;
+        uint16 u16 @ws_base(DEC_HEX);
+        int16  i16 @ws_desc("Signed 16");
+        uint32 u32 @ws("Unsigned 32", uint.test);
         int32  i32;
         uint64 u64;
         int64  i64;
-- 
2.4.3



More information about the Spice-devel mailing list