Skip to content

Commit

Permalink
Fix registration (#5)
Browse files Browse the repository at this point in the history
Managing modules load order using require doesn't always work (especially when installing the plugin globally. It is better to register subprotocols in init() function which is called when every module is registered.
  • Loading branch information
louisroyer authored Jun 25, 2021
1 parent 867fc5f commit 0c2080b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ stds.wireshark = {
"ProtoField",
"base",
"ftypes",
"package",
"set_plugin_info",
}
}
12 changes: 6 additions & 6 deletions rls-3-1.lua → protocol/rls-3-1.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
-- CC0-1.0 2021 - Louis Royer (<https://github.com/louisroyer/RLS-wireshark-dissector>)
--
--]]
require("rls")

local rlsProtocol31 = Proto("RLS-3.1", "UERANSIM 3.1.x Radio Link Simulation (RLS) Protocol")
local fields = rlsProtocol31.fields

Expand Down Expand Up @@ -128,7 +126,9 @@ function rlsProtocol31.dissector(buffer, pinfo, tree)
end
end

local rls = DissectorTable.get("rls")
rls:add(0x0301, rlsProtocol31)
local udp_port = DissectorTable.get("udp.port")
udp_port:add_for_decode_as(rlsProtocol31)
function rlsProtocol31.init()
local rls = DissectorTable.get("rls")
rls:add(0x0301, rlsProtocol31)
local udp_port = DissectorTable.get("udp.port")
udp_port:add_for_decode_as(rlsProtocol31)
end
12 changes: 6 additions & 6 deletions rls-3-2.lua → protocol/rls-3-2.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
-- CC0-1.0 2021 - Louis Royer (<https://github.com/louisroyer/RLS-wireshark-dissector>)
--
--]]
require("rls")

local rlsProtocol32 = Proto("RLS-3.2", "UERANSIM 3.2.x Radio Link Simulation (RLS) Protocol")
local fields = rlsProtocol32.fields

Expand Down Expand Up @@ -124,7 +122,9 @@ function rlsProtocol32.dissector(buffer, pinfo, tree)
end
end

local rls = DissectorTable.get("rls")
rls:add(0x0302, rlsProtocol32)
local udp_port = DissectorTable.get("udp.port")
udp_port:add_for_decode_as(rlsProtocol32)
function rlsProtocol32.init()
local rls = DissectorTable.get("rls")
rls:add(0x0302, rlsProtocol32)
local udp_port = DissectorTable.get("udp.port")
udp_port:add_for_decode_as(rlsProtocol32)
end
78 changes: 42 additions & 36 deletions rls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,59 @@
--
--]]

-- This file must only be loaded once, but each version requires it.
if package.loaded['rls'] == nil then
-- Update the following when adding new versions
local latestVersion = 0x0302
local oldestVersion = 0x0301
local pluginVersion = "1.1.1"
-- Update the following when adding new versions
local latestVersion = 0x0302
local oldestVersion = 0x0301

local rlsProtocol = Proto("RLS", "UERANSIM Radio Link Simulation (RLS) Protocol")
local rlsProtocol = Proto("RLS", "UERANSIM Radio Link Simulation (RLS) Protocol")
set_plugin_info({
version = pluginVersion,
author = "Louis Royer",
repository = "https://github.com/louisroyer/RLS-wireshark-dissector",
description = "Dissector for Radio Link Simulation Protocol"
})

-- Create a DissectorTable to register dissector for each version of RLS
DissectorTable.new("rls", "RLS version", ftypes.UINT32, base.HEX, rlsProtocol)
-- Create a DissectorTable to register dissector for each version of RLS
DissectorTable.new("rls", "RLS version", ftypes.UINT32, base.HEX, rlsProtocol)

-- Preferences
rlsProtocol.prefs.udp_port = Pref.uint("RLS UDP port", 4997, "UDP port for RLS")
-- Preferences
rlsProtocol.prefs.udp_port = Pref.uint("RLS UDP port", 4997, "UDP port for RLS")

-- Add version field
local fields = rlsProtocol.fields
fields.Version = ProtoField.string("rls.version", "Version")
-- Add version field
local fields = rlsProtocol.fields
fields.Version = ProtoField.string("rls.version", "Version")

function rlsProtocol.dissector(buffer, pinfo, tree)
-- Generic check
if buffer:len() == 0 then return end
if buffer(0, 1):uint() ~= 0x03 then return end
function rlsProtocol.dissector(buffer, pinfo, tree)
-- Generic check
if buffer:len() == 0 then return end
if buffer(0, 1):uint() ~= 0x03 then return end

pinfo.cols.protocol = rlsProtocol.name
pinfo.cols.protocol = rlsProtocol.name

local version = buffer(1,2):uint()
local subprotocol = DissectorTable.get("rls"):get_dissector(version)
local version = buffer(1,2):uint()
local subprotocol = DissectorTable.get("rls"):get_dissector(version)
if subprotocol == nil then
if version > latestVersion then
-- fallback to latest version
version = latestVersion
elseif version < oldestVersion then
-- fallback to oldest version
version = oldestVersion
end
subprotocol = DissectorTable.get("rls"):get_dissector(version)
if subprotocol == nil then
if version > latestVersion then
-- fallback to latest version
version = latestVersion
elseif version < oldestVersion then
-- fallback to oldest version
version = oldestVersion
end
subprotocol = DissectorTable.get("rls"):get_dissector(version)
if subprotocol == nil then
local versionNumber = buffer(1, 1):uint() .. "." .. buffer(2, 1):uint() .. "." .. buffer(3, 1):uint()
local subtree = tree:add(rlsProtocol, buffer(), "UERANSIM Radio Link Simulation (RLS) protocol")
pinfo.cols.info = "Unsupported version - Cannot decode"
subtree:add(fields.Version, buffer(1, 3), versionNumber)
return 4
end
local versionNumber = buffer(1, 1):uint() .. "." .. buffer(2, 1):uint() .. "." .. buffer(3, 1):uint()
local subtree = tree:add(rlsProtocol, buffer(), "UERANSIM Radio Link Simulation (RLS) protocol")
pinfo.cols.info = "Unsupported version - Cannot decode"
subtree:add(fields.Version, buffer(1, 3), versionNumber)
return 4
end
subprotocol:call(buffer():tvb(), pinfo, tree)
end
subprotocol:call(buffer():tvb(), pinfo, tree)
end

function rlsProtocol.init()
-- Export protocol
local udp_port = DissectorTable.get("udp.port")
udp_port:add(rlsProtocol.prefs.udp_port, rlsProtocol)
Expand Down

0 comments on commit 0c2080b

Please sign in to comment.