Skip to content
Snippets Groups Projects
Commit c7b722ee authored by Pierre LALET's avatar Pierre LALET
Browse files

Fix layers import

parent 40978686
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,7 @@ All layers. Configurable with conf.load_layers. ...@@ -10,6 +10,7 @@ All layers. Configurable with conf.load_layers.
from __future__ import absolute_import from __future__ import absolute_import
from scapy.config import conf from scapy.config import conf
from scapy.error import log_loading from scapy.error import log_loading
from scapy.main import load_layer
import logging, importlib import logging, importlib
import scapy.modules.six as six import scapy.modules.six as six
ignored = list(six.moves.builtins.__dict__.keys()) + ["sys"] ignored = list(six.moves.builtins.__dict__.keys()) + ["sys"]
...@@ -17,36 +18,11 @@ log = logging.getLogger("scapy.loading") ...@@ -17,36 +18,11 @@ log = logging.getLogger("scapy.loading")
__all__ = [] __all__ = []
def _validate_local(x):
"""Returns whether or not a variable should be imported.
Will return False for any default modules (sys), or if
they are detected as private vars (starting with a _)"""
global ignored
return x[0] != "_" and not x in ignored
def _import_star(m):
mod = importlib.import_module("." + m, "scapy.layers")
if '__all__' in mod.__dict__:
# only import the exported symbols in __all__
for name in mod.__dict__['__all__']:
__all__.append(name)
globals()[name] = mod.__dict__[name]
else:
# import all the non-private symbols
for name, sym in six.iteritems(mod.__dict__):
if _validate_local(name):
__all__.append(name)
globals()[name] = sym
LAYER_ALIASES = {
"tls": "tls.all",
}
for _l in conf.load_layers: for _l in conf.load_layers:
log_loading.debug("Loading layer %s" % _l) log_loading.debug("Loading layer %s" % _l)
try: try:
_import_star(LAYER_ALIASES.get(_l, _l)) load_layer(_l, globals_dict=globals(), symb_list=__all__)
except Exception as e: except Exception as e:
log.warning("can't import layer %s: %s" % (_l,e)) log.warning("can't import layer %s: %s" % (_l,e))
del _l
...@@ -18,7 +18,12 @@ import importlib ...@@ -18,7 +18,12 @@ import importlib
ignored = list(six.moves.builtins.__dict__.keys()) ignored = list(six.moves.builtins.__dict__.keys())
from scapy.error import * from scapy.error import *
from scapy.layers.all import LAYER_ALIASES
__all__ = []
LAYER_ALIASES = {
"tls": "tls.all"
}
def _probe_config_file(cf): def _probe_config_file(cf):
cf_path = os.path.join(os.path.expanduser("~"), cf) cf_path = os.path.join(os.path.expanduser("~"), cf)
...@@ -65,26 +70,33 @@ from scapy.themes import DefaultTheme ...@@ -65,26 +70,33 @@ from scapy.themes import DefaultTheme
###################### ######################
def _load(module): def _load(module, globals_dict=None, symb_list=None):
if globals_dict is None:
globals_dict = six.moves.builtins.__dict__
try: try:
mod = importlib.import_module(module) mod = importlib.import_module(module)
if '__all__' in mod.__dict__: if '__all__' in mod.__dict__:
# import listed symbols # import listed symbols
for name in mod.__dict__['__all__']: for name in mod.__dict__['__all__']:
six.moves.builtins.__dict__[name] = mod.__dict__[name] if symb_list is not None:
symb_list.append(name)
globals_dict[name] = mod.__dict__[name]
else: else:
# only import non-private symbols # only import non-private symbols
for name, sym in six.iteritems(mod.__dict__): for name, sym in six.iteritems(mod.__dict__):
if _validate_local(name): if _validate_local(name):
six.moves.builtins.__dict__[name] = sym if symb_list is not None:
except Exception as e: symb_list.append(name)
log_interactive.error(e) globals_dict[name] = sym
except Exception:
log_interactive.error("Loading module %s", module, exc_info=True)
def load_module(name): def load_module(name):
_load("scapy.modules."+name) _load("scapy.modules."+name)
def load_layer(name): def load_layer(name, globals_dict=None, symb_list=None):
_load("scapy.layers." + LAYER_ALIASES.get(name, name)) _load("scapy.layers." + LAYER_ALIASES.get(name, name),
globals_dict=globals_dict, symb_list=symb_list)
def load_contrib(name): def load_contrib(name):
try: try:
......
...@@ -8,7 +8,6 @@ Routing and handling of network interfaces. ...@@ -8,7 +8,6 @@ Routing and handling of network interfaces.
""" """
from __future__ import absolute_import from __future__ import absolute_import
import socket
from scapy.consts import LOOPBACK_NAME, LOOPBACK_INTERFACE from scapy.consts import LOOPBACK_NAME, LOOPBACK_INTERFACE
from scapy.utils import atol, ltoa, itom, pretty_routes from scapy.utils import atol, ltoa, itom, pretty_routes
from scapy.config import conf from scapy.config import conf
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment