aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/lua/packet-lua.c
blob: e7a0b25c6c571e72a7910b60b9b433d1b58c4647 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * packet-lua.c
 *
 * Ethereal's interface to the Lua Programming Language
 *
 * (c) 2006, Luis E. Garcia Ontanon <luis.ontanon@gmail.com>
 *
 * $Id$
 *
 * Ethereal - Network traffic analyzer
 * By Gerald Combs <gerald@ethereal.com>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#include "packet-lua.h"
#include <epan/nstime.h>
#include <math.h>

static lua_State* L = NULL;

packet_info* lua_pinfo;
proto_tree* lua_tree;
tvbuff_t* lua_tvb;
int lua_malformed;
dissector_handle_t lua_data_handle;

static int lua_format_date(lua_State* LS) {
    lua_Number time = luaL_checknumber(LS,1);
    nstime_t then;
    gchar* str;
    
    then.secs = (guint32)floor(time);
    then.nsecs = (guint32) ( (time-(double)(then.secs))*1000000000);
    str = abs_time_to_str(&then);    
    lua_pushstring(LS,str);
    
    return 1;
}

static int lua_format_time(lua_State* LS) {
    lua_Number time = luaL_checknumber(LS,1);
    nstime_t then;
    gchar* str;
    
    then.secs = (guint32)floor(time);
    then.nsecs = (guint32) ( (time-(double)(then.secs))*1000000000);
    str = rel_time_to_str(&then);    
    lua_pushstring(LS,str);
    
    return 1;
}

static int lua_report_failure(lua_State* LS) {
    const gchar* s = luaL_checkstring(LS,1);
    report_failure("%s",s);
    return 0;
}

/* ethereal uses lua */

int lua_tap_packet(void *tapdata, packet_info *pinfo, epan_dissect_t *edt, const void *data _U_) {
    Tap tap = tapdata;
    
    lua_pushstring(L, "_ethereal_pinfo");
    pushPinfo(L, pinfo);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    lua_tree = edt->tree;
    
    /* XXX in C */
    lua_dostring(L,ep_strdup_printf("taps.%s(_ethereal_pinfo);",tap->name));
    
    return 1;
}

void lua_tap_reset(void *tapdata) {
    Tap tap = tapdata;
    /* XXX in C */
    lua_dostring(L,ep_strdup_printf("tap_resets.%s();",tap->name));
}

void lua_tap_draw(void *tapdata) {
    Tap tap = tapdata;
    /* XXX in C */
    lua_dostring(L,ep_strdup_printf("tap_draws.%s();",tap->name));
}

void dissect_lua(tvbuff_t* tvb, packet_info* pinfo, proto_tree* tree) {
    lua_pinfo = pinfo;
    lua_tree = tree;
    lua_tvb = tvb;

    /*
     * equivalent to Lua:
     * dissectors[current_proto](tvb,pinfo,tree)
     */
    
    lua_settop(L,0);

    lua_getglobal(L, LUA_DISSECTORS_TABLE);
    
    if (!lua_istable(L, -1)) {
        g_warning("either `" LUA_DISSECTORS_TABLE "' does not exist or it is not a table!");

        lua_pinfo = NULL;
        lua_tree = NULL;
        lua_tvb = NULL;
        
        return;
    }
    
    
    lua_pushstring(L, pinfo->current_proto);

    lua_gettable(L, -2);  

    lua_remove(L,1);

    
    if (!lua_isfunction(L,1)) {
        g_warning("`" LUA_DISSECTORS_TABLE ".%s' is not a function, is a %s",
                  pinfo->current_proto,lua_typename(L,lua_type(L,1)));

        lua_pinfo = NULL;
        lua_tree = NULL;
        lua_tvb = NULL;
        
        return;        
    }
    
    pushTvb(L, tvb);
    pushPinfo(L, pinfo);
    pushProtoTree(L, tree);
    
    switch ( lua_pcall(L,3,0,0) ) {
        case 0:
            /* OK */
            break;
        case LUA_ERRRUN:
            g_warning("Runtime error while calling dissectors.%s() ",pinfo->current_proto);
            break;
        case LUA_ERRMEM:
            g_warning("Memory alloc error while calling dissectors.%s() ",pinfo->current_proto);
            break;
        default:
            g_warning("-X2-");
            g_assert_not_reached();
            break;
    }

    lua_pinfo = NULL;
    lua_tree = NULL;
    lua_tvb = NULL;
}

static void init_lua(void) {
    GString* tap_error = register_all_lua_taps();
    
    if ( tap_error ) {
        report_failure("lua tap registration problem: %s",tap_error->str);
    }
    
    /* XXX in C */
    if (L)
        lua_dostring(L, "for k in init_routines do init_routines[k]() end;");
}

void proto_reg_handoff_lua(void) {
    lua_data_handle = find_dissector("data");
    /* XXX in C */
    if (L)
        lua_dostring(L, "for k in handoff_routines do handoff_routines[k]() end ;");
}

static const char *getF(lua_State *L _U_, void *ud, size_t *size)
{
    FILE *f=(FILE *)ud;
    static char buff[512];
    if (feof(f)) return NULL;
    *size=fread(buff,1,sizeof(buff),f);
    return (*size>0) ? buff : NULL;
}

void proto_register_lua(void)
{
    FILE* file;
    gchar* filename = getenv("ETHEREAL_LUA_INIT");
    
    /* TODO: 
        disable if not running with the right credentials
        
        if (euid == 0 && euid != ruid) return;
    */
    
    if (!filename) filename = get_persconffile_path("init.lua", FALSE);

    file = fopen(filename,"r");

    if (! file) return;
    
    register_init_routine(init_lua);    
    
    L = lua_open();
    
    luaopen_base(L);
    luaopen_table(L);
    luaopen_io(L);
    luaopen_string(L);

    ProtoField_register(L);
    ProtoFieldArray_register(L);
    SubTreeType_register(L);
    SubTreeTypeArray_register(L);
    ByteArray_register(L);
    Tvb_register(L);
    Proto_register(L);
    Column_register(L);
    Pinfo_register(L);
    ProtoTree_register(L);
    ProtoItem_register(L);
    Dissector_register(L);
    DissectorTable_register(L);
    Field_register(L);
    Columns_register(L);
    Tap_register(L);
    Address_register(L);
    
    lua_pushstring(L, "format_date");
    lua_pushcfunction(L, lua_format_date);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    lua_pushstring(L, "format_time");
    lua_pushcfunction(L, lua_format_time);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    lua_pushstring(L, "report_failure");
    lua_pushcfunction(L, lua_report_failure);
    lua_settable(L, LUA_GLOBALSINDEX);
            
    lua_pushstring(L, "handoff_routines");
    lua_newtable (L);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    lua_pushstring(L, "init_routines");
    lua_newtable (L);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    lua_pushstring(L, LUA_DISSECTORS_TABLE);
    lua_newtable (L);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    lua_pushstring(L, "taps");
    lua_newtable (L);
    lua_settable(L, LUA_GLOBALSINDEX);

    lua_pushstring(L, "tap_resets");
    lua_newtable (L);
    lua_settable(L, LUA_GLOBALSINDEX);

    lua_pushstring(L, "tap_draws");
    lua_newtable (L);
    lua_settable(L, LUA_GLOBALSINDEX);
    
    if (lua_load(L,getF,file,filename) || lua_pcall(L,0,0,0))
        fprintf(stderr,"%s\n",lua_tostring(L,-1));
    
    fclose(file);
    
    lua_data_handle = NULL;
    lua_pinfo = NULL;
    lua_tree = NULL;
    lua_tvb = NULL;
    
    lua_malformed = proto_get_id_by_filter_name("malformed");
    
}