#P10344. PCAP Reordering and Filtering
PCAP Reordering and Filtering
PCAP Reordering and Filtering
All network traffic in the problems is stored in the PCAP file format (see the "Study Manual" for details). In this problem, you are required to perform basic reading, parsing, and writing of PCAP files.
You will be given a PCAP file in a simplified text format. The file begins with a global header line followed by an integer N denoting the number of packets. Each packet is described in two consecutive lines. The first line of a packet contains four space‐separated fields:
ts_sec
: the timestamp seconds (integer)ts_usec
: the timestamp microseconds (integer)caplen
: the captured length (integer)orig_len
: the original full Ethernet frame length (integer)
The second line contains the packet data represented as a hexadecimal string (you do not need to process its content). Important: You should retain the entire packet header when processing (including the timestamp fields).
Your task is to:
- Filter out any packet whose original Ethernet frame length is greater than \(1000\) (i.e., only keep packets with \(orig\_len \leq 1000\)).
- Sort the remaining packets in ascending order by their timestamps. The timestamps are compared first by
ts_sec
and then byts_usec
. It is guaranteed that after processing, no two packets share the same timestamp. - Assemble the result into a valid PCAP file. This means you should output the original global header, followed by the number of valid packets, and then each valid packet (both its header and its data) in the sorted order.
You can verify the correctness of your output by checking it with tools such as Wireshark.
inputFormat
The input begins with a global header line (a string) of the PCAP file. The second line contains an integer N indicating the number of packets. This is followed by N packets, each described with two lines:
- The first line contains four space-separated integers:
ts_sec ts_usec caplen orig_len
. - The second line is a hexadecimal string representing the packet data.
Note: Only the orig_len
(the complete Ethernet frame length, not including the PCAP packet header) is used for filtering.
outputFormat
The output must be a valid PCAP file in the same simplified text format as the input:
- The first line is the global header (copied exactly from input).
- The second line is an integer representing the number of valid packets after filtering.
- Each valid packet is then printed in two lines (header and data), in ascending order by timestamp.
The output file should be openable in Wireshark or similar tools (assuming conversion to the real binary format), so make sure you preserve the header information without modifications (other than reordering and filtering the packets).
sample
PCAP_GLOBAL_HEADER
3
10 500000 60 60
001122334455
5 100000 100 1500
112233445566
10 400000 60 60
aabbccddeeff
PCAP_GLOBAL_HEADER
2
10 400000 60 60
aabbccddeeff
10 500000 60 60
001122334455
</p>