[Spice-devel] [PATCH 32/33] Use a class to register wireshark fields
Frediano Ziglio
fziglio at redhat.com
Wed Jul 1 10:10:24 PDT 2015
Allow to reuse code and check better if field was already registered
Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
python_modules/dissector.py | 59 +++++++++++++++++++++++++++++++++++++--------
1 file changed, 49 insertions(+), 10 deletions(-)
diff --git a/python_modules/dissector.py b/python_modules/dissector.py
index edb8533..7070c3a 100644
--- a/python_modules/dissector.py
+++ b/python_modules/dissector.py
@@ -20,8 +20,50 @@ def new_ett(writer):
return name
+# handle registration of wireshark flags
hf_writer = None
hf_defs = None
+class HF:
+ def __init__(self, hf_name, desc=''):
+ self.hf_name = hf_name
+ self.desc = desc
+ self.mask = '0'
+
+ def __str__(self):
+ x = []
+ for f in 'hf_name desc ws_name f_type base vals mask'.split():
+ x.append(f + ':' + self.__dict__[f])
+ return ' '.join(x)
+
+ def debug(self, writer, s):
+ raise Exception(s)
+
+ # declare a field identifier
+ def add_wireshark_field(self):
+ if hf_writer.variable_defined(self.hf_name):
+ raise Exception('HF field %s already defined' % self.hf_name)
+ hf_writer.variable_def("static int", "%s = -1" % self.hf_name)
+
+ def create(self, writer):
+ other = hf_defs.fields.get(self.ws_name)
+ if other:
+ for f in 'hf_name desc ws_name base vals mask'.split():
+ if other.__dict__[f] != self.__dict__[f]:
+ self.debug(writer, 'HF Field different from previous for\n\t%s\n\t%s' % (other, self))
+ if other.f_type != self.f_type:
+ if other.f_type[:7] != 'FT_UINT' or self.f_type[:7] != 'FT_UINT':
+ self.debug(writer, 'HF Field different from previous for\n\t%s\n\t%s' % (other, self))
+ return
+
+ hf_defs.fields[self.ws_name] = self
+
+ self.add_wireshark_field()
+
+ hf_defs.writeln('{ &%s,' % self.hf_name)
+ hf_defs.writeln(' { "%s", "spice2.%s",' % (self.desc, self.ws_name))
+ hf_defs.writeln(' %s, %s, %s, %s,' % (self.f_type, self.base, self.vals, self.mask))
+ hf_defs.writeln(' NULL, HFILL }')
+ hf_defs.writeln('},')
def write_parser_helpers(writer):
@@ -274,16 +316,13 @@ def write_wireshark_field(writer, container, member, t, tree, size, encoding='EN
writer.statement("%sproto_tree_add_item(%s, %s, glb->tvb, offset, %s, %s)" %
(prefix, tree, hf_name, size, encoding))
- # TODO handle better duplications
- if hf_writer.variable_defined(hf_name):
- return
- hf_writer.variable_def("static int", "%s = -1" % hf_name)
-
- hf_defs.writeln('{ &%s,' % hf_name)
- hf_defs.writeln(' { "%s", "spice2.%s",' % (desc, ws_name))
- hf_defs.writeln(' %s, %s, %s, 0,' % (f_type, base, vals))
- hf_defs.writeln(' NULL, HFILL }')
- hf_defs.writeln('},')
+ # write definition
+ hf = HF(hf_name, desc)
+ hf.ws_name = ws_name
+ hf.f_type = f_type
+ hf.base = base
+ hf.vals = vals
+ hf.create(writer)
# Note: during parsing, byte_size types have been converted to count during validation
--
2.1.0
More information about the Spice-devel
mailing list