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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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)
94 * Register protocol, fields, subtrees
96 * Done by calling register method of the object
98 PyObject_CallMethod(py_dissector, "register_protocol", NULL);
102 static const char *get_py_register_file(void)
104 static const char * wspython_register_file = NULL;
106 if (!wspython_register_file) {
108 wspython_register_file = g_strdup_printf("%s\\register-dissector.py", get_wspython_dir());
110 wspython_register_file = g_strdup_printf("%s/register-dissector.py", get_wspython_dir());
113 return wspython_register_file;
117 * Finds out all the python dissectors and register them
119 void register_all_py_protocols_func(void)
122 PyObject * global_dict, * main_module, * register_fn;
123 PyObject * py_dissectors, * py_dissector;
128 /* intialize the hash table where all the python dissectors are kept */
129 g_py_dissectors = g_hash_table_new(g_str_hash, g_str_equal);
131 /* STA TODO : init only if prefs is enabled */
134 /* load the python register module */
135 py_reg = fopen(get_py_register_file(), "r");
136 if (py_reg == NULL) {
137 printf("Can't open Python registration file: %s\n", get_py_register_file());
140 PyRun_SimpleFile(py_reg, get_py_register_file());
142 /* Getting the global symbols from the python register module */
143 main_module = PyImport_AddModule("__main__");
144 global_dict = PyModule_GetDict(main_module);
146 /* Get the python register function */
147 register_fn = PyDict_GetItemString(global_dict, "register_dissectors");
148 if (register_fn == NULL) {
149 printf("Error in Python registration file: %s\n", get_py_register_file());
153 /* Execute the python register function */
154 /* This function returns a sequence of python dissectors objects */
155 py_args = Py_BuildValue("ss", get_wspython_dir(), get_plugins_pers_dir());
156 py_dissectors = PyObject_CallObject(register_fn, py_args);
158 /* Check that the py_dissectors is really a sequence */
159 if (!py_dissectors || !PySequence_Check(py_dissectors)) {
160 printf("Python dissectors not registered ...\n");
165 * For each dissector, register it in cb and registers all fields, subtrees,
166 * protocol name, etc ...
168 for (index = 0; (py_dissector = PySequence_GetItem(py_dissectors, index)); index++)
170 name = py_dissector_name(py_dissector);
171 py_dissector_register(py_dissector);
172 g_hash_table_insert(g_py_dissectors, (gpointer*)name, py_dissector);
176 void py_dissector_args(tvbuff_t ** tvb, packet_info ** pinfo, proto_tree ** tree)
184 * Generic Python Dissector
186 * Search the correct PyObject dissector based on
187 * pinfo->current_proto in the hash table py_dissectors.
189 * We then call the method "dissect" of this PyObject.
191 void py_dissect(tvbuff_t * tvb, packet_info * pinfo,
194 PyObject * py_dissector;
196 /* printf("pinfo->current_proto : %s\n", pinfo->current_proto); */
197 /* NOTE => pinfo->current_proto == "HomePlug" */
203 py_dissector = g_hash_table_lookup(g_py_dissectors, pinfo->current_proto);
204 assert(py_dissector);
206 PyObject_CallMethod(py_dissector, "pre_dissect", NULL);
209 dissector_handle_t py_create_dissector_handle(const int proto)
211 return create_dissector_handle(&py_dissect, proto);
214 static void register_all_py_handoffs_foreach(gpointer key _U_, gpointer value, gpointer user_data _U_)
216 PyObject * py_dissector = (PyObject *)value;
218 PyObject_CallMethod(py_dissector, "register_handoff", NULL);
222 * Finalize the registration of the python protocol dissectors
225 register_all_py_handoffs_func(void)
227 g_hash_table_foreach(g_py_dissectors, register_all_py_handoffs_foreach, NULL);
230 #endif /* HAVE_PYTHON */