Skip to content

Commit 79c507e

Browse files
committed
PCAP parser: Add skeleton parsing code
1 parent ceb0d4e commit 79c507e

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

xcloud/scripts/pcap_reader.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,32 @@
33
"""
44
import argparse
55

6+
import dpkt
7+
from construct.lib import containers
68

7-
def parse_file(filepath: str) -> None:
8-
print(f"hello -> {filepath}")
9+
10+
containers.setGlobalPrintFullStrings(True)
11+
12+
13+
def packet_filter(filepath):
14+
with open(filepath, 'rb') as fh:
15+
for ts, buf in dpkt.pcap.Reader(fh):
16+
eth = dpkt.ethernet.Ethernet(buf)
17+
18+
# Make sure the Ethernet data contains an IP packet
19+
if not isinstance(eth.data, dpkt.ip.IP):
20+
continue
21+
22+
ip = eth.data
23+
if not isinstance(ip.data, dpkt.udp.UDP):
24+
continue
25+
26+
yield(ip.data, ts)
27+
28+
29+
def parse_file(pcap_filepath: str) -> None:
30+
for packet, timestamp in packet_filter(pcap_filepath):
31+
print(packet, timestamp)
932

1033

1134
def main():

0 commit comments

Comments
 (0)