2929from scapy .config import conf
3030from scapy .data import MTU , ETH_P_ALL
3131from scapy .supersocket import SuperSocket
32- from scapy .error import warning , Scapy_Exception , log_interactive , \
33- log_loading , ScapyInvalidPlatformException
34- from scapy .arch .common import get_if , get_bpf_pointer
32+ from scapy .error import warning , Scapy_Exception , \
33+ ScapyInvalidPlatformException
34+ from scapy .arch .common import get_if , compile_filter
3535import scapy .modules .six as six
3636from scapy .modules .six .moves import range
3737
9292# Unused, PACKET_FASTROUTE and PACKET_LOOPBACK are invisible to user space
9393
9494
95- with os .popen ("%s -V 2> /dev/null" % conf .prog .tcpdump ) as _f :
96- if _f .close () >> 8 == 0x7f :
97- log_loading .warning ("Failed to execute tcpdump. Check it is installed and in the PATH" ) # noqa: E501
98- TCPDUMP = 0
99- else :
100- TCPDUMP = 1
101- del (_f )
102-
103-
10495def get_if_raw_hwaddr (iff ):
10596 return struct .unpack ("16xh6s8x" , get_if (iff , SIOCGIFHWADDR ))
10697
@@ -139,36 +130,15 @@ def get_working_if():
139130 return LOOPBACK_NAME
140131
141132
142- def attach_filter (s , bpf_filter , iface ):
133+ def attach_filter (sock , bpf_filter , iface ):
143134 # XXX We generate the filter on the interface conf.iface
144135 # because tcpdump open the "any" interface and ppp interfaces
145136 # in cooked mode. As we use them in raw mode, the filter will not
146137 # work... one solution could be to use "any" interface and translate
147138 # the filter from cooked mode to raw mode
148139 # mode
149- if not TCPDUMP :
150- return
151- try :
152- f = os .popen ("%s -p -i %s -ddd -s %d '%s'" % (
153- conf .prog .tcpdump ,
154- conf .iface if iface is None else iface ,
155- MTU ,
156- bpf_filter ,
157- ))
158- except OSError :
159- log_interactive .warning ("Failed to attach filter." ,
160- exc_info = True )
161- return
162- lines = f .readlines ()
163- ret = f .close ()
164- if ret :
165- log_interactive .warning (
166- "Failed to attach filter: tcpdump returned %d" , ret
167- )
168- return
169-
170- bp = get_bpf_pointer (lines )
171- s .setsockopt (socket .SOL_SOCKET , SO_ATTACH_FILTER , bp )
140+ bp = compile_filter (bpf_filter , iface )
141+ sock .setsockopt (socket .SOL_SOCKET , SO_ATTACH_FILTER , bp )
172142
173143
174144def set_promisc (s , iff , val = 1 ):
@@ -468,6 +438,7 @@ def __init__(self, iface=None, type=ETH_P_ALL, promisc=None, filter=None,
468438 nofilter = 0 , monitor = None ):
469439 self .iface = conf .iface if iface is None else iface
470440 self .type = type
441+ self .promisc = conf .sniff_promisc if promisc is None else promisc
471442 if monitor is not None :
472443 if not set_iface_monitor (iface , monitor ):
473444 warning ("Could not change interface mode !" )
@@ -481,7 +452,6 @@ def __init__(self, iface=None, type=ETH_P_ALL, promisc=None, filter=None,
481452 filter = "not (%s)" % conf .except_filter
482453 if filter is not None :
483454 attach_filter (self .ins , filter , iface )
484- self .promisc = conf .sniff_promisc if promisc is None else promisc
485455 if self .promisc :
486456 set_promisc (self .ins , self .iface )
487457 self .ins .bind ((self .iface , type ))
0 commit comments