wireshark lua debug.getinfo is nil - lua

I'm currently writing some dissector for Wireshark in Lua. The Lua code has become quite large. Because of that I'm splitting it up into multiple files (modules). I got that working. By the way my goal is that the user just needs to copy the files into the plugins directory so that the dissector is automatically loaded every time Wireshark is started.
Now, to gain access to the other files from the "main file" I need to do this:
package.prepend_path(".\plugins\3.3\modulesDir")
local mymodule= require "module"
This works fine, but it has some disadvantages. Most importantly if a user uses a different version of Wireshark I need to change the path in the Lua code. Same if it's a different directory (Linuy, Mac OS).
To get around this I did some research on how to get the path of the current Lua file and came up with this:
local moduleDir = debug.getinfo(2, "S").source:sub(2)
moduleDir = moduleDir:match("(.*[/\\])")
This works platform indepedently, so it looks to be the perfect solution for what I want. If I execute this using Wireshark > Tools > Lua > Evaluate it work perfectly fine.
BUT: If I do it in the Lua file (which is my dissector) then I get the error "attempt to index a nil value". I tried various different versions of this line but it always appears that the debug table is nil. I'm using Wireshark version 3.3.
Has anyone an idea how to get it running? Or a different approach to getting the directory where the Lua file is in? Thanks in advance.

If you are looking for "global configuration directory" then you can use Dir.global_config_path(). Here you have that init.lua is in this path, and here lua function dtails.

On Windows, I have my Lua files in the "Personal Lua Plugins" directory, which when you look at Wireshark's "Help -> About Wireshark -> Folders" dialog, is just %APPDATA%\Wireshark\plugins. So, perhaps you can just move your folder from path\to\plugins\3.3\modulesDir to just path\to\plugins\modulesDir?
I believe you will then only require:
package.prepend_path("modulesDir")
And this will allow your Lua dissector and modules to work not only with the Wireshark 3.3 development version, but also future releases as well. And if your dissector can't work with older versions of Wireshark for some reason, you can always do something like:
if get_version() < "3.3" then
return
end
Lastly, have a look at how Hadriel Kaplan solved this for his protobuf.lua dissector, where his required modules are in the "modules" directory. It's basically as I've described above. See: https://github.com/128technology/protobuf_dissector

Related

require os, Am i missing somehting?

I come from C-family "mainstream" langages and i'm currently giving a try in Lua .
I made a simple code that check for a user entry and try to open an URL (built with user entry) in the default browser.
Saw the command os.execute("start "URL") that failed, saying that "os is undefined".
Well, seemed to be logical. I then researched the reason and discovered the "require" key word (which seems to act as a LoadLibrary or kind).
This is where I'm lost !
All forums says "yeah yeah just add require os and it will do". But it actually fail !
I obviously suspect that i am missing a "file" or path pointing at that "os" description. And that it's so obvious nobody found useful enough to explain or ask for it.
Can someone explain me what does require, in details ? Which file am i supposed to add (if i really need to ?).
If someone also have an online lesson to advise me, i'll accept it with pleasure. I feel like i'm missing a lot of basics and that's really not a "try to step-up" friendly langage
The standard Lua environment has os available without using require, so you must be using a non-standard Lua environment.
When Lua is embedded into different software, access to libraries like os is usually removed, as it is a security risk. (For example, if you allowed full access to the os library to anyone using Lua on a webserver, it would mean that anyone could run random shell commands on that server.)
If your Lua environment has been altered in this way, then there is a good chance that you will never be able to use the os library whatever you do.

execute lua string as lua code

I want to share lua modules with coworkers. In order to get the latest version of shared modules I want to store and fetch them with a web server.
My questions is:
Is it possible to load lua code directly from http request or string?
I want to achieve something like that :
module = [[
local sharedModule = {}
function sharedModule.greet(name) print("hello " .. name) end
return sharedModule
]]
greeter = require (module)
greeter.greet("john")
Maybe this is not the right thing to do. Is there a better approach than this one?
There's a whole section in Programming in Lua devoted to that. Your needs will be directly fulfilled with loadstring in Lua 5.1 and older, or load in Lua 5.2 and newer.
I would carefully verify the code you're actually executing, though. At the very least, version it (running a wrong version would most probably end up in all sorts of problems, if the code being run depends on the environment being in a certain state). Optimally checksum and sign the code, and verify the signature before doing anything. If your environment isn't protected, this is essentially a huge backdoor opening.
You could also use rings library to isolate the code you're running within the Lua environment itself. It might not be airtight security-wise, but should at least prevent the received code from crashing your application if/when it goes awry.

.kmod and .ko - difference?

Have been using Ndisgen to try to generate a .ko kernel module for an rtl8192se driver for my Freebsd 9 netbook having followed instructions found on several different dev blogger sites.
Somehow, i've just not been able to generate a file with extension .ko. Instead, i keep getting a .kmod file.
Question is, what is the difference between these ?
I have also attempted kldload for this .kmod file. When i check it via kldstat, ok, i see it there but, when i then check with dmesg and pciconf -lv, my realtek card is still not hooked up.
So i reckon i really need to generate the .ko file in the first place, but what am i doing wrong or missing, such that only a kmod is generated?
Any pointers would be appreciated! thanks! :)
Update::
There was a message I had ignored.
My bad!
the message after conversion was :
"...Cleaning up... rm: machine: is a directory cleanup failed.Exiting"
That's all because i had pasted a copy of the "/usr/include/machine" folder with all the headers i thought was required in the path where I was converting the driver.
But i ignored it thinking, well since ndisgen had already created a .kmod file(which was what I had assumed was also a kernel module, just not in .ko form) then it was alright.
SO finally, since it's complaining that it's a directory and can't be cleaned, i then created a symbolic link to that folder instead.
Et voila! the clean was successful and now i have the .ko file! :D
The ndisgen script renames the .ko file to .kmod temporarily to do some cleanup.
If that cleanup works, it should rename it back to a .ko file. See the drvgen function /usr/src/usr.sbin/ndiscvt/ndisgen.sh.
I'm assuming that something goes wrong in between both renames. Do you get any error messages?
Keep in mind that if you load the driver, it should show up as the ndis0 device!
Looks like you are getting a NetBSD kernel module, not a FreeBSD one. See these posts:
hubertf's NetBSD Blog
Modern net bsd kernel module
Is the source code that you are using available publicly for us to try follow your steps?

How to load plugins in the new tab in mac vim?

I'm using macvim to code rails project.
I used some plugins, which is specific to rails(like vim-rails) and will be loaded only in a rails' app folder.
After I entered a rails's folder, I run mvim and everything goes fine.
However, when I use command + T to open a new tab. the function of those plugins disabled..seems they are not loaded..
How to load them when I open a new tab?
If these plugins use the similar code to that one fugitive does, then putting something like
augroup LaunchFugitiveForAllBuffers
autocmd!
autocmd BufNew :doautocmd fugitive BufRead .
augroup END
(replace fugitive with actual event group name [1]). You can find this name by either grepping plugin files for BufRead (note: case does not really matter) or walking through the output of au BufRead like I did (there should not be many items). Note that things may be more complicated: for example your plugins attach to Filetype event and changing the above to doautocmd fugitive Filetype ruby may not help. Also note that you can purge out word fugitive at all leaving just a space, but it is potentially destructive operation and can be used only for testing (potentially very destructive in case of Filetype event and some others).
[1] Note: event group, not plugin name. These groups are likely to have the name that is a derivative of plugin name, but they are not forced to be equal to it.
Update: It seems that you need railsPluginDetect group for Tim Pope’s rails plugin. I do not have any rails project so I can’t say this for sure, but autocommand looks very similar to fugitive one. It is better though that you go to plugin bug tracker and add a request there (do not forget to search for an existing one).
Additional informations may be needed but I think that's because the new tab creates an empty virtual buffer.
Because your RoR-related plugins only work in a Rails folder and you are not in a Rails folder -- you are probably in ~, check :pwd to know what the working directory is -- those plugins don't work.

How to make RSense autocomplete and jump to definitions of a rails project?

I switched from netbeans to emacs and I am pretty happy with the change. The thing I am missing the most is autocompletion and jump to definitions. In order to get this I have installed Rsense. It works fine for the gems code, though, I cannot jump-to-definitions of my Rails project nor autocomplete according to the methods I defined.
I tried to add my project's load path to Rsense's load_path configuration, though, it still doesn't work.
Does anyone know how to get this working?
You can use tags for browsing through files and jumping directly to function definitions.
I use Exuberant Ctags (its got Ruby support). You can download it from here.
I am assuming that you are working on windows. Getting the tags to work initially on windows is a pain especially if you are using emacs for the first time.
These are the steps I followed:
Install Cygwin from here.
Include the cygwin\bin\ folder in your environment variable PATH. E.g. here
Install exhuberant ctags. Note that emacs may sometime have a built in ctags. Later on you will have to use the ctags command in cygwin to create tags. At that time you may encounter some errors in case it uses the ctags in emacs instead of exuberant ctag.
Once you have installed ctags, add that to the environmental variable PATH as well.
If you have a small project with relatively lesser number of files (<500). So you just need a single global TAGS file. For that open cygwin, change your directory to the root directory of your project and type in the command ctags -R -e Check this out for other approaches
Your tags file will be created. It will be named "TAGS" and will be present in the root directory of your project.
Next open emacs, and browse through the code. In case you come across a function and want to jump to its definition, put your cursor on the function name and press M-. your minibuffer should then show something like Find tag (default <function-name>): Press Enter and voila!!! you are magically transported to the function definition!!!
Note: You may have to specify the TAGS file the first time you use the M-. This needs to be done only once after emacs startup. You can also modify your .emacs file to take in the TAGS file automatically on startup.
Refer to this and this for more info for tags related commands in emacs.
Until now, I have been using rtags to jump to definitions. It's not perfect, but it does the trick in many cases.

Resources