From 2ec520aa444990a595f5280e666ab4c3f2f3795f Mon Sep 17 00:00:00 2001 From: Guillaume Valadon <guillaume@valadon.net> Date: Tue, 5 Apr 2016 13:52:49 +0200 Subject: [PATCH] Scapy can now use dumbnet --- scapy/arch/pcapdnet.py | 43 ++++++++++++++++++++++++++++++++---------- test/regression.uts | 14 ++++++++++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/scapy/arch/pcapdnet.py b/scapy/arch/pcapdnet.py index c608b70b..13ec1fae 100755 --- a/scapy/arch/pcapdnet.py +++ b/scapy/arch/pcapdnet.py @@ -12,7 +12,7 @@ if not sys.platform.startswith("win"): from fcntl import ioctl from scapy.data import * from scapy.config import conf -from scapy.utils import warning +from scapy.utils import warning, mac2str from scapy.supersocket import SuperSocket from scapy.error import Scapy_Exception import scapy.arch @@ -460,12 +460,15 @@ if conf.use_pcap: conf.L2listen = L2pcapListenSocket - - if conf.use_dnet: try: - import dnet + try: + # First try to import dnet + import dnet + except ImportError: + # Then, try to import dumbnet as dnet + import dumbnet as dnet except ImportError,e: if conf.interactive: log_loading.error("Unable to import dnet module: %s" % e) @@ -483,21 +486,41 @@ if conf.use_dnet: raise else: def get_if_raw_hwaddr(iff): + """Return a tuple containing the link type and the raw hardware + address corresponding to the interface 'iff'""" + if iff == scapy.arch.LOOPBACK_NAME: - return (772, '\x00'*6) + return (ARPHDR_LOOPBACK, '\x00'*6) + + # Retrieve interface information try: l = dnet.intf().get(iff) - l = l["link_addr"] + link_addr = l["link_addr"] except: - raise Scapy_Exception("Error in attempting to get hw address for interface [%s]" % iff) - return l.type,l.data + raise Scapy_Exception("Error in attempting to get hw address" + " for interface [%s]" % iff) + + if hasattr(link_addr, "type"): + # Legacy dnet module + return link_addr.type, link_addr.data + + else: + # dumbnet module + mac = mac2str(str(link_addr)) + + # Adjust the link type + if l["type"] == 6: # INTF_TYPE_ETH from dnet + return (ARPHDR_ETHER, mac) + + return (l["type"], mac) + def get_if_raw_addr(ifname): i = dnet.intf() return i.get(ifname)["addr"].data def get_if_list(): return [i.get("name", None) for i in dnet.intf()] - - + + if conf.use_pcap and conf.use_dnet: class L3dnetSocket(SuperSocket): desc = "read/write packets at layer 3 using libdnet and libpcap" diff --git a/test/regression.uts b/test/regression.uts index 7f25708c..d86a5fb5 100644 --- a/test/regression.uts +++ b/test/regression.uts @@ -24,6 +24,20 @@ lsc() ~ conf conf.debug_dissector = True + +############ +############ ++ Scapy functions tests + += Interface related functions + +get_if_raw_hwaddr(conf.iface) + +get_if_raw_addr(conf.iface).encode("hex") + +get_if_list() + + ############ ############ + Basic tests -- GitLab