#!/usr/bin/env python3
"""
Generate a PCAP with a single TCP flow containing 100,000 packets.
Usage: python3 gen_flow.py [output.pcap]
"""

import sys
from scapy.all import IP, TCP, Ether, wrpcap
from scapy.utils import PcapWriter

OUTPUT = sys.argv[1] if len(sys.argv) > 1 else "flow_100K.pcap"
NUM_PACKETS = 50_000 

# Flow endpoints
SRC_IP = "10.0.0.1"
DST_IP = "10.0.0.2"
SRC_PORT = 12345
DST_PORT = 80
SRC_MAC = "02:00:00:00:00:01"
DST_MAC = "02:00:00:00:00:02"

seq_src = 1000
seq_dst = 5000

print(f"Generating {NUM_PACKETS * 2:,} packets ({NUM_PACKETS:,} data + {NUM_PACKETS:,} ACK) for a single TCP flow...")

with PcapWriter(OUTPUT, append=False, sync=True) as pkt_writer:
    for i in range(NUM_PACKETS):
        pkt = (
            Ether(src=SRC_MAC, dst=DST_MAC)
            / IP(src=SRC_IP, dst=DST_IP)
            / TCP(sport=SRC_PORT, dport=DST_PORT, flags="PA", seq=seq_src, ack=seq_dst)
            / f"GET /data/{i}.bin HTTP/1.1\r\nHost: 10.0.0.2\r\n\r\n"
        )
        pkt_writer.write(pkt)
        seq_src += len(pkt[TCP].payload)

        ack = (
            Ether(src=DST_MAC, dst=SRC_MAC)
            / IP(src=DST_IP, dst=SRC_IP)
            / TCP(sport=DST_PORT, dport=SRC_PORT, flags="A", seq=seq_dst, ack=seq_src)
        )
        pkt_writer.write(ack)

        if (i + 1) % 10_000 == 0:
            print(f"  {(i + 1) * 2:,} / {NUM_PACKETS * 2:,} packets written...")

print(f"Done. Written to {OUTPUT}")
