aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/lua/lua_tap.c
blob: 54aa9a1c30ab8cd6f0faf5d7346ffd9b2f580780 (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
/*
 * lua_tap.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"

LUA_CLASS_DEFINE(Tap,TAP,NOP);
LUA_CLASS_DEFINE(Field,FIELD,NOP);

static int Field_get (lua_State *L) {
    const gchar* name = luaL_checkstring(L,1);
    Field i;
    
    if (!name) return 0;
    
    i = proto_registrar_get_byname(name);
    
    if (!i) {
        luaL_error(L,"Could not find field `%s'",name);
        return 0;
    }
    
    pushField(L,i);
    return 1;
}    

static int Field_fetch (lua_State* L) {
    Field in = checkField(L,1);
    int items_found = 0;
    
    for (;in;in = in->same_name_next) {
        GPtrArray* found = proto_find_finfo(lua_tree, in->id);
        guint i;
        
        for (i=0; i<found->len; i++) {
            field_info* fi = g_ptr_array_index(found,i);
            switch(fi->hfinfo->type) {
                case FT_UINT8:
                case FT_UINT16:
                case FT_UINT24:
                case FT_UINT32:
                case FT_FRAMENUM:
                case FT_INT8:
                case FT_INT16:
                case FT_INT24:
                case FT_INT32:
                    lua_pushnumber(L,(lua_Number)fvalue_get_integer(&(fi->value)));
                    items_found++;
                    break;
                case FT_FLOAT:
                case FT_DOUBLE:
                    lua_pushnumber(L,(lua_Number)fvalue_get_floating(&(fi->value)));
                    items_found++;
                    break;
                case FT_UINT64:
                case FT_INT64:
                    /* XXX: get them as strings for now */
                case FT_STRING:
                case FT_STRINGZ:
                case FT_ETHER:
                case FT_BYTES:
                case FT_UINT_BYTES:
                case FT_IPv4:
                case FT_IPv6:
                case FT_IPXNET:
                case FT_GUID:
                case FT_OID:
                    lua_pushstring(L,fvalue_to_string_repr(&fi->value,FTREPR_DISPLAY,NULL));
                    items_found++;
                    break;
                default:
                    luaL_error(L,"FT_ not yet supported");
                    return items_found;
            }
        }
    }
    
    return items_found;
    
}

static int Field_tostring (lua_State* L) {
    Field in = checkField(L,1);
    
    lua_pushfstring(L,"Field: %s",in->abbrev);
    return 1;
}

static const luaL_reg Field_methods[] = {
    {"get", Field_get},
    {"fetch", Field_fetch },
    {0,0}
};

static const luaL_reg Field_meta[] = {
    {"__tostring", Field_tostring},
    {0, 0}
};

int Field_register(lua_State* L) {
    luaL_openlib(L, FIELD, Field_methods, 0);
    luaL_newmetatable(L, FIELD);
    luaL_openlib(L, 0, Field_meta, 0);
    lua_pushliteral(L, "__index");
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
    lua_pushliteral(L, "__metatable");
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
    lua_pop(L, 1);
    
    return 1;
};


static int Tap_new(lua_State* L) {
    const gchar* name = luaL_checkstring(L,1);
    Tap tap;
    
    if (!name) return 0;
    
    if( find_tap_id(name) ) {
        luaL_error(L,"a tap with this name exists already");
        return 0;
    }

    tap = g_malloc(sizeof(struct _eth_tap));
    
    tap->name = g_strdup(name);
    tap->interesting_fields = g_ptr_array_new();
    tap->filter = NULL;
    
    pushTap(L,tap);
    return 1;
}

static int Tap_add(lua_State* L) {
    Tap tap = checkTap(L,1);
    Field in = checkField(L,2);
    
    if (!(tap && in)) return 0;
    
    g_ptr_array_add(tap->interesting_fields,in);
    
    return 0;
}

static int Tap_tostring(lua_State* L) {
    Tap tap = checkTap(L,1);
    gchar* str;
    
    if (!tap) return 0;
    
    str = g_strdup_printf("Tap->%s filter: %s",tap->name, tap->filter ? tap->filter : "NONE");
    lua_pushstring(L,str);
    g_free(str);
    
    return 1;
}

static int Tap_set_filter(lua_State* L) {
    Tap tap = checkTap(L,1);
    const gchar* filter = luaL_checkstring(L,2);
    
    if (!(tap && filter)) return 0;
    
    if (tap->filter) {
        luaL_error(L,"tap has filter already");
        return 0;
    }

    tap->filter = g_strdup(filter);
    
    return 0;
}

static int Tap_register_to_ethereal(lua_State*L) {
    Tap tap = checkTap(L,1);
    GString* filter_s;
    GString* error;
    GPtrArray* ins;
    guint i;

    if (!tap) return 0;

    filter_s = g_string_new("");
    
    if (tap->filter)
        g_string_sprintfa(filter_s,"( %s ) && frame ",tap->filter);
    
    g_free(tap->filter);

    ins = tap->interesting_fields;

    for (i=0; i < ins->len; i++) {
        Field in = g_ptr_array_index(ins,i);
        g_string_sprintfa(filter_s," ||%s",in->abbrev);
    }

    tap->filter = filter_s->str;
    g_string_free(filter_s,FALSE);

    error = register_tap_listener("frame", tap, tap->filter, lua_tap_reset, lua_tap_packet, lua_tap_draw);

    if (error) {
        luaL_error(L,"tap registration error: %s",error);
        g_string_free(error,TRUE);
    }
    
    return 0;
}

static const luaL_reg Tap_methods[] = {
    {"new", Tap_new},
    {"add", Tap_add},
    {"set_filter", Tap_set_filter},
    {"register", Tap_register_to_ethereal},
    {0,0}
};

static const luaL_reg Tap_meta[] = {
    {"__tostring", Tap_tostring},
    {0, 0}
};

int Tap_register(lua_State* L) {
    luaL_openlib(L, TAP, Tap_methods, 0);
    luaL_newmetatable(L, TAP);
    luaL_openlib(L, 0, Tap_meta, 0);
    lua_pushliteral(L, "__index");
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
    lua_pushliteral(L, "__metatable");
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
    lua_pop(L, 1);
    
    return 1;
};