diff --git a/scapy/arch/pcapdnet.py b/scapy/arch/pcapdnet.py index c608b70b4f4d6b4db86ec26b86f13b1aff14d6ea..13ec1fae7fafbc7b11cc5ad64eb59eb7d91f7764 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 7f25708ca833fa1c8ebd56ed8dca9f97d83f2cf8..d86a5fb53ed4359ba3d32f899a4678c6943186ce 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