Skip to content

Commit

Permalink
Splitting FieldExtractor into Field and FieldInfo to more closely res…
Browse files Browse the repository at this point in the history
…emble the wireshark API.
  • Loading branch information
MarkoPaul0 committed Feb 28, 2019
1 parent e78c7ee commit 3a2707a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 35 deletions.
2 changes: 1 addition & 1 deletion wirebaitlib/dissector/DissectorRunner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function DissectorRunnerClass.new(options_table) --[[options_table uses named ar
newgt.Int64 = require("wirebaitlib.primitives.Int64");
newgt.ProtoField = require("wirebaitlib.dissector.ProtoField");
newgt.Proto = require("wirebaitlib.dissector.Proto").new;
newgt.Field = require("wirebaitlib.dissector.FieldExtractor").Field;
newgt.Field = require("wirebaitlib.dissector.Field");
newgt.ftypes = newgt.ProtoField.ftypes;
newgt.base = newgt.ProtoField.base;
newgt.__wirebait_state = createRunnerState(); --TODO: this is not used
Expand Down
76 changes: 76 additions & 0 deletions wirebaitlib/dissector/Field.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
--[[
WireBait for wirebait is a lua package to help write Wireshark
Dissectors in lua
[Wirebait on Github](https://github.com/MarkoPaul0/WireBait)
Copyright (C) 2015-2017 Markus Leballeux
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
]]

local FieldInfoClass = require("wirebaitlib.dissector.FieldInfo");

local FieldClass = {};

--TODO: enforce the fact that a field must be defined before Taps or Dissectors get called
--[[
//Constructor
<Field> FieldClass.new(<string> fieldname)
example of fieldname = "ip.src"
]]
function FieldClass.new(fieldname)
local field = {
m_field_info = nil;
name = fieldname;
display = nil;
type = nil;
};

---------------------------------------------- initialization ------------------------------------------------------

for _, v in pairs(__wirebait_state.proto.fields) do
if v.m_abbr == fieldname then
field.m_field_info = FieldInfoClass.new(v);
field.name = v.m_abbr;
field.display = v.m_name;
field.type = v.m_type;
end
end
if not field.m_field_info then
error("The dissector has no defined field '" .. field_path .. "' the field extractor could find!");
end

------------------------------------------------ metamethods -------------------------------------------------------

function field:__tostring()
return self.name;
end

--TODO: enfore the fact that fields cannot be used outside of dissector or tap
function field:__call()
return self.m_field_info;
end

setmetatable(field, field)

return field;
end

--[[TODO: we need a Field.List() (which might not be defined in this library) as described here:fieldname
--https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Field.html#lua_class_Field
--c.f. item 11.2.1.2. Field.list()
]]

return FieldClass;
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,49 @@

--TODO: this class is a work in progress

local FieldExtractor = { FieldInfo = {}, Field = {} };
local FieldInfoClass = {};

function FieldExtractor.FieldInfo.new(protofield)
function FieldInfoClass.new(protofield)
assert(protofield);
local field_info = {
m_protofield = protofield;
}

------------------------------------------------ metamethods -------------------------------------------------------

function field_info:__len()
--print("[WARNING] FieldInfo:__len() is not supported yet! Contact MarkoPaul0, the Wirebait developer.");
return 0;
error("FieldInfo:__len() is not supported yet!");
end

function field_info:__unm()
error("FieldInfo:__unm() is not supported yet! Contact MarkoPaul0, the Wirebait developer.");
error("FieldInfo:__unm() is not supported yet!");
end

function field_info:__call()
return self.m_protofield:getValueFromBuffer(self.m_protofield.m_last_buffer);
--error("TODO: FieldInfo:__call()");
end

setmetatable(field_info, field_info)
return field_info;
end

function FieldExtractor.Field.new(field_path) --Field Extractors
local field = {
m_info = nil;
name = nil;
display = nil;
};

for k, v in pairs(__wirebait_state.proto.fields) do
if v.m_abbr == field_path then
field.m_info = FieldExtractor.FieldInfo.new(v);
field.name = v.m_abbr;
field.display = v.m_name;
field['type'] = v.m_type;
end
function field_info:__tostring()
return self.m_protofield:getDisplayValueFromBuffer(self.m_protofield.m_last_buffer);
end
if not field.m_info then
error("The dissector has no defined field '" .. field_path .. "' the field extractor could find!");

function field_info.__le()
error("FieldInfo.__le() is not supported yet");
end

function field:__tostring()
return self.name;
function field_info.__lt()
error("FieldInfo.__lt() is not supported yet");
end

function field:__call()
return self.m_info;
function field_info.__eq()
error("FieldInfo.__eq() is not supported yet");
end

setmetatable(field, field)
--table.insert(state.field_extractors, field);
return field;
setmetatable(field_info, field_info)

return field_info;
end

return FieldExtractor;


return FieldInfoClass;

0 comments on commit 3a2707a

Please sign in to comment.