5 * Wireshark Protocol Python Binding
7 * Copyright (c) 2009 by Sebastien Tandel <sebastien [AT] tandel [dot] be>
8 * Copyright (c) 2001 by Gerald Combs <gerald@wireshark.org>
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
40 #include "filesystem.h"
42 #include "wspy_register.h"
44 /* hash table containing all the registered python dissectors */
45 GHashTable * g_py_dissectors=NULL;
48 * Global objects that python method dissect() will get. Avoid to write a
49 * function for each proto_tree*.
50 * Is it the only way to do that? I can't believe it ... think about it
52 tvbuff_t * g_tvb = NULL;
53 packet_info * g_pinfo = NULL;
54 proto_tree * g_tree = NULL;
57 /* Initialization of the Python Interpreter */
64 /* Finalization of the Python Interpreter */
66 void wspy_finalize(void)
71 /*const char * py_dissector_short_desc(PyObject * py_dissector)
76 * Returns the __str__ of the python object
78 char * py_dissector_name(PyObject * py_dissector)
80 PyObject * py_object_name;
83 py_object_name = PyObject_Str(py_dissector);
85 return PyString_AS_STRING(py_object_name);
89 * Register the dissector
91 void py_dissector_register(PyObject * py_dissector, char * py_name, register_cb cb, gpointer client_data)
93 /* const char * py_name; */
95 /* Get the name of the dissector */
96 /* py_name = py_dissector_name(py_dissector); */
98 /* Register dissector in register_cb */
100 (*cb)(RA_REGISTER, py_name, client_data);
103 * Register protocol, fields, subtrees
105 * Done by calling register method of the object
107 PyObject_CallMethod(py_dissector, "register_protocol", NULL);
111 static const char *get_py_register_file(void)
113 static const char * wspython_register_file = NULL;
115 if (!wspython_register_file) {
117 wspython_register_file = g_strdup_printf("%s\\register-dissector.py", get_wspython_dir());
119 wspython_register_file = g_strdup_printf("%s/register-dissector.py", get_wspython_dir());
122 return wspython_register_file;
126 * Finds out all the python dissectors and register them
128 void register_all_py_protocols_func(register_cb cb, gpointer client_data)
131 PyObject * global_dict, * main_module, * register_fn;
132 PyObject * py_dissectors, * py_dissector;
138 nothing = client_data;
140 /* intialize the hash table where all the python dissectors are kept */
141 g_py_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
143 /* STA TODO : init only if prefs is enabled */
146 /* load the python register module */
147 py_reg = fopen(get_py_register_file(), "r");
148 if (py_reg == NULL) {
149 printf("no register file %s\n", get_py_register_file());
152 PyRun_SimpleFile(py_reg, get_py_register_file());
154 /* Getting the global symbols from the python register module */
155 main_module = PyImport_AddModule("__main__");
156 global_dict = PyModule_GetDict(main_module);
158 /* Get the python register function */
159 register_fn = PyDict_GetItemString(global_dict, "register_dissectors");
161 /* Execute the python register function */
162 /* This function returns a sequence of python dissectors objects */
163 py_args = Py_BuildValue("(s)", get_wspython_dir());
164 py_dissectors = PyObject_CallObject(register_fn, py_args);
166 /* Check that the py_dissectors is really a sequence */
167 if (!PySequence_Check(py_dissectors)) {
168 printf("not registered ...\n");
173 * For each dissector, register it in cb and registers all fields, subtrees,
174 * protocol name, etc ...
176 for (index = 0; (py_dissector = PySequence_GetItem(py_dissectors, index)); index++)
178 name = py_dissector_name(py_dissector);
179 py_dissector_register(py_dissector, name, cb, client_data);
180 g_hash_table_insert(g_py_dissectors, (gpointer*)name, py_dissector);
184 tvbuff_t *py_tvbuff(void)
189 packet_info * py_pinfo(void)
194 proto_tree * py_tree(void)
200 * Generic Python Dissector
202 * Search the correct PyObject dissector based on
203 * pinfo->current_proto in the hash table py_dissectors.
205 * We then call the method "dissect" of this PyObject.
207 void py_dissect(tvbuff_t * tvb, packet_info * pinfo,
210 PyObject * py_dissector;
212 /* printf("pinfo->current_proto : %s\n", pinfo->current_proto); */
213 /* NOTE => pinfo->current_proto == "HomePlug" */
219 py_dissector = g_hash_table_lookup(g_py_dissectors, pinfo->current_proto);
220 assert(py_dissector);
222 PyObject_CallMethod(py_dissector, "pre_dissect", NULL);
226 * Return the pointer to the generic python dissector
228 * One could think that it could be a PyCObject but it is a
229 * workaround because ctypes is used and it complains later -not
230 * knowing how to conver the parameter - in the Python code when
231 * calling back a C function with a PyCObject as parameter
233 dissector_t py_generic_dissector(void)
238 struct SRegisterHandoffsForeach {
240 gpointer client_data;
243 static void register_all_py_handoffs_foreach(gpointer key _U_, gpointer value, gpointer user_data)
245 PyObject * py_dissector = (PyObject *)value;
246 struct SRegisterHandoffsForeach *rhf = (struct SRegisterHandoffsForeach*)user_data;
248 /* STA TODO : it's the short_desc field ... not really the filter field! */
249 char * handoff_name = g_strdup_printf("handoff_%s", py_dissector_name(py_dissector));
252 (*(rhf->cb))(RA_HANDOFF, handoff_name, rhf->client_data);
254 PyObject_CallMethod(py_dissector, "register_handoff", NULL);
258 * Finalize the registration of the python protocol dissectors
261 register_all_py_handoffs_func(register_cb cb, gpointer client_data)
263 struct SRegisterHandoffsForeach rhf;
266 rhf.client_data = client_data;
268 g_hash_table_foreach(g_py_dissectors, register_all_py_handoffs_foreach, &rhf);
271 #endif /* HAVE_PYTHON */