blob: 9a3f08de84614dc738c5d4ca7a37f44add85cc75 [file] [log] [blame]
Sunil Ravi036cec52023-03-29 11:35:17 -07001#!/usr/bin/env python3
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002#
Sunil Ravi036cec52023-03-29 11:35:17 -07003# Copyright (c) 2012-2022, Intel Corporation
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004#
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
10import sys, struct, re
Sunil Ravi036cec52023-03-29 11:35:17 -070011from binascii import unhexlify
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080012
13def write_pcap_header(pcap_file):
14 pcap_file.write(
15 struct.pack('<IHHIIII',
16 0xa1b2c3d4, 2, 4, 0, 0, 65535,
17 105 # raw 802.11 format
18 ))
19
20def pcap_addpacket(pcap_file, ts, data):
21 # ts in seconds, float
22 pcap_file.write(struct.pack('<IIII',
23 int(ts), int(1000000 * ts) % 1000000,
24 len(data), len(data)))
25 pcap_file.write(data)
26
27if __name__ == "__main__":
28 try:
29 input = sys.argv[1]
30 pcap = sys.argv[2]
31 except IndexError:
Hai Shalom74f70d42019-02-11 14:42:39 -080032 print("Usage: %s <log file> <pcap file>" % sys.argv[0])
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080033 sys.exit(2)
34
35 input_file = open(input, 'r')
Sunil Ravi036cec52023-03-29 11:35:17 -070036 pcap_file = open(pcap, 'wb')
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080037 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})*)')
38
39 write_pcap_header(pcap_file)
40
41 for line in input_file:
42 m = frame_re.match(line)
43 if m is None:
44 continue
45 if m.group(2):
46 ts = float(m.group(2))
47 else:
48 ts = 0
49 hexdata = m.group(3)
50 hexdata = hexdata.split()
Sunil Ravi036cec52023-03-29 11:35:17 -070051 data = unhexlify("".join(hexdata))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080052 pcap_addpacket(pcap_file, ts, data)
53
54 input_file.close()
55 pcap_file.close()