1414from scapy .arch .bpf .core import get_dev_bpf , attach_filter
1515from scapy .arch .bpf .consts import BIOCGBLEN , BIOCGDLT , BIOCGSTATS , \
1616 BIOCIMMEDIATE , BIOCPROMISC , BIOCSBLEN , BIOCSETIF , BIOCSHDRCMPLT , \
17- BPF_BUFFER_LENGTH
17+ BPF_BUFFER_LENGTH , BIOCSDLT , DLT_IEEE802_11_RADIO
1818from scapy .config import conf
19- from scapy .consts import FREEBSD , NETBSD
19+ from scapy .consts import FREEBSD , NETBSD , DARWIN
2020from scapy .data import ETH_P_ALL
2121from scapy .error import Scapy_Exception , warning
2222from scapy .supersocket import SuperSocket
@@ -40,7 +40,8 @@ class _L2bpfSocket(SuperSocket):
4040 ins = None
4141 closed = False
4242
43- def __init__ (self , iface = None , type = ETH_P_ALL , promisc = None , filter = None , nofilter = 0 ):
43+ def __init__ (self , iface = None , type = ETH_P_ALL , promisc = None , filter = None ,
44+ nofilter = 0 , monitor = False ):
4445
4546 # SuperSocket mandatory variables
4647 if promisc is None :
@@ -75,6 +76,17 @@ def __init__(self, iface=None, type=ETH_P_ALL, promisc=None, filter=None, nofilt
7576 if self .promisc :
7677 self .set_promisc (1 )
7778
79+ # Set the interface to monitor mode
80+ # Note: - trick from libpcap/pcap-bpf.c - monitor_mode()
81+ # - it only works on OS X 10.5 and later
82+ if DARWIN and monitor :
83+ dlt_radiotap = struct .pack ('I' , DLT_IEEE802_11_RADIO )
84+ try :
85+ fcntl .ioctl (self .ins , BIOCSDLT , dlt_radiotap )
86+ except IOError :
87+ raise Scapy_Exception ("Can't set %s into monitor mode!" %
88+ self .iface )
89+
7890 # Don't block on read
7991 try :
8092 fcntl .ioctl (self .ins , BIOCIMMEDIATE , struct .pack ('I' , 1 ))
0 commit comments