4 * Wireshark's interface to the Lua Programming Language
5 * for file handling related source file internal functions.
7 * (c) 2014, Hadriel Kaplan <hadrielk@yahoo.com>
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * SPDX-License-Identifier: GPL-2.0-or-later
17 * The following is for handling private data for the duration of the file
18 * read_open/read/close cycle, or write_open/write/write_close cycle.
19 * In other words it handles the "priv" member of wtap and wtap_dumper,
20 * but for the Lua script's use. A Lua script can set a Lua table
21 * to CaptureInfo/CaptureInfoConst and have it saved and retrievable this way.
22 * We need to offer that, because there needs to be a way for Lua scripts
23 * to save state for a given file's operations cycle. Since there can be
24 * two files opened at the same time for the same Lua script (due to reload
25 * and other such events), the script can't just have one file state.
29 #include "wslua_file_common.h"
32 /* create and set the wtap->priv private data for the file instance */
33 void create_wth_priv(lua_State
* L
, wtap
*wth
) {
34 file_priv_t
*priv
= g_new(file_priv_t
, 1);
36 if (wth
->priv
!= NULL
) {
38 luaL_error(L
, "Cannot create wtap private data because there already is private data");
41 priv
->table_ref
= LUA_NOREF
;
42 wth
->priv
= (void*) priv
;
45 /* gets the private data table from wtap */
46 int get_wth_priv_table_ref(lua_State
* L
, wtap
*wth
) {
47 file_priv_t
*priv
= (file_priv_t
*) wth
->priv
;
50 /* shouldn't be possible */
51 luaL_error(L
, "Cannot get wtap private data: it is null");
55 /* the following might push a nil, but that's ok */
56 lua_rawgeti(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
61 /* sets the private data to wtap - the table is presumed on top of stack */
62 int set_wth_priv_table_ref(lua_State
* L
, wtap
*wth
) {
63 file_priv_t
*priv
= (file_priv_t
*) wth
->priv
;
66 /* shouldn't be possible */
67 luaL_error(L
, "Cannot get wtap private data: it is null");
71 if (lua_isnil(L
, -1)){
72 /* user is setting it nil - ok, de-ref any previous one */
73 luaL_unref(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
74 priv
->table_ref
= LUA_NOREF
;
78 if (!lua_istable(L
, -1)) {
79 luaL_error(L
, "The private_table member can only be set to a table or nil");
83 /* if we had a table already referenced, de-ref it first */
84 if (priv
->table_ref
!= LUA_NOREF
) {
85 luaL_unref(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
88 priv
->table_ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
93 /* remove, deref, and free the wtap->priv data */
94 void remove_wth_priv(lua_State
* L
, wtap
*wth
) {
95 file_priv_t
*priv
= (file_priv_t
*) wth
->priv
;
98 /* shouldn't be possible */
99 luaL_error(L
, "Cannot remove wtap private data: it is null");
103 luaL_unref(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
109 /* create and set the wtap_dumper->priv private data for the file instance */
110 void create_wdh_priv(lua_State
* L
, wtap_dumper
*wdh
) {
111 file_priv_t
*priv
= g_new(file_priv_t
, 1);
113 if (wdh
->priv
!= NULL
) {
115 luaL_error(L
, "Cannot create wtap_dumper private data because there already is private data");
118 priv
->table_ref
= LUA_NOREF
;
119 wdh
->priv
= (void*) priv
;
122 /* get the private data from wtap_dumper */
123 int get_wdh_priv_table_ref(lua_State
* L
, wtap_dumper
*wdh
) {
124 file_priv_t
*priv
= (file_priv_t
*) wdh
->priv
;
127 /* shouldn't be possible */
128 luaL_error(L
, "Cannot get wtap_dumper private data: it is null");
132 /* the following might push a nil, but that's ok */
133 lua_rawgeti(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
138 /* sets the private data to wtap - the table is presumed on top of stack */
139 int set_wdh_priv_table_ref(lua_State
* L
, wtap_dumper
*wdh
) {
140 file_priv_t
*priv
= (file_priv_t
*) wdh
->priv
;
143 /* shouldn't be possible */
144 luaL_error(L
, "Cannot get wtap private data: it is null");
148 if (lua_isnil(L
, -1)){
149 /* user is setting it nil - ok, de-ref any previous one */
150 luaL_unref(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
151 priv
->table_ref
= LUA_NOREF
;
155 if (!lua_istable(L
, -1)) {
156 luaL_error(L
, "The private_table member can only be set to a table or nil");
160 /* if we had a table already referenced, de-ref it first */
161 if (priv
->table_ref
!= LUA_NOREF
) {
162 luaL_unref(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
165 priv
->table_ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
170 /* remove and deref the wtap_dumper->priv data */
171 void remove_wdh_priv(lua_State
* L
, wtap_dumper
*wdh
) {
172 file_priv_t
*priv
= (file_priv_t
*) wdh
->priv
;
175 /* shouldn't be possible */
176 luaL_error(L
, "Cannot remove wtap_dumper private data: it is null");
180 luaL_unref(L
, LUA_REGISTRYINDEX
, priv
->table_ref
);
181 /* we do NOT free wtap_dumper's priv member - wtap_dump_close() free's it */
186 * Editor modelines - https://www.wireshark.org/tools/modelines.html
191 * indent-tabs-mode: nil
194 * vi: set shiftwidth=4 tabstop=8 expandtab:
195 * :indentSize=4:tabSize=8:noTabs=true: