Skip to content

Commit

Permalink
dsl: Allow usage of _ as a name for anonymous hidden fields.
Browse files Browse the repository at this point in the history
Fixes #10.
  • Loading branch information
Snaipe committed Nov 6, 2016
1 parent 693bbdc commit a555d7d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/wssdl/core.lua
Expand Up @@ -156,7 +156,7 @@ setmetatable(wssdl._packet, {
local out = pkt._create(pkt, ...)

for k, v in pairs(wssdl._current_def) do
if k:sub(1,1) ~= '_' then
if type(v) == 'table' then
v._pktdef = nil
end
end
Expand Down Expand Up @@ -278,7 +278,7 @@ setmetatable(wssdl, {
newpacket._properties = newprops
setmetatable(newpacket, getmetatable(wssdl._packet))

wssdl._current_def = { _pktdef = newpacket }
wssdl._current_def = {}

-- Switch to the packet definition metatable
local env = setmetatable({}, packetdef_metatable)
Expand Down
24 changes: 17 additions & 7 deletions src/wssdl/placeholder.lua
Expand Up @@ -305,14 +305,24 @@ placeholder.metatable = function(defenv, packetdef_metatable, make_pktfield)
return nil
end

if field._name:sub(1,1) == '_' then
error('wssdl: Invalid identifier for field ' .. utils.quote(field._name) .. ': Fields must not start with an underscore', 3)
end
if wssdl._current_def[field._name] and wssdl._current_def[field._name] ~= field then
error('wssdl: Duplicate field ' .. utils.quote(field._name) .. ' in packet definition.', 3)
end
if wssdl._current_def[field._name] ~= field then
if wssdl._current_def[field._name] then
error('wssdl: Duplicate field ' .. utils.quote(field._name) .. ' in packet definition.', 3)
end

wssdl._current_def[field._name] = field
if field._name ~= '_' then
if field._name:sub(1,1) == '_' then
error('wssdl: Invalid identifier for field ' .. utils.quote(field._name) .. ': Fields must not start with an underscore', 3)
end
else
local i = wssdl._current_def._anonymous_counter or 1
field._name = '_anonymous_' .. i .. ''
wssdl._current_def._anonymous_counter = i + 1
field._hidden = true
end

wssdl._current_def[field._name] = field
end

local type = rawget(specifiers.field_types, k)
if type == nil then
Expand Down
13 changes: 10 additions & 3 deletions src/wssdl/wireshark.lua
Expand Up @@ -99,7 +99,9 @@ ws.make_fields = function (fields, pkt, prefix)
local prefix = prefix or ''

for i, field in ipairs(pkt._definition) do
make_field(fields, prefix, field)
if not field._hidden then
make_field(fields, prefix, field)
end
end
end

Expand Down Expand Up @@ -225,12 +227,17 @@ ws.dissector = function (pkt, proto)
local labels = pktval.label[field._name] or {}
local val = pktval.val[field._name]
local raw = pktval.buf[field._name]
local node

if field._type == 'packet' then
local pktval = { buf = raw, label = labels, val = val }
node = tree:add(protofield, raw._self, '', unpack(labels))
if field._hidden then
node = tree:add(protofield, raw._self, '', '')
else
node = tree:add(protofield, raw._self, '', unpack(labels))
end
tree_add_fields(field._packet, prefix .. field._name .. '.', node, pktval)
else
elseif not field._hidden then
node = tree:add(protofield, raw, val, unpack(labels))
end
end
Expand Down

0 comments on commit a555d7d

Please sign in to comment.