How to add to a Lua DissectorTable? - lua

I'm writing a Lua dissector for Wireshark for a complex protocol. The protocol has a message header that includes a msgType field. I want to write a subdissector for each message type, with each subdissector stored in a separate source file.
My top-level script is general.lua which dissects the message header and creates the dissector table:
DissectorTable.new("myProtocol.Message")
dofile(DATA_DIR.."cplane.lua")
cplane.lua is a subdissector for message type 'cplane' and includes the code:
my_dissector_table = DissectorTable.get("myProtocol.Message")
my_dissector_table:add(0x02, myProtocol_cplane_proto)
Both scripts are in the same subdirectory of Wireshark's plugins directory.
When I load the plugins I get error:
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins\2.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
Lua: Error during loading:
[string "C:\Program Files (x86)\Wireshark\plugins\2.4...."]:170: bad
argument #1 to 'dofile' (dofile: file does not exist)
How can I fix this? Is the problem to do with the loading order of the scripts? Is the dofile() call necessary?

It is not necessary to use dofile as all scripts in the plugins directory are loaded. The order of loading is however not fixed (at least, it is not documented to be fixed). Currently Lua plugins are loaded after other dissectors, so trying to lookup dissector tables in the "global scope" will only work for built-in dissectors, such as tcp.port:
local myproto = Proto("myproto", "My Protocol")
function myproto.dissector(tvb, pinfo, tree)
...
end
-- Register with a built-in dissector table
DissectorTable.get("tcp.port"):add(1234, myproto)
For registering with custom dissector tables, this registration has to be deferred. In C dissectors, you would put the registration in proto_reg_handoff_PROTOABBREV (where PROTOABBREV should be substituted accordingly), but in Lua there is no such function.
The closest you can get is the "init" routine (a property of the Proto class, proto.init). These are called when a capture file is opened, before dissecting any packets. Example:
function myproto.init()
DissectorTable.get("your-custom-table"):add(1234, myproto)
end

Lua: Error during loading: [string "C:\Program Files
(x86)\Wireshark\plugins\2.4...."]:9: bad argument
#1 to 'get' (DissectorTable_get: no such dissector_table)
Answer: This error means that Dissector table is not found. Reason could be that the path is not correct, or the sequence of the file execution.
Lua: Error during loading: [string "C:\Program Files
(x86)\Wireshark\plugins\2.4...."]:170: bad argument #1 to 'dofile'
(dofile: file does not exist)
Answer: For me this error is gone by entering the exactly correct path

Related

neovim init.lua module "lua.plguins" not found

I'm in the configuration neovim
I added the configuration file in C:\Users\wupan\AppData\Local\nvim\init.lua
require("lua.plugins")
and C:\Users\wupan\AppData\Local\nvim\lua\plugins.lua
return require('packer').startup(function()
use "wbthomason/packer.nvim"
end)
After the start-up neovim
Error detected while processing C:\Users\wupan\AppData\Local\nvim\init.lua:
E5113: Error while calling lua chunk: C:\Users\wupan\AppData\Local\nvim\init.lua:1: module 'lua.plugins' not found:
no field package.preload['lua.plugins']
no file '.\lua\plugins.lua'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\lua\plugins.lua'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\lua\plugins\init.lua'
no file '.\lua\plugins.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\plugins.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\loadall.dll'
no file '.\lua.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua.dll'
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\loadall.dll'
stack traceback:
[C]: in function 'require'
C:\Users\wupan\AppData\Local\nvim\init.lua:1: in main chunk
Press ENTER or type command to continue
May I ask why
I tried to install Lua test the require
scoop install lua
C:\Users\wupan\code\lua\test.lua
require("hello")
C:\Users\wupan\code\lua\hello.lua
print("hello world")
There are also problems
C:\Users\wupan\scoop\apps\lua\current\lua54.exe: .\test.lua:1: module 'hello' not found:
no field package.preload['hello']
no file 'C:\Users\wupan\scoop\apps\lua\current'
no file 'C:\Users\wupan\scoop\apps\lua\current'
stack traceback:
[C]: in function 'require'
.\test.lua:1: in main chunk
[C]: in ?
Problem analysis
Let's use the following example which you gave us:
C:\Users\wupan\AppData\Local\nvim\init.lua
require("lua.plugins")
C:\Users\wupan\AppData\Local\nvim\lua\plugins.lua
return require('packer').startup(function()
use "wbthomason/packer.nvim"
end)
And now let's take a look into your error message:
no file 'C:\Users\wupan\scoop\apps\neovim\current\bin\lua\lua\plugins.lua'
The important part is the bin\lua\lua thing here. It's because your
require("lua.plugins") starts to look up a directory in one of your runtime
path (see :h rtp for more information).
Solution
You just need to fix your content in
C:\Users\wupan\AppData\Local\nvim\init.lua to:
require("plugins")
because then neovim will look up a directory which is named plugins in
C:\Users\wupan\scoop\apps\neovim\current\bin\lua for example, since its in
your runtime path.
Little note
You can change your content from
C:\Users\wupan\AppData\Local\nvim\lua\plugins.lua to:
require('packer').startup(function()
use "wbthomason/packer.nvim"
end)
or how I did it:
local packer = require('packer')
packer.startup(function(use)
use 'wbthomason/packer.nvim'
end)
That makes it a little bit more readable in my opinion :)

Lua error -129 for most mediawiki templates

I've been attempting to import some wikipedia templates in my local mediawiki wiki, but most attempts at using them end up with the same error. For instance, for the following code :
==Title==
{{Main|Title}}
(which uses the main template), I get the following error :
Lua error: Internal error: The interpreter has terminated with signal "-129".
No further details are available.
I haven't been able to find any occurence of that error, or even a relevant use of 129 in the source code I could find, and there is nothing generated in the Lua error log.
The Lua binaries have all been given permission to be executed, and I have installed the Scribunto and TemplateStyles extensions and a variety of modules, although beyond the ones advised for main, I don't know where to find a list of missing modules if there are any.
Edit : Here is the output of $status in Scribunto :
array(8) {
["command"]=> string(459) "'exec' '/bin/sh' '/XXX/extensions/Scribunto/includes/engines/LuaStandalone/lua_ulimit.sh' '7' '8' '51200' ''\''/XXX/extensions/Scribunto/includes/engines/LuaStandalone/binaries/lua5_1_5_linux_32_generic/lua'\'' '\''/XXX/extensions/Scribunto/includes/engines/LuaStandalone/mw_main.lua'\'' '\''/XXX/extensions/Scribunto/includes'\'' '\''0'\'' '\''4'\'''"
["pid"]=> int(17067)
["running"]=> bool(false)
["signaled"]=> bool(true)
["stopped"]=> bool(false)
["exitcode"]=> int(-1)
["termsig"]=> int(-129)
["stopsig"]=> int(0)
}
I am beginning to suspect that the issue may be that my server doesn't allow executables.

How to open file for writing?

I'm trying to open and write to a file using Dart's IO library.
I have this code:
File file = File("text.txt");
RandomAccessFile raf = file.openSync();
raf.writeStringSync("A string!");
Now when doing this I get the following error in the console:
(OS Error: Access is denied., errno = 5)
So file is not opened for writing, and I'm looking here: open method, and can't figure out how to use open or openSync to get RandomAccessFile I can write to.
It says I need to use write constant to do that but just can't figure out how?
If I try to create FileMode and add it to open method as an argument I get an error saying:
Error: Too many positional arguments: 0 allowed, but 1 found.
So open and openSync methods can't take any arguments, how would one use FileMode, and open method to open a file that is ready for writing? So I need to get RandomAccessFile that is in writing mode? And by default its only in read mode? I'm not trying to use writeString or writeStringSync, I know those methods exist, but I'm interested in how is this done using open and openSync methods that return RandomAccessFile!
Update:
You are getting this error:
Error: Too many positional arguments: 0 allowed, but 1 found.
because the openSync method has no positional arguments, but just one named parameter (mode).
So to fix your code you must add it:
RandomAccessFile raf = file.openSync(mode: FileMode.append); //Or whatever mode you'd to apply
Having said that, there are several other ways to write to a file, most of them listed in the docs:
writeString or writeStringSync, I'd suggest these if what you need is just to write once to a file.
openWrite, which returns a Stream that can be written in order to write to the file.
(All of these methods have a FileMode mode named parameter)

lua - invalid argument type

I am a newbie to Lua. Currently getting the following error message:
invalid argument type for argument -model (should be the model checkpoint
to use for sampling)
Usage: [options] <model>
I am sure it is something pretty easy to solve, but cannot manage to find the solution.
The 'model' is a file lm_checkpoint_epoch50.00_2.7196.t7, which is in the directory
/home/ubuntu/xxx/nn/cv
I am running the program from the parent directory (/home/ubuntu/xxx/nn)
I have tried out the following options to run the program (from one directory above the one the model is saved):
th sample.lua - model lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua /cv/lm_chelm_checkpoint_epoch50.00_2.5344.t7
th sample.lua - /cv/model lm_chelm_checkpoint_epoch50.00_2.5344.t7
Also, the program has a torch.CmdLine() object where :argument equals '/cv/lm_checkpoint_epoch50.00_2.7196.t7'. The program prints the parameters, so that you see the following output on the screen:
Options
<model> /cv/lm_checkpoint_epoch50.00_2.7196.t7
so it finds a value for argument 'model', which is picked up from the .lua file, not the parameter in the command line. This file is a valid mode.
Pretty lost, hope someone relates to this issue. Thanks.
found the issue - it was a bug as smhx suggested. I inadvertently changed the source code from:
require 'torch'
cmd = torch.CmdLine()
cmd:argument('-model','model checkpoint to use for sampling')
Note that there is no argument in the source code. To:
cmd:argument('-model','/cv/model lm_chelm_checkpoint_epoch50.00_2.5344.t7'
'model checkpoint to use for sampling')
So the argument must be passed through the command line, not the source code. With parameters, it is different - you can include them in the source code.
So if I change back the source code and run the following from the command line:
th sample.lua cv/lm_chelm_checkpoint_epoch50.00_2.5344.t7
it works.

How to create a file name with current date & time in lua?

I want to write a table into a file which named by the date and time it created.
I can open a file with hard coded name, write the table into it, like below:
FILENAME_EVENTS="Events.txt" -- filename in string
local fp=io.open(FILENAME_EVENTS, a) -- open a new file with the file name
io.output(FILENAME_EVENTS) -- redirect the io output to the file
-- write the table into the file
for i, e in ipairs(eventlist) do io.write(e.title, e.category, e.ds, e.de, e.td) end
But when I try to:
FILENAME_EVENTS=os.date().."\.txt" -- filename in string with date
local fp=io.open(FILENAME_EVENTS, a) -- open a new file with the file name
io.output(FILENAME_EVENTS) -- redirect the io output to the file
-- write the table into the file
for i, e in ipairs(eventlist) do io.write(e.title, e.category, e.ds, e.de, e.td) end
I got an error
bad argument #1 to 'output' (10/06/11 17:45:01.txt: Invalid argument)
stack traceback:
[C]: in function 'output'
Why this "10/06/11 17:45:01.txt" is an invalid argument? due to it contains spaces or '/'? Or any other reasons?
BTW, the platform is win7 Pro + Lua 5.1.4 for win
Apparently it's both / and : that bork. The first probably because it is regarded as directory separator. This can be demonstrated as below:
fn=os.date()..'.txt'
print(io.open(fn,'w')) -- returns invalid argument
fn=os.date():gsub(':','_')..'.txt'
print(io.open(fn,'w')) -- returns nil, no such file or directory
fn=os.date():gsub('[:/]','_')..'.txt'
print(io.open(fn,'w')) -- returns file(0x...), nil <-- Works
BTW, instead of using strange gsub and concatenation tricks, you might also consider using something like
fn=os.date('%d_%m_%y %H_%M.txt')

Resources