10 read_timeout = 100 # in milliseconds
11 pc = pcapy.open_live(iface, max_bytes, promiscuous, read_timeout)
21 def dealWithPacket ( hdr , data ) :
23 if hdr.getlen() != hdr.getcaplen() :
24 print "Error in header : %d vs. %d" % ( hdr.getlen() , hdr.getcaplen() )
26 if len(data) != hdr.getlen() :
27 print "Data lenght does not match"
30 it_version , it_len , it_present = struct.unpack("<Bxhl",data[:8])
32 print "Bad version (%s), it is probably not radiotap header" % it_version
35 print "Bad length on radiotap header"
38 radiotap = data[:it_len]
39 payload = data[it_len:]
41 format , padstr = "<" , ""
43 for name,bit,fmt,pad in ratiotap_header_bits :
44 # What about 'it_present & ( 0x1 << bit )' ??
45 if it_present & pow(2,bit) == pow(2,bit) :
47 print "Unknown bit %s (%d) set" % ( name , bit )
51 fields.append( "CHANNEL_BITMAP" )
55 values = struct.unpack(format+padstr,radiotap[8:])
58 for name,value in radiotap_flags :
59 if values[1] & value == value :
63 for name,value in channel_flags :
64 if values[4] & value == value :
65 channel.append( name )
68 pcktlen = len(payload)
70 frame_ctl , frame_subtype , duration_id = struct.unpack("BBh",payload[:4])
74 for name,value in frame_type :
75 if frame_ctl & 0x0c == value :
79 print "Unknown frame type %s" % ( frame_ctl & 0x0c , )
83 for name,value in management_subtypes :
84 if frame_ctl & 0xf0 == value :
88 print "Unknown MGT subtype %s" % ( frame_ctl & 0xf0 , )
92 for name,value in control_subtypes :
93 if frame_ctl & 0xf0 == value :
97 print "Unknown CTL subtype %s" % ( frame_ctl & 0xf0 , )
100 elif type == "DATA" :
102 for name,value in data_subtypes :
103 if frame_ctl & 0xf0 == value :
104 _subtype.append( name )
105 subtype = "-".join( _subtype )
107 for name,value in directions :
108 if frame_subtype & 0x03 == value :
112 print "Unknown direction %s" % ( frame_subtype & 0x03 , )
116 mac_str = "BBBBBB" # is leading '<' required
117 mac_fmt = "%02X:%02X:%02X:%02X:%02X:%02X"
121 maclist.append( mac_fmt % struct.unpack( mac_str , payload[pointer:pointer+6] ) )
124 # FIXME : only CTL/ACK have single address ???
125 # FIXME : Is this the same than pcktlen == 2
129 sequence = struct.unpack("BB",payload[pointer:pointer+2])
135 # print "Radiotap flags : %s" % " ".join(flags)
136 # print "Channel %d (%s)" % ( values[3] , " ".join(channel) )
137 ## print "Control info",frame_ctl,frame_subtype,duration_id,"seq",sequence
138 # print type,":",maclist,"->",pcktlen
141 global tstamp,max_time,discovered
143 if not mac in discovered :
145 discovered.append( mac )
146 if time.time()-tstamp > max_time :
147 fd = open( "discovered.list" , "a" )
148 for mac in discovered :
149 fd.write( "%s\n" % mac )
151 raise Exception( "Neighborhoud scan completed" )
153 print "%4s %13s %6s %4d %4d from %4d" % (type,subtype,direction,values[3],pcktlen,len(payload))," ; %4d %4d "%sequence,":"+" %s"*len(maclist) % tuple(maclist)
155 packet_limit = -1 # infinite
156 pc.loop( packet_limit , dealWithPacket )