]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/wpa/wpa_supplicant/utils/log2pcap.py
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / wpa / wpa_supplicant / utils / log2pcap.py
1 #!/usr/bin/env python
2 #
3 # Copyright (c) 2012, Intel Corporation
4 #
5 # Author: Johannes Berg <johannes@sipsolutions.net>
6 #
7 # This software may be distributed under the terms of the BSD license.
8 # See README for more details.
9
10 import sys, struct, re
11
12 def write_pcap_header(pcap_file):
13     pcap_file.write(
14         struct.pack('<IHHIIII',
15                     0xa1b2c3d4, 2, 4, 0, 0, 65535,
16                     105 # raw 802.11 format
17                     ))
18
19 def pcap_addpacket(pcap_file, ts, data):
20     # ts in seconds, float
21     pcap_file.write(struct.pack('<IIII',
22         int(ts), int(1000000 * ts) % 1000000,
23         len(data), len(data)))
24     pcap_file.write(data)
25
26 if __name__ == "__main__":
27     try:
28         input = sys.argv[1]
29         pcap = sys.argv[2]
30     except IndexError:
31         print "Usage: %s <log file> <pcap file>" % sys.argv[0]
32         sys.exit(2)
33
34     input_file = open(input, 'r')
35     pcap_file = open(pcap, 'w')
36     frame_re = re.compile(r'(([0-9]+.[0-9]{6}):\s*)?nl80211: MLME event frame - hexdump\(len=[0-9]*\):((\s*[0-9a-fA-F]{2})*)')
37
38     write_pcap_header(pcap_file)
39
40     for line in input_file:
41         m = frame_re.match(line)
42         if m is None:
43             continue
44         if m.group(2):
45             ts = float(m.group(2))
46         else:
47             ts = 0
48         hexdata = m.group(3)
49         hexdata = hexdata.split()
50         data = ''.join([chr(int(x, 16)) for x in hexdata])
51         pcap_addpacket(pcap_file, ts, data)
52
53     input_file.close()
54     pcap_file.close()