1 # register-dissector.py
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.
29 # Build a list of files belonging to a directory and matching a regexp (f.e.
30 # '(?P<plugin>.*)\.py$' )
32 def get_plugin_list(dir, regexp
):
33 lDir
= os
.listdir(dir)
37 MatchedObject
= re
.match(regexp
, sDir
)
38 if (MatchedObject
!= None):
39 lPlugins
.append(MatchedObject
.group("plugin"))
42 #Import the module "name"
43 def plugin_import(name
):
44 #if the module was already loaded
46 return sys
.modules
[name
]
53 def register_dissectors(wspython_dir
, plugins_pers_dir
=None):
54 #append dir to be able to import py_lib
55 sys
.path
.append(wspython_dir
)
56 from wspy_libws
import get_libws_handle
57 libws
= get_libws_handle()
60 os
.path
.join(wspython_dir
, 'wspy_dissectors'),
64 registered_protocols
= []
65 for dissectors_dir
in dissectors_dirs
:
66 #Check if we have the dissectors directory
67 if not os
.path
.isdir(dissectors_dir
):
70 #append dir to be able to import python dissectors
71 sys
.path
.append(dissectors_dir
)
73 #Read all python dissectors
74 dissectors
= get_plugin_list(dissectors_dir
, "(?P<plugin>.*)\.py$")
76 #For each dissector, register it and put it in the list of registered
78 for dissector
in dissectors
:
80 d
= plugin_import(dissector
)
81 registered_protocol
= d
.register_protocol()
82 if registered_protocol
:
83 registered_protocols
.append(registered_protocol
)
84 except Exception as e
:
85 print('register dissector %s exception %s' % (dissector
, e
))
86 return registered_protocols
91 # Start tracing when import has finished
92 def tracer(frame
, event
, arg
):
94 lineno
= frame
.f_lineno
95 filename
= frame
.f_globals
["__file__"]
96 if (filename
.endswith(".pyc") or
97 filename
.endswith(".pyo")):
98 filename
= filename
[:-1]
99 name
= frame
.f_globals
["__name__"]
100 line
= linecache
.getline(filename
, lineno
)
101 print("%s:%s: %s" % (name
, lineno
, line
.rstrip()))
102 if event
== "exception":
103 print("exception", arg
)