feat: add 0.6 support to the pcap printer example

This commit is contained in:
ChillerDragon 2024-02-23 12:48:12 +08:00
parent 69e6dce190
commit 0d296d83d0
5 changed files with 58 additions and 29 deletions

View file

@ -28,9 +28,14 @@ for msg in packet.messages:
```
More examples can be found in the [examples/](./examples/) folder:
### 0.7
- [map downloader client](./examples/07/download_map/)
- [flood client (connect multiple tees to a server)](./examples/07/flood/)
- [pcap printer (capture with tcpdump and print teeworlds traffic details)](./examples/07/print_pcap_files/)
### 0.6 and 0.7
- [pcap printer (capture with tcpdump and print teeworlds traffic details)](./examples/06_and_07/print_pcap_files/)
## Features

View file

@ -0,0 +1,52 @@
#!/usr/bin/env python3
import sys
import dpkt
import twnet_parser.packet
arg_pcap = None
arg_version = 7
def print_tw_packets(pcap):
for _ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
if not isinstance(ip.data, dpkt.udp.UDP):
continue
udp_payload = ip.data.data
try:
if arg_version == 6:
packet = twnet_parser.packet.parse6(udp_payload)
else:
packet = twnet_parser.packet.parse7(udp_payload)
except:
continue
names = [msg.message_name for msg in packet.messages]
print(f"[TW{arg_version}] {', '.join(names)}")
def usage():
print(f'usage: {sys.argv[0]} <pcap file> [-6|-7]')
print('options:')
print(' -6 try to parse as teeworlds 0.6 packets')
print(' -7 try to parse as teeworlds 0.7 packets (default)')
if len(sys.argv) < 2:
usage()
exit(1)
for arg in sys.argv[1:]:
if arg == '-6':
arg_version = 6
elif arg == '-7':
arg_version = 7
elif arg_pcap is None:
arg_pcap = arg
if arg_pcap is None:
usage()
exit(1)
with open(arg_pcap, 'rb') as f:
pcap = dpkt.pcap.Reader(f)
print_tw_packets(pcap)

View file

@ -1,28 +0,0 @@
#!/usr/bin/env python3
import sys
import dpkt
import twnet_parser.packet
def print_tw_packets(pcap):
for _ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
if not isinstance(ip.data, dpkt.udp.UDP):
continue
udp_payload = ip.data.data
try:
packet = twnet_parser.packet.parse7(udp_payload)
except:
continue
names = [msg.message_name for msg in packet.messages]
print(', '.join(names))
if len(sys.argv) < 2:
print(f'usage: {sys.argv[0]} <pcap file>')
exit(1)
with open(sys.argv[1], 'rb') as f:
pcap = dpkt.pcap.Reader(f)
print_tw_packets(pcap)