forked from ashdnazg/pyreshark
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyreshark.c
More file actions
245 lines (208 loc) · 7.25 KB
/
pyreshark.c
File metadata and controls
245 lines (208 loc) · 7.25 KB
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
/* pyreshark.c
*
* Pyreshark Plugin for Wireshark. (https://github.com/ashdnazg/pyreshark)
*
* Copyright (c) 2013 by Eshed Shaham
*
* 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.
*/
/* The following two lines prevent redefinition of ssize_t on win64*/
#define _SSIZE_T_DEFINED
#define QT_VERSION
#include "config.h"
#include "pyreshark.h"
#include "python_loader.h"
#include "param_structs.h"
#include <file.h>
#include <glib.h>
#include <epan/packet.h>
#include <epan/expert.h>
#include <epan/filesystem.h>
int g_num_dissectors = 0;
py_dissector_t **g_dissectors = NULL;
python_lib_t *g_python_lib;
void
init_pyreshark(void)
{
char *py_init_path;
char *python_cmd;
void *py_init_file;
python_version_t py_version = PYTHON_VERSION_NOT_SET;
g_python_lib = load_python(&py_version);
if (g_python_lib == NULL)
{
return;
}
python_cmd = g_strdup_printf("import sys;sys.path.append(\'%s\')", get_datafile_path(PYTHON_DIR));
g_python_lib->PyRun_SimpleStringFlags(python_cmd, NULL);
g_free(python_cmd);
py_init_path = get_datafile_path(PYTHON_DIR G_DIR_SEPARATOR_S PYRESHARK_INIT_FILE);
py_init_file = g_python_lib->PyFile_FromString((char *)py_init_path, (char *)"rb");
if (NULL == py_init_file)
{
printf("Can't open Pyreshark init file: %s\n", py_init_path);
g_free(py_init_path);
return;
}
g_free(py_init_path);
if (py_version == PYTHON_VERSION_27)
{
g_python_lib->PyRun_SimpleFileExFlags(g_python_lib->PyFile_AsFile(py_init_file), PYRESHARK_INIT_FILE, FALSE, NULL);
} else {
g_python_lib->PyRun_SimpleFileExFlags(g_python_lib->PyFile_AsFile(py_init_file), PYRESHARK_INIT_FILE, TRUE, NULL);
}
g_python_lib->Py_DecRef(py_init_file);
}
void
handoff_pyreshark(void)
{
if (g_python_lib != NULL)
{
g_python_lib->PyRun_SimpleStringFlags("g_pyreshark.handoff()", NULL);
}
}
void
dissect_pyreshark(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
int i;
for (i=0;i<g_num_dissectors; i++)
{
if (strcmp(g_dissectors[i]->name, pinfo->current_proto) == 0)
{
dissect_proto(g_dissectors[i], tvb, pinfo, tree);
return;
}
}
if (tree)
{
expert_add_info_format(pinfo, NULL, PI_MALFORMED,
PI_ERROR, "PyreShark: protocol %s not found",
pinfo->current_proto);
}
}
void
dissect_proto(py_dissector_t *py_dissector, tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
{
int i;
int offset = 0;
tvb_and_tree_t tvb_and_tree = {tvb, tree};;
for (i=0;i<py_dissector->length;i++)
{
py_dissector->dissection_chain[i]->func(&tvb_and_tree, pinfo, &offset, py_dissector->dissection_chain[i]->params);
}
}
void
register_dissectors_array(int num_dissectors, py_dissector_t ** dissectors_array)
{
g_num_dissectors = num_dissectors;
g_dissectors = dissectors_array;
}
void
add_tree_item(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_, int *p_offset, add_tree_item_params_t *params)
{
params->out_item = proto_tree_add_item(tvb_and_tree->tree, *(params->p_hf_index), tvb_and_tree->tvb, *p_offset, params->length, params->encoding);
}
void
add_text_item(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_, int *p_offset, add_text_item_params_t *params)
{
params->out_item = proto_tree_add_none_format(tvb_and_tree->tree, *(params->p_hf_index), tvb_and_tree->tvb, *p_offset, params->length, "%s", params->text);
}
void
push_tree(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_, int *p_offset, push_tree_params_t *params)
{
if (tvb_and_tree->tree)
{
*(params->p_start_offset) = *p_offset;
*(params->p_old_tree) = tvb_and_tree->tree;
tvb_and_tree->tree = proto_item_add_subtree(*(params->parent), *(params->p_index));
}
}
void
pop_tree(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_, int *p_offset, pop_tree_params_t *params)
{
if (tvb_and_tree->tree)
{
proto_item_set_len(tvb_and_tree->tree, *p_offset - *(params->p_start_offset));
tvb_and_tree->tree = *(params->p_old_tree);
}
}
void
push_tvb(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_, int *p_offset, push_tvb_params_t *params)
{
guint8 *data;
tvbuff_t *new_tvb;
data = (guint8 *) g_malloc(params->length);
memcpy(data, params->data, params->length);
*(params->p_old_offset) = *p_offset;
*(params->p_old_tvb) = tvb_and_tree->tvb;
new_tvb = tvb_new_child_real_data(tvb_and_tree->tvb, data, params->length, params->length);
tvb_set_free_cb(new_tvb, g_free);
add_new_data_source(pinfo, new_tvb, params->name);
tvb_and_tree->tvb = new_tvb;
*p_offset = 0;
}
void
pop_tvb(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_, int *p_offset, pop_tvb_params_t *params)
{
*p_offset = *(params->p_old_offset);
tvb_and_tree->tvb = *(params->p_old_tvb);
}
void
advance_offset(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo _U_ , int *p_offset, advance_offset_params_t *params)
{
if (params->flags == OFFSET_FLAGS_READ_LENGTH || params->flags == OFFSET_FLAGS_READ_LENGTH_INCLUDING)
{
*p_offset += get_uint_value(tvb_and_tree->tvb, *p_offset, params->length, params->encoding);
}
if (params->flags == OFFSET_FLAGS_NONE || params->flags == OFFSET_FLAGS_READ_LENGTH)
{
*p_offset += params->length;
}
}
void
set_column_text(tvb_and_tree_t *tvb_and_tree _U_, packet_info *pinfo, int *p_offset _U_, set_column_text_params_t *params)
{
if (check_col(pinfo->cinfo, params->col_id))
{
col_add_str(pinfo->cinfo, params->col_id, params->text);
}
}
void call_next_dissector(tvb_and_tree_t *tvb_and_tree, packet_info *pinfo, int *p_offset, call_next_dissector_params_t *params)
{
char *temp_name = *(params->name);
gint temp_length = *(params->length);
*(params->name) = params->default_name;
*(params->length) = params->default_length;
call_dissector(find_dissector(temp_name), tvb_new_subset(tvb_and_tree->tvb, *p_offset, temp_length, temp_length), pinfo, tvb_and_tree->tree);
}
guint32
get_uint_value(tvbuff_t *tvb, gint offset, gint length, const guint encoding)
{
switch (length) {
case 1:
return tvb_get_guint8(tvb, offset);
case 2:
return encoding ? tvb_get_letohs(tvb, offset)
: tvb_get_ntohs(tvb, offset);
case 3:
return encoding ? tvb_get_letoh24(tvb, offset)
: tvb_get_ntoh24(tvb, offset);
case 4:
return encoding ? tvb_get_letohl(tvb, offset)
: tvb_get_ntohl(tvb, offset);
default:
return 0;
}
}