From 0d296d83d0bdfe23cff10f1a2d71886553ce9a81 Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Fri, 23 Feb 2024 12:48:12 +0800 Subject: [PATCH] feat: add 0.6 support to the pcap printer example --- README.md | 7 ++- .../print_pcap_files/07_dm1.pcap | Bin .../print_pcap_files/README.md | 0 .../print_pcap_files/print_pcap_files.py | 52 ++++++++++++++++++ .../07/print_pcap_files/print_pcap_files.py | 28 ---------- 5 files changed, 58 insertions(+), 29 deletions(-) rename examples/{07 => 06_and_07}/print_pcap_files/07_dm1.pcap (100%) rename examples/{07 => 06_and_07}/print_pcap_files/README.md (100%) create mode 100755 examples/06_and_07/print_pcap_files/print_pcap_files.py delete mode 100755 examples/07/print_pcap_files/print_pcap_files.py diff --git a/README.md b/README.md index 0dcb516..d511d0b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/examples/07/print_pcap_files/07_dm1.pcap b/examples/06_and_07/print_pcap_files/07_dm1.pcap similarity index 100% rename from examples/07/print_pcap_files/07_dm1.pcap rename to examples/06_and_07/print_pcap_files/07_dm1.pcap diff --git a/examples/07/print_pcap_files/README.md b/examples/06_and_07/print_pcap_files/README.md similarity index 100% rename from examples/07/print_pcap_files/README.md rename to examples/06_and_07/print_pcap_files/README.md diff --git a/examples/06_and_07/print_pcap_files/print_pcap_files.py b/examples/06_and_07/print_pcap_files/print_pcap_files.py new file mode 100755 index 0000000..545dab4 --- /dev/null +++ b/examples/06_and_07/print_pcap_files/print_pcap_files.py @@ -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]} [-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) diff --git a/examples/07/print_pcap_files/print_pcap_files.py b/examples/07/print_pcap_files/print_pcap_files.py deleted file mode 100755 index 32d54b0..0000000 --- a/examples/07/print_pcap_files/print_pcap_files.py +++ /dev/null @@ -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]} ') - exit(1) - -with open(sys.argv[1], 'rb') as f: - pcap = dpkt.pcap.Reader(f) - print_tw_packets(pcap)