Esempio n. 1
0
    def _register_dissector(self):
        """Add code for registering the dissector in the dissector table."""
        # Dissector message id, which maps to name
        if self.id is None:
            message_ids = ['nil']
        else:
            message_ids = self.id

        # Dissector Sizes and platforms
        sizes = {i.platform.flag: i.size for i in self.dissectors.values()}
        sizes = create_lua_valuestring(sizes, wrap=False)

        data = []
        for id in message_ids:
            data.append('{func}({var}, "{name}", {id}, {sizes})'.format(
                    func=self.REGISTER_FUNC, var=self.var,
                    name=self.name, id=id, sizes=sizes))
        data.extend(['', ''])
        return '\n'.join(i for i in data if i is not None)
Esempio n. 2
0
    def _dissector_func(self):
        """Add the code for the dissector function for the protocol."""
        data = ['-- Dissector function for: %s' % self.name]

        def retrieve_pinfo():
            data.append('\tif pinfo.private.field_name then')
            t = '\t\tsubtree:set_text(pinfo.private.field_name .. ": {name}")'
            data.append(t.format(name=child.name))
            data.append('\t\tpinfo.private.field_name = nil\n\telse')
            t = '\t\tpinfo.cols.info:append("({desc})")'
            data.append(t.format(desc=self.description))
            data.append('\tend')

        # Dissector function
        func_diss = 'function {var}.dissector(buffer, pinfo, tree)'
        data.append(func_diss.format(var=self.var))

        # Retrieve flag value from private info table
        flag_var = create_lua_var('flag')
        flag = '\tlocal {var} = tonumber(pinfo.private.platform_flag)'
        data.append(flag.format(var=flag_var))

        # If only 1 or less dissectors, insert dissector code directly
        if len(self.dissectors) < 2:
            if self.dissectors:
                child = list(self.dissectors.values())[0]
                sub_tree = '\tlocal subtree = tree:{add}({var}, buffer())'
                data.append(sub_tree.format(add=child.add_var, var=self.var))
                retrieve_pinfo()
                data.append(child.get_code(0))
            data.extend(['end', ''])
            return '\n'.join(i for i in data if i is not None)

        # Get flags and call the platform specific function
        table = {}
        for child in self.dissectors.values():
            child._func_name = create_lua_var(
                    '%s_%s' % (self.var, child.platform.name))
            table[child.platform.flag] = child._func_name
        table = create_lua_valuestring(table, wrap=False)
        data.append('\tlocal func_mapping = {table}'.format(table=table))
        data.append('\tif func_mapping[{var}] then'.format(var=flag_var))
        call = '\t\t func_mapping[{var}](buffer, pinfo, tree)'
        data.append(call.format(var=flag_var))
        data.extend(['\tend', 'end', ''])

        # Modify name if sub-dissector
        pinfo_func = create_lua_var('%s_pinfo_magic' % self.var)
        data.append('-- Function for retrieving parent dissector name')
        data.append('function {func}(pinfo, subtree)'.format(func=pinfo_func))
        retrieve_pinfo()
        data.extend(['end', ''])

        # Create dissector function for each dissector
        for child in self.dissectors.values():
            data.append('-- Dissector function for: %s (platform: %s)' % (
                    child.name, child.platform.name))
            func = 'function {name}(buffer, pinfo, tree)'
            data.append(func.format(name=child._func_name))

            # Add subtree
            sub_tree = '\tlocal subtree = tree:{add}({var}, buffer())'
            data.append(sub_tree.format(add=child.add_var, var=self.var))
            data.append('\t{func}(pinfo, subtree)'.format(func=pinfo_func))

            # Add the actual field code for each field
            data.append(child.get_code(0))
            data.extend(['end', ''])

        return '\n'.join(i for i in data if i is not None)